2013-07-03 16:27:40 +08:00
require 'spec_helper'
require 'boost_trust_level'
describe BoostTrustLevel do
2013-08-14 00:04:28 +08:00
let ( :user ) { Fabricate ( :user , trust_level : TrustLevel . levels [ :newuser ] ) }
2013-07-24 05:58:26 +08:00
let ( :logger ) { StaffActionLogger . new ( Fabricate ( :admin ) ) }
2013-07-08 17:53:22 +08:00
2013-07-03 16:27:40 +08:00
it " should upgrade the trust level of a user " do
2013-07-08 17:53:22 +08:00
boostr = BoostTrustLevel . new ( user : user , level : TrustLevel . levels [ :basic ] , logger : logger )
2013-07-03 16:27:40 +08:00
boostr . save! . should be_true
user . trust_level . should == TrustLevel . levels [ :basic ]
end
2013-07-08 17:53:22 +08:00
it " should log the action " do
2013-08-14 00:04:28 +08:00
StaffActionLogger . any_instance . expects ( :log_trust_level_change ) . with ( user , TrustLevel . levels [ :newuser ] , TrustLevel . levels [ :basic ] ) . once
2013-07-08 17:53:22 +08:00
boostr = BoostTrustLevel . new ( user : user , level : TrustLevel . levels [ :basic ] , logger : logger )
boostr . save!
end
2013-07-03 16:27:40 +08:00
describe " demotions " do
context " for a user that has not done the requisite things to attain their trust level " do
before do
# scenario: admin mistakenly promotes user's trust level
user . update_attributes ( trust_level : TrustLevel . levels [ :basic ] )
end
2013-07-08 17:53:22 +08:00
it " should demote the user and log the action " do
2013-08-14 00:04:28 +08:00
StaffActionLogger . any_instance . expects ( :log_trust_level_change ) . with ( user , TrustLevel . levels [ :basic ] , TrustLevel . levels [ :newuser ] ) . once
2013-07-08 17:53:22 +08:00
boostr = BoostTrustLevel . new ( user : user , level : TrustLevel . levels [ :newuser ] , logger : logger )
2013-07-03 16:27:40 +08:00
boostr . save! . should be_true
user . trust_level . should == TrustLevel . levels [ :newuser ]
end
end
context " for a user that has done the requisite things to attain their trust level " do
before do
2013-10-04 11:28:49 +08:00
stat = user . user_stat
stat . topics_entered = SiteSetting . basic_requires_topics_entered + 1
stat . posts_read_count = SiteSetting . basic_requires_read_posts + 1
stat . time_read = SiteSetting . basic_requires_time_spent_mins * 60
2013-07-03 16:27:40 +08:00
user . save!
user . update_attributes ( trust_level : TrustLevel . levels [ :basic ] )
end
2013-07-08 18:51:35 +08:00
it " should not demote the user and not log the action " do
2013-08-14 00:04:28 +08:00
StaffActionLogger . any_instance . expects ( :log_trust_level_change ) . never
2013-07-08 17:53:22 +08:00
boostr = BoostTrustLevel . new ( user : user , level : TrustLevel . levels [ :newuser ] , logger : logger )
2013-07-03 16:27:40 +08:00
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 ]
end
2013-07-08 17:53:22 +08:00
2013-07-03 16:27:40 +08:00
end
end
end