2017-05-18 20:09:59 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2017 Lukas Reschke <lukas@statuscode.ch>
|
|
|
|
*
|
2020-03-31 11:49:10 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2020-08-24 15:54:25 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author rakekniven <mark.ziegler@rakekniven.de>
|
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
*
|
2017-05-18 20:09:59 +03:00
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2017-05-18 20:09:59 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\OAuth2\Tests\Controller;
|
|
|
|
|
|
|
|
use OC\Authentication\Token\DefaultTokenMapper;
|
|
|
|
use OCA\OAuth2\Controller\SettingsController;
|
|
|
|
use OCA\OAuth2\Db\AccessTokenMapper;
|
|
|
|
use OCA\OAuth2\Db\Client;
|
|
|
|
use OCA\OAuth2\Db\ClientMapper;
|
2018-06-26 16:27:20 +03:00
|
|
|
use OCP\AppFramework\Http;
|
2018-06-13 22:24:06 +03:00
|
|
|
use OCP\AppFramework\Http\JSONResponse;
|
2018-06-26 16:27:20 +03:00
|
|
|
use OCP\IL10N;
|
2017-05-18 20:09:59 +03:00
|
|
|
use OCP\IRequest;
|
|
|
|
use OCP\Security\ISecureRandom;
|
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
class SettingsControllerTest extends TestCase {
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
|
2017-05-18 20:09:59 +03:00
|
|
|
private $request;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var ClientMapper|\PHPUnit\Framework\MockObject\MockObject */
|
2017-05-18 20:09:59 +03:00
|
|
|
private $clientMapper;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var ISecureRandom|\PHPUnit\Framework\MockObject\MockObject */
|
2017-05-18 20:09:59 +03:00
|
|
|
private $secureRandom;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var AccessTokenMapper|\PHPUnit\Framework\MockObject\MockObject */
|
2017-05-18 20:09:59 +03:00
|
|
|
private $accessTokenMapper;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var DefaultTokenMapper|\PHPUnit\Framework\MockObject\MockObject */
|
2017-05-18 20:09:59 +03:00
|
|
|
private $defaultTokenMapper;
|
|
|
|
/** @var SettingsController */
|
|
|
|
private $settingsController;
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
protected function setUp(): void {
|
2017-05-18 20:09:59 +03:00
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->request = $this->createMock(IRequest::class);
|
|
|
|
$this->clientMapper = $this->createMock(ClientMapper::class);
|
|
|
|
$this->secureRandom = $this->createMock(ISecureRandom::class);
|
|
|
|
$this->accessTokenMapper = $this->createMock(AccessTokenMapper::class);
|
|
|
|
$this->defaultTokenMapper = $this->createMock(DefaultTokenMapper::class);
|
2018-06-26 16:27:20 +03:00
|
|
|
$l = $this->createMock(IL10N::class);
|
|
|
|
$l->method('t')
|
|
|
|
->willReturnArgument(0);
|
2017-05-18 20:09:59 +03:00
|
|
|
|
|
|
|
$this->settingsController = new SettingsController(
|
|
|
|
'oauth2',
|
|
|
|
$this->request,
|
|
|
|
$this->clientMapper,
|
|
|
|
$this->secureRandom,
|
|
|
|
$this->accessTokenMapper,
|
2018-06-26 16:27:20 +03:00
|
|
|
$this->defaultTokenMapper,
|
|
|
|
$l
|
2017-05-18 20:09:59 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddClient() {
|
|
|
|
$this->secureRandom
|
|
|
|
->expects($this->at(0))
|
|
|
|
->method('generate')
|
|
|
|
->with(64, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789')
|
|
|
|
->willReturn('MySecret');
|
|
|
|
$this->secureRandom
|
|
|
|
->expects($this->at(1))
|
|
|
|
->method('generate')
|
|
|
|
->with(64, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789')
|
|
|
|
->willReturn('MyClientIdentifier');
|
|
|
|
|
|
|
|
$client = new Client();
|
|
|
|
$client->setName('My Client Name');
|
|
|
|
$client->setRedirectUri('https://example.com/');
|
|
|
|
$client->setSecret('MySecret');
|
|
|
|
$client->setClientIdentifier('MyClientIdentifier');
|
|
|
|
|
|
|
|
$this->clientMapper
|
|
|
|
->expects($this->once())
|
|
|
|
->method('insert')
|
2018-06-13 22:24:06 +03:00
|
|
|
->with($this->callback(function (Client $c) {
|
|
|
|
return $c->getName() === 'My Client Name' &&
|
|
|
|
$c->getRedirectUri() === 'https://example.com/' &&
|
|
|
|
$c->getSecret() === 'MySecret' &&
|
|
|
|
$c->getClientIdentifier() === 'MyClientIdentifier';
|
2020-03-26 00:21:27 +03:00
|
|
|
}))->willReturnCallback(function (Client $c) {
|
2018-06-13 22:24:06 +03:00
|
|
|
$c->setId(42);
|
|
|
|
return $c;
|
2020-03-26 00:21:27 +03:00
|
|
|
});
|
2017-05-18 20:09:59 +03:00
|
|
|
|
2018-06-13 22:24:06 +03:00
|
|
|
$result = $this->settingsController->addClient('My Client Name', 'https://example.com/');
|
|
|
|
$this->assertInstanceOf(JSONResponse::class, $result);
|
|
|
|
|
|
|
|
$data = $result->getData();
|
2017-05-18 20:09:59 +03:00
|
|
|
|
2018-06-13 22:24:06 +03:00
|
|
|
$this->assertEquals([
|
|
|
|
'id' => 42,
|
|
|
|
'name' => 'My Client Name',
|
|
|
|
'redirectUri' => 'https://example.com/',
|
|
|
|
'clientId' => 'MyClientIdentifier',
|
|
|
|
'clientSecret' => 'MySecret',
|
|
|
|
], $data);
|
2017-05-18 20:09:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testDeleteClient() {
|
|
|
|
$client = new Client();
|
2018-06-13 22:24:06 +03:00
|
|
|
$client->setId(123);
|
2017-05-18 20:09:59 +03:00
|
|
|
$client->setName('My Client Name');
|
|
|
|
$client->setRedirectUri('https://example.com/');
|
|
|
|
$client->setSecret('MySecret');
|
|
|
|
$client->setClientIdentifier('MyClientIdentifier');
|
|
|
|
|
|
|
|
$this->clientMapper
|
|
|
|
->method('getByUid')
|
|
|
|
->with(123)
|
|
|
|
->willReturn($client);
|
|
|
|
$this->accessTokenMapper
|
|
|
|
->expects($this->once())
|
|
|
|
->method('deleteByClientId')
|
|
|
|
->with(123);
|
|
|
|
$this->defaultTokenMapper
|
|
|
|
->expects($this->once())
|
|
|
|
->method('deleteByName')
|
|
|
|
->with('My Client Name');
|
|
|
|
$this->clientMapper
|
|
|
|
->method('delete')
|
|
|
|
->with($client);
|
|
|
|
|
2018-06-13 22:24:06 +03:00
|
|
|
$result = $this->settingsController->deleteClient(123);
|
|
|
|
$this->assertInstanceOf(JSONResponse::class, $result);
|
|
|
|
$this->assertEquals([], $result->getData());
|
|
|
|
}
|
|
|
|
|
2018-06-26 16:27:20 +03:00
|
|
|
public function testInvalidRedirectUri() {
|
|
|
|
$result = $this->settingsController->addClient('test', 'invalidurl');
|
|
|
|
|
|
|
|
$this->assertEquals(Http::STATUS_BAD_REQUEST, $result->getStatus());
|
2018-06-30 09:51:58 +03:00
|
|
|
$this->assertSame(['message' => 'Your redirect URL needs to be a full URL for example: https://yourdomain.com/path'], $result->getData());
|
2018-06-26 16:27:20 +03:00
|
|
|
}
|
2017-05-18 20:09:59 +03:00
|
|
|
}
|