Skip to content

Commit 4e93fe9

Browse files
committed
tests: add tests for polling
1 parent 3853582 commit 4e93fe9

File tree

4 files changed

+94
-4
lines changed

4 files changed

+94
-4
lines changed

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: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,14 @@ describe('POST api/runs', () => {
172172

173173
// there is a delay of 1000 for onSuccess, so setting 2000ms delay here.
174174
await delay(2000);
175-
const submission = await DB.submissions.findById(res.body.id);
175+
const resultResponse = await chai.request(app).get(`/api/result/${res.body.id}`).set({
176+
Authorization: 'Bearer 7718330d2794406c980bdbded6c9dc1d',
177+
Accept: 'application/json'
178+
}).send(params);
176179

177180
expect(res.body.id).to.exist;
178181
expect(res.status).to.equal(200);
179-
expect(submission.results.stdout).to.equal(expectedOutput);
182+
expect(resultResponse.body.stdout).to.equal(expectedOutput);
180183
});
181184

182185
it('should return id and send result to callback url in callback mode', async () => {

test/e2e/SubmitScenario.spec.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,14 @@ describe('POST api/submissions', () => {
172172

173173
// there is a delay of 1000 for onSuccess, so setting 2000ms delay here.
174174
await delay(2000);
175-
const submission = await DB.submissions.findById(res.body.id);
175+
const resultResponse = await chai.request(app).get(`/api/result/${res.body.id}`).set({
176+
Authorization: 'Bearer 7718330d2794406c980bdbded6c9dc1d',
177+
Accept: 'application/json'
178+
}).send(params);
176179

177180
expect(res.body.id).to.exist;
178181
expect(res.status).to.equal(200);
179-
expect(submission.results.result).to.equal(expectedResult);
182+
expect(resultResponse.body.result).to.equal(expectedResult);
180183
});
181184

182185
it('should return id and send result to callback url in callback mode', async () => {

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)