mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
Move logic for updating a user into a service class
This commit is contained in:
@@ -853,29 +853,9 @@ describe UsersController do
|
||||
json = JSON.parse(response.body)
|
||||
expect(json['user']['id']).to eq user.id
|
||||
end
|
||||
|
||||
context 'when website includes http' do
|
||||
it 'does not add http before updating' do
|
||||
user = log_in
|
||||
|
||||
put :update, username: user.username, website: 'http://example.com'
|
||||
|
||||
expect(user.reload.website).to eq 'http://example.com'
|
||||
end
|
||||
end
|
||||
|
||||
context 'when website does not include http' do
|
||||
it 'adds http before updating' do
|
||||
user = log_in
|
||||
|
||||
put :update, username: user.username, website: 'example.com'
|
||||
|
||||
expect(user.reload.website).to eq 'http://example.com'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'without permission to update any attributes' do
|
||||
context 'without permission to update' do
|
||||
it 'does not allow the update' do
|
||||
user = Fabricate(:user, name: 'Billy Bob')
|
||||
log_in_user(user)
|
||||
@@ -889,20 +869,6 @@ describe UsersController do
|
||||
expect(user.reload.name).not_to eq 'Jim Tom'
|
||||
end
|
||||
end
|
||||
|
||||
context 'without permission to update title' do
|
||||
it 'does not allow the user to update their title' do
|
||||
user = Fabricate(:user, title: 'Emperor')
|
||||
log_in_user(user)
|
||||
guardian = Guardian.new(user)
|
||||
guardian.stubs(can_grant_title?: false).with(user)
|
||||
Guardian.stubs(new: guardian).with(user)
|
||||
|
||||
put :update, username: user.username, title: 'Minion'
|
||||
|
||||
expect(user.reload.title).not_to eq 'Minion'
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
83
spec/services/user_updater_spec.rb
Normal file
83
spec/services/user_updater_spec.rb
Normal file
@@ -0,0 +1,83 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe UserUpdater do
|
||||
describe '#update' do
|
||||
it 'saves user' do
|
||||
user = Fabricate(:user, name: 'Billy Bob')
|
||||
updater = UserUpdater.new(user)
|
||||
|
||||
updater.update(name: 'Jim Tom')
|
||||
|
||||
expect(user.reload.name).to eq 'Jim Tom'
|
||||
end
|
||||
|
||||
context 'when update succeeds' do
|
||||
it 'returns true' do
|
||||
user = Fabricate(:user)
|
||||
updater = UserUpdater.new(user)
|
||||
|
||||
expect(updater.update).to be_true
|
||||
end
|
||||
end
|
||||
|
||||
context 'when update fails' do
|
||||
it 'returns false' do
|
||||
user = Fabricate(:user)
|
||||
user.stubs(save: false)
|
||||
updater = UserUpdater.new(user)
|
||||
|
||||
expect(updater.update).to be_false
|
||||
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)
|
||||
Guardian.stubs(:new).with(user).returns(guardian)
|
||||
updater = UserUpdater.new(user)
|
||||
|
||||
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)
|
||||
Guardian.stubs(:new).with(user).returns(guardian)
|
||||
updater = UserUpdater.new(user)
|
||||
|
||||
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)
|
||||
updater = UserUpdater.new(user)
|
||||
|
||||
updater.update(website: 'http://example.com')
|
||||
|
||||
expect(user.reload.website).to eq 'http://example.com'
|
||||
end
|
||||
end
|
||||
|
||||
context 'when website does not include http' do
|
||||
it 'adds http before updating' do
|
||||
user = Fabricate(:user)
|
||||
updater = UserUpdater.new(user)
|
||||
|
||||
updater.update(website: 'example.com')
|
||||
|
||||
expect(user.reload.website).to eq 'http://example.com'
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user