2019-03-30 03:23:56 -04:00
# frozen_string_literal: true
2016-04-21 11:13:14 -04:00
require 'spec_helper'
2021-05-17 17:10:42 -04:00
RSpec . describe Integrations :: EmailsOnPush do
2021-04-06 11:09:23 -04:00
let_it_be ( :project ) { create_default ( :project ) . freeze }
2016-04-21 11:13:14 -04:00
describe 'Validations' do
2021-07-01 14:07:29 -04:00
context 'when integration is active' do
2017-06-14 14:18:56 -04:00
before do
subject . active = true
end
2016-04-21 11:13:14 -04:00
it { is_expected . to validate_presence_of ( :recipients ) }
end
2021-07-01 14:07:29 -04:00
context 'when integration is inactive' do
2017-06-14 14:18:56 -04:00
before do
subject . active = false
end
2016-04-21 11:13:14 -04:00
it { is_expected . not_to validate_presence_of ( :recipients ) }
end
2021-04-06 11:09:23 -04:00
describe 'validates number of recipients' do
before do
stub_const ( " #{ described_class } ::RECIPIENTS_LIMIT " , 2 )
end
2021-07-01 14:07:29 -04:00
subject ( :integration ) { described_class . new ( project : project , recipients : recipients , active : true ) }
2021-04-06 11:09:23 -04:00
context 'valid number of recipients' do
let ( :recipients ) { 'foo@bar.com duplicate@example.com Duplicate@example.com invalid-email' }
it 'does not count duplicates and invalid emails' do
is_expected . to be_valid
end
end
context 'invalid number of recipients' do
let ( :recipients ) { 'foo@bar.com bar@foo.com bob@gitlab.com' }
it { is_expected . not_to be_valid }
it 'adds an error message' do
2021-07-01 14:07:29 -04:00
integration . valid?
2021-04-06 11:09:23 -04:00
2021-07-01 14:07:29 -04:00
expect ( integration . errors ) . to contain_exactly ( 'Recipients can\'t exceed 2' )
2021-04-06 11:09:23 -04:00
end
2021-07-01 14:07:29 -04:00
context 'when integration is not active' do
2021-04-06 11:09:23 -04:00
before do
2021-07-01 14:07:29 -04:00
integration . active = false
2021-04-06 11:09:23 -04:00
end
it { is_expected . to be_valid }
end
end
end
2016-04-21 11:13:14 -04:00
end
2019-08-15 13:37:36 -04:00
2020-05-20 05:08:11 -04:00
describe '.new' do
context 'when properties is missing branches_to_be_notified' do
subject { described_class . new ( properties : { } ) }
2020-02-03 04:08:42 -05:00
2020-05-20 05:08:11 -04:00
it 'sets the default value to all' do
expect ( subject . branches_to_be_notified ) . to eq ( 'all' )
end
2020-02-03 04:08:42 -05:00
end
2020-05-20 05:08:11 -04:00
context 'when branches_to_be_notified is already set' do
subject { described_class . new ( properties : { branches_to_be_notified : 'protected' } ) }
2020-02-03 04:08:42 -05:00
2020-05-20 05:08:11 -04:00
it 'does not overwrite it with the default value' do
expect ( subject . branches_to_be_notified ) . to eq ( 'protected' )
end
2020-02-03 04:08:42 -05:00
end
end
2021-04-06 11:09:23 -04:00
describe '.valid_recipients' do
let ( :recipients ) { '<invalid> foobar Valid@recipient.com Dup@lica.te dup@lica.te Dup@Lica.te' }
it 'removes invalid email addresses and removes duplicates by keeping the original capitalization' do
expect ( described_class . valid_recipients ( recipients ) ) . to contain_exactly ( 'Valid@recipient.com' , 'Dup@lica.te' )
end
end
2020-05-20 05:08:11 -04:00
describe '#execute' do
2019-08-15 13:37:36 -04:00
let ( :push_data ) { { object_kind : 'push' } }
let ( :project ) { create ( :project , :repository ) }
2021-06-16 14:10:35 -04:00
let ( :integration ) { create ( :emails_on_push_integration , project : project ) }
2019-12-31 10:08:53 -05:00
let ( :recipients ) { 'test@gitlab.com' }
2019-08-15 13:37:36 -04:00
2019-12-31 10:08:53 -05:00
before do
subject . recipients = recipients
end
shared_examples 'sending email' do | branches_to_be_notified , branch_being_pushed_to |
let ( :push_data ) { { object_kind : 'push' , object_attributes : { ref : branch_being_pushed_to } } }
2019-08-15 13:37:36 -04:00
2019-12-31 10:08:53 -05:00
before do
subject . branches_to_be_notified = branches_to_be_notified
end
it 'sends email' do
expect ( EmailsOnPushWorker ) . not_to receive ( :perform_async )
2021-06-16 14:10:35 -04:00
integration . execute ( push_data )
2019-12-31 10:08:53 -05:00
end
2019-08-15 13:37:36 -04:00
end
2019-12-31 10:08:53 -05:00
shared_examples 'not sending email' do | branches_to_be_notified , branch_being_pushed_to |
let ( :push_data ) { { object_kind : 'push' , object_attributes : { ref : branch_being_pushed_to } } }
2019-08-15 13:37:36 -04:00
2019-12-31 10:08:53 -05:00
before do
subject . branches_to_be_notified = branches_to_be_notified
end
it 'does not send email' do
expect ( EmailsOnPushWorker ) . not_to receive ( :perform_async )
2021-06-16 14:10:35 -04:00
integration . execute ( push_data )
2019-12-31 10:08:53 -05:00
end
end
context 'when emails are disabled on the project' do
it 'does not send emails' do
expect ( project ) . to receive ( :emails_disabled? ) . and_return ( true )
expect ( EmailsOnPushWorker ) . not_to receive ( :perform_async )
2021-06-16 14:10:35 -04:00
integration . execute ( push_data )
2019-12-31 10:08:53 -05:00
end
end
context 'when emails are enabled on the project' do
before do
create ( :protected_branch , project : project , name : 'a-protected-branch' )
expect ( project ) . to receive ( :emails_disabled? ) . and_return ( true )
end
using RSpec :: Parameterized :: TableSyntax
where ( :case_name , :branches_to_be_notified , :branch_being_pushed_to , :expected_action ) do
'pushing to a random branch and notification configured for all branches' | 'all' | 'random' | 'sending email'
'pushing to the default branch and notification configured for all branches' | 'all' | 'master' | 'sending email'
'pushing to a protected branch and notification configured for all branches' | 'all' | 'a-protected-branch' | 'sending email'
'pushing to a random branch and notification configured for default branch only' | 'default' | 'random' | 'not sending email'
'pushing to the default branch and notification configured for default branch only' | 'default' | 'master' | 'sending email'
'pushing to a protected branch and notification configured for default branch only' | 'default' | 'a-protected-branch' | 'not sending email'
'pushing to a random branch and notification configured for protected branches only' | 'protected' | 'random' | 'not sending email'
'pushing to the default branch and notification configured for protected branches only' | 'protected' | 'master' | 'not sending email'
'pushing to a protected branch and notification configured for protected branches only' | 'protected' | 'a-protected-branch' | 'sending email'
'pushing to a random branch and notification configured for default and protected branches only' | 'default_and_protected' | 'random' | 'not sending email'
'pushing to the default branch and notification configured for default and protected branches only' | 'default_and_protected' | 'master' | 'sending email'
'pushing to a protected branch and notification configured for default and protected branches only' | 'default_and_protected' | 'a-protected-branch' | 'sending email'
end
with_them do
include_examples params [ :expected_action ] , branches_to_be_notified : params [ :branches_to_be_notified ] , branch_being_pushed_to : params [ :branch_being_pushed_to ]
end
2019-08-15 13:37:36 -04:00
end
end
2016-04-21 11:13:14 -04:00
end