Skip to content

A free open source IT asset/license management system

fix check in and check out tests

Godfrey Md181a17863d7
diff --git a/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php b/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php
deleted file mode 100644
index 793e505ab4..0000000000
--- a/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php
+++ /dev/null
@@ -1,295 +0,0 @@
-<?php
-
-namespace Tests\Feature\Notifications\Webhooks;
-
-use App\Events\CheckoutableCheckedIn;
-use App\Models\Accessory;
-use App\Models\Asset;
-use App\Models\AssetModel;
-use App\Models\Category;
-use App\Models\Component;
-use App\Models\LicenseSeat;
-use App\Models\Location;
-use App\Models\User;
-use App\Notifications\CheckinAccessoryNotification;
-use App\Notifications\CheckinAssetNotification;
-use App\Notifications\CheckinComponentNotification;
-use App\Notifications\CheckinLicenseSeatNotification;
-use App\Notifications\CheckoutAssetNotification;
-use Illuminate\Database\Eloquent\Model;
-use Illuminate\Notifications\AnonymousNotifiable;
-use Illuminate\Notifications\Messages\SlackMessage;
-use Illuminate\Support\Facades\Notification;
-use NotificationChannels\GoogleChat\GoogleChatMessage;
-use PHPUnit\Framework\Attributes\DataProvider;
-use PHPUnit\Framework\Attributes\Group;
-use Tests\TestCase;
-
-#[Group('notifications')]
-class SlackNotificationsUponCheckinTest extends TestCase
-{
- protected function setUp(): void
- {
- parent::setUp();
-
- Notification::fake();
- }
-
- public static function assetCheckInTargets(): array
- {
- return [
- 'Asset checked out to user' => [fn () => User::factory()->create()],
- 'Asset checked out to asset' => [fn () => Asset::factory()->laptopMbp()->create()],
- 'Asset checked out to location' => [fn () => Location::factory()->create()],
- ];
- }
-
- public static function licenseCheckInTargets(): array
- {
- return [
- 'License checked out to user' => [fn () => User::factory()->create()],
- 'License checked out to asset' => [fn () => Asset::factory()->laptopMbp()->create()],
- ];
- }
-
- public function test_accessory_checkin_sends_slack_notification_when_setting_enabled()
- {
- $this->settings->enableSlackWebhook();
-
- $this->fireCheckInEvent(
- Accessory::factory()->create(),
- User::factory()->create(),
- );
-
- $this->assertSlackNotificationSent(CheckinAccessoryNotification::class);
- }
-
- public function test_accessory_checkin_does_not_send_slack_notification_when_setting_disabled()
- {
- $this->settings->disableSlackWebhook();
-
- $this->fireCheckInEvent(
- Accessory::factory()->create(),
- User::factory()->create(),
- );
-
- $this->assertNoSlackNotificationSent(CheckinAccessoryNotification::class);
- }
-
- #[DataProvider('assetCheckInTargets')]
- public function test_asset_checkin_sends_slack_notification_when_setting_enabled($checkoutTarget)
- {
- $this->settings->enableSlackWebhook();
-
- $this->fireCheckInEvent(
- Asset::factory()->create(),
- $checkoutTarget(),
- );
-
- $this->assertSlackNotificationSent(CheckinAssetNotification::class);
- }
-
- #[DataProvider('assetCheckInTargets')]
- public function test_asset_checkin_does_not_send_slack_notification_when_setting_disabled($checkoutTarget)
- {
- $this->settings->disableSlackWebhook();
-
- $this->fireCheckInEvent(
- Asset::factory()->create(),
- $checkoutTarget(),
- );
-
- $this->assertNoSlackNotificationSent(CheckinAssetNotification::class);
- }
-
- #[DataProvider('assetCheckInTargets')]
- public function test_component_checkin_sends_slack_notification_when_setting_enabled($checkoutTarget)
- {
- $this->settings->enableSlackWebhook();
-
- $this->fireCheckInEvent(
- Component::factory()->create(),
- $checkoutTarget(),
- );
-
- $this->assertSlackNotificationSent(CheckinComponentNotification::class);
- }
-
- #[DataProvider('assetCheckInTargets')]
- public function test_component_checkin_does_not_send_slack_notification_when_setting_disabled($checkoutTarget)
- {
- $this->settings->disableSlackWebhook();
-
- $this->fireCheckInEvent(
- Component::factory()->create(),
- $checkoutTarget(),
- );
-
- $this->assertNoSlackNotificationSent(CheckinComponentNotification::class);
- }
-
- public function test_slack_notification_is_still_sent_when_category_email_is_not_set_to_send_emails()
- {
- $this->settings->enableSlackWebhook();
-
- $category = Category::factory()->create([
- 'checkin_email' => false,
- 'eula_text' => null,
- 'require_acceptance' => false,
- 'use_default_eula' => false,
- ]);
- $assetModel = AssetModel::factory()->for($category)->create();
- $asset = Asset::factory()->for($assetModel, 'model')->assignedToUser()->create();
-
- $this->fireCheckInEvent(
- $asset,
- User::factory()->create(),
- );
-
- $this->assertSlackNotificationSent(CheckinAssetNotification::class);
- }
-
- public function test_asset_checkin_slack_notification_uses_updated_asset_location()
- {
- $this->settings->enableSlackWebhook();
-
- $previousLocation = Location::factory()->create(['name' => 'Region B']);
- $newLocation = Location::factory()->create(['name' => 'Office A']);
- $target = User::factory()->create(['location_id' => $previousLocation->id]);
- $asset = Asset::factory()->create(['location_id' => $previousLocation->id]);
-
- // Simulate post-checkin state while keeping a stale in-memory relation loaded.
- $asset->location_id = $newLocation->id;
- $asset->save();
-
- $this->fireCheckInEvent($asset, $target);
-
- Notification::assertSentTo(
- new AnonymousNotifiable,
- CheckinAssetNotification::class,
- fn (CheckinAssetNotification $notification): bool => $notification->item->location?->is($newLocation)
- );
- }
-
- #[DataProvider('licenseCheckInTargets')]
- public function test_license_checkin_sends_slack_notification_when_setting_enabled($checkoutTarget)
- {
- $this->settings->enableSlackWebhook();
-
- $this->fireCheckInEvent(
- LicenseSeat::factory()->create(),
- $checkoutTarget(),
- );
-
- $this->assertSlackNotificationSent(CheckinLicenseSeatNotification::class);
- }
-
- #[DataProvider('licenseCheckInTargets')]
- public function test_license_checkin_does_not_send_slack_notification_when_setting_disabled($checkoutTarget)
- {
- $this->settings->disableSlackWebhook();
-
- $this->fireCheckInEvent(
- LicenseSeat::factory()->create(),
- $checkoutTarget(),
- );
-
- $this->assertNoSlackNotificationSent(CheckinLicenseSeatNotification::class);
- }
-
- private function fireCheckInEvent(Model $checkoutable, Model $target)
- {
- event(new CheckoutableCheckedIn(
- $checkoutable,
- $target,
- User::factory()->superuser()->create(),
- ''
- ));
- }
-
- #[DataProvider('checkinNotificationCases')]
- public function test_checkin_slack_notifications_can_be_built($createNotification)
- {
- $this->settings->enableSlackWebhook();
-
- $notification = $createNotification();
-
- $this->assertInstanceOf(
- SlackMessage::class,
- $notification->toSlack()
- );
- }
-
- #[DataProvider('checkinNotificationCases')]
- public function test_checkin_google_chat_notifications_can_be_built($createNotification)
- {
- $this->settings->enableGoogleChatWebhook();
-
- $notification = $createNotification();
-
- $this->assertInstanceOf(
- GoogleChatMessage::class,
- $notification->toGoogleChat()
- );
- }
-
- #[DataProvider('checkinNotificationCases')]
- public function test_checkin_microsoft_teams_workflow_notifications_can_be_built($createNotification)
- {
- $this->settings->enableMicrosoftTeamsWebhook();
-
- $notification = $createNotification();
-
- $message = $notification->toMicrosoftTeams();
-
- $this->assertIsArray($message);
- $this->assertCount(2, $message);
- $this->assertIsString($message[0]);
- $this->assertIsArray($message[1]);
- }
-
- public static function checkinNotificationCases(): array
- {
- return [
- 'Asset' => [
- fn() => new CheckinAssetNotification(
- Asset::factory()->create(),
- User::factory()->create(),
- User::factory()->superuser()->create(),
- null,
- '',
- ),
- ],
-
- 'Accessory' => [
- fn() => new CheckinAccessoryNotification(
- Accessory::factory()->create(),
- User::factory()->create(),
- User::factory()->superuser()->create(),
- null,
- '',
- ),
- ],
-
- 'Component' => [
- fn() => new CheckinComponentNotification(
- Component::factory()->create(),
- User::factory()->create(),
- User::factory()->superuser()->create(),
- null,
- '',
- ),
- ],
-
- 'License seat' => [
- fn() => new CheckinLicenseSeatNotification(
- LicenseSeat::factory()->create(),
- User::factory()->create(),
- User::factory()->superuser()->create(),
- null,
- '',
- ),
- ],
- ];
- }
-}
diff --git a/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckoutTest.php b/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckoutTest.php
deleted file mode 100644
index 521be8700a..0000000000
--- a/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckoutTest.php
+++ /dev/null
@@ -1,341 +0,0 @@
-<?php
-
-namespace Tests\Feature\Notifications\Webhooks;
-
-use App\Events\CheckoutableCheckedOut;
-use App\Models\Accessory;
-use App\Models\Asset;
-use App\Models\AssetModel;
-use App\Models\Category;
-use App\Models\Component;
-use App\Models\Consumable;
-use App\Models\LicenseSeat;
-use App\Models\Location;
-use App\Models\User;
-use App\Notifications\CheckoutAccessoryNotification;
-use App\Notifications\CheckoutAssetNotification;
-use App\Notifications\CheckoutComponentNotification;
-use App\Notifications\CheckoutConsumableNotification;
-use App\Notifications\CheckoutLicenseSeatNotification;
-use Illuminate\Database\Eloquent\Model;
-use Illuminate\Notifications\AnonymousNotifiable;
-use Illuminate\Support\Facades\Mail;
-use Illuminate\Support\Facades\Notification;
-use PHPUnit\Framework\Attributes\DataProvider;
-use PHPUnit\Framework\Attributes\Group;
-use Tests\TestCase;
-use Illuminate\Notifications\Messages\SlackMessage;
-use NotificationChannels\GoogleChat\GoogleChatMessage;
-
-
-#[Group('notifications')]
-class SlackNotificationsUponCheckoutTest extends TestCase
-{
- protected function setUp(): void
- {
- parent::setUp();
-
- Notification::fake();
- Mail::fake();
- }
-
- public static function assetCheckoutTargets(): array
- {
- return [
- 'Asset checked out to user' => [fn () => User::factory()->create(['email' => null])],
- 'Asset checked out to asset' => [fn () => Asset::factory()->laptopMbp()->create()],
- 'Asset checked out to location' => [fn () => Location::factory()->create()],
- ];
- }
-
- public static function licenseCheckoutTargets(): array
- {
- return [
- 'License checked out to user' => [fn () => User::factory()->create(['email' => null])],
- 'License checked out to asset' => [fn () => Asset::factory()->laptopMbp()->create()],
- ];
- }
-
- public function test_accessory_checkout_sends_slack_notification_when_setting_enabled()
- {
- $this->settings->enableSlackWebhook();
-
- $this->fireCheckOutEvent(
- Accessory::factory()->create(),
- User::factory()->create(),
- );
-
- $this->assertSlackNotificationSent(CheckoutAccessoryNotification::class);
- }
-
- public function test_accessory_checkout_does_not_send_slack_notification_when_setting_disabled()
- {
- $this->settings->disableSlackWebhook();
-
- $this->fireCheckOutEvent(
- Accessory::factory()->create(),
- User::factory()->create(),
- );
-
- $this->assertNoSlackNotificationSent(CheckoutAccessoryNotification::class);
- }
-
- #[DataProvider('assetCheckoutTargets')]
- public function test_asset_checkout_sends_slack_notification_when_setting_enabled($checkoutTarget)
- {
- $this->settings->enableSlackWebhook();
-
- $this->fireCheckOutEvent(
- Asset::factory()->create(),
- $checkoutTarget(),
- );
-
- $this->assertSlackNotificationSent(CheckoutAssetNotification::class);
- }
-
- #[DataProvider('assetCheckoutTargets')]
- public function test_asset_checkout_does_not_send_slack_notification_when_setting_disabled($checkoutTarget)
- {
- $this->settings->disableSlackWebhook();
-
- $this->fireCheckOutEvent(
- Asset::factory()->create(),
- $checkoutTarget(),
- );
-
- $this->assertNoSlackNotificationSent(CheckoutAssetNotification::class);
- }
-
- #[DataProvider('assetCheckoutTargets')]
- public function test_component_checkout_sends_slack_notification_when_setting_enabled($checkoutTarget)
- {
- $this->settings->enableSlackWebhook();
- $component = Component::factory()->create([
- 'category_id' => Category::factory()->create([
- 'require_acceptance' => false,
- 'eula_text' => null,
- ]),
- ]);
- $this->fireCheckOutEvent(
- $component,
- $checkoutTarget(),
- );
-
- $this->assertSlackNotificationSent(CheckoutComponentNotification::class);
- }
-
- #[DataProvider('assetCheckoutTargets')]
- public function test_component_checkout_does_not_send_slack_notification_when_setting_disabled($checkoutTarget)
- {
- $this->settings->disableSlackWebhook();
- $component = Component::factory()->create([
- 'category_id' => Category::factory()->create([
- 'require_acceptance' => false,
- 'eula_text' => null,
- ]),
- ]);
- $this->fireCheckOutEvent(
- $component,
- $checkoutTarget(),
- );
-
- $this->assertNoSlackNotificationSent(CheckoutComponentNotification::class);
- }
-
- public function test_slack_notification_is_still_sent_when_category_email_is_not_set_to_send_emails()
- {
- $this->settings->enableSlackWebhook();
-
- $category = Category::factory()->create([
- 'checkin_email' => false,
- 'eula_text' => null,
- 'require_acceptance' => false,
- 'use_default_eula' => false,
- ]);
- $assetModel = AssetModel::factory()->for($category)->create();
- $asset = Asset::factory()->for($assetModel, 'model')->create();
-
- $this->fireCheckOutEvent(
- $asset,
- User::factory()->create(),
- );
-
- $this->assertSlackNotificationSent(CheckoutAssetNotification::class);
- }
-
- public function test_asset_checkout_slack_notification_uses_updated_asset_location()
- {
- $this->settings->enableSlackWebhook();
-
- $previousLocation = Location::factory()->create(['name' => 'Office A']);
- $newLocation = Location::factory()->create(['name' => 'Region B']);
- $target = User::factory()->create(['location_id' => $newLocation->id, 'email' => null]);
- $asset = Asset::factory()->create(['location_id' => $previousLocation->id]);
-
- // Simulate post-checkout state while keeping a stale in-memory relation loaded.
- $asset->location_id = $newLocation->id;
- $asset->save();
-
- $this->fireCheckOutEvent($asset, $target);
-
- Notification::assertSentTo(
- new AnonymousNotifiable,
- CheckoutAssetNotification::class,
- fn (CheckoutAssetNotification $notification): bool => $notification->item->location?->is($newLocation)
- );
- }
-
- public function test_consumable_checkout_sends_slack_notification_when_setting_enabled()
- {
- $this->settings->enableSlackWebhook();
-
- $this->fireCheckOutEvent(
- Consumable::factory()->create(),
- User::factory()->create(),
- );
-
- $this->assertSlackNotificationSent(CheckoutConsumableNotification::class);
- }
-
- public function test_consumable_checkout_does_not_send_slack_notification_when_setting_disabled()
- {
- $this->settings->disableSlackWebhook();
-
- $this->fireCheckOutEvent(
- Consumable::factory()->create(),
- User::factory()->create(),
- );
-
- $this->assertNoSlackNotificationSent(CheckoutConsumableNotification::class);
- }
-
- #[DataProvider('licenseCheckoutTargets')]
- public function test_license_checkout_sends_slack_notification_when_setting_enabled($checkoutTarget)
- {
- $this->settings->enableSlackWebhook();
-
- $this->fireCheckOutEvent(
- LicenseSeat::factory()->create(),
- $checkoutTarget(),
- );
-
- $this->assertSlackNotificationSent(CheckoutLicenseSeatNotification::class);
- }
-
- #[DataProvider('licenseCheckoutTargets')]
- public function test_license_checkout_does_not_send_slack_notification_when_setting_disabled($checkoutTarget)
- {
- $this->settings->disableSlackWebhook();
-
- $this->fireCheckOutEvent(
- LicenseSeat::factory()->create(),
- $checkoutTarget(),
- );
-
- $this->assertNoSlackNotificationSent(CheckoutLicenseSeatNotification::class);
- }
-
- private function fireCheckOutEvent(Model $checkoutable, Model $target)
- {
- event(new CheckoutableCheckedOut(
- $checkoutable,
- $target,
- User::factory()->superuser()->create(),
- '',
- ));
- }
-
- #[DataProvider('checkoutNotificationCases')]
- public function test_checkout_slack_notifications_can_be_built($createNotification)
- {
- $this->settings->enableSlackWebhook();
-
- $notification = $createNotification();
-
- $this->assertInstanceOf(
- SlackMessage::class,
- $notification->toSlack()
- );
- }
-
- #[DataProvider('checkoutNotificationCases')]
- public function test_checkout_google_chat_notifications_can_be_built($createNotification)
- {
- $this->settings->enableGoogleChatWebhook();
- $notification = $createNotification();
-
- $this->assertInstanceOf(
- GoogleChatMessage::class,
- $notification->toGoogleChat()
- );
- }
-
- #[DataProvider('checkoutNotificationCases')]
- public function test_checkout_microsoft_teams_workflow_notifications_can_be_built($createNotification)
- {
- $this->settings->enableMicrosoftTeamsWebhook();
- $notification = $createNotification();
-
- $message = $notification->toMicrosoftTeams();
-
- $this->assertIsArray($message);
- $this->assertCount(2, $message);
- $this->assertIsString($message[0]);
- $this->assertIsArray($message[1]);
- }
-
- public static function checkoutNotificationCases(): array
- {
- return [
- 'Accessory' => [
- fn() => new CheckoutAccessoryNotification(
- Accessory::factory()->create(),
- User::factory()->create(),
- User::factory()->superuser()->create(),
- null,
- 'Test note',
- ),
- ],
-
- 'Asset' => [
- fn() => new CheckoutAssetNotification(
- Asset::factory()->create(),
- User::factory()->create(),
- User::factory()->superuser()->create(),
- null,
- 'Test note',
- ),
- ],
-
- 'Component' => [
- fn() => new CheckoutComponentNotification(
- Component::factory()->create(),
- User::factory()->create(),
- User::factory()->superuser()->create(),
- null,
- 'Test note',
- ),
- ],
-
- 'Consumable' => [
- fn() => new CheckoutConsumableNotification(
- Consumable::factory()->create(),
- User::factory()->create(),
- User::factory()->superuser()->create(),
- null,
- 'Test note',
- ),
- ],
-
- 'License seat' => [
- fn() => new CheckoutLicenseSeatNotification(
- LicenseSeat::factory()->create(),
- User::factory()->create(),
- User::factory()->superuser()->create(),
- null,
- 'Test note',
- ),
- ],
- ];
- }
-}