-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathpolicy_chain_spec.lua
261 lines (206 loc) · 7.92 KB
/
policy_chain_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
local policy = require 'apicast.policy'
local _M = require 'apicast.policy_chain'
describe('policy_chain', function()
it('defines a method for each of the nginx phases supported', function()
for _, phase in policy.phases() do
assert.equals('function', type(_M[phase]))
end
end)
it('when calling one of the nginx phases, calls that phase on each of its policies', function()
-- Define a policy an stub its phase methods
local custom_policy_1 = policy.new('policy_1')
custom_policy_1.init = function () end
custom_policy_1.rewrite = function() end
stub(custom_policy_1, 'init')
stub(custom_policy_1, 'rewrite')
-- Define another policy and stub its phase methods
local custom_policy_2 = policy.new('policy_2')
custom_policy_2.init = function () end
custom_policy_2.access = function() end
stub(custom_policy_2, 'init')
stub(custom_policy_2, 'access')
-- Build the policy chain
local chain = _M.build({ custom_policy_1, custom_policy_2 })
chain:init()
assert.stub(custom_policy_1.init).was_called()
assert.stub(custom_policy_2.init).was_called()
chain:rewrite()
assert.stub(custom_policy_1.rewrite).was_called()
chain:access()
assert.stub(custom_policy_2.access).was_called()
end)
it('uses APIcast as default when no policies are specified', function()
assert.equal(1, #_M)
assert.equal('APIcast', _M[1]._NAME)
end)
it('calls the policies in the order specified when building the chain', function()
-- Each policy inserts its name in a table so we know the order in which
-- they were run.
local execution_order = {}
local policies = { policy.new('1'), policy.new('2'), policy.new('3') }
for _, custom_policy in ipairs(policies) do
custom_policy['init'] = function()
table.insert(execution_order, custom_policy._NAME)
end
end
local sorted_policies = { policies[2], policies[3], policies[1] }
local chain = _M.build(sorted_policies)
chain:init()
assert.same({'2', '3', '1'}, execution_order)
end)
it('does not allow to modify phase methods after the chain has been built', function()
for _, phase in policy.phases() do
assert.has_error(function()
_M[phase] = function() end
end, 'readonly table')
end
end)
it('does not allow to add new methods to the chain after it has been built', function()
assert.has_error(function()
_M['new_phase'] = function() end
end, 'readonly table')
end)
describe('.insert', function()
it('adds policy to the end of the chain', function()
local chain = _M.new({ 'one', 'two' })
chain:insert(policy)
assert.equal(policy, chain[3])
assert.equal(3, #chain)
end)
it('adds a policy to specific position', function()
local chain = _M.new({ 'one', 'two'})
chain:insert(policy, 2)
assert.equal(policy, chain[2])
assert.equal('one', chain[1])
assert.equal('two', chain[3])
assert.equal(3, #chain)
end)
it('errors when inserting to frozen chain', function()
local chain = _M.new({}):freeze()
local ok, err = chain:insert(policy, 1)
assert.is_nil(ok)
assert.equal(err, 'frozen')
assert.equal(0, #chain)
end)
end)
describe('.export', function()
it('returns the data exposed by each of its policies', function()
local policy_1 = policy.new('1')
policy_1.export = function() return { shared_data_1 = '1' } end
local policy_2 = policy.new('2')
policy_2.export = function() return { shared_data_2 = '2' } end
local chain = _M.build({ policy_1, policy_2 })
local shared_data = chain:export()
assert.equal('1', shared_data['shared_data_1'])
assert.equal('2', shared_data['shared_data_2'])
end)
it('returns a read-only list', function()
local policy_1 = policy.new('1')
policy_1.export = function() return { shared_data = '1' } end
local chain = _M.build({ policy_1 })
local shared_data = chain:export()
assert.has_error(function()
shared_data.new_shared_data = 'some_data'
end, 'readonly list')
assert.is_nil(shared_data.new_shared_data)
end)
describe('when several policies expose the same data', function()
it('returns the data exposed by the policy that comes first in the chain', function()
local policy_1 = policy.new('custom_reporter')
policy_1.export = function() return { shared_data_1 = '1' } end
local policy_2 = policy.new('custom_authorizer')
policy_2.export = function() return { shared_data_1 = '2' } end
local chain = _M.build({ policy_1, policy_2 })
local shared_data = chain:export()
assert.equal('1', shared_data['shared_data_1'])
end)
end)
end)
describe('.default', function()
it('returns a default policy chain', function()
local default = _M.default()
assert(#default > 1, 'has <= 1 policy')
end)
it('returns not frozen chain', function()
assert.falsy(_M.default().frozen)
end)
end)
describe('.build', function()
it('returns original load error', function()
assert.error_matches(function ()
_M.build({'unknown'})
end, [[module "unknown" could not be loaded: module 'init' not found:]])
end)
end)
describe('.load_policy', function()
it('loads defined policy', function()
assert.same(require('apicast.policy.echo').new({ status = 200 }),
_M.load_policy('echo', 'builtin', { status = 200 }))
end)
it('returns error on missing policy', function()
local _, err = _M.load_policy('unknown')
assert.match([[module 'init' not found]], err)
assert.match([[no file]], err)
assert.match([[apicast/policy/unknown]], err)
end)
describe('when there is an error instantiating the policy', function()
before_each(function()
local PolicyLoader = require('apicast.policy_loader')
-- Make any policy crash when initialized
stub(PolicyLoader, 'pcall').returns(
{ new = function() error('Policy crashed in .new()') end }
)
end)
it('returns nil an an error instead of crashing', function()
local res, err = _M.load_policy('echo', 'builtin')
assert.is_nil(res)
assert(err)
end)
end)
describe('when the policy returns nil, err in .new()', function()
local policy_error = 'Some error'
before_each(function()
local PolicyLoader = require('apicast.policy_loader')
stub(PolicyLoader, 'pcall').returns(
{ new = function() return nil, policy_error end }
)
end)
it('returns nil an the policy error instead of crashing', function()
local res, err = _M.load_policy('echo', 'builtin')
assert.is_nil(res)
assert.equals(policy_error, err)
end)
end)
end)
describe("policy_error_callback", function()
local context = {}
before_each(function()
context = {
policy_error_callback = function(name, error_message)
return name
end
}
spy.on(context, 'policy_error_callback')
end)
it("trigger when fails on init", function()
local policy_chain = _M.new()
local res, err = policy_chain:add_policy("invalid", "builtin")
assert.falsy(res)
assert(err)
assert.truthy(policy_chain.init_failed)
assert.same(policy_chain.init_failed_policy.name, "invalid")
assert.truthy(policy_chain.init_failed_policy.err)
policy_chain:access(context)
assert.spy(context.policy_error_callback).was.called()
end)
it("trigger when fails on any phase", function()
local policy_chain = _M.new()
local res, err = policy_chain:add_policy("echo", "builtin")
assert.truthy(res)
assert.falsy(err)
policy_chain[1].access = function() error("invalid one") end
policy_chain:access(context)
assert.spy(context.policy_error_callback).was.called()
end)
end)
end)