summaryrefslogtreecommitdiffstats
path: root/apps
diff options
context:
space:
mode:
authorRoeland Jago Douma <rullzer@owncloud.com>2016-04-01 17:35:37 +0200
committerRoeland Jago Douma <rullzer@owncloud.com>2016-04-08 14:17:24 +0200
commiteda71240e2a158e2f282486fb7404512478dc19b (patch)
treede5d177038bd98694a9231955c88929f304939f9 /apps
parent375f6fcab1897803ea52e8f64954929f854ee8f1 (diff)
downloadnextcloud-server-eda71240e2a158e2f282486fb7404512478dc19b.tar.gz
nextcloud-server-eda71240e2a158e2f282486fb7404512478dc19b.zip
Add unit tests for public auth webdav
Diffstat (limited to 'apps')
-rw-r--r--apps/dav/tests/unit/connector/publicauth.php170
1 files changed, 170 insertions, 0 deletions
diff --git a/apps/dav/tests/unit/connector/publicauth.php b/apps/dav/tests/unit/connector/publicauth.php
new file mode 100644
index 00000000000..5f46651d372
--- /dev/null
+++ b/apps/dav/tests/unit/connector/publicauth.php
@@ -0,0 +1,170 @@
+<?php
+
+namespace OCA\DAV\Tests\Unit\Connector;
+
+use OCP\IRequest;
+use OCP\ISession;
+use OCP\Share\Exceptions\ShareNotFound;
+use OCP\Share\IManager;
+
+class PublicAuth extends \Test\TestCase {
+
+ /** @var ISession|\PHPUnit_Framework_MockObject_MockObject */
+ private $session;
+ /** @var IRequest|\PHPUnit_Framework_MockObject_MockObject */
+ private $request;
+ /** @var IManager|\PHPUnit_Framework_MockObject_MockObject */
+ private $shareManager;
+ /** @var \OCA\DAV\Connector\PublicAuth */
+ private $auth;
+
+ /** @var string */
+ private $oldUser;
+
+ protected function setUp() {
+ parent::setUp();
+
+ $this->session = $this->getMock('\OCP\ISession');
+ $this->request = $this->getMock('\OCP\IRequest');
+ $this->shareManager = $this->getMock('\OCP\Share\IManager');
+
+ $this->auth = new \OCA\DAV\Connector\PublicAuth(
+ $this->request,
+ $this->shareManager,
+ $this->session
+ );
+
+ // Store current user
+ $this->oldUser = \OC_User::getUser();
+ }
+
+ protected function tearDown() {
+ \OC_User::setIncognitoMode(false);
+
+ // Set old user
+ \OC_User::setUserId($this->oldUser);
+ \OC_Util::setupFS($this->oldUser);
+
+ parent::tearDown();
+ }
+
+ public function testNoShare() {
+ $this->shareManager->expects($this->once())
+ ->method('getShareByToken')
+ ->willThrowException(new ShareNotFound());
+
+ $result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
+
+ $this->assertFalse($result);
+ }
+
+ public function testShareNoPassword() {
+ $share = $this->getMock('OCP\Share\IShare');
+ $share->method('getPassword')->willReturn(null);
+
+ $this->shareManager->expects($this->once())
+ ->method('getShareByToken')
+ ->willReturn($share);
+
+ $result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
+
+ $this->assertTrue($result);
+ }
+
+ public function testSharePasswordFancyShareType() {
+ $share = $this->getMock('OCP\Share\IShare');
+ $share->method('getPassword')->willReturn('password');
+ $share->method('getShareType')->willReturn(42);
+
+ $this->shareManager->expects($this->once())
+ ->method('getShareByToken')
+ ->willReturn($share);
+
+ $result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
+
+ $this->assertFalse($result);
+ }
+
+
+ public function testSharePasswordRemote() {
+ $share = $this->getMock('OCP\Share\IShare');
+ $share->method('getPassword')->willReturn('password');
+ $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_REMOTE);
+
+ $this->shareManager->expects($this->once())
+ ->method('getShareByToken')
+ ->willReturn($share);
+
+ $result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
+
+ $this->assertTrue($result);
+ }
+
+ public function testSharePasswordLinkValidPassword() {
+ $share = $this->getMock('OCP\Share\IShare');
+ $share->method('getPassword')->willReturn('password');
+ $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_LINK);
+
+ $this->shareManager->expects($this->once())
+ ->method('getShareByToken')
+ ->willReturn($share);
+
+ $this->shareManager->expects($this->once())
+ ->method('checkPassword')->with(
+ $this->equalTo($share),
+ $this->equalTo('password')
+ )->willReturn(true);
+
+ $result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
+
+ $this->assertTrue($result);
+ }
+
+ public function testSharePasswordLinkValidSession() {
+ $share = $this->getMock('OCP\Share\IShare');
+ $share->method('getPassword')->willReturn('password');
+ $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_LINK);
+ $share->method('getId')->willReturn('42');
+
+ $this->shareManager->expects($this->once())
+ ->method('getShareByToken')
+ ->willReturn($share);
+
+ $this->shareManager->method('checkPassword')
+ ->with(
+ $this->equalTo($share),
+ $this->equalTo('password')
+ )->willReturn(false);
+
+ $this->session->method('exists')->with('public_link_authenticated')->willReturn(true);
+ $this->session->method('get')->with('public_link_authenticated')->willReturn('42');
+
+ $result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
+
+ $this->assertTrue($result);
+ }
+
+ public function testSharePasswordLinkInvalidSession() {
+ $share = $this->getMock('OCP\Share\IShare');
+ $share->method('getPassword')->willReturn('password');
+ $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_LINK);
+ $share->method('getId')->willReturn('42');
+
+ $this->shareManager->expects($this->once())
+ ->method('getShareByToken')
+ ->willReturn($share);
+
+ $this->shareManager->method('checkPassword')
+ ->with(
+ $this->equalTo($share),
+ $this->equalTo('password')
+ )->willReturn(false);
+
+ $this->session->method('exists')->with('public_link_authenticated')->willReturn(true);
+ $this->session->method('get')->with('public_link_authenticated')->willReturn('43');
+
+ $result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
+
+ $this->assertFalse($result);
+ }
+}