Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test for project creation #436

Merged
merged 3 commits into from
Aug 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 115 additions & 0 deletions api/src/paths/project.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import chai, { expect } from 'chai';
import { describe } from 'mocha';
import sinon from 'sinon';
import sinonChai from 'sinon-chai';
import * as project from './project';
import * as db from '../database/db';
import * as project_create_queries from '../queries/project/project-create-queries';

chai.use(sinonChai);

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

const dbConnectionObj = {
systemUserId: () => {
return null;
},
open: async () => {
// do nothing
},
release: () => {
// do nothing
},
commit: async () => {
// do nothing
},
rollback: async () => {
// do nothing
},
query: async () => {
// do nothing
}
};

const sampleReq = {
keycloak_token: {},
body1: {
coordinator: {
first_name: 'John',
last_name: 'Smith',
email_address: '[email protected]',
coordinator_agency: 'A Rocha Canada',
share_contact_details: 'false'
},
permit: { permits: [], existing_permits: [] },
project: {
project_name: 'Tatyana Douglas',
project_type: 2,
project_activities: [],
start_date: '1900-01-01',
end_date: ''
},
objectives: { objectives: 'an objective', caveats: '' },
location: {
regions: ['West Coast'],
location_description: '',
geometry: [
{ type: 'Feature', properties: {}, geometry: { type: 'Point', coordinates: [-124.716797, 52.88902] } }
]
},
iucn: { classificationDetails: [] },
funding: { funding_sources: [] },
partnerships: { indigenous_partnerships: [], stakeholder_partnerships: [] }
},
params: {
projectId: 1
}
} as any;

afterEach(() => {
sinon.restore();
});

it('should throw a 400 error when no request body present', async () => {
sinon.stub(db, 'getDBConnection').returns({
...dbConnectionObj,
systemUserId: () => {
return 20;
}
});

try {
const result = project.createProject();

await result({ ...sampleReq, body: null }, (null as unknown) as any, (null as unknown) as any);
expect.fail();
} catch (actualError) {
expect(actualError.status).to.equal(400);
expect(actualError.message).to.equal('Failed to insert project general information data');
}
});

it('should throw a 400 error when no sql statement returned for postProjectSQLStatement', async () => {
sinon.stub(db, 'getDBConnection').returns({
...dbConnectionObj,
systemUserId: () => {
return 20;
}
});

sinon.stub(project_create_queries, 'postProjectSQL').returns(null);

try {
const result = project.createProject();

await result(sampleReq, (null as unknown) as any, (null as unknown) as any);
expect.fail();
} catch (actualError) {
expect(actualError.status).to.equal(400);
expect(actualError.message).to.equal('Failed to build SQL insert statement');
}
});
});
2 changes: 1 addition & 1 deletion api/src/paths/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ POST.apiDoc = {
*
* @returns {RequestHandler}
*/
function createProject(): RequestHandler {
export function createProject(): RequestHandler {
return async (req, res) => {
const connection = getDBConnection(req['keycloak_token']);

Expand Down