2017-06-13 09:32:21 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-07-10 14:24:02 +00:00
|
|
|
describe Users::UpdateService do
|
2017-06-13 09:32:21 +00:00
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
describe '#execute' do
|
|
|
|
it 'updates the name' do
|
2017-06-23 09:34:07 +00:00
|
|
|
result = update_user(user, name: 'New Name')
|
2017-06-13 14:24:36 +00:00
|
|
|
|
2017-06-23 15:11:31 +00:00
|
|
|
expect(result).to eq(status: :success)
|
2017-06-13 09:32:21 +00:00
|
|
|
expect(user.name).to eq('New Name')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an error result when record cannot be updated' do
|
2017-08-18 17:31:59 +00:00
|
|
|
result = {}
|
2017-06-13 11:52:20 +00:00
|
|
|
expect do
|
2017-08-18 17:31:59 +00:00
|
|
|
result = update_user(user, { email: 'invalid' })
|
2017-06-23 09:34:07 +00:00
|
|
|
end.not_to change { user.reload.email }
|
2017-08-18 17:31:59 +00:00
|
|
|
expect(result[:status]).to eq(:error)
|
|
|
|
expect(result[:message]).to eq('Email is invalid')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes namespace error messages' do
|
|
|
|
create(:group, name: 'taken', path: 'something_else')
|
|
|
|
result = {}
|
|
|
|
expect do
|
|
|
|
result = update_user(user, { username: 'taken' })
|
|
|
|
end.not_to change { user.reload.username }
|
|
|
|
expect(result[:status]).to eq(:error)
|
|
|
|
expect(result[:message]).to eq('Namespace name has already been taken')
|
2017-06-13 09:32:21 +00:00
|
|
|
end
|
|
|
|
|
2017-06-23 09:34:07 +00:00
|
|
|
def update_user(user, opts)
|
|
|
|
described_class.new(user, opts).execute
|
2017-06-13 09:32:21 +00:00
|
|
|
end
|
|
|
|
end
|
2017-06-13 14:24:36 +00:00
|
|
|
|
|
|
|
describe '#execute!' do
|
|
|
|
it 'updates the name' do
|
2017-06-23 09:34:07 +00:00
|
|
|
result = update_user(user, name: 'New Name')
|
2017-06-13 14:24:36 +00:00
|
|
|
|
|
|
|
expect(result).to be true
|
|
|
|
expect(user.name).to eq('New Name')
|
|
|
|
end
|
|
|
|
|
2017-06-23 15:11:31 +00:00
|
|
|
it 'raises an error when record cannot be updated' do
|
2017-06-13 14:24:36 +00:00
|
|
|
expect do
|
2017-06-23 15:11:31 +00:00
|
|
|
update_user(user, email: 'invalid')
|
2017-06-23 09:34:07 +00:00
|
|
|
end.to raise_error(ActiveRecord::RecordInvalid)
|
2017-06-13 14:24:36 +00:00
|
|
|
end
|
|
|
|
|
2017-06-23 09:34:07 +00:00
|
|
|
def update_user(user, opts)
|
|
|
|
described_class.new(user, opts).execute!
|
2017-06-13 14:24:36 +00:00
|
|
|
end
|
|
|
|
end
|
2017-06-13 09:32:21 +00:00
|
|
|
end
|