Welcome to mirror list, hosted at ThFree Co, Russian Federation.

user_finder_spec.rb « github_import « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 998fa8b2c9fb56feed0dffd387065dd87973d989 (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
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::GithubImport::UserFinder, :clean_gitlab_redis_cache, feature_category: :importers 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([Users::Internal.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([Users::Internal.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' }
    let(:username) { 'kittens' }
    let(:user) { {} }
    let(:etag) { 'etag' }
    let(:lease_name) { "gitlab:github_import:user_finder:#{project.id}" }
    let(:cache_key) { described_class::EMAIL_FOR_USERNAME_CACHE_KEY % username }
    let(:etag_cache_key) { described_class::USERNAME_ETAG_CACHE_KEY % username }
    let(:email_fetched_for_project_key) do
      format(described_class::EMAIL_FETCHED_FOR_PROJECT_CACHE_KEY, project: project.id, username: username)
    end

    subject(:email_for_github_username) { finder.email_for_github_username(username) }

    shared_examples 'returns and caches the email' do
      it 'returns the email' do
        expect(email_for_github_username).to eq(email)
      end

      it 'caches the email and expires the etag and project check caches' do
        expect(Gitlab::Cache::Import::Caching).to receive(:write).with(cache_key, email).once
        expect(Gitlab::Cache::Import::Caching).to receive(:expire).with(etag_cache_key, 0).once
        expect(Gitlab::Cache::Import::Caching).to receive(:expire).with(email_fetched_for_project_key, 0).once

        email_for_github_username
        email_for_github_username
      end
    end

    shared_examples 'returns nil and caches a negative lookup' do
      it 'returns nil' do
        expect(email_for_github_username).to be_nil
      end

      it 'caches a blank email and marks the project as checked' do
        expect(Gitlab::Cache::Import::Caching).to receive(:write).with(cache_key, '').once
        expect(Gitlab::Cache::Import::Caching).not_to receive(:write).with(etag_cache_key, anything)
        expect(Gitlab::Cache::Import::Caching).to receive(:write).with(email_fetched_for_project_key, 1).once

        email_for_github_username
        email_for_github_username
      end
    end

    shared_examples 'does not change caches' do
      it 'does not write to any of the caches' do
        expect(Gitlab::Cache::Import::Caching).not_to receive(:write).with(cache_key, anything)
        expect(Gitlab::Cache::Import::Caching).not_to receive(:write).with(etag_cache_key, anything)
        expect(Gitlab::Cache::Import::Caching).not_to receive(:write).with(email_fetched_for_project_key, anything)

        email_for_github_username
        email_for_github_username
      end
    end

    shared_examples 'a user resource not found on GitHub' do
      before do
        allow(client).to receive(:user).and_raise(::Octokit::NotFound)
      end

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

      it 'caches a blank email' do
        expect(Gitlab::Cache::Import::Caching).to receive(:write).with(cache_key, '').once
        expect(Gitlab::Cache::Import::Caching).not_to receive(:write).with(etag_cache_key, anything)
        expect(Gitlab::Cache::Import::Caching).not_to receive(:write).with(email_fetched_for_project_key, anything)

        email_for_github_username
        email_for_github_username
      end
    end

    context 'when the email is cached' do
      before do
        Gitlab::Cache::Import::Caching.write(cache_key, email)
      end

      it 'returns the email from the cache' do
        expect(email_for_github_username).to eq(email)
      end

      it 'does not make a rate-limited API call' do
        expect(client).not_to receive(:user).with(username, { headers: {} })

        email_for_github_username
        email_for_github_username
      end
    end

    context 'when the email cache is nil' do
      context 'if the email has not been checked for the project' do
        context 'if the cached etag is nil' do
          before do
            allow(client).to receive_message_chain(:octokit, :last_response, :headers).and_return({ etag: etag })
          end

          it 'makes an API call' do
            expect(client).to receive(:user).with(username, { headers: {} }).and_return({ email: email }).once
            expect(finder).to receive(:in_lock).with(
              lease_name, ttl: 3.minutes, sleep_sec: 1.second, retries: 30
            ).and_call_original

            email_for_github_username
          end

          context 'if the response contains an email' do
            before do
              allow(client).to receive(:user).and_return({ email: email })
            end

            it_behaves_like 'returns and caches the email'

            context 'when retried' do
              before do
                allow(finder).to receive(:in_lock).and_yield(true)
              end

              it_behaves_like 'returns and caches the email'
            end
          end

          context 'if the response does not contain an email' do
            before do
              allow(client).to receive(:user).and_return({})
            end

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

            it 'caches a blank email and etag and marks the project as checked' do
              expect(Gitlab::Cache::Import::Caching).to receive(:write).with(cache_key, '').once
              expect(Gitlab::Cache::Import::Caching).to receive(:write).with(etag_cache_key, etag).once
              expect(Gitlab::Cache::Import::Caching).to receive(:write).with(email_fetched_for_project_key, 1).once

              email_for_github_username
              email_for_github_username
            end
          end
        end

        context 'if the cached etag is not nil' do
          before do
            Gitlab::Cache::Import::Caching.write(etag_cache_key, etag)
          end

          it 'makes a non-rate-limited API call' do
            expect(client).to receive(:user).with(username, { headers: { 'If-None-Match' => etag } }).once
            expect(finder).to receive(:in_lock).with(
              lease_name, ttl: 3.minutes, sleep_sec: 1.second, retries: 30
            ).and_call_original

            email_for_github_username
          end

          context 'if the response contains an email' do
            before do
              allow(client).to receive(:user).and_return({ email: email })
            end

            it_behaves_like 'returns and caches the email'
          end

          context 'if the response does not contain an email' do
            before do
              allow(client).to receive(:user).and_return({})
            end

            it_behaves_like 'returns nil and caches a negative lookup'
          end

          context 'if the response is nil' do
            before do
              allow(client).to receive(:user).and_return(nil)
            end

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

            it 'marks the project as checked' do
              expect(Gitlab::Cache::Import::Caching).not_to receive(:write).with(cache_key, anything)
              expect(Gitlab::Cache::Import::Caching).not_to receive(:write).with(etag_cache_key, anything)
              expect(Gitlab::Cache::Import::Caching).to receive(:write).with(email_fetched_for_project_key, 1).once

              email_for_github_username
              email_for_github_username
            end
          end
        end
      end

      context 'if the email has been checked for the project' do
        before do
          Gitlab::Cache::Import::Caching.write(email_fetched_for_project_key, 1)
        end

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

        it_behaves_like 'does not change caches'
      end

      it_behaves_like 'a user resource not found on GitHub'
    end

    context 'when the email cache is blank' do
      before do
        Gitlab::Cache::Import::Caching.write(cache_key, '')
      end

      context 'if the email has not been checked for the project' do
        context 'if the cached etag is not nil' do
          before do
            Gitlab::Cache::Import::Caching.write(etag_cache_key, etag)
          end

          it 'makes a non-rate-limited API call' do
            expect(client).to receive(:user).with(username, { headers: { 'If-None-Match' => etag } }).once
            expect(finder).to receive(:in_lock).with(
              lease_name, ttl: 3.minutes, sleep_sec: 1.second, retries: 30
            ).and_call_original

            email_for_github_username
          end

          context 'if the response contains an email' do
            before do
              allow(client).to receive(:user).and_return({ email: email })
            end

            it_behaves_like 'returns and caches the email'
          end

          context 'if the response does not contain an email' do
            before do
              allow(client).to receive(:user).and_return({})
            end

            it_behaves_like 'returns nil and caches a negative lookup'
          end

          context 'if the response is nil' do
            before do
              allow(client).to receive(:user).and_return(nil)
            end

            it_behaves_like 'returns nil and caches a negative lookup'
          end

          it_behaves_like 'a user resource not found on GitHub'
        end

        context 'if the cached etag is nil' do
          context 'when lock was executed by another process and an email was fetched' do
            it 'does not fetch user detail' do
              expect(finder).to receive(:read_email_from_cache).ordered.and_return('')
              expect(finder).to receive(:read_email_from_cache).ordered.and_return(email)
              expect(finder).to receive(:in_lock).and_yield(true)
              expect(client).not_to receive(:user)

              email_for_github_username
            end
          end

          context 'when lock was executed by another process and an email in cache is still blank' do
            it 'fetch user detail' do
              expect(finder).to receive(:read_email_from_cache).ordered.and_return('')
              expect(finder).to receive(:read_email_from_cache).ordered.and_return('')
              expect(finder).to receive(:in_lock).and_yield(true)
              expect(client).to receive(:user).with(username, { headers: {} }).and_return({ email: email }).once

              email_for_github_username
            end
          end
        end
      end

      context 'if the email has been checked for the project' do
        before do
          Gitlab::Cache::Import::Caching.write(email_fetched_for_project_key, 1)
        end

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

        it_behaves_like 'does not change caches'
      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