Skip to content

Commit

Permalink
#SKFP-40 remove gen3 and dfc tokens from localstorage
Browse files Browse the repository at this point in the history
  • Loading branch information
evans-g-crsj committed Jul 14, 2021
1 parent f0eb43a commit 55dce61
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 113 deletions.
1 change: 0 additions & 1 deletion src/common/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ export const GEN3 = 'gen3';
export const FENCES = [GEN3, DCF];

export const CAVATICA = 'cavatica';
export const SERVICES = [...FENCES, CAVATICA];

export const GOOGLE = 'google';
export const FACEBOOK = 'facebook';
Expand Down
4 changes: 0 additions & 4 deletions src/components/Login/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import isArrayLikeObject from 'lodash/isArrayLikeObject';
import toLower from 'lodash/toLower';

import { CAVATICA, FENCES } from 'common/constants';
import { INTEGRATION_PREFIX } from 'common/constants';
import { getUser as getCavaticaUser } from 'services/cavatica';
import { getAccessToken } from 'services/fence';
import { createProfile, getProfile } from 'services/profiles';
Expand Down Expand Up @@ -106,6 +105,3 @@ export const fetchIntegrationTokens = ({ setIntegrationToken, api }) => {
});
});
};

export const hasIntegrationTokenForFence = (fenceName) =>
!!localStorage.getItem(`${INTEGRATION_PREFIX}${fenceName}`);
11 changes: 0 additions & 11 deletions src/components/UserProfile/Integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import React from 'react';
import { connect as reduxConnect } from 'react-redux';
import { BookOutlined } from '@ant-design/icons';
import { notification } from 'antd';
import { injectState } from 'freactal';
import get from 'lodash/get';
import PropTypes from 'prop-types';
import { compose, setPropTypes } from 'recompose';
Expand Down Expand Up @@ -63,15 +62,13 @@ const disconnect = async ({
fence,
api,
setConnecting,
effects,
setError,
removeFenceConnection,
removeFenceStudies,
}) => {
setConnecting(true);
try {
await deleteFenceTokens(api, fence);
await effects.setIntegrationToken(fence, null);
removeFenceConnection(fence);
removeFenceStudies(fence);
await trackFenceAction({
Expand All @@ -91,7 +88,6 @@ const connect = async ({
fence,
api,
setConnecting,
effects,
setError,
fetchFenceStudiesIfNeeded,
addFenceConnection,
Expand All @@ -100,7 +96,6 @@ const connect = async ({
try {
await fenceConnect(api, fence);
const token = await getAccessToken(api, fence);
effects.setIntegrationToken(fence, token);
const details = convertTokenToUser(token);
addFenceConnection(fence, details);
fetchFenceStudiesIfNeeded(api, fence, computeAclsForConnection(details));
Expand Down Expand Up @@ -137,7 +132,6 @@ function Integration(props) {
fence,
logo,
description,
effects,
api,
} = props;

Expand Down Expand Up @@ -165,7 +159,6 @@ function Integration(props) {
connectCbParams: {
fence,
api,
effects,
addFenceConnection,
fetchFenceStudiesIfNeeded,
},
Expand All @@ -175,7 +168,6 @@ function Integration(props) {
disConnectCbParams: {
fence,
api,
effects,
removeFenceConnection,
removeFenceStudies,
},
Expand All @@ -197,15 +189,12 @@ const connector = reduxConnect(null, mapDispatchToProps);

const Enhanced = compose(
connector,
injectState,
withApi,
setPropTypes({
logo: PropTypes.node.isRequired,
description: PropTypes.string.isRequired,
connecting: PropTypes.bool,
fence: PropTypes.string.isRequired,
state: PropTypes.object.isRequired,
effects: PropTypes.object.isRequired,
api: PropTypes.func.isRequired,
addFenceConnection: PropTypes.func,
fetchFenceStudiesIfNeeded: PropTypes.func,
Expand Down
28 changes: 10 additions & 18 deletions src/stateProviders/provideLoggedInUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { mergeIntoState, provideState } from 'freactal';
import jwtDecode from 'jwt-decode';
import get from 'lodash/get';

import { EGO_JWT_KEY, SERVICES } from 'common/constants';
import { CAVATICA, EGO_JWT_KEY } from 'common/constants';
import { INTEGRATION_PREFIX } from 'common/constants';
import ROUTES from 'common/routes';
import { handleJWT, isAdminToken, validateJWT } from 'components/Login/utils';
Expand Down Expand Up @@ -74,12 +74,10 @@ export default provideState({
});

// Get all integration keys from local storage
SERVICES.forEach((service) => {
const storedToken = localStorage.getItem(`integration_${service}`);
if (storedToken) {
state.integrationTokens[service] = storedToken;
}
});
const storedToken = localStorage.getItem(`${INTEGRATION_PREFIX}${CAVATICA}`);
if (storedToken) {
state.integrationTokens[CAVATICA] = storedToken;
}
setEgoTokenCookie(jwt);
};

Expand Down Expand Up @@ -170,26 +168,20 @@ export default provideState({
return { ...state, loggedInUserToken: token, loginProvider: provider };
},
setIntegrationToken: (effects, service, token) => (state) => {
if (SERVICES.includes(service)) {
const tokenKey = `${INTEGRATION_PREFIX}${service}`;
if (CAVATICA === service) {
const tokenKey = `${INTEGRATION_PREFIX}${CAVATICA}`;
if (token) {
localStorage.setItem(tokenKey, token);
state.integrationTokens[service] = token;
state.integrationTokens[CAVATICA] = token;
} else {
localStorage.removeItem(tokenKey);
delete state.integrationTokens[service];
delete state.integrationTokens[CAVATICA];
}
}
return { ...state, integrationTokens: { ...state.integrationTokens } };
},
getIntegrationToken: (effects, service) => () => {
if (SERVICES.includes(service)) {
const tokenKey = `${INTEGRATION_PREFIX}${service}`;
return tokenKey ? localStorage.getItem(tokenKey) : null;
}
},
clearIntegrationTokens: () => (state) => {
SERVICES.forEach((service) => localStorage.removeItem(`${INTEGRATION_PREFIX}${service}`));
localStorage.removeItem(`${INTEGRATION_PREFIX}${CAVATICA}`);
return { ...state, integrationTokens: {} };
},
setIsLoadingUser: (isLoading) => (state) => ({ ...state, isLoadingUser: isLoading }),
Expand Down
46 changes: 14 additions & 32 deletions src/store/actionCreators/Test/fenceConnections.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ import { AnyAction } from 'redux';
import configureMockStore from 'redux-mock-store';
import thunk from 'redux-thunk';

import { DCF, FENCES, GEN3, INTEGRATION_PREFIX } from 'common/constants';
import { DCF, GEN3 } from 'common/constants';
import { getFenceUser } from 'services/fence';
import { DispatchFenceConnections, FenceConnectionsActions } from 'store/fenceConnectionsTypes';
import { makeFakeLocalStorage } from 'utils';

import {
addFenceConnection,
Expand All @@ -29,18 +28,11 @@ const mockStore = configureMockStore(middleware);
jest.mock('services/fence');

describe('Fence Connections actions', () => {
beforeAll(() => {
Object.defineProperty(window, 'localStorage', {
value: makeFakeLocalStorage(),
});
});

beforeEach(() => {
(getFenceUser as jest.Mock).mockReset();
});

afterEach(() => {
window.localStorage.clear();
jest.clearAllMocks();
});

Expand All @@ -61,8 +53,11 @@ describe('Fence Connections actions', () => {
});

it('should manage acls adequately', () => {
// @ts-ignore
expect(concatAllFencesAcls(MOCK_FENCE_CONNECTIONS)).toEqual(['a', 'b', 'c', 'x', 'y', 'z']);
// @ts-ignore
expect(computeAclsForConnection(MOCK_FENCE_CONNECTIONS[GEN3])).toEqual(['a', 'b', 'c']);
// @ts-ignore
expect(computeAclsByFence(MOCK_FENCE_CONNECTIONS)).toEqual({
[GEN3]: ['a', 'b', 'c'],
[DCF]: ['x', 'y', 'z'],
Expand All @@ -75,6 +70,7 @@ describe('Fence Connections actions', () => {
fenceName: GEN3,
connection: MOCK_GEN3_CONNECTION,
};
// @ts-ignore
expect(addFenceConnection(GEN3, MOCK_GEN3_CONNECTION)).toEqual(expectedAction);
});

Expand All @@ -101,30 +97,11 @@ describe('Fence Connections actions', () => {
expect(store.getActions()).toEqual(expectedActions);
});

it('should not fetch fence connections if user has no integration token for particular fence', async () => {
localStorage.removeItem(`${INTEGRATION_PREFIX}${GEN3}`);

const store = mockStore({
fenceConnections: {
fenceConnections: {},
isFetchingAllFenceConnections: false,
},
});

const dispatch: DispatchFenceConnections = store.dispatch;

await dispatch(fetchFencesConnectionsIfNeeded(mockApi, GEN3));

expect(store.getActions()).toEqual(NO_ACTIONS);
});

it(
'should not fetch fence connections if user has' +
' 1) an integration token' +
' 2) already has connection for particular fence',
' already has connection for particular fence',
async () => {
(getFenceUser as jest.Mock).mockImplementation(() => Promise.resolve(MOCK_GEN3_CONNECTION));
localStorage.setItem(`${INTEGRATION_PREFIX}${GEN3}`, 'token');

const store = mockStore({
fenceConnections: {
Expand Down Expand Up @@ -152,18 +129,23 @@ describe('Fence Connections actions', () => {
});
const expectedActions = [
{
type: FenceConnectionsActions.toggleIsFetchingAllFenceConnections,
isLoading: true,
type: 'toggleIsFetchingAllFenceConnections',
},
{
connection: MOCK_GEN3_CONNECTION,
fenceName: GEN3,
type: FenceConnectionsActions.addFenceConnection,
},
{
type: FenceConnectionsActions.toggleIsFetchingAllFenceConnections,
isLoading: false,
type: 'toggleIsFetchingAllFenceConnections',
},
];

const dispatch: DispatchFenceConnections = store.dispatch;

await dispatch(fetchAllFencesConnectionsIfNeeded(mockApi, FENCES));
await dispatch(fetchAllFencesConnectionsIfNeeded(mockApi, [GEN3]));

expect(store.getActions()).toEqual(expectedActions);
});
Expand Down
49 changes: 9 additions & 40 deletions src/store/actionCreators/Test/fenceStudies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ import { AnyAction } from 'redux';
import configureMockStore from 'redux-mock-store';
import thunk from 'redux-thunk';

import { DCF, GEN3, INTEGRATION_PREFIX } from 'common/constants';
import { DCF, GEN3 } from 'common/constants';
import { getAuthStudiesIdAndCount, getStudiesCountByNameAndAcl } from 'services/fenceStudies';
import { DispatchFenceStudies, FenceStudiesActions } from 'store/fenceStudiesTypes';
import { makeFakeLocalStorage } from 'utils';

import {
addFenceStudies,
Expand All @@ -31,19 +30,12 @@ const mockStore = configureMockStore(middleware);
jest.mock('services/fenceStudies');

describe('Fence Studies actions', () => {
beforeAll(() => {
Object.defineProperty(window, 'localStorage', {
value: makeFakeLocalStorage(),
});
});

beforeEach(() => {
(getAuthStudiesIdAndCount as jest.Mock).mockReset();
(getStudiesCountByNameAndAcl as jest.Mock).mockReset();
});

afterEach(() => {
window.localStorage.clear();
jest.clearAllMocks();
});

Expand Down Expand Up @@ -117,12 +109,17 @@ describe('Fence Studies actions', () => {
expect(store.getActions()).toEqual(expectedActions);
});

it('should not fetch fence studies if user has no integration token for particular fence', async () => {
localStorage.removeItem(`${INTEGRATION_PREFIX}${GEN3}`);
it('should fetch fence studies if user has already has connection for particular fence', async () => {
(getAuthStudiesIdAndCount as jest.Mock).mockImplementation(() =>
Promise.resolve(MOCK_STUDIES_IDS_AND_COUNTS),
);
(getStudiesCountByNameAndAcl as jest.Mock).mockImplementation(() =>
Promise.resolve(MOCK_AUTH_STUDIES_FROM_GEN3),
);

const store = mockStore({
fenceStudies: {
fenceStudies: {},
fenceStudies: MOCK_AUTH_STUDIES_WITH_2_FENCES,
isFetchingAllFenceStudies: false,
},
});
Expand All @@ -133,32 +130,4 @@ describe('Fence Studies actions', () => {

expect(store.getActions()).toEqual(NO_ACTIONS);
});

it(
'should fetch fence studies if user has' +
' 1) an integration token' +
' 2) already has connection for particular fence',
async () => {
(getAuthStudiesIdAndCount as jest.Mock).mockImplementation(() =>
Promise.resolve(MOCK_STUDIES_IDS_AND_COUNTS),
);
(getStudiesCountByNameAndAcl as jest.Mock).mockImplementation(() =>
Promise.resolve(MOCK_AUTH_STUDIES_FROM_GEN3),
);
localStorage.setItem(`${INTEGRATION_PREFIX}${GEN3}`, 'token');

const store = mockStore({
fenceStudies: {
fenceStudies: MOCK_AUTH_STUDIES_WITH_2_FENCES,
isFetchingAllFenceStudies: false,
},
});

const dispatch: DispatchFenceStudies = store.dispatch;

await dispatch(fetchFenceStudiesIfNeeded(mockApi, GEN3, { [GEN3]: ['phs'] }));

expect(store.getActions()).toEqual(NO_ACTIONS);
},
);
});
3 changes: 1 addition & 2 deletions src/store/actionCreators/fenceConnections.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import isEmpty from 'lodash/isEmpty';
import { ThunkAction } from 'redux-thunk';

import { hasIntegrationTokenForFence } from 'components/Login/utils';
import { getFenceUser } from 'services/fence';

import { Api } from '../apiTypes';
Expand Down Expand Up @@ -34,7 +33,7 @@ export const toggleIsFetchingAllFenceConnections = (
});

const shouldFetchConnections = (fenceName: FenceName, state: RootState): boolean =>
hasIntegrationTokenForFence(fenceName) && isEmpty(selectFenceConnections(state)[fenceName]);
isEmpty(selectFenceConnections(state)[fenceName]);

export const fetchFencesConnections = (
api: Api,
Expand Down
6 changes: 1 addition & 5 deletions src/store/actionCreators/fenceStudies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import flatMap from 'lodash/flatMap';
import isEmpty from 'lodash/isEmpty';
import { ThunkAction } from 'redux-thunk';

import { hasIntegrationTokenForFence } from 'components/Login/utils';
import { getAuthStudiesIdAndCount, getStudiesCountByNameAndAcl } from 'services/fenceStudies';

import { Api } from '../apiTypes';
Expand Down Expand Up @@ -30,10 +29,7 @@ export const removeFenceStudies = (fenceName: FenceName): FenceStudiesActionType

const shouldFetchFenceStudies = (fenceName: FenceName, state: RootState) => {
const studiesForFence = selectFenceStudies(state)[fenceName];
return (
hasIntegrationTokenForFence(fenceName) &&
(isEmpty(studiesForFence) || isEmpty(studiesForFence.authorizedStudies))
);
return isEmpty(studiesForFence) || isEmpty(studiesForFence.authorizedStudies);
};

export const fetchFenceStudies = (
Expand Down
Loading

0 comments on commit 55dce61

Please sign in to comment.