summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/identifier_spec.rb
blob: 29912da2e252038fcbd2bd33e2f19c4b8e38f631 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
require 'spec_helper'

describe Gitlab::Identifier do
  let(:identifier) do
    Class.new { include Gitlab::Identifier }.new
  end

  let(:project) { create(:empty_project) }
  let(:user) { create(:user) }
  let(:key) { create(:key, user: user) }

  describe '#identify' do
    context 'without an identifier' do
      it 'identifies the user using a commit' do
        expect(identifier).to receive(:identify_using_commit)
          .with(project, '123')

        identifier.identify('', project, '123')
      end
    end

    context 'with a user identifier' do
      it 'identifies the user using a user ID' do
        expect(identifier).to receive(:identify_using_user)
          .with("user-#{user.id}")

        identifier.identify("user-#{user.id}", project, '123')
      end
    end

    context 'with an SSH key identifier' do
      it 'identifies the user using an SSH key ID' do
        expect(identifier).to receive(:identify_using_ssh_key)
          .with("key-#{key.id}")

        identifier.identify("key-#{key.id}", project, '123')
      end
    end
  end

  describe '#identify_using_commit' do
    it "returns the User for an existing commit author's Email address" do
      commit = double(:commit, author: user, author_email: user.email)

      expect(project).to receive(:commit).with('123').and_return(commit)

      expect(identifier.identify_using_commit(project, '123')).to eq(user)
    end

    it 'returns nil when no user could be found' do
      allow(project).to receive(:commit).with('123').and_return(nil)

      expect(identifier.identify_using_commit(project, '123')).to be_nil
    end

    it 'returns nil when the commit does not have an author Email' do
      commit = double(:commit, author_email: nil)

      expect(project).to receive(:commit).with('123').and_return(commit)

      expect(identifier.identify_using_commit(project, '123')).to be_nil
    end

    it 'caches the found users per Email' do
      commit = double(:commit, author: user, author_email: user.email)

      expect(project).to receive(:commit).with('123').twice.and_return(commit)

      2.times do
        expect(identifier.identify_using_commit(project, '123')).to eq(user)
      end
    end
  end

  describe '#identify_using_user' do
    it 'returns the User for an existing ID in the identifier' do
      found = identifier.identify_using_user("user-#{user.id}")

      expect(found).to eq(user)
    end

    it 'returns nil for a non existing user ID' do
      found = identifier.identify_using_user('user--1')

      expect(found).to be_nil
    end

    it 'caches the found users per ID' do
      expect(User).to receive(:find_by).once.and_call_original

      2.times do
        found = identifier.identify_using_user("user-#{user.id}")

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

  describe '#identify_using_ssh_key' do
    it 'returns the User for an existing SSH key' do
      found = identifier.identify_using_ssh_key("key-#{key.id}")

      expect(found).to eq(user)
    end

    it 'returns nil for an invalid SSH key' do
      found = identifier.identify_using_ssh_key('key--1')

      expect(found).to be_nil
    end

    it 'caches the found users per key' do
      expect(User).to receive(:find_by_ssh_key_id).once.and_call_original

      2.times do
        found = identifier.identify_using_ssh_key("key-#{key.id}")

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