summaryrefslogtreecommitdiff
path: root/spec/services/achievements/award_service_spec.rb
blob: c70c1d5c22dda47144ea20ca2f9411b20e721942 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Achievements::AwardService, feature_category: :user_profile do
  describe '#execute' do
    let_it_be(:developer) { create(:user) }
    let_it_be(:maintainer) { create(:user) }
    let_it_be(:group) { create(:group) }
    let_it_be(:achievement) { create(:achievement, namespace: group) }
    let_it_be(:recipient) { create(:user) }

    let(:achievement_id) { achievement.id }
    let(:recipient_id) { recipient.id }

    subject(:response) { described_class.new(current_user, achievement_id, recipient_id).execute }

    before_all do
      group.add_developer(developer)
      group.add_maintainer(maintainer)
    end

    context 'when user does not have permission' do
      let(:current_user) { developer }

      it 'returns an error' do
        expect(response).to be_error
        expect(response.message).to match_array(
          ['You have insufficient permissions to award this achievement'])
      end
    end

    context 'when user has permission' do
      let(:current_user) { maintainer }
      let(:notification_service) { instance_double(NotificationService) }
      let(:mail_message) { instance_double(ActionMailer::MessageDelivery) }

      it 'creates an achievement and sends an e-mail' do
        allow(NotificationService).to receive(:new).and_return(notification_service)
        expect(notification_service).to receive(:new_achievement_email).with(recipient, achievement)
          .and_return(mail_message)
        expect(mail_message).to receive(:deliver_later)

        expect(response).to be_success
      end

      context 'when the achievement is not persisted' do
        let(:user_achievement) { instance_double('Achievements::UserAchievement') }

        it 'returns the correct error' do
          allow(user_achievement).to receive(:persisted?).and_return(false)
          allow(user_achievement).to receive(:errors).and_return(nil)
          allow(Achievements::UserAchievement).to receive(:create).and_return(user_achievement)

          expect(response).to be_error
          expect(response.message).to match_array(["Failed to award achievement"])
        end
      end

      context 'when the achievement does not exist' do
        let(:achievement_id) { non_existing_record_id }

        it 'returns the correct error' do
          expect(response).to be_error
          expect(response.message)
            .to contain_exactly("Couldn't find Achievements::Achievement with 'id'=#{non_existing_record_id}")
        end
      end

      context 'when the recipient does not exist' do
        let(:recipient_id) { non_existing_record_id }

        it 'returns the correct error' do
          expect(response).to be_error
          expect(response.message).to contain_exactly("Couldn't find User with 'id'=#{non_existing_record_id}")
        end
      end
    end
  end
end