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() {
super(...arguments);
// do not dismiss any banners if the user has updated their version
const dismissedBanner = localStorage.getItem(`dismiss-license-banner-${this.currentVersion}`); // returns either warning or expired
this.updateDismissType(dismissedBanner);
// reset and show a previously dismissed license banner if:
// the version has been updated or the license has been updated (indicated by a change in the expiry date).
const bannerType = localStorage.getItem(this.dismissedBannerKey); // returns either warning or expired
this.updateDismissType(bannerType);
}
get currentVersion() {
return this.version.version;
}
get dismissedBannerKey() {
return `dismiss-license-banner-${this.currentVersion}-${this.args.expiry}`;
}
get licenseExpired() {
if (!this.args.expiry) return false;
return isAfter(timestamp.now(), new Date(this.args.expiry));
@ -54,9 +60,9 @@ export default class LicenseBanners extends Component {
@action
dismissBanner(dismissAction) {
// 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
localStorage.setItem(`dismiss-license-banner-${this.currentVersion}`, dismissAction);
localStorage.setItem(this.dismissedBannerKey, dismissAction);
this.updateDismissType(dismissAction);
}

View File

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

View File

@ -1,9 +1,11 @@
<Sidebar::Nav::Cluster />
<LicenseBanners
@expiry={{this.activeCluster.licenseExpiry}}
@autoloaded={{eq this.activeCluster.licenseState "autoloaded"}}
/>
{{! Only show license banners for Enterprise }}
{{#if this.activeCluster.version.isEnterprise}}
<LicenseBanners
@expiry={{this.activeCluster.licenseExpiry}}
@autoloaded={{eq this.activeCluster.licenseState "autoloaded"}}
/>
{{/if}}
<div class="global-flash">
{{#each this.flashMessages.queue as |flash|}}
<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.nextMonth = addDays(mockNow, 30);
this.outside30 = addDays(mockNow, 32);
this.tomorrow = addDays(mockNow, 1);
this.version = this.owner.lookup('service:version');
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) {
assert.expect(3);
this.set('expiry', formatRFC3339(this.yesterday));
const key = `dismiss-license-banner-${this.version.version}-${this.expiry}`;
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}} />`);
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
.dom('[data-test-license-banner-expired]')
.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) {
assert.expect(3);
this.set('expiry', formatRFC3339(this.nextMonth));
const key = `dismiss-license-banner-${this.version.version}-${this.expiry}`;
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}} />`);
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
.dom('[data-test-license-banner-warning]')
.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) {
assert.expect(3);
this.version.version = '1.12.1+ent';
this.set('expiry', formatRFC3339(this.nextMonth));
const keyOldVersion = `dismiss-license-banner-${this.version.version}-${this.expiry}`;
await render(hbs`<LicenseBanners @expiry={{this.expiry}} />`);
await click('[data-test-dismiss-warning]');
this.version.version = '1.13.1+ent';
const keyNewVersion = `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 localStorageResultNewVersion = JSON.parse(
localStorage.getItem(`dismiss-license-banner-1.13.1+ent`)
);
const localStorageResultOldVersion = JSON.parse(
localStorage.getItem(`dismiss-license-banner-1.12.1+ent`)
);
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);
assert.strictEqual(localStorageResultNewVersion, 'warning');
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'
);
// 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) {
assert.expect(1);
assert.strictEqual(
LocalStorage.cleanUpStorage('something', 'something-key'),
LocalStorage.cleanupStorage('something', 'something-key'),
undefined,
'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('string-key', 'string-key-value');
const storageLengthBefore = window.localStorage.length;
LocalStorage.cleanUpStorage('string', 'string-key');
LocalStorage.cleanupStorage('string', 'string-key');
const storageLengthAfter = window.localStorage.length;
assert.strictEqual(
storageLengthBefore - storageLengthAfter,