Skip to content

Commit

Permalink
conflicts in imports
Browse files Browse the repository at this point in the history
  • Loading branch information
anissa-agahchen committed May 26, 2021
2 parents d729a83 + be1abb4 commit e356fd3
Show file tree
Hide file tree
Showing 5 changed files with 251 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
import chai, { expect } from 'chai';
import { describe } from 'mocha';
import sinon from 'sinon';
import sinonChai from 'sinon-chai';
import * as get_signed_url from './getSignedUrl';
import * as db from '../../../../../database/db';
import * as project_attachments_queries from '../../../../../queries/project/project-attachments-queries';
import SQL from 'sql-template-strings';
import * as file_utils from '../../../../../utils/file-utils';

chai.use(sinonChai);

describe('getSingleAttachmentURL', () => {
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: {},
params: {
projectId: 1,
attachmentId: 2
}
} as any;

let actualResult: any = null;

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

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

try {
const result = get_signed_url.getSingleAttachmentURL();

await result(
{ ...sampleReq, params: { ...sampleReq.params, projectId: 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('Missing required path param `projectId`');
}
});

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

try {
const result = get_signed_url.getSingleAttachmentURL();

await result(
{ ...sampleReq, params: { ...sampleReq.params, attachmentId: 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('Missing required path param `attachmentId`');
}
});

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

sinon.stub(project_attachments_queries, 'getProjectAttachmentS3KeySQL').returns(null);

try {
const result = get_signed_url.getSingleAttachmentURL();

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 get statement');
}
});

it('should return null when getting signed url from S3 fails', async () => {
const mockQuery = sinon.stub();

mockQuery.resolves({ rows: [{ key: 's3Key' }] });

sinon.stub(db, 'getDBConnection').returns({
...dbConnectionObj,
systemUserId: () => {
return 20;
},
query: mockQuery
});

sinon.stub(project_attachments_queries, 'getProjectAttachmentS3KeySQL').returns(SQL`some query`);
sinon.stub(file_utils, 'getS3SignedURL').resolves(null);

const result = get_signed_url.getSingleAttachmentURL();

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

expect(actualResult).to.equal(null);
});

it('should return the signed url response on success', async () => {
const mockQuery = sinon.stub();

mockQuery.resolves({ rows: [{ key: 's3Key' }] });

sinon.stub(db, 'getDBConnection').returns({
...dbConnectionObj,
systemUserId: () => {
return 20;
},
query: mockQuery
});

sinon.stub(project_attachments_queries, 'getProjectAttachmentS3KeySQL').returns(SQL`some query`);
sinon.stub(file_utils, 'getS3SignedURL').resolves('myurlsigned.com');

const result = get_signed_url.getSingleAttachmentURL();

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

expect(actualResult).to.eql('myurlsigned.com');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ GET.apiDoc = attachmentApiDocObject(
'GET response containing the signed url of an attachment.'
);

function getSingleAttachmentURL(): RequestHandler {
export function getSingleAttachmentURL(): RequestHandler {
return async (req, res) => {
defaultLog.debug({ label: 'Get single attachment url', message: 'params', req_params: req.params });

Expand Down Expand Up @@ -51,7 +51,7 @@ function getSingleAttachmentURL(): RequestHandler {

await connection.commit();

const s3Key = result && result.rows.length && result.rows[0]?.key;
const s3Key = result && result.rows.length && result.rows[0].key;

const s3SignedUrl = await getS3SignedURL(s3Key);

Expand Down
47 changes: 46 additions & 1 deletion api/src/queries/survey/survey-create-queries.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { expect } from 'chai';
import { describe } from 'mocha';
import { PostSurveyObject, PostSurveyProprietorData } from '../../models/survey-create';
import { postSurveyProprietorSQL, postSurveySQL } from './survey-create-queries';
import {
postFocalSpeciesSQL,
postAncillarySpeciesSQL,
postSurveyProprietorSQL,
postSurveySQL
} from './survey-create-queries';

describe('postSurveySQL', () => {
it('returns null when null project param provided', () => {
Expand Down Expand Up @@ -101,3 +106,43 @@ describe('postSurveyProprietarySQL', () => {
expect(response).to.not.be.null;
});
});

describe('postFocalSpeciesSQL', () => {
it('returns null when null speciesId provided', () => {
const response = postFocalSpeciesSQL((null as unknown) as number, 1);

expect(response).to.be.null;
});

it('returns null when null surveyId provided', () => {
const response = postFocalSpeciesSQL(1, (null as unknown) as number);

expect(response).to.be.null;
});

it('returns sql statement when valid params provided', () => {
const response = postFocalSpeciesSQL(1, 2);

expect(response).to.not.be.null;
});
});

describe('postAncillarySpeciesSQL', () => {
it('returns null when null speciesId provided', () => {
const response = postAncillarySpeciesSQL((null as unknown) as number, 1);

expect(response).to.be.null;
});

it('returns null when null surveyId provided', () => {
const response = postAncillarySpeciesSQL(1, (null as unknown) as number);

expect(response).to.be.null;
});

it('returns sql statement when valid params provided', () => {
const response = postAncillarySpeciesSQL(1, 2);

expect(response).to.not.be.null;
});
});
29 changes: 26 additions & 3 deletions api/src/queries/survey/survey-update-queries.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { expect } from 'chai';
import { describe } from 'mocha';
import { PutSurveyDetailsData } from '../../models/survey-update';
import { putSurveyDetailsSQL } from './survey-update-queries';
import {putSurveyDetailsSQL } from './survey-update-queries';
import { getSurveyDetailsForUpdateSQL} from './survey-view-update-queries';

describe('putSurveySQL', () => {


describe('putSurveyDetailsSQL', () => {
const surveyData: PutSurveyDetailsData = {
name: 'test',
objectives: 'objectives',
Expand Down Expand Up @@ -55,9 +58,29 @@ describe('putSurveySQL', () => {
expect(response).to.be.null;
});

it('returns non null response when valid params provided', () => {
it('returns non null response when valid params provided with geometry', () => {
const response = putSurveyDetailsSQL(1, 2, surveyData, 1);

expect(response).to.not.be.null;
});

it('returns non null response when valid params provided without geometry', () => {
const response = putSurveyDetailsSQL(1, 2, { ...surveyData, geometry: null as any }, 1);

expect(response).to.not.be.null;
});
});

describe('getSurveyForUpdateSQL', () => {
it('returns null when no surveyId provided', () => {
const response = getSurveyDetailsForUpdateSQL((null as unknown) as number);

expect(response).to.be.null;
});

it('returns sql statement when valid params provided', () => {
const response = getSurveyDetailsForUpdateSQL(1);

expect(response).to.not.be.null;
});
});
17 changes: 16 additions & 1 deletion api/src/queries/survey/survey-view-queries.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { expect } from 'chai';
import { describe } from 'mocha';
import { getSurveyListSQL } from './survey-view-queries';
import { getSurveyProprietorForUpdateSQL } from './survey-view-update-queries';
import { getSurveyForViewSQL } from './survey-view-queries';
import { getSurveyListSQL } from '../../queries/survey/survey-view-queries';

describe('getSurveyProprietorSQL', () => {
it('returns null when null survey id param provided', () => {
Expand Down Expand Up @@ -30,3 +31,17 @@ describe('getSurveyListSQL', () => {
expect(response).to.not.be.null;
});
});

describe('getSurveyForViewSQL', () => {
it('returns a null response when null survey id param provided', () => {
const response = getSurveyForViewSQL((null as unknown) as number);

expect(response).to.be.null;
});

it('returns a non null response when valid params passed in', () => {
const response = getSurveyForViewSQL(1);

expect(response).to.not.be.null;
});
});

0 comments on commit e356fd3

Please sign in to comment.