If the change doesn't go through, don't log anything

This commit is contained in:
Navin 2013-07-08 12:51:35 +02:00
parent d77ce23de2
commit 45d85f4054
3 changed files with 9 additions and 8 deletions

View File

@ -11,12 +11,13 @@ class BoostTrustLevel
end end
def save! def save!
success = if @level < @user.trust_level
demote!
else
@user.update_attributes!(trust_level: @level)
end
@logger.log_trust_level_change(@user, @level) @logger.log_trust_level_change(@user, @level)
if @level < @user.trust_level success
demote!
else
@user.update_attributes!(trust_level: @level)
end
end end
protected protected

View File

@ -48,8 +48,8 @@ describe BoostTrustLevel do
user.update_attributes(trust_level: TrustLevel.levels[:basic]) user.update_attributes(trust_level: TrustLevel.levels[:basic])
end end
it "should not demote the user but log the action anyway" do it "should not demote the user and not log the action" do
AdminLogger.any_instance.expects(:log_trust_level_change).with(user, TrustLevel.levels[:newuser]).once AdminLogger.any_instance.expects(:log_trust_level_change).with(user, TrustLevel.levels[:newuser]).never
boostr = BoostTrustLevel.new(user: user, level: TrustLevel.levels[:newuser], logger: logger) boostr = BoostTrustLevel.new(user: user, level: TrustLevel.levels[:newuser], logger: logger)
expect { boostr.save! }.to raise_error(Discourse::InvalidAccess, "You attempted to demote #{user.name} to 'newuser'. However their trust level is already 'basic'. #{user.name} will remain at 'basic'") expect { boostr.save! }.to raise_error(Discourse::InvalidAccess, "You attempted to demote #{user.name} to 'newuser'. However their trust level is already 'basic'. #{user.name} will remain at 'basic'")
user.trust_level.should == TrustLevel.levels[:basic] user.trust_level.should == TrustLevel.levels[:basic]

View File

@ -144,7 +144,7 @@ describe Admin::UsersController do
end end
it "raises an error when demoting a user below their current trust level" do it "raises an error when demoting a user below their current trust level" do
AdminLogger.any_instance.expects(:log_trust_level_change).with(@another_user, TrustLevel.levels[:newuser]).once AdminLogger.any_instance.expects(:log_trust_level_change).with(@another_user, TrustLevel.levels[:newuser]).never
@another_user.topics_entered = SiteSetting.basic_requires_topics_entered + 1 @another_user.topics_entered = SiteSetting.basic_requires_topics_entered + 1
@another_user.posts_read_count = SiteSetting.basic_requires_read_posts + 1 @another_user.posts_read_count = SiteSetting.basic_requires_read_posts + 1
@another_user.time_read = SiteSetting.basic_requires_time_spent_mins * 60 @another_user.time_read = SiteSetting.basic_requires_time_spent_mins * 60