-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
118 changed files
with
5,783 additions
and
3,682 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
172 changes: 172 additions & 0 deletions
172
api/src/paths/draft/{draftId}/attachments/upload.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
}); | ||
}); |
Oops, something went wrong.