From 07585809b329129597e3c48fe58f8bb6448900fb Mon Sep 17 00:00:00 2001 From: Chris Hartjes Date: Fri, 12 Aug 2022 13:00:26 -0400 Subject: [PATCH 1/2] Got unit and browser tests working, added documentation --- TESTING.md | 56 +++++++++++++++++++++++++++++++++++++ docker-compose.yml | 4 ++- tests/Browser/LoginTest.php | 35 ++++++++++------------- tests/DuskTestCase.php | 2 +- 4 files changed, 75 insertions(+), 22 deletions(-) create mode 100644 TESTING.md diff --git a/TESTING.md b/TESTING.md new file mode 100644 index 000000000..e18f6bca7 --- /dev/null +++ b/TESTING.md @@ -0,0 +1,56 @@ +# Using the Test Suite + +This document is targeted at developers looking to make modifications to +this application's code base and want to run the existing test suite. + + +## Setup + +Follow the instructions for installing the application locally, +making sure to have also run the [database migrations](link to db migrations). + + +## Unit Tests + +The application will use values in the `.env.testing` file located +in the root directory to override the +default settings and/or other values that exist in your `.env` files. + +Make sure to modify the section in `.env.testing` that has the +database settings. In the example below, it is connecting to the +[MariaDB](link-to-maria-db) server that is used if you install the +application using [Docker](https://docker.com). + +```dotenv +# -------------------------------------------- +# REQUIRED: DATABASE SETTINGS +# -------------------------------------------- +DB_CONNECTION=mysql +DB_HOST=127.0.0.1 +DB_DATABASE=snipeit +DB_USERNAME=root +DB_PASSWORD=changeme1234 +``` + +To run the entire unit test suite, use the following command from your terminal: + +`php artisan test --env=testing` + +To run individual test files, you can pass the path to the test that +you want to run. + +`php artisan test --env=testing tests/Unit/AccessoryTest.php` + +## Browser Tests + +The browser tests use [Dusk](https://laravel.com/docs/8.x/dusk) to run them. +When troubleshooting any problems, make sure that your `.env` file is configured +correctly to run the existing application. + +To run the test suite use the following command from your terminal: + +`php artisan dusk` + +To run individual test files, you can pass the path to the test that you want to run. + +`php artisan dusk tests/Browser/LoginTest.php` \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 4007a9499..101c15d3b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -26,6 +26,8 @@ services: - .env.docker networks: - snipeit-backend + ports: + - "3306:3306" redis: image: redis:6.2.5-buster @@ -45,4 +47,4 @@ volumes: db: {} networks: - snipeit-backend: {} \ No newline at end of file + snipeit-backend: {} diff --git a/tests/Browser/LoginTest.php b/tests/Browser/LoginTest.php index 0d54dec8f..5ed055cda 100644 --- a/tests/Browser/LoginTest.php +++ b/tests/Browser/LoginTest.php @@ -2,6 +2,7 @@ namespace Tests\Browser; +use App\Models\User; use Illuminate\Foundation\Testing\DatabaseMigrations; use Laravel\Dusk\Browser; use Tests\DuskTestCase; @@ -15,37 +16,31 @@ class LoginTest extends DuskTestCase */ public function testLoginPageLoadsAndUserCanLogin() { + // Create a new user + $user = User::factory()->make(); + + // We override the existing password to use a hash of one we know + $user->password = '$2y$10$8o5W8fgAKJbN3Kz4taepeeRVgKsG8pkZ1L4eJfdEKrn2mgI/JgCJy'; + + // We want a user that is a superuser + $user->permissions = '{"superuser": 1}'; + + $user->save(); $this->browse(function (Browser $browser) { $browser->visitRoute('login') ->assertSee(trans('auth/general.login_prompt')); }); - $this->browse(function ($browser) { + $this->browse(function ($browser) use ($user) { $browser->visitRoute('login') - ->type('username', 'snipe') + ->type('username', $user->username) ->type('password', 'password') ->press(trans('auth/general.login')) ->assertPathIs('/'); $browser->screenshot('dashboard'); }); - } - - /** - * Test dashboard loads - * - * @todo Flesh this out further to make sure the individual tables actually load with - * content inside them. - * - * @return void - */ - public function testDashboardLoadsWithSuperAdmin() - { - $this->browse(function ($browser) { - $browser->assertSee(trans('general.dashboard')); - $browser->assertSee(trans('general.loading')); - $browser->screenshot('dashboard-2'); - }); - + // Delete the user afterwards + $user->delete(); } } diff --git a/tests/DuskTestCase.php b/tests/DuskTestCase.php index f3427e441..ac750d9ef 100644 --- a/tests/DuskTestCase.php +++ b/tests/DuskTestCase.php @@ -41,7 +41,7 @@ abstract class DuskTestCase extends BaseTestCase })->all()); return RemoteWebDriver::create( - $_ENV['DUSK_DRIVER_URL'] ?? 'http://127.0.0.1:8000', + $_ENV['DUSK_DRIVER_URL'] ?? 'http://127.0.0.1:9515', DesiredCapabilities::chrome()->setCapability( ChromeOptions::CAPABILITY, $options ) From 74fe3dc733dde8c10c9e36b148b2681c95b97d07 Mon Sep 17 00:00:00 2001 From: Chris Hartjes Date: Fri, 26 Aug 2022 13:55:12 -0400 Subject: [PATCH 2/2] Updated testing-related documentation --- TESTING.md | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/TESTING.md b/TESTING.md index e18f6bca7..662428975 100644 --- a/TESTING.md +++ b/TESTING.md @@ -47,10 +47,19 @@ The browser tests use [Dusk](https://laravel.com/docs/8.x/dusk) to run them. When troubleshooting any problems, make sure that your `.env` file is configured correctly to run the existing application. -To run the test suite use the following command from your terminal: +### Test Setup + +Your application needs to be configued and up and running in order for the browser +tests to actually run. When running the tests locally, you can start the application +using the following command: + +`php artisan serve` + + +To run the test suite use the following command from another terminal tab or window: `php artisan dusk` To run individual test files, you can pass the path to the test that you want to run. -`php artisan dusk tests/Browser/LoginTest.php` \ No newline at end of file +`php artisan dusk tests/Browser/LoginTest.php`