snipe-it/tests/Feature/Checkouts/Api/AssetCheckoutTest.php

215 lines
7.7 KiB
PHP
Raw Normal View History

2024-04-10 17:08:45 -07:00
<?php
2024-06-04 10:48:53 -07:00
namespace Tests\Feature\Checkouts\Api;
2024-04-10 17:08:45 -07:00
2024-08-06 13:25:21 -07:00
use PHPUnit\Framework\Attributes\DataProvider;
2024-04-10 17:08:45 -07:00
use App\Events\CheckoutableCheckedOut;
use App\Models\Asset;
2024-04-10 17:27:07 -07:00
use App\Models\Location;
use App\Models\Statuslabel;
2024-04-10 17:08:45 -07:00
use App\Models\User;
2024-04-10 17:27:07 -07:00
use Illuminate\Support\Carbon;
2024-04-10 17:08:45 -07:00
use Illuminate\Support\Facades\Event;
use Tests\TestCase;
2024-08-06 13:25:22 -07:00
final class AssetCheckoutTest extends TestCase
2024-04-10 17:08:45 -07:00
{
protected function setUp(): void
{
parent::setUp();
Event::fake([CheckoutableCheckedOut::class]);
}
2024-08-06 13:25:22 -07:00
public function testCheckingOutAssetRequiresCorrectPermission(): void
2024-04-10 17:08:45 -07:00
{
$this->actingAsForApi(User::factory()->create())
->postJson(route('api.asset.checkout', Asset::factory()->create()), [
'checkout_to_type' => 'user',
'assigned_user' => User::factory()->create()->id,
])
->assertForbidden();
}
2024-08-06 13:25:22 -07:00
public function testNonExistentAssetCannotBeCheckedOut(): void
2024-04-10 17:08:45 -07:00
{
$this->actingAsForApi(User::factory()->checkoutAssets()->create())
->postJson(route('api.asset.checkout', 1000), [
'checkout_to_type' => 'user',
'assigned_user' => User::factory()->create()->id,
])
->assertStatusMessageIs('error');
}
2024-08-06 13:25:22 -07:00
public function testAssetNotAvailableForCheckoutCannotBeCheckedOut(): void
2024-04-10 17:08:45 -07:00
{
$assetAlreadyCheckedOut = Asset::factory()->assignedToUser()->create();
$this->actingAsForApi(User::factory()->checkoutAssets()->create())
->postJson(route('api.asset.checkout', $assetAlreadyCheckedOut), [
'checkout_to_type' => 'user',
'assigned_user' => User::factory()->create()->id,
])
->assertStatusMessageIs('error');
}
2024-08-06 13:25:22 -07:00
public function testAssetCannotBeCheckedOutToItself(): void
2024-04-10 17:27:07 -07:00
{
2024-04-10 17:29:44 -07:00
$asset = Asset::factory()->create();
$this->actingAsForApi(User::factory()->checkoutAssets()->create())
->postJson(route('api.asset.checkout', $asset), [
'checkout_to_type' => 'asset',
'assigned_asset' => $asset->id,
])
->assertStatusMessageIs('error');
2024-04-10 17:27:07 -07:00
}
2024-08-06 13:25:22 -07:00
public function testValidationWhenCheckingOutAsset(): void
2024-04-10 17:08:45 -07:00
{
$this->actingAsForApi(User::factory()->checkoutAssets()->create())
->postJson(route('api.asset.checkout', Asset::factory()->create()), [])
->assertStatusMessageIs('error');
Event::assertNotDispatched(CheckoutableCheckedOut::class);
}
2024-08-06 13:25:22 -07:00
public function testCannotCheckoutAcrossCompaniesWhenFullCompanySupportEnabled(): void
2024-04-10 17:08:45 -07:00
{
$this->markTestIncomplete('This is not implemented');
}
2024-04-10 17:27:07 -07:00
/**
* This data provider contains checkout targets along with the
* asset's expected location after the checkout process.
*/
2024-08-06 13:25:21 -07:00
public static function checkoutTargets(): array
2024-04-10 17:08:45 -07:00
{
2024-04-10 17:27:07 -07:00
return [
2024-05-22 13:33:41 -07:00
'Checkout to User' => [
2024-05-22 13:33:12 -07:00
function () {
$userLocation = Location::factory()->create();
2024-05-22 13:33:41 -07:00
$user = User::factory()->for($userLocation)->create();
2024-05-22 13:33:12 -07:00
return [
'checkout_type' => 'user',
'target' => $user,
2024-05-22 13:33:41 -07:00
'expected_location' => $userLocation,
2024-05-22 13:33:12 -07:00
];
}
],
2024-05-22 13:33:41 -07:00
'Checkout to User without location set' => [
2024-05-22 12:47:33 -07:00
function () {
$userLocation = Location::factory()->create();
2024-05-22 13:33:41 -07:00
$user = User::factory()->for($userLocation)->create(['location_id' => null]);
2024-05-22 12:47:33 -07:00
return [
'checkout_type' => 'user',
'target' => $user,
2024-05-22 13:33:41 -07:00
'expected_location' => null,
2024-05-22 12:47:33 -07:00
];
}
],
2024-05-22 13:33:41 -07:00
'Checkout to Asset with location set' => [
2024-05-22 12:47:33 -07:00
function () {
$rtdLocation = Location::factory()->create();
2024-05-22 13:33:41 -07:00
$location = Location::factory()->create();
$asset = Asset::factory()->for($location)->for($rtdLocation, 'defaultLoc')->create();
2024-05-22 12:47:33 -07:00
return [
'checkout_type' => 'asset',
'target' => $asset,
2024-05-22 13:33:41 -07:00
'expected_location' => $location,
2024-05-22 12:47:33 -07:00
];
}
],
2024-05-22 13:33:41 -07:00
'Checkout to Asset without location set' => [
2024-05-22 12:47:33 -07:00
function () {
$rtdLocation = Location::factory()->create();
2024-05-22 13:33:41 -07:00
$asset = Asset::factory()->for($rtdLocation, 'defaultLoc')->create(['location_id' => null]);
2024-05-22 12:47:33 -07:00
return [
'checkout_type' => 'asset',
'target' => $asset,
2024-05-22 13:33:41 -07:00
'expected_location' => null,
2024-05-22 12:47:33 -07:00
];
}
],
'Checkout to Location' => [
function () {
$location = Location::factory()->create();
return [
'checkout_type' => 'location',
'target' => $location,
'expected_location' => $location,
];
}
],
2024-04-10 17:27:07 -07:00
];
}
2024-08-06 13:25:21 -07:00
#[DataProvider('checkoutTargets')]
2024-08-06 13:25:22 -07:00
public function testAssetCanBeCheckedOut($data): void
2024-04-10 17:27:07 -07:00
{
['checkout_type' => $type, 'target' => $target, 'expected_location' => $expectedLocation] = $data();
$newStatus = Statuslabel::factory()->readyToDeploy()->create();
2024-05-22 13:23:49 -07:00
$asset = Asset::factory()->forLocation()->create();
2024-04-10 17:27:07 -07:00
$admin = User::factory()->checkoutAssets()->create();
$this->actingAsForApi($admin)
->postJson(route('api.asset.checkout', $asset), [
'checkout_to_type' => $type,
2024-05-22 15:38:23 -07:00
'assigned_'.$type => $target->id,
2024-04-10 17:27:07 -07:00
'status_id' => $newStatus->id,
'checkout_at' => '2024-04-01',
'expected_checkin' => '2024-04-08',
'name' => 'Changed Name',
'note' => 'Here is a cool note!',
2024-05-22 15:38:23 -07:00
])
->assertOk();
2024-04-10 17:27:07 -07:00
$asset->refresh();
$this->assertTrue($asset->assignedTo()->is($target));
$this->assertEquals('Changed Name', $asset->name);
$this->assertTrue($asset->assetstatus->is($newStatus));
$this->assertEquals('2024-04-01 00:00:00', $asset->last_checkout);
2024-05-22 15:38:23 -07:00
$this->assertEquals('2024-04-08 00:00:00', (string) $asset->expected_checkin);
2024-04-10 17:27:07 -07:00
2024-05-22 13:15:51 -07:00
$expectedLocation
? $this->assertTrue($asset->location->is($expectedLocation))
: $this->assertNull($asset->location);
2024-04-10 17:27:07 -07:00
Event::assertDispatched(CheckoutableCheckedOut::class, 1);
Event::assertDispatched(function (CheckoutableCheckedOut $event) use ($admin, $asset, $target) {
2024-05-22 15:38:23 -07:00
$this->assertTrue($event->checkoutable->is($asset));
$this->assertTrue($event->checkedOutTo->is($target));
$this->assertTrue($event->checkedOutBy->is($admin));
$this->assertEquals('Here is a cool note!', $event->note);
return true;
2024-04-10 17:27:07 -07:00
});
2024-04-10 17:08:45 -07:00
}
2024-08-06 13:25:22 -07:00
public function testLicenseSeatsAreAssignedToUserUponCheckout(): void
2024-04-10 17:08:45 -07:00
{
$this->markTestIncomplete('This is not implemented');
}
2024-08-06 13:25:22 -07:00
public function testLastCheckoutUsesCurrentDateIfNotProvided(): void
2024-04-10 17:08:45 -07:00
{
2024-04-10 17:27:07 -07:00
$asset = Asset::factory()->create(['last_checkout' => now()->subMonth()]);
$this->actingAsForApi(User::factory()->checkoutAssets()->create())
->postJson(route('api.asset.checkout', $asset), [
'checkout_to_type' => 'user',
'assigned_user' => User::factory()->create()->id,
]);
$asset->refresh();
$this->assertTrue(Carbon::parse($asset->last_checkout)->diffInSeconds(now()) < 2);
2024-04-10 17:08:45 -07:00
}
}