Use build_stubbed in Policy specs (#4017) [ci skip]
This commit is contained in:
parent
1e8954e581
commit
64f70fff8f
18 changed files with 80 additions and 84 deletions
|
|
@ -1,14 +1,14 @@
|
|||
require "rails_helper"
|
||||
|
||||
RSpec.describe AdminPolicy do
|
||||
subject { described_class }
|
||||
RSpec.describe AdminPolicy, type: :policy do
|
||||
let(:admin_policy) { described_class }
|
||||
|
||||
permissions :show? do
|
||||
context "when regular user" do
|
||||
let(:user) { build(:user) }
|
||||
let(:user) { build_stubbed(:user) }
|
||||
|
||||
it "does not allow someone without admin privileges to do continue" do
|
||||
expect(subject).not_to permit(user) # rubocop:disable RSpec/NamedSubject
|
||||
expect(admin_policy).not_to permit(user)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -16,7 +16,7 @@ RSpec.describe AdminPolicy do
|
|||
let(:user) { build(:user, :super_admin) }
|
||||
|
||||
it "allow someone with admin privileges to continue" do
|
||||
expect(subject).to permit(user) # rubocop:disable RSpec/NamedSubject
|
||||
expect(admin_policy).to permit(user)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,26 +1,28 @@
|
|||
require "rails_helper"
|
||||
|
||||
RSpec.describe ApiSecretPolicy do
|
||||
RSpec.describe ApiSecretPolicy, type: :policy do
|
||||
subject { described_class.new(user, api_secret) }
|
||||
|
||||
let(:api_secret) { build(:api_secret) }
|
||||
let(:valid_attributes) { %i[description] }
|
||||
|
||||
context "when user is not signed in" do
|
||||
let(:user) { nil }
|
||||
let(:user) { nil }
|
||||
let(:api_secret) { build_stubbed(:api_secret) }
|
||||
|
||||
it { within_block_is_expected.to raise_error(Pundit::NotAuthorizedError) }
|
||||
end
|
||||
|
||||
context "when user owns the secret" do
|
||||
let(:user) { api_secret.user }
|
||||
let(:user) { build_stubbed(:user) }
|
||||
let(:api_secret) { build_stubbed(:api_secret, user: user) }
|
||||
|
||||
it { is_expected.to permit_actions %i[create destroy] }
|
||||
it { is_expected.to permit_mass_assignment_of(valid_attributes) }
|
||||
end
|
||||
|
||||
context "when user does not own the secret" do
|
||||
let(:user) { create(:user) }
|
||||
let(:user) { build_stubbed(:user) }
|
||||
let(:api_secret) { build_stubbed(:api_secret) }
|
||||
|
||||
it { is_expected.to permit_actions %i[create] }
|
||||
it { is_expected.to forbid_actions %i[destroy] }
|
||||
|
|
|
|||
|
|
@ -3,13 +3,15 @@ require "rails_helper"
|
|||
RSpec.describe ArticlePolicy do
|
||||
subject { described_class.new(user, article) }
|
||||
|
||||
let(:article) { create(:article) }
|
||||
let(:article) { build_stubbed(:article) }
|
||||
let(:valid_attributes) do
|
||||
%i[title body_html body_markdown main_image published
|
||||
description allow_small_edits allow_big_edits tag_list publish_under_org
|
||||
video video_code video_source_url video_thumbnail_url receive_notifications]
|
||||
end
|
||||
|
||||
before { allow(article).to receive(:published).and_return(true) }
|
||||
|
||||
context "when user is not signed-in" do
|
||||
let(:user) { nil }
|
||||
|
||||
|
|
@ -17,13 +19,13 @@ RSpec.describe ArticlePolicy do
|
|||
end
|
||||
|
||||
context "when user is not the author" do
|
||||
let(:user) { create(:user) }
|
||||
let(:user) { build_stubbed(:user) }
|
||||
|
||||
it { is_expected.to permit_actions(%i[new create preview]) }
|
||||
it { is_expected.to forbid_actions(%i[update edit manage delete_confirm destroy]) }
|
||||
|
||||
context "with banned status" do
|
||||
before { user.add_role :banned }
|
||||
before { user.add_role(:banned) }
|
||||
|
||||
it { is_expected.to permit_actions(%i[new preview]) }
|
||||
it { is_expected.to forbid_actions(%i[create edit manage update delete_confirm destroy]) }
|
||||
|
|
@ -31,13 +33,14 @@ RSpec.describe ArticlePolicy do
|
|||
end
|
||||
|
||||
context "when user is the author" do
|
||||
let(:user) { article.user }
|
||||
let(:user) { build_stubbed(:user) }
|
||||
let(:article) { build_stubbed(:article, user: user) }
|
||||
|
||||
it { is_expected.to permit_actions(%i[update edit manage new create delete_confirm destroy preview]) }
|
||||
it { is_expected.to permit_mass_assignment_of(valid_attributes) }
|
||||
|
||||
context "with banned status" do
|
||||
before { user.add_role :banned }
|
||||
before { user.add_role(:banned) }
|
||||
|
||||
it { is_expected.to permit_actions(%i[update new delete_confirm destroy preview]) }
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
require "rails_helper"
|
||||
|
||||
RSpec.describe BlockPolicy do
|
||||
RSpec.describe BlockPolicy, type: :policy do
|
||||
subject { described_class.new(user, block) }
|
||||
|
||||
let(:block) { build(:block) }
|
||||
let(:block) { build_stubbed(:block) }
|
||||
|
||||
let(:valid_attributes) do
|
||||
%i[input_html input_css input_javascript featured index_position publish_now]
|
||||
|
|
@ -16,7 +16,7 @@ RSpec.describe BlockPolicy do
|
|||
end
|
||||
|
||||
context "when signed in as a regular user" do
|
||||
let(:user) { build(:user) }
|
||||
let(:user) { build_stubbed(:user) }
|
||||
|
||||
it { is_expected.to forbid_actions(%i[index show new edit create update destroy]) }
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
require "rails_helper"
|
||||
|
||||
RSpec.describe BufferUpdatePolicy do
|
||||
RSpec.describe BufferUpdatePolicy, type: :policy do
|
||||
subject { described_class.new(user, article) }
|
||||
|
||||
let(:article) { build(:article) }
|
||||
let(:article) { build_stubbed(:article) }
|
||||
|
||||
context "when user is trusted" do
|
||||
let(:user) { build(:user, :trusted) }
|
||||
|
|
|
|||
|
|
@ -1,33 +1,27 @@
|
|||
require "rails_helper"
|
||||
|
||||
RSpec.describe ChatChannelMembershipPolicy do
|
||||
RSpec.describe ChatChannelMembershipPolicy, type: :policy do
|
||||
subject { described_class.new(user, chat_channel_membership) }
|
||||
|
||||
let(:chat_channel_membership) { build(:chat_channel_membership) }
|
||||
let(:user) { build_stubbed(:user) }
|
||||
let(:chat_channel) { build_stubbed(:chat_channel) }
|
||||
|
||||
context "when user is not signed-in" do
|
||||
let(:user) { nil }
|
||||
let(:chat_channel_membership) { build_stubbed(:chat_channel_membership) }
|
||||
|
||||
it { within_block_is_expected.to raise_error(Pundit::NotAuthorizedError) }
|
||||
end
|
||||
|
||||
context "when user belongs to membership" do
|
||||
let(:user) { create(:user) }
|
||||
let(:chat_channel) { create(:chat_channel) }
|
||||
let(:chat_channel_membership) do
|
||||
create(:chat_channel_membership, user_id: user.id, chat_channel_id: chat_channel.id)
|
||||
end
|
||||
let(:chat_channel_membership) { build_stubbed(:chat_channel_membership, user: user, chat_channel: chat_channel) }
|
||||
|
||||
it { is_expected.to permit_actions(%i[update destroy]) }
|
||||
end
|
||||
|
||||
context "when user does not belong to membership" do
|
||||
let(:user) { create(:user) }
|
||||
let(:other_user) { create(:user) }
|
||||
let(:chat_channel) { create(:chat_channel) }
|
||||
let(:chat_channel_membership) do
|
||||
create(:chat_channel_membership, user_id: other_user.id, chat_channel_id: chat_channel.id)
|
||||
end
|
||||
let(:other_user) { build_stubbed(:user) }
|
||||
let(:chat_channel_membership) { build_stubbed(:chat_channel_membership, user: other_user, chat_channel: chat_channel) }
|
||||
|
||||
it { is_expected.to forbid_actions(%i[update destroy]) }
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,9 +1,10 @@
|
|||
require "rails_helper"
|
||||
|
||||
RSpec.describe ChatChannelPolicy do
|
||||
RSpec.describe ChatChannelPolicy, type: :policy do
|
||||
subject { described_class.new(user, chat_channel) }
|
||||
|
||||
let(:chat_channel) { create(:chat_channel, channel_type: "invite_only") }
|
||||
let(:chat_channel) { build_stubbed(:chat_channel, channel_type: "invite_only") }
|
||||
let(:user) { build_stubbed(:user) }
|
||||
|
||||
context "when user is not signed-in" do
|
||||
let(:user) { nil }
|
||||
|
|
@ -12,24 +13,18 @@ RSpec.describe ChatChannelPolicy do
|
|||
end
|
||||
|
||||
context "when user is not a part of channel" do
|
||||
let(:user) { build(:user) }
|
||||
|
||||
it { is_expected.to permit_actions(%i[index]) }
|
||||
it { is_expected.to forbid_actions(%i[show open moderate update]) }
|
||||
end
|
||||
|
||||
context "when user is a part of channel" do
|
||||
let(:user) { create(:user) }
|
||||
|
||||
before { chat_channel.add_users [user] }
|
||||
before { allow(chat_channel).to receive(:has_member?).with(user).and_return(true) }
|
||||
|
||||
it { is_expected.to permit_actions(%i[index show open]) }
|
||||
it { is_expected.to forbid_actions(%i[moderate update]) }
|
||||
end
|
||||
|
||||
context "when user is an admin but not part of channel" do
|
||||
let(:user) { create(:user) }
|
||||
|
||||
before { user.add_role(:super_admin) }
|
||||
|
||||
it { is_expected.to permit_actions(%i[index moderate update]) }
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
require "rails_helper"
|
||||
|
||||
RSpec.describe CommentPolicy do
|
||||
RSpec.describe CommentPolicy, type: :policy do
|
||||
subject(:comment_policy) { described_class.new(user, comment) }
|
||||
|
||||
let(:comment) { build(:comment) }
|
||||
let(:comment) { build_stubbed(:comment) }
|
||||
|
||||
let(:valid_attributes_for_create) do
|
||||
%i[body_markdown commentable_id commentable_type parent_id]
|
||||
|
|
@ -20,7 +20,7 @@ RSpec.describe CommentPolicy do
|
|||
end
|
||||
|
||||
context "when user is not the author" do
|
||||
let(:user) { create(:user) }
|
||||
let(:user) { build_stubbed(:user) }
|
||||
|
||||
it { is_expected.to permit_actions(%i[create]) }
|
||||
it { is_expected.to forbid_actions(%i[edit update destroy delete_confirm]) }
|
||||
|
|
@ -28,13 +28,13 @@ RSpec.describe CommentPolicy do
|
|||
it { is_expected.to permit_mass_assignment_of(valid_attributes_for_create).for_action(:create) }
|
||||
|
||||
context "with banned status" do
|
||||
before { user.add_role :banned }
|
||||
before { user.add_role(:banned) }
|
||||
|
||||
it { is_expected.to forbid_actions(%i[create edit update destroy delete_confirm]) }
|
||||
end
|
||||
|
||||
context "with banned_comment status" do
|
||||
before { user.add_role :comment_banned }
|
||||
before { user.add_role(:comment_banned) }
|
||||
|
||||
it { is_expected.to forbid_actions(%i[create edit update destroy delete_confirm]) }
|
||||
end
|
||||
|
|
@ -49,7 +49,7 @@ RSpec.describe CommentPolicy do
|
|||
it { is_expected.to permit_mass_assignment_of(valid_attributes_for_update).for_action(:update) }
|
||||
|
||||
context "with banned status" do
|
||||
before { user.add_role :banned }
|
||||
before { user.add_role(:banned) }
|
||||
|
||||
it { is_expected.to permit_actions(%i[edit update destroy delete_confirm]) }
|
||||
it { is_expected.to forbid_actions(%i[create]) }
|
||||
|
|
@ -60,7 +60,7 @@ RSpec.describe CommentPolicy do
|
|||
end
|
||||
|
||||
context "with banned_comment status" do
|
||||
before { user.add_role :comment_banned }
|
||||
before { user.add_role(:comment_banned) }
|
||||
|
||||
it { is_expected.to permit_actions(%i[edit update destroy delete_confirm]) }
|
||||
it { is_expected.to forbid_actions(%i[create]) }
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
require "rails_helper"
|
||||
|
||||
RSpec.describe FollowPolicy do
|
||||
RSpec.describe FollowPolicy, type: :policy do
|
||||
subject { described_class.new(user, Follow) }
|
||||
|
||||
context "when user is not signed in" do
|
||||
|
|
@ -10,12 +10,12 @@ RSpec.describe FollowPolicy do
|
|||
end
|
||||
|
||||
context "when user is signed in" do
|
||||
let(:user) { build(:user) }
|
||||
let(:user) { build_stubbed(:user) }
|
||||
|
||||
it { is_expected.to permit_actions(%i[create]) }
|
||||
|
||||
context "when user is banned" do
|
||||
let(:user) { build(:user, :banned) }
|
||||
before { user.add_role(:banned) }
|
||||
|
||||
it { is_expected.to forbid_actions(%i[create]) }
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
require "rails_helper"
|
||||
|
||||
RSpec.describe GithubRepoPolicy do
|
||||
RSpec.describe GithubRepoPolicy, type: :policy do
|
||||
subject { described_class.new(user, github_repo) }
|
||||
|
||||
let(:github_repo) { build(:github_repo) }
|
||||
let(:github_repo) { build_stubbed(:github_repo) }
|
||||
let(:valid_attributes) { %i[github_id_code] }
|
||||
|
||||
context "when user is not signed in" do
|
||||
|
|
@ -13,13 +13,13 @@ RSpec.describe GithubRepoPolicy do
|
|||
end
|
||||
|
||||
context "when user is not the owner" do
|
||||
let(:user) { create(:user) }
|
||||
let(:user) { build_stubbed(:user) }
|
||||
|
||||
it { is_expected.to permit_actions(%i[create]) }
|
||||
it { is_expected.to forbid_actions(%i[update]) }
|
||||
|
||||
context "when user is banned" do
|
||||
let(:user) { build(:user, :banned) }
|
||||
before { user.add_role(:banned) }
|
||||
|
||||
it { is_expected.to forbid_actions(%i[create update]) }
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,18 +1,18 @@
|
|||
require "rails_helper"
|
||||
|
||||
RSpec.describe HtmlVariantPolicy do
|
||||
RSpec.describe HtmlVariantPolicy, type: :policy do
|
||||
subject { described_class.new(user, html_variant) }
|
||||
|
||||
context "when user is not an admin" do
|
||||
let(:user) { build(:user) }
|
||||
let(:html_variant) { build(:html_variant) }
|
||||
let(:user) { build_stubbed(:user) }
|
||||
let(:html_variant) { build_stubbed(:html_variant) }
|
||||
|
||||
it { is_expected.to forbid_actions(%i[index show edit update create]) }
|
||||
end
|
||||
|
||||
context "when user is an admin" do
|
||||
let(:user) { build(:user, :super_admin) }
|
||||
let(:html_variant) { build(:html_variant) }
|
||||
let(:user) { build(:user, :super_admin) }
|
||||
let(:html_variant) { build_stubbed(:html_variant) }
|
||||
|
||||
it { is_expected.to permit_actions(%i[index show edit update create]) }
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
require "rails_helper"
|
||||
|
||||
RSpec.describe ImageUploadPolicy do
|
||||
RSpec.describe ImageUploadPolicy, type: :policy do
|
||||
subject { described_class.new(user, image) }
|
||||
|
||||
let(:image) { "📸.jpg" }
|
||||
|
|
@ -12,7 +12,7 @@ RSpec.describe ImageUploadPolicy do
|
|||
end
|
||||
|
||||
context "when user is signed in" do
|
||||
let(:user) { build(:user) }
|
||||
let(:user) { build_stubbed(:user) }
|
||||
|
||||
it { is_expected.to permit_actions(%i[create]) }
|
||||
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
require "rails_helper"
|
||||
|
||||
RSpec.describe OrganizationPolicy do
|
||||
RSpec.describe OrganizationPolicy, type: :policy do
|
||||
subject(:organization_policy) { described_class.new(user, organization) }
|
||||
|
||||
let(:organization) { build(:organization) }
|
||||
let(:organization) { build_stubbed(:organization) }
|
||||
|
||||
context "when user is not signed-in" do
|
||||
let(:user) { nil }
|
||||
|
|
@ -12,7 +12,7 @@ RSpec.describe OrganizationPolicy do
|
|||
end
|
||||
|
||||
context "when a non-org user" do
|
||||
let(:user) { build(:user) }
|
||||
let(:user) { build_stubbed(:user) }
|
||||
|
||||
it { is_expected.to forbid_action(:update) }
|
||||
it { is_expected.to permit_action(:create) }
|
||||
|
|
@ -27,10 +27,12 @@ RSpec.describe OrganizationPolicy do
|
|||
context "when user is an org admin of an org" do
|
||||
subject(:organization_policy) { described_class.new(user, org) }
|
||||
|
||||
let(:user) { create(:user) }
|
||||
let(:org) { create(:organization) }
|
||||
let(:user) { build_stubbed(:user) }
|
||||
let(:org) { build_stubbed(:organization) }
|
||||
|
||||
before { create(:organization_membership, user: user, organization: org, type_of_user: "admin") }
|
||||
before do
|
||||
create(:organization_membership, user: user, organization: org, type_of_user: "admin")
|
||||
end
|
||||
|
||||
it "allows the user to update their own org" do
|
||||
expect(organization_policy).to permit_action(:update)
|
||||
|
|
@ -40,9 +42,9 @@ RSpec.describe OrganizationPolicy do
|
|||
context "when user is an org admin of another org" do
|
||||
subject(:organization_policy) { described_class.new(user, new_org) }
|
||||
|
||||
let(:user) { create(:user) }
|
||||
let(:org) { create(:organization) }
|
||||
let(:new_org) { create(:organization) }
|
||||
let(:user) { build_stubbed(:user) }
|
||||
let(:org) { build_stubbed(:organization) }
|
||||
let(:new_org) { build_stubbed(:organization) }
|
||||
|
||||
before { create(:organization_membership, user: user, organization: org, type_of_user: "admin") }
|
||||
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ require "rails_helper"
|
|||
RSpec.describe ReactionPolicy do
|
||||
subject { described_class.new(user, reaction) }
|
||||
|
||||
let(:reaction) { build(:reaction) }
|
||||
let(:reaction) { build_stubbed(:reaction) }
|
||||
|
||||
context "when user is not signed in" do
|
||||
let(:user) { nil }
|
||||
|
|
@ -12,11 +12,11 @@ RSpec.describe ReactionPolicy do
|
|||
end
|
||||
|
||||
context "when user is signed in" do
|
||||
let(:user) { build(:user) }
|
||||
let(:user) { build_stubbed(:user) }
|
||||
|
||||
it { is_expected.to permit_actions(%i[index create]) }
|
||||
context "when user is banned" do
|
||||
let(:user) { build(:user, :banned) }
|
||||
before { user.add_role(:banned) }
|
||||
|
||||
it { is_expected.to permit_actions(%i[index]) }
|
||||
it { is_expected.to forbid_actions(%i[create]) }
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
require "rails_helper"
|
||||
|
||||
RSpec.describe StripeActiveCardPolicy do
|
||||
RSpec.describe StripeActiveCardPolicy, type: :policy do
|
||||
subject { described_class.new(user, :stripe_subscription) }
|
||||
|
||||
context "when user is not signed in" do
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
require "rails_helper"
|
||||
|
||||
RSpec.describe StripeSubscriptionPolicy do
|
||||
RSpec.describe StripeSubscriptionPolicy, type: :policy do
|
||||
subject { described_class.new(user, :stripe_subscription) }
|
||||
|
||||
context "when user is not signed in" do
|
||||
|
|
@ -10,7 +10,7 @@ RSpec.describe StripeSubscriptionPolicy do
|
|||
end
|
||||
|
||||
context "when user is signed in" do
|
||||
let(:user) { build(:user) }
|
||||
let(:user) { build_stubbed(:user) }
|
||||
|
||||
it { is_expected.to permit_actions(%i[create update destroy]) }
|
||||
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
require "rails_helper"
|
||||
|
||||
RSpec.describe TagPolicy do
|
||||
RSpec.describe TagPolicy, type: :policy do
|
||||
subject { described_class.new(user, tag) }
|
||||
|
||||
let(:tag) { build(:tag) }
|
||||
let(:tag) { build_stubbed(:tag) }
|
||||
|
||||
context "when user is not signed-in" do
|
||||
let(:user) { nil }
|
||||
|
|
@ -12,7 +12,7 @@ RSpec.describe TagPolicy do
|
|||
end
|
||||
|
||||
context "when user is not a moderator" do
|
||||
let(:user) { build(:user) }
|
||||
let(:user) { build_stubbed(:user) }
|
||||
|
||||
it { is_expected.to permit_actions(%i[index]) }
|
||||
it { is_expected.to forbid_actions(%i[edit update]) }
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
require "rails_helper"
|
||||
|
||||
RSpec.describe UserPolicy do
|
||||
RSpec.describe UserPolicy, type: :policy do
|
||||
subject { described_class.new(user, other_user) }
|
||||
|
||||
let(:other_user) { build(:user) }
|
||||
let(:other_user) { build_stubbed(:user) }
|
||||
|
||||
context "when user is not signed-in" do
|
||||
let(:user) { nil }
|
||||
|
|
@ -34,7 +34,7 @@ RSpec.describe UserPolicy do
|
|||
end
|
||||
|
||||
context "when user is not trusted" do
|
||||
let(:user) { build(:user) }
|
||||
let(:user) { build_stubbed(:user) }
|
||||
|
||||
it { is_expected.to forbid_actions(%i[moderation_routes]) }
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue