Skip to content

Commit

Permalink
feat: improvements AuthController tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
victore13 committed Sep 18, 2024
1 parent 6f1eba5 commit 88350c9
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions tests/Feature/ControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Session;
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
Expand All @@ -23,7 +24,9 @@
->andReturn($mockGuard);

$response = $this->get(route('oauth.request'));
$response->assertRedirect('/');

expect($response->getStatusCode())->toBe(302);
expect($response->headers->get('Location'))->toBe(Redirect::to('/')->getTargetUrl());
});

it('redirects unauthenticated users to the OAuth provider', function () {
Expand Down Expand Up @@ -52,7 +55,8 @@
$this->assertEquals('fake_state', Session::get('oauth2-state'));
$this->assertEquals('fake_pkce_code', Session::get('oauth2-pkceCode'));

$response->assertRedirect('https://example.com/oauth/authorize');
expect($response->getStatusCode())->toBe(302);
expect($response->headers->get('Location'))->toBe('https://example.com/oauth/authorize');
});

it('redirect where the user intends to go if authenticated in the callback', function () {
Expand All @@ -64,7 +68,9 @@
->andReturn($mockGuard);

$response = $this->get(route('oauth.callback'));
$response->assertRedirect('/');

expect($response->getStatusCode())->toBe(302);
expect($response->headers->get('Location'))->toBe(Redirect::intended()->getTargetUrl());
});

it('handles invalid or missing code in callback', function () {
Expand All @@ -79,8 +85,9 @@
'state' => 'correct_state',
]);

$response->assertRedirect(route(config('oauth.login_route_name')))
->assertSessionHas('message', 'Authentication failed. Please try again.');
expect($response->getStatusCode())->toBe(302);
expect($response->headers->get('Location'))->toBe(route(config('oauth.login_route_name')));
expect(session('message'))->toBe('Authentication failed. Please try again.');
});

it('handles invalid state in callback', function () {
Expand All @@ -93,8 +100,9 @@
'code' => 'valid_code',
]));

$response->assertRedirect(route(config('oauth.login_route_name')))
->assertSessionHas('message', 'Authentication failed. Please try again.');
expect($response->getStatusCode())->toBe(302);
expect($response->headers->get('Location'))->toBe(route(config('oauth.login_route_name')));
expect(session('message'))->toBe('Authentication failed. Please try again.');
});

it('logs in the user after a successful OAuth callback', function () {
Expand Down Expand Up @@ -149,7 +157,8 @@
'state' => $stateCode,
]));

$response->assertRedirect(config('oauth.redirect_route_callback_ok'));
expect($response->getStatusCode())->toBe(302);
expect($response->headers->get('Location'))->toBe(Redirect::to(config()->string('oauth.redirect_route_callback_ok'))->getTargetUrl());
});

it('renews the OAuth token if the user is authenticated and the token is expired', function () {
Expand Down

0 comments on commit 88350c9

Please sign in to comment.