Skip to content

Commit e5465b0

Browse files
committed
add tests for project validator
1 parent ed9620f commit e5465b0

File tree

1 file changed

+265
-0
lines changed

1 file changed

+265
-0
lines changed
Lines changed: 265 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,265 @@
1+
// @ts-nocheck
2+
import ProjectValidator from '../../../src/routes/api/project/validators';
3+
4+
const sinon = require('sinon');
5+
const chai = require('chai');
6+
const {expect} = chai;
7+
8+
var res, err, sentData, sentStatus, nextSpy;
9+
const projectValidator = new ProjectValidator();
10+
11+
describe('SubmitValidator', async () => {
12+
beforeEach(() => {
13+
// set the data to default;
14+
sentData = {};
15+
sentStatus = null;
16+
res = {
17+
status: function (status) {
18+
sentStatus = status;
19+
return this;
20+
},
21+
json: function (msg) {
22+
err = msg.err;
23+
sentData = msg
24+
}
25+
};
26+
nextSpy = sinon.spy();
27+
});
28+
29+
it('should throw an error with language missing', async () => {
30+
const req = {
31+
body: {
32+
problem: 'https://minio.cb.lk/public/input',
33+
submission: 'https://minio.cb.lk/public/input',
34+
submissionDirs: 'src',
35+
mode: 'poll',
36+
timelimit: 1
37+
}
38+
};
39+
40+
await projectValidator.POST(req, res, nextSpy);
41+
42+
expect(sentStatus).to.equal(400);
43+
expect(sentData.err.message).to.equal('"lang" is required');
44+
expect(nextSpy.calledOnce).to.be.false;
45+
});
46+
47+
it('should throw an error with problem is missing', async () => {
48+
const req = {
49+
body: {
50+
lang: 'node',
51+
submission: 'https://minio.cb.lk/public/input',
52+
submissionDirs: 'src',
53+
mode: 'poll',
54+
timelimit: 1
55+
}
56+
};
57+
58+
await projectValidator.POST(req, res, nextSpy);
59+
60+
expect(sentStatus).to.equal(400);
61+
expect(sentData.err.message).to.equal('"problem" is required');
62+
expect(nextSpy.calledOnce).to.be.false;
63+
});
64+
65+
it('should throw an error when problem is not a url', async () => {
66+
const req = {
67+
body: {
68+
lang: 'node',
69+
problem: 'not-a-url',
70+
submission: 'https://minio.cb.lk/public/input',
71+
submissionDirs: 'src',
72+
mode: 'poll',
73+
timelimit: 1
74+
}
75+
};
76+
77+
await projectValidator.POST(req, res, nextSpy);
78+
79+
expect(sentStatus).to.equal(400);
80+
expect(sentData.err.message).to.equal('"problem" must be a valid uri');
81+
expect(nextSpy.calledOnce).to.be.false;
82+
});
83+
84+
it('should throw an error when problem is not a string', async () => {
85+
const req = {
86+
body: {
87+
lang: 'node',
88+
problem: 123,
89+
submission: 'https://minio.cb.lk/public/input',
90+
submissionDirs: 'src',
91+
mode: 'poll',
92+
timelimit: 1
93+
}
94+
};
95+
96+
await projectValidator.POST(req, res, nextSpy);
97+
98+
expect(sentStatus).to.equal(400);
99+
expect(sentData.err.message).to.equal('"problem" must be a string');
100+
expect(nextSpy.calledOnce).to.be.false;
101+
});
102+
103+
it('should throw an error with submission is missing', async () => {
104+
const req = {
105+
body: {
106+
lang: 'node',
107+
problem: 'https://minio.cb.lk/public/input',
108+
submissionDirs: 'src',
109+
mode: 'poll',
110+
timelimit: 1
111+
}
112+
};
113+
114+
await projectValidator.POST(req, res, nextSpy);
115+
116+
expect(sentStatus).to.equal(400);
117+
expect(sentData.err.message).to.equal('"submission" is required');
118+
expect(nextSpy.calledOnce).to.be.false;
119+
});
120+
121+
it('should throw an error when submission is not a url', async () => {
122+
const req = {
123+
body: {
124+
lang: 'node',
125+
problem: 'https://minio.cb.lk/public/input',
126+
submission: 'not-a-url',
127+
submissionDirs: 'src',
128+
mode: 'poll',
129+
timelimit: 1
130+
}
131+
};
132+
133+
await projectValidator.POST(req, res, nextSpy);
134+
135+
expect(sentStatus).to.equal(400);
136+
expect(sentData.err.message).to.equal('"submission" must be a valid uri');
137+
expect(nextSpy.calledOnce).to.be.false;
138+
});
139+
140+
it('should throw an error when submission is not a string', async () => {
141+
const req = {
142+
body: {
143+
lang: 'node',
144+
problem: 'https://minio.cb.lk/public/input',
145+
submission: 123,
146+
submissionDirs: 'src',
147+
mode: 'poll',
148+
timelimit: 1
149+
}
150+
};
151+
152+
await projectValidator.POST(req, res, nextSpy);
153+
154+
expect(sentStatus).to.equal(400);
155+
expect(sentData.err.message).to.equal('"submission" must be a string');
156+
expect(nextSpy.calledOnce).to.be.false;
157+
});
158+
159+
it('should NOT throw an error when mode is missing', async () => {
160+
const req = {
161+
body: {
162+
lang: 'node',
163+
problem: 'https://minio.cb.lk/public/input',
164+
submission: 'https://minio.cb.lk/public/input',
165+
submissionDirs: 'src',
166+
timelimit: 1
167+
}
168+
};
169+
170+
await projectValidator.POST(req, res, nextSpy);
171+
172+
expect(nextSpy.calledOnce).to.be.true;
173+
});
174+
175+
it('should throw an error when mode is NOT string', async () => {
176+
const req = {
177+
body: {
178+
lang: 'node',
179+
problem: 'https://minio.cb.lk/public/input',
180+
submission: 'https://minio.cb.lk/public/input',
181+
submissionDirs: 'src',
182+
mode: 123,
183+
timelimit: 1
184+
}
185+
};
186+
187+
await projectValidator.POST(req, res, nextSpy);
188+
189+
expect(nextSpy.calledOnce).to.be.false;
190+
});
191+
192+
it('should throw an error when mode is not one of "callback, poll, sync"', async () => {
193+
const req = {
194+
body: {
195+
lang: 'node',
196+
problem: 'https://minio.cb.lk/public/input',
197+
submission: 'https://minio.cb.lk/public/input',
198+
submissionDirs: 'src',
199+
mode: 'wrongMode',
200+
timelimit: 1
201+
}
202+
};
203+
204+
205+
await projectValidator.POST(req, res, nextSpy);
206+
207+
expect(nextSpy.calledOnce).to.be.false;
208+
expect(sentData.err.message).to.equal('"mode" must be one of [sync, callback, poll]');
209+
});
210+
211+
it('should throw error if mode is callback and callback is missing', async () => {
212+
const req = {
213+
body: {
214+
lang: 'node',
215+
problem: 'https://minio.cb.lk/public/input',
216+
submission: 'https://minio.cb.lk/public/input',
217+
submissionDirs: 'src',
218+
mode: 'callback',
219+
timelimit: 1
220+
}
221+
};
222+
223+
await projectValidator.POST(req, res, nextSpy);
224+
225+
expect(sentStatus).to.be.equal(400);
226+
expect(sentData.err.message).to.equal('"callback" is required');
227+
expect(nextSpy.calledOnce).to.be.false;
228+
});
229+
230+
it('should throw an error when timelimit is not integer', async () => {
231+
const req = {
232+
body: {
233+
lang: 'node',
234+
problem: 'https://minio.cb.lk/public/input',
235+
submission: 'https://minio.cb.lk/public/input',
236+
submissionDirs: 'src',
237+
mode: 'poll',
238+
timelimit: 'abc'
239+
}
240+
};
241+
242+
await projectValidator.POST(req, res, nextSpy);
243+
244+
expect(sentStatus).to.be.equal(400);
245+
expect(sentData.err.message).to.equal('"timelimit" must be a number');
246+
expect(nextSpy.calledOnce).to.be.false;
247+
});
248+
249+
it('should NOT throw error for correct values', async () => {
250+
const req = {
251+
body: {
252+
lang: 'node',
253+
problem: 'https://minio.cb.lk/public/input',
254+
submission: 'https://minio.cb.lk/public/input',
255+
submissionDirs: 'src',
256+
mode: 'poll',
257+
timelimit: 1
258+
}
259+
};
260+
261+
await projectValidator.POST(req, res, nextSpy);
262+
263+
expect(nextSpy.calledOnce).to.be.true;
264+
});
265+
});

0 commit comments

Comments
 (0)