Skip to content

Commit 7e05442

Browse files
committed
add api key tests
1 parent 2530359 commit 7e05442

File tree

2 files changed

+296
-0
lines changed

2 files changed

+296
-0
lines changed

test/apiKey.init.spec.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
const test = require('ava')
2+
const helpers = require('./_helpers')
3+
const fixtures = require('./fixtures')
4+
const apiKey = require('../src/apiKey')
5+
6+
const cfg = helpers.getOptions({ publicKey: fixtures.common.publicKeyJwk })
7+
const mockResponse = {
8+
access_token: 'barfoo'
9+
}
10+
const defaults = {
11+
url: 'http://barfoo.com/foo/bar',
12+
in: 'headers',
13+
prefix: 'Api-Key ',
14+
name: 'authorization',
15+
request: {},
16+
tokenPath: 'access_token'
17+
}
18+
19+
test('Do not replace api key with bearer token because of missing options', async (t) => {
20+
const server = await helpers.getServer(cfg, false)
21+
22+
apiKey.init(server, cfg)
23+
24+
const { result } = await server.inject({
25+
url: '/proxy',
26+
headers: {
27+
authorization: 'Api-Key foobar'
28+
}
29+
})
30+
31+
t.is(result.headers.authorization, 'Api-Key foobar')
32+
t.is(Object.keys(result.query).length, 0)
33+
})
34+
35+
test('Do not replace api key with bearer token because of missing api key', async (t) => {
36+
const server = await helpers.getServer(cfg, false)
37+
38+
apiKey.init(server, Object.assign({
39+
apiKey: defaults
40+
}, cfg))
41+
42+
const { result } = await server.inject({
43+
url: '/proxy'
44+
})
45+
46+
t.falsy(result.headers.authorization)
47+
t.is(Object.keys(result.query).length, 0)
48+
})
49+
50+
test('Do not replace api key with bearer token because of failing request', async (t) => {
51+
helpers.mockApiKey(401, mockResponse, false)
52+
const server = await helpers.getServer(cfg, false)
53+
54+
apiKey.init(server, Object.assign({
55+
apiKey: defaults
56+
}, cfg))
57+
58+
const res = await server.inject({
59+
url: '/proxy',
60+
headers: {
61+
authorization: 'Api-Key foobar'
62+
}
63+
})
64+
65+
t.is(res.statusCode, 401)
66+
t.truthy(res.result.attributes.reason)
67+
})
68+
69+
test('Replace api key with bearer token', async (t) => {
70+
helpers.mockApiKey(200, mockResponse, false)
71+
const server = await helpers.getServer(cfg, false)
72+
73+
apiKey.init(server, Object.assign({
74+
apiKey: defaults
75+
}, cfg))
76+
77+
const { result } = await server.inject({
78+
url: '/proxy',
79+
headers: {
80+
authorization: 'Api-Key foobar'
81+
}
82+
})
83+
84+
t.is(result.headers.authorization, 'Bearer barfoo')
85+
t.is(Object.keys(result.query).length, 0)
86+
})

