2023-03-15 16:00:52 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*/
|
|
|
|
|
2021-06-03 20:30:26 +00:00
|
|
|
import { module, test } from 'qunit';
|
2023-03-22 18:19:11 +00:00
|
|
|
import sinon from 'sinon';
|
2021-06-03 20:30:26 +00:00
|
|
|
import { setupRenderingTest } from 'ember-qunit';
|
2023-02-14 17:00:24 +00:00
|
|
|
import { render, click } from '@ember/test-helpers';
|
2021-06-03 20:30:26 +00:00
|
|
|
import { hbs } from 'ember-cli-htmlbars';
|
|
|
|
import subDays from 'date-fns/subDays';
|
|
|
|
import addDays from 'date-fns/addDays';
|
|
|
|
import formatRFC3339 from 'date-fns/formatRFC3339';
|
2023-03-22 18:19:11 +00:00
|
|
|
import timestamp from 'core/utils/timestamp';
|
2023-02-14 17:00:24 +00:00
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
module('Integration | Component | license-banners', function (hooks) {
|
2021-06-03 20:30:26 +00:00
|
|
|
setupRenderingTest(hooks);
|
|
|
|
|
2023-03-22 18:19:11 +00:00
|
|
|
hooks.before(function () {
|
|
|
|
sinon.stub(timestamp, 'now').callsFake(() => new Date('2018-04-03T14:15:30'));
|
|
|
|
});
|
2023-02-14 17:00:24 +00:00
|
|
|
hooks.beforeEach(function () {
|
2023-03-22 18:19:11 +00:00
|
|
|
const mockNow = timestamp.now();
|
|
|
|
this.now = mockNow;
|
|
|
|
this.yesterday = subDays(mockNow, 1);
|
|
|
|
this.nextMonth = addDays(mockNow, 30);
|
|
|
|
this.outside30 = addDays(mockNow, 32);
|
2023-06-09 18:03:58 +00:00
|
|
|
this.tomorrow = addDays(mockNow, 1);
|
2023-02-14 17:00:24 +00:00
|
|
|
this.version = this.owner.lookup('service:version');
|
|
|
|
this.version.version = '1.13.1+ent';
|
|
|
|
});
|
2023-03-22 18:19:11 +00:00
|
|
|
hooks.after(function () {
|
|
|
|
timestamp.now.restore();
|
|
|
|
});
|
2023-02-14 17:00:24 +00:00
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('it does not render if no expiry', async function (assert) {
|
2023-02-14 17:00:24 +00:00
|
|
|
assert.expect(1);
|
2021-06-03 20:30:26 +00:00
|
|
|
await render(hbs`<LicenseBanners />`);
|
|
|
|
assert.dom('[data-test-license-banner]').doesNotExist('License banner does not render');
|
|
|
|
});
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('it renders an error if expiry is before now', async function (assert) {
|
2023-02-14 17:00:24 +00:00
|
|
|
assert.expect(2);
|
2023-03-22 18:19:11 +00:00
|
|
|
this.set('expiry', formatRFC3339(this.yesterday));
|
2022-10-18 15:46:02 +00:00
|
|
|
await render(hbs`<LicenseBanners @expiry={{this.expiry}} />`);
|
2021-06-03 20:30:26 +00:00
|
|
|
assert.dom('[data-test-license-banner-expired]').exists('Expired license banner renders');
|
|
|
|
assert.dom('.message-title').hasText('License expired', 'Shows correct title on alert');
|
|
|
|
});
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('it renders a warning if expiry is within 30 days', async function (assert) {
|
2023-02-14 17:00:24 +00:00
|
|
|
assert.expect(2);
|
2023-03-22 18:19:11 +00:00
|
|
|
this.set('expiry', formatRFC3339(this.nextMonth));
|
2022-10-18 15:46:02 +00:00
|
|
|
await render(hbs`<LicenseBanners @expiry={{this.expiry}} />`);
|
2021-06-03 20:30:26 +00:00
|
|
|
assert.dom('[data-test-license-banner-warning]').exists('Warning license banner renders');
|
|
|
|
assert.dom('.message-title').hasText('Vault license expiring', 'Shows correct title on alert');
|
|
|
|
});
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('it does not render a banner if expiry is outside 30 days', async function (assert) {
|
2023-02-14 17:00:24 +00:00
|
|
|
assert.expect(1);
|
2023-03-22 18:19:11 +00:00
|
|
|
this.set('expiry', formatRFC3339(this.outside30));
|
2022-10-18 15:46:02 +00:00
|
|
|
await render(hbs`<LicenseBanners @expiry={{this.expiry}} />`);
|
2021-06-03 20:30:26 +00:00
|
|
|
assert.dom('[data-test-license-banner]').doesNotExist('License banner does not render');
|
|
|
|
});
|
2023-02-14 17:00:24 +00:00
|
|
|
|
|
|
|
test('it does not render the expired banner if it has been dismissed', async function (assert) {
|
|
|
|
assert.expect(3);
|
2023-03-22 18:19:11 +00:00
|
|
|
this.set('expiry', formatRFC3339(this.yesterday));
|
2023-06-09 18:03:58 +00:00
|
|
|
const key = `dismiss-license-banner-${this.version.version}-${this.expiry}`;
|
2023-02-14 17:00:24 +00:00
|
|
|
await render(hbs`<LicenseBanners @expiry={{this.expiry}} />`);
|
|
|
|
await click('[data-test-dismiss-expired]');
|
|
|
|
assert.dom('[data-test-license-banner-expired]').doesNotExist('Expired license banner does not render');
|
|
|
|
|
|
|
|
await render(hbs`<LicenseBanners @expiry={{this.expiry}} />`);
|
2023-06-09 18:03:58 +00:00
|
|
|
const localStorageResult = JSON.parse(localStorage.getItem(key));
|
2023-02-14 17:00:24 +00:00
|
|
|
assert.strictEqual(localStorageResult, 'expired');
|
|
|
|
assert
|
|
|
|
.dom('[data-test-license-banner-expired]')
|
|
|
|
.doesNotExist('The expired banner still does not render after a re-render.');
|
2023-06-09 18:03:58 +00:00
|
|
|
localStorage.removeItem(key);
|
2023-02-14 17:00:24 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('it does not render the warning banner if it has been dismissed', async function (assert) {
|
|
|
|
assert.expect(3);
|
2023-03-22 18:19:11 +00:00
|
|
|
this.set('expiry', formatRFC3339(this.nextMonth));
|
2023-06-09 18:03:58 +00:00
|
|
|
const key = `dismiss-license-banner-${this.version.version}-${this.expiry}`;
|
2023-02-14 17:00:24 +00:00
|
|
|
await render(hbs`<LicenseBanners @expiry={{this.expiry}} />`);
|
|
|
|
await click('[data-test-dismiss-warning]');
|
|
|
|
assert.dom('[data-test-license-banner-warning]').doesNotExist('Warning license banner does not render');
|
|
|
|
|
|
|
|
await render(hbs`<LicenseBanners @expiry={{this.expiry}} />`);
|
2023-06-09 18:03:58 +00:00
|
|
|
const localStorageResult = JSON.parse(localStorage.getItem(key));
|
2023-02-14 17:00:24 +00:00
|
|
|
assert.strictEqual(localStorageResult, 'warning');
|
|
|
|
assert
|
|
|
|
.dom('[data-test-license-banner-warning]')
|
|
|
|
.doesNotExist('The warning banner still does not render after a re-render.');
|
2023-06-09 18:03:58 +00:00
|
|
|
localStorage.removeItem(key);
|
2023-02-14 17:00:24 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('it renders a banner if the vault license has changed', async function (assert) {
|
|
|
|
assert.expect(3);
|
|
|
|
this.version.version = '1.12.1+ent';
|
2023-03-22 18:19:11 +00:00
|
|
|
this.set('expiry', formatRFC3339(this.nextMonth));
|
2023-06-09 18:03:58 +00:00
|
|
|
const keyOldVersion = `dismiss-license-banner-${this.version.version}-${this.expiry}`;
|
2023-02-14 17:00:24 +00:00
|
|
|
await render(hbs`<LicenseBanners @expiry={{this.expiry}} />`);
|
|
|
|
await click('[data-test-dismiss-warning]');
|
|
|
|
this.version.version = '1.13.1+ent';
|
2023-06-09 18:03:58 +00:00
|
|
|
const keyNewVersion = `dismiss-license-banner-${this.version.version}-${this.expiry}`;
|
2023-02-14 17:00:24 +00:00
|
|
|
await render(hbs`<LicenseBanners @expiry={{this.expiry}} />`);
|
|
|
|
assert
|
|
|
|
.dom('[data-test-license-banner-warning]')
|
|
|
|
.exists('The warning banner shows even though we have dismissed it earlier.');
|
|
|
|
|
|
|
|
await click('[data-test-dismiss-warning]');
|
2023-06-09 18:03:58 +00:00
|
|
|
const localStorageResultNewVersion = JSON.parse(localStorage.getItem(keyNewVersion));
|
|
|
|
const localStorageResultOldVersion = JSON.parse(localStorage.getItem(keyOldVersion));
|
|
|
|
// Check that localStorage was cleaned and no longer contains the old version storage key.
|
|
|
|
assert.strictEqual(localStorageResultOldVersion, null, 'local storage was cleared for the old version');
|
|
|
|
assert.strictEqual(
|
|
|
|
localStorageResultNewVersion,
|
|
|
|
'warning',
|
|
|
|
'local storage holds the new version with a warning'
|
2023-02-14 17:00:24 +00:00
|
|
|
);
|
2023-06-09 18:03:58 +00:00
|
|
|
// If debugging this test remember to clear localStorage if the test was not run to completion.
|
|
|
|
localStorage.removeItem(keyNewVersion);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('it renders a banner if the vault expiry has changed', async function (assert) {
|
|
|
|
assert.expect(3);
|
|
|
|
this.set('expiry', formatRFC3339(this.tomorrow));
|
|
|
|
const keyOldExpiry = `dismiss-license-banner-${this.version.version}-${this.expiry}`;
|
|
|
|
await render(hbs`<LicenseBanners @expiry={{this.expiry}} />`);
|
|
|
|
await click('[data-test-dismiss-warning]');
|
|
|
|
this.set('expiry', formatRFC3339(this.nextMonth));
|
|
|
|
const keyNewExpiry = `dismiss-license-banner-${this.version.version}-${this.expiry}`;
|
|
|
|
await render(hbs`<LicenseBanners @expiry={{this.expiry}} />`);
|
|
|
|
assert
|
|
|
|
.dom('[data-test-license-banner-warning]')
|
|
|
|
.exists('The warning banner shows even though we have dismissed it earlier.');
|
|
|
|
|
|
|
|
await click('[data-test-dismiss-warning]');
|
|
|
|
const localStorageResultNewExpiry = JSON.parse(localStorage.getItem(keyNewExpiry));
|
|
|
|
const localStorageResultOldExpiry = JSON.parse(localStorage.getItem(keyOldExpiry));
|
2023-02-14 17:00:24 +00:00
|
|
|
// Check that localStorage was cleaned and no longer contains the old version storage key.
|
2023-06-09 18:03:58 +00:00
|
|
|
assert.strictEqual(localStorageResultOldExpiry, null, 'local storage was cleared for the old expiry');
|
|
|
|
assert.strictEqual(
|
|
|
|
localStorageResultNewExpiry,
|
|
|
|
'warning',
|
|
|
|
'local storage holds the new expiry with a warning'
|
|
|
|
);
|
2023-02-14 17:00:24 +00:00
|
|
|
// If debugging this test remember to clear localStorage if the test was not run to completion.
|
2023-06-09 18:03:58 +00:00
|
|
|
localStorage.removeItem(keyNewExpiry);
|
2023-02-14 17:00:24 +00:00
|
|
|
});
|
2021-06-03 20:30:26 +00:00
|
|
|
});
|