diff options
author | Roeland Jago Douma <rullzer@owncloud.com> | 2016-01-15 09:41:51 +0100 |
---|---|---|
committer | Roeland Jago Douma <rullzer@owncloud.com> | 2016-01-20 21:12:26 +0100 |
commit | 717697313b07106fd7941b83ff0271272c2c7791 (patch) | |
tree | 5a7b062392ea014f952fa8e43c07724be8059f8f /apps | |
parent | 0ebc92c44ab6332e74e88fb3a32e860139417677 (diff) | |
download | nextcloud-server-717697313b07106fd7941b83ff0271272c2c7791.tar.gz nextcloud-server-717697313b07106fd7941b83ff0271272c2c7791.zip |
[Share 2.0] Move tests over the sharemanager
Nice side effect... pure unit tests!
Diffstat (limited to 'apps')
-rw-r--r-- | apps/files_sharing/appinfo/application.php | 12 | ||||
-rw-r--r-- | apps/files_sharing/lib/controllers/sharecontroller.php | 51 | ||||
-rw-r--r-- | apps/files_sharing/tests/controller/sharecontroller.php | 244 |
3 files changed, 196 insertions, 111 deletions
diff --git a/apps/files_sharing/appinfo/application.php b/apps/files_sharing/appinfo/application.php index 8f953b236c4..5fa400db7d2 100644 --- a/apps/files_sharing/appinfo/application.php +++ b/apps/files_sharing/appinfo/application.php @@ -47,15 +47,13 @@ class Application extends App { return new ShareController( $c->query('AppName'), $c->query('Request'), - $c->query('UserSession'), - $server->getAppConfig(), $server->getConfig(), $c->query('URLGenerator'), $c->query('UserManager'), $server->getLogger(), $server->getActivityManager(), - $server->getShareManager(), - $server->getSession(), + $c->query('ShareManager'), + $c->query('Session'), $server->getPreviewManager() ); }); @@ -71,6 +69,12 @@ class Application extends App { /** * Core class wrappers */ + $container->registerService('Session', function(SimpleContainer $c) use ($server) { + return $server->getSession(); + }); + $container->registerService('ShareManager', function(SimpleContainer $c) use ($server) { + return $server->getShareManager(); + }); $container->registerService('UserSession', function (SimpleContainer $c) use ($server) { return $server->getUserSession(); }); diff --git a/apps/files_sharing/lib/controllers/sharecontroller.php b/apps/files_sharing/lib/controllers/sharecontroller.php index bf4f297260d..f701a66dd0f 100644 --- a/apps/files_sharing/lib/controllers/sharecontroller.php +++ b/apps/files_sharing/lib/controllers/sharecontroller.php @@ -41,9 +41,12 @@ use OCP\IRequest; use OCP\AppFramework\Http\TemplateResponse; use OCP\AppFramework\Http\RedirectResponse; use OCP\AppFramework\Http\NotFoundResponse; -use OC\URLGenerator; -use OC\AppConfig; +use OCP\IURLGenerator; +use OCP\IConfig; use OCP\ILogger; +use OCP\IUserManager; +use OCP\ISession; +use OCP\IPreview; use OCA\Files_Sharing\Helper; use OCP\Util; use OCA\Files_Sharing\Activity; @@ -57,57 +60,47 @@ use \OC\Share20\IShare; */ class ShareController extends Controller { - /** @var \OC\User\Session */ - protected $userSession; - /** @var \OC\AppConfig */ - protected $appConfig; - /** @var \OCP\IConfig */ + /** @var IConfig */ protected $config; - /** @var \OC\URLGenerator */ + /** @var IURLGenerator */ protected $urlGenerator; - /** @var \OC\User\Manager */ + /** @var IUserManager */ protected $userManager; - /** @var \OCP\ILogger */ + /** @var ILogger */ protected $logger; /** @var OCP\Activity\IManager */ protected $activityManager; /** @var OC\Share20\Manager */ protected $shareManager; - /** @var \OCP\ISession */ + /** @var ISession */ protected $session; - /** @var \OCP\IPreview */ + /** @var IPreview */ protected $previewManager; /** * @param string $appName * @param IRequest $request - * @param OC\User\Session $userSession - * @param AppConfig $appConfig - * @param OCP\IConfig $config - * @param URLGenerator $urlGenerator - * @param OCP\IUserManager $userManager + * @param IConfig $config + * @param IURLGenerator $urlGenerator + * @param IUserManager $userManager * @param ILogger $logger * @param OCP\Activity\IManager $activityManager * @param \OC\Share20\Manager $shareManager - * @param \OCP\ISession $session - * @param \OCP\IPreview $previewManager + * @param ISession $session + * @param IPreview $previewManager */ public function __construct($appName, IRequest $request, - OC\User\Session $userSession, - AppConfig $appConfig, - OCP\IConfig $config, - URLGenerator $urlGenerator, - OCP\IUserManager $userManager, + IConfig $config, + IURLGenerator $urlGenerator, + IUserManager $userManager, ILogger $logger, - OCP\Activity\IManager $activityManager, + \OCP\Activity\IManager $activityManager, \OC\Share20\Manager $shareManager, - \OCP\ISession $session, - \OCP\IPreview $previewManager) { + ISession $session, + IPreview $previewManager) { parent::__construct($appName, $request); - $this->userSession = $userSession; - $this->appConfig = $appConfig; $this->config = $config; $this->urlGenerator = $urlGenerator; $this->userManager = $userManager; diff --git a/apps/files_sharing/tests/controller/sharecontroller.php b/apps/files_sharing/tests/controller/sharecontroller.php index 914c98d9470..29dfbd1e8b1 100644 --- a/apps/files_sharing/tests/controller/sharecontroller.php +++ b/apps/files_sharing/tests/controller/sharecontroller.php @@ -30,15 +30,17 @@ namespace OCA\Files_Sharing\Controllers; use OC\Files\Filesystem; +use OC\Share20\Exception\ShareNotFound; use OCA\Files_Sharing\AppInfo\Application; use OCP\AppFramework\Http\NotFoundResponse; use OCP\AppFramework\IAppContainer; use OCP\AppFramework\Http\RedirectResponse; use OCP\AppFramework\Http\TemplateResponse; +use OCP\ISession; use OCP\Security\ISecureRandom; use OC\Files\View; use OCP\Share; -use OC\URLGenerator; +use OCP\IURLGenerator; /** * @group DB @@ -57,21 +59,26 @@ class ShareControllerTest extends \Test\TestCase { private $oldUser; /** @var ShareController */ private $shareController; - /** @var URLGenerator */ + /** @var IURLGenerator */ private $urlGenerator; + /** @var ISession | \PHPUnit_Framework_MockObject_MockObject */ + private $session; + /** @var \OC\Share20\Manager | \PHPUnit_Framework_MockObject_MockObject */ + private $shareManager; protected function setUp() { $app = new Application(); $this->container = $app->getContainer(); - $this->container['Config'] = $this->getMockBuilder('\OCP\IConfig') - ->disableOriginalConstructor()->getMock(); + $this->container['Config'] = $this->getMock('\OCP\IConfig'); $this->container['AppName'] = 'files_sharing'; - $this->container['UserSession'] = $this->getMockBuilder('\OC\User\Session') - ->disableOriginalConstructor()->getMock(); - $this->container['URLGenerator'] = $this->getMockBuilder('\OC\URLGenerator') - ->disableOriginalConstructor()->getMock(); - $this->container['UserManager'] = $this->getMockBuilder('\OCP\IUserManager') + $this->container['URLGenerator'] = $this->getMock('\OCP\IURLGenerator'); + $this->container['UserManager'] = $this->getMock('\OCP\IUserManager'); + $this->container['ShareManager'] = $this->getMockBuilder('\OC\Share20\Manager') ->disableOriginalConstructor()->getMock(); + $this->container['Session'] = $this->getMock('\OCP\ISession'); + + $this->session = $this->container['Session']; + $this->shareManager = $this->container['ShareManager']; $this->urlGenerator = $this->container['URLGenerator']; $this->shareController = $this->container['ShareController']; @@ -112,72 +119,184 @@ class ShareControllerTest extends \Test\TestCase { \OC_Util::setupFS($this->oldUser); } - public function testShowAuthenticate() { - $linkItem = \OCP\Share::getShareByToken($this->token, false); + public function testShowAuthenticateNotAuthenticated() { + $share = $this->getMock('\OC\Share20\IShare'); - // Test without being authenticated - $response = $this->shareController->showAuthenticate($this->token); - $expectedResponse = new TemplateResponse($this->container['AppName'], 'authenticate', array(), 'guest'); + $this->shareManager + ->expects($this->once()) + ->method('getShareByToken') + ->with('token') + ->willReturn($share); + + $response = $this->shareController->showAuthenticate('token'); + $expectedResponse = new TemplateResponse($this->container['AppName'], 'authenticate', [], 'guest'); $this->assertEquals($expectedResponse, $response); + } + + public function testShowAuthenticateAuthenticatedForDifferentShare() { + $share = $this->getMock('\OC\Share20\IShare'); + $share->method('getId')->willReturn(1); + + $this->shareManager + ->expects($this->once()) + ->method('getShareByToken') + ->with('token') + ->willReturn($share); - // Test with being authenticated for another file - \OC::$server->getSession()->set('public_link_authenticated', $linkItem['id']-1); - $response = $this->shareController->showAuthenticate($this->token); - $expectedResponse = new TemplateResponse($this->container['AppName'], 'authenticate', array(), 'guest'); + $this->session->method('exists')->with('public_link_authenticated')->willReturn(true); + $this->session->method('get')->with('public_link_authenticated')->willReturn('2'); + + $response = $this->shareController->showAuthenticate('token'); + $expectedResponse = new TemplateResponse($this->container['AppName'], 'authenticate', [], 'guest'); $this->assertEquals($expectedResponse, $response); + } - // Test with being authenticated for the correct file - \OC::$server->getSession()->set('public_link_authenticated', $linkItem['id']); - $response = $this->shareController->showAuthenticate($this->token); - $expectedResponse = new RedirectResponse($this->urlGenerator->linkToRoute('files_sharing.sharecontroller.showShare', array('token' => $this->token))); + public function testShowAuthenticateCorrectShare() { + $share = $this->getMock('\OC\Share20\IShare'); + $share->method('getId')->willReturn(1); + + $this->shareManager + ->expects($this->once()) + ->method('getShareByToken') + ->with('token') + ->willReturn($share); + + $this->session->method('exists')->with('public_link_authenticated')->willReturn(true); + $this->session->method('get')->with('public_link_authenticated')->willReturn('1'); + + $response = $this->shareController->showAuthenticate('token'); + $expectedResponse = new RedirectResponse($this->urlGenerator->linkToRoute('files_sharing.sharecontroller.showShare', array('token' => 'token'))); $this->assertEquals($expectedResponse, $response); } - public function testAuthenticate() { - // Test without a not existing token - $response = $this->shareController->authenticate('ThisTokenShouldHopefullyNeverExistSoThatTheUnitTestWillAlwaysPass :)'); + public function testAutehnticateInvalidToken() { + $this->shareManager + ->expects($this->once()) + ->method('getShareByToken') + ->with('token') + ->will($this->throwException(new \OC\Share20\Exception\ShareNotFound())); + + $response = $this->shareController->authenticate('token'); $expectedResponse = new NotFoundResponse(); $this->assertEquals($expectedResponse, $response); + } - // Test with a valid password - $response = $this->shareController->authenticate($this->token, 'IAmPasswordProtected!'); - $expectedResponse = new RedirectResponse($this->urlGenerator->linkToRoute('files_sharing.sharecontroller.showShare', array('token' => $this->token))); + public function testAuthenticateValidPassword() { + $share = $this->getMock('\OC\Share20\IShare'); + $share->method('getId')->willReturn(42); + + $this->shareManager + ->expects($this->once()) + ->method('getShareByToken') + ->with('token') + ->willReturn($share); + + $this->shareManager + ->expects($this->once()) + ->method('checkPassword') + ->with($share, 'validpassword') + ->willReturn(true); + + $this->session + ->expects($this->once()) + ->method('set') + ->with('public_link_authenticated', '42'); + + $response = $this->shareController->authenticate('token', 'validpassword'); + $expectedResponse = new RedirectResponse($this->urlGenerator->linkToRoute('files_sharing.sharecontroller.showShare', array('token' => 'token'))); $this->assertEquals($expectedResponse, $response); + } + + public function testAuthenticateInvalidPassword() { + $share = $this->getMock('\OC\Share20\IShare'); + $share->method('getId')->willReturn(42); - // Test with a invalid password - $response = $this->shareController->authenticate($this->token, 'WrongPw!'); + $this->shareManager + ->expects($this->once()) + ->method('getShareByToken') + ->with('token') + ->willReturn($share); + + $this->shareManager + ->expects($this->once()) + ->method('checkPassword') + ->with($share, 'invalidpassword') + ->willReturn(false); + + $this->session + ->expects($this->never()) + ->method('set'); + + $response = $this->shareController->authenticate('token', 'invalidpassword'); $expectedResponse = new TemplateResponse($this->container['AppName'], 'authenticate', array('wrongpw' => true), 'guest'); $this->assertEquals($expectedResponse, $response); } - public function testShowShare() { - $this->container['UserManager']->expects($this->exactly(2)) - ->method('userExists') - ->with($this->user) - ->will($this->returnValue(true)); + public function testShowShareInvalidToken() { + $this->shareManager + ->expects($this->once()) + ->method('getShareByToken') + ->with('invalidtoken') + ->will($this->throwException(new ShareNotFound())); // Test without a not existing token - $response = $this->shareController->showShare('ThisTokenShouldHopefullyNeverExistSoThatTheUnitTestWillAlwaysPass :)'); + $response = $this->shareController->showShare('invalidtoken'); $expectedResponse = new NotFoundResponse(); $this->assertEquals($expectedResponse, $response); + } - // Test with a password protected share and no authentication - $response = $this->shareController->showShare($this->token); - $expectedResponse = new RedirectResponse($this->urlGenerator->linkToRoute('files_sharing.sharecontroller.authenticate', array('token' => $this->token))); + public function testShowShareNotAuthenticated() { + $share = $this->getMock('\OC\Share20\IShare'); + $share->method('getPassword')->willReturn('password'); + + $this->shareManager + ->expects($this->once()) + ->method('getShareByToken') + ->with('validtoken') + ->willReturn($share); + + // Test without a not existing token + $response = $this->shareController->showShare('validtoken'); + $expectedResponse = new RedirectResponse($this->urlGenerator->linkToRoute('files_sharing.sharecontroller.authenticate', array('token' => 'validtoken'))); $this->assertEquals($expectedResponse, $response); + } - // Test with password protected share and authentication - $linkItem = Share::getShareByToken($this->token, false); - \OC::$server->getSession()->set('public_link_authenticated', $linkItem['id']); - $response = $this->shareController->showShare($this->token); + + public function testShowShare() { + $owner = $this->getMock('OCP\IUser'); + $owner->method('getDisplayName')->willReturn('ownerDisplay'); + $owner->method('getUID')->willReturn('ownerUID'); + + $file = $this->getMock('OCP\Files\File'); + $file->method('getName')->willReturn('file1.txt'); + $file->method('getMimetype')->willReturn('text/plain'); + $file->method('getSize')->willReturn(33); + + $share = $this->getMock('\OC\Share20\IShare'); + $share->method('getId')->willReturn('42'); + $share->method('getPassword')->willReturn('password'); + $share->method('getShareOwner')->willReturn($owner); + $share->method('getPath')->willReturn($file); + $share->method('getTarget')->willReturn('/file1.txt'); + + $this->session->method('exists')->with('public_link_authenticated')->willReturn(true); + $this->session->method('get')->with('public_link_authenticated')->willReturn('42'); + + $this->shareManager + ->expects($this->once()) + ->method('getShareByToken') + ->with('token') + ->willReturn($share); + + $response = $this->shareController->showShare('token'); $sharedTmplParams = array( - 'displayName' => $this->user, - 'owner' => $this->user, + 'displayName' => 'ownerDisplay', + 'owner' => 'ownerUID', 'filename' => 'file1.txt', 'directory_path' => '/file1.txt', 'mimetype' => 'text/plain', - 'dirToken' => $this->token, - 'sharingToken' => $this->token, + 'dirToken' => 'token', + 'sharingToken' => 'token', 'server2serversharing' => true, 'protected' => 'true', 'dir' => '', @@ -208,22 +327,6 @@ class ShareControllerTest extends \Test\TestCase { /** * @expectedException \OCP\Files\NotFoundException */ - public function testShowShareWithDeletedFile() { - $this->container['UserManager']->expects($this->once()) - ->method('userExists') - ->with($this->user) - ->will($this->returnValue(true)); - - $view = new View('/'. $this->user . '/files'); - $view->unlink('file1.txt'); - $linkItem = Share::getShareByToken($this->token, false); - \OC::$server->getSession()->set('public_link_authenticated', $linkItem['id']); - $this->shareController->showShare($this->token); - } - - /** - * @expectedException \OCP\Files\NotFoundException - */ public function testDownloadShareWithDeletedFile() { $this->container['UserManager']->expects($this->once()) ->method('userExists') @@ -237,19 +340,4 @@ class ShareControllerTest extends \Test\TestCase { $this->shareController->downloadShare($this->token); } - /** - * @expectedException \Exception - * @expectedExceptionMessage Owner of the share does not exist anymore - */ - public function testShowShareWithNotExistingUser() { - $this->container['UserManager']->expects($this->once()) - ->method('userExists') - ->with($this->user) - ->will($this->returnValue(false)); - - $linkItem = Share::getShareByToken($this->token, false); - \OC::$server->getSession()->set('public_link_authenticated', $linkItem['id']); - $this->shareController->showShare($this->token); - } - } |