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

Add some missing tests for authz view in UI #677

Merged
merged 1 commit into from
Sep 25, 2020
Merged
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
6 changes: 6 additions & 0 deletions web/src/api/__fixtures__/index/33.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"authorization_enabled": true,
"predefined_policy": null,
"custom_policy": "package artifacthub.authz\n\nallow = true\nallowed_actions = [\"all\"]",
"policy_data": {}
}
1 change: 1 addition & 0 deletions web/src/api/__fixtures__/index/34.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["addOrganizationMember", "addOrganizationRepository", "deleteOrganizationMember", "deleteOrganizationRepository"]
3 changes: 3 additions & 0 deletions web/src/api/__fixtures__/index/35.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"result": "https://play.openpolicyagent.org/p/wkFwnwti9Z"
}
4 changes: 4 additions & 0 deletions web/src/api/__mocks__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,8 @@ export const API = {
getOptOutList: jest.fn(),
addOptOut: jest.fn(),
deleteOptOut: jest.fn(),
getAuthorizationPolicy: jest.fn(),
updateAuthorizationPolicy: jest.fn(),
getUserAllowedActions: jest.fn(),
triggerTestInRegoPlayground: jest.fn(),
};
101 changes: 101 additions & 0 deletions web/src/api/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ import fetchMock from 'jest-fetch-mock';

import {
APIKey,
AuthorizationPolicy,
CheckAvailabilityProps,
ErrorKind,
OptOutItem,
Organization,
Package,
PackageStars,
Profile,
RegoPlaygroundPolicy,
RegoPlaygroundResult,
Repository,
RepositoryKind,
SearchResults,
Expand Down Expand Up @@ -1614,5 +1617,103 @@ describe('index API', () => {
expect(response).toBe('');
});
});

describe('getAuthorizationPolicy', () => {
it('success', async () => {
const authz: AuthorizationPolicy = getData('33') as AuthorizationPolicy;
fetchMock.mockResponse(JSON.stringify(authz), {
headers: {
'content-type': 'application/json',
},
status: 200,
});

const response = await methods.API.getAuthorizationPolicy('org1');

expect(fetchMock.mock.calls.length).toEqual(1);
expect(fetchMock.mock.calls[0][0]).toEqual('/api/v1/orgs/org1/authorizationPolicy');
expect(response).toEqual(methods.toCamelCase(authz));
});
});

describe('updateAuthorizationPolicy', () => {
it('success', async () => {
fetchMock.mockResponse('', {
headers: {
'content-type': 'text/plain; charset=utf-8',
},
status: 204,
});

const policy = {
authorizationEnabled: true,
predefinedPolicy: null,
customPolicy: 'custom',
policyData: '{}',
};

const response = await methods.API.updateAuthorizationPolicy('org1', policy);

expect(fetchMock.mock.calls.length).toEqual(1);
expect(fetchMock.mock.calls[0][0]).toEqual('/api/v1/orgs/org1/authorizationPolicy');
expect(fetchMock.mock.calls[0][1]!.method).toBe('PUT');
expect(fetchMock.mock.calls[0][1]!.body).toBe(
JSON.stringify(
renameKeysInObject(
{ ...policy },
{
authorizationEnabled: 'authorization_enabled',
predefinedPolicy: 'predefined_policy',
customPolicy: 'custom_policy',
policyData: 'policy_data',
}
)
)
);
expect(response).toBe('');
});
});

describe('getUserAllowedActions', () => {
it('success', async () => {
const actions: string[] = getData('34') as string[];
fetchMock.mockResponse(JSON.stringify(actions), {
headers: {
'content-type': 'application/json',
},
status: 200,
});

const response = await methods.API.getUserAllowedActions('org1');

expect(fetchMock.mock.calls.length).toEqual(1);
expect(fetchMock.mock.calls[0][0]).toEqual('/api/v1/orgs/org1/userAllowedActions');
expect(response).toEqual(methods.toCamelCase(actions));
});
});

describe('triggerTestInRegoPlayground', () => {
it('success', async () => {
const playgroundPolicy: RegoPlaygroundResult = getData('35') as RegoPlaygroundResult;
fetchMock.mockResponse(JSON.stringify(playgroundPolicy), {
headers: {
'content-type': 'application/json',
},
status: 200,
});

const data: RegoPlaygroundPolicy = {
rego_modules: { 'policy.rego': 'package artifacthub.authz\n\nallow = true\nallowed_actions = ["all"]' },
input: { user: 'cynthiasg', action: 'updateOrganization' },
data: {},
};

const response = await methods.API.triggerTestInRegoPlayground(data);

expect(fetchMock.mock.calls.length).toEqual(1);
expect(fetchMock.mock.calls[0][0]).toEqual('https://play.openpolicyagent.org/v1/share');
expect(response).toEqual(methods.toCamelCase(playgroundPolicy));
});
});
});
});
130 changes: 130 additions & 0 deletions web/src/layout/controlPanel/ActionBtn.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import { fireEvent, render, waitFor } from '@testing-library/react';
import React from 'react';

import { AppCtx } from '../../context/AppCtx';
import { AuthorizerAction, AuthorizerInput } from '../../types';
import ActionBtn from './ActionBtn';

jest.mock('../../utils/authorizer', () => ({
check: (params: AuthorizerInput) => {
if (params.user === 'member') {
return false;
}
return true;
},
}));

const onClickMock = jest.fn();

