107 lines
3.4 KiB
PHP
107 lines
3.4 KiB
PHP
<?php
|
|
|
|
use App\Models\Role;
|
|
use App\Models\User;
|
|
use App\Repositories\Role\RoleRepository;
|
|
use App\Services\Auth\Social\SocialManager;
|
|
use App\Support\Enum\UserStatus;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Laravel\Socialite\Contracts\User as SocialUser;
|
|
use Tests\UpdatesSettings;
|
|
|
|
uses(RefreshDatabase::class);
|
|
uses(UpdatesSettings::class);
|
|
|
|
beforeEach(function () {
|
|
$this->artisan('db:seed');
|
|
});
|
|
|
|
test('associates existing user when email matches', function () {
|
|
$existing = User::factory()->create(['email' => 'foo@bar.com']);
|
|
|
|
$socialUser = makeSocialUser('foo@bar.com', 'John Doe');
|
|
|
|
$manager = app(SocialManager::class);
|
|
$user = $manager->associate($socialUser, 'facebook');
|
|
|
|
$this->assertTrue($existing->is($user));
|
|
$this->assertDatabaseHas('social_logins', [
|
|
'user_id' => $user->id,
|
|
'provider' => 'facebook',
|
|
]);
|
|
});
|
|
|
|
test('creates new user when email does not exist', function () {
|
|
$this->setSettings(['approval.enabled' => false]);
|
|
|
|
$socialUser = makeSocialUser('new@bar.com', 'Jane Smith');
|
|
|
|
$manager = app(SocialManager::class);
|
|
$user = $manager->associate($socialUser, 'google');
|
|
|
|
$this->assertSame('new@bar.com', $user->email);
|
|
$this->assertSame('Jane', $user->first_name);
|
|
$this->assertSame('Smith', $user->last_name);
|
|
$this->assertSame(UserStatus::ACTIVE, $user->status);
|
|
$this->assertNotNull($user->email_verified_at);
|
|
$this->assertDatabaseHas('social_logins', [
|
|
'user_id' => $user->id,
|
|
'provider' => 'google',
|
|
]);
|
|
});
|
|
|
|
test('new user status is waiting approval when approval enabled', function () {
|
|
$this->setSettings(['approval.enabled' => true]);
|
|
|
|
$socialUser = makeSocialUser('newer@bar.com', 'Foo Bar');
|
|
|
|
$manager = app(SocialManager::class);
|
|
$user = $manager->associate($socialUser, 'google');
|
|
|
|
$this->assertSame(UserStatus::WAITING_APPROVAL, $user->status);
|
|
});
|
|
|
|
test('single name is parsed without last name', function () {
|
|
$socialUser = makeSocialUser('single@bar.com', 'Cher');
|
|
|
|
$manager = app(SocialManager::class);
|
|
$user = $manager->associate($socialUser, 'facebook');
|
|
|
|
$this->assertSame('Cher', $user->first_name);
|
|
$this->assertSame('', $user->last_name);
|
|
});
|
|
|
|
test('new user gets default user role', function () {
|
|
$socialUser = makeSocialUser('role@bar.com', 'Role User');
|
|
|
|
$manager = app(SocialManager::class);
|
|
$user = $manager->associate($socialUser, 'facebook');
|
|
|
|
$this->assertSame(
|
|
app(RoleRepository::class)->findByName(Role::DEFAULT_USER_ROLE)->id,
|
|
$user->role_id,
|
|
);
|
|
});
|
|
|
|
test('facebook avatar url is resized', function () {
|
|
$socialUser = makeSocialUser('fb@bar.com', 'FB User');
|
|
$socialUser->avatar_original = 'https://avatar.fb/me?width=1920';
|
|
|
|
$manager = app(SocialManager::class);
|
|
$user = $manager->associate($socialUser, 'facebook');
|
|
|
|
$this->assertSame('https://avatar.fb/me?width=150', $user->avatar);
|
|
});
|
|
|
|
function makeSocialUser(string $email, string $name): SocialUser
|
|
{
|
|
$socialUser = Mockery::mock(SocialUser::class);
|
|
$socialUser->shouldReceive('getId')->andReturn((string) fake()->randomNumber());
|
|
$socialUser->shouldReceive('getEmail')->andReturn($email);
|
|
$socialUser->shouldReceive('getName')->andReturn($name);
|
|
$socialUser->shouldReceive('getAvatar')->andReturn('https://example.com/avatar.jpg');
|
|
$socialUser->avatar_original = 'https://example.com/avatar.jpg';
|
|
|
|
return $socialUser;
|
|
}
|