forked from 3scale/APIcast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauths_cache_spec.lua
More file actions
130 lines (95 loc) · 4.23 KB
/
Copy pathauths_cache_spec.lua
File metadata and controls
130 lines (95 loc) · 4.23 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
local AuthsCache = require 'apicast.policy.3scale_batcher.auths_cache'
local Usage = require 'apicast.usage'
local Transaction = require 'apicast.policy.3scale_batcher.transaction'
local lrucache =require 'resty.lrucache'
local storage
local cache
local usage
local service_id = 's1'
local auth_status = 200
describe('Auths cache', function()
before_each(function()
storage = lrucache.new(100)
cache = AuthsCache.new(storage)
usage = Usage.new()
usage:add('m1', 1)
end)
it('caches auth with user key', function()
local user_key = { user_key = 'uk' }
local transaction = Transaction.new(service_id, user_key, usage)
cache:set(transaction, auth_status)
local cached_status = cache:get(transaction)
assert.equals(auth_status, cached_status)
end)
it('caches auth with app id + app key', function()
local app_id_and_key = { app_id = 'an_id', app_key = 'a_key' }
local transaction = Transaction.new(service_id, app_id_and_key, usage)
cache:set(transaction, auth_status)
local cached_status = cache:get(transaction)
assert.equals(auth_status, cached_status)
end)
it('caches auth with access token', function()
local access_token = { access_token = 'a_token' }
local transaction = Transaction.new(service_id, access_token, usage)
cache:set(transaction, auth_status)
local cached_status = cache:get(transaction)
assert.equals(auth_status, cached_status)
end)
it('caches auths with same usages but different order in the same key', function()
local usage_order_1 = Usage.new()
usage_order_1:add('m1', 1)
usage_order_1:add('m2', 1)
local usage_order_2 = Usage.new()
usage_order_2:add('m2', 1)
usage_order_2:add('m1', 1)
local user_key = { user_key = 'uk' }
local transaction_with_order_1 = Transaction.new(service_id, user_key, usage_order_1)
local transaction_with_order_2 = Transaction.new(service_id, user_key, usage_order_2)
cache:set(transaction_with_order_1, auth_status)
local cached_status = cache:get(transaction_with_order_2)
assert.equals(auth_status, cached_status)
end)
it('caches a rejection reason when given', function()
local rejection_reason = 'limits_exceeded'
local app_id_and_key = { app_id = 'an_id', app_key = 'a_key' }
local transaction = Transaction.new(service_id, app_id_and_key, usage)
local not_authorized_status = 409
cache:set(transaction, not_authorized_status, rejection_reason)
local cached_status, cached_rejection_reason = cache:get(transaction)
assert.equals(not_authorized_status, cached_status)
assert.equals(rejection_reason, cached_rejection_reason)
end)
it('returns nil when something is not cached', function()
local user_key = { user_key = 'uk' }
local transaction = Transaction.new(service_id, user_key, usage)
assert.is_nil(cache:get(transaction))
end)
it('returns status without rejection_reason when cached without one', function()
local user_key = { user_key = 'uk' }
local transaction = Transaction.new(service_id, user_key, usage)
cache:set(transaction, auth_status)
local cached_status, cached_rejection_reason = cache:get(transaction)
assert.equals(auth_status, cached_status)
assert.is_nil(cached_rejection_reason)
end)
it('parses rejection reason containing colons', function()
local app_id_and_key = { app_id = 'an_id', app_key = 'a_key' }
local transaction = Transaction.new(service_id, app_id_and_key, usage)
local rejection_reason = 'reason:with:colons'
cache:set(transaction, 409, rejection_reason)
local cached_status, cached_rejection_reason = cache:get(transaction)
assert.equals(409, cached_status)
assert.equals(rejection_reason, cached_rejection_reason)
end)
it('returns correct status for different HTTP status codes', function()
local user_key = { user_key = 'uk' }
for _, status_code in ipairs({ 200, 403, 404, 409, 500 }) do
local tx_usage = Usage.new()
tx_usage:add('m_' .. status_code, 1)
local transaction = Transaction.new(service_id, user_key, tx_usage)
cache:set(transaction, status_code)
local cached_status, cached_rejection_reason = cache:get(transaction)
assert.equals(status_code, cached_status)
end
end)
end)