Skip to content

Commit

Permalink
Merge pull request hms-dbmi-cellenics#512 from biomage-org/65-restric…
Browse files Browse the repository at this point in the history
…t-upload-status-changes

Restrict upload status changes
  • Loading branch information
cosa65 authored Nov 30, 2023
2 parents 377d9d3 + 2d45781 commit 1921729
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 39 deletions.
8 changes: 4 additions & 4 deletions src/api.v2/controllers/sampleFileController.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ const beginUpload = async (req, res) => {

const patchFile = async (req, res) => {
const {
params: { experimentId, sampleId, sampleFileType },
params: { experimentId, sampleFileId },
body: { uploadStatus },
} = req;
logger.log(`Patching file ${sampleFileType} for sample ${sampleId} in experiment ${experimentId}`);
logger.log(`Patching file ${sampleFileId} in experiment ${experimentId}`);

await new SampleFile().updateUploadStatus(sampleId, sampleFileType, uploadStatus);
await new SampleFile().updateUploadStatus(sampleFileId, uploadStatus);

logger.log(`Finished patching sample file for experiment ${experimentId}, sample ${sampleId}, sampleFileType ${sampleFileType}`);
logger.log(`Finished patching sample file for experiment ${experimentId}, file: ${sampleFileId}`);
res.json(OK());
};

Expand Down
12 changes: 3 additions & 9 deletions src/api.v2/model/SampleFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,10 @@ class SampleFile extends BasicModel {
return files;
}

async updateUploadStatus(sampleId, sampleFileType, uploadStatus) {
await this.sql({ sf: tableNames.SAMPLE_FILE })
async updateUploadStatus(sampleFileId, uploadStatus) {
return this.sql({ sf: tableNames.SAMPLE_FILE })
.update({ upload_status: uploadStatus })
.whereExists(
this.sql({ sf_map: tableNames.SAMPLE_TO_SAMPLE_FILE_MAP })
.select(['sample_file_id'])
.where('sf_map.sample_file_id', '=', this.sql.ref('sf.id'))
.where('sf_map.sample_id', '=', sampleId),
)
.andWhere({ sample_file_type: sampleFileType });
.where({ id: sampleFileId });
}
}

Expand Down
11 changes: 3 additions & 8 deletions src/specs/api.v2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1574,9 +1574,9 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/HTTPError"
"/experiments/{experimentId}/sampleFiles/{sampleFileId}":
patch:
summary: Update a sample files property
description: Update property of a sample file
operationId: patchFile
x-eov-operation-id: sampleFile#patch
x-eov-operation-handler: routes/sampleFile
Expand All @@ -1587,13 +1587,7 @@ paths:
description: ID of the experiment.
schema:
type: string
- name: sampleId
in: path
description: ID of the sample.
required: true
schema:
type: string
- name: sampleFileType
- name: sampleFileId
in: path
description: Type of the file.
required: true
Expand Down Expand Up @@ -1641,6 +1635,7 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/HTTPError"

"/experiments/{experimentId}/sampleFiles/{sampleFileId}/beginUpload":
post:
summary: Begins the multipart upload of a sample file
Expand Down
7 changes: 3 additions & 4 deletions tests/api.v2/controllers/sampleFileController.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,12 @@ describe('sampleFileController', () => {

it('patchFile works correctly', async () => {
const experimentId = 'experimentId';
const sampleId = 'sampleId';
const sampleFileType = 'features10x';
const sampleFileId = 'sampleFileId';

const uploadStatus = 'uploaded';

const mockReq = {
params: { experimentId, sampleId, sampleFileType },
params: { experimentId, sampleFileId },
body: { uploadStatus },
};

Expand All @@ -178,7 +177,7 @@ describe('sampleFileController', () => {
// Used with normal client
expect(SampleFile).toHaveBeenCalled();

expect(sampleFileInstance.updateUploadStatus).toHaveBeenCalledWith('sampleId', 'features10x', uploadStatus);
expect(sampleFileInstance.updateUploadStatus).toHaveBeenCalledWith(sampleFileId, uploadStatus);

// Response is generated signed url
expect(mockRes.json).toHaveBeenCalledWith(OK());
Expand Down
11 changes: 3 additions & 8 deletions tests/api.v2/model/SampleFile.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,15 @@ describe('model/Sample', () => {

it('updateUploadStatus works correctly if valid params are passed', async () => {
const mockUploadStatus = 'fileNotFound';
const mockSampleFileId = 'fileId1';

const sfIdRef = 'sf.idRef';
mockSqlClient.ref.mockReturnValueOnce(sfIdRef);

await new SampleFile().updateUploadStatus(mockSampleId, mockSampleFileType, mockUploadStatus);
await new SampleFile().updateUploadStatus(mockSampleFileId, mockUploadStatus);

expect(mockSqlClient).toHaveBeenCalledWith({ sf: tableNames.SAMPLE_FILE });
expect(mockSqlClient.update).toHaveBeenCalledWith({ upload_status: mockUploadStatus });
expect(mockSqlClient.whereExists).toHaveBeenCalled();

expect(mockSqlClient).toHaveBeenCalledWith({ sf_map: tableNames.SAMPLE_TO_SAMPLE_FILE_MAP });
expect(mockSqlClient.select).toHaveBeenCalledWith(['sample_file_id']);
expect(mockSqlClient.where).toHaveBeenCalledWith('sf_map.sample_file_id', '=', sfIdRef);
expect(mockSqlClient.where).toHaveBeenCalledWith('sf_map.sample_id', '=', mockSampleId);
expect(mockSqlClient.andWhere).toHaveBeenCalledWith({ sample_file_type: mockSampleFileType });
expect(mockSqlClient.where.mock.calls).toMatchSnapshot();
});
});
11 changes: 11 additions & 0 deletions tests/api.v2/model/__snapshots__/SampleFile.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`model/Sample updateUploadStatus works correctly if valid params are passed 1`] = `
Array [
Array [
Object {
"id": "fileId1",
},
],
]
`;
10 changes: 4 additions & 6 deletions tests/api.v2/routes/sampleFile.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,14 @@ describe('tests for experiment route', () => {
});

const experimentId = 'experiment-id';
const sampleId = 'sample-id';
const sampleFileType = 'features.tsv.gz';
const sampleFileId = 'sample-file-id';

const patchFileBody = {
uploadStatus: 'uploading',
};

request(app)
.patch(`/v2/experiments/${experimentId}/samples/${sampleId}/sampleFiles/${sampleFileType}`)
.patch(`/v2/experiments/${experimentId}/sampleFiles/${sampleFileId}`)
.send(patchFileBody)
.expect(200)
.end((err) => {
Expand All @@ -126,15 +125,14 @@ describe('tests for experiment route', () => {
});

const experimentId = 'experiment-id';
const sampleId = 'sample-id';
const sampleFileType = 'features.tsv.gz';
const sampleFileId = 'sample-file-id';

const patchFileBody = {
s3Path: 'features.tsv.gz',
};

request(app)
.patch(`/v2/experiments/${experimentId}/samples/${sampleId}/sampleFiles/${sampleFileType}`)
.patch(`/v2/experiments/${experimentId}/sampleFiles/${sampleFileId}`)
.send(patchFileBody)
.expect(400)
.end((err) => {
Expand Down

0 comments on commit 1921729

Please sign in to comment.