2015-10-11 10:41:23 +01:00
require 'rails_helper'
2013-11-01 14:06:20 -07:00
describe UserUpdater do
2013-12-10 12:46:35 -05:00
let ( :acting_user ) { Fabricate . build ( :user ) }
2015-03-31 10:16:11 +11:00
describe '#update_muted_users' do
it 'has no cross talk' do
u1 = Fabricate ( :user )
u2 = Fabricate ( :user )
u3 = Fabricate ( :user )
updater = UserUpdater . new ( u1 , u1 )
updater . update_muted_users ( " #{ u2 . username } , #{ u3 . username } " )
updater = UserUpdater . new ( u2 , u2 )
updater . update_muted_users ( " #{ u3 . username } , #{ u1 . username } " )
updater = UserUpdater . new ( u3 , u3 )
updater . update_muted_users ( " " )
expect ( MutedUser . where ( user_id : u2 . id ) . count ) . to eq 2
expect ( MutedUser . where ( user_id : u1 . id ) . count ) . to eq 2
expect ( MutedUser . where ( user_id : u3 . id ) . count ) . to eq 0
end
end
2013-11-01 14:06:20 -07:00
describe '#update' do
it 'saves user' do
user = Fabricate ( :user , name : 'Billy Bob' )
2016-02-17 15:46:19 +11:00
updater = UserUpdater . new ( acting_user , user )
2013-11-01 14:06:20 -07:00
updater . update ( name : 'Jim Tom' )
expect ( user . reload . name ) . to eq 'Jim Tom'
end
2016-02-17 15:46:19 +11:00
it 'updates various fields' do
2014-06-10 01:19:08 -04:00
user = Fabricate ( :user )
2016-02-17 15:46:19 +11:00
updater = UserUpdater . new ( acting_user , user )
updater . update ( bio_raw : 'my new bio' ,
email_always : 'true' ,
mailing_list_mode : true ,
2016-02-18 16:57:22 +11:00
digest_after_days : " 8 " ,
new_topic_duration_minutes : 100 ,
auto_track_topics_after_msecs : 101
)
2016-02-17 15:46:19 +11:00
user . reload
expect ( user . user_profile . bio_raw ) . to eq 'my new bio'
expect ( user . user_option . email_always ) . to eq true
expect ( user . user_option . mailing_list_mode ) . to eq true
expect ( user . user_option . digest_after_days ) . to eq 8
2016-02-18 16:57:22 +11:00
expect ( user . user_option . new_topic_duration_minutes ) . to eq 100
expect ( user . user_option . auto_track_topics_after_msecs ) . to eq 101
2014-06-10 01:19:08 -04:00
end
2013-11-01 14:06:20 -07:00
context 'when update succeeds' do
it 'returns true' do
user = Fabricate ( :user )
2016-02-17 15:46:19 +11:00
updater = UserUpdater . new ( acting_user , user )
2013-11-01 14:06:20 -07:00
2014-09-25 17:44:48 +02:00
expect ( updater . update ) . to be_truthy
2013-11-01 14:06:20 -07:00
end
end
context 'when update fails' do
it 'returns false' do
user = Fabricate ( :user )
user . stubs ( save : false )
2016-02-17 15:46:19 +11:00
updater = UserUpdater . new ( acting_user , user )
2013-11-01 14:06:20 -07:00
2014-09-25 17:44:48 +02:00
expect ( updater . update ) . to be_falsey
2013-11-01 14:06:20 -07:00
end
end
context 'with permission to update title' do
it 'allows user to change title' do
user = Fabricate ( :user , title : 'Emperor' )
guardian = stub
guardian . stubs ( :can_grant_title? ) . with ( user ) . returns ( true )
2013-12-10 12:46:35 -05:00
Guardian . stubs ( :new ) . with ( acting_user ) . returns ( guardian )
2016-02-17 15:46:19 +11:00
updater = UserUpdater . new ( acting_user , user )
2013-11-01 14:06:20 -07:00
updater . update ( title : 'Minion' )
expect ( user . reload . title ) . to eq 'Minion'
end
end
context 'without permission to update title' do
it 'does not allow user to change title' do
user = Fabricate ( :user , title : 'Emperor' )
guardian = stub
guardian . stubs ( :can_grant_title? ) . with ( user ) . returns ( false )
2013-12-10 12:46:35 -05:00
Guardian . stubs ( :new ) . with ( acting_user ) . returns ( guardian )
updater = described_class . new ( acting_user , user )
2013-11-01 14:06:20 -07:00
updater . update ( title : 'Minion' )
expect ( user . reload . title ) . not_to eq 'Minion'
end
end
context 'when website includes http' do
it 'does not add http before updating' do
user = Fabricate ( :user )
2013-12-10 12:46:35 -05:00
updater = described_class . new ( acting_user , user )
2013-11-01 14:06:20 -07:00
updater . update ( website : 'http://example.com' )
2014-06-06 21:54:32 -07:00
expect ( user . reload . user_profile . website ) . to eq 'http://example.com'
2013-11-01 14:06:20 -07:00
end
end
context 'when website does not include http' do
it 'adds http before updating' do
user = Fabricate ( :user )
2013-12-10 12:46:35 -05:00
updater = described_class . new ( acting_user , user )
2013-11-01 14:06:20 -07:00
updater . update ( website : 'example.com' )
2014-06-06 21:54:32 -07:00
expect ( user . reload . user_profile . website ) . to eq 'http://example.com'
2013-11-01 14:06:20 -07:00
end
end
2014-09-17 13:09:39 -04:00
context 'when custom_fields is empty string' do
it " update is successful " do
user = Fabricate ( :user )
user . custom_fields = { 'import_username' = > 'my_old_username' }
user . save
updater = described_class . new ( acting_user , user )
updater . update ( website : 'example.com' , custom_fields : '' )
2015-04-25 11:18:35 -04:00
expect ( user . reload . custom_fields ) . to eq ( { 'import_username' = > 'my_old_username' } )
2014-09-17 13:09:39 -04:00
end
end
2013-11-01 14:06:20 -07:00
end
end