You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

OCSAuthAPIControllerTest.php 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bjoern Schiessle <bjoern@schiessle.org>
  6. * @author Björn Schießle <bjoern@schiessle.org>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\Federation\Tests\Controller;
  26. use OC\BackgroundJob\JobList;
  27. use OCA\Federation\Controller\OCSAuthAPIController;
  28. use OCA\Federation\DbHandler;
  29. use OCA\Federation\TrustedServers;
  30. use OCP\AppFramework\OCS\OCSForbiddenException;
  31. use OCP\AppFramework\Utility\ITimeFactory;
  32. use OCP\ILogger;
  33. use OCP\IRequest;
  34. use OCP\Security\ISecureRandom;
  35. use Test\TestCase;
  36. class OCSAuthAPIControllerTest extends TestCase {
  37. /** @var \PHPUnit_Framework_MockObject_MockObject|IRequest */
  38. private $request;
  39. /** @var \PHPUnit_Framework_MockObject_MockObject|ISecureRandom */
  40. private $secureRandom;
  41. /** @var \PHPUnit_Framework_MockObject_MockObject|JobList */
  42. private $jobList;
  43. /** @var \PHPUnit_Framework_MockObject_MockObject|TrustedServers */
  44. private $trustedServers;
  45. /** @var \PHPUnit_Framework_MockObject_MockObject|DbHandler */
  46. private $dbHandler;
  47. /** @var \PHPUnit_Framework_MockObject_MockObject|ILogger */
  48. private $logger;
  49. /** @var \PHPUnit_Framework_MockObject_MockObject|ITimeFactory */
  50. private $timeFactory;
  51. /** @var OCSAuthAPIController */
  52. private $ocsAuthApi;
  53. /** @var int simulated timestamp */
  54. private $currentTime = 1234567;
  55. public function setUp() {
  56. parent::setUp();
  57. $this->request = $this->createMock(IRequest::class);
  58. $this->secureRandom = $this->createMock(ISecureRandom::class);
  59. $this->trustedServers = $this->createMock(TrustedServers::class);
  60. $this->dbHandler = $this->createMock(DbHandler::class);
  61. $this->jobList = $this->createMock(JobList::class);
  62. $this->logger = $this->createMock(ILogger::class);
  63. $this->timeFactory = $this->createMock(ITimeFactory::class);
  64. $this->ocsAuthApi = new OCSAuthAPIController(
  65. 'federation',
  66. $this->request,
  67. $this->secureRandom,
  68. $this->jobList,
  69. $this->trustedServers,
  70. $this->dbHandler,
  71. $this->logger,
  72. $this->timeFactory
  73. );
  74. $this->timeFactory->method('getTime')
  75. ->willReturn($this->currentTime);
  76. }
  77. /**
  78. * @dataProvider dataTestRequestSharedSecret
  79. *
  80. * @param string $token
  81. * @param string $localToken
  82. * @param bool $isTrustedServer
  83. * @param bool $ok
  84. */
  85. public function testRequestSharedSecret($token, $localToken, $isTrustedServer, $ok) {
  86. $url = 'url';
  87. $this->trustedServers
  88. ->expects($this->once())
  89. ->method('isTrustedServer')->with($url)->willReturn($isTrustedServer);
  90. $this->dbHandler->expects($this->any())
  91. ->method('getToken')->with($url)->willReturn($localToken);
  92. if ($ok) {
  93. $this->jobList->expects($this->once())->method('add')
  94. ->with('OCA\Federation\BackgroundJob\GetSharedSecret', ['url' => $url, 'token' => $token, 'created' => $this->currentTime]);
  95. } else {
  96. $this->jobList->expects($this->never())->method('add');
  97. $this->jobList->expects($this->never())->method('remove');
  98. }
  99. try {
  100. $this->ocsAuthApi->requestSharedSecret($url, $token);
  101. $this->assertTrue($ok);
  102. } catch (OCSForbiddenException $e) {
  103. $this->assertFalse($ok);
  104. }
  105. }
  106. public function dataTestRequestSharedSecret() {
  107. return [
  108. ['token2', 'token1', true, true],
  109. ['token1', 'token2', false, false],
  110. ['token1', 'token2', true, false],
  111. ];
  112. }
  113. /**
  114. * @dataProvider dataTestGetSharedSecret
  115. *
  116. * @param bool $isTrustedServer
  117. * @param bool $isValidToken
  118. * @param bool $ok
  119. */
  120. public function testGetSharedSecret($isTrustedServer, $isValidToken, $ok) {
  121. $url = 'url';
  122. $token = 'token';
  123. /** @var OCSAuthAPIController | \PHPUnit_Framework_MockObject_MockObject $ocsAuthApi */
  124. $ocsAuthApi = $this->getMockBuilder('OCA\Federation\Controller\OCSAuthAPIController')
  125. ->setConstructorArgs(
  126. [
  127. 'federation',
  128. $this->request,
  129. $this->secureRandom,
  130. $this->jobList,
  131. $this->trustedServers,
  132. $this->dbHandler,
  133. $this->logger,
  134. $this->timeFactory
  135. ]
  136. )->setMethods(['isValidToken'])->getMock();
  137. $this->trustedServers
  138. ->expects($this->any())
  139. ->method('isTrustedServer')->with($url)->willReturn($isTrustedServer);
  140. $ocsAuthApi->expects($this->any())
  141. ->method('isValidToken')->with($url, $token)->willReturn($isValidToken);
  142. if($ok) {
  143. $this->secureRandom->expects($this->once())->method('generate')->with(32)
  144. ->willReturn('secret');
  145. $this->trustedServers->expects($this->once())
  146. ->method('addSharedSecret')->willReturn($url, 'secret');
  147. } else {
  148. $this->secureRandom->expects($this->never())->method('generate');
  149. $this->trustedServers->expects($this->never())->method('addSharedSecret');
  150. }
  151. try {
  152. $result = $ocsAuthApi->getSharedSecret($url, $token);
  153. $this->assertTrue($ok);
  154. $data = $result->getData();
  155. $this->assertSame('secret', $data['sharedSecret']);
  156. } catch (OCSForbiddenException $e) {
  157. $this->assertFalse($ok);
  158. }
  159. }
  160. public function dataTestGetSharedSecret() {
  161. return [
  162. [true, true, true],
  163. [false, true, false],
  164. [true, false, false],
  165. [false, false, false],
  166. ];
  167. }
  168. }