Added user and organization deletion system; Added coverage annotations

This commit is contained in:
Constantin Graf
2024-06-07 19:17:40 +02:00
committed by Constantin Graf
parent 8857befc6c
commit 86f5ea47bb
65 changed files with 2651 additions and 135 deletions

View File

@@ -4,6 +4,9 @@ declare(strict_types=1);
namespace Tests\Feature;
use App\Enums\Role;
use App\Models\Member;
use App\Models\Organization;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
@@ -42,4 +45,24 @@ class DeleteAccountTest extends TestCase
// Assert
$this->assertNotNull($user->fresh());
}
public function test_user_account_can_not_be_deleted_if_attached_to_a_organization_with_multiple_users(): void
{
// Arrange
$user = User::factory()->create();
$organization = Organization::factory()->withOwner($user)->create();
$userMember = Member::factory()->forOrganization($organization)->forUser($user)->role(Role::Owner)->create();
$otherUser = User::factory()->create();
$otherMember = Member::factory()->forOrganization($organization)->forUser($otherUser)->role(Role::Admin)->create();
$this->actingAs($user);
// Act
$response = $this->delete('/user', [
'password' => 'password',
]);
// Assert
$response->assertInvalid(['password']);
$this->assertNotNull($user->fresh());
}
}

View File

@@ -4,6 +4,8 @@ declare(strict_types=1);
namespace Tests\Feature;
use App\Enums\Role;
use App\Models\Member;
use App\Models\Organization;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
@@ -13,30 +15,70 @@ class DeleteTeamTest extends TestCase
{
use RefreshDatabase;
public function test_teams_can_be_deleted(): void
public function test_teams_can_be_deleted_and_users_of_the_organization_that_have_no_organization_get_a_new_one(): void
{
$this->actingAs($user = User::factory()->withPersonalOrganization()->create());
// Arrange
$user = User::factory()->withPersonalOrganization()->create();
$this->actingAs($user);
$user->ownedTeams()->save($team = Organization::factory()->make([
$organization = Organization::factory()->withOwner($user)->create([
'personal_team' => false,
]));
]);
Member::factory()->forOrganization($organization)->forUser($user)->role(Role::Owner)->create();
$team->users()->attach(
$otherUser = User::factory()->create(), ['role' => 'test-role']
$otherUser = User::factory()->create();
$organization->users()->attach(
$otherUser, ['role' => 'test-role']
);
$response = $this->delete('/teams/'.$team->getKey());
// Act
$response = $this->withoutExceptionHandling()->delete('/teams/'.$organization->getKey());
$this->assertNull($team->fresh());
$this->assertCount(0, $otherUser->fresh()->teams);
// Assert
$this->assertNull($organization->fresh());
$this->assertCount(1, $otherUser->fresh()->teams);
$this->assertFalse($otherUser->fresh()->teams->first()->is($organization));
}
public function test_personal_teams_cant_be_deleted(): void
public function test_personal_teams_can_be_deleted_but_user_gets_an_new_one_if_this_is_the_only_one_left(): void
{
$this->actingAs($user = User::factory()->withPersonalOrganization()->create());
// Arrange
$user = User::factory()->withPersonalOrganization()->create();
$organization = $user->currentTeam;
$this->actingAs($user);
$response = $this->delete('/teams/'.$user->currentTeam->getKey());
// Act
$response = $this->delete('/teams/'.$organization->getKey());
$this->assertNotNull($user->currentTeam->fresh());
// Assert
$user->refresh();
$this->assertDatabaseMissing(Organization::class, [
'id' => $organization->getKey(),
]);
$this->assertTrue($user->currentTeam->isNot($organization));
}
public function test_organization_can_not_be_deleted_if_user_is_not_owner(): void
{
// Arrange
$user = User::factory()->withPersonalOrganization()->create();
$organization = Organization::factory()->withOwner($user)->create([
'personal_team' => false,
]);
$this->actingAs($user);
$otherUser = User::factory()->create();
$organization->users()->attach(
$otherUser, ['role' => Role::Admin->value]
);
// Act
$response = $this->delete('/teams/'.$organization->getKey());
// Assert
$response->assertForbidden();
$this->assertDatabaseHas(Organization::class, [
'id' => $organization->getKey(),
]);
}
}