Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

Commit

Permalink
fix: Include ExportType for export routes (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyen102 authored Mar 2, 2021
1 parent 08ada00 commit bd518a6
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 144 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module.exports = {
'@typescript-eslint/no-useless-constructor': 'error',
'no-empty-function': 'off',
'@typescript-eslint/no-empty-function': 'error',
'import/no-extraneous-dependencies': ['error', {'devDependencies': ['**/*.test.ts']}],
},
settings: {
'import/resolver': {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"eslint-plugin-import": "^2.20.0",
"eslint-plugin-prettier": "^3.1.2",
"jest": "^25.1.0",
"jest-each": "^26.6.2",
"jest-mock-extended": "^1.0.8",
"prettier": "^1.19.1",
"ts-jest": "^25.1.0",
Expand Down
253 changes: 118 additions & 135 deletions src/utilities.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,173 +3,156 @@
* SPDX-License-Identifier: Apache-2.0
*/

import each from 'jest-each';
import { getRequestInformation } from './utilities';
import { ExportType } from './bulkDataAccess';

describe('getRequestInformation', () => {
test('verb: PUT; normal update', async () => {
const results = getRequestInformation('PUT', '/Patient/123');
expect(results).toEqual({ operation: 'update', resourceType: 'Patient', id: '123' });
});
test('verb: PUT; conditional update', async () => {
const results = getRequestInformation('PUT', '/Patient/123?name=john');
expect(results).toEqual({ operation: 'update', resourceType: 'Patient', id: '123' });
});
test('verb: PUT; invalid update', async () => {
const results = getRequestInformation('PUT', 'fake');
expect(results).toEqual({ operation: 'update', resourceType: 'fake' });
});
test('verb: PATCH; normal patch', async () => {
const results = getRequestInformation('PATCH', '/Patient/123');
expect(results).toEqual({ operation: 'patch', resourceType: 'Patient', id: '123' });
});
test('verb: PATCH; conditional patch', async () => {
const results = getRequestInformation('PATCH', '/Patient/123?name=john');
expect(results).toEqual({ operation: 'patch', resourceType: 'Patient', id: '123' });
});
test('verb: PATCH; invalid patch', async () => {
const results = getRequestInformation('PATCH', 'fake');
expect(results).toEqual({ operation: 'patch', resourceType: 'fake' });
});
test('verb: DELETE; normal delete', async () => {
const results = getRequestInformation('DELETE', '/Patient/123');
expect(results).toEqual({ operation: 'delete', resourceType: 'Patient', id: '123' });
});
test('verb: DELETE; conditional delete', async () => {
const results = getRequestInformation('DELETE', '/Patient/123?name=john');
expect(results).toEqual({ operation: 'delete', resourceType: 'Patient', id: '123' });
});
test('verb: DELETE; invalid delete', async () => {
const results = getRequestInformation('DELETE', 'fake');
expect(results).toEqual({ operation: 'delete', resourceType: 'fake' });
});
test('verb: GET; read: metadata', async () => {
const results = getRequestInformation('GET', '/metadata');
expect(results).toEqual({ operation: 'read', resourceType: 'metadata' });
});
test('verb: GET; read: metadata; with search', async () => {
const results = getRequestInformation('GET', '/metadata?mode=full');
expect(results).toEqual({ operation: 'read', resourceType: 'metadata' });
});
test('verb: GET; vread', async () => {
const results = getRequestInformation('GET', '/Patient/123/_history/345');
expect(results).toEqual({ operation: 'vread', resourceType: 'Patient', id: '123', vid: '345' });
});
test('verb: GET; instance-history with query', async () => {
const results = getRequestInformation('GET', '/Patient/123/_history?name=joe');
expect(results).toEqual({ operation: 'history-instance', resourceType: 'Patient', id: '123' });
});
test('verb: GET; instance-history without query', async () => {
const results = getRequestInformation('GET', '/Patient/123/_history');
expect(results).toEqual({ operation: 'history-instance', resourceType: 'Patient', id: '123' });
});
test('verb: GET; type-history with query', async () => {
const results = getRequestInformation('GET', '/Patient/_history?name=joe');
expect(results).toEqual({ operation: 'history-type', resourceType: 'Patient' });
});
test('verb: GET; type-history without query', async () => {
const results = getRequestInformation('GET', '/Patient/_history/');
expect(results).toEqual({ operation: 'history-type', resourceType: 'Patient' });
});
test('verb: GET; history with query', async () => {
const results = getRequestInformation('GET', '/_history?name=joe');
expect(results).toEqual({ operation: 'history-system' });
});
test('verb: GET; history without query', async () => {
const results = getRequestInformation('GET', '_history');
expect(results).toEqual({ operation: 'history-system' });
});
test('verb: GET; read', async () => {
const results = getRequestInformation('GET', 'Patient/123');
expect(results).toEqual({ operation: 'read', resourceType: 'Patient', id: '123' });
});
test('verb: GET; type-search with query', async () => {
const results = getRequestInformation('GET', '/Patient?name=joe');
expect(results).toEqual({ operation: 'search-type', resourceType: 'Patient' });
});
test('verb: GET; type-search without query', async () => {
const results = getRequestInformation('GET', '/Patient');
expect(results).toEqual({ operation: 'search-type', resourceType: 'Patient' });
});
test('verb: GET; search globally with query', async () => {
const results = getRequestInformation('GET', '/?name=joe');
expect(results).toEqual({ operation: 'search-system' });
});
test('verb: GET; search globally without query', async () => {
const results = getRequestInformation('GET', '');
expect(results).toEqual({ operation: 'search-system' });
describe('verb: PUT', () => {
each([
['normal update', '/Patient/123', { operation: 'update', resourceType: 'Patient', id: '123' }],
[
'conditional update',
'/Patient/123?name=john',
{ operation: 'update', resourceType: 'Patient', id: '123' },
],
['invalid update', 'fake', { operation: 'update', resourceType: 'fake' }],
]).test('%s', (testName: string, urlPath: string, expectedResponse: any) => {
const results = getRequestInformation('PUT', urlPath);
expect(results).toEqual(expectedResponse);
});
});
test('verb: POST; search on type', async () => {
const results = getRequestInformation('POST', '/Patient/_search?name=joe');
expect(results).toEqual({ operation: 'search-type', resourceType: 'Patient' });
describe('verb: PATCH', () => {
each([
['normal patch', '/Patient/123', { operation: 'patch', resourceType: 'Patient', id: '123' }],
['conditional patch', '/Patient/123?name=john', { operation: 'patch', resourceType: 'Patient', id: '123' }],
['invalid patch', 'fake', { operation: 'patch', resourceType: 'fake' }],
]).test('%s', (testName: string, urlPath: string, expectedResponse: any) => {
const results = getRequestInformation('PATCH', urlPath);
expect(results).toEqual(expectedResponse);
});
});
test('verb: POST; search globally', async () => {
const results = getRequestInformation('POST', '/_search/');
expect(results).toEqual({ operation: 'search-system' });
describe('verb: DELETE', () => {
each([
['normal delete', '/Patient/123', { operation: 'delete', resourceType: 'Patient', id: '123' }],
[
'conditional delete',
'/Patient/123?name=john',
{ operation: 'delete', resourceType: 'Patient', id: '123' },
],
['invalid delete', 'fake', { operation: 'delete', resourceType: 'fake' }],
]).test('%s', (testName: string, urlPath: string, expectedResponse: any) => {
const results = getRequestInformation('DELETE', urlPath);
expect(results).toEqual(expectedResponse);
});
});
test('verb: POST; batch', async () => {
const results = getRequestInformation('POST', '?format=json');
expect(results).toEqual({ operation: 'transaction' });
describe('verb: GET', () => {
each([
['read: metadata', '/metadata', { operation: 'read', resourceType: 'metadata' }],
['read: metadata; with search', '/metadata?mode=full', { operation: 'read', resourceType: 'metadata' }],
[
'vread',
'/Patient/123/_history/345',
{ operation: 'vread', resourceType: 'Patient', id: '123', vid: '345' },
],
[
'instance-history with query',
'/Patient/123/_history?name=joe',
{ operation: 'history-instance', resourceType: 'Patient', id: '123' },
],
[
'instance-history without query',
'/Patient/123/_history',
{ operation: 'history-instance', resourceType: 'Patient', id: '123' },
],
[
'type-history with query',
'/Patient/_history?name=joe',
{ operation: 'history-type', resourceType: 'Patient' },
],
[
'type-history without query',
'/Patient/_history/',
{ operation: 'history-type', resourceType: 'Patient' },
],
['history with query', '/_history?name=joe', { operation: 'history-system' }],
['history without query', '_history', { operation: 'history-system' }],
['read', 'Patient/123', { operation: 'read', resourceType: 'Patient', id: '123' }],
['type-search with query', '/Patient?name=joe', { operation: 'search-type', resourceType: 'Patient' }],
['type-search without query', '/Patient', { operation: 'search-type', resourceType: 'Patient' }],
['search globally with query', '/?name=joe', { operation: 'search-system' }],
['search globally without query', '', { operation: 'search-system' }],
]).test('%s', (testName: string, urlPath: string, expectedResponse: any) => {
const results = getRequestInformation('GET', urlPath);
expect(results).toEqual(expectedResponse);
});
});
test('verb: POST; create', async () => {
const results = getRequestInformation('POST', 'Patient/?format=json');
expect(results).toEqual({ operation: 'create', resourceType: 'Patient' });
describe('verb: POST', () => {
each([
['search on type', '/Patient/_search?name=joe', { operation: 'search-type', resourceType: 'Patient' }],
['search globally', '/_search/', { operation: 'search-system' }],
['batch', '?format=json', { operation: 'transaction' }],
['create', 'Patient/?format=json', { operation: 'create', resourceType: 'Patient' }],
]).test('%s', (testName: string, urlPath: string, expectedResponse: any) => {
const results = getRequestInformation('POST', urlPath);
expect(results).toEqual(expectedResponse);
});
});
test('verb: FAKE', async () => {
test('verb: FAKE', () => {
expect(() => {
getRequestInformation('FAKE', '/Patient');
}).toThrow(new Error('Unable to parse the http verb'));
});
describe('Export', () => {
describe('initiate-export', () => {
test('system', async () => {
const results = getRequestInformation('GET', '/$export');
each([
['system', '/$export', 'system'],
['patient', '/Patient/$export', 'patient'],
['group', '/Group/1/$export', 'group'],
]).test('%s', (testName: string, urlPath: string, exportType: ExportType) => {
const results = getRequestInformation('GET', urlPath);
expect(results).toEqual({
operation: 'read',
bulkDataAuth: {
operation: 'initiate-export',
exportType: 'system',
exportType,
},
});
});
test('patient', async () => {
const results = getRequestInformation('GET', '/Patient/$export');
});

describe('get-status', () => {
each([
['system', '/$export/a91b2a31-a964-4919-a220-8be73fb053dd', 'system'],
['patient', '/Patient/$export/a91b2a31-a964-4919-a220-8be73fb053dd', 'patient'],
['group', '/Group/1/$export/a91b2a31-a964-4919-a220-8be73fb053dd', 'group'],
]).test('%s', (testName: string, urlPath: string, exportType: ExportType) => {
const results = getRequestInformation('GET', urlPath);
expect(results).toEqual({
operation: 'read',
bulkDataAuth: {
operation: 'initiate-export',
exportType: 'patient',
exportType,
operation: 'get-status-export',
},
});
});
test('group', async () => {
const results = getRequestInformation('GET', '/Group/1/$export');
});

describe('cancel-export', () => {
each([
['system', '/$export/a91b2a31-a964-4919-a220-8be73fb053dd', 'system'],
['patient', '/Patient/$export/a91b2a31-a964-4919-a220-8be73fb053dd', 'patient'],
['group', '/Group/1/$export/a91b2a31-a964-4919-a220-8be73fb053dd', 'group'],
]).test('%s', (testName: string, urlPath: string, exportType: ExportType) => {
const results = getRequestInformation('DELETE', urlPath);
expect(results).toEqual({
operation: 'read',
operation: 'delete',
bulkDataAuth: {
operation: 'initiate-export',
exportType: 'group',
exportType,
operation: 'cancel-export',
},
});
});
});
test('get-status', async () => {
const results = getRequestInformation('GET', '/$export/a91b2a31-a964-4919-a220-8be73fb053dd');
expect(results).toEqual({
operation: 'read',
bulkDataAuth: {
operation: 'get-status-export',
},
});
});

test('cancel-export', async () => {
const results = getRequestInformation('DELETE', '/$export/a91b2a31-a964-4919-a220-8be73fb053dd');
expect(results).toEqual({
operation: 'delete',
bulkDataAuth: {
operation: 'cancel-export',
},
});
});
});
});
23 changes: 15 additions & 8 deletions src/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ function cleanUrlPath(urlPath: string): string {
return path;
}

function getExportType(urlPath: string): ExportType {
let exportType: ExportType = 'system';
if (urlPath.includes('/Patient/')) {
exportType = 'patient';
}
if (urlPath.includes('/Group/')) {
exportType = 'group';
}
return exportType;
}

export function getRequestInformation(
verb: string,
urlPath: string,
Expand Down Expand Up @@ -74,10 +85,12 @@ export function getRequestInformation(
}
case 'DELETE': {
if (exportJobUrlRegExp.test(urlPath)) {
const exportType = getExportType(urlPath);
const operation = 'cancel-export';
return {
operation: 'delete',
bulkDataAuth: {
exportType,
operation,
},
};
Expand All @@ -90,23 +103,17 @@ export function getRequestInformation(
}
case 'GET': {
if (urlPath.includes('$export')) {
const exportType = getExportType(urlPath);
if (exportJobUrlRegExp.test(urlPath)) {
const operation = 'get-status-export';
return {
operation: 'read',
bulkDataAuth: {
exportType,
operation,
},
};
}

let exportType: ExportType = 'system';
if (urlPath.includes('/Patient/')) {
exportType = 'patient';
}
if (urlPath.includes('/Group/')) {
exportType = 'group';
}
return {
operation: 'read',
bulkDataAuth: {
Expand Down
Loading

0 comments on commit bd518a6

Please sign in to comment.