Skip to content

Commit 092a6e2

Browse files
committed
fix: language validation in submit and run
1 parent e418264 commit 092a6e2

File tree

5 files changed

+22
-33
lines changed

5 files changed

+22
-33
lines changed

src/middlewares/langValidator.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import DB from 'models';
2+
import { Request, Response, NextFunction } from 'express';
3+
4+
export default (key = 'body') => async (req: Request, res: Response, next: NextFunction) => {
5+
const lang_slug = req[key].lang
6+
const lang = await DB.langs.findOne({
7+
where: {
8+
lang_slug
9+
}
10+
})
11+
if (!lang) {
12+
return res.status(400).json({
13+
message: 'Language not Found'
14+
})
15+
}
16+
17+
return next()
18+
}

src/routes/api/run/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ import { Router } from 'express'
22
import Controller from './controller'
33
import Validator from './validators'
44
import { successListener } from 'rabbitmq/jobqueue'
5+
import langValidator from 'middlewares/langValidator';
56

67
const router: Router = Router()
78
const validator = new Validator()
89

9-
router.post('/', validator.POST, Controller.runPOST)
10+
router.post('/', validator.POST, langValidator(), Controller.runPOST)
1011
successListener.on('run_result', Controller.onSuccess)
1112

1213
export default router

src/routes/api/submit/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ import { Router } from 'express'
22
import Controller from './controller'
33
import Validator from './validators'
44
import { successListener } from 'rabbitmq/jobqueue'
5+
import langValidator from 'middlewares/langValidator'
56

67
const router: Router = Router()
78
const validator = new Validator()
89

9-
router.post('/', validator.POST, Controller.submitPOST)
10+
router.post('/', validator.POST, langValidator(), Controller.submitPOST)
1011
successListener.on('submit_result', Controller.onSuccess)
1112

1213
export default router

test/unit/validators/RunValidator.spec.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,6 @@ describe('RunValidator', async () => {
4141
expect(nextSpy.calledOnce).to.be.false;
4242
});
4343

44-
it('should throw an error with incorrect language', async () => {
45-
const req = {
46-
body: {
47-
source: 'LKJSDFKLMC414CcnBcba12',
48-
lang: 'wrongLang',
49-
mode: 'sync',
50-
stdin: 'INPUT'
51-
}
52-
};
53-
54-
await runValidator.POST(req, res, nextSpy);
55-
expect(sentStatus).to.equal(400);
56-
expect(nextSpy.calledOnce).to.be.false;
57-
});
58-
5944
it('should throw an error when source is missing', async () => {
6045
const req = {
6146
body: {

test/unit/validators/SubmitValidator.spec.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,6 @@ describe('SubmitValidator', async () => {
5353
expect(nextSpy.calledOnce).to.be.false;
5454
});
5555

56-
it('should throw an error with incorrect language', async () => {
57-
const req = {
58-
body: {
59-
source: 'LKJSDFKLMC414CcnBcba12',
60-
lang: 'wrongLang',
61-
mode: 'sync',
62-
testcases
63-
}
64-
};
65-
66-
await submitValidator.POST(req, res, nextSpy);
67-
68-
expect(sentStatus).to.equal(400);
69-
expect(nextSpy.calledOnce).to.be.false;
70-
});
71-
7256
it('should throw an error when source is missing', async () => {
7357
const req = {
7458
body: {

0 commit comments

Comments
 (0)