-
Notifications
You must be signed in to change notification settings - Fork 9
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
5 changed files
with
251 additions
and
7 deletions.
There are no files selected for viewing
161 changes: 161 additions & 0 deletions
161
api/src/paths/project/{projectId}/attachments/{attachmentId}/getSignedUrl.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,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'); | ||
}); | ||
}); |
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
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