2024-05-07 10:59:24 -07:00
|
|
|
<?php
|
|
|
|
|
2024-06-04 10:48:53 -07:00
|
|
|
namespace Tests\Feature\Users\Ui;
|
2024-05-07 10:59:24 -07:00
|
|
|
|
2024-06-05 06:16:28 -07:00
|
|
|
use Tests\TestCase;
|
2024-06-05 06:02:04 -07:00
|
|
|
use App\Models\LicenseSeat;
|
2024-05-07 10:59:24 -07:00
|
|
|
use App\Models\Location;
|
2024-06-05 06:03:33 -07:00
|
|
|
use App\Models\Accessory;
|
2024-05-07 10:59:24 -07:00
|
|
|
use App\Models\User;
|
2024-06-12 04:31:41 -07:00
|
|
|
use App\Models\Company;
|
2024-06-05 06:16:28 -07:00
|
|
|
|
|
|
|
use App\Models\Asset;
|
2024-05-07 10:59:24 -07:00
|
|
|
|
2024-06-03 16:54:59 -07:00
|
|
|
class DeleteUserTest extends TestCase
|
2024-05-07 10:59:24 -07:00
|
|
|
{
|
|
|
|
|
2024-06-22 09:45:42 -07:00
|
|
|
public function testUserCanDeleteAnotherUser()
|
|
|
|
{
|
|
|
|
$user = User::factory()->deleteUsers()->viewUsers()->create();
|
|
|
|
$this->actingAs(User::factory()->deleteUsers()->viewUsers()->create())->assertTrue($user->isDeletable());
|
|
|
|
|
|
|
|
$response = $this->actingAs(User::factory()->deleteUsers()->viewUsers()->create())
|
|
|
|
->delete(route('users.destroy', ['user' => $user->id]))
|
|
|
|
->assertStatus(302)
|
|
|
|
->assertRedirect(route('users.index'));
|
|
|
|
|
|
|
|
$this->followRedirects($response)->assertSee(trans('general.notification_success'));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function testErrorReturnedIfUserDoesNotExist()
|
|
|
|
{
|
|
|
|
$response = $this->actingAs(User::factory()->deleteUsers()->viewUsers()->create())
|
|
|
|
->delete(route('users.destroy', ['user' => '40596803548609346']))
|
|
|
|
->assertStatus(302)
|
|
|
|
->assertRedirect(route('users.index'));
|
2024-06-22 10:36:02 -07:00
|
|
|
$this->followRedirects($response)->assertSee(trans('alert-danger'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testErrorReturnedIfUserIsAlreadyDeleted()
|
|
|
|
{
|
|
|
|
$user = User::factory()->deletedUser()->viewUsers()->create();
|
|
|
|
$response = $this->actingAs(User::factory()->deleteUsers()->viewUsers()->create())
|
|
|
|
->delete(route('users.destroy', $user->id))
|
|
|
|
->assertStatus(302)
|
|
|
|
->assertRedirect(route('users.index'));
|
|
|
|
|
|
|
|
$this->followRedirects($response)->assertSee(trans('general.error'));
|
2024-06-22 09:45:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-06-12 04:12:15 -07:00
|
|
|
public function testPermissionsToDeleteUser()
|
|
|
|
{
|
|
|
|
|
|
|
|
$this->settings->enableMultipleFullCompanySupport();
|
|
|
|
|
|
|
|
[$companyA, $companyB] = Company::factory()->count(2)->create();
|
|
|
|
|
|
|
|
$superuser = User::factory()->superuser()->create();
|
|
|
|
$userFromA = User::factory()->for($companyA)->create();
|
|
|
|
$userFromB = User::factory()->for($companyB)->create();
|
|
|
|
|
|
|
|
$this->followingRedirects()->actingAs(User::factory()->deleteUsers()->for($companyA)->create())
|
|
|
|
->delete(route('users.destroy', ['user' => $userFromB->id]))
|
|
|
|
->assertStatus(403);
|
|
|
|
|
|
|
|
$this->actingAs(User::factory()->deleteUsers()->for($companyA)->create())
|
|
|
|
->delete(route('users.destroy', ['user' => $userFromA->id]))
|
|
|
|
->assertStatus(302)
|
|
|
|
->assertRedirect(route('users.index'));
|
|
|
|
|
|
|
|
}
|
2024-05-07 10:59:24 -07:00
|
|
|
|
2024-06-12 04:31:41 -07:00
|
|
|
|
2024-05-07 10:59:24 -07:00
|
|
|
public function testDisallowUserDeletionIfStillManagingPeople()
|
|
|
|
{
|
2024-05-31 20:01:09 -07:00
|
|
|
$manager = User::factory()->create();
|
2024-06-05 06:09:57 -07:00
|
|
|
User::factory()->count(1)->create(['manager_id' => $manager->id]);
|
2024-05-31 20:01:09 -07:00
|
|
|
|
2024-06-05 06:13:51 -07:00
|
|
|
$this->actingAs(User::factory()->deleteUsers()->viewUsers()->create())->assertFalse($manager->isDeletable());
|
2024-05-31 20:01:09 -07:00
|
|
|
|
2024-05-31 20:24:47 -07:00
|
|
|
$response = $this->actingAs(User::factory()->deleteUsers()->viewUsers()->create())
|
2024-05-31 20:01:09 -07:00
|
|
|
->delete(route('users.destroy', $manager->id))
|
|
|
|
->assertStatus(302)
|
|
|
|
->assertRedirect(route('users.index'));
|
2024-05-31 20:24:47 -07:00
|
|
|
|
|
|
|
$this->followRedirects($response)->assertSee('Error');
|
2024-05-07 10:59:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testDisallowUserDeletionIfStillManagingLocations()
|
|
|
|
{
|
2024-05-31 20:01:09 -07:00
|
|
|
$manager = User::factory()->create();
|
2024-06-05 06:09:57 -07:00
|
|
|
Location::factory()->count(2)->create(['manager_id' => $manager->id]);
|
2024-05-31 20:01:09 -07:00
|
|
|
|
2024-06-05 06:13:51 -07:00
|
|
|
$this->actingAs(User::factory()->deleteUsers()->viewUsers()->create())->assertFalse($manager->isDeletable());
|
2024-05-31 20:01:09 -07:00
|
|
|
|
2024-05-31 20:24:47 -07:00
|
|
|
$response = $this->actingAs(User::factory()->deleteUsers()->viewUsers()->create())
|
2024-05-31 20:01:09 -07:00
|
|
|
->delete(route('users.destroy', $manager->id))
|
|
|
|
->assertStatus(302)
|
|
|
|
->assertRedirect(route('users.index'));
|
2024-05-31 20:24:47 -07:00
|
|
|
|
|
|
|
$this->followRedirects($response)->assertSee('Error');
|
2024-05-07 10:59:24 -07:00
|
|
|
}
|
|
|
|
|
2024-06-05 06:02:04 -07:00
|
|
|
public function testDisallowUserDeletionIfStillHaveAccessories()
|
|
|
|
{
|
|
|
|
$user = User::factory()->create();
|
2024-06-05 06:09:57 -07:00
|
|
|
Accessory::factory()->count(3)->checkedOutToUser($user)->create();
|
2024-06-05 06:02:04 -07:00
|
|
|
|
2024-06-05 06:13:51 -07:00
|
|
|
$this->actingAs(User::factory()->deleteUsers()->viewUsers()->create())->assertFalse($user->isDeletable());
|
2024-06-05 06:02:04 -07:00
|
|
|
|
|
|
|
$response = $this->actingAs(User::factory()->deleteUsers()->viewUsers()->create())
|
|
|
|
->delete(route('users.destroy', $user->id))
|
|
|
|
->assertStatus(302)
|
|
|
|
->assertRedirect(route('users.index'));
|
|
|
|
|
|
|
|
$this->followRedirects($response)->assertSee('Error');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDisallowUserDeletionIfStillHaveLicenses()
|
|
|
|
{
|
|
|
|
$user = User::factory()->create();
|
2024-06-05 06:09:57 -07:00
|
|
|
LicenseSeat::factory()->count(4)->create(['assigned_to' => $user->id]);
|
2024-06-05 06:02:04 -07:00
|
|
|
|
2024-06-05 06:13:51 -07:00
|
|
|
$this->actingAs(User::factory()->deleteUsers()->viewUsers()->create())->assertFalse($user->isDeletable());
|
2024-06-05 06:02:04 -07:00
|
|
|
|
|
|
|
$response = $this->actingAs(User::factory()->deleteUsers()->viewUsers()->create())
|
|
|
|
->delete(route('users.destroy', $user->id))
|
|
|
|
->assertStatus(302)
|
|
|
|
->assertRedirect(route('users.index'));
|
|
|
|
|
|
|
|
$this->followRedirects($response)->assertSee('Error');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-05-07 10:59:24 -07:00
|
|
|
public function testAllowUserDeletionIfNotManagingLocations()
|
|
|
|
{
|
2024-05-31 20:01:09 -07:00
|
|
|
$manager = User::factory()->create();
|
2024-06-05 06:13:51 -07:00
|
|
|
$this->actingAs(User::factory()->deleteUsers()->viewUsers()->create())->assertTrue($manager->isDeletable());
|
2024-05-31 20:01:09 -07:00
|
|
|
|
2024-05-31 20:24:47 -07:00
|
|
|
$response = $this->actingAs(User::factory()->deleteUsers()->viewUsers()->create())
|
2024-05-31 20:01:09 -07:00
|
|
|
->delete(route('users.destroy', $manager->id))
|
|
|
|
->assertStatus(302)
|
|
|
|
->assertRedirect(route('users.index'));
|
2024-05-31 20:24:47 -07:00
|
|
|
|
|
|
|
$this->followRedirects($response)->assertSee('Success');
|
|
|
|
|
2024-05-07 10:59:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testDisallowUserDeletionIfNoDeletePermissions()
|
|
|
|
{
|
2024-05-31 20:01:09 -07:00
|
|
|
$manager = User::factory()->create();
|
2024-05-07 10:59:24 -07:00
|
|
|
Location::factory()->create(['manager_id' => $manager->id]);
|
2024-06-05 06:13:51 -07:00
|
|
|
$this->actingAs(User::factory()->editUsers()->viewUsers()->create())->assertFalse($manager->isDeletable());
|
2024-05-07 10:59:24 -07:00
|
|
|
}
|
|
|
|
|
2024-06-05 06:16:28 -07:00
|
|
|
public function testDisallowUserDeletionIfTheyStillHaveAssets()
|
|
|
|
{
|
|
|
|
$user = User::factory()->create();
|
2024-06-05 06:50:45 -07:00
|
|
|
$asset = Asset::factory()->create();
|
|
|
|
|
|
|
|
$this->actingAs(User::factory()->checkoutAssets()->create())
|
|
|
|
->post(route('hardware.checkout.store', $asset->id), [
|
|
|
|
'checkout_to_type' => 'user',
|
|
|
|
'assigned_user' => $user->id,
|
|
|
|
'name' => 'Changed Name',
|
|
|
|
]);
|
2024-06-05 06:16:28 -07:00
|
|
|
|
|
|
|
$this->actingAs(User::factory()->deleteUsers()->viewUsers()->create())->assertFalse($user->isDeletable());
|
|
|
|
|
|
|
|
$response = $this->actingAs(User::factory()->deleteUsers()->viewUsers()->create())
|
|
|
|
->delete(route('users.destroy', $user->id))
|
|
|
|
->assertStatus(302)
|
|
|
|
->assertRedirect(route('users.index'));
|
|
|
|
|
|
|
|
$this->followRedirects($response)->assertSee('Error');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-06-05 06:02:04 -07:00
|
|
|
public function testUsersCannotDeleteThemselves()
|
|
|
|
{
|
2024-06-05 06:13:51 -07:00
|
|
|
$manager = User::factory()->deleteUsers()->viewUsers()->create();
|
|
|
|
$this->actingAs(User::factory()->deleteUsers()->viewUsers()->create())->assertTrue($manager->isDeletable());
|
2024-06-05 06:02:04 -07:00
|
|
|
|
|
|
|
$response = $this->actingAs($manager)
|
|
|
|
->delete(route('users.destroy', $manager->id))
|
|
|
|
->assertStatus(302)
|
|
|
|
->assertRedirect(route('users.index'));
|
|
|
|
|
|
|
|
$this->followRedirects($response)->assertSee('Error');
|
|
|
|
}
|
|
|
|
|
2024-05-07 10:59:24 -07:00
|
|
|
|
|
|
|
}
|