test/apiKey.spec.js

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
const test = require('ava')
2+
const apiKey = require('../src/apiKey')
3+
4+
test('Get endpoint url without any changes', (t) => {
5+
t.is(apiKey.parseUrl({
6+
apiKey: { url: 'http://barfoo.com/foo/bar' },
7+
clientId: 'bar',
8+
realmUrl: 'http://foobar.com/foo/bar'
9+
}), 'http://barfoo.com/foo/bar')
10+
})
11+
12+
test('Get endpoint url with replaced `clientId`', (t) => {
13+
t.is(apiKey.parseUrl({
14+
apiKey: { url: 'http://barfoo.com/foo/{clientId}' },
15+
clientId: 'bar',
16+
realmUrl: 'http://foobar.com/foo/bar'
17+
}), 'http://barfoo.com/foo/bar')
18+
})
19+
20+
test('Get endpoint url with replaced `realm`', (t) => {
21+
t.is(apiKey.parseUrl({
22+
apiKey: { url: 'http://barfoo.com/foo/{realm}' },
23+
clientId: 'bar',
24+
realmUrl: 'http://foobar.com/foo/bar'
25+
}), 'http://barfoo.com/foo/bar')
26+
})
27+
28+
test('Get endpoint url with replaced unknown placeholder', (t) => {
29+
t.is(apiKey.parseUrl({
30+
apiKey: { url: 'http://barfoo.com/foo/{foobar}' },
31+
clientId: 'bar',
32+
realmUrl: 'http://foobar.com/foo/bar'
33+
}), 'http://barfoo.com/foo/')
34+
})
35+
36+
test('Get no api key if there is neither header nor query', (t) => {
37+
const req = {
38+
headers: {},
39+
query: {}
40+
}
41+
42+
const options = {
43+
in: 'headers',
44+
name: 'authorization',
45+
prefix: 'Api-Key '
46+
}
47+
48+
t.false(apiKey.getApiKey(req, options))
49+
})
50+
51+
test('Get no api key if value is not prefixed', (t) => {
52+
const req = {
53+
headers: {
54+
authorization: 'foobar'
55+
},
56+
query: {}
57+
}
58+
59+
const options = {
60+
in: 'headers',
61+
name: 'authorization',
62+
prefix: 'Api-Key '
63+
}
64+
65+
t.false(apiKey.getApiKey(req, options))
66+
})
67+
68+
test('Get api key if there is there is a prefixed value', (t) => {
69+
const req = {
70+
headers: {
71+
authorization: 'Api-Key foobar'
72+
},
73+
query: {}
74+
}
75+
76+
const options = {
77+
in: 'headers',
78+
name: 'authorization',
79+
prefix: 'Api-Key '
80+
}
81+
82+
const result = apiKey.getApiKey(req, options)
83+
84+
t.truthy(result)
85+
t.is(result, 'Api-Key foobar')
86+
})
87+
88+
test('Get no request options because of missing api key', (t) => {
89+
const req = {
90+
headers: {},
91+
query: {}
92+
}
93+
94+
const options = {
95+
in: 'headers',
96+
name: 'authorization',
97+
prefix: 'Api-Key ',
98+
request: {}
99+
}
100+
101+
t.false(apiKey.getRequestOptions(req, options))
102+
})
103+
104+
test('Get request options with updated header', (t) => {
105+
const req = {
106+
headers: {
107+
authorization: 'Api-Key foobar'
108+
},
109+
query: {}
110+
}
111+
112+
const options = {
113+
in: 'headers',
114+
name: 'authorization',
115+
prefix: 'Api-Key ',
116+
request: {
117+
foo: 'bar'
118+
}
119+
}
120+
121+
t.deepEqual(apiKey.getRequestOptions(req, options), {
122+
foo: 'bar',
123+
headers: {
124+
authorization: 'Api-Key foobar'
125+
}
126+
})
127+
})
128+
129+
test('Get request options with updated query', (t) => {
130+
const req = {
131+
query: {
132+
authorization: 'Api-Key foobar'
133+
},
134+
headers: {}
135+
}
136+
137+
const options = {
138+
in: 'query',
139+
name: 'authorization',
140+
prefix: 'Api-Key ',
141+
request: {
142+
foo: 'bar'
143+
}
144+
}
145+
146+
t.deepEqual(apiKey.getRequestOptions(req, options), {
147+
foo: 'bar',
148+
query: {
149+
authorization: 'Api-Key foobar'
150+
}
151+
})
152+
})
153+
154+
test('Get request options with deeply updated header', (t) => {
155+
const req = {
156+
headers: {
157+
authorization: 'Api-Key foobar'
158+
},
159+
query: {}
160+
}
161+
162+
const options = {
163+
in: 'headers',
164+
name: 'authorization',
165+
prefix: 'Api-Key ',
166+
request: {
167+
foo: 'bar',
168+
headers: {
169+
'x-foo': 'bar'
170+
}
171+
}
172+
}
173+
174+
t.deepEqual(apiKey.getRequestOptions(req, options), {
175+
foo: 'bar',
176+
headers: {
177+
authorization: 'Api-Key foobar',
178+
'x-foo': 'bar'
179+
}
180+
})
181+
})
182+
183+
test('Get request options with deeply updated query', (t) => {
184+
const req = {
185+
query: {
186+
authorization: 'Api-Key foobar'
187+
},
188+
headers: {}
189+
}
190+
191+
const options = {
192+
in: 'query',
193+
name: 'authorization',
194+
prefix: 'Api-Key ',
195+
request: {
196+
foo: 'bar',
197+
query: {
198+
'x-foo': 'bar'
199+
}
200+
}
201+
}
202+
203+
t.deepEqual(apiKey.getRequestOptions(req, options), {
204+
foo: 'bar',
205+
query: {
206+
authorization: 'Api-Key foobar',
207+
'x-foo': 'bar'
208+
}
209+
})
210+
})

0 commit comments

Comments
 (0)