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;
|
2016-06-27 16:23:52 +03:00
|
|
|
use OC\Authentication\Token\DefaultToken;
|
2016-09-07 21:29:27 +03:00
|
|
|
use OC\Authentication\Token\IProvider;
|
2016-05-18 19:25:05 +03:00
|
|
|
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;
|
2016-09-07 21:29:27 +03:00
|
|
|
use OCP\IRequest;
|
|
|
|
use OCP\ISession;
|
|
|
|
use OCP\IUser;
|
|
|
|
use OCP\IUserManager;
|
|
|
|
use OCP\Security\ISecureRandom;
|
2016-05-18 19:25:05 +03:00
|
|
|
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;
|
2016-11-11 13:35:11 +03:00
|
|
|
/** @var IProvider|\PHPUnit_Framework_MockObject_MockObject */
|
2016-05-18 13:03:22 +03:00
|
|
|
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();
|
|
|
|
|
2016-09-07 21:29:27 +03:00
|
|
|
$this->request = $this->createMock(IRequest::class);
|
|
|
|
$this->tokenProvider = $this->createMock(IProvider::class);
|
|
|
|
$this->userManager = $this->createMock(IUserManager::class);
|
|
|
|
$this->session = $this->createMock(ISession::class);
|
|
|
|
$this->secureRandom = $this->createMock(ISecureRandom::class);
|
2016-05-18 13:03:22 +03:00
|
|
|
$this->uid = 'jane';
|
2016-09-07 21:29:27 +03:00
|
|
|
$this->user = $this->createMock(IUser::class);
|
2016-05-18 13:03:22 +03:00
|
|
|
|
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() {
|
2016-06-27 16:23:52 +03:00
|
|
|
$token1 = new DefaultToken();
|
|
|
|
$token1->setId(100);
|
|
|
|
$token2 = new DefaultToken();
|
|
|
|
$token2->setId(200);
|
|
|
|
$tokens = [
|
|
|
|
$token1,
|
|
|
|
$token2,
|
2016-05-18 13:03:22 +03:00
|
|
|
];
|
2016-06-27 16:23:52 +03:00
|
|
|
$sessionToken = new DefaultToken();
|
|
|
|
$sessionToken->setId(100);
|
|
|
|
|
2016-05-18 13:03:22 +03:00
|
|
|
$this->tokenProvider->expects($this->once())
|
|
|
|
->method('getTokenByUser')
|
2018-05-29 10:29:29 +03:00
|
|
|
->with($this->uid)
|
2016-06-27 16:23:52 +03:00
|
|
|
->will($this->returnValue($tokens));
|
|
|
|
$this->session->expects($this->once())
|
|
|
|
->method('getId')
|
|
|
|
->will($this->returnValue('session123'));
|
|
|
|
$this->tokenProvider->expects($this->once())
|
|
|
|
->method('getToken')
|
|
|
|
->with('session123')
|
|
|
|
->will($this->returnValue($sessionToken));
|
2016-05-18 13:03:22 +03:00
|
|
|
|
2016-06-27 16:23:52 +03:00
|
|
|
$this->assertEquals([
|
|
|
|
[
|
|
|
|
'id' => 100,
|
|
|
|
'name' => null,
|
2016-10-31 16:26:29 +03:00
|
|
|
'lastActivity' => 0,
|
|
|
|
'type' => 0,
|
2016-06-27 16:23:52 +03:00
|
|
|
'canDelete' => false,
|
2016-08-15 18:15:14 +03:00
|
|
|
'current' => true,
|
2016-10-31 16:01:31 +03:00
|
|
|
'scope' => ['filesystem' => true]
|
2016-06-27 16:23:52 +03:00
|
|
|
],
|
|
|
|
[
|
|
|
|
'id' => 200,
|
|
|
|
'name' => null,
|
2016-10-31 16:26:29 +03:00
|
|
|
'lastActivity' => 0,
|
|
|
|
'type' => 0,
|
2016-06-27 16:23:52 +03:00
|
|
|
'canDelete' => true,
|
2016-10-31 16:01:31 +03:00
|
|
|
'scope' => ['filesystem' => true]
|
2016-06-27 16:23:52 +03:00
|
|
|
]
|
|
|
|
], $this->controller->index());
|
2016-05-18 13:03:22 +03:00
|
|
|
}
|
|
|
|
|
2016-05-18 19:25:05 +03:00
|
|
|
public function testCreate() {
|
|
|
|
$name = 'Nexus 4';
|
2016-09-07 21:29:27 +03:00
|
|
|
$sessionToken = $this->createMock(IToken::class);
|
|
|
|
$deviceToken = $this->createMock(IToken::class);
|
2016-05-18 19:25:05 +03:00
|
|
|
$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
|
|
|
|
2017-05-08 00:10:02 +03:00
|
|
|
$this->secureRandom->expects($this->exactly(5))
|
2016-05-18 19:25:05 +03:00
|
|
|
->method('generate')
|
2017-05-08 00:10:02 +03:00
|
|
|
->with(5, ISecureRandom::CHAR_HUMAN_READABLE)
|
2016-05-18 19:25:05 +03:00
|
|
|
->will($this->returnValue('XXXXX'));
|
2017-05-08 00:10:02 +03:00
|
|
|
$newToken = 'XXXXX-XXXXX-XXXXX-XXXXX-XXXXX';
|
2016-05-18 19:25:05 +03:00
|
|
|
|
|
|
|
$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));
|
|
|
|
|
2016-10-31 16:01:31 +03:00
|
|
|
$deviceToken->expects($this->once())
|
|
|
|
->method('jsonSerialize')
|
|
|
|
->will($this->returnValue(['dummy' => 'dummy', 'canDelete' => true]));
|
|
|
|
|
2016-05-18 19:25:05 +03:00
|
|
|
$expected = [
|
|
|
|
'token' => $newToken,
|
2016-10-31 16:01:31 +03:00
|
|
|
'deviceToken' => ['dummy' => 'dummy', 'canDelete' => true],
|
2016-06-27 16:23:52 +03:00
|
|
|
'loginName' => 'User13',
|
2016-05-18 19:25:05 +03:00
|
|
|
];
|
2016-11-02 17:16:14 +03:00
|
|
|
$response = $this->controller->create($name);
|
|
|
|
$this->assertInstanceOf(JSONResponse::class, $response);
|
|
|
|
$this->assertEquals($expected, $response->getData());
|
2016-05-18 19:25:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2016-09-07 21:29:27 +03:00
|
|
|
$user = $this->createMock(IUser::class);
|
2016-05-19 12:20:22 +03:00
|
|
|
|
|
|
|
$this->tokenProvider->expects($this->once())
|
|
|
|
->method('invalidateTokenById')
|
2018-05-29 10:29:29 +03:00
|
|
|
->with($this->uid, $id);
|
2016-05-19 12:20:22 +03:00
|
|
|
|
|
|
|
$this->assertEquals([], $this->controller->destroy($id));
|
|
|
|
}
|
|
|
|
|
2016-11-11 13:35:11 +03:00
|
|
|
public function testUpdateToken() {
|
|
|
|
$token = $this->createMock(DefaultToken::class);
|
|
|
|
|
|
|
|
$this->tokenProvider->expects($this->once())
|
|
|
|
->method('getTokenById')
|
|
|
|
->with($this->equalTo(42))
|
|
|
|
->willReturn($token);
|
|
|
|
|
2018-01-09 13:41:08 +03:00
|
|
|
$token->expects($this->once())
|
|
|
|
->method('getUID')
|
|
|
|
->willReturn('jane');
|
|
|
|
|
2016-11-11 13:35:11 +03:00
|
|
|
$token->expects($this->once())
|
|
|
|
->method('setScope')
|
|
|
|
->with($this->equalTo([
|
2016-11-16 18:27:56 +03:00
|
|
|
'filesystem' => true
|
2016-11-11 13:35:11 +03:00
|
|
|
]));
|
|
|
|
|
|
|
|
$this->tokenProvider->expects($this->once())
|
|
|
|
->method('updateToken')
|
|
|
|
->with($this->equalTo($token));
|
|
|
|
|
2016-11-16 18:27:56 +03:00
|
|
|
$this->assertSame([], $this->controller->update(42, ['filesystem' => true]));
|
2016-11-11 13:35:11 +03:00
|
|
|
}
|
|
|
|
|
2018-01-09 13:41:08 +03:00
|
|
|
public function testUpdateTokenWrongUser() {
|
|
|
|
$token = $this->createMock(DefaultToken::class);
|
|
|
|
|
|
|
|
$this->tokenProvider->expects($this->once())
|
|
|
|
->method('getTokenById')
|
|
|
|
->with($this->equalTo(42))
|
|
|
|
->willReturn($token);
|
|
|
|
|
|
|
|
$token->expects($this->once())
|
|
|
|
->method('getUID')
|
|
|
|
->willReturn('foobar');
|
|
|
|
|
|
|
|
$token->expects($this->never())
|
|
|
|
->method('setScope');
|
|
|
|
$this->tokenProvider->expects($this->never())
|
|
|
|
->method('updateToken');
|
|
|
|
|
|
|
|
$response = $this->controller->update(42, ['filesystem' => true]);
|
|
|
|
$this->assertSame([], $response->getData());
|
|
|
|
$this->assertSame(\OCP\AppFramework\Http::STATUS_NOT_FOUND, $response->getStatus());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testUpdateTokenNonExisting() {
|
|
|
|
$this->tokenProvider->expects($this->once())
|
|
|
|
->method('getTokenById')
|
|
|
|
->with($this->equalTo(42))
|
|
|
|
->willThrowException(new InvalidTokenException('Token does not exist'));
|
|
|
|
|
|
|
|
$this->tokenProvider->expects($this->never())
|
|
|
|
->method('updateToken');
|
|
|
|
|
|
|
|
$response = $this->controller->update(42, ['filesystem' => true]);
|
|
|
|
$this->assertSame([], $response->getData());
|
|
|
|
$this->assertSame(\OCP\AppFramework\Http::STATUS_NOT_FOUND, $response->getStatus());
|
|
|
|
}
|
|
|
|
|
2016-05-18 13:03:22 +03:00
|
|
|
}
|