diff options
author | Joas Schilling <coding@schilljs.com> | 2018-02-23 12:08:08 +0300 |
---|---|---|
committer | Joas Schilling <coding@schilljs.com> | 2018-02-23 12:08:08 +0300 |
commit | 671941043a32b847d0c3e261e778a37f3b1cc9de (patch) | |
tree | 2458bbfdf80233d42563832c2268bbf3d80835d3 /tests | |
parent | 262654303ef6f544fb706a7ab754f8f3fc544628 (diff) |
Fix unit tests
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/unit/Controller/SettingsControllerTest.php | 221 |
1 files changed, 187 insertions, 34 deletions
diff --git a/tests/unit/Controller/SettingsControllerTest.php b/tests/unit/Controller/SettingsControllerTest.php index 0f62787..3e11fdd 100644 --- a/tests/unit/Controller/SettingsControllerTest.php +++ b/tests/unit/Controller/SettingsControllerTest.php @@ -11,12 +11,18 @@ namespace OCA\Impersonate\Tests\Controller; +use OC\Group\Manager; +use OC\SubAdmin; use OCA\Impersonate\Controller\SettingsController; use OCP\AppFramework\Http\JSONResponse; use OCP\AppFramework\Http; +use OCP\IConfig; +use OCP\IGroupManager; +use OCP\IL10N; use OCP\IRequest; use OCP\ILogger; use OCP\ISession; +use OCP\IUser; use OCP\IUserManager; use OCP\IUserSession; use Test\TestCase; @@ -34,33 +40,56 @@ class SettingsControllerTest extends TestCase { private $request; /** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */ private $userManager; + /** @var IGroupManager|\PHPUnit_Framework_MockObject_MockObject */ + private $groupManager; + /** @var SubAdmin|\PHPUnit_Framework_MockObject_MockObject */ + private $subadmin; /** @var IUserSession|\PHPUnit_Framework_MockObject_MockObject */ private $userSession; - /** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */ - private $logger; /** @var ISession|\PHPUnit_Framework_MockObject_MockObject */ private $session; + /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */ + private $config; + /** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */ + private $logger; + /** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */ + private $l; /** @var SettingsController */ private $controller; public function setUp() { + parent::setUp(); + $this->appName = 'impersonate'; $this->request = $this->createMock(IRequest::class); $this->userManager = $this->createMock(IUserManager::class); + $this->groupManager = $this->createMock(Manager::class); $this->userSession = $this->createMock(IUserSession::class); - $this->logger = $this->createMock(ILogger::class); $this->session = $this->createMock(ISession::class); + $this->config = $this->createMock(IConfig::class); + $this->logger = $this->createMock(ILogger::class); + $this->l = $this->createMock(IL10N::class); + $this->l->expects($this->any()) + ->method('t') + ->will($this->returnCallback(function($text, $parameters = array()) { + return vsprintf($text, $parameters); + })); + $this->subadmin = $this->createMock(SubAdmin::class); + $this->groupManager->expects($this->any()) + ->method('getSubAdmin') + ->willReturn($this->subadmin); $this->controller = new SettingsController( $this->appName, $this->request, $this->userManager, + $this->groupManager, $this->userSession, $this->session, - $this->logger + $this->config, + $this->logger, + $this->l ); - - parent::setUp(); } public function testImpersonateNotFound() { @@ -75,7 +104,7 @@ class SettingsControllerTest extends TestCase { ->method('setUser'); $this->assertEquals( - new JSONResponse(['message' => 'No user found for notexisting@uid'], Http::STATUS_NOT_FOUND), + new JSONResponse(['message' => 'User not found'], Http::STATUS_NOT_FOUND), $this->controller->impersonate('notexisting@uid') ); } @@ -86,19 +115,26 @@ class SettingsControllerTest extends TestCase { ['UserName', 'username'] ]; } + /** * @dataProvider usersProvider * @param $query * @param $uid */ - public function testImpersonate($query, $uid) { - $user = $this->createMock('\OCP\IUser'); - $user->method('getUID') + public function testAdminImpersonate($query, $uid) { + $currentUser = $this->createMock(IUser::class); + $currentUser->expects($this->any()) + ->method('getUID') + ->willReturn('admin'); + + $user = $this->createMock(IUser::class); + $user->expects($this->any()) + ->method('getUID') ->willReturn($uid); $this->userSession ->method('getUser') - ->willReturn($user); + ->willReturn($currentUser); $this->userManager->expects($this->once()) ->method('get') @@ -109,62 +145,179 @@ class SettingsControllerTest extends TestCase { ->method('setUser') ->with($user); + $this->groupManager->expects($this->once()) + ->method('isAdmin') + ->with('admin') + ->willReturn(true); + + $this->groupManager->expects($this->once()) + ->method('getUserGroupIds') + ->with($currentUser) + ->willReturn(['admin']); + + $this->config->expects($this->once()) + ->method('getAppValue') + ->with('impersonate', 'authorized', '["admin"]') + ->willReturnArgument(2); + $this->assertEquals( new JSONResponse(), $this->controller->impersonate($query) ); } - public function normalUsers() { - return [ - ['username', 'username'], - ['UserName', 'username'] - ]; + /** + * @dataProvider usersProvider + * @param $query + * @param $uid + */ + public function testSubAdminImpersonate($query, $uid) { + $currentUser = $this->createMock(IUser::class); + $currentUser->expects($this->any()) + ->method('getUID') + ->willReturn('admin'); + + $user = $this->createMock(IUser::class); + $user->expects($this->any()) + ->method('getUID') + ->willReturn($uid); + + $this->userSession + ->method('getUser') + ->willReturn($currentUser); + + $this->userManager->expects($this->once()) + ->method('get') + ->with($query) + ->willReturn($user); + + $this->userSession->expects($this->once()) + ->method('setUser') + ->with($user); + + $this->groupManager->expects($this->once()) + ->method('isAdmin') + ->with('admin') + ->willReturn(false); + + $this->subadmin->expects($this->once()) + ->method('isUserAccessible') + ->with($currentUser, $user) + ->willReturn(true); + + $this->groupManager->expects($this->once()) + ->method('getUserGroupIds') + ->with($currentUser) + ->willReturn(['subadmin']); + + $this->config->expects($this->once()) + ->method('getAppValue') + ->with('impersonate', 'authorized', '["admin"]') + ->willReturn(json_encode(['admin', 'subadmin'])); + + $this->assertEquals( + new JSONResponse(), + $this->controller->impersonate($query) + ); } /** - * @dataProvider normalUsers + * @dataProvider usersProvider * @param $query + * @param $uid */ - - public function testAdminImpersonateNormalUsers($query,$uid) { - $loggedInUser = $this->createMock('OCP\IUser'); - $loggedInUser - ->expects($this->once()) + public function testSubAdminImpersonateNotAllowed($query, $uid) { + $currentUser = $this->createMock(IUser::class); + $currentUser->expects($this->any()) ->method('getUID') ->willReturn('admin'); + $user = $this->createMock(IUser::class); + $user->expects($this->any()) + ->method('getUID') + ->willReturn($uid); + $this->userSession - ->expects($this->once()) ->method('getUser') - ->willReturn($loggedInUser); + ->willReturn($currentUser); + + $this->userManager->expects($this->once()) + ->method('get') + ->with($query) + ->willReturn($user); + + $this->groupManager->expects($this->once()) + ->method('isAdmin') + ->with('admin') + ->willReturn(false); + + $this->subadmin->expects($this->once()) + ->method('isUserAccessible') + ->with($currentUser, $user) + ->willReturn(true); + + $this->userSession->expects($this->never()) + ->method('setUser') + ->with($user); + + $this->groupManager->expects($this->once()) + ->method('getUserGroupIds') + ->with($currentUser) + ->willReturn(['subadmin']); - $userManager = $this->getMockBuilder('OC\User\Manager') - ->disableOriginalConstructor() - ->getMock(); + $this->config->expects($this->once()) + ->method('getAppValue') + ->with('impersonate', 'authorized', '["admin"]') + ->willReturnArgument(2); + + $this->assertEquals( + new JSONResponse(['message' => 'Not enough permissions to impersonate user'], Http::STATUS_FORBIDDEN), + $this->controller->impersonate($query) + ); + } - $userManager->expects($this->any()) - ->method('createUser') - ->with($query,'123'); + /** + * @dataProvider usersProvider + * @param $query + * @param $uid + */ + public function testSubAdminImpersonateNotAccessible($query, $uid) { + $currentUser = $this->createMock(IUser::class); + $currentUser->expects($this->any()) + ->method('getUID') + ->willReturn('admin'); - $user = $this->createMock('\OCP\IUser'); + $user = $this->createMock(IUser::class); $user->expects($this->any()) ->method('getUID') ->willReturn($uid); + $this->userSession + ->method('getUser') + ->willReturn($currentUser); + $this->userManager->expects($this->once()) ->method('get') ->with($query) ->willReturn($user); - $this->userSession->expects($this->once()) + $this->groupManager->expects($this->once()) + ->method('isAdmin') + ->with('admin') + ->willReturn(false); + + $this->subadmin->expects($this->once()) + ->method('isUserAccessible') + ->with($currentUser, $user) + ->willReturn(false); + + $this->userSession->expects($this->never()) ->method('setUser') ->with($user); $this->assertEquals( - new JSONResponse(), + new JSONResponse(['message' => 'Not enough permissions to impersonate user'], Http::STATUS_FORBIDDEN), $this->controller->impersonate($query) ); } - } |