Skip to content

Commit

Permalink
fix: fix error not caught by try/catch (#143)
Browse files Browse the repository at this point in the history
* fix: fix error not caught by try/catch

* chore: add e2e tests for upload
  • Loading branch information
wa0x6e authored Aug 7, 2023
1 parent e0f61b2 commit 8e2cd8f
Show file tree
Hide file tree
Showing 9 changed files with 380 additions and 24 deletions.
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
"scripts": {
"lint": "yarn lint:nofix --fix",
"lint:nofix": "eslint . --ext .ts",
"typecheck": "tsc --noEmit",
"build": "tsc",
"dev": "nodemon src/index.ts",
"start": "node dist/src/index.js",
"test": "yarn jest",
"typecheck": "tsc --noEmit"
"start:test": "dotenv -e test/.env.test yarn dev",
"test": "PORT=3003 start-server-and-test 'yarn start:test' 3003 'dotenv -e test/.env.test jest --runInBand'",
"test:unit": "dotenv -e test/.env.test jest test/unit/",
"test:e2e": "PORT=3003 start-server-and-test 'yarn start:test' 3003 'dotenv -e test/.env.test jest --runInBand --collectCoverage=false test/e2e/'"
},
"eslintConfig": {
"extends": "@snapshot-labs"
Expand Down Expand Up @@ -41,9 +44,12 @@
"@types/multer": "^1.4.7",
"@types/node": "^17.0.35",
"@types/sharp": "^0.30.2",
"dotenv-cli": "^7.2.1",
"eslint": "^8.45.0",
"jest": "^28.1.0",
"prettier": "^2.8.8",
"start-server-and-test": "^2.0.0",
"supertest": "^6.3.3",
"ts-jest": "^28.0.4"
}
}
29 changes: 17 additions & 12 deletions src/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ const upload = multer({
}).single('file');

router.post('/upload', async (req, res) => {
try {
upload(req, res, async err => {
if (err) return rpcError(res, 500, err.message, null);
if (!req.file) return rpcError(res, 500, 'no file', null);
upload(req, res, async err => {
try {
if (err) return rpcError(res, 400, err.message, null);
if (!req.file) return rpcError(res, 400, 'no file', null);

const transformer = sharp()
.resize({
Expand All @@ -37,6 +37,7 @@ router.post('/upload', async (req, res) => {
.createReadStream(req.file?.path as string)
.pipe(transformer)
.toBuffer();

const result = await Promise.any([
setFleek(buffer),
setInfura(buffer),
Expand All @@ -49,15 +50,19 @@ router.post('/upload', async (req, res) => {
};
console.log('Upload success', result.provider, result.cid);
return rpcSuccess(res, file, null);
});
} catch (e) {
capture(e);
return rpcError(res, 500, e, null);
} finally {
if (req.file) {
await fs.promises.unlink(req.file.path as string);
} catch (e: any) {
if (e.message === 'Input buffer contains unsupported image format') {
return rpcError(res, 415, 'Unsupported file type', null);
}

capture(e);
return rpcError(res, 500, e, null);
} finally {
if (req.file) {
await fs.promises.unlink(req.file.path as string);
}
}
}
});
});

export default router;
Empty file added test/.env.test
Empty file.
3 changes: 3 additions & 0 deletions test/e2e/fixtures/file.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "Snapshot"
}
3 changes: 3 additions & 0 deletions test/e2e/fixtures/json-file.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/e2e/fixtures/too-heavy.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/e2e/fixtures/valid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
53 changes: 53 additions & 0 deletions test/e2e/upload.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import path from 'path';
import request from 'supertest';

const HOST = `http://localhost:${process.env.PORT || 3003}`;

describe('GET /upload', () => {
describe('when the image exceed the maximum file size', () => {
it('returns a 400 error', async () => {
const response = await request(HOST)
.post('/upload')
.attach('file', path.join(__dirname, './fixtures/too-heavy.jpg'));

expect(response.statusCode).toBe(400);
expect(response.body.error.data).toContain('large');
});
});

describe('when the file is not an image', () => {
it.each([
['non-image file extension', 'file.json'],
['non-image impersonating image extension', 'json-file.png']
])('returns a 415 error on %s', async (title, filename) => {
const response = await request(HOST)
.post('/upload')
.attach('file', path.join(__dirname, `./fixtures/${filename}`));

expect(response.statusCode).toBe(415);
expect(response.body.error.data).toBe('Unsupported file type');
});
});

describe('when the file is missing', () => {
it('returns a 400 error', async () => {
const response = await request(HOST).post('/upload');

expect(response.statusCode).toBe(400);
expect(response.body.error.data).toBe('no file');
});
});

describe('when the file is correct', () => {
it('uploads the file and returns a JSO-RPC response with the CID and its provider', async () => {
const response = await request(HOST)
.post('/upload')
.attach('file', path.join(__dirname, './fixtures/valid.png'));

expect(response.statusCode).toBe(200);
expect(response.body.jsonrpc).toBe('2.0');
expect(response.body.result.cid.length).toBeGreaterThan(10);
expect(['4everland', 'infura', 'fleek', 'pinata']).toContain(response.body.result.provider);
});
});
});
Loading

0 comments on commit 8e2cd8f

Please sign in to comment.