2017-09-10 17:25:29 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Users::UpdateService do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
describe '#execute' do
|
|
|
|
it 'updates the name' do
|
|
|
|
result = update_user(user, name: 'New Name')
|
|
|
|
|
|
|
|
expect(result).to eq(status: :success)
|
|
|
|
expect(user.name).to eq('New Name')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an error result when record cannot be updated' do
|
|
|
|
result = {}
|
|
|
|
expect do
|
|
|
|
result = update_user(user, { email: 'invalid' })
|
|
|
|
end.not_to change { user.reload.email }
|
|
|
|
expect(result[:status]).to eq(:error)
|
|
|
|
expect(result[:message]).to eq('Email is invalid')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes namespace error messages' do
|
2018-03-17 18:26:18 +05:30
|
|
|
create(:group, path: 'taken')
|
2017-09-10 17:25:29 +05:30
|
|
|
result = {}
|
|
|
|
expect do
|
|
|
|
result = update_user(user, { username: 'taken' })
|
|
|
|
end.not_to change { user.reload.username }
|
|
|
|
expect(result[:status]).to eq(:error)
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(result[:message]).to eq('Username has already been taken')
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
it 'updates the status if status params were given' do
|
|
|
|
update_user(user, status: { message: "On a call" })
|
|
|
|
|
|
|
|
expect(user.status.message).to eq("On a call")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not delete the status if no status param was passed' do
|
|
|
|
create(:user_status, user: user, message: 'Busy!')
|
|
|
|
|
|
|
|
update_user(user, name: 'New name')
|
|
|
|
|
|
|
|
expect(user.status.message).to eq('Busy!')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes status error messages' do
|
|
|
|
result = update_user(user, status: { emoji: "Moo!" })
|
|
|
|
|
|
|
|
expect(result[:status]).to eq(:error)
|
|
|
|
expect(result[:message]).to eq("Emoji is not included in the list")
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
def update_user(user, opts)
|
2018-03-17 18:26:18 +05:30
|
|
|
described_class.new(user, opts.merge(user: user)).execute
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#execute!' do
|
|
|
|
it 'updates the name' do
|
2018-03-17 18:26:18 +05:30
|
|
|
service = described_class.new(user, user: user, name: 'New Name')
|
2017-09-10 17:25:29 +05:30
|
|
|
expect(service).not_to receive(:notify_new_user)
|
|
|
|
|
|
|
|
result = service.execute!
|
|
|
|
|
|
|
|
expect(result).to be true
|
|
|
|
expect(user.name).to eq('New Name')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an error when record cannot be updated' do
|
|
|
|
expect do
|
|
|
|
update_user(user, email: 'invalid')
|
|
|
|
end.to raise_error(ActiveRecord::RecordInvalid)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'fires system hooks when a new user is saved' do
|
|
|
|
system_hook_service = spy(:system_hook_service)
|
|
|
|
user = build(:user)
|
2018-03-17 18:26:18 +05:30
|
|
|
service = described_class.new(user, user: user, name: 'John Doe')
|
2017-09-10 17:25:29 +05:30
|
|
|
expect(service).to receive(:notify_new_user).and_call_original
|
|
|
|
expect(service).to receive(:system_hook_service).and_return(system_hook_service)
|
|
|
|
|
|
|
|
service.execute
|
|
|
|
|
|
|
|
expect(system_hook_service).to have_received(:execute_hooks_for).with(user, :create)
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_user(user, opts)
|
2018-03-17 18:26:18 +05:30
|
|
|
described_class.new(user, opts.merge(user: user)).execute!
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|