1
0

DefaultTokenMapperTest.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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\Authentication\Token;
  22. use OC;
  23. use OC\Authentication\Token\DefaultToken;
  24. use OC\Authentication\Token\DefaultTokenMapper;
  25. use OC\Authentication\Token\IToken;
  26. use OCP\DB\QueryBuilder\IQueryBuilder;
  27. use OCP\IDBConnection;
  28. use OCP\IUser;
  29. use Test\TestCase;
  30. /**
  31. * Class DefaultTokenMapperTest
  32. *
  33. * @group DB
  34. * @package Test\Authentication
  35. */
  36. class DefaultTokenMapperTest extends TestCase {
  37. /** @var DefaultTokenMapper */
  38. private $mapper;
  39. /** @var IDBConnection */
  40. private $dbConnection;
  41. private $time;
  42. protected function setUp() {
  43. parent::setUp();
  44. $this->dbConnection = OC::$server->getDatabaseConnection();
  45. $this->time = time();
  46. $this->resetDatabase();
  47. $this->mapper = new DefaultTokenMapper($this->dbConnection);
  48. }
  49. private function resetDatabase() {
  50. $qb = $this->dbConnection->getQueryBuilder();
  51. $qb->delete('authtoken')->execute();
  52. $qb->insert('authtoken')->values([
  53. 'uid' => $qb->createNamedParameter('user1'),
  54. 'login_name' => $qb->createNamedParameter('User1'),
  55. 'password' => $qb->createNamedParameter('a75c7116460c082912d8f6860a850904|3nz5qbG1nNSLLi6V|c55365a0e54cfdfac4a175bcf11a7612aea74492277bba6e5d96a24497fa9272488787cb2f3ad34d8b9b8060934fce02f008d371df3ff3848f4aa61944851ff0'),
  56. 'name' => $qb->createNamedParameter('Firefox on Linux'),
  57. 'token' => $qb->createNamedParameter('9c5a2e661482b65597408a6bb6c4a3d1af36337381872ac56e445a06cdb7fea2b1039db707545c11027a4966919918b19d875a8b774840b18c6cbb7ae56fe206'),
  58. 'type' => $qb->createNamedParameter(IToken::TEMPORARY_TOKEN),
  59. 'last_activity' => $qb->createNamedParameter($this->time - 120, IQueryBuilder::PARAM_INT), // Two minutes ago
  60. 'last_check' => $this->time - 60 * 10, // 10mins ago
  61. ])->execute();
  62. $qb->insert('authtoken')->values([
  63. 'uid' => $qb->createNamedParameter('user2'),
  64. 'login_name' => $qb->createNamedParameter('User2'),
  65. 'password' => $qb->createNamedParameter('971a337057853344700bbeccf836519f|UwOQwyb34sJHtqPV|036d4890f8c21d17bbc7b88072d8ef049a5c832a38e97f3e3d5f9186e896c2593aee16883f617322fa242728d0236ff32d163caeb4bd45e14ca002c57a88665f'),
  66. 'name' => $qb->createNamedParameter('Firefox on Android'),
  67. 'token' => $qb->createNamedParameter('1504445f1524fc801035448a95681a9378ba2e83930c814546c56e5d6ebde221198792fd900c88ed5ead0555780dad1ebce3370d7e154941cd5de87eb419899b'),
  68. 'type' => $qb->createNamedParameter(IToken::TEMPORARY_TOKEN),
  69. 'last_activity' => $qb->createNamedParameter($this->time - 60 * 60 * 24 * 3, IQueryBuilder::PARAM_INT), // Three days ago
  70. 'last_check' => $this->time - 10, // 10secs ago
  71. ])->execute();
  72. $qb->insert('authtoken')->values([
  73. 'uid' => $qb->createNamedParameter('user1'),
  74. 'login_name' => $qb->createNamedParameter('User1'),
  75. 'password' => $qb->createNamedParameter('063de945d6f6b26862d9b6f40652f2d5|DZ/z520tfdXPtd0T|395f6b89be8d9d605e409e20b9d9abe477fde1be38a3223f9e508f979bf906e50d9eaa4dca983ca4fb22a241eb696c3f98654e7775f78c4caf13108f98642b53'),
  76. 'name' => $qb->createNamedParameter('Iceweasel on Linux'),
  77. 'token' => $qb->createNamedParameter('47af8697ba590fb82579b5f1b3b6e8066773a62100abbe0db09a289a62f5d980dc300fa3d98b01d7228468d1ab05c1aa14c8d14bd5b6eee9cdf1ac14864680c3'),
  78. 'type' => $qb->createNamedParameter(IToken::TEMPORARY_TOKEN),
  79. 'last_activity' => $qb->createNamedParameter($this->time - 120, IQueryBuilder::PARAM_INT), // Two minutes ago
  80. 'last_check' => $this->time - 60 * 10, // 10mins ago
  81. ])->execute();
  82. }
  83. private function getNumberOfTokens() {
  84. $qb = $this->dbConnection->getQueryBuilder();
  85. $result = $qb->select($qb->createFunction('count(*) as `count`'))
  86. ->from('authtoken')
  87. ->execute()
  88. ->fetch();
  89. return (int) $result['count'];
  90. }
  91. public function testInvalidate() {
  92. $token = '9c5a2e661482b65597408a6bb6c4a3d1af36337381872ac56e445a06cdb7fea2b1039db707545c11027a4966919918b19d875a8b774840b18c6cbb7ae56fe206';
  93. $this->mapper->invalidate($token);
  94. $this->assertSame(2, $this->getNumberOfTokens());
  95. }
  96. public function testInvalidateInvalid() {
  97. $token = 'youwontfindthisoneinthedatabase';
  98. $this->mapper->invalidate($token);
  99. $this->assertSame(3, $this->getNumberOfTokens());
  100. }
  101. public function testInvalidateOld() {
  102. $olderThan = $this->time - 60 * 60; // One hour
  103. $this->mapper->invalidateOld($olderThan);
  104. $this->assertSame(2, $this->getNumberOfTokens());
  105. }
  106. public function testGetToken() {
  107. $token = new DefaultToken();
  108. $token->setUid('user2');
  109. $token->setLoginName('User2');
  110. $token->setPassword('971a337057853344700bbeccf836519f|UwOQwyb34sJHtqPV|036d4890f8c21d17bbc7b88072d8ef049a5c832a38e97f3e3d5f9186e896c2593aee16883f617322fa242728d0236ff32d163caeb4bd45e14ca002c57a88665f');
  111. $token->setName('Firefox on Android');
  112. $token->setToken('1504445f1524fc801035448a95681a9378ba2e83930c814546c56e5d6ebde221198792fd900c88ed5ead0555780dad1ebce3370d7e154941cd5de87eb419899b');
  113. $token->setType(IToken::TEMPORARY_TOKEN);
  114. $token->setRemember(IToken::DO_NOT_REMEMBER);
  115. $token->setLastActivity($this->time - 60 * 60 * 24 * 3);
  116. $token->setLastCheck($this->time - 10);
  117. $dbToken = $this->mapper->getToken($token->getToken());
  118. $token->setId($dbToken->getId()); // We don't know the ID
  119. $token->resetUpdatedFields();
  120. $this->assertEquals($token, $dbToken);
  121. }
  122. /**
  123. * @expectedException \OCP\AppFramework\Db\DoesNotExistException
  124. */
  125. public function testGetInvalidToken() {
  126. $token = 'thisisaninvalidtokenthatisnotinthedatabase';
  127. $this->mapper->getToken($token);
  128. }
  129. public function testGetTokenById() {
  130. $token = new DefaultToken();
  131. $token->setUid('user2');
  132. $token->setLoginName('User2');
  133. $token->setPassword('971a337057853344700bbeccf836519f|UwOQwyb34sJHtqPV|036d4890f8c21d17bbc7b88072d8ef049a5c832a38e97f3e3d5f9186e896c2593aee16883f617322fa242728d0236ff32d163caeb4bd45e14ca002c57a88665f');
  134. $token->setName('Firefox on Android');
  135. $token->setToken('1504445f1524fc801035448a95681a9378ba2e83930c814546c56e5d6ebde221198792fd900c88ed5ead0555780dad1ebce3370d7e154941cd5de87eb419899b');
  136. $token->setType(IToken::TEMPORARY_TOKEN);
  137. $token->setRemember(IToken::DO_NOT_REMEMBER);
  138. $token->setLastActivity($this->time - 60 * 60 * 24 * 3);
  139. $token->setLastCheck($this->time - 10);
  140. $dbToken = $this->mapper->getToken($token->getToken());
  141. $token->setId($dbToken->getId()); // We don't know the ID
  142. $token->resetUpdatedFields();
  143. $dbToken = $this->mapper->getTokenById($token->getId());
  144. $this->assertEquals($token, $dbToken);
  145. }
  146. /**
  147. * @expectedException \OCP\AppFramework\Db\DoesNotExistException
  148. */
  149. public function testGetTokenByIdNotFound() {
  150. $this->mapper->getTokenById(-1);
  151. }
  152. /**
  153. * @expectedException \OCP\AppFramework\Db\DoesNotExistException
  154. */
  155. public function testGetInvalidTokenById() {
  156. $id = 42;
  157. $this->mapper->getToken($id);
  158. }
  159. public function testGetTokenByUser() {
  160. $user = $this->createMock(IUser::class);
  161. $user->expects($this->once())
  162. ->method('getUID')
  163. ->will($this->returnValue('user1'));
  164. $this->assertCount(2, $this->mapper->getTokenByUser($user));
  165. }
  166. public function testGetTokenByUserNotFound() {
  167. $user = $this->createMock(IUser::class);
  168. $user->expects($this->once())
  169. ->method('getUID')
  170. ->will($this->returnValue('user1000'));
  171. $this->assertCount(0, $this->mapper->getTokenByUser($user));
  172. }
  173. public function testDeleteById() {
  174. $user = $this->createMock(IUser::class);
  175. $qb = $this->dbConnection->getQueryBuilder();
  176. $qb->select('id')
  177. ->from('authtoken')
  178. ->where($qb->expr()->eq('token', $qb->createNamedParameter('9c5a2e661482b65597408a6bb6c4a3d1af36337381872ac56e445a06cdb7fea2b1039db707545c11027a4966919918b19d875a8b774840b18c6cbb7ae56fe206')));
  179. $result = $qb->execute();
  180. $id = $result->fetch()['id'];
  181. $user->expects($this->once())
  182. ->method('getUID')
  183. ->will($this->returnValue('user1'));
  184. $this->mapper->deleteById($user, $id);
  185. $this->assertEquals(2, $this->getNumberOfTokens());
  186. }
  187. public function testDeleteByIdWrongUser() {
  188. $user = $this->createMock(IUser::class);
  189. $id = 33;
  190. $user->expects($this->once())
  191. ->method('getUID')
  192. ->will($this->returnValue('user10000'));
  193. $this->mapper->deleteById($user, $id);
  194. $this->assertEquals(3, $this->getNumberOfTokens());
  195. }
  196. }