StoreTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. /**
  3. * @copyright 2016 Christoph Wurst <christoph@winzerhof-wurst.at>
  4. *
  5. * @author 2016 Christoph Wurst <christoph@winzerhof-wurst.at>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  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
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace Test\Authentication\LoginCredentials;
  24. use OC\Authentication\Exceptions\InvalidTokenException;
  25. use OC\Authentication\Exceptions\PasswordlessTokenException;
  26. use OC\Authentication\LoginCredentials\Credentials;
  27. use OC\Authentication\LoginCredentials\Store;
  28. use OC\Authentication\Token\IProvider;
  29. use OC\Authentication\Token\IToken;
  30. use OCP\Authentication\Exceptions\CredentialsUnavailableException;
  31. use OCP\ISession;
  32. use OCP\Session\Exceptions\SessionNotAvailableException;
  33. use Psr\Log\LoggerInterface;
  34. use Test\TestCase;
  35. use function json_encode;
  36. class StoreTest extends TestCase {
  37. /** @var ISession|\PHPUnit\Framework\MockObject\MockObject */
  38. private $session;
  39. /** @var IProvider|\PHPUnit\Framework\MockObject\MockObject */
  40. private $tokenProvider;
  41. /** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
  42. private $logger;
  43. /** @var Store */
  44. private $store;
  45. protected function setUp(): void {
  46. parent::setUp();
  47. $this->session = $this->createMock(ISession::class);
  48. $this->tokenProvider = $this->createMock(IProvider::class);
  49. $this->logger = $this->createMock(LoggerInterface::class);
  50. $this->store = new Store($this->session, $this->logger, $this->tokenProvider);
  51. }
  52. public function testAuthenticate() {
  53. $params = [
  54. 'run' => true,
  55. 'uid' => 'user123',
  56. 'password' => 123456,
  57. ];
  58. $this->session->expects($this->once())
  59. ->method('set')
  60. ->with($this->equalTo('login_credentials'), $this->equalTo(json_encode($params)));
  61. $this->store->authenticate($params);
  62. }
  63. public function testSetSession() {
  64. $session = $this->createMock(ISession::class);
  65. $this->store->setSession($session);
  66. $this->addToAssertionCount(1);
  67. }
  68. public function testGetLoginCredentialsNoTokenProvider() {
  69. $this->store = new Store($this->session, $this->logger, null);
  70. $this->expectException(CredentialsUnavailableException::class);
  71. $this->store->getLoginCredentials();
  72. }
  73. public function testGetLoginCredentials() {
  74. $uid = 'uid';
  75. $user = 'user123';
  76. $password = 'passme';
  77. $token = $this->createMock(IToken::class);
  78. $this->session->expects($this->once())
  79. ->method('getId')
  80. ->willReturn('sess2233');
  81. $this->tokenProvider->expects($this->once())
  82. ->method('getToken')
  83. ->with('sess2233')
  84. ->willReturn($token);
  85. $token->expects($this->once())
  86. ->method('getUID')
  87. ->willReturn($uid);
  88. $token->expects($this->once())
  89. ->method('getLoginName')
  90. ->willReturn($user);
  91. $this->tokenProvider->expects($this->once())
  92. ->method('getPassword')
  93. ->with($token, 'sess2233')
  94. ->willReturn($password);
  95. $expected = new Credentials($uid, $user, $password);
  96. $creds = $this->store->getLoginCredentials();
  97. $this->assertEquals($expected, $creds);
  98. }
  99. public function testGetLoginCredentialsSessionNotAvailable() {
  100. $this->session->expects($this->once())
  101. ->method('getId')
  102. ->will($this->throwException(new SessionNotAvailableException()));
  103. $this->expectException(CredentialsUnavailableException::class);
  104. $this->store->getLoginCredentials();
  105. }
  106. public function testGetLoginCredentialsInvalidToken() {
  107. $this->session->expects($this->once())
  108. ->method('getId')
  109. ->willReturn('sess2233');
  110. $this->tokenProvider->expects($this->once())
  111. ->method('getToken')
  112. ->with('sess2233')
  113. ->will($this->throwException(new InvalidTokenException()));
  114. $this->expectException(CredentialsUnavailableException::class);
  115. $this->store->getLoginCredentials();
  116. }
  117. public function testGetLoginCredentialsPartialCredentialsAndSessionName() {
  118. $uid = 'id987';
  119. $user = 'user987';
  120. $password = '7389374';
  121. $this->session->expects($this->once())
  122. ->method('getId')
  123. ->willReturn('sess2233');
  124. $this->tokenProvider->expects($this->once())
  125. ->method('getToken')
  126. ->with('sess2233')
  127. ->will($this->throwException(new InvalidTokenException()));
  128. $this->session->expects($this->once())
  129. ->method('exists')
  130. ->with($this->equalTo('login_credentials'))
  131. ->willReturn(true);
  132. $this->session->expects($this->exactly(2))
  133. ->method('get')
  134. ->willReturnMap([
  135. [
  136. 'login_credentials',
  137. json_encode([
  138. 'uid' => $uid,
  139. 'password' => $password,
  140. ])
  141. ],
  142. [
  143. 'loginname',
  144. $user,
  145. ],
  146. ]);
  147. $expected = new Credentials($uid, $user, $password);
  148. $actual = $this->store->getLoginCredentials();
  149. $this->assertEquals($expected, $actual);
  150. }
  151. public function testGetLoginCredentialsPartialCredentials() {
  152. $uid = 'id987';
  153. $password = '7389374';
  154. $this->session->expects($this->once())
  155. ->method('getId')
  156. ->willReturn('sess2233');
  157. $this->tokenProvider->expects($this->once())
  158. ->method('getToken')
  159. ->with('sess2233')
  160. ->will($this->throwException(new InvalidTokenException()));
  161. $this->session->expects($this->once())
  162. ->method('exists')
  163. ->with($this->equalTo('login_credentials'))
  164. ->willReturn(true);
  165. $this->session->expects($this->exactly(2))
  166. ->method('get')
  167. ->willReturnMap([
  168. [
  169. 'login_credentials',
  170. json_encode([
  171. 'uid' => $uid,
  172. 'password' => $password,
  173. ])
  174. ],
  175. [
  176. 'loginname',
  177. null,
  178. ],
  179. ]);
  180. $expected = new Credentials($uid, $uid, $password);
  181. $actual = $this->store->getLoginCredentials();
  182. $this->assertEquals($expected, $actual);
  183. }
  184. public function testGetLoginCredentialsInvalidTokenLoginCredentials() {
  185. $uid = 'id987';
  186. $user = 'user987';
  187. $password = '7389374';
  188. $this->session->expects($this->once())
  189. ->method('getId')
  190. ->willReturn('sess2233');
  191. $this->tokenProvider->expects($this->once())
  192. ->method('getToken')
  193. ->with('sess2233')
  194. ->will($this->throwException(new InvalidTokenException()));
  195. $this->session->expects($this->once())
  196. ->method('exists')
  197. ->with($this->equalTo('login_credentials'))
  198. ->willReturn(true);
  199. $this->session->expects($this->once())
  200. ->method('get')
  201. ->with($this->equalTo('login_credentials'))
  202. ->willReturn('{"run":true,"uid":"id987","loginName":"user987","password":"7389374"}');
  203. $expected = new Credentials($uid, $user, $password);
  204. $actual = $this->store->getLoginCredentials();
  205. $this->assertEquals($expected, $actual);
  206. }
  207. public function testGetLoginCredentialsPasswordlessToken() {
  208. $this->session->expects($this->once())
  209. ->method('getId')
  210. ->willReturn('sess2233');
  211. $this->tokenProvider->expects($this->once())
  212. ->method('getToken')
  213. ->with('sess2233')
  214. ->will($this->throwException(new PasswordlessTokenException()));
  215. $this->expectException(CredentialsUnavailableException::class);
  216. $this->store->getLoginCredentials();
  217. }
  218. }