2013-07-03 04:27:40 -04:00
require 'spec_helper'
require 'boost_trust_level'
describe BoostTrustLevel do
2013-08-13 12:04:28 -04:00
let ( :user ) { Fabricate ( :user , trust_level : TrustLevel . levels [ :newuser ] ) }
2013-07-23 17:58:26 -04:00
let ( :logger ) { StaffActionLogger . new ( Fabricate ( :admin ) ) }
2013-07-08 05:53:22 -04:00
2013-07-03 04:27:40 -04:00
it " should upgrade the trust level of a user " do
2013-07-08 05:53:22 -04:00
boostr = BoostTrustLevel . new ( user : user , level : TrustLevel . levels [ :basic ] , logger : logger )
2013-07-03 04:27:40 -04:00
boostr . save! . should be_true
user . trust_level . should == TrustLevel . levels [ :basic ]
end
2013-07-08 05:53:22 -04:00
it " should log the action " do
2013-08-13 12:04:28 -04:00
StaffActionLogger . any_instance . expects ( :log_trust_level_change ) . with ( user , TrustLevel . levels [ :newuser ] , TrustLevel . levels [ :basic ] ) . once
2013-07-08 05:53:22 -04:00
boostr = BoostTrustLevel . new ( user : user , level : TrustLevel . levels [ :basic ] , logger : logger )
boostr . save!
end
2013-07-03 04:27:40 -04: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 05:53:22 -04:00
it " should demote the user and log the action " do
2013-08-13 12:04:28 -04:00
StaffActionLogger . any_instance . expects ( :log_trust_level_change ) . with ( user , TrustLevel . levels [ :basic ] , TrustLevel . levels [ :newuser ] ) . once
2013-07-08 05:53:22 -04:00
boostr = BoostTrustLevel . new ( user : user , level : TrustLevel . levels [ :newuser ] , logger : logger )
2013-07-03 04:27:40 -04: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-03 23:28:49 -04: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 04:27:40 -04:00
user . save!
user . update_attributes ( trust_level : TrustLevel . levels [ :basic ] )
end
2013-07-08 06:51:35 -04:00
it " should not demote the user and not log the action " do
2013-08-13 12:04:28 -04:00
StaffActionLogger . any_instance . expects ( :log_trust_level_change ) . never
2013-07-08 05:53:22 -04:00
boostr = BoostTrustLevel . new ( user : user , level : TrustLevel . levels [ :newuser ] , logger : logger )
2013-07-03 04:27:40 -04: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 05:53:22 -04:00
2013-07-03 04:27:40 -04:00
end
end
end