2024-05-31 18:59:04 -07:00
|
|
|
<?php
|
|
|
|
|
2024-06-04 10:48:53 -07:00
|
|
|
namespace Tests\Feature\Users\Api;
|
2024-05-31 18:59:04 -07:00
|
|
|
|
2024-05-31 19:10:29 -07:00
|
|
|
use App\Models\Company;
|
2024-06-04 10:48:53 -07:00
|
|
|
use App\Models\LicenseSeat;
|
2024-05-31 18:59:04 -07:00
|
|
|
use App\Models\Location;
|
|
|
|
use App\Models\User;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
2024-06-03 16:54:59 -07:00
|
|
|
class DeleteUserTest extends TestCase
|
2024-05-31 18:59:04 -07:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
public function testDisallowUserDeletionViaApiIfStillManagingPeople()
|
|
|
|
{
|
|
|
|
$manager = User::factory()->create();
|
|
|
|
User::factory()->count(5)->create(['manager_id' => $manager->id]);
|
|
|
|
$this->assertFalse($manager->isDeletable());
|
|
|
|
|
|
|
|
$this->actingAsForApi(User::factory()->deleteUsers()->create())
|
|
|
|
->deleteJson(route('api.users.destroy', $manager->id))
|
|
|
|
->assertOk()
|
|
|
|
->assertStatus(200)
|
|
|
|
->assertStatusMessageIs('error')
|
|
|
|
->json();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDisallowUserDeletionViaApiIfStillManagingLocations()
|
|
|
|
{
|
|
|
|
$manager = User::factory()->create();
|
|
|
|
Location::factory()->count(5)->create(['manager_id' => $manager->id]);
|
|
|
|
|
|
|
|
$this->assertFalse($manager->isDeletable());
|
|
|
|
|
|
|
|
$this->actingAsForApi(User::factory()->deleteUsers()->create())
|
|
|
|
->deleteJson(route('api.users.destroy', $manager->id))
|
|
|
|
->assertOk()
|
|
|
|
->assertStatus(200)
|
|
|
|
->assertStatusMessageIs('error')
|
|
|
|
->json();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDisallowUserDeletionViaApiIfStillHasLicenses()
|
|
|
|
{
|
|
|
|
$manager = User::factory()->create();
|
|
|
|
LicenseSeat::factory()->count(5)->create(['assigned_to' => $manager->id]);
|
|
|
|
|
|
|
|
$this->assertFalse($manager->isDeletable());
|
|
|
|
|
|
|
|
$this->actingAsForApi(User::factory()->deleteUsers()->create())
|
|
|
|
->deleteJson(route('api.users.destroy', $manager->id))
|
|
|
|
->assertOk()
|
|
|
|
->assertStatus(200)
|
|
|
|
->assertStatusMessageIs('error')
|
|
|
|
->json();
|
|
|
|
}
|
|
|
|
|
2024-06-12 04:03:33 -07:00
|
|
|
public function testPermissionsForDeletingUsers()
|
2024-05-31 18:59:04 -07:00
|
|
|
{
|
|
|
|
|
|
|
|
$this->actingAsForApi(User::factory()->create())
|
|
|
|
->deleteJson(route('api.users.destroy', User::factory()->create()))
|
|
|
|
->assertStatus(403)
|
|
|
|
->json();
|
|
|
|
}
|
|
|
|
|
2024-06-12 04:03:33 -07:00
|
|
|
public function testPermissionsIfNotInSameCompanyAndNotSuperadmin()
|
2024-05-31 19:10:29 -07:00
|
|
|
{
|
|
|
|
$this->settings->enableMultipleFullCompanySupport();
|
|
|
|
[$companyA, $companyB] = Company::factory()->count(2)->create();
|
|
|
|
|
|
|
|
$superUser = $companyA->users()->save(User::factory()->superuser()->make());
|
|
|
|
$userInCompanyA = $companyA->users()->save(User::factory()->deleteUsers()->make());
|
|
|
|
$userInCompanyB = $companyB->users()->save(User::factory()->deleteUsers()->make());
|
|
|
|
|
|
|
|
$this->actingAsForApi($userInCompanyA)
|
|
|
|
->deleteJson(route('api.users.destroy', $userInCompanyB))
|
|
|
|
->assertStatus(403)
|
|
|
|
->json();
|
|
|
|
|
|
|
|
$this->actingAsForApi($userInCompanyB)
|
|
|
|
->deleteJson(route('api.users.destroy', $userInCompanyA))
|
|
|
|
->assertStatus(403)
|
|
|
|
->json();
|
|
|
|
|
|
|
|
$this->actingAsForApi($superUser)
|
|
|
|
->deleteJson(route('api.users.destroy', $userInCompanyA))
|
|
|
|
->assertOk()
|
|
|
|
->assertStatus(200)
|
|
|
|
->assertStatusMessageIs('success')
|
|
|
|
->json();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-05-31 20:01:09 -07:00
|
|
|
public function testUsersCannotDeleteThemselves()
|
|
|
|
{
|
|
|
|
$user = User::factory()->deleteUsers()->create();
|
|
|
|
$this->actingAsForApi($user)
|
|
|
|
->deleteJson(route('api.users.destroy', $user))
|
|
|
|
->assertOk()
|
|
|
|
->assertStatus(200)
|
|
|
|
->assertStatusMessageIs('error')
|
|
|
|
->json();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-05-31 19:10:29 -07:00
|
|
|
|
|
|
|
|
2024-05-31 18:59:04 -07:00
|
|
|
|
|
|
|
|
|
|
|
}
|