Skip to content

Commit 9fd9269

Browse files
author
Alistair Davidson
committed
re-scope the reporting API controller to be under /reporting-api/, and inherit a base_controller which handles the auth and ensures that reporting_app feature flag is enabled. Fixes MAV-1558 and MAV-1588
1 parent 73282eb commit 9fd9269

File tree

7 files changed

+121
-79
lines changed

7 files changed

+121
-79
lines changed

app/controllers/reporting/totals_controller.rb

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# frozen_string_literal: true
2+
3+
class ReportingAPI::BaseController < ActionController::API
4+
# we need to still include the AuthenticationConcern even though
5+
# we're not using the authenticate_user! callback, because we call it
6+
# explicitly after validating the users' JWT in order to use the
7+
# CIS2 organisation/workgroup validation code
8+
include AuthenticationConcern
9+
include TokenAuthenticationConcern
10+
11+
before_action :ensure_reporting_app_feature_enabled
12+
before_action :authenticate_user_by_jwt!
13+
14+
private
15+
16+
def ensure_reporting_app_feature_enabled
17+
render status: :forbidden and return unless Flipper.enabled?(:reporting_app)
18+
end
19+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# frozen_string_literal: true
2+
3+
module ReportingAPI
4+
class TotalsController < ::ReportingAPI::BaseController
5+
def index
6+
render json: { total: "some total" }
7+
end
8+
end
9+
end

config/routes.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@
320320

321321
# for commissioner reporting app
322322
post "/tokens/authorize", controller: :one_time_tokens, action: "authorize"
323-
namespace :reporting do
323+
namespace :reporting_api, path: '/reporting-api/' do
324324
get "totals", controller: :totals, action: :index
325325
end
326326

docs/diagrams/reporting_auth/user-logs-in-to-mavis-with-local-pwd.puml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ if (CIS2 login enabled?) is (N) then
2020
:choose role;
2121
|Mavis|
2222
:store user & role info in session as 'cis2_info';
23-
:store pwd_auth_session_token in DB;
23+
:store reporting_app_session_token in DB;
2424
:respond with cookie and redirect;
2525
|User|
2626
:access redirected URL;

spec/controllers/reporting/totals_controller_spec.rb

Lines changed: 0 additions & 64 deletions
This file was deleted.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# frozen_string_literal: true
2+
3+
require "spec_helper"
4+
5+
RSpec.describe ReportingAPI::TotalsController do
6+
let(:user) { create(:user) }
7+
let(:org) { user.organisations.first }
8+
9+
let(:valid_payload) do
10+
{
11+
data: {
12+
user: user.as_json,
13+
cis2_info: {
14+
selected_org: {
15+
name: org.name,
16+
code: org.ods_code
17+
},
18+
selected_role: {
19+
code: "S8000:G8000:R8001",
20+
workgroups: ["schoolagedimmunisations"]
21+
}
22+
}
23+
}
24+
}
25+
end
26+
27+
let(:invalid_payload) { { user: { id: -1 } } }
28+
29+
context "when the :reporting_app feature flag is not enabled" do
30+
before { Flipper.disable(:reporting_app) }
31+
32+
describe "#index" do
33+
context "when the request has a JWT param" do
34+
let(:params) { { jwt: jwt } }
35+
36+
context "which is valid" do
37+
let(:jwt) do
38+
JWT.encode(
39+
valid_payload,
40+
Settings.mavis_reporting_app.secret,
41+
"HS512"
42+
)
43+
end
44+
45+
it "responds with status :forbidden" do
46+
get :index, params: { jwt: jwt }
47+
expect(response.status).to eq(403)
48+
end
49+
end
50+
end
51+
end
52+
end
53+
context "when the :reporting_app feature flag is enabled" do
54+
before { Flipper.enable(:reporting_app) }
55+
describe "#index" do
56+
context "when the request has a JWT param" do
57+
let(:params) { { jwt: jwt } }
58+
59+
context "which is valid" do
60+
let(:jwt) do
61+
JWT.encode(
62+
valid_payload,
63+
Settings.mavis_reporting_app.secret,
64+
"HS512"
65+
)
66+
end
67+
68+
it "responds with status 200" do
69+
get :index, params: { jwt: jwt }
70+
expect(response.status).to eq(200)
71+
end
72+
end
73+
74+
context "which is not valid" do
75+
let(:jwt) do
76+
JWT.encode(
77+
invalid_payload,
78+
Settings.mavis_reporting_app.secret,
79+
"HS512"
80+
)
81+
end
82+
83+
it "responds with status :forbidden" do
84+
get :index, params: { jwt: jwt }
85+
expect(response.status).to eq(403)
86+
end
87+
end
88+
end
89+
end
90+
end
91+
end

0 commit comments

Comments
 (0)