Merge pull request #11671 from chartjes/ch-fix-tests

Fixed unit test suite that was not running, added working browser test
This commit is contained in:
snipe 2022-09-27 16:20:56 -07:00 committed by GitHub
commit 8d3fe423e1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 84 additions and 22 deletions

65
TESTING.md Normal file
View file

@ -0,0 +1,65 @@
# 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.
### 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`

View file

@ -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: {}
snipeit-backend: {}

View file

@ -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();
}
}

View file

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