Skip to content

Commit

Permalink
Adds error component tests to each added error component
Browse files Browse the repository at this point in the history
Signed-off-by: Darshit Chanpura <[email protected]>
  • Loading branch information
DarshitChanpura committed May 25, 2024
1 parent ffe27b4 commit 94c1588
Show file tree
Hide file tree
Showing 23 changed files with 648 additions and 66 deletions.
File renamed without changes.
29 changes: 15 additions & 14 deletions public/apps/configuration/panels/audit-logging/audit-logging.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import {
EuiForm,
EuiFormRow,
EuiHorizontalRule,
EuiLoadingContent,
EuiPageHeader,
EuiPanel,
EuiSpacer,
EuiSwitch,
Expand All @@ -46,7 +48,7 @@ import { ViewSettingGroup } from './view-setting-group';
import { DocLinks } from '../../constants';
import { DataSourceContext } from '../../app-router';
import { SecurityPluginTopNavMenu } from '../../top-nav-menu';
import { AccessErrorComponent } from '../../../access-error-component';
import { AccessErrorComponent } from '../../access-error-component';

interface AuditLoggingProps extends AppDependencies {
fromType: string;
Expand All @@ -55,10 +57,6 @@ interface AuditLoggingProps extends AppDependencies {
function renderStatusPanel(onSwitchChange: () => void, auditLoggingEnabled: boolean) {
return (
<EuiPanel>
<EuiTitle>
<h3>Audit logging</h3>
</EuiTitle>
<EuiHorizontalRule margin="m" />
<EuiForm>
<EuiDescribedFormGroup title={<h3>Storage location</h3>} className="described-form-group">
<EuiFormRow className="form-row">
Expand Down Expand Up @@ -97,13 +95,11 @@ function renderStatusPanel(onSwitchChange: () => void, auditLoggingEnabled: bool

function renderAccessErrorPanel(loading: boolean, dataSource: DataSourceOption) {
return (
<EuiPanel>
<EuiTitle>
<h3>Audit logging</h3>
</EuiTitle>
<EuiHorizontalRule margin="m" />
<AccessErrorComponent loading={loading} dataSourceLabel={dataSource && dataSource.label} />
</EuiPanel>
<AccessErrorComponent
loading={loading}
dataSourceLabel={dataSource && dataSource.label}
message="You do not have permissions to configure audit logging settings"
/>
);
}

Expand Down Expand Up @@ -201,7 +197,6 @@ export function AuditLogging(props: AuditLoggingProps) {
<>
{statusPanel}
<EuiSpacer />

<EuiPanel data-test-subj="general-settings">
<EuiFlexGroup>
<EuiFlexItem>
Expand Down Expand Up @@ -262,7 +257,13 @@ export function AuditLogging(props: AuditLoggingProps) {
setDataSource={setDataSource}
selectedDataSource={dataSource}
/>
{content}
<EuiPageHeader>
<EuiTitle size="l">
<h1>Audit Logging</h1>
</EuiTitle>
</EuiPageHeader>
<EuiSpacer />
{loading ? <EuiLoadingContent /> : content}
</div>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -256,15 +256,17 @@ exports[`Audit logs render when AuditLoggingSettings.enabled is true 1`] = `
}
setDataSource={[MockFunction]}
/>
<EuiPanel>
<EuiTitle>
<h3>
Audit logging
</h3>
<EuiPageHeader>
<EuiTitle
size="l"
>
<h1>
Audit Logging
</h1>
</EuiTitle>
<EuiHorizontalRule
margin="m"
/>
</EuiPageHeader>
<EuiSpacer />
<EuiPanel>
<EuiForm>
<EuiDescribedFormGroup
className="described-form-group"
Expand Down Expand Up @@ -644,3 +646,39 @@ exports[`Audit logs render when AuditLoggingSettings.enabled is true 1`] = `
</EuiPanel>
</div>
`;

exports[`Audit logs should load access error component 1`] = `
<div
className="panel-restrict-width"
>
<Memo()
coreStart={
Object {
"http": 1,
}
}
dataSourcePickerReadOnly={false}
navigation={Object {}}
selectedDataSource={
Object {
"id": "test",
}
}
setDataSource={[MockFunction]}
/>
<EuiPageHeader>
<EuiTitle
size="l"
>
<h1>
Audit Logging
</h1>
</EuiTitle>
</EuiPageHeader>
<EuiSpacer />
<AccessErrorComponent
loading={false}
message="You do not have permissions to configure audit logging settings"
/>
</div>
`;
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,17 @@ describe('Audit logs', () => {
buildHashUrl(ResourceType.auditLogging) + SUB_URL_FOR_COMPLIANCE_SETTINGS_EDIT
);
});

it('should load access error component', () => {
const auditLoggingSettings = { enabled: true };
jest
.spyOn(React, 'useState')
.mockImplementationOnce(() => [auditLoggingSettings, setState])
.mockImplementationOnce(() => [false, jest.fn()])
.mockImplementationOnce(() => [true, jest.fn()]);
const component = shallow(
<AuditLogging coreStart={mockCoreStart as any} navigation={{} as any} />
);
expect(component).toMatchSnapshot();
});
});
16 changes: 9 additions & 7 deletions public/apps/configuration/panels/auth-view/auth-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
* permissions and limitations under the License.
*/

import React, { useContext, useState } from 'react';
import { EuiPageHeader, EuiSpacer, EuiTitle } from '@elastic/eui';
import React, { useContext } from 'react';
import { EuiLoadingContent, EuiPageHeader, EuiSpacer, EuiTitle } from '@elastic/eui';
import { isEmpty } from 'lodash';
import { AuthenticationSequencePanel } from './authentication-sequence-panel';
import { AuthorizationPanel } from './authorization-panel';
Expand All @@ -25,12 +25,12 @@ import { getSecurityConfig } from '../../utils/auth-view-utils';
import { InstructionView } from './instruction-view';
import { DataSourceContext } from '../../app-router';
import { SecurityPluginTopNavMenu } from '../../top-nav-menu';
import { AccessErrorComponent } from '../../../access-error-component';
import { AccessErrorComponent } from '../../access-error-component';

export function AuthView(props: AppDependencies) {
const [authentication, setAuthentication] = React.useState([]);
const [authorization, setAuthorization] = React.useState([]);
const [loading, setLoading] = useState(false);
const [loading, setLoading] = React.useState(false);
const { dataSource, setDataSource } = useContext(DataSourceContext)!;
const [errorFlag, setErrorFlag] = React.useState(false);
const [accessErrorFlag, setAccessErrorFlag] = React.useState(false);
Expand Down Expand Up @@ -59,7 +59,7 @@ export function AuthView(props: AppDependencies) {
fetchData();
}, [props.coreStart.http, dataSource]);

if (isEmpty(authentication)) {
if (isEmpty(authentication) && !loading) {
return (
<>
<SecurityPluginTopNavMenu
Expand Down Expand Up @@ -97,14 +97,16 @@ export function AuthView(props: AppDependencies) {
<EuiTitle size="l">
<h1>Authentication and authorization</h1>
</EuiTitle>
{!errorFlag && props.config.ui.backend_configurable && (
{!loading && !errorFlag && props.config.ui.backend_configurable && (
<ExternalLinkButton
href={DocLinks.BackendConfigurationDoc}
text="Manage via config.yml"
/>
)}
</EuiPageHeader>
{accessErrorFlag ? (
{loading ? (
<EuiLoadingContent />
) : accessErrorFlag ? (
<AccessErrorComponent loading={loading} dataSourceLabel={dataSource && dataSource.label} />
) : (
<>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Auth view should load access error component 1`] = `
<Fragment>
<Memo()
coreStart={
Object {
"http": 1,
}
}
dataSourcePickerReadOnly={false}
navigation={Object {}}
selectedDataSource={
Object {
"id": "test",
}
}
setDataSource={[MockFunction]}
/>
<EuiTitle
size="l"
>
<h1>
Authentication and authorization
</h1>
</EuiTitle>
<AccessErrorComponent
loading={false}
/>
</Fragment>
`;
49 changes: 32 additions & 17 deletions public/apps/configuration/panels/auth-view/test/auth-view.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,38 +29,37 @@ describe('Auth view', () => {
const mockCoreStart = {
http: 1,
};
const config = {
authc: {
basic_internal_auth_domain: {
authentication_backend: {
type: 'intern',
config: {},
},
},
},
authz: {
ldap: {
http_enabled: true,
},
},
};

const setState = jest.fn();

beforeEach(() => {
// jest.spyOn(React, 'useState').mockImplementation((initialValue) => [initialValue, setState]);
jest.spyOn(React, 'useState').mockRestore();
jest
.spyOn(React, 'useState')
.mockImplementationOnce(() => [[], setState])
.mockImplementationOnce(() => [[], setState])
.mockImplementationOnce(() => [false, jest.fn()])
.mockImplementationOnce(() => [false, jest.fn()])
.mockImplementationOnce(() => [false, jest.fn()]);
jest.spyOn(React, 'useEffect').mockImplementationOnce((f) => f());
});

it('valid data', (done) => {
const config = {
authc: {
basic_internal_auth_domain: {
authentication_backend: {
type: 'intern',
config: {},
},
},
},
authz: {
ldap: {
http_enabled: true,
},
},
};

mockAuthViewUtils.getSecurityConfig = jest.fn().mockReturnValue(config);

shallow(<AuthView coreStart={mockCoreStart as any} navigation={{} as any} />);
Expand Down Expand Up @@ -92,4 +91,20 @@ describe('Auth view', () => {
done();
});
});

it('should load access error component', async () => {
jest.spyOn(React, 'useState').mockRestore();
jest
.spyOn(React, 'useState')
.mockImplementationOnce(() => [[], setState])
.mockImplementationOnce(() => [[], setState])
.mockImplementationOnce(() => [false, jest.fn()])
.mockImplementationOnce(() => [false, jest.fn()])
.mockImplementationOnce(() => [true, jest.fn()]);
mockAuthViewUtils.getSecurityConfig = jest
.fn()
.mockRejectedValue({ response: { status: 403 } });
const component = shallow(<AuthView coreStart={mockCoreStart as any} navigation={{} as any} />);
expect(component).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
RIGHT_ALIGNMENT,
EuiButtonEmpty,
Query,
EuiLoadingContent,
} from '@elastic/eui';
import { difference } from 'lodash';
import React, {
Expand Down Expand Up @@ -63,7 +64,7 @@ import { generateResourceName } from '../../utils/resource-utils';
import { DocLinks } from '../../constants';
import { SecurityPluginTopNavMenu } from '../../top-nav-menu';
import { DataSourceContext } from '../../app-router';
import { AccessErrorComponent } from '../../../access-error-component';
import { AccessErrorComponent } from '../../access-error-component';

export function renderBooleanToCheckMark(value: boolean): React.ReactNode {
return value ? <EuiIcon type="check" /> : '';
Expand Down Expand Up @@ -367,7 +368,9 @@ export function PermissionList(props: AppDependencies) {
<h1>Permissions</h1>
</EuiTitle>
</EuiPageHeader>
{accessErrorFlag ? (
{loading ? (
<EuiLoadingContent />
) : accessErrorFlag ? (
<AccessErrorComponent loading={loading} dataSourceLabel={dataSource && dataSource.label} />
) : (
<EuiPageContent>
Expand Down
7 changes: 5 additions & 2 deletions public/apps/configuration/panels/role-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
EuiButtonEmpty,
EuiSearchBarProps,
Query,
EuiLoadingContent,
} from '@elastic/eui';
import { difference } from 'lodash';
import { AppDependencies } from '../../types';
Expand All @@ -56,7 +57,7 @@ import { useContextMenuState } from '../utils/context-menu';
import { DocLinks } from '../constants';
import { DataSourceContext } from '../app-router';
import { SecurityPluginTopNavMenu } from '../top-nav-menu';
import { AccessErrorComponent } from '../../access-error-component';
import { AccessErrorComponent } from '../access-error-component';

const columns: Array<EuiBasicTableColumn<RoleListing>> = [
{
Expand Down Expand Up @@ -271,7 +272,9 @@ export function RoleList(props: AppDependencies) {
<h1>Roles</h1>
</EuiTitle>
</EuiPageHeader>
{accessErrorFlag ? (
{loading ? (
<EuiLoadingContent />
) : accessErrorFlag ? (
<AccessErrorComponent loading={loading} dataSourceLabel={dataSource && dataSource.label} />
) : (
<EuiPageContent>
Expand Down
Loading

0 comments on commit 94c1588

Please sign in to comment.