import { currentRouteName, settled, click } from '@ember/test-helpers';
import { module, test } from 'qunit';
import { setupApplicationTest } from 'ember-qunit';
import page from 'vault/tests/pages/settings/configure-secret-backends/pki/section-cert';
import { SELECTORS } from 'vault/tests/helpers/pki';
import authPage from 'vault/tests/pages/auth';
import enablePage from 'vault/tests/pages/settings/mount-secret-backend';

module('Acceptance | settings/configure/secrets/pki/cert', function (hooks) {
  setupApplicationTest(hooks);

  hooks.beforeEach(function () {
    return authPage.login();
  });

  //prettier-ignore
  const PEM_BUNDLE = `-----BEGIN CERTIFICATE-----
MIIDGjCCAgKgAwIBAgIUFvnhb2nQ8+KNS3SzjlfYDMHGIRgwDQYJKoZIhvcNAQEL
BQAwDTELMAkGA1UEAxMCZmEwHhcNMTgwMTEwMTg1NDI5WhcNMTgwMjExMTg1NDU5
WjANMQswCQYDVQQDEwJmYTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB
AN2VtBn6EMlA4aYre/xoKHxlgNDxJnfSQWfs6yF/K201qPnt4QF9AXChatbmcKVn
OaURq+XEJrGVgF/u2lSos3NRZdhWVe8o3/sOetsGxcrd0gXAieOSmkqJjp27bYdl
uY3WsxhyiPvdfS6xz39OehsK/YCB6qCzwB4eEfSKqbkvfDL9sLlAiOlaoHC9pczf
6/FANKp35UDwInSwmq5vxGbnWk9zMkh5Jq6hjOWHZnVc2J8J49PYvkIM8uiHDgOE
w71T2xM5plz6crmZnxPCOcTKIdF7NTEP2lUfiqc9lONV9X1Pi4UclLPHJf5bwTmn
JaWgbKeY+IlF61/mgxzhC7cCAwEAAaNyMHAwDgYDVR0PAQH/BAQDAgEGMA8GA1Ud
EwEB/wQFMAMBAf8wHQYDVR0OBBYEFLDtc6+HZN2lv60JSDAZq3+IHoq7MB8GA1Ud
IwQYMBaAFLDtc6+HZN2lv60JSDAZq3+IHoq7MA0GA1UdEQQGMASCAmZhMA0GCSqG
SIb3DQEBCwUAA4IBAQDVt6OddTV1MB0UvF5v4zL1bEB9bgXvWx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Fj7VwECgYEA5P6aIcGfCZayEJtHKlTCA2/KBkGTOP/0iNKWhntBQT/GK+bjmr+D
GO1zR8ZFEIRdlUA5MjC9wU2AQikgFQzzmtz604Wt34fDN2NFrxq8sWN7Hjr65Fjf
ivJ6faT5r5gcNEq3EM/GLF9oJH8M+B5ccFe9iXH8AbmZHOO0FZtYxIcCgYEA97dm
Kj1qyuKlINXKt4KXdYMuIT+Z3G1B92wNN9TY/eJZgCJ7zlNcinUF/OFbiGgsk4t+
P0yVMs8BENQML0TH4Gebf4HfnDFno4J1M9HDt6HSMhsLKyvFYjFvb8hF4SPrY1pF
wW3lM3zMMzAVi8044vRrTvxfxL8QJX+1Hesye1ECgYAT5/H8Fzm8+qWV/fmMu3t2
EwSr0I18uftG3Y+KNzKv+lw+ur50WEuMIjAQQDMGwYrlC4UtUMFeCV+p4KtSSSLw
Bl+jfY5kzQdyTCXll9xpSy2LrjLbIMKl8Hgnbezqj7176jbJtlYSy2RhL84vz2vX
tDjcttTiTYD62uxvqGZqBwKBgFQ3tPM9aDZL8coFBWN4cZfRHnjNT7kCKEA/KwtF
QPSn5LfMgXz3GGo2OO/tihoJGMac0TIiDkN03y7ieLYFU1L2xoYGGIjYvxx2+PPC
KCEhUf4Y9aYavoOQvQsq8p8FgDyJ71dAzoC/uAjbGygpgGKgqG71HHYeYxXsoh3m
3YXRAoGAE7MBnVJWiIN5s63gGz9f9V6k1dPLfcPE1I0KMM/SDOIV0oLMsYQecTTB
ZzkXwRCdcJARkaKulTfjby7+oGpQydP8iZr+CNKFxwf838UbhhsXHnN6rc62qzYD
BXUV2Uwtxf+QCphnlht9muX2fsLIzDJea0JipWj1uf2H8OZsjE8=
-----END RSA PRIVATE KEY-----`;

  const mountAndNav = async (assert, prefix) => {
    const path = `${prefix}pki-${new Date().getTime()}`;
    await enablePage.enable('pki', path);
    await settled();
    await page.visit({ backend: path });
    await settled();
    return path;
  };

  test('cert config: generate', async function (assert) {
    assert.expect(10);
    await mountAndNav(assert);
    await settled();
    assert.strictEqual(currentRouteName(), 'vault.cluster.settings.configure-secret-backend.section');

    await page.form.generateCA();
    await settled();

    assert.dom(SELECTORS.certificate).exists('certificate is present and masked');
    assert.dom(SELECTORS.commonName).exists('displays common name');
    assert.dom(SELECTORS.issueDate).exists('displays issue date');
    assert.dom(SELECTORS.expiryDate).exists('displays expiration date');
    assert.dom(SELECTORS.issuingCa).exists('displays masked issuing CA');
    assert.dom(SELECTORS.serialNumber).exists('displays serial number');
    assert.dom(SELECTORS.csr).doesNotExist('does not display empty CSR');
    assert.dom(SELECTORS.caChain).doesNotExist('does not display empty CA chain');
    assert.dom(SELECTORS.privateKey).doesNotExist('does not display empty private key');
    await page.form.back();
    await page.form.generateCA();
    await settled();
  });

  test('cert config: upload', async function (assert) {
    assert.expect(2);
    await mountAndNav(assert);
    await settled();
    assert.strictEqual(page.form.downloadLinks.length, 0, 'there are no download links');

    await page.form.uploadCA(PEM_BUNDLE);
    await settled();
    assert.ok(
      page.flash.latestMessage.startsWith('The certificate for this backend has been updated'),
      'flash message displays properly'
    );
  });

  test('cert config: sign intermediate and set signed intermediate', async function (assert) {
    assert.expect(3);
    const rootPath = await mountAndNav(assert, 'root-');
    await page.form.generateCA();
    await settled();
    const intermediatePath = await mountAndNav(assert, 'intermediate-');
    await page.form.generateCA('Intermediate CA', 'intermediate');
    await settled();
    // cache csr
    await click('.masked-input-toggle');
    const csrVal = document.querySelector('.masked-value').innerText;
    await page.form.back();
    await settled();
    await page.visit({ backend: rootPath });
    await settled();
    await page.form.signIntermediate('Intermediate CA');
    await settled();
    await page.form.csrField(csrVal).submit();
    await settled();
    assert.dom(SELECTORS.caChain).exists('full CA chain is shown');
    assert.dom(SELECTORS.privateKey).doesNotExist('does not display empty private key');
    await click('.masked-input-toggle');
    const intermediateCert = document.querySelector('[data-test-masked-input]').innerText;
    await page.form.back();
    await settled();
    await page.visit({ backend: intermediatePath });
    await settled();
    await page.form.setSignedIntermediateBtn().signedIntermediate(intermediateCert);
    await settled();
    await page.form.submit();
    await settled();
    assert.dom('[data-test-go-replace-ca]').exists();
  });
});
