2015-10-11 17:41:23 +08:00
require 'rails_helper'
2013-11-02 05:06:20 +08:00
describe UserUpdater do
2013-12-11 01:46:35 +08:00
let ( :acting_user ) { Fabricate . build ( :user ) }
2015-03-31 07:16:11 +08: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-02 05:06:20 +08:00
describe '#update' do
it 'saves user' do
user = Fabricate ( :user , name : 'Billy Bob' )
2016-02-17 12:46:19 +08:00
updater = UserUpdater . new ( acting_user , user )
2013-11-02 05:06:20 +08:00
updater . update ( name : 'Jim Tom' )
expect ( user . reload . name ) . to eq 'Jim Tom'
end
2016-07-08 10:58:18 +08:00
it 'can update categories and tags' do
category = Fabricate ( :category )
tag = Fabricate ( :tag )
user = Fabricate ( :user )
updater = UserUpdater . new ( acting_user , user )
updater . update ( watched_tags : [ tag . name ] , muted_category_ids : [ category . id ] )
expect ( TagUser . where (
user_id : user . id ,
tag_id : tag . id ,
notification_level : TagUser . notification_levels [ :watching ]
) . count ) . to eq ( 1 )
expect ( CategoryUser . where (
user_id : user . id ,
category_id : category . id ,
notification_level : CategoryUser . notification_levels [ :muted ]
) . count ) . to eq ( 1 )
end
2016-02-17 12:46:19 +08:00
it 'updates various fields' do
2014-06-10 13:19:08 +08:00
user = Fabricate ( :user )
2016-02-17 12:46:19 +08:00
updater = UserUpdater . new ( acting_user , user )
2016-11-15 16:10:20 +08:00
date_of_birth = Time . zone . now
2016-02-17 12:46:19 +08:00
2017-05-16 00:48:08 +08:00
theme = Theme . create! ( user_id : - 1 , name : " test " , user_selectable : true )
seq = user . user_option . theme_key_seq
2016-08-01 13:29:28 +08:00
val = updater . update ( bio_raw : 'my new bio' ,
2016-02-17 12:46:19 +08:00
email_always : 'true' ,
mailing_list_mode : true ,
2016-03-03 04:26:27 +08:00
digest_after_minutes : " 45 " ,
2016-02-18 13:57:22 +08:00
new_topic_duration_minutes : 100 ,
2016-02-25 21:05:40 +08:00
auto_track_topics_after_msecs : 101 ,
2016-10-01 00:36:43 +08:00
notification_level_when_replying : 3 ,
2016-11-15 16:10:20 +08:00
email_in_reply_to : false ,
2017-05-16 00:48:08 +08:00
date_of_birth : date_of_birth ,
theme_key : theme . key
2016-02-18 13:57:22 +08:00
)
2016-08-01 13:29:28 +08:00
expect ( val ) . to be_truthy
2016-02-17 12:46:19 +08: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
2016-03-03 04:26:27 +08:00
expect ( user . user_option . digest_after_minutes ) . to eq 45
2016-02-18 13:57:22 +08:00
expect ( user . user_option . new_topic_duration_minutes ) . to eq 100
expect ( user . user_option . auto_track_topics_after_msecs ) . to eq 101
2016-10-01 00:36:43 +08:00
expect ( user . user_option . notification_level_when_replying ) . to eq 3
2016-02-25 21:05:40 +08:00
expect ( user . user_option . email_in_reply_to ) . to eq false
2017-05-16 00:48:08 +08:00
expect ( user . user_option . theme_key ) . to eq theme . key
expect ( user . user_option . theme_key_seq ) . to eq ( seq + 1 )
2016-11-15 16:10:20 +08:00
expect ( user . date_of_birth ) . to eq ( date_of_birth . to_date )
2014-06-10 13:19:08 +08:00
end
2016-11-28 22:52:35 +08:00
it " disables email_digests when enabling mailing_list_mode " do
user = Fabricate ( :user )
updater = UserUpdater . new ( acting_user , user )
val = updater . update ( mailing_list_mode : true , email_digests : true )
expect ( val ) . to be_truthy
user . reload
expect ( user . user_option . email_digests ) . to eq false
expect ( user . user_option . mailing_list_mode ) . to eq true
end
2016-08-01 13:29:28 +08:00
context 'when sso overrides bio' do
it 'does not change bio' do
SiteSetting . enable_sso = true
SiteSetting . sso_overrides_bio = true
2013-11-02 05:06:20 +08:00
user = Fabricate ( :user )
2016-02-17 12:46:19 +08:00
updater = UserUpdater . new ( acting_user , user )
2013-11-02 05:06:20 +08:00
2016-08-01 13:29:28 +08:00
expect ( updater . update ( bio_raw : " new bio " ) ) . to be_truthy
user . reload
expect ( user . user_profile . bio_raw ) . not_to eq 'new bio'
2013-11-02 05:06:20 +08:00
end
end
context 'when update fails' do
it 'returns false' do
user = Fabricate ( :user )
user . stubs ( save : false )
2016-02-17 12:46:19 +08:00
updater = UserUpdater . new ( acting_user , user )
2013-11-02 05:06:20 +08:00
2014-09-25 23:44:48 +08:00
expect ( updater . update ) . to be_falsey
2013-11-02 05:06:20 +08: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-11 01:46:35 +08:00
Guardian . stubs ( :new ) . with ( acting_user ) . returns ( guardian )
2016-02-17 12:46:19 +08:00
updater = UserUpdater . new ( acting_user , user )
2013-11-02 05:06:20 +08: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-11 01:46:35 +08:00
Guardian . stubs ( :new ) . with ( acting_user ) . returns ( guardian )
2017-02-28 04:02:00 +08:00
updater = UserUpdater . new ( acting_user , user )
2013-11-02 05:06:20 +08: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 )
2017-02-28 04:02:00 +08:00
updater = UserUpdater . new ( acting_user , user )
2013-11-02 05:06:20 +08:00
updater . update ( website : 'http://example.com' )
2014-06-07 12:54:32 +08:00
expect ( user . reload . user_profile . website ) . to eq 'http://example.com'
2013-11-02 05:06:20 +08:00
end
end
context 'when website does not include http' do
it 'adds http before updating' do
user = Fabricate ( :user )
2017-02-28 04:02:00 +08:00
updater = UserUpdater . new ( acting_user , user )
2013-11-02 05:06:20 +08:00
updater . update ( website : 'example.com' )
2014-06-07 12:54:32 +08:00
expect ( user . reload . user_profile . website ) . to eq 'http://example.com'
2013-11-02 05:06:20 +08:00
end
end
2014-09-18 01:09:39 +08: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
2017-02-28 04:02:00 +08:00
updater = UserUpdater . new ( acting_user , user )
2014-09-18 01:09:39 +08:00
updater . update ( website : 'example.com' , custom_fields : '' )
2015-04-25 23:18:35 +08:00
expect ( user . reload . custom_fields ) . to eq ( { 'import_username' = > 'my_old_username' } )
2014-09-18 01:09:39 +08:00
end
end
2017-02-23 13:48:57 +08:00
it " logs the action " do
2017-03-01 16:12:17 +08:00
user_without_name = Fabricate ( :user , name : nil )
2017-02-23 13:48:57 +08:00
user = Fabricate ( :user , name : 'Billy Bob' )
2017-02-28 04:02:00 +08:00
expect { UserUpdater . new ( acting_user , user ) . update ( name : 'Jim Tom' ) } . to change { UserHistory . count } . by ( 1 )
expect { UserUpdater . new ( acting_user , user ) . update ( name : 'Jim Tom' ) } . to change { UserHistory . count } . by ( 0 ) # make sure it does not log a dupe
2017-03-01 16:12:17 +08:00
expect { UserUpdater . new ( acting_user , user_without_name ) . update ( bio_raw : 'foo bar' ) } . to change { UserHistory . count } . by ( 0 ) # make sure user without name (name = nil) does not raise an error
expect { UserUpdater . new ( acting_user , user_without_name ) . update ( name : 'Jim Tom' ) } . to change { UserHistory . count } . by ( 1 )
2017-02-23 13:48:57 +08:00
end
2013-11-02 05:06:20 +08:00
end
end