2019-04-11 08:17:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-06-13 05:32:21 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-07-10 10:24:02 -04:00
|
|
|
describe Users::UpdateService do
|
2017-06-13 05:32:21 -04:00
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
describe '#execute' do
|
|
|
|
it 'updates the name' do
|
2017-06-23 05:34:07 -04:00
|
|
|
result = update_user(user, name: 'New Name')
|
2017-06-13 10:24:36 -04:00
|
|
|
|
2017-06-23 11:11:31 -04:00
|
|
|
expect(result).to eq(status: :success)
|
2017-06-13 05:32:21 -04:00
|
|
|
expect(user.name).to eq('New Name')
|
|
|
|
end
|
|
|
|
|
2019-05-07 17:58:16 -04:00
|
|
|
it 'updates time preferences' do
|
|
|
|
result = update_user(user, timezone: 'Europe/Warsaw', time_display_relative: true, time_format_in_24h: false)
|
|
|
|
|
|
|
|
expect(result).to eq(status: :success)
|
|
|
|
expect(user.reload.timezone).to eq('Europe/Warsaw')
|
|
|
|
expect(user.time_display_relative).to eq(true)
|
|
|
|
expect(user.time_format_in_24h).to eq(false)
|
|
|
|
end
|
|
|
|
|
2017-06-13 05:32:21 -04:00
|
|
|
it 'returns an error result when record cannot be updated' do
|
2017-08-18 13:31:59 -04:00
|
|
|
result = {}
|
2017-06-13 07:52:20 -04:00
|
|
|
expect do
|
2017-08-18 13:31:59 -04:00
|
|
|
result = update_user(user, { email: 'invalid' })
|
2017-06-23 05:34:07 -04:00
|
|
|
end.not_to change { user.reload.email }
|
2017-08-18 13:31:59 -04:00
|
|
|
expect(result[:status]).to eq(:error)
|
|
|
|
expect(result[:message]).to eq('Email is invalid')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes namespace error messages' do
|
2018-01-31 11:52:09 -05:00
|
|
|
create(:group, path: 'taken')
|
2017-08-18 13:31:59 -04:00
|
|
|
result = {}
|
|
|
|
expect do
|
|
|
|
result = update_user(user, { username: 'taken' })
|
|
|
|
end.not_to change { user.reload.username }
|
|
|
|
expect(result[:status]).to eq(:error)
|
2018-01-31 11:52:09 -05:00
|
|
|
expect(result[:message]).to eq('Username has already been taken')
|
2017-06-13 05:32:21 -04:00
|
|
|
end
|
|
|
|
|
2018-07-13 11:52:31 -04:00
|
|
|
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-06-23 05:34:07 -04:00
|
|
|
def update_user(user, opts)
|
2017-09-27 05:48:33 -04:00
|
|
|
described_class.new(user, opts.merge(user: user)).execute
|
2017-06-13 05:32:21 -04:00
|
|
|
end
|
|
|
|
end
|
2017-06-13 10:24:36 -04:00
|
|
|
|
|
|
|
describe '#execute!' do
|
|
|
|
it 'updates the name' do
|
2017-09-27 05:48:33 -04:00
|
|
|
service = described_class.new(user, user: user, name: 'New Name')
|
2017-08-25 19:15:43 -04:00
|
|
|
expect(service).not_to receive(:notify_new_user)
|
|
|
|
|
|
|
|
result = service.execute!
|
2017-06-13 10:24:36 -04:00
|
|
|
|
|
|
|
expect(result).to be true
|
|
|
|
expect(user.name).to eq('New Name')
|
|
|
|
end
|
|
|
|
|
2017-06-23 11:11:31 -04:00
|
|
|
it 'raises an error when record cannot be updated' do
|
2017-06-13 10:24:36 -04:00
|
|
|
expect do
|
2017-06-23 11:11:31 -04:00
|
|
|
update_user(user, email: 'invalid')
|
2017-06-23 05:34:07 -04:00
|
|
|
end.to raise_error(ActiveRecord::RecordInvalid)
|
2017-06-13 10:24:36 -04:00
|
|
|
end
|
|
|
|
|
2017-08-25 19:15:43 -04:00
|
|
|
it 'fires system hooks when a new user is saved' do
|
|
|
|
system_hook_service = spy(:system_hook_service)
|
|
|
|
user = build(:user)
|
2017-09-27 05:48:33 -04:00
|
|
|
service = described_class.new(user, user: user, name: 'John Doe')
|
2017-08-25 19:15:43 -04:00
|
|
|
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
|
|
|
|
|
2017-06-23 05:34:07 -04:00
|
|
|
def update_user(user, opts)
|
2017-09-27 05:48:33 -04:00
|
|
|
described_class.new(user, opts.merge(user: user)).execute!
|
2017-06-13 10:24:36 -04:00
|
|
|
end
|
|
|
|
end
|
2017-06-13 05:32:21 -04:00
|
|
|
end
|