Skip to content

Commit

Permalink
Refactor Pundit specs
Browse files Browse the repository at this point in the history
- Use `subject { described_class }`, as recommended in the project:

https://github.com/varvet/pundit?tab=readme-ov-file#rspec

- Use `is_expected` instead of `expect(subject)` for clarity.
  • Loading branch information
eduardoj committed Mar 14, 2024
1 parent c015695 commit 3c4718c
Show file tree
Hide file tree
Showing 13 changed files with 44 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/api/spec/policies/appeal_policy_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
RSpec.describe AppealPolicy do
subject { AppealPolicy }
subject { described_class }

let(:anonymous_user) { create(:user_nobody) }
let(:user) { create(:confirmed_user) }
Expand Down
8 changes: 4 additions & 4 deletions src/api/spec/policies/attrib_namespace_policy_spec.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
RSpec.describe AttribNamespacePolicy do
subject { AttribNamespacePolicy }
subject { described_class }

let(:anonymous_user) { create(:user_nobody) }
let(:user_in_a_group) { create(:groups_user, user: create(:confirmed_user)).user }
let(:admin_user) { create(:admin_user) }
let(:attrib_namespace) { create(:attrib_namespace) }

context :without_explicit_permissions do
context 'without explicit permissions' do
permissions :create?, :update?, :destroy? do
it { is_expected.not_to permit(anonymous_user, attrib_namespace) }
it { is_expected.not_to permit(user_in_a_group, attrib_namespace) }
it { is_expected.to permit(admin_user, attrib_namespace) }
end
end

context :with_explicit_permissions_for_a_group do
context 'with explicit permissions for a group' do
before do
create(:attrib_namespace_modifiable_by, attrib_namespace: attrib_namespace, group: user_in_a_group.groups.first, user: nil)
end
Expand All @@ -26,7 +26,7 @@
end
end

context :with_explicit_permissions_for_a_user do
context 'with explicit permissions for a user' do
before do
create(:attrib_namespace_modifiable_by, attrib_namespace: attrib_namespace, user: user_in_a_group, group: nil)
end
Expand Down
12 changes: 6 additions & 6 deletions src/api/spec/policies/attrib_policy_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
RSpec.describe AttribPolicy do
subject { AttribPolicy }
subject { described_class }

let(:anonymous_user) { create(:user_nobody) }
let(:user_in_a_group) { create(:groups_user, user: create(:confirmed_user, :with_home)).user }
let(:admin_user) { create(:admin_user) }

context :without_explicit_permissions do
context 'without explicit permissions' do
let(:attrib) { create(:attrib) }

permissions :create?, :update?, :destroy? do
Expand All @@ -15,7 +15,7 @@
end
end

context :with_permissions_on_attrib_container do
context 'with permissions on attrib container' do
let(:attrib) { create(:attrib, project: user_in_a_group.home_project) }

permissions :create?, :update?, :destroy? do
Expand All @@ -25,7 +25,7 @@
end
end

context :with_permissions_for_a_group do
context 'with permissions for a group' do
let(:attrib) { create(:attrib) }

before do
Expand All @@ -39,7 +39,7 @@
end
end

context :with_permissions_for_a_user do
context 'with permissions for a user' do
let(:attrib) { create(:attrib) }

before do
Expand All @@ -53,7 +53,7 @@
end
end

context :with_permissions_for_a_role do
context 'with permissions for a role' do
let(:attrib) { create(:attrib, project: user_in_a_group.home_project) }
let(:user_role) { create(:roles_user, user: user_in_a_group).role }

Expand Down
2 changes: 1 addition & 1 deletion src/api/spec/policies/cloud/user/upload_job_policy_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
RSpec.describe Cloud::User::UploadJobPolicy do
subject { Cloud::User::UploadJobPolicy }
subject { described_class }

let(:uploader) { create(:confirmed_user) }
let(:user) { create(:confirmed_user) }
Expand Down
14 changes: 7 additions & 7 deletions src/api/spec/policies/comment_policy_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
RSpec.describe CommentPolicy do
subject { CommentPolicy }
subject { described_class }

