Skip to content

Commit 98639b0

Browse files
author
Alistair Davidson
committed
move token_authentication_concern under reporting_api namespace
1 parent c8d2037 commit 98639b0

File tree

2 files changed

+59
-31
lines changed

2 files changed

+59
-31
lines changed

app/controllers/concerns/token_authentication_concern.rb renamed to app/controllers/concerns/reporting_api/token_authentication_concern.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: true
22

3-
module TokenAuthenticationConcern
3+
module ReportingAPI::TokenAuthenticationConcern
44
extend ActiveSupport::Concern
55

66
included do
@@ -36,9 +36,11 @@ def authenticate_user_by_jwt!
3636
data = jwt_info.first["data"]
3737
@current_user =
3838
User.find_by(
39-
data
40-
.fetch("user", {})
41-
.slice("id", "session_token", "reporting_api_session_token")
39+
data.fetch("user", {}).slice(
40+
"id",
41+
"session_token",
42+
"reporting_api_session_token"
43+
)
4244
)
4345
if @current_user
4446
session["user"] = data["user"]

spec/controllers/concerns/token_authentication_concern_spec.rb renamed to spec/controllers/concerns/reporting_api/token_authentication_concern_spec.rb

Lines changed: 53 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# frozen_string_literal: true
22

3-
describe TokenAuthenticationConcern do
3+
describe ReportingAPI::TokenAuthenticationConcern do
44
let(:user) { @user = build(:user) }
55
let(:mock_request) { instance_double(request.class, headers: {}) }
66
let(:an_object_which_includes_the_concern) do
77
Class
88
.new do # rubocop:disable Style/BlockDelimiters
9-
include TokenAuthenticationConcern
9+
include ReportingAPI::TokenAuthenticationConcern
1010
attr_accessor :request, :session
1111

1212
def authenticate_user!
@@ -34,11 +34,15 @@ def current_user
3434
describe "#jwt_if_given" do
3535
context "when there is a jwt param" do
3636
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" })
3840
end
3941

4042
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+
)
4246
end
4347
end
4448

@@ -62,7 +66,9 @@ def current_user
6266

6367
context "and there is no Authorization header" do
6468
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
6672
end
6773
end
6874
end
@@ -76,24 +82,32 @@ def current_user
7682

7783
context "and the client_id param is provided" do
7884
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)
8288
end
8389

8490
context "and the client_id param contains the reporting app's client_id" do
8591
let(:client_id) { Settings.reporting_api.client_app.client_id }
8692

8793
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+
)
90100
end
91101
end
92102

93103
context "and the client_id param does not contain the reporting app client_id" do
94104
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+
)
97111
end
98112
end
99113
end
@@ -169,14 +183,18 @@ def current_user
169183
end
170184

171185
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!
174191
)
175-
allow(an_object_which_includes_the_concern).to receive(:authenticate_user!)
176192
end
177193

178194
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)
180198
an_object_which_includes_the_concern.send(:authenticate_user_by_jwt!)
181199
end
182200

@@ -192,13 +210,15 @@ def current_user
192210

193211
it "copies the cis2_info key into session['cis2_info']" do
194212
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"])
198216
end
199217

200218
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+
)
202222
an_object_which_includes_the_concern.send(:authenticate_user_by_jwt!)
203223
end
204224
end
@@ -223,15 +243,19 @@ def current_user
223243
end
224244

225245
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+
)
227249
an_object_which_includes_the_concern.send(:authenticate_user_by_jwt!)
228250
end
229251
end
230252
end
231253

232254
context "when a valid jwt is not given" do
233255
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+
)
235259
an_object_which_includes_the_concern.send(:authenticate_user_by_jwt!)
236260
end
237261
end
@@ -265,17 +289,19 @@ def current_user
265289
end
266290

267291
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)
269295
end
270296
end
271297

272298
context "when decoding does not work" do
273299
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)
277303
end
278304
end
279305
end
280306
end
281-
end
307+
end

0 commit comments

Comments
 (0)