AuthSettingsControllerTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. private $tokenProvider;
  41. private $userManager;
  42. private $session;
  43. private $secureRandom;
  44. private $uid;
  45. protected function setUp() {
  46. parent::setUp();
  47. $this->request = $this->createMock(IRequest::class);
  48. $this->tokenProvider = $this->createMock(IProvider::class);
  49. $this->userManager = $this->createMock(IUserManager::class);
  50. $this->session = $this->createMock(ISession::class);
  51. $this->secureRandom = $this->createMock(ISecureRandom::class);
  52. $this->uid = 'jane';
  53. $this->user = $this->createMock(IUser::class);
  54. $this->controller = new AuthSettingsController('core', $this->request, $this->tokenProvider, $this->userManager, $this->session, $this->secureRandom, $this->uid);
  55. }
  56. public function testIndex() {
  57. $token1 = new DefaultToken();
  58. $token1->setId(100);
  59. $token2 = new DefaultToken();
  60. $token2->setId(200);
  61. $tokens = [
  62. $token1,
  63. $token2,
  64. ];
  65. $sessionToken = new DefaultToken();
  66. $sessionToken->setId(100);
  67. $this->userManager->expects($this->once())
  68. ->method('get')
  69. ->with($this->uid)
  70. ->will($this->returnValue($this->user));
  71. $this->tokenProvider->expects($this->once())
  72. ->method('getTokenByUser')
  73. ->with($this->user)
  74. ->will($this->returnValue($tokens));
  75. $this->session->expects($this->once())
  76. ->method('getId')
  77. ->will($this->returnValue('session123'));
  78. $this->tokenProvider->expects($this->once())
  79. ->method('getToken')
  80. ->with('session123')
  81. ->will($this->returnValue($sessionToken));
  82. $this->assertEquals([
  83. [
  84. 'id' => 100,
  85. 'name' => null,
  86. 'lastActivity' => null,
  87. 'type' => null,
  88. 'canDelete' => false,
  89. 'current' => true,
  90. ],
  91. [
  92. 'id' => 200,
  93. 'name' => null,
  94. 'lastActivity' => null,
  95. 'type' => null,
  96. 'canDelete' => true,
  97. ]
  98. ], $this->controller->index());
  99. }
  100. public function testCreate() {
  101. $name = 'Nexus 4';
  102. $sessionToken = $this->createMock(IToken::class);
  103. $deviceToken = $this->createMock(IToken::class);
  104. $password = '123456';
  105. $this->session->expects($this->once())
  106. ->method('getId')
  107. ->will($this->returnValue('sessionid'));
  108. $this->tokenProvider->expects($this->once())
  109. ->method('getToken')
  110. ->with('sessionid')
  111. ->will($this->returnValue($sessionToken));
  112. $this->tokenProvider->expects($this->once())
  113. ->method('getPassword')
  114. ->with($sessionToken, 'sessionid')
  115. ->will($this->returnValue($password));
  116. $sessionToken->expects($this->once())
  117. ->method('getLoginName')
  118. ->will($this->returnValue('User13'));
  119. $this->secureRandom->expects($this->exactly(4))
  120. ->method('generate')
  121. ->with(5, implode('', range('A', 'Z')))
  122. ->will($this->returnValue('XXXXX'));
  123. $newToken = 'XXXXX-XXXXX-XXXXX-XXXXX';
  124. $this->tokenProvider->expects($this->once())
  125. ->method('generateToken')
  126. ->with($newToken, $this->uid, 'User13', $password, $name, IToken::PERMANENT_TOKEN)
  127. ->will($this->returnValue($deviceToken));
  128. $expected = [
  129. 'token' => $newToken,
  130. 'deviceToken' => $deviceToken,
  131. 'loginName' => 'User13',
  132. ];
  133. $this->assertEquals($expected, $this->controller->create($name));
  134. }
  135. public function testCreateSessionNotAvailable() {
  136. $name = 'personal phone';
  137. $this->session->expects($this->once())
  138. ->method('getId')
  139. ->will($this->throwException(new SessionNotAvailableException()));
  140. $expected = new JSONResponse();
  141. $expected->setStatus(Http::STATUS_SERVICE_UNAVAILABLE);
  142. $this->assertEquals($expected, $this->controller->create($name));
  143. }
  144. public function testCreateInvalidToken() {
  145. $name = 'Company IPhone';
  146. $this->session->expects($this->once())
  147. ->method('getId')
  148. ->will($this->returnValue('sessionid'));
  149. $this->tokenProvider->expects($this->once())
  150. ->method('getToken')
  151. ->with('sessionid')
  152. ->will($this->throwException(new InvalidTokenException()));
  153. $expected = new JSONResponse();
  154. $expected->setStatus(Http::STATUS_SERVICE_UNAVAILABLE);
  155. $this->assertEquals($expected, $this->controller->create($name));
  156. }
  157. public function testDestroy() {
  158. $id = 123;
  159. $user = $this->createMock(IUser::class);
  160. $this->userManager->expects($this->once())
  161. ->method('get')
  162. ->with($this->uid)
  163. ->will($this->returnValue($user));
  164. $this->tokenProvider->expects($this->once())
  165. ->method('invalidateTokenById')
  166. ->with($user, $id);
  167. $this->assertEquals([], $this->controller->destroy($id));
  168. }
  169. }