|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Unit; |
| 4 | + |
| 5 | +use Mockery; |
| 6 | +use Carbon\Carbon; |
| 7 | +use Tests\TestCase; |
| 8 | +use Tests\Fixtures\User; |
| 9 | +use Illuminate\Support\Str; |
| 10 | +use SocialiteProviders\GitLab\Provider; |
| 11 | +use Tests\Fixtures\Concern\UseTestFixtures; |
| 12 | +use Illuminate\Auth\AuthenticationException; |
| 13 | +use Oneofftech\Identities\Facades\IdentityCrypt; |
| 14 | +use Illuminate\Foundation\Testing\RefreshDatabase; |
| 15 | +use SocialiteProviders\Manager\OAuth2\User as OauthUser; |
| 16 | +use Oneofftech\Identities\Facades\Identity as IdentityFacade; |
| 17 | + |
| 18 | +class ConnectControllerTest extends TestCase |
| 19 | +{ |
| 20 | + use RefreshDatabase, UseTestFixtures; |
| 21 | + |
| 22 | + public function test_redirect_to_provider_require_authentication() |
| 23 | + { |
| 24 | + IdentityFacade::useNamespace('Tests\\Fixtures'); |
| 25 | + IdentityFacade::routes(); |
| 26 | + |
| 27 | + $router = $this->app->make('router'); |
| 28 | + |
| 29 | + $router->get('login', function () { |
| 30 | + })->name('login'); |
| 31 | + |
| 32 | + $this->withoutExceptionHandling(); |
| 33 | + |
| 34 | + $this->expectException(AuthenticationException::class); |
| 35 | + $this->expectExceptionMessage('Unauthenticated'); |
| 36 | + |
| 37 | + $this->get(route('oneofftech::connect.provider', ['provider' => 'gitlab'])); |
| 38 | + } |
| 39 | + |
| 40 | + public function test_redirect_to_provider() |
| 41 | + { |
| 42 | + IdentityFacade::useNamespace('Tests\\Fixtures'); |
| 43 | + IdentityFacade::routes(); |
| 44 | + |
| 45 | + $user = tap((new User()), function ($u) { |
| 46 | + $u->forceFill([ |
| 47 | + |
| 48 | + 'name' => 'User', |
| 49 | + 'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', |
| 50 | + 'remember_token' => Str::random(10), |
| 51 | + ])->save(); |
| 52 | + }); |
| 53 | + |
| 54 | + $response = $this->actingAs($user) |
| 55 | + ->get(route('oneofftech::connect.provider', ['provider' => 'gitlab'])); |
| 56 | + |
| 57 | + $response->assertRedirect(); |
| 58 | + |
| 59 | + $location = urldecode($response->headers->get('Location')); |
| 60 | + |
| 61 | + $this->assertStringContainsString('gitlab.com', $location); |
| 62 | + $this->assertStringContainsString(route('oneofftech::connect.callback', ['provider' => 'gitlab']), $location); |
| 63 | + } |
| 64 | + |
| 65 | + public function test_connect_creates_identity() |
| 66 | + { |
| 67 | + $this->useTestFixtures(); |
| 68 | + |
| 69 | + $user = $this->createUser(); |
| 70 | + |
| 71 | + $this->withoutExceptionHandling(); |
| 72 | + |
| 73 | + $driverMock = Mockery::mock(Provider::class)->makePartial(); |
| 74 | + |
| 75 | + Carbon::setTestNow(Carbon::create(2020, 11, 12, 10, 20)); |
| 76 | + |
| 77 | + $oauthFakeUser = (new OauthUser())->map([ |
| 78 | + 'id' => 'U1', |
| 79 | + 'nickname' => 'User', |
| 80 | + 'name' => 'User', |
| 81 | + |
| 82 | + 'avatar' => 'https://gitlab.com', |
| 83 | + 'token' => 'T2', |
| 84 | + 'refreshToken' => 'RT2', |
| 85 | + 'expiresIn' => Carbon::SECONDS_PER_MINUTE, |
| 86 | + ]); |
| 87 | + |
| 88 | + $driverMock->shouldReceive('user')->andReturn($oauthFakeUser); |
| 89 | + |
| 90 | + $driverMock->shouldReceive('redirectUrl')->andReturn($driverMock); |
| 91 | + |
| 92 | + IdentityFacade::shouldReceive('driver')->with('gitlab')->andReturn($driverMock); |
| 93 | + |
| 94 | + $response = $this->actingAs($user) |
| 95 | + ->get(route('oneofftech::connect.callback', ['provider' => 'gitlab'])); |
| 96 | + |
| 97 | + $response->assertRedirect('http://localhost/home'); |
| 98 | + |
| 99 | + $updatedIdentity = $user->identities->first(); |
| 100 | + |
| 101 | + $this->assertEquals(IdentityCrypt::hash('U1'), $updatedIdentity->provider_id); |
| 102 | + $this->assertEquals('gitlab', $updatedIdentity->provider); |
| 103 | + $this->assertEquals(Carbon::create(2020, 11, 12, 10, 21), $updatedIdentity->expires_at); |
| 104 | + $this->assertEquals('T2', IdentityCrypt::decryptString($updatedIdentity->token)); |
| 105 | + $this->assertEquals('RT2', IdentityCrypt::decryptString($updatedIdentity->refresh_token)); |
| 106 | + } |
| 107 | + |
| 108 | + public function test_connect_updates_existing_identity() |
| 109 | + { |
| 110 | + $this->useTestFixtures(); |
| 111 | + |
| 112 | + $user = $this->createUser(); |
| 113 | + |
| 114 | + $identity = $user->identities()->create([ |
| 115 | + 'provider'=> 'gitlab', |
| 116 | + 'provider_id'=> IdentityCrypt::hash('U1'), |
| 117 | + 'token'=> 'T1', |
| 118 | + 'refresh_token'=> null, |
| 119 | + 'expires_at'=> null, |
| 120 | + 'registration' => true, |
| 121 | + ]); |
| 122 | + |
| 123 | + $this->withoutExceptionHandling(); |
| 124 | + |
| 125 | + $driverMock = Mockery::mock(Provider::class)->makePartial(); |
| 126 | + |
| 127 | + Carbon::setTestNow(Carbon::create(2020, 11, 12, 10, 20)); |
| 128 | + |
| 129 | + $oauthFakeUser = (new OauthUser())->map([ |
| 130 | + 'id' => 'U1', |
| 131 | + 'nickname' => 'User', |
| 132 | + 'name' => 'User', |
| 133 | + |
| 134 | + 'avatar' => 'https://gitlab.com', |
| 135 | + 'token' => 'T2', |
| 136 | + 'refreshToken' => 'RT2', |
| 137 | + 'expiresIn' => Carbon::SECONDS_PER_MINUTE, |
| 138 | + ]); |
| 139 | + |
| 140 | + $driverMock->shouldReceive('user')->andReturn($oauthFakeUser); |
| 141 | + |
| 142 | + $driverMock->shouldReceive('redirectUrl')->andReturn($driverMock); |
| 143 | + |
| 144 | + IdentityFacade::shouldReceive('driver')->with('gitlab')->andReturn($driverMock); |
| 145 | + |
| 146 | + $response = $this->actingAs($user) |
| 147 | + ->get(route('oneofftech::connect.callback', ['provider' => 'gitlab'])); |
| 148 | + |
| 149 | + $response->assertRedirect('http://localhost/home'); |
| 150 | + |
| 151 | + $updatedIdentity = $user->identities->first(); |
| 152 | + |
| 153 | + $this->assertEquals($identity->provider_id, $updatedIdentity->provider_id); |
| 154 | + $this->assertEquals('gitlab', $updatedIdentity->provider); |
| 155 | + $this->assertEquals(Carbon::create(2020, 11, 12, 10, 21), $updatedIdentity->expires_at); |
| 156 | + $this->assertEquals('T2', IdentityCrypt::decryptString($updatedIdentity->token)); |
| 157 | + $this->assertEquals('RT2', IdentityCrypt::decryptString($updatedIdentity->refresh_token)); |
| 158 | + } |
| 159 | +} |
0 commit comments