2024-03-30 16:49:25 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Feature\KoelPlus\SSO;
|
|
|
|
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
use Laravel\Socialite\Facades\Socialite;
|
|
|
|
use Laravel\Socialite\Two\User as GoogleUser;
|
|
|
|
use Mockery;
|
2024-10-24 10:45:45 +00:00
|
|
|
use PHPUnit\Framework\Attributes\Test;
|
2024-03-30 16:49:25 +00:00
|
|
|
use Tests\PlusTestCase;
|
|
|
|
|
|
|
|
use function Tests\create_user;
|
|
|
|
|
|
|
|
class GoogleTest extends PlusTestCase
|
|
|
|
{
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function callbackWithNewUser(): void
|
2024-03-30 16:49:25 +00:00
|
|
|
{
|
|
|
|
$googleUser = Mockery::mock(GoogleUser::class, [
|
|
|
|
'getEmail' => 'bruce@iron.com',
|
|
|
|
'getName' => 'Bruce Dickinson',
|
|
|
|
'getAvatar' => 'https://lh3.googleusercontent.com/a/vatar',
|
|
|
|
'getId' => Str::random(),
|
|
|
|
]);
|
|
|
|
|
|
|
|
Socialite::shouldReceive('driver->user')->andReturn($googleUser);
|
|
|
|
|
|
|
|
$response = $this->get('auth/google/callback');
|
|
|
|
$response->assertOk();
|
|
|
|
$response->assertViewIs('sso-callback');
|
|
|
|
$response->assertViewHas('token');
|
|
|
|
}
|
|
|
|
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function callbackWithExistingEmail(): void
|
2024-03-30 16:49:25 +00:00
|
|
|
{
|
|
|
|
create_user(['email' => 'bruce@iron.com']);
|
|
|
|
|
|
|
|
$googleUser = Mockery::mock(GoogleUser::class, [
|
|
|
|
'getEmail' => 'bruce@iron.com',
|
|
|
|
'getName' => 'Bruce Dickinson',
|
|
|
|
'getAvatar' => 'https://lh3.googleusercontent.com/a/vatar',
|
|
|
|
'getId' => Str::random(),
|
|
|
|
]);
|
|
|
|
|
|
|
|
Socialite::shouldReceive('driver->user')->andReturn($googleUser);
|
|
|
|
|
|
|
|
$response = $this->get('auth/google/callback');
|
|
|
|
$response->assertOk();
|
|
|
|
$response->assertViewIs('sso-callback');
|
|
|
|
$response->assertViewHas('token');
|
|
|
|
}
|
|
|
|
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function callbackWithExistingSSOUser(): void
|
2024-03-30 16:49:25 +00:00
|
|
|
{
|
|
|
|
create_user([
|
|
|
|
'sso_provider' => 'Google',
|
|
|
|
'sso_id' => '123',
|
|
|
|
'email' => 'bruce@iron.com',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$googleUser = Mockery::mock(GoogleUser::class, [
|
|
|
|
'getEmail' => 'bruce@iron.com',
|
|
|
|
'getName' => 'Bruce Dickinson',
|
|
|
|
'getAvatar' => 'https://lh3.googleusercontent.com/a/vatar',
|
|
|
|
'getId' => '123',
|
|
|
|
]);
|
|
|
|
|
|
|
|
Socialite::shouldReceive('driver->user')->andReturn($googleUser);
|
|
|
|
|
|
|
|
$response = $this->get('auth/google/callback');
|
|
|
|
$response->assertOk();
|
|
|
|
$response->assertViewIs('sso-callback');
|
|
|
|
$response->assertViewHas('token');
|
|
|
|
}
|
|
|
|
}
|