Skip to content

Commit 82cc57b

Browse files
authored
Merge pull request #833 from rubyforgood/drop-pundit-shared-contexts
Drop pundit rspec shared contexts
2 parents 8086ba8 + 646d4e5 commit 82cc57b

File tree

6 files changed

+47
-92
lines changed

6 files changed

+47
-92
lines changed

spec/policies/person_policy_spec.rb

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
require 'rails_helper'
22

33
RSpec.describe PersonPolicy do
4-
subject { PersonPolicy.new(acting_user, person) }
4+
subject { PersonPolicy.new(user, person) }
5+
56
let(:resolved_scope) {
6-
PersonPolicy::Scope.new(acting_user, Person.all).resolve
7+
PersonPolicy::Scope.new(user, Person.all).resolve
78
}
89

9-
shared_context "own Person" do
10-
let(:person) { FactoryBot.create(:person, user: acting_user) }
11-
end
12-
shared_context "different Person" do
13-
let(:person) { FactoryBot.create(:person) }
14-
end
10+
let(:user) { create(:user) }
11+
let(:own_person) { create(:person, user: user) }
12+
let(:other_person) { create(:person) }
13+
1514

1615
context "given a normal signed-in user and their Person" do
17-
include_context "acting as signed-in user"
18-
include_context "own Person"
19-
16+
let(:person) { own_person }
17+
2018
it { is_expected.to permit_action(:show) }
2119
it { is_expected.to permit_new_and_create_actions }
2220
it { is_expected.to permit_edit_and_update_actions }
@@ -28,9 +26,8 @@
2826
end
2927

3028
context "given a normal signed-in user and a different Person" do
31-
include_context "acting as signed-in user"
32-
include_context "different Person"
33-
29+
let(:person) { other_person }
30+
3431
it { is_expected.to forbid_action(:show) }
3532
it { is_expected.to forbid_new_and_create_actions }
3633
it { is_expected.to forbid_edit_and_update_actions }
@@ -42,9 +39,9 @@
4239
end
4340

4441
context "given a sysadmin and a their Person" do
45-
include_context "acting as sysadmin"
46-
include_context "own Person"
47-
42+
let(:user) { create(:user, :sys_admin) }
43+
let(:person) { own_person }
44+
4845
it { is_expected.to permit_action(:show) }
4946
it { is_expected.to permit_new_and_create_actions }
5047
it { is_expected.to permit_edit_and_update_actions }
@@ -56,9 +53,9 @@
5653
end
5754

5855
context "given a sysadmin and a different Person" do
59-
include_context "acting as sysadmin"
60-
include_context "different Person"
61-
56+
let(:user) { create(:user, :sys_admin) }
57+
let(:person) { other_person }
58+
6259
it { is_expected.to permit_action(:show) }
6360
it { is_expected.to permit_new_and_create_actions }
6461
it { is_expected.to permit_edit_and_update_actions }
@@ -70,9 +67,9 @@
7067
end
7168

7269
context "given an admin and their Person" do
73-
include_context "acting as admin"
74-
include_context "own Person"
75-
70+
let(:user) { create(:user, :admin) }
71+
let(:person) { own_person }
72+
7673
it { is_expected.to permit_action(:show) }
7774
it { is_expected.to permit_new_and_create_actions }
7875
it { is_expected.to permit_edit_and_update_actions }
@@ -84,9 +81,9 @@
8481
end
8582

8683
context "given an admin and a different Person" do
87-
include_context "acting as admin"
88-
include_context "different Person"
89-
84+
let(:user) { create(:user, :admin) }
85+
let(:person) { other_person }
86+
9087
it { is_expected.to permit_action(:show) }
9188
it { is_expected.to forbid_new_and_create_actions }
9289
it { is_expected.to permit_edit_and_update_actions }

spec/policies/user_policy_spec.rb

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
11
require 'rails_helper'
22

33
RSpec.describe UserPolicy do
4-
subject { UserPolicy.new(acting_user, target_user) }
4+
subject { UserPolicy.new(current_user, target_user) }
5+
56
let(:resolved_scope) {
6-
UserPolicy::Scope.new(acting_user, User.all).resolve
7+
UserPolicy::Scope.new(current_user, User.all).resolve
78
}
89

