Welcome to mirror list, hosted at ThFree Co, Russian Federation.

create_service_spec.rb « emails « services « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b13197f21b82b02bfb65cbed40d05b1cb287a12d (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'

RSpec.describe Emails::CreateService do
  let_it_be(:user) { create(:user) }

  let(:opts) { { email: 'new@email.com', user: user } }

  subject(:service) { described_class.new(user, opts) }

  describe '#execute' do
    it 'creates an email with valid attributes' do
      expect { service.execute }.to change { Email.count }.by(1)
      expect(Email.where(opts)).not_to be_empty
    end

    it 'creates an email with additional attributes' do
      expect { service.execute(confirmation_token: 'abc') }.to change { Email.count }.by(1)
      expect(Email.find_by(opts).confirmation_token).to eq 'abc'
    end

    it 'has the right user association' do
      service.execute

      expect(user.emails).to include(Email.find_by(opts))
    end

    it 'sends a notification to the user' do
      expect_next_instance_of(NotificationService) do |notification_service|
        expect(notification_service).to receive(:new_email_address_added)
      end

      service.execute
    end

    it 'does not send a notification when the email is not persisted' do
      allow_next_instance_of(NotificationService) do |notification_service|
        expect(notification_service).not_to receive(:new_email_address_added)
      end

      service.execute(email: 'invalid@@example.com')
    end

    it 'does not send a notification email when the email is the primary, because we are creating the user' do
      allow_next_instance_of(NotificationService) do |notification_service|
        expect(notification_service).not_to receive(:new_email_address_added)
      end

      # This is here to ensure that the service is actually called.
      allow_next_instance_of(described_class) do |create_service|
        expect(create_service).to receive(:execute).and_call_original
      end

      create(:user)
    end
  end
end