Skip to content

Commit

Permalink
fix: skip processing invalid body payload (#132)
Browse files Browse the repository at this point in the history
* fix: fix processing invalid payload

* fix: disable typecheck errors on lib
  • Loading branch information
wa0x6e authored Aug 23, 2023
1 parent ff00588 commit befdf6f
Show file tree
Hide file tree
Showing 9 changed files with 2,257 additions and 29 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: Test

on: [push]

jobs:
test:
uses: snapshot-labs/actions/.github/workflows/test.yml@main
secrets: inherit
with:
mysql_database_name: snapshot_relayer_test
mysql_schema_path: ./src/schema.sql
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.DS_Store
node_modules
dist
build
coverage

# Remove some common IDE working directories
.idea
Expand Down
14 changes: 14 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export default {
clearMocks: true,
collectCoverage: true,
collectCoverageFrom: ['./src/**'],
coverageDirectory: 'coverage',
coverageProvider: 'v8',
coveragePathIgnorePatterns: ['/node_modules/', '<rootDir>/dist/', '<rootDir>/test/fixtures/'],
preset: 'ts-jest',
testEnvironment: 'jest-environment-node-single-context',
setupFiles: ['dotenv/config'],
moduleFileExtensions: ['js', 'ts'],
testPathIgnorePatterns: ['dist/'],
verbose: true
};
15 changes: 13 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
"typecheck": "tsc --noEmit",
"build": "tsc",
"dev": "nodemon src/index.ts",
"start": "node dist/src/index.js"
"start": "node dist/src/index.js",
"start:test": "dotenv -e test/.env.test yarn dev",
"test": "PORT=3003 start-server-and-test 'yarn start:test' http://localhost:3003/api 'dotenv -e test/.env.test jest --runInBand test'",
"test:e2e": "PORT=3003 start-server-and-test 'yarn start:test' http://localhost:3003/api 'dotenv -e test/.env.test jest --runInBand --collectCoverage=false test/e2e/'"
},
"eslintConfig": {
"extends": "@snapshot-labs"
Expand Down Expand Up @@ -37,9 +40,17 @@
"@snapshot-labs/eslint-config": "^0.1.0-beta.7",
"@snapshot-labs/prettier-config": "^0.1.0-beta.7",
"@types/express": "^4.17.11",
"@types/jest": "^29.5.3",
"@types/node": "^14.14.21",
"@types/supertest": "^2.0.12",
"dotenv-cli": "^7.3.0",
"eslint": "^8.32.0",
"jest": "^29.6.3",
"jest-environment-node-single-context": "^29.1.0",
"nodemon": "^2.0.15",
"prettier": "^2.8.0"
"prettier": "^2.8.0",
"start-server-and-test": "^2.0.0",
"supertest": "^6.3.3",
"ts-jest": "^29.1.1"
}
}
6 changes: 6 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ router.get('/api/messages/:hash', async (req, res) => {
});

router.post('/', async (req, res) => {
if (!req.body.data || !req.body.data.message) {
return res.status(400).json({
error: 'Invalid format request'
});
}

try {
const msg = req.body.data.message;
const msgHash = snapshot.utils.getHash(req.body.data);
Expand Down
1 change: 1 addition & 0 deletions test/.env.test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DATABASE_URL=mysql://root:[email protected]:3306/snapshot_relayer_test
19 changes: 19 additions & 0 deletions test/e2e/api.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import request from 'supertest';

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

describe('POST /', () => {
describe('when the request body is invalid', () => {
const inputs: [string, any][] = [
['empty', {}],
['missing the data key', { foo: 'bar' }],
['missing the data.msg key', { data: { foo: 'bar' } }]
];

it.each(inputs)('returns a 400 error when the input is %s', async (name, input) => {
const response = await request(HOST).post('/').send(input);

expect(response.statusCode).toBe(400);
});
});
});
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"strict": true,
"noImplicitAny": false,
"resolveJsonModule": true,
"moduleResolution": "Node"
"moduleResolution": "Node",
"skipLibCheck": true
}
}
Loading

0 comments on commit befdf6f

Please sign in to comment.