Skip to content

Commit

Permalink
fix: tests to verify correct endpoints creation
Browse files Browse the repository at this point in the history
  • Loading branch information
soleksy-splunk committed Jan 11, 2024
1 parent 6378201 commit 0d700b9
Show file tree
Hide file tree
Showing 2 changed files with 179 additions and 2 deletions.
110 changes: 108 additions & 2 deletions ui/src/components/EntityModal/EntityModal.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { AxiosResponse } from 'axios';
import EntityModal, { EntityModalProps } from './EntityModal';
import { setUnifiedConfig } from '../../util/util';
import {
Expand All @@ -13,8 +14,10 @@ import {
getConfigOauthOauthDisableonEdit,
getConfigWithOauthDefaultValue,
getConfigWarningMessage,
getConfigWithSeparatedEndpointsOAuth,
} from './TestConfig';
import { ERROR_AUTH_PROCESS_TERMINATED_TRY_AGAIN } from '../../constants/oAuthErrorMessage';
import * as axiosWrapper from '../../util/axiosCallWrapper';

describe('Oauth field disabled on edit - diableonEdit property', () => {
const handleRequestClose = jest.fn();
Expand Down Expand Up @@ -222,10 +225,10 @@ describe('EntityModal - auth_endpoint_token_access_type', () => {

renderModalWithProps(props);

const cliendIdField = document.querySelector('.client_id')?.querySelector('input');
const cliendIdField = document.querySelector('.client_id input');
expect(cliendIdField).toBeInTheDocument();

const secretField = document.querySelector('.client_secret')?.querySelector('input');
const secretField = document.querySelector('.client_secret input');
expect(secretField).toBeInTheDocument();

const redirectField = document.querySelector('.redirect_url');
Expand Down Expand Up @@ -340,3 +343,106 @@ describe('Default value', () => {
expect(component).toHaveValue(DEFAULT_VALUE);
});
});

describe('Oauth - separated endpoint authorization', () => {
const handleRequestClose = jest.fn();
const setUpConfigWithSeparatedEndpoints = () => {
const newConfig = getConfigWithSeparatedEndpointsOAuth();
setUnifiedConfig(newConfig);
};

const renderModalWithProps = (props: EntityModalProps) => {
render(<EntityModal {...props} handleRequestClose={handleRequestClose} />);
};

const getFilledOauthFields = async () => {
const endpointAuth = document.querySelector('.endpoint_authorize input');
const endpointToken = document.querySelector('.endpoint_token input');

if (endpointAuth) {
await userEvent.type(endpointAuth, 'authendpoint');
}
if (endpointToken) {
await userEvent.type(endpointToken, 'tokenendpoint');
}
return [endpointAuth, endpointToken];
};

const spyOnWindowOpen = async (addButton: HTMLElement) => {
const windowOpenSpy = jest.spyOn(window, 'open') as jest.Mock;

// mock opening verification window
windowOpenSpy.mockImplementation((url) => {
expect(url).toEqual(
'https://authendpoint/services/oauth2/authorize?response_type=code&client_id=Client%20Id&redirect_uri=http%3A%2F%2Flocalhost%2F'
);

return { closed: true };
});

await userEvent.click(addButton);
windowOpenSpy.mockRestore();
};

const props = {
serviceName: 'account',
mode: 'create',
stanzaName: undefined,
formLabel: 'formLabel',
page: 'configuration',
groupName: '',
open: true,
handleRequestClose: () => {},
} satisfies EntityModalProps;

it('render modal with separated oauth fields', async () => {
setUpConfigWithSeparatedEndpoints();
renderModalWithProps(props);

const [endpointAuth, endpointToken] = await getFilledOauthFields();

expect(endpointAuth).toBeInTheDocument();
expect(endpointAuth).toHaveValue('authendpoint');
expect(endpointToken).toBeInTheDocument();
expect(endpointToken).toHaveValue('tokenendpoint');
});

it('check if correct authorization endpoint called', async () => {
setUpConfigWithSeparatedEndpoints();
renderModalWithProps(props);

await getFilledOauthFields();

const addButton = screen.getByRole('button', { name: /add/i });
expect(addButton).toBeInTheDocument();

await spyOnWindowOpen(addButton);
});

it('check if correct auth token endpoint created', async () => {
setUpConfigWithSeparatedEndpoints();
renderModalWithProps(props);
const backendTokenFunction = jest.fn();

await getFilledOauthFields();
const addButton = screen.getByRole('button', { name: /add/i });
expect(addButton).toBeInTheDocument();

await spyOnWindowOpen(addButton);

// token is aquired on backend side so only thing we can check is if there is correct url created
jest.spyOn(axiosWrapper, 'axiosCallWrapper').mockImplementation((params) => {
backendTokenFunction((params?.body as unknown as URLSearchParams)?.get('url'));
return new Promise((r) => r({} as unknown as PromiseLike<AxiosResponse>));
});

// triggering manually external oauth window behaviour after success authorization
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(window as any).getMessage({ code: 200, msg: 'testing message for oauth' });

// only purpose is to check if backend function receinved correct token url
expect(backendTokenFunction).toHaveBeenCalledWith(
'https://tokenendpoint/services/oauth2/token'
);
});
});
71 changes: 71 additions & 0 deletions ui/src/components/EntityModal/TestConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,3 +421,74 @@ export const getConfigWithOauthDefaultValue = () => {
};
return newConfig satisfies z.infer<typeof GlobalConfigSchema>;
};

const entityOauthOauthSeparatedEndpoints = [
{
type: 'oauth',
field: 'oauth_jest_test',
label: 'Not used',
required: true,
encrypted: false,
options: {
auth_type: ['oauth'],
oauth: [
{
oauth_field: 'client_id',
label: 'Client Id',
field: 'client_id',
help: 'Enter the Client Id for this account.',
defaultValue: 'Client Id',
},
{
oauth_field: 'client_secret',
label: 'Client Secret',
field: 'client_secret',
encrypted: true,
help: 'Enter the Client Secret key for this account.',
defaultValue: 'Client Secret',
},
{
oauth_field: 'redirect_url',
label: 'Redirect url',
field: 'redirect_url',
help: 'Copy and paste this URL into your app.',
defaultValue: 'Redirect url',
},
{
oauth_field: 'endpoint_token',
label: 'Token endpoint',
field: 'endpoint_token',
help: 'Put here endpoint used for token acqusition ie. login.salesforce.com',
},
{
oauth_field: 'endpoint_authorize',
label: 'Authorize endpoint',
field: 'endpoint_authorize',
help: 'Put here endpoint used for authorization ie. login.salesforce.com',
},
],
auth_code_endpoint: '/services/oauth2/authorize',
access_token_endpoint: '/services/oauth2/token',
oauth_timeout: 3000,
oauth_state_enabled: false,
display: true,
disableonEdit: false,
enable: true,
},
} satisfies z.infer<typeof OAuthEntity>,
];

export const getConfigWithSeparatedEndpointsOAuth = () => {
const globalConfig = getGlobalConfigMock();
const newConfig = {
...globalConfig,
pages: {
...globalConfig.pages,
configuration: {
...globalConfig.pages.configuration,
tabs: [{ entity: entityOauthOauthSeparatedEndpoints, ...defaultTableProps }],
},
},
};
return newConfig satisfies z.infer<typeof GlobalConfigSchema>;
};

0 comments on commit 0d700b9

Please sign in to comment.