Skip to content

Commit

Permalink
Merge pull request #956 from codaco/fix/decode-url
Browse files Browse the repository at this point in the history
Decode url escape sequences when importing protocol.
  • Loading branch information
jthrilly authored Aug 22, 2019
2 parents 1253350 + 004736c commit 296571d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 156 deletions.
164 changes: 9 additions & 155 deletions src/ducks/modules/__tests__/importProtocol.test.js
Original file line number Diff line number Diff line change
@@ -1,165 +1,19 @@
/* eslint-env jest */
import reducer, { initialState } from '../importProtocol';
import reducer, { initialState, helpers } from '../importProtocol';

describe('importProtocol', () => {
describe('helpers', () => {
it('filenameFromURI should correctly extract protocol name', () => {
const exampleUrl = 'https://documentation.networkcanvas.com/protocols/Public%20Health%20Protocol.netcanvas?foo=bar#bazz';
expect(helpers.filenameFromURI(exampleUrl)).toEqual('Public Health Protocol.netcanvas');
});
});

describe('protocol module', () => {
describe('reducer', () => {
it('should return the initial state', () => {
expect(
reducer(undefined, {}),
).toEqual(initialState);
});

// TODO: rewrite these to account for the new import thunk flow.
// it('setProtocol() sets protocol on state and changes the loaded state', () => {
// expect(
// reducer(
// initialState,
// actionCreators.setProtocol(
// '/tmp/foo/mockPath.protocol',
// { stages: [{ foo: 'bar' }] },
// ),
// ),
// ).toEqual({
// ...initialState,
// path: '/tmp/foo/mockPath.protocol',
// stages: [{ foo: 'bar' }],
// isLoaded: true,
// isLoading: false,
// });
// });

// it('downloadProtocol()', () => {
// expect(
// reducer(
// initialState,
// actionCreators.downloadProtocol(
// 'https://tmp/foo/mockPath.protocol',
// ),
// ),
// ).toEqual({
// ...initialState,
// isLoaded: false,
// isLoading: true,
// });
// });

// it('importProtocol()', () => {
// expect(
// reducer(
// initialState,
// actionCreators.importProtocol(
// '/tmp/foo/mockPath.protocol',
// ),
// ),
// ).toEqual({
// ...initialState,
// isLoaded: false,
// isLoading: true,
// });
// });

// it('loadProtocol()', () => {
// expect(
// reducer(
// initialState,
// actionCreators.loadProtocol(
// '/tmp/foo/mockPath.protocol',
// ),
// ),
// ).toEqual({
// ...initialState,
// isLoaded: false,
// isLoading: true,
// type: 'download',
// });
// });

// it('loadFactoryProtocol()', () => {
// expect(
// reducer(
// initialState,
// actionCreators.loadFactoryProtocol(
// '/tmp/foo/mockPath.protocol',
// ),
// ),
// ).toEqual({
// ...initialState,
// isLoaded: false,
// isLoading: true,
// });
// });

// it('should clear protocol state when session ends', () => {
// const newState = reducer(
// initialState,
// actionCreators.setProtocol(
// '/tmp/foo/mockPath.protocol',
// { stages: [{ foo: 'bar' }] },
// ),
// );
// expect(
// reducer(newState, { type: SessionActionTypes.END_SESSION }),
// ).toEqual(initialState);
// });
});

// describe('epics', () => {
// beforeAll(() => {
// getEnvironment.mockReturnValue(environments.ELECTRON);
// });

// it('downloadProtocolEpic', () => {
// const action$ = ActionsObservable.of(
// actionCreators.downloadProtocol('https://tmp/foo/mockPath.protocol'),
// );

// const expectedActions =
// [actionCreators.importProtocol('/downloaded/protocol/to/temp/path')];
// return epics(action$).toArray().toPromise().then((result) => {
// expect(result).toEqual(expectedActions);
// });
// });

// it('importProtocolEpic', () => {
// const action$ = ActionsObservable.of(
// actionCreators.importProtocol('/downloaded/protocol/to/temp/path'),
// );

// const expectedActions = [actionCreators.loadProtocol('/app/data/protocol/path')];
// return epics(action$).toArray().toPromise().then((result) => {
// expect(result.map(element => omit(element, 'sessionId'))).toEqual(
// expectedActions.map(action => omit(action, 'sessionId')));
// });
// });

// it('loadProtocolEpic', () => {
// const action$ = ActionsObservable.of(
// actionCreators.loadProtocol('/app/data/protocol/path'),
// );

// const expectedAction = actionCreators.setProtocol(
// '/app/data/protocol/path',
// { fake: { installedProtocols: { json: true } } },
// false,
// );
// return epics(action$).toArray().toPromise().then((result) => {
// expect(result).toContainEqual(expectedAction);
// });
// });

// it('loadFactoryProtocolEpic', () => {
// const action$ = ActionsObservable.of(
// actionCreators.loadFactoryProtocol('factory_protocol_name'),
// );

// const expectedAction = actionCreators.setProtocol(
// 'factory_protocol_name',
// { fake: { factory: { installedProtocols: { json: true } } } },
// true,
// );
// return epics(action$).toArray().toPromise().then((result) => {
// expect(result).toContainEqual(expectedAction);
// });
// });
// });
});
7 changes: 6 additions & 1 deletion src/ducks/modules/importProtocol.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ const cleanUpProtocol = (uid) => {

const cancelledImport = () => Promise.reject(new CancellationError('Import cancelled.'));

const filenameFromURI = uri => uri.split('/').pop().split('#')[0].split('?')[0];
const filenameFromURI = uri =>
decodeURIComponent(uri.split('/').pop().split('#')[0].split('?')[0]);

const filenameFromPath = path => path.split(/.*[/|\\]/)[1];

Expand Down Expand Up @@ -228,6 +229,9 @@ const importProtocolFromFile = filePath => (dispatch, getState) => {
);
};

const helpers = {
filenameFromURI,
};

const actionCreators = {
importProtocolFromURI,
Expand Down Expand Up @@ -256,4 +260,5 @@ const actionTypes = {
export {
actionCreators,
actionTypes,
helpers,
};

0 comments on commit 296571d

Please sign in to comment.