diff options
author | Julius Härtl <jus@bitgrid.net> | 2021-10-05 13:06:59 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-05 13:06:59 +0200 |
commit | d68f0282515cb5122cdbd834217b62079dcad9bc (patch) | |
tree | 194113d2fbaef56e5faee18fa3f64b02991e7e66 /tests | |
parent | ee32c7c3282407c4cc714dc7e00a374781d9ef56 (diff) | |
parent | 1684fd0b6f9a79641f4558ad33c30b185d5d0bd0 (diff) | |
download | nextcloud-server-d68f0282515cb5122cdbd834217b62079dcad9bc.tar.gz nextcloud-server-d68f0282515cb5122cdbd834217b62079dcad9bc.zip |
Merge pull request #27733 from PhrozenByte/enhancement/noid/IURLGenerator-linkToDefaultPageUrl
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Core/Controller/LoginControllerTest.php | 32 | ||||
-rw-r--r-- | tests/Core/Controller/TwoFactorChallengeControllerTest.php | 7 | ||||
-rw-r--r-- | tests/lib/UrlGeneratorTest.php | 111 | ||||
-rw-r--r-- | tests/lib/UtilTest.php | 105 |
4 files changed, 136 insertions, 119 deletions
diff --git a/tests/Core/Controller/LoginControllerTest.php b/tests/Core/Controller/LoginControllerTest.php index e3469621eec..fe42b55db15 100644 --- a/tests/Core/Controller/LoginControllerTest.php +++ b/tests/Core/Controller/LoginControllerTest.php @@ -211,8 +211,12 @@ class LoginControllerTest extends TestCase { ->expects($this->once()) ->method('isLoggedIn') ->willReturn(true); + $this->urlGenerator + ->expects($this->once()) + ->method('linkToDefaultPageUrl') + ->willReturn('/default/foo'); - $expectedResponse = new RedirectResponse(\OC_Util::getDefaultPageUrl()); + $expectedResponse = new RedirectResponse('/default/foo'); $this->assertEquals($expectedResponse, $this->loginController->showLoginForm('', '', '')); } @@ -443,7 +447,7 @@ class LoginControllerTest extends TestCase { 'direct' => 1, ]) ->willReturn($loginPageUrl); - $expected = new \OCP\AppFramework\Http\RedirectResponse($loginPageUrl); + $expected = new RedirectResponse($loginPageUrl); $expected->throttle(['user' => 'MyUserName']); $response = $this->loginController->tryLogin($user, $password, '/apps/files'); @@ -454,7 +458,6 @@ class LoginControllerTest extends TestCase { public function testLoginWithValidCredentials() { $user = 'MyUserName'; $password = 'secret'; - $indexPageUrl = \OC_Util::getDefaultPageUrl(); $this->request ->expects($this->once()) @@ -470,11 +473,13 @@ class LoginControllerTest extends TestCase { ->method('process') ->with($this->equalTo($loginData)) ->willReturn($loginResult); - $expected = new \OCP\AppFramework\Http\RedirectResponse($indexPageUrl); - - $response = $this->loginController->tryLogin($user, $password); + $this->urlGenerator + ->expects($this->once()) + ->method('linkToDefaultPageUrl') + ->willReturn('/default/foo'); - $this->assertEquals($expected, $response); + $expected = new RedirectResponse('/default/foo'); + $this->assertEquals($expected, $this->loginController->tryLogin($user, $password)); } public function testLoginWithoutPassedCsrfCheckAndNotLoggedIn() { @@ -498,8 +503,12 @@ class LoginControllerTest extends TestCase { ->method('deleteUserValue'); $this->userSession->expects($this->never()) ->method('createRememberMeToken'); + $this->urlGenerator + ->expects($this->once()) + ->method('linkToDefaultPageUrl') + ->willReturn('/default/foo'); - $expected = new \OCP\AppFramework\Http\RedirectResponse(\OC_Util::getDefaultPageUrl()); + $expected = new RedirectResponse('/default/foo'); $this->assertEquals($expected, $this->loginController->tryLogin('Jane', $password, $originalUrl)); } @@ -534,14 +543,13 @@ class LoginControllerTest extends TestCase { ->with('remember_login_cookie_lifetime') ->willReturn(1234); - $expected = new \OCP\AppFramework\Http\RedirectResponse($redirectUrl); + $expected = new RedirectResponse($redirectUrl); $this->assertEquals($expected, $this->loginController->tryLogin('Jane', $password, $originalUrl)); } public function testLoginWithValidCredentialsAndRedirectUrl() { $user = 'MyUserName'; $password = 'secret'; - $indexPageUrl = \OC_Util::getDefaultPageUrl(); $redirectUrl = 'https://next.cloud/apps/mail'; $this->request @@ -566,7 +574,7 @@ class LoginControllerTest extends TestCase { ->method('getAbsoluteURL') ->with('/apps/mail') ->willReturn($redirectUrl); - $expected = new \OCP\AppFramework\Http\RedirectResponse($redirectUrl); + $expected = new RedirectResponse($redirectUrl); $response = $this->loginController->tryLogin($user, $password, '/apps/mail'); @@ -601,7 +609,7 @@ class LoginControllerTest extends TestCase { 'direct' => 1, ]) ->willReturn($loginPageUrl); - $expected = new \OCP\AppFramework\Http\RedirectResponse($loginPageUrl); + $expected = new RedirectResponse($loginPageUrl); $expected->throttle(['user' => 'john']); $response = $this->loginController->tryLogin( diff --git a/tests/Core/Controller/TwoFactorChallengeControllerTest.php b/tests/Core/Controller/TwoFactorChallengeControllerTest.php index b899a1b236a..2cb8d4bd50f 100644 --- a/tests/Core/Controller/TwoFactorChallengeControllerTest.php +++ b/tests/Core/Controller/TwoFactorChallengeControllerTest.php @@ -25,7 +25,6 @@ namespace Test\Core\Controller; use OC\Authentication\TwoFactorAuth\Manager; use OC\Authentication\TwoFactorAuth\ProviderSet; use OC\Core\Controller\TwoFactorChallengeController; -use OC_Util; use OCP\AppFramework\Http\RedirectResponse; use OCP\AppFramework\Http\StandaloneTemplateResponse; use OCP\Authentication\TwoFactorAuth\IActivatableAtLogin; @@ -212,8 +211,12 @@ class TwoFactorChallengeControllerTest extends TestCase { ->method('verifyChallenge') ->with('myprovider', $user, 'token') ->willReturn(true); + $this->urlGenerator + ->expects($this->once()) + ->method('linkToDefaultPageUrl') + ->willReturn('/default/foo'); - $expected = new RedirectResponse(OC_Util::getDefaultPageUrl()); + $expected = new RedirectResponse('/default/foo'); $this->assertEquals($expected, $this->controller->solveChallenge('myprovider', 'token')); } diff --git a/tests/lib/UrlGeneratorTest.php b/tests/lib/UrlGeneratorTest.php index 761f4b42eea..9e5795fc41e 100644 --- a/tests/lib/UrlGeneratorTest.php +++ b/tests/lib/UrlGeneratorTest.php @@ -13,6 +13,8 @@ use OCP\ICacheFactory; use OCP\IConfig; use OCP\IRequest; use OCP\IURLGenerator; +use OCP\IUser; +use OCP\IUserSession; /** * Class UrlGeneratorTest @@ -23,6 +25,8 @@ class UrlGeneratorTest extends \Test\TestCase { /** @var \PHPUnit\Framework\MockObject\MockObject|IConfig */ private $config; + /** @var \PHPUnit\Framework\MockObject\MockObject|IUserSession */ + private $userSession; /** @var \PHPUnit\Framework\MockObject\MockObject|ICacheFactory */ private $cacheFactory; /** @var \PHPUnit\Framework\MockObject\MockObject|IRequest */ @@ -37,11 +41,13 @@ class UrlGeneratorTest extends \Test\TestCase { protected function setUp(): void { parent::setUp(); $this->config = $this->createMock(IConfig::class); + $this->userSession = $this->createMock(IUserSession::class); $this->cacheFactory = $this->createMock(ICacheFactory::class); $this->request = $this->createMock(IRequest::class); $this->router = $this->createMock(Router::class); $this->urlGenerator = new \OC\URLGenerator( $this->config, + $this->userSession, $this->cacheFactory, $this->request, $this->router @@ -210,4 +216,109 @@ class UrlGeneratorTest extends \Test\TestCase { ['core.WhatsNew.dismiss', 'http://localhost/nextcloud/ocs/v2.php/core/whatsnew'], ]; } + + private function mockLinkToDefaultPageUrl(string $defaultAppConfig = '', bool $ignoreFrontControllerConfig = false) { + $this->config->expects($this->exactly(2)) + ->method('getSystemValue') + ->withConsecutive( + ['defaultapp', $this->anything()], + ['htaccess.IgnoreFrontController', $this->anything()], + ) + ->will($this->onConsecutiveCalls( + $defaultAppConfig, + $ignoreFrontControllerConfig + )); + $this->config->expects($this->once()) + ->method('getAppValue') + ->with('core', 'defaultpage') + ->willReturn(''); + } + + public function testLinkToDefaultPageUrlWithRedirectUrlWithoutFrontController() { + $this->mockBaseUrl(); + + $_REQUEST['redirect_url'] = 'myRedirectUrl.com'; + $this->assertSame('http://localhost' . \OC::$WEBROOT . '/myRedirectUrl.com', $this->urlGenerator->linkToDefaultPageUrl()); + } + + public function testLinkToDefaultPageUrlWithRedirectUrlRedirectBypassWithoutFrontController() { + $this->mockBaseUrl(); + $this->mockLinkToDefaultPageUrl(); + putenv('front_controller_active=false'); + + $_REQUEST['redirect_url'] = 'myRedirectUrl.com@foo.com:a'; + $this->assertSame('http://localhost' . \OC::$WEBROOT . '/index.php/apps/files/', $this->urlGenerator->linkToDefaultPageUrl()); + } + + public function testLinkToDefaultPageUrlWithRedirectUrlRedirectBypassWithFrontController() { + $this->mockBaseUrl(); + $this->mockLinkToDefaultPageUrl(); + putenv('front_controller_active=true'); + + $_REQUEST['redirect_url'] = 'myRedirectUrl.com@foo.com:a'; + $this->assertSame('http://localhost' . \OC::$WEBROOT . '/apps/files/', $this->urlGenerator->linkToDefaultPageUrl()); + } + + public function testLinkToDefaultPageUrlWithRedirectUrlWithIgnoreFrontController() { + $this->mockBaseUrl(); + $this->mockLinkToDefaultPageUrl('', true); + putenv('front_controller_active=false'); + + $_REQUEST['redirect_url'] = 'myRedirectUrl.com@foo.com:a'; + $this->assertSame('http://localhost' . \OC::$WEBROOT . '/apps/files/', $this->urlGenerator->linkToDefaultPageUrl()); + } + + /** + * @dataProvider provideDefaultApps + */ + public function testLinkToDefaultPageUrlWithDefaultApps($defaultAppConfig, $expectedPath) { + $userId = $this->getUniqueID(); + + /** @var \PHPUnit\Framework\MockObject\MockObject|IUser $userMock */ + $userMock = $this->createMock(IUser::class); + $userMock->expects($this->once()) + ->method('getUID') + ->willReturn($userId); + + $this->mockBaseUrl(); + $this->mockLinkToDefaultPageUrl($defaultAppConfig); + + $this->config->expects($this->once()) + ->method('getUserValue') + ->with($userId, 'core', 'defaultapp') + ->willReturn(''); + $this->userSession->expects($this->once()) + ->method('isLoggedIn') + ->willReturn(true); + $this->userSession->expects($this->once()) + ->method('getUser') + ->willReturn($userMock); + + $this->assertEquals('http://localhost' . \OC::$WEBROOT . $expectedPath, $this->urlGenerator->linkToDefaultPageUrl()); + } + + public function provideDefaultApps() { + return [ + // none specified, default to files + [ + '', + '/index.php/apps/files/', + ], + // unexisting or inaccessible app specified, default to files + [ + 'unexist', + '/index.php/apps/files/', + ], + // non-standard app + [ + 'settings', + '/index.php/apps/settings/', + ], + // non-standard app with fallback + [ + 'unexist,settings', + '/index.php/apps/settings/', + ], + ]; + } } diff --git a/tests/lib/UtilTest.php b/tests/lib/UtilTest.php index e21a5323b1b..04d4858fb26 100644 --- a/tests/lib/UtilTest.php +++ b/tests/lib/UtilTest.php @@ -9,7 +9,6 @@ namespace Test; use OC_Util; -use OCP\App\IAppManager; /** * Class UtilTest @@ -170,96 +169,6 @@ class UtilTest extends \Test\TestCase { } /** - * Test default apps - * - * @dataProvider defaultAppsProvider - * @group DB - */ - public function testDefaultApps($defaultAppConfig, $expectedPath, $enabledApps) { - $oldDefaultApps = \OC::$server->getConfig()->getSystemValue('defaultapp', ''); - // CLI is doing messy stuff with the webroot, so need to work it around - $oldWebRoot = \OC::$WEBROOT; - \OC::$WEBROOT = ''; - - $appManager = $this->createMock(IAppManager::class); - $appManager->expects($this->any()) - ->method('isEnabledForUser') - ->willReturnCallback(function ($appId) use ($enabledApps) { - return in_array($appId, $enabledApps); - }); - Dummy_OC_Util::$appManager = $appManager; - - // need to set a user id to make sure enabled apps are read from cache - \OC_User::setUserId($this->getUniqueID()); - \OC::$server->getConfig()->setSystemValue('defaultapp', $defaultAppConfig); - $this->assertEquals('http://localhost/' . $expectedPath, Dummy_OC_Util::getDefaultPageUrl()); - - // restore old state - \OC::$WEBROOT = $oldWebRoot; - \OC::$server->getConfig()->setSystemValue('defaultapp', $oldDefaultApps); - \OC_User::setUserId(null); - } - - public function defaultAppsProvider() { - return [ - // none specified, default to files - [ - '', - 'index.php/apps/files/', - ['files'], - ], - // unexisting or inaccessible app specified, default to files - [ - 'unexist', - 'index.php/apps/files/', - ['files'], - ], - // non-standard app - [ - 'calendar', - 'index.php/apps/calendar/', - ['files', 'calendar'], - ], - // non-standard app with fallback - [ - 'contacts,calendar', - 'index.php/apps/calendar/', - ['files', 'calendar'], - ], - ]; - } - - public function testGetDefaultPageUrlWithRedirectUrlWithoutFrontController() { - putenv('front_controller_active=false'); - \OC::$server->getConfig()->deleteSystemValue('htaccess.IgnoreFrontController'); - - $_REQUEST['redirect_url'] = 'myRedirectUrl.com'; - $this->assertSame('http://localhost'.\OC::$WEBROOT.'/myRedirectUrl.com', OC_Util::getDefaultPageUrl()); - } - - public function testGetDefaultPageUrlWithRedirectUrlRedirectBypassWithoutFrontController() { - putenv('front_controller_active=false'); - \OC::$server->getConfig()->deleteSystemValue('htaccess.IgnoreFrontController'); - - $_REQUEST['redirect_url'] = 'myRedirectUrl.com@foo.com:a'; - $this->assertSame('http://localhost'.\OC::$WEBROOT.'/index.php/apps/files/', OC_Util::getDefaultPageUrl()); - } - - public function testGetDefaultPageUrlWithRedirectUrlRedirectBypassWithFrontController() { - putenv('front_controller_active=true'); - $_REQUEST['redirect_url'] = 'myRedirectUrl.com@foo.com:a'; - $this->assertSame('http://localhost'.\OC::$WEBROOT.'/apps/files/', OC_Util::getDefaultPageUrl()); - } - - public function testGetDefaultPageUrlWithRedirectUrlWithIgnoreFrontController() { - putenv('front_controller_active=false'); - \OC::$server->getConfig()->setSystemValue('htaccess.IgnoreFrontController', true); - - $_REQUEST['redirect_url'] = 'myRedirectUrl.com@foo.com:a'; - $this->assertSame('http://localhost'.\OC::$WEBROOT.'/apps/files/', OC_Util::getDefaultPageUrl()); - } - - /** * Test needUpgrade() when the core version is increased */ public function testNeedUpgradeCore() { @@ -390,17 +299,3 @@ class UtilTest extends \Test\TestCase { ], \OC_Util::$styles); } } - -/** - * Dummy OC Util class to make it possible to override the app manager - */ -class Dummy_OC_Util extends OC_Util { - /** - * @var \OCP\App\IAppManager - */ - public static $appManager; - - protected static function getAppManager() { - return self::$appManager; - } -} |