Skip to content

Commit

Permalink
425 + 427 - Show / Download Artifacts + Improved Security Rules (#228)
Browse files Browse the repository at this point in the history
* New static security rules
* Artifact download button + generating signed URL from API.
  • Loading branch information
MacQSL authored Jan 15, 2024
1 parent a108222 commit 4573d87
Show file tree
Hide file tree
Showing 24 changed files with 948 additions and 300 deletions.
131 changes: 33 additions & 98 deletions api/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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);
});
});
Loading

0 comments on commit 4573d87

Please sign in to comment.