diff --git a/spec/policies/admin_policy_spec.rb b/spec/policies/admin_policy_spec.rb index 65235d898..9df210f50 100644 --- a/spec/policies/admin_policy_spec.rb +++ b/spec/policies/admin_policy_spec.rb @@ -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 diff --git a/spec/policies/api_secret_policy_spec.rb b/spec/policies/api_secret_policy_spec.rb index 9e4f6a922..3fad0044c 100644 --- a/spec/policies/api_secret_policy_spec.rb +++ b/spec/policies/api_secret_policy_spec.rb @@ -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] } diff --git a/spec/policies/article_policy_spec.rb b/spec/policies/article_policy_spec.rb index 509789b78..493be65c7 100644 --- a/spec/policies/article_policy_spec.rb +++ b/spec/policies/article_policy_spec.rb @@ -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 diff --git a/spec/policies/block_policy_spec.rb b/spec/policies/block_policy_spec.rb index d13525d0b..3723efdf0 100644 --- a/spec/policies/block_policy_spec.rb +++ b/spec/policies/block_policy_spec.rb @@ -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 diff --git a/spec/policies/buffer_update_policy_spec.rb b/spec/policies/buffer_update_policy_spec.rb index f7a316623..aac163c00 100644 --- a/spec/policies/buffer_update_policy_spec.rb +++ b/spec/policies/buffer_update_policy_spec.rb @@ -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) } diff --git a/spec/policies/chat_channel_membership_policy_spec.rb b/spec/policies/chat_channel_membership_policy_spec.rb index b53a93965..341f92873 100644 --- a/spec/policies/chat_channel_membership_policy_spec.rb +++ b/spec/policies/chat_channel_membership_policy_spec.rb @@ -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 diff --git a/spec/policies/chat_channel_policy_spec.rb b/spec/policies/chat_channel_policy_spec.rb index 076463434..42756d3ba 100644 --- a/spec/policies/chat_channel_policy_spec.rb +++ b/spec/policies/chat_channel_policy_spec.rb @@ -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]) } diff --git a/spec/policies/comment_policy_spec.rb b/spec/policies/comment_policy_spec.rb index d3a5f2a68..3946a14f4 100644 --- a/spec/policies/comment_policy_spec.rb +++ b/spec/policies/comment_policy_spec.rb @@ -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]) } diff --git a/spec/policies/follow_policy_spec.rb b/spec/policies/follow_policy_spec.rb index 9dd5c2a7b..01a7fee5e 100644 --- a/spec/policies/follow_policy_spec.rb +++ b/spec/policies/follow_policy_spec.rb @@ -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 diff --git a/spec/policies/github_repo_policy_spec.rb b/spec/policies/github_repo_policy_spec.rb index 18b0385b0..a7f887a6a 100644 --- a/spec/policies/github_repo_policy_spec.rb +++ b/spec/policies/github_repo_policy_spec.rb @@ -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 diff --git a/spec/policies/html_variant_policy_spec.rb b/spec/policies/html_variant_policy_spec.rb index d7a2c5eb7..4eb9db5b7 100644 --- a/spec/policies/html_variant_policy_spec.rb +++ b/spec/policies/html_variant_policy_spec.rb @@ -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 diff --git a/spec/policies/image_upload_policy_spec.rb b/spec/policies/image_upload_policy_spec.rb index ad1bf2ed5..c62bf0fb0 100644 --- a/spec/policies/image_upload_policy_spec.rb +++ b/spec/policies/image_upload_policy_spec.rb @@ -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]) } diff --git a/spec/policies/organization_policy_spec.rb b/spec/policies/organization_policy_spec.rb index e44282399..07fea4b80 100644 --- a/spec/policies/organization_policy_spec.rb +++ b/spec/policies/organization_policy_spec.rb @@ -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") } diff --git a/spec/policies/reaction_policy_spec.rb b/spec/policies/reaction_policy_spec.rb index 2c1d80cf8..929c096a3 100644 --- a/spec/policies/reaction_policy_spec.rb +++ b/spec/policies/reaction_policy_spec.rb @@ -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]) } diff --git a/spec/policies/stripe_active_card_policy_spec.rb b/spec/policies/stripe_active_card_policy_spec.rb index a7b73c095..215106563 100644 --- a/spec/policies/stripe_active_card_policy_spec.rb +++ b/spec/policies/stripe_active_card_policy_spec.rb @@ -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 diff --git a/spec/policies/stripe_subscription_policy_spec.rb b/spec/policies/stripe_subscription_policy_spec.rb index 457650ad0..615a4720d 100644 --- a/spec/policies/stripe_subscription_policy_spec.rb +++ b/spec/policies/stripe_subscription_policy_spec.rb @@ -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]) } diff --git a/spec/policies/tag_policy_spec.rb b/spec/policies/tag_policy_spec.rb index ed298ba7a..a820726ce 100644 --- a/spec/policies/tag_policy_spec.rb +++ b/spec/policies/tag_policy_spec.rb @@ -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]) } diff --git a/spec/policies/user_policy_spec.rb b/spec/policies/user_policy_spec.rb index 3c4490e5a..e8fa6abe0 100644 --- a/spec/policies/user_policy_spec.rb +++ b/spec/policies/user_policy_spec.rb @@ -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