koel/tests/Integration/Services/UserInvitationServiceTest.php

102 lines
2.9 KiB
PHP
Raw Normal View History

2023-08-20 22:35:58 +00:00
<?php
namespace Tests\Integration\Services;
use App\Exceptions\InvitationNotFoundException;
use App\Mail\UserInvite;
use App\Models\User;
use App\Services\UserInvitationService;
2024-01-11 12:41:33 +00:00
use Illuminate\Support\Facades\Hash;
2023-08-20 22:35:58 +00:00
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Str;
use Tests\TestCase;
2024-01-11 12:41:33 +00:00
use function Tests\create_admin;
2023-08-20 22:35:58 +00:00
class UserInvitationServiceTest extends TestCase
{
private UserInvitationService $service;
public function setUp(): void
{
parent::setUp();
$this->service = app(UserInvitationService::class);
}
public function testInvite(): void
{
Mail::fake();
$emails = ['foo@bar.com', 'bar@baz.io'];
2024-01-11 12:41:33 +00:00
$user = create_admin();
2023-08-20 22:35:58 +00:00
$this->service
->invite($emails, true, $user)
->each(static function (User $prospect) use ($user): void {
self::assertTrue($prospect->is_admin);
self::assertTrue($prospect->invitedBy->is($user));
self::assertTrue($prospect->is_prospect);
self::assertNotNull($prospect->invitation_token);
self::assertNotNull($prospect->invited_at);
self::assertNull($prospect->invitation_accepted_at);
});
Mail::assertQueued(UserInvite::class, 2);
}
public function testGetUserProspectByToken(): void
{
$token = Str::uuid()->toString();
2024-01-11 12:41:33 +00:00
$user = create_admin();
2023-08-20 22:35:58 +00:00
$prospect = User::factory()->for($user, 'invitedBy')->create([
'invitation_token' => $token,
'invited_at' => now(),
]);
self::assertTrue($this->service->getUserProspectByToken($token)->is($prospect));
}
public function testGetUserProspectByTokenThrowsIfTokenNotFound(): void
{
$this->expectException(InvitationNotFoundException::class);
$this->service->getUserProspectByToken(Str::uuid()->toString());
}
public function testRevokeByEmail(): void
{
2024-01-11 12:41:33 +00:00
$user = create_admin();
2023-08-20 22:35:58 +00:00
/** @var User $prospect */
$prospect = User::factory()->for($user, 'invitedBy')->create([
'invitation_token' => Str::uuid()->toString(),
'invited_at' => now(),
]);
$this->service->revokeByEmail($prospect->email);
self::assertModelMissing($prospect);
}
public function testAccept(): void
{
$token = Str::uuid()->toString();
2024-01-11 12:41:33 +00:00
$user = create_admin();
2023-08-20 22:35:58 +00:00
User::factory()->for($user, 'invitedBy')->create([
'invitation_token' => $token,
'invited_at' => now(),
'is_admin' => true,
]);
$user = $this->service->accept($token, 'Bruce Dickinson', 'SuperSecretPassword');
self::assertFalse($user->is_prospect);
self::assertTrue($user->is_admin);
self::assertNull($user->invitation_token);
self::assertNotNull($user->invitation_accepted_at);
self::assertTrue(Hash::check('SuperSecretPassword', $user->password));
}
}