9-
shared_context "own User" do
10-
let(:target_user) { acting_user }
11-
end
12-
shared_context "different User" do
13-
let(:target_user) { FactoryBot.create(:user) }
14-
end
15-
1610
context "given a normal signed-in user, targeting themselves" do
17-
include_context "acting as signed-in user"
18-
include_context "own User"
19-
11+
let(:current_user) { create(:user) }
12+
let(:target_user) { current_user }
13+
2014
it { is_expected.to permit_action(:show) }
2115
it { is_expected.to permit_new_and_create_actions }
2216
it { is_expected.to permit_edit_and_update_actions }
@@ -28,9 +22,9 @@
2822
end
2923

3024
context "given a normal signed-in user and a different User" do
31-
include_context "acting as signed-in user"
32-
include_context "different User"
33-
25+
let(:current_user) { create(:user) }
26+
let(:target_user) { create(:user) }
27+
3428
it { is_expected.to forbid_action(:show) }
3529
it { is_expected.to permit_new_and_create_actions }
3630
it { is_expected.to forbid_edit_and_update_actions }
@@ -42,9 +36,9 @@
4236
end
4337

4438
context "given a sysadmin and a their User" do
45-
include_context "acting as sysadmin"
46-
include_context "own User"
47-
39+
let(:current_user) { create(:user, :sys_admin) }
40+
let(:target_user) { create(:user) }
41+
4842
it { is_expected.to permit_action(:show) }
4943
it { is_expected.to permit_new_and_create_actions }
5044
it { is_expected.to permit_edit_and_update_actions }
@@ -56,9 +50,9 @@
5650
end
5751

5852
context "given a sysadmin and a different User" do
59-
include_context "acting as sysadmin"
60-
include_context "different User"
61-
53+
let(:current_user) { create(:user, :sys_admin) }
54+
let(:target_user) { create(:user) }
55+
6256
it { is_expected.to permit_action(:show) }
6357
it { is_expected.to permit_new_and_create_actions }
6458
it { is_expected.to permit_edit_and_update_actions }
@@ -70,9 +64,9 @@
7064
end
7165

7266
context "given an admin and their User" do
73-
include_context "acting as admin"
74-
include_context "own User"
75-
67+
let(:current_user) { create(:user, :admin) }
68+
let(:target_user) { current_user }
69+
7670
it { is_expected.to permit_action(:show) }
7771
it { is_expected.to permit_new_and_create_actions }
7872
it { is_expected.to permit_edit_and_update_actions }
@@ -84,9 +78,9 @@
8478
end
8579

8680
context "given an admin and a different User" do
87-
include_context "acting as admin"
88-
include_context "different User"
89-
81+
let(:current_user) { create(:user, :admin) }
82+
let(:target_user) { current_user }
83+
9084
it { is_expected.to permit_action(:show) }
9185
it { is_expected.to permit_new_and_create_actions }
9286
it { is_expected.to permit_edit_and_update_actions }

spec/rails_helper.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
# directory. Alternatively, in the individual `*_spec.rb` files, manually
2424
# require only the support files necessary.
2525
#
26-
Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f }
26+
# Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f }
2727

2828
# Checks for pending migrations and applies them before tests are run.
2929
# If you are not using ActiveRecord, you can remove these lines.
@@ -76,7 +76,3 @@
7676
# config.filter_gems_from_backtrace("gem name")
7777
#
7878
end
79-
80-
Pundit::Matchers.configure do |config|
81-
config.user_alias = :acting_user
82-
end

spec/requests/people_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require 'rails_helper'
22

33
RSpec.describe "/people", type: :request do
4-
include_context "signed in as sysadmin"
4+
before { sign_in FactoryBot.create(:user, :sys_admin) }
55

66
describe 'GET /index' do
77
let!(:people) { create_list(:person, 40) }

spec/requests/users_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require 'rails_helper'
22

33
RSpec.describe "/users", type: :request do
4-
include_context "signed in as sysadmin"
4+
before { sign_in FactoryBot.create(:user, :sys_admin) }
55

66
describe "GET /index" do
77
it "can render" do

spec/support/shared_contexts/authorization.rb

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)