2013-06-07 12:40:21 +02:00
|
|
|
<?php
|
2016-02-08 15:41:00 +01:00
|
|
|
/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */
|
2013-06-07 12:40:21 +02:00
|
|
|
|
2013-07-26 15:58:16 +02:00
|
|
|
namespace Tests\Icinga;
|
2013-08-06 18:02:40 +02:00
|
|
|
|
2021-01-22 15:59:01 +01:00
|
|
|
use Icinga\Authentication\Role;
|
2014-04-22 09:43:53 +02:00
|
|
|
use Mockery;
|
|
|
|
use DateTimeZone;
|
2014-04-10 10:32:50 +02:00
|
|
|
use Icinga\User;
|
|
|
|
use Icinga\Test\BaseTestCase;
|
2013-08-06 18:02:40 +02:00
|
|
|
|
2014-04-10 10:32:50 +02:00
|
|
|
class UserTest extends BaseTestCase
|
2013-06-07 12:40:21 +02:00
|
|
|
{
|
2013-08-06 18:02:40 +02:00
|
|
|
public function testGetDefaultTimezoneIfTimezoneNotSet()
|
|
|
|
{
|
2014-04-10 10:32:50 +02:00
|
|
|
$user = new User('unittest');
|
2014-04-22 09:43:53 +02:00
|
|
|
$prefs = Mockery::mock('Icinga\User\Preferences');
|
|
|
|
$prefs->shouldReceive('get')->with('timezone')->andReturnNull();
|
2013-08-06 18:02:40 +02:00
|
|
|
$user->setPreferences($prefs);
|
2014-04-22 09:43:53 +02:00
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
new DateTimeZone(date_default_timezone_get()),
|
|
|
|
$user->getTimeZone(),
|
2013-08-06 18:02:40 +02:00
|
|
|
'User\'s timezone does not match the default timezone'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetTimezoneIfTimezoneSet()
|
|
|
|
{
|
|
|
|
$explicitTz = 'Europe/Berlin';
|
2014-04-10 10:32:50 +02:00
|
|
|
$user = new User('unittest');
|
2014-04-22 09:43:53 +02:00
|
|
|
$prefs = Mockery::mock('Icinga\User\Preferences');
|
|
|
|
$prefs->shouldReceive('get')->with('timezone')->andReturn($explicitTz);
|
2013-08-06 18:02:40 +02:00
|
|
|
$user->setPreferences($prefs);
|
2013-08-20 11:27:25 +02:00
|
|
|
|
2014-04-22 09:43:53 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
new DateTimeZone($explicitTz),
|
|
|
|
$user->getTimeZone(),
|
2024-01-09 00:55:09 +01:00
|
|
|
'User\'s timezone does not match the timezone set by themself'
|
2013-08-06 18:02:40 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-04-22 09:43:53 +02:00
|
|
|
public function testWhetherValidEmailsCanBeSet()
|
|
|
|
{
|
|
|
|
$user = new User('unittest');
|
|
|
|
$user->setEmail('mySampleEmail@someDomain.org');
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
$user->getEmail(),
|
|
|
|
'mySampleEmail@someDomain.org',
|
|
|
|
'Valid emails set with setEmail are not returned by getEmail'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testWhetherInvalidEmailsCannotBeSet()
|
|
|
|
{
|
2021-04-09 10:49:17 +02:00
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
|
|
|
2014-04-22 09:43:53 +02:00
|
|
|
$user = new User('unittest');
|
|
|
|
$user->setEmail('mySampleEmail at someDomain dot org');
|
|
|
|
}
|
2014-09-18 14:37:18 +02:00
|
|
|
|
|
|
|
public function testPermissions()
|
|
|
|
{
|
2021-01-22 15:59:01 +01:00
|
|
|
$role = new Role();
|
|
|
|
$role->setPermissions([
|
2014-09-18 14:37:18 +02:00
|
|
|
'test',
|
|
|
|
'test/some/specific',
|
2015-05-05 12:36:26 +02:00
|
|
|
'test/more/*',
|
2015-06-02 12:01:02 +02:00
|
|
|
'test/wildcard-with-wildcard/*',
|
|
|
|
'test/even-more/specific-with-wildcard/*'
|
2021-01-22 15:59:01 +01:00
|
|
|
]);
|
|
|
|
|
|
|
|
$user = new User('test');
|
|
|
|
$user->setRoles([$role]);
|
|
|
|
|
2014-09-18 14:37:18 +02:00
|
|
|
$this->assertTrue($user->can('test'));
|
2021-01-29 15:48:48 +01:00
|
|
|
$this->assertTrue($user->can('test/some/*'));
|
2014-09-18 14:37:18 +02:00
|
|
|
$this->assertTrue($user->can('test/some/specific'));
|
|
|
|
$this->assertTrue($user->can('test/more/everything'));
|
2015-05-05 12:36:26 +02:00
|
|
|
$this->assertTrue($user->can('test/wildcard-with-wildcard/*'));
|
2015-05-07 17:19:00 +02:00
|
|
|
$this->assertTrue($user->can('test/wildcard-with-wildcard/sub/sub'));
|
2015-06-02 12:01:02 +02:00
|
|
|
$this->assertTrue($user->can('test/even-more/*'));
|
2014-09-18 14:37:18 +02:00
|
|
|
$this->assertFalse($user->can('not/test'));
|
|
|
|
$this->assertFalse($user->can('test/some/not/so/specific'));
|
2015-05-05 12:36:26 +02:00
|
|
|
$this->assertFalse($user->can('test/wildcard2/*'));
|
2014-09-18 14:37:18 +02:00
|
|
|
}
|
2021-01-29 15:48:48 +01:00
|
|
|
|
|
|
|
public function testRefusals()
|
|
|
|
{
|
|
|
|
$role = new Role();
|
|
|
|
$role->setPermissions([
|
|
|
|
'a',
|
|
|
|
'a/b/*',
|
|
|
|
'a/b/c/d',
|
|
|
|
'c/*',
|
|
|
|
'd/*'
|
|
|
|
]);
|
|
|
|
$role->setRefusals([
|
|
|
|
'a/b/c',
|
|
|
|
'a/b/e',
|
|
|
|
'c/b/a',
|
|
|
|
'c/d/*',
|
|
|
|
'd/f',
|
|
|
|
'e/g'
|
|
|
|
]);
|
|
|
|
|
|
|
|
$user = new User('test');
|
|
|
|
$user->setRoles([$role]);
|
|
|
|
|
|
|
|
$this->assertFalse($user->can('a/b/c'));
|
|
|
|
$this->assertFalse($user->can('a/b/e'));
|
|
|
|
$this->assertTrue($user->can('a/b/d'));
|
|
|
|
$this->assertTrue($user->can('a/b/c/d'));
|
|
|
|
$this->assertFalse($user->can('c/b/a'));
|
|
|
|
$this->assertTrue($user->can('c/b/d'));
|
|
|
|
$this->assertFalse($user->can('c/d/u'));
|
|
|
|
$this->assertFalse($user->can('c/d/*'));
|
|
|
|
$this->assertTrue($user->can('c/*'));
|
|
|
|
$this->assertTrue($user->can('d/*'));
|
|
|
|
$this->assertFalse($user->can('e/*'));
|
|
|
|
|
|
|
|
$secondRole = new Role();
|
|
|
|
$role->setRefusals(['a/b/*']);
|
|
|
|
|
|
|
|
$user->setRoles([$role, $secondRole]);
|
|
|
|
|
|
|
|
$this->assertFalse($user->can('a/b/d'));
|
|
|
|
$this->assertFalse($user->can('a/b/c/d'));
|
|
|
|
$this->assertTrue($user->can('c/b/d'));
|
|
|
|
}
|
2013-06-07 12:40:21 +02:00
|
|
|
}
|