78 lines
2.4 KiB
PHP
78 lines
2.4 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
use App\Events\PreviewSiteBuilt;
|
||
|
|
use App\Models\User;
|
||
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||
|
|
use Illuminate\Support\Facades\Event;
|
||
|
|
|
||
|
|
use function Pest\Laravel\actingAs;
|
||
|
|
use function Pest\Laravel\postJson;
|
||
|
|
|
||
|
|
uses(RefreshDatabase::class);
|
||
|
|
|
||
|
|
test('send preview site built notification requires authentication', function () {
|
||
|
|
postJson('/api/notifications/preview-site-built', [
|
||
|
|
'message' => 'Test notification',
|
||
|
|
])->assertUnauthorized();
|
||
|
|
});
|
||
|
|
|
||
|
|
test('send preview site built notification requires admin permissions', function () {
|
||
|
|
$user = User::factory()->create();
|
||
|
|
|
||
|
|
actingAs($user)
|
||
|
|
->postJson('/api/notifications/preview-site-built', [
|
||
|
|
'message' => 'Test notification',
|
||
|
|
])
|
||
|
|
->assertForbidden();
|
||
|
|
});
|
||
|
|
|
||
|
|
test('send preview site built notification validates message requirement', function () {
|
||
|
|
$adminEmail = config('app.admin_email');
|
||
|
|
$admin = User::factory()->create(['email' => $adminEmail]);
|
||
|
|
|
||
|
|
actingAs($admin)
|
||
|
|
->postJson('/api/notifications/preview-site-built', [])
|
||
|
|
->assertUnprocessable()
|
||
|
|
->assertJsonValidationErrors(['message']);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('send preview site built notification validates message length', function () {
|
||
|
|
$adminEmail = config('app.admin_email');
|
||
|
|
$admin = User::factory()->create(['email' => $adminEmail]);
|
||
|
|
|
||
|
|
actingAs($admin)
|
||
|
|
->postJson('/api/notifications/preview-site-built', [
|
||
|
|
'message' => str_repeat('a', 256), // 256 characters, over the 255 limit
|
||
|
|
])
|
||
|
|
->assertUnprocessable()
|
||
|
|
->assertJsonValidationErrors(['message']);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('send preview site built notification dispatches event successfully', function () {
|
||
|
|
Event::fake();
|
||
|
|
|
||
|
|
$adminEmail = config('app.admin_email');
|
||
|
|
$admin = User::factory()->create(['email' => $adminEmail]);
|
||
|
|
$message = 'Test preview site notification';
|
||
|
|
|
||
|
|
actingAs($admin)
|
||
|
|
->postJson('/api/notifications/preview-site-built', [
|
||
|
|
'message' => $message,
|
||
|
|
])
|
||
|
|
->assertSuccessful()
|
||
|
|
->assertJsonStructure([
|
||
|
|
'success',
|
||
|
|
'message',
|
||
|
|
'data' => [
|
||
|
|
'notification_message',
|
||
|
|
],
|
||
|
|
])
|
||
|
|
->assertJsonPath('success', true)
|
||
|
|
->assertJsonPath('data.notification_message', $message);
|
||
|
|
|
||
|
|
Event::assertDispatched(PreviewSiteBuilt::class, function ($event) use ($message) {
|
||
|
|
return $event->message === $message
|
||
|
|
&& $event->type === 'success';
|
||
|
|
});
|
||
|
|
});
|