123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425 |
- <?php
- /**
- * @copyright Copyright (c) 2016, ownCloud, Inc.
- *
- * @author Bjoern Schiessle <bjoern@schiessle.org>
- * @author Joas Schilling <coding@schilljs.com>
- * @author Lukas Reschke <lukas@statuscode.ch>
- * @author Morris Jobke <hey@morrisjobke.de>
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- * @author Thomas Müller <thomas.mueller@tmit.eu>
- *
- * @license AGPL-3.0
- *
- * This code is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License, version 3,
- * as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License, version 3,
- * along with this program. If not, see <http://www.gnu.org/licenses/>
- *
- */
- namespace OCA\DAV\Tests\unit\Connector;
- use OCP\IRequest;
- use OCP\ISession;
- use OCP\Security\Bruteforce\IThrottler;
- use OCP\Share\Exceptions\ShareNotFound;
- use OCP\Share\IManager;
- use OCP\Share\IShare;
- use Psr\Log\LoggerInterface;
- /**
- * Class PublicAuthTest
- *
- * @group DB
- *
- * @package OCA\DAV\Tests\unit\Connector
- */
- class PublicAuthTest extends \Test\TestCase {
- /** @var ISession|MockObject */
- private $session;
- /** @var IRequest|MockObject */
- private $request;
- /** @var IManager|MockObject */
- private $shareManager;
- /** @var PublicAuth */
- private $auth;
- /** @var IThrottler|MockObject */
- private $throttler;
- /** @var LoggerInterface|MockObject */
- private $logger;
- /** @var string */
- private $oldUser;
- protected function setUp(): void {
- parent::setUp();
- $this->session = $this->createMock(ISession::class);
- $this->request = $this->createMock(IRequest::class);
- $this->shareManager = $this->createMock(IManager::class);
- $this->throttler = $this->createMock(IThrottler::class);
- $this->logger = $this->createMock(LoggerInterface::class);
- $this->auth = new \OCA\DAV\Connector\Sabre\PublicAuth(
- $this->request,
- $this->shareManager,
- $this->session,
- $this->throttler,
- $this->logger,
- );
- // Store current user
- $this->oldUser = \OC_User::getUser();
- }
- protected function tearDown(): void {
- \OC_User::setIncognitoMode(false);
- // Set old user
- \OC_User::setUserId($this->oldUser);
- \OC_Util::setupFS($this->oldUser);
- parent::tearDown();
- }
- public function testGetToken(): void {
- $this->request->method('getPathInfo')
- ->willReturn('/dav/files/GX9HSGQrGE');
- $result = $this->invokePrivate($this->auth, 'getToken');
- $this->assertSame('GX9HSGQrGE', $result);
- }
- public function testGetTokenInvalid(): void {
- $this->request->method('getPathInfo')
- ->willReturn('/dav/files');
- $this->expectException(\Sabre\DAV\Exception\NotFound::class);
- $this->invokePrivate($this->auth, 'getToken');
- }
- public function testCheckTokenValidShare(): void {
- $this->request->method('getPathInfo')
- ->willReturn('/dav/files/GX9HSGQrGE');
- $share = $this->getMockBuilder(IShare::class)
- ->disableOriginalConstructor()
- ->getMock();
- $share->method('getPassword')->willReturn(null);
- $this->shareManager->expects($this->once())
- ->method('getShareByToken')
- ->with('GX9HSGQrGE')
- ->willReturn($share);
- $result = $this->invokePrivate($this->auth, 'checkToken');
- $this->assertSame([true, 'principals/GX9HSGQrGE'], $result);
- }
- public function testCheckTokenInvalidShare(): void {
- $this->request->method('getPathInfo')
- ->willReturn('/dav/files/GX9HSGQrGE');
- $this->shareManager
- ->expects($this->once())
- ->method('getShareByToken')
- ->with('GX9HSGQrGE')
- ->will($this->throwException(new ShareNotFound()));
- $this->expectException(\Sabre\DAV\Exception\NotFound::class);
- $this->invokePrivate($this->auth, 'checkToken');
- }
- public function testCheckTokenAlreadyAuthenticated(): void {
- $this->request->method('getPathInfo')
- ->willReturn('/dav/files/GX9HSGQrGE');
- $share = $this->getMockBuilder(IShare::class)
- ->disableOriginalConstructor()
- ->getMock();
- $share->method('getShareType')->willReturn(42);
- $this->shareManager->expects($this->once())
- ->method('getShareByToken')
- ->with('GX9HSGQrGE')
- ->willReturn($share);
- $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, 'checkToken');
- $this->assertSame([true, 'principals/GX9HSGQrGE'], $result);
- }
- public function testCheckTokenPasswordNotAuthenticated(): void {
- $this->request->method('getPathInfo')
- ->willReturn('/dav/files/GX9HSGQrGE');
- $share = $this->getMockBuilder(IShare::class)
- ->disableOriginalConstructor()
- ->getMock();
- $share->method('getPassword')->willReturn('password');
- $share->method('getShareType')->willReturn(42);
- $this->shareManager->expects($this->once())
- ->method('getShareByToken')
- ->with('GX9HSGQrGE')
- ->willReturn($share);
- $this->session->method('exists')->with('public_link_authenticated')->willReturn(false);
-
- $this->expectException(\Sabre\DAV\Exception\NotAuthenticated::class);
- $this->invokePrivate($this->auth, 'checkToken');
- }
- public function testCheckTokenPasswordAuthenticatedWrongShare(): void {
- $this->request->method('getPathInfo')
- ->willReturn('/dav/files/GX9HSGQrGE');
- $share = $this->getMockBuilder(IShare::class)
- ->disableOriginalConstructor()
- ->getMock();
- $share->method('getPassword')->willReturn('password');
- $share->method('getShareType')->willReturn(42);
- $this->shareManager->expects($this->once())
- ->method('getShareByToken')
- ->with('GX9HSGQrGE')
- ->willReturn($share);
- $this->session->method('exists')->with('public_link_authenticated')->willReturn(false);
- $this->session->method('get')->with('public_link_authenticated')->willReturn('43');
-
- $this->expectException(\Sabre\DAV\Exception\NotAuthenticated::class);
- $this->invokePrivate($this->auth, 'checkToken');
- }
- public function testNoShare(): void {
- $this->request->method('getPathInfo')
- ->willReturn('/dav/files/GX9HSGQrGE');
- $this->shareManager->expects($this->once())
- ->method('getShareByToken')
- ->with('GX9HSGQrGE')
- ->willThrowException(new ShareNotFound());
- $result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
- $this->assertFalse($result);
- }
- public function testShareNoPassword(): void {
- $this->request->method('getPathInfo')
- ->willReturn('/dav/files/GX9HSGQrGE');
- $share = $this->getMockBuilder(IShare::class)
- ->disableOriginalConstructor()
- ->getMock();
- $share->method('getPassword')->willReturn(null);
- $this->shareManager->expects($this->once())
- ->method('getShareByToken')
- ->with('GX9HSGQrGE')
- ->willReturn($share);
- $result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
- $this->assertTrue($result);
- }
- public function testSharePasswordFancyShareType(): void {
- $this->request->method('getPathInfo')
- ->willReturn('/dav/files/GX9HSGQrGE');
- $share = $this->getMockBuilder(IShare::class)
- ->disableOriginalConstructor()
- ->getMock();
- $share->method('getPassword')->willReturn('password');
- $share->method('getShareType')->willReturn(42);
- $this->shareManager->expects($this->once())
- ->method('getShareByToken')
- ->with('GX9HSGQrGE')
- ->willReturn($share);
- $result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
- $this->assertFalse($result);
- }
- public function testSharePasswordRemote(): void {
- $this->request->method('getPathInfo')
- ->willReturn('/dav/files/GX9HSGQrGE');
- $share = $this->getMockBuilder(IShare::class)
- ->disableOriginalConstructor()
- ->getMock();
- $share->method('getPassword')->willReturn('password');
- $share->method('getShareType')->willReturn(IShare::TYPE_REMOTE);
- $this->shareManager->expects($this->once())
- ->method('getShareByToken')
- ->with('GX9HSGQrGE')
- ->willReturn($share);
- $result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
- $this->assertTrue($result);
- }
- public function testSharePasswordLinkValidPassword(): void {
- $this->request->method('getPathInfo')
- ->willReturn('/dav/files/GX9HSGQrGE');
- $share = $this->getMockBuilder(IShare::class)
- ->disableOriginalConstructor()
- ->getMock();
- $share->method('getPassword')->willReturn('password');
- $share->method('getShareType')->willReturn(IShare::TYPE_LINK);
- $this->shareManager->expects($this->once())
- ->method('getShareByToken')
- ->with('GX9HSGQrGE')
- ->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 testSharePasswordMailValidPassword(): void {
- $this->request->method('getPathInfo')
- ->willReturn('/dav/files/GX9HSGQrGE');
- $share = $this->getMockBuilder(IShare::class)
- ->disableOriginalConstructor()
- ->getMock();
- $share->method('getPassword')->willReturn('password');
- $share->method('getShareType')->willReturn(IShare::TYPE_EMAIL);
- $this->shareManager->expects($this->once())
- ->method('getShareByToken')
- ->with('GX9HSGQrGE')
- ->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 testInvalidSharePasswordLinkValidSession(): void {
- $this->request->method('getPathInfo')
- ->willReturn('/dav/files/GX9HSGQrGE');
- $share = $this->getMockBuilder(IShare::class)
- ->disableOriginalConstructor()
- ->getMock();
- $share->method('getPassword')->willReturn('password');
- $share->method('getShareType')->willReturn(IShare::TYPE_LINK);
- $share->method('getId')->willReturn('42');
- $this->shareManager->expects($this->once())
- ->method('getShareByToken')
- ->with('GX9HSGQrGE')
- ->willReturn($share);
- $this->shareManager->expects($this->once())
- ->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(): void {
- $this->request->method('getPathInfo')
- ->willReturn('/dav/files/GX9HSGQrGE');
- $share = $this->getMockBuilder(IShare::class)
- ->disableOriginalConstructor()
- ->getMock();
- $share->method('getPassword')->willReturn('password');
- $share->method('getShareType')->willReturn(IShare::TYPE_LINK);
- $share->method('getId')->willReturn('42');
- $this->shareManager->expects($this->once())
- ->method('getShareByToken')
- ->with('GX9HSGQrGE')
- ->willReturn($share);
- $this->shareManager->expects($this->once())
- ->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);
- }
- public function testSharePasswordMailInvalidSession(): void {
- $this->request->method('getPathInfo')
- ->willReturn('/dav/files/GX9HSGQrGE');
- $share = $this->getMockBuilder(IShare::class)
- ->disableOriginalConstructor()
- ->getMock();
- $share->method('getPassword')->willReturn('password');
- $share->method('getShareType')->willReturn(IShare::TYPE_EMAIL);
- $share->method('getId')->willReturn('42');
- $this->shareManager->expects($this->once())
- ->method('getShareByToken')
- ->with('GX9HSGQrGE')
- ->willReturn($share);
- $this->shareManager->expects($this->once())
- ->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);
- }
- }
|