Skip to content

Commit 15ba4d8

Browse files
committed
feat(e2e tests) : add tests for POST 'api/runs'
1 parent 4ab9da9 commit 15ba4d8

File tree

1 file changed

+212
-0
lines changed

1 file changed

+212
-0
lines changed

test/e2e/RunScenario.spec.ts

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

0 commit comments

Comments
 (0)