2019-07-07 11:18:12 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
RSpec.describe Emails::CreateService, feature_category: :user_management do
|
2021-12-11 22:18:48 +05:30
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
let(:opts) { { email: 'new@email.com', user: user } }
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
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
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
it 'creates an email with additional attributes' do
|
|
|
|
expect { service.execute(confirmation_token: 'abc') }.to change { Email.count }.by(1)
|
2020-04-22 19:07:51 +05:30
|
|
|
expect(Email.find_by(opts).confirmation_token).to eq 'abc'
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
it 'has the right user association' do
|
|
|
|
service.execute
|
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
expect(user.emails).to include(Email.find_by(opts))
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
2022-06-21 17:19:12 +05:30
|
|
|
|
|
|
|
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
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
end
|