Dusk: improve speed and safety (#13370)

* Dusk: improve speed
Instead of refreshing the database between tests, just cleanup all side effects.
Update UserFactory to upstream so hash never has to be generated for default

* Actually, migrate, but not fresh
This commit is contained in:
Tony Murray
2021-10-18 03:01:07 -05:00
committed by GitHub
parent 4685ea13cc
commit 66d0a34f40
2 changed files with 10 additions and 9 deletions

View File

@@ -22,14 +22,12 @@ class UserFactory extends Factory
*/
public function definition()
{
static $password;
return [
'auth_type' => 'mysql',
'username' => $this->faker->unique()->userName,
'realname' => $this->faker->name,
'email' => $this->faker->safeEmail,
'password' => $password ?: $password = bcrypt('secret'),
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
'level' => 1,
];
}

View File

@@ -4,7 +4,7 @@ namespace LibreNMS\Tests\Browser;
use App\Models\User;
use App\Models\UserPref;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Hash;
use Laravel\Dusk\Browser;
use LibreNMS\Config;
use LibreNMS\Tests\Browser\Pages\LoginPage;
@@ -18,7 +18,11 @@ use LibreNMS\Tests\DuskTestCase;
*/
class LoginTest extends DuskTestCase
{
use DatabaseMigrations;
protected function setUp(): void
{
parent::setUp();
$this->artisan('migrate');
}
/**
* @throws \Throwable
@@ -28,8 +32,7 @@ class LoginTest extends DuskTestCase
$this->browse(function (Browser $browser) {
$password = 'some_password';
$user = User::factory()->create([
/** @phpstan-ignore-next-line */
'password' => password_hash($password, PASSWORD_DEFAULT),
'password' => Hash::make($password),
]); /** @var User $user */
$browser->visit(new LoginPage())
->type('username', $user->username)
@@ -54,8 +57,7 @@ class LoginTest extends DuskTestCase
$this->browse(function (Browser $browser) {
$password = 'another_password';
$user = User::factory()->create([
/** @phpstan-ignore-next-line */
'password' => password_hash($password, PASSWORD_DEFAULT),
'password' => Hash::make($password),
]); /** @var User $user */
Config::persist('twofactor', true); // set to db
UserPref::setPref($user, 'twofactor', [
@@ -78,6 +80,7 @@ class LoginTest extends DuskTestCase
->logout();
$user->delete();
\App\Models\Config::where('config_name', 'twofactor')->delete();
});
}
}