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