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

184 lines
6.4 KiB
PHP
Raw Normal View History

2024-04-10 17:08:45 -07:00
<?php
namespace Tests\Feature\Api\Assets;
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;
class AssetCheckoutTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
Event::fake([CheckoutableCheckedOut::class]);
}
public function testCheckingOutAssetRequiresCorrectPermission()
{
$this->actingAsForApi(User::factory()->create())
->postJson(route('api.asset.checkout', Asset::factory()->create()), [
'checkout_to_type' => 'user',
'assigned_user' => User::factory()->create()->id,
])
->assertForbidden();
}
public function testNonExistentAssetCannotBeCheckedOut()
{
$this->actingAsForApi(User::factory()->checkoutAssets()->create())
->postJson(route('api.asset.checkout', 1000), [
'checkout_to_type' => 'user',
'assigned_user' => User::factory()->create()->id,
])
->assertStatusMessageIs('error');
}
public function testAssetNotAvailableForCheckoutCannotBeCheckedOut()
{
$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-04-10 17:27:07 -07:00
public function testAssetCannotBeCheckedOutToItself()
{
$this->markTestIncomplete();
}
2024-04-10 17:08:45 -07:00
public function testValidationWhenCheckingOutAsset()
{
$this->actingAsForApi(User::factory()->checkoutAssets()->create())
->postJson(route('api.asset.checkout', Asset::factory()->create()), [])
->assertStatusMessageIs('error');
Event::assertNotDispatched(CheckoutableCheckedOut::class);
}
public function testCannotCheckoutAcrossCompaniesWhenFullCompanySupportEnabled()
{
$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.
*/
public function checkoutTargets(): array
2024-04-10 17:08:45 -07:00
{
2024-04-10 17:27:07 -07:00
return [
'User' => [function () {
$userLocation = Location::factory()->create();
$user = User::factory()->for($userLocation)->create();
return [
'checkout_type' => 'user',
'target' => $user,
'expected_location' => $userLocation,
];
}],
'Asset without location set' => [function () {
$rtdLocation = Location::factory()->create();
$asset = Asset::factory()->for($rtdLocation, 'defaultLoc')->create(['location_id' => null]);
return [
'checkout_type' => 'asset',
'target' => $asset,
'expected_location' => $rtdLocation,
];
}],
'Asset with location set' => [function () {
$rtdLocation = Location::factory()->create();
$location = Location::factory()->create();
$asset = Asset::factory()->for($location)->for($rtdLocation, 'defaultLoc')->create();
return [
'checkout_type' => 'asset',
'target' => $asset,
'expected_location' => $location,
];
}],
'Location' => [function () {
$location = Location::factory()->create();
return [
'checkout_type' => 'location',
'target' => $location,
'expected_location' => $location,
];
}],
];
}
/** @dataProvider checkoutTargets */
public function testAssetCanBeCheckedOut($data)
{
// $this->markTestIncomplete();
['checkout_type' => $type, 'target' => $target, 'expected_location' => $expectedLocation] = $data();
$newStatus = Statuslabel::factory()->readyToDeploy()->create();
$asset = Asset::factory()->create();
$admin = User::factory()->checkoutAssets()->create();
$this->actingAsForApi($admin)
->postJson(route('api.asset.checkout', $asset), [
'checkout_to_type' => $type,
'assigned_' . $type => $target->id,
'status_id' => $newStatus->id,
'checkout_at' => '2024-04-01',
'expected_checkin' => '2024-04-08',
'name' => 'Changed Name',
'note' => 'Here is a cool note!',
]);
$asset->refresh();
$this->assertTrue($asset->assignedTo()->is($target));
$this->assertTrue($asset->location->is($expectedLocation));
$this->assertEquals('Changed Name', $asset->name);
$this->assertTrue($asset->assetstatus->is($newStatus));
$this->assertEquals('2024-04-01 00:00:00', $asset->last_checkout);
$this->assertEquals('2024-04-08 00:00:00', (string)$asset->expected_checkin);
Event::assertDispatched(CheckoutableCheckedOut::class, 1);
Event::assertDispatched(function (CheckoutableCheckedOut $event) use ($admin, $asset, $target) {
return $event->checkoutable->is($asset)
&& $event->checkedOutTo->is($target)
&& $event->checkedOutBy->is($admin)
&& $event->note === 'Here is a cool note!';
});
2024-04-10 17:08:45 -07:00
}
public function testLicenseSeatsAreAssignedToUserUponCheckout()
{
$this->markTestIncomplete('This is not implemented');
}
public function testLastCheckoutUsesCurrentDateIfNotProvided()
{
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
}
}