Merge pull request #14436 from marcusmoore/chore/sc-24901

Organized notification test cases
This commit is contained in:
snipe 2024-03-18 12:20:23 +00:00 committed by GitHub
commit 09d69b214b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 421 additions and 475 deletions

View file

@ -1,96 +0,0 @@
<?php
namespace Tests\Feature\Notifications;
use App\Events\CheckoutableCheckedIn;
use App\Events\CheckoutableCheckedOut;
use App\Models\Accessory;
use App\Models\Setting;
use App\Models\User;
use App\Notifications\CheckinAccessoryNotification;
use App\Notifications\CheckoutAccessoryNotification;
use Illuminate\Notifications\AnonymousNotifiable;
use Illuminate\Support\Facades\Notification;
use Tests\Support\InteractsWithSettings;
use Tests\TestCase;
class AccessoryWebhookTest extends TestCase
{
use InteractsWithSettings;
public function testAccessoryCheckoutSendsWebhookNotificationWhenSettingEnabled()
{
Notification::fake();
$this->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);
}
}

View file

@ -1,163 +0,0 @@
<?php
namespace Tests\Feature\Notifications;
use App\Events\CheckoutableCheckedIn;
use App\Events\CheckoutableCheckedOut;
use App\Models\Asset;
use App\Models\Location;
use App\Models\Setting;
use App\Models\User;
use App\Notifications\CheckinAssetNotification;
use App\Notifications\CheckoutAssetNotification;
use Illuminate\Notifications\AnonymousNotifiable;
use Illuminate\Support\Facades\Notification;
use Tests\Support\InteractsWithSettings;
use Tests\TestCase;
class AssetWebhookTest extends TestCase
{
use InteractsWithSettings;
public function targets(): array
{
return [
'Asset checked out to user' => [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();
}
}

View file

@ -1,50 +0,0 @@
<?php
namespace Tests\Feature\Notifications;
use App\Events\CheckoutableCheckedIn;
use App\Events\CheckoutableCheckedOut;
use App\Models\Asset;
use App\Models\Component;
use App\Models\User;
use Illuminate\Support\Facades\Notification;
use Tests\Support\InteractsWithSettings;
use Tests\TestCase;
class ComponentWebhookTest extends TestCase
{
use InteractsWithSettings;
public function testComponentCheckoutDoesNotSendWebhookNotification()
{
Notification::fake();
$this->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();
}
}

View file

@ -1,56 +0,0 @@
<?php
namespace Tests\Feature\Notifications;
use App\Events\CheckoutableCheckedOut;
use App\Models\Consumable;
use App\Models\Setting;
use App\Models\User;
use App\Notifications\CheckoutConsumableNotification;
use Illuminate\Notifications\AnonymousNotifiable;
use Illuminate\Support\Facades\Notification;
use Tests\Support\InteractsWithSettings;
use Tests\TestCase;
class ConsumableWebhookTest extends TestCase
{
use InteractsWithSettings;
public function testConsumableCheckoutSendsWebhookNotificationWhenSettingEnabled()
{
Notification::fake();
$this->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);
}
}

View file

@ -0,0 +1,70 @@
<?php
namespace Tests\Feature\Notifications\Email;
use App\Events\CheckoutableCheckedIn;
use App\Models\Asset;
use App\Models\User;
use App\Notifications\CheckinAssetNotification;
use Illuminate\Support\Facades\Notification;
use Tests\Support\InteractsWithSettings;
use Tests\TestCase;
/**
* @group notifications
*/
class EmailNotificationsUponCheckinTest extends TestCase
{
use InteractsWithSettings;
protected function setUp(): void
{
parent::setUp();
Notification::fake();
}
public function testCheckInEmailSentToUserIfSettingEnabled()
{
$user = User::factory()->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(),
''
));
}
}

View file

@ -1,109 +0,0 @@
<?php
namespace Tests\Feature\Notifications;
use App\Events\CheckoutableCheckedIn;
use App\Events\CheckoutableCheckedOut;
use App\Models\Asset;
use App\Models\LicenseSeat;
use App\Models\Setting;
use App\Models\User;
use App\Notifications\CheckinLicenseSeatNotification;
use App\Notifications\CheckoutLicenseSeatNotification;
use Illuminate\Notifications\AnonymousNotifiable;
use Illuminate\Support\Facades\Notification;
use Tests\Support\InteractsWithSettings;
use Tests\TestCase;
class LicenseWebhookTest extends TestCase
{
use InteractsWithSettings;
public function targets(): array
{
return [
'License checked out to user' => [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);
}
}

View file

@ -0,0 +1,148 @@
<?php
namespace Tests\Feature\Notifications\Webhooks;
use App\Events\CheckoutableCheckedIn;
use App\Models\Accessory;
use App\Models\Asset;
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\CheckinLicenseSeatNotification;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Notification;
use Tests\Support\InteractsWithSettings;
use Tests\TestCase;
/**
* @group notifications
*/
class SlackNotificationsUponCheckinTest extends TestCase
{
use InteractsWithSettings;
protected function setUp(): void
{
parent::setUp();
Notification::fake();
}
public 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 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(),
''
));
}
}

View file

@ -0,0 +1,174 @@
<?php
namespace Tests\Feature\Notifications\Webhooks;
use App\Events\CheckoutableCheckedOut;
use App\Models\Accessory;
use App\Models\Asset;
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\CheckoutConsumableNotification;
use App\Notifications\CheckoutLicenseSeatNotification;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Notification;
use Tests\Support\InteractsWithSettings;
use Tests\TestCase;
/**
* @group notifications
*/
class SlackNotificationsUponCheckoutTest extends TestCase
{
use InteractsWithSettings;
protected function setUp(): void
{
parent::setUp();
Notification::fake();
}
public function assetCheckoutTargets(): 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 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(),
'',
));
}
}

View file

@ -0,0 +1,26 @@
<?php
namespace Tests\Support;
use App\Models\Setting;
use Illuminate\Notifications\AnonymousNotifiable;
use Illuminate\Support\Facades\Notification;
trait AssertsAgainstSlackNotifications
{
public function assertSlackNotificationSent(string $notificationClass)
{
Notification::assertSentTo(
new AnonymousNotifiable,
$notificationClass,
function ($notification, $channels, $notifiable) {
return $notifiable->routes['slack'] === Setting::getSettings()->webhook_endpoint;
}
);
}
public function assertNoSlackNotificationSent(string $notificationClass)
{
Notification::assertNotSentTo(new AnonymousNotifiable, $notificationClass);
}
}

View file

@ -49,7 +49,7 @@ class Settings
]);
}
public function disableWebhook(): Settings
public function disableSlackWebhook(): Settings
{
return $this->update([
'webhook_selected' => '',

View file

@ -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;