Skip to content

Commit

Permalink
up the test coverage (and fix a bug!)
Browse files Browse the repository at this point in the history
Signed-off-by: Jason Sherman <[email protected]>
  • Loading branch information
usingtechnology committed Aug 19, 2024
1 parent f516e86 commit 6ef612c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/src/forms/proxy/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const service = {
if (submissionId) {
const meta = await SubmissionMetadata.query().where('submissionId', submissionId).first();
if (meta) {
formId = meta.submissionId;
formId = meta.formId;
versionId = meta.formVersionId;
submissionId = meta.submissionId;
}
Expand Down
47 changes: 46 additions & 1 deletion app/tests/unit/forms/proxy/service.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const { encryptionService, ENCRYPTION_ALGORITHMS } = require('../../../../src/co
const service = require('../../../../src/forms/proxy/service');
const { ExternalAPI } = require('../../../../src/forms/common/models');

jest.mock('../../../../src/forms/common/models/views/submissionMetadata', () => MockModel);

const goodPayload = {
formId: '123',
submissionId: null,
Expand Down Expand Up @@ -60,6 +62,41 @@ afterEach(() => {
});

describe('Proxy Service', () => {
describe('_getIds', () => {
beforeEach(() => {
MockModel.mockReset();
});
afterEach(() => {
jest.restoreAllMocks();
});
it('should return all ids', async () => {
const payload = { formId: null, versionId: null, submissionId: '1' };
const meta = { formId: '2', formVersionId: '3', submissionId: '1' }; // meta query result
const ids = { formId: '2', versionId: '3', submissionId: '1' }; // _getIds result
MockModel.mockResolvedValue(meta);
const result = await service._getIds(payload);
await expect(result).toStrictEqual(ids);
expect(MockModel.query).toBeCalledTimes(1); // submission id means we query
expect(MockModel.first).toBeCalledTimes(1);
});
it('should return form and version id', async () => {
const payload = { formId: '2', versionId: '3', submissionId: null };
const ids = { formId: '2', versionId: '3', submissionId: null }; // _getIds result
const result = await service._getIds(payload);
await expect(result).toStrictEqual(ids);
expect(MockModel.query).toBeCalledTimes(0); // no submission id, no query
});
it('should return form and version id when submission id not found', async () => {
const payload = { formId: null, versionId: null, submissionId: '1' };
const meta = null; // meta query result
const ids = { formId: null, versionId: null, submissionId: '1' }; // _getIds result
MockModel.mockResolvedValue(meta);
const result = await service._getIds(payload);
await expect(result).toStrictEqual(ids);
expect(MockModel.query).toBeCalledTimes(1); // submission id means we query
expect(MockModel.first).toBeCalledTimes(1);
});
});
describe('generateProxyHeaders', () => {
it('should throw error with no payload', async () => {
await expect(service.generateProxyHeaders(undefined, goodCurrentUser, token)).rejects.toThrow();
Expand Down Expand Up @@ -107,7 +144,9 @@ describe('Proxy Service', () => {
it('should throw error with no headers', async () => {
await expect(service.readProxyHeaders(undefined)).rejects.toThrow();
});

it('should throw error with bad headers', async () => {
await expect(service.readProxyHeaders('string-not-object')).rejects.toThrow();
});
it('should throw error with wrong header name', async () => {
await expect(service.readProxyHeaders({ 'X-CHEFS-WRONG_HEADER_NAME': 'headers' })).rejects.toThrow();
});
Expand Down Expand Up @@ -154,6 +193,12 @@ describe('Proxy Service', () => {
await expect(service.getExternalAPI(headers, goodProxyHeaderInfo)).rejects.toThrow();
});

it('should throw error with bad header', async () => {
// set the external api name...
const headers = 'string-not-object';
await expect(service.getExternalAPI(headers, goodProxyHeaderInfo)).rejects.toThrow();
});

it('should throw error with no proxy header info', async () => {
// set the external api name...
const headers = { 'X-CHEFS-EXTERNAL-API-NAME': 'testapi' };
Expand Down

0 comments on commit 6ef612c

Please sign in to comment.