Skip to content

Commit

Permalink
add reducers and actions for auth2 probe responses
Browse files Browse the repository at this point in the history
- correct a spec name in accessTokens.test.js
- stop refetching accessTokens if state has an ok true/false attribute
- #3789
  • Loading branch information
barmintor committed Oct 25, 2023
1 parent 477a7e0 commit bc166cb
Show file tree
Hide file tree
Showing 10 changed files with 365 additions and 3 deletions.
52 changes: 52 additions & 0 deletions __tests__/src/actions/probeResponse.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { JSONLDResource } from 'manifesto.js';
import * as actions from '../../../src/state/actions';
import ActionTypes from '../../../src/state/actions/action-types';

describe('probeResponse actions', () => {
describe('requestProbeResponse', () => {
it('requests a probeResponse from given a url', () => {
const id = 'abc123';
const expectedAction = {
probeId: id,
type: ActionTypes.REQUEST_PROBE_RESPONSE,
};
expect(actions.requestProbeResponse(id)).toEqual(expectedAction);
});
});
describe('receiveProbeResponse', () => {
it('receives a probeResponse', () => {
const id = 'abc123';
const json = {
content: 'probe service request',
id,
};
const expectedAction = {
probeId: id,
probeJson: json,
type: ActionTypes.RECEIVE_PROBE_RESPONSE,
};
expect(actions.receiveProbeResponse(id, json)).toEqual(expectedAction);
});
});
describe('fetchProbeResponse', () => {
describe('success response', () => {
it('dispatches the REQUEST_PROBE_RESPONSE action', () => {
const resource = new JSONLDResource({ services: [{ id: 'someUrl', type: 'AuthProbeService2' }] });
expect(actions.fetchProbeResponse({ resource })).toEqual({
probeId: 'someUrl',
resource,
type: 'mirador/REQUEST_PROBE_RESPONSE',
});
});
});
});
describe('removeProbeResponse', () => {
it('removes an existing probeResponse', () => {
const expectedAction = {
probeId: 'foo',
type: ActionTypes.REMOVE_PROBE_RESPONSE,
};
expect(actions.removeProbeResponse('foo')).toEqual(expectedAction);
});
});
});
47 changes: 46 additions & 1 deletion __tests__/src/reducers/accessTokens.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('access tokens response reducer', () => {
},
});
});
it('should handle RECEIVE_INFO_RESPONSE_FAILURE', () => {
it('should handle RECEIVE_ACCESS_TOKEN_FAILURE', () => {
expect(accessTokensReducer(
{
abc123: {
Expand Down Expand Up @@ -73,4 +73,49 @@ describe('access tokens response reducer', () => {
})).toEqual({});
});
});
it('should handle RECEIVE_INFO_RESPONSE', () => {
expect(accessTokensReducer(
{
efg456: {
id: 'efg456',
isFetching: true,
},
},
{
infoId: 'abc123',
infoJson: {
'@type': 'sc:Manifest',
content: 'lots of canvases and metadata and such',
id: 'abc123',
},
tokenServiceId: 'efg456',
type: ActionTypes.RECEIVE_INFO_RESPONSE,
},
)).toMatchObject({
efg456: {
id: 'efg456',
success: true,
},
});
});
it('should handle RECEIVE_PROBE_RESPONSE', () => {
expect(accessTokensReducer(
{
efg456: {
id: 'efg456',
isFetching: true,
},
},
{
infoId: 'abc123',
tokenServiceId: 'efg456',
type: ActionTypes.RECEIVE_PROBE_RESPONSE,
},
)).toMatchObject({
efg456: {
id: 'efg456',
success: true,
},
});
});
});
95 changes: 95 additions & 0 deletions __tests__/src/reducers/probeResponse.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { probeResponsesReducer } from '../../../src/state/reducers/probeResponses';
import ActionTypes from '../../../src/state/actions/action-types';

describe('probe response reducer', () => {
it('should handle REQUEST_PROBE_RESPONSE', () => {
expect(probeResponsesReducer({}, {
probeId: 'abc123',
type: ActionTypes.REQUEST_PROBE_RESPONSE,
})).toEqual({
abc123: {
id: 'abc123',
isFetching: true,
},
});
});
it('should handle RECEIVE_PROBE_RESPONSE', () => {
expect(probeResponsesReducer(
{
abc123: {
id: 'abc123',
isFetching: true,
},
},
{
probeId: 'abc123',
probeJson: {
'@type': 'sc:Manifest',
content: 'lots of canvases and metadata and such',
id: 'abc123',
},
tokenServiceId: 'efg456',
type: ActionTypes.RECEIVE_PROBE_RESPONSE,
},
)).toMatchObject({
abc123: {
id: 'abc123',
isFetching: false,
json: {},
tokenServiceId: 'efg456',
},
});
});
it('should handle RECEIVE_PROBE_RESPONSE_FAILURE', () => {
expect(probeResponsesReducer(
{
abc123: {
id: 'abc123',
isFetching: true,
},
},
{
error: "This institution didn't enable CORS.",
probeId: 'abc123',
tokenServiceId: 'efg456',
type: ActionTypes.RECEIVE_PROBE_RESPONSE_FAILURE,
},
)).toEqual({
abc123: {
error: "This institution didn't enable CORS.",
id: 'abc123',
isFetching: false,
tokenServiceId: 'efg456',
},
});
});
it('should handle REMOVE_PROBE_RESPONSE', () => {
expect(probeResponsesReducer(
{
abc123: {
id: 'abc123',
stuff: 'foo',
},
def456: {
id: 'def456',
stuff: 'foo',
},
},
{
probeId: 'abc123',
type: ActionTypes.REMOVE_PROBE_RESPONSE,
},
)).toEqual({
def456: {
id: 'def456',
stuff: 'foo',
},
});
});
it('should handle IMPORT_MIRADOR_STATE setting to clean state', () => {
expect(probeResponsesReducer({}, {
state: { probeResponses: { new: 'stuff' } },
type: ActionTypes.IMPORT_MIRADOR_STATE,
})).toEqual({});
});
});
6 changes: 6 additions & 0 deletions src/state/actions/action-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ const ActionTypes = {
RECEIVE_DEGRADED_INFO_RESPONSE: 'mirador/RECEIVE_DEGRADED_INFO_RESPONSE',
RECEIVE_INFO_RESPONSE_FAILURE: 'mirador/RECEIVE_INFO_RESPONSE_FAILURE',
REMOVE_INFO_RESPONSE: 'mirador/REMOVE_INFO_RESPONSE',

REQUEST_PROBE_RESPONSE: 'mirador/REQUEST_PROBE_RESPONSE',
RECEIVE_PROBE_RESPONSE: 'mirador/RECEIVE_PROBE_RESPONSE',
RECEIVE_DEGRADED_PROBE_RESPONSE: 'mirador/RECEIVE_DEGRADED_PROBE_RESPONSE',
RECEIVE_PROBE_RESPONSE_FAILURE: 'mirador/RECEIVE_PROBE_RESPONSE_FAILURE',
REMOVE_PROBE_RESPONSE: 'mirador/REMOVE_PROBE_RESPONSE',
UPDATE_WORKSPACE_MOSAIC_LAYOUT: 'mirador/UPDATE_WORKSPACE_MOSAIC_LAYOUT',
UPDATE_VIEWPORT: 'mirador/UPDATE_VIEWPORT',
UPDATE_ELASTIC_WINDOW_LAYOUT: 'mirador/UPDATE_ELASTIC_WINDOW_LAYOUT',
Expand Down
1 change: 1 addition & 0 deletions src/state/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ export * from './elasticLayout';
export * from './search';
export * from './layers';
export * from './catalog';
export * from './probeResponse';
90 changes: 90 additions & 0 deletions src/state/actions/probeResponse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import ActionTypes from './action-types';
import { getProbeService } from '../../lib/getServices';

/**
* requestProbeResponse - action creator
*
* @param {String} infoId
* @memberof ActionCreators
*/
export function requestProbeResponse(probeId, resource, windowId) {
return {
probeId,
resource,
type: ActionTypes.REQUEST_PROBE_RESPONSE,
windowId,
};
}

/**
* receiveProbeResponse - action creator
*
* @param {String} infoId
* @param {Object} manifestJson
* @memberof ActionCreators
*/
export function receiveProbeResponse(probeId, probeJson, ok, tokenServiceId) {
return {
ok,
probeId,
probeJson,
tokenServiceId,
type: ActionTypes.RECEIVE_PROBE_RESPONSE,
};
}

/**
* receiveDegradedProbeResponse - action creator
*
* @param {String} infoId
* @param {Object} manifestJson
* @memberof ActionCreators
*/
export function receiveDegradedProbeResponse(probeId, probeJson, ok, tokenServiceId, windowId) {
return {
ok,
probeId,
probeJson,
tokenServiceId,
type: ActionTypes.RECEIVE_DEGRADED_PROBE_RESPONSE,
windowId,
};
}

/**
* receiveProbeResponseFailure - action creator
*
* @param {String} infoId
* @param {String} error
* @memberof ActionCreators
*/
export function receiveProbeResponseFailure(probeId, error, tokenServiceId) {
return {
error,
probeId,
tokenServiceId,
type: ActionTypes.RECEIVE_PROBE_RESPONSE_FAILURE,
};
}

/**
* fetchProbeResponse - action creator
*
* @param {String} infoId
* @memberof ActionCreators
*/
export function fetchProbeResponse({ resource, resourceId, windowId }) {
const probeService = resource && getProbeService(resource);
const probeId = (resourceId || probeService.id);
return requestProbeResponse(probeId, resource, windowId);
}

/**
* removeProbeResponse - action creator
*
* @param {String} probeId
* @memberof ActionCreators
*/
export function removeProbeResponse(probeId) {
return { probeId, type: ActionTypes.REMOVE_PROBE_RESPONSE };
}
15 changes: 13 additions & 2 deletions src/state/reducers/accessTokens.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export function accessTokensReducer(state = {}, action) {
[action.tokenServiceId]: {
authId: action.id,
id: action.tokenServiceId,
isFetching: true,
isFetching: !Object.hasOwn(action, 'ok'), // do not attempt to refetch resolved actions that were rejected
},
};
case ActionTypes.REQUEST_ACCESS_TOKEN:
Expand Down Expand Up @@ -44,7 +44,18 @@ export function accessTokensReducer(state = {}, action) {
return omit(state, action.tokenServiceId);
case ActionTypes.RECEIVE_INFO_RESPONSE:
if (!action.tokenServiceId) return state;
if (state[action.tokenServiceId].success) return state;
if (state[action.tokenServiceId]?.success) return state;

return {
...state,
[action.tokenServiceId]: {
...state[action.tokenServiceId],
success: true,
},
};
case ActionTypes.RECEIVE_PROBE_RESPONSE:
if (!action.tokenServiceId) return state;
if (state[action.tokenServiceId]?.success) return state;

return {
...state,
Expand Down
1 change: 1 addition & 0 deletions src/state/reducers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ export * from './elasticLayout';
export * from './search';
export * from './layers';
export * from './catalog';
export * from './probeResponses';
Loading

0 comments on commit bc166cb

Please sign in to comment.