request = $this->createMock(IRequest::class); $this->capabilitiesManager = $this->createMock(CapabilitiesManager::class); $this->userSession = $this->createMock(IUserSession::class); $this->userManager = $this->createMock(IUserManager::class); $this->keyManager = $this->createMock(Manager::class); $this->controller = new OCSController( 'core', $this->request, $this->capabilitiesManager, $this->userSession, $this->userManager, $this->keyManager ); } public function testGetConfig() { $this->request->method('getServerHost') ->willReturn('awesomehost.io'); $data = [ 'version' => '1.7', 'website' => 'Nextcloud', 'host' => 'awesomehost.io', 'contact' => '', 'ssl' => 'false', ]; $expected = new DataResponse($data); $this->assertEquals($expected, $this->controller->getConfig()); return new DataResponse($data); } public function testGetCapabilities() { $this->userSession->expects($this->once()) ->method('isLoggedIn') ->willReturn(true); [$major, $minor, $micro] = \OCP\Util::getVersion(); $result = []; $result['version'] = [ 'major' => $major, 'minor' => $minor, 'micro' => $micro, 'string' => \OC_Util::getVersionString(), 'edition' => '', 'extendedSupport' => false ]; $capabilities = [ 'foo' => 'bar', 'a' => [ 'b' => true, 'c' => 11, ] ]; $this->capabilitiesManager->method('getCapabilities') ->willReturn($capabilities); $result['capabilities'] = $capabilities; $expected = new DataResponse($result); $expected->setETag(md5(json_encode($result))); $this->assertEquals($expected, $this->controller->getCapabilities()); } public function testGetCapabilitiesPublic() { $this->userSession->expects($this->once()) ->method('isLoggedIn') ->willReturn(false); [$major, $minor, $micro] = \OCP\Util::getVersion(); $result = []; $result['version'] = [ 'major' => $major, 'minor' => $minor, 'micro' => $micro, 'string' => \OC_Util::getVersionString(), 'edition' => '', 'extendedSupport' => false ]; $capabilities = [ 'foo' => 'bar', 'a' => [ 'b' => true, 'c' => 11, ] ]; $this->capabilitiesManager->method('getCapabilities') ->with(true) ->willReturn($capabilities); $result['capabilities'] = $capabilities; $expected = new DataResponse($result); $expected->setETag(md5(json_encode($result))); $this->assertEquals($expected, $this->controller->getCapabilities()); } public function testPersonCheckValid() { $this->userManager->method('checkPassword') ->with( $this->equalTo('user'), $this->equalTo('pass') )->willReturn($this->createMock(IUser::class)); $expected = new DataResponse([ 'person' => [ 'personid' => 'user' ] ]); $this->assertEquals($expected, $this->controller->personCheck('user', 'pass')); } public function testPersonInvalid() { $this->userManager->method('checkPassword') ->with( $this->equalTo('user'), $this->equalTo('wrongpass') )->willReturn(false); $expected = new DataResponse([], 102); $expected->throttle(); $this->assertEquals($expected, $this->controller->personCheck('user', 'wrongpass')); } public function testPersonNoLogin() { $this->userManager->method('checkPassword') ->with( $this->equalTo('user'), $this->equalTo('wrongpass') )->willReturn(false); $expected = new DataResponse([], 101); $this->assertEquals($expected, $this->controller->personCheck('', '')); } public function testGetIdentityProofWithNotExistingUser() { $this->userManager ->expects($this->once()) ->method('get') ->with('NotExistingUser') ->willReturn(null); $expected = new DataResponse(['Account not found'], 404); $this->assertEquals($expected, $this->controller->getIdentityProof('NotExistingUser')); } public function testGetIdentityProof() { $user = $this->createMock(IUser::class); $key = $this->createMock(Key::class); $this->userManager ->expects($this->once()) ->method('get') ->with('ExistingUser') ->willReturn($user); $this->keyManager ->expects($this->once()) ->method('getKey') ->with($user) ->willReturn($key); $key ->expects($this->once()) ->method('getPublic') ->willReturn('Existing Users public key'); $expected = new DataResponse([ 'public' => 'Existing Users public key', ]); $this->assertEquals($expected, $this->controller->getIdentityProof('ExistingUser')); } }