2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe AnonymousShadowCreator do
|
2015-04-07 04:02:10 -04:00
|
|
|
it "returns no shadow by default" do
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(AnonymousShadowCreator.get(Fabricate.build(:user))).to eq(nil)
|
2015-04-07 04:02:10 -04:00
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when anonymous posting is enabled" do
|
2023-12-12 22:50:13 -05:00
|
|
|
fab!(:user) { Fabricate(:user, trust_level: 3, refresh_auto_groups: true) }
|
2015-04-07 04:02:10 -04:00
|
|
|
|
2023-10-24 21:45:10 -04:00
|
|
|
before do
|
|
|
|
SiteSetting.allow_anonymous_posting = true
|
|
|
|
SiteSetting.anonymous_posting_allowed_groups = "11"
|
|
|
|
end
|
2015-04-07 22:29:43 -04:00
|
|
|
|
2023-10-24 21:45:10 -04:00
|
|
|
it "returns no shadow if the user is not in a group that is allowed to anonymously post" do
|
|
|
|
user = Fabricate(:user, trust_level: 0)
|
|
|
|
expect(AnonymousShadowCreator.get(user)).to eq(nil)
|
2015-05-15 08:20:15 -04:00
|
|
|
end
|
2015-04-07 22:29:43 -04:00
|
|
|
|
2018-02-13 01:58:38 -05:00
|
|
|
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
|
|
|
|
|
2015-05-15 08:20:15 -04:00
|
|
|
it "returns a new shadow once time expires" do
|
|
|
|
SiteSetting.anonymous_account_duration_minutes = 1
|
2015-04-07 22:29:43 -04:00
|
|
|
|
2015-05-15 08:20:15 -04:00
|
|
|
shadow = AnonymousShadowCreator.get(user)
|
2015-04-07 22:29:43 -04:00
|
|
|
|
2015-05-15 08:20:15 -04:00
|
|
|
freeze_time 2.minutes.from_now
|
|
|
|
shadow2 = AnonymousShadowCreator.get(user)
|
2015-04-07 22:29:43 -04:00
|
|
|
|
2015-05-15 08:20:15 -04:00
|
|
|
expect(shadow.id).to eq(shadow2.id)
|
2024-01-25 01:28:26 -05:00
|
|
|
shadow.send(:trigger_user_automatic_group_refresh)
|
2015-05-15 08:20:15 -04:00
|
|
|
create_post(user: shadow)
|
|
|
|
|
2019-05-29 00:26:06 -04:00
|
|
|
user.reload
|
|
|
|
shadow.reload
|
|
|
|
|
2015-05-15 08:20:15 -04:00
|
|
|
freeze_time 4.minutes.from_now
|
|
|
|
shadow3 = AnonymousShadowCreator.get(user)
|
|
|
|
|
2016-02-16 23:46:19 -05:00
|
|
|
expect(shadow3.user_option.email_digests).to eq(false)
|
2019-03-15 10:55:11 -04:00
|
|
|
expect(shadow3.user_option.email_messages_level).to eq(UserOption.email_level_types[:never])
|
2016-02-16 23:46:19 -05:00
|
|
|
|
2015-05-15 08:20:15 -04:00
|
|
|
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)
|
2015-04-07 22:29:43 -04:00
|
|
|
|
2015-05-15 08:20:15 -04:00
|
|
|
expect(shadow.trust_level).to eq(1)
|
|
|
|
expect(shadow.username).to eq("anonymous")
|
2015-04-07 04:02:10 -04:00
|
|
|
|
2020-03-26 11:32:41 -04:00
|
|
|
expect(shadow.created_at).not_to eq_time(user.created_at)
|
2015-04-07 04:02:10 -04:00
|
|
|
|
2015-05-15 08:20:15 -04:00
|
|
|
p = create_post
|
2019-05-29 00:26:06 -04:00
|
|
|
|
2015-05-15 08:20:15 -04:00
|
|
|
expect(Guardian.new(shadow).post_can_act?(p, :like)).to eq(false)
|
|
|
|
expect(Guardian.new(user).post_can_act?(p, :like)).to eq(true)
|
2015-04-07 04:02:10 -04:00
|
|
|
|
2015-05-15 08:20:15 -04:00
|
|
|
expect(user.anonymous?).to eq(false)
|
|
|
|
expect(shadow.anonymous?).to eq(true)
|
|
|
|
end
|
2015-04-07 22:29:43 -04:00
|
|
|
|
2015-05-15 08:20:15 -04:00
|
|
|
it "works even when names are required" do
|
|
|
|
SiteSetting.full_name_required = true
|
2015-04-07 22:29:43 -04:00
|
|
|
|
2015-05-15 08:20:15 -04:00
|
|
|
expect { AnonymousShadowCreator.get(user) }.to_not raise_error
|
|
|
|
end
|
2015-04-07 22:29:43 -04:00
|
|
|
|
2020-07-26 20:23:54 -04:00
|
|
|
it "works when there is an email allowlist" do
|
|
|
|
SiteSetting.allowed_email_domains = "wayne.com"
|
2018-03-06 12:14:53 -05:00
|
|
|
|
|
|
|
expect { AnonymousShadowCreator.get(user) }.to_not raise_error
|
|
|
|
end
|
2015-04-07 04:02:10 -04:00
|
|
|
|
2021-12-21 12:09:55 -05:00
|
|
|
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
|
2015-04-07 04:02:10 -04:00
|
|
|
end
|