1+ const expect = require ( 'chai' ) . expect ;
2+ const request = require ( 'supertest' ) ;
3+ const parse = require ( 'csv-parse' ) ;
4+ const settings = require ( '../../../settings' ) ;
5+ const defVersion = "1.0" ;
6+
7+ module . exports = ( server , version = defVersion ) => {
8+
9+ const fields = [ 'gender' , 'name' , 'location' , 'email' ,
10+ 'login' , 'registered' , 'dob' , 'phone' ,
11+ 'cell' , 'id' , 'picture' , 'nat'
12+ ] ;
13+
14+ const nats = [
15+ 'AU' , 'BR' , 'CA' , 'CH' ,
16+ 'DE' , 'DK' , 'ES' , 'FI' ,
17+ 'FR' , 'GB' , 'IE' , 'IR' ,
18+ 'NL' , 'NZ' , 'TR' , 'US'
19+ ] ;
20+
21+ // Include fields
22+ describe ( `Include fields` , ( ) => {
23+ for ( let i = 0 ; i < 5 ; i ++ ) {
24+ let copy = [ ...fields ] ;
25+ let chosen = new Array ( 5 ) . fill ( ) . map ( i => {
26+ return copy . splice ( Math . floor ( Math . random ( ) * copy . length ) , 1 ) [ 0 ] ;
27+ } ) . sort ( ) . join ( ',' ) ;
28+
29+ it ( `should return only included fields [check ${ i + 1 } of 5] using fields (${ chosen } )` , async ( ) => {
30+ const res = await request ( server ) . get ( `/api/${ version } ?inc=${ chosen } ` ) ;
31+ const result = JSON . parse ( res . text ) ;
32+ expect ( Object . keys ( result . results [ 0 ] ) . sort ( ) . join ( ',' ) ) . to . equal ( chosen ) ;
33+ } ) ;
34+ }
35+ } ) ;
36+
37+ // Exclude fields
38+ describe ( `Exclude fields` , ( ) => {
39+ for ( let i = 0 ; i < 5 ; i ++ ) {
40+ let copy = [ ...fields ] ;
41+ let chosen = new Array ( 5 ) . fill ( ) . map ( i => {
42+ return copy . splice ( Math . floor ( Math . random ( ) * copy . length ) , 1 ) [ 0 ] ;
43+ } ) . sort ( ) . join ( ',' ) ;
44+
45+ it ( `should return all fields except excluded fields [check ${ i + 1 } of 5] using fields (${ chosen } )` , async ( ) => {
46+ const res = await request ( server ) . get ( `/api/${ version } ?exc=${ chosen } ` ) ;
47+ const result = JSON . parse ( res . text ) ;
48+ expect ( Object . keys ( result . results [ 0 ] ) . sort ( ) . join ( ',' ) ) . to . equal ( copy . sort ( ) . toString ( ',' ) ) ;
49+ } ) ;
50+ }
51+ } ) ;
52+
53+ // Request multiple nats
54+ describe ( `Request multiple nats` , ( ) => {
55+ for ( let i = 0 ; i < 5 ; i ++ ) {
56+ let copy = [ ...nats ] ;
57+ let chosen = new Array ( 3 ) . fill ( ) . map ( i => {
58+ return copy . splice ( Math . floor ( Math . random ( ) * copy . length ) , 1 ) [ 0 ] ;
59+ } ) . sort ( ) . join ( ',' ) ;
60+
61+ it ( `should return multiple nats [check ${ i + 1 } of 5] using nats (${ chosen } )` , async ( ) => {
62+ const res = await request ( server ) . get ( `/api/${ version } ?nat=${ chosen } &results=100` ) ;
63+ const result = JSON . parse ( res . text ) ;
64+ let resultNats = result . results . reduce ( ( acc , cur ) => {
65+ acc . add ( cur . nat )
66+ return acc ;
67+ } , new Set ( ) ) ;
68+ expect ( [ ...resultNats ] . sort ( ) . join ( ',' ) ) . to . equal ( chosen ) ;
69+ } ) ;
70+ }
71+ } ) ;
72+
73+ // Pretty json
74+ describe ( `Pretty JSON format` , ( ) => {
75+ it ( `should return output in nicely formatted json` , async ( ) => {
76+ const res = await request ( server ) . get ( `/api/${ version } ?fmt=pretty` ) ;
77+ expect ( res . text . slice ( 0 , 22 ) ) . to . equal ( `{\n \"results\": [\n {` ) ;
78+ } ) ;
79+ } ) ;
80+
81+ // Fetch limits
82+ describe ( `Fetch Limits` , ( ) => {
83+ it ( `should fetch 5,000 users in 1 request` , async ( ) => {
84+ const res = await request ( server ) . get ( `/api/${ version } ?results=5000` ) ;
85+ const result = JSON . parse ( res . text ) ;
86+ expect ( result . results . length ) . to . equal ( 5000 ) ;
87+ } ) ;
88+
89+ it ( `should return 1 request when more than 5,000 users are requested` , async ( ) => {
90+ const res = await request ( server ) . get ( `/api/${ version } ?results=5001` ) ;
91+ const result = JSON . parse ( res . text ) ;
92+ expect ( result . results . length ) . to . equal ( 1 ) ;
93+ } ) ;
94+ } ) ;
95+
96+ describe ( 'Format parameter testing' , ( ) => {
97+ it ( 'should return JSON when JSON format specified' , async ( ) => {
98+ const res = await request ( server ) . get ( `/api/?fmt=json` ) ;
99+ const result = res . text ;
100+ try {
101+ JSON . parse ( result ) ;
102+ } catch ( e ) {
103+ throw e ;
104+ }
105+ } ) ;
106+
107+ it ( 'should return JSON when prettyjson format specified' , async ( ) => {
108+ const res = await request ( server ) . get ( `/api/?fmt=prettyjson` ) ;
109+ const result = res . text ;
110+ try {
111+ JSON . parse ( result ) ;
112+ } catch ( e ) {
113+ throw e ;
114+ }
115+ } ) ;
116+
117+ it ( 'should return JSON when pretty format specified' , async ( ) => {
118+ const res = await request ( server ) . get ( `/api/?fmt=pretty` ) ;
119+ const result = res . text ;
120+ try {
121+ JSON . parse ( result ) ;
122+ } catch ( e ) {
123+ throw e ;
124+ }
125+ } ) ;
126+
127+ it ( 'should return JSON when invalid format specified' , async ( ) => {
128+ const res = await request ( server ) . get ( `/api/?fmt=blahblah` ) ;
129+ const result = res . text ;
130+ try {
131+ JSON . parse ( result ) ;
132+ } catch ( e ) {
133+ throw e ;
134+ }
135+ } ) ;
136+
137+ it ( 'should return CSV when CSV format specified' , async ( ) => {
138+ const res = await request ( server ) . get ( `/api/?fmt=csv` ) ;
139+ const result = res . text ;
140+ parse ( res . text , ( err , text ) => {
141+ if ( err ) throw err ;
142+ } ) ;
143+ } ) ;
144+
145+ it ( 'should return content type text/xml when XML format specified' , async ( ) => {
146+ const res = await request ( server ) . get ( `/api/?fmt=xml` ) ;
147+ expect ( res . header [ 'content-type' ] ) . to . equal ( "text/xml; charset=utf-8" ) ;
148+ } ) ;
149+
150+ it ( 'should return content type text/x-yaml when YAML format specified' , async ( ) => {
151+ const res = await request ( server ) . get ( `/api/?fmt=yaml` ) ;
152+ expect ( res . header [ 'content-type' ] ) . to . equal ( "text/x-yaml; charset=utf-8" ) ;
153+ } ) ;
154+
155+ it ( 'should return content type text/csv when CSV format specified' , async ( ) => {
156+ const res = await request ( server ) . get ( `/api/?fmt=csv` ) ;
157+ expect ( res . header [ 'content-type' ] ) . to . equal ( "text/csv; charset=utf-8" ) ;
158+ } ) ;
159+ } ) ;
160+
161+ // Nat check
162+ describe ( `Nat check` , ( ) => {
163+ for ( let i = 0 ; i < nats . length ; i ++ ) {
164+ let nat = nats [ i ] ;
165+ it ( `should retrieve ${ nat } nat when specified` , async ( ) => {
166+ const res = await request ( server ) . get ( `/api/${ version } ?nat=${ nat } ` ) ;
167+ const result = JSON . parse ( res . text ) ;
168+ expect ( result . results [ 0 ] . nat ) . to . equal ( nat ) ;
169+ } ) ;
170+ }
171+
172+ // Special Lego check
173+ it ( `should retrieve lego nat when lego parameter is specified` , async ( ) => {
174+ const res = await request ( server ) . get ( `/api/${ version } ?lego` ) ;
175+ const result = JSON . parse ( res . text ) ;
176+ expect ( result . results [ 0 ] . nat ) . to . equal ( 'LEGO' ) ;
177+ } ) ;
178+
179+ // Invalid nat check
180+ it ( `should retrieve random nat when invalid nat is specified` , async ( ) => {
181+ const res = await request ( server ) . get ( `/api/${ version } ?nat=blah` ) ;
182+ const result = JSON . parse ( res . text ) ;
183+ expect ( nats ) . to . include ( result . results [ 0 ] . nat ) ;
184+ } ) ;
185+ } ) ;
186+ } ;
0 commit comments