Skip to content

Commit

Permalink
[Backport 1.3] Switch to new tenant after loading a copied long URL (#…
Browse files Browse the repository at this point in the history
…1450) (#1517)

* Switch to new tenant after loading a copied long URL (#1450)

Signed-off-by: leanneeliatra <leanne.laceybyrne@eliatra.com>
Signed-off-by: Darshit Chanpura <dchanp@amazon.com>
Signed-off-by: Darshit Chanpura <35282393+DarshitChanpura@users.noreply.github.com>
Co-authored-by: leanneeliatra <131779422+leanneeliatra@users.noreply.github.com>
  • Loading branch information
DarshitChanpura and leanneeliatra authored Jul 19, 2023
1 parent b914ecf commit 390fd60
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 2 deletions.
23 changes: 22 additions & 1 deletion public/apps/account/account-nav-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export function AccountNavButton(props: {
}}
handleSwitchAndClose={() => {
setModal(null);
window.location.reload();
reloadAfterTenantSwitch();
}}
/>
),
Expand Down Expand Up @@ -164,3 +164,24 @@ export function AccountNavButton(props: {
</EuiHeaderSectionItemButton>
);
}

export function reloadAfterTenantSwitch(): void {
// the below portion is to clear URLs starting with 'lastUrl'
// when switching tenants, the last URLs will be from the old tenancy therefore we need to remove these from sessionStorage.
const lastUrls = [];
const { sessionStorage } = window;

for (let i = 0; i < sessionStorage.length; i++) {
const key = sessionStorage.key(i);
if (key?.startsWith('lastUrl')) {
lastUrls.push(key);
}
}
for (let i = 0; i < lastUrls.length; i++) {
sessionStorage.removeItem(lastUrls[i]);
}

// rather than just reload when we switch tenants, we set the URL to the pathname. i.e. the portion like: '/app/dashboards'
// therefore, the copied URL will now allow tenancy changes.
window.location.href = window.location.pathname;
}
59 changes: 58 additions & 1 deletion public/apps/account/test/account-nav-button.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import { shallow } from 'enzyme';
import React from 'react';
import { AccountNavButton } from '../account-nav-button';
import { AccountNavButton, reloadAfterTenantSwitch } from '../account-nav-button';
import { getShouldShowTenantPopup, setShouldShowTenantPopup } from '../../../utils/storage-utils';

jest.mock('../../../utils/storage-utils', () => ({
Expand Down Expand Up @@ -102,3 +102,60 @@ describe('Account navigation button', () => {
expect(setState).toBeCalledTimes(1);
});
});

describe('Reload window after tenant switch', () => {
const originalLocation = window.location;
const mockSetWindowHref = jest.fn();
let pathname: string = '';
beforeAll(() => {
pathname = '/app/myapp';
Object.defineProperty(window, 'location', {
value: {
get pathname() {
return pathname;
},
get href() {
return '/app/dashboards?security_tenant=admin_tenant';
},
set href(value: string) {
mockSetWindowHref(value);
},
},
});
});

afterAll(() => {
window.location = originalLocation;
});

it('should remove the tenant query parameter before reloading', () => {
pathname = '/app/pathname-only';
reloadAfterTenantSwitch();
expect(mockSetWindowHref).toHaveBeenCalledWith(pathname);
});
});

describe('Clear lastUrls after tenant switch', () => {
afterAll(() => {
jest.clearAllMocks();
});

it('should clear out keys with a lastUrl prefix', () => {
window.sessionStorage.setItem('lastUrl:dashboard', '/dashboard1');
window.sessionStorage.setItem('lastUrl:otherApp', '/otherApp');
window.sessionStorage.setItem('somethingElse:here', '/random');
const mockRemoveItem = jest.spyOn(Object.getPrototypeOf(window.sessionStorage), 'removeItem');
reloadAfterTenantSwitch();
expect(mockRemoveItem).toHaveBeenCalledWith('lastUrl:dashboard');
expect(mockRemoveItem).toHaveBeenCalledWith('lastUrl:otherApp');
expect(mockRemoveItem).toHaveBeenCalledTimes(2);
});

it('should not clear out keys without a lastUrl prefix', () => {
window.sessionStorage.setItem('somethingElse:here', '/random');
const mockRemoveItem = jest.spyOn(Object.getPrototypeOf(window.sessionStorage), 'removeItem');

reloadAfterTenantSwitch();
expect(mockRemoveItem).toHaveBeenCalledTimes(0);
});
});
73 changes: 73 additions & 0 deletions public/apps/account/test/switch-tenants.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright OpenSearch Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

import { reloadAfterTenantSwitch } from '../account-nav-button';

describe('Reload window after tenant switch', () => {
const originalLocation = window.location;
const mockSetWindowHref = jest.fn();
let pathname: string = '';
beforeAll(() => {
pathname = '/app/myapp';
Object.defineProperty(window, 'location', {
value: {
get pathname() {
return pathname;
},
get href() {
return '/app/dashboards?security_tenant=admin_tenant';
},
set href(value: string) {
mockSetWindowHref(value);
},
},
});
});

afterAll(() => {
window.location = originalLocation;
});

it('should remove the tenant query parameter before reloading', () => {
pathname = '/app/pathname-only';
reloadAfterTenantSwitch();
expect(mockSetWindowHref).toHaveBeenCalledWith(pathname);
});
});

describe('Clear lastUrls after tenant switch', () => {
afterAll(() => {
jest.clearAllMocks();
});

it('should clear out keys with a lastUrl prefix', () => {
window.sessionStorage.setItem('lastUrl:dashboard', '/dashboard1');
window.sessionStorage.setItem('lastUrl:otherApp', '/otherApp');
window.sessionStorage.setItem('somethingElse:here', '/random');
const mockRemoveItem = jest.spyOn(Object.getPrototypeOf(window.sessionStorage), 'removeItem');
reloadAfterTenantSwitch();
expect(mockRemoveItem).toHaveBeenCalledWith('lastUrl:dashboard');
expect(mockRemoveItem).toHaveBeenCalledWith('lastUrl:otherApp');
expect(mockRemoveItem).toHaveBeenCalledTimes(2);
});

it('should not clear out keys without a lastUrl prefix', () => {
window.sessionStorage.setItem('somethingElse:here', '/random');
const mockRemoveItem = jest.spyOn(Object.getPrototypeOf(window.sessionStorage), 'removeItem');

reloadAfterTenantSwitch();
expect(mockRemoveItem).toHaveBeenCalledTimes(0);
});
});

0 comments on commit 390fd60

Please sign in to comment.