1
0

AuthSettingsControllerTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. /**
  3. * @author Christoph Wurst <christoph@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2016, 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 Test\Settings\Controller;
  22. use OC\AppFramework\Http;
  23. use OC\Authentication\Exceptions\InvalidTokenException;
  24. use OC\Authentication\Token\DefaultToken;
  25. use OC\Authentication\Token\IProvider;
  26. use OC\Authentication\Token\IToken;
  27. use OC\Settings\Controller\AuthSettingsController;
  28. use OCP\AppFramework\Http\JSONResponse;
  29. use OCP\IRequest;
  30. use OCP\ISession;
  31. use OCP\IUser;
  32. use OCP\IUserManager;
  33. use OCP\Security\ISecureRandom;
  34. use OCP\Session\Exceptions\SessionNotAvailableException;
  35. use Test\TestCase;
  36. class AuthSettingsControllerTest extends TestCase {
  37. /** @var AuthSettingsController */
  38. private $controller;
  39. private $request;
  40. /** @var IProvider|\PHPUnit_Framework_MockObject_MockObject */
  41. private $tokenProvider;
  42. private $userManager;
  43. private $session;
  44. private $secureRandom;
  45. private $uid;
  46. protected function setUp() {
  47. parent::setUp();
  48. $this->request = $this->createMock(IRequest::class);
  49. $this->tokenProvider = $this->createMock(IProvider::class);
  50. $this->userManager = $this->createMock(IUserManager::class);
  51. $this->session = $this->createMock(ISession::class);
  52. $this->secureRandom = $this->createMock(ISecureRandom::class);
  53. $this->uid = 'jane';
  54. $this->user = $this->createMock(IUser::class);
  55. $this->controller = new AuthSettingsController('core', $this->request, $this->tokenProvider, $this->userManager, $this->session, $this->secureRandom, $this->uid);
  56. }
  57. public function testIndex() {
  58. $token1 = new DefaultToken();
  59. $token1->setId(100);
  60. $token2 = new DefaultToken();
  61. $token2->setId(200);
  62. $tokens = [
  63. $token1,
  64. $token2,
  65. ];
  66. $sessionToken = new DefaultToken();
  67. $sessionToken->setId(100);
  68. $this->userManager->expects($this->once())
  69. ->method('get')
  70. ->with($this->uid)
  71. ->will($this->returnValue($this->user));
  72. $this->tokenProvider->expects($this->once())
  73. ->method('getTokenByUser')
  74. ->with($this->user)
  75. ->will($this->returnValue($tokens));
  76. $this->session->expects($this->once())
  77. ->method('getId')
  78. ->will($this->returnValue('session123'));
  79. $this->tokenProvider->expects($this->once())
  80. ->method('getToken')
  81. ->with('session123')
  82. ->will($this->returnValue($sessionToken));
  83. $this->assertEquals([
  84. [
  85. 'id' => 100,
  86. 'name' => null,
  87. 'lastActivity' => 0,
  88. 'type' => 0,
  89. 'canDelete' => false,
  90. 'current' => true,
  91. 'scope' => ['filesystem' => true]
  92. ],
  93. [
  94. 'id' => 200,
  95. 'name' => null,
  96. 'lastActivity' => 0,
  97. 'type' => 0,
  98. 'canDelete' => true,
  99. 'scope' => ['filesystem' => true]
  100. ]
  101. ], $this->controller->index());
  102. }
  103. public function testCreate() {
  104. $name = 'Nexus 4';
  105. $sessionToken = $this->createMock(IToken::class);
  106. $deviceToken = $this->createMock(IToken::class);
  107. $password = '123456';
  108. $this->session->expects($this->once())
  109. ->method('getId')
  110. ->will($this->returnValue('sessionid'));
  111. $this->tokenProvider->expects($this->once())
  112. ->method('getToken')
  113. ->with('sessionid')
  114. ->will($this->returnValue($sessionToken));
  115. $this->tokenProvider->expects($this->once())
  116. ->method('getPassword')
  117. ->with($sessionToken, 'sessionid')
  118. ->will($this->returnValue($password));
  119. $sessionToken->expects($this->once())
  120. ->method('getLoginName')
  121. ->will($this->returnValue('User13'));
  122. $this->secureRandom->expects($this->exactly(4))
  123. ->method('generate')
  124. ->with(5, implode('', range('A', 'Z')))
  125. ->will($this->returnValue('XXXXX'));
  126. $newToken = 'XXXXX-XXXXX-XXXXX-XXXXX';
  127. $this->tokenProvider->expects($this->once())
  128. ->method('generateToken')
  129. ->with($newToken, $this->uid, 'User13', $password, $name, IToken::PERMANENT_TOKEN)
  130. ->will($this->returnValue($deviceToken));
  131. $deviceToken->expects($this->once())
  132. ->method('jsonSerialize')
  133. ->will($this->returnValue(['dummy' => 'dummy', 'canDelete' => true]));
  134. $expected = [
  135. 'token' => $newToken,
  136. 'deviceToken' => ['dummy' => 'dummy', 'canDelete' => true],
  137. 'loginName' => 'User13',
  138. ];
  139. $response = $this->controller->create($name);
  140. $this->assertInstanceOf(JSONResponse::class, $response);
  141. $this->assertEquals($expected, $response->getData());
  142. }
  143. public function testCreateSessionNotAvailable() {
  144. $name = 'personal phone';
  145. $this->session->expects($this->once())
  146. ->method('getId')
  147. ->will($this->throwException(new SessionNotAvailableException()));
  148. $expected = new JSONResponse();
  149. $expected->setStatus(Http::STATUS_SERVICE_UNAVAILABLE);
  150. $this->assertEquals($expected, $this->controller->create($name));
  151. }
  152. public function testCreateInvalidToken() {
  153. $name = 'Company IPhone';
  154. $this->session->expects($this->once())
  155. ->method('getId')
  156. ->will($this->returnValue('sessionid'));
  157. $this->tokenProvider->expects($this->once())
  158. ->method('getToken')
  159. ->with('sessionid')
  160. ->will($this->throwException(new InvalidTokenException()));
  161. $expected = new JSONResponse();
  162. $expected->setStatus(Http::STATUS_SERVICE_UNAVAILABLE);
  163. $this->assertEquals($expected, $this->controller->create($name));
  164. }
  165. public function testDestroy() {
  166. $id = 123;
  167. $user = $this->createMock(IUser::class);
  168. $this->userManager->expects($this->once())
  169. ->method('get')
  170. ->with($this->uid)
  171. ->will($this->returnValue($user));
  172. $this->tokenProvider->expects($this->once())
  173. ->method('invalidateTokenById')
  174. ->with($user, $id);
  175. $this->assertEquals([], $this->controller->destroy($id));
  176. }
  177. public function testUpdateToken() {
  178. $token = $this->createMock(DefaultToken::class);
  179. $this->tokenProvider->expects($this->once())
  180. ->method('getTokenById')
  181. ->with($this->equalTo(42))
  182. ->willReturn($token);
  183. $token->expects($this->once())
  184. ->method('setScope')
  185. ->with($this->equalTo([
  186. 'filesystem' => true
  187. ]));
  188. $this->tokenProvider->expects($this->once())
  189. ->method('updateToken')
  190. ->with($this->equalTo($token));
  191. $this->assertSame([], $this->controller->update(42, ['filesystem' => true]));
  192. }
  193. }