2016-05-18 13:03:22 +03:00
|
|
|
<?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;
|
|
|
|
|
2016-05-18 19:25:05 +03:00
|
|
|
use OC\AppFramework\Http;
|
|
|
|
use OC\Authentication\Exceptions\InvalidTokenException;
|
|
|
|
use OC\Authentication\Token\IToken;
|
2016-05-18 13:03:22 +03:00
|
|
|
use OC\Settings\Controller\AuthSettingsController;
|
2016-05-18 19:25:05 +03:00
|
|
|
use OCP\AppFramework\Http\JSONResponse;
|
|
|
|
use OCP\Session\Exceptions\SessionNotAvailableException;
|
2016-05-18 13:03:22 +03:00
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
class AuthSettingsControllerTest extends TestCase {
|
|
|
|
|
|
|
|
/** @var AuthSettingsController */
|
|
|
|
private $controller;
|
|
|
|
private $request;
|
|
|
|
private $tokenProvider;
|
|
|
|
private $userManager;
|
2016-05-18 19:25:05 +03:00
|
|
|
private $session;
|
|
|
|
private $secureRandom;
|
2016-05-18 13:03:22 +03:00
|
|
|
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');
|
2016-05-18 19:25:05 +03:00
|
|
|
$this->session = $this->getMock('\OCP\ISession');
|
|
|
|
$this->secureRandom = $this->getMock('\OCP\Security\ISecureRandom');
|
2016-05-18 13:03:22 +03:00
|
|
|
$this->uid = 'jane';
|
|
|
|
$this->user = $this->getMock('\OCP\IUser');
|
|
|
|
|
2016-05-18 19:25:05 +03:00
|
|
|
$this->controller = new AuthSettingsController('core', $this->request, $this->tokenProvider, $this->userManager, $this->session, $this->secureRandom, $this->uid);
|
2016-05-18 13:03:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2016-05-18 19:25:05 +03:00
|
|
|
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));
|
2016-05-24 11:50:18 +03:00
|
|
|
$sessionToken->expects($this->once())
|
|
|
|
->method('getLoginName')
|
|
|
|
->will($this->returnValue('User13'));
|
2016-05-18 19:25:05 +03:00
|
|
|
|
|
|
|
$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')
|
2016-05-24 11:50:18 +03:00
|
|
|
->with($newToken, $this->uid, 'User13', $password, $name, IToken::PERMANENT_TOKEN)
|
2016-05-18 19:25:05 +03:00
|
|
|
->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));
|
|
|
|
}
|
|
|
|
|
2016-05-19 12:20:22 +03:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2016-05-18 13:03:22 +03:00
|
|
|
}
|