mirror of
https://github.com/solidtime-io/solidtime.git
synced 2026-08-08 00:02:15 +01:00
Added placeholder users; Better exception handling; Enhanced local setup
This commit is contained in:
@@ -4,9 +4,11 @@ declare(strict_types=1);
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\TimeEntry;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use Illuminate\Support\Facades\URL;
|
||||
use Laravel\Jetstream\Mail\TeamInvitation;
|
||||
use Tests\TestCase;
|
||||
|
||||
@@ -31,6 +33,49 @@ class InviteTeamMemberTest extends TestCase
|
||||
$this->assertCount(1, $user->currentTeam->fresh()->teamInvitations);
|
||||
}
|
||||
|
||||
public function test_team_member_can_not_be_invited_to_team_if_already_on_team(): void
|
||||
{
|
||||
// Arrange
|
||||
Mail::fake();
|
||||
$user = User::factory()->withPersonalOrganization()->create();
|
||||
$existingUser = User::factory()->create();
|
||||
$user->currentTeam->users()->attach($existingUser, ['role' => 'admin']);
|
||||
$this->actingAs($user);
|
||||
|
||||
// Act
|
||||
$response = $this->post('/teams/'.$user->currentTeam->id.'/members', [
|
||||
'email' => $existingUser->email,
|
||||
'role' => 'admin',
|
||||
]);
|
||||
|
||||
// Assert
|
||||
$response->assertInvalid(['email'], 'addTeamMember');
|
||||
Mail::assertNotSent(TeamInvitation::class);
|
||||
$this->assertCount(0, $user->currentTeam->fresh()->teamInvitations);
|
||||
}
|
||||
|
||||
public function test_team_member_can_be_invited_to_team_if_already_on_team_as_placeholder(): void
|
||||
{
|
||||
// Arrange
|
||||
Mail::fake();
|
||||
$user = User::factory()->withPersonalOrganization()->create();
|
||||
$existingUser = User::factory()->create([
|
||||
'is_placeholder' => true,
|
||||
]);
|
||||
$user->currentTeam->users()->attach($existingUser, ['role' => 'employee']);
|
||||
$this->actingAs($user);
|
||||
|
||||
// Act
|
||||
$response = $this->post('/teams/'.$user->currentTeam->id.'/members', [
|
||||
'email' => $existingUser->email,
|
||||
'role' => 'employee',
|
||||
]);
|
||||
|
||||
// Assert
|
||||
Mail::assertSent(TeamInvitation::class);
|
||||
$this->assertCount(1, $user->currentTeam->fresh()->teamInvitations);
|
||||
}
|
||||
|
||||
public function test_team_member_invitations_can_be_cancelled(): void
|
||||
{
|
||||
// Arrange
|
||||
@@ -49,4 +94,97 @@ class InviteTeamMemberTest extends TestCase
|
||||
// Assert
|
||||
$this->assertCount(0, $user->currentTeam->fresh()->teamInvitations);
|
||||
}
|
||||
|
||||
public function test_team_member_invitations_can_be_accepted(): void
|
||||
{
|
||||
// Arrange
|
||||
Mail::fake();
|
||||
$owner = User::factory()->withPersonalOrganization()->create();
|
||||
$user = User::factory()->withPersonalOrganization()->create();
|
||||
$invitation = $owner->currentTeam->teamInvitations()->create([
|
||||
'email' => $user->email,
|
||||
'role' => 'employee',
|
||||
]);
|
||||
$this->actingAs($user);
|
||||
|
||||
// Act
|
||||
$acceptUrl = URL::temporarySignedRoute(
|
||||
'team-invitations.accept',
|
||||
now()->addMinutes(60),
|
||||
[$invitation->getKey()]
|
||||
);
|
||||
$response = $this->get($acceptUrl);
|
||||
|
||||
// Assert
|
||||
$this->assertCount(0, $owner->currentTeam->fresh()->teamInvitations);
|
||||
$user->refresh();
|
||||
$this->assertCount(1, $user->organizations);
|
||||
$this->assertContains($owner->currentTeam->getKey(), $user->organizations->pluck('id'));
|
||||
}
|
||||
|
||||
public function test_team_member_invitations_of_placeholder_can_be_accepted_and_migrates_date_to_real_user(): void
|
||||
{
|
||||
// Arrange
|
||||
Mail::fake();
|
||||
$placeholder = User::factory()->withPersonalOrganization()->create([
|
||||
'is_placeholder' => true,
|
||||
]);
|
||||
|
||||
$owner = User::factory()->withPersonalOrganization()->create();
|
||||
$owner->currentTeam->users()->attach($placeholder, ['role' => 'employee']);
|
||||
$timeEntries = TimeEntry::factory()->forOrganization($owner->currentTeam)->forUser($placeholder)->createMany(5);
|
||||
|
||||
$user = User::factory()->withPersonalOrganization()->create([
|
||||
'email' => $placeholder->email,
|
||||
]);
|
||||
|
||||
$invitation = $owner->currentTeam->teamInvitations()->create([
|
||||
'email' => $user->email,
|
||||
'role' => 'employee',
|
||||
]);
|
||||
$this->actingAs($user);
|
||||
|
||||
// Act
|
||||
$acceptUrl = URL::temporarySignedRoute(
|
||||
'team-invitations.accept',
|
||||
now()->addMinutes(60),
|
||||
[$invitation->getKey()]
|
||||
);
|
||||
$response = $this->get($acceptUrl);
|
||||
|
||||
// Assert
|
||||
$user->refresh();
|
||||
$placeholder->refresh();
|
||||
$this->assertCount(0, $owner->currentTeam->fresh()->teamInvitations);
|
||||
$this->assertCount(1, $user->organizations);
|
||||
$this->assertContains($owner->currentTeam->getKey(), $user->organizations->pluck('id'));
|
||||
$this->assertCount(5, $user->timeEntries);
|
||||
$this->assertCount(0, $placeholder->timeEntries);
|
||||
}
|
||||
|
||||
public function test_team_member_accept_fails_if_user_with_that_email_does_not_exist(): void
|
||||
{
|
||||
// Arrange
|
||||
Mail::fake();
|
||||
$owner = User::factory()->withPersonalOrganization()->create();
|
||||
$user = User::factory()->withPersonalOrganization()->create();
|
||||
$invitation = $owner->currentTeam->teamInvitations()->create([
|
||||
'email' => 'firstname.lastname@mail.test',
|
||||
'role' => 'employee',
|
||||
]);
|
||||
$this->actingAs($user);
|
||||
|
||||
// Act
|
||||
$acceptUrl = URL::temporarySignedRoute(
|
||||
'team-invitations.accept',
|
||||
now()->addMinutes(60),
|
||||
[$invitation->getKey()]
|
||||
);
|
||||
$response = $this->get($acceptUrl);
|
||||
|
||||
// Assert
|
||||
$this->assertCount(1, $owner->currentTeam->fresh()->teamInvitations);
|
||||
$user->refresh();
|
||||
$this->assertCount(0, $user->organizations);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user