Skip to content

Commit

Permalink
fix(userSelectors): issues/307 missing error check (#308)
Browse files Browse the repository at this point in the history
* userSelectors, missing error check, check for global errors
  • Loading branch information
cdcabrera committed Jun 16, 2020
1 parent a8f8a0a commit a799987
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`UserSelectors should not authorize a user when global errors exist: global errors, unauthorized 1`] = `
Object {
"session": Object {
"admin": false,
"authorized": false,
"entitled": false,
"error": true,
"errorCodes": Array [
"loremIpsum",
],
"errorMessage": "lorem ipsum",
"permissions": Array [],
"status": 403,
},
}
`;

exports[`UserSelectors should pass data with administrator checks: administrator, and missing user data 1`] = `
Object {
"session": Object {
"admin": true,
"authorized": false,
"entitled": false,
"error": false,
"permissions": Array [],
},
}
Expand All @@ -17,6 +35,7 @@ Object {
"admin": false,
"authorized": false,
"entitled": true,
"error": false,
"permissions": Array [],
},
}
Expand All @@ -28,6 +47,7 @@ Object {
"admin": false,
"authorized": true,
"entitled": false,
"error": false,
"permissions": Array [
Object {
"definitions": undefined,
Expand Down Expand Up @@ -84,6 +104,7 @@ Object {
"admin": false,
"authorized": false,
"entitled": false,
"error": false,
"locale": "en-US",
"permissions": Array [],
},
Expand All @@ -96,6 +117,7 @@ Object {
"admin": false,
"authorized": false,
"entitled": false,
"error": false,
"permissions": Array [],
},
}
Expand Down
43 changes: 43 additions & 0 deletions src/redux/selectors/__tests__/userSelectors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe('UserSelectors', () => {
}
}
};

expect(userSelectors.userSession(state)).toMatchSnapshot('existing state data');
});

Expand All @@ -34,6 +35,7 @@ describe('UserSelectors', () => {
}
}
};

expect(userSelectors.userSession(state)).toMatchSnapshot('error state data');
});

Expand Down Expand Up @@ -103,4 +105,45 @@ describe('UserSelectors', () => {

expect(userSelectors.userSession(state)).toMatchSnapshot('permissions, and missing user data');
});

it('should not authorize a user when global errors exist', () => {
const state = {
user: {
session: {
error: true,
errorCodes: ['loremIpsum'],
errorMessage: 'lorem ipsum',
status: 403,
fulfilled: true,
data: {
user: {
[platformApiTypes.PLATFORM_API_RESPONSE_USER_ENTITLEMENTS]: {
[helpers.UI_NAME]: {
[platformApiTypes.PLATFORM_API_RESPONSE_USER_ENTITLEMENTS_APP_TYPES.ENTITLED]: true
}
},
[platformApiTypes.PLATFORM_API_RESPONSE_USER_IDENTITY]: {
[platformApiTypes.PLATFORM_API_RESPONSE_USER_IDENTITY_TYPES.USER]: {
[platformApiTypes.PLATFORM_API_RESPONSE_USER_IDENTITY_USER_TYPES.ORG_ADMIN]: true
}
}
},
permissions: [
{
[platformApiTypes.PLATFORM_API_RESPONSE_USER_PERMISSION_TYPES.PERMISSION]: `${helpers.UI_NAME}:*:*`
},
{
[platformApiTypes.PLATFORM_API_RESPONSE_USER_PERMISSION_TYPES.PERMISSION]: `${helpers.UI_NAME}:*:read`
},
{
[platformApiTypes.PLATFORM_API_RESPONSE_USER_PERMISSION_TYPES.PERMISSION]: `${helpers.UI_NAME}:*:write`
}
]
}
}
}
};

expect(userSelectors.userSession(state)).toMatchSnapshot('global errors, unauthorized');
});
});
7 changes: 4 additions & 3 deletions src/redux/selectors/userSelectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,20 @@ const userSession = state => ({
/**
* Create selector, transform combined state, props into a consumable graph/charting object.
*
* @type {{session: {entitled: boolean, permissions: Array, authorized: boolean, admin: boolean}}}
* @type {{session: {entitled: boolean, permissions: Array, authorized: boolean, admin: boolean, error: boolean}}}
*/
const userSessionSelector = createSelector([userSession], response => {
const { fulfilled = false, data = {}, ...rest } = response || {};
const { error = false, fulfilled = false, data = {}, ...rest } = response || {};
const updatedSession = {
...rest,
admin: false,
authorized: false,
entitled: false,
error,
permissions: []
};

if (fulfilled) {
if (!error && fulfilled) {
const { user = {}, permissions = [] } = data;

const admin = _get(
Expand Down

0 comments on commit a799987

Please sign in to comment.