summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/private_commit_email_spec.rb
blob: bc86cd3842a2f68437e8a47a7fb3e2b4db788292 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# frozen_string_literal: true

require 'spec_helper'

describe Gitlab::PrivateCommitEmail do
  let(:hostname) { Gitlab::CurrentSettings.current_application_settings.commit_email_hostname }

  context '.regex' do
    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

  context '.user_id_for_email' do
    let(:id) { 1 }

    it 'parses user id from email' do
      email = "#{id}-foo@#{hostname}"

      expect(described_class.user_id_for_email(email)).to eq(id)
    end

    it 'returns nil on invalid commit email' do
      email = "#{id}-foo@users.noreply.bar.com"

      expect(described_class.user_id_for_email(email)).to be_nil
    end
  end

  context '.for_user' do
    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