Skip to content

Commit

Permalink
Merge pull request #11671 from chartjes/ch-fix-tests
Browse files Browse the repository at this point in the history
Fixed unit test suite that was not running, added working browser test
  • Loading branch information
snipe authored Sep 27, 2022
2 parents 1377725 + 74fe3dc commit 8d3fe42
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 22 deletions.
65 changes: 65 additions & 0 deletions TESTING.md
Original file line number Diff line number Diff line change
@@ -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`
4 changes: 3 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ services:
- .env.docker
networks:
- snipeit-backend
ports:
- "3306:3306"

redis:
image: redis:6.2.5-buster
Expand All @@ -45,4 +47,4 @@ volumes:
db: {}

networks:
snipeit-backend: {}
snipeit-backend: {}
35 changes: 15 additions & 20 deletions tests/Browser/LoginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Tests\Browser;

use App\Models\User;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Laravel\Dusk\Browser;
use Tests\DuskTestCase;
Expand All @@ -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();
}
}
2 changes: 1 addition & 1 deletion tests/DuskTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ protected function driver()
})->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
)
Expand Down

0 comments on commit 8d3fe42

Please sign in to comment.