diff --git a/api/src/paths/project.test.ts b/api/src/paths/project.test.ts new file mode 100644 index 0000000000..e50b2cb974 --- /dev/null +++ b/api/src/paths/project.test.ts @@ -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: 'a@b.com', + 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'); + } + }); +}); diff --git a/api/src/paths/project.ts b/api/src/paths/project.ts index 45c167efee..524fbba085 100644 --- a/api/src/paths/project.ts +++ b/api/src/paths/project.ts @@ -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']);