-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
425 + 427 - Show / Download Artifacts + Improved Security Rules (#228)
* New static security rules * Artifact download button + generating signed URL from API.
- Loading branch information
Showing
24 changed files
with
948 additions
and
300 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
99 changes: 99 additions & 0 deletions
99
api/src/paths/submission/{submissionId}/features/{submissionFeatureId}/signed-url.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,99 @@ | ||
import chai, { expect } from 'chai'; | ||
import { describe } from 'mocha'; | ||
import sinon from 'sinon'; | ||
import sinonChai from 'sinon-chai'; | ||
import * as db from '../../../../../database/db'; | ||
import { HTTP400, HTTPError } from '../../../../../errors/http-error'; | ||
import { SubmissionService } from '../../../../../services/submission-service'; | ||
import { UserService } from '../../../../../services/user-service'; | ||
import { getMockDBConnection, getRequestHandlerMocks } from '../../../../../__mocks__/db'; | ||
import { getSubmissionFeatureSignedUrl } from './signed-url'; | ||
|
||
chai.use(sinonChai); | ||
|
||
describe('getSubmissionFeatureSignedUrl', () => { | ||
afterEach(() => { | ||
sinon.restore(); | ||
}); | ||
|
||
it('throws error if submissionService throws error', async () => { | ||
const dbConnectionObj = getMockDBConnection(); | ||
|
||
const getDBConnectionStub = sinon.stub(db, 'getDBConnection').returns(dbConnectionObj); | ||
|
||
const getSubmissionFeatureSignedUrlStub = sinon | ||
.stub(SubmissionService.prototype, 'getSubmissionFeatureSignedUrl') | ||
.throws(new HTTP400('Error', ['Error'])); | ||
|
||
const isSystemUserAdminStub = sinon.stub(UserService.prototype, 'isSystemUserAdmin').resolves(false); | ||
|
||
const requestHandler = getSubmissionFeatureSignedUrl(); | ||
|
||
const { mockReq, mockRes, mockNext } = getRequestHandlerMocks(); | ||
|
||
mockReq['keycloak_token'] = 'TOKEN'; | ||
|
||
mockReq.params = { | ||
submissionId: '1', | ||
submissionFeatureId: '2' | ||
}; | ||
|
||
mockReq.query = { | ||
key: 'KEY', | ||
value: 'VALUE' | ||
}; | ||
|
||
try { | ||
await requestHandler(mockReq, mockRes, mockNext); | ||
|
||
expect.fail(); | ||
} catch (error) { | ||
expect(getDBConnectionStub).to.have.been.calledWith('TOKEN'); | ||
expect(isSystemUserAdminStub).to.have.been.calledOnce; | ||
expect(getSubmissionFeatureSignedUrlStub).to.have.been.calledOnce; | ||
expect((error as HTTPError).status).to.equal(400); | ||
expect((error as HTTPError).message).to.equal('Error'); | ||
} | ||
}); | ||
|
||
it('should return 200 on success', async () => { | ||
const dbConnectionObj = getMockDBConnection(); | ||
|
||
const getAPIUserDBConnectionStub = sinon.stub(db, 'getAPIUserDBConnection').returns(dbConnectionObj); | ||
|
||
const mockResponse = [] as unknown as any; | ||
|
||
const getSubmissionFeatureSignedUrlStub = sinon | ||
.stub(SubmissionService.prototype, 'getSubmissionFeatureSignedUrl') | ||
.resolves(mockResponse); | ||
|
||
const isSystemUserAdminStub = sinon.stub(UserService.prototype, 'isSystemUserAdmin').resolves(false); | ||
|
||
const requestHandler = getSubmissionFeatureSignedUrl(); | ||
|
||
const { mockReq, mockRes, mockNext } = getRequestHandlerMocks(); | ||
|
||
mockReq.params = { | ||
submissionId: '1', | ||
submissionFeatureId: '2' | ||
}; | ||
|
||
mockReq.query = { | ||
key: 'KEY', | ||
value: 'VALUE' | ||
}; | ||
|
||
await requestHandler(mockReq, mockRes, mockNext); | ||
|
||
expect(getAPIUserDBConnectionStub).to.have.been.calledOnce; | ||
expect(getSubmissionFeatureSignedUrlStub).to.have.been.calledOnce; | ||
expect(getSubmissionFeatureSignedUrlStub).to.have.been.calledWith({ | ||
submissionFeatureId: 2, | ||
submissionFeatureObj: { key: 'KEY', value: 'VALUE' }, | ||
isAdmin: false | ||
}); | ||
expect(isSystemUserAdminStub).to.have.been.calledOnce; | ||
expect(mockRes.statusValue).to.eql(200); | ||
expect(mockRes.jsonValue).to.eql(mockResponse); | ||
}); | ||
}); |
Oops, something went wrong.