GlobalAuth.php 2.9 KB

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