backport of commit 6fa423e3f3e0bde47686fd4bfc6dd2b37031afa4 (#21046)

Co-authored-by: Angel Garbarino <Monkeychip@users.noreply.github.com>
This commit is contained in:
hc-github-team-secure-vault-core 2023-06-09 14:03:58 -04:00 committed by GitHub
parent d8c82657e4
commit 3a4286e713
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 66 additions and 26 deletions

View File

@ -31,15 +31,21 @@ export default class LicenseBanners extends Component {
constructor() { constructor() {
super(...arguments); super(...arguments);
// do not dismiss any banners if the user has updated their version // reset and show a previously dismissed license banner if:
const dismissedBanner = localStorage.getItem(`dismiss-license-banner-${this.currentVersion}`); // returns either warning or expired // the version has been updated or the license has been updated (indicated by a change in the expiry date).
this.updateDismissType(dismissedBanner); const bannerType = localStorage.getItem(this.dismissedBannerKey); // returns either warning or expired
this.updateDismissType(bannerType);
} }
get currentVersion() { get currentVersion() {
return this.version.version; return this.version.version;
} }
get dismissedBannerKey() {
return `dismiss-license-banner-${this.currentVersion}-${this.args.expiry}`;
}
get licenseExpired() { get licenseExpired() {
if (!this.args.expiry) return false; if (!this.args.expiry) return false;
return isAfter(timestamp.now(), new Date(this.args.expiry)); return isAfter(timestamp.now(), new Date(this.args.expiry));
@ -54,9 +60,9 @@ export default class LicenseBanners extends Component {
@action @action
dismissBanner(dismissAction) { dismissBanner(dismissAction) {
// if a client's version changed their old localStorage key will still exists. // if a client's version changed their old localStorage key will still exists.
localStorage.cleanUpStorage('dismiss-license-banner', `dismiss-license-banner-${this.currentVersion}`); localStorage.cleanupStorage('dismiss-license-banner', this.dismissedBannerKey);
// updates localStorage and then updates the template by calling updateDismissType // updates localStorage and then updates the template by calling updateDismissType
localStorage.setItem(`dismiss-license-banner-${this.currentVersion}`, dismissAction); localStorage.setItem(this.dismissedBannerKey, dismissAction);
this.updateDismissType(dismissAction); this.updateDismissType(dismissAction);
} }

View File

@ -21,7 +21,7 @@ export default {
return Object.keys(window.localStorage); return Object.keys(window.localStorage);
}, },
cleanUpStorage(string, keyToKeep) { cleanupStorage(string, keyToKeep) {
if (!string) return; if (!string) return;
const relevantKeys = this.keys().filter((str) => str.startsWith(string)); const relevantKeys = this.keys().filter((str) => str.startsWith(string));
relevantKeys?.forEach((key) => { relevantKeys?.forEach((key) => {

View File

@ -1,9 +1,11 @@
<Sidebar::Nav::Cluster /> <Sidebar::Nav::Cluster />
{{! Only show license banners for Enterprise }}
<LicenseBanners {{#if this.activeCluster.version.isEnterprise}}
@expiry={{this.activeCluster.licenseExpiry}} <LicenseBanners
@autoloaded={{eq this.activeCluster.licenseState "autoloaded"}} @expiry={{this.activeCluster.licenseExpiry}}
/> @autoloaded={{eq this.activeCluster.licenseState "autoloaded"}}
/>
{{/if}}
<div class="global-flash"> <div class="global-flash">
{{#each this.flashMessages.queue as |flash|}} {{#each this.flashMessages.queue as |flash|}}
<FlashMessage data-test-flash-message={{true}} @flash={{flash}} as |customComponent flash close|> <FlashMessage data-test-flash-message={{true}} @flash={{flash}} as |customComponent flash close|>

View File

@ -25,6 +25,7 @@ module('Integration | Component | license-banners', function (hooks) {
this.yesterday = subDays(mockNow, 1); this.yesterday = subDays(mockNow, 1);
this.nextMonth = addDays(mockNow, 30); this.nextMonth = addDays(mockNow, 30);
this.outside30 = addDays(mockNow, 32); this.outside30 = addDays(mockNow, 32);
this.tomorrow = addDays(mockNow, 1);
this.version = this.owner.lookup('service:version'); this.version = this.owner.lookup('service:version');
this.version.version = '1.13.1+ent'; this.version.version = '1.13.1+ent';
}); });
@ -64,58 +65,89 @@ module('Integration | Component | license-banners', function (hooks) {
test('it does not render the expired banner if it has been dismissed', async function (assert) { test('it does not render the expired banner if it has been dismissed', async function (assert) {
assert.expect(3); assert.expect(3);
this.set('expiry', formatRFC3339(this.yesterday)); this.set('expiry', formatRFC3339(this.yesterday));
const key = `dismiss-license-banner-${this.version.version}-${this.expiry}`;
await render(hbs`<LicenseBanners @expiry={{this.expiry}} />`); await render(hbs`<LicenseBanners @expiry={{this.expiry}} />`);
await click('[data-test-dismiss-expired]'); await click('[data-test-dismiss-expired]');
assert.dom('[data-test-license-banner-expired]').doesNotExist('Expired license banner does not render'); assert.dom('[data-test-license-banner-expired]').doesNotExist('Expired license banner does not render');
await render(hbs`<LicenseBanners @expiry={{this.expiry}} />`); await render(hbs`<LicenseBanners @expiry={{this.expiry}} />`);
const localStorageResult = JSON.parse(localStorage.getItem(`dismiss-license-banner-1.13.1+ent`)); const localStorageResult = JSON.parse(localStorage.getItem(key));
assert.strictEqual(localStorageResult, 'expired'); assert.strictEqual(localStorageResult, 'expired');
assert assert
.dom('[data-test-license-banner-expired]') .dom('[data-test-license-banner-expired]')
.doesNotExist('The expired banner still does not render after a re-render.'); .doesNotExist('The expired banner still does not render after a re-render.');
localStorage.removeItem(`dismiss-license-banner-1.13.1+ent`); localStorage.removeItem(key);
}); });
test('it does not render the warning banner if it has been dismissed', async function (assert) { test('it does not render the warning banner if it has been dismissed', async function (assert) {
assert.expect(3); assert.expect(3);
this.set('expiry', formatRFC3339(this.nextMonth)); this.set('expiry', formatRFC3339(this.nextMonth));
const key = `dismiss-license-banner-${this.version.version}-${this.expiry}`;
await render(hbs`<LicenseBanners @expiry={{this.expiry}} />`); await render(hbs`<LicenseBanners @expiry={{this.expiry}} />`);
await click('[data-test-dismiss-warning]'); await click('[data-test-dismiss-warning]');
assert.dom('[data-test-license-banner-warning]').doesNotExist('Warning license banner does not render'); assert.dom('[data-test-license-banner-warning]').doesNotExist('Warning license banner does not render');
await render(hbs`<LicenseBanners @expiry={{this.expiry}} />`); await render(hbs`<LicenseBanners @expiry={{this.expiry}} />`);
const localStorageResult = JSON.parse(localStorage.getItem(`dismiss-license-banner-1.13.1+ent`)); const localStorageResult = JSON.parse(localStorage.getItem(key));
assert.strictEqual(localStorageResult, 'warning'); assert.strictEqual(localStorageResult, 'warning');
assert assert
.dom('[data-test-license-banner-warning]') .dom('[data-test-license-banner-warning]')
.doesNotExist('The warning banner still does not render after a re-render.'); .doesNotExist('The warning banner still does not render after a re-render.');
localStorage.removeItem(`dismiss-license-banner-1.13.1+ent`); localStorage.removeItem(key);
}); });
test('it renders a banner if the vault license has changed', async function (assert) { test('it renders a banner if the vault license has changed', async function (assert) {
assert.expect(3); assert.expect(3);
this.version.version = '1.12.1+ent'; this.version.version = '1.12.1+ent';
this.set('expiry', formatRFC3339(this.nextMonth)); this.set('expiry', formatRFC3339(this.nextMonth));
const keyOldVersion = `dismiss-license-banner-${this.version.version}-${this.expiry}`;
await render(hbs`<LicenseBanners @expiry={{this.expiry}} />`); await render(hbs`<LicenseBanners @expiry={{this.expiry}} />`);
await click('[data-test-dismiss-warning]'); await click('[data-test-dismiss-warning]');
this.version.version = '1.13.1+ent'; this.version.version = '1.13.1+ent';
const keyNewVersion = `dismiss-license-banner-${this.version.version}-${this.expiry}`;
await render(hbs`<LicenseBanners @expiry={{this.expiry}} />`); await render(hbs`<LicenseBanners @expiry={{this.expiry}} />`);
assert assert
.dom('[data-test-license-banner-warning]') .dom('[data-test-license-banner-warning]')
.exists('The warning banner shows even though we have dismissed it earlier.'); .exists('The warning banner shows even though we have dismissed it earlier.');
await click('[data-test-dismiss-warning]'); await click('[data-test-dismiss-warning]');
const localStorageResultNewVersion = JSON.parse( const localStorageResultNewVersion = JSON.parse(localStorage.getItem(keyNewVersion));
localStorage.getItem(`dismiss-license-banner-1.13.1+ent`) const localStorageResultOldVersion = JSON.parse(localStorage.getItem(keyOldVersion));
);
const localStorageResultOldVersion = JSON.parse(
localStorage.getItem(`dismiss-license-banner-1.12.1+ent`)
);
// Check that localStorage was cleaned and no longer contains the old version storage key. // Check that localStorage was cleaned and no longer contains the old version storage key.
assert.strictEqual(localStorageResultOldVersion, null); assert.strictEqual(localStorageResultOldVersion, null, 'local storage was cleared for the old version');
assert.strictEqual(localStorageResultNewVersion, 'warning'); assert.strictEqual(
localStorageResultNewVersion,
'warning',
'local storage holds the new version with a warning'
);
// If debugging this test remember to clear localStorage if the test was not run to completion. // If debugging this test remember to clear localStorage if the test was not run to completion.
localStorage.removeItem(`dismiss-license-banner-1.13.1+ent`); 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));
// Check that localStorage was cleaned and no longer contains the old version storage key.
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'
);
// If debugging this test remember to clear localStorage if the test was not run to completion.
localStorage.removeItem(keyNewExpiry);
}); });
}); });

View File

@ -17,7 +17,7 @@ module('Unit | lib | local-storage', function (hooks) {
test('it does not error if nothing is in local storage', async function (assert) { test('it does not error if nothing is in local storage', async function (assert) {
assert.expect(1); assert.expect(1);
assert.strictEqual( assert.strictEqual(
LocalStorage.cleanUpStorage('something', 'something-key'), LocalStorage.cleanupStorage('something', 'something-key'),
undefined, undefined,
'returns undefined and does not throw an error when method is called and nothing exist in localStorage.' 'returns undefined and does not throw an error when method is called and nothing exist in localStorage.'
); );
@ -29,7 +29,7 @@ module('Unit | lib | local-storage', function (hooks) {
LocalStorage.setItem('beep-boop-bop-key', 'beep-boop-bop-value'); LocalStorage.setItem('beep-boop-bop-key', 'beep-boop-bop-value');
LocalStorage.setItem('string-key', 'string-key-value'); LocalStorage.setItem('string-key', 'string-key-value');
const storageLengthBefore = window.localStorage.length; const storageLengthBefore = window.localStorage.length;
LocalStorage.cleanUpStorage('string', 'string-key'); LocalStorage.cleanupStorage('string', 'string-key');
const storageLengthAfter = window.localStorage.length; const storageLengthAfter = window.localStorage.length;
assert.strictEqual( assert.strictEqual(
storageLengthBefore - storageLengthAfter, storageLengthBefore - storageLengthAfter,