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
+ } ) ;
0 commit comments