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