-
-
Notifications
You must be signed in to change notification settings - Fork 0
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
11 changed files
with
303 additions
and
21 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
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
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Carbon\Carbon; | ||
use Illuminate\Contracts\Auth\Guard; | ||
use Illuminate\Support\Facades\Auth; | ||
use Illuminate\Support\Facades\Config; | ||
use Illuminate\Support\Facades\Route; | ||
use Illuminate\Support\Facades\Session; | ||
use League\OAuth2\Client\Token\AccessToken; | ||
use Raiolanetworks\OAuth\Controllers\OAuthController; | ||
use Raiolanetworks\OAuth\Services\OAuthService; | ||
use Raiolanetworks\OAuth\Tests\Models\TestUser; | ||
|
||
it('redirect authenticated users to the homepage', function () { | ||
$mockGuard = Mockery::mock(Guard::class); | ||
$mockGuard->shouldReceive('check')->andReturnTrue(); | ||
|
||
Auth::shouldReceive('guard') | ||
->with(config('oauth.guard_name')) | ||
->andReturn($mockGuard); | ||
|
||
$response = $this->get(route('oauth.request')); | ||
$response->assertRedirect('/'); | ||
}); | ||
|
||
it('redirects unauthenticated users to the OAuth provider', function () { | ||
$mockGuard = Mockery::mock(Guard::class); | ||
$mockGuard->shouldReceive('check')->andReturnFalse(); | ||
|
||
Auth::shouldReceive('guard') | ||
->with(config('oauth.guard_name')) | ||
->andReturn($mockGuard); | ||
|
||
$mockProvider = Mockery::mock(OAuthService::class); | ||
$mockProvider->shouldReceive('getAuthorizationUrl') | ||
->with(['prompt' => 'consent']) | ||
->andReturn('https://example.com/oauth/authorize'); | ||
|
||
$mockProvider->shouldReceive('getState') | ||
->andReturn('fake_state'); | ||
|
||
$mockProvider->shouldReceive('getPkceCode') | ||
->andReturn('fake_pkce_code'); | ||
|
||
$this->instance(OAuthService::class, $mockProvider); | ||
|
||
$response = $this->get(route('oauth.request')); | ||
|
||
$this->assertEquals('fake_state', Session::get('oauth2-state')); | ||
$this->assertEquals('fake_pkce_code', Session::get('oauth2-pkceCode')); | ||
|
||
$response->assertRedirect('https://example.com/oauth/authorize'); | ||
}); | ||
|
||
it('redirect where the user intends to go if authenticated in the callback', function () { | ||
$mockGuard = Mockery::mock(Guard::class); | ||
$mockGuard->shouldReceive('check')->andReturnTrue(); | ||
|
||
Auth::shouldReceive('guard') | ||
->with(config('oauth.guard_name')) | ||
->andReturn($mockGuard); | ||
|
||
$response = $this->get(route('oauth.callback')); | ||
$response->assertRedirect('/'); | ||
}); | ||
|
||
it('handles invalid or missing code in callback', function () { | ||
// Temporal route | ||
Route::get('/login', function () { | ||
return 'Login Page'; | ||
})->name(config('oauth.login_route')); | ||
|
||
Session::put('oauth2-state', 'correct_state'); | ||
|
||
$response = $this->get(route('oauth.callback'), [ | ||
'state' => 'correct_state', | ||
]); | ||
|
||
$response->assertRedirect(route(config('oauth.login_route'))) | ||
->assertSessionHas('message', 'Authentication failed. Please try again.'); | ||
}); | ||
|
||
it('handles invalid state in callback', function () { | ||
// Temporal route | ||
Route::get('/login', function () { | ||
return 'Login Page'; | ||
})->name(config('oauth.login_route')); | ||
|
||
$response = $this->get(route('oauth.callback', [ | ||
'code' => 'valid_code', | ||
])); | ||
|
||
$response->assertRedirect(route(config('oauth.login_route'))) | ||
->assertSessionHas('message', 'Authentication failed. Please try again.'); | ||
}); | ||
|
||
it('logs in the user after a successful OAuth callback', function () { | ||
// Temporal route | ||
Route::get('/login', function () { | ||
return 'Login Page'; | ||
})->name(config('oauth.login_route')); | ||
|
||
$mockOAuthService = Mockery::mock(OAuthService::class); | ||
|
||
$pkceCode = 'valid_pkce_code'; | ||
$stateCode = 'valid_state'; | ||
$validCode = 'valid_code'; | ||
|
||
Session::put('oauth2-state', $stateCode); | ||
Session::put('oauth2-pkceCode', $pkceCode); | ||
|
||
Config::set('oauth.user_model_name', TestUser::class); | ||
|
||
$mockOAuthService->shouldReceive('setPkceCode') | ||
->with($pkceCode) | ||
->once(); | ||
|
||
$mockAccessToken = Mockery::mock(AccessToken::class); | ||
$mockAccessToken->shouldReceive('getToken')->andReturn('mock_token'); | ||
$mockAccessToken->shouldReceive('getRefreshToken')->andReturn('mock_refresh_token'); | ||
$mockAccessToken->shouldReceive('getExpires')->andReturn(time() + 3600); | ||
|
||
$mockOAuthService->shouldReceive('getAccessToken') | ||
->with('authorization_code', ['code' => $validCode]) | ||
->andReturn($mockAccessToken); | ||
|
||
$mockOAuthService->shouldReceive('getResourceOwner') | ||
->andReturn(Mockery::mock([ | ||
'toArray' => [ | ||
'id' => 1, | ||
'name' => 'user', | ||
'email' => '[email protected]', | ||
'groups' => ['admin'], | ||
'sub' => '123456abc', | ||
], | ||
])); | ||
|
||
$this->instance(OAuthService::class, $mockOAuthService); | ||
|
||
Auth::shouldReceive('guard') | ||
->with(config('oauth.guard_name')) | ||
->andReturn(Mockery::mock(Guard::class, ['login' => null, 'check' => false])); | ||
|
||
$response = $this->get(route('oauth.callback', [ | ||
'code' => $validCode, | ||
'state' => $stateCode, | ||
])); | ||
|
||
$response->assertRedirect(config('oauth.redirect_route_callback_ok')); | ||
}); | ||
|
||
it('renews the OAuth token if the user is authenticated and the token is expired', function () { | ||
$mockOAuthService = Mockery::mock(OAuthService::class); | ||
|
||
// $mockUser = Mockery::mock(\Illuminate\Contracts\Auth\Authenticatable::class); | ||
// $mockUser->oauth_token = 'expired_token'; | ||
// $mockUser->oauth_refresh_token = 'valid_refresh_token'; | ||
// $mockUser->oauth_token_expires_at = Carbon::now()->subHour()->timestamp; | ||
$mockUser = TestUser::factory(state: [ | ||
'oauth_token_expires_at' => Carbon::now()->subHour() | ||
])->create(); | ||
|
||
Auth::shouldReceive('guard') | ||
->with(config('oauth.guard_name')) | ||
->andReturn(Mockery::mock(Guard::class, ['check' => true, 'user' => $mockUser])); | ||
|
||
$newOAuthToken = 'new_oauth_token'; | ||
$newRefreshToken = 'new_refresh_token'; | ||
$newExpiredDate = Carbon::now()->addHour()->timestamp; | ||
|
||
$mockAccessToken = Mockery::mock(AccessToken::class); | ||
$mockAccessToken->shouldReceive('getToken')->andReturn($newOAuthToken); | ||
$mockAccessToken->shouldReceive('getRefreshToken')->andReturn($newRefreshToken); | ||
$mockAccessToken->shouldReceive('getExpires')->andReturn($newExpiredDate); | ||
|
||
$mockOAuthService->shouldReceive('getAccessToken') | ||
->with('refresh_token', ['refresh_token' => 'oauth_refresh_token']) | ||
->andReturn($mockAccessToken); | ||
|
||
$mockOAuthService->shouldReceive('getResourceOwner') | ||
->andReturn(Mockery::mock(['toArray' => [ | ||
'groups' => ['admin'], | ||
]])); | ||
|
||
$this->instance(OAuthService::class, $mockOAuthService); | ||
|
||
$response = $this->app->make(OAuthController::class)->renew(); | ||
|
||
expect($mockUser->oauth_token)->toBe($newOAuthToken); | ||
expect($mockUser->oauth_refresh_token)->toBe($newRefreshToken); | ||
expect($mockUser->oauth_token_expires_at)->toBe($newExpiredDate); | ||
|
||
expect($response)->toBeNull(); | ||
}); |
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
Oops, something went wrong.