2019-04-15 06:17:05 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-09-04 13:23:33 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-03 14:08:28 -04:00
|
|
|
RSpec.describe Profiles::EmailsController do
|
2020-09-11 05:08:44 -04:00
|
|
|
let_it_be(:user) { create(:user) }
|
2017-09-04 13:23:33 -04:00
|
|
|
|
|
|
|
before do
|
|
|
|
sign_in(user)
|
|
|
|
end
|
|
|
|
|
2020-05-18 05:08:12 -04:00
|
|
|
around do |example|
|
|
|
|
perform_enqueued_jobs do
|
|
|
|
example.run
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-10-01 14:10:20 -04:00
|
|
|
shared_examples_for 'respects the rate limit' do
|
|
|
|
context 'after the rate limit is exceeded' do
|
|
|
|
before do
|
|
|
|
allowed_threshold = Gitlab::ApplicationRateLimiter.rate_limits[action][:threshold]
|
|
|
|
|
|
|
|
allow(Gitlab::ApplicationRateLimiter)
|
|
|
|
.to receive(:increment)
|
|
|
|
.and_return(allowed_threshold + 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not send any email' do
|
|
|
|
expect { subject }.not_to change { ActionMailer::Base.deliveries.size }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'displays an alert' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:redirect)
|
|
|
|
expect(flash[:alert]).to eq(_('This action has been performed too many times. Try again later.'))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-04 13:23:33 -04:00
|
|
|
describe '#create' do
|
2020-09-11 05:08:44 -04:00
|
|
|
let(:email) { 'add_email@example.com' }
|
|
|
|
let(:params) { { email: { email: email } } }
|
2017-09-04 13:23:33 -04:00
|
|
|
|
2020-09-11 05:08:44 -04:00
|
|
|
subject { post(:create, params: params) }
|
|
|
|
|
|
|
|
it 'sends an email confirmation' do
|
|
|
|
expect { subject }.to change { ActionMailer::Base.deliveries.size }
|
2020-05-06 05:10:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when email address is invalid' do
|
2020-09-11 05:08:44 -04:00
|
|
|
let(:email) { 'invalid.@example.com' }
|
2020-05-06 05:10:02 -04:00
|
|
|
|
|
|
|
it 'does not send an email confirmation' do
|
2020-09-11 05:08:44 -04:00
|
|
|
expect { subject }.not_to change { ActionMailer::Base.deliveries.size }
|
2020-05-06 05:10:02 -04:00
|
|
|
end
|
2017-09-04 13:23:33 -04:00
|
|
|
end
|
2020-10-01 14:10:20 -04:00
|
|
|
|
|
|
|
it_behaves_like 'respects the rate limit' do
|
|
|
|
let(:action) { :profile_add_new_email }
|
|
|
|
end
|
2017-09-04 13:23:33 -04:00
|
|
|
end
|
2017-09-09 09:55:07 -04:00
|
|
|
|
|
|
|
describe '#resend_confirmation_instructions' do
|
2020-09-11 05:08:44 -04:00
|
|
|
let_it_be(:email) { create(:email, user: user) }
|
|
|
|
let(:params) { { id: email.id } }
|
|
|
|
|
|
|
|
subject { put(:resend_confirmation_instructions, params: params) }
|
2017-09-09 09:55:07 -04:00
|
|
|
|
|
|
|
it 'resends an email confirmation' do
|
2020-09-11 05:08:44 -04:00
|
|
|
expect { subject }.to change { ActionMailer::Base.deliveries.size }
|
2017-10-01 11:07:26 -04:00
|
|
|
|
2020-09-11 05:08:44 -04:00
|
|
|
expect(ActionMailer::Base.deliveries.last.to).to eq [email.email]
|
|
|
|
expect(ActionMailer::Base.deliveries.last.subject).to match 'Confirmation instructions'
|
2017-09-09 09:55:07 -04:00
|
|
|
end
|
|
|
|
|
2020-09-11 05:08:44 -04:00
|
|
|
context 'email does not exist' do
|
|
|
|
let(:params) { { id: non_existing_record_id } }
|
|
|
|
|
|
|
|
it 'does not send an email confirmation' do
|
|
|
|
expect { subject }.not_to change { ActionMailer::Base.deliveries.size }
|
|
|
|
end
|
2017-09-09 09:55:07 -04:00
|
|
|
end
|
2020-10-01 14:10:20 -04:00
|
|
|
|
|
|
|
it_behaves_like 'respects the rate limit' do
|
|
|
|
let(:action) { :profile_resend_email_confirmation }
|
|
|
|
end
|
2017-09-09 09:55:07 -04:00
|
|
|
end
|
2017-09-04 13:23:33 -04:00
|
|
|
end
|