Skip to content

Commit e9cc06b

Browse files
committed
Update unit testing for website and all api versions
1 parent 1af4b3a commit e9cc06b

10 files changed

Lines changed: 517 additions & 425 deletions

File tree

spec/api/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const expect = require('chai').expect;
2+
const request = require('supertest');
3+
const settings = require('../../settings');
4+
5+
module.exports = (server) => {
6+
it('should return 200 when visiting api route', async () => {
7+
const res = await request(server).get('/api')
8+
expect(res.status).to.equal(200);
9+
});
10+
11+
it('should use latest version when hitting default api route', async () => {
12+
const res = await request(server).get('/api')
13+
const result = JSON.parse(res.text);
14+
expect(result.info.version).to.equal(settings.latestVersion);
15+
});
16+
}

spec/api/legacy/index.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const expect = require('chai').expect;
2+
const request = require('supertest');
3+
const settings = require('../../../settings');
4+
5+
// No test coverage for 0.7 & 0.8 (php versions)
6+
const versions = [
7+
"0.1", "0.2", "0.2.1", "0.3",
8+
"0.3.1", "0.3.2", "0.4", "0.4.1",
9+
"0.5", "0.6",
10+
];
11+
module.exports = (server) => {
12+
versions.forEach((version) => {
13+
describe(version, () => {
14+
suite(version);
15+
});
16+
});
17+
18+
// For legacy versions, just make sure routes are accessible with provided RandomAPI hashes in routes/api
19+
function suite(version) {
20+
it(`should return 200 when visiting api route (/api/${version})`, async () => {
21+
const res = await request(server).get(`/api/${version}`)
22+
expect(res.status).to.equal(200);
23+
});
24+
25+
it(`should return correct versioned response when visiting api route (/api/${version})`, async () => {
26+
const res = await request(server).get(`/api/${version}`)
27+
const result = JSON.parse(res.text);
28+
expect(versions.slice(-3).indexOf(version) !== -1 ? result.results[0].user.version : result.results[0].version).to.equal(version);
29+
});
30+
31+
it(`should return the same user object when given the same seed`, async () => {
32+
// First request to get the initial seed
33+
const req1 = await request(server).get(`/api/${version}`);
34+
const req1Obj = JSON.parse(req1.text);
35+
const seed = req1Obj.results[0].seed;
36+
37+
// Make another request using seed and make sure JSON objects are the same
38+
const req2 = await request(server).get(`/api/${version}?seed=${seed}`);
39+
40+
expect(req1.text).to.equal(req2.text);
41+
});
42+
}
43+
};

spec/api/modern/1.0.js

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
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+
};

spec/api/modern/1.1.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
const expect = require('chai').expect;
2+
const request = require('supertest');
3+
const settings = require('../../../settings');
4+
const defVersion = "1.1";
5+
6+
module.exports = (server, version=defVersion) => {
7+
8+
// DoB and registration overlap
9+
describe(`DoB and registration overlap`, () => {
10+
it(`DoB should be before registration dates`, async () => {
11+
const res = await request(server).get(`/api/${version}?results=5000`);
12+
const result = JSON.parse(res.text);
13+
14+
expect(result.results.every(i => {
15+
if (version === "1.0" || version === "1.1") {
16+
return new Date(i.dob) < new Date(i.registered);
17+
} else {
18+
return new Date(i.dob.date) < new Date(i.registered.date);
19+
}
20+
})).to.equal(true);
21+
});
22+
});
23+
24+
// Password features
25+
describe(`Password features`, () => {
26+
const charSets = {
27+
special: new Set(`!"#$%&'()*+,- ./:;<=>?@[\\]^_\`{|}~`),
28+
upper: new Set(`ABCDEFGHIJKLMNOPQRSTUVWXYZ`),
29+
lower: new Set(`abcdefghijklmnopqrstuvwxyz`),
30+
number: new Set(`0123456789`),
31+
};
32+
33+
Object.keys(charSets).forEach(set => {
34+
it(`should support ${set} charset`, async () => {
35+
const res = await request(server).get(`/api/${version}?password=${set}&results=50`);
36+
const result = JSON.parse(res.text);
37+
38+
expect(result.results.every(i => {
39+
return i.login.password.split('').every(c => {
40+
return charSets[set].has(c);
41+
});
42+
})).to.equal(true);
43+
});
44+
});
45+
46+
for (let i = 0; i < 5; i++) {
47+
let min = Math.floor(Math.random() * 15) + 1;
48+
let max = min + Math.floor(Math.random() * 20);
49+
50+
it(`should support passwords using all charsets with range ${min} to ${max} [check ${i+1} of 5]`, async () => {
51+
const res = await request(server).get(`/api/${version}?password=special,upper,lower,number,${min}-${max}&results=50`);
52+
const result = JSON.parse(res.text);
53+
54+
expect(result.results.every(i => {
55+
return i.login.password.length >= min && i.login.password.length <= max;
56+
})).to.equal(true);
57+
});
58+
}
59+
});
60+
61+
// 1.1 should support all previous version features as well
62+
describe(`Checking support of previous version features`, () => {
63+
describe('1.0', require('./1.0').bind(this, server, version));
64+
});
65+
66+
};

spec/api/modern/1.2.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const expect = require('chai').expect;
2+
const request = require('supertest');
3+
const settings = require('../../../settings');
4+
const defVersion = "1.2";
5+
6+
module.exports = (server, version=defVersion) => {
7+
8+
// Norway check
9+
describe(`Norway check`, () => {
10+
let nat = 'NO';
11+
it(`should retrieve ${nat} nat when specified`, async () => {
12+
const res = await request(server).get(`/api/${version}?nat=${nat}`);
13+
const result = JSON.parse(res.text);
14+
expect(result.results[0].nat).to.equal(nat);
15+
});
16+
});
17+
18+
// 1.2 should support all previous version features as well
19+
describe(`Checking support of previous version features`, () => {
20+
describe('1.1', require('./1.1').bind(this, server, version));
21+
});
22+
23+
};

spec/api/modern/1.3.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const expect = require('chai').expect;
2+
const request = require('supertest');
3+
const settings = require('../../../settings');
4+
const defVersion = "1.3";
5+
6+
module.exports = (server, version=defVersion) => {
7+
8+
// 1.3 should support all previous version features as well
9+
describe(`Checking support of previous version features`, () => {
10+
describe('1.2', require('./1.2').bind(this, server, version));
11+
});
12+
13+
};

0 commit comments

Comments
 (0)