/*
 This file is part of GNU Taler
 (C) 2021 Taler Systems S.A.

 GNU Taler is free software; you can redistribute it and/or modify it under the
 terms of the GNU General Public License as published by the Free Software
 Foundation; either version 3, or (at your option) any later version.

 GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with
 GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */

/**
 * Imports.
 */
import { createSimpleTestkudosEnvironmentV3 } from "harness/environments.js";
import { By } from "selenium-webdriver";
import {
  GlobalTestState,
  MERCHANT_DEFAULT_AUTH,
} from "../harness/harness.js";

/**
 * Do basic checks on instance management and authentication.
 */
export async function runWebMerchantLoginTest(t: GlobalTestState) {
  // Set up test environment

  const { merchant, createBrowser } =
    await createSimpleTestkudosEnvironmentV3(t);

  const browser = await createBrowser({});

  await browser.get(merchant.makeInstanceBaseUrl());

  const title = await browser.getTitle();

  t.assertDeepEqual("GNU Taler Merchant Backoffice", title);

  await browser.manage().setTimeouts({ implicit: 2000 });

  const form = await browser.findElement(By.css("form"));
  const username = await form.findElement(By.css("input[name=username]"));
  const password = await form.findElement(By.css("input[name=token]"));
  const submit = await form.findElement(By.css("button[type=submit]"));
  await browser.sleep(100);

  const inferedUsername = await username.getAttribute("value")
  t.assertTrue(inferedUsername === "admin");
  await password.sendKeys(MERCHANT_DEFAULT_AUTH.password);
  await submit.click();

  await browser.sleep(100);

  const nousername = await browser.findElement(By.css("aside"));
  t.assertTrue(nousername !== undefined);

  await browser.quit();
}

runWebMerchantLoginTest.suites = ["web", "merchant"];
