Skip to content

Commit

Permalink
Add test. Fix multer clamav scan
Browse files Browse the repository at this point in the history
  • Loading branch information
NickPhura committed Jan 16, 2024
1 parent 7ba312a commit aabed16
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 11 deletions.
24 changes: 15 additions & 9 deletions api/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,30 +54,36 @@ const openAPIFramework = initialize({
docsPath: '/raw-api-docs', // path to view raw openapi spec
consumesMiddleware: {
'application/json': express.json({ limit: MAX_REQ_BODY_SIZE }),
'multipart/form-data': function (req, res, next) {
'multipart/form-data': async function (req, res, next) {
console.log('AA');

Check warning on line 58 in api/src/app.ts

View check run for this annotation

Codecov / codecov/patch

api/src/app.ts#L58

Added line #L58 was not covered by tests
const multerRequestHandler = multer({
storage: multer.memoryStorage(), // TOOD change to local/PVC storage and stream file uploads to S3?
limits: { fileSize: MAX_UPLOAD_FILE_SIZE }
}).array('media', MAX_UPLOAD_NUM_FILES);

multerRequestHandler(req, res, (error?: any) => {
console.log('BB');
return multerRequestHandler(req, res, async function (error?: any) {
console.log('CC');

Check warning on line 65 in api/src/app.ts

View check run for this annotation

Codecov / codecov/patch

api/src/app.ts#L63-L65

Added lines #L63 - L65 were not covered by tests
if (error) {
return next(error);
}

console.log('DD');

Check warning on line 69 in api/src/app.ts

View check run for this annotation

Codecov / codecov/patch

api/src/app.ts#L69

Added line #L69 was not covered by tests
if (req.files && req.files.length) {
console.log('EE');

Check warning on line 71 in api/src/app.ts

View check run for this annotation

Codecov / codecov/patch

api/src/app.ts#L71

Added line #L71 was not covered by tests
// Set original request file field to empty string to satisfy OpenAPI validation
// See: https://www.npmjs.com/package/express-openapi#argsconsumesmiddleware
(req.files as Express.Multer.File[]).forEach((file) => {
for (const file of req.files as Express.Multer.File[]) {
req.body[file.fieldname] = '';

console.log('FF');
console.log(file);

Check warning on line 77 in api/src/app.ts

View check run for this annotation

Codecov / codecov/patch

api/src/app.ts#L74-L77

Added lines #L74 - L77 were not covered by tests
// Scan file for malicious content, if enabled
if (!scanFileForVirus(file)) {
const isFileSafe = await scanFileForVirus(file);
console.log('!!!!!');

Check warning on line 80 in api/src/app.ts

View check run for this annotation

Codecov / codecov/patch

api/src/app.ts#L79-L80

Added lines #L79 - L80 were not covered by tests
if (!isFileSafe) {
throw new HTTP400('Malicious file content detected.', [{ file_name: file.originalname }]);

Check warning on line 82 in api/src/app.ts

View check run for this annotation

Codecov / codecov/patch

api/src/app.ts#L82

Added line #L82 was not covered by tests
}
});
}
}

console.log('GG');

Check warning on line 86 in api/src/app.ts

View check run for this annotation

Codecov / codecov/patch

api/src/app.ts#L86

Added line #L86 was not covered by tests
return next();
});
},
Expand Down
59 changes: 57 additions & 2 deletions api/src/repositories/code-repository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,61 @@ describe('CodeRepository', () => {
});

describe('getFeaturePropertyByName', () => {
// @TODO
})
afterEach(() => {
sinon.restore();
});

it('throws an error if no matching record found', async () => {
const mockQueryResponse = {
rowCount: 0,
rows: []
} as any as Promise<QueryResult<any>>;

const mockDBConnection = getMockDBConnection({
sql: () => mockQueryResponse
});

const codeRepository = new CodeRepository(mockDBConnection);

const featurePropertyName = 'name';

try {
await codeRepository.getFeaturePropertyByName(featurePropertyName);

expect.fail();
} catch (error) {
expect((error as Error).message).to.equal('Failed to get feature property record');
}
});

it('should return row if succeeds', async () => {
const mockRow: FeatureTypeCode & FeaturePropertyCode = {
feature_type_id: 1,
feature_type_name: 'dataset',
feature_type_display_name: 'Dataset',
feature_property_id: 2,
feature_property_name: 'name',
feature_property_display_name: 'Name',
feature_property_type_id: 3,
feature_property_type_name: 'string'
};

const mockQueryResponse = {
rowCount: 1,
rows: [mockRow]
} as any as Promise<QueryResult<any>>;

const mockDBConnection = getMockDBConnection({
sql: () => mockQueryResponse
});

const codeRepository = new CodeRepository(mockDBConnection);

const featurePropertyName = 'name';

const result = await codeRepository.getFeaturePropertyByName(featurePropertyName);

expect(result).to.be.eql(mockRow);
});
});
});

0 comments on commit aabed16

Please sign in to comment.