2013-11-02 05:06:20 +08:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe UserUpdater do
|
2013-12-11 01:46:35 +08:00
|
|
|
|
|
|
|
let(:acting_user) { Fabricate.build(:user) }
|
|
|
|
|
2013-11-02 05:06:20 +08:00
|
|
|
describe '#update' do
|
|
|
|
it 'saves user' do
|
|
|
|
user = Fabricate(:user, name: 'Billy Bob')
|
2013-12-11 01:46:35 +08:00
|
|
|
updater = described_class.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
|
|
|
|
|
2014-06-10 13:19:08 +08:00
|
|
|
it 'updates bio' do
|
|
|
|
user = Fabricate(:user)
|
|
|
|
updater = described_class.new(acting_user, user)
|
|
|
|
|
|
|
|
updater.update(bio_raw: 'my new bio')
|
|
|
|
|
|
|
|
expect(user.reload.user_profile.bio_raw).to eq 'my new bio'
|
|
|
|
end
|
|
|
|
|
2013-11-02 05:06:20 +08:00
|
|
|
context 'when update succeeds' do
|
|
|
|
it 'returns true' do
|
|
|
|
user = Fabricate(:user)
|
2013-12-11 01:46:35 +08:00
|
|
|
updater = described_class.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_truthy
|
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)
|
2013-12-11 01:46:35 +08:00
|
|
|
updater = described_class.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)
|
|
|
|
updater = described_class.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)
|
|
|
|
updater = described_class.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)
|
2013-12-11 01:46:35 +08:00
|
|
|
updater = described_class.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)
|
2013-12-11 01:46:35 +08:00
|
|
|
updater = described_class.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
|
|
|
|
updater = described_class.new(acting_user, user)
|
|
|
|
|
|
|
|
updater.update(website: 'example.com', custom_fields: '')
|
|
|
|
user.reload.custom_fields.should == {'import_username' => 'my_old_username'}
|
|
|
|
end
|
|
|
|
end
|
2013-11-02 05:06:20 +08:00
|
|
|
end
|
|
|
|
end
|