Skip to content

Commit 2fdf791

Browse files
committed
feat(unit-tests): add tests for API Validator
1 parent 29dca6f commit 2fdf791

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import * as utils from '../../utils/utils';
2+
import {checkValidApiKey} from '../../../src/validators/ApiKeyValidators';
3+
import {Request} from 'express'
4+
import DB from "../../../src/models";
5+
6+
const chai = require('chai');
7+
const chaiAsPromised = require('chai-as-promised');
8+
const {expect} = chai;
9+
chai.use(chaiAsPromised);
10+
11+
const APIKEY = '7718330d2794406c980bdbded6c9dc1d';
12+
13+
describe('API Key Validtors', async () => {
14+
beforeEach(async () => {
15+
await DB.apikeys.bulkCreate([
16+
{key: APIKEY, whitelist_domains: ['*'], whitelist_ips: ['*']}
17+
]);
18+
});
19+
afterEach(utils.truncateTables);
20+
21+
it('should reject an invalid api', () => {
22+
// @ts-ignore
23+
const req: Request = {
24+
header(name): any {
25+
if (name === 'Authorization') {
26+
return 'Bearer someWrongAPI'
27+
}
28+
}
29+
};
30+
31+
expect(checkValidApiKey(req)).to.be.rejectedWith('Invalid API Key');
32+
});
33+
34+
it('should reject no api', () => {
35+
// @ts-ignore
36+
const req: Request = {
37+
header(name): any {
38+
}
39+
};
40+
41+
expect(checkValidApiKey(req)).to.be.rejectedWith('No API Key in request');
42+
});
43+
44+
it('should reject api without whitelist dommain/ip', async () => {
45+
const currentKey = 'SDLKJFLSJDKCWEKRJC';
46+
await DB.apikeys.bulkCreate([
47+
{key: currentKey}
48+
]);
49+
50+
// @ts-ignore
51+
const req: Request = {
52+
header(name): any {
53+
if (name === 'Authorization') {
54+
return `Bearer ${currentKey}`
55+
}
56+
}
57+
};
58+
59+
expect(checkValidApiKey(req)).to.be.rejectedWith('IP or Domain not in whitelist');
60+
});
61+
62+
it('should NOT reject api with a whitelist ip', async () => {
63+
const currentKey = 'SDLKJFLSJDKCWEKRJC';
64+
const remoteAddress = '10.9.2.41';
65+
66+
await DB.apikeys.bulkCreate([
67+
{key: currentKey, whitelist_ips: [remoteAddress]}
68+
]);
69+
70+
// @ts-ignore
71+
const req: Request = {
72+
header(name): any {
73+
if (name === 'Authorization') {
74+
return `Bearer ${currentKey}`
75+
}
76+
}
77+
};
78+
// @ts-ignore
79+
req.connection = {
80+
remoteAddress
81+
};
82+
83+
expect(checkValidApiKey(req)).to.not.be.rejected;
84+
});
85+
86+
it('should NOT reject api with a whitelist domain', async () => {
87+
const currentKey = 'SDLKJFLSJDKCWEKRJC';
88+
89+
await DB.apikeys.bulkCreate([
90+
{key: currentKey, whitelist_domains: ['Referer']}
91+
]);
92+
93+
// @ts-ignore
94+
const req: Request = {
95+
header(name): any {
96+
if (name === 'Authorization') {
97+
return `Bearer ${currentKey}`
98+
}
99+
}
100+
};
101+
102+
// TODO code is wrong
103+
// expect(checkValidApiKey(req)).to.not.be.rejected;
104+
});
105+
106+
it('should NOT reject api with whitelist ip/domain as "*"', async () => {
107+
// @ts-ignore
108+
const req: Request = {
109+
header(name): any {
110+
if (name === 'Authorization') {
111+
return `Bearer ${APIKEY}`
112+
}
113+
}
114+
};
115+
116+
expect(checkValidApiKey(req)).to.not.be.rejected;
117+
})
118+
});

0 commit comments

Comments
 (0)