-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware_spec.rb
313 lines (276 loc) · 11.5 KB
/
middleware_spec.rb
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
# frozen_string_literal: true
require 'spec_helper'
require 'faraday'
require 'mauth/rack'
require 'mauth/fake/rack'
require 'mauth/faraday'
shared_examples MAuth::Middleware do
it 'uses a given mauth_client if given' do
mauth_client = double
expect(mauth_client).to eq(described_class.new(double('app'), mauth_client: mauth_client).mauth_client)
expect(mauth_client).to eq(described_class.new(double('app'), 'mauth_client' => mauth_client).mauth_client)
end
it 'builds a mauth client if not given a mauth_client' do
mauth_config = { mauth_baseurl: 'http://mauth', mauth_api_version: 'v1' }
middleware_instance = described_class.new(double('app'), mauth_config)
expect(mauth_config[:mauth_baseurl]).to eq(middleware_instance.mauth_client.mauth_baseurl)
expect(mauth_config[:mauth_api_version]).to eq(middleware_instance.mauth_client.mauth_api_version)
end
end
describe MAuth::Rack do
let(:res) { [200, {}, ['hello world']] }
let(:rack_app) { proc { |_env| res } }
let(:v2_only_authenticate) { false }
let(:mw) { described_class.new(rack_app, v2_only_authenticate: v2_only_authenticate) }
describe MAuth::Rack::RequestAuthenticator do
include_examples MAuth::Middleware
it 'calls the app without authentication if should_authenticate check indicates not to' do
mw_auth_false = described_class.new(rack_app, should_authenticate_check: proc { false })
env = { 'HTTP_X_MWS_AUTHENTICATION' => 'MWS foo:bar' }
expect(mw_auth_false.mauth_client).not_to receive(:authentic?)
expect(rack_app).to receive(:call).with(env).and_return(res)
status, _headers, body = mw_auth_false.call(env)
expect(200).to eq(status)
expect(['hello world']).to eq(body)
end
it 'authenticates if should_authenticate_check is omitted or indicates to' do
[nil, proc { |_env| true }].each do |should_authenticate_check|
mw_w_flag = described_class.new(rack_app, should_authenticate_check: should_authenticate_check)
env = { 'HTTP_X_MWS_AUTHENTICATION' => 'MWS foo:bar' }
expect(mw_w_flag.mauth_client).to receive(:authentic?).and_return(true)
expect(rack_app).to receive(:call).with(env.merge(
MAuth::Client::RACK_ENV_APP_UUID_KEY => 'foo',
'mauth.authentic' => true,
'mauth.protocol_version' => 1
)).and_return(res)
status, _headers, body = mw_w_flag.call(env)
expect(status).to eq(200)
expect(body).to eq(['hello world'])
end
end
it 'returns 401 and does not call the app if authentication fails' do
expect(mw.mauth_client).to receive(:authentic?).and_return(false)
expect(rack_app).not_to receive(:call)
status, _headers, body = mw.call({ 'REQUEST_METHOD' => 'GET' })
expect(401).to eq(status)
expect(body.join).to match(/Unauthorized/)
end
it 'returns 401 with no body if the request method is HEAD and authentication fails' do
expect(mw.mauth_client).to receive(:authentic?).and_return(false)
expect(rack_app).not_to receive(:call)
status, headers, body = mw.call({ 'REQUEST_METHOD' => 'HEAD' })
expect(headers['Content-Length'].to_i).to be > 0
expect(401).to eq(status)
expect([]).to eq(body)
end
it 'returns 500 and does not call the app if unable to authenticate' do
expect(mw.mauth_client).to receive(:authentic?).and_raise(MAuth::UnableToAuthenticateError.new(''))
expect(rack_app).not_to receive(:call)
status, _headers, body = mw.call({ 'REQUEST_METHOD' => 'GET' })
expect(500).to eq(status)
expect(body.join).to match(/Could not determine request authenticity/)
end
context 'the V2_ONLY_AUTHENTICATE flag is true and the request has only v1 headers' do
let(:v2_only_authenticate) { true }
it 'returns 401 with an informative message and does not call the app' do
env = { 'HTTP_X_MWS_AUTHENTICATION' => 'MWS foo:bar', 'REQUEST_METHOD' => 'GET' }
expect(mw.mauth_client).not_to receive(:authentic?)
expect(rack_app).not_to receive(:call)
status, headers, body = mw.call(env)
expect(401).to eq(status)
expect(headers['Content-Type']).to eq('application/json')
expect(JSON.parse(body.join)).to eq({
'type' => 'errors:mauth:missing_v2',
'title' => 'This service requires mAuth v2 mcc-authentication header. ' \
'Upgrade your mAuth library and configure it properly.'
})
end
end
end
describe MAuth::Rack::RequestAuthenticationFaker do
it 'does not call check authenticity for any request by default' do
env = { 'HTTP_X_MWS_AUTHENTICATION' => 'MWS foo:bar' }
expect(mw.mauth_client).not_to receive(:authentic?)
expect(rack_app).to receive(:call).with(env.merge({
MAuth::Client::RACK_ENV_APP_UUID_KEY => 'foo',
'mauth.authentic' => true,
'mauth.protocol_version' => 1
})).and_return(res)
status, _headers, body = mw.call(env)
expect(status).to eq(200)
expect(body).to eq(['hello world'])
end
it 'calls the app when the request is set to be authentic' do
described_class.authentic = true
env = { 'HTTP_X_MWS_AUTHENTICATION' => 'MWS foo:bar' }
expect(rack_app).to receive(:call).with(env.merge({
MAuth::Client::RACK_ENV_APP_UUID_KEY => 'foo',
'mauth.authentic' => true,
'mauth.protocol_version' => 1
})).and_return(res)
status, _headers, body = mw.call(env)
expect(status).to eq(200)
expect(body).to eq(['hello world'])
end
it 'does not call the app when the request is set to be inauthentic' do
described_class.authentic = false
env = { 'REQUEST_METHOD' => 'GET', 'HTTP_X_MWS_AUTHENTICATION' => 'MWS foo:bar' }
mw.call(env)
expect(rack_app).not_to receive(:call)
end
it 'returns appropriate responses when the request is set to be inauthentic' do
described_class.authentic = false
env = { 'REQUEST_METHOD' => 'GET', 'HTTP_X_MWS_AUTHENTICATION' => 'MWS foo:bar' }
status, _headers, _body = mw.call(env)
expect(status).to eq(401)
end
it 'after an inauthentic request, the next request is authentic by default' do
described_class.authentic = false
env = { 'REQUEST_METHOD' => 'GET', 'HTTP_X_MWS_AUTHENTICATION' => 'MWS foo:bar' }
status, _headers, _body = mw.call(env)
expect(status).to eq(401)
status, _headers, _body = mw.call(env)
expect(status).to eq(200)
end
end
describe MAuth::Rack::ResponseSigner do
include_examples MAuth::Middleware
context 'request with v2 headers' do
let(:env) do
{
'HTTP_MCC_AUTHENTICATION' => 'MWSV2 foo:bar;',
'REQUEST_METHOD' => 'GET',
'mauth.protocol_version' => 2
}
end
it 'signs the response with only v2' do
allow(rack_app).to receive(:call).with(env).and_return(res)
expect(mw.mauth_client).to receive(:signed_v2).with(
an_instance_of(MAuth::Rack::Response)
).and_return(MAuth::Rack::Response.new(*res))
mw.call(env)
end
end
context 'request with v1 headers' do
let(:env) do
{
'HTTP_X_MWS_AUTHENTICATION' => 'MWS foo:bar',
'REQUEST_METHOD' => 'GET',
'mauth.protocol_version' => 1
}
end
it 'signs the response with only v1' do
allow(rack_app).to receive(:call).with(env).and_return(res)
expect(mw.mauth_client).to receive(:signed_v1).with(
an_instance_of(MAuth::Rack::Response)
).and_return(MAuth::Rack::Response.new(*res))
mw.call(env)
end
end
context 'request with invalid headers' do
let(:env) do
{
'HTTP_MCC_AUTHENTICATION' => 'MWSV500 foo:bar;',
'REQUEST_METHOD' => 'GET'
}
end
it 'signs the response with the default headers' do
allow(rack_app).to receive(:call).with(env).and_return(res)
expect(mw.mauth_client).to receive(:signed).with(
an_instance_of(MAuth::Rack::Response)
).and_return(MAuth::Rack::Response.new(*res))
mw.call(env)
end
end
end
describe MAuth::Rack::Response do
let(:status) { 200 }
let(:headers) { {} }
let(:body) { %w[hello world] }
let(:response) { described_class.new(status, headers, body) }
describe '#status_headers_body' do
it 'returns status, headers and body' do
expect(response.status_headers_body).to eq([status, headers, body])
end
end
describe '#attributes_for_signing' do
it 'returns attributes_for_signing' do
expect(response.attributes_for_signing).to eq(status_code: 200, body: 'helloworld')
end
end
describe '#merge_headers' do
it 'merges headers' do
expect(response.merge_headers('foo' => 'bar').status_headers_body).to eq([status, { 'foo' => 'bar' }, body])
end
end
end
end
describe MAuth::Faraday do
describe MAuth::Faraday::ResponseAuthenticator do
include_examples MAuth::Middleware
let(:faraday_app) do
proc do
res = Object.new
def res.on_complete
response_env = Faraday::Env.new
response_env[:status] = 200
response_env[:response_headers] = { 'x-mws-authentication' => 'MWS foo:bar' }
response_env[:body] = 'hello world'
yield(response_env)
end
res
end
end
let(:mw) { described_class.new(faraday_app) }
it 'returns the response with env indicating authenticity when authentic' do
allow(mw.mauth_client).to receive(:authenticate!)
res = mw.call({})
expect(200).to eq(res[:status])
expect('foo').to eq(res[MAuth::Client::RACK_ENV_APP_UUID_KEY])
expect(true).to eq(res['mauth.authentic'])
end
it 'raises InauthenticError on inauthentic response' do
allow(mw.mauth_client).to receive(:authenticate!).and_raise(MAuth::InauthenticError.new)
expect { mw.call({}) }.to raise_error(MAuth::InauthenticError)
end
it 'raises UnableToAuthenticateError when unable to authenticate' do
allow(mw.mauth_client).to receive(:authenticate!).and_raise(MAuth::UnableToAuthenticateError.new)
expect { mw.call({}) }.to raise_error(MAuth::UnableToAuthenticateError)
end
it 'is usable via the name mauth_response_authenticator' do
# if this doesn't error, that's fine; means it looked up the middleware and is using it
Faraday::Connection.new do |conn|
conn.response :mauth_response_authenticator
conn.adapter Faraday.default_adapter
end
end
end
describe MAuth::Faraday::RequestSigner do
include_examples MAuth::Middleware
it 'is usable via the name mauth_request_signer' do
# if this doesn't error, that's fine; means it looked up the middleware and is using it
Faraday::Connection.new do |conn|
conn.request :mauth_request_signer
conn.adapter Faraday.default_adapter
end
end
end
describe MAuth::Faraday::MAuthClientUserAgent do
let(:fake_app) do
Class.new do
def call(env); end
end
end
let(:agent_base) { 'Sallust' }
let(:app) { fake_app.new }
let(:middleware) { described_class.new(app, agent_base) }
it 'sets the User-Agent request header' do
request_headers = {}
request_env = {}
request_env[:request_headers] = request_headers
middleware.call(request_env)
expected = "#{agent_base} (MAuth-Client: #{MAuth::VERSION}; Ruby: #{RUBY_VERSION}; platform: #{RUBY_PLATFORM})"
expect(request_headers['User-Agent']).to eq(expected)
end
end
end