Skip to content

A platform for community discussion. Free, open, simple.

DEV: Move tag related guardian tests to tag_guardian_spec (#33817)

Ted Johansson7a5a37ff8e9b
diff --git a/lib/guardian/tag_guardian.rb b/lib/guardian/tag_guardian.rb
index 025a7e0c013..21bf4a62af8 100644
--- a/lib/guardian/tag_guardian.rb
+++ b/lib/guardian/tag_guardian.rb
@@ -16,7 +16,7 @@ module TagGuardian
def can_tag_pms?
return false if !SiteSetting.tagging_enabled
- return false if @user.blank?
+ return false if !authenticated?
return true if @user == Discourse.system_user
group_ids = SiteSetting.pm_tags_allowed_for_groups_map
diff --git a/spec/lib/guardian/tag_guardian_spec.rb b/spec/lib/guardian/tag_guardian_spec.rb
new file mode 100644
index 00000000000..cdcd6645986
--- /dev/null
+++ b/spec/lib/guardian/tag_guardian_spec.rb
@@ -0,0 +1,118 @@
+# frozen_string_literal: true
+
+RSpec.describe TagGuardian do
+ fab!(:user)
+ fab!(:admin)
+ fab!(:trust_level_0)
+ fab!(:trust_level_1)
+ fab!(:trust_level_2)
+ fab!(:trust_level_3)
+
+ ###### VISIBILITY ######
+
+ describe "#can_see_tag?" do
+ it "returns true for everyone" do
+ expect(Guardian.new(nil).can_see_tag?(anything)).to be_truthy
+ end
+ end
+
+ ###### CREATION ######
+
+ describe "#can_create_tag?" do
+ it "returns false when tagging is disabled" do
+ SiteSetting.tagging_enabled = false
+
+ expect(Guardian.new(admin).can_create_tag?).to be_falsey
+ end
+
+ it "returns false when user is not in an allowed group" do
+ expect(Guardian.new(trust_level_2).can_create_tag?).to be_falsey
+ end
+
+ it "returns true when user is in an allowed group" do
+ expect(Guardian.new(trust_level_3).can_create_tag?).to be_truthy
+ end
+ end
+
+ ###### TAGGING ######
+
+ describe "#can_tag_topics?" do
+ it "returns false when tagging is disabled" do
+ SiteSetting.tagging_enabled = false
+
+ expect(Guardian.new(admin).can_create_tag?).to be_falsey
+ end
+
+ it "returns false when user is not in an allowed group" do
+ SiteSetting.tag_topic_allowed_groups = "1|2|11"
+
+ expect(Guardian.new(trust_level_0).can_tag_topics?).to be_falsey
+ end
+
+ it "returns true when user is in an allowed group" do
+ expect(Guardian.new(trust_level_1).can_tag_topics?).to be_truthy
+ end
+ end
+
+ describe "#can_tag_pms?" do
+ it "returns false when tagging is disabled" do
+ SiteSetting.tagging_enabled = false
+
+ expect(Guardian.new(admin).can_tag_pms?).to be_falsey
+ end
+
+ it "returns true when the actor is the system user" do
+ expect(Guardian.new(Discourse.system_user).can_tag_pms?).to be_truthy
+ end
+
+ it "returns false for a guest user" do
+ expect(Guardian.new(nil).can_tag_pms?).to be_falsey
+ end
+
+ it "returns false when user is not in an allowed group" do
+ SiteSetting.pm_tags_allowed_for_groups = "1|2|11"
+
+ expect(Guardian.new(trust_level_0).can_tag_pms?).to be_falsey
+ end
+
+ it "returns true when user is in an allowed group" do
+ SiteSetting.pm_tags_allowed_for_groups = "1|2|11"
+
+ expect(Guardian.new(trust_level_1).can_tag_pms?).to be_truthy
+ end
+ end
+
+ ###### ADMIN ######
+
+ describe "#can_admin_tags?" do
+ it "returns false when tagging is disabled" do
+ SiteSetting.tagging_enabled = false
+
+ expect(Guardian.new(admin).can_admin_tags?).to be_falsey
+ end
+
+ it "returns false for a regular user" do
+ expect(Guardian.new(user).can_admin_tags?).to be_falsey
+ end
+
+ it "returns true for a staff user" do
+ expect(Guardian.new(admin).can_admin_tags?).to be_truthy
+ end
+ end
+
+ describe "#can_admin_tag_groups?" do
+ it "returns false when tagging is disabled" do
+ SiteSetting.tagging_enabled = false
+
+ expect(Guardian.new(admin).can_admin_tag_groups?).to be_falsey
+ end
+
+ it "returns false for a regular user" do
+ expect(Guardian.new(user).can_admin_tag_groups?).to be_falsey
+ end
+
+ it "returns true for a staff user" do
+ expect(Guardian.new(admin).can_admin_tag_groups?).to be_truthy
+ end
+ end
+end
diff --git a/spec/lib/guardian_spec.rb b/spec/lib/guardian_spec.rb
index 0fc051b88c3..deb36d945fd 100644
--- a/spec/lib/guardian_spec.rb
+++ b/spec/lib/guardian_spec.rb
@@ -2698,126 +2698,6 @@ RSpec.describe Guardian do
end
end
- describe "Tags" do
- context "with tagging disabled" do
- before { SiteSetting.tagging_enabled = false }
-
- it "can_create_tag returns false" do
- expect(Guardian.new(admin).can_create_tag?).to be_falsey
- end
-
- it "can_admin_tags returns false" do
- expect(Guardian.new(admin).can_admin_tags?).to be_falsey
- end
-
- it "can_admin_tag_groups returns false" do
- expect(Guardian.new(admin).can_admin_tag_groups?).to be_falsey
- end
- end
-
- context "when tagging is enabled" do
- before do
- SiteSetting.tagging_enabled = true
- SiteSetting.tag_topic_allowed_groups = Group::AUTO_GROUPS[:trust_level_1]
- end
-
- context "when minimum trust level to create tags is 3" do
- before do
- SiteSetting.create_tag_allowed_groups = "1|3|#{Group::AUTO_GROUPS[:trust_level_3]}"
- end
-
- describe "#can_see_tag?" do
- it "is always true" do
- expect(Guardian.new.can_see_tag?(anything)).to be_truthy
- end
- end
-
- describe "can_create_tag" do
- it "returns false if trust level is too low" do
- expect(Guardian.new(trust_level_2).can_create_tag?).to be_falsey
- end
-
- it "returns true if trust level is high enough" do
- expect(Guardian.new(trust_level_3).can_create_tag?).to be_truthy
- end
-
- it "returns true for staff" do
- expect(Guardian.new(admin).can_create_tag?).to be_truthy
- expect(Guardian.new(moderator).can_create_tag?).to be_truthy
- end
- end
-
- describe "can_tag_topics" do
- it "returns false if trust level is too low" do
- expect(
- Guardian.new(
- Fabricate(:user, trust_level: 0, refresh_auto_groups: true),
- ).can_tag_topics?,
- ).to be_falsey
- end
-
- it "returns true if trust level is high enough" do
- expect(
- Guardian.new(
- Fabricate(:user, trust_level: 1, refresh_auto_groups: true),
- ).can_tag_topics?,
- ).to be_truthy
- end
-
- it "returns true for staff" do
- expect(Guardian.new(admin).can_tag_topics?).to be_truthy
- expect(Guardian.new(moderator).can_tag_topics?).to be_truthy
- end
- end
- end
-
- context "when staff and admin groups are allowed to create tags" do
- before do
- SiteSetting.min_trust_to_create_tag = "staff"
- SiteSetting.create_tag_allowed_groups =
- "#{Group::AUTO_GROUPS[:staff]}|#{Group::AUTO_GROUPS[:admins]}"
- end
-
- it "returns false if not staff" do
- expect(Guardian.new(trust_level_4).can_create_tag?).to eq(false)
- end
-
- it "returns true if staff" do
- expect(Guardian.new(admin).can_create_tag?).to be_truthy
- expect(Guardian.new(moderator).can_create_tag?).to be_truthy
- end
- end
-
- context "when only admin group is allowed to create tags" do
- before do
- SiteSetting.min_trust_to_create_tag = "admin"
- SiteSetting.create_tag_allowed_groups = Group::AUTO_GROUPS[:admins]
- end
-
- it "returns true if admin or moderator" do
- expect(Guardian.new(admin).can_create_tag?).to be_truthy
- expect(Guardian.new(moderator).can_create_tag?).to be_truthy
- end
- end
- end
-
- context "when tagging PMs" do
- it "pm_tags_allowed_for_groups contains everyone" do
- SiteSetting.pm_tags_allowed_for_groups = "#{Group::AUTO_GROUPS[:everyone]}"
-
- expect(Guardian.new(user).can_tag_pms?).to be_truthy
- end
-
- it "pm_tags_allowed_for_groups contains a group" do
- SiteSetting.pm_tags_allowed_for_groups = "#{group.id}"
- group.add(member)
-
- expect(Guardian.new(user).can_tag_pms?).to be_falsey
- expect(Guardian.new(member).can_tag_pms?).to be_truthy
- end
- end
- end
-
describe "#can_see_group?" do
it "Correctly handles owner visible groups" do
group = Group.new(name: "group", visibility_level: Group.visibility_levels[:owners])