From 67ca6ac342b5c492e6b7c7b71d1cb03ca5c59470 Mon Sep 17 00:00:00 2001
From: Mistah J <26472282+mistahj67@users.noreply.github.com>
Date: Tue, 3 Dec 2024 10:24:10 -0700
Subject: [PATCH] chore: fix and update ui tests
---
.../SSOProviderInfoPanel.test.tsx | 24 +++++++++++++
.../SSOProviderTable.test.tsx | 13 +++++++
.../UpdateUserDialog.test.tsx | 20 +++++++++++
.../UpsertSAMLProviderForm.test.tsx | 17 +++++++++-
.../bh-shared-ui/src/mocks/index.ts | 1 +
.../bh-shared-ui/src/mocks/resizeObserver.ts | 26 ++++++++++++++
.../SSOConfiguration.test.tsx | 34 +++++++++++++++++--
.../src/views/Users/Users.test.tsx | 5 +++
8 files changed, 137 insertions(+), 3 deletions(-)
create mode 100644 packages/javascript/bh-shared-ui/src/mocks/resizeObserver.ts
diff --git a/packages/javascript/bh-shared-ui/src/components/SSOProviderInfoPanel/SSOProviderInfoPanel.test.tsx b/packages/javascript/bh-shared-ui/src/components/SSOProviderInfoPanel/SSOProviderInfoPanel.test.tsx
index 1763464ccc..10e14a84f5 100644
--- a/packages/javascript/bh-shared-ui/src/components/SSOProviderInfoPanel/SSOProviderInfoPanel.test.tsx
+++ b/packages/javascript/bh-shared-ui/src/components/SSOProviderInfoPanel/SSOProviderInfoPanel.test.tsx
@@ -35,6 +35,11 @@ const samlProvider: SSOProvider = {
} as SAMLProviderInfo,
created_at: '2022-02-24T23:38:41.420271Z',
updated_at: '2022-02-24T23:38:41.420271Z',
+ login_uri: '',
+ callback_uri: '',
+ config: {
+ auto_provision: { enabled: false, role_provision: false, default_role: 1 },
+ },
};
const oidcProvider: SSOProvider = {
@@ -48,6 +53,11 @@ const oidcProvider: SSOProvider = {
} as OIDCProviderInfo,
created_at: '2022-02-24T23:38:41.420271Z',
updated_at: '2022-02-24T23:38:41.420271Z',
+ login_uri: '',
+ callback_uri: 'http://bloodhound.localhost/api/v2/sso/test-idp-2/callback',
+ config: {
+ auto_provision: { enabled: true, role_provision: true, default_role: 1 },
+ },
};
describe('SSOProviderTable', () => {
@@ -60,6 +70,15 @@ describe('SSOProviderTable', () => {
expect(await screen.findByText(samlInfo.sp_sso_uri)).toBeInTheDocument();
expect(await screen.findByText(samlInfo.sp_acs_uri)).toBeInTheDocument();
expect(await screen.findByText(samlInfo.sp_metadata_uri)).toBeInTheDocument();
+
+ expect(await screen.findByText('Automatically create new users on login')).toBeInTheDocument();
+ // This provider has IDP provisioning disabled which should hide these 2 fields
+ expect(screen.queryByText('Allow SSO provider to manage roles for new users')).not.toBeInTheDocument();
+ expect(screen.queryByText('Default role when creating new users')).not.toBeInTheDocument();
+
+ expect(
+ screen.getByRole('button', { name: `Download ${samlProvider.name} SP Certificate` })
+ ).toBeInTheDocument();
});
it('should render oidc info provider', async () => {
@@ -69,5 +88,10 @@ describe('SSOProviderTable', () => {
expect(await screen.findByText(oidcInfo.issuer)).toBeInTheDocument();
expect(await screen.findByText(oidcInfo.client_id)).toBeInTheDocument();
+ expect(await screen.findByText(oidcProvider.callback_uri)).toBeInTheDocument();
+
+ expect(await screen.findByText('Automatically create new users on login')).toBeInTheDocument();
+ expect(await screen.findByText('Allow SSO provider to manage roles for new users')).toBeInTheDocument();
+ expect(await screen.findByText('Default role when creating new users')).toBeInTheDocument();
});
});
diff --git a/packages/javascript/bh-shared-ui/src/components/SSOProviderTable/SSOProviderTable.test.tsx b/packages/javascript/bh-shared-ui/src/components/SSOProviderTable/SSOProviderTable.test.tsx
index 220df2ca0f..29a8db5584 100644
--- a/packages/javascript/bh-shared-ui/src/components/SSOProviderTable/SSOProviderTable.test.tsx
+++ b/packages/javascript/bh-shared-ui/src/components/SSOProviderTable/SSOProviderTable.test.tsx
@@ -28,6 +28,11 @@ const samlProvider: SSOProvider = {
created_at: '2022-02-24T23:38:41.420271Z',
updated_at: '2022-02-24T23:38:41.420271Z',
details: {} as SAMLProviderInfo,
+ login_uri: '',
+ callback_uri: '',
+ config: {
+ auto_provision: { enabled: false, role_provision: false, default_role: 1 },
+ },
};
const oidcProvider: SSOProvider = {
@@ -38,6 +43,11 @@ const oidcProvider: SSOProvider = {
created_at: '2022-02-24T23:38:41.420271Z',
updated_at: '2022-02-24T23:38:41.420271Z',
details: {} as OIDCProviderInfo,
+ login_uri: '',
+ callback_uri: '',
+ config: {
+ auto_provision: { enabled: false, role_provision: false, default_role: 1 },
+ },
};
const ssoProviders = [samlProvider, oidcProvider];
@@ -45,6 +55,7 @@ const ssoProviders = [samlProvider, oidcProvider];
describe('SSOProviderTable', () => {
const onClickSSOProvider = vi.fn();
const onDeleteSSOProvider = vi.fn();
+ const onUpdateSSOProvider = vi.fn();
it('should render', async () => {
const onToggleTypeSortOrder = vi.fn();
@@ -56,6 +67,7 @@ describe('SSOProviderTable', () => {
onClickSSOProvider={onClickSSOProvider}
onDeleteSSOProvider={onDeleteSSOProvider}
onToggleTypeSortOrder={onToggleTypeSortOrder}
+ onUpdateSSOProvider={onUpdateSSOProvider}
/>
);
@@ -84,6 +96,7 @@ describe('SSOProviderTable', () => {
onDeleteSSOProvider={onDeleteSSOProvider}
onToggleTypeSortOrder={onToggleTypeSortOrder}
typeSortOrder={typeSortOrder}
+ onUpdateSSOProvider={onUpdateSSOProvider}
/>
);
diff --git a/packages/javascript/bh-shared-ui/src/components/UpdateUserDialog/UpdateUserDialog.test.tsx b/packages/javascript/bh-shared-ui/src/components/UpdateUserDialog/UpdateUserDialog.test.tsx
index b7a98111b4..a874553fd7 100644
--- a/packages/javascript/bh-shared-ui/src/components/UpdateUserDialog/UpdateUserDialog.test.tsx
+++ b/packages/javascript/bh-shared-ui/src/components/UpdateUserDialog/UpdateUserDialog.test.tsx
@@ -51,6 +51,11 @@ const testSSOProviders: SSOProvider[] = [
id: 1,
created_at: '2024-01-01T12:00:00Z',
updated_at: '2024-01-01T12:00:00Z',
+ login_uri: '',
+ callback_uri: '',
+ config: {
+ auto_provision: { enabled: false, role_provision: false, default_role: 1 },
+ },
},
{
name: 'saml-provider-2',
@@ -74,6 +79,11 @@ const testSSOProviders: SSOProvider[] = [
id: 2,
created_at: '2024-01-01T12:00:00Z',
updated_at: '2024-01-01T12:00:00Z',
+ login_uri: '',
+ callback_uri: '',
+ config: {
+ auto_provision: { enabled: false, role_provision: false, default_role: 1 },
+ },
},
{
name: 'saml-provider-3',
@@ -97,6 +107,11 @@ const testSSOProviders: SSOProvider[] = [
id: 3,
created_at: '2024-01-01T12:00:00Z',
updated_at: '2024-01-01T12:00:00Z',
+ login_uri: '',
+ callback_uri: '',
+ config: {
+ auto_provision: { enabled: false, role_provision: false, default_role: 1 },
+ },
},
{
name: 'saml-provider-4',
@@ -120,6 +135,11 @@ const testSSOProviders: SSOProvider[] = [
id: 4,
created_at: '2024-01-01T12:00:00Z',
updated_at: '2024-01-01T12:00:00Z',
+ login_uri: '',
+ callback_uri: '',
+ config: {
+ auto_provision: { enabled: false, role_provision: false, default_role: 1 },
+ },
},
];
diff --git a/packages/javascript/bh-shared-ui/src/components/UpsertSSOProviders/UpsertSAMLProviderForm/UpsertSAMLProviderForm.test.tsx b/packages/javascript/bh-shared-ui/src/components/UpsertSSOProviders/UpsertSAMLProviderForm/UpsertSAMLProviderForm.test.tsx
index c8d780b0a5..8054f4a901 100644
--- a/packages/javascript/bh-shared-ui/src/components/UpsertSSOProviders/UpsertSAMLProviderForm/UpsertSAMLProviderForm.test.tsx
+++ b/packages/javascript/bh-shared-ui/src/components/UpsertSSOProviders/UpsertSAMLProviderForm/UpsertSAMLProviderForm.test.tsx
@@ -16,9 +16,20 @@
import userEvent from '@testing-library/user-event';
import { render, screen, waitFor } from '../../../test-utils';
+import { resizeObserver } from '../../../mocks';
import UpsertSAMLProviderForm from './UpsertSAMLProviderForm';
+import { Role } from 'js-client-library';
+
+const testRoles = [
+ { id: 1, name: 'Read-Only' },
+ { id: 2, name: 'Power User' },
+ { id: 3, name: 'Administrator' },
+ { id: 4, name: 'Upload Only' },
+] as Role[];
describe('UpsertSAMLProviderForm', () => {
+ beforeAll(() => resizeObserver());
+
it('should render inputs, labels, and action buttons', () => {
const testOnClose = vi.fn();
const testOnSubmit = vi.fn();
@@ -28,6 +39,10 @@ describe('UpsertSAMLProviderForm', () => {
expect(screen.getByLabelText('Choose File')).toBeInTheDocument();
+ expect(screen.getByTestId('sso-provider-config-form_toggle-auto-provision')).toBeInTheDocument();
+ expect(screen.getByTestId('sso-provider-config-form_toggle-role-provision')).toBeInTheDocument();
+ expect(screen.getByTestId('sso-provider-config-form_select-default-role')).toBeInTheDocument();
+
expect(screen.getByRole('button', { name: 'Cancel' })).toBeInTheDocument();
expect(screen.getByRole('button', { name: 'Submit' })).toBeInTheDocument();
@@ -65,7 +80,7 @@ describe('UpsertSAMLProviderForm', () => {
const testOnSubmit = vi.fn();
const validProviderName = 'test-provider-name';
const validMetadata = new File([], 'test-metadata.xml');
- render();
+ render();
await user.type(screen.getByLabelText('SAML Provider Name'), validProviderName);
diff --git a/packages/javascript/bh-shared-ui/src/mocks/index.ts b/packages/javascript/bh-shared-ui/src/mocks/index.ts
index d756c64945..ac94969803 100644
--- a/packages/javascript/bh-shared-ui/src/mocks/index.ts
+++ b/packages/javascript/bh-shared-ui/src/mocks/index.ts
@@ -16,3 +16,4 @@
export * from './factories';
export * from './webgl';
+export * from './resizeObserver';
diff --git a/packages/javascript/bh-shared-ui/src/mocks/resizeObserver.ts b/packages/javascript/bh-shared-ui/src/mocks/resizeObserver.ts
new file mode 100644
index 0000000000..d2e6ecdc37
--- /dev/null
+++ b/packages/javascript/bh-shared-ui/src/mocks/resizeObserver.ts
@@ -0,0 +1,26 @@
+// Copyright 2024 Specter Ops, Inc.
+//
+// Licensed under the Apache License, Version 2.0
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License 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.
+//
+// SPDX-License-Identifier: Apache-2.0
+
+export const resizeObserver = () => {
+ Object.defineProperty(window, 'ResizeObserver', {
+ writable: true,
+ value: vi.fn().mockImplementation(() => ({
+ observe: vi.fn(),
+ unobserve: vi.fn(),
+ disconnect: vi.fn(),
+ })),
+ });
+};
diff --git a/packages/javascript/bh-shared-ui/src/views/SSOConfiguration/SSOConfiguration.test.tsx b/packages/javascript/bh-shared-ui/src/views/SSOConfiguration/SSOConfiguration.test.tsx
index 685d79dfab..b79f51520e 100644
--- a/packages/javascript/bh-shared-ui/src/views/SSOConfiguration/SSOConfiguration.test.tsx
+++ b/packages/javascript/bh-shared-ui/src/views/SSOConfiguration/SSOConfiguration.test.tsx
@@ -19,7 +19,15 @@ import { rest } from 'msw';
import { setupServer } from 'msw/node';
import { render, screen } from '../../test-utils';
import SSOConfiguration from './SSOConfiguration';
-import { ListSSOProvidersResponse, SAMLProviderInfo, SSOProvider } from 'js-client-library';
+import { resizeObserver } from '../../mocks';
+import { ListRolesResponse, ListSSOProvidersResponse, Role, SAMLProviderInfo, SSOProvider } from 'js-client-library';
+
+const testRoles = [
+ { id: 1, name: 'Read-Only' },
+ { id: 2, name: 'Power User' },
+ { id: 3, name: 'Administrator' },
+ { id: 4, name: 'Upload Only' },
+] as Role[];
const initialSAMLProvider: SSOProvider = {
id: 1,
@@ -37,6 +45,11 @@ const initialSAMLProvider: SSOProvider = {
} as SAMLProviderInfo,
created_at: '2022-02-24T23:38:41.420271Z',
updated_at: '2022-02-24T23:38:41.420271Z',
+ login_uri: '',
+ callback_uri: '',
+ config: {
+ auto_provision: { enabled: false, role_provision: false, default_role: 1 },
+ },
};
const ssoProviders = [initialSAMLProvider];
@@ -57,6 +70,11 @@ const newSAMLProvider: SSOProvider = {
} as SAMLProviderInfo,
created_at: new Date().toISOString(),
updated_at: new Date().toISOString(),
+ login_uri: '',
+ callback_uri: '',
+ config: {
+ auto_provision: { enabled: false, role_provision: false, default_role: 1 },
+ },
};
interface CreateSAMLProviderBody {
@@ -80,6 +98,15 @@ interface CreateSAMLProviderResponse {
}
const server = setupServer(
+ rest.get(`/api/v2/roles`, (req, res, ctx) => {
+ return res(
+ ctx.json({
+ data: {
+ roles: testRoles,
+ },
+ })
+ );
+ }),
rest.get('/api/v2/sso-providers', (req, res, ctx) => {
return res(
ctx.json({
@@ -107,7 +134,10 @@ beforeEach(() => {
};
});
});
-beforeAll(() => server.listen());
+beforeAll(() => {
+ server.listen();
+ resizeObserver();
+});
afterEach(() => server.resetHandlers());
afterAll(() => server.close());
diff --git a/packages/javascript/bh-shared-ui/src/views/Users/Users.test.tsx b/packages/javascript/bh-shared-ui/src/views/Users/Users.test.tsx
index bab27dc271..0b4f032bf8 100644
--- a/packages/javascript/bh-shared-ui/src/views/Users/Users.test.tsx
+++ b/packages/javascript/bh-shared-ui/src/views/Users/Users.test.tsx
@@ -116,6 +116,11 @@ const testSSOProviders: SSOProvider[] = [
id: 1,
created_at: '2024-01-01T12:00:00Z',
updated_at: '2024-01-01T12:00:00Z',
+ login_uri: '',
+ callback_uri: '',
+ config: {
+ auto_provision: { enabled: false, role_provision: false, default_role: 1 },
+ },
},
];