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

feat(profile): change profile api endpoint #4909

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
44feb25
fix(profile): use new endpoint for profile log in status
annawen1 Jan 6, 2021
07705be
Merge branch 'master' into feat/change-profile-api-endpoint
annawen1 Jan 12, 2021
4a10e4f
fix(profile): updating from jsonp to axios
annawen1 Jan 14, 2021
1eea2f7
Merge branch 'master' into feat/change-profile-api-endpoint
annawen1 Jan 14, 2021
f759031
fix(profile): use preprod endpoint of profile api
annawen1 Jan 14, 2021
df551b0
chore(dependencies): remove jsonp package
annawen1 Jan 14, 2021
dc1a5dc
Merge branch 'master' into feat/change-profile-api-endpoint
annawen1 Jan 14, 2021
e6c978f
fix(profile): update default endpoint to point to prod
annawen1 Jan 15, 2021
62e58cb
Merge branch 'feat/change-profile-api-endpoint' of https://github.com…
annawen1 Jan 15, 2021
26bd76c
Merge branch 'master' into feat/change-profile-api-endpoint
annawen1 Jan 15, 2021
ebfc226
chore(dependencies): jsonp remove from package yarnlock file
annawen1 Jan 15, 2021
3565bba
fix(profile): using getUserStatus call from services
annawen1 Jan 15, 2021
914e8a6
fix(profile): make call with all credentials
annawen1 Jan 15, 2021
24ea549
fix(profile): update to check for user field that has user email
annawen1 Jan 15, 2021
f990547
Merge branch 'master' into feat/change-profile-api-endpoint
annawen1 Jan 15, 2021
f431ba9
fix(profile): update tests remove deleted objects
annawen1 Jan 15, 2021
45917fb
Merge branch 'feat/change-profile-api-endpoint' of https://github.com…
annawen1 Jan 15, 2021
c47b525
fix(profile): update profile API tests
annawen1 Jan 15, 2021
4df4e33
fix(profile): set userStatus default to Unauthenticated
annawen1 Jan 15, 2021
d292b39
Merge branch 'master' into feat/change-profile-api-endpoint
jeffchew Jan 15, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Binary file removed .yarn/offline-mirror/jsonp-0.2.1.tgz
Binary file not shown.
2 changes: 1 addition & 1 deletion packages/react/src/components/Masthead/Masthead.js
Expand Up @@ -88,7 +88,7 @@ const Masthead = ({
(async () => {
const status = await ProfileAPI.getUserStatus();
if (!unmounted) {
setStatus(status.user === 'Authenticated');
setStatus(status.user !== 'Unauthenticated');
}
})();
return () => {
Expand Down
73 changes: 35 additions & 38 deletions packages/services-store/src/actions/__tests__/profileAPI.test.ts
@@ -1,7 +1,7 @@
/**
* @license
*
* Copyright IBM Corp. 2020
* Copyright IBM Corp. 2020, 2021
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
Expand All @@ -10,10 +10,10 @@
import configureMockStore from 'redux-mock-store';
import { AnyAction } from 'redux';
import thunk, { ThunkDispatch } from 'redux-thunk';
import ProfileAPI from '@carbon/ibmdotcom-services/es/services/Profile/Profile.js';
import { USER_AUTHENTICATION_STATUS, PROFILE_API_ACTION, ProfileAPIState } from '../../types/profileAPI';
import convertValue from '../../../tests/utils/convert-value';
import { setUserStatus, monitorUserStatus } from '../profileAPI';
// import ProfileAPI from '@carbon/ibmdotcom-services/es/services/Profile/Profile.js';
import { PROFILE_API_ACTION, ProfileAPIState } from '../../types/profileAPI';
// import convertValue from '../../../tests/utils/convert-value';
import { setUserStatus } from '../profileAPI';

jest.mock('@carbon/ibmdotcom-services/es/services/Profile/Profile');

Expand All @@ -25,45 +25,42 @@ const mockStore = configureMockStore<
describe('Redux actions for `ProfileAPI`', () => {
it('dispatches the action to set user authentication status', () => {
const store = mockStore();
store.dispatch(setUserStatus({ user: USER_AUTHENTICATION_STATUS.AUTHENTICATED }));
store.dispatch(setUserStatus({ user: 'test.user@ibm.com' }));
expect(store.getActions()).toEqual([
{
type: PROFILE_API_ACTION.SET_USER_STATUS,
status: { user: USER_AUTHENTICATION_STATUS.AUTHENTICATED },
status: { user: 'test.user@ibm.com' },
},
]);
});

it('dispatches the action to monitor user authentication status', () => {
ProfileAPI.monitorUserStatus.mockImplementation(callback => {
callback(null, { user: USER_AUTHENTICATION_STATUS.AUTHENTICATED });
callback(null, { user: USER_AUTHENTICATION_STATUS.UNAUTHENTICATED });
});
const store = mockStore();
store.dispatch(monitorUserStatus());
expect(convertValue(store.getActions())).toEqual([
{
type: PROFILE_API_ACTION.SET_USER_STATUS,
status: { user: USER_AUTHENTICATION_STATUS.AUTHENTICATED },
},
{
type: PROFILE_API_ACTION.SET_USER_STATUS,
status: { user: USER_AUTHENTICATION_STATUS.UNAUTHENTICATED },
},
]);
});
// it('dispatches the action to get user authentication status', async () => {
// ProfileAPI.getUserStatus.mockResolvedValue({ user: 'Unauthenticated' });
// const store = mockStore();
// await store.dispatch(ProfileAPI.getUserStatus());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of ProfileAPI.getUserStatus() (that makes a call to @carbon/ibmdotcom-services module), we need to call a Redux action here. Here is a test in this category for reference: https://github.com/carbon-design-system/carbon-for-ibm-dotcom/blob/v1.14.0/packages/services-store/src/actions/__tests__/localeAPI.test.ts#L84

// expect(convertValue(store.getActions())).toEqual([
// {
// type: PROFILE_API_ACTION.SET_REQUEST_USER_STATUS_IN_PROGRESS,
// request: 'PROMISE',
// },
// {
// type: PROFILE_API_ACTION.SET_USER_STATUS,
// status: { user: 'Unauthenticated' },
// },
// ]);
// });

it('dispatches the action of error in monitoring user authentication status', () => {
ProfileAPI.monitorUserStatus.mockImplementation(callback => {
callback(new Error('error-monitoruserstatus'));
});
const store = mockStore();
store.dispatch(monitorUserStatus());
expect(convertValue(store.getActions())).toEqual([
{
type: PROFILE_API_ACTION.SET_ERROR_MONITOR_USER_STATUS,
error: 'error-monitoruserstatus',
},
]);
});
// it('dispatches the action of error in monitoring user authentication status', () => {
// ProfileAPI.getUserStatus.mockImplementation(callback => {
// callback(new Error('error-getuserstatus'));
// });
// const store = mockStore();
// store.dispatch(getUserStatus());
// expect(convertValue(store.getActions())).toEqual([
// {
// type: PROFILE_API_ACTION.SET_ERROR_REQUEST_USER_STATUS,
// error: 'error-getuserstatus',
// },
// ]);
// });
annawen1 marked this conversation as resolved.
Show resolved Hide resolved
});
45 changes: 31 additions & 14 deletions packages/services-store/src/actions/profileAPI.ts
@@ -1,7 +1,7 @@
/**
* @license
*
* Copyright IBM Corp. 2020
* Copyright IBM Corp. 2020, 2021
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
Expand All @@ -16,13 +16,25 @@ import { UserStatus, PROFILE_API_ACTION, ProfileAPIState } from '../types/profil
* @returns A Redux action to set the state that the JSONP call for user authentication status failed.
* @private
*/
export function setMonitorUserStatusError(error: Error) {
export function setErrorRequestUserStatus(error: Error) {
return {
type: PROFILE_API_ACTION.SET_ERROR_MONITOR_USER_STATUS,
type: PROFILE_API_ACTION.SET_ERROR_REQUEST_USER_STATUS,
error,
};
}

/**
* @param status The promise of the REST call for user status that is in progress.
* @returns A Redux action to set the state that the REST call for user status is in progress.
* @private
*/
export function setRequestUserStatusInProgress(status: Promise<UserStatus>) {
return {
type: PROFILE_API_ACTION.SET_REQUEST_USER_STATUS_IN_PROGRESS,
status,
};
}
annawen1 marked this conversation as resolved.
Show resolved Hide resolved

/**
* @param status The user authentication status from the JSONP call.
* @returns A Redux action to set the given user authentication status.
Expand All @@ -34,19 +46,24 @@ export function setUserStatus(status: UserStatus) {
};
}

export type ProfileAPIActions = ReturnType<typeof setMonitorUserStatusError> | ReturnType<typeof setUserStatus>;
export type ProfileAPIActions =
| ReturnType<typeof setErrorRequestUserStatus>
| ReturnType<typeof setRequestUserStatusInProgress>
| ReturnType<typeof setUserStatus>;

/**
* @returns A Redux action that monitors user authentication status.
* @returns A Redux action that gets user authentication status.
*/
export function monitorUserStatus(): ThunkAction<void, { profileAPI: ProfileAPIState }, void, ProfileAPIActions> {
return dispatch => {
ProfileAPI.monitorUserStatus((error: Error, status: UserStatus) => {
if (error) {
dispatch(setMonitorUserStatusError(error));
} else {
dispatch(setUserStatus(status));
}
});
export function getUserStatus(): ThunkAction<Promise<UserStatus>, { profileAPI: ProfileAPIState }, void, ProfileAPIActions> {
annawen1 marked this conversation as resolved.
Show resolved Hide resolved
return async dispatch => {
const promiseStatus: Promise<UserStatus> = ProfileAPI.getUserStatus();
dispatch(setRequestUserStatusInProgress(promiseStatus));
try {
dispatch(setUserStatus(await promiseStatus));
} catch (error) {
dispatch(setErrorRequestUserStatus(error));
throw error;
}
return promiseStatus;
};
}
@@ -1,33 +1,33 @@
/**
* @license
*
* Copyright IBM Corp. 2020
* Copyright IBM Corp. 2020, 2021
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

import { USER_AUTHENTICATION_STATUS, PROFILE_API_ACTION, ProfileAPIState } from '../../types/profileAPI';
import { PROFILE_API_ACTION, ProfileAPIState } from '../../types/profileAPI';
import { ProfileAPIActions } from '../../actions/profileAPI';
import convertValue from '../../../tests/utils/convert-value';
import reducer from '../profileAPI';

describe('Redux reducers for `ProfileAPI`', () => {
it('should return the state unmodified for unknown action', () => {
const state = { status: { user: USER_AUTHENTICATION_STATUS.AUTHENTICATED } };
const state = { status: { user: 'test.user@ibm.com' } };
expect(reducer(state, {} as ProfileAPIActions)).toEqual(state);
});

it('should support setting error in monitoring user authentication status', () => {
expect(
convertValue(
reducer({} as ProfileAPIState, {
type: PROFILE_API_ACTION.SET_ERROR_MONITOR_USER_STATUS,
type: PROFILE_API_ACTION.SET_ERROR_REQUEST_USER_STATUS,
error: new Error('error-user-status'),
})
)
).toEqual({
errorMonitorUserStatus: 'error-user-status',
errorGetUserStatus: 'error-user-status',
});
});

Expand All @@ -36,11 +36,11 @@ describe('Redux reducers for `ProfileAPI`', () => {
convertValue(
reducer({} as ProfileAPIState, {
type: PROFILE_API_ACTION.SET_USER_STATUS,
status: { user: USER_AUTHENTICATION_STATUS.AUTHENTICATED },
status: { user: 'test.user@ibm.com' },
})
)
).toEqual({
status: { user: USER_AUTHENTICATION_STATUS.AUTHENTICATED },
status: { user: 'test.user@ibm.com' },
});
});
});
10 changes: 5 additions & 5 deletions packages/services-store/src/reducers/profileAPI.ts
@@ -1,14 +1,14 @@
/**
* @license
*
* Copyright IBM Corp. 2020
* Copyright IBM Corp. 2020, 2021
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

import { PROFILE_API_ACTION, ProfileAPIState } from '../types/profileAPI';
import { setMonitorUserStatusError, setUserStatus, ProfileAPIActions } from '../actions/profileAPI';
import { setErrorRequestUserStatus, setUserStatus, ProfileAPIActions } from '../actions/profileAPI';

/**
* @param state The state for profile API.
Expand All @@ -17,11 +17,11 @@ import { setMonitorUserStatusError, setUserStatus, ProfileAPIActions } from '../
*/
export default function reducer(state: ProfileAPIState = {}, action: ProfileAPIActions): ProfileAPIState {
switch (action.type) {
case PROFILE_API_ACTION.SET_ERROR_MONITOR_USER_STATUS: {
const { error: errorMonitorUserStatus } = action as ReturnType<typeof setMonitorUserStatusError>;
case PROFILE_API_ACTION.SET_ERROR_REQUEST_USER_STATUS: {
const { error: errorGetUserStatus } = action as ReturnType<typeof setErrorRequestUserStatus>;
return {
...state,
errorMonitorUserStatus,
errorGetUserStatus,
};
}
case PROFILE_API_ACTION.SET_USER_STATUS: {
Expand Down
31 changes: 10 additions & 21 deletions packages/services-store/src/types/profileAPI.ts
@@ -1,45 +1,34 @@
/**
* @license
*
* Copyright IBM Corp. 2020
* Copyright IBM Corp. 2020, 2021
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

/**
* The user authentication status.
*/
export enum USER_AUTHENTICATION_STATUS {
/**
* Authenticated.
*/
AUTHENTICATED = 'Authenticated',

/**
* Unauthenticated.
*/
UNAUTHENTICATED = 'Unauthenticated',
}

/**
* The user authentication status, etc.
*/
export interface UserStatus {
/**
* The user authentication status.
*/
user: USER_AUTHENTICATION_STATUS;
user: string;
}

/**
* The Redux action ID for `ProfileAPI`.
*/
export enum PROFILE_API_ACTION {
/**
* One to set the state that the JSONP call for user authentication status failed.
* One to set the state that the REST call for user status is in progress or not.
*/
SET_REQUEST_USER_STATUS_IN_PROGRESS = 'SET_REQUEST_USER_STATUS_IN_PROGRESS',
/**
* One to set the state that the call user authentication status failed.
*/
SET_ERROR_MONITOR_USER_STATUS = 'SET_ERROR_MONITOR_USER_STATUS',
SET_ERROR_REQUEST_USER_STATUS = 'SET_ERROR_REQUEST_USER_STATUS',

/**
* One to set the given user authentication status.
Expand All @@ -52,9 +41,9 @@ export enum PROFILE_API_ACTION {
*/
export interface ProfileAPIState {
/**
* The error from the JSONP call for the user authentication status.
* The error from the call for the user authentication status.
*/
errorMonitorUserStatus?: Error;
errorGetUserStatus?: Error;

/**
* The user authentication status.
Expand Down
1 change: 0 additions & 1 deletion packages/services/package.json
Expand Up @@ -45,7 +45,6 @@
"@babel/runtime": "^7.5.0",
"@carbon/ibmdotcom-utilities": "1.14.0",
"axios": "^0.21.1",
"jsonp": "^0.2.1",
"window-or-global": "^1.0.1"
},
"devDependencies": {
Expand Down