Skip to content

A free open source IT asset/license management system

Implement remaining tests

Marcus Mooree84279c40220
diff --git a/app/Http/Controllers/Api/LicenseSeatsController.php b/app/Http/Controllers/Api/LicenseSeatsController.php
index fc2cda44cc..cb145cf642 100644
--- a/app/Http/Controllers/Api/LicenseSeatsController.php
+++ b/app/Http/Controllers/Api/LicenseSeatsController.php
@@ -98,10 +98,11 @@ class LicenseSeatsController extends Controller
*/
public function update(Request $request, $licenseId, $seatId) : JsonResponse | array
{
- $this->validate($request, [
+ $validated = $this->validate($request, [
'assigned_to' => [
'sometimes',
'int',
+ 'nullable',
'prohibits:asset_id',
// must be a valid user or null to unassign
function ($attribute, $value, $fail) {
@@ -113,6 +114,7 @@ class LicenseSeatsController extends Controller
'asset_id' => [
'sometimes',
'int',
+ 'nullable',
'prohibits:assigned_to',
// must be a valid asset or null to unassign
function ($attribute, $value, $fail) {
@@ -140,7 +142,7 @@ class LicenseSeatsController extends Controller
$oldAsset = $licenseSeat->asset()->first();
// attempt to update the license seat
- $licenseSeat->notes = $request->input('notes');
+ $licenseSeat->fill($validated);
// check if this update is a checkin operation
// 1. are relevant fields touched at all?
diff --git a/database/factories/LicenseSeatFactory.php b/database/factories/LicenseSeatFactory.php
index 6946a21792..7146a61333 100644
--- a/database/factories/LicenseSeatFactory.php
+++ b/database/factories/LicenseSeatFactory.php
@@ -43,6 +43,13 @@ class LicenseSeatFactory extends Factory
});
}
+ public function unreassignable()
+ {
+ return $this->afterMaking(function (LicenseSeat $seat) {
+ $seat->license->update(['reassignable' => false]);
+ });
+ }
+
public function notReassignable()
{
return $this->afterMaking(function (LicenseSeat $seat) {
diff --git a/tests/Feature/LicenseSeats/Api/LicenseSeatUpdateTest.php b/tests/Feature/LicenseSeats/Api/LicenseSeatUpdateTest.php
index 24538e1b52..d43ee698b4 100644
--- a/tests/Feature/LicenseSeats/Api/LicenseSeatUpdateTest.php
+++ b/tests/Feature/LicenseSeats/Api/LicenseSeatUpdateTest.php
@@ -162,35 +162,31 @@ class LicenseSeatUpdateTest extends TestCase
public function test_cannot_reassign_unreassignable_license_seat()
{
- $this->markTestIncomplete();
-
- $licenseSeat = LicenseSeat::factory()->unreassignable()->assignedToUser()->create();
+ $licenseSeat = LicenseSeat::factory()->assignedToUser()->create(['unreassignable_seat' => true]);
$this->actingAsForApi(User::factory()->superuser()->create())
->patchJson($this->route($licenseSeat), [
- 'assigned_to' => User::factory()->create()->id,
+ 'asset_id' => Asset::factory()->create()->id,
'notes' => 'Attempting to reassign an unreassignable seat',
])
->assertStatus(200)
->assertStatusMessageIs('error');
+
+ $licenseSeat->refresh();
+ $this->assertNull($licenseSeat->asset_id);
}
public function test_license_seat_can_be_checked_out_to_user_when_updating()
{
- $this->markTestIncomplete();
-
- $license = License::factory()->create();
- $licenseSeat = LicenseSeat::factory()->for($license)->create([
- 'assigned_to' => null,
- ]);
-
+ $licenseSeat = LicenseSeat::factory()->create(['assigned_to' => null]);
$targetUser = User::factory()->create();
$this->actingAsForApi(User::factory()->superuser()->create())
->patchJson($this->route($licenseSeat), [
'assigned_to' => $targetUser->id,
'notes' => 'Checking out the seat to a user',
- ])->assertStatus(200)
+ ])
+ ->assertStatus(200)
->assertJsonFragment([
'status' => 'success',
]);
@@ -199,31 +195,46 @@ class LicenseSeatUpdateTest extends TestCase
$this->assertEquals($targetUser->id, $licenseSeat->assigned_to);
$this->assertEquals('Checking out the seat to a user', $licenseSeat->notes);
- $this->assertHasTheseActionLogs($license, ['add seats', 'create', 'checkout']); //FIXME - backwards
+ $this->assertHasTheseActionLogs($licenseSeat->license, ['add seats', 'create', 'checkout']); //FIXME - backwards
}
public function test_license_seat_can_be_checked_out_to_asset_when_updating()
{
- $this->markTestIncomplete();
+ $licenseSeat = LicenseSeat::factory()->create(['assigned_to' => null]);
+ $targetAsset = Asset::factory()->create();
+
+ $this->actingAsForApi(User::factory()->superuser()->create())
+ ->patchJson($this->route($licenseSeat), [
+ 'asset_id' => $targetAsset->id,
+ 'notes' => 'Checking out the seat to an asset',
+ ])
+ ->assertStatus(200)
+ ->assertJsonFragment([
+ 'status' => 'success',
+ ]);
+
+ $licenseSeat->refresh();
+ $this->assertEquals($targetAsset->id, $licenseSeat->asset_id);
+ $this->assertEquals('Checking out the seat to an asset', $licenseSeat->notes);
+ $this->assertHasTheseActionLogs($licenseSeat->license, ['add seats', 'create', 'checkout']); //FIXME - backwards
}
public function test_license_seat_can_be_checked_in_when_updating()
{
- $this->markTestIncomplete();
-
- $licenseSeat = LicenseSeat::factory()->reassignable()->assignedToUser()->create();
+ $licenseSeat = LicenseSeat::factory()->unreassignable()->assignedToUser()->create();
$this->actingAsForApi(User::factory()->superuser()->create())
->patchJson($this->route($licenseSeat), [
'assigned_to' => null,
- 'notes' => '',
+ 'notes' => 'Checking in the seat',
])
->assertStatus(200)
- ->assertStatusMessageIs('error');
+ ->assertStatusMessageIs('success');
$licenseSeat->refresh();
$this->assertNull($licenseSeat->assigned_to);
+ $this->assertTrue($licenseSeat->unreassignable_seat);
}
private function route(LicenseSeat $licenseSeat)