CertificateControllerTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /**
  3. * @author Lukas Reschke <lukas@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2015, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace Tests\Settings\Controller;
  22. use OC\Settings\Controller\CertificateController;
  23. use OCP\App\IAppManager;
  24. use OCP\AppFramework\Http;
  25. use OCP\AppFramework\Http\DataResponse;
  26. use OCP\IRequest;
  27. use OCP\IL10N;
  28. use OCP\ICertificateManager;
  29. /**
  30. * Class CertificateControllerTest
  31. *
  32. * @package Tests\Settings\Controller
  33. */
  34. class CertificateControllerTest extends \Test\TestCase {
  35. /** @var CertificateController */
  36. private $certificateController;
  37. /** @var IRequest */
  38. private $request;
  39. /** @var ICertificateManager */
  40. private $certificateManager;
  41. /** @var IL10N */
  42. private $l10n;
  43. /** @var IAppManager */
  44. private $appManager;
  45. /** @var ICertificateManager */
  46. private $systemCertificateManager;
  47. public function setUp() {
  48. parent::setUp();
  49. $this->request = $this->getMockBuilder('\OCP\IRequest')->getMock();
  50. $this->certificateManager = $this->getMockBuilder('\OCP\ICertificateManager')->getMock();
  51. $this->systemCertificateManager = $this->getMockBuilder('\OCP\ICertificateManager')->getMock();
  52. $this->l10n = $this->getMockBuilder('\OCP\IL10N')->getMock();
  53. $this->appManager = $this->getMockBuilder('OCP\App\IAppManager')->getMock();
  54. $this->certificateController = $this->getMockBuilder('OC\Settings\Controller\CertificateController')
  55. ->setConstructorArgs(
  56. [
  57. 'settings',
  58. $this->request,
  59. $this->certificateManager,
  60. $this->systemCertificateManager,
  61. $this->l10n,
  62. $this->appManager
  63. ]
  64. )->setMethods(['isCertificateImportAllowed'])->getMock();
  65. $this->certificateController->expects($this->any())
  66. ->method('isCertificateImportAllowed')->willReturn(true);
  67. }
  68. public function testAddPersonalRootCertificateWithEmptyFile() {
  69. $this->request
  70. ->expects($this->once())
  71. ->method('getUploadedFile')
  72. ->with('rootcert_import')
  73. ->will($this->returnValue(null));
  74. $expected = new DataResponse(['message' => 'No file uploaded'], Http::STATUS_UNPROCESSABLE_ENTITY);
  75. $this->assertEquals($expected, $this->certificateController->addPersonalRootCertificate());
  76. }
  77. public function testAddPersonalRootCertificateValidCertificate() {
  78. $uploadedFile = [
  79. 'tmp_name' => __DIR__ . '/../../data/certificates/goodCertificate.crt',
  80. 'name' => 'goodCertificate.crt',
  81. ];
  82. $certificate = $this->getMockBuilder('\OCP\ICertificate')->getMock();
  83. $certificate
  84. ->expects($this->once())
  85. ->method('getName')
  86. ->will($this->returnValue('Name'));
  87. $certificate
  88. ->expects($this->once())
  89. ->method('getCommonName')
  90. ->will($this->returnValue('CommonName'));
  91. $certificate
  92. ->expects($this->once())
  93. ->method('getOrganization')
  94. ->will($this->returnValue('Organization'));
  95. $certificate
  96. ->expects($this->exactly(2))
  97. ->method('getIssueDate')
  98. ->will($this->returnValue(new \DateTime('@1429099555')));
  99. $certificate
  100. ->expects($this->exactly(2))
  101. ->method('getExpireDate')
  102. ->will($this->returnValue(new \DateTime('@1529099555')));
  103. $certificate
  104. ->expects($this->once())
  105. ->method('getIssuerName')
  106. ->will($this->returnValue('Issuer'));
  107. $certificate
  108. ->expects($this->once())
  109. ->method('getIssuerOrganization')
  110. ->will($this->returnValue('IssuerOrganization'));
  111. $this->request
  112. ->expects($this->once())
  113. ->method('getUploadedFile')
  114. ->with('rootcert_import')
  115. ->will($this->returnValue($uploadedFile));
  116. $this->certificateManager
  117. ->expects($this->once())
  118. ->method('addCertificate')
  119. ->with(file_get_contents($uploadedFile['tmp_name'], 'goodCertificate.crt'))
  120. ->will($this->returnValue($certificate));
  121. $this->l10n
  122. ->expects($this->at(0))
  123. ->method('l')
  124. ->with('date', new \DateTime('@1429099555'))
  125. ->will($this->returnValue('Valid From as String'));
  126. $this->l10n
  127. ->expects($this->at(1))
  128. ->method('l')
  129. ->with('date', new \DateTime('@1529099555'))
  130. ->will($this->returnValue('Valid Till as String'));
  131. $expected = new DataResponse([
  132. 'name' => 'Name',
  133. 'commonName' => 'CommonName',
  134. 'organization' => 'Organization',
  135. 'validFrom' => 1429099555,
  136. 'validTill' => 1529099555,
  137. 'validFromString' => 'Valid From as String',
  138. 'validTillString' => 'Valid Till as String',
  139. 'issuer' => 'Issuer',
  140. 'issuerOrganization' => 'IssuerOrganization',
  141. ]);
  142. $this->assertEquals($expected, $this->certificateController->addPersonalRootCertificate());
  143. }
  144. public function testAddPersonalRootCertificateInvalidCertificate() {
  145. $uploadedFile = [
  146. 'tmp_name' => __DIR__ . '/../../data/certificates/badCertificate.crt',
  147. 'name' => 'badCertificate.crt',
  148. ];
  149. $this->request
  150. ->expects($this->once())
  151. ->method('getUploadedFile')
  152. ->with('rootcert_import')
  153. ->will($this->returnValue($uploadedFile));
  154. $this->certificateManager
  155. ->expects($this->once())
  156. ->method('addCertificate')
  157. ->with(file_get_contents($uploadedFile['tmp_name'], 'badCertificate.crt'))
  158. ->will($this->throwException(new \Exception()));
  159. $expected = new DataResponse('An error occurred.', Http::STATUS_UNPROCESSABLE_ENTITY);
  160. $this->assertEquals($expected, $this->certificateController->addPersonalRootCertificate());
  161. }
  162. public function testRemoveCertificate() {
  163. $this->certificateManager
  164. ->expects($this->once())
  165. ->method('removeCertificate')
  166. ->with('CertificateToRemove');
  167. $this->assertEquals(new DataResponse(), $this->certificateController->removePersonalRootCertificate('CertificateToRemove'));
  168. }
  169. }