Skip to content

Commit

Permalink
chore: fix and update ui tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mistahj67 committed Dec 3, 2024
1 parent b6b3e86 commit 67ca6ac
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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', () => {
Expand All @@ -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 () => {
Expand All @@ -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();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -38,13 +43,19 @@ 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];

describe('SSOProviderTable', () => {
const onClickSSOProvider = vi.fn();
const onDeleteSSOProvider = vi.fn();
const onUpdateSSOProvider = vi.fn();

it('should render', async () => {
const onToggleTypeSortOrder = vi.fn();
Expand All @@ -56,6 +67,7 @@ describe('SSOProviderTable', () => {
onClickSSOProvider={onClickSSOProvider}
onDeleteSSOProvider={onDeleteSSOProvider}
onToggleTypeSortOrder={onToggleTypeSortOrder}
onUpdateSSOProvider={onUpdateSSOProvider}
/>
);

Expand Down Expand Up @@ -84,6 +96,7 @@ describe('SSOProviderTable', () => {
onDeleteSSOProvider={onDeleteSSOProvider}
onToggleTypeSortOrder={onToggleTypeSortOrder}
typeSortOrder={typeSortOrder}
onUpdateSSOProvider={onUpdateSSOProvider}
/>
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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 },
},
},
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -65,7 +80,7 @@ describe('UpsertSAMLProviderForm', () => {
const testOnSubmit = vi.fn();
const validProviderName = 'test-provider-name';
const validMetadata = new File([], 'test-metadata.xml');
render(<UpsertSAMLProviderForm onClose={testOnClose} onSubmit={testOnSubmit} />);
render(<UpsertSAMLProviderForm onClose={testOnClose} onSubmit={testOnSubmit} roles={testRoles} />);

await user.type(screen.getByLabelText('SAML Provider Name'), validProviderName);

Expand Down
1 change: 1 addition & 0 deletions packages/javascript/bh-shared-ui/src/mocks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@

export * from './factories';
export * from './webgl';
export * from './resizeObserver';
26 changes: 26 additions & 0 deletions packages/javascript/bh-shared-ui/src/mocks/resizeObserver.ts
Original file line number Diff line number Diff line change
@@ -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(),
})),
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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];
Expand All @@ -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 {
Expand All @@ -80,6 +98,15 @@ interface CreateSAMLProviderResponse {
}

const server = setupServer(
rest.get<any, any, ListRolesResponse>(`/api/v2/roles`, (req, res, ctx) => {
return res(
ctx.json({
data: {
roles: testRoles,
},
})
);
}),
rest.get<any, any, ListSSOProvidersResponse>('/api/v2/sso-providers', (req, res, ctx) => {
return res(
ctx.json({
Expand Down Expand Up @@ -107,7 +134,10 @@ beforeEach(() => {
};
});
});
beforeAll(() => server.listen());
beforeAll(() => {
server.listen();
resizeObserver();
});
afterEach(() => server.resetHandlers());
afterAll(() => server.close());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
},
},
];

Expand Down

0 comments on commit 67ca6ac

Please sign in to comment.