Testing cleanup (#11677)

All tests use transactions
One test isn't a db test...
This commit is contained in:
Tony Murray
2020-05-24 13:49:01 -05:00
committed by GitHub
parent 06c991ef57
commit ee13299a77
4 changed files with 22 additions and 32 deletions

View File

@@ -27,10 +27,8 @@ namespace LibreNMS\Tests;
use LibreNMS\Authentication\LegacyAuth; use LibreNMS\Authentication\LegacyAuth;
use LibreNMS\Config; use LibreNMS\Config;
use LibreNMS\Exceptions\AuthenticationException;
// Note that as this test set depends on mres(), it is a DBTestCase even though the database is unused class AuthHTTPTest extends TestCase
class AuthHTTPTest extends DBTestCase
{ {
private $original_auth_mech; private $original_auth_mech;
private $server; private $server;

View File

@@ -25,12 +25,15 @@
namespace LibreNMS\Tests; namespace LibreNMS\Tests;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Str;
use LibreNMS\Authentication\LegacyAuth; use LibreNMS\Authentication\LegacyAuth;
use LibreNMS\Config; use LibreNMS\Config;
class AuthSSOTest extends DBTestCase class AuthSSOTest extends DBTestCase
{ {
private $last_user = null; use DatabaseTransactions;
private $original_auth_mech = null; private $original_auth_mech = null;
private $server; private $server;
@@ -75,7 +78,7 @@ class AuthSSOTest extends DBTestCase
$_SERVER['REMOTE_USER'] = 'test'; $_SERVER['REMOTE_USER'] = 'test';
$_SERVER['mail'] = 'test@example.org'; $_SERVER['mail'] = 'test@example.org';
$_SERVER['displayName'] = bin2hex(openssl_random_pseudo_bytes(16)); $_SERVER['displayName'] = Str::random();
} }
@@ -87,34 +90,18 @@ class AuthSSOTest extends DBTestCase
Config::set('sso.mode', 'header'); Config::set('sso.mode', 'header');
$_SERVER['REMOTE_ADDR'] = '::1'; $_SERVER['REMOTE_ADDR'] = '::1';
$_SERVER['REMOTE_USER'] = bin2hex(openssl_random_pseudo_bytes(16)); $_SERVER['REMOTE_USER'] = Str::random();
$_SERVER['HTTP_MAIL'] = 'test@example.org'; $_SERVER['HTTP_MAIL'] = 'test@example.org';
$_SERVER['HTTP_DISPLAYNAME'] = 'Test User'; $_SERVER['HTTP_DISPLAYNAME'] = 'Test User';
} }
public function makeBreakUser() public function makeUser()
{ {
$this->breakUser(); $user = Str::random();
$_SERVER['REMOTE_USER'] = $user;
$u = bin2hex(openssl_random_pseudo_bytes(16)); return $user;
$this->last_user = $u;
$_SERVER['REMOTE_USER'] = $u;
return $u;
}
public function breakUser()
{
$a = LegacyAuth::reset();
if ($this->last_user !== null) {
$r = $a->deleteUser($a->getUserid($this->last_user));
$this->last_user = null;
return $r;
}
return true;
} }
// Excercise general auth flow // Excercise general auth flow
@@ -128,7 +115,7 @@ class AuthSSOTest extends DBTestCase
// Create a random username and store it with the defaults // Create a random username and store it with the defaults
$this->basicEnvironmentEnv(); $this->basicEnvironmentEnv();
$user = $this->makeBreakUser(); $user = $this->makeUser();
$this->assertTrue($a->authenticate(['username' => $user])); $this->assertTrue($a->authenticate(['username' => $user]));
// Retrieve it and validate // Retrieve it and validate
@@ -147,7 +134,7 @@ class AuthSSOTest extends DBTestCase
// Create a random username and store it with the defaults // Create a random username and store it with the defaults
$this->basicEnvironmentEnv(); $this->basicEnvironmentEnv();
$user = $this->makeBreakUser(); $user = $this->makeUser();
$this->assertTrue($a->authenticate(['username' => $user])); $this->assertTrue($a->authenticate(['username' => $user]));
// Retrieve it and validate // Retrieve it and validate
@@ -177,7 +164,7 @@ class AuthSSOTest extends DBTestCase
// Create a random username and store it with the defaults // Create a random username and store it with the defaults
$this->basicEnvironmentEnv(); $this->basicEnvironmentEnv();
$user = $this->makeBreakUser(); $user = $this->makeUser();
$this->assertTrue($a->authenticate(['username' => $user])); $this->assertTrue($a->authenticate(['username' => $user]));
// Change a few things and reauth // Change a few things and reauth
@@ -222,13 +209,13 @@ class AuthSSOTest extends DBTestCase
unset($_SERVER['displayName']); unset($_SERVER['displayName']);
unset($_SERVER['mail']); unset($_SERVER['mail']);
$this->assertTrue($a->authenticate(['username' => $this->makeBreakUser()])); $this->assertTrue($a->authenticate(['username' => $this->makeUser()]));
$this->basicEnvironmentHeader(); $this->basicEnvironmentHeader();
unset($_SERVER['HTTP_DISPLAYNAME']); unset($_SERVER['HTTP_DISPLAYNAME']);
unset($_SERVER['HTTP_MAIL']); unset($_SERVER['HTTP_MAIL']);
$this->assertTrue($a->authenticate(['username' => $this->makeBreakUser()])); $this->assertTrue($a->authenticate(['username' => $this->makeUser()]));
} }
// Document the modules current behaviour, so that changes trigger test failures // Document the modules current behaviour, so that changes trigger test failures
@@ -472,7 +459,6 @@ class AuthSSOTest extends DBTestCase
{ {
Config::set('auth_mechanism', $this->original_auth_mech); Config::set('auth_mechanism', $this->original_auth_mech);
Config::forget('sso'); Config::forget('sso');
$this->breakUser();
$_SERVER = $this->server; $_SERVER = $this->server;
parent::tearDown(); parent::tearDown();
} }

View File

@@ -28,9 +28,12 @@ namespace LibreNMS\Tests;
use App\Models\ApiToken; use App\Models\ApiToken;
use App\Models\Device; use App\Models\Device;
use App\Models\User; use App\Models\User;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class BasicApiTest extends DBTestCase class BasicApiTest extends DBTestCase
{ {
use DatabaseTransactions;
public function testListDevices() public function testListDevices()
{ {
$user = factory(User::class)->state('admin')->create(); $user = factory(User::class)->state('admin')->create();

View File

@@ -26,6 +26,7 @@
namespace LibreNMS\Tests; namespace LibreNMS\Tests;
use DeviceCache; use DeviceCache;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use LibreNMS\Config; use LibreNMS\Config;
use LibreNMS\Exceptions\FileNotFoundException; use LibreNMS\Exceptions\FileNotFoundException;
use LibreNMS\Exceptions\InvalidModuleException; use LibreNMS\Exceptions\InvalidModuleException;
@@ -34,6 +35,8 @@ use LibreNMS\Util\ModuleTestHelper;
class OSModulesTest extends DBTestCase class OSModulesTest extends DBTestCase
{ {
use DatabaseTransactions;
private $discoveryModules; private $discoveryModules;
private $pollerModules; private $pollerModules;