summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/jira_import/user_mapper_spec.rb
blob: c8c8bd3c5b007edfcc8c7dfa28ae1a4b27297299 (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# frozen_string_literal: true

require 'spec_helper'

describe Gitlab::JiraImport::UserMapper do
  let_it_be(:group)   { create(:group) }
  let_it_be(:project) { create(:project, group: group) }
  let_it_be(:user)    { create(:user, email: 'user@example.com') }
  let_it_be(:email)   { create(:email, user: user, email: 'second_email@example.com', confirmed_at: nil) }

  let(:jira_user) { { 'acountId' => '1a2b', 'emailAddress' => 'user@example.com' } }

  describe '#execute' do
    subject { described_class.new(project, jira_user).execute }

    context 'when jira_user is nil' do
      let(:jira_user) { nil }

      it 'returns nil' do
        expect(subject).to be_nil
      end
    end

    context 'when Gitlab user is not found by email' do
      let(:jira_user) { { 'acountId' => '1a2b', 'emailAddress' => 'other@example.com' } }

      it 'returns nil' do
        expect(subject).to be_nil
      end
    end

    context 'when jira_user emailAddress is nil' do
      let(:jira_user) { { 'acountId' => '1a2b', 'emailAddress' => nil } }

      it 'returns nil' do
        expect(subject).to be_nil
      end
    end

    context 'when jira_user emailAddress key is missing' do
      let(:jira_user) { { 'acountId' => '1a2b' } }

      it 'returns nil' do
        expect(subject).to be_nil
      end
    end

    context 'when found user is not a project member' do
      it 'returns nil' do
        expect(subject).to be_nil
      end
    end

    context 'when found user is a project member' do
      it 'returns the found user' do
        project.add_developer(user)

        expect(subject).to eq(user)
      end
    end

    context 'when user found by unconfirmd secondary address is a project member' do
      let(:jira_user) { { 'acountId' => '1a2b', 'emailAddress' => 'second_email@example.com' } }

      it 'returns the found user' do
        project.add_developer(user)

        expect(subject).to eq(user)
      end
    end

    context 'when user is a group member' do
      it 'returns the found user' do
        group.add_developer(user)

        expect(subject).to eq(user)
      end
    end
  end
end