Skip to content

Commit cb21303

Browse files
committed
feat(e2e-tests): add tests for POST 'api/submit'
1 parent 4e2d215 commit cb21303

File tree

1 file changed

+213
-0
lines changed

1 file changed

+213
-0
lines changed

test/e2e/SubmitScenario.spec.ts

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
import app from '../../src/server';
2+
import DB from '../../src/models';
3+
import express = require('express');
4+
import { Router } from 'express';
5+
import * as utils from '../utils/utils';
6+
7+
8+
const chai = require('chai');
9+
const chaiHttp = require('chai-http');
10+
11+
chai.use(chaiHttp);
12+
const {expect} = chai;
13+
14+
const APIKEY = '7718330d2794406c980bdbded6c9dc1d';
15+
const source = `
16+
#include <iostream>
17+
using namespace std;
18+
int main () {
19+
char in[20];
20+
cin>>in;
21+
cout<<in;
22+
return 0;
23+
}`;
24+
const testcases = [
25+
{
26+
id: 1,
27+
stdin: 'https://minio.cb.lk/public/input',
28+
stdout: 'https://minio.cb.lk/public/input'
29+
},
30+
{
31+
id: 2,
32+
stdin: 'https://minio.cb.lk/public/input',
33+
stdout: 'https://minio.cb.lk/public/input'
34+
}
35+
];
36+
const expectedResult = 'Success';
37+
38+
39+
function delay(ms: number) {
40+
return new Promise( resolve => setTimeout(resolve, ms) );
41+
}
42+
43+
describe('POST api/submissions', () => {
44+
before(async () => {
45+
await DB.apikeys.bulkCreate([
46+
{id: 1, key: APIKEY, whitelist_domains: ['*'], whitelist_ips: ['*']}
47+
]);
48+
await DB.langs.bulkCreate([
49+
{lang_slug: 'cpp', lang_name: 'C++', lang_version: '11'}
50+
]);
51+
await utils.setupMockQueue();
52+
53+
});
54+
after(utils.truncateTables);
55+
56+
it('should throw 403 error if API Key is absent', async () => {
57+
const res = await chai.request(app).post(`/api/submissions`);
58+
59+
expect(res.status).to.equal(403);
60+
expect(res.body.message).to.equal('No API Key in request');
61+
});
62+
63+
it('should throw 404 error if GET request is made', async () => {
64+
const res = await chai.request(app).get(`/api/submissions`).set({
65+
Authorization: 'Bearer 7718330d2794406c980bdbded6c9dc1d',
66+
Accept: 'application/json'
67+
});
68+
69+
expect(res.status).to.equal(404);
70+
});
71+
72+
it('should throw 400 error if correct params are not sent along with the POST request', async () => {
73+
const res = await chai.request(app).post(`/api/submissions`).set({
74+
Authorization: 'Bearer 7718330d2794406c980bdbded6c9dc1d',
75+
Accept: 'application/json'
76+
}).send({});
77+
78+
expect(res.status).to.equal(400);
79+
expect(res.body.err.message).to.equal('"lang" is required');
80+
});
81+
82+
it('should throw error for incorrect language', async () => {
83+
const params = {
84+
source: (new Buffer(source).toString('base64')),
85+
lang: 'prabal',
86+
mode: 'sync',
87+
timelimit: 1,
88+
testcases
89+
};
90+
91+
const res = await chai.request(app).post(`/api/submissions`).set({
92+
Authorization: 'Bearer 7718330d2794406c980bdbded6c9dc1d',
93+
Accept: 'application/json'
94+
}).send(params);
95+
96+
// TODO WRONG, didn't throw error
97+
expect(res.body.result).to.equal(expectedResult);
98+
expect(res.status).to.equal(200);
99+
});
100+
101+
it('should throw error for source missing', async () => {
102+
const params = {
103+
lang: 'prabal',
104+
mode: 'sync',
105+
timelimit: 1,
106+
testcases
107+
};
108+
109+
const res = await chai.request(app).post(`/api/submissions`).set({
110+
Authorization: 'Bearer 7718330d2794406c980bdbded6c9dc1d',
111+
Accept: 'application/json'
112+
}).send(params);
113+
114+
expect(res.status).to.equal(400);
115+
expect(res.body.err.message).to.equal('"source" is required');
116+
});
117+
118+
it('should throw 400 error for incorrect mode ', async () => {
119+
const params = {
120+
source: (new Buffer(source).toString('base64')),
121+
lang: 'cpp',
122+
mode: 'incorrect',
123+
timelimit: 1,
124+
testcases
125+
};
126+
127+
const res = await chai.request(app).post(`/api/submissions`).set({
128+
Authorization: 'Bearer 7718330d2794406c980bdbded6c9dc1d',
129+
Accept: 'application/json'
130+
}).send(params);
131+
132+
expect(res.status).to.equal(400);
133+
expect(res.body.err.message).to.equal('"mode" must be one of [sync, callback, poll]');
134+
});
135+
136+
it('should return correct result in sync mode ', async () => {
137+
const params = {
138+
source: (new Buffer(source).toString('base64')),
139+
lang: 'cpp',
140+
mode: 'sync',
141+
timelimit: 1,
142+
testcases
143+
};
144+
145+
const res = await chai.request(app).post(`/api/submissions`).set({
146+
Authorization: 'Bearer 7718330d2794406c980bdbded6c9dc1d',
147+
Accept: 'application/json'
148+
}).send(params);
149+
150+
expect(res.body.result).to.equal(expectedResult);
151+
expect(res.status).to.equal(200);
152+
});
153+
154+
155+
it('should return correct submission.id in poll mode', async () => {
156+
const params = {
157+
source: (new Buffer(source).toString('base64')),
158+
lang: 'cpp',
159+
mode: 'poll',
160+
timelimit: 1,
161+
testcases
162+
};
163+
164+
const res = await chai.request(app).post(`/api/submissions`).set({
165+
Authorization: 'Bearer 7718330d2794406c980bdbded6c9dc1d',
166+
Accept: 'application/json'
167+
}).send(params);
168+
169+
170+
// there is a delay of 1000 for onSuccess, so setting 2000ms delay here.
171+
await delay(2000);
172+
const submission = await DB.submissions.findById(res.body.id);
173+
174+
expect(res.body.id).to.exist;
175+
expect(res.status).to.equal(200);
176+
expect(submission.results.result).to.equal(expectedResult);
177+
});
178+
179+
it('should return id and send result to callback url in callback mode', async () => {
180+
const params = {
181+
source: (new Buffer(source).toString('base64')),
182+
lang: 'cpp',
183+
mode: 'callback',
184+
callback: 'http://localhost:2405',
185+
timelimit: 1,
186+
testcases
187+
};
188+
189+
const res = await chai.request(app).post(`/api/submissions`).set({
190+
Authorization: 'Bearer 7718330d2794406c980bdbded6c9dc1d',
191+
Accept: 'application/json'
192+
}).send(params);
193+
194+
let resultFromCallBack;
195+
196+
// Mock server for callback
197+
const app2 = express();
198+
app2.use(express.json());
199+
const router = Router();
200+
app2.listen(2405, () => {
201+
router.post('/', (req, res) => {
202+
resultFromCallBack = req.body;
203+
});
204+
app2.use('/', router);
205+
});
206+
207+
await delay(2000);
208+
209+
expect(res.body.id).to.exist;
210+
expect(res.status).to.equal(200);
211+
expect(resultFromCallBack.result).to.equal(expectedResult);
212+
});
213+
});

0 commit comments

Comments
 (0)