2018-11-07 06:00:21 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Gitlab::PrivateCommitEmail do
|
|
|
|
let(:hostname) { Gitlab::CurrentSettings.current_application_settings.commit_email_hostname }
|
2018-11-14 13:42:36 -05:00
|
|
|
let(:id) { 1 }
|
|
|
|
let(:valid_email) { "#{id}-foo@#{hostname}" }
|
|
|
|
let(:invalid_email) { "#{id}-foo@users.noreply.bar.com" }
|
2018-11-07 06:00:21 -05:00
|
|
|
|
2020-02-06 16:08:48 -05:00
|
|
|
describe '.regex' do
|
2018-11-07 06:00:21 -05:00
|
|
|
subject { described_class.regex }
|
|
|
|
|
|
|
|
it { is_expected.to match("1-foo@#{hostname}") }
|
|
|
|
it { is_expected.not_to match("1-foo@#{hostname}.foo") }
|
|
|
|
it { is_expected.not_to match('1-foo@users.noreply.gitlab.com') }
|
|
|
|
it { is_expected.not_to match('foo-1@users.noreply.gitlab.com') }
|
|
|
|
it { is_expected.not_to match('foobar@gitlab.com') }
|
|
|
|
end
|
|
|
|
|
2020-02-06 16:08:48 -05:00
|
|
|
describe '.user_id_for_email' do
|
2018-11-07 06:00:21 -05:00
|
|
|
it 'parses user id from email' do
|
2018-11-14 13:42:36 -05:00
|
|
|
expect(described_class.user_id_for_email(valid_email)).to eq(id)
|
2018-11-07 06:00:21 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns nil on invalid commit email' do
|
2018-11-14 13:42:36 -05:00
|
|
|
expect(described_class.user_id_for_email(invalid_email)).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-06 16:08:48 -05:00
|
|
|
describe '.user_ids_for_email' do
|
2018-11-14 13:42:36 -05:00
|
|
|
it 'returns deduplicated user IDs for each valid email' do
|
|
|
|
result = described_class.user_ids_for_emails([valid_email, valid_email, invalid_email])
|
|
|
|
|
|
|
|
expect(result).to eq([id])
|
|
|
|
end
|
2018-11-07 06:00:21 -05:00
|
|
|
|
2018-11-14 13:42:36 -05:00
|
|
|
it 'returns an empty array with no valid emails' do
|
|
|
|
result = described_class.user_ids_for_emails([invalid_email])
|
|
|
|
expect(result).to eq([])
|
2018-11-07 06:00:21 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-06 16:08:48 -05:00
|
|
|
describe '.for_user' do
|
2018-11-07 06:00:21 -05:00
|
|
|
it 'returns email in the format id-username@hostname' do
|
|
|
|
user = create(:user)
|
|
|
|
|
|
|
|
expect(described_class.for_user(user)).to eq("#{user.id}-#{user.username}@#{hostname}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|