Skip to content

Commit

Permalink
fix(authorize): the authorize component was using the wrong axi struct
Browse files Browse the repository at this point in the history
The Authorize Component calls the axiUserPermissiosn Api but expects a structure of the type sdk/permissions api returns.
  • Loading branch information
availity-bot committed May 9, 2019
1 parent 4766061 commit acb90b1
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 102 deletions.
39 changes: 11 additions & 28 deletions packages/authorize/Authorize.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,7 @@ import { avUserPermissionsApi, avRegionsApi } from '@availity/api-axios';
import BlockUi from 'react-block-ui';
import 'react-block-ui/style.css';

const warned = {};

function warnOnce(message) {
if (!warned[message]) {
// eslint-disable-next-line no-console
if (typeof console !== 'undefined' && typeof console.error === 'function') {
console.error(message); // eslint-disable-line no-console
}
warned[message] = true;
}
}

const watching = ['region', 'organizationId', 'customId'];
const watching = ['region', 'organizationId'];

class Authorize extends Component {
static propTypes = {
Expand All @@ -36,7 +24,6 @@ class Authorize extends Component {
region: PropTypes.oneOfType([PropTypes.bool, PropTypes.string]),
loader: PropTypes.oneOfType([PropTypes.bool, PropTypes.node]),
organizationId: PropTypes.string,
customerId: PropTypes.string,
unauthorized: PropTypes.node,
children: PropTypes.node,
negate: PropTypes.bool,
Expand Down Expand Up @@ -73,25 +60,19 @@ class Authorize extends Component {
}

checkPermission(permission) {
const { organizationId, customerId } = this.props;
const { organizationId } = this.props;
if (!permission) return false;

if (organizationId) {
if (customerId) {
warnOnce(
'You provided both `organizationId` and `customerId` to Authorize but both cannot be used together; `organizationId` will be used and `customerId` will be ignored. If you want to use `customerId` do not provide `organizationId`.'
);
}
return (
permission.organizations.filter(org => org.id === organizationId)
.length > 0
);
}
if (customerId) {
console.log("Permission",permission,"OrgId",organizationId);
console.log("Are they equal?",organizationId.toString() === permission.organizationIds[0]);
console.log(`orgId:${organizationId.toString()} permissionOrg: ${permission.organizationIds[0]}`)
return (
permission.organizations.filter(org => org.customerId === customerId)
permission.organizationIds.filter(orgId => orgId === organizationId)
.length > 0
);
}

return true;
}

Expand All @@ -105,7 +86,7 @@ class Authorize extends Component {
? permissions
: [permissions];
const permissionsList = [].concat(...permissionsSets);
const newPermissions = (await avUserPermissionsApi.getPermissions(
let newPermissions = (await avUserPermissionsApi.getPermissions(
permissionsList,
await this.getRegion()
)).reduce((prev, cur) => {
Expand All @@ -119,6 +100,8 @@ class Authorize extends Component {
this.checkPermission(newPermissions[permission])
);
}
console.log("New permissions,",newPermissions);
console.log("permissionSet",permissionSet);
return this.checkPermission(newPermissions[permissionSet]);
});
if (
Expand Down
1 change: 0 additions & 1 deletion packages/authorize/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ Component which validates the user's permissions to determine if they should be
- **array**: The array can contain Permission ID strings as well as other arrays which contain permission ID strings/numbers, eg: `['1234', '2345', ['3456', '4567'], ['5678', '6789']]`. The items in deep/nested array indicate permission IDs which must all be granted to the user to be consider authorized, they act as "and". The items in the top array act as 'or', if any are granted to the user the user would be consider authorized. The example `['1234', '2345', ['3456', '4567'], ['5678', '6789']]` would be similar `'1234' || '2345' || ('3456' && '4567') || ('5678' && '6789')`
- **`loader`**: Boolean or node. Optional, default: `true`. When `true`, `BlockUi` will be used when loading the permissions. When a `node`, that node will be render instead of `BlockUi` when loading the permissions. When `false`, nothing will be render when loading the permissions.
- **`organizationId`**: String. Optional, when present, the permission will be validated to ensure it is assigned to the organization.
- **`customerId`**: String. Optional, when present, the permission will be validated to ensure it is assigned to the customer. Note: Cannot be used in combination with the `organizationId` prop
- **`region`**: String or boolean. Optional, default: `true`. When a string, the permission will be validated to ensure it is assigned in the region. When true, the current region will be used.
- **`unauthorized`**: Node. Optional. The content which will be render when the user does not have the permissions required.
- **`children`**: Node. Required. The content which will be render when the user does have the permissions required.
Expand Down
94 changes: 77 additions & 17 deletions packages/authorize/tests/Authorize.test.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,109 @@
import React from 'react';
import { render, cleanup } from 'react-testing-library';
import { render, cleanup, waitForElement } from 'react-testing-library';
import { avUserPermissionsApi } from '@availity/api-axios';
import Authorize from '..';

afterEach(cleanup);
jest.mock('@availity/api-axios');

afterEach(() => {
cleanup();

jest.clearAllMocks();
});

beforeEach(() => {
avUserPermissionsApi.getPermissions.mockResolvedValue([
{
id: '1234',
organizationIds: ['1111']
},
]);
});

describe('Authorize', () => {
test('should render', () => {
const { container } = render(<Authorize permissions="1234" loader />);
test('should render authorized content', async () => {
const { getByText } = render(
<Authorize permissions="1234" loader>
You have permission to see this
</Authorize>
);

expect(container.firstChild).toMatchSnapshot();
await waitForElement(() => getByText('You have permission to see this'));
});

test('should render with single permission', () => {
const { container } = render(
test('should render authorized content', async () => {
const { getByText } = render(
<Authorize
permissions="1234"
permissions="12345"
unauthorized="You do not have permission to see this"
/>
);

expect(container).toMatchSnapshot();
await waitForElement(() =>
getByText('You do not have permission to see this')
);
});

test('should render with array of permissions', () => {
const { container } = render(
test('should render authorized with array of permissions', async () => {
const { getByText } = render(
<Authorize
permissions={['1234', 2345, [3456, '4567']]}
unauthorized="You do not have permission to see this"
/>
>
You have permission to see this
</Authorize>
);

expect(container.firstChild).toMatchSnapshot();
await waitForElement(() => getByText('You have permission to see this'));
});

test('should render negate permissions', () => {
const { container } = render(
test('should render negate permissions', async () => {
const { getByText } = render(
<Authorize
permissions="1234"
negate
unauthorized="You do not have permission to see this"
>
You can see this
You have permission to see this
</Authorize>
);

expect(container.firstChild).toMatchSnapshot();
await waitForElement(() =>
getByText('You do not have permission to see this')
);
});

test('should render authorized with correct organizationId', async () => {
const { getByText } = render(
<Authorize
permissions="1234"
organizationId="1111"
negate
unauthorized="You do not have permission to see this"
>
You have permission to see this
</Authorize>
);

await waitForElement(() =>
getByText('You have permission to see this')
);
});

test('should render unauthorized with incorrect organizationId', async () => {
const { getByText } = render(
<Authorize
permissions="1234"
organizationId="1112"
negate
unauthorized="You do not have permission to see this"
>
You have permission to see this
</Authorize>
);

await waitForElement(() =>
getByText('You do not have permission to see this')
);
});
});
56 changes: 0 additions & 56 deletions packages/authorize/tests/__snapshots__/Authorize.test.js.snap

This file was deleted.

0 comments on commit acb90b1

Please sign in to comment.