debian-mirror-gitlab/spec/controllers/profiles/emails_controller_spec.rb

90 lines
2.4 KiB
Ruby
Raw Normal View History

2019-07-31 22:56:46 +05:30
# frozen_string_literal: true
2018-03-17 18:26:18 +05:30
require 'spec_helper'
2020-06-23 00:09:42 +05:30
RSpec.describe Profiles::EmailsController do
2020-10-04 03:57:07 +05:30
let_it_be(:user) { create(:user) }
2018-03-17 18:26:18 +05:30
before do
sign_in(user)
end
2020-05-24 23:13:21 +05:30
around do |example|
perform_enqueued_jobs do
example.run
end
end
2020-10-04 03:57:07 +05:30
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
2018-03-17 18:26:18 +05:30
2020-10-04 03:57:07 +05:30
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.'))
2020-05-24 23:13:21 +05:30
end
end
2020-10-04 03:57:07 +05:30
end
describe '#create' do
let(:email) { 'add_email@example.com' }
let(:params) { { email: { email: email } } }
subject { post(:create, params: params) }
it 'sends an email confirmation' do
expect { subject }.to change { ActionMailer::Base.deliveries.size }
end
2020-05-24 23:13:21 +05:30
context 'when email address is invalid' do
2020-10-04 03:57:07 +05:30
let(:email) { 'invalid.@example.com' }
2020-05-24 23:13:21 +05:30
it 'does not send an email confirmation' do
2020-10-04 03:57:07 +05:30
expect { subject }.not_to change { ActionMailer::Base.deliveries.size }
2020-05-24 23:13:21 +05:30
end
2018-03-17 18:26:18 +05:30
end
2020-10-04 03:57:07 +05:30
it_behaves_like 'respects the rate limit' do
let(:action) { :profile_add_new_email }
end
2018-03-17 18:26:18 +05:30
end
describe '#resend_confirmation_instructions' do
2020-10-04 03:57:07 +05:30
let_it_be(:email) { create(:email, user: user) }
let(:params) { { id: email.id } }
subject { put(:resend_confirmation_instructions, params: params) }
2018-03-17 18:26:18 +05:30
it 'resends an email confirmation' do
2020-10-04 03:57:07 +05:30
expect { subject }.to change { ActionMailer::Base.deliveries.size }
2018-03-17 18:26:18 +05:30
2020-10-04 03:57:07 +05:30
expect(ActionMailer::Base.deliveries.last.to).to eq [email.email]
expect(ActionMailer::Base.deliveries.last.subject).to match 'Confirmation instructions'
end
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
2018-03-17 18:26:18 +05:30
end
2020-10-04 03:57:07 +05:30
it_behaves_like 'respects the rate limit' do
let(:action) { :profile_resend_email_confirmation }
2018-03-17 18:26:18 +05:30
end
end
end