2019-03-30 03:23:56 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-02-09 09:51:06 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe Email do
|
2020-05-18 05:08:12 -04:00
|
|
|
describe 'modules' do
|
|
|
|
subject { described_class }
|
|
|
|
|
|
|
|
it { is_expected.to include_module(AsyncDeviseEmail) }
|
|
|
|
end
|
|
|
|
|
2016-02-09 09:51:06 -05:00
|
|
|
describe 'validations' do
|
2022-01-13 07:14:38 -05:00
|
|
|
it_behaves_like 'an object with email-formatted attributes', :email do
|
2016-02-09 09:51:06 -05:00
|
|
|
subject { build(:email) }
|
|
|
|
end
|
2021-11-03 20:12:36 -04:00
|
|
|
|
|
|
|
context 'when the email conflicts with the primary email of a different user' do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:email) { build(:email, email: user.email) }
|
|
|
|
|
|
|
|
it 'is invalid' do
|
|
|
|
expect(email).to be_invalid
|
|
|
|
end
|
|
|
|
end
|
2016-02-09 09:51:06 -05:00
|
|
|
end
|
2016-05-23 05:19:27 -04:00
|
|
|
|
|
|
|
it 'normalize email value' do
|
|
|
|
expect(described_class.new(email: ' inFO@exAMPLe.com ').email)
|
|
|
|
.to eq 'info@example.com'
|
|
|
|
end
|
2017-09-18 08:50:32 -04:00
|
|
|
|
|
|
|
describe '#update_invalid_gpg_signatures' do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
it 'synchronizes the gpg keys when the email is updated' do
|
2021-05-19 08:10:33 -04:00
|
|
|
email = user.emails.create!(email: 'new@email.com')
|
2017-10-01 11:07:26 -04:00
|
|
|
|
2017-09-18 08:50:32 -04:00
|
|
|
expect(user).to receive(:update_invalid_gpg_signatures)
|
2017-10-01 11:07:26 -04:00
|
|
|
|
2017-09-18 08:50:32 -04:00
|
|
|
email.confirm
|
|
|
|
end
|
|
|
|
end
|
2017-09-24 13:52:49 -04:00
|
|
|
|
2017-09-20 07:24:10 -04:00
|
|
|
describe 'scopes' do
|
2021-11-03 20:12:36 -04:00
|
|
|
let(:user) { create(:user, :unconfirmed) }
|
2017-09-20 07:24:10 -04:00
|
|
|
|
|
|
|
it 'scopes confirmed emails' do
|
|
|
|
create(:email, :confirmed, user: user)
|
2017-09-20 08:51:27 -04:00
|
|
|
create(:email, user: user)
|
2017-10-01 11:07:26 -04:00
|
|
|
|
2017-09-20 08:51:27 -04:00
|
|
|
expect(user.emails.count).to eq 2
|
2017-09-20 07:24:10 -04:00
|
|
|
expect(user.emails.confirmed.count).to eq 1
|
|
|
|
end
|
|
|
|
end
|
2017-10-27 11:35:40 -04:00
|
|
|
|
2021-05-11 14:10:36 -04:00
|
|
|
describe 'delegations' do
|
|
|
|
it { is_expected.to delegate_method(:can?).to(:user) }
|
|
|
|
it { is_expected.to delegate_method(:username).to(:user) }
|
|
|
|
it { is_expected.to delegate_method(:pending_invitations).to(:user) }
|
|
|
|
it { is_expected.to delegate_method(:accept_pending_invitations!).to(:user) }
|
2017-10-27 10:32:48 -04:00
|
|
|
end
|
2020-05-18 05:08:12 -04:00
|
|
|
|
|
|
|
describe 'Devise emails' do
|
|
|
|
let!(:user) { create(:user) }
|
|
|
|
|
|
|
|
describe 'behaviour' do
|
|
|
|
it 'sends emails asynchronously' do
|
|
|
|
expect do
|
|
|
|
user.emails.create!(email: 'hello@hello.com')
|
|
|
|
end.to have_enqueued_job.on_queue('mailers')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-01-05 01:13:32 -05:00
|
|
|
|
|
|
|
describe '#confirm' do
|
|
|
|
let(:expired_confirmation_sent_at) { Date.today - described_class.confirm_within - 7.days }
|
|
|
|
let(:extant_confirmation_sent_at) { Date.today }
|
|
|
|
|
|
|
|
let(:email) do
|
|
|
|
create(:email, email: 'test@gitlab.com').tap do |email|
|
|
|
|
email.update!(confirmation_sent_at: confirmation_sent_at)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples_for 'unconfirmed email' do
|
|
|
|
it 'returns unconfirmed' do
|
|
|
|
expect(email.confirmed?).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the confirmation period has expired' do
|
|
|
|
let(:confirmation_sent_at) { expired_confirmation_sent_at }
|
|
|
|
|
|
|
|
it_behaves_like 'unconfirmed email'
|
|
|
|
|
|
|
|
it 'does not confirm the email' do
|
|
|
|
email.confirm
|
|
|
|
|
|
|
|
expect(email.confirmed?).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the confirmation period has not expired' do
|
|
|
|
let(:confirmation_sent_at) { extant_confirmation_sent_at }
|
|
|
|
|
|
|
|
it_behaves_like 'unconfirmed email'
|
|
|
|
|
|
|
|
it 'confirms the email' do
|
|
|
|
email.confirm
|
|
|
|
|
|
|
|
expect(email.confirmed?).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#force_confirm' do
|
|
|
|
let(:expired_confirmation_sent_at) { Date.today - described_class.confirm_within - 7.days }
|
|
|
|
let(:extant_confirmation_sent_at) { Date.today }
|
|
|
|
|
|
|
|
let(:email) do
|
|
|
|
create(:email, email: 'test@gitlab.com').tap do |email|
|
|
|
|
email.update!(confirmation_sent_at: confirmation_sent_at)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples_for 'unconfirmed email' do
|
|
|
|
it 'returns unconfirmed' do
|
|
|
|
expect(email.confirmed?).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples_for 'confirms the email on force_confirm' do
|
|
|
|
it 'confirms an email' do
|
|
|
|
email.force_confirm
|
|
|
|
|
|
|
|
expect(email.reload.confirmed?).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the confirmation period has expired' do
|
|
|
|
let(:confirmation_sent_at) { expired_confirmation_sent_at }
|
|
|
|
|
|
|
|
it_behaves_like 'unconfirmed email'
|
|
|
|
it_behaves_like 'confirms the email on force_confirm'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the confirmation period has not expired' do
|
|
|
|
let(:confirmation_sent_at) { extant_confirmation_sent_at }
|
|
|
|
|
|
|
|
it_behaves_like 'unconfirmed email'
|
|
|
|
it_behaves_like 'confirms the email on force_confirm'
|
|
|
|
end
|
|
|
|
end
|
2016-02-09 09:51:06 -05:00
|
|
|
end
|