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 @@ +