/**
 * Copyright (c) HashiCorp, Inc.
 * SPDX-License-Identifier: MPL-2.0
 */

import { module, test } from 'qunit';
import { create } from 'ember-cli-page-object';
import { v4 as uuidv4 } from 'uuid';
import { setupApplicationTest } from 'ember-qunit';
import { click, fillIn } from '@ember/test-helpers';
import authPage from 'vault/tests/pages/auth';
import enablePage from 'vault/tests/pages/settings/auth/enable';
import consoleClass from 'vault/tests/pages/components/console/ui-panel';
import { setupMirage } from 'ember-cli-mirage/test-support';

const consoleComponent = create(consoleClass);
const USER = 'end-user';
const PASSWORD = 'mypassword';
const POLICY_NAME = 'identity_policy';

const writePolicy = async function (path) {
  await enablePage.enable('userpass', path);
  const identityPolicy = `path "identity/*" {
    capabilities = ["create", "read", "update", "delete", "list"]
  }`;
  await consoleComponent.runCommands([
    `write sys/policies/acl/${POLICY_NAME} policy=${btoa(identityPolicy)}`,
  ]);
};

const writeUserWithPolicy = async function (path) {
  await consoleComponent.runCommands([
    `write auth/${path}/users/${USER} password=${PASSWORD} policies=${POLICY_NAME}`,
  ]);
};

const setupUser = async function (path) {
  await writePolicy(path);
  await writeUserWithPolicy(path);
  await click('[data-test-save-config="true"]');
};

module('Acceptance | mfa-setup', function (hooks) {
  setupApplicationTest(hooks);
  setupMirage(hooks);

  hooks.beforeEach(async function () {
    const path = `userpass-${uuidv4()}`;
    await authPage.login();
    await setupUser(path);
    await authPage.logout();
    await authPage.loginUsername(USER, PASSWORD, path);
    await click('[data-test-user-menu-trigger]');
    await click('[data-test-user-menu-item="mfa"]');
  });

  test('it should login through MFA and post to generate and be able to restart the setup', async function (assert) {
    assert.expect(5);
    // the network requests required in this test
    this.server.post('/identity/mfa/method/totp/generate', (scheme, req) => {
      const json = JSON.parse(req.requestBody);
      assert.strictEqual(json.method_id, '123', 'sends the UUID value');
      return {
        data: {
          barcode:
            'iVBORw0KGgoAAAANSUhEUgAAAMgAAADIEAAAAADYoy0BAAAGbUlEQVR4nOydUW7kOAxEk0Xuf+VZzABepAXTrKLUmMrivY8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otpauth://totp/Vault:26606dbe-d8ea-82ca-41b0-1250a4484079?algorithm=SHA1&digits=6&issuer=Vault&period=30&secret=FID3WRPRRADQDN3CGPVVOLKCXTZZPSML',
          lease_duration: 0,
        },
      };
    });
    this.server.post('/identity/mfa/method/totp/admin-destroy', (scheme, req) => {
      const json = JSON.parse(req.requestBody);
      assert.strictEqual(json.method_id, '123', 'sends the UUID value');
      // returns nothing
      return {};
    });
    await fillIn('[data-test-input="uuid"]', 123);
    await click('[data-test-verify]');
    assert.dom('[data-test-qrcode]').exists('the qrCode is shown.');
    assert.dom('[data-test-mfa-enabled-warning]').doesNotExist('warning does not show.');
    await click('[data-test-restart]');
    assert.dom('[data-test-step-one]').exists('back to step one.');
  });

  test('it should show a warning if you enter in the same UUID without restarting the setup', async function (assert) {
    assert.expect(2);
    // the network requests required in this test
    this.server.post('/identity/mfa/method/totp/generate', () => {
      return {
        data: null,
        warnings: ['Entity already has a secret for MFA method “”'],
      };
    });

    await fillIn('[data-test-input="uuid"]', 123);
    await click('[data-test-verify]');
    assert.dom('[data-test-qrcode]').doesNotExist('the qrCode is not shown.');
    assert.dom('[data-test-mfa-enabled-warning]').exists('the mfa-enabled warning shows.');
  });
});
