From d4974b70938b07a60d8e6edbd258940a3aeb2c01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Fri, 15 May 2015 14:20:15 +0200 Subject: [PATCH] FIX: anonymous mode don't work when names are required --- app/services/anonymous_shadow_creator.rb | 29 +++----- .../services/anonymous_shadow_creator_spec.rb | 70 +++++++++++-------- 2 files changed, 50 insertions(+), 49 deletions(-) diff --git a/app/services/anonymous_shadow_creator.rb b/app/services/anonymous_shadow_creator.rb index f2abd9e1b1a..18b7ecceaf3 100644 --- a/app/services/anonymous_shadow_creator.rb +++ b/app/services/anonymous_shadow_creator.rb @@ -2,18 +2,17 @@ class AnonymousShadowCreator def self.get_master(user) return unless user - return if !SiteSetting.allow_anonymous_posting + return unless SiteSetting.allow_anonymous_posting if (master_id = user.custom_fields["master_id"].to_i) > 0 User.find_by(id: master_id) end - end def self.get(user) return unless user - return if !SiteSetting.allow_anonymous_posting || - user.trust_level < SiteSetting.anonymous_posting_min_trust_level + return unless SiteSetting.allow_anonymous_posting + return if user.trust_level < SiteSetting.anonymous_posting_min_trust_level if (shadow_id = user.custom_fields["shadow_id"].to_i) > 0 shadow = User.find_by(id: shadow_id) @@ -30,12 +29,14 @@ class AnonymousShadowCreator end def self.create_shadow(user) + username = UserNameSuggester.suggest(I18n.t(:anonymous).downcase) + User.transaction do shadow = User.create!( password: SecureRandom.hex, email: "#{SecureRandom.hex}@anon.#{Discourse.current_hostname}", - name: "", - username: UserNameSuggester.suggest(I18n.t(:anonymous).downcase), + name: username, # prevents error when names are required + username: username, active: true, trust_level: 1, trust_level_locked: true, @@ -44,27 +45,19 @@ class AnonymousShadowCreator created_at: 1.day.ago # bypass new user restrictions ) - shadow.email_tokens.update_all confirmed: true + shadow.email_tokens.update_all(confirmed: true) shadow.activate - # can not hold dupes - UserCustomField.where(user_id: user.id, - name: "shadow_id").destroy_all + UserCustomField.where(user_id: user.id, name: "shadow_id").destroy_all - UserCustomField.create!(user_id: user.id, - name: "shadow_id", - value: shadow.id) - - UserCustomField.create!(user_id: shadow.id, - name: "master_id", - value: user.id) + UserCustomField.create!(user_id: user.id, name: "shadow_id", value: shadow.id) + UserCustomField.create!(user_id: shadow.id, name: "master_id", value: user.id) shadow.reload user.reload shadow - end end end diff --git a/spec/services/anonymous_shadow_creator_spec.rb b/spec/services/anonymous_shadow_creator_spec.rb index 13b990ac8fd..85602260988 100644 --- a/spec/services/anonymous_shadow_creator_spec.rb +++ b/spec/services/anonymous_shadow_creator_spec.rb @@ -6,52 +6,60 @@ describe AnonymousShadowCreator do expect(AnonymousShadowCreator.get(Fabricate.build(:user))).to eq(nil) end - it "returns no shadow if trust level is not met" do - SiteSetting.allow_anonymous_posting = true - expect(AnonymousShadowCreator.get(Fabricate.build(:user, trust_level: 0))).to eq(nil) - end + context "Anonymous posting is enabled" do - it "returns a new shadow once time expires" do - SiteSetting.allow_anonymous_posting = true - SiteSetting.anonymous_account_duration_minutes = 1 + before { SiteSetting.allow_anonymous_posting = true } - user = Fabricate(:user, trust_level: 3) - shadow = AnonymousShadowCreator.get(user) + let(:user) { Fabricate(:user, trust_level: 3) } - freeze_time 2.minutes.from_now - shadow2 = AnonymousShadowCreator.get(user) + it "returns no shadow if trust level is not met" do + expect(AnonymousShadowCreator.get(Fabricate.build(:user, trust_level: 0))).to eq(nil) + end - expect(shadow.id).to eq(shadow2.id) - create_post(user: shadow) + it "returns a new shadow once time expires" do + SiteSetting.anonymous_account_duration_minutes = 1 - freeze_time 4.minutes.from_now - shadow3 = AnonymousShadowCreator.get(user) + shadow = AnonymousShadowCreator.get(user) - expect(shadow2.id).not_to eq(shadow3.id) + freeze_time 2.minutes.from_now + shadow2 = AnonymousShadowCreator.get(user) - end + expect(shadow.id).to eq(shadow2.id) + create_post(user: shadow) - it "returns a shadow for a legit user" do - SiteSetting.allow_anonymous_posting = true - user = Fabricate(:user, trust_level: 3) + freeze_time 4.minutes.from_now + shadow3 = AnonymousShadowCreator.get(user) - shadow = AnonymousShadowCreator.get(user) - shadow2 = AnonymousShadowCreator.get(user) + expect(shadow2.id).not_to eq(shadow3.id) - expect(shadow.id).to eq(shadow2.id) + end - expect(shadow.trust_level).to eq(1) - expect(shadow.username).to eq("anonymous") + it "returns a shadow for a legit user" do + shadow = AnonymousShadowCreator.get(user) + shadow2 = AnonymousShadowCreator.get(user) - expect(shadow.created_at).not_to eq(user.created_at) + 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(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) + 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_required = true + + expect { AnonymousShadowCreator.get(user) }.to_not raise_error + end - expect(user.anonymous?).to eq(false) - expect(shadow.anonymous?).to eq(true) end end