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