2023-08-20 22:35:58 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Feature;
|
|
|
|
|
2024-01-25 16:21:26 +00:00
|
|
|
use App\Http\Resources\UserProspectResource;
|
2023-08-20 22:35:58 +00:00
|
|
|
use App\Mail\UserInvite;
|
|
|
|
use App\Models\User;
|
2024-01-09 18:34:40 +00:00
|
|
|
use Illuminate\Support\Facades\Mail;
|
2023-08-20 22:35:58 +00:00
|
|
|
use Illuminate\Support\Str;
|
2024-01-09 18:34:40 +00:00
|
|
|
use Tests\TestCase;
|
2023-08-20 22:35:58 +00:00
|
|
|
|
2024-01-11 12:41:33 +00:00
|
|
|
use function Tests\create_admin;
|
|
|
|
|
2023-08-20 22:35:58 +00:00
|
|
|
class UserInvitationTest extends TestCase
|
|
|
|
{
|
|
|
|
public function testInvite(): void
|
|
|
|
{
|
|
|
|
Mail::fake();
|
|
|
|
|
|
|
|
$this->postAs('api/invitations', [
|
|
|
|
'emails' => ['foo@bar.io', 'bar@baz.ai'],
|
|
|
|
'is_admin' => true,
|
2024-01-11 12:41:33 +00:00
|
|
|
], create_admin())
|
2023-08-20 22:35:58 +00:00
|
|
|
->assertSuccessful()
|
2024-01-25 16:21:26 +00:00
|
|
|
->assertJsonStructure(['*' => UserProspectResource::JSON_STRUCTURE]);
|
2023-08-20 22:35:58 +00:00
|
|
|
|
|
|
|
Mail::assertQueued(UserInvite::class, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNonAdminCannotInvite(): void
|
|
|
|
{
|
|
|
|
Mail::fake();
|
|
|
|
|
2024-01-11 12:41:33 +00:00
|
|
|
$this->postAs('api/invitations', ['emails' => ['foo@bar.io', 'bar@baz.ai']])
|
2023-08-20 22:35:58 +00:00
|
|
|
->assertForbidden();
|
|
|
|
|
|
|
|
Mail::assertNothingQueued();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetProspect(): void
|
|
|
|
{
|
|
|
|
$prospect = self::createProspect();
|
|
|
|
|
|
|
|
$this->get("api/invitations?token=$prospect->invitation_token")
|
|
|
|
->assertSuccessful()
|
2024-01-25 16:21:26 +00:00
|
|
|
->assertJsonStructure(UserProspectResource::JSON_STRUCTURE);
|
2023-08-20 22:35:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testRevoke(): void
|
|
|
|
{
|
|
|
|
$prospect = self::createProspect();
|
|
|
|
|
2024-01-11 12:41:33 +00:00
|
|
|
$this->deleteAs('api/invitations', ['email' => $prospect->email], create_admin())
|
2023-08-20 22:35:58 +00:00
|
|
|
->assertSuccessful();
|
|
|
|
|
|
|
|
self::assertModelMissing($prospect);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNonAdminCannotRevoke(): void
|
|
|
|
{
|
|
|
|
$prospect = self::createProspect();
|
|
|
|
|
|
|
|
$this->deleteAs('api/invitations', ['email' => $prospect->email])
|
|
|
|
->assertForbidden();
|
|
|
|
|
|
|
|
self::assertModelExists($prospect);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAccept(): void
|
|
|
|
{
|
|
|
|
$prospect = self::createProspect();
|
|
|
|
|
|
|
|
$this->post('api/invitations/accept', [
|
|
|
|
'token' => $prospect->invitation_token,
|
|
|
|
'name' => 'Bruce Dickinson',
|
|
|
|
'password' => 'SuperSecretPassword',
|
|
|
|
])
|
|
|
|
->assertSuccessful()
|
|
|
|
->assertJsonStructure(['token', 'audio-token']);
|
|
|
|
|
|
|
|
$prospect->refresh();
|
|
|
|
|
|
|
|
self::assertFalse($prospect->is_prospect);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static function createProspect(): User
|
|
|
|
{
|
2024-01-11 12:41:33 +00:00
|
|
|
return User::factory()->for(create_admin(), 'invitedBy')->create([
|
2023-08-20 22:35:58 +00:00
|
|
|
'invitation_token' => Str::uuid()->toString(),
|
|
|
|
'invited_at' => now(),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|