Skip to content

Commit 2921893

Browse files
committed
standardized policy tests using pundit matchers
1 parent 0b272be commit 2921893

File tree

7 files changed

+78
-47
lines changed

7 files changed

+78
-47
lines changed

spec/policies/application_policy_spec.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@
77
let(:patient) { create(:patient, user: user) }
88
let(:another_owner_patient) { create(:patient) }
99

10-
permissions :index?, :show?, :create?, :new?, :update?, :edit?, :destroy? do
11-
it { expect(described_class).not_to permit(nil, nil) }
10+
describe "permissions" do
11+
subject { described_class.new(nil, nil) }
12+
13+
it "forbids all default actions when no user is present" do
14+
is_expected.to forbid_all_actions
15+
end
1216
end
1317

1418
permissions :user_owner? do

spec/policies/event_procedure_policy_spec.rb

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
require "rails_helper"
44

55
RSpec.describe EventProcedurePolicy do
6-
subject(:policy) { described_class }
7-
86
let(:user) { create(:user) }
97
let(:event_procedure) { create(:event_procedure, user: user) }
108
let(:other_event_procedure) { create(:event_procedure) }
@@ -42,18 +40,29 @@
4240
end
4341
end
4442

45-
permissions :index?, :create? do
46-
context "when user is nil" do
47-
it { is_expected.not_to permit(nil, event_procedure) }
43+
describe "permissions" do
44+
context "when has no user" do
45+
subject { described_class.new(nil, event_procedure) }
46+
47+
it { is_expected.to forbid_all_actions }
4848
end
4949

5050
context "when user is present" do
51-
it { is_expected.to permit(user, event_procedure) }
51+
subject { described_class.new(user, event_procedure) }
52+
53+
it { is_expected.to permit_actions(%i[index create]) }
5254
end
53-
end
5455

55-
permissions :update?, :destroy? do
56-
it { is_expected.to permit(user, event_procedure) }
57-
it { is_expected.not_to permit(user, other_event_procedure) }
56+
context "when user is owner" do
57+
subject { described_class.new(user, event_procedure) }
58+
59+
it { is_expected.to permit_actions(%i[index create update destroy]) }
60+
end
61+
62+
context "when user is not owner" do
63+
subject { described_class.new(user, other_event_procedure) }
64+
65+
it { is_expected.to forbid_actions(%i[update destroy]) }
66+
end
5867
end
5968
end

spec/policies/health_insurance_policy_spec.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,16 @@
33
require "rails_helper"
44

55
RSpec.describe HealthInsurancePolicy do
6-
subject { described_class }
7-
86
let(:user) { create(:user) }
97
let(:health_insurance) { create(:health_insurance, user: user) }
108

119
permissions :index?, :create? do
1210
context "when user is present" do
13-
it { is_expected.to permit(user, health_insurance) }
11+
it { expect(described_class).to permit(user, health_insurance) }
1412
end
1513

1614
context "when user is nil" do
17-
it { is_expected.not_to permit(nil, health_insurance) }
15+
it { expect(described_class).not_to permit(nil, health_insurance) }
1816
end
1917
end
2018
end

spec/policies/hospital_policy_spec.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,16 @@
33
require "rails_helper"
44

55
RSpec.describe HospitalPolicy do
6-
subject(:policy) { described_class }
7-
86
let(:user) { create(:user) }
97
let(:hospital) { create(:hospital) }
108

119
permissions :index?, :create?, :update?, :destroy? do
1210
context "when user is present" do
13-
it { expect(policy).to permit(user, hospital) }
11+
it { expect(described_class).to permit(user, hospital) }
1412
end
1513

1614
context "when user is nil" do
17-
it { expect(policy).not_to permit(nil, hospital) }
15+
it { expect(described_class).not_to permit(nil, hospital) }
1816
end
1917
end
2018
end

spec/policies/medical_shift_policy_spec.rb

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,29 @@
3333
end
3434
end
3535

