From 9b8c764b0d44ccdcbb547d375be2292e1e21b321 Mon Sep 17 00:00:00 2001 From: Henry Whitaker Date: Tue, 25 Aug 2020 21:54:39 +0100 Subject: [PATCH] Added more tests and fixed changelog --- changelog.json | 2 +- .../HealthcheckTest.php | 94 +++++++++++++++++++ .../NotificationTest.php | 35 +++++++ .../SpeedtestController/DeleteTest.php | 66 +++++++++++++ .../SpeedtestController/FailTest.php | 43 +++++++++ .../SpeedtestController/IndexTest.php | 34 +++++++ .../SpeedtestController/LatestTest.php | 58 ++++++++++++ .../SpeedtestController/RunTest.php | 42 +++++++++ .../SpeedtestController/TimeTest.php | 43 +++++++++ .../SpeedtestFailedListener/SlackTest.php | 28 +++++- .../SpeedtestFailedListener/TelegramTest.php | 30 +++++- .../SpeedtestOverviewListener/SlackTest.php | 26 ++++- .../TelegramTest.php | 30 +++++- .../TestNotificationListener/SlackTest.php | 39 ++++++++ .../TestNotificationListener/TelegramTest.php | 41 ++++++++ 15 files changed, 602 insertions(+), 9 deletions(-) create mode 100644 tests/Unit/Controllers/IntegrationsController/HealthcheckTest.php create mode 100644 tests/Unit/Controllers/IntegrationsController/NotificationTest.php create mode 100644 tests/Unit/Controllers/SpeedtestController/DeleteTest.php create mode 100644 tests/Unit/Controllers/SpeedtestController/FailTest.php create mode 100644 tests/Unit/Controllers/SpeedtestController/IndexTest.php create mode 100644 tests/Unit/Controllers/SpeedtestController/LatestTest.php create mode 100644 tests/Unit/Controllers/SpeedtestController/RunTest.php create mode 100644 tests/Unit/Controllers/SpeedtestController/TimeTest.php create mode 100644 tests/Unit/Listeners/TestNotificationListener/SlackTest.php create mode 100644 tests/Unit/Listeners/TestNotificationListener/TelegramTest.php diff --git a/changelog.json b/changelog.json index f22433c4..de141caa 100644 --- a/changelog.json +++ b/changelog.json @@ -3,7 +3,7 @@ { "description": "Changed integration config loading.", "link": "" - }. + }, { "description": "Added more tests.", "link": "" diff --git a/tests/Unit/Controllers/IntegrationsController/HealthcheckTest.php b/tests/Unit/Controllers/IntegrationsController/HealthcheckTest.php new file mode 100644 index 00000000..13e055a7 --- /dev/null +++ b/tests/Unit/Controllers/IntegrationsController/HealthcheckTest.php @@ -0,0 +1,94 @@ +controller = new IntegrationsController(); + + $this->uuid = env('HEALTHCHECKS_UUID'); + $this->bindFacade($this->uuid); + } + + public function testStartPing() + { + $resp = $this->controller->testHealthchecks('start')->original; + + $this->assertEquals([ + 'method' => 'test healthchecks \'start\' endpoint', + 'success' => true + ], $resp); + } + + public function testSuccessPing() + { + $resp = $this->controller->testHealthchecks('success')->original; + + $this->assertEquals([ + 'method' => 'test healthchecks \'success\' endpoint', + 'success' => true + ], $resp); + } + + public function testFailPing() + { + $resp = $this->controller->testHealthchecks('fail')->original; + + $this->assertEquals([ + 'method' => 'test healthchecks \'fail\' endpoint', + 'success' => true + ], $resp); + } + + public function testInvalidUUID() + { + $this->bindFacade('test'); + + $resp = $this->controller->testHealthchecks('start')->original; + + $this->assertEquals([ + 'method' => 'test healthchecks \'start\' endpoint', + 'success' => false, + 'error' => 'Invalid UUID' + ], $resp); + + $this->bindFacade($this->uuid); + } + + /** + * As clean install before setting up, there is no healthchecks + * uuid in the db, so the facade doesn't get created during boot, + * now just bind it in the container on test setup + * + * @param String $uuid + * @return void + */ + private function bindFacade(String $uuid) + { + App::bind('healthcheck', function() use ($uuid) { + return new Healthchecks($uuid); + }); + } +} diff --git a/tests/Unit/Controllers/IntegrationsController/NotificationTest.php b/tests/Unit/Controllers/IntegrationsController/NotificationTest.php new file mode 100644 index 00000000..f8efa1bc --- /dev/null +++ b/tests/Unit/Controllers/IntegrationsController/NotificationTest.php @@ -0,0 +1,35 @@ +controller = new IntegrationsController(); + } + + public function testNotificationsTest() + { + $resp = $this->controller->testNotification()->original; + + $this->assertEquals([ + 'method' => 'test notification agents' + ], $resp); + } +} diff --git a/tests/Unit/Controllers/SpeedtestController/DeleteTest.php b/tests/Unit/Controllers/SpeedtestController/DeleteTest.php new file mode 100644 index 00000000..00e1ed95 --- /dev/null +++ b/tests/Unit/Controllers/SpeedtestController/DeleteTest.php @@ -0,0 +1,66 @@ +controller = new SpeedtestController(); + } + + public function testDeleteAll() + { + for($i = 0; $i < 5; $i++) { + Speedtest::create([ + 'download' => 5, + 'upload' => 5, + 'ping' => 5 + ]); + } + + $this->assertEquals(5, Speedtest::count()); + + $resp = $this->controller->deleteAll()->original; + + $this->assertArrayHasKey('method', $resp); + $this->assertArrayHasKey('success', $resp); + + $this->assertEquals(0, Speedtest::count()); + } + + public function testDeleteSpecific() + { + $test = Speedtest::create([ + 'download' => 5, + 'upload' => 5, + 'ping' => 5 + ]); + $id = $test->id; + + $this->assertNotNull(Speedtest::find($id)); + + $resp = $this->controller->delete($test)->original; + + $this->assertArrayHasKey('method', $resp); + $this->assertArrayHasKey('success', $resp); + + $this->assertNull(Speedtest::find($id)); + } +} diff --git a/tests/Unit/Controllers/SpeedtestController/FailTest.php b/tests/Unit/Controllers/SpeedtestController/FailTest.php new file mode 100644 index 00000000..b8fd70c5 --- /dev/null +++ b/tests/Unit/Controllers/SpeedtestController/FailTest.php @@ -0,0 +1,43 @@ +controller = new SpeedtestController(); + } + + public function testFail() + { + $resp = $this->controller->fail(5)->original; + + $this->assertArrayHasKey('method', $resp); + $this->assertArrayHasKey('data', $resp); + $this->assertArrayHasKey('days', $resp); + } + + public function testFailInvalidInput() + { + $resp = $this->controller->fail('test')->original; + + $this->assertArrayHasKey('method', $resp); + $this->assertArrayHasKey('error', $resp); + } +} diff --git a/tests/Unit/Controllers/SpeedtestController/IndexTest.php b/tests/Unit/Controllers/SpeedtestController/IndexTest.php new file mode 100644 index 00000000..02a34f1d --- /dev/null +++ b/tests/Unit/Controllers/SpeedtestController/IndexTest.php @@ -0,0 +1,34 @@ +controller = new SpeedtestController(); + } + + public function testIndex() + { + $resp = $this->controller->index()->original; + + $this->assertArrayHasKey('method', $resp); + $this->assertArrayHasKey('data', $resp); + } +} diff --git a/tests/Unit/Controllers/SpeedtestController/LatestTest.php b/tests/Unit/Controllers/SpeedtestController/LatestTest.php new file mode 100644 index 00000000..0b6189c3 --- /dev/null +++ b/tests/Unit/Controllers/SpeedtestController/LatestTest.php @@ -0,0 +1,58 @@ +controller = new SpeedtestController(); + } + + public function testLatestNoEntries() + { + DB::table('speedtests')->delete(); + + $resp = $this->controller->latest(); + $resp = $resp->original; + + $this->assertEquals([ + 'method' => 'get latest speedtest', + 'error' => 'no speedtests have been run' + ], $resp); + } + + public function testLatest() + { + $test = Speedtest::create([ + 'download' => 5, + 'upload' => 5, + 'ping' => 5 + ]); + $test = $test->attributesToArray(); + + $resp = $this->controller->latest(); + $resp = $resp->original; + + $this->assertArrayHasKey('data', $resp); + $this->assertArrayHasKey('average', $resp); + $this->assertArrayHasKey('max', $resp); + } +} diff --git a/tests/Unit/Controllers/SpeedtestController/RunTest.php b/tests/Unit/Controllers/SpeedtestController/RunTest.php new file mode 100644 index 00000000..aa156184 --- /dev/null +++ b/tests/Unit/Controllers/SpeedtestController/RunTest.php @@ -0,0 +1,42 @@ +controller = new SpeedtestController(); + } + + public function testRun() + { + Queue::fake(); + + $resp = $this->controller->run()->original; + + $this->assertArrayHasKey('method', $resp); + $this->assertArrayHasKey('data', $resp); + + Queue::assertPushed(SpeedtestJob::class, function($job) { + return true; + }); + } +} diff --git a/tests/Unit/Controllers/SpeedtestController/TimeTest.php b/tests/Unit/Controllers/SpeedtestController/TimeTest.php new file mode 100644 index 00000000..04e58e69 --- /dev/null +++ b/tests/Unit/Controllers/SpeedtestController/TimeTest.php @@ -0,0 +1,43 @@ +controller = new SpeedtestController(); + } + + public function testTime() + { + $resp = $this->controller->time(5)->original; + + $this->assertArrayHasKey('method', $resp); + $this->assertArrayHasKey('data', $resp); + $this->assertArrayHasKey('days', $resp); + } + + public function testTimeInvalidInput() + { + $resp = $this->controller->time('test')->original; + + $this->assertArrayHasKey('method', $resp); + $this->assertArrayHasKey('error', $resp); + } +} diff --git a/tests/Unit/Listeners/SpeedtestFailedListener/SlackTest.php b/tests/Unit/Listeners/SpeedtestFailedListener/SlackTest.php index 48bdaa05..fea50dc1 100644 --- a/tests/Unit/Listeners/SpeedtestFailedListener/SlackTest.php +++ b/tests/Unit/Listeners/SpeedtestFailedListener/SlackTest.php @@ -2,17 +2,41 @@ namespace Tests\Unit\Listeners\SpeedtestFailedListener; -use PHPUnit\Framework\TestCase; +use App\Helpers\SettingsHelper; +use App\Listeners\SpeedtestFailedListener; +use App\Speedtest; +use Exception; +use Illuminate\Foundation\Testing\RefreshDatabase; +use stdClass; +use Tests\TestCase; class SlackTest extends TestCase { + use RefreshDatabase; + /** * A basic unit test example. * * @return void */ - public function testExample() + public function testSlackFailedNotification() { + SettingsHelper::set('speedtest_notifications', true); + SettingsHelper::set('slack_webhook', env('SLACK_WEBHOOK')); + + $l = new SpeedtestFailedListener(); + $test = Speedtest::create([ 'download' => 0, 'upload' => 0, 'ping' => 0, 'failed' => true ]); + + $event = new stdClass(); + $event->speedtest = $test; + + try { + $l->handle($event); + } catch(Exception $e) { + $this->assertTrue(false); + return false; + } + $this->assertTrue(true); } } diff --git a/tests/Unit/Listeners/SpeedtestFailedListener/TelegramTest.php b/tests/Unit/Listeners/SpeedtestFailedListener/TelegramTest.php index c7476869..2e273738 100644 --- a/tests/Unit/Listeners/SpeedtestFailedListener/TelegramTest.php +++ b/tests/Unit/Listeners/SpeedtestFailedListener/TelegramTest.php @@ -2,17 +2,43 @@ namespace Tests\Unit\Listeners\SpeedtestFailedListener; -use PHPUnit\Framework\TestCase; +use App\Helpers\SettingsHelper; +use App\Listeners\SpeedtestFailedListener; +use App\Speedtest; +use Exception; +use Illuminate\Foundation\Testing\RefreshDatabase; +use stdClass; +use Tests\TestCase; class TelegramTest extends TestCase { + use RefreshDatabase; + /** * A basic unit test example. * * @return void */ - public function testExample() + public function testTelegramFailedNotification() { + SettingsHelper::set('speedtest_notifications', true); + SettingsHelper::set('telegram_bot_token', env('TELEGRAM_BOT_TOKEN')); + SettingsHelper::set('telegram_chat_id', env('TELEGRAM_CHAT_ID')); + SettingsHelper::set('slack_webhook', false); + + $l = new SpeedtestFailedListener(); + $test = Speedtest::create([ 'download' => 5, 'upload' => 5, 'ping' => 5, 'failed' => true ]); + + $event = new stdClass(); + $event->speedtest = $test; + + try { + $l->handle($event); + } catch(Exception $e) { + $this->assertTrue(false); + return false; + } + $this->assertTrue(true); } } diff --git a/tests/Unit/Listeners/SpeedtestOverviewListener/SlackTest.php b/tests/Unit/Listeners/SpeedtestOverviewListener/SlackTest.php index 9d90032f..8e471aa0 100644 --- a/tests/Unit/Listeners/SpeedtestOverviewListener/SlackTest.php +++ b/tests/Unit/Listeners/SpeedtestOverviewListener/SlackTest.php @@ -2,17 +2,39 @@ namespace Tests\Unit\Listeners\SpeedtestOverviewListener; -use PHPUnit\Framework\TestCase; +use App\Helpers\SettingsHelper; +use App\Listeners\SpeedtestOverviewListener; +use App\Speedtest; +use Exception; +use Illuminate\Foundation\Testing\RefreshDatabase; +use stdClass; +use Tests\TestCase; class SlackTest extends TestCase { + use RefreshDatabase; + /** * A basic unit test example. * * @return void */ - public function testExample() + public function testSlackOverviewNotification() { + SettingsHelper::set('speedtest_overview_notification', true); + SettingsHelper::set('slack_webhook', env('SLACK_WEBHOOK')); + + $l = new SpeedtestOverviewListener(); + + $event = new stdClass(); + + try { + $l->handle($event); + } catch(Exception $e) { + $this->assertTrue(false); + return false; + } + $this->assertTrue(true); } } diff --git a/tests/Unit/Listeners/SpeedtestOverviewListener/TelegramTest.php b/tests/Unit/Listeners/SpeedtestOverviewListener/TelegramTest.php index 2ca16b0f..fb841e03 100644 --- a/tests/Unit/Listeners/SpeedtestOverviewListener/TelegramTest.php +++ b/tests/Unit/Listeners/SpeedtestOverviewListener/TelegramTest.php @@ -2,17 +2,43 @@ namespace Tests\Unit\Listeners\SpeedtestOverviewListener; -use PHPUnit\Framework\TestCase; +use App\Helpers\SettingsHelper; +use App\Listeners\SpeedtestOverviewListener; +use App\Speedtest; +use Exception; +use Illuminate\Foundation\Testing\RefreshDatabase; +use stdClass; +use Tests\TestCase; class TelegramTest extends TestCase { + use RefreshDatabase; + /** * A basic unit test example. * * @return void */ - public function testExample() + public function testTelegramOverviewNotification() { + SettingsHelper::set('speedtest_overview_notification', true); + SettingsHelper::set('telegram_bot_token', env('TELEGRAM_BOT_TOKEN')); + SettingsHelper::set('telegram_chat_id', env('TELEGRAM_CHAT_ID')); + SettingsHelper::set('slack_webhook', false); + + $l = new SpeedtestOverviewListener(); + $test = Speedtest::create([ 'download' => 5, 'upload' => 5, 'ping' => 5, 'failed' => true ]); + + $event = new stdClass(); + $event->speedtest = $test; + + try { + $l->handle($event); + } catch(Exception $e) { + $this->assertTrue(false); + return false; + } + $this->assertTrue(true); } } diff --git a/tests/Unit/Listeners/TestNotificationListener/SlackTest.php b/tests/Unit/Listeners/TestNotificationListener/SlackTest.php new file mode 100644 index 00000000..08527b1c --- /dev/null +++ b/tests/Unit/Listeners/TestNotificationListener/SlackTest.php @@ -0,0 +1,39 @@ +agents = [ 'slack' ]; + + try { + $l->handle($event); + } catch(Exception $e) { + $this->assertTrue(false); + return false; + } + + $this->assertTrue(true); + } +} diff --git a/tests/Unit/Listeners/TestNotificationListener/TelegramTest.php b/tests/Unit/Listeners/TestNotificationListener/TelegramTest.php new file mode 100644 index 00000000..27b94c60 --- /dev/null +++ b/tests/Unit/Listeners/TestNotificationListener/TelegramTest.php @@ -0,0 +1,41 @@ +agents = [ 'telegram' ]; + + try { + $l->handle($event); + } catch(Exception $e) { + $this->assertTrue(false); + return false; + } + + $this->assertTrue(true); + } +}