Skip to content

A free open source IT asset/license management system

Scaffold tests

Marcus Moore5055aafe1dcb
diff --git a/app/Http/Controllers/Api/LicenseSeatsController.php b/app/Http/Controllers/Api/LicenseSeatsController.php
index 17953cce66..cbb078ed7c 100644
--- a/app/Http/Controllers/Api/LicenseSeatsController.php
+++ b/app/Http/Controllers/Api/LicenseSeatsController.php
@@ -98,7 +98,18 @@ class LicenseSeatsController extends Controller
*/
public function update(Request $request, $licenseId, $seatId) : JsonResponse | array
{
- $this->validate($request, ['assigned_to' => 'int']);
+ $this->validate($request, [
+ 'assigned_to' => [
+ 'sometimes',
+ 'int',
+ // must be a valid user or null to unassign
+ function ($attribute, $value, $fail) {
+ if (!is_null($value) && !User::find($value)) {
+ $fail('The selected assigned_to is invalid.');
+ }
+ },
+ ],
+ ]);
$this->authorize('checkout', License::class);
diff --git a/tests/Feature/Checkouts/Api/LicenseCheckOutTest.php b/tests/Feature/Checkouts/Api/LicenseCheckOutTest.php
index ed3ff82ed0..eef019d55e 100644
--- a/tests/Feature/Checkouts/Api/LicenseCheckOutTest.php
+++ b/tests/Feature/Checkouts/Api/LicenseCheckOutTest.php
@@ -40,11 +40,14 @@ class LicenseCheckOutTest extends TestCase {
$this->assertHasTheseActionLogs($license, ['add seats', 'create', 'checkout']); //FIXME - backwards
}
+ public function test_license_update_without_checkout()
+ {
+ $this->markTestIncomplete();
+ }
+
public function test_assigned_to_cannot_be_array()
{
- $licenseSeat = LicenseSeat::factory()->create([
- 'assigned_to' => null,
- ]);
+ $licenseSeat = LicenseSeat::factory()->create(['assigned_to' => null]);
$targets = User::factory()->count(2)->create();
@@ -60,6 +63,53 @@ class LicenseCheckOutTest extends TestCase {
]
)
->assertStatus(200)
+ ->assertStatusMessageIs('error')
+ ->assertMessagesContains('assigned_to');
+ }
+
+ public function test_assigned_to_must_be_valid_user()
+ {
+ $licenseSeat = LicenseSeat::factory()->create(['assigned_to' => null]);
+
+ $softDeletedUser = User::factory()->trashed()->create();
+
+ $this->actingAsForApi(User::factory()->superuser()->create())
+ ->patchJson(
+ route('api.licenses.seats.update', [$licenseSeat->license->id, $licenseSeat->id]),
+ [
+ 'assigned_to' => $softDeletedUser->id,
+ 'notes' => '',
+ ]
+ )
+ ->assertStatus(200)
+ ->assertStatusMessageIs('error')
+ ->assertMessagesContains('assigned_to');
+ }
+
+ public function test_null_assigned_to_checks_in_license_seat()
+ {
+ $this->markTestIncomplete();
+
+ $licenseSeat = LicenseSeat::factory()->reassignable()->assignedToUser()->create();
+
+ $this->actingAsForApi(User::factory()->superuser()->create())
+ ->patchJson(
+ route('api.licenses.seats.update', [$licenseSeat->license->id, $licenseSeat->id]),
+ [
+ 'assigned_to' => null,
+ 'notes' => '',
+ ]
+ )
+ ->assertStatus(200)
->assertStatusMessageIs('error');
+
+ $licenseSeat->refresh();
+
+ $this->assertNull($licenseSeat->assigned_to);
+ }
+
+ public function test_cannot_reassign_unreassignable_license_seat()
+ {
+ $this->markTestIncomplete();
}
}
diff --git a/tests/Support/CustomTestMacros.php b/tests/Support/CustomTestMacros.php
index be9a527f4c..ae24f738cf 100644
--- a/tests/Support/CustomTestMacros.php
+++ b/tests/Support/CustomTestMacros.php
@@ -112,6 +112,8 @@ trait CustomTestMacros
function (array|string $keys) {
Assert::assertArrayHasKey('messages', $this, 'Response did not contain any messages');
+ Assert::assertIsArray($this['messages'], '"messages" not an array so specific message existence cannot be checked.');
+
if (is_string($keys)) {
$keys = [$keys];
}