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