2018-06-01 04:45:49 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Tests\Browser;
|
|
|
|
|
|
|
|
use Laravel\Dusk\TestCase;
|
2018-06-01 05:30:05 +00:00
|
|
|
use BadMethodCallException;
|
2018-07-15 18:44:08 +00:00
|
|
|
use Pterodactyl\Models\User;
|
2018-07-15 23:20:36 +00:00
|
|
|
use Pterodactyl\Console\Kernel;
|
2018-07-15 18:44:08 +00:00
|
|
|
use Illuminate\Support\Facades\Hash;
|
2018-06-01 05:30:05 +00:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2021-01-23 20:09:16 +00:00
|
|
|
use Pterodactyl\Tests\CreatesApplication;
|
2018-06-01 04:45:49 +00:00
|
|
|
use Facebook\WebDriver\Chrome\ChromeOptions;
|
|
|
|
use Facebook\WebDriver\Remote\RemoteWebDriver;
|
|
|
|
use Facebook\WebDriver\Remote\DesiredCapabilities;
|
|
|
|
|
|
|
|
abstract class BrowserTestCase extends TestCase
|
|
|
|
{
|
2018-07-15 23:20:36 +00:00
|
|
|
use CreatesApplication;
|
2018-06-01 04:45:49 +00:00
|
|
|
|
2018-07-15 18:44:08 +00:00
|
|
|
/**
|
|
|
|
* The default password to use for new accounts.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected static $userPassword = 'Password123';
|
|
|
|
|
2018-07-15 23:20:36 +00:00
|
|
|
/**
|
|
|
|
* Create a fresh database instance before each test class is initialized. This is different
|
|
|
|
* than the default DatabaseMigrations as it is only run when the class is setup. The trait
|
|
|
|
* provided by Laravel will run on EACH test function, slowing things down significantly.
|
|
|
|
*
|
|
|
|
* If you need to reset the DB between function runs just include the trait in that specific
|
|
|
|
* test. In most cases you probably wont need to do this, or can modify the test slightly to
|
|
|
|
* avoid the need to do so.
|
|
|
|
*/
|
|
|
|
public static function setUpBeforeClass()
|
|
|
|
{
|
|
|
|
parent::setUpBeforeClass();
|
|
|
|
|
|
|
|
$app = require __DIR__ . '/../../bootstrap/app.php';
|
|
|
|
|
|
|
|
/** @var \Pterodactyl\Console\Kernel $kernel */
|
|
|
|
$kernel = $app->make(Kernel::class);
|
|
|
|
|
|
|
|
$kernel->bootstrap();
|
|
|
|
$kernel->call('migrate:fresh');
|
|
|
|
}
|
|
|
|
|
2018-06-01 05:30:05 +00:00
|
|
|
/**
|
|
|
|
* Setup tests.
|
|
|
|
*/
|
2020-05-09 16:00:52 +00:00
|
|
|
protected function setUp(): void
|
2018-06-01 05:30:05 +00:00
|
|
|
{
|
|
|
|
// Don't accidentally run the migrations aganist the non-testing database. Ask me
|
|
|
|
// how many times I've accidentally dropped my database...
|
|
|
|
if (env('DB_CONNECTION') !== 'testing') {
|
|
|
|
throw new BadMethodCallException('Cannot call browser tests using the non-testing database connection.');
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
// Gotta unset this to continue avoiding issues with the validation.
|
|
|
|
Model::unsetEventDispatcher();
|
|
|
|
}
|
|
|
|
|
2018-06-01 04:45:49 +00:00
|
|
|
/**
|
|
|
|
* Create the RemoteWebDriver instance.
|
|
|
|
*
|
|
|
|
* @return \Facebook\WebDriver\Remote\RemoteWebDriver
|
|
|
|
*/
|
|
|
|
protected function driver()
|
|
|
|
{
|
|
|
|
$options = (new ChromeOptions)->addArguments([
|
|
|
|
'--disable-gpu',
|
2018-06-01 05:42:52 +00:00
|
|
|
'--disable-infobars',
|
2018-06-01 04:45:49 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
return RemoteWebDriver::create(
|
2018-07-15 04:16:18 +00:00
|
|
|
'http://host.pterodactyl.local:4444/wd/hub', DesiredCapabilities::chrome()->setCapability(
|
2018-06-01 04:45:49 +00:00
|
|
|
ChromeOptions::CAPABILITY, $options
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an instance of the browser to be used for tests.
|
|
|
|
*
|
|
|
|
* @param \Facebook\WebDriver\Remote\RemoteWebDriver $driver
|
|
|
|
* @return \Pterodactyl\Tests\Browser\PterodactylBrowser
|
|
|
|
*/
|
|
|
|
protected function newBrowser($driver): PterodactylBrowser
|
|
|
|
{
|
|
|
|
return new PterodactylBrowser($driver);
|
|
|
|
}
|
2018-07-15 18:44:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tear down the test and delete all cookies from the browser instance to address
|
|
|
|
* instances where the test would be kicked over to the login page.
|
|
|
|
*/
|
2020-05-09 16:00:52 +00:00
|
|
|
protected function tearDown(): void
|
2018-07-15 18:44:08 +00:00
|
|
|
{
|
|
|
|
/** @var \Pterodactyl\Tests\Browser\PterodactylBrowser $browser */
|
|
|
|
foreach (static::$browsers as $browser) {
|
|
|
|
$browser->driver->manage()->deleteAllCookies();
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a user model to authenticate aganist and use in the tests.
|
|
|
|
*
|
|
|
|
* @param array $attributes
|
|
|
|
* @return \Pterodactyl\Models\User
|
|
|
|
*/
|
|
|
|
protected function user(array $attributes = []): User
|
|
|
|
{
|
2021-01-23 20:09:16 +00:00
|
|
|
return User::factory()->create(array_merge([
|
2018-07-15 18:44:08 +00:00
|
|
|
'password' => Hash::make(static::$userPassword),
|
|
|
|
], $attributes));
|
|
|
|
}
|
2018-06-01 04:45:49 +00:00
|
|
|
}
|