Add page visit Telegram notifications
Some checks failed
Tests / Laravel tests (pull_request) Failing after 6m57s
Some checks failed
Tests / Laravel tests (pull_request) Failing after 6m57s
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
<?php
|
||||
|
||||
use App\Jobs\NotifyTelegramAboutContactMessage;
|
||||
use App\Jobs\NotifyTelegramAboutPageVisit;
|
||||
use App\Jobs\NotifyTelegramAboutPersonaliaClick;
|
||||
use App\Models\Education;
|
||||
use App\Models\Personalia;
|
||||
@@ -99,3 +100,55 @@ test('a contact message requires a name and message', function () {
|
||||
|
||||
Queue::assertNotPushed(NotifyTelegramAboutContactMessage::class);
|
||||
});
|
||||
|
||||
test('a page visit can be submitted and is queued for notification', function () {
|
||||
Queue::fake();
|
||||
|
||||
$this->withHeaders([
|
||||
'User-Agent' => 'Pest Browser',
|
||||
'Accept-Language' => 'nl-NL,nl;q=0.9',
|
||||
])->postJson(route('page-visits.store'), [
|
||||
'visitor_id' => 'visitor-123',
|
||||
'url' => 'https://cv.robert.ooo/',
|
||||
'path' => '/',
|
||||
'title' => 'CV Roberto',
|
||||
'referrer' => 'https://example.com',
|
||||
'language' => 'nl-NL',
|
||||
'timezone' => 'Europe/Amsterdam',
|
||||
'screen' => [
|
||||
'width' => 1920,
|
||||
'height' => 1080,
|
||||
],
|
||||
'viewport' => [
|
||||
'width' => 1440,
|
||||
'height' => 900,
|
||||
],
|
||||
'device_pixel_ratio' => 1,
|
||||
'color_depth' => 24,
|
||||
'platform' => 'Linux x86_64',
|
||||
'vendor' => 'Google Inc.',
|
||||
'hardware_concurrency' => 8,
|
||||
'device_memory' => 8,
|
||||
'cookies_enabled' => true,
|
||||
'online' => true,
|
||||
])
|
||||
->assertOk()
|
||||
->assertJson([
|
||||
'status' => 'queued',
|
||||
]);
|
||||
|
||||
Queue::assertPushed(NotifyTelegramAboutPageVisit::class);
|
||||
});
|
||||
|
||||
test('a page visit requires a visitor id and valid url', function () {
|
||||
Queue::fake();
|
||||
|
||||
$this->postJson(route('page-visits.store'), [
|
||||
'visitor_id' => '',
|
||||
'url' => 'not-a-url',
|
||||
])
|
||||
->assertUnprocessable()
|
||||
->assertJsonValidationErrors(['visitor_id', 'url']);
|
||||
|
||||
Queue::assertNotPushed(NotifyTelegramAboutPageVisit::class);
|
||||
});
|
||||
|
||||
78
tests/Feature/TelegramNotificationServiceTest.php
Normal file
78
tests/Feature/TelegramNotificationServiceTest.php
Normal file
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
use App\Models\Personalia;
|
||||
use App\Services\TelegramNotificationService;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
|
||||
test('telegram service sends a contact message notification', function () {
|
||||
config([
|
||||
'services.telegram.bot_token' => 'telegram-token',
|
||||
'services.telegram.chat_id' => 'telegram-chat',
|
||||
]);
|
||||
|
||||
Http::fake();
|
||||
|
||||
app(TelegramNotificationService::class)->notifyContactMessage(
|
||||
name: 'Roberto',
|
||||
message: 'Hoi, ik wil graag contact opnemen.',
|
||||
ip: '127.0.0.1',
|
||||
userAgent: 'Pest Browser',
|
||||
email: 'roberto@example.com',
|
||||
phone: '+31612345678'
|
||||
);
|
||||
|
||||
Http::assertSent(fn ($request) => $request->url() === 'https://api.telegram.org/bottelegram-token/sendMessage'
|
||||
&& $request['chat_id'] === 'telegram-chat'
|
||||
&& str_contains($request['text'], 'Nieuw contactbericht ontvangen')
|
||||
&& str_contains($request['text'], 'roberto@example.com'));
|
||||
});
|
||||
|
||||
test('telegram service sends a personalia click notification', function () {
|
||||
config([
|
||||
'services.telegram.bot_token' => 'telegram-token',
|
||||
'services.telegram.chat_id' => 'telegram-chat',
|
||||
]);
|
||||
|
||||
Http::fake();
|
||||
|
||||
$personalia = Personalia::factory()->create([
|
||||
'value' => 'roberto@example.com',
|
||||
]);
|
||||
|
||||
app(TelegramNotificationService::class)->notifyPersonaliaClick($personalia, '127.0.0.1', 'Pest Browser');
|
||||
|
||||
Http::assertSent(fn ($request) => str_contains($request['text'], 'Persoonlijke gegevens bekeken')
|
||||
&& str_contains($request['text'], 'roberto@example.com'));
|
||||
});
|
||||
|
||||
test('telegram service sends a page visit notification', function () {
|
||||
config([
|
||||
'services.telegram.bot_token' => 'telegram-token',
|
||||
'services.telegram.chat_id' => 'telegram-chat',
|
||||
]);
|
||||
|
||||
Http::fake();
|
||||
|
||||
app(TelegramNotificationService::class)->notifyPageVisit([
|
||||
'visitor_id' => 'visitor-123',
|
||||
'url' => 'https://cv.robert.ooo/',
|
||||
'path' => '/',
|
||||
'title' => 'CV Roberto',
|
||||
'referrer' => 'https://example.com',
|
||||
'language' => 'nl-NL',
|
||||
'timezone' => 'Europe/Amsterdam',
|
||||
'screen' => [
|
||||
'width' => 1920,
|
||||
'height' => 1080,
|
||||
],
|
||||
'viewport' => [
|
||||
'width' => 1440,
|
||||
'height' => 900,
|
||||
],
|
||||
], '127.0.0.1', 'Pest Browser', 'nl-NL,nl;q=0.9');
|
||||
|
||||
Http::assertSent(fn ($request) => str_contains($request['text'], 'Pagina bezocht')
|
||||
&& str_contains($request['text'], 'Visitor ID: visitor-123')
|
||||
&& str_contains($request['text'], 'Scherm: 1920x1080')
|
||||
&& str_contains($request['text'], 'Viewport: 1440x900'));
|
||||
});
|
||||
Reference in New Issue
Block a user