summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorVincent Petry <pvince81@owncloud.com>2016-05-23 14:17:01 +0200
committerVincent Petry <pvince81@owncloud.com>2016-05-23 14:17:01 +0200
commit57525a02f8e0153dcb8c83e0e8b7bafe34a3f820 (patch)
treed6aaafba86dbe9df75fabe369ad6b53ef62234cf /tests
parentbd87f6747376063b05ad5f1f7ce12446dddd5697 (diff)
parent74277c25be2f3231e52a73a684bd14452a9ff2aa (diff)
downloadnextcloud-server-57525a02f8e0153dcb8c83e0e8b7bafe34a3f820.tar.gz
nextcloud-server-57525a02f8e0153dcb8c83e0e8b7bafe34a3f820.zip
Merge pull request #24703 from owncloud/personal-settings-auth-tokens
Personal settings auth tokens
Diffstat (limited to 'tests')
-rw-r--r--tests/lib/Authentication/Token/DefaultTokenMapperTest.php27
-rw-r--r--tests/lib/Authentication/Token/DefaultTokenProviderTest.php11
-rw-r--r--tests/settings/controller/AuthSettingsControllerTest.php156
3 files changed, 194 insertions, 0 deletions
diff --git a/tests/lib/Authentication/Token/DefaultTokenMapperTest.php b/tests/lib/Authentication/Token/DefaultTokenMapperTest.php
index e17149a5c1b..9179e23bfb2 100644
--- a/tests/lib/Authentication/Token/DefaultTokenMapperTest.php
+++ b/tests/lib/Authentication/Token/DefaultTokenMapperTest.php
@@ -159,4 +159,31 @@ class DefaultTokenMapperTest extends TestCase {
$this->assertCount(0, $this->mapper->getTokenByUser($user));
}
+ public function testDeleteById() {
+ $user = $this->getMock('\OCP\IUser');
+ $qb = $this->dbConnection->getQueryBuilder();
+ $qb->select('id')
+ ->from('authtoken')
+ ->where($qb->expr()->eq('token', $qb->createNamedParameter('9c5a2e661482b65597408a6bb6c4a3d1af36337381872ac56e445a06cdb7fea2b1039db707545c11027a4966919918b19d875a8b774840b18c6cbb7ae56fe206')));
+ $result = $qb->execute();
+ $id = $result->fetch()['id'];
+ $user->expects($this->once())
+ ->method('getUID')
+ ->will($this->returnValue('user1'));
+
+ $this->mapper->deleteById($user, $id);
+ $this->assertEquals(2, $this->getNumberOfTokens());
+ }
+
+ public function testDeleteByIdWrongUser() {
+ $user = $this->getMock('\OCP\IUser');
+ $id = 33;
+ $user->expects($this->once())
+ ->method('getUID')
+ ->will($this->returnValue('user10000'));
+
+ $this->mapper->deleteById($user, $id);
+ $this->assertEquals(3, $this->getNumberOfTokens());
+ }
+
}
diff --git a/tests/lib/Authentication/Token/DefaultTokenProviderTest.php b/tests/lib/Authentication/Token/DefaultTokenProviderTest.php
index eeb249cfa8a..8af5e1e933a 100644
--- a/tests/lib/Authentication/Token/DefaultTokenProviderTest.php
+++ b/tests/lib/Authentication/Token/DefaultTokenProviderTest.php
@@ -170,6 +170,17 @@ class DefaultTokenProviderTest extends TestCase {
$this->tokenProvider->invalidateToken('token7');
}
+ public function testInvaildateTokenById() {
+ $id = 123;
+ $user = $this->getMock('\OCP\IUser');
+
+ $this->mapper->expects($this->once())
+ ->method('deleteById')
+ ->with($user, $id);
+
+ $this->tokenProvider->invalidateTokenById($user, $id);
+ }
+
public function testInvalidateOldTokens() {
$defaultSessionLifetime = 60 * 60 * 24;
$this->config->expects($this->once())
diff --git a/tests/settings/controller/AuthSettingsControllerTest.php b/tests/settings/controller/AuthSettingsControllerTest.php
new file mode 100644
index 00000000000..49491c8ff52
--- /dev/null
+++ b/tests/settings/controller/AuthSettingsControllerTest.php
@@ -0,0 +1,156 @@
+<?php
+
+/**
+ * @author Christoph Wurst <christoph@owncloud.com>
+ *
+ * @copyright Copyright (c) 2016, ownCloud, Inc.
+ * @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 Test\Settings\Controller;
+
+use OC\AppFramework\Http;
+use OC\Authentication\Exceptions\InvalidTokenException;
+use OC\Authentication\Token\IToken;
+use OC\Settings\Controller\AuthSettingsController;
+use OCP\AppFramework\Http\JSONResponse;
+use OCP\Session\Exceptions\SessionNotAvailableException;
+use Test\TestCase;
+
+class AuthSettingsControllerTest extends TestCase {
+
+ /** @var AuthSettingsController */
+ private $controller;
+ private $request;
+ private $tokenProvider;
+ private $userManager;
+ private $session;
+ private $secureRandom;
+ private $uid;
+
+ protected function setUp() {
+ parent::setUp();
+
+ $this->request = $this->getMock('\OCP\IRequest');
+ $this->tokenProvider = $this->getMock('\OC\Authentication\Token\IProvider');
+ $this->userManager = $this->getMock('\OCP\IUserManager');
+ $this->session = $this->getMock('\OCP\ISession');
+ $this->secureRandom = $this->getMock('\OCP\Security\ISecureRandom');
+ $this->uid = 'jane';
+ $this->user = $this->getMock('\OCP\IUser');
+
+ $this->controller = new AuthSettingsController('core', $this->request, $this->tokenProvider, $this->userManager, $this->session, $this->secureRandom, $this->uid);
+ }
+
+ public function testIndex() {
+ $result = [
+ 'token1',
+ 'token2',
+ ];
+ $this->userManager->expects($this->once())
+ ->method('get')
+ ->with($this->uid)
+ ->will($this->returnValue($this->user));
+ $this->tokenProvider->expects($this->once())
+ ->method('getTokenByUser')
+ ->with($this->user)
+ ->will($this->returnValue($result));
+
+ $this->assertEquals($result, $this->controller->index());
+ }
+
+ public function testCreate() {
+ $name = 'Nexus 4';
+ $sessionToken = $this->getMock('\OC\Authentication\Token\IToken');
+ $deviceToken = $this->getMock('\OC\Authentication\Token\IToken');
+ $password = '123456';
+
+ $this->session->expects($this->once())
+ ->method('getId')
+ ->will($this->returnValue('sessionid'));
+ $this->tokenProvider->expects($this->once())
+ ->method('getToken')
+ ->with('sessionid')
+ ->will($this->returnValue($sessionToken));
+ $this->tokenProvider->expects($this->once())
+ ->method('getPassword')
+ ->with($sessionToken, 'sessionid')
+ ->will($this->returnValue($password));
+
+ $this->secureRandom->expects($this->exactly(4))
+ ->method('generate')
+ ->with(5, implode('', range('A', 'Z')))
+ ->will($this->returnValue('XXXXX'));
+ $newToken = 'XXXXX-XXXXX-XXXXX-XXXXX';
+
+ $this->tokenProvider->expects($this->once())
+ ->method('generateToken')
+ ->with($newToken, $this->uid, $password, $name, IToken::PERMANENT_TOKEN)
+ ->will($this->returnValue($deviceToken));
+
+ $expected = [
+ 'token' => $newToken,
+ 'deviceToken' => $deviceToken,
+ ];
+ $this->assertEquals($expected, $this->controller->create($name));
+ }
+
+ public function testCreateSessionNotAvailable() {
+ $name = 'personal phone';
+
+ $this->session->expects($this->once())
+ ->method('getId')
+ ->will($this->throwException(new SessionNotAvailableException()));
+
+ $expected = new JSONResponse();
+ $expected->setStatus(Http::STATUS_SERVICE_UNAVAILABLE);
+
+ $this->assertEquals($expected, $this->controller->create($name));
+ }
+
+ public function testCreateInvalidToken() {
+ $name = 'Company IPhone';
+
+ $this->session->expects($this->once())
+ ->method('getId')
+ ->will($this->returnValue('sessionid'));
+ $this->tokenProvider->expects($this->once())
+ ->method('getToken')
+ ->with('sessionid')
+ ->will($this->throwException(new InvalidTokenException()));
+
+ $expected = new JSONResponse();
+ $expected->setStatus(Http::STATUS_SERVICE_UNAVAILABLE);
+
+ $this->assertEquals($expected, $this->controller->create($name));
+ }
+
+ public function testDestroy() {
+ $id = 123;
+ $user = $this->getMock('\OCP\IUser');
+
+ $this->userManager->expects($this->once())
+ ->method('get')
+ ->with($this->uid)
+ ->will($this->returnValue($user));
+ $this->tokenProvider->expects($this->once())
+ ->method('invalidateTokenById')
+ ->with($user, $id);
+
+ $this->assertEquals([], $this->controller->destroy($id));
+ }
+
+}