Added some tests for listeners and updated workflows with secrets

This commit is contained in:
Henry Whitaker
2020-08-25 19:01:48 +01:00
parent 94e4919b44
commit 0eff685277
9 changed files with 321 additions and 0 deletions

View File

@@ -13,6 +13,11 @@ jobs:
- uses: actions/checkout@v2
- name: Copy .env
run: php -r "file_exists('.env') || copy('.env.example', '.env');"
- name: Update .env with secrets
run: |
echo SLACK_WEBHOOK=${{ secrets.SLACK_WEBHOOK }} >> .env
echo TELEGRAM_BOT_TOKEN=${{ secrets.TELEGRAM_BOT_TOKEN }} >> .env
echo TELEGRAM_CHAT_ID=${{ secrets.TELEGRAM_CHAT_ID }} >> .env
- name: Install Dependencies
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
- name: Create Database

View File

@@ -13,6 +13,11 @@ jobs:
- uses: actions/checkout@v2
- name: Copy .env
run: php -r "file_exists('.env') || copy('.env.example', '.env');"
- name: Update .env with secrets
run: |
echo SLACK_WEBHOOK=${{ secrets.SLACK_WEBHOOK }} >> .env
echo TELEGRAM_BOT_TOKEN=${{ secrets.TELEGRAM_BOT_TOKEN }} >> .env
echo TELEGRAM_CHAT_ID=${{ secrets.TELEGRAM_CHAT_ID }} >> .env
- name: Install Dependencies
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
- name: Create Database

View File

@@ -13,6 +13,11 @@ jobs:
- uses: actions/checkout@v2
- name: Copy .env
run: php -r "file_exists('.env') || copy('.env.example', '.env');"
- name: Update .env with secrets
run: |
echo SLACK_WEBHOOK=${{ secrets.SLACK_WEBHOOK }} >> .env
echo TELEGRAM_BOT_TOKEN=${{ secrets.TELEGRAM_BOT_TOKEN }} >> .env
echo TELEGRAM_CHAT_ID=${{ secrets.TELEGRAM_CHAT_ID }} >> .env
- name: Install Dependencies
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
- name: Create Database

View File

@@ -0,0 +1,127 @@
<?php
namespace Tests\Unit\Listeners\SpeedtestCompleteListener;
use App\Helpers\SettingsHelper;
use App\Listeners\SpeedtestCompleteListener;
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 testSlackSpeedtestNotification()
{
SettingsHelper::set('speedtest_notifications', true);
SettingsHelper::set('slack_webhook', env('SLACK_WEBHOOK'));
$l = new SpeedtestCompleteListener();
$test = Speedtest::create([ 'download' => 5, 'upload' => 5, 'ping' => 5 ]);
$event = new stdClass();
$event->speedtest = $test;
try {
$l->handle($event);
} catch(Exception $e) {
$this->assertTrue(false);
return false;
}
$this->assertTrue(true);
}
/**
* A basic unit test example.
*
* @return void
*/
public function testInvalidSlackWebhook()
{
SettingsHelper::set('speedtest_notifications', true);
SettingsHelper::set('slack_webhook', 'invalid');
$l = new SpeedtestCompleteListener();
$test = Speedtest::create([ 'download' => 5, 'upload' => 5, 'ping' => 5 ]);
$event = new stdClass();
$event->speedtest = $test;
try {
$l->handle($event);
} catch(Exception $e) {
$this->assertTrue(false);
return false;
}
$this->assertTrue(true);
}
/**
* A basic unit test example.
*
* @return void
*/
public function testSlackPercentageThresholdNotification()
{
SettingsHelper::set('speedtest_notifications', false);
SettingsHelper::set('slack_webhook', env('SLACK_WEBHOOK'));
SettingsHelper::set('threshold_alert_percentage_notifications', 1);
Speedtest::create([ 'download' => 50, 'upload' => 50, 'ping' => 1 ]);
$l = new SpeedtestCompleteListener();
$test = Speedtest::create([ 'download' => 5, 'upload' => 5, 'ping' => 5 ]);
$event = new stdClass();
$event->speedtest = $test;
try {
$l->handle($event);
} catch(Exception $e) {
$this->assertTrue(false);
return false;
}
$this->assertTrue(true);
}
/**
* A basic unit test example.
*
* @return void
*/
public function testSlackAbsoluteThresholdNotification()
{
SettingsHelper::set('speedtest_notifications', false);
SettingsHelper::set('slack_webhook', env('SLACK_WEBHOOK'));
SettingsHelper::set('threshold_alert_absolute_notifications', 1);
SettingsHelper::set('threshold_alert_absolute_download', 50);
SettingsHelper::set('threshold_alert_absolute_upload', 50);
SettingsHelper::set('threshold_alert_absolute_ping', 1);
$l = new SpeedtestCompleteListener();
$test = Speedtest::create([ 'download' => 5, 'upload' => 5, 'ping' => 5 ]);
$event = new stdClass();
$event->speedtest = $test;
try {
$l->handle($event);
} catch(Exception $e) {
$this->assertTrue(false);
return false;
}
$this->assertTrue(true);
}
}

