0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-06 13:08:40 +08:00
discourse/spec/services/anonymous_shadow_creator_spec.rb
Martin Brennan 5ff45e59b7
DEV: Rename anonymous group to anonymous_users (#40435)
Followup 9359227645

As pointed out in https://meta.discourse.org/t/-/404037,
the group name "anonymous" is too generic and can conflict
with existing _users_ as well as existing groups. This commit
renames it to "anonymous_users" to avoid such conflicts, and
also to match "logged_in_users" a bit better.

In addition, this PR fixes some issues with the upcoming
change introduced in 9359227645...
the anonymous/logged_in_users groups were not showing in the admin
UI when the UC was enabled, and the logic for keeping everyone
saved in the DB for now was not working for site settings.
2026-06-02 09:29:46 +10:00

105 lines
3.3 KiB
Ruby
Vendored

# frozen_string_literal: true
RSpec.describe AnonymousShadowCreator do
it "returns no shadow by default" do
expect(AnonymousShadowCreator.get(Fabricate.build(:user))).to eq(nil)
end
context "when anonymous posting is enabled" do
fab!(:user) { Fabricate(:user, trust_level: TrustLevel[3]) }
before do
SiteSetting.allow_anonymous_mode = true
SiteSetting.anonymous_posting_allowed_groups = "11"
end
it "returns no shadow if the user is not in a group that is allowed to anonymously post" do
user = Fabricate(:user, trust_level: TrustLevel[0])
expect(AnonymousShadowCreator.get(user)).to eq(nil)
end
it "returns no shadow if the user is suspended" do
user.update!(suspended_at: Time.zone.now, suspended_till: 1.day.from_now)
expect(AnonymousShadowCreator.get(user)).to eq(nil)
end
it "returns no shadow if the user is inactive" do
user.update!(active: false)
expect(AnonymousShadowCreator.get(user)).to eq(nil)
end
it "returns no shadow if must_approve_users is true and user is not approved" do
SiteSetting.must_approve_users = true
expect(AnonymousShadowCreator.get(Fabricate.build(:user, approved: false))).to eq(nil)
end
it "returns a new shadow once time expires" do
SiteSetting.anonymous_account_duration_minutes = 1
shadow = AnonymousShadowCreator.get(user)
freeze_time 2.minutes.from_now
shadow2 = AnonymousShadowCreator.get(user)
expect(shadow.id).to eq(shadow2.id)
shadow.send(:trigger_user_automatic_group_refresh)
create_post(user: shadow)
user.reload
shadow.reload
freeze_time 4.minutes.from_now
shadow3 = AnonymousShadowCreator.get(user)
expect(shadow3.user_option.email_digests).to eq(false)
expect(shadow3.user_option.email_messages_level).to eq(UserOption.email_level_types[:never])
expect(shadow2.id).not_to eq(shadow3.id)
end
it "returns a shadow for a legit user" do
shadow = AnonymousShadowCreator.get(user)
shadow2 = AnonymousShadowCreator.get(user)
expect(shadow.id).to eq(shadow2.id)
expect(shadow.trust_level).to eq(1)
expect(shadow.username).to eq("anonymous")
expect(shadow.created_at).not_to eq_time(user.created_at)
p = create_post
expect(Guardian.new(shadow).post_can_act?(p, :like)).to eq(false)
expect(Guardian.new(user).post_can_act?(p, :like)).to eq(true)
expect(user.anonymous?).to eq(false)
expect(shadow.anonymous?).to eq(true)
end
it "works even when names are required" do
SiteSetting.full_name_requirement = "required_at_signup"
expect { AnonymousShadowCreator.get(user) }.to_not raise_error
end
it "works when there is an email allowlist" do
SiteSetting.allowed_email_domains = "wayne.com"
expect { AnonymousShadowCreator.get(user) }.to_not raise_error
end
it "falls back to username 'anonymous' if the translation for 'anonymous' consists entirely of disallowed characters" do
# use russian locale but do not allow russian characters:
I18n.locale = :ru
SiteSetting.unicode_usernames = true
SiteSetting.allowed_unicode_username_characters = "[äöü]"
shadow = AnonymousShadowCreator.get(user)
expect(shadow.username).to eq("anonymous")
end
end
end