let(:anonymous_user) { create(:user_nobody) }
let(:comment_author) { create(:confirmed_user, login: 'burdenski') }
Expand Down Expand Up @@ -49,8 +49,8 @@
allow(other_user).to receive(:has_local_permission?).with('change_package', package).and_return(false)
end

it { expect(subject).to permit(user, comment_on_package) }
it { expect(subject).not_to permit(other_user, comment_on_package) }
it { is_expected.to permit(user, comment_on_package) }
it { is_expected.not_to permit(other_user, comment_on_package) }
end

context 'with a comment of a Project' do
Expand All @@ -59,8 +59,8 @@
allow(other_user).to receive(:has_local_permission?).with('change_project', project).and_return(false)
end

it { expect(subject).to permit(user, comment) }
it { expect(subject).not_to permit(other_user, comment) }
it { is_expected.to permit(user, comment) }
it { is_expected.not_to permit(other_user, comment) }
end

context 'with a comment of a Request' do
Expand All @@ -69,8 +69,8 @@
allow(request).to receive(:is_target_maintainer?).with(other_user).and_return(false)
end

it { expect(subject).to permit(user, comment_on_request) }
it { expect(subject).not_to permit(other_user, comment_on_request) }
it { is_expected.to permit(user, comment_on_request) }
it { is_expected.not_to permit(other_user, comment_on_request) }
end
end

Expand Down
2 changes: 1 addition & 1 deletion src/api/spec/policies/group_policy_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
RSpec.describe GroupPolicy do
subject { GroupPolicy }
subject { described_class }

let(:group) { create(:group) }
let(:anonymous_user) { create(:user_nobody) }
Expand Down
16 changes: 8 additions & 8 deletions src/api/spec/policies/package_policy_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
RSpec.describe PackagePolicy do
subject { PackagePolicy }
subject { described_class }

let(:anonymous_user) { create(:user_nobody) }
let(:user) { create(:confirmed_user, :with_home) }
Expand All @@ -8,7 +8,7 @@
let(:project) { user.home_project }
let(:package) { create(:package, name: 'my_package', project: project) }

context :create_in_locked_project_without_ignore_lock do
context 'create in locked project without ignore lock' do
permissions :create? do
before do
allow(package.project).to receive(:is_locked?).and_return(true)
Expand All @@ -23,7 +23,7 @@
end
end

context :create_in_locked_project_with_ignore_lock do
context 'create in locked project with ignore lock' do
permissions :create? do
before do
allow(package.project).to receive(:is_locked?).and_return(true)
Expand All @@ -39,7 +39,7 @@
end
end

context :create_in_unlocked_project do
context 'create in unlocked project' do
permissions :create? do
before do
allow(other_user).to receive(:has_global_permission?).with('create_package').and_return(true)
Expand All @@ -53,7 +53,7 @@
end
end

context :branch_as_anonymous do
context 'branch as anonymous' do
permissions :create_branch? do
before do
skip('it should fail but its passing')
Expand All @@ -63,7 +63,7 @@
end
end

context :branch_as_other_user do
context 'branch as other user' do
permissions :create_branch? do
it { expect(subject).to permit(other_user, package) }
end
Expand All @@ -77,7 +77,7 @@
it { expect(subject).to permit(user, package) }
end

context :source_access_enabled do
context 'source access enabled' do
permissions :source_access? do
before do
allow_any_instance_of(Package).to receive(:disabled_for?).with('sourceaccess', nil, nil).and_return(true)
Expand All @@ -87,7 +87,7 @@
end
end

context :source_access_disabled do
context 'source access disabled' do
permissions :source_access? do
before do
allow_any_instance_of(Package).to receive(:disabled_for?).with('sourceaccess', nil, nil).and_return(false)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
RSpec.describe Staging::StagedRequestPolicy do
subject { Staging::StagedRequestPolicy }
subject { described_class }