View File

@@ -0,0 +1,107 @@
<?php
namespace Tests\Unit\Listeners\SpeedtestCompleteListener;
use App\Helpers\SettingsHelper;
use App\Listeners\SpeedtestCompleteListener;
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 testTelegramSpeedtestNotification()
{
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 SpeedtestCompleteListener();
$test = Speedtest::create([ 'download' => 5, 'upload' => 5, 'ping' => 5 ]);
$event = new stdClass();
$event->speedtest = $test;
try {
$l->handle($event);
} catch(Exception $e) {
$this->assertTrue(false);
return false;
}
$this->assertTrue(true);
}
/**
* A basic unit test example.
*
* @return void
*/
public function testTelegramPercentageThresholdNotification()
{
SettingsHelper::set('speedtest_notifications', false);
SettingsHelper::set('telegram_bot_token', env('TELEGRAM_BOT_TOKEN'));
SettingsHelper::set('telegram_chat_id', env('TELEGRAM_CHAT_ID'));
SettingsHelper::set('slack_webhook', false);
SettingsHelper::set('threshold_alert_percentage_notifications', 1);
Speedtest::create([ 'download' => 50, 'upload' => 50, 'ping' => 1 ]);
$l = new SpeedtestCompleteListener();
$test = Speedtest::create([ 'download' => 5, 'upload' => 5, 'ping' => 5 ]);
$event = new stdClass();
$event->speedtest = $test;
try {
$l->handle($event);
} catch(Exception $e) {
$this->assertTrue(false);
return false;
}
$this->assertTrue(true);
}
/**
* A basic unit test example.
*
* @return void
*/
public function testTelegramAbsoluteThresholdNotification()
{
SettingsHelper::set('speedtest_notifications', false);
SettingsHelper::set('telegram_bot_token', env('TELEGRAM_BOT_TOKEN'));
SettingsHelper::set('telegram_chat_id', env('TELEGRAM_CHAT_ID'));
SettingsHelper::set('slack_webhook', false);
SettingsHelper::set('threshold_alert_absolute_notifications', 1);
SettingsHelper::set('threshold_alert_absolute_download', 50);
SettingsHelper::set('threshold_alert_absolute_upload', 50);
SettingsHelper::set('threshold_alert_absolute_ping', 1);
$l = new SpeedtestCompleteListener();
$test = Speedtest::create([ 'download' => 5, 'upload' => 5, 'ping' => 5 ]);
$event = new stdClass();
$event->speedtest = $test;
try {
$l->handle($event);
} catch(Exception $e) {
$this->assertTrue(false);
return false;
}
$this->assertTrue(true);
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Tests\Unit\Listeners\SpeedtestFailedListener;
use PHPUnit\Framework\TestCase;
class SlackTest extends TestCase
{
/**
* A basic unit test example.
*
* @return void
*/
public function testExample()
{
$this->assertTrue(true);
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Tests\Unit\Listeners\SpeedtestFailedListener;
use PHPUnit\Framework\TestCase;
class TelegramTest extends TestCase
{
/**
* A basic unit test example.
*
* @return void
*/
public function testExample()
{
$this->assertTrue(true);
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Tests\Unit\Listeners\SpeedtestOverviewListener;
use PHPUnit\Framework\TestCase;
class SlackTest extends TestCase
{
/**
* A basic unit test example.
*
* @return void
*/
public function testExample()
{
$this->assertTrue(true);
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Tests\Unit\Listeners\SpeedtestOverviewListener;
use PHPUnit\Framework\TestCase;
class TelegramTest extends TestCase
{
/**
* A basic unit test example.
*
* @return void
*/
public function testExample()
{
$this->assertTrue(true);
}
}