OCSAuthAPITest.php 6.1 KB

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