36-
permissions :index?, :create? do
37-
context "when user is present" do
38-
it { expect(described_class).to permit(user, medical_shift) }
36+
describe "permissions" do
37+
context "when has user" do
38+
subject { described_class.new(user, medical_shift) }
39+
40+
it { is_expected.to permit_actions(%i[index create hospital_name_suggestion_index amount_suggestions_index]) }
3941
end
4042

41-
context "when user is nil" do
42-
it { expect(described_class).not_to permit(nil, medical_shift) }
43+
context "when has no user" do
44+
subject { described_class.new(nil, medical_shift) }
45+
46+
it { is_expected.to forbid_all_actions }
4347
end
44-
end
4548

46-
permissions :update?, :destroy? do
4749
context "when user is owner" do
48-
it { expect(described_class).to permit(user, medical_shift) }
49-
end
50+
subject { described_class.new(user, medical_shift) }
5051

51-
context "when user is nil" do
52-
it { expect(described_class).not_to permit(nil, medical_shift) }
52+
it { is_expected.to permit_actions(%i[index create update destroy]) }
5353
end
5454

5555
context "when user is not owner" do
56-
it { expect(described_class).not_to permit(user, medical_shift_without_user) }
56+
subject { described_class.new(user, medical_shift_without_user) }
57+
58+
it { is_expected.to forbid_actions(%i[update destroy]) }
5759
end
5860
end
5961
end

spec/policies/patient_policy_spec.rb

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,29 @@
3434
end
3535
end
3636

37-
permissions :index?, :create? do
37+
describe "permissions" do
3838
context "when user is nil" do
39-
it { expect(described_class).not_to permit(nil, patient) }
39+
subject { described_class.new(nil, patient) }
40+
41+
it { is_expected.to forbid_all_actions }
4042
end
41-
end
4243

43-
permissions :update?, :destroy? do
44-
context "when user is not the owner" do
45-
it { expect(described_class).not_to permit(user, patient_another_user) }
44+
context "when user is present" do
45+
subject { described_class.new(user, patient) }
46+
47+
it { is_expected.to permit_actions(%i[index create]) }
4648
end
4749

48-
context "when user is the owner" do
49-
it { expect(described_class).to permit(user, patient) }
50+
context "when user is owner" do
51+
subject { described_class.new(user, patient) }
52+
53+
it { is_expected.to permit_actions(%i[index create update destroy]) }
54+
end
55+
56+
context "when user is not owner" do
57+
subject { described_class.new(user, patient_another_user) }
58+
59+
it { is_expected.to forbid_actions(%i[update destroy]) }
5060
end
5161
end
5262
end

spec/policies/procedure_policy_spec.rb

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,29 @@
77
let(:procedure) { create(:procedure, user: user) }
88
let(:procedure_without_user) { create(:procedure) }
99

10-
permissions :index?, :create? do
10+
describe "permissions" do
1111
context "when has no user" do
12-
it { expect(described_class).not_to permit(nil, procedure) }
12+
subject { described_class.new(nil, procedure) }
13+
14+
it { is_expected.to forbid_all_actions }
1315
end
14-
end
1516

16-
permissions :update?, :destroy? do
17-
context "when user is not owner" do
18-
it { expect(described_class).not_to permit(user, procedure_without_user) }
17+
context "when user is present" do
18+
subject { described_class.new(user, procedure) }
19+
20+
it { is_expected.to permit_actions(%i[index create]) }
1921
end
2022

2123
context "when user is owner" do
22-
it { expect(described_class).to permit(user, procedure) }
24+
subject { described_class.new(user, procedure) }
25+
26+
it { is_expected.to permit_actions(%i[index create update destroy]) }
27+
end
28+
29+
context "when user is not owner" do
30+
subject { described_class.new(user, procedure_without_user) }
31+
32+
it { is_expected.to forbid_actions(%i[update destroy]) }
2333
end
2434
end
2535
end

0 commit comments

Comments
 (0)