trypost/tests/Feature/SocialLoginControllerTest.php
2026-03-30 21:18:07 -03:00

93 lines
2.8 KiB
PHP

<?php
declare(strict_types=1);
use App\Models\User;
use Laravel\Socialite\Facades\Socialite;
use Laravel\Socialite\Two\User as SocialiteUser;
beforeEach(function () {
config([
'services.google-auth.client_id' => 'test-client-id',
'services.google-auth.client_secret' => 'test-client-secret',
'services.google-auth.redirect' => 'https://app.trypost.test/auth/google/callback',
]);
});
test('google redirect returns redirect response', function () {
$response = $this->get(route('auth.google.redirect'));
$response->assertRedirect();
expect($response->headers->get('Location'))->toContain('accounts.google.com');
});
test('google callback logs in existing user by email', function () {
$user = User::factory()->create([
'email' => 'existing@example.com',
]);
$socialiteUser = new SocialiteUser;
$socialiteUser->map([
'id' => '123456',
'name' => 'Existing User',
'email' => 'existing@example.com',
]);
Socialite::shouldReceive('driver')
->with('google-auth')
->andReturn($driver = Mockery::mock());
$driver->shouldReceive('user')->andReturn($socialiteUser);
$response = $this->get(route('auth.google.callback'));
$response->assertRedirect(route('app.home'));
$this->assertAuthenticatedAs($user);
});
test('google callback creates new user when email does not exist', function () {
$socialiteUser = new SocialiteUser;
$socialiteUser->map([
'id' => '789',
'name' => 'New User',
'email' => 'new@example.com',
]);
Socialite::shouldReceive('driver')
->with('google-auth')
->andReturn($driver = Mockery::mock());
$driver->shouldReceive('user')->andReturn($socialiteUser);
$response = $this->get(route('auth.google.callback'));
$response->assertRedirect(route('app.onboarding.role'));
$user = User::where('email', 'new@example.com')->first();
expect($user)->not->toBeNull();
expect($user->name)->toBe('New User');
expect($user->email_verified_at)->not->toBeNull();
expect($user->currentWorkspace)->not->toBeNull();
$this->assertAuthenticatedAs($user);
});
test('google callback marks unverified existing user as verified', function () {
$user = User::factory()->create([
'email' => 'unverified@example.com',
'email_verified_at' => null,
]);
$socialiteUser = new SocialiteUser;
$socialiteUser->map([
'id' => '456',
'name' => 'Unverified User',
'email' => 'unverified@example.com',
]);
Socialite::shouldReceive('driver')
->with('google-auth')
->andReturn($driver = Mockery::mock());
$driver->shouldReceive('user')->andReturn($socialiteUser);
$this->get(route('auth.google.callback'));
expect($user->fresh()->email_verified_at)->not->toBeNull();
});