2010-04-12 23:16:26 +02:00
|
|
|
<?php
|
2016-06-23 01:37:22 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Security\Tests;
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2017-06-15 04:20:12 +02:00
|
|
|
use SilverStripe\Security\Member;
|
|
|
|
use SilverStripe\Security\Permission;
|
|
|
|
use SilverStripe\Security\DefaultAdminService;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class SecurityDefaultAdminTest extends SapphireTest
|
|
|
|
{
|
|
|
|
protected $usesDatabase = true;
|
|
|
|
|
|
|
|
protected $defaultUsername = null;
|
2017-06-15 04:20:12 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
protected $defaultPassword = null;
|
|
|
|
|
2017-03-24 04:00:54 +01:00
|
|
|
protected function setUp()
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
// TODO Workaround to force database clearing with no fixture present,
|
|
|
|
// and avoid sideeffects from other tests
|
2017-06-22 12:50:45 +02:00
|
|
|
if (!static::$tempDB->isUsed()) {
|
|
|
|
static::$tempDB->build();
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
2017-06-22 12:50:45 +02:00
|
|
|
static::$tempDB->clearAllData();
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-06-15 04:20:12 +02:00
|
|
|
if (DefaultAdminService::hasDefaultAdmin()) {
|
|
|
|
$this->defaultUsername = DefaultAdminService::getDefaultAdminUsername();
|
|
|
|
$this->defaultPassword = DefaultAdminService::getDefaultAdminPassword();
|
|
|
|
DefaultAdminService::clearDefaultAdmin();
|
|
|
|
} else {
|
|
|
|
$this->defaultUsername = null;
|
|
|
|
$this->defaultPassword = null;
|
|
|
|
}
|
|
|
|
DefaultAdminService::setDefaultAdmin('admin', 'password');
|
2017-01-20 03:57:50 +01:00
|
|
|
Permission::reset();
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
2017-03-24 04:00:54 +01:00
|
|
|
protected function tearDown()
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
2017-06-15 04:20:12 +02:00
|
|
|
DefaultAdminService::clearDefaultAdmin();
|
|
|
|
if ($this->defaultUsername) {
|
|
|
|
DefaultAdminService::setDefaultAdmin($this->defaultUsername, $this->defaultPassword);
|
|
|
|
}
|
2017-01-20 03:57:50 +01:00
|
|
|
Permission::reset();
|
2016-12-16 05:34:21 +01:00
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCheckDefaultAdmin()
|
|
|
|
{
|
2017-06-15 04:20:12 +02:00
|
|
|
$this->assertTrue(DefaultAdminService::hasDefaultAdmin());
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertTrue(
|
2017-06-15 04:20:12 +02:00
|
|
|
DefaultAdminService::isDefaultAdminCredentials('admin', 'password'),
|
2016-12-16 05:34:21 +01:00
|
|
|
'Succeeds with correct username and password'
|
|
|
|
);
|
|
|
|
$this->assertFalse(
|
2017-06-15 04:20:12 +02:00
|
|
|
DefaultAdminService::isDefaultAdminCredentials('wronguser', 'password'),
|
2016-12-16 05:34:21 +01:00
|
|
|
'Fails with incorrect username'
|
|
|
|
);
|
|
|
|
$this->assertFalse(
|
2017-06-15 04:20:12 +02:00
|
|
|
DefaultAdminService::isDefaultAdminCredentials('admin', 'wrongpassword'),
|
2016-12-16 05:34:21 +01:00
|
|
|
'Fails with incorrect password'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFindAnAdministratorCreatesNewUser()
|
|
|
|
{
|
|
|
|
$adminMembers = Permission::get_members_by_permission('ADMIN');
|
|
|
|
$this->assertEquals(0, $adminMembers->count());
|
|
|
|
|
2017-06-15 04:20:12 +02:00
|
|
|
$admin = DefaultAdminService::singleton()->findOrCreateDefaultAdmin();
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$this->assertInstanceOf(Member::class, $admin);
|
|
|
|
$this->assertTrue(Permission::checkMember($admin, 'ADMIN'));
|
2017-06-15 04:20:12 +02:00
|
|
|
$this->assertEquals($admin->Email, DefaultAdminService::getDefaultAdminUsername());
|
|
|
|
$this->assertTrue(DefaultAdminService::isDefaultAdmin($admin->Email));
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertNull($admin->Password);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFindAnAdministratorWithoutDefaultAdmin()
|
|
|
|
{
|
|
|
|
// Clear default admin
|
2017-06-15 04:20:12 +02:00
|
|
|
$service = DefaultAdminService::singleton();
|
2017-06-13 11:04:43 +02:00
|
|
|
DefaultAdminService::clearDefaultAdmin();
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$adminMembers = Permission::get_members_by_permission('ADMIN');
|
|
|
|
$this->assertEquals(0, $adminMembers->count());
|
|
|
|
|
2017-06-13 11:04:43 +02:00
|
|
|
$admin = $service->findOrCreateDefaultAdmin();
|
|
|
|
$this->assertNull($admin);
|
2017-06-15 04:20:12 +02:00
|
|
|
|
2017-06-13 11:04:43 +02:00
|
|
|
// When clearing the admin, it will not re-instate it anymore
|
|
|
|
DefaultAdminService::setDefaultAdmin('admin', 'password');
|
2017-06-15 04:20:12 +02:00
|
|
|
$admin = $service->findOrCreateDefaultAdmin();
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertTrue(Permission::checkMember($admin, 'ADMIN'));
|
|
|
|
|
2017-06-15 04:20:12 +02:00
|
|
|
// User should have Email but no Password
|
|
|
|
$this->assertEquals('admin', $admin->Email);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEmpty($admin->Password);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDefaultAdmin()
|
|
|
|
{
|
|
|
|
$adminMembers = Permission::get_members_by_permission('ADMIN');
|
|
|
|
$this->assertEquals(0, $adminMembers->count());
|
|
|
|
|
2017-06-15 04:20:12 +02:00
|
|
|
$admin = DefaultAdminService::singleton()->findOrCreateDefaultAdmin();
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertInstanceOf(Member::class, $admin);
|
|
|
|
$this->assertTrue(Permission::checkMember($admin, 'ADMIN'));
|
2017-06-15 04:20:12 +02:00
|
|
|
$this->assertEquals($admin->Email, DefaultAdminService::getDefaultAdminUsername());
|
|
|
|
$this->assertTrue(DefaultAdminService::isDefaultAdmin($admin->Email));
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertNull($admin->Password);
|
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|