Skip to content

Commit 3c5ae9b

Browse files
author
Al Davidson
authored
Merge pull request #4483 from nhsuk/add-shared-example-for-reporting-api-controller-tests
Refactor tests for common Reporting API controller behaviour to a shared example
2 parents 7e19f02 + 256b4d6 commit 3c5ae9b

File tree

3 files changed

+94
-82
lines changed

3 files changed

+94
-82
lines changed

spec/controllers/api/reporting/totals_controller_spec.rb

Lines changed: 1 addition & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -3,86 +3,5 @@
33
require "spec_helper"
44

55
RSpec.describe API::Reporting::TotalsController do
6-
let(:team) { create(:team, :with_one_nurse) }
7-
let(:user) { team.users.first }
8-
9-
let(:valid_payload) do
10-
{
11-
data: {
12-
user: user.as_json,
13-
cis2_info: {
14-
organisation_code: team.organisation.ods_code,
15-
workgroups: [team.workgroup],
16-
role_code: CIS2Info::NURSE_ROLE
17-
}
18-
}
19-
}
20-
end
21-
22-
let(:invalid_payload) { { user: { id: -1 } } }
23-
24-
context "when the :reporting_api feature flag is not enabled" do
25-
before { Flipper.disable(:reporting_api) }
26-
27-
describe "#index" do
28-
context "when the request has a JWT param" do
29-
let(:params) { { jwt: jwt } }
30-
31-
context "which is valid" do
32-
let(:jwt) do
33-
JWT.encode(
34-
valid_payload,
35-
Settings.reporting_api.client_app.secret,
36-
ReportingAPI::OneTimeToken::JWT_SIGNING_ALGORITHM
37-
)
38-
end
39-
40-
it "responds with status :forbidden" do
41-
get :index, params: { jwt: jwt }
42-
expect(response.status).to eq(403)
43-
end
44-
end
45-
end
46-
end
47-
end
48-
49-
context "when the :reporting_api feature flag is enabled" do
50-
before { Flipper.enable(:reporting_api) }
51-
52-
describe "#index" do
53-
context "when the request has a JWT param" do
54-
let(:params) { { jwt: jwt } }
55-
56-
context "which is valid" do
57-
let(:jwt) do
58-
JWT.encode(
59-
valid_payload,
60-
Settings.reporting_api.client_app.secret,
61-
ReportingAPI::OneTimeToken::JWT_SIGNING_ALGORITHM
62-
)
63-
end
64-
65-
it "responds with status 200" do
66-
get :index, params: { jwt: jwt }
67-
expect(response.status).to eq(200)
68-
end
69-
end
70-
71-
context "which is not valid" do
72-
let(:jwt) do
73-
JWT.encode(
74-
invalid_payload,
75-
Settings.reporting_api.client_app.secret,
76-
ReportingAPI::OneTimeToken::JWT_SIGNING_ALGORITHM
77-
)
78-
end
79-
80-
it "responds with status :forbidden" do
81-
get :index, params: { jwt: jwt }
82-
expect(response.status).to eq(403)
83-
end
84-
end
85-
end
86-
end
87-
end
6+
it_behaves_like "a ReportingAPI controller"
887
end
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# frozen_string_literal: true
2+
3+
module ReportingAPIHelper
4+
def valid_jwt_payload
5+
team = create(:team, :with_one_nurse)
6+
user = team.users.first
7+
{
8+
data: {
9+
user: user.as_json,
10+
cis2_info: {
11+
organisation_code: team.organisation.ods_code,
12+
workgroups: [team.workgroup],
13+
role_code: CIS2Info::NURSE_ROLE
14+
}
15+
}
16+
}
17+
end
18+
19+
def valid_jwt
20+
JWT.encode(
21+
valid_jwt_payload,
22+
Settings.reporting_api.client_app.secret,
23+
"HS512"
24+
)
25+
end
26+
27+
def invalid_jwt_payload
28+
{ user: { id: -1 } }
29+
end
30+
31+
def jwt_with_invalid_payload
32+
JWT.encode(
33+
invalid_jwt_payload,
34+
Settings.reporting_api.client_app.secret,
35+
"HS512"
36+
)
37+
end
38+
end
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# frozen_string_literal: true
2+
3+
shared_examples "a ReportingAPI controller" do
4+
let(:team) { create(:team, :with_one_nurse) }
5+
let(:user) { team.users.first }
6+
7+
include ReportingAPIHelper
8+
9+
context "when the reporting_api feature flag is disabled" do
10+
before { Flipper.disable(:reporting_api) }
11+
12+
describe "#index" do
13+
context "when the request has a JWT param" do
14+
let(:params) { { jwt: jwt } }
15+
16+
context "which is valid" do
17+
let(:jwt) { valid_jwt }
18+
19+
it "responds with status :forbidden" do
20+
get :index, params: { jwt: jwt }
21+
expect(response.status).to eq(403)
22+
end
23+
end
24+
end
25+
end
26+
end
27+
28+
context "when the :reporting_api feature flag is enabled" do
29+
before { Flipper.enable(:reporting_api) }
30+
31+
describe "#index" do
32+
context "when the request has a JWT param" do
33+
let(:params) { { jwt: jwt } }
34+
35+
context "which is valid" do
36+
let(:jwt) { valid_jwt }
37+
38+
it "responds with status 200" do
39+
get :index, params: { jwt: jwt }
40+
expect(response.status).to eq(200)
41+
end
42+
end
43+
44+
context "which is not valid" do
45+
let(:jwt) { jwt_with_invalid_payload }
46+
47+
it "responds with status :forbidden" do
48+
get :index, params: { jwt: jwt }
49+
expect(response.status).to eq(403)
50+
end
51+
end
52+
end
53+
end
54+
end
55+
end

0 commit comments

Comments
 (0)