|
| 1 | +<?php |
| 2 | + |
| 3 | +use App\Jobs\UpdateUserIdenticonStatus; |
| 4 | +use App\Models\User; |
| 5 | +use Illuminate\Foundation\Testing\RefreshDatabase; |
| 6 | +use Illuminate\Support\Facades\Queue; |
| 7 | +use Laravel\Socialite\Contracts\Provider; |
| 8 | +use Laravel\Socialite\Facades\Socialite; |
| 9 | +use Laravel\Socialite\Two\User as SocialiteUser; |
| 10 | +use Tests\TestCase; |
| 11 | + |
| 12 | +uses(TestCase::class); |
| 13 | +uses(RefreshDatabase::class); |
| 14 | + |
| 15 | +test('users can start connecting their GitHub account from settings', function () { |
| 16 | + $user = $this->login(); |
| 17 | + |
| 18 | + $response = $this->actingAs($user)->post('/settings/github/connect'); |
| 19 | + |
| 20 | + $response->assertRedirect(route('login.github')); |
| 21 | + |
| 22 | + expect(session('settings.github.connect.intended'))->toBeTrue(); |
| 23 | +}); |
| 24 | + |
| 25 | +test('users can disconnect their GitHub account from settings', function () { |
| 26 | + $user = $this->login([ |
| 27 | + 'github_id' => '11405387', |
| 28 | + 'github_username' => 'theHocineSaad', |
| 29 | + 'github_has_identicon' => true, |
| 30 | + ]); |
| 31 | + |
| 32 | + $response = $this->actingAs($user)->post('/settings/github/disconnect'); |
| 33 | + |
| 34 | + $response->assertRedirect(route('settings.profile')); |
| 35 | + $response->assertSessionHas('success', 'Your GitHub account has been disconnected.'); |
| 36 | + |
| 37 | + $user->refresh(); |
| 38 | + |
| 39 | + expect($user->github_id)->toBeNull(); |
| 40 | + expect($user->github_username)->toBeNull(); |
| 41 | + expect($user->github_has_identicon)->toBeFalse(); |
| 42 | +}); |
| 43 | + |
| 44 | +test('users can connect their GitHub account after returning from GitHub', function () { |
| 45 | + Queue::fake(); |
| 46 | + |
| 47 | + $user = $this->login([ |
| 48 | + 'github_id' => null, |
| 49 | + 'github_username' => null, |
| 50 | + ]); |
| 51 | + |
| 52 | + $socialiteUser = fakeSocialiteUser('11405387', 'theHocineSaad'); |
| 53 | + |
| 54 | + mockGitHubProvider($socialiteUser); |
| 55 | + |
| 56 | + $this->withSession(['settings.github.connect.intended' => true]); |
| 57 | + |
| 58 | + $response = $this->actingAs($user)->get('/auth/github'); |
| 59 | + |
| 60 | + $response->assertRedirect(route('settings.profile')); |
| 61 | + $response->assertSessionHas('success', 'Your GitHub account has been connected.'); |
| 62 | + |
| 63 | + $user->refresh(); |
| 64 | + |
| 65 | + expect($user->github_id)->toBe('11405387'); |
| 66 | + expect($user->github_username)->toBe('theHocineSaad'); |
| 67 | + |
| 68 | + Queue::assertPushed(UpdateUserIdenticonStatus::class); |
| 69 | +}); |
| 70 | + |
| 71 | +test('users cannot connect a GitHub account that belongs to another user', function () { |
| 72 | + Queue::fake(); |
| 73 | + |
| 74 | + User::factory()->create([ |
| 75 | + 'github_id' => '11405387', |
| 76 | + 'github_username' => 'theHocineSaad', |
| 77 | + ]); |
| 78 | + |
| 79 | + $user = $this->login([ |
| 80 | + 'github_id' => null, |
| 81 | + 'github_username' => null, |
| 82 | + ]); |
| 83 | + |
| 84 | + $socialiteUser = fakeSocialiteUser('11405387', 'theHocineSaad'); |
| 85 | + |
| 86 | + mockGitHubProvider($socialiteUser); |
| 87 | + |
| 88 | + $this->withSession(['settings.github.connect.intended' => true]); |
| 89 | + |
| 90 | + $response = $this->actingAs($user)->get('/auth/github'); |
| 91 | + |
| 92 | + $response->assertRedirect(route('settings.profile')); |
| 93 | + $response->assertSessionHas('error', 'This GitHub account is already connected to another user.'); |
| 94 | + |
| 95 | + $user->refresh(); |
| 96 | + |
| 97 | + expect($user->github_id)->toBeNull(); |
| 98 | + expect($user->github_username)->toBeNull(); |
| 99 | + |
| 100 | + Queue::assertNothingPushed(); |
| 101 | +}); |
| 102 | + |
| 103 | +function fakeSocialiteUser(string $id, string $nickname): SocialiteUser |
| 104 | +{ |
| 105 | + return tap(new SocialiteUser()) |
| 106 | + ->setRaw([ |
| 107 | + 'id' => $id, |
| 108 | + 'login' => $nickname, |
| 109 | + ]) |
| 110 | + ->map([ |
| 111 | + 'id' => $id, |
| 112 | + 'nickname' => $nickname, |
| 113 | + ]); |
| 114 | +} |
| 115 | + |
| 116 | +function mockGitHubProvider(SocialiteUser $user): void |
| 117 | +{ |
| 118 | + $provider = Mockery::mock(Provider::class); |
| 119 | + $provider->shouldReceive('user')->once()->andReturn($user); |
| 120 | + |
| 121 | + Socialite::shouldReceive('driver')->once()->with('github')->andReturn($provider); |
| 122 | +} |
0 commit comments