2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
RSpec.describe Users::SetStatusService, feature_category: :user_management do
|
2018-11-18 11:00:15 +05:30
|
|
|
let(:current_user) { create(:user) }
|
2020-01-01 13:55:28 +05:30
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
subject(:service) { described_class.new(current_user, params) }
|
|
|
|
|
|
|
|
describe '#execute' do
|
2021-10-27 15:23:28 +05:30
|
|
|
shared_examples_for 'bumps user' do
|
|
|
|
it 'bumps User#updated_at' do
|
|
|
|
expect { service.execute }.to change { current_user.updated_at }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples_for 'does not bump user' do
|
|
|
|
it 'does not bump User#updated_at' do
|
|
|
|
expect { service.execute }.not_to change { current_user.updated_at }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
context 'when params are set' do
|
2021-01-29 00:20:46 +05:30
|
|
|
let(:params) { { emoji: 'taurus', message: 'a random status', availability: 'busy' } }
|
2018-11-18 11:00:15 +05:30
|
|
|
|
|
|
|
it 'creates a status' do
|
|
|
|
service.execute
|
|
|
|
|
|
|
|
expect(current_user.status.emoji).to eq('taurus')
|
|
|
|
expect(current_user.status.message).to eq('a random status')
|
2021-01-29 00:20:46 +05:30
|
|
|
expect(current_user.status.availability).to eq('busy')
|
2018-11-18 11:00:15 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates a status if it already existed' do
|
|
|
|
create(:user_status, user: current_user)
|
|
|
|
|
|
|
|
expect { service.execute }.not_to change { UserStatus.count }
|
|
|
|
expect(current_user.status.message).to eq('a random status')
|
|
|
|
end
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
it 'returns true' do
|
|
|
|
create(:user_status, user: current_user)
|
|
|
|
expect(service.execute).to be(true)
|
|
|
|
end
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
it_behaves_like 'bumps user'
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
context 'when setting availability to not_set' do
|
|
|
|
before do
|
|
|
|
params[:availability] = 'not_set'
|
|
|
|
|
|
|
|
create(:user_status, user: current_user, availability: 'busy')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates the availability' do
|
|
|
|
expect { service.execute }.to change { current_user.status.availability }.from('busy').to('not_set')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
context 'when the given availability value is not valid' do
|
2021-02-22 17:27:13 +05:30
|
|
|
before do
|
|
|
|
params[:availability] = 'not a valid value'
|
|
|
|
end
|
2021-01-29 00:20:46 +05:30
|
|
|
|
|
|
|
it 'does not update the status' do
|
|
|
|
user_status = create(:user_status, user: current_user)
|
|
|
|
|
|
|
|
expect { service.execute }.not_to change { user_status.reload }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
context 'for another user' do
|
|
|
|
let(:target_user) { create(:user) }
|
|
|
|
let(:params) do
|
|
|
|
{ emoji: 'taurus', message: 'a random status', user: target_user }
|
|
|
|
end
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
context 'the current user is admin', :enable_admin_mode do
|
2018-11-18 11:00:15 +05:30
|
|
|
let(:current_user) { create(:admin) }
|
|
|
|
|
|
|
|
it 'changes the status when the current user is allowed to do that' do
|
|
|
|
expect { service.execute }.to change { target_user.status }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not update the status if the current user is not allowed' do
|
|
|
|
expect { service.execute }.not_to change { target_user.status }
|
|
|
|
end
|
2021-10-27 15:23:28 +05:30
|
|
|
|
|
|
|
it_behaves_like 'does not bump user'
|
2018-11-18 11:00:15 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'without params' do
|
|
|
|
let(:params) { {} }
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
shared_examples 'removes user status record' do
|
2021-10-27 15:23:28 +05:30
|
|
|
it 'deletes the user status record' do
|
2021-02-22 17:27:13 +05:30
|
|
|
expect { service.execute }
|
2021-10-27 15:23:28 +05:30
|
|
|
.to change { current_user.reload.status }.from(user_status).to(nil)
|
2021-02-22 17:27:13 +05:30
|
|
|
end
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
it_behaves_like 'bumps user'
|
|
|
|
end
|
2021-02-22 17:27:13 +05:30
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
context 'when user has existing user status record' do
|
|
|
|
let!(:user_status) { create(:user_status, user: current_user) }
|
2018-11-18 11:00:15 +05:30
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
it_behaves_like 'removes user status record'
|
2021-10-27 15:23:28 +05:30
|
|
|
|
|
|
|
context 'when not_set is given for availability' do
|
|
|
|
let(:params) { { availability: 'not_set' } }
|
|
|
|
|
|
|
|
it_behaves_like 'removes user status record'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user has no existing user status record' do
|
|
|
|
it_behaves_like 'does not bump user'
|
2018-11-18 11:00:15 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|