2023-03-13 15:47:50 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Feature\Notifications;
|
|
|
|
|
|
|
|
use App\Models\Asset;
|
|
|
|
use App\Models\Location;
|
|
|
|
use App\Models\Setting;
|
|
|
|
use App\Models\User;
|
|
|
|
use App\Notifications\CheckoutAssetNotification;
|
2023-03-21 17:03:51 -07:00
|
|
|
use Illuminate\Notifications\AnonymousNotifiable;
|
2023-03-13 15:47:50 -07:00
|
|
|
use Illuminate\Support\Facades\Notification;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
class AssetCheckoutSlackNotificationTest extends TestCase
|
|
|
|
{
|
2023-03-21 17:03:51 -07:00
|
|
|
private string $slackWebhookUrl = 'https://hooks.slack.com/services/NZ59O2F54K/Q4465WNLM8/672N8MU5JV15RP436WDHRN58';
|
|
|
|
|
2023-03-13 15:47:50 -07:00
|
|
|
public function testNotificationSentToSlackWhenAssetCheckedOutToUserAndSlackNotificationEnabled()
|
|
|
|
{
|
|
|
|
Notification::fake();
|
|
|
|
|
2023-03-22 12:27:36 -07:00
|
|
|
Setting::factory()->create(['webhook_endpoint' => $this->slackWebhookUrl]);
|
2023-03-13 15:47:50 -07:00
|
|
|
|
2023-03-22 12:27:36 -07:00
|
|
|
$asset = Asset::factory()->laptopMbp()->create();
|
2023-03-13 15:47:50 -07:00
|
|
|
$user = User::factory()->create();
|
|
|
|
|
|
|
|
$asset->checkOut(
|
|
|
|
$user,
|
|
|
|
User::factory()->superuser()->create()->id
|
|
|
|
);
|
|
|
|
|
2023-03-21 17:03:51 -07:00
|
|
|
Notification::assertSentTo(
|
|
|
|
$user,
|
|
|
|
function (CheckoutAssetNotification $notification, $channels) {
|
|
|
|
// @todo: is this actually accurate?
|
|
|
|
return in_array('slack', $channels);
|
|
|
|
}
|
|
|
|
);
|
2023-03-13 15:47:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testNotificationSentToSlackWhenAssetCheckedOutToAssetAndSlackNotificationEnabled()
|
|
|
|
{
|
|
|
|
Notification::fake();
|
|
|
|
|
2023-03-22 12:27:36 -07:00
|
|
|
Setting::factory()->create(['webhook_endpoint' => $this->slackWebhookUrl]);
|
2023-03-13 15:47:50 -07:00
|
|
|
|
2023-03-22 12:27:36 -07:00
|
|
|
$assetBeingCheckedOut = Asset::factory()->laptopMbp()->create();
|
2023-03-21 17:03:51 -07:00
|
|
|
$assetBeingCheckedOut->checkOut(
|
2023-03-22 12:27:36 -07:00
|
|
|
Asset::factory()->laptopMbp()->create(),
|
2023-03-21 17:03:51 -07:00
|
|
|
User::factory()->superuser()->create()->id
|
|
|
|
);
|
|
|
|
|
|
|
|
// Since the target is not a user with an email address we have
|
|
|
|
// to check if an AnonymousNotifiable was sent.
|
|
|
|
Notification::assertSentTo(
|
|
|
|
new AnonymousNotifiable,
|
|
|
|
CheckoutAssetNotification::class,
|
|
|
|
function ($notification, $channels, $notifiable) {
|
|
|
|
return $notifiable->routes['slack'] === $this->slackWebhookUrl;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDoesNotSendNotificationViaSlackIfWebHookEndpointIsNotSetWhenCheckingOutAssetToAsset()
|
|
|
|
{
|
|
|
|
Notification::fake();
|
2023-03-13 15:47:50 -07:00
|
|
|
|
2023-03-22 12:27:36 -07:00
|
|
|
$assetBeingCheckedOut = Asset::factory()->laptopMbp()->create();
|
2023-03-13 15:47:50 -07:00
|
|
|
$assetBeingCheckedOut->checkOut(
|
2023-03-22 12:27:36 -07:00
|
|
|
Asset::factory()->laptopMbp()->create(),
|
2023-03-13 15:47:50 -07:00
|
|
|
User::factory()->superuser()->create()->id
|
|
|
|
);
|
|
|
|
|
2023-03-21 17:03:51 -07:00
|
|
|
Notification::assertNotSentTo(
|
|
|
|
new AnonymousNotifiable,
|
|
|
|
CheckoutAssetNotification::class,
|
|
|
|
);
|
2023-03-13 15:47:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testNotificationSentToSlackWhenAssetCheckedOutToLocationAndSlackNotificationEnabled()
|
|
|
|
{
|
|
|
|
Notification::fake();
|
|
|
|
|
2023-03-22 12:27:36 -07:00
|
|
|
Setting::factory()->create(['webhook_endpoint' => $this->slackWebhookUrl]);
|
2023-03-13 15:47:50 -07:00
|
|
|
|
2023-03-22 12:27:36 -07:00
|
|
|
$asset = Asset::factory()->laptopMbp()->create();
|
2023-03-13 15:47:50 -07:00
|
|
|
$asset->checkOut(
|
2023-03-21 17:03:51 -07:00
|
|
|
Location::factory()->create(),
|
2023-03-13 15:47:50 -07:00
|
|
|
User::factory()->superuser()->create()->id
|
|
|
|
);
|
|
|
|
|
2023-03-21 17:03:51 -07:00
|
|
|
// Since the target is not a user with an email address we have
|
|
|
|
// to check if an AnonymousNotifiable was sent.
|
|
|
|
Notification::assertSentTo(
|
|
|
|
new AnonymousNotifiable,
|
|
|
|
CheckoutAssetNotification::class,
|
|
|
|
function ($notification, $channels, $notifiable) {
|
|
|
|
return $notifiable->routes['slack'] === $this->slackWebhookUrl;
|
|
|
|
}
|
|
|
|
);
|
2023-03-13 15:47:50 -07:00
|
|
|
}
|
|
|
|
|
2023-03-22 11:56:54 -07:00
|
|
|
public function testDoesNotSendNotificationViaSlackIfWebHookEndpointIsNotSetWhenCheckingOutAssetToLocation()
|
2023-03-13 15:47:50 -07:00
|
|
|
{
|
2023-03-21 17:03:51 -07:00
|
|
|
Notification::fake();
|
|
|
|
|
2023-03-22 12:27:36 -07:00
|
|
|
$asset = Asset::factory()->laptopMbp()->create();
|
2023-03-21 17:03:51 -07:00
|
|
|
$asset->checkOut(
|
|
|
|
Location::factory()->create(),
|
|
|
|
User::factory()->superuser()->create()->id
|
|
|
|
);
|
|
|
|
|
|
|
|
Notification::assertNotSentTo(
|
|
|
|
new AnonymousNotifiable,
|
|
|
|
CheckoutAssetNotification::class,
|
|
|
|
);
|
2023-03-13 15:47:50 -07:00
|
|
|
}
|
|
|
|
}
|