Skip to content

A free open source IT asset/license management system

Add failing tests for pending acceptances surviving checkin

Marcus Moore4aeca25a1c7b
diff --git a/database/factories/CheckoutAcceptanceFactory.php b/database/factories/CheckoutAcceptanceFactory.php
index 7ef978ef87..597faf4b46 100644
--- a/database/factories/CheckoutAcceptanceFactory.php
+++ b/database/factories/CheckoutAcceptanceFactory.php
@@ -5,6 +5,7 @@ namespace Database\Factories;
use App\Models\Accessory;
use App\Models\Asset;
use App\Models\CheckoutAcceptance;
+use App\Models\LicenseSeat;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
@@ -64,6 +65,14 @@ class CheckoutAcceptanceFactory extends Factory
]);
}
+ public function forLicenseSeat()
+ {
+ return $this->state([
+ 'checkoutable_type' => LicenseSeat::class,
+ 'checkoutable_id' => LicenseSeat::factory(),
+ ]);
+ }
+
public function pending()
{
return $this->state([
diff --git a/tests/Feature/Checkins/Ui/AccessoryCheckinTest.php b/tests/Feature/Checkins/Ui/AccessoryCheckinTest.php
index 20953b28bb..03ef269d43 100644
--- a/tests/Feature/Checkins/Ui/AccessoryCheckinTest.php
+++ b/tests/Feature/Checkins/Ui/AccessoryCheckinTest.php
@@ -5,9 +5,14 @@ namespace Tests\Feature\Checkins\Ui;
use App\Events\CheckoutableCheckedIn;
use App\Mail\CheckinAccessoryMail;
use App\Models\Accessory;
+use App\Models\Asset;
+use App\Models\CheckoutAcceptance;
+use App\Models\Setting;
use App\Models\User;
+use App\Notifications\CheckinAccessoryNotification;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Mail;
+use Illuminate\Support\Facades\Notification;
use Tests\TestCase;
class AccessoryCheckinTest extends TestCase
@@ -92,4 +97,162 @@ class AccessoryCheckinTest extends TestCase
return $mail->hasTo($user->email);
});
}
+
+ public function test_accessory_checkin_clears_pending_acceptance_when_notifications_are_fully_disabled()
+ {
+ $this->settings->disableAdminCC()->disableAdminCCAlways()->disableSlackWebhook();
+
+ $user = User::factory()->create();
+ $accessory = Accessory::factory()->checkedOutToUser($user)->create();
+
+ $accessory->category->update([
+ 'require_acceptance' => true,
+ 'checkin_email' => false,
+ ]);
+
+ $acceptance = CheckoutAcceptance::factory()->forAccessory()->pending()->create([
+ 'checkoutable_id' => $accessory->id,
+ 'assigned_to_id' => $user->id,
+ ]);
+
+ $this->assertTrue($acceptance->isPending());
+ $this->assertNoNotificationChannelIsConfigured($accessory);
+
+ $this->checkInAccessoryFrom($accessory, $user);
+
+ $this->assertAcceptanceWasSoftDeleted(
+ $acceptance,
+ 'The pending acceptance should have been soft-deleted when the accessory was checked in but it survived.'
+ );
+ }
+
+ public function test_accessory_checkin_clears_pending_acceptance_when_only_a_webhook_is_configured()
+ {
+ Notification::fake();
+
+ $this->settings->disableAdminCC()->disableAdminCCAlways()->enableSlackWebhook();
+
+ $user = User::factory()->create();
+ $accessory = Accessory::factory()->checkedOutToUser($user)->create();
+
+ $accessory->category->update([
+ 'require_acceptance' => true,
+ 'checkin_email' => false,
+ ]);
+
+ $acceptance = CheckoutAcceptance::factory()->forAccessory()->pending()->create([
+ 'checkoutable_id' => $accessory->id,
+ 'assigned_to_id' => $user->id,
+ ]);
+
+ $this->assertTrue($acceptance->isPending());
+
+ $this->checkInAccessoryFrom($accessory, $user);
+
+ $this->assertSlackNotificationSent(CheckinAccessoryNotification::class);
+
+ $this->assertAcceptanceWasSoftDeleted(
+ $acceptance,
+ 'The pending acceptance should have been soft-deleted when the accessory was checked in but it survived.'
+ );
+ }
+
+ public function test_accessory_checkin_does_not_clear_a_same_id_pending_acceptance_of_another_type()
+ {
+ Mail::fake();
+
+ $this->settings->disableAdminCC()->disableAdminCCAlways()->disableSlackWebhook();
+
+ $user = User::factory()->create();
+
+ [$accessory, $asset] = $this->createAccessoryAndAssetSharingAnId($user);
+
+ // Notifications on for this one. With checkin_email off the delete never
+ // runs at all and this would be a re-run of the two tests above rather
+ // than a test of the missing morph filter.
+ $accessory->category->update(['checkin_email' => true]);
+ $this->assertTrue((bool) $accessory->fresh()->checkin_email());
+
+ $accessoryAcceptance = CheckoutAcceptance::factory()->forAccessory()->pending()->create([
+ 'checkoutable_id' => $accessory->id,
+ 'assigned_to_id' => $user->id,
+ ]);
+
+ $assetAcceptance = CheckoutAcceptance::factory()->pending()->create([
+ 'checkoutable_id' => $asset->id,
+ 'assigned_to_id' => $user->id,
+ ]);
+
+ $this->checkInAccessoryFrom($accessory, $user);
+
+ Mail::assertSent(CheckinAccessoryMail::class);
+
+ $this->assertAcceptanceWasSoftDeleted(
+ $accessoryAcceptance,
+ 'The checked-in accessory\'s own pending acceptance should have been soft-deleted.'
+ );
+
+ $this->assertNull(
+ CheckoutAcceptance::withTrashed()->findOrFail($assetAcceptance->id)->deleted_at,
+ 'Checking in the accessory should have left the asset acceptance alone. It was soft-deleted too, because the listener matches on checkoutable_id and assigned_to_id without filtering on checkoutable_type, and the asset happens to share the accessory\'s id.'
+ );
+ }
+
+ private function checkInAccessoryFrom(Accessory $accessory, User $user): void
+ {
+ $checkout = $accessory->checkouts()
+ ->where('assigned_type', User::class)
+ ->where('assigned_to', $user->id)
+ ->firstOrFail();
+
+ $this->actingAs(User::factory()->checkinAccessories()->create())
+ ->post(route('accessories.checkin.store', $checkout->id))
+ ->assertRedirect();
+
+ $this->assertDatabaseMissing('accessories_checkout', ['id' => $checkout->id]);
+ }
+
+ /**
+ * The missing morph filter only shows up when an accessory and an asset
+ * share a primary key, and factories won't hand out a matching pair by
+ * accident. Nudge whichever sequence is behind until the ids line up.
+ *
+ * @return array{0: Accessory, 1: Asset}
+ */
+ private function createAccessoryAndAssetSharingAnId(User $user): array
+ {
+ $accessory = Accessory::factory()->checkedOutToUser($user)->create();
+ $asset = Asset::factory()->create();
+
+ for ($attempt = 0; $accessory->id !== $asset->id && $attempt < 25; $attempt++) {
+ if ($accessory->id < $asset->id) {
+ $accessory = Accessory::factory()->checkedOutToUser($user)->create();
+ } else {
+ $asset = Asset::factory()->create();
+ }
+ }
+
+ $this->assertSame(
+ $accessory->id,
+ $asset->id,
+ 'Fixture setup failed: could not get an accessory and an asset onto the same id.'
+ );
+
+ return [$accessory, $asset];
+ }
+
+ private function assertNoNotificationChannelIsConfigured(Accessory $accessory): void
+ {
+ $this->assertFalse((bool) $accessory->fresh()->checkin_email());
+ $this->assertEmpty(Setting::getSettings()->admin_cc_email);
+ $this->assertEmpty(Setting::getSettings()->webhook_endpoint);
+ }
+
+ private function assertAcceptanceWasSoftDeleted(CheckoutAcceptance $acceptance, string $message): void
+ {
+ $this->assertNotNull(
+ CheckoutAcceptance::withTrashed()->findOrFail($acceptance->id)->deleted_at,
+ $message
+ );
+ }
}
diff --git a/tests/Feature/Checkins/Ui/LicenseCheckinTest.php b/tests/Feature/Checkins/Ui/LicenseCheckinTest.php
index 2aeef50b08..31458a2f60 100644
--- a/tests/Feature/Checkins/Ui/LicenseCheckinTest.php
+++ b/tests/Feature/Checkins/Ui/LicenseCheckinTest.php
@@ -3,10 +3,18 @@
namespace Tests\Feature\Checkins\Ui;
use App\Events\CheckoutableCheckedIn;
+use App\Mail\CheckinLicenseMail;
use App\Models\Asset;
+use App\Models\Category;
+use App\Models\CheckoutAcceptance;
+use App\Models\License;
use App\Models\LicenseSeat;
+use App\Models\Setting;
use App\Models\User;
+use App\Notifications\CheckinLicenseSeatNotification;
use Illuminate\Support\Facades\Event;
+use Illuminate\Support\Facades\Mail;
+use Illuminate\Support\Facades\Notification;
use Tests\TestCase;
class LicenseCheckinTest extends TestCase
@@ -126,4 +134,179 @@ class LicenseCheckinTest extends TestCase
->assertOk();
}
+
+ public function test_license_seat_checkin_clears_pending_acceptance_when_notifications_are_fully_disabled()
+ {
+ $this->settings->disableAdminCC()->disableAdminCCAlways()->disableSlackWebhook();
+
+ $user = User::factory()->create();
+ $seat = $this->seatAssignedTo($user);
+
+ $acceptance = $this->pendingSeatAcceptance($seat, $user);
+
+ $this->assertNoNotificationChannelIsConfigured($seat);
+
+ $this->checkInSeat($seat);
+
+ $this->assertAcceptanceWasSoftDeleted(
+ $acceptance,
+ 'The pending acceptance should have been soft-deleted when the seat was checked in, but it survived: with no email and no webhook configured the listener returns before it reaches the delete.'
+ );
+ }
+
+ public function test_license_seat_checkin_clears_pending_acceptance_when_only_a_webhook_is_configured()
+ {
+ Notification::fake();
+
+ $this->settings->disableAdminCC()->disableAdminCCAlways()->enableSlackWebhook();
+
+ $user = User::factory()->create();
+ $seat = $this->seatAssignedTo($user);
+
+ $acceptance = $this->pendingSeatAcceptance($seat, $user);
+
+ $this->checkInSeat($seat);
+
+ $this->assertSlackNotificationSent(CheckinLicenseSeatNotification::class);
+
+ $this->assertAcceptanceWasSoftDeleted(
+ $acceptance,
+ 'The pending acceptance should have been soft-deleted when the seat was checked in, but it survived: a webhook-only install never enters the notification branch that holds the delete.'
+ );
+ }
+
+ public function test_license_seat_checkin_does_not_clear_a_same_id_pending_acceptance_of_another_type()
+ {
+ Mail::fake();
+
+ $this->settings->disableAdminCC()->disableAdminCCAlways()->disableSlackWebhook();
+
+ $user = User::factory()->create();
+
+ [$seat, $asset] = $this->createSeatAndAssetSharingAnId($user);
+
+ // Notifications on for this one. With checkin_email off the delete never
+ // runs at all and this would be a re-run of the two tests above rather
+ // than a test of the missing morph filter.
+ $seat->license->category->update(['checkin_email' => true]);
+ $this->assertTrue((bool) $seat->license->fresh()->checkin_email());
+
+ $seatAcceptance = $this->pendingSeatAcceptance($seat, $user);
+
+ $assetAcceptance = CheckoutAcceptance::factory()->pending()->create([
+ 'checkoutable_id' => $asset->id,
+ 'assigned_to_id' => $user->id,
+ ]);
+ $this->assertSame(Asset::class, $assetAcceptance->checkoutable_type);
+
+ $this->checkInSeat($seat);
+
+ Mail::assertSent(CheckinLicenseMail::class);
+
+ $this->assertAcceptanceWasSoftDeleted(
+ $seatAcceptance,
+ 'The checked-in seat\'s own pending acceptance should have been soft-deleted.'
+ );
+
+ $this->assertNull(
+ CheckoutAcceptance::withTrashed()->findOrFail($assetAcceptance->id)->deleted_at,
+ 'Checking in the license seat should have left the asset acceptance alone. It was soft-deleted too, because the listener matches on checkoutable_id and assigned_to_id without filtering on checkoutable_type, and the asset happens to share the seat\'s id.'
+ );
+ }
+
+ /**
+ * Acceptance required, checkin email off — the configuration where a stale
+ * pending acceptance can outlive the assignment it belongs to.
+ */
+ private function seatAssignedTo(User $user): LicenseSeat
+ {
+ $license = License::factory()->create([
+ 'reassignable' => 1,
+ 'category_id' => Category::factory()->create([
+ 'category_type' => 'license',
+ 'require_acceptance' => 1,
+ 'checkin_email' => 0,
+ ])->id,
+ ]);
+
+ return LicenseSeat::factory()
+ ->assignedToUser($user)
+ ->create(['license_id' => $license->id]);
+ }
+
+ /**
+ * A real seat acceptance is keyed to the SEAT — LicenseSeat::class plus the
+ * seat's own id — which is what CreateCheckoutAcceptanceAction writes. There
+ * is no morph map aliasing License and LicenseSeat.
+ */
+ private function pendingSeatAcceptance(LicenseSeat $seat, User $user): CheckoutAcceptance
+ {
+ $acceptance = CheckoutAcceptance::factory()
+ ->forLicenseSeat()
+ ->pending()
+ ->create([
+ 'checkoutable_id' => $seat->id,
+ 'assigned_to_id' => $user->id,
+ ]);
+
+ $this->assertSame(LicenseSeat::class, $acceptance->checkoutable_type);
+ $this->assertTrue($acceptance->isPending());
+
+ return $acceptance;
+ }
+
+ private function checkInSeat(LicenseSeat $seat): void
+ {
+ $this->actingAs(User::factory()->checkinLicenses()->create())
+ ->post(route('licenses.checkin.save', $seat->id))
+ ->assertRedirect();
+
+ // Confirm the checkin actually happened before asserting anything about
+ // what it did or didn't clean up.
+ $this->assertNull($seat->refresh()->assigned_to, 'The seat was not checked in, so nothing downstream ran.');
+ }
+
+ /**
+ * The missing morph filter only shows up when a seat and an asset share a
+ * primary key, and factories won't hand out a matching pair by accident.
+ * Nudge whichever sequence is behind until the ids line up.
+ *
+ * @return array{0: LicenseSeat, 1: Asset}
+ */
+ private function createSeatAndAssetSharingAnId(User $user): array
+ {
+ $seat = $this->seatAssignedTo($user);
+ $asset = Asset::factory()->create();
+
+ for ($attempt = 0; $seat->id !== $asset->id && $attempt < 25; $attempt++) {
+ if ($seat->id < $asset->id) {
+ $seat = $this->seatAssignedTo($user);
+ } else {
+ $asset = Asset::factory()->create();
+ }
+ }
+
+ $this->assertSame(
+ $seat->id,
+ $asset->id,
+ 'Fixture setup failed: could not get a license seat and an asset onto the same id.'
+ );
+
+ return [$seat, $asset];
+ }
+
+ private function assertNoNotificationChannelIsConfigured(LicenseSeat $seat): void
+ {
+ $this->assertFalse((bool) $seat->license->checkin_email());
+ $this->assertEmpty(Setting::getSettings()->admin_cc_email);
+ $this->assertEmpty(Setting::getSettings()->webhook_endpoint);
+ }
+
+ private function assertAcceptanceWasSoftDeleted(CheckoutAcceptance $acceptance, string $message): void
+ {
+ $this->assertNotNull(
+ CheckoutAcceptance::withTrashed()->findOrFail($acceptance->id)->deleted_at,
+ $message
+ );
+ }
}