Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport 2.11] Disable tenancy pop-ups when disabled or default tenant set #1765

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion public/apps/account/account-app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
} from '../../utils/storage-utils';
import { constructErrorMessageAndLog } from '../error-utils';
import { fetchCurrentAuthType } from '../../utils/logout-utils';
import { getDashboardsInfoSafe } from '../../utils/dashboards-info-utils';

function tenantSpecifiedInUrl() {
return (
Expand Down Expand Up @@ -71,7 +72,15 @@ export async function setupTopNavButton(coreStart: CoreStart, config: ClientConf

let shouldShowTenantPopup = true;

if (tenantSpecifiedInUrl() || getShouldShowTenantPopup() === false) {
const dashboardsInfo = await getDashboardsInfoSafe(coreStart.http);
const multitenancyEnabled = dashboardsInfo?.multitenancy_enabled && config.multitenancy.enabled;
const defaultTenantSet = dashboardsInfo?.default_tenant !== '';
if (
tenantSpecifiedInUrl() ||
getShouldShowTenantPopup() === false ||
!multitenancyEnabled ||
defaultTenantSet
) {
shouldShowTenantPopup = false;
} else {
// Switch to previous tenant based on localStorage, it may fail due to
Expand Down
2 changes: 1 addition & 1 deletion public/apps/account/account-nav-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export function AccountNavButton(props: {
}, [props.coreStart.http]);

// Check if the tenant modal should be shown on load
if (isMultiTenancyEnabled && getShouldShowTenantPopup() && props.config.multitenancy.enabled) {
if (getShouldShowTenantPopup()) {
setShouldShowTenantPopup(false);
showTenantSwitchPanel();
}
Expand Down
68 changes: 60 additions & 8 deletions public/apps/account/test/account-app.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,12 @@
* permissions and limitations under the License.
*/

import { shallow } from 'enzyme';
import React from 'react';
import { setupTopNavButton } from '../account-app';
import {
getShouldShowTenantPopup,
setShouldShowTenantPopup,
getSavedTenant,
} from '../../../utils/storage-utils';
import { setShouldShowTenantPopup, getSavedTenant } from '../../../utils/storage-utils';
import { fetchAccountInfoSafe } from '../utils';
import { fetchCurrentAuthType } from '../../../utils/logout-utils';
import { fetchCurrentTenant, selectTenant } from '../../configuration/utils/tenant-utils';
import { fetchCurrentTenant } from '../../configuration/utils/tenant-utils';
import { getDashboardsInfoSafe } from '../../../utils/dashboards-info-utils';

jest.mock('../../../utils/storage-utils', () => ({
getShouldShowTenantPopup: jest.fn(),
Expand All @@ -35,6 +30,10 @@ jest.mock('../utils', () => ({
fetchAccountInfoSafe: jest.fn(),
}));

jest.mock('../../../utils/dashboards-info-utils.tsx', () => ({
getDashboardsInfoSafe: jest.fn(),
}));

jest.mock('../../../utils/logout-utils', () => ({
fetchCurrentAuthType: jest.fn(),
}));
Expand All @@ -56,6 +55,7 @@ describe('Account app', () => {
const mockConfig = {
multitenancy: {
enable_aggregation_view: true,
enabled: true,
},
};

Expand All @@ -67,12 +67,18 @@ describe('Account app', () => {
},
};

const mockDashboardsInfo = {
default_tenant: '',
multitenancy_enabled: true,
};

const mockTenant = 'test1';

beforeAll(() => {
(fetchAccountInfoSafe as jest.Mock).mockResolvedValue(mockAccountInfo);
(fetchCurrentAuthType as jest.Mock).mockResolvedValue('dummy');
(fetchCurrentTenant as jest.Mock).mockResolvedValue(mockTenant);
(getDashboardsInfoSafe as jest.Mock).mockResolvedValue(mockDashboardsInfo);
});

it('Should skip if auto swich if securitytenant in url', (done) => {
Expand Down Expand Up @@ -116,4 +122,50 @@ describe('Account app', () => {
done();
});
});

it('Should not show tenant selection popup when multitenancy is disabled from security index', (done) => {
(getDashboardsInfoSafe as jest.Mock).mockResolvedValueOnce({
default_tenant: '',
multitenancy_enabled: false,
});

setupTopNavButton(mockCoreStart, mockConfig as any);

process.nextTick(() => {
expect(getSavedTenant).toBeCalledTimes(0);
expect(setShouldShowTenantPopup).toBeCalledWith(false);
done();
});
});

it('Should not show tenant selection popup when multitenancy is disabled dynamically', (done) => {
const multiTenancyDisabledConfig = {
multitenancy: {
enable_aggregation_view: true,
enabled: false,
},
};
setupTopNavButton(mockCoreStart, multiTenancyDisabledConfig as any);

process.nextTick(() => {
expect(getSavedTenant).toBeCalledTimes(0);
expect(setShouldShowTenantPopup).toBeCalledWith(false);
done();
});
});

it('Should not show tenant selection popup when default tenant is set from security index', (done) => {
(getDashboardsInfoSafe as jest.Mock).mockResolvedValueOnce({
default_tenant: 'Global',
multitenancy_enabled: true,
});

setupTopNavButton(mockCoreStart, mockConfig as any);

process.nextTick(() => {
expect(getSavedTenant).toBeCalledTimes(0);
expect(setShouldShowTenantPopup).toBeCalledWith(false);
done();
});
});
});
11 changes: 2 additions & 9 deletions public/apps/account/test/account-nav-button.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,8 @@ describe('Account navigation button, multitenancy disabled', () => {
jest.clearAllMocks();
});

it('should not set modal when show popup is true', () => {
(getDashboardsInfo as jest.Mock).mockImplementation(() => {
return {
multitenancy_enabled: false,
private_tenant_enabled: false,
default_tenant: '',
};
});
(getShouldShowTenantPopup as jest.Mock).mockReturnValueOnce(true);
it('should not set modal when getShouldShowTenantPopup is false', () => {
(getShouldShowTenantPopup as jest.Mock).mockReturnValueOnce(false);
shallow(
<AccountNavButton
coreStart={mockCoreStart}
Expand Down
Loading