Skip to content

Commit

Permalink
License banner - check for new license on localStorage. (#20999)
Browse files Browse the repository at this point in the history
* fix things twice

* fix test and a whitespace that showed on my linter

* fix comment

* address pr comments

* change const name
  • Loading branch information
Monkeychip authored Jun 7, 2023
1 parent f079b7b commit 6fa423e
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 25 deletions.
16 changes: 11 additions & 5 deletions ui/app/components/license-banners.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion ui/app/lib/local-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
12 changes: 7 additions & 5 deletions ui/app/templates/vault/cluster.hbs
Original file line number Diff line number Diff line change
@@ -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|>
Expand Down
56 changes: 44 additions & 12 deletions ui/tests/integration/components/license-banners-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
});
Expand Down Expand Up @@ -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, '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(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(localStorageResultOldVersion, null);
assert.strictEqual(localStorageResultNewVersion, 'warning');
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(`dismiss-license-banner-1.13.1+ent`);
localStorage.removeItem(keyNewExpiry);
});
});
4 changes: 2 additions & 2 deletions ui/tests/unit/lib/local-storage-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
);
Expand All @@ -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,
Expand Down

0 comments on commit 6fa423e

Please sign in to comment.