Added member and invitation endpoints

This commit is contained in:
Constantin Graf
2024-04-10 17:45:53 +02:00
parent 234fa06324
commit b67961cb07
48 changed files with 1186 additions and 106 deletions

View File

@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Tests\Feature;
use App\Models\Membership;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
@@ -14,13 +15,20 @@ class CreateTeamTest extends TestCase
public function test_teams_can_be_created(): void
{
$this->actingAs($user = User::factory()->withPersonalOrganization()->create());
// Arrange
$user = User::factory()->withPersonalOrganization()->create();
$this->actingAs($user);
// Act
$response = $this->post('/teams', [
'name' => 'Test Organization',
]);
// Assert
$newOrganization = $user->fresh()->ownedTeams()->latest('id')->first();
$this->assertCount(2, $user->fresh()->ownedTeams);
$this->assertEquals('Test Organization', $user->fresh()->ownedTeams()->latest('id')->first()->name);
$this->assertEquals('Test Organization', $newOrganization->name);
$member = Membership::query()->whereBelongsTo($user, 'user')->whereBelongsTo($newOrganization, 'organization')->firstOrFail();
$this->assertSame('owner', $member->role);
}
}

View File

@@ -118,7 +118,7 @@ class InviteTeamMemberTest extends TestCase
// Assert
$this->assertCount(0, $owner->currentTeam->fresh()->teamInvitations);
$user->refresh();
$this->assertCount(1, $user->organizations);
$this->assertCount(2, $user->organizations);
$this->assertContains($owner->currentTeam->getKey(), $user->organizations->pluck('id'));
}
@@ -126,9 +126,7 @@ class InviteTeamMemberTest extends TestCase
{
// Arrange
Mail::fake();
$placeholder = User::factory()->withPersonalOrganization()->create([
'is_placeholder' => true,
]);
$placeholder = User::factory()->withPersonalOrganization()->placeholder()->create();
$owner = User::factory()->withPersonalOrganization()->create();
$owner->currentTeam->users()->attach($placeholder, ['role' => 'employee']);
@@ -154,12 +152,11 @@ class InviteTeamMemberTest extends TestCase
// Assert
$user->refresh();
$placeholder->refresh();
$this->assertDatabaseMissing(User::class, ['id' => $placeholder->id]);
$this->assertCount(0, $owner->currentTeam->fresh()->teamInvitations);
$this->assertCount(1, $user->organizations);
$this->assertCount(2, $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
@@ -185,6 +182,6 @@ class InviteTeamMemberTest extends TestCase
// Assert
$this->assertCount(1, $owner->currentTeam->fresh()->teamInvitations);
$user->refresh();
$this->assertCount(0, $user->organizations);
$this->assertCount(1, $user->organizations);
}
}

View File

@@ -24,7 +24,7 @@ class LeaveTeamTest extends TestCase
$response = $this->delete('/teams/'.$user->currentTeam->id.'/members/'.$otherUser->id);
$this->assertCount(0, $user->currentTeam->fresh()->users);
$this->assertCount(1, $user->currentTeam->fresh()->users);
}
public function test_team_owners_cant_leave_their_own_team(): void

View File

@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Tests\Feature;
use App\Models\Membership;
use App\Models\User;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Testing\RefreshDatabase;
@@ -52,7 +53,12 @@ class RegistrationTest extends TestCase
$this->assertAuthenticated();
$response->assertRedirect(RouteServiceProvider::HOME);
$user = User::where('email', 'test@example.com')->firstOrFail();
$this->assertSame('Test User', $user->name);
$this->assertSame('UTC', $user->timezone);
$organization = $user->organizations()->firstOrFail();
$this->assertSame(true, $organization->personal_team);
$member = Membership::query()->whereBelongsTo($user, 'user')->whereBelongsTo($organization, 'organization')->firstOrFail();
$this->assertSame('owner', $member->role);
}
public function test_new_users_can_register_and_frontend_can_send_timezone_for_user(): void

View File

@@ -20,9 +20,9 @@ class RemoveTeamMemberTest extends TestCase
$otherUser = User::factory()->create(), ['role' => 'admin']
);
$response = $this->delete('/teams/'.$user->currentTeam->id.'/members/'.$otherUser->id);
$response = $this->withoutExceptionHandling()->delete('/teams/'.$user->currentTeam->id.'/members/'.$otherUser->id);
$this->assertCount(0, $user->currentTeam->fresh()->users);
$this->assertCount(1, $user->currentTeam->fresh()->users);
}
public function test_only_team_owner_can_remove_team_members(): void

View File

@@ -14,23 +14,69 @@ class UpdateTeamMemberRoleTest extends TestCase
public function test_team_member_roles_can_be_updated(): void
{
$this->actingAs($user = User::factory()->withPersonalOrganization()->create());
// Arrange
$user = User::factory()->withPersonalOrganization()->create();
$this->actingAs($user);
$user->currentTeam->users()->attach(
$otherUser = User::factory()->create(), ['role' => 'admin']
);
// Act
$response = $this->put('/teams/'.$user->currentTeam->id.'/members/'.$otherUser->id, [
'role' => 'employee',
]);
// Assert
$this->assertTrue($otherUser->fresh()->hasTeamRole(
$user->currentTeam->fresh(), 'employee'
));
}
public function test_team_member_roles_can_not_be_updated_to_placeholder(): void
{
// Arrange
$user = User::factory()->withPersonalOrganization()->create();
$this->actingAs($user);
$user->currentTeam->users()->attach(
$otherUser = User::factory()->create(), ['role' => 'admin']
);
// Act
$response = $this->put('/teams/'.$user->currentTeam->id.'/members/'.$otherUser->id, [
'role' => 'placeholder',
]);
// Assert
$this->assertTrue($otherUser->fresh()->hasTeamRole(
$user->currentTeam->fresh(), 'admin'
));
}
public function test_team_member_roles_can_be_updated_to_owner_which_changes_ownership(): void
{
// Arrange
$user = User::factory()->withPersonalOrganization()->create();
$this->actingAs($user);
$otherUser = User::factory()->create();
$user->currentTeam->users()->attach($otherUser, ['role' => 'admin']);
// Act
$response = $this->withoutExceptionHandling()->put('/teams/'.$user->currentTeam->id.'/members/'.$otherUser->getKey(), [
'role' => 'owner',
]);
// Assert
$this->assertTrue($otherUser->fresh()->hasTeamRole(
$user->currentTeam->fresh(), 'owner'
));
$this->assertSame($user->currentTeam->fresh()->user_id, $otherUser->getKey());
}
public function test_only_team_owner_can_update_team_member_roles(): void
{
// Arrange
$user = User::factory()->withPersonalOrganization()->create();
$user->currentTeam->users()->attach(
@@ -39,10 +85,13 @@ class UpdateTeamMemberRoleTest extends TestCase
$this->actingAs($otherUser);
// Act
$response = $this->put('/teams/'.$user->currentTeam->id.'/members/'.$otherUser->id, [
'role' => 'employee',
]);
// Assert
$response->assertStatus(403);
$this->assertTrue($otherUser->fresh()->hasTeamRole(
$user->currentTeam->fresh(), 'admin'
));