Skip to content

Commit

Permalink
fix(platformServices): issues/260 confirm user permissions
Browse files Browse the repository at this point in the history
* userReducer, map user permissions
* platformServices, remove user permissions async/await, name param
* platformApiTypes, user permission types
  • Loading branch information
cdcabrera committed May 19, 2020
1 parent 5ad0c5f commit 0775154
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 13 deletions.
6 changes: 5 additions & 1 deletion src/redux/reducers/userReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ const userReducer = (state = initialState, action) => {
false
);

const subscriptionPermissions = permissions.filter(value => new RegExp(helpers.UI_NAME, 'i').test(value));
const subscriptionPermissions = permissions.map(value => ({
permission: value[platformApiTypes.PLATFORM_API_RESPONSE_USER_PERMISSION_TYPES.PERMISSION],
definitions: value[platformApiTypes.PLATFORM_API_RESPONSE_USER_PERMISSION_TYPES.RESOURCE_DEFS]
}));

return reduxHelpers.setStateProp(
'session',
Expand Down Expand Up @@ -127,6 +130,7 @@ const userReducer = (state = initialState, action) => {
error: true,
errorMessage: reduxHelpers.getMessageFromResults(action),
locale: state.session.locale,
permissions: state.session.permissions,
status: reduxHelpers.getStatusFromResults(action)
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

exports[`PlatformServices should return a failed getUser: failed authorized user 1`] = `[Error: { getUser } = insights.chrome.auth, insights.chrome.auth.getUser is not a function]`;

exports[`PlatformServices should return a failed getUserPermissions: failed user permissions 1`] = `[Error: { getUserPermissions } = insights.chrome, insights.chrome.getUserPermissions is not a function]`;

exports[`PlatformServices should return a failed initializeChrome: failed initializeChrome 1`] = `[Error: { init } = insights.chrome, insights.chrome.init is not a function]`;

exports[`PlatformServices should return a failed setAppName: failed setAppName 1`] = `"{ identifyApp } = insights.chrome, insights.chrome.identifyApp is not a function"`;
Expand Down
9 changes: 4 additions & 5 deletions src/services/__tests__/platformServices.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,12 @@ describe('PlatformServices', () => {
});
});

it('should return a failed getUserPermissions', done => {
it('should return a failed getUserPermissions', () => {
window.insights.chrome.getUserPermissions = undefined;

platformServices.getUserPermissions().catch(error => {
expect(error).toMatchSnapshot('failed user permissions');
done();
});
expect(platformServices.getUserPermissions).toThrowError(
'{ getUserPermissions } = insights.chrome, insights.chrome.getUserPermissions is not a function'
);
});

it('should return a failed initializeChrome', done => {
Expand Down
10 changes: 8 additions & 2 deletions src/services/platformServices.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,21 @@ const getUser = async () => {
}
};

/**
* FixMe: revert this back towards async/await
* Removed because there appears to be some quirky behavior where permissions will not come through
* unless the function, and/or await are specifically returned, i.e. "return await insights.chrome...".
*/
/**
* Basic user permissions.
*
* @param {string} appName
* @returns {Promise<void>}
*/
const getUserPermissions = async () => {
const getUserPermissions = (appName = '') => {
const { insights } = window;
try {
await insights.chrome.getUserPermissions();
return insights.chrome.getUserPermissions(appName);
} catch (e) {
throw new Error(`{ getUserPermissions } = insights.chrome, ${e.message}`);
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/userServices.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const authorizeUser = async () => {

try {
userData = await getUser();
userPermissions = await getUserPermissions();
userPermissions = await getUserPermissions(helpers.UI_NAME);
} catch (e) {
message = e.message;
}
Expand Down
16 changes: 16 additions & 0 deletions src/types/__tests__/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ Object {
"PLATFORM_API_RESPONSE_USER_IDENTITY_USER_TYPES": Object {
"ORG_ADMIN": "is_org_admin",
},
"PLATFORM_API_RESPONSE_USER_PERMISSION_TYPES": Object {
"PERMISSION": "permission",
"RESOURCE_DEFS": "resourceDefinitions",
},
},
"rhsmApi": Object {
"RHSM_API_PATH_ID_TYPES": Object {
Expand Down Expand Up @@ -101,6 +105,10 @@ Object {
"PLATFORM_API_RESPONSE_USER_IDENTITY_USER_TYPES": Object {
"ORG_ADMIN": "is_org_admin",
},
"PLATFORM_API_RESPONSE_USER_PERMISSION_TYPES": Object {
"PERMISSION": "permission",
"RESOURCE_DEFS": "resourceDefinitions",
},
},
"rhsmApi": Object {
"RHSM_API_PATH_ID_TYPES": Object {
Expand Down Expand Up @@ -186,6 +194,10 @@ Object {
"PLATFORM_API_RESPONSE_USER_IDENTITY_USER_TYPES": Object {
"ORG_ADMIN": "is_org_admin",
},
"PLATFORM_API_RESPONSE_USER_PERMISSION_TYPES": Object {
"PERMISSION": "permission",
"RESOURCE_DEFS": "resourceDefinitions",
},
},
"rhsmApiTypes": Object {
"RHSM_API_PATH_ID_TYPES": Object {
Expand Down Expand Up @@ -275,6 +287,10 @@ Object {
"PLATFORM_API_RESPONSE_USER_IDENTITY_USER_TYPES": Object {
"ORG_ADMIN": "is_org_admin",
},
"PLATFORM_API_RESPONSE_USER_PERMISSION_TYPES": Object {
"PERMISSION": "permission",
"RESOURCE_DEFS": "resourceDefinitions",
},
},
"rhsmApi": Object {
"RHSM_API_PATH_ID_TYPES": Object {
Expand Down
17 changes: 15 additions & 2 deletions src/types/platformApiTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ const PLATFORM_API_RESPONSE_USER_IDENTITY_USER_TYPES = {
ORG_ADMIN: 'is_org_admin'
};

/**
* Platform response of USER PERMISSION type values.
* Schema/map of expected response identity user permission types.
*
* @type {{PERMISSION: string}}
*/
const PLATFORM_API_RESPONSE_USER_PERMISSION_TYPES = {
PERMISSION: 'permission',
RESOURCE_DEFS: 'resourceDefinitions'
};

/**
* Platform API types.
*
Expand All @@ -55,7 +66,8 @@ const platformApiTypes = {
PLATFORM_API_RESPONSE_USER_ENTITLEMENTS_APP_TYPES,
PLATFORM_API_RESPONSE_USER_IDENTITY,
PLATFORM_API_RESPONSE_USER_IDENTITY_TYPES,
PLATFORM_API_RESPONSE_USER_IDENTITY_USER_TYPES
PLATFORM_API_RESPONSE_USER_IDENTITY_USER_TYPES,
PLATFORM_API_RESPONSE_USER_PERMISSION_TYPES
};

export {
Expand All @@ -65,5 +77,6 @@ export {
PLATFORM_API_RESPONSE_USER_ENTITLEMENTS_APP_TYPES,
PLATFORM_API_RESPONSE_USER_IDENTITY,
PLATFORM_API_RESPONSE_USER_IDENTITY_TYPES,
PLATFORM_API_RESPONSE_USER_IDENTITY_USER_TYPES
PLATFORM_API_RESPONSE_USER_IDENTITY_USER_TYPES,
PLATFORM_API_RESPONSE_USER_PERMISSION_TYPES
};

0 comments on commit 0775154

Please sign in to comment.