-
-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
227 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
namespace App\Exceptions; | ||
|
||
class InvalidAccessTokenException extends \RuntimeException | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
tests/Feature/Controllers/AuthController/HandleProviderCallbackTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use App\Models\User; | ||
use App\Providers\RouteServiceProvider; | ||
use Laravel\Socialite\Facades\Socialite; | ||
|
||
it('creates a new user if the user doesn\'t exist and logs them in', function () { | ||
mockSocialiteFacade(); | ||
|
||
$this->assertDatabaseMissing(User::class, [ | ||
'github_id' => 1234567890, | ||
]); | ||
|
||
session()->put('auth_scope', 'read:user'); | ||
|
||
$this->get('/auth/github/callback')->assertRedirect(RouteServiceProvider::HOME); | ||
|
||
$this->assertAuthenticated(); | ||
|
||
$this->assertDatabaseHas(User::class, [ | ||
'github_id' => 1234567890, | ||
'username' => 'JaneDoe', | ||
'name' => 'Jane Doe', | ||
'avatar' => 'https://en.gravatar.com/userimage', | ||
'scope' => 'read:user', | ||
]); | ||
}); | ||
|
||
it('updates the user\'s info and logins them in if they already exist', function () { | ||
mockSocialiteFacade(); | ||
|
||
$user = User::factory()->create([ | ||
'github_id' => 1234567890, | ||
'username' => 'OldUsername', | ||
'name' => 'Old Name', | ||
'avatar' => 'https://old.gravatar.com/userimage', | ||
'scope' => 'read:user', | ||
]); | ||
|
||
session()->put('auth_scope', 'read:user'); | ||
|
||
$this->get('/auth/github/callback')->assertRedirect(RouteServiceProvider::HOME); | ||
|
||
$this->assertAuthenticated(); | ||
|
||
$this->assertDatabaseHas(User::class, [ | ||
'github_id' => 1234567890, | ||
'username' => 'JaneDoe', | ||
'name' => 'Jane Doe', | ||
'avatar' => 'https://en.gravatar.com/userimage', | ||
'scope' => 'read:user', | ||
]); | ||
|
||
expect(User::count())->toBe(1); | ||
}); | ||
|
||
it('redirects authenticated users back to the dashboard') | ||
->login() | ||
->get('/auth/github/callback') | ||
->assertRedirect(RouteServiceProvider::HOME); | ||
|
||
// Helpers | ||
function mockSocialiteFacade() | ||
{ | ||
$abstractUser = Mockery::mock(Laravel\Socialite\Two\User::class); | ||
$abstractUser->shouldReceive('getId') | ||
->andReturn(1234567890) | ||
->shouldReceive('getNickname') | ||
->andReturn('JaneDoe') | ||
->shouldReceive('getName') | ||
->andReturn('Jane Doe') | ||
->shouldReceive('getAvatar') | ||
->andReturn('https://en.gravatar.com/userimage'); | ||
$abstractUser->token = 'abcde12345'; | ||
|
||
$provider = Mockery::mock(Laravel\Socialite\Contracts\Provider::class); | ||
$provider->shouldReceive('user')->andReturn($abstractUser); | ||
|
||
Socialite::shouldReceive('driver')->with('github')->andReturn($provider); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
it('logs out an authenticated user', function() { | ||
$this->login() | ||
->get('/logout') | ||
->assertRedirect(route('auth.show')); | ||
|
||
$this->assertGuest(); | ||
}); | ||
|
||
it('redirects guest users back to the login page') | ||
->get('/logout') | ||
->assertRedirect('/login'); |
37 changes: 37 additions & 0 deletions
37
tests/Feature/Controllers/AuthController/RedirectToProviderTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use App\Providers\RouteServiceProvider; | ||
|
||
it('validates the scope if present', function (array $badData, array|string $errors) { | ||
$this | ||
->get(route('github.auth', $badData)) | ||
->assertInvalid($errors); | ||
})->with([ | ||
[['scope' => 'admin:org'], 'scope'], | ||
[['scope' => 'repo'], 'scope'], | ||
[['scope' => 'user'], 'scope'], | ||
]); | ||
|
||
it('stores a valid scope in the current session', function (string $scope) { | ||
$this | ||
->get(route('github.auth', ['scope' => $scope])) | ||
->assertSessionHas('auth_scope', $scope); | ||
})->with(['read:user', 'public_repo']); | ||
|
||
it('defaults to the `read:user` scope if no scope is provided', function () { | ||
$this | ||
->get(route('github.auth', ['scope' => null])) | ||
->assertSessionHas('auth_scope', 'read:user'); | ||
}); | ||
|
||
it('redirects to the auth provider when a valid scope is present', function (?string $scope) { | ||
// TODO: Can we perform some assertions on what we passed to Socialite? | ||
$this->get(route('github.auth', ['scope' => $scope]))->assertRedirect(); | ||
})->with(['read:user', 'public_repo', null]); | ||
|
||
it('redirects authenticated users back to the dashboard') | ||
->login() | ||
->get('/auth/github') | ||
->assertRedirect(RouteServiceProvider::HOME); |
36 changes: 36 additions & 0 deletions
36
tests/Feature/Controllers/AuthController/RevokeGrantTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Illuminate\Support\Facades\Http; | ||
use App\Exceptions\InvalidAccessTokenException; | ||
|
||
it('sends an API reqest to GitHub to revoke the user\'s access token', function () { | ||
Http::fake([ | ||
'api.github.com/*' => Http::response('ok', 200), | ||
]); | ||
|
||
$this | ||
->login() | ||
->post('/revoke-grant')->assertRedirect(route('auth.destroy')); | ||
|
||
expect(auth()->user()->access_token)->toBeNull(); | ||
}); | ||
|
||
it('throws an InvalidAccessTokenException if the api request comes back with a 404', function () { | ||
$this->withoutExceptionHandling(); | ||
|
||
Http::fake([ | ||
'api.github.com/*' => Http::response('not-found', 404), | ||
]); | ||
|
||
$this | ||
->login() | ||
->post('/revoke-grant'); | ||
|
||
expect(auth()->user()->access_token)->not->toBeNull(); | ||
})->throws(InvalidAccessTokenException::class); | ||
|
||
it('redirects guest users back to the login page') | ||
->post('/revoke-grant') | ||
->assertRedirect('/login'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use App\Providers\RouteServiceProvider; | ||
|
||
it('renders the login page for unauthenticated users') | ||
->get('/auth') | ||
->assertStatus(200) | ||
->assertHybridView('auth'); | ||
|
||
it('redirects authenticated users back to the dashboard') | ||
->login() | ||
->get('/auth') | ||
->assertRedirect(RouteServiceProvider::HOME); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters