1
1
# frozen_string_literal: true
2
2
3
- describe TokenAuthenticationConcern do
3
+ describe ReportingAPI :: TokenAuthenticationConcern do
4
4
let ( :user ) { @user = build ( :user ) }
5
5
let ( :mock_request ) { instance_double ( request . class , headers : { } ) }
6
6
let ( :an_object_which_includes_the_concern ) do
7
7
Class
8
8
. new do # rubocop:disable Style/BlockDelimiters
9
- include TokenAuthenticationConcern
9
+ include ReportingAPI :: TokenAuthenticationConcern
10
10
attr_accessor :request , :session
11
11
12
12
def authenticate_user!
@@ -34,11 +34,15 @@ def current_user
34
34
describe "#jwt_if_given" do
35
35
context "when there is a jwt param" do
36
36
before do
37
- allow ( an_object_which_includes_the_concern ) . to receive ( :params ) . and_return ( { jwt : "myjwt" } )
37
+ allow ( an_object_which_includes_the_concern ) . to receive (
38
+ :params
39
+ ) . and_return ( { jwt : "myjwt" } )
38
40
end
39
41
40
42
it "returns the jwt param" do
41
- expect ( an_object_which_includes_the_concern . send ( :jwt_if_given ) ) . to eq ( "myjwt" )
43
+ expect ( an_object_which_includes_the_concern . send ( :jwt_if_given ) ) . to eq (
44
+ "myjwt"
45
+ )
42
46
end
43
47
end
44
48
@@ -62,7 +66,9 @@ def current_user
62
66
63
67
context "and there is no Authorization header" do
64
68
it "returns nil" do
65
- expect ( an_object_which_includes_the_concern . send ( :jwt_if_given ) ) . to be_nil
69
+ expect (
70
+ an_object_which_includes_the_concern . send ( :jwt_if_given )
71
+ ) . to be_nil
66
72
end
67
73
end
68
74
end
@@ -76,24 +82,32 @@ def current_user
76
82
77
83
context "and the client_id param is provided" do
78
84
before do
79
- allow ( an_object_which_includes_the_concern ) . to receive ( :params ) . and_return (
80
- { client_id : client_id } . with_indifferent_access
81
- )
85
+ allow ( an_object_which_includes_the_concern ) . to receive (
86
+ :params
87
+ ) . and_return ( { client_id : client_id } . with_indifferent_access )
82
88
end
83
89
84
90
context "and the client_id param contains the reporting app's client_id" do
85
91
let ( :client_id ) { Settings . reporting_api . client_app . client_id }
86
92
87
93
it "does not cause a token error" do
88
- expect ( an_object_which_includes_the_concern ) . not_to receive ( :client_id_error! )
89
- an_object_which_includes_the_concern . send ( :authenticate_app_by_client_id! )
94
+ expect ( an_object_which_includes_the_concern ) . not_to receive (
95
+ :client_id_error!
96
+ )
97
+ an_object_which_includes_the_concern . send (
98
+ :authenticate_app_by_client_id!
99
+ )
90
100
end
91
101
end
92
102
93
103
context "and the client_id param does not contain the reporting app client_id" do
94
104
it "causes a token error" do
95
- expect ( an_object_which_includes_the_concern ) . to receive ( :client_id_error! )
96
- an_object_which_includes_the_concern . send ( :authenticate_app_by_client_id! )
105
+ expect ( an_object_which_includes_the_concern ) . to receive (
106
+ :client_id_error!
107
+ )
108
+ an_object_which_includes_the_concern . send (
109
+ :authenticate_app_by_client_id!
110
+ )
97
111
end
98
112
end
99
113
end
@@ -169,14 +183,18 @@ def current_user
169
183
end
170
184
171
185
before do
172
- allow ( an_object_which_includes_the_concern ) . to receive ( :decode_jwt! ) . with ( jwt ) . and_return (
173
- user_info
186
+ allow ( an_object_which_includes_the_concern ) . to receive (
187
+ :decode_jwt!
188
+ ) . with ( jwt ) . and_return ( user_info )
189
+ allow ( an_object_which_includes_the_concern ) . to receive (
190
+ :authenticate_user!
174
191
)
175
- allow ( an_object_which_includes_the_concern ) . to receive ( :authenticate_user! )
176
192
end
177
193
178
194
it "decodes the JWT" do
179
- expect ( an_object_which_includes_the_concern ) . to receive ( :decode_jwt! ) . with ( jwt )
195
+ expect ( an_object_which_includes_the_concern ) . to receive (
196
+ :decode_jwt!
197
+ ) . with ( jwt )
180
198
an_object_which_includes_the_concern . send ( :authenticate_user_by_jwt! )
181
199
end
182
200
@@ -192,13 +210,15 @@ def current_user
192
210
193
211
it "copies the cis2_info key into session['cis2_info']" do
194
212
an_object_which_includes_the_concern . send ( :authenticate_user_by_jwt! )
195
- expect ( an_object_which_includes_the_concern . session [ "cis2_info" ] ) . to eq (
196
- user_info . first [ "data" ] [ "cis2_info" ]
197
- )
213
+ expect (
214
+ an_object_which_includes_the_concern . session [ "cis2_info" ]
215
+ ) . to eq ( user_info . first [ "data" ] [ "cis2_info" ] )
198
216
end
199
217
200
218
it "calls authenticate_user!" do
201
- expect ( an_object_which_includes_the_concern ) . to receive ( :authenticate_user! )
219
+ expect ( an_object_which_includes_the_concern ) . to receive (
220
+ :authenticate_user!
221
+ )
202
222
an_object_which_includes_the_concern . send ( :authenticate_user_by_jwt! )
203
223
end
204
224
end
@@ -223,15 +243,19 @@ def current_user
223
243
end
224
244
225
245
it "calls client_id_error!" do
226
- expect ( an_object_which_includes_the_concern ) . to receive ( :client_id_error! )
246
+ expect ( an_object_which_includes_the_concern ) . to receive (
247
+ :client_id_error!
248
+ )
227
249
an_object_which_includes_the_concern . send ( :authenticate_user_by_jwt! )
228
250
end
229
251
end
230
252
end
231
253
232
254
context "when a valid jwt is not given" do
233
255
it "causes a client_id_error!" do
234
- expect ( an_object_which_includes_the_concern ) . to receive ( :client_id_error! )
256
+ expect ( an_object_which_includes_the_concern ) . to receive (
257
+ :client_id_error!
258
+ )
235
259
an_object_which_includes_the_concern . send ( :authenticate_user_by_jwt! )
236
260
end
237
261
end
@@ -265,17 +289,19 @@ def current_user
265
289
end
266
290
267
291
it "returns the decoded JWT" do
268
- expect ( an_object_which_includes_the_concern . send ( :decode_jwt! , jwt ) ) . to eq ( decoded_jwt )
292
+ expect (
293
+ an_object_which_includes_the_concern . send ( :decode_jwt! , jwt )
294
+ ) . to eq ( decoded_jwt )
269
295
end
270
296
end
271
297
272
298
context "when decoding does not work" do
273
299
it "raises an exception" do
274
- expect { an_object_which_includes_the_concern . send ( :decode_jwt! , jwt ) } . to raise_error (
275
- JWT :: DecodeError
276
- )
300
+ expect {
301
+ an_object_which_includes_the_concern . send ( :decode_jwt! , jwt )
302
+ } . to raise_error ( JWT :: DecodeError )
277
303
end
278
304
end
279
305
end
280
306
end
281
- end
307
+ end
0 commit comments