mirror of
https://github.com/solidtime-io/solidtime.git
synced 2026-08-14 11:12:16 +01:00
Add checks for placeholder invitation; Fixed bug in member deletion
This commit is contained in:
@@ -129,26 +129,31 @@ class InvitationEndpointTest extends ApiEndpointTestAbstract
|
||||
$response->assertJsonPath('message', 'User is already a member of the organization');
|
||||
}
|
||||
|
||||
public function test_store_fails_if_user_invites_user_who_is_already_invited_to_organization(): void
|
||||
public function test_store_fails_if_an_invitation_with_the_same_email_already_exists(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
'invitations:create',
|
||||
]);
|
||||
Passport::actingAs($data->user);
|
||||
$invitation = OrganizationInvitation::factory()->forOrganization($data->organization)->create();
|
||||
$email = 'user@email.test';
|
||||
$invitation = OrganizationInvitation::factory()->forOrganization($data->organization)->create([
|
||||
'email' => $email,
|
||||
]);
|
||||
|
||||
// Act
|
||||
$response = $this->postJson(route('api.v1.invitations.store', $data->organization->getKey()), [
|
||||
'email' => $invitation->email,
|
||||
'email' => $email,
|
||||
'role' => Role::Employee->value,
|
||||
]);
|
||||
|
||||
// Assert
|
||||
$response->assertInvalid([
|
||||
'email' => 'The email has already been invited to the organization. Please wait for the user to accept the invitation or resend the invitation email.',
|
||||
$response->assertStatus(400);
|
||||
$response->assertExactJson([
|
||||
'error' => true,
|
||||
'key' => 'invitation_for_the_email_already_exists',
|
||||
'message' => 'The email has already been invited to the organization. Please wait for the user to accept the invitation or resend the invitation email.',
|
||||
]);
|
||||
$response->assertStatus(422);
|
||||
}
|
||||
|
||||
public function test_store_works_if_user_invites_user_who_is_also_a_placeholder(): void
|
||||
|
||||
@@ -10,6 +10,7 @@ use App\Events\MemberRemoved;
|
||||
use App\Http\Controllers\Api\V1\MemberController;
|
||||
use App\Models\Member;
|
||||
use App\Models\Organization;
|
||||
use App\Models\OrganizationInvitation;
|
||||
use App\Models\Project;
|
||||
use App\Models\ProjectMember;
|
||||
use App\Models\TimeEntry;
|
||||
@@ -653,6 +654,103 @@ class MemberEndpointTest extends ApiEndpointTestAbstract
|
||||
Event::assertNotDispatched(MemberRemoved::class);
|
||||
}
|
||||
|
||||
public function test_destroy_endpoint_also_deletes_user_if_member_is_placeholder(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
'members:delete',
|
||||
]);
|
||||
$user = User::factory()->placeholder()->create();
|
||||
$member = Member::factory()->forUser($user)->forOrganization($data->organization)->role(Role::Placeholder)->create();
|
||||
Passport::actingAs($data->user);
|
||||
Event::fake([
|
||||
MemberRemoved::class,
|
||||
]);
|
||||
|
||||
// Act
|
||||
$response = $this->deleteJson(route('api.v1.members.destroy', [$data->organization->getKey(), $member->getKey()]));
|
||||
|
||||
// Assert
|
||||
$response->assertStatus(204);
|
||||
$this->assertDatabaseMissing(Member::class, [
|
||||
'id' => $member->getKey(),
|
||||
]);
|
||||
$this->assertDatabaseMissing(User::class, [
|
||||
'id' => $user->getKey(),
|
||||
]);
|
||||
Event::assertDispatched(function (MemberRemoved $event) use ($data, $member): bool {
|
||||
return $event->organization->is($data->organization) &&
|
||||
$event->member->is($member);
|
||||
}, 1);
|
||||
}
|
||||
|
||||
public function test_destroy_endpoint_sets_current_organization_to_organization_the_user_is_still_member_of(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
'members:delete',
|
||||
]);
|
||||
$user = $data->user;
|
||||
$otherOrganization = Organization::factory()->create();
|
||||
$otherMember = Member::factory()->forOrganization($otherOrganization)->forUser($user)->role(Role::Employee)->create();
|
||||
Passport::actingAs($user);
|
||||
Event::fake([
|
||||
MemberRemoved::class,
|
||||
]);
|
||||
|
||||
// Act
|
||||
$response = $this->deleteJson(route('api.v1.members.destroy', [$data->organization->getKey(), $data->member->getKey()]));
|
||||
|
||||
// Assert
|
||||
$response->assertStatus(204);
|
||||
$this->assertDatabaseMissing(Member::class, [
|
||||
'id' => $data->member->getKey(),
|
||||
]);
|
||||
$user->refresh();
|
||||
$this->assertSame($otherOrganization->getKey(), $user->currentOrganization->getKey());
|
||||
Event::assertDispatched(function (MemberRemoved $event) use ($data): bool {
|
||||
return $event->organization->is($data->organization) &&
|
||||
$event->member->is($data->member);
|
||||
}, 1);
|
||||
}
|
||||
|
||||
public function test_destroy_endpoint_creates_new_organization_and_sets_the_current_organization_to_it_if_user_is_not_member_of_any_other_organization(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
'members:delete',
|
||||
]);
|
||||
$organization = $data->organization;
|
||||
$user = $data->user;
|
||||
Passport::actingAs($user);
|
||||
Event::fake([
|
||||
MemberRemoved::class,
|
||||
]);
|
||||
$this->assertDatabaseCount(Organization::class, 1);
|
||||
|
||||
// Act
|
||||
$response = $this->deleteJson(route('api.v1.members.destroy', [$data->organization->getKey(), $data->member->getKey()]));
|
||||
|
||||
// Assert
|
||||
$response->assertStatus(204);
|
||||
$this->assertDatabaseCount(Organization::class, 2);
|
||||
$newOrganization = Organization::where('id', '!=', $organization->getKey())->first();
|
||||
$this->assertNotNull($newOrganization);
|
||||
$this->assertDatabaseMissing(Member::class, [
|
||||
'id' => $data->member->getKey(),
|
||||
]);
|
||||
$this->assertDatabaseHas(Member::class, [
|
||||
'organization_id' => $newOrganization->getKey(),
|
||||
'user_id' => $user->getKey(),
|
||||
]);
|
||||
$user->refresh();
|
||||
$this->assertNotNull($user->currentOrganization);
|
||||
Event::assertDispatched(function (MemberRemoved $event) use ($data): bool {
|
||||
return $event->organization->is($data->organization) &&
|
||||
$event->member->is($data->member);
|
||||
}, 1);
|
||||
}
|
||||
|
||||
public function test_destroy_endpoint_succeeds_if_member_is_still_in_use_by_a_project_member_and_delete_related_is_active(): void
|
||||
{
|
||||
// Arrange
|
||||
@@ -937,6 +1035,37 @@ class MemberEndpointTest extends ApiEndpointTestAbstract
|
||||
$response->assertForbidden();
|
||||
}
|
||||
|
||||
public function test_invite_placeholder_fails_if_there_is_already_an_invitation_with_the_same_email(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
'members:invite-placeholder',
|
||||
'invitations:create',
|
||||
]);
|
||||
$placeholder = User::factory()->placeholder()->create([
|
||||
'email' => 'user@mail.test',
|
||||
]);
|
||||
$placeholderMember = Member::factory()->forUser($placeholder)->forOrganization($data->organization)->role(Role::Placeholder)->create();
|
||||
OrganizationInvitation::factory()->forOrganization($data->organization)->create([
|
||||
'email' => $placeholder->email,
|
||||
]);
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
// Act
|
||||
$response = $this->postJson(route('api.v1.members.invite-placeholder', [
|
||||
'organization' => $data->organization->id,
|
||||
'member' => $placeholderMember->id,
|
||||
]));
|
||||
|
||||
// Assert
|
||||
$response->assertStatus(400);
|
||||
$response->assertExactJson([
|
||||
'error' => true,
|
||||
'key' => 'invitation_for_the_email_already_exists',
|
||||
'message' => 'The email has already been invited to the organization. Please wait for the user to accept the invitation or resend the invitation email.',
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_invite_placeholder_returns_400_if_user_is_not_placeholder(): void
|
||||
{
|
||||
// Arrange
|
||||
|
||||
Reference in New Issue
Block a user