-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
protected_resources_controller_spec.rb
363 lines (296 loc) · 10.7 KB
/
protected_resources_controller_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
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
# frozen_string_literal: true
require "spec_helper"
module ControllerActions
def index
render plain: "index"
end
def show
render plain: "show"
end
def doorkeeper_unauthorized_render_options(*); end
def doorkeeper_forbidden_render_options(*); end
end
RSpec.describe "doorkeeper authorize filter" do
render_views
context "when accepts token code specified as" do
controller do
before_action :doorkeeper_authorize!
def index
render plain: "index"
end
end
let(:token_string) { "1A2BC3" }
let(:token) do
double(
Doorkeeper::AccessToken,
acceptable?: true, previous_refresh_token: "",
revoke_previous_refresh_token!: true,
)
end
it "access_token param" do
expect(Doorkeeper::AccessToken).to receive(:by_token).with(token_string).and_return(token)
get :index, params: { access_token: token_string }
end
it "bearer_token param" do
expect(Doorkeeper::AccessToken).to receive(:by_token).with(token_string).and_return(token)
get :index, params: { bearer_token: token_string }
end
it "Authorization header" do
expect(Doorkeeper::AccessToken).to receive(:by_token).with(token_string).and_return(token)
request.env["HTTP_AUTHORIZATION"] = "Bearer #{token_string}"
get :index
end
it "different kind of Authorization header" do
expect(Doorkeeper::AccessToken).not_to receive(:by_token)
request.env["HTTP_AUTHORIZATION"] = "MAC #{token_string}"
get :index
end
it "does not change Authorization header value" do
expect(Doorkeeper::AccessToken).to receive(:by_token).twice.and_return(token)
request.env["HTTP_AUTHORIZATION"] = "Bearer #{token_string}"
get :index
controller.send(:remove_instance_variable, :@doorkeeper_token)
get :index
end
end
context "when defined for all actions" do
controller do
before_action :doorkeeper_authorize!
include ControllerActions
end
context "with valid token", token: :valid do
it "allows into index action" do
get :index, params: { access_token: token_string }
expect(response).to be_successful
end
it "allows into show action" do
get :show, params: { id: "4", access_token: token_string }
expect(response).to be_successful
end
end
context "with invalid token", token: :invalid do
it "does not allow into index action" do
get :index, params: { access_token: token_string }
expect(response.status).to eq 401
expect(response.header["WWW-Authenticate"]).to match(/^Bearer/)
end
it "does not allow into show action" do
get :show, params: { id: "4", access_token: token_string }
expect(response.status).to eq 401
expect(response.header["WWW-Authenticate"]).to match(/^Bearer/)
end
end
end
context "when defined with scopes" do
controller do
before_action -> { doorkeeper_authorize! :write }
include ControllerActions
end
let(:token_string) { "1A2DUWE" }
it "allows if the token has particular scopes" do
token = double(
Doorkeeper::AccessToken,
accessible?: true, scopes: %w[write public],
previous_refresh_token: "",
revoke_previous_refresh_token!: true,
)
expect(token).to receive(:acceptable?).with([:write]).and_return(true)
expect(
Doorkeeper::AccessToken,
).to receive(:by_token).with(token_string).and_return(token)
get :index, params: { access_token: token_string }
expect(response).to be_successful
end
it "does not allow if the token does not include given scope" do
token = double(
Doorkeeper::AccessToken,
accessible?: true, scopes: ["public"], revoked?: false,
expired?: false, previous_refresh_token: "",
revoke_previous_refresh_token!: true,
)
expect(
Doorkeeper::AccessToken,
).to receive(:by_token).with(token_string).and_return(token)
expect(token).to receive(:acceptable?).with([:write]).and_return(false)
get :index, params: { access_token: token_string }
expect(response.status).to eq 403
expect(response.header).not_to include("WWW-Authenticate")
end
end
context "when custom unauthorized render options are configured" do
controller do
before_action :doorkeeper_authorize!
include ControllerActions
end
context "with a JSON custom render", token: :invalid do
before do
module ControllerActions
remove_method :doorkeeper_unauthorized_render_options
def doorkeeper_unauthorized_render_options(error: nil)
{ json: ActiveSupport::JSON.encode(error_message: error.description) }
end
end
end
after do
module ControllerActions
remove_method :doorkeeper_unauthorized_render_options
def doorkeeper_unauthorized_render_options(error: nil); end
end
end
it "renders a custom JSON response", token: :invalid do
get :index, params: { access_token: token_string }
expect(response.status).to eq 401
expect(response.content_type).to include("application/json")
expect(response.header["WWW-Authenticate"]).to match(/^Bearer/)
expect(json_response).not_to be_nil
expect(json_response["error_message"]).to match("token is invalid")
end
end
context "with a text custom render", token: :invalid do
before do
module ControllerActions
remove_method :doorkeeper_unauthorized_render_options
def doorkeeper_unauthorized_render_options(**)
{ plain: "Unauthorized" }
end
end
end
after do
module ControllerActions
remove_method :doorkeeper_unauthorized_render_options
def doorkeeper_unauthorized_render_options(error: nil); end
end
end
it "renders a custom text response", token: :invalid do
get :index, params: { access_token: token_string }
expect(response.status).to eq 401
expect(response.content_type).to include("text/plain")
expect(response.header["WWW-Authenticate"]).to match(/^Bearer/)
expect(response.body).to eq("Unauthorized")
end
end
end
context "when custom forbidden render options are configured" do
before do
expect(Doorkeeper::AccessToken).to receive(:by_token).with(token_string).and_return(token)
expect(token).to receive(:acceptable?).with([:write]).and_return(false)
end
after do
module ControllerActions
remove_method :doorkeeper_forbidden_render_options
def doorkeeper_forbidden_render_options(*); end
end
end
controller do
before_action -> { doorkeeper_authorize! :write }
include ControllerActions
end
let(:token) do
double(
Doorkeeper::AccessToken,
accessible?: true, scopes: ["public"], revoked?: false,
expired?: false, previous_refresh_token: "",
revoke_previous_refresh_token!: true,
)
end
let(:token_string) { "1A2DUWE" }
context "with a JSON custom render" do
before do
module ControllerActions
remove_method :doorkeeper_forbidden_render_options
def doorkeeper_forbidden_render_options(*)
{ json: { error_message: "Forbidden" } }
end
end
end
it "renders a custom JSON response" do
get :index, params: { access_token: token_string }
expect(response.header).not_to include("WWW-Authenticate")
expect(response.content_type).to include("application/json")
expect(response.status).to eq 403
expect(json_response).not_to be_nil
expect(json_response["error_message"]).to match("Forbidden")
end
end
context "with a status and JSON custom render" do
before do
module ControllerActions
remove_method :doorkeeper_forbidden_render_options
def doorkeeper_forbidden_render_options(*)
{ json: { error_message: "Not Found" },
respond_not_found_when_forbidden: true, }
end
end
end
it "overrides the default status code" do
get :index, params: { access_token: token_string }
expect(response.status).to eq 404
end
end
context "with a text custom render" do
before do
module ControllerActions
remove_method :doorkeeper_forbidden_render_options
def doorkeeper_forbidden_render_options(*)
{ plain: "Forbidden" }
end
end
end
it "renders a custom status code and text response" do
get :index, params: { access_token: token_string }
expect(response.header).not_to include("WWW-Authenticate")
expect(response.status).to eq 403
expect(response.body).to eq("Forbidden")
end
end
context "with a status and text custom render" do
before do
module ControllerActions
remove_method :doorkeeper_forbidden_render_options
def doorkeeper_forbidden_render_options(*)
{ respond_not_found_when_forbidden: true, plain: "Not Found" }
end
end
end
it "overrides the default status code" do
get :index, params: { access_token: token_string }
expect(response.status).to eq 404
end
end
end
context "when handle_auth_errors option is set to :raise" do
subject(:request) { get :index, params: { access_token: token_string } }
before do
config_is_set(:handle_auth_errors, :raise)
end
controller do
before_action :doorkeeper_authorize!
include ControllerActions
end
context "when token is unknown" do
it "raises Doorkeeper::Errors::TokenUnknown exception", token: :invalid do
expect { request }.to raise_error(Doorkeeper::Errors::TokenUnknown)
end
end
context "when token is expired" do
it "raises Doorkeeper::Errors::TokenExpired exception", token: :expired do
expect { request }.to raise_error(Doorkeeper::Errors::TokenExpired)
end
end
context "when token is revoked" do
it "raises Doorkeeper::Errors::TokenRevoked exception", token: :revoked do
expect { request }.to raise_error(Doorkeeper::Errors::TokenRevoked)
end
end
context "when token is forbidden" do
it "raises Doorkeeper::Errors::TokenForbidden exception", token: :forbidden do
expect { request }.to raise_error(Doorkeeper::Errors::TokenForbidden)
end
end
context "when token is valid" do
it "allows into index action", token: :valid do
expect(response).to be_successful
end
end
end
end