CredentialsManagerTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @author Robin McCorkell <rmccorkell@owncloud.com>
  5. *
  6. * @copyright Copyright (c) 2015, ownCloud, Inc.
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace Test\Security;
  23. use OC\Security\CredentialsManager;
  24. use OCP\DB\IResult;
  25. use OCP\DB\QueryBuilder\IExpressionBuilder;
  26. use OCP\DB\QueryBuilder\IQueryBuilder;
  27. use OCP\IDBConnection;
  28. use OCP\Security\ICrypto;
  29. /**
  30. * @group DB
  31. */
  32. class CredentialsManagerTest extends \Test\TestCase {
  33. /** @var ICrypto */
  34. protected $crypto;
  35. /** @var IDBConnection */
  36. protected $dbConnection;
  37. /** @var CredentialsManager */
  38. protected $manager;
  39. protected function setUp(): void {
  40. parent::setUp();
  41. $this->crypto = $this->createMock(ICrypto::class);
  42. $this->dbConnection = $this->getMockBuilder(IDBConnection::class)
  43. ->disableOriginalConstructor()
  44. ->getMock();
  45. $this->manager = new CredentialsManager($this->crypto, $this->dbConnection);
  46. }
  47. private function getQueryResult($row) {
  48. $result = $this->createMock(IResult::class);
  49. $result->expects($this->any())
  50. ->method('fetch')
  51. ->willReturn($row);
  52. return $result;
  53. }
  54. public function testStore() {
  55. $userId = 'abc';
  56. $identifier = 'foo';
  57. $credentials = 'bar';
  58. $this->crypto->expects($this->once())
  59. ->method('encrypt')
  60. ->with(json_encode($credentials))
  61. ->willReturn('baz');
  62. $this->dbConnection->expects($this->once())
  63. ->method('setValues')
  64. ->with(CredentialsManager::DB_TABLE,
  65. ['user' => $userId, 'identifier' => $identifier],
  66. ['credentials' => 'baz']
  67. );
  68. $this->manager->store($userId, $identifier, $credentials);
  69. }
  70. public function testRetrieve() {
  71. $userId = 'abc';
  72. $identifier = 'foo';
  73. $this->crypto->expects($this->once())
  74. ->method('decrypt')
  75. ->with('baz')
  76. ->willReturn(json_encode('bar'));
  77. $eb = $this->createMock(IExpressionBuilder::class);
  78. $qb = $this->createMock(IQueryBuilder::class);
  79. $qb->method('select')->willReturnSelf();
  80. $qb->method('from')->willReturnSelf();
  81. $qb->method('where')->willReturnSelf();
  82. $qb->method('expr')->willReturn($eb);
  83. $qb->expects($this->once())
  84. ->method('execute')
  85. ->willReturn($this->getQueryResult(['credentials' => 'baz']));
  86. $this->dbConnection->expects($this->once())
  87. ->method('getQueryBuilder')
  88. ->willReturn($qb);
  89. $this->manager->retrieve($userId, $identifier);
  90. }
  91. /**
  92. * @dataProvider credentialsProvider
  93. */
  94. public function testWithDB($userId, $identifier) {
  95. $credentialsManager = \OC::$server->getCredentialsManager();
  96. $secrets = 'Open Sesame';
  97. $credentialsManager->store($userId, $identifier, $secrets);
  98. $received = $credentialsManager->retrieve($userId, $identifier);
  99. $this->assertSame($secrets, $received);
  100. $removedRows = $credentialsManager->delete($userId, $identifier);
  101. $this->assertSame(1, $removedRows);
  102. }
  103. public function credentialsProvider() {
  104. return [
  105. [
  106. 'alice',
  107. 'privateCredentials'
  108. ],
  109. [
  110. '',
  111. 'systemCredentials',
  112. ],
  113. ];
  114. }
  115. }