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/langs' , ( ) => {
14+ before ( async ( ) => {
15+ await DB . langs . bulkCreate ( [
16+ { lang_slug : 'py2' , lang_name : 'Python 2' , lang_version : '2.7' } ,
17+ { lang_slug : 'py3' , lang_name : 'Python 3' , lang_version : '3.6' } ,
18+ { lang_slug : 'java8' , lang_name : 'Java' , lang_version : '1.8' }
19+ ] ) ;
20+ await DB . apikeys . bulkCreate ( [
21+ { id : 1 , key : APIKEY , whitelist_domains : [ '*' ] , whitelist_ips : [ '*' ] }
22+ ] ) ;
23+ } ) ;
24+ after ( utils . truncateTables ) ;
25+
26+
27+ it ( 'should throw 403 error API key is absent in the request' , async ( ) => {
28+ const res = await chai . request ( app ) . get ( `/api/langs` ) ;
29+ expect ( res . status ) . to . equal ( 403 ) ;
30+ // TODO this should be res.body.err maybe? (consistent with other error)
31+ expect ( res . body . message ) . to . equal ( 'No API Key in request' ) ;
32+ } ) ;
33+
34+ it ( 'should throw error if incorrect API key is present' , async ( ) => {
35+ const res = await chai . request ( app ) . get ( '/api/langs' ) . set ( {
36+ 'Authorization' : 'Bearer incorrectAPI-KEY' ,
37+ Accept : 'application/json'
38+ } ) ;
39+ expect ( res . status ) . to . equal ( 403 ) ;
40+ expect ( res . body . message ) . to . equal ( 'Invalid API Key' ) ;
41+ // maybe 400 error?
42+ } ) ;
43+
44+ it ( 'should throw 404 error if POST request is made' , async ( ) => {
45+ const res = await chai . request ( app ) . post ( '/api/langs' ) . set ( {
46+ Authorization : 'Bearer 7718330d2794406c980bdbded6c9dc1d' ,
47+ Accept : 'application/json'
48+ } ) ;
49+ expect ( res . status ) . to . equal ( 404 ) ;
50+ } ) ;
51+
52+ it ( 'should return all the languages when correct request is made' , async ( ) => {
53+ const res = await chai . request ( app ) . get ( '/api/langs' ) . set ( {
54+ Authorization : `Bearer ${ APIKEY } ` ,
55+ Accept : 'application/json'
56+ } ) ;
57+
58+ expect ( res . status ) . to . equal ( 200 ) ;
59+ const languages = res . body ;
60+ expect ( languages . length ) . to . equal ( 3 ) ;
61+ languages . forEach ( ( language ) => {
62+ expect ( language ) . to . have . keys (
63+ 'lang_slug' ,
64+ 'lang_name' ,
65+ 'lang_version'
66+ ) ;
67+ } )
68+ } ) ;
69+ } ) ;
0 commit comments