- Add highly optimized Dockerfile with Nginx and PHP-FPM 8.4 - Add docker-compose.yml configured with Redis and MariaDB 10.11 - Implement entrypoint.sh and supervisord.conf for background workers - Refactor legacy IMAP scripts into scheduled Artisan Commands - Secure app by removing old routes with hardcoded basic auth credentials - Configure email attachments to use Laravel Storage instead of insecure public/tmp
160 lines
4.7 KiB
PHP
160 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit;
|
|
|
|
use App\Http\Controllers\WebhookController;
|
|
use App\NotifyMe;
|
|
use Exception;
|
|
use Illuminate\Support\Facades\Config;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Tests\TestCase;
|
|
|
|
// Create a test class that uses the trait
|
|
class TestNotifier
|
|
{
|
|
use NotifyMe;
|
|
}
|
|
|
|
class NotifyMeTest extends TestCase
|
|
{
|
|
public $notifier;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->notifier = new TestNotifier;
|
|
}
|
|
|
|
/** @test */
|
|
public function it_sends_telegram_notification_successfully(): void
|
|
{
|
|
Config::set('app.notify_tg_bot_token', 'test_bot_token');
|
|
Config::set('app.notify_tg_chat_id', 'test_chat_id');
|
|
|
|
Http::fake([
|
|
'https://api.telegram.org/bottest_bot_token/sendMessage' => Http::response([
|
|
'ok' => true,
|
|
'result' => ['message_id' => 123],
|
|
], 200),
|
|
]);
|
|
|
|
$result = $this->notifier->sendTelegramNotification('Test message');
|
|
|
|
$this->assertTrue($result);
|
|
Http::assertSent(fn (array $request): bool => $request->url() === 'https://api.telegram.org/bottest_bot_token/sendMessage' &&
|
|
$request['chat_id'] === 'test_chat_id' &&
|
|
$request['text'] === 'Test message' &&
|
|
$request['parse_mode'] === 'HTML');
|
|
}
|
|
|
|
/** @test */
|
|
public function it_fails_when_bot_token_is_not_configured(): void
|
|
{
|
|
Config::set('app.notify_tg_bot_token');
|
|
Config::set('app.notify_tg_chat_id', 'test_chat_id');
|
|
|
|
Log::shouldReceive('error')
|
|
->once()
|
|
->with('Telegram bot token or chat ID not configured');
|
|
|
|
$result = $this->notifier->sendTelegramNotification('Test message');
|
|
|
|
$this->assertFalse($result);
|
|
}
|
|
|
|
/** @test */
|
|
public function it_fails_when_chat_id_is_not_configured(): void
|
|
{
|
|
Config::set('app.notify_tg_bot_token', 'test_bot_token');
|
|
Config::set('app.notify_tg_chat_id');
|
|
|
|
Log::shouldReceive('error')
|
|
->once()
|
|
->with('Telegram bot token or chat ID not configured');
|
|
|
|
$result = $this->notifier->sendTelegramNotification('Test message');
|
|
|
|
$this->assertFalse($result);
|
|
}
|
|
|
|
/** @test */
|
|
public function it_handles_http_errors_gracefully(): void
|
|
{
|
|
Config::set('app.notify_tg_bot_token', 'test_bot_token');
|
|
Config::set('app.notify_tg_chat_id', 'test_chat_id');
|
|
|
|
Http::fake([
|
|
'https://api.telegram.org/bottest_bot_token/sendMessage' => Http::response([
|
|
'ok' => false,
|
|
'error_code' => 400,
|
|
'description' => 'Bad Request',
|
|
], 400),
|
|
]);
|
|
|
|
$result = $this->notifier->sendTelegramNotification('Test message');
|
|
|
|
$this->assertFalse($result);
|
|
}
|
|
|
|
/** @test */
|
|
public function it_handles_network_exceptions(): void
|
|
{
|
|
Config::set('app.notify_tg_bot_token', 'test_bot_token');
|
|
Config::set('app.notify_tg_chat_id', 'test_chat_id');
|
|
|
|
Http::fake([
|
|
'https://api.telegram.org/bottest_bot_token/sendMessage' => Http::throw(function ($request): void {
|
|
throw new Exception('Network error');
|
|
}),
|
|
]);
|
|
|
|
Log::shouldReceive('error')
|
|
->once();
|
|
|
|
$result = $this->notifier->sendTelegramNotification('Test message');
|
|
|
|
$this->assertFalse($result);
|
|
}
|
|
|
|
/** @test */
|
|
public function it_sends_messages_with_html_parsing_mode(): void
|
|
{
|
|
Config::set('app.notify_tg_bot_token', 'test_bot_token');
|
|
Config::set('app.notify_tg_chat_id', 'test_chat_id');
|
|
|
|
Http::fake([
|
|
'https://api.telegram.org/bottest_bot_token/sendMessage' => Http::response([
|
|
'ok' => true,
|
|
'result' => ['message_id' => 123],
|
|
], 200),
|
|
]);
|
|
|
|
$htmlMessage = '<b>Bold text</b> and <i>italic text</i>';
|
|
$this->notifier->sendTelegramNotification($htmlMessage);
|
|
|
|
Http::assertSent(fn (array $request): bool => $request['parse_mode'] === 'HTML' &&
|
|
$request['text'] === $htmlMessage);
|
|
}
|
|
|
|
/** @test */
|
|
public function it_can_be_used_in_controller_context(): void
|
|
{
|
|
Config::set('app.notify_tg_bot_token', 'test_bot_token');
|
|
Config::set('app.notify_tg_chat_id', 'test_chat_id');
|
|
|
|
Http::fake([
|
|
'https://api.telegram.org/bottest_bot_token/sendMessage' => Http::response([
|
|
'ok' => true,
|
|
'result' => ['message_id' => 123],
|
|
], 200),
|
|
]);
|
|
|
|
$controller = new WebhookController;
|
|
$result = $controller->sendTelegramNotification('Test from controller');
|
|
|
|
$this->assertTrue($result);
|
|
}
|
|
}
|