summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/legacy_github_import/user_formatter_spec.rb
blob: 3cd096eb0ad98e92bdadbb39c7adac19fa91c966 (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
require 'spec_helper'

describe Gitlab::LegacyGithubImport::UserFormatter do
  let(:client) { double }
  let(:octocat) { double(id: 123456, login: 'octocat', email: 'octocat@example.com') }

  subject(:user) { described_class.new(client, octocat) }

  before do
    allow(client).to receive(:user).and_return(octocat)
  end

  describe '#gitlab_id' do
    context 'when GitHub user is a GitLab user' do
      it 'return GitLab user id when user associated their account with GitHub' do
        gl_user = create(:omniauth_user, extern_uid: octocat.id, provider: 'github')

        expect(user.gitlab_id).to eq gl_user.id
      end

      it 'returns GitLab user id when user primary email matches GitHub email' do
        gl_user = create(:user, email: octocat.email)

        expect(user.gitlab_id).to eq gl_user.id
      end

      it 'returns GitLab user id when any of user linked emails matches GitHub email' do
        gl_user = create(:user, email: 'johndoe@example.com')
        create(:email, user: gl_user, email: octocat.email)

        expect(user.gitlab_id).to eq gl_user.id
      end
    end

    it 'returns nil when GitHub user is not a GitLab user' do
      expect(user.gitlab_id).to be_nil
    end
  end
end