-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathbackend_client_spec.lua
417 lines (342 loc) · 14.8 KB
/
backend_client_spec.lua
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
local _M = require('apicast.backend_client')
local configuration = require('apicast.configuration')
local http_ng = require 'resty.http_ng'
local http_ng_resty = require 'resty.http_ng.backend.resty'
local ReportsBatch = require 'apicast.policy.3scale_batcher.reports_batch'
local backend_calls_metrics = require 'apicast.metrics.3scale_backend_calls'
function encode(data)
tablex = require "pl.tablex"
if tablex.size(data) == 0 then
return ""
end
local result = ""
local first = true
for k,v in tablex.sort(data) do
local prefix = "&"
if first then
prefix = ""
first = false
end
result = string.format("%s%s%s=%s",result, prefix, k, v)
end
return result
end
describe('backend client', function()
local test_backend
local options_header_oauth_native = 'rejection_reason_header=1&limit_headers=1'
local options_header_no_oauth_native = 'rejection_reason_header=1&limit_headers=1&no_body=1'
before_each(function()
stub(ngx, 'encode_args', encode)
test_backend = http_ng.backend()
stub(backend_calls_metrics, 'report')
end)
describe('authrep', function()
it('works without params', function()
local service = configuration.parse_service({
id = '42', backend_version = '1',
proxy = {
backend = { endpoint = 'http://example.com' },
},
backend_authentication_type = 'auth', backend_authentication_value = 'val'
})
test_backend.expect{
url = 'http://example.com/transactions/authrep.xml?' ..
ngx.encode_args({ auth = service.backend_authentication.value, service_id = service.id }),
headers = { host = 'example.com',
['3scale-options'] = options_header_no_oauth_native }
}.respond_with{ status = 200 }
local backend_client = assert(_M:new(service, test_backend))
local res = backend_client:authrep()
assert.equal(200, res.status)
end)
it('passes params along', function()
local service = configuration.parse_service({
id = '42', backend_version = '1',
proxy = {
backend = { endpoint = 'http://example.com' },
},
backend_authentication_type = 'auth', backend_authentication_value = 'val'
})
test_backend.expect{
url = 'http://example.com/transactions/authrep.xml?' ..
string.format("auth=val&service_id=42&usage[hits]=1&user_key=foobar"),
headers = { ['3scale-options'] = options_header_no_oauth_native }
}.respond_with{ status = 200 }
local backend_client = assert(_M:new(service, test_backend))
local res = backend_client:authrep({ ['usage[hits]'] = 1 }, { user_key = 'foobar' })
assert.equal(200, res.status)
end)
it('can override backend host header', function()
local service = configuration.parse_service({
id = '42', backend_version = '1',
proxy = {
backend = { endpoint = 'http://example.com', host = 'foo.example.com' },
}
})
test_backend.expect{
url = 'http://example.com/transactions/authrep.xml?service_id=42',
headers = { host = 'foo.example.com',
['3scale-options'] = options_header_no_oauth_native }
}.respond_with{ status = 200 }
local backend_client = assert(_M:new(service, test_backend))
local res = backend_client:authrep()
assert.equal(200, res.status)
end)
it('detects oauth native', function()
local service = configuration.parse_service({
id = '42', backend_version = 'oauth',
proxy = {
backend = { endpoint = 'http://example.com' },
}
})
test_backend.expect{
url = 'http://example.com/transactions/oauth_authrep.xml?service_id=42',
headers = { ['3scale-options'] = options_header_oauth_native }
}.respond_with{ status = 200 }
local backend_client = assert(_M:new(service, test_backend))
local res = backend_client:authrep()
assert.equal(200, res.status)
end)
it('detects OIDC', function()
local service = configuration.parse_service({
id = '42', backend_version = 'oauth',
proxy = {
authentication_method = 'oidc',
backend = { endpoint = 'http://example.com' },
}
})
test_backend.expect{
url = 'http://example.com/transactions/oauth_authrep.xml?service_id=42',
headers = { ['3scale-options'] = options_header_no_oauth_native }
}.respond_with{ status = 200 }
local backend_client = assert(_M:new(service, test_backend))
local res = backend_client:authrep()
assert.equal(200, res.status)
end)
it('reports the call with the status', function()
local service = configuration.parse_service({ id = '42' })
local status = 200
test_backend.expect({}).respond_with({ status = status })
local backend_client = assert(_M:new(service, test_backend))
backend_client:authrep()
assert.stub(backend_calls_metrics.report).was_called_with('authrep', status)
end)
end)
describe('authorize', function()
it('works without params', function()
local service = configuration.parse_service({
id = '42', backend_version = '1',
proxy = {
backend = { endpoint = 'http://example.com' },
},
backend_authentication_type = 'auth', backend_authentication_value = 'val'
})
test_backend.expect{
url = 'http://example.com/transactions/authorize.xml?' ..
ngx.encode_args({ service_id = service.id, auth = service.backend_authentication.value }),
headers = { host = 'example.com',
['3scale-options'] = options_header_no_oauth_native }
}.respond_with{ status = 200 }
local backend_client = assert(_M:new(service, test_backend))
local res = backend_client:authorize()
assert.equal(200, res.status)
end)
it('passes params along', function()
local service = configuration.parse_service({
id = '42', backend_version = '1',
proxy = {
backend = { endpoint = 'http://example.com' },
},
backend_authentication_type = 'auth', backend_authentication_value = 'val'
})
test_backend.expect{
url = 'http://example.com/transactions/authorize.xml?' ..
string.format("auth=%s&service_id=%s&usage[hits]=1&user_key=foobar",service.backend_authentication.value, service.id),
headers = { ['3scale-options'] = options_header_no_oauth_native }
}.respond_with{ status = 200 }
local backend_client = assert(_M:new(service, test_backend))
local res = backend_client:authorize({ ['usage[hits]'] = 1 }, { user_key = 'foobar' })
assert.equal(200, res.status)
end)
it('can override backend host header', function()
local service = configuration.parse_service({
id = '42', backend_version = '1',
proxy = {
backend = { endpoint = 'http://example.com', host = 'foo.example.com' },
}
})
test_backend.expect{
url = 'http://example.com/transactions/authorize.xml?service_id=42',
headers = { host = 'foo.example.com',
['3scale-options'] = options_header_no_oauth_native }
}.respond_with{ status = 200 }
local backend_client = assert(_M:new(service, test_backend))
local res = backend_client:authorize()
assert.equal(200, res.status)
end)
it('detects oauth', function()
local service = configuration.parse_service({
id = '42', backend_version = 'oauth',
proxy = {
backend = { endpoint = 'http://example.com' },
}
})
test_backend.expect{
url = 'http://example.com/transactions/oauth_authorize.xml?service_id=42',
headers = { ['3scale-options'] = options_header_oauth_native }
}.respond_with{ status = 200 }
local backend_client = assert(_M:new(service, test_backend))
local res = backend_client:authorize()
assert.equal(200, res.status)
end)
it('detects oauth', function()
local service = configuration.parse_service({
id = '42', backend_version = 'oauth',
proxy = {
authentication_method = 'oidc',
backend = { endpoint = 'http://example.com' },
}
})
test_backend.expect{
url = 'http://example.com/transactions/oauth_authorize.xml?service_id=42',
headers = { ['3scale-options'] = options_header_no_oauth_native }
}.respond_with{ status = 200 }
local backend_client = assert(_M:new(service, test_backend))
local res = backend_client:authorize()
assert.equal(200, res.status)
end)
it('reports the call with the status', function()
local service = configuration.parse_service({ id = '42' })
local status = 200
test_backend.expect({}).respond_with({ status = status })
local backend_client = assert(_M:new(service, test_backend))
backend_client:authorize()
assert.stub(backend_calls_metrics.report).was_called_with('auth', status)
end)
it('retries when auth return a "closed" error and uses the "resty" http backend', function()
local service = configuration.parse_service({ id = '42' })
local backend_client = assert(_M:new(service, http_ng_resty))
stub(backend_client.http_client, 'get').returns(
{ status = 0, error = 'closed' }
)
backend_client:authorize()
assert.stub(backend_client.http_client.get).was_called(2)
end)
end)
describe('report', function()
describe('when the service is configured to use app IDs', function()
it('makes the call to backend with the right params', function()
local service = configuration.parse_service({
id = '42',
backend_version = '2',
proxy = { backend = { endpoint = 'http://example.com' } },
backend_authentication_type = 'auth', backend_authentication_value = 'val'
})
-- It's tricky to test with several reports because they can go in
-- any order in the request.
local reports = { { app_id = 'id1', metric = 'm1', value = 1 } }
local transactions = {}
transactions["transactions[0][app_id]"] = 'id1'
transactions["transactions[0][usage][m1]"] = 1
local reports_batch = ReportsBatch.new(service.id, reports)
test_backend.expect{
url = 'http://example.com/transactions.xml?' ..
ngx.encode_args({ service_id = service.id,
auth = service.backend_authentication.value}),
body = ngx.encode_args(transactions)
}.respond_with{ status = 200 }
local backend_client = assert(_M:new(service, test_backend))
local res = backend_client:report(reports_batch)
assert.equal(200, res.status)
end)
end)
describe('when the service is configured to use user keys', function()
it('makes the call to backend with the right params', function()
local service = configuration.parse_service({
id = '42',
backend_version = '1',
proxy = { backend = { endpoint = 'http://example.com' } },
backend_authentication_type = 'auth', backend_authentication_value = 'val'
})
-- It's tricky to test with several reports because they can go in
-- any order in the request.
local reports = { { user_key = 'uk1', metric = 'm1', value = 1 } }
local transactions = {}
transactions["transactions[0][user_key]"] = 'uk1'
transactions["transactions[0][usage][m1]"] = 1
local reports_batch = ReportsBatch.new(service.id, reports)
test_backend.expect{
url = 'http://example.com/transactions.xml?' ..
ngx.encode_args({ service_id = service.id,
auth = service.backend_authentication.value}),
body = ngx.encode_args(transactions)
}.respond_with{ status = 200 }
local backend_client = assert(_M:new(service, test_backend))
local res = backend_client:report(reports_batch)
assert.equal(200, res.status)
end)
end)
describe('when the service is configured to use oauth tokens', function()
it('makes the call to backend with the right params', function()
local service = configuration.parse_service({
id = '42',
backend_version = 'oauth',
proxy = { backend = { endpoint = 'http://example.com' } },
backend_authentication_type = 'auth', backend_authentication_value = 'val'
})
-- It's tricky to test with several reports because they can go in
-- any order in the request.
local reports = { { access_token = 'token', metric = 'm1', value = 1 } }
local transactions = {}
transactions["transactions[0][access_token]"] = 'token'
transactions["transactions[0][usage][m1]"] = 1
local reports_batch = ReportsBatch.new(service.id, reports)
test_backend.expect{
url = 'http://example.com/transactions.xml?' ..
ngx.encode_args({ service_id = service.id,
auth = service.backend_authentication.value}),
body = ngx.encode_args(transactions)
}.respond_with{ status = 200 }
local backend_client = assert(_M:new(service, test_backend))
local res = backend_client:report(reports_batch)
assert.equal(200, res.status)
end)
end)
it('reports the call with the status', function()
local service = configuration.parse_service({ id = '42' })
local status = 200
test_backend.expect({}).respond_with({ status = status })
local backend_client = assert(_M:new(service, test_backend))
backend_client:report(ReportsBatch.new(service.id, {}))
assert.stub(backend_calls_metrics.report).was_called_with('report', status)
end)
end)
describe('store_oauth_token', function()
it('makes the right call to backend', function()
local service_id = '42'
local service = configuration.parse_service({
id = service_id,
backend_version = 'oauth',
proxy = {
backend = { endpoint = 'http://example.com' },
},
backend_authentication_type = 'service_token',
backend_authentication_value = '123'
})
test_backend.expect{
-- Notice that the service_id appears twice, but it's not a problem.
url = 'http://example.com/services/42/'..
'oauth_access_tokens.xml?service_id=42&service_token=123',
body = 'app_id=an_app_id&token=my_token&ttl=3600&user_id=a_user_id'
}.respond_with{ status = 200 }
local backend_client = assert(_M:new(service, test_backend))
local res = backend_client:store_oauth_token({
app_id = 'an_app_id',
token = 'my_token',
ttl = 3600,
user_id = 'a_user_id',
})
assert.equal(200, res.status)
end)
end)
end)