adds tests, webhook enable settings
This commit is contained in:
@@ -33,6 +33,10 @@ class AuditNotification extends Notification
|
||||
//
|
||||
$this->settings = Setting::getSettings();
|
||||
$this->params = $params;
|
||||
$item = $params['item'];
|
||||
if (!$item || !is_object($item)) {
|
||||
throw new \InvalidArgumentException('Notification requires a valid item.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -93,12 +97,12 @@ class AuditNotification extends Notification
|
||||
return MicrosoftTeamsMessage::create()
|
||||
->to($setting->webhook_endpoint)
|
||||
->type('success')
|
||||
->title(class_basename(get_class($params['item'])) .' '.trans('general.audited'))
|
||||
->title(class_basename($item).' '.trans('general.audited'))
|
||||
->addStartGroupToSection('activityText')
|
||||
->fact(trans('mail.asset'), $item)
|
||||
->fact(trans('general.administrator'), $admin_user->present()->viewUrl() . '|' . $admin_user->display_name);
|
||||
}
|
||||
$message = class_basename(get_class($params['item'])) . ' Audited By '.$admin_user->display_name;
|
||||
$message = class_basename(get_class($params['item'])) . trans('general.audited_by').' '.$admin_user->display_name;
|
||||
$details = [
|
||||
trans('mail.asset') => htmlspecialchars_decode($item->display_name),
|
||||
trans('mail.notes') => $note ?: '',
|
||||
|
||||
@@ -39,6 +39,7 @@ return [
|
||||
'accept_items' => 'Accept Items',
|
||||
'audit' => 'Audit',
|
||||
'audited' => 'Audited',
|
||||
'audited_by' => 'Audited By',
|
||||
'audits' => 'Audits',
|
||||
'audit_report' => 'Audit Log',
|
||||
'assets' => 'Assets',
|
||||
|
||||
60
tests/Feature/Notifications/Webhooks/NotificationTests.php
Normal file
60
tests/Feature/Notifications/Webhooks/NotificationTests.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
namespace Tests\Feature\Notifications\Webhooks;
|
||||
|
||||
use App\Models\Asset;
|
||||
use App\Models\User;
|
||||
use App\Notifications\AuditNotification;
|
||||
use Illuminate\Support\Facades\Notification;
|
||||
use PHPUnit\Framework\Attributes\Group;
|
||||
use Tests\TestCase;
|
||||
use Tests\Support\Settings;
|
||||
|
||||
#[Group('notifications')]
|
||||
class NotificationTests extends TestCase
|
||||
{
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
Notification::fake();
|
||||
}
|
||||
public function testAuditNotificationThrowsWhenItemIsNull()
|
||||
{
|
||||
try {
|
||||
new AuditNotification([
|
||||
'item' => null,
|
||||
]);
|
||||
$this->fail('Expected Error was not thrown');
|
||||
} catch (\Throwable $e) {
|
||||
$this->assertInstanceOf(\InvalidArgumentException::class, $e);
|
||||
$this->assertSame('Notification requires a valid item.', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function testAuditNotificationFires()
|
||||
{
|
||||
$webhook_options = [
|
||||
'enableSlackWebhook',
|
||||
'enableMicrosoftTeamsWebhook',
|
||||
];
|
||||
|
||||
Notification::fake();
|
||||
|
||||
foreach($webhook_options as $option) {
|
||||
|
||||
$this->settings->{$option}();
|
||||
|
||||
$user = User::factory()->create();
|
||||
$item = Asset::factory()->create();
|
||||
|
||||
try {
|
||||
$user->notify(new \App\Notifications\AuditNotification([
|
||||
'item' => $item,
|
||||
]));
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
$this->fail("AuditNotification threw for [{$option}]: {$e->getMessage()}");
|
||||
}
|
||||
}
|
||||
Notification::assertSentTimes(AuditNotification::class, count($webhook_options));
|
||||
}
|
||||
}
|
||||
@@ -93,6 +93,13 @@ class Settings
|
||||
'webhook_channel' => '#it',
|
||||
]);
|
||||
}
|
||||
public function enableMicrosoftTeamsWebhook(): Settings
|
||||
{
|
||||
return $this->update([
|
||||
'webhook_selected' => 'microsoft',
|
||||
'webhook_endpoint' => 'https://defaultd07ceb04416641fca1b9d3e0ac7600.84.environment.api.powerplatform.com:443/powerautomate/automations/direct/workflows/1babbc7a3cdd4cf99c0fbed4367cf147/triggers/manual/paths/invoke?api-version=1&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=sVXmAYF5luz3oOEjvN-G7mJqEEvFjBSETuAG8c3Qmkg',
|
||||
]);
|
||||
}
|
||||
|
||||
public function disableSlackWebhook(): Settings
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user