Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 168f3a1

Browse files
Merge pull request #75 from prabalsingh24/polling-endpoint
Polling endpoint
2 parents f5d1208 + 4d94076 commit 168f3a1

File tree

7 files changed

+126
-18
lines changed

7 files changed

+126
-18
lines changed

‎src/routes/api/index.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { NextFunction, Request, Response, Router } from 'express'
22
import run from './run'
33
import submit from './submit'
4+
import result from './result'
45
import { route as langs } from './langs'
56
import { checkValidApiKey } from '../../validators/ApiKeyValidators'
67
import * as debug from 'debug'
@@ -26,6 +27,7 @@ route.use((req: Request, res: Response, next: NextFunction) => {
2627
})
2728

2829
route.use('/runs', run)
30+
route.use('/result', result)
2931
route.use('/submissions', submit)
3032
route.use('/langs', langs)
3133

‎src/routes/api/result/controller.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import {Request, Response} from 'express';
2+
import DB from 'models'
3+
4+
export default {
5+
async sendResult(req: Request, res: Response) {
6+
const submissionId = req.params.id ? parseInt(req.params.id) : null
7+
if (!submissionId) {
8+
res.status(400).json({err: 'SubmissionId not found'})
9+
} else {
10+
DB.submissions.findByPk(submissionId)
11+
.then((submission) => {
12+
res.status(200).json(submission.results)
13+
}).catch((err) => {
14+
res.status(404).json({err: 'Submission not found'})
15+
})
16+
}
17+
}
18+
}

‎src/routes/api/result/index.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Router } from 'express'
2+
import Controller from './controller'
3+
4+
const router: Router = Router()
5+
6+
router.get('/:id', Controller.sendResult)
7+
8+
export default router

‎test/e2e/ResultScenario.spec.ts‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import app from '../../src/server';
2+
import DB from '../../src/models';
3+
import * as utils from "../utils/utils";
4+
5+
const chai = require('chai');
6+
const chaiHttp = require('chai-http');
7+
8+
chai.use(chaiHttp);
9+
const {expect} = chai;
10+
11+
const APIKEY = '7718330d2794406c980bdbded6c9dc1d';
12+
13+
describe('GET api/result/:id', () => {
14+
before(async () => {
15+
await DB.apikeys.create({
16+
id: 1,
17+
key: APIKEY,
18+
whitelist_domains: ['*'],
19+
whitelist_ips: ['*']
20+
});
21+
});
22+
after(utils.truncateTables);
23+
24+
25+
it('should throw 403 error API key is absent in the request', async () => {
26+
const res = await chai.request(app).get(`/api/result/1`);
27+
expect(res.status).to.equal(403);
28+
expect(res.body.message).to.equal('No API Key in request');
29+
});
30+
31+
it('should throw error if incorrect API key is present', async () => {
32+
const res = await chai.request(app).get('/api/result/1').set({
33+
'Authorization': 'Bearer incorrectAPI-KEY',
34+
Accept: 'application/json'
35+
});
36+
expect(res.status).to.equal(403);
37+
expect(res.body.message).to.equal('Invalid API Key');
38+
});
39+
40+
it('should throw 404 error if POST request is made', async () => {
41+
const res = await chai.request(app).post('/api/result/1').set({
42+
Authorization: 'Bearer 7718330d2794406c980bdbded6c9dc1d',
43+
Accept: 'application/json'
44+
});
45+
expect(res.status).to.equal(404);
46+
});
47+
48+
it('should throw 404 error resultId is not present', async () => {
49+
const res = await chai.request(app).get('/api/result').set({
50+
Authorization: `Bearer ${APIKEY}`,
51+
Accept: 'application/json'
52+
});
53+
54+
expect(res.status).to.equal(404);
55+
});
56+
57+
it('should throw 404 error if result is not found ', async () => {
58+
const res = await chai.request(app).get('/api/result/12').set({
59+
Authorization: `Bearer ${APIKEY}`,
60+
Accept: 'application/json'
61+
});
62+
63+
expect(res.status).to.equal(404);
64+
});
65+
66+
it('should return correct result if everything is correct', async () => {
67+
const submission = await DB.submissions.create({
68+
lang: 'cpp',
69+
mode: 'poll',
70+
results: {stdout: 'SUCCESS'}
71+
});
72+
const res = await chai.request(app).get(`/api/result/${submission.id}`).set({
73+
Authorization: `Bearer ${APIKEY}`,
74+
Accept: 'application/json'
75+
});
76+
77+
expect(res.status).to.equal(200);
78+
expect(res.body).to.deep.equal(submission.results);
79+
});
80+
});

