diff --git a/tests/Feature/Notifications/AccessoryWebhookTest.php b/tests/Feature/Notifications/AccessoryWebhookTest.php deleted file mode 100644 index 2e3ef999ba..0000000000 --- a/tests/Feature/Notifications/AccessoryWebhookTest.php +++ /dev/null @@ -1,96 +0,0 @@ -settings->enableSlackWebhook(); - - event(new CheckoutableCheckedOut( - Accessory::factory()->appleBtKeyboard()->create(), - User::factory()->create(), - User::factory()->superuser()->create(), - '' - )); - - Notification::assertSentTo( - new AnonymousNotifiable, - CheckoutAccessoryNotification::class, - function ($notification, $channels, $notifiable) { - return $notifiable->routes['slack'] === Setting::getSettings()->webhook_endpoint; - } - ); - } - - public function testAccessoryCheckoutDoesNotSendWebhookNotificationWhenSettingDisabled() - { - Notification::fake(); - - $this->settings->disableWebhook(); - - event(new CheckoutableCheckedOut( - Accessory::factory()->appleBtKeyboard()->create(), - User::factory()->create(), - User::factory()->superuser()->create(), - '' - )); - - Notification::assertNotSentTo(new AnonymousNotifiable, CheckoutAccessoryNotification::class); - } - - public function testAccessoryCheckinSendsWebhookNotificationWhenSettingEnabled() - { - Notification::fake(); - - $this->settings->enableSlackWebhook(); - - event(new CheckoutableCheckedIn( - Accessory::factory()->appleBtKeyboard()->create(), - User::factory()->create(), - User::factory()->superuser()->create(), - '' - )); - - Notification::assertSentTo( - new AnonymousNotifiable, - CheckinAccessoryNotification::class, - function ($notification, $channels, $notifiable) { - return $notifiable->routes['slack'] === Setting::getSettings()->webhook_endpoint; - } - ); - } - - public function testAccessoryCheckinDoesNotSendWebhookNotificationWhenSettingDisabled() - { - Notification::fake(); - - $this->settings->disableWebhook(); - - event(new CheckoutableCheckedIn( - Accessory::factory()->appleBtKeyboard()->create(), - User::factory()->create(), - User::factory()->superuser()->create(), - '' - )); - - Notification::assertNotSentTo(new AnonymousNotifiable, CheckinAccessoryNotification::class); - } -} diff --git a/tests/Feature/Notifications/AssetWebhookTest.php b/tests/Feature/Notifications/AssetWebhookTest.php deleted file mode 100644 index 95218f98e7..0000000000 --- a/tests/Feature/Notifications/AssetWebhookTest.php +++ /dev/null @@ -1,163 +0,0 @@ - [fn() => User::factory()->create()], - 'Asset checked out to asset' => [fn() => $this->createAsset()], - 'Asset checked out to location' => [fn() => Location::factory()->create()], - ]; - } - - /** @dataProvider targets */ - public function testAssetCheckoutSendsWebhookNotificationWhenSettingEnabled($checkoutTarget) - { - Notification::fake(); - - $this->settings->enableSlackWebhook(); - - event(new CheckoutableCheckedOut( - $this->createAsset(), - $checkoutTarget(), - User::factory()->superuser()->create(), - '' - )); - - Notification::assertSentTo( - new AnonymousNotifiable, - CheckoutAssetNotification::class, - function ($notification, $channels, $notifiable) { - return $notifiable->routes['slack'] === Setting::getSettings()->webhook_endpoint; - } - ); - } - - /** @dataProvider targets */ - public function testAssetCheckoutDoesNotSendWebhookNotificationWhenSettingDisabled($checkoutTarget) - { - Notification::fake(); - - $this->settings->disableWebhook(); - - event(new CheckoutableCheckedOut( - $this->createAsset(), - $checkoutTarget(), - User::factory()->superuser()->create(), - '' - )); - - Notification::assertNotSentTo(new AnonymousNotifiable, CheckoutAssetNotification::class); - } - - /** @dataProvider targets */ - public function testAssetCheckinSendsWebhookNotificationWhenSettingEnabled($checkoutTarget) - { - Notification::fake(); - - $this->settings->enableSlackWebhook(); - - event(new CheckoutableCheckedIn( - $this->createAsset(), - $checkoutTarget(), - User::factory()->superuser()->create(), - '' - )); - - Notification::assertSentTo( - new AnonymousNotifiable, - CheckinAssetNotification::class, - function ($notification, $channels, $notifiable) { - return $notifiable->routes['slack'] === Setting::getSettings()->webhook_endpoint; - } - ); - } - - /** @dataProvider targets */ - public function testAssetCheckinDoesNotSendWebhookNotificationWhenSettingDisabled($checkoutTarget) - { - Notification::fake(); - - $this->settings->disableWebhook(); - - event(new CheckoutableCheckedIn( - $this->createAsset(), - $checkoutTarget(), - User::factory()->superuser()->create(), - '' - )); - - Notification::assertNotSentTo(new AnonymousNotifiable, CheckinAssetNotification::class); - } - - public function testCheckInEmailSentToUserIfSettingEnabled() - { - Notification::fake(); - - $user = User::factory()->create(); - $asset = Asset::factory()->assignedToUser($user)->create(); - - $asset->model->category->update(['checkin_email' => true]); - - event(new CheckoutableCheckedIn( - $asset, - $user, - User::factory()->checkinAssets()->create(), - '' - )); - - Notification::assertSentTo( - [$user], - function (CheckinAssetNotification $notification, $channels) { - return in_array('mail', $channels); - }, - ); - } - - public function testCheckInEmailNotSentToUserIfSettingDisabled() - { - Notification::fake(); - - $user = User::factory()->create(); - $asset = Asset::factory()->assignedToUser($user)->create(); - - $asset->model->category->update(['checkin_email' => false]); - - event(new CheckoutableCheckedIn( - $asset, - $user, - User::factory()->checkinAssets()->create(), - '' - )); - - Notification::assertNotSentTo( - [$user], - function (CheckinAssetNotification $notification, $channels) { - return in_array('mail', $channels); - } - ); - } - - private function createAsset() - { - return Asset::factory()->laptopMbp()->create(); - } -} diff --git a/tests/Feature/Notifications/ComponentWebhookTest.php b/tests/Feature/Notifications/ComponentWebhookTest.php deleted file mode 100644 index 2e2a535219..0000000000 --- a/tests/Feature/Notifications/ComponentWebhookTest.php +++ /dev/null @@ -1,50 +0,0 @@ -settings->enableSlackWebhook(); - - event(new CheckoutableCheckedOut( - Component::factory()->ramCrucial8()->create(), - Asset::factory()->laptopMbp()->create(), - User::factory()->superuser()->create(), - '' - )); - - Notification::assertNothingSent(); - } - - public function testComponentCheckinDoesNotSendWebhookNotification() - { - Notification::fake(); - - $this->settings->enableSlackWebhook(); - - event(new CheckoutableCheckedIn( - Component::factory()->ramCrucial8()->create(), - Asset::factory()->laptopMbp()->create(), - User::factory()->superuser()->create(), - '' - )); - - Notification::assertNothingSent(); - } -} diff --git a/tests/Feature/Notifications/ConsumableWebhookTest.php b/tests/Feature/Notifications/ConsumableWebhookTest.php deleted file mode 100644 index 2815731bd2..0000000000 --- a/tests/Feature/Notifications/ConsumableWebhookTest.php +++ /dev/null @@ -1,56 +0,0 @@ -settings->enableSlackWebhook(); - - event(new CheckoutableCheckedOut( - Consumable::factory()->cardstock()->create(), - User::factory()->create(), - User::factory()->superuser()->create(), - '' - )); - - Notification::assertSentTo( - new AnonymousNotifiable, - CheckoutConsumableNotification::class, - function ($notification, $channels, $notifiable) { - return $notifiable->routes['slack'] === Setting::getSettings()->webhook_endpoint; - } - ); - } - - public function testConsumableCheckoutDoesNotSendWebhookNotificationWhenSettingDisabled() - { - Notification::fake(); - - $this->settings->disableWebhook(); - - event(new CheckoutableCheckedOut( - Consumable::factory()->cardstock()->create(), - User::factory()->create(), - User::factory()->superuser()->create(), - '' - )); - - Notification::assertNotSentTo(new AnonymousNotifiable, CheckoutConsumableNotification::class); - } -} diff --git a/tests/Feature/Notifications/Email/EmailNotificationsUponCheckinTest.php b/tests/Feature/Notifications/Email/EmailNotificationsUponCheckinTest.php new file mode 100644 index 0000000000..dbe79c5727 --- /dev/null +++ b/tests/Feature/Notifications/Email/EmailNotificationsUponCheckinTest.php @@ -0,0 +1,70 @@ +create(); + $asset = Asset::factory()->assignedToUser($user)->create(); + + $asset->model->category->update(['checkin_email' => true]); + + $this->fireCheckInEvent($asset, $user); + + Notification::assertSentTo( + $user, + function (CheckinAssetNotification $notification, $channels) { + return in_array('mail', $channels); + }, + ); + } + + public function testCheckInEmailNotSentToUserIfSettingDisabled() + { + $user = User::factory()->create(); + $asset = Asset::factory()->assignedToUser($user)->create(); + + $asset->model->category->update(['checkin_email' => false]); + + $this->fireCheckInEvent($asset, $user); + + Notification::assertNotSentTo( + $user, + function (CheckinAssetNotification $notification, $channels) { + return in_array('mail', $channels); + } + ); + } + + private function fireCheckInEvent($asset, $user): void + { + event(new CheckoutableCheckedIn( + $asset, + $user, + User::factory()->checkinAssets()->create(), + '' + )); + } +} diff --git a/tests/Feature/Notifications/LicenseWebhookTest.php b/tests/Feature/Notifications/LicenseWebhookTest.php deleted file mode 100644 index 24ec53a75e..0000000000 --- a/tests/Feature/Notifications/LicenseWebhookTest.php +++ /dev/null @@ -1,109 +0,0 @@ - [fn() => User::factory()->create()], - 'License checked out to asset' => [fn() => Asset::factory()->laptopMbp()->create()], - ]; - } - - /** @dataProvider targets */ - public function testLicenseCheckoutSendsWebhookNotificationWhenSettingEnabled($checkoutTarget) - { - Notification::fake(); - - $this->settings->enableSlackWebhook(); - - event(new CheckoutableCheckedOut( - LicenseSeat::factory()->create(), - $checkoutTarget(), - User::factory()->superuser()->create(), - '' - )); - - Notification::assertSentTo( - new AnonymousNotifiable, - CheckoutLicenseSeatNotification::class, - function ($notification, $channels, $notifiable) { - return $notifiable->routes['slack'] === Setting::getSettings()->webhook_endpoint; - } - ); - } - - /** @dataProvider targets */ - public function testLicenseCheckoutDoesNotSendWebhookNotificationWhenSettingDisabled($checkoutTarget) - { - Notification::fake(); - - $this->settings->disableWebhook(); - - event(new CheckoutableCheckedOut( - LicenseSeat::factory()->create(), - $checkoutTarget(), - User::factory()->superuser()->create(), - '' - )); - - Notification::assertNotSentTo(new AnonymousNotifiable, CheckoutLicenseSeatNotification::class); - } - - /** @dataProvider targets */ - public function testLicenseCheckinSendsWebhookNotificationWhenSettingEnabled($checkoutTarget) - { - Notification::fake(); - - $this->settings->enableSlackWebhook(); - - event(new CheckoutableCheckedIn( - LicenseSeat::factory()->create(), - $checkoutTarget(), - User::factory()->superuser()->create(), - '' - )); - - Notification::assertSentTo( - new AnonymousNotifiable, - CheckinLicenseSeatNotification::class, - function ($notification, $channels, $notifiable) { - return $notifiable->routes['slack'] === Setting::getSettings()->webhook_endpoint; - } - ); - } - - /** @dataProvider targets */ - public function testLicenseCheckinDoesNotSendWebhookNotificationWhenSettingDisabled($checkoutTarget) - { - Notification::fake(); - - $this->settings->disableWebhook(); - - event(new CheckoutableCheckedIn( - LicenseSeat::factory()->create(), - $checkoutTarget(), - User::factory()->superuser()->create(), - '' - )); - - Notification::assertNotSentTo(new AnonymousNotifiable, CheckinLicenseSeatNotification::class); - } -} diff --git a/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php b/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php new file mode 100644 index 0000000000..b6bb7801a7 --- /dev/null +++ b/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php @@ -0,0 +1,148 @@ + [fn() => User::factory()->create()], + 'Asset checked out to asset' => [fn() => Asset::factory()->laptopMbp()->create()], + 'Asset checked out to location' => [fn() => Location::factory()->create()], + ]; + } + + public 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 testAccessoryCheckinSendsSlackNotificationWhenSettingEnabled() + { + $this->settings->enableSlackWebhook(); + + $this->fireCheckInEvent( + Accessory::factory()->create(), + User::factory()->create(), + ); + + $this->assertSlackNotificationSent(CheckinAccessoryNotification::class); + } + + public function testAccessoryCheckinDoesNotSendSlackNotificationWhenSettingDisabled() + { + $this->settings->disableSlackWebhook(); + + $this->fireCheckInEvent( + Accessory::factory()->create(), + User::factory()->create(), + ); + + $this->assertNoSlackNotificationSent(CheckinAccessoryNotification::class); + } + + /** @dataProvider assetCheckInTargets */ + public function testAssetCheckinSendsSlackNotificationWhenSettingEnabled($checkoutTarget) + { + $this->settings->enableSlackWebhook(); + + $this->fireCheckInEvent( + Asset::factory()->create(), + $checkoutTarget(), + ); + + $this->assertSlackNotificationSent(CheckinAssetNotification::class); + } + + /** @dataProvider assetCheckInTargets */ + public function testAssetCheckinDoesNotSendSlackNotificationWhenSettingDisabled($checkoutTarget) + { + $this->settings->disableSlackWebhook(); + + $this->fireCheckInEvent( + Asset::factory()->create(), + $checkoutTarget(), + ); + + $this->assertNoSlackNotificationSent(CheckinAssetNotification::class); + } + + public function testComponentCheckinDoesNotSendSlackNotification() + { + $this->settings->enableSlackWebhook(); + + $this->fireCheckInEvent( + Component::factory()->create(), + Asset::factory()->laptopMbp()->create(), + ); + + Notification::assertNothingSent(); + } + + /** @dataProvider licenseCheckInTargets */ + public function testLicenseCheckinSendsSlackNotificationWhenSettingEnabled($checkoutTarget) + { + $this->settings->enableSlackWebhook(); + + $this->fireCheckInEvent( + LicenseSeat::factory()->create(), + $checkoutTarget(), + ); + + $this->assertSlackNotificationSent(CheckinLicenseSeatNotification::class); + } + + /** @dataProvider licenseCheckInTargets */ + public function testLicenseCheckinDoesNotSendSlackNotificationWhenSettingDisabled($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(), + '' + )); + } +} diff --git a/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckoutTest.php b/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckoutTest.php new file mode 100644 index 0000000000..550f7c5b18 --- /dev/null +++ b/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckoutTest.php @@ -0,0 +1,174 @@ + [fn() => User::factory()->create()], + 'Asset checked out to asset' => [fn() => Asset::factory()->laptopMbp()->create()], + 'Asset checked out to location' => [fn() => Location::factory()->create()], + ]; + } + + public function licenseCheckoutTargets(): array + { + return [ + 'License checked out to user' => [fn() => User::factory()->create()], + 'License checked out to asset' => [fn() => Asset::factory()->laptopMbp()->create()], + ]; + } + + public function testAccessoryCheckoutSendsSlackNotificationWhenSettingEnabled() + { + $this->settings->enableSlackWebhook(); + + $this->fireCheckOutEvent( + Accessory::factory()->create(), + User::factory()->create(), + ); + + $this->assertSlackNotificationSent(CheckoutAccessoryNotification::class); + } + + public function testAccessoryCheckoutDoesNotSendSlackNotificationWhenSettingDisabled() + { + $this->settings->disableSlackWebhook(); + + $this->fireCheckOutEvent( + Accessory::factory()->create(), + User::factory()->create(), + ); + + $this->assertNoSlackNotificationSent(CheckoutAccessoryNotification::class); + } + + /** @dataProvider assetCheckoutTargets */ + public function testAssetCheckoutSendsSlackNotificationWhenSettingEnabled($checkoutTarget) + { + $this->settings->enableSlackWebhook(); + + $this->fireCheckOutEvent( + Asset::factory()->create(), + $checkoutTarget(), + ); + + $this->assertSlackNotificationSent(CheckoutAssetNotification::class); + } + + /** @dataProvider assetCheckoutTargets */ + public function testAssetCheckoutDoesNotSendSlackNotificationWhenSettingDisabled($checkoutTarget) + { + $this->settings->disableSlackWebhook(); + + $this->fireCheckOutEvent( + Asset::factory()->create(), + $checkoutTarget(), + ); + + $this->assertNoSlackNotificationSent(CheckoutAssetNotification::class); + } + + public function testComponentCheckoutDoesNotSendSlackNotification() + { + $this->settings->enableSlackWebhook(); + + $this->fireCheckOutEvent( + Component::factory()->create(), + Asset::factory()->laptopMbp()->create(), + ); + + Notification::assertNothingSent(); + } + + public function testConsumableCheckoutSendsSlackNotificationWhenSettingEnabled() + { + $this->settings->enableSlackWebhook(); + + $this->fireCheckOutEvent( + Consumable::factory()->create(), + User::factory()->create(), + ); + + $this->assertSlackNotificationSent(CheckoutConsumableNotification::class); + } + + public function testConsumableCheckoutDoesNotSendSlackNotificationWhenSettingDisabled() + { + $this->settings->disableSlackWebhook(); + + $this->fireCheckOutEvent( + Consumable::factory()->create(), + User::factory()->create(), + ); + + $this->assertNoSlackNotificationSent(CheckoutConsumableNotification::class); + } + + /** @dataProvider licenseCheckoutTargets */ + public function testLicenseCheckoutSendsSlackNotificationWhenSettingEnabled($checkoutTarget) + { + $this->settings->enableSlackWebhook(); + + $this->fireCheckOutEvent( + LicenseSeat::factory()->create(), + $checkoutTarget(), + ); + + $this->assertSlackNotificationSent(CheckoutLicenseSeatNotification::class); + } + + /** @dataProvider licenseCheckoutTargets */ + public function testLicenseCheckoutDoesNotSendSlackNotificationWhenSettingDisabled($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(), + '', + )); + } +} diff --git a/tests/Support/AssertsAgainstSlackNotifications.php b/tests/Support/AssertsAgainstSlackNotifications.php new file mode 100644 index 0000000000..11e6beea2d --- /dev/null +++ b/tests/Support/AssertsAgainstSlackNotifications.php @@ -0,0 +1,26 @@ +routes['slack'] === Setting::getSettings()->webhook_endpoint; + } + ); + } + + public function assertNoSlackNotificationSent(string $notificationClass) + { + Notification::assertNotSentTo(new AnonymousNotifiable, $notificationClass); + } +} diff --git a/tests/Support/Settings.php b/tests/Support/Settings.php index 3104851152..2d499838c2 100644 --- a/tests/Support/Settings.php +++ b/tests/Support/Settings.php @@ -49,7 +49,7 @@ class Settings ]); } - public function disableWebhook(): Settings + public function disableSlackWebhook(): Settings { return $this->update([ 'webhook_selected' => '', diff --git a/tests/TestCase.php b/tests/TestCase.php index 03f273ad6f..535f9a3e21 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -6,12 +6,14 @@ use App\Http\Middleware\SecurityHeaders; use Illuminate\Foundation\Testing\LazilyRefreshDatabase; use Illuminate\Foundation\Testing\TestCase as BaseTestCase; use RuntimeException; +use Tests\Support\AssertsAgainstSlackNotifications; use Tests\Support\CustomTestMacros; use Tests\Support\InteractsWithAuthentication; use Tests\Support\InteractsWithSettings; abstract class TestCase extends BaseTestCase { + use AssertsAgainstSlackNotifications; use CreatesApplication; use CustomTestMacros; use InteractsWithAuthentication;