-
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.
Add test cases for GoogleAuthController
- Loading branch information
1 parent
ed98b5e
commit b92ff2e
Showing
1 changed file
with
165 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
<?php | ||
|
||
namespace Tests\Feature\ExternalAuth; | ||
|
||
use App\Models\User; | ||
use App\Notifications\AccountCreatedViaProvider; | ||
use App\Providers\RouteServiceProvider; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Illuminate\Log\Events\MessageLogged; | ||
use Illuminate\Notifications\Events\NotificationSent; | ||
use Illuminate\Support\Facades\Config; | ||
use Illuminate\Support\Facades\Event; | ||
use Illuminate\Support\Str; | ||
use Laravel\Socialite\Facades\Socialite; | ||
use Laravel\Socialite\Two\GoogleProvider; | ||
use Laravel\Socialite\Two\User as SocialiteUser; | ||
use Mockery; | ||
use Tests\TestCase; | ||
|
||
class GoogleAuthTest extends TestCase | ||
{ | ||
use RefreshDatabase; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
Config::set('services.google.enabled', true); | ||
Str::createRandomStringsUsing(fn () => '6XRVFqGe42nIWRhrfNc84xlNlopjmt2NL3AihLmk'); | ||
} | ||
|
||
private function mockGoogleProvider(array $userData): void | ||
{ | ||
$mockGoogleProvider = Mockery::mock(GoogleProvider::class) | ||
->makePartial() | ||
->shouldReceive('user') | ||
->andReturn((new SocialiteUser())->map($userData)) | ||
->getMock(); | ||
|
||
Socialite::partialMock()->shouldReceive('createGoogleDriver')->andReturn($mockGoogleProvider); | ||
} | ||
|
||
public function test_authenticated_user_redirected_to_dashboard_page(): void | ||
{ | ||
$user = User::factory()->create(); | ||
|
||
$response = $this | ||
->actingAs($user) | ||
->get(route('external-auth.google')); | ||
|
||
$response->assertRedirect(RouteServiceProvider::HOME); | ||
} | ||
|
||
public function test_guest_not_redirected_to_google_authorization_page_when_service_is_disabled(): void | ||
{ | ||
Config::set('services.google.enabled', false); | ||
|
||
$response = $this->get(route('external-auth.google')); | ||
|
||
$response->assertRedirect('/'); | ||
} | ||
|
||
public function test_guest_redirected_to_google_authorization_page(): void | ||
{ | ||
$response = $this->get(route('external-auth.google')); | ||
|
||
$response->assertRedirect(Socialite::driver('Google')->redirect()->getTargetUrl()); | ||
} | ||
|
||
public function test_guest_cannot_create_account_via_google_when_username_is_already_taken(): void | ||
{ | ||
Event::fake(); | ||
|
||
User::factory()->create([ | ||
'username' => 'mark-smith', | ||
]); | ||
|
||
$googleUser = [ | ||
'id' => '1234567812345678', | ||
'nickname' => null, | ||
'name' => 'Mark Smith', | ||
'email' => 'mark-smith@test.com', | ||
'avatar' => 'https://graph.google.com/v3.3/1234567812345678/picture', | ||
'avatar_original' => 'https://graph.google.com/v3.3/1234567812345678/picture', | ||
'profileUrl' => null, | ||
]; | ||
|
||
$this->mockGoogleProvider($googleUser); | ||
|
||
$response = $this->get(route('external-auth.google-callback')); | ||
|
||
$this->assertFalse($this->isAuthenticated()); | ||
|
||
Event::assertDispatched( | ||
MessageLogged::class, | ||
fn (MessageLogged $event) => | ||
$event->level === 'info' && | ||
Str::endsWith($event->message, 'Authentication failed for Google user ' . $googleUser['email']), | ||
); | ||
|
||
$response->assertRedirect('/'); | ||
} | ||
|
||
public function test_guest_can_create_account_via_google(): void | ||
{ | ||
Event::fake(); | ||
|
||
$googleUser = [ | ||
'id' => '1234567812345678', | ||
'nickname' => null, | ||
'name' => 'John Doe', | ||
'email' => 'john-doe@test.com', | ||
'avatar' => 'https://graph.google.com/v3.3/1234567812345678/picture', | ||
'avatar_original' => 'https://graph.google.com/v3.3/1234567812345678/picture', | ||
'profileUrl' => null, | ||
]; | ||
|
||
$this->mockGoogleProvider($googleUser); | ||
|
||
$response = $this->get(route('external-auth.google-callback')); | ||
|
||
$user = User::first(); | ||
|
||
$this->assertSame(1, User::count()); | ||
$this->assertSame(Str::before($googleUser['email'], '@'), $user->username); | ||
$this->assertSame($googleUser['email'], $user->email); | ||
$this->assertNotNull($user->email_verified_at); | ||
$this->assertNotNull($user->remember_token); | ||
$this->assertFalse($user->is_admin); | ||
|
||
Event::assertDispatched( | ||
NotificationSent::class, | ||
fn (NotificationSent $event) => $event->notification instanceof AccountCreatedViaProvider, | ||
); | ||
|
||
$this->assertAuthenticatedAs($user); | ||
|
||
$response->assertRedirect(RouteServiceProvider::HOME); | ||
} | ||
|
||
public function test_existing_user_can_login_via_google(): void | ||
{ | ||
$user = User::factory()->create([ | ||
'email' => 'john-doe@test.com', | ||
]); | ||
|
||
$googleUser = [ | ||
'id' => '1234567812345678', | ||
'nickname' => null, | ||
'name' => 'John Doe', | ||
'email' => 'john-doe@test.com', | ||
'avatar' => 'https://graph.google.com/v3.3/1234567812345678/picture', | ||
'avatar_original' => 'https://graph.google.com/v3.3/1234567812345678/picture', | ||
'profileUrl' => null, | ||
]; | ||
|
||
$this->mockGoogleProvider($googleUser); | ||
|
||
$response = $this->get(route('external-auth.google-callback')); | ||
|
||
$this->assertAuthenticatedAs($user); | ||
|
||
$response->assertRedirect(RouteServiceProvider::HOME); | ||
} | ||
} |