const defaultProps = {
testId: 'test',
onClick: onClickMock,
action: AuthorizerAction.AddOrganizationMember,
};

const mockCtx = {
user: { alias: 'test', email: 'test@test.com' },
prefs: {
controlPanel: { selectedOrg: 'orgTest' },
search: { limit: 25 },
theme: {
configured: 'light',
automatic: false,
},
},
};

describe('ActionBtn', () => {
beforeEach(() => {
jest.useFakeTimers();
});

afterEach(() => {
jest.resetAllMocks();
jest.runOnlyPendingTimers();
jest.useRealTimers();
});

it('renders correctly', () => {
const result = render(
<AppCtx.Provider value={{ ctx: mockCtx, dispatch: jest.fn() }}>
<ActionBtn {...defaultProps}>
<div>button content</div>
</ActionBtn>
</AppCtx.Provider>
);
expect(result.asFragment()).toMatchSnapshot();
});

it('renders enabled button', () => {
const { getByTestId, getByText } = render(
<AppCtx.Provider value={{ ctx: mockCtx, dispatch: jest.fn() }}>
<ActionBtn {...defaultProps}>
<div>button content</div>
</ActionBtn>
</AppCtx.Provider>
);

const btn = getByTestId(defaultProps.testId);
expect(btn).toBeInTheDocument();
expect(btn).not.toHaveClass('disabled');

fireEvent.click(btn);
expect(onClickMock).toHaveBeenCalledTimes(1);

expect(getByText('button content')).toBeInTheDocument();
});

it('renders disabled button', () => {
const { getByTestId, getByText } = render(
<AppCtx.Provider
value={{
ctx: {
...mockCtx,
user: { alias: 'member', email: 'test@test.com' },
},
dispatch: jest.fn(),
}}
>
<ActionBtn {...defaultProps}>
<div>button content</div>
</ActionBtn>
</AppCtx.Provider>
);

const btn = getByTestId(defaultProps.testId);
expect(btn).toBeInTheDocument();
expect(btn).toHaveClass('disabled');

fireEvent.click(btn);
expect(onClickMock).toHaveBeenCalledTimes(0);

expect(getByText('button content')).toBeInTheDocument();
});

it('displays tooltip', async () => {
const { getByTestId, getByRole, getByText } = render(
<AppCtx.Provider
value={{
ctx: {
...mockCtx,
user: { alias: 'member', email: 'test@test.com' },
},
dispatch: jest.fn(),
}}
>
<ActionBtn {...defaultProps}>
<div>button content</div>
</ActionBtn>
</AppCtx.Provider>
);

const btn = getByTestId(defaultProps.testId);
fireEvent.mouseEnter(btn);

await waitFor(() => {
expect(getByRole('tooltip')).toBeInTheDocument();
expect(getByText('You are not allowed to perform this action')).toBeInTheDocument();
});
});
});
23 changes: 23 additions & 0 deletions web/src/layout/controlPanel/__snapshots__/ActionBtn.test.tsx.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions web/src/layout/controlPanel/members/Card.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ import { Member } from '../../../types';
import Card from './Card';
jest.mock('../../../api');

jest.mock('../../../utils/authorizer', () => ({
check: () => {
return true;
},
}));

const memberMock: Member = {
alias: 'test',
firstName: 'first',
Expand Down
6 changes: 6 additions & 0 deletions web/src/layout/controlPanel/members/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ import { ErrorKind, User } from '../../../types';
import MembersSection from './index';
jest.mock('../../../api');

jest.mock('../../../utils/authorizer', () => ({
check: () => {
return true;
},
}));

const getMembers = (fixtureId: string): User[] => {
return require(`./__fixtures__/index/${fixtureId}.json`) as User[];
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"authorizationEnabled": false,
"predefinedPolicy": null,
"customPolicy": null,
"policyData": null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"authorizationEnabled": true,
"predefinedPolicy": null,
"customPolicy": "package artifacthub.authz↵↵allow = true↵allowed_actions = [\"all\"]",
"policyData": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"authorizationEnabled": true,
"predefinedPolicy": null,
"customPolicy": "package artifacthub.authz↵↵allow = true↵allowed_actions = [\"all\"]",
"policyData": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"authorizationEnabled": false,
"predefinedPolicy": null,
"customPolicy": null,
"policyData": null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"authorizationEnabled": true,
"predefinedPolicy": "rbac.v1",
"customPolicy": null,
"policyData": {
"roles": {
"owner": {
"users": ["demo", "cintia"]
},
"customRole1": {
"users": [],
"allowed_actions": [
"addOrganizationMember",
"addOrganizationRepository",
"deleteOrganizationMember",
"deleteOrganizationRepository",
"getAuthorizationPolicy",
"transferOrganizationRepository",
"updateAuthorizationPolicy",
"updateOrganization",
"updateOrganizationRepository"
]
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"authorizationEnabled": true,
"predefinedPolicy": null,
"customPolicy": "package artifacthub.authz\n\nallow = true\nallowed_actions = [\"all\"]",
"policyData": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"authorizationEnabled": true,
"predefinedPolicy": null,
"customPolicy": "package artifacthub.authz\n\nallow = true\nallowed_actions = [\"all\"]",
"policyData": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"authorizationEnabled": true,
"predefinedPolicy": null,
"customPolicy": "package artifacthub.authz\n\nallow = true\nallowed_actions = [\"all\"]",
"policyData": {}
}
Loading