mirror of
https://github.com/snipe/snipe-it.git
synced 2026-04-07 00:08:06 +00:00
192 lines
6.1 KiB
PHP
192 lines
6.1 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Users\Api;
|
|
|
|
use App\Models\Company;
|
|
use App\Models\LicenseSeat;
|
|
use App\Models\Location;
|
|
use App\Models\User;
|
|
use Tests\Concerns\TestsFullMultipleCompaniesSupport;
|
|
use Tests\Concerns\TestsPermissionsRequirement;
|
|
use Tests\TestCase;
|
|
|
|
class DeleteUsersTest extends TestCase implements TestsFullMultipleCompaniesSupport, TestsPermissionsRequirement
|
|
{
|
|
public function test_requires_permission()
|
|
{
|
|
$user = User::factory()->create();
|
|
|
|
$this->actingAsForApi(User::factory()->create())
|
|
->deleteJson(route('api.users.destroy', $user))
|
|
->assertForbidden();
|
|
|
|
$this->assertNotSoftDeleted($user);
|
|
}
|
|
|
|
public function test_error_returned_via_api_if_user_does_not_exist()
|
|
{
|
|
$this->actingAsForApi(User::factory()->deleteUsers()->create())
|
|
->deleteJson(route('api.users.destroy', 'invalid-id'))
|
|
->assertOk()
|
|
->assertStatus(200)
|
|
->assertStatusMessageIs('error')
|
|
->json();
|
|
}
|
|
|
|
public function test_error_returned_via_api_if_user_is_already_deleted()
|
|
{
|
|
$user = User::factory()->deletedUser()->create();
|
|
$this->actingAsForApi(User::factory()->deleteUsers()->create())
|
|
->deleteJson(route('api.users.destroy', $user))
|
|
->assertOk()
|
|
->assertStatus(200)
|
|
->assertStatusMessageIs('error')
|
|
->json();
|
|
}
|
|
|
|
public function test_disallow_user_deletion_via_api_if_still_managing_people()
|
|
{
|
|
$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))
|
|
->assertOk()
|
|
->assertStatus(200)
|
|
->assertStatusMessageIs('error')
|
|
->json();
|
|
}
|
|
|
|
public function test_disallow_user_deletion_via_api_if_still_managing_locations()
|
|
{
|
|
$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))
|
|
->assertOk()
|
|
->assertStatus(200)
|
|
->assertStatusMessageIs('error')
|
|
->json();
|
|
}
|
|
|
|
public function test_disallow_user_deletion_via_api_if_still_has_licenses()
|
|
{
|
|
$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))
|
|
->assertOk()
|
|
->assertStatus(200)
|
|
->assertStatusMessageIs('error')
|
|
->json();
|
|
}
|
|
|
|
public function test_users_cannot_delete_themselves()
|
|
{
|
|
$user = User::factory()->deleteUsers()->create();
|
|
$this->actingAsForApi($user)
|
|
->deleteJson(route('api.users.destroy', $user))
|
|
->assertOk()
|
|
->assertStatus(200)
|
|
->assertStatusMessageIs('error')
|
|
->json();
|
|
|
|
}
|
|
|
|
public function test_adheres_to_full_multiple_companies_support_scoping()
|
|
{
|
|
$this->settings->enableMultipleFullCompanySupport();
|
|
|
|
[$companyA, $companyB] = Company::factory()->count(2)->create();
|
|
|
|
$superuser = User::factory()->superuser()->create();
|
|
$userFromA = User::factory()->deleteUsers()->for($companyA)->create();
|
|
$userFromB = User::factory()->deleteUsers()->for($companyB)->create();
|
|
|
|
$this->actingAsForApi($userFromA)
|
|
->deleteJson(route('api.users.destroy', $userFromB))
|
|
->assertOk()
|
|
->assertStatus(200)
|
|
->assertStatusMessageIs('error')
|
|
->json();
|
|
|
|
$userFromB->refresh();
|
|
$this->assertNull($userFromB->deleted_at);
|
|
|
|
$this->actingAsForApi($userFromB)
|
|
->deleteJson(route('api.users.destroy', $userFromA))
|
|
->assertOk()
|
|
->assertStatus(200)
|
|
->assertStatusMessageIs('error')
|
|
->json();
|
|
|
|
$userFromA->refresh();
|
|
$this->assertNull($userFromA->deleted_at);
|
|
|
|
$this->actingAsForApi($superuser)
|
|
->deleteJson(route('api.users.destroy', $userFromA))
|
|
->assertOk()
|
|
->assertStatus(200)
|
|
->assertStatusMessageIs('success')
|
|
->json();
|
|
|
|
$userFromA->refresh();
|
|
$this->assertNotNull($userFromA->deleted_at);
|
|
}
|
|
|
|
public function test_can_delete_user()
|
|
{
|
|
$user = User::factory()->create();
|
|
|
|
$this->actingAsForApi(User::factory()->deleteUsers()->create())
|
|
->deleteJson(route('api.users.destroy', $user))
|
|
->assertOk()
|
|
->assertStatusMessageIs('success');
|
|
|
|
$this->assertSoftDeleted($user);
|
|
}
|
|
|
|
public function test_admin_cannot_delete_super_user()
|
|
{
|
|
$superuser = User::factory()->superuser()->create();
|
|
$admin = User::factory()->admin()->create();
|
|
|
|
$this->actingAsForApi($admin)
|
|
->deleteJson(route('api.users.destroy', $superuser))
|
|
->assertOk()
|
|
->assertStatusMessageIs('error');
|
|
|
|
}
|
|
|
|
public function test_user_cannot_delete_admin_user()
|
|
{
|
|
$user = User::factory()->deleteUsers()->create();
|
|
$admin = User::factory()->admin()->create();
|
|
|
|
$this->actingAsForApi($user)
|
|
->deleteJson(route('api.users.destroy', $admin))
|
|
->assertOk()
|
|
->assertStatusMessageIs('error');
|
|
|
|
}
|
|
|
|
public function test_user_cannot_delete_super_user()
|
|
{
|
|
$user = User::factory()->deleteUsers()->create();
|
|
$superuser = User::factory()->superuser()->create();
|
|
|
|
$this->actingAsForApi($user)
|
|
->deleteJson(route('api.users.destroy', $superuser))
|
|
->assertOk()
|
|
->assertStatusMessageIs('error');
|
|
|
|
}
|
|
}
|