1
0

CredentialsManagerTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * @author Robin McCorkell <rmccorkell@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2015, 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\Security;
  22. use OC\SystemConfig;
  23. use OCP\ILogger;
  24. use \OCP\Security\ICrypto;
  25. use \OCP\IDBConnection;
  26. use \OC\Security\CredentialsManager;
  27. class CredentialsManagerTest extends \Test\TestCase {
  28. /** @var ICrypto */
  29. protected $crypto;
  30. /** @var IDBConnection */
  31. protected $dbConnection;
  32. /** @var CredentialsManager */
  33. protected $manager;
  34. protected function setUp() {
  35. parent::setUp();
  36. $this->crypto = $this->createMock(ICrypto::class);
  37. $this->dbConnection = $this->getMockBuilder('\OC\DB\Connection')
  38. ->disableOriginalConstructor()
  39. ->getMock();
  40. $this->manager = new CredentialsManager($this->crypto, $this->dbConnection);
  41. }
  42. private function getQueryResult($row) {
  43. $result = $this->getMockBuilder('\Doctrine\DBAL\Driver\Statement')
  44. ->disableOriginalConstructor()
  45. ->getMock();
  46. $result->expects($this->any())
  47. ->method('fetch')
  48. ->will($this->returnValue($row));
  49. return $result;
  50. }
  51. public function testStore() {
  52. $userId = 'abc';
  53. $identifier = 'foo';
  54. $credentials = 'bar';
  55. $this->crypto->expects($this->once())
  56. ->method('encrypt')
  57. ->with(json_encode($credentials))
  58. ->willReturn('baz');
  59. $this->dbConnection->expects($this->once())
  60. ->method('setValues')
  61. ->with(CredentialsManager::DB_TABLE,
  62. ['user' => $userId, 'identifier' => $identifier],
  63. ['credentials' => 'baz']
  64. );
  65. $this->manager->store($userId, $identifier, $credentials);
  66. }
  67. public function testRetrieve() {
  68. $userId = 'abc';
  69. $identifier = 'foo';
  70. $this->crypto->expects($this->once())
  71. ->method('decrypt')
  72. ->with('baz')
  73. ->willReturn(json_encode('bar'));
  74. $qb = $this->getMockBuilder('\OC\DB\QueryBuilder\QueryBuilder')
  75. ->setConstructorArgs([
  76. $this->dbConnection,
  77. $this->createMock(SystemConfig::class),
  78. $this->createMock(ILogger::class),
  79. ])
  80. ->setMethods(['execute'])
  81. ->getMock();
  82. $qb->expects($this->once())
  83. ->method('execute')
  84. ->willReturn($this->getQueryResult(['credentials' => 'baz']));
  85. $this->dbConnection->expects($this->once())
  86. ->method('getQueryBuilder')
  87. ->willReturn($qb);
  88. $this->manager->retrieve($userId, $identifier);
  89. }
  90. }