GlobalAuth.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_External\Tests\Auth\Password;
  8. use OCA\Files_External\Lib\Auth\Password\GlobalAuth;
  9. use OCA\Files_External\Lib\InsufficientDataForMeaningfulAnswerException;
  10. use OCA\Files_external\Lib\StorageConfig;
  11. use OCP\IL10N;
  12. use OCP\Security\ICredentialsManager;
  13. use Test\TestCase;
  14. class GlobalAuthTest extends TestCase {
  15. /**
  16. * @var IL10N|\PHPUnit\Framework\MockObject\MockObject
  17. */
  18. private $l10n;
  19. /**
  20. * @var ICredentialsManager|\PHPUnit\Framework\MockObject\MockObject
  21. */
  22. private $credentialsManager;
  23. /**
  24. * @var GlobalAuth
  25. */
  26. private $instance;
  27. protected function setUp(): void {
  28. parent::setUp();
  29. $this->l10n = $this->createMock(IL10N::class);
  30. $this->credentialsManager = $this->createMock(ICredentialsManager::class);
  31. $this->instance = new GlobalAuth($this->l10n, $this->credentialsManager);
  32. }
  33. private function getStorageConfig($type, $config = []) {
  34. /** @var \OCA\Files_External\Lib\StorageConfig|\PHPUnit\Framework\MockObject\MockObject $storageConfig */
  35. $storageConfig = $this->createMock(StorageConfig::class);
  36. $storageConfig->expects($this->any())
  37. ->method('getType')
  38. ->willReturn($type);
  39. $storageConfig->expects($this->any())
  40. ->method('getBackendOptions')
  41. ->willReturnCallback(function () use (&$config) {
  42. return $config;
  43. });
  44. $storageConfig->expects($this->any())
  45. ->method('getBackendOption')
  46. ->willReturnCallback(function ($key) use (&$config) {
  47. return $config[$key];
  48. });
  49. $storageConfig->expects($this->any())
  50. ->method('setBackendOption')
  51. ->willReturnCallback(function ($key, $value) use (&$config): void {
  52. $config[$key] = $value;
  53. });
  54. return $storageConfig;
  55. }
  56. public function testNoCredentials(): void {
  57. $this->credentialsManager->expects($this->once())
  58. ->method('retrieve')
  59. ->willReturn(null);
  60. $storage = $this->getStorageConfig(StorageConfig::MOUNT_TYPE_ADMIN);
  61. $this->instance->manipulateStorageConfig($storage);
  62. $this->assertEquals([], $storage->getBackendOptions());
  63. }
  64. public function testSavedCredentials(): void {
  65. $this->credentialsManager->expects($this->once())
  66. ->method('retrieve')
  67. ->willReturn([
  68. 'user' => 'a',
  69. 'password' => 'b'
  70. ]);
  71. $storage = $this->getStorageConfig(StorageConfig::MOUNT_TYPE_ADMIN);
  72. $this->instance->manipulateStorageConfig($storage);
  73. $this->assertEquals([
  74. 'user' => 'a',
  75. 'password' => 'b'
  76. ], $storage->getBackendOptions());
  77. }
  78. public function testNoCredentialsPersonal(): void {
  79. $this->expectException(InsufficientDataForMeaningfulAnswerException::class);
  80. $this->credentialsManager->expects($this->never())
  81. ->method('retrieve');
  82. $storage = $this->getStorageConfig(StorageConfig::MOUNT_TYPE_PERSONAL);
  83. $this->instance->manipulateStorageConfig($storage);
  84. $this->assertEquals([], $storage->getBackendOptions());
  85. }
  86. }