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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::GithubImport::UserFinder, :clean_gitlab_redis_cache do
let(:project) do
create(
:project,
import_type: 'github',
import_url: 'https://api.github.com/user/repo'
)
end
let(:client) { double(:client) }
let(:finder) { described_class.new(project, client) }
describe '#author_id_for' do
context 'with default author_key' do
it 'returns the user ID for the author of an object' do
user = { id: 4, login: 'kittens' }
note = { author: user }
expect(finder).to receive(:user_id_for).with(user).and_return(42)
expect(finder.author_id_for(note)).to eq([42, true])
end
it 'returns the ID of the project creator if no user ID could be found' do
user = { id: 4, login: 'kittens' }
note = { author: user }
expect(finder).to receive(:user_id_for).with(user).and_return(nil)
expect(finder.author_id_for(note)).to eq([project.creator_id, false])
end
it 'returns the ID of the ghost user when the object has no user' do
note = { author: nil }
expect(finder.author_id_for(note)).to eq([User.ghost.id, true])
end
it 'returns the ID of the ghost user when the given object is nil' do
expect(finder.author_id_for(nil)).to eq([User.ghost.id, true])
end
end
context 'with a non-default author_key' do
let(:user) { { id: 4, login: 'kittens' } }
shared_examples 'user ID finder' do |author_key|
it 'returns the user ID for an object' do
expect(finder).to receive(:user_id_for).with(user).and_return(42)
expect(finder.author_id_for(issue_event, author_key: author_key)).to eq([42, true])
end
end
context 'when the author_key parameter is :actor' do
let(:issue_event) { { actor: user } }
it_behaves_like 'user ID finder', :actor
end
context 'when the author_key parameter is :assignee' do
let(:issue_event) { { assignee: user } }
it_behaves_like 'user ID finder', :assignee
end
context 'when the author_key parameter is :requested_reviewer' do
let(:issue_event) { { requested_reviewer: user } }
it_behaves_like 'user ID finder', :requested_reviewer
end
context 'when the author_key parameter is :review_requester' do
let(:issue_event) { { review_requester: user } }
it_behaves_like 'user ID finder', :review_requester
end
end
end
describe '#assignee_id_for' do
it 'returns the user ID for the assignee of an issuable' do
user = { id: 4, login: 'kittens' }
issue = { assignee: user }
expect(finder).to receive(:user_id_for).with(user).and_return(42)
expect(finder.assignee_id_for(issue)).to eq(42)
end
it 'returns nil if the issuable does not have an assignee' do
issue = { assignee: nil }
expect(finder).not_to receive(:user_id_for)
expect(finder.assignee_id_for(issue)).to be_nil
end
end
describe '#user_id_for' do
it 'returns the user ID for the given user' do
user = { id: 4, login: 'kittens' }
expect(finder).to receive(:find).with(user[:id], user[:login]).and_return(42)
expect(finder.user_id_for(user)).to eq(42)
end
it 'does not fail with empty input' do
expect(finder.user_id_for(nil)).to eq(nil)
end
end
describe '#find' do
let(:user) { create(:user) }
before do
allow(finder).to receive(:email_for_github_username)
.and_return(user.email)
end
context 'without a cache' do
before do
allow(finder).to receive(:find_from_cache).and_return([false, nil])
expect(finder).to receive(:find_id_from_database).and_call_original
end
it 'finds a GitLab user for a GitHub user ID' do
user.identities.create!(provider: :github, extern_uid: 42)
expect(finder.find(42, user.username)).to eq(user.id)
end
it 'finds a GitLab user for a GitHub Email address' do
expect(finder.find(42, user.username)).to eq(user.id)
end
end
context 'with a cache' do
it 'returns the cached user ID' do
expect(finder).to receive(:find_from_cache).and_return([true, user.id])
expect(finder).not_to receive(:find_id_from_database)
expect(finder.find(42, user.username)).to eq(user.id)
end
it 'does not query the database if the cache key exists but is empty' do
expect(finder).to receive(:find_from_cache).and_return([true, nil])
expect(finder).not_to receive(:find_id_from_database)
expect(finder.find(42, user.username)).to be_nil
end
end
end
describe '#find_from_cache' do
it 'retrieves a GitLab user ID for a GitHub user ID' do
expect(finder)
.to receive(:cached_id_for_github_id)
.with(42)
.and_return([true, 4])
expect(finder.find_from_cache(42)).to eq([true, 4])
end
it 'retrieves a GitLab user ID for a GitHub Email address' do
email = 'kittens@example.com'
expect(finder)
.to receive(:cached_id_for_github_id)
.with(42)
.and_return([false, nil])
expect(finder)
.to receive(:cached_id_for_github_email)
.with(email)
.and_return([true, 4])
expect(finder.find_from_cache(42, email)).to eq([true, 4])
end
it 'does not query the cache for an Email address when none is given' do
expect(finder)
.to receive(:cached_id_for_github_id)
.with(42)
.and_return([false, nil])
expect(finder).not_to receive(:cached_id_for_github_id)
expect(finder.find_from_cache(42)).to eq([false])
end
end
describe '#find_id_from_database' do
let(:user) { create(:user) }
it 'returns the GitLab user ID for a GitHub user ID' do
user.identities.create!(provider: :github, extern_uid: 42)
expect(finder.find_id_from_database(42, user.email)).to eq(user.id)
end
it 'returns the GitLab user ID for a GitHub Email address' do
expect(finder.find_id_from_database(42, user.email)).to eq(user.id)
end
end
describe '#email_for_github_username' do
let(:email) { 'kittens@example.com' }
context 'when an Email address is cached' do
it 'reads the Email address from the cache' do
expect(Gitlab::Cache::Import::Caching)
.to receive(:read)
.and_return(email)
expect(client).not_to receive(:user)
expect(finder.email_for_github_username('kittens')).to eq(email)
end
end
context 'when an Email address is not cached' do
let(:user) { { email: email } }
it 'retrieves the Email address from the GitHub API' do
expect(client).to receive(:user).with('kittens').and_return(user)
expect(finder.email_for_github_username('kittens')).to eq(email)
end
it 'caches the Email address when an Email address is available' do
expect(client).to receive(:user).with('kittens').and_return(user)
expect(Gitlab::Cache::Import::Caching)
.to receive(:write)
.with(an_instance_of(String), email, timeout: Gitlab::Cache::Import::Caching::TIMEOUT)
finder.email_for_github_username('kittens')
end
it 'returns nil if the user does not exist' do
expect(client)
.to receive(:user)
.with('kittens')
.and_return(nil)
expect(Gitlab::Cache::Import::Caching)
.not_to receive(:write)
expect(finder.email_for_github_username('kittens')).to be_nil
end
it 'shortens the timeout for Email address in cache when an Email address is private/nil from GitHub' do
user = { email: nil }
expect(client).to receive(:user).with('kittens').and_return(user)
expect(Gitlab::Cache::Import::Caching)
.to receive(:write).with(an_instance_of(String), nil, timeout: Gitlab::Cache::Import::Caching::SHORTER_TIMEOUT)
expect(finder.email_for_github_username('kittens')).to be_nil
end
end
end
describe '#cached_id_for_github_id' do
let(:id) { 4 }
it 'reads a user ID from the cache' do
Gitlab::Cache::Import::Caching
.write(described_class::ID_CACHE_KEY % id, 4)
expect(finder.cached_id_for_github_id(id)).to eq([true, 4])
end
it 'reads a non existing cache key' do
expect(finder.cached_id_for_github_id(id)).to eq([false, nil])
end
end
describe '#cached_id_for_github_email' do
let(:email) { 'kittens@example.com' }
it 'reads a user ID from the cache' do
Gitlab::Cache::Import::Caching
.write(described_class::ID_FOR_EMAIL_CACHE_KEY % email, 4)
expect(finder.cached_id_for_github_email(email)).to eq([true, 4])
end
it 'reads a non existing cache key' do
expect(finder.cached_id_for_github_email(email)).to eq([false, nil])
end
end
describe '#id_for_github_id' do
let(:id) { 4 }
it 'queries and caches the user ID for a given GitHub ID' do
expect(finder).to receive(:query_id_for_github_id)
.with(id)
.and_return(42)
expect(Gitlab::Cache::Import::Caching)
.to receive(:write)
.with(described_class::ID_CACHE_KEY % id, 42)
finder.id_for_github_id(id)
end
it 'caches a nil value if no ID could be found' do
expect(finder).to receive(:query_id_for_github_id)
.with(id)
.and_return(nil)
expect(Gitlab::Cache::Import::Caching)
.to receive(:write)
.with(described_class::ID_CACHE_KEY % id, nil)
finder.id_for_github_id(id)
end
context 'when importing from github enterprise' do
let(:project) do
create(
:project,
import_type: 'github',
import_url: 'https://othergithub.net/user/repo'
)
end
it 'does not look up the user by external id' do
expect(finder).not_to receive(:query_id_for_github_id)
expect(Gitlab::Cache::Import::Caching)
.to receive(:write)
.with(described_class::ID_CACHE_KEY % id, nil)
finder.id_for_github_id(id)
end
end
end
describe '#id_for_github_email' do
let(:email) { 'kittens@example.com' }
it 'queries and caches the user ID for a given Email address' do
expect(finder).to receive(:query_id_for_github_email)
.with(email)
.and_return(42)
expect(Gitlab::Cache::Import::Caching)
.to receive(:write)
.with(described_class::ID_FOR_EMAIL_CACHE_KEY % email, 42)
finder.id_for_github_email(email)
end
it 'caches a nil value if no ID could be found' do
expect(finder).to receive(:query_id_for_github_email)
.with(email)
.and_return(nil)
expect(Gitlab::Cache::Import::Caching)
.to receive(:write)
.with(described_class::ID_FOR_EMAIL_CACHE_KEY % email, nil)
finder.id_for_github_email(email)
end
end
describe '#query_id_for_github_id' do
it 'returns the ID of the user for the given GitHub user ID' do
user = create(:user)
user.identities.create!(provider: :github, extern_uid: '42')
expect(finder.query_id_for_github_id(42)).to eq(user.id)
end
it 'returns nil when no user ID could be found' do
expect(finder.query_id_for_github_id(42)).to be_nil
end
end
describe '#query_id_for_github_email' do
it 'returns the ID of the user for the given Email address' do
user = create(:user, email: 'kittens@example.com')
expect(finder.query_id_for_github_email(user.email)).to eq(user.id)
end
it 'returns nil if no user ID could be found' do
expect(finder.query_id_for_github_email('kittens@example.com')).to be_nil
end
end
describe '#read_id_from_cache' do
it 'reads an ID from the cache' do
Gitlab::Cache::Import::Caching.write('foo', 10)
expect(finder.read_id_from_cache('foo')).to eq([true, 10])
end
it 'reads a cache key with an empty value' do
Gitlab::Cache::Import::Caching.write('foo', nil)
expect(finder.read_id_from_cache('foo')).to eq([true, nil])
end
it 'reads a cache key that does not exist' do
expect(finder.read_id_from_cache('foo')).to eq([false, nil])
end
end
end
|