Skip to content

Commit

Permalink
Update API Tests (#264)
Browse files Browse the repository at this point in the history
  • Loading branch information
KjartanE authored Oct 17, 2024
1 parent cf7de56 commit e85b2f0
Show file tree
Hide file tree
Showing 16 changed files with 1,313 additions and 4 deletions.
16 changes: 16 additions & 0 deletions api/src/models/models.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { expect } from 'chai';
import { models } from './models';

describe('models', () => {
it('should have project', () => {
expect(models.project).to.exist;
});

it('should have user', () => {
expect(models.user).to.exist;
});

it('should have gcnotify', () => {
expect(models.gcnotify).to.exist;
});
});
131 changes: 131 additions & 0 deletions api/src/models/report-view.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import { expect } from 'chai';
import { describe } from 'mocha';
import {
GetReportLast,
GetReportLastData,
GetReportPlanData,
GetReportProjectData,
GetReportUserData
} from './report-view';

describe('GetReportProjectData', () => {
describe('No values provided', () => {
let data: GetReportProjectData;

before(() => {
data = new GetReportProjectData(null as unknown as any[]);
});

it('sets reports', function () {
expect(data.published_projects).to.eql(null);
expect(data.draft_projects).to.eql(null);
expect(data.archived_projects).to.eql(null);
});
});

describe('All values provided', () => {
let data: GetReportProjectData;

const reports = { published_projects: 1, draft_projects: 2, archived_projects: 3 };

before(() => {
data = new GetReportProjectData(reports);
});

it('sets reports', function () {
expect(data).to.eql(reports);
});
});
});

describe('GetReportPlanData', () => {
describe('No values provided', () => {
let data: GetReportPlanData;

before(() => {
data = new GetReportPlanData(null as unknown as any[]);
});

it('sets reports', function () {
expect(data.published_plans).to.eql(null);
expect(data.draft_plans).to.eql(null);
expect(data.archived_plans).to.eql(null);
});
});

describe('All values provided', () => {
let data: GetReportPlanData;

const reports = { published_plans: 1, draft_plans: 2, archived_plans: 3 };

before(() => {
data = new GetReportPlanData(reports);
});

it('sets reports', function () {
expect(data).to.eql(reports);
});
});
});

describe('GetReportUserData', () => {
describe('No values provided', () => {
let data: GetReportUserData;

before(() => {
data = new GetReportUserData(null as unknown as any[]);
});

it('sets reports', function () {
expect(data.admins).to.eql(null);
expect(data.maintainers).to.eql(null);
expect(data.creators).to.eql(null);
});
});

describe('All values provided', () => {
let data: GetReportUserData;

const reports = { admins: 1, maintainers: 2, creators: 3 };

before(() => {
data = new GetReportUserData(reports);
});

it('sets reports', function () {
expect(data).to.eql(reports);
});
});
});

describe('GetReportLastData', () => {
describe('No values provided', () => {
let data: GetReportLastData;

before(() => {
data = { project: new GetReportLast(null), plan: new GetReportLast(null) };
});

it('sets reports', function () {
expect(data.project).to.eql({ id: null, name: null, datetime: null });
expect(data.plan).to.eql({ id: null, name: null, datetime: null });
});
});

describe('All values provided', () => {
let data: GetReportLastData;

const reports = {
project: { id: 1, name: 'name 1', datetime: 'datetime 1' },
plan: { id: 2, name: 'name 2', datetime: 'datetime 2' }
};

before(() => {
data = { project: new GetReportLast(reports.project), plan: new GetReportLast(reports.plan) };
});

it('sets reports', function () {
expect(data).to.eql(reports);
});
});
});
2 changes: 1 addition & 1 deletion api/src/models/report-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export interface GetReportLastData {
plan: GetReportLast;
}

class GetReportLast {
export class GetReportLast {
id: number | null;
name: string | null;
datetime: string | null;
Expand Down
48 changes: 47 additions & 1 deletion api/src/paths/codes/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,16 @@ describe('codes', () => {
const dbConnectionObj = getMockDBConnection();

const sampleReq = {
keycloak_token: {}
keycloak_token: {},
body: {
codeType: 'first_nations',
codeData: {
id: 1,
name: 'management action',
value: 'management action',
fs_id: 1
}
}
} as any;

let actualResult = {
Expand Down Expand Up @@ -80,4 +89,41 @@ describe('codes', () => {
}
});
});

describe('updateCode', () => {
afterEach(() => {
sinon.restore();
});

it('should throw a 500 error when fails to update code', async () => {
const dbConnection = getMockDBConnection();
sinon.stub(db, 'getDBConnection').returns(dbConnection);
sinon.stub(CodeService.prototype, 'updateCode').resolves(null);

try {
const result = codes.updateCode();

await result(sampleReq, null as unknown as any, null as unknown as any);
expect.fail();
} catch (actualError) {
expect((actualError as HTTPError).message).to.equal('Failed to update code');
}
});

it('should return the updated code on success', async () => {
const dbConnection = getMockDBConnection();
sinon.stub(db, 'getDBConnection').returns(dbConnection);

sinon.stub(CodeService.prototype, 'updateCode').resolves({
id: 1,
name: 'management action type'
} as any);

const result = codes.updateCode();

await result(sampleReq, sampleRes as any, null as unknown as any);

expect(actualResult).to.eql({ success: true });
});
});
});
172 changes: 172 additions & 0 deletions api/src/paths/draft/{draftId}/attachments/upload.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
import chai, { expect } from 'chai';
import { describe } from 'mocha';
import sinon from 'sinon';
import sinonChai from 'sinon-chai';
import { getMockDBConnection } from '../../../../__mocks__/db';
import * as db from '../../../../database/db';
import { HTTPError } from '../../../../errors/custom-error';
import { DraftRepository } from '../../../../repositories/draft-repository';
import * as file_utils from '../../../../utils/file-utils';
import * as upload from './upload';

chai.use(sinonChai);

describe('uploadMedia', () => {
afterEach(() => {
sinon.restore();
});

const dbConnectionObj = getMockDBConnection();

const mockReq = {
keycloak_token: {},
params: {
draftId: 1,
attachmentId: 2
},
files: [
{
fieldname: 'media',
originalname: 'test.txt',
encoding: '7bit',
mimetype: 'text/plain',
size: 340
}
],
body: {
media: 'test.txt',
fileType: 'draft'
}
} as any;

let actualResult: any = null;

const mockRes = {
status: () => {
return {
json: (result: any) => {
actualResult = result;
}
};
}
} as any;

it('should throw an error when draftId is missing', async () => {
sinon.stub(db, 'getDBConnection').returns(dbConnectionObj);

try {
const result = upload.uploadDraftAttachment();

await result(
{ ...mockReq, params: { ...mockReq.params, draftId: null } },
null as unknown as any,
null as unknown as any
);
expect.fail();
} catch (actualError) {
expect((actualError as HTTPError).status).to.equal(400);
expect((actualError as HTTPError).message).to.equal('Missing draftId');
}
});

it('should throw an error when files are missing', async () => {
sinon.stub(db, 'getDBConnection').returns(dbConnectionObj);

try {
const result = upload.uploadDraftAttachment();

await result({ ...mockReq, files: [] }, null as unknown as any, null as unknown as any);
expect.fail();
} catch (actualError) {
expect((actualError as HTTPError).status).to.equal(400);
expect((actualError as HTTPError).message).to.equal('Missing upload data');
}
});

it('should throw an error when body is missing', async () => {
sinon.stub(db, 'getDBConnection').returns(dbConnectionObj);

try {
const result = upload.uploadDraftAttachment();

await result({ ...mockReq, body: null }, null as unknown as any, null as unknown as any);
expect.fail();
} catch (actualError) {
expect((actualError as HTTPError).status).to.equal(400);
expect((actualError as HTTPError).message).to.equal('Missing request body');
}
});

it('should throw a 400 error when file contains malicious content', async () => {
sinon.stub(db, 'getDBConnection').returns({
...dbConnectionObj,
systemUserId: () => {
return 20;
}
});

sinon.stub(file_utils, 'scanFileForVirus').resolves(false);

try {
const result = upload.uploadDraftAttachment();

await result(mockReq, null as unknown as any, null as unknown as any);
expect.fail();
} catch (actualError) {
expect((actualError as HTTPError).status).to.equal(400);
expect((actualError as HTTPError).message).to.equal('Malicious content detected, upload cancelled');
}
});

it('should return id and revision_count on success (with username and email) with valid parameters', async () => {
sinon.stub(db, 'getDBConnection').returns({
...dbConnectionObj,
systemUserId: () => {
return 20;
}
});

sinon.stub(file_utils, 'scanFileForVirus').resolves(true);
sinon
.stub(DraftRepository.prototype, 'getDraft')
.resolves({ id: 1, is_project: false, name: 'Name', data: { project: {} } });
sinon.stub(file_utils, 'uploadFileToS3').resolves({ Key: '1/1/test.txt' } as any);
sinon.stub(DraftRepository.prototype, 'updateDraft').resolves({
id: 1,
is_project: false,
name: 'Name',
update_date: '2021-08-31T00:00:00.000Z',
create_date: '2021-08-31T00:00:00.000Z'
});

const result = upload.uploadDraftAttachment();

await result(mockReq, mockRes as any, null as unknown as any);

expect(actualResult).to.eql(undefined);
});

it('catches and returns a 500 error', async () => {
sinon.stub(db, 'getDBConnection').returns({
...dbConnectionObj,
systemUserId: () => {
return 20;
}
});

sinon.stub(file_utils, 'scanFileForVirus').resolves(true);
sinon
.stub(DraftRepository.prototype, 'getDraft')
.resolves({ id: 1, is_project: false, name: 'Name', data: { project: {} } });
sinon.stub(file_utils, 'uploadFileToS3').throws(new Error('An error occurred'));

try {
const result = upload.uploadDraftAttachment();

await result(mockReq, mockRes as any, null as unknown as any);
expect.fail();
} catch (actualError) {
expect((actualError as HTTPError).message).to.equal('An error occurred');
}
});
});
Loading

0 comments on commit e85b2f0

Please sign in to comment.