summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/phabricator_import/user_finder_spec.rb
blob: 918ff28c8f5115548cc18a6fdf695235d7cfb305 (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
81
82
83
84
85
86
87
88
89
90
91
# frozen_string_literal: true

require 'spec_helper'

describe Gitlab::PhabricatorImport::UserFinder, :clean_gitlab_redis_cache do
  let(:project) { create(:project, namespace: create(:group)) }
  subject(:finder) { described_class.new(project, ['first-phid', 'second-phid']) }

  before do
    project.namespace.add_developer(existing_user)
  end

  describe '#find' do
    let!(:existing_user) { create(:user, username: 'existing-user') }
    let(:cache) { Gitlab::PhabricatorImport::Cache::Map.new(project) }

    before do
      allow(finder).to receive(:object_map).and_return(cache)
    end

    context 'for a cached phid' do
      before do
        cache.set_gitlab_model(existing_user, 'first-phid')
      end

      it 'returns the existing user' do
        expect(finder.find('first-phid')).to eq(existing_user)
      end

      it 'does not perform a find using the API' do
        expect(finder).not_to receive(:find_user_for_phid)

        finder.find('first-phid')
      end

      it 'excludes the phid from the request if one needs to be made' do
        client = instance_double(Gitlab::PhabricatorImport::Conduit::User)
        allow(finder).to receive(:client).and_return(client)

        expect(client).to receive(:users).with(['second-phid']).and_return([])

        finder.find('first-phid')
        finder.find('second-phid')
      end
    end

    context 'when the phid is not cached' do
      let(:response) do
        [
          instance_double(
            Gitlab::PhabricatorImport::Conduit::UsersResponse,
            users: [instance_double(Gitlab::PhabricatorImport::Representation::User, phabricator_id: 'second-phid', username: 'existing-user')]
          ),
          instance_double(
            Gitlab::PhabricatorImport::Conduit::UsersResponse,
            users: [instance_double(Gitlab::PhabricatorImport::Representation::User, phabricator_id: 'first-phid', username: 'other-user')]
          )
        ]
      end
      let(:client) do
        client = instance_double(Gitlab::PhabricatorImport::Conduit::User)
        allow(client).to receive(:users).and_return(response)

        client
      end

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

      it 'loads the users from the API once' do
        expect(client).to receive(:users).and_return(response).once

        expect(finder.find('second-phid')).to eq(existing_user)
        expect(finder.find('first-phid')).to be_nil
      end

      it 'adds found users to the cache' do
        expect { finder.find('second-phid') }
          .to change { cache.get_gitlab_model('second-phid') }
                .from(nil).to(existing_user)
      end

      it 'only returns users that are members of the project' do
        create(:user, username: 'other-user')

        expect(finder.find('first-phid')).to eq(nil)
      end
    end
  end
end