From f70963d6bbbaaf7ee0e76a5fdc49f74883c37f74 Mon Sep 17 00:00:00 2001 From: Saeed Vaziry <61919774+saeedvaziry@users.noreply.github.com> Date: Fri, 16 Feb 2024 21:10:17 +0100 Subject: [PATCH] fix notification chanels and more tests (#108) * fix notification chanels and more tests * fix code style --- app/Helpers/Notifier.php | 4 +- app/NotificationChannels/Discord.php | 5 - app/NotificationChannels/Email.php | 5 +- app/NotificationChannels/Slack.php | 5 - app/NotificationChannels/Telegram.php | 5 - app/Notifications/AbstractNotification.php | 8 ++ app/ServerTypes/Regular.php | 2 +- package.json | 4 +- tests/Feature/{Http => }/ApplicationTest.php | 2 +- .../{Http => }/Auth/AuthenticationTest.php | 2 +- tests/Feature/{Http => }/Auth/LogoutTest.php | 2 +- .../Auth/PasswordConfirmationTest.php | 2 +- .../{Http => }/Auth/PasswordResetTest.php | 2 +- .../{Http => }/Auth/PasswordUpdateTest.php | 2 +- tests/Feature/{Http => }/CronjobTest.php | 2 +- .../Feature/{Http => }/DatabaseBackupTest.php | 2 +- tests/Feature/{Http => }/DatabaseTest.php | 2 +- tests/Feature/{Http => }/DatabaseUserTest.php | 2 +- tests/Feature/{Http => }/FirewallTest.php | 2 +- tests/Feature/{Http => }/LogsTest.php | 2 +- .../{Http => }/NotificationChannelsTest.php | 2 +- tests/Feature/{Http => }/PHP.php | 2 +- tests/Feature/{Http => }/ProfileTest.php | 2 +- tests/Feature/{Http => }/ProjectsTest.php | 2 +- tests/Feature/{Http => }/QueuesTest.php | 2 +- tests/Feature/{Http => }/ServerKeysTest.php | 2 +- .../{Http => }/ServerProvidersTest.php | 2 +- tests/Feature/{Http => }/ServerTest.php | 2 +- tests/Feature/{Http => }/ServicesTest.php | 2 +- tests/Feature/{Http => }/SitesTest.php | 2 +- .../Feature/{Http => }/SourceControlsTest.php | 2 +- tests/Feature/{Http => }/SshKeysTest.php | 2 +- tests/Feature/{Http => }/SslTest.php | 2 +- .../{Http => }/StorageProvidersTest.php | 2 +- .../Models/ServerModelTest.php | 2 +- .../Unit/NotificationChannels/DiscordTest.php | 87 ++++++++++++++ tests/Unit/NotificationChannels/EmailTest.php | 83 ++++++++++++++ tests/Unit/NotificationChannels/SlackTest.php | 87 ++++++++++++++ .../NotificationChannels/TelegramTest.php | 108 ++++++++++++++++++ .../NotificationChannels/TestNotification.php | 13 +++ .../CronJob/UpdateCronJobsCommandTest.php | 2 +- .../Database/BackupDatabaseCommandTest.php | 2 +- .../Database/CreateCommandTest.php | 2 +- .../Database/CreateUserCommandTest.php | 2 +- .../Database/DeleteCommandTest.php | 2 +- .../Database/DeleteUserCommandTest.php | 2 +- .../Database/InstallMariadbCommandTest.php | 2 +- .../Database/InstallMysqlCommandTest.php | 2 +- .../SSHCommands/Database/LinkCommandTest.php | 2 +- .../Database/UnlinkCommandTest.php | 2 +- .../Firewall/AddRuleCommandTest.php | 2 +- .../Firewall/InstallUfwCommandTest.php | 2 +- .../Firewall/RemoveRuleCommandTest.php | 2 +- .../Installation/InstallNodejsCommandTest.php | 2 +- .../Installation/InstallRedisCommandTest.php | 2 +- .../InstallRequirementsCommandTest.php | 2 +- .../ChangeNginxPHPVersionCommandTest.php | 2 +- .../Nginx/CreateNginxVHostCommandTest.php | 2 +- .../Nginx/DeleteNginxSiteCommandTest.php | 2 +- .../Nginx/InstallNginxCommandTest.php | 2 +- .../Nginx/UpdateNginxRedirectsCommandTest.php | 2 +- .../Nginx/UpdateNginxVHostCommandTest.php | 2 +- .../PHP/ChangeDefaultPHPCommandTest.php | 2 +- .../SSHCommands/PHP/GetPHPIniCommandTest.php | 2 +- .../PHP/InstallComposerCommandTest.php | 2 +- .../SSHCommands/PHP/InstallPHPCommandTest.php | 2 +- .../PHP/InstallPHPExtensionCommandTest.php | 2 +- .../PHP/UninstallPHPCommandTest.php | 2 +- .../CreateNginxPHPMyAdminVHostCommandTest.php | 2 +- .../DeleteNginxPHPMyAdminVHostCommandTest.php | 2 +- .../DownloadPHPMyAdminCommandTest.php | 2 +- .../SSL/CreateCustomSSLCommandTest.php | 2 +- .../SSL/CreateLetsencryptSSLCommandTest.php | 2 +- .../SSL/InstallCertbotCommandTest.php | 2 +- .../SSHCommands/SSL/RemoveSSLCommandTest.php | 2 +- .../Service/RestartServiceCommandTest.php | 2 +- .../Service/ServiceStatusCommandTest.php | 2 +- .../Service/StartServiceCommandTest.php | 2 +- .../Service/StopServiceCommandTest.php | 2 +- .../DownloadFromDropboxCommandTest.php | 2 +- .../Storage/DownloadFromFTPCommandTest.php | 2 +- .../Storage/UploadToDropboxCommandTest.php | 2 +- .../Storage/UploadToFTPCommandTest.php | 2 +- .../Supervisor/CreateWorkerCommandTest.php | 2 +- .../Supervisor/DeleteWorkerCommandTest.php | 2 +- .../InstallSupervisorCommandTest.php | 2 +- .../Supervisor/RestartWorkerCommandTest.php | 2 +- .../Supervisor/StartWorkerCommandTest.php | 2 +- .../Supervisor/StopWorkerCommandTest.php | 2 +- .../System/CreateUserCommandTest.php | 2 +- .../System/DeleteSshKeyCommandTest.php | 2 +- .../System/DeploySshKeyCommandTest.php | 2 +- .../System/EditFileCommandTest.php | 2 +- .../System/GenerateSshKeyCommandTest.php | 2 +- .../System/GetPublicKeyCommandTest.php | 2 +- .../System/ReadFileCommandTest.php | 2 +- .../System/ReadSshKeyCommandTest.php | 2 +- .../SSHCommands/System/RebootCommandTest.php | 2 +- .../System/RunScriptCommandTest.php | 2 +- .../SSHCommands/System/UpgradeCommandTest.php | 2 +- .../Website/CloneRepositoryCommandTest.php | 2 +- .../Website/ComposerInstallCommandTest.php | 2 +- .../Website/UpdateBranchCommandTest.php | 2 +- 103 files changed, 484 insertions(+), 112 deletions(-) rename tests/Feature/{Http => }/ApplicationTest.php (98%) rename tests/Feature/{Http => }/Auth/AuthenticationTest.php (96%) rename tests/Feature/{Http => }/Auth/LogoutTest.php (94%) rename tests/Feature/{Http => }/Auth/PasswordConfirmationTest.php (96%) rename tests/Feature/{Http => }/Auth/PasswordResetTest.php (98%) rename tests/Feature/{Http => }/Auth/PasswordUpdateTest.php (96%) rename tests/Feature/{Http => }/CronjobTest.php (98%) rename tests/Feature/{Http => }/DatabaseBackupTest.php (98%) rename tests/Feature/{Http => }/DatabaseTest.php (98%) rename tests/Feature/{Http => }/DatabaseUserTest.php (98%) rename tests/Feature/{Http => }/FirewallTest.php (98%) rename tests/Feature/{Http => }/LogsTest.php (95%) rename tests/Feature/{Http => }/NotificationChannelsTest.php (99%) rename tests/Feature/{Http => }/PHP.php (98%) rename tests/Feature/{Http => }/ProfileTest.php (97%) rename tests/Feature/{Http => }/ProjectsTest.php (98%) rename tests/Feature/{Http => }/QueuesTest.php (98%) rename tests/Feature/{Http => }/ServerKeysTest.php (99%) rename tests/Feature/{Http => }/ServerProvidersTest.php (98%) rename tests/Feature/{Http => }/ServerTest.php (97%) rename tests/Feature/{Http => }/ServicesTest.php (99%) rename tests/Feature/{Http => }/SitesTest.php (99%) rename tests/Feature/{Http => }/SourceControlsTest.php (98%) rename tests/Feature/{Http => }/SshKeysTest.php (98%) rename tests/Feature/{Http => }/SslTest.php (98%) rename tests/Feature/{Http => }/StorageProvidersTest.php (98%) rename tests/{Feature => Unit}/Models/ServerModelTest.php (93%) create mode 100644 tests/Unit/NotificationChannels/DiscordTest.php create mode 100644 tests/Unit/NotificationChannels/EmailTest.php create mode 100644 tests/Unit/NotificationChannels/SlackTest.php create mode 100644 tests/Unit/NotificationChannels/TelegramTest.php create mode 100644 tests/Unit/NotificationChannels/TestNotification.php rename tests/{Feature => Unit}/SSHCommands/CronJob/UpdateCronJobsCommandTest.php (90%) rename tests/{Feature => Unit}/SSHCommands/Database/BackupDatabaseCommandTest.php (92%) rename tests/{Feature => Unit}/SSHCommands/Database/CreateCommandTest.php (91%) rename tests/{Feature => Unit}/SSHCommands/Database/CreateUserCommandTest.php (91%) rename tests/{Feature => Unit}/SSHCommands/Database/DeleteCommandTest.php (88%) rename tests/{Feature => Unit}/SSHCommands/Database/DeleteUserCommandTest.php (91%) rename tests/{Feature => Unit}/SSHCommands/Database/InstallMariadbCommandTest.php (94%) rename tests/{Feature => Unit}/SSHCommands/Database/InstallMysqlCommandTest.php (97%) rename tests/{Feature => Unit}/SSHCommands/Database/LinkCommandTest.php (93%) rename tests/{Feature => Unit}/SSHCommands/Database/UnlinkCommandTest.php (91%) rename tests/{Feature => Unit}/SSHCommands/Firewall/AddRuleCommandTest.php (93%) rename tests/{Feature => Unit}/SSHCommands/Firewall/InstallUfwCommandTest.php (96%) rename tests/{Feature => Unit}/SSHCommands/Firewall/RemoveRuleCommandTest.php (93%) rename tests/{Feature => Unit}/SSHCommands/Installation/InstallNodejsCommandTest.php (91%) rename tests/{Feature => Unit}/SSHCommands/Installation/InstallRedisCommandTest.php (92%) rename tests/{Feature => Unit}/SSHCommands/Installation/InstallRequirementsCommandTest.php (92%) rename tests/{Feature => Unit}/SSHCommands/Nginx/ChangeNginxPHPVersionCommandTest.php (94%) rename tests/{Feature => Unit}/SSHCommands/Nginx/CreateNginxVHostCommandTest.php (96%) rename tests/{Feature => Unit}/SSHCommands/Nginx/DeleteNginxSiteCommandTest.php (93%) rename tests/{Feature => Unit}/SSHCommands/Nginx/InstallNginxCommandTest.php (97%) rename tests/{Feature => Unit}/SSHCommands/Nginx/UpdateNginxRedirectsCommandTest.php (93%) rename tests/{Feature => Unit}/SSHCommands/Nginx/UpdateNginxVHostCommandTest.php (93%) rename tests/{Feature => Unit}/SSHCommands/PHP/ChangeDefaultPHPCommandTest.php (93%) rename tests/{Feature => Unit}/SSHCommands/PHP/GetPHPIniCommandTest.php (90%) rename tests/{Feature => Unit}/SSHCommands/PHP/InstallComposerCommandTest.php (93%) rename tests/{Feature => Unit}/SSHCommands/PHP/InstallPHPCommandTest.php (95%) rename tests/{Feature => Unit}/SSHCommands/PHP/InstallPHPExtensionCommandTest.php (92%) rename tests/{Feature => Unit}/SSHCommands/PHP/UninstallPHPCommandTest.php (94%) rename tests/{Feature => Unit}/SSHCommands/PHPMyAdmin/CreateNginxPHPMyAdminVHostCommandTest.php (95%) rename tests/{Feature => Unit}/SSHCommands/PHPMyAdmin/DeleteNginxPHPMyAdminVHostCommandTest.php (93%) rename tests/{Feature => Unit}/SSHCommands/PHPMyAdmin/DownloadPHPMyAdminCommandTest.php (95%) rename tests/{Feature => Unit}/SSHCommands/SSL/CreateCustomSSLCommandTest.php (95%) rename tests/{Feature => Unit}/SSHCommands/SSL/CreateLetsencryptSSLCommandTest.php (93%) rename tests/{Feature => Unit}/SSHCommands/SSL/InstallCertbotCommandTest.php (91%) rename tests/{Feature => Unit}/SSHCommands/SSL/RemoveSSLCommandTest.php (90%) rename tests/{Feature => Unit}/SSHCommands/Service/RestartServiceCommandTest.php (90%) rename tests/{Feature => Unit}/SSHCommands/Service/ServiceStatusCommandTest.php (89%) rename tests/{Feature => Unit}/SSHCommands/Service/StartServiceCommandTest.php (90%) rename tests/{Feature => Unit}/SSHCommands/Service/StopServiceCommandTest.php (90%) rename tests/{Feature => Unit}/SSHCommands/Storage/DownloadFromDropboxCommandTest.php (93%) rename tests/{Feature => Unit}/SSHCommands/Storage/DownloadFromFTPCommandTest.php (93%) rename tests/{Feature => Unit}/SSHCommands/Storage/UploadToDropboxCommandTest.php (94%) rename tests/{Feature => Unit}/SSHCommands/Storage/UploadToFTPCommandTest.php (93%) rename tests/{Feature => Unit}/SSHCommands/Supervisor/CreateWorkerCommandTest.php (94%) rename tests/{Feature => Unit}/SSHCommands/Supervisor/DeleteWorkerCommandTest.php (94%) rename tests/{Feature => Unit}/SSHCommands/Supervisor/InstallSupervisorCommandTest.php (91%) rename tests/{Feature => Unit}/SSHCommands/Supervisor/RestartWorkerCommandTest.php (90%) rename tests/{Feature => Unit}/SSHCommands/Supervisor/StartWorkerCommandTest.php (90%) rename tests/{Feature => Unit}/SSHCommands/Supervisor/StopWorkerCommandTest.php (90%) rename tests/{Feature => Unit}/SSHCommands/System/CreateUserCommandTest.php (95%) rename tests/{Feature => Unit}/SSHCommands/System/DeleteSshKeyCommandTest.php (90%) rename tests/{Feature => Unit}/SSHCommands/System/DeploySshKeyCommandTest.php (91%) rename tests/{Feature => Unit}/SSHCommands/System/EditFileCommandTest.php (91%) rename tests/{Feature => Unit}/SSHCommands/System/GenerateSshKeyCommandTest.php (90%) rename tests/{Feature => Unit}/SSHCommands/System/GetPublicKeyCommandTest.php (89%) rename tests/{Feature => Unit}/SSHCommands/System/ReadFileCommandTest.php (89%) rename tests/{Feature => Unit}/SSHCommands/System/ReadSshKeyCommandTest.php (89%) rename tests/{Feature => Unit}/SSHCommands/System/RebootCommandTest.php (89%) rename tests/{Feature => Unit}/SSHCommands/System/RunScriptCommandTest.php (91%) rename tests/{Feature => Unit}/SSHCommands/System/UpgradeCommandTest.php (92%) rename tests/{Feature => Unit}/SSHCommands/Website/CloneRepositoryCommandTest.php (96%) rename tests/{Feature => Unit}/SSHCommands/Website/ComposerInstallCommandTest.php (93%) rename tests/{Feature => Unit}/SSHCommands/Website/UpdateBranchCommandTest.php (92%) diff --git a/app/Helpers/Notifier.php b/app/Helpers/Notifier.php index cdcc346..6373b44 100644 --- a/app/Helpers/Notifier.php +++ b/app/Helpers/Notifier.php @@ -8,12 +8,12 @@ class Notifier { /** - * In the future we can send notifications based on the notifiable instance + * In the future we can send notifications based on the notifiable instance, * For example, If it was a server then we will send the channels specified by that server * For now, we will send all channels. */ public function send(object $notifiable, Notification $notification): void { - // NotificationChannel::notifyAll($notification); + NotificationChannel::notifyAll($notification); } } diff --git a/app/NotificationChannels/Discord.php b/app/NotificationChannels/Discord.php index 14fcc49..ed9228d 100644 --- a/app/NotificationChannels/Discord.php +++ b/app/NotificationChannels/Discord.php @@ -7,11 +7,6 @@ class Discord extends AbstractNotificationChannel { - public function channel(): string - { - return 'discord'; - } - public function createRules(array $input): array { return [ diff --git a/app/NotificationChannels/Email.php b/app/NotificationChannels/Email.php index f429764..79c3a8b 100644 --- a/app/NotificationChannels/Email.php +++ b/app/NotificationChannels/Email.php @@ -35,7 +35,10 @@ public function connect(): bool { try { Mail::to($this->data()['email'])->send( - new NotificationMail('Test VitoDeploy', 'This is a test email!') + new NotificationMail( + 'Connected to VitoDeploy', + 'This email confirms that you have connected your email to VitoDeploy.' + ) ); } catch (Throwable) { return false; diff --git a/app/NotificationChannels/Slack.php b/app/NotificationChannels/Slack.php index f5c368b..dc7f8f4 100644 --- a/app/NotificationChannels/Slack.php +++ b/app/NotificationChannels/Slack.php @@ -7,11 +7,6 @@ class Slack extends AbstractNotificationChannel { - public function channel(): string - { - return 'slack'; - } - public function createRules(array $input): array { return [ diff --git a/app/NotificationChannels/Telegram.php b/app/NotificationChannels/Telegram.php index dc9ee1c..d8ab492 100644 --- a/app/NotificationChannels/Telegram.php +++ b/app/NotificationChannels/Telegram.php @@ -10,11 +10,6 @@ class Telegram extends AbstractNotificationChannel { protected string $apiUrl = 'https://api.telegram.org/bot'; - public function channel(): string - { - return 'telegram'; - } - public function createRules(array $input): array { return [ diff --git a/app/Notifications/AbstractNotification.php b/app/Notifications/AbstractNotification.php index 34a225f..62ba544 100644 --- a/app/Notifications/AbstractNotification.php +++ b/app/Notifications/AbstractNotification.php @@ -3,6 +3,7 @@ namespace App\Notifications; use App\Contracts\Notification as NotificationInterface; +use App\Models\NotificationChannel; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Notifications\Messages\MailMessage; @@ -13,9 +14,16 @@ abstract class AbstractNotification extends Notification implements Notification { use Queueable, SerializesModels; + public function via(object $notifiable): string + { + /** @var NotificationChannel $notifiable */ + return get_class($notifiable->provider()); + } + public function toMail(object $notifiable): MailMessage { return (new MailMessage()) + ->subject('Notification') ->line($this->rawText()); } diff --git a/app/ServerTypes/Regular.php b/app/ServerTypes/Regular.php index c524275..9b07815 100755 --- a/app/ServerTypes/Regular.php +++ b/app/ServerTypes/Regular.php @@ -91,7 +91,7 @@ public function install(): void 'server' => $this->server, ]) ); - // Notifier::send($this->server, new ServerInstallationSucceed($this->server)); + Notifier::send($this->server, new ServerInstallationSucceed($this->server)); }; Bus::chain($jobs) diff --git a/package.json b/package.json index 35d94e6..3a31d36 100644 --- a/package.json +++ b/package.json @@ -2,9 +2,7 @@ "private": true, "scripts": { "dev": "vite", - "build": "vite build", - "queue:listen": "php artisan queue:listen --timeout=600 --queue=default,ssh,ssh-long", - "scheduler:run": "php artisan schedule:run" + "build": "vite build" }, "devDependencies": { "@ryangjchandler/alpine-clipboard": "^2.2.0", diff --git a/tests/Feature/Http/ApplicationTest.php b/tests/Feature/ApplicationTest.php similarity index 98% rename from tests/Feature/Http/ApplicationTest.php rename to tests/Feature/ApplicationTest.php index 9088698..f798da0 100644 --- a/tests/Feature/Http/ApplicationTest.php +++ b/tests/Feature/ApplicationTest.php @@ -1,6 +1,6 @@ create([ + 'provider' => 'discord', + ])); + + $this->assertSame([ + 'webhook_url' => 'required|url', + ], $provider->createRules([])); + } + + public function test_create_data(): void + { + $provider = new Discord(NotificationChannel::factory()->create([ + 'provider' => 'discord', + ])); + + $this->assertSame([ + 'webhook_url' => 'https://discord.com/xxxxx', + ], $provider->createData([ + 'webhook_url' => 'https://discord.com/xxxxx', + ])); + } + + public function test_data(): void + { + $provider = new Discord(NotificationChannel::factory()->create([ + 'provider' => 'discord', + 'data' => [ + 'webhook_url' => 'https://discord.com/xxxxx', + ], + ])); + + $this->assertSame([ + 'webhook_url' => 'https://discord.com/xxxxx', + ], $provider->data()); + } + + public function test_connect(): void + { + $provider = new Discord(NotificationChannel::factory()->create([ + 'provider' => 'discord', + 'data' => [ + 'webhook_url' => 'https://discord.com/xxxxx', + ], + ])); + + Http::fake(); + + $this->assertTrue($provider->connect()); + + Http::assertSent(function ($request) { + return $request->url() === 'https://discord.com/xxxxx'; + }); + } + + public function test_send(): void + { + $channel = NotificationChannel::factory()->create([ + 'provider' => 'discord', + 'data' => [ + 'webhook_url' => 'https://discord.com/xxxxx', + ], + ]); + $provider = new Discord($channel); + + Http::fake(); + + $provider->send($channel, new TestNotification()); + + Http::assertSent(function (Request $request) { + return $request->body() === '{"content":"Hello"}'; + }); + } +} diff --git a/tests/Unit/NotificationChannels/EmailTest.php b/tests/Unit/NotificationChannels/EmailTest.php new file mode 100644 index 0000000..d0c7c53 --- /dev/null +++ b/tests/Unit/NotificationChannels/EmailTest.php @@ -0,0 +1,83 @@ +create([ + 'provider' => 'email', + ])); + + $this->assertSame([ + 'email' => 'required|email', + ], $provider->createRules([])); + } + + public function test_create_data(): void + { + $provider = new Email(NotificationChannel::factory()->create([ + 'provider' => 'email', + ])); + + $this->assertSame([ + 'email' => 'user@example.com', + ], $provider->createData([ + 'email' => 'user@example.com', + ])); + } + + public function test_data(): void + { + $provider = new Email(NotificationChannel::factory()->create([ + 'provider' => 'email', + 'data' => [ + 'email' => 'user@example.com', + ], + ])); + + $this->assertSame([ + 'email' => 'user@example.com', + ], $provider->data()); + } + + public function test_connect(): void + { + $provider = new Email(NotificationChannel::factory()->create([ + 'provider' => 'email', + 'data' => [ + 'email' => 'user@example.com', + ], + ])); + + Mail::fake(); + + $this->assertTrue($provider->connect()); + + Mail::assertSent(NotificationMail::class); + } + + public function test_send(): void + { + $channel = NotificationChannel::factory()->create([ + 'provider' => 'email', + 'data' => [ + 'email' => 'user@example.com', + ], + ]); + $provider = new Email($channel); + + Mail::fake(); + + $provider->send($channel, new TestNotification()); + + Mail::assertSent(NotificationMail::class); + } +} diff --git a/tests/Unit/NotificationChannels/SlackTest.php b/tests/Unit/NotificationChannels/SlackTest.php new file mode 100644 index 0000000..4a89438 --- /dev/null +++ b/tests/Unit/NotificationChannels/SlackTest.php @@ -0,0 +1,87 @@ +create([ + 'provider' => 'slack', + ])); + + $this->assertSame([ + 'webhook_url' => 'required|url', + ], $provider->createRules([])); + } + + public function test_create_data(): void + { + $provider = new Slack(NotificationChannel::factory()->create([ + 'provider' => 'slack', + ])); + + $this->assertSame([ + 'webhook_url' => 'https://slack.com/xxxxx', + ], $provider->createData([ + 'webhook_url' => 'https://slack.com/xxxxx', + ])); + } + + public function test_data(): void + { + $provider = new Slack(NotificationChannel::factory()->create([ + 'provider' => 'slack', + 'data' => [ + 'webhook_url' => 'https://slack.com/xxxxx', + ], + ])); + + $this->assertSame([ + 'webhook_url' => 'https://slack.com/xxxxx', + ], $provider->data()); + } + + public function test_connect(): void + { + $provider = new Slack(NotificationChannel::factory()->create([ + 'provider' => 'slack', + 'data' => [ + 'webhook_url' => 'https://slack.com/xxxxx', + ], + ])); + + Http::fake(); + + $this->assertTrue($provider->connect()); + + Http::assertSent(function ($request) { + return $request->url() === 'https://slack.com/xxxxx'; + }); + } + + public function test_send(): void + { + $channel = NotificationChannel::factory()->create([ + 'provider' => 'slack', + 'data' => [ + 'webhook_url' => 'https://slack.com/xxxxx', + ], + ]); + $provider = new Slack($channel); + + Http::fake(); + + $provider->send($channel, new TestNotification()); + + Http::assertSent(function (Request $request) { + return $request->body() === '{"text":"Hello"}'; + }); + } +} diff --git a/tests/Unit/NotificationChannels/TelegramTest.php b/tests/Unit/NotificationChannels/TelegramTest.php new file mode 100644 index 0000000..7104250 --- /dev/null +++ b/tests/Unit/NotificationChannels/TelegramTest.php @@ -0,0 +1,108 @@ +create([ + 'provider' => 'telegram', + ])); + + $this->assertSame([ + 'bot_token' => 'required|string', + 'chat_id' => 'required', + ], $provider->createRules([])); + } + + public function test_create_data(): void + { + $provider = new Telegram(NotificationChannel::factory()->create([ + 'provider' => 'telegram', + ])); + + $this->assertSame([ + 'bot_token' => 'xxxxx', + 'chat_id' => '12345', + ], $provider->createData([ + 'bot_token' => 'xxxxx', + 'chat_id' => '12345', + ])); + } + + public function test_data(): void + { + $provider = new Telegram(NotificationChannel::factory()->create([ + 'provider' => 'telegram', + 'data' => [ + 'bot_token' => 'xxxxx', + 'chat_id' => '12345', + ], + ])); + + $this->assertSame([ + 'bot_token' => 'xxxxx', + 'chat_id' => '12345', + ], $provider->data()); + } + + public function test_connect(): void + { + $provider = new Telegram(NotificationChannel::factory()->create([ + 'provider' => 'telegram', + 'data' => [ + 'bot_token' => 'xxxxx', + 'chat_id' => '12345', + ], + ])); + + Http::fake(); + + $this->assertTrue($provider->connect()); + + Http::assertSent(function ($request) { + if ($request->url() === 'https://api.telegram.org/botxxxxx/sendMessage') { + return $request->data() === [ + 'chat_id' => '12345', + 'text' => 'Connected!', + 'parse_mode' => 'markdown', + 'disable_web_page_preview' => true, + ]; + } + }); + } + + public function test_send(): void + { + $channel = NotificationChannel::factory()->create([ + 'provider' => 'telegram', + 'data' => [ + 'bot_token' => 'xxxxx', + 'chat_id' => '12345', + ], + ]); + $provider = new Telegram($channel); + + Http::fake(); + + $provider->send($channel, new TestNotification()); + + Http::assertSent(function (Request $request) { + if ($request->url() === 'https://api.telegram.org/botxxxxx/sendMessage') { + return $request->data() === [ + 'chat_id' => '12345', + 'text' => 'Hello', + 'parse_mode' => 'markdown', + 'disable_web_page_preview' => true, + ]; + } + }); + } +} diff --git a/tests/Unit/NotificationChannels/TestNotification.php b/tests/Unit/NotificationChannels/TestNotification.php new file mode 100644 index 0000000..70f1a10 --- /dev/null +++ b/tests/Unit/NotificationChannels/TestNotification.php @@ -0,0 +1,13 @@ +