summaryrefslogtreecommitdiff
path: root/spec/services/users/set_status_service_spec.rb
blob: 7c26be483458c582d09d2359eadab3ffdf592fed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# frozen_string_literal: true

require 'spec_helper'

describe Users::SetStatusService do
  let(:current_user) { create(:user) }
  subject(:service) { described_class.new(current_user, params) }

  describe '#execute' do
    context 'when params are set' do
      let(:params) { { emoji: 'taurus', message: 'a random status' } }

      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')
      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

      context 'for another user' do
        let(:target_user) { create(:user) }
        let(:params) do
          { emoji: 'taurus', message: 'a random status', user: target_user }
        end

        context 'the current user is admin' do
          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
      end
    end

    context 'without params' do
      let(:params) { {} }

      it 'deletes the status' do
        status = create(:user_status, user: current_user)

        expect { service.execute }
          .to change { current_user.reload.status }.from(status).to(nil)
      end
    end
  end
end