diff options
author | Frederic Guillot <fred@kanboard.net> | 2015-12-05 20:31:27 -0500 |
---|---|---|
committer | Frederic Guillot <fred@kanboard.net> | 2015-12-05 20:31:27 -0500 |
commit | e9fedf3e5cd63aea4da7a71f6647ee427c62fa49 (patch) | |
tree | abc2de5aebace4a2d7c94805552264dab6b10bc7 /tests/units/Model/ProjectPermissionTest.php | |
parent | 346b8312e5ac877ce3192c2db3a26b500018bbb5 (diff) |
Rewrite of the authentication and authorization system
Diffstat (limited to 'tests/units/Model/ProjectPermissionTest.php')
-rw-r--r-- | tests/units/Model/ProjectPermissionTest.php | 318 |
1 files changed, 43 insertions, 275 deletions
diff --git a/tests/units/Model/ProjectPermissionTest.php b/tests/units/Model/ProjectPermissionTest.php index 1ee63a76..8f118cd9 100644 --- a/tests/units/Model/ProjectPermissionTest.php +++ b/tests/units/Model/ProjectPermissionTest.php @@ -2,286 +2,54 @@ require_once __DIR__.'/../Base.php'; -use Kanboard\Model\Project; use Kanboard\Model\ProjectPermission; +use Kanboard\Model\Project; use Kanboard\Model\User; +use Kanboard\Model\Group; +use Kanboard\Model\GroupMember; +use Kanboard\Model\ProjectGroupRole; +use Kanboard\Model\ProjectUserRole; +use Kanboard\Core\Security\Role; class ProjectPermissionTest extends Base { - public function testAllowEverybody() - { - $user = new User($this->container); - $this->assertNotFalse($user->create(array('username' => 'unittest#1', 'password' => 'unittest'))); - $this->assertNotFalse($user->create(array('username' => 'unittest#2', 'password' => 'unittest'))); - - $p = new Project($this->container); - $pp = new ProjectPermission($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'UnitTest'))); - $this->assertFalse($pp->isEverybodyAllowed(1)); - $this->assertTrue($pp->isUserAllowed(1, 1)); - $this->assertFalse($pp->isUserAllowed(1, 2)); - $this->assertFalse($pp->isUserAllowed(1, 3)); - $this->assertEquals(array(), $pp->getMembers(1)); - $this->assertEquals(array('Unassigned'), $pp->getMemberList(1)); - - $this->assertEmpty($pp->getMemberProjects(1)); - $this->assertEmpty($pp->getMemberProjects(2)); - $this->assertEmpty($pp->getMemberProjects(3)); - - $this->assertEmpty($pp->getMemberProjectIds(1)); - $this->assertEmpty($pp->getMemberProjectIds(2)); - $this->assertEmpty($pp->getMemberProjectIds(3)); - - $this->assertEmpty($pp->getActiveMemberProjectIds(1)); - $this->assertEmpty($pp->getActiveMemberProjectIds(2)); - $this->assertEmpty($pp->getActiveMemberProjectIds(3)); - - $this->assertEmpty($pp->getActiveMemberProjects(1)); - $this->assertEmpty($pp->getActiveMemberProjects(2)); - $this->assertEmpty($pp->getActiveMemberProjects(3)); - - $this->assertTrue($p->update(array('id' => 1, 'is_everybody_allowed' => 1))); - $this->assertTrue($pp->isEverybodyAllowed(1)); - $this->assertTrue($pp->isUserAllowed(1, 1)); - $this->assertTrue($pp->isUserAllowed(1, 2)); - $this->assertTrue($pp->isUserAllowed(1, 3)); - $this->assertEquals(array('1' => 'admin', '2' => 'unittest#1', '3' => 'unittest#2'), $pp->getMembers(1)); - $this->assertEquals(array('Unassigned', '1' => 'admin', '2' => 'unittest#1', '3' => 'unittest#2'), $pp->getMemberList(1)); - - $this->assertNotEmpty($pp->getMemberProjects(1)); - $this->assertNotEmpty($pp->getMemberProjects(2)); - $this->assertNotEmpty($pp->getMemberProjects(3)); - - $this->assertNotEmpty($pp->getMemberProjectIds(1)); - $this->assertNotEmpty($pp->getMemberProjectIds(2)); - $this->assertNotEmpty($pp->getMemberProjectIds(3)); - - $this->assertNotEmpty($pp->getActiveMemberProjectIds(1)); - $this->assertNotEmpty($pp->getActiveMemberProjectIds(2)); - $this->assertNotEmpty($pp->getActiveMemberProjectIds(3)); - - $this->assertNotEmpty($pp->getActiveMemberProjects(1)); - $this->assertNotEmpty($pp->getActiveMemberProjects(2)); - $this->assertNotEmpty($pp->getActiveMemberProjects(3)); - - $this->assertTrue($p->disable(1)); - - $this->assertEmpty($pp->getActiveMemberProjectIds(1)); - $this->assertEmpty($pp->getActiveMemberProjectIds(2)); - $this->assertEmpty($pp->getActiveMemberProjectIds(3)); - - $this->assertEmpty($pp->getActiveMemberProjects(1)); - $this->assertEmpty($pp->getActiveMemberProjects(2)); - $this->assertEmpty($pp->getActiveMemberProjects(3)); - } - - public function testDisallowEverybody() - { - // We create a regular user - $user = new User($this->container); - $user->create(array('username' => 'unittest', 'password' => 'unittest')); - - $p = new Project($this->container); - $pp = new ProjectPermission($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'UnitTest'))); - - $this->assertEmpty($pp->getMembers(1)); // Nobody is specified for the given project - $this->assertTrue($pp->isUserAllowed(1, 1)); // Admin should be allowed - $this->assertFalse($pp->isUserAllowed(1, 2)); // Regular user should be denied - } - - public function testAllowUser() - { - $p = new Project($this->container); - $pp = new ProjectPermission($this->container); - $user = new User($this->container); - - $this->assertNotFalse($user->create(array('username' => 'unittest', 'password' => 'unittest'))); - - // We create a project - $this->assertEquals(1, $p->create(array('name' => 'UnitTest'))); - - $this->assertEmpty($pp->getMemberProjects(1)); - $this->assertEmpty($pp->getMemberProjects(2)); - - $this->assertEmpty($pp->getMemberProjectIds(1)); - $this->assertEmpty($pp->getMemberProjectIds(2)); - - $this->assertEmpty($pp->getActiveMemberProjectIds(1)); - $this->assertEmpty($pp->getActiveMemberProjectIds(2)); - - $this->assertEmpty($pp->getActiveMemberProjects(1)); - $this->assertEmpty($pp->getActiveMemberProjects(2)); - - // We allow the admin user - $this->assertTrue($pp->addMember(1, 1)); - $this->assertTrue($pp->addMember(1, 2)); - - // Non-existant project - $this->assertFalse($pp->addMember(50, 1)); - - // Non-existant user - $this->assertFalse($pp->addMember(1, 50)); - - // Both users should be allowed - $this->assertEquals(array('1' => 'admin', '2' => 'unittest'), $pp->getMembers(1)); - $this->assertTrue($pp->isUserAllowed(1, 1)); - $this->assertTrue($pp->isUserAllowed(1, 2)); - - $this->assertNotEmpty($pp->getMemberProjects(1)); - $this->assertNotEmpty($pp->getMemberProjects(2)); - - $this->assertNotEmpty($pp->getMemberProjectIds(1)); - $this->assertNotEmpty($pp->getMemberProjectIds(2)); - - $this->assertNotEmpty($pp->getActiveMemberProjectIds(1)); - $this->assertNotEmpty($pp->getActiveMemberProjectIds(2)); - - $this->assertNotEmpty($pp->getActiveMemberProjects(1)); - $this->assertNotEmpty($pp->getActiveMemberProjects(2)); - } - - public function testRevokeUser() - { - $p = new Project($this->container); - $pp = new ProjectPermission($this->container); - $user = new User($this->container); - - $user->create(array('username' => 'unittest', 'password' => 'unittest')); - - // We create a project - $this->assertEquals(1, $p->create(array('name' => 'UnitTest'))); - - // We revoke our admin user (not existing row) - $this->assertFalse($pp->revokeMember(1, 1)); - - // We should have nobody in the users list - $this->assertEmpty($pp->getMembers(1)); - - // Only admin is allowed - $this->assertTrue($pp->isUserAllowed(1, 1)); - $this->assertFalse($pp->isUserAllowed(1, 2)); - - // We allow only the regular user - $this->assertTrue($pp->addMember(1, 2)); - - // All users should be allowed (admin and regular) - $this->assertTrue($pp->isUserAllowed(1, 1)); - $this->assertTrue($pp->isUserAllowed(1, 2)); - - // However, we should have only our regular user in the list - $this->assertEquals(array('2' => 'unittest'), $pp->getMembers(1)); - - // We allow our admin, we should have both in the list - $this->assertTrue($pp->addMember(1, 1)); - $this->assertEquals(array('1' => 'admin', '2' => 'unittest'), $pp->getMembers(1)); - $this->assertTrue($pp->isUserAllowed(1, 1)); - $this->assertTrue($pp->isUserAllowed(1, 2)); - - // We revoke the regular user - $this->assertTrue($pp->revokeMember(1, 2)); - - // Only admin should be allowed - $this->assertTrue($pp->isUserAllowed(1, 1)); - $this->assertFalse($pp->isUserAllowed(1, 2)); - - // We should have only admin in the list - $this->assertEquals(array('1' => 'admin'), $pp->getMembers(1)); - - // We revoke the admin user - $this->assertTrue($pp->revokeMember(1, 1)); - $this->assertEmpty($pp->getMembers(1)); - - // Only admin should be allowed again - $this->assertTrue($pp->isUserAllowed(1, 1)); - $this->assertFalse($pp->isUserAllowed(1, 2)); - } - - public function testManager() - { - $p = new Project($this->container); - $pp = new ProjectPermission($this->container); - $u = new User($this->container); - - $this->assertEquals(2, $u->create(array('username' => 'unittest', 'password' => 'unittest'))); - - $this->assertEquals(1, $p->create(array('name' => 'UnitTest1'))); - $this->assertFalse($pp->isMember(1, 2)); - $this->assertFalse($pp->isManager(1, 2)); - - $this->assertEquals(2, $p->create(array('name' => 'UnitTest2'), 1, true)); - $this->assertFalse($pp->isMember(2, 2)); - $this->assertFalse($pp->isManager(2, 2)); - - $this->assertEquals(3, $p->create(array('name' => 'UnitTest3'), 2, true)); - $this->assertTrue($pp->isMember(3, 2)); - $this->assertTrue($pp->isManager(3, 2)); - - $this->assertEquals(4, $p->create(array('name' => 'UnitTest4'))); - - $this->assertTrue($pp->addManager(4, 2)); - $this->assertTrue($pp->isMember(4, 2)); - $this->assertTrue($pp->isManager(4, 2)); - - $this->assertEquals(5, $p->create(array('name' => 'UnitTest5'))); - $this->assertTrue($pp->addMember(5, 2)); - $this->assertTrue($pp->changeRole(5, 2, 1)); - $this->assertTrue($pp->isMember(5, 2)); - $this->assertTrue($pp->isManager(5, 2)); - $this->assertTrue($pp->changeRole(5, 2, 0)); - $this->assertTrue($pp->isMember(5, 2)); - $this->assertFalse($pp->isManager(5, 2)); - } - - public function testUsersList() + public function testDuplicate() { - $p = new Project($this->container); - $pp = new ProjectPermission($this->container); - - $user = new User($this->container); - $this->assertNotFalse($user->create(array('username' => 'unittest', 'password' => 'unittest'))); - - // We create project - $this->assertEquals(1, $p->create(array('name' => 'UnitTest'))); - - // No restriction, we should have no body - $this->assertEquals( - array('Unassigned'), - $pp->getMemberList(1) - ); - - // We allow only the regular user - $this->assertTrue($pp->addMember(1, 2)); - - $this->assertEquals( - array(0 => 'Unassigned', 2 => 'unittest'), - $pp->getMemberList(1) - ); - - // We allow the admin user - $this->assertTrue($pp->addMember(1, 1)); - - $this->assertEquals( - array(0 => 'Unassigned', 1 => 'admin', 2 => 'unittest'), - $pp->getMemberList(1) - ); - - // We revoke only the regular user - $this->assertTrue($pp->revokeMember(1, 2)); - - $this->assertEquals( - array(0 => 'Unassigned', 1 => 'admin'), - $pp->getMemberList(1) - ); - - // We revoke only the admin user, we should have everybody - $this->assertTrue($pp->revokeMember(1, 1)); - - $this->assertEquals( - array(0 => 'Unassigned'), - $pp->getMemberList(1) - ); + $userModel = new User($this->container); + $projectModel = new Project($this->container); + $groupModel = new Group($this->container); + $groupMemberModel = new GroupMember($this->container); + $groupRoleModel = new ProjectGroupRole($this->container); + $userRoleModel = new ProjectUserRole($this->container); + $projectPermission = new ProjectPermission($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Project 1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'Project 2'))); + + $this->assertEquals(2, $userModel->create(array('username' => 'user 1', 'name' => 'User #1'))); + $this->assertEquals(3, $userModel->create(array('username' => 'user 2'))); + $this->assertEquals(4, $userModel->create(array('username' => 'user 3'))); + $this->assertEquals(5, $userModel->create(array('username' => 'user 4'))); + $this->assertEquals(6, $userModel->create(array('username' => 'user 5', 'name' => 'User #5'))); + + $this->assertEquals(1, $groupModel->create('Group C')); + $this->assertEquals(2, $groupModel->create('Group B')); + $this->assertEquals(3, $groupModel->create('Group A')); + + $this->assertTrue($groupMemberModel->addUser(1, 4)); + $this->assertTrue($groupMemberModel->addUser(2, 5)); + $this->assertTrue($groupMemberModel->addUser(3, 3)); + $this->assertTrue($groupMemberModel->addUser(3, 2)); + + $this->assertTrue($groupRoleModel->addGroup(1, 1, Role::PROJECT_VIEWER)); + $this->assertTrue($groupRoleModel->addGroup(1, 3, Role::PROJECT_MANAGER)); + + $this->assertTrue($userRoleModel->addUser(1, 5, Role::PROJECT_MANAGER)); + $this->assertTrue($userRoleModel->addUser(1, 6, Role::PROJECT_MEMBER)); + + $this->assertTrue($projectPermission->duplicate(1, 2)); + + $this->assertCount(2, $userRoleModel->getUsers(2)); + $this->assertCount(3, $groupRoleModel->getUsers(2)); } } |