2016-05-18 13:03:22 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
2019-12-03 21:57:53 +03:00
|
|
|
*
|
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
|
|
|
* @author Daniel Kesselberg <mail@danielkesselberg.de>
|
|
|
|
* @author Fabrizio Steiner <fabrizio.steiner@gmail.com>
|
|
|
|
* @author Greta Doci <gretadoci@gmail.com>
|
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
* @author Sergej Nikolaev <kinolaev@gmail.com>
|
|
|
|
*
|
2016-05-18 13:03:22 +03:00
|
|
|
* @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,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2016-05-18 13:03:22 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Test\Settings\Controller;
|
|
|
|
|
2016-05-18 19:25:05 +03:00
|
|
|
use OC\AppFramework\Http;
|
2019-10-04 03:28:48 +03:00
|
|
|
use OC\Authentication\Exceptions\ExpiredTokenException;
|
2019-11-22 22:52:10 +03:00
|
|
|
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;
|
2020-03-16 10:52:46 +03:00
|
|
|
use OC\Authentication\Token\IWipeableToken;
|
2019-07-03 10:44:37 +03:00
|
|
|
use OC\Authentication\Token\RemoteWipe;
|
2019-09-17 17:33:27 +03:00
|
|
|
use OCA\Settings\Controller\AuthSettingsController;
|
2019-01-12 19:48:27 +03:00
|
|
|
use OCP\Activity\IEvent;
|
|
|
|
use OCP\Activity\IManager;
|
2016-05-18 19:25:05 +03:00
|
|
|
use OCP\AppFramework\Http\JSONResponse;
|
2019-01-12 19:48:27 +03:00
|
|
|
use OCP\ILogger;
|
2016-09-07 21:29:27 +03:00
|
|
|
use OCP\IRequest;
|
|
|
|
use OCP\ISession;
|
2019-09-11 15:57:11 +03:00
|
|
|
use OCP\IUserSession;
|
2016-09-07 21:29:27 +03:00
|
|
|
use OCP\Security\ISecureRandom;
|
2016-05-18 19:25:05 +03:00
|
|
|
use OCP\Session\Exceptions\SessionNotAvailableException;
|
2019-07-03 10:44:37 +03:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2016-05-18 13:03:22 +03:00
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
class AuthSettingsControllerTest extends TestCase {
|
|
|
|
|
|
|
|
/** @var AuthSettingsController */
|
|
|
|
private $controller;
|
2019-07-03 10:44:37 +03:00
|
|
|
/** @var IRequest|MockObject */
|
2016-05-18 13:03:22 +03:00
|
|
|
private $request;
|
2019-07-03 10:44:37 +03:00
|
|
|
/** @var IProvider|MockObject */
|
2016-05-18 13:03:22 +03:00
|
|
|
private $tokenProvider;
|
2019-07-03 10:44:37 +03:00
|
|
|
/** @var ISession|MockObject */
|
2016-05-18 19:25:05 +03:00
|
|
|
private $session;
|
2019-09-11 15:57:11 +03:00
|
|
|
/**@var IUserSession|MockObject */
|
|
|
|
private $userSession;
|
2019-07-03 10:44:37 +03:00
|
|
|
/** @var ISecureRandom|MockObject */
|
2016-05-18 19:25:05 +03:00
|
|
|
private $secureRandom;
|
2019-07-03 10:44:37 +03:00
|
|
|
/** @var IManager|MockObject */
|
2019-01-22 20:12:03 +03:00
|
|
|
private $activityManager;
|
2019-07-03 10:44:37 +03:00
|
|
|
/** @var RemoteWipe|MockObject */
|
|
|
|
private $remoteWipe;
|
2019-01-22 20:12:03 +03:00
|
|
|
private $uid = 'jane';
|
2016-05-18 13:03:22 +03:00
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
protected function setUp(): void {
|
2016-05-18 13:03:22 +03:00
|
|
|
parent::setUp();
|
|
|
|
|
2016-09-07 21:29:27 +03:00
|
|
|
$this->request = $this->createMock(IRequest::class);
|
|
|
|
$this->tokenProvider = $this->createMock(IProvider::class);
|
|
|
|
$this->session = $this->createMock(ISession::class);
|
2019-09-11 15:57:11 +03:00
|
|
|
$this->userSession = $this->createMock(IUserSession::class);
|
2016-09-07 21:29:27 +03:00
|
|
|
$this->secureRandom = $this->createMock(ISecureRandom::class);
|
2019-01-22 20:12:03 +03:00
|
|
|
$this->activityManager = $this->createMock(IManager::class);
|
2019-07-03 10:44:37 +03:00
|
|
|
$this->remoteWipe = $this->createMock(RemoteWipe::class);
|
|
|
|
/** @var ILogger|MockObject $logger */
|
2019-01-12 19:48:27 +03:00
|
|
|
$logger = $this->createMock(ILogger::class);
|
|
|
|
|
2019-01-22 20:12:03 +03:00
|
|
|
$this->controller = new AuthSettingsController(
|
|
|
|
'core',
|
|
|
|
$this->request,
|
|
|
|
$this->tokenProvider,
|
|
|
|
$this->session,
|
|
|
|
$this->secureRandom,
|
|
|
|
$this->uid,
|
2019-09-11 15:57:11 +03:00
|
|
|
$this->userSession,
|
2019-01-22 20:12:03 +03:00
|
|
|
$this->activityManager,
|
2019-07-03 10:44:37 +03:00
|
|
|
$this->remoteWipe,
|
2019-01-22 20:12:03 +03:00
|
|
|
$logger
|
|
|
|
);
|
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')
|
2019-01-22 20:12:03 +03:00
|
|
|
->willReturn('sessionid');
|
2016-05-18 19:25:05 +03:00
|
|
|
$this->tokenProvider->expects($this->once())
|
|
|
|
->method('getToken')
|
|
|
|
->with('sessionid')
|
2019-01-22 20:12:03 +03:00
|
|
|
->willReturn($sessionToken);
|
2016-05-18 19:25:05 +03:00
|
|
|
$this->tokenProvider->expects($this->once())
|
|
|
|
->method('getPassword')
|
|
|
|
->with($sessionToken, 'sessionid')
|
2019-01-22 20:12:03 +03:00
|
|
|
->willReturn($password);
|
2016-05-24 11:50:18 +03:00
|
|
|
$sessionToken->expects($this->once())
|
|
|
|
->method('getLoginName')
|
2019-01-22 20:12:03 +03:00
|
|
|
->willReturn('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)
|
2019-01-22 20:12:03 +03:00
|
|
|
->willReturn('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)
|
2019-01-22 20:12:03 +03:00
|
|
|
->willReturn($deviceToken);
|
2016-05-18 19:25:05 +03:00
|
|
|
|
2016-10-31 16:01:31 +03:00
|
|
|
$deviceToken->expects($this->once())
|
|
|
|
->method('jsonSerialize')
|
2019-01-22 20:12:03 +03:00
|
|
|
->willReturn(['dummy' => 'dummy', 'canDelete' => true]);
|
|
|
|
|
|
|
|
$this->mockActivityManager();
|
2016-10-31 16:01:31 +03:00
|
|
|
|
2016-05-18 19:25:05 +03:00
|
|
|
$expected = [
|
|
|
|
'token' => $newToken,
|
2019-02-02 14:01:27 +03:00
|
|
|
'deviceToken' => ['dummy' => 'dummy', 'canDelete' => true, 'canRename' => true],
|
2016-06-27 16:23:52 +03:00
|
|
|
'loginName' => 'User13',
|
2016-05-18 19:25:05 +03:00
|
|
|
];
|
2019-01-22 20:12:03 +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')
|
2019-01-22 20:12:03 +03:00
|
|
|
->willReturn('sessionid');
|
2016-05-18 19:25:05 +03:00
|
|
|
$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() {
|
2019-01-22 20:12:03 +03:00
|
|
|
$tokenId = 124;
|
|
|
|
$token = $this->createMock(DefaultToken::class);
|
|
|
|
|
|
|
|
$this->mockGetTokenById($tokenId, $token);
|
|
|
|
$this->mockActivityManager();
|
|
|
|
|
|
|
|
$token->expects($this->exactly(2))
|
|
|
|
->method('getId')
|
|
|
|
->willReturn($tokenId);
|
|
|
|
|
|
|
|
$token->expects($this->once())
|
|
|
|
->method('getUID')
|
|
|
|
->willReturn('jane');
|
2016-05-19 12:20:22 +03:00
|
|
|
|
|
|
|
$this->tokenProvider->expects($this->once())
|
|
|
|
->method('invalidateTokenById')
|
2019-01-22 20:12:03 +03:00
|
|
|
->with($this->uid, $tokenId);
|
|
|
|
|
|
|
|
$this->assertEquals([], $this->controller->destroy($tokenId));
|
|
|
|
}
|
|
|
|
|
2019-10-04 03:28:48 +03:00
|
|
|
public function testDestroyExpired() {
|
|
|
|
$tokenId = 124;
|
|
|
|
$token = $this->createMock(DefaultToken::class);
|
|
|
|
|
|
|
|
$token->expects($this->exactly(2))
|
|
|
|
->method('getId')
|
|
|
|
->willReturn($tokenId);
|
|
|
|
|
|
|
|
$token->expects($this->once())
|
|
|
|
->method('getUID')
|
|
|
|
->willReturn($this->uid);
|
|
|
|
|
|
|
|
$this->tokenProvider->expects($this->once())
|
|
|
|
->method('getTokenById')
|
|
|
|
->with($this->equalTo($tokenId))
|
|
|
|
->willThrowException(new ExpiredTokenException($token));
|
|
|
|
|
|
|
|
$this->tokenProvider->expects($this->once())
|
|
|
|
->method('invalidateTokenById')
|
|
|
|
->with($this->uid, $tokenId);
|
|
|
|
|
|
|
|
$this->assertSame([], $this->controller->destroy($tokenId));
|
|
|
|
}
|
|
|
|
|
2019-01-22 20:12:03 +03:00
|
|
|
public function testDestroyWrongUser() {
|
|
|
|
$tokenId = 124;
|
|
|
|
$token = $this->createMock(DefaultToken::class);
|
|
|
|
|
|
|
|
$this->mockGetTokenById($tokenId, $token);
|
|
|
|
|
|
|
|
$token->expects($this->once())
|
|
|
|
->method('getUID')
|
|
|
|
->willReturn('foobar');
|
2016-05-19 12:20:22 +03:00
|
|
|
|
2019-01-22 20:12:03 +03:00
|
|
|
$response = $this->controller->destroy($tokenId);
|
|
|
|
$this->assertSame([], $response->getData());
|
|
|
|
$this->assertSame(\OCP\AppFramework\Http::STATUS_NOT_FOUND, $response->getStatus());
|
2016-05-19 12:20:22 +03:00
|
|
|
}
|
|
|
|
|
2019-02-13 20:48:42 +03:00
|
|
|
public function dataRenameToken(): array {
|
|
|
|
return [
|
|
|
|
'App password => Other token name' => ['App password', 'Other token name'],
|
|
|
|
'Other token name => App password' => ['Other token name', 'App password'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataRenameToken
|
2019-07-03 10:44:37 +03:00
|
|
|
*
|
2019-02-13 20:48:42 +03:00
|
|
|
* @param string $name
|
|
|
|
* @param string $newName
|
|
|
|
*/
|
|
|
|
public function testUpdateRename(string $name, string $newName): void {
|
|
|
|
$tokenId = 42;
|
|
|
|
$token = $this->createMock(DefaultToken::class);
|
|
|
|
|
|
|
|
$this->mockGetTokenById($tokenId, $token);
|
|
|
|
$this->mockActivityManager();
|
|
|
|
|
|
|
|
$token->expects($this->once())
|
|
|
|
->method('getUID')
|
|
|
|
->willReturn('jane');
|
|
|
|
|
|
|
|
$token->expects($this->once())
|
|
|
|
->method('getName')
|
|
|
|
->willReturn($name);
|
2019-01-22 20:12:03 +03:00
|
|
|
|
2019-02-13 20:48:42 +03:00
|
|
|
$token->expects($this->once())
|
|
|
|
->method('getScopeAsArray')
|
|
|
|
->willReturn(['filesystem' => true]);
|
|
|
|
|
|
|
|
$token->expects($this->once())
|
|
|
|
->method('setName')
|
|
|
|
->with($this->equalTo($newName));
|
|
|
|
|
|
|
|
$this->tokenProvider->expects($this->once())
|
|
|
|
->method('updateToken')
|
|
|
|
->with($this->equalTo($token));
|
|
|
|
|
|
|
|
$this->assertSame([], $this->controller->update($tokenId, ['filesystem' => true], $newName));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dataUpdateFilesystemScope(): array {
|
|
|
|
return [
|
|
|
|
'Grant filesystem access' => [false, true],
|
|
|
|
'Revoke filesystem access' => [true, false],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataUpdateFilesystemScope
|
2019-07-03 10:44:37 +03:00
|
|
|
*
|
2019-02-13 20:48:42 +03:00
|
|
|
* @param bool $filesystem
|
|
|
|
* @param bool $newFilesystem
|
|
|
|
*/
|
|
|
|
public function testUpdateFilesystemScope(bool $filesystem, bool $newFilesystem): void {
|
2019-01-22 20:12:03 +03:00
|
|
|
$tokenId = 42;
|
2016-11-11 13:35:11 +03:00
|
|
|
$token = $this->createMock(DefaultToken::class);
|
|
|
|
|
2019-01-22 20:12:03 +03:00
|
|
|
$this->mockGetTokenById($tokenId, $token);
|
|
|
|
$this->mockActivityManager();
|
2016-11-11 13:35:11 +03:00
|
|
|
|
2018-01-09 13:41:08 +03:00
|
|
|
$token->expects($this->once())
|
|
|
|
->method('getUID')
|
|
|
|
->willReturn('jane');
|
|
|
|
|
2019-02-13 20:48:42 +03:00
|
|
|
$token->expects($this->once())
|
|
|
|
->method('getName')
|
|
|
|
->willReturn('App password');
|
|
|
|
|
|
|
|
$token->expects($this->once())
|
|
|
|
->method('getScopeAsArray')
|
|
|
|
->willReturn(['filesystem' => $filesystem]);
|
|
|
|
|
2016-11-11 13:35:11 +03:00
|
|
|
$token->expects($this->once())
|
|
|
|
->method('setScope')
|
2019-02-13 20:48:42 +03:00
|
|
|
->with($this->equalTo(['filesystem' => $newFilesystem]));
|
|
|
|
|
|
|
|
$this->tokenProvider->expects($this->once())
|
|
|
|
->method('updateToken')
|
|
|
|
->with($this->equalTo($token));
|
|
|
|
|
|
|
|
$this->assertSame([], $this->controller->update($tokenId, ['filesystem' => $newFilesystem], 'App password'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testUpdateNoChange(): void {
|
|
|
|
$tokenId = 42;
|
|
|
|
$token = $this->createMock(DefaultToken::class);
|
|
|
|
|
|
|
|
$this->mockGetTokenById($tokenId, $token);
|
|
|
|
|
|
|
|
$token->expects($this->once())
|
|
|
|
->method('getUID')
|
|
|
|
->willReturn('jane');
|
|
|
|
|
|
|
|
$token->expects($this->once())
|
|
|
|
->method('getName')
|
|
|
|
->willReturn('App password');
|
|
|
|
|
|
|
|
$token->expects($this->once())
|
|
|
|
->method('getScopeAsArray')
|
|
|
|
->willReturn(['filesystem' => true]);
|
|
|
|
|
|
|
|
$token->expects($this->never())
|
|
|
|
->method('setName');
|
|
|
|
|
|
|
|
$token->expects($this->never())
|
|
|
|
->method('setScope');
|
2016-11-11 13:35:11 +03:00
|
|
|
|
|
|
|
$this->tokenProvider->expects($this->once())
|
|
|
|
->method('updateToken')
|
|
|
|
->with($this->equalTo($token));
|
|
|
|
|
2019-01-20 23:30:51 +03:00
|
|
|
$this->assertSame([], $this->controller->update($tokenId, ['filesystem' => true], 'App password'));
|
2016-11-11 13:35:11 +03:00
|
|
|
}
|
|
|
|
|
2019-10-04 03:28:48 +03:00
|
|
|
public function testUpdateExpired() {
|
|
|
|
$tokenId = 42;
|
|
|
|
$token = $this->createMock(DefaultToken::class);
|
|
|
|
|
|
|
|
$token->expects($this->once())
|
|
|
|
->method('getUID')
|
|
|
|
->willReturn($this->uid);
|
|
|
|
|
|
|
|
$this->tokenProvider->expects($this->once())
|
|
|
|
->method('getTokenById')
|
|
|
|
->with($this->equalTo($tokenId))
|
|
|
|
->willThrowException(new ExpiredTokenException($token));
|
|
|
|
|
|
|
|
$this->tokenProvider->expects($this->once())
|
|
|
|
->method('updateToken')
|
|
|
|
->with($this->equalTo($token));
|
|
|
|
|
|
|
|
$this->assertSame([], $this->controller->update($tokenId, ['filesystem' => true], 'App password'));
|
|
|
|
}
|
|
|
|
|
2018-01-09 13:41:08 +03:00
|
|
|
public function testUpdateTokenWrongUser() {
|
2019-01-22 20:12:03 +03:00
|
|
|
$tokenId = 42;
|
2018-01-09 13:41:08 +03:00
|
|
|
$token = $this->createMock(DefaultToken::class);
|
|
|
|
|
2019-01-22 20:12:03 +03:00
|
|
|
$this->mockGetTokenById($tokenId, $token);
|
2018-01-09 13:41:08 +03:00
|
|
|
|
|
|
|
$token->expects($this->once())
|
|
|
|
->method('getUID')
|
|
|
|
->willReturn('foobar');
|
|
|
|
|
|
|
|
$token->expects($this->never())
|
|
|
|
->method('setScope');
|
|
|
|
$this->tokenProvider->expects($this->never())
|
|
|
|
->method('updateToken');
|
|
|
|
|
2019-01-20 23:30:51 +03:00
|
|
|
$response = $this->controller->update($tokenId, ['filesystem' => true], 'App password');
|
2018-01-09 13:41:08 +03:00
|
|
|
$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');
|
|
|
|
|
2019-01-20 23:30:51 +03:00
|
|
|
$response = $this->controller->update(42, ['filesystem' => true], 'App password');
|
2018-01-09 13:41:08 +03:00
|
|
|
$this->assertSame([], $response->getData());
|
|
|
|
$this->assertSame(\OCP\AppFramework\Http::STATUS_NOT_FOUND, $response->getStatus());
|
|
|
|
}
|
|
|
|
|
2019-01-22 20:12:03 +03:00
|
|
|
private function mockActivityManager(): void {
|
|
|
|
$this->activityManager->expects($this->once())
|
|
|
|
->method('generateEvent')
|
|
|
|
->willReturn($this->createMock(IEvent::class));
|
|
|
|
$this->activityManager->expects($this->once())
|
|
|
|
->method('publish');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $tokenId
|
|
|
|
* @param $token
|
|
|
|
*/
|
|
|
|
private function mockGetTokenById(int $tokenId, $token): void {
|
|
|
|
$this->tokenProvider->expects($this->once())
|
|
|
|
->method('getTokenById')
|
|
|
|
->with($this->equalTo($tokenId))
|
|
|
|
->willReturn($token);
|
|
|
|
}
|
2019-07-03 10:44:37 +03:00
|
|
|
|
|
|
|
public function testRemoteWipeNotSuccessful(): void {
|
2020-03-16 10:52:46 +03:00
|
|
|
$token = $this->createMock(IToken::class);
|
|
|
|
$token->expects($this->once())
|
|
|
|
->method('getUID')
|
|
|
|
->willReturn($this->uid);
|
|
|
|
$this->mockGetTokenById(123, $token);
|
|
|
|
|
2019-07-03 10:44:37 +03:00
|
|
|
$this->remoteWipe->expects($this->once())
|
|
|
|
->method('markTokenForWipe')
|
2020-03-16 10:52:46 +03:00
|
|
|
->with($token)
|
2019-07-03 10:44:37 +03:00
|
|
|
->willReturn(false);
|
|
|
|
|
|
|
|
$response = $this->controller->wipe(123);
|
|
|
|
|
|
|
|
$expected = new JSONResponse([], Http::STATUS_BAD_REQUEST);
|
|
|
|
$this->assertEquals($expected, $response);
|
|
|
|
}
|
|
|
|
|
2020-03-16 10:52:46 +03:00
|
|
|
public function testRemoteWipeWrongUser(): void {
|
|
|
|
$token = $this->createMock(IToken::class);
|
|
|
|
$token->expects($this->once())
|
|
|
|
->method('getUID')
|
|
|
|
->willReturn('definetly-not-' . $this->uid);
|
|
|
|
$this->mockGetTokenById(123, $token);
|
|
|
|
|
|
|
|
$this->remoteWipe->expects($this->never())
|
|
|
|
->method('markTokenForWipe');
|
|
|
|
|
|
|
|
$response = $this->controller->wipe(123);
|
|
|
|
|
|
|
|
$expected = new JSONResponse([], Http::STATUS_NOT_FOUND);
|
|
|
|
$this->assertEquals($expected, $response);
|
|
|
|
}
|
|
|
|
|
2019-07-03 10:44:37 +03:00
|
|
|
public function testRemoteWipeSuccessful(): void {
|
2020-03-16 10:52:46 +03:00
|
|
|
$token = $this->createMock(IWipeableToken::class);
|
|
|
|
$token->expects($this->once())
|
|
|
|
->method('getUID')
|
|
|
|
->willReturn($this->uid);
|
|
|
|
$this->mockGetTokenById(123, $token);
|
|
|
|
|
2019-07-03 10:44:37 +03:00
|
|
|
$this->remoteWipe->expects($this->once())
|
|
|
|
->method('markTokenForWipe')
|
2020-03-16 10:52:46 +03:00
|
|
|
->with($token)
|
2019-07-03 10:44:37 +03:00
|
|
|
->willReturn(true);
|
|
|
|
|
|
|
|
$response = $this->controller->wipe(123);
|
|
|
|
|
|
|
|
$expected = new JSONResponse([]);
|
|
|
|
$this->assertEquals($expected, $response);
|
|
|
|
}
|
|
|
|
|
2016-05-18 13:03:22 +03:00
|
|
|
}
|