diff options
author | sualko <klaus@jsxc.org> | 2017-07-26 17:16:43 +0300 |
---|---|---|
committer | sualko <klaus@jsxc.org> | 2017-07-26 17:16:43 +0300 |
commit | d16756107d15ee4bec23665ea100b084237a81c5 (patch) | |
tree | 2ec6af3508ed40359c0f5aff3a63239c1b86a98f /tests/unit | |
parent | d7fd8c788095e0284129877f61d325bf78eb9aa0 (diff) |
run php-cs-fixer
Diffstat (limited to 'tests/unit')
-rw-r--r-- | tests/unit/Middleware/ExternalApiMiddlewareTest.php | 284 | ||||
-rw-r--r-- | tests/unit/NewContentContainerTest.php | 17 | ||||
-rw-r--r-- | tests/unit/TimeLimitedTokenTest.php | 81 | ||||
-rw-r--r-- | tests/unit/controller/ExternalApiControllerTest.php | 653 | ||||
-rw-r--r-- | tests/unit/controller/HttpBindControllerTest.php | 86 | ||||
-rw-r--r-- | tests/unit/controller/ManagedServerControllerTest.php | 447 | ||||
-rw-r--r-- | tests/unit/controller/SettingsControllerTest.php | 372 | ||||
-rw-r--r-- | tests/unit/http/XMPPResponseTest.php | 21 | ||||
-rw-r--r-- | tests/unit/stanzahandlers/IQTest.php | 15 | ||||
-rw-r--r-- | tests/unit/stanzahandlers/MessageTest.php | 14 | ||||
-rw-r--r-- | tests/unit/stanzahandlers/PresenceTest.php | 27 |
11 files changed, 1045 insertions, 972 deletions
diff --git a/tests/unit/Middleware/ExternalApiMiddlewareTest.php b/tests/unit/Middleware/ExternalApiMiddlewareTest.php index 5af16ae..2fa90c4 100644 --- a/tests/unit/Middleware/ExternalApiMiddlewareTest.php +++ b/tests/unit/Middleware/ExternalApiMiddlewareTest.php @@ -10,142 +10,150 @@ use OCP\IRequest; use OCP\IConfig; use PHPUnit\Framework\TestCase; -class ExternalApiMiddlewareTest extends TestCase { - private $request; - private $config; - private $rawRequest; - private $externalApiMiddleware; - - public function setUp() { - parent::setUp(); - - $this->request = $this->createMock(IRequest::class); - $this->config = $this->createMock(IConfig::class); - $this->rawRequest = $this->createMock(RawRequest::class); - - $this->externalApiMiddleware = new ExternalApiMiddleware( - $this->request, - $this->config, - $this->rawRequest - ); - } - - public function testBeforeControllerWithoutHeader() { - $this->request - ->expects($this->once()) - ->method('getHeader') - ->with('X-JSXC-SIGNATURE') - ->willReturn(null); - - $this->expectException(SecurityException::class); - $this->expectExceptionMessage('HTTP header "X-JSXC-Signature" is missing.'); - - $controller = $this->createMock(SignatureProtectedApiController::class); - $this->externalApiMiddleware->beforeController($controller, 'someMethod'); - } - - public function testBeforeControllerWithoutSecret() { - $this->request - ->expects($this->once()) - ->method('getHeader') - ->with('X-JSXC-SIGNATURE') - ->willReturn('foo=bar'); - $this->config - ->expects($this->once()) - ->method('getAppValue') - ->with('ojsxc', 'apiSecret') - ->willReturn(null); - - $this->expectException(SecurityException::class); - $this->expectExceptionMessage('Missing secret.'); - - $controller = $this->createMock(SignatureProtectedApiController::class); - $this->externalApiMiddleware->beforeController($controller, 'someMethod'); - } - - public function testBeforeControllerWithUnsupportedAlgo() { - $this->request - ->expects($this->once()) - ->method('getHeader') - ->with('X-JSXC-SIGNATURE') - ->willReturn('foo=bar'); - $this->config - ->expects($this->once()) - ->method('getAppValue') - ->with('ojsxc', 'apiSecret') - ->willReturn('secret'); - - $this->expectException(SecurityException::class); - $this->expectExceptionMessage('Hash algorithm \'foo\' is not supported.'); - - $controller = $this->createMock(SignatureProtectedApiController::class); - $this->externalApiMiddleware->beforeController($controller, 'someMethod'); - } - - public function testBeforeControllerWithInvalidHeaderFormat() { - $this->request - ->expects($this->once()) - ->method('getHeader') - ->with('X-JSXC-SIGNATURE') - ->willReturn('foobar'); - $this->config - ->expects($this->once()) - ->method('getAppValue') - ->with('ojsxc', 'apiSecret') - ->willReturn('secret'); - - $this->expectException(SecurityException::class); - $this->expectExceptionMessage('Hash algorithm \'foobar\' is not supported.'); - - $controller = $this->createMock(SignatureProtectedApiController::class); - $this->externalApiMiddleware->beforeController($controller, 'someMethod'); - } - - public function testBeforeControllerWithInvalidHeader() { - $this->request - ->expects($this->once()) - ->method('getHeader') - ->with('X-JSXC-SIGNATURE') - ->willReturn('sha1=foobar'); - $this->config - ->expects($this->once()) - ->method('getAppValue') - ->with('ojsxc', 'apiSecret') - ->willReturn('secret'); - $this->rawRequest - ->expects($this->once()) - ->method('get') - ->willReturn('asdf'); - - $this->expectException(SecurityException::class); - $this->expectExceptionMessage('Signature does not match.'); - - $controller = $this->createMock(SignatureProtectedApiController::class); - $this->externalApiMiddleware->beforeController($controller, 'someMethod'); - } - - public function testBeforeControllerWithValidHeader() { - $algo = 'sha1'; - $apiSecret = 'secret'; - $rawRequestBody = 'rawRequestBody'; - $hash = hash_hmac($algo, $rawRequestBody, $apiSecret); - - $this->request - ->expects($this->once()) - ->method('getHeader') - ->with('X-JSXC-SIGNATURE') - ->willReturn($algo.'='.$hash); - $this->config - ->expects($this->once()) - ->method('getAppValue') - ->with('ojsxc', 'apiSecret') - ->willReturn($apiSecret); - $this->rawRequest - ->expects($this->once()) - ->method('get') - ->willReturn($rawRequestBody); - - $controller = $this->createMock(SignatureProtectedApiController::class); - $this->externalApiMiddleware->beforeController($controller, 'someMethod'); - } +class ExternalApiMiddlewareTest extends TestCase +{ + private $request; + private $config; + private $rawRequest; + private $externalApiMiddleware; + + public function setUp() + { + parent::setUp(); + + $this->request = $this->createMock(IRequest::class); + $this->config = $this->createMock(IConfig::class); + $this->rawRequest = $this->createMock(RawRequest::class); + + $this->externalApiMiddleware = new ExternalApiMiddleware( + $this->request, + $this->config, + $this->rawRequest + ); + } + + public function testBeforeControllerWithoutHeader() + { + $this->request + ->expects($this->once()) + ->method('getHeader') + ->with('X-JSXC-SIGNATURE') + ->willReturn(null); + + $this->expectException(SecurityException::class); + $this->expectExceptionMessage('HTTP header "X-JSXC-Signature" is missing.'); + + $controller = $this->createMock(SignatureProtectedApiController::class); + $this->externalApiMiddleware->beforeController($controller, 'someMethod'); + } + + public function testBeforeControllerWithoutSecret() + { + $this->request + ->expects($this->once()) + ->method('getHeader') + ->with('X-JSXC-SIGNATURE') + ->willReturn('foo=bar'); + $this->config + ->expects($this->once()) + ->method('getAppValue') + ->with('ojsxc', 'apiSecret') + ->willReturn(null); + + $this->expectException(SecurityException::class); + $this->expectExceptionMessage('Missing secret.'); + + $controller = $this->createMock(SignatureProtectedApiController::class); + $this->externalApiMiddleware->beforeController($controller, 'someMethod'); + } + + public function testBeforeControllerWithUnsupportedAlgo() + { + $this->request + ->expects($this->once()) + ->method('getHeader') + ->with('X-JSXC-SIGNATURE') + ->willReturn('foo=bar'); + $this->config + ->expects($this->once()) + ->method('getAppValue') + ->with('ojsxc', 'apiSecret') + ->willReturn('secret'); + + $this->expectException(SecurityException::class); + $this->expectExceptionMessage('Hash algorithm \'foo\' is not supported.'); + + $controller = $this->createMock(SignatureProtectedApiController::class); + $this->externalApiMiddleware->beforeController($controller, 'someMethod'); + } + + public function testBeforeControllerWithInvalidHeaderFormat() + { + $this->request + ->expects($this->once()) + ->method('getHeader') + ->with('X-JSXC-SIGNATURE') + ->willReturn('foobar'); + $this->config + ->expects($this->once()) + ->method('getAppValue') + ->with('ojsxc', 'apiSecret') + ->willReturn('secret'); + + $this->expectException(SecurityException::class); + $this->expectExceptionMessage('Hash algorithm \'foobar\' is not supported.'); + + $controller = $this->createMock(SignatureProtectedApiController::class); + $this->externalApiMiddleware->beforeController($controller, 'someMethod'); + } + + public function testBeforeControllerWithInvalidHeader() + { + $this->request + ->expects($this->once()) + ->method('getHeader') + ->with('X-JSXC-SIGNATURE') + ->willReturn('sha1=foobar'); + $this->config + ->expects($this->once()) + ->method('getAppValue') + ->with('ojsxc', 'apiSecret') + ->willReturn('secret'); + $this->rawRequest + ->expects($this->once()) + ->method('get') + ->willReturn('asdf'); + + $this->expectException(SecurityException::class); + $this->expectExceptionMessage('Signature does not match.'); + + $controller = $this->createMock(SignatureProtectedApiController::class); + $this->externalApiMiddleware->beforeController($controller, 'someMethod'); + } + + public function testBeforeControllerWithValidHeader() + { + $algo = 'sha1'; + $apiSecret = 'secret'; + $rawRequestBody = 'rawRequestBody'; + $hash = hash_hmac($algo, $rawRequestBody, $apiSecret); + + $this->request + ->expects($this->once()) + ->method('getHeader') + ->with('X-JSXC-SIGNATURE') + ->willReturn($algo.'='.$hash); + $this->config + ->expects($this->once()) + ->method('getAppValue') + ->with('ojsxc', 'apiSecret') + ->willReturn($apiSecret); + $this->rawRequest + ->expects($this->once()) + ->method('get') + ->willReturn($rawRequestBody); + + $controller = $this->createMock(SignatureProtectedApiController::class); + $this->externalApiMiddleware->beforeController($controller, 'someMethod'); + } } diff --git a/tests/unit/NewContentContainerTest.php b/tests/unit/NewContentContainerTest.php index e2caf39..ded45c3 100644 --- a/tests/unit/NewContentContainerTest.php +++ b/tests/unit/NewContentContainerTest.php @@ -6,18 +6,21 @@ use OCA\OJSXC\Db\Message; use OCA\OJSXC\Db\Stanza; use PHPUnit_Framework_TestCase; -class NewContentContainerTest extends PHPUnit_Framework_TestCase { +class NewContentContainerTest extends PHPUnit_Framework_TestCase +{ /** * @var NewContentContainer $newContentContainer */ private $newContentContainer; - public function setUp() { + public function setUp() + { $this->newContentContainer = new NewContentContainer(); } - public function testProvider() { + public function testProvider() + { $stanza1 = new Stanza(); $stanza1->setFrom('test@own.dev'); $stanza1->setTo('adsffdsst@own.dev'); @@ -38,7 +41,8 @@ class NewContentContainerTest extends PHPUnit_Framework_TestCase { /** * @dataProvider testProvider */ - public function test($stanzas, $count) { + public function test($stanzas, $count) + { foreach ($stanzas as $stanza) { $this->newContentContainer->addStanza($stanza); } @@ -46,8 +50,5 @@ class NewContentContainerTest extends PHPUnit_Framework_TestCase { $result = $this->newContentContainer->getStanzas(); $this->assertEquals(sort($stanzas), sort($result)); - } - - -}
\ No newline at end of file +} diff --git a/tests/unit/TimeLimitedTokenTest.php b/tests/unit/TimeLimitedTokenTest.php index f7ab789..7142728 100644 --- a/tests/unit/TimeLimitedTokenTest.php +++ b/tests/unit/TimeLimitedTokenTest.php @@ -4,43 +4,46 @@ namespace OCA\OJSXC; use PHPUnit\Framework\TestCase; -class TimeLimitedTokenTest extends TestCase { - public function testGenerateUser() { - $this->assertEquals( - 'AJP4Mvv5P8qZZJcENQhzfH$ruF%1458', - TimeLimitedToken::generateUser('foo', 'bar', 'secret', 60*60, 1500894607) - ); - - $this->assertEquals( - 'AELcjDTQjxEJptXaWb29gkt+LF%2Yi8', - TimeLimitedToken::generateUser('foo-bar', 'localhost.xyz', 'AJP4Mvv5P8', 60*60*10, 1500894607) - ); - - $this->assertEquals( - 'AEU+Upmh-jRtoHQ2Um1cYMcMV1%2Yi8', - TimeLimitedToken::generateUser('foo.bar', 'local.host.xyz', 'iiGTp+LF%2', 60*60*10, 1500894607) - ); - } - - public function testGenerateTURN() { - $this->assertEquals( - [(60*60 + 1500894607).':foobar', 'u66TdvZP9USnoCeOBFtVQa4DCkw='], - TimeLimitedToken::generateTURN('foobar', 'secret', 60*60, 1500894607) - ); - - $this->assertEquals( - [(3600 * 24 + 1500894607).':foo.bar', 'zfLkyJlJPx+KnLo5eLEUwJXDbGo='], - TimeLimitedToken::generateTURN('foo.bar', 'CeOBFtVQa', 3600 * 24, 1500894607) - ); - - $this->assertEquals( - [(3600 * 24 + 1500894607).':foo:bar', 'e+dKdn0JtGWccYCJ3NKaDUD6JZk='], - TimeLimitedToken::generateTURN('foo:bar', 'nLo5eLEUwJXD', 3600 * 24, 1500894607) - ); - - $this->assertEquals( - [(3600 * 24 + 1500894607).':foobar', 'q01XUfO0p37h5dGDd5R2PO2RhpM='], - TimeLimitedToken::generateTURN('foobar', 'nLo5eLEUwJXD', 3600 * 24, 1500894607) - ); - } +class TimeLimitedTokenTest extends TestCase +{ + public function testGenerateUser() + { + $this->assertEquals( + 'AJP4Mvv5P8qZZJcENQhzfH$ruF%1458', + TimeLimitedToken::generateUser('foo', 'bar', 'secret', 60 * 60, 1500894607) + ); + + $this->assertEquals( + 'AELcjDTQjxEJptXaWb29gkt+LF%2Yi8', + TimeLimitedToken::generateUser('foo-bar', 'localhost.xyz', 'AJP4Mvv5P8', 60 * 60 * 10, 1500894607) + ); + + $this->assertEquals( + 'AEU+Upmh-jRtoHQ2Um1cYMcMV1%2Yi8', + TimeLimitedToken::generateUser('foo.bar', 'local.host.xyz', 'iiGTp+LF%2', 60 * 60 * 10, 1500894607) + ); + } + + public function testGenerateTURN() + { + $this->assertEquals( + [(60 * 60 + 1500894607).':foobar', 'u66TdvZP9USnoCeOBFtVQa4DCkw='], + TimeLimitedToken::generateTURN('foobar', 'secret', 60 * 60, 1500894607) + ); + + $this->assertEquals( + [(3600 * 24 + 1500894607).':foo.bar', 'zfLkyJlJPx+KnLo5eLEUwJXDbGo='], + TimeLimitedToken::generateTURN('foo.bar', 'CeOBFtVQa', 3600 * 24, 1500894607) + ); + + $this->assertEquals( + [(3600 * 24 + 1500894607).':foo:bar', 'e+dKdn0JtGWccYCJ3NKaDUD6JZk='], + TimeLimitedToken::generateTURN('foo:bar', 'nLo5eLEUwJXD', 3600 * 24, 1500894607) + ); + + $this->assertEquals( + [(3600 * 24 + 1500894607).':foobar', 'q01XUfO0p37h5dGDd5R2PO2RhpM='], + TimeLimitedToken::generateTURN('foobar', 'nLo5eLEUwJXD', 3600 * 24, 1500894607) + ); + } } diff --git a/tests/unit/controller/ExternalApiControllerTest.php b/tests/unit/controller/ExternalApiControllerTest.php index 7f5c44d..aac2183 100644 --- a/tests/unit/controller/ExternalApiControllerTest.php +++ b/tests/unit/controller/ExternalApiControllerTest.php @@ -13,322 +13,339 @@ use OCP\IUser; use OCP\IGroup; use PHPUnit\Framework\TestCase; -class ExternalApiControllerTest extends TestCase { - private $request; - private $userManager; - private $userSession; - private $groupManager; - private $logger; - private $user; - private $externalApiController; - - public function setUp() { - parent::setUp(); - - $this->request = $this->createMock(IRequest::class); - $this->userManager = $this->createMock(IUserManager::class); - $this->userSession = $this->createMock(IUserSession::class); - $this->groupManager = $this->createMock(IGroupManager::class); - $this->logger = $this->createMock(ILogger::class); - $this->user = $this->createMock(IUser::class); - - $this->externalApiController = new ExternalApiController( - 'ojsxc', - $this->request, - $this->userManager, - $this->userSession, - $this->groupManager, - $this->logger - ); - } - - public function testSignatureProtected() { - $this->assertInstanceOf(SignatureProtectedApiController::class, $this->externalApiController); - } - - public function testCheckPasswordWithInvalidParams() { - $this->userSession - ->expects($this->once()) - ->method('login') - ->with('foo', 'bar') - ->willReturn(false); - - $return = $this->externalApiController->checkPassword('foo', 'bar'); - - $this->assertEquals('noauth', $return['result']); - } - - public function testCheckPasswordWithInvalidParamsAndDomain() { - $this->userSession - ->expects($this->exactly(2)) - ->method('login') - ->will($this->returnValueMap([ - ['foo@localhost', 'bar', false], - ['foo', 'bar', false] - ])); - - $return = $this->externalApiController->checkPassword('foo', 'bar', 'localhost'); - - $this->assertEquals('noauth', $return['result']); - } - - public function testCheckPasswordWithValidParams() { - $uid = 'foo'; - $this->user - ->expects($this->once()) - ->method('getUID') - ->willReturn($uid); - $this->userSession - ->expects($this->once()) - ->method('login') - ->with('foo', 'bar') - ->willReturn(true); - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->willReturn($this->user); - - $return = $this->externalApiController->checkPassword('foo', 'bar'); - - $this->assertEquals('success', $return['result']); - $this->assertEquals($uid, $return['data']['uid']); - } - - public function testCheckPasswordWithValidParamsAndDomain() { - $uid = 'foo'; - $this->user - ->expects($this->once()) - ->method('getUID') - ->willReturn($uid); - $this->userSession - ->expects($this->once()) - ->method('login') - ->with('foo@localhost', 'bar') - ->willReturn(true); - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->willReturn($this->user); - - $return = $this->externalApiController->checkPassword('foo', 'bar', 'localhost'); - - $this->assertEquals('success', $return['result']); - $this->assertEquals($uid, $return['data']['uid']); - } - - public function testIsUserFail() { - $this->userManager - ->expects($this->once()) - ->method('userExists') - ->with('foo') - ->willReturn(false); - - $return = $this->externalApiController->isUser('foo'); - - $this->assertEquals('success', $return['result']); - $this->assertEquals(false, $return['data']['isUser']); - } - - public function testIsUserFailWithDomain() { - $this->userManager - ->expects($this->exactly(2)) - ->method('userExists') - ->will($this->returnValueMap([ - ['foo@localhost', false], - ['foo', false] - ])); - - $return = $this->externalApiController->isUser('foo', 'localhost'); - - $this->assertEquals('success', $return['result']); - $this->assertEquals(false, $return['data']['isUser']); - } - - public function testIsUserSuccess() { - $this->userManager - ->expects($this->once()) - ->method('userExists') - ->with('foo') - ->willReturn(true); - - $return = $this->externalApiController->isUser('foo'); - - $this->assertEquals('success', $return['result']); - $this->assertEquals(true, $return['data']['isUser']); - } - - public function testIsUserSuccessWithDomain() { - $this->userManager - ->expects($this->once()) - ->method('userExists') - ->with('foo@localhost') - ->willReturn(true); - - $return = $this->externalApiController->isUser('foo', 'localhost'); - - $this->assertEquals('success', $return['result']); - $this->assertEquals(true, $return['data']['isUser']); - } - - public function testSharedRosterWithoutUsername() { - $this->expectException(\Exception::class); - $this->expectExceptionMessage('No username provided'); - - $this->externalApiController->sharedRoster(); - } - - public function testSharedRosterNoGroups() { - $user = $this->createUserMock('foobar'); - - $this->userManager - ->expects($this->once()) - ->method('get') - ->with($user->getUID()) - ->willReturn($user); - - $this->groupManager - ->expects($this->once()) - ->method('getUserGroups') - ->with($user) - ->willReturn([]); - - $return = $this->externalApiController->sharedRoster($user->getUID()); - - $this->assertEquals('success', $return['result']); - $this->assertEquals([], $return['data']['sharedRoster']); - } - - public function testSharedRosterMultipleDistinctGroups() { - $user = $this->createUserMock('foobar'); - - $this->userManager - ->expects($this->once()) - ->method('get') - ->with($user->getUID()) - ->willReturn($user); - - $this->groupManager - ->expects($this->once()) - ->method('getUserGroups') - ->with($user) - ->willReturn([ - $this->createGroupMock('Group1', ['Foo Bar', 'John Doo', 'user42']), - $this->createGroupMock('Group2', ['Fritz', 'John', 'Eve']) - ]); - - $expectedResult = [ - 'Foo-Bar' => [ - 'name' => 'Foo Bar', - 'groups' => ['Group1'] - ], - 'John-Doo' => [ - 'name' => 'John Doo', - 'groups' => ['Group1'] - ], - 'user42' => [ - 'name' => 'user42', - 'groups' => ['Group1'] - ], - 'Fritz' => [ - 'name' => 'Fritz', - 'groups' => ['Group2'] - ], - 'John' => [ - 'name' => 'John', - 'groups' => ['Group2'] - ], - 'Eve' => [ - 'name' => 'Eve', - 'groups' => ['Group2'] - ] - ]; - - $return = $this->externalApiController->sharedRoster($user->getUID()); - - $this->assertEquals('success', $return['result']); - - foreach($return['data']['sharedRoster'] as $key=>$value) { - $this->assertEquals($expectedResult[$key], $value); - } - } - - public function testSharedRosterMultipleOverlapGroups() { - $user = $this->createUserMock('foobar'); - - $this->userManager - ->expects($this->once()) - ->method('get') - ->with($user->getUID()) - ->willReturn($user); - - $this->groupManager - ->expects($this->once()) - ->method('getUserGroups') - ->with($user) - ->willReturn([ - $this->createGroupMock('Group1', ['Foo Bar', 'John Doo', 'user42']), - $this->createGroupMock('Group2', ['user42', 'John', 'Foo Bar']) - ]); - - $expectedResult = [ - 'Foo-Bar' => [ - 'name' => 'Foo Bar', - 'groups' => ['Group1', 'Group2'] - ], - 'John-Doo' => [ - 'name' => 'John Doo', - 'groups' => ['Group1'] - ], - 'user42' => [ - 'name' => 'user42', - 'groups' => ['Group1', 'Group2'] - ], - 'John' => [ - 'name' => 'John', - 'groups' => ['Group2'] - ] - ]; - - $return = $this->externalApiController->sharedRoster($user->getUID()); - - $this->assertEquals('success', $return['result']); - - foreach($return['data']['sharedRoster'] as $key=>$value) { - $this->assertEquals($expectedResult[$key], $value); - } - } - - private function createGroupMock($groupName, $displayNames = []) { - $users = []; - - foreach($displayNames as $displayName) { - $users[] = $this->createUserMock($displayName); - } - - $group = $this->createMock(IGroup::class); - - $group - ->method('getDisplayName') - ->willReturn($groupName); - - $group - ->method('getUsers') - ->willReturn($users); - - return $group; - } - - private function createUserMock($displayName) { - $user = $this->createMock(IUser::class); - - $user - ->method('getUID') - ->willReturn(preg_replace('/ /', '-', $displayName)); - - $user - ->method('getDisplayName') - ->willReturn($displayName); - - return $user; - } +class ExternalApiControllerTest extends TestCase +{ + private $request; + private $userManager; + private $userSession; + private $groupManager; + private $logger; + private $user; + private $externalApiController; + + public function setUp() + { + parent::setUp(); + + $this->request = $this->createMock(IRequest::class); + $this->userManager = $this->createMock(IUserManager::class); + $this->userSession = $this->createMock(IUserSession::class); + $this->groupManager = $this->createMock(IGroupManager::class); + $this->logger = $this->createMock(ILogger::class); + $this->user = $this->createMock(IUser::class); + + $this->externalApiController = new ExternalApiController( + 'ojsxc', + $this->request, + $this->userManager, + $this->userSession, + $this->groupManager, + $this->logger + ); + } + + public function testSignatureProtected() + { + $this->assertInstanceOf(SignatureProtectedApiController::class, $this->externalApiController); + } + + public function testCheckPasswordWithInvalidParams() + { + $this->userSession + ->expects($this->once()) + ->method('login') + ->with('foo', 'bar') + ->willReturn(false); + + $return = $this->externalApiController->checkPassword('foo', 'bar'); + + $this->assertEquals('noauth', $return['result']); + } + + public function testCheckPasswordWithInvalidParamsAndDomain() + { + $this->userSession + ->expects($this->exactly(2)) + ->method('login') + ->will($this->returnValueMap([ + ['foo@localhost', 'bar', false], + ['foo', 'bar', false] + ])); + + $return = $this->externalApiController->checkPassword('foo', 'bar', 'localhost'); + + $this->assertEquals('noauth', $return['result']); + } + + public function testCheckPasswordWithValidParams() + { + $uid = 'foo'; + $this->user + ->expects($this->once()) + ->method('getUID') + ->willReturn($uid); + $this->userSession + ->expects($this->once()) + ->method('login') + ->with('foo', 'bar') + ->willReturn(true); + $this->userSession + ->expects($this->once()) + ->method('getUser') + ->willReturn($this->user); + + $return = $this->externalApiController->checkPassword('foo', 'bar'); + + $this->assertEquals('success', $return['result']); + $this->assertEquals($uid, $return['data']['uid']); + } + + public function testCheckPasswordWithValidParamsAndDomain() + { + $uid = 'foo'; + $this->user + ->expects($this->once()) + ->method('getUID') + ->willReturn($uid); + $this->userSession + ->expects($this->once()) + ->method('login') + ->with('foo@localhost', 'bar') + ->willReturn(true); + $this->userSession + ->expects($this->once()) + ->method('getUser') + ->willReturn($this->user); + + $return = $this->externalApiController->checkPassword('foo', 'bar', 'localhost'); + + $this->assertEquals('success', $return['result']); + $this->assertEquals($uid, $return['data']['uid']); + } + + public function testIsUserFail() + { + $this->userManager + ->expects($this->once()) + ->method('userExists') + ->with('foo') + ->willReturn(false); + + $return = $this->externalApiController->isUser('foo'); + + $this->assertEquals('success', $return['result']); + $this->assertEquals(false, $return['data']['isUser']); + } + + public function testIsUserFailWithDomain() + { + $this->userManager + ->expects($this->exactly(2)) + ->method('userExists') + ->will($this->returnValueMap([ + ['foo@localhost', false], + ['foo', false] + ])); + + $return = $this->externalApiController->isUser('foo', 'localhost'); + + $this->assertEquals('success', $return['result']); + $this->assertEquals(false, $return['data']['isUser']); + } + + public function testIsUserSuccess() + { + $this->userManager + ->expects($this->once()) + ->method('userExists') + ->with('foo') + ->willReturn(true); + + $return = $this->externalApiController->isUser('foo'); + + $this->assertEquals('success', $return['result']); + $this->assertEquals(true, $return['data']['isUser']); + } + + public function testIsUserSuccessWithDomain() + { + $this->userManager + ->expects($this->once()) + ->method('userExists') + ->with('foo@localhost') + ->willReturn(true); + + $return = $this->externalApiController->isUser('foo', 'localhost'); + + $this->assertEquals('success', $return['result']); + $this->assertEquals(true, $return['data']['isUser']); + } + + public function testSharedRosterWithoutUsername() + { + $this->expectException(\Exception::class); + $this->expectExceptionMessage('No username provided'); + + $this->externalApiController->sharedRoster(); + } + + public function testSharedRosterNoGroups() + { + $user = $this->createUserMock('foobar'); + + $this->userManager + ->expects($this->once()) + ->method('get') + ->with($user->getUID()) + ->willReturn($user); + + $this->groupManager + ->expects($this->once()) + ->method('getUserGroups') + ->with($user) + ->willReturn([]); + + $return = $this->externalApiController->sharedRoster($user->getUID()); + + $this->assertEquals('success', $return['result']); + $this->assertEquals([], $return['data']['sharedRoster']); + } + + public function testSharedRosterMultipleDistinctGroups() + { + $user = $this->createUserMock('foobar'); + + $this->userManager + ->expects($this->once()) + ->method('get') + ->with($user->getUID()) + ->willReturn($user); + + $this->groupManager + ->expects($this->once()) + ->method('getUserGroups') + ->with($user) + ->willReturn([ + $this->createGroupMock('Group1', ['Foo Bar', 'John Doo', 'user42']), + $this->createGroupMock('Group2', ['Fritz', 'John', 'Eve']) + ]); + + $expectedResult = [ + 'Foo-Bar' => [ + 'name' => 'Foo Bar', + 'groups' => ['Group1'] + ], + 'John-Doo' => [ + 'name' => 'John Doo', + 'groups' => ['Group1'] + ], + 'user42' => [ + 'name' => 'user42', + 'groups' => ['Group1'] + ], + 'Fritz' => [ + 'name' => 'Fritz', + 'groups' => ['Group2'] + ], + 'John' => [ + 'name' => 'John', + 'groups' => ['Group2'] + ], + 'Eve' => [ + 'name' => 'Eve', + 'groups' => ['Group2'] + ] + ]; + + $return = $this->externalApiController->sharedRoster($user->getUID()); + + $this->assertEquals('success', $return['result']); + + foreach ($return['data']['sharedRoster'] as $key => $value) { + $this->assertEquals($expectedResult[$key], $value); + } + } + + public function testSharedRosterMultipleOverlapGroups() + { + $user = $this->createUserMock('foobar'); + + $this->userManager + ->expects($this->once()) + ->method('get') + ->with($user->getUID()) + ->willReturn($user); + + $this->groupManager + ->expects($this->once()) + ->method('getUserGroups') + ->with($user) + ->willReturn([ + $this->createGroupMock('Group1', ['Foo Bar', 'John Doo', 'user42']), + $this->createGroupMock('Group2', ['user42', 'John', 'Foo Bar']) + ]); + + $expectedResult = [ + 'Foo-Bar' => [ + 'name' => 'Foo Bar', + 'groups' => ['Group1', 'Group2'] + ], + 'John-Doo' => [ + 'name' => 'John Doo', + 'groups' => ['Group1'] + ], + 'user42' => [ + 'name' => 'user42', + 'groups' => ['Group1', 'Group2'] + ], + 'John' => [ + 'name' => 'John', + 'groups' => ['Group2'] + ] + ]; + + $return = $this->externalApiController->sharedRoster($user->getUID()); + + $this->assertEquals('success', $return['result']); + + foreach ($return['data']['sharedRoster'] as $key => $value) { + $this->assertEquals($expectedResult[$key], $value); + } + } + + private function createGroupMock($groupName, $displayNames = []) + { + $users = []; + + foreach ($displayNames as $displayName) { + $users[] = $this->createUserMock($displayName); + } + + $group = $this->createMock(IGroup::class); + + $group + ->method('getDisplayName') + ->willReturn($groupName); + + $group + ->method('getUsers') + ->willReturn($users); + + return $group; + } + + private function createUserMock($displayName) + { + $user = $this->createMock(IUser::class); + + $user + ->method('getUID') + ->willReturn(preg_replace('/ /', '-', $displayName)); + + $user + ->method('getDisplayName') + ->willReturn($displayName); + + return $user; + } } diff --git a/tests/unit/controller/HttpBindControllerTest.php b/tests/unit/controller/HttpBindControllerTest.php index 97bd035..c611292 100644 --- a/tests/unit/controller/HttpBindControllerTest.php +++ b/tests/unit/controller/HttpBindControllerTest.php @@ -13,8 +13,8 @@ use PHPUnit_Framework_TestCase; use Sabre\Xml\Writer; use PHPUnit_Framework_MockObject_MockObject; - -class HttpBindControllerTest extends PHPUnit_Framework_TestCase { +class HttpBindControllerTest extends PHPUnit_Framework_TestCase +{ /** * @var HttpBindController @@ -63,11 +63,13 @@ class HttpBindControllerTest extends PHPUnit_Framework_TestCase { */ private $stanzaLogger; - public function setUp() { + public function setUp() + { $this->stanzaLogger = $this->getMockBuilder('OCA\OJSXC\StanzaLogger')->disableOriginalConstructor()->getMock(); } - private function mockLock() { + private function mockLock() + { $this->lock->expects($this->any()) ->method('setLock') ->will($this->returnValue(null)); @@ -75,14 +77,15 @@ class HttpBindControllerTest extends PHPUnit_Framework_TestCase { $this->lock->expects($this->any()) ->method('stillLocked') ->will($this->returnValue(true)); - } + } /** * Helper function to set up the controller. This can't be done in the setUp, * since the requestBody is different for every test. * @param $requestBody */ - private function setUpController($requestBody) { + private function setUpController($requestBody) + { $request = $this->getMockBuilder('OCP\IRequest')->disableOriginalConstructor()->getMock(); $this->stanzaMapper = $this->getMockBuilder('OCA\OJSXC\Db\StanzaMapper')->disableOriginalConstructor()->getMock(); $this->presenceMapper = $this->getMockBuilder('OCA\OJSXC\Db\PresenceMapper')->disableOriginalConstructor()->getMock(); @@ -115,7 +118,8 @@ class HttpBindControllerTest extends PHPUnit_Framework_TestCase { ); } - public function testNewContentContainerNoNew() { + public function testNewContentContainerNoNew() + { $this->setUpController('<body xmlns=\'http://jabber.org/protocol/httpbind\'/>'); $this->mockLock(); $ex = new DoesNotExistException(''); @@ -139,10 +143,10 @@ class HttpBindControllerTest extends PHPUnit_Framework_TestCase { $response = $this->controller->index(); $this->assertEquals($expResponse, $response); - } - public function testNewContentContainerNoNewWithDbResults() { + public function testNewContentContainerNoNewWithDbResults() + { $result = new Stanza('test'); $this->setUpController('<body rid=\'897878797\' xmlns=\'http://jabber.org/protocol/httpbind\' sid=\'7862\'/>'); $this->mockLock(); @@ -156,7 +160,7 @@ class HttpBindControllerTest extends PHPUnit_Framework_TestCase { $r1 = $this->getMockBuilder('OCA\OJSXC\Db\Stanza')->disableOriginalConstructor()->getMock(); $r1->expects($this->once()) ->method('xmlSerialize') - ->will($this->returnCallback(function(Writer $writer){ + ->will($this->returnCallback(function (Writer $writer) { $writer->write('test'); })); @@ -182,7 +186,8 @@ class HttpBindControllerTest extends PHPUnit_Framework_TestCase { } - public function testNewContentContainerWithNewWithDbResults() { + public function testNewContentContainerWithNewWithDbResults() + { $result = new Stanza('test'); $this->setUpController('<body rid=\'897878797\' xmlns=\'http://jabber.org/protocol/httpbind\' sid=\'7862\'/>'); $this->mockLock(); @@ -196,7 +201,7 @@ class HttpBindControllerTest extends PHPUnit_Framework_TestCase { $r1 = $this->getMockBuilder('OCA\OJSXC\Db\Stanza')->disableOriginalConstructor()->getMock(); $r1->expects($this->once()) ->method('xmlSerialize') - ->will($this->returnCallback(function(Writer $writer){ + ->will($this->returnCallback(function (Writer $writer) { $writer->write('test'); })); @@ -213,7 +218,7 @@ class HttpBindControllerTest extends PHPUnit_Framework_TestCase { ->method('getCount') ->will($this->returnValue(5)); - $testStanza = new Stanza(); + $testStanza = new Stanza(); $testStanza->setFrom('derp@own.dev'); $testStanza->setTo('admin@own.dev'); @@ -239,12 +244,13 @@ class HttpBindControllerTest extends PHPUnit_Framework_TestCase { * {"reqId":"HmbEV6qTWF68ii1G\/kz1","remoteAddr":"","app":"PHP","message":"XMLReader::read(): An Error Occured while reading at \/var\/www\/owncloud\/apps\/ojsxc\/vendor\/sabre\/xml\/lib\/Reader.php#66","level":0,"time":"2016-01-30T14:52:44+00:00","method":"--","url":"--"} * {"reqId":"HmbEV6qTWF68ii1G\/kz1","remoteAddr":"","app":"PHP","message":"XMLReader::read(): An Error Occured while reading at \/var\/www\/owncloud\/apps\/ojsxc\/vendor\/sabre\/xml\/lib\/Reader.php#145","level":0,"time":"2016-01-30T14:52:44+00:00","method":"--","url":"--"} */ - public function testInvalidXML() { + public function testInvalidXML() + { $ex = new DoesNotExistException(''); $expResponse = new XMPPResponse($this->stanzaLogger); $this->setUpController('<x>'); - $this->mockLock(); + $this->mockLock(); $this->stanzaMapper->expects($this->exactly(10)) ->method('findByTo') ->with('john') @@ -258,7 +264,8 @@ class HttpBindControllerTest extends PHPUnit_Framework_TestCase { $this->assertEquals($expResponse, $response); } - public function IQProvider() { + public function IQProvider() + { $expStanza1 = new Stanza(); $expStanza1->setStanza('<iq to="admin@localhost" type="result" id="2:sendIQ"><query xmlns="jabber:iq:roster"><item jid="derp@localhost" name="derp"></item></query></iq><iq to="admin@localhost" type="result" id="2:sendIQ"><query xmlns="jabber:iq:roster"><item jid="derp@localhost" name="derp"></item></query></iq><iq to="admin@localhost" type="result" id="2:sendIQ"><query xmlns="jabber:iq:roster"><item jid="derp@localhost" name="derp"></item></query></iq>'); @@ -290,10 +297,11 @@ class HttpBindControllerTest extends PHPUnit_Framework_TestCase { /** * @dataProvider IQProvider */ - public function testIQHandlerWhenNoDbResults($body, $result, $expected, $pollCount, $handlerCount) { + public function testIQHandlerWhenNoDbResults($body, $result, $expected, $pollCount, $handlerCount) + { $ex = new DoesNotExistException(''); $this->setUpController($body); - $this->mockLock(); + $this->mockLock(); $expResponse = new XMPPResponse($this->stanzaLogger); $expResponse->write($expected); @@ -313,13 +321,13 @@ class HttpBindControllerTest extends PHPUnit_Framework_TestCase { $response = $this->controller->index(); $this->assertEquals($expResponse, $response); $this->assertEquals($expResponse->render(), $response->render()); - } - public function testDbResults() { + public function testDbResults() + { $result = new Stanza('test'); $this->setUpController('<body rid=\'897878797\' xmlns=\'http://jabber.org/protocol/httpbind\' sid=\'7862\'/>'); - $this->mockLock(); + $this->mockLock(); $expResponse = new XMPPResponse($this->stanzaLogger, $result); @@ -330,7 +338,7 @@ class HttpBindControllerTest extends PHPUnit_Framework_TestCase { $r1 = $this->getMockBuilder('OCA\OJSXC\Db\Stanza')->disableOriginalConstructor()->getMock(); $r1->expects($this->once()) ->method('xmlSerialize') - ->will($this->returnCallback(function(Writer $writer){ + ->will($this->returnCallback(function (Writer $writer) { $writer->write('test'); })); @@ -348,11 +356,12 @@ class HttpBindControllerTest extends PHPUnit_Framework_TestCase { $this->assertEquals($expResponse->render(), $response->render()); } - public function testMessageNoDbHandler() { + public function testMessageNoDbHandler() + { $body = '<body rid=\'897878959\' xmlns=\'http://jabber.org/protocol/httpbind\' sid=\'7862\'><message to=\'derp@own.dev\' type=\'chat\' id=\'1452960296859-msg\' xmlns=\'jabber:client\'><body>abc</body><request xmlns=\'urn:xmpp:receipts\'/></message></body>'; $ex = new DoesNotExistException(''); $this->setUpController($body); - $this->mockLock(); + $this->mockLock(); $expResponse = new XMPPResponse($this->stanzaLogger); @@ -374,7 +383,8 @@ class HttpBindControllerTest extends PHPUnit_Framework_TestCase { } - public function testMultipleMessageNoDbHandler() { + public function testMultipleMessageNoDbHandler() + { $body = <<<XML <body rid='897878959' xmlns='http://jabber.org/protocol/httpbind' sid='7862'> <message to='derp@own.dev' type='chat' id='1452960296859-msg' xmlns='jabber:client'><body>abc</body></message> @@ -402,7 +412,8 @@ XML; 'to' => 'derp@own.dev', 'type' => 'chat', 'id' => '1452960296859-msg', - ]]), + ]] + ), $this->equalTo([ 'name' => '{jabber:client}message', 'value' => [ '{jabber:client}body' => 'abc2', @@ -436,10 +447,11 @@ XML; $this->assertEquals($expResponse->render(), $response->render()); } - public function testMessageDbHandler() { + public function testMessageDbHandler() + { $body = '<body rid=\'897878959\' xmlns=\'http://jabber.org/protocol/httpbind\' sid=\'7862\'><message to=\'derp@own.dev\' type=\'chat\' id=\'1452960296859-msg\' xmlns=\'jabber:client\'><body>abc</body><request xmlns=\'urn:xmpp:receipts\'/></message></body>'; $this->setUpController($body); - $this->mockLock(); + $this->mockLock(); $expResponse = new XMPPResponse($this->stanzaLogger, new Stanza('test')); @@ -449,7 +461,7 @@ XML; $r1 = $this->getMockBuilder('OCA\OJSXC\Db\Stanza')->disableOriginalConstructor()->getMock(); $r1->expects($this->once()) ->method('xmlSerialize') - ->will($this->returnCallback(function(Writer $writer){ + ->will($this->returnCallback(function (Writer $writer) { $writer->write('test'); })); @@ -467,13 +479,14 @@ XML; $this->assertEquals($expResponse->render(), $response->render()); } - public function testPresenceReturnNothingHandler() { + public function testPresenceReturnNothingHandler() + { $body = "<body xmlns='http://jabber.org/protocol/httpbind'><presence xmlns='jabber:client'><show>chat</show></presence></body>"; $ex = new DoesNotExistException(''); $expResponse = new XMPPResponse($this->stanzaLogger); $this->setUpController($body); - $this->mockLock(); + $this->mockLock(); $this->presenceHandler->expects($this->once()) ->method('handle') @@ -491,10 +504,10 @@ XML; $response = $this->controller->index(); $this->assertEquals($expResponse, $response); $this->assertEquals($expResponse->render(), $response->render()); - } - public function testPresenceHandler() { + public function testPresenceHandler() + { $body = "<body xmlns='http://jabber.org/protocol/httpbind'><presence xmlns='jabber:client'><show>chat</show></presence></body>"; $ex = new DoesNotExistException(''); $expResponse = new XMPPResponse($this->stanzaLogger); @@ -533,14 +546,14 @@ XML; $response = $this->controller->index(); $this->assertEquals($expResponse, $response); $this->assertEquals($expResponse->render(), $response->render()); - } - public function testBodyHandler() { + public function testBodyHandler() + { $ex = new DoesNotExistException(''); $body = '<body rid=\'897878985\' xmlns=\'http://jabber.org/protocol/httpbind\' sid=\'7862\'/>'; $this->setUpController($body); - $this->mockLock(); + $this->mockLock(); $expResponse = new XMPPResponse($this->stanzaLogger); $this->stanzaMapper->expects($this->exactly(10)) @@ -556,5 +569,4 @@ XML; $this->assertEquals($expResponse, $response); $this->assertEquals($expResponse->render(), $response->render()); } - } diff --git a/tests/unit/controller/ManagedServerControllerTest.php b/tests/unit/controller/ManagedServerControllerTest.php index e9e65b3..af1d861 100644 --- a/tests/unit/controller/ManagedServerControllerTest.php +++ b/tests/unit/controller/ManagedServerControllerTest.php @@ -16,221 +16,234 @@ use PHPUnit\Framework\TestCase; class ManagedServerControllerTest extends TestCase { - private $request; - private $urlGenerator; - private $config; - private $userSession; - private $logger; - private $dataRetriever; - private $registrationUrl; - - private $apiUrl; - private $apiSecret; - private $userId; - - private $managedServerController; - - public function setUp() { - parent::setUp(); - - $this->request = $this->createMock(IRequest::class); - $this->urlGenerator = $this->createMock(IURLGenerator::class); - $this->config = $this->createMock(IConfig::class); - $this->userSession = $this->createMock(IUserSession::class); - $this->logger = $this->createMock(ILogger::class); - $this->dataRetriever = $this->createMock(IDataRetriever::class); - $this->registrationUrl = ''; - - $this->apiUrl = 'https://localhost/api'; - $this->apiSecret = 'dummySecret'; - $this->userId = 'dummyUser'; - - $this->urlGenerator - ->expects($this->once()) - ->method('linkToRouteAbsolute') - ->with('ojsxc.externalApi.index') - ->willReturn($this->apiUrl); - $this->config - ->expects($this->once()) - ->method('getAppValue') - ->with('ojsxc', 'apiSecret') - ->willReturn($this->apiSecret); - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->willReturn($this->createUserMock($this->userId)); - - $this->managedServerController = new ManagedServerController( - 'ojsxc', - $this->request, - $this->urlGenerator, - $this->config, - $this->userSession, - $this->logger, - $this->dataRetriever, - $this->registrationUrl - ); - } - - public function testRegisterWithoutPromoCode() { - $this->doSuccessfulRegister(); - } - - public function testRegisterWithPromoCode() { - $this->doSuccessfulRegister('asdflkj3j9sdjkfj3bas', 'asdflkj3j9sdjkfj3bas'); - } - - public function testRegisterWithInvalidPromotionCode() { - $this->doSuccessfulRegister('as3-xs<>#'); - } - - public function testRegisterWithUnavailableEndpoint() { - $this->logger - ->expects($this->once()) - ->method('warning') - ->with('RMS: Abort with message: Couldn\'t reach the registration server'); - $this->dataRetriever - ->expects($this->once()) - ->method('fetchUrl') - ->willReturn(['body' => false]); - - $return = $this->managedServerController->register(); - - $this->assertEquals('error', $return->getData()['result']); - $this->assertEquals(500, $return->getStatus()); - } - - public function testRegisterWithInvalidJson() { - $this->logger - ->expects($this->once()) - ->method('warning') - ->with('RMS: Abort with message: Couldn\'t parse the response. Response code: 123'); - $this->dataRetriever - ->expects($this->once()) - ->method('fetchUrl') - ->willReturn(['body' => '{"": "}', 'headers' => ['response_code' => 123]]); - - $return = $this->managedServerController->register(); - - $this->assertEquals('error', $return->getData()['result']); - $this->assertEquals(500, $return->getStatus()); - } - - public function testRegisterWithErrorResponse() { - $this->logger - ->expects($this->once()) - ->method('warning') - ->with('RMS: Abort with message: foobar'); - $this->logger - ->expects($this->once()) - ->method('info') - ->with('RMS: Response code: 123'); - $this->dataRetriever - ->expects($this->once()) - ->method('fetchUrl') - ->willReturn(['body' => '{"message": "foobar"}', 'headers' => ['response_code' => 123]]); - - $return = $this->managedServerController->register(); - - $this->assertEquals('error', $return->getData()['result']); - $this->assertEquals(500, $return->getStatus()); - } - - public function testRegisterWithBadBoshUrl() { - $this->doRegisterFailWithBoshUrl('http://localhost/http-bind'); - $this->setUp(); - $this->doRegisterFailWithBoshUrl('https://localhost/http-bind/foo'); - $this->setUp(); - $this->doRegisterFailWithBoshUrl('https://localhost/eval?/http-bind'); - $this->setUp(); - $this->doRegisterFailWithBoshUrl('https://localhost/eval#/http-bind'); - $this->setUp(); - $this->doRegisterFailWithBoshUrl('/localhost/http-bind'); - } - - public function testRegisterWithBadDomain() { - $this->doRegisterFailWithDomain('--this-is-no.domain'); - $this->setUp(); - $this->doRegisterFailWithDomain('foo bar'); - $this->setUp(); - $this->doRegisterFailWithDomain('localhost/bad'); - $this->setUp(); - $this->doRegisterFailWithDomain('local?host'); - $this->setUp(); - $this->doRegisterFailWithDomain('foo.'); - } - - private function doSuccessfulRegister($promotionCode = '', $expectedPromotionCode = null) { - $this->dataRetriever - ->expects($this->once()) - ->method('fetchUrl') - ->with($this->registrationUrl, [ - 'apiUrl' => $this->apiUrl, - 'apiSecret' => $this->apiSecret, - 'apiVersion' => 1, - 'userId' => $this->userId, - 'promotionCode' => $expectedPromotionCode - ]) - ->willReturn([ - 'body' => '{"boshUrl":"https://localhost/http-bind","domain":"localhost.xyz","externalServices":["https://localhost"]}', - 'headers' => ['response_code' => 200] - ]); - - $return = $this->managedServerController->register($promotionCode); - - $this->assertTrue(is_array($return), 'The return value is no array.'); - $this->assertEquals('success', $return['result']); - } - - private function doRegisterFailWithBoshUrl($boshUrl) { - $this->logger - ->method('warning') - ->with('RMS: Abort with message: Got a bad bosh URL'); - $this->dataRetriever - ->expects($this->once()) - ->method('fetchUrl') - ->willReturn([ - 'body' => '{"boshUrl": "'.$boshUrl.'"}', - 'headers' => ['response_code' => 200] - ]); - - $return = $this->managedServerController->register(); - - $this->assertInstanceOf(JSONResponse::class, $return); - $this->assertEquals('error', $return->getData()['result']); - $this->assertEquals(500, $return->getStatus()); - } - - private function doRegisterFailWithDomain($domain) { - $this->logger - ->method('warning') - ->with('RMS: Abort with message: Got a bad domain'); - $this->dataRetriever - ->expects($this->once()) - ->method('fetchUrl') - ->willReturn([ - 'body' => '{"boshUrl": "https://localhost/http-bind", "domain": "'.$domain.'"}', - 'headers' => ['response_code' => 200] - ]); - - $return = $this->managedServerController->register(); - - $this->assertInstanceOf(JSONResponse::class, $return); - $this->assertEquals('error', $return->getData()['result']); - $this->assertEquals(500, $return->getStatus()); - } - - private function createUserMock($displayName) { - $user = $this->createMock(IUser::class); - - $user - ->method('getUID') - ->willReturn(preg_replace('/ /', '-', $displayName)); - - $user - ->method('getDisplayName') - ->willReturn($displayName); - - return $user; - } + private $request; + private $urlGenerator; + private $config; + private $userSession; + private $logger; + private $dataRetriever; + private $registrationUrl; + + private $apiUrl; + private $apiSecret; + private $userId; + + private $managedServerController; + + public function setUp() + { + parent::setUp(); + + $this->request = $this->createMock(IRequest::class); + $this->urlGenerator = $this->createMock(IURLGenerator::class); + $this->config = $this->createMock(IConfig::class); + $this->userSession = $this->createMock(IUserSession::class); + $this->logger = $this->createMock(ILogger::class); + $this->dataRetriever = $this->createMock(IDataRetriever::class); + $this->registrationUrl = ''; + + $this->apiUrl = 'https://localhost/api'; + $this->apiSecret = 'dummySecret'; + $this->userId = 'dummyUser'; + + $this->urlGenerator + ->expects($this->once()) + ->method('linkToRouteAbsolute') + ->with('ojsxc.externalApi.index') + ->willReturn($this->apiUrl); + $this->config + ->expects($this->once()) + ->method('getAppValue') + ->with('ojsxc', 'apiSecret') + ->willReturn($this->apiSecret); + $this->userSession + ->expects($this->once()) + ->method('getUser') + ->willReturn($this->createUserMock($this->userId)); + + $this->managedServerController = new ManagedServerController( + 'ojsxc', + $this->request, + $this->urlGenerator, + $this->config, + $this->userSession, + $this->logger, + $this->dataRetriever, + $this->registrationUrl + ); + } + + public function testRegisterWithoutPromoCode() + { + $this->doSuccessfulRegister(); + } + + public function testRegisterWithPromoCode() + { + $this->doSuccessfulRegister('asdflkj3j9sdjkfj3bas', 'asdflkj3j9sdjkfj3bas'); + } + + public function testRegisterWithInvalidPromotionCode() + { + $this->doSuccessfulRegister('as3-xs<>#'); + } + + public function testRegisterWithUnavailableEndpoint() + { + $this->logger + ->expects($this->once()) + ->method('warning') + ->with('RMS: Abort with message: Couldn\'t reach the registration server'); + $this->dataRetriever + ->expects($this->once()) + ->method('fetchUrl') + ->willReturn(['body' => false]); + + $return = $this->managedServerController->register(); + + $this->assertEquals('error', $return->getData()['result']); + $this->assertEquals(500, $return->getStatus()); + } + + public function testRegisterWithInvalidJson() + { + $this->logger + ->expects($this->once()) + ->method('warning') + ->with('RMS: Abort with message: Couldn\'t parse the response. Response code: 123'); + $this->dataRetriever + ->expects($this->once()) + ->method('fetchUrl') + ->willReturn(['body' => '{"": "}', 'headers' => ['response_code' => 123]]); + + $return = $this->managedServerController->register(); + + $this->assertEquals('error', $return->getData()['result']); + $this->assertEquals(500, $return->getStatus()); + } + + public function testRegisterWithErrorResponse() + { + $this->logger + ->expects($this->once()) + ->method('warning') + ->with('RMS: Abort with message: foobar'); + $this->logger + ->expects($this->once()) + ->method('info') + ->with('RMS: Response code: 123'); + $this->dataRetriever + ->expects($this->once()) + ->method('fetchUrl') + ->willReturn(['body' => '{"message": "foobar"}', 'headers' => ['response_code' => 123]]); + + $return = $this->managedServerController->register(); + + $this->assertEquals('error', $return->getData()['result']); + $this->assertEquals(500, $return->getStatus()); + } + + public function testRegisterWithBadBoshUrl() + { + $this->doRegisterFailWithBoshUrl('http://localhost/http-bind'); + $this->setUp(); + $this->doRegisterFailWithBoshUrl('https://localhost/http-bind/foo'); + $this->setUp(); + $this->doRegisterFailWithBoshUrl('https://localhost/eval?/http-bind'); + $this->setUp(); + $this->doRegisterFailWithBoshUrl('https://localhost/eval#/http-bind'); + $this->setUp(); + $this->doRegisterFailWithBoshUrl('/localhost/http-bind'); + } + + public function testRegisterWithBadDomain() + { + $this->doRegisterFailWithDomain('--this-is-no.domain'); + $this->setUp(); + $this->doRegisterFailWithDomain('foo bar'); + $this->setUp(); + $this->doRegisterFailWithDomain('localhost/bad'); + $this->setUp(); + $this->doRegisterFailWithDomain('local?host'); + $this->setUp(); + $this->doRegisterFailWithDomain('foo.'); + } + + private function doSuccessfulRegister($promotionCode = '', $expectedPromotionCode = null) + { + $this->dataRetriever + ->expects($this->once()) + ->method('fetchUrl') + ->with($this->registrationUrl, [ + 'apiUrl' => $this->apiUrl, + 'apiSecret' => $this->apiSecret, + 'apiVersion' => 1, + 'userId' => $this->userId, + 'promotionCode' => $expectedPromotionCode + ]) + ->willReturn([ + 'body' => '{"boshUrl":"https://localhost/http-bind","domain":"localhost.xyz","externalServices":["https://localhost"]}', + 'headers' => ['response_code' => 200] + ]); + + $return = $this->managedServerController->register($promotionCode); + + $this->assertTrue(is_array($return), 'The return value is no array.'); + $this->assertEquals('success', $return['result']); + } + + private function doRegisterFailWithBoshUrl($boshUrl) + { + $this->logger + ->method('warning') + ->with('RMS: Abort with message: Got a bad bosh URL'); + $this->dataRetriever + ->expects($this->once()) + ->method('fetchUrl') + ->willReturn([ + 'body' => '{"boshUrl": "'.$boshUrl.'"}', + 'headers' => ['response_code' => 200] + ]); + + $return = $this->managedServerController->register(); + + $this->assertInstanceOf(JSONResponse::class, $return); + $this->assertEquals('error', $return->getData()['result']); + $this->assertEquals(500, $return->getStatus()); + } + + private function doRegisterFailWithDomain($domain) + { + $this->logger + ->method('warning') + ->with('RMS: Abort with message: Got a bad domain'); + $this->dataRetriever + ->expects($this->once()) + ->method('fetchUrl') + ->willReturn([ + 'body' => '{"boshUrl": "https://localhost/http-bind", "domain": "'.$domain.'"}', + 'headers' => ['response_code' => 200] + ]); + + $return = $this->managedServerController->register(); + + $this->assertInstanceOf(JSONResponse::class, $return); + $this->assertEquals('error', $return->getData()['result']); + $this->assertEquals(500, $return->getStatus()); + } + + private function createUserMock($displayName) + { + $user = $this->createMock(IUser::class); + + $user + ->method('getUID') + ->willReturn(preg_replace('/ /', '-', $displayName)); + + $user + ->method('getDisplayName') + ->willReturn($displayName); + + return $user; + } } diff --git a/tests/unit/controller/SettingsControllerTest.php b/tests/unit/controller/SettingsControllerTest.php index 29b1c72..3eda211 100644 --- a/tests/unit/controller/SettingsControllerTest.php +++ b/tests/unit/controller/SettingsControllerTest.php @@ -9,183 +9,197 @@ use OCP\IUser; use OCP\IUserSession; use PHPUnit\Framework\TestCase; -class SettingsControllerTest extends TestCase { - private $request; - private $config; - private $userManager; - private $userSession; - private $settingsController; - - public function setUp() { - parent::setUp(); - - $this->request = $this->createMock(IRequest::class); - $this->config = $this->createMock(IConfig::class); - $this->userManager = $this->createMock(IUserManager::class); - $this->userSession = $this->createMock(IUserSession::class); - - $this->settingsController = new SettingsController( - 'ojsxc', - $this->request, - $this->config, - $this->userManager, - $this->userSession - ); - } - - public function testIndexWithoutUser() { - $return = $this->settingsController->index(); - - $this->assertEquals('noauth', $return['result']); - } - - public function testIndexDefaultServerType() { - $this->expectsInternalServerSettings(null); - } - - public function testIndexServerTypeInternal() { - $this->expectsInternalServerSettings('internal'); - } - - public function testIndexPreferPersonalEmail() { - $mapGetAppValue = [ - ['ojsxc', 'serverType', null, 'external'], - ['ojsxc', 'xmppPreferMail', null, 'true'] - ]; - - $node = 'foobar'; - $domain = 'host'; - - $mapGetUserValue = [ - ['Foo', 'settings', 'email', '', $node.'@'.$domain] - ]; - - $this->setUpAuthenticatedIndex($mapGetAppValue, $mapGetUserValue); - - $return = $this->settingsController->index(); - - $this->assertEquals('success', $return['result']); - $this->assertEquals($node, $return['data']['xmpp']['username']); - $this->assertEquals($domain, $return['data']['xmpp']['domain']); - } - - public function testIndexTimeLimitedToken() { - $mapGetAppValue = [ - ['ojsxc', 'serverType', null, 'external'], - ['ojsxc', 'timeLimitedToken', null, 'true'], - ['ojsxc', 'xmppDomain', null, 'localhost'] - ]; - - $this->setUpAuthenticatedIndex($mapGetAppValue); - - $return = $this->settingsController->index(); - - $this->assertEquals('success', $return['result']); - $this->assertNotEquals(null, $return['data']['xmpp']['password']); - } - - public function testGetIceServersNoData() { - $this->setUpGetIceServers(); - - $return = $this->settingsController->getIceServers(); - - $this->assertEquals([], $return); - } - - public function testGetIceServersStoredDataWithPrefix() { - $this->setUpGetIceServers('turn:localhost', '12345', 'foobar', 'password', 'secret'); - - $return = $this->settingsController->getIceServers(); - - $this->assertEquals('12345', $return['ttl']); - $this->assertEquals('turn:localhost', $return['iceServers'][0]['urls'][0]); - $this->assertEquals('foobar', $return['iceServers'][0]['username']); - $this->assertEquals('password', $return['iceServers'][0]['credential']); - } - - public function testGetIceServersGeneratedToken() { - $ttl = 12345; - $this->setUpGetIceServers('turn:localhost', ''.$ttl, '', 'password', 'secret'); - - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->willReturn($this->createUserMock('Foo')); - - $return = $this->settingsController->getIceServers(); - - $this->assertEquals('12345', $return['ttl']); - $this->assertEquals('turn:localhost', $return['iceServers'][0]['urls'][0]); - - $username = $return['iceServers'][0]['username']; - list($validUntil, $uid) = explode(':', $username); - - $this->assertGreaterThan(time(), intval($validUntil)); - $this->assertLessThanOrEqual(time() + $ttl, intval($validUntil)); - $this->assertEquals('Foo', $uid); - $this->assertNotEquals('password', $return['iceServers'][0]['credential']); - $this->assertFalse(empty($return['iceServers'][0]['credential'])); - } - - private function expectsInternalServerSettings($serverType) { - $mapGetAppValue = [ - ['ojsxc', 'serverType', null, $serverType] - ]; - - $this->setUpAuthenticatedIndex($mapGetAppValue); - - $this->request - ->expects($this->once()) - ->method('getServerHost') - ->willReturn('localhost'); - - $return = $this->settingsController->index(); - - $this->assertEquals('success', $return['result']); - $this->assertEquals('internal', $return['data']['serverType']); - $this->assertEquals('localhost', $return['data']['adminSettings']['xmppDomain']); - } - - private function setUpAuthenticatedIndex($mapGetAppValue = [], $mapGetUserValue = []) { - $mapGetParam = [ - ['username', null, 'foo'], - ['password', null, 'bar'] - ]; - - $this->request->method('getParam')->will($this->returnValueMap($mapGetParam)); - $this->config->method('getAppValue')->will($this->returnValueMap($mapGetAppValue)); - $this->config->method('getUserValue')->will($this->returnValueMap($mapGetUserValue)); - - $this->userManager - ->expects($this->once()) - ->method('checkPassword') - ->with('foo', 'bar') - ->willReturn($this->createUserMock('Foo')); - } - - private function setUpGetIceServers($iceUrl = '', $iceTtl = '', $iceUsername = '', $iceCredential = '', $iceSecret = '') { - $mapGetAppValue = [ - ['ojsxc', 'iceSecret', null, $iceSecret], - ['ojsxc', 'iceTtl', 3600*24, $iceTtl], - ['ojsxc', 'iceUrl', null, $iceUrl], - ['ojsxc', 'iceUsername', '', $iceUsername], - ['ojsxc', 'iceCredential', '', $iceCredential] - ]; - - $this->config->method('getAppValue')->will($this->returnValueMap($mapGetAppValue)); - } - - private function createUserMock($displayName) { - $user = $this->createMock(IUser::class); - - $user - ->method('getUID') - ->willReturn(preg_replace('/ /', '-', $displayName)); - - $user - ->method('getDisplayName') - ->willReturn($displayName); - - return $user; - } +class SettingsControllerTest extends TestCase +{ + private $request; + private $config; + private $userManager; + private $userSession; + private $settingsController; + + public function setUp() + { + parent::setUp(); + + $this->request = $this->createMock(IRequest::class); + $this->config = $this->createMock(IConfig::class); + $this->userManager = $this->createMock(IUserManager::class); + $this->userSession = $this->createMock(IUserSession::class); + + $this->settingsController = new SettingsController( + 'ojsxc', + $this->request, + $this->config, + $this->userManager, + $this->userSession + ); + } + + public function testIndexWithoutUser() + { + $return = $this->settingsController->index(); + + $this->assertEquals('noauth', $return['result']); + } + + public function testIndexDefaultServerType() + { + $this->expectsInternalServerSettings(null); + } + + public function testIndexServerTypeInternal() + { + $this->expectsInternalServerSettings('internal'); + } + + public function testIndexPreferPersonalEmail() + { + $mapGetAppValue = [ + ['ojsxc', 'serverType', null, 'external'], + ['ojsxc', 'xmppPreferMail', null, 'true'] + ]; + + $node = 'foobar'; + $domain = 'host'; + + $mapGetUserValue = [ + ['Foo', 'settings', 'email', '', $node.'@'.$domain] + ]; + + $this->setUpAuthenticatedIndex($mapGetAppValue, $mapGetUserValue); + + $return = $this->settingsController->index(); + + $this->assertEquals('success', $return['result']); + $this->assertEquals($node, $return['data']['xmpp']['username']); + $this->assertEquals($domain, $return['data']['xmpp']['domain']); + } + + public function testIndexTimeLimitedToken() + { + $mapGetAppValue = [ + ['ojsxc', 'serverType', null, 'external'], + ['ojsxc', 'timeLimitedToken', null, 'true'], + ['ojsxc', 'xmppDomain', null, 'localhost'] + ]; + + $this->setUpAuthenticatedIndex($mapGetAppValue); + + $return = $this->settingsController->index(); + + $this->assertEquals('success', $return['result']); + $this->assertNotEquals(null, $return['data']['xmpp']['password']); + } + + public function testGetIceServersNoData() + { + $this->setUpGetIceServers(); + + $return = $this->settingsController->getIceServers(); + + $this->assertEquals([], $return); + } + + public function testGetIceServersStoredDataWithPrefix() + { + $this->setUpGetIceServers('turn:localhost', '12345', 'foobar', 'password', 'secret'); + + $return = $this->settingsController->getIceServers(); + + $this->assertEquals('12345', $return['ttl']); + $this->assertEquals('turn:localhost', $return['iceServers'][0]['urls'][0]); + $this->assertEquals('foobar', $return['iceServers'][0]['username']); + $this->assertEquals('password', $return['iceServers'][0]['credential']); + } + + public function testGetIceServersGeneratedToken() + { + $ttl = 12345; + $this->setUpGetIceServers('turn:localhost', ''.$ttl, '', 'password', 'secret'); + + $this->userSession + ->expects($this->once()) + ->method('getUser') + ->willReturn($this->createUserMock('Foo')); + + $return = $this->settingsController->getIceServers(); + + $this->assertEquals('12345', $return['ttl']); + $this->assertEquals('turn:localhost', $return['iceServers'][0]['urls'][0]); + + $username = $return['iceServers'][0]['username']; + list($validUntil, $uid) = explode(':', $username); + + $this->assertGreaterThan(time(), intval($validUntil)); + $this->assertLessThanOrEqual(time() + $ttl, intval($validUntil)); + $this->assertEquals('Foo', $uid); + $this->assertNotEquals('password', $return['iceServers'][0]['credential']); + $this->assertFalse(empty($return['iceServers'][0]['credential'])); + } + + private function expectsInternalServerSettings($serverType) + { + $mapGetAppValue = [ + ['ojsxc', 'serverType', null, $serverType] + ]; + + $this->setUpAuthenticatedIndex($mapGetAppValue); + + $this->request + ->expects($this->once()) + ->method('getServerHost') + ->willReturn('localhost'); + + $return = $this->settingsController->index(); + + $this->assertEquals('success', $return['result']); + $this->assertEquals('internal', $return['data']['serverType']); + $this->assertEquals('localhost', $return['data']['adminSettings']['xmppDomain']); + } + + private function setUpAuthenticatedIndex($mapGetAppValue = [], $mapGetUserValue = []) + { + $mapGetParam = [ + ['username', null, 'foo'], + ['password', null, 'bar'] + ]; + + $this->request->method('getParam')->will($this->returnValueMap($mapGetParam)); + $this->config->method('getAppValue')->will($this->returnValueMap($mapGetAppValue)); + $this->config->method('getUserValue')->will($this->returnValueMap($mapGetUserValue)); + + $this->userManager + ->expects($this->once()) + ->method('checkPassword') + ->with('foo', 'bar') + ->willReturn($this->createUserMock('Foo')); + } + + private function setUpGetIceServers($iceUrl = '', $iceTtl = '', $iceUsername = '', $iceCredential = '', $iceSecret = '') + { + $mapGetAppValue = [ + ['ojsxc', 'iceSecret', null, $iceSecret], + ['ojsxc', 'iceTtl', 3600 * 24, $iceTtl], + ['ojsxc', 'iceUrl', null, $iceUrl], + ['ojsxc', 'iceUsername', '', $iceUsername], + ['ojsxc', 'iceCredential', '', $iceCredential] + ]; + + $this->config->method('getAppValue')->will($this->returnValueMap($mapGetAppValue)); + } + + private function createUserMock($displayName) + { + $user = $this->createMock(IUser::class); + + $user + ->method('getUID') + ->willReturn(preg_replace('/ /', '-', $displayName)); + + $user + ->method('getDisplayName') + ->willReturn($displayName); + + return $user; + } } diff --git a/tests/unit/http/XMPPResponseTest.php b/tests/unit/http/XMPPResponseTest.php index ffc3788..94cf1bc 100644 --- a/tests/unit/http/XMPPResponseTest.php +++ b/tests/unit/http/XMPPResponseTest.php @@ -1,7 +1,8 @@ <?php namespace OCA\OJSXC\Db { - function uniqid() { + function uniqid() + { return 4; // chosen by fair dice roll. // guaranteed to be unique. } @@ -13,10 +14,10 @@ namespace OCA\OJSXC\Http { use OCA\OJSXC\Db\Stanza; use PHPUnit_Framework_TestCase; - - class XMPPResponseTest extends PHPUnit_Framework_TestCase { - - public function writingProvider() { + class XMPPResponseTest extends PHPUnit_Framework_TestCase + { + public function writingProvider() + { $stanza1 = new Stanza(); $stanza1->setFrom('test@test.be'); $stanza1->setTo('test.be'); @@ -50,9 +51,10 @@ namespace OCA\OJSXC\Http { } /** - * @dataProvider writingProvider - */ - public function testWriting($stanzas, $expected) { + * @dataProvider writingProvider + */ + public function testWriting($stanzas, $expected) + { $stanzaLogger = $this->getMockBuilder('OCA\OJSXC\StanzaLogger')->disableOriginalConstructor()->getMock(); $response = new XMPPResponse($stanzaLogger); foreach ($stanzas as $stanza) { @@ -61,7 +63,6 @@ namespace OCA\OJSXC\Http { $result = $response->render(); $this->assertEquals($expected, $result); } - } -}
\ No newline at end of file +} diff --git a/tests/unit/stanzahandlers/IQTest.php b/tests/unit/stanzahandlers/IQTest.php index 9fddef2..8ab22be 100644 --- a/tests/unit/stanzahandlers/IQTest.php +++ b/tests/unit/stanzahandlers/IQTest.php @@ -6,8 +6,8 @@ use OCA\OJSXC\Db\IQRoster; use PHPUnit_Framework_MockObject_MockObject; use PHPUnit_Framework_TestCase; - -class IQTest extends PHPUnit_Framework_TestCase { +class IQTest extends PHPUnit_Framework_TestCase +{ /** * @var IQ $iq @@ -29,14 +29,16 @@ class IQTest extends PHPUnit_Framework_TestCase { */ private $host; - public function setUp() { + public function setUp() + { $this->host = 'localhost'; $this->userId = 'john'; $this->userManager = $this->getMockBuilder('OCP\IUserManager')->disableOriginalConstructor()->getMock(); $this->iq = new IQ($this->userId, $this->host, $this->userManager); } - public function iqRosterProvider() { + public function iqRosterProvider() + { $user1 = $this->getMockBuilder('OCP\IUser')->disableOriginalConstructor()->getMock(); $user1->expects($this->any()) ->method('getUID') @@ -140,7 +142,8 @@ class IQTest extends PHPUnit_Framework_TestCase { /** * @dataProvider iqRosterProvider */ - public function testIqRoster(array $stanza, array $users, $searchCount, $expected) { + public function testIqRoster(array $stanza, array $users, $searchCount, $expected) + { $this->userManager->expects($searchCount) ->method('search') ->with('') @@ -149,7 +152,6 @@ class IQTest extends PHPUnit_Framework_TestCase { $result = $this->iq->handle($stanza); if ($expected instanceof IQRoster) { - $this->assertEquals($expected->getFrom(), $result->getFrom()); $this->assertEquals($expected->getId(), $result->getId()); $this->assertEquals($expected->getItems(), $result->getItems()); @@ -161,5 +163,4 @@ class IQTest extends PHPUnit_Framework_TestCase { $this->assertEquals($expected, $result); } } - } diff --git a/tests/unit/stanzahandlers/MessageTest.php b/tests/unit/stanzahandlers/MessageTest.php index 1ac3577..01670d0 100644 --- a/tests/unit/stanzahandlers/MessageTest.php +++ b/tests/unit/stanzahandlers/MessageTest.php @@ -6,7 +6,8 @@ use OCA\OJSXC\Db\Message as MessageEntity; use PHPUnit_Framework_TestCase; use PHPUnit_Framework_MockObject_MockObject; -class MessageTest extends PHPUnit_Framework_TestCase { +class MessageTest extends PHPUnit_Framework_TestCase +{ /** * @var Message $message @@ -28,14 +29,16 @@ class MessageTest extends PHPUnit_Framework_TestCase { */ private $host; - public function setUp() { + public function setUp() + { $this->host = 'localhost'; $this->userId = 'john'; $this->messageMapper = $this->getMockBuilder('OCA\OJSXC\Db\MessageMapper')->disableOriginalConstructor()->getMock(); $this->message = new Message($this->userId, $this->host, $this->messageMapper); } - public function messageProvider(){ + public function messageProvider() + { $values = [ [ "name" => "body", @@ -78,13 +81,12 @@ class MessageTest extends PHPUnit_Framework_TestCase { /** * @dataProvider messageProvider */ - public function testMessage(array $stanza, $expected) { + public function testMessage(array $stanza, $expected) + { $this->messageMapper->expects($this->once()) ->method('insert') ->with($expected); $this->message->handle($stanza); - } - } diff --git a/tests/unit/stanzahandlers/PresenceTest.php b/tests/unit/stanzahandlers/PresenceTest.php index 1c4e6aa..1f24f35 100644 --- a/tests/unit/stanzahandlers/PresenceTest.php +++ b/tests/unit/stanzahandlers/PresenceTest.php @@ -7,9 +7,8 @@ use OCA\OJSXC\Db\Presence as PresenceEntity; use PHPUnit_Framework_MockObject_MockObject; use PHPUnit_Framework_TestCase; - -class PresenceTest extends PHPUnit_Framework_TestCase { - +class PresenceTest extends PHPUnit_Framework_TestCase +{ private $host; private $userId; @@ -29,17 +28,18 @@ class PresenceTest extends PHPUnit_Framework_TestCase { */ private $presence; - public function setUp() { + public function setUp() + { $this->host = 'localhost'; $this->userId = 'john'; $this->presenceMapper = $this->getMockBuilder('OCA\OJSXC\Db\PresenceMapper')->disableOriginalConstructor()->getMock(); $this->messageMapper = $this->getMockBuilder('OCA\OJSXC\Db\MessageMapper')->disableOriginalConstructor()->getMock(); - $this->presence = new Presence($this->userId, $this->host - , $this->presenceMapper, $this->messageMapper); + $this->presence = new Presence($this->userId, $this->host, $this->presenceMapper, $this->messageMapper); } - public function handleProvider() { + public function handleProvider() + { $presence = new PresenceEntity(); $presence->setPresence('online'); $presence->setUserid('john'); @@ -68,8 +68,8 @@ class PresenceTest extends PHPUnit_Framework_TestCase { /** * @dataProvider handleProvider */ - public function testHandle($presenceEntity, $connectedUsers, $presences, $insert) { - + public function testHandle($presenceEntity, $connectedUsers, $presences, $insert) + { $this->presenceMapper->expects($this->once()) ->method('setPresence') ->with($presenceEntity); @@ -97,7 +97,8 @@ class PresenceTest extends PHPUnit_Framework_TestCase { } - public function unavailableHandleProvider() { + public function unavailableHandleProvider() + { $presence = new PresenceEntity(); $presence->setPresence('unavailable'); $presence->setUserid('john'); @@ -127,8 +128,8 @@ class PresenceTest extends PHPUnit_Framework_TestCase { /** * @dataProvider UnavailableHandleProvider */ - public function testUnavailableHandle($presenceEntity, $connectedUsers, $presences, $insert) { - + public function testUnavailableHandle($presenceEntity, $connectedUsers, $presences, $insert) + { $this->presenceMapper->expects($this->once()) ->method('setPresence') ->with($presenceEntity); @@ -151,4 +152,4 @@ class PresenceTest extends PHPUnit_Framework_TestCase { $result = $this->presence->handle($presenceEntity); $this->assertEquals($presences, $result); } -}
\ No newline at end of file +} |