Skip to content

Commit

Permalink
fix(userServices): issues/159 emulate response error
Browse files Browse the repository at this point in the history
* user auth, emulate a response for app level error handling
  • Loading branch information
cdcabrera committed Jan 10, 2020
1 parent dc4c318 commit 1277d80
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
15 changes: 13 additions & 2 deletions src/services/__tests__/__snapshots__/userServices.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`UserServices should return a failed authorized user: failed authorized user 1`] = `[Error: { getUser } = insights.chrome.auth]`;
exports[`UserServices should return a failed authorized user: failed authorized user 1`] = `
Object {
"message": "{ getUser } = insights.chrome.auth",
"status": 418,
}
`;

exports[`UserServices should return a specific locale cookie value 1`] = `
Object {
Expand All @@ -11,7 +16,13 @@ Object {
}
`;

exports[`UserServices should return a successful authorized user: success authorized user 1`] = `"lorem ipsum"`;
exports[`UserServices should return a successful authorized user: success authorized user 1`] = `
Object {
"data": "lorem ipsum",
"message": "{ getUser } = insights.chrome.auth",
"status": 200,
}
`;

exports[`UserServices should return default locale if no locale cookie is present 1`] = `
Object {
Expand Down
24 changes: 16 additions & 8 deletions src/services/userServices.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,34 @@ import Cookies from 'js-cookie';
import LocaleCode from 'locale-code';
import { helpers } from '../common/helpers';

/**
* Emulate service response http status to aid in error handling.
* @returns {Promise<{statusText: string, message: string, status: number}>}
*/
const authorizeUser = async () => {
const response = {
status: 418,
message: '{ getUser } = insights.chrome.auth'
};
let getUserData = (helpers.TEST_MODE || helpers.DEV_MODE) && {};
let platformResponse;

if (!helpers.DEV_MODE && window.insights && window.insights.chrome.auth.getUser) {
getUserData = await window.insights.chrome.auth.getUser();
}

/**
* ToDo: evaluate this periodically, expecting specific platform behavior, this could be simplified
* Basic check for missing user data. Allowing GUI auth to pass in those cases affects our API
* auth for RHSM, so we block it.
* ToDo: evaluate this periodically, expecting specific platform behavior.
* Basic check for missing user data. Allowing GUI auth to pass with missing data affects our API
* auth for RHSM, so we block it. An additional, more specific, check for "account_number" may be needed.
*/
if (getUserData) {
platformResponse = Promise.resolve(getUserData);
} else {
platformResponse = Promise.reject(new Error('{ getUser } = insights.chrome.auth'));
response.status = 200;
response.data = getUserData;
return Promise.resolve(response);
}

return platformResponse;
const emulatedError = { ...new Error('{ getUser } = insights.chrome.auth'), ...response };
return Promise.reject(emulatedError);
};

const getLocaleFromCookie = () => {
Expand Down

0 comments on commit 1277d80

Please sign in to comment.