let(:admin) { create(:admin_user) }
let(:authorized_user) { create(:confirmed_user, login: 'Tom') }
Expand Down
2 changes: 1 addition & 1 deletion src/api/spec/policies/staging/workflow_policy_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
RSpec.describe Staging::WorkflowPolicy do
subject { Staging::WorkflowPolicy }
subject { described_class }

let(:admin) { create(:admin_user) }
let(:user_nobody) { build(:user_nobody) }
Expand Down
18 changes: 9 additions & 9 deletions src/api/spec/policies/status/report_policy_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
RSpec.describe Status::ReportPolicy do
subject { Status::ReportPolicy }
subject { described_class }

let(:anonymous_user) { create(:user_nobody) }

Expand All @@ -11,28 +11,28 @@
context "users that don't belong to the repository project" do
let(:user) { create(:confirmed_user) }

it { expect(subject).not_to permit(user, status_report) }
it { is_expected.not_to permit(user, status_report) }
end

context 'user is admin' do
let(:user) { create(:admin_user) }

it { expect(subject).to permit(user, status_report) }
it { is_expected.to permit(user, status_report) }
end

context 'user is project maintainer' do
let(:user) { create(:confirmed_user) }
let!(:relationship) { create(:relationship_project_user, user: user, project: project) }

it { expect(subject).to permit(user, status_report) }
it { is_expected.to permit(user, status_report) }
end

context 'user is member of project maintainer group' do
let(:user) { create(:user) }
let(:group) { create(:group_with_user, user: user) }
let!(:relationship) { create(:relationship_project_group, group: group, project: project) }

it { expect(subject).to permit(user, status_report) }
it { is_expected.to permit(user, status_report) }
end
end

Expand All @@ -49,28 +49,28 @@
context "users that don't belong to the repository project" do
let(:user) { create(:confirmed_user) }

it { expect(subject).not_to permit(user, status_report) }
it { is_expected.not_to permit(user, status_report) }
end

context 'user is admin' do
let(:user) { create(:admin_user) }

it { expect(subject).to permit(user, status_report) }
it { is_expected.to permit(user, status_report) }
end

context 'user is project maintainer' do
let(:user) { create(:confirmed_user) }
let!(:relationship) { create(:relationship_project_user, user: user, project: target_project) }

it { expect(subject).to permit(user, status_report) }
it { is_expected.to permit(user, status_report) }
end

context 'user is member of project maintainer group' do
let(:user) { create(:user) }
let(:group) { create(:group_with_user, user: user) }
let!(:relationship) { create(:relationship_project_group, group: group, project: target_project) }

it { expect(subject).to permit(user, status_report) }
it { is_expected.to permit(user, status_report) }
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion src/api/spec/policies/status_message_policy_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
RSpec.describe StatusMessagePolicy do
subject { StatusMessagePolicy }
subject { described_class }

let(:anonymous_user) { create(:user_nobody) }
let(:user) { create(:confirmed_user) }
Expand Down
2 changes: 1 addition & 1 deletion src/api/spec/policies/token/workflow_policy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
let(:user) { create(:confirmed_user, login: 'foo') }

permissions :trigger? do
it { expect(subject).to permit(user, user_token) }
it { is_expected.to permit(user, user_token) }
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions src/api/spec/policies/user_policy_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
RSpec.describe UserPolicy do
subject { UserPolicy }
subject { described_class }

let(:user) { create(:confirmed_user) }
let(:other_user) { create(:confirmed_user) }
Expand All @@ -14,15 +14,15 @@
allow(user).to receive(:can_modify_user?).with(other_user).and_return true
end

it { expect(subject).to permit(user, other_user) }
it { is_expected.to permit(user, other_user) }
end

context 'user can not modify the other user' do
before do
allow(user).to receive(:can_modify_user?).with(other_user).and_return false
end

it { expect(subject).not_to permit(user, other_user) }
it { is_expected.not_to permit(user, other_user) }
end
end
end

0 comments on commit 3c4718c

Please sign in to comment.