‎test/e2e/RunScenario.spec.ts‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@ const source = `
2323
const stdin = 'Success';
2424
const expectedOutput = 'Success';
2525

26-
27-
function delay(ms: number) {
28-
return new Promise( resolve => setTimeout(resolve, ms) );
29-
}
30-
3126
describe('POST api/runs', () => {
3227
before(async () => {
3328
await DB.apikeys.create({
@@ -171,12 +166,15 @@ describe('POST api/runs', () => {
171166
expect(res.status).to.equal(200);
172167

173168
// there is a delay of 1000 for onSuccess, so setting 2000ms delay here.
174-
await delay(2000);
175-
const submission = await DB.submissions.findById(res.body.id);
169+
await utils.delay(2000);
170+
const resultResponse = await chai.request(app).get(`/api/result/${res.body.id}`).set({
171+
Authorization: 'Bearer 7718330d2794406c980bdbded6c9dc1d',
172+
Accept: 'application/json'
173+
}).send(params);
176174

177175
expect(res.body.id).to.exist;
178176
expect(res.status).to.equal(200);
179-
expect(submission.results.stdout).to.equal(expectedOutput);
177+
expect(resultResponse.body.stdout).to.equal(expectedOutput);
180178
});
181179

182180
it('should return id and send result to callback url in callback mode', async () => {
@@ -206,7 +204,7 @@ describe('POST api/runs', () => {
206204
app2.use('/', router);
207205
});
208206

209-
await delay(2000);
207+
await utils.delay(2000);
210208

211209
expect(res.body.id).to.exist;
212210
expect(res.status).to.equal(200);

‎test/e2e/SubmitScenario.spec.ts‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,6 @@ const testcases = [
3535
];
3636
const expectedResult = 'Success';
3737

38-
39-
function delay(ms: number) {
40-
return new Promise( resolve => setTimeout(resolve, ms) );
41-
}
42-
4338
describe('POST api/submissions', () => {
4439
before(async () => {
4540
await DB.apikeys.create({
@@ -171,12 +166,15 @@ describe('POST api/submissions', () => {
171166

172167

173168
// there is a delay of 1000 for onSuccess, so setting 2000ms delay here.
174-
await delay(2000);
175-
const submission = await DB.submissions.findById(res.body.id);
169+
await utils.delay(2000);
170+
const resultResponse = await chai.request(app).get(`/api/result/${res.body.id}`).set({
171+
Authorization: 'Bearer 7718330d2794406c980bdbded6c9dc1d',
172+
Accept: 'application/json'
173+
}).send(params);
176174

177175
expect(res.body.id).to.exist;
178176
expect(res.status).to.equal(200);
179-
expect(submission.results.result).to.equal(expectedResult);
177+
expect(resultResponse.body.result).to.equal(expectedResult);
180178
});
181179

182180
it('should return id and send result to callback url in callback mode', async () => {
@@ -207,7 +205,7 @@ describe('POST api/submissions', () => {
207205
app2.use('/', router);
208206
});
209207

210-
await delay(2000);
208+
await utils.delay(2000);
211209

212210
expect(res.body.id).to.exist;
213211
expect(res.status).to.equal(200);

‎test/utils/utils.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,8 @@ export async function truncateTables() {
4848
await DB.apikeys.destroy({truncate: true});
4949
await DB.langs.destroy({truncate: true});
5050
await DB.submissions.destroy({truncate: true});
51+
}
52+
53+
export function delay(ms: number) {
54+
return new Promise( resolve => setTimeout(resolve, ms) );
5155
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /