GlobalAuth.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\Files_External\Tests\Auth\Password;
  26. use OCA\Files_External\Lib\Auth\Password\GlobalAuth;
  27. use OCA\Files_external\Lib\StorageConfig;
  28. use OCP\IL10N;
  29. use OCP\Security\ICredentialsManager;
  30. use Test\TestCase;
  31. class GlobalAuthTest extends TestCase {
  32. /**
  33. * @var \OCP\IL10N|\PHPUnit\Framework\MockObject\MockObject
  34. */
  35. private $l10n;
  36. /**
  37. * @var \OCP\Security\ICredentialsManager|\PHPUnit\Framework\MockObject\MockObject
  38. */
  39. private $credentialsManager;
  40. /**
  41. * @var GlobalAuth
  42. */
  43. private $instance;
  44. protected function setUp(): void {
  45. parent::setUp();
  46. $this->l10n = $this->createMock(IL10N::class);
  47. $this->credentialsManager = $this->createMock(ICredentialsManager::class);
  48. $this->instance = new GlobalAuth($this->l10n, $this->credentialsManager);
  49. }
  50. private function getStorageConfig($type, $config = []) {
  51. /** @var \OCA\Files_External\Lib\StorageConfig|\PHPUnit\Framework\MockObject\MockObject $storageConfig */
  52. $storageConfig = $this->createMock(StorageConfig::class);
  53. $storageConfig->expects($this->any())
  54. ->method('getType')
  55. ->willReturn($type);
  56. $storageConfig->expects($this->any())
  57. ->method('getBackendOptions')
  58. ->willReturnCallback(function () use (&$config) {
  59. return $config;
  60. });
  61. $storageConfig->expects($this->any())
  62. ->method('getBackendOption')
  63. ->willReturnCallback(function ($key) use (&$config) {
  64. return $config[$key];
  65. });
  66. $storageConfig->expects($this->any())
  67. ->method('setBackendOption')
  68. ->willReturnCallback(function ($key, $value) use (&$config) {
  69. $config[$key] = $value;
  70. });
  71. return $storageConfig;
  72. }
  73. public function testNoCredentials() {
  74. $this->credentialsManager->expects($this->once())
  75. ->method('retrieve')
  76. ->willReturn(null);
  77. $storage = $this->getStorageConfig(StorageConfig::MOUNT_TYPE_ADMIN);
  78. $this->instance->manipulateStorageConfig($storage);
  79. $this->assertEquals([], $storage->getBackendOptions());
  80. }
  81. public function testSavedCredentials() {
  82. $this->credentialsManager->expects($this->once())
  83. ->method('retrieve')
  84. ->willReturn([
  85. 'user' => 'a',
  86. 'password' => 'b'
  87. ]);
  88. $storage = $this->getStorageConfig(StorageConfig::MOUNT_TYPE_ADMIN);
  89. $this->instance->manipulateStorageConfig($storage);
  90. $this->assertEquals([
  91. 'user' => 'a',
  92. 'password' => 'b'
  93. ], $storage->getBackendOptions());
  94. }
  95. public function testNoCredentialsPersonal() {
  96. $this->expectException(\OCA\Files_External\Lib\InsufficientDataForMeaningfulAnswerException::class);
  97. $this->credentialsManager->expects($this->never())
  98. ->method('retrieve');
  99. $storage = $this->getStorageConfig(StorageConfig::MOUNT_TYPE_PERSONAl);
  100. $this->instance->manipulateStorageConfig($storage);
  101. $this->assertEquals([], $storage->getBackendOptions());
  102. }
  103. }