diff options
author | Lukas Reschke <lukas@statuscode.ch> | 2016-08-15 21:55:09 +0200 |
---|---|---|
committer | Lukas Reschke <lukas@statuscode.ch> | 2016-08-15 21:55:09 +0200 |
commit | 6c6338b81072ba58b662d2f03cab6cfdbfdf4a18 (patch) | |
tree | 7e5bf139a601f03dc7c458d7ef84881e63b5ee20 /tests | |
parent | 7ffb7b0d846241c8cc6b45a3c85b2374c6d026ae (diff) | |
parent | 2061d40ba292d2b906132dfcd261e4c631d9854b (diff) | |
download | nextcloud-server-6c6338b81072ba58b662d2f03cab6cfdbfdf4a18.tar.gz nextcloud-server-6c6338b81072ba58b662d2f03cab6cfdbfdf4a18.zip |
Merge branch 'master' into implement_712
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Core/Controller/AvatarControllerTest.php | 40 | ||||
-rw-r--r-- | tests/Core/Controller/LoginControllerTest.php | 128 | ||||
-rw-r--r-- | tests/Settings/Controller/AuthSettingsControllerTest.php | 1 | ||||
-rw-r--r-- | tests/Settings/Controller/CheckSetupControllerTest.php | 86 | ||||
-rw-r--r-- | tests/phpunit-autotest.xml | 5 | ||||
-rw-r--r-- | tests/phpunit.xml.dist | 5 |
6 files changed, 163 insertions, 102 deletions
diff --git a/tests/Core/Controller/AvatarControllerTest.php b/tests/Core/Controller/AvatarControllerTest.php index 937d8aaf17b..d45d0618230 100644 --- a/tests/Core/Controller/AvatarControllerTest.php +++ b/tests/Core/Controller/AvatarControllerTest.php @@ -19,6 +19,16 @@ * */ +namespace OC\Core\Controller; + +/** + * Overwrite is_uploaded_file in the OC\Core\Controller namespace to allow + * proper unit testing of the postAvatar call. + */ +function is_uploaded_file($filename) { + return file_exists($filename); +} + namespace Tests\Core\Controller; use OC\Core\Application; @@ -32,14 +42,6 @@ use Punic\Exception; use Test\Traits\UserTrait; /** - * Overwrite is_uploaded_file in this namespace to allow proper unit testing of - * the postAvatar call. - */ -function is_uploaded_file($filename) { - return file_exists($filename); -} - -/** * Class AvatarControllerTest * * @group DB @@ -68,19 +70,19 @@ class AvatarControllerTest extends \Test\TestCase { $app = new Application; $this->container = $app->getContainer(); $this->container['AppName'] = 'core'; - $this->container['AvatarManager'] = $this->getMock('OCP\IAvatarManager'); + $this->container['AvatarManager'] = $this->getMockBuilder('OCP\IAvatarManager')->getMock(); $this->container['Cache'] = $this->getMockBuilder('OC\Cache\File') ->disableOriginalConstructor()->getMock(); - $this->container['L10N'] = $this->getMock('OCP\IL10N'); + $this->container['L10N'] = $this->getMockBuilder('OCP\IL10N')->getMock(); $this->container['L10N']->method('t')->will($this->returnArgument(0)); - $this->container['UserManager'] = $this->getMock('OCP\IUserManager'); - $this->container['UserSession'] = $this->getMock('OCP\IUserSession'); - $this->container['Request'] = $this->getMock('OCP\IRequest'); - $this->container['UserFolder'] = $this->getMock('OCP\Files\Folder'); - $this->container['Logger'] = $this->getMock('OCP\ILogger'); + $this->container['UserManager'] = $this->getMockBuilder('OCP\IUserManager')->getMock(); + $this->container['UserSession'] = $this->getMockBuilder('OCP\IUserSession')->getMock(); + $this->container['Request'] = $this->getMockBuilder('OCP\IRequest')->getMock(); + $this->container['UserFolder'] = $this->getMockBuilder('OCP\Files\Folder')->getMock(); + $this->container['Logger'] = $this->getMockBuilder('OCP\ILogger')->getMock(); - $this->avatarMock = $this->getMock('OCP\IAvatar'); - $this->userMock = $this->getMock('OCP\IUser'); + $this->avatarMock = $this->getMockBuilder('OCP\IAvatar')->getMock(); + $this->userMock = $this->getMockBuilder('OCP\IUser')->getMock(); $this->avatarController = $this->container['AvatarController']; @@ -91,7 +93,7 @@ class AvatarControllerTest extends \Test\TestCase { ->willReturnMap([['userId', $this->userMock]]); $this->container['UserSession']->method('getUser')->willReturn($this->userMock); - $this->avatarFile = $this->getMock('OCP\Files\File'); + $this->avatarFile = $this->getMockBuilder('OCP\Files\File')->getMock(); $this->avatarFile->method('getContent')->willReturn('image data'); $this->avatarFile->method('getMimeType')->willReturn('image type'); $this->avatarFile->method('getEtag')->willReturn('my etag'); @@ -326,7 +328,7 @@ class AvatarControllerTest extends \Test\TestCase { * Test posting avatar from existing folder */ public function testPostAvatarFromNoFile() { - $file = $this->getMock('OCP\Files\Node'); + $file = $this->getMockBuilder('OCP\Files\Node')->getMock(); $this->container['UserFolder'] ->method('get') ->with('folder') diff --git a/tests/Core/Controller/LoginControllerTest.php b/tests/Core/Controller/LoginControllerTest.php index f09f3c98118..8eaa7c9843b 100644 --- a/tests/Core/Controller/LoginControllerTest.php +++ b/tests/Core/Controller/LoginControllerTest.php @@ -57,14 +57,14 @@ class LoginControllerTest extends TestCase { public function setUp() { parent::setUp(); - $this->request = $this->getMock('\\OCP\\IRequest'); - $this->userManager = $this->getMock('\\OCP\\IUserManager'); - $this->config = $this->getMock('\\OCP\\IConfig'); - $this->session = $this->getMock('\\OCP\\ISession'); + $this->request = $this->getMockBuilder('\\OCP\\IRequest')->getMock(); + $this->userManager = $this->getMockBuilder('\\OCP\\IUserManager')->getMock(); + $this->config = $this->getMockBuilder('\\OCP\\IConfig')->getMock(); + $this->session = $this->getMockBuilder('\\OCP\\ISession')->getMock(); $this->userSession = $this->getMockBuilder('\\OC\\User\\Session') ->disableOriginalConstructor() ->getMock(); - $this->urlGenerator = $this->getMock('\\OCP\\IURLGenerator'); + $this->urlGenerator = $this->getMockBuilder('\\OCP\\IURLGenerator')->getMock(); $this->twoFactorManager = $this->getMockBuilder('\OC\Authentication\TwoFactorAuth\Manager') ->disableOriginalConstructor() ->getMock(); @@ -110,7 +110,7 @@ class LoginControllerTest extends TestCase { ->method('getCookie') ->with('oc_token') ->willReturn('MyLoginToken'); - $user = $this->getMock('\\OCP\\IUser'); + $user = $this->getMockBuilder('\\OCP\\IUser')->getMock(); $user ->expects($this->once()) ->method('getUID') @@ -181,6 +181,7 @@ class LoginControllerTest extends TestCase { 'alt_login' => [], 'rememberLoginAllowed' => \OC_Util::rememberLoginAllowed(), 'rememberLoginState' => 0, + 'resetPasswordLink' => null, ], 'guest' ); @@ -217,7 +218,7 @@ class LoginControllerTest extends TestCase { ->method('getSystemValue') ->with('lost_password_link') ->willReturn(false); - $user = $this->getMock('\\OCP\\IUser'); + $user = $this->getMockBuilder('\\OCP\\IUser')->getMock(); $user ->expects($this->once()) ->method('canChangePassword') @@ -239,6 +240,7 @@ class LoginControllerTest extends TestCase { 'alt_login' => [], 'rememberLoginAllowed' => \OC_Util::rememberLoginAllowed(), 'rememberLoginState' => 0, + 'resetPasswordLink' => false, ], 'guest' ); @@ -255,7 +257,7 @@ class LoginControllerTest extends TestCase { ->method('getSystemValue') ->with('lost_password_link') ->willReturn(false); - $user = $this->getMock('\\OCP\\IUser'); + $user = $this->getMockBuilder('\\OCP\\IUser')->getMock(); $user ->expects($this->once()) ->method('canChangePassword') @@ -277,6 +279,7 @@ class LoginControllerTest extends TestCase { 'alt_login' => [], 'rememberLoginAllowed' => \OC_Util::rememberLoginAllowed(), 'rememberLoginState' => 0, + 'resetPasswordLink' => false, ], 'guest' ); @@ -292,6 +295,10 @@ class LoginControllerTest extends TestCase { ->expects($this->exactly(4)) ->method('getRemoteAddress') ->willReturn('192.168.0.1'); + $this->request + ->expects($this->once()) + ->method('passesCSRFCheck') + ->willReturn(true); $this->throttler ->expects($this->exactly(2)) ->method('sleepDelay') @@ -322,14 +329,18 @@ class LoginControllerTest extends TestCase { public function testLoginWithValidCredentials() { /** @var IUser | \PHPUnit_Framework_MockObject_MockObject $user */ - $user = $this->getMock('\OCP\IUser'); + $user = $this->getMockBuilder('\OCP\IUser')->getMock(); $password = 'secret'; - $indexPageUrl = 'some url'; + $indexPageUrl = \OC_Util::getDefaultPageUrl(); $this->request ->expects($this->exactly(2)) ->method('getRemoteAddress') ->willReturn('192.168.0.1'); + $this->request + ->expects($this->once()) + ->method('passesCSRFCheck') + ->willReturn(true); $this->throttler ->expects($this->once()) ->method('sleepDelay') @@ -352,18 +363,89 @@ class LoginControllerTest extends TestCase { ->method('isTwoFactorAuthenticated') ->with($user) ->will($this->returnValue(false)); - $this->urlGenerator->expects($this->once()) - ->method('linkToRoute') - ->with('files.view.index') - ->will($this->returnValue($indexPageUrl)); $expected = new \OCP\AppFramework\Http\RedirectResponse($indexPageUrl); $this->assertEquals($expected, $this->loginController->tryLogin($user, $password, null)); } + public function testLoginWithoutPassedCsrfCheckAndNotLoggedIn() { + /** @var IUser | \PHPUnit_Framework_MockObject_MockObject $user */ + $user = $this->getMockBuilder('\OCP\IUser')->getMock(); + $user->expects($this->any()) + ->method('getUID') + ->will($this->returnValue('jane')); + $password = 'secret'; + $originalUrl = 'another%20url'; + + $this->request + ->expects($this->exactly(2)) + ->method('getRemoteAddress') + ->willReturn('192.168.0.1'); + $this->request + ->expects($this->once()) + ->method('passesCSRFCheck') + ->willReturn(false); + $this->throttler + ->expects($this->once()) + ->method('sleepDelay') + ->with('192.168.0.1'); + $this->throttler + ->expects($this->once()) + ->method('getDelay') + ->with('192.168.0.1') + ->willReturn(200); + $this->userSession->expects($this->once()) + ->method('isLoggedIn') + ->with() + ->will($this->returnValue(false)); + + $expected = new \OCP\AppFramework\Http\RedirectResponse(\OC_Util::getDefaultPageUrl()); + $this->assertEquals($expected, $this->loginController->tryLogin('Jane', $password, $originalUrl)); + } + + public function testLoginWithoutPassedCsrfCheckAndLoggedIn() { + /** @var IUser | \PHPUnit_Framework_MockObject_MockObject $user */ + $user = $this->getMockBuilder('\OCP\IUser')->getMock(); + $user->expects($this->any()) + ->method('getUID') + ->will($this->returnValue('jane')); + $password = 'secret'; + $originalUrl = 'another%20url'; + $redirectUrl = 'http://localhost/another url'; + + $this->request + ->expects($this->exactly(2)) + ->method('getRemoteAddress') + ->willReturn('192.168.0.1'); + $this->request + ->expects($this->once()) + ->method('passesCSRFCheck') + ->willReturn(false); + $this->throttler + ->expects($this->once()) + ->method('sleepDelay') + ->with('192.168.0.1'); + $this->throttler + ->expects($this->once()) + ->method('getDelay') + ->with('192.168.0.1') + ->willReturn(200); + $this->userSession->expects($this->once()) + ->method('isLoggedIn') + ->with() + ->will($this->returnValue(true)); + $this->urlGenerator->expects($this->once()) + ->method('getAbsoluteURL') + ->with(urldecode($originalUrl)) + ->will($this->returnValue($redirectUrl)); + + $expected = new \OCP\AppFramework\Http\RedirectResponse($redirectUrl); + $this->assertEquals($expected, $this->loginController->tryLogin('Jane', $password, $originalUrl)); + } + public function testLoginWithValidCredentialsAndRedirectUrl() { /** @var IUser | \PHPUnit_Framework_MockObject_MockObject $user */ - $user = $this->getMock('\OCP\IUser'); + $user = $this->getMockBuilder('\OCP\IUser')->getMock(); $user->expects($this->any()) ->method('getUID') ->will($this->returnValue('jane')); @@ -375,6 +457,10 @@ class LoginControllerTest extends TestCase { ->expects($this->exactly(2)) ->method('getRemoteAddress') ->willReturn('192.168.0.1'); + $this->request + ->expects($this->once()) + ->method('passesCSRFCheck') + ->willReturn(true); $this->throttler ->expects($this->once()) ->method('sleepDelay') @@ -406,7 +492,7 @@ class LoginControllerTest extends TestCase { public function testLoginWithTwoFactorEnforced() { /** @var IUser | \PHPUnit_Framework_MockObject_MockObject $user */ - $user = $this->getMock('\OCP\IUser'); + $user = $this->getMockBuilder('\OCP\IUser')->getMock(); $user->expects($this->any()) ->method('getUID') ->will($this->returnValue('john')); @@ -417,6 +503,10 @@ class LoginControllerTest extends TestCase { ->expects($this->exactly(2)) ->method('getRemoteAddress') ->willReturn('192.168.0.1'); + $this->request + ->expects($this->once()) + ->method('passesCSRFCheck') + ->willReturn(true); $this->throttler ->expects($this->once()) ->method('sleepDelay') @@ -453,7 +543,7 @@ class LoginControllerTest extends TestCase { public function testToNotLeakLoginName() { /** @var IUser | \PHPUnit_Framework_MockObject_MockObject $user */ - $user = $this->getMock('\OCP\IUser'); + $user = $this->getMockBuilder('\OCP\IUser')->getMock(); $user->expects($this->any()) ->method('getUID') ->will($this->returnValue('john')); @@ -479,6 +569,10 @@ class LoginControllerTest extends TestCase { ->expects($this->exactly(3)) ->method('getRemoteAddress') ->willReturn('192.168.0.1'); + $this->request + ->expects($this->once()) + ->method('passesCSRFCheck') + ->willReturn(true); $this->throttler ->expects($this->once()) ->method('getDelay') diff --git a/tests/Settings/Controller/AuthSettingsControllerTest.php b/tests/Settings/Controller/AuthSettingsControllerTest.php index 1705cb5ddf1..b57aea0930c 100644 --- a/tests/Settings/Controller/AuthSettingsControllerTest.php +++ b/tests/Settings/Controller/AuthSettingsControllerTest.php @@ -91,6 +91,7 @@ class AuthSettingsControllerTest extends TestCase { 'lastActivity' => null, 'type' => null, 'canDelete' => false, + 'current' => true, ], [ 'id' => 200, diff --git a/tests/Settings/Controller/CheckSetupControllerTest.php b/tests/Settings/Controller/CheckSetupControllerTest.php index 770d5a4934e..e4f66b5d879 100644 --- a/tests/Settings/Controller/CheckSetupControllerTest.php +++ b/tests/Settings/Controller/CheckSetupControllerTest.php @@ -37,24 +37,11 @@ use Test\TestCase; use OC\IntegrityCheck\Checker; /** - * Mock version_compare - * @param string $version1 - * @param string $version2 - * @return int - */ -function version_compare($version1, $version2) { - return CheckSetupControllerTest::$version_compare; -} - -/** * Class CheckSetupControllerTest * * @package Tests\Settings\Controller */ class CheckSetupControllerTest extends TestCase { - /** @var int */ - public static $version_compare; - /** @var CheckSetupController */ private $checkSetupController; /** @var IRequest */ @@ -111,7 +98,7 @@ class CheckSetupControllerTest extends TestCase { $this->checker, $this->logger ]) - ->setMethods(['getCurlVersion'])->getMock(); + ->setMethods(['getCurlVersion', 'isPhpOutdated'])->getMock(); } public function testIsInternetConnectionWorkingDisabledViaConfig() { @@ -136,12 +123,8 @@ class CheckSetupControllerTest extends TestCase { $client = $this->getMockBuilder('\OCP\Http\Client\IClient') ->disableOriginalConstructor()->getMock(); - $client->expects($this->at(0)) - ->method('get') - ->with('https://www.owncloud.org/', []); - $client->expects($this->at(1)) - ->method('get') - ->with('http://www.owncloud.org/', []); + $client->expects($this->any()) + ->method('get'); $this->clientService->expects($this->once()) ->method('newClient') @@ -156,7 +139,7 @@ class CheckSetupControllerTest extends TestCase { ); } - public function testIsInternetConnectionHttpsFail() { + public function testIsInternetConnectionFail() { $this->config->expects($this->once()) ->method('getSystemValue') ->with('has_internet_connection', true) @@ -164,12 +147,11 @@ class CheckSetupControllerTest extends TestCase { $client = $this->getMockBuilder('\OCP\Http\Client\IClient') ->disableOriginalConstructor()->getMock(); - $client->expects($this->at(0)) + $client->expects($this->any()) ->method('get') - ->with('https://www.owncloud.org/', []) ->will($this->throwException(new \Exception())); - $this->clientService->expects($this->once()) + $this->clientService->expects($this->exactly(3)) ->method('newClient') ->will($this->returnValue($client)); @@ -181,33 +163,6 @@ class CheckSetupControllerTest extends TestCase { ); } - public function testIsInternetConnectionHttpFail() { - $this->config->expects($this->once()) - ->method('getSystemValue') - ->with('has_internet_connection', true) - ->will($this->returnValue(true)); - - $client = $this->getMockBuilder('\OCP\Http\Client\IClient') - ->disableOriginalConstructor()->getMock(); - $client->expects($this->at(0)) - ->method('get') - ->with('https://www.owncloud.org/', []); - $client->expects($this->at(1)) - ->method('get') - ->with('http://www.owncloud.org/', []) - ->will($this->throwException(new \Exception())); - - $this->clientService->expects($this->once()) - ->method('newClient') - ->will($this->returnValue($client)); - - $this->assertFalse( - self::invokePrivate( - $this->checkSetupController, - 'isInternetConnectionWorking' - ) - ); - } public function testIsMemcacheConfiguredFalse() { $this->config->expects($this->once()) @@ -238,7 +193,10 @@ class CheckSetupControllerTest extends TestCase { } public function testIsPhpSupportedFalse() { - self::$version_compare = -1; + $this->checkSetupController + ->expects($this->once()) + ->method('isPhpOutdated') + ->willReturn(true); $this->assertEquals( ['eol' => true, 'version' => PHP_VERSION], @@ -247,7 +205,10 @@ class CheckSetupControllerTest extends TestCase { } public function testIsPhpSupportedTrue() { - self::$version_compare = 0; + $this->checkSetupController + ->expects($this->exactly(2)) + ->method('isPhpOutdated') + ->willReturn(false); $this->assertEquals( ['eol' => false, 'version' => PHP_VERSION], @@ -255,8 +216,6 @@ class CheckSetupControllerTest extends TestCase { ); - self::$version_compare = 1; - $this->assertEquals( ['eol' => false, 'version' => PHP_VERSION], self::invokePrivate($this->checkSetupController, 'isPhpSupported') @@ -323,13 +282,17 @@ class CheckSetupControllerTest extends TestCase { ->disableOriginalConstructor()->getMock(); $client->expects($this->at(0)) ->method('get') - ->with('https://www.owncloud.org/', []); + ->with('http://www.nextcloud.com/', []) + ->will($this->throwException(new \Exception())); $client->expects($this->at(1)) ->method('get') - ->with('http://www.owncloud.org/', []) + ->with('http://www.google.com/', []) ->will($this->throwException(new \Exception())); - - $this->clientService->expects($this->once()) + $client->expects($this->at(2)) + ->method('get') + ->with('http://www.github.com/', []) + ->will($this->throwException(new \Exception())); + $this->clientService->expects($this->exactly(3)) ->method('newClient') ->will($this->returnValue($client)); $this->urlGenerator->expects($this->at(0)) @@ -340,7 +303,10 @@ class CheckSetupControllerTest extends TestCase { ->method('linkToDocs') ->with('admin-security') ->willReturn('https://doc.owncloud.org/server/8.1/admin_manual/configuration_server/hardening.html'); - self::$version_compare = -1; + $this->checkSetupController + ->expects($this->once()) + ->method('isPhpOutdated') + ->willReturn(true); $this->urlGenerator->expects($this->at(2)) ->method('linkToDocs') ->with('admin-reverse-proxy') diff --git a/tests/phpunit-autotest.xml b/tests/phpunit-autotest.xml index 07de887c8cb..301f63a375d 100644 --- a/tests/phpunit-autotest.xml +++ b/tests/phpunit-autotest.xml @@ -8,9 +8,8 @@ > <testsuite name='ownCloud'> <directory suffix='.php'>lib/</directory> - <directory suffix='.php'>settings/</directory> - <directory suffix='.php'>core/</directory> - <directory suffix='.php'>ocs-provider/</directory> + <directory suffix='.php'>Settings/</directory> + <directory suffix='.php'>Core/</directory> <file>apps.php</file> </testsuite> <!-- filters for code coverage --> diff --git a/tests/phpunit.xml.dist b/tests/phpunit.xml.dist index 19fa38209a9..d3a5897dfaf 100644 --- a/tests/phpunit.xml.dist +++ b/tests/phpunit.xml.dist @@ -2,9 +2,8 @@ <phpunit bootstrap="bootstrap.php"> <testsuite name='ownCloud'> <directory suffix='.php'>lib/</directory> - <directory suffix='.php'>settings/</directory> - <directory suffix='.php'>core/</directory> - <directory suffix='.php'>ocs-provider/</directory> + <directory suffix='.php'>Settings/</directory> + <directory suffix='.php'>Core/</directory> <file>apps.php</file> </testsuite> <!-- filters for code coverage --> |