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

155 lines
5.5 KiB
PHP
Raw Normal View History

2023-08-21 12:31:51 -07:00
<?php
namespace Tests\Feature\Api\Assets;
2024-02-14 10:48:49 -08:00
use App\Events\CheckoutableCheckedIn;
2023-08-21 12:31:51 -07:00
use App\Models\Asset;
2024-02-14 11:33:03 -08:00
use App\Models\LicenseSeat;
2024-02-14 11:11:08 -08:00
use App\Models\Location;
2024-02-14 10:48:49 -08:00
use App\Models\Statuslabel;
2023-08-21 12:31:51 -07:00
use App\Models\User;
2024-02-14 10:48:49 -08:00
use Illuminate\Support\Facades\Event;
2023-08-21 12:31:51 -07:00
use Tests\Support\InteractsWithSettings;
use Tests\TestCase;
class AssetCheckinTest extends TestCase
{
use InteractsWithSettings;
2024-02-13 12:15:59 -08:00
public function testCheckingInAssetRequiresCorrectPermission()
{
2024-02-13 17:50:26 -08:00
$this->actingAsForApi(User::factory()->create())
->postJson(route('api.asset.checkin', Asset::factory()->assignedToUser()->create()))
->assertForbidden();
2024-02-13 12:15:59 -08:00
}
2024-02-13 12:35:31 -08:00
public function testCannotCheckInNonExistentAsset()
2024-02-13 12:15:59 -08:00
{
2024-02-13 17:50:26 -08:00
$this->actingAsForApi(User::factory()->checkinAssets()->create())
->postJson(route('api.asset.checkin', ['id' => 'does-not-exist']))
->assertStatusMessageIs('error');
2024-02-13 12:15:59 -08:00
}
2024-02-13 12:35:31 -08:00
public function testCannotCheckInAssetThatIsNotCheckedOut()
2024-02-13 12:15:59 -08:00
{
2024-02-13 17:50:26 -08:00
$this->actingAsForApi(User::factory()->checkinAssets()->create())
->postJson(route('api.asset.checkin', Asset::factory()->create()->id))
->assertStatusMessageIs('error');
2024-02-13 12:15:59 -08:00
}
2024-02-13 17:50:26 -08:00
public function testAssetCanBeCheckedIn()
2024-02-13 12:15:59 -08:00
{
2024-02-14 10:48:49 -08:00
Event::fake([CheckoutableCheckedIn::class]);
$user = User::factory()->create();
2024-02-14 12:14:27 -08:00
$location = Location::factory()->create();
2024-02-14 10:48:49 -08:00
$status = Statuslabel::factory()->create();
$asset = Asset::factory()->assignedToUser($user)->create([
'expected_checkin' => now()->addDay(),
'last_checkin' => null,
'accepted' => 'accepted',
]);
$this->assertTrue($asset->assignedTo->is($user));
$this->actingAsForApi(User::factory()->checkinAssets()->create())
2024-02-14 11:11:08 -08:00
->postJson(route('api.asset.checkin', $asset), [
2024-02-14 10:48:49 -08:00
'name' => 'Changed Name',
'status_id' => $status->id,
2024-02-14 12:14:27 -08:00
'location_id' => $location->id,
2024-02-14 10:48:49 -08:00
])
->assertOk();
Event::assertDispatched(CheckoutableCheckedIn::class, 1);
$this->assertNull($asset->refresh()->assignedTo);
$this->assertNull($asset->expected_checkin);
$this->assertNull($asset->last_checkout);
$this->assertNotNull($asset->last_checkin);
$this->assertNull($asset->assignedTo);
$this->assertNull($asset->assigned_type);
$this->assertNull($asset->accepted);
$this->assertEquals('Changed Name', $asset->name);
$this->assertEquals($status->id, $asset->status_id);
2024-02-14 12:14:27 -08:00
$this->assertTrue($asset->location()->is($location));
2024-02-13 12:15:59 -08:00
}
2024-02-14 11:11:08 -08:00
public function testLocationIsSetToRTDLocationByDefaultUponCheckin()
2023-08-21 12:31:51 -07:00
{
2024-02-14 11:11:08 -08:00
$rtdLocation = Location::factory()->create();
$asset = Asset::factory()->assignedToUser()->create([
'location_id' => Location::factory()->create()->id,
'rtd_location_id' => $rtdLocation->id,
]);
2023-08-21 12:31:51 -07:00
2024-02-14 11:11:08 -08:00
$this->actingAsForApi(User::factory()->checkinAssets()->create())
->postJson(route('api.asset.checkin', $asset->id));
$this->assertTrue($asset->refresh()->location()->is($rtdLocation));
}
public function testDefaultLocationCanBeUpdatedUponCheckin()
{
2024-02-14 11:33:03 -08:00
$location = Location::factory()->create();
$asset = Asset::factory()->assignedToUser()->create();
$this->actingAsForApi(User::factory()->checkinAssets()->create())
->postJson(route('api.asset.checkin', $asset), [
'location_id' => $location->id,
'update_default_location' => 0
]);
$this->assertTrue($asset->refresh()->defaultLoc()->is($location));
2024-02-14 11:11:08 -08:00
}
public function testAssetsLicenseSeatsAreClearedUponCheckin()
{
2024-02-14 11:33:03 -08:00
$asset = Asset::factory()->assignedToUser()->create();
LicenseSeat::factory()->assignedToUser()->for($asset)->create();
$this->assertNotNull($asset->licenseseats->first()->assigned_to);
$this->actingAsForApi(User::factory()->checkinAssets()->create())
->postJson(route('api.asset.checkin', $asset));
$this->assertNull($asset->refresh()->licenseseats->first()->assigned_to);
2024-02-14 11:11:08 -08:00
}
public function testLegacyLocationValuesSetToZeroAreUpdated()
{
$asset = Asset::factory()->canBeInvalidUponCreation()->assignedToUser()->create([
'rtd_location_id' => 0,
'location_id' => 0,
]);
$this->actingAsForApi(User::factory()->checkinAssets()->create())
->postJson(route('api.asset.checkin', $asset));
$this->assertNull($asset->refresh()->rtd_location_id);
$this->assertEquals($asset->location_id, $asset->rtd_location_id);
2023-08-21 12:31:51 -07:00
}
2024-02-13 12:35:31 -08:00
public function testPendingCheckoutAcceptancesAreClearedUponCheckin()
2024-02-14 11:11:08 -08:00
{
$this->markTestIncomplete('Not currently in controller');
}
public function testCheckinTimeAndActionLogNoteCanBeSet()
2024-02-13 12:35:31 -08:00
{
2024-02-14 11:33:03 -08:00
$this->markTestIncomplete(
'checkin_at currently takes a date and applies a time which is not inline with what the web controller does.'
);
Event::fake();
$this->actingAsForApi(User::factory()->checkinAssets()->create())
->postJson(route('api.asset.checkin', Asset::factory()->assignedToUser()->create()), [
'checkin_at' => '2023-01-02 12:34:56',
'note' => 'hi there',
]);
Event::assertDispatched(function (CheckoutableCheckedIn $event) {
return $event->action_date === '2023-01-02 12:34:56' && $event->note === 'hi there';
}, 1);
2024-02-13 12:35:31 -08:00
}
2023-08-21 12:31:51 -07:00
}