2015-04-15 15:21:23 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc.
|
2019-12-03 21:57:53 +03:00
|
|
|
*
|
|
|
|
* @author Björn Schießle <bjoern@schiessle.org>
|
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
*
|
2015-04-15 15:21:23 +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/>
|
2015-04-15 15:21:23 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2019-09-17 17:33:27 +03:00
|
|
|
namespace OCA\Settings\Tests\Controller;
|
2015-04-15 15:21:23 +03:00
|
|
|
|
2019-09-17 17:33:27 +03:00
|
|
|
use OCA\Settings\Controller\CertificateController;
|
2015-08-04 19:33:19 +03:00
|
|
|
use OCP\App\IAppManager;
|
2015-04-15 15:21:23 +03:00
|
|
|
use OCP\AppFramework\Http;
|
|
|
|
use OCP\AppFramework\Http\DataResponse;
|
|
|
|
use OCP\ICertificateManager;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OCP\IL10N;
|
|
|
|
use OCP\IRequest;
|
2015-04-15 15:21:23 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class CertificateControllerTest
|
|
|
|
*
|
2016-05-19 12:17:01 +03:00
|
|
|
* @package Tests\Settings\Controller
|
2015-04-15 15:21:23 +03:00
|
|
|
*/
|
|
|
|
class CertificateControllerTest extends \Test\TestCase {
|
|
|
|
/** @var CertificateController */
|
|
|
|
private $certificateController;
|
|
|
|
/** @var IRequest */
|
|
|
|
private $request;
|
|
|
|
/** @var ICertificateManager */
|
|
|
|
private $certificateManager;
|
|
|
|
/** @var IL10N */
|
|
|
|
private $l10n;
|
2015-08-04 19:33:19 +03:00
|
|
|
/** @var IAppManager */
|
|
|
|
private $appManager;
|
2015-12-22 19:42:28 +03:00
|
|
|
/** @var ICertificateManager */
|
|
|
|
private $systemCertificateManager;
|
2015-04-15 15:21:23 +03:00
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
protected function setUp(): void {
|
2015-04-15 15:21:23 +03:00
|
|
|
parent::setUp();
|
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
$this->request = $this->getMockBuilder(IRequest::class)->getMock();
|
2019-09-17 17:33:27 +03:00
|
|
|
$this->certificateManager = $this->getMockBuilder(ICertificateManager::class)->getMock();
|
|
|
|
$this->systemCertificateManager = $this->getMockBuilder(ICertificateManager::class)->getMock();
|
2017-10-24 16:26:53 +03:00
|
|
|
$this->l10n = $this->getMockBuilder(IL10N::class)->getMock();
|
2019-09-17 17:33:27 +03:00
|
|
|
$this->appManager = $this->getMockBuilder(IAppManager::class)->getMock();
|
2015-04-15 15:21:23 +03:00
|
|
|
|
2019-09-17 17:33:27 +03:00
|
|
|
$this->certificateController = $this->getMockBuilder(CertificateController::class)
|
2015-08-04 19:33:19 +03:00
|
|
|
->setConstructorArgs(
|
|
|
|
[
|
|
|
|
'settings',
|
|
|
|
$this->request,
|
|
|
|
$this->certificateManager,
|
2015-12-22 19:42:28 +03:00
|
|
|
$this->systemCertificateManager,
|
2015-08-04 19:33:19 +03:00
|
|
|
$this->l10n,
|
|
|
|
$this->appManager
|
|
|
|
]
|
|
|
|
)->setMethods(['isCertificateImportAllowed'])->getMock();
|
|
|
|
|
|
|
|
$this->certificateController->expects($this->any())
|
|
|
|
->method('isCertificateImportAllowed')->willReturn(true);
|
2015-04-15 15:21:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddPersonalRootCertificateWithEmptyFile() {
|
|
|
|
$this->request
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getUploadedFile')
|
|
|
|
->with('rootcert_import')
|
|
|
|
->will($this->returnValue(null));
|
|
|
|
|
|
|
|
$expected = new DataResponse(['message' => 'No file uploaded'], Http::STATUS_UNPROCESSABLE_ENTITY);
|
|
|
|
$this->assertEquals($expected, $this->certificateController->addPersonalRootCertificate());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddPersonalRootCertificateValidCertificate() {
|
|
|
|
$uploadedFile = [
|
2019-09-17 17:33:27 +03:00
|
|
|
'tmp_name' => __DIR__ . '/../../../../tests/data/certificates/goodCertificate.crt',
|
2015-04-15 15:21:23 +03:00
|
|
|
'name' => 'goodCertificate.crt',
|
|
|
|
];
|
|
|
|
|
2016-08-16 21:51:43 +03:00
|
|
|
$certificate = $this->getMockBuilder('\OCP\ICertificate')->getMock();
|
2015-04-15 15:21:23 +03:00
|
|
|
$certificate
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getName')
|
|
|
|
->will($this->returnValue('Name'));
|
|
|
|
$certificate
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getCommonName')
|
|
|
|
->will($this->returnValue('CommonName'));
|
|
|
|
$certificate
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getOrganization')
|
|
|
|
->will($this->returnValue('Organization'));
|
|
|
|
$certificate
|
|
|
|
->expects($this->exactly(2))
|
|
|
|
->method('getIssueDate')
|
|
|
|
->will($this->returnValue(new \DateTime('@1429099555')));
|
|
|
|
$certificate
|
|
|
|
->expects($this->exactly(2))
|
|
|
|
->method('getExpireDate')
|
|
|
|
->will($this->returnValue(new \DateTime('@1529099555')));
|
|
|
|
$certificate
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getIssuerName')
|
|
|
|
->will($this->returnValue('Issuer'));
|
|
|
|
$certificate
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getIssuerOrganization')
|
|
|
|
->will($this->returnValue('IssuerOrganization'));
|
|
|
|
|
|
|
|
$this->request
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getUploadedFile')
|
|
|
|
->with('rootcert_import')
|
|
|
|
->will($this->returnValue($uploadedFile));
|
|
|
|
$this->certificateManager
|
|
|
|
->expects($this->once())
|
|
|
|
->method('addCertificate')
|
|
|
|
->with(file_get_contents($uploadedFile['tmp_name'], 'goodCertificate.crt'))
|
|
|
|
->will($this->returnValue($certificate));
|
|
|
|
|
|
|
|
$this->l10n
|
|
|
|
->expects($this->at(0))
|
|
|
|
->method('l')
|
|
|
|
->with('date', new \DateTime('@1429099555'))
|
|
|
|
->will($this->returnValue('Valid From as String'));
|
|
|
|
$this->l10n
|
|
|
|
->expects($this->at(1))
|
|
|
|
->method('l')
|
|
|
|
->with('date', new \DateTime('@1529099555'))
|
|
|
|
->will($this->returnValue('Valid Till as String'));
|
|
|
|
|
|
|
|
|
|
|
|
$expected = new DataResponse([
|
|
|
|
'name' => 'Name',
|
|
|
|
'commonName' => 'CommonName',
|
|
|
|
'organization' => 'Organization',
|
|
|
|
'validFrom' => 1429099555,
|
|
|
|
'validTill' => 1529099555,
|
|
|
|
'validFromString' => 'Valid From as String',
|
|
|
|
'validTillString' => 'Valid Till as String',
|
|
|
|
'issuer' => 'Issuer',
|
|
|
|
'issuerOrganization' => 'IssuerOrganization',
|
|
|
|
]);
|
|
|
|
$this->assertEquals($expected, $this->certificateController->addPersonalRootCertificate());
|
|
|
|
}
|
|
|
|
|
2015-04-16 11:48:25 +03:00
|
|
|
public function testAddPersonalRootCertificateInvalidCertificate() {
|
2015-04-15 15:21:23 +03:00
|
|
|
$uploadedFile = [
|
2019-09-17 17:33:27 +03:00
|
|
|
'tmp_name' => __DIR__ . '/../../../../tests/data/certificates/badCertificate.crt',
|
2015-04-15 15:21:23 +03:00
|
|
|
'name' => 'badCertificate.crt',
|
|
|
|
];
|
|
|
|
|
|
|
|
$this->request
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getUploadedFile')
|
|
|
|
->with('rootcert_import')
|
|
|
|
->will($this->returnValue($uploadedFile));
|
|
|
|
$this->certificateManager
|
|
|
|
->expects($this->once())
|
|
|
|
->method('addCertificate')
|
2015-04-16 11:48:25 +03:00
|
|
|
->with(file_get_contents($uploadedFile['tmp_name'], 'badCertificate.crt'))
|
2015-04-15 15:21:23 +03:00
|
|
|
->will($this->throwException(new \Exception()));
|
|
|
|
|
2017-07-25 16:12:49 +03:00
|
|
|
$expected = new DataResponse(['An error occurred.'], Http::STATUS_UNPROCESSABLE_ENTITY);
|
2015-04-15 15:21:23 +03:00
|
|
|
$this->assertEquals($expected, $this->certificateController->addPersonalRootCertificate());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRemoveCertificate() {
|
|
|
|
$this->certificateManager
|
|
|
|
->expects($this->once())
|
|
|
|
->method('removeCertificate')
|
|
|
|
->with('CertificateToRemove');
|
|
|
|
|
|
|
|
$this->assertEquals(new DataResponse(), $this->certificateController->removePersonalRootCertificate('CertificateToRemove'));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|