1
0

GlobalAuth.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Robin Appelman <robin@icewind.nl>
  6. *
  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 OCA\Files_External\Tests\Auth\Password;
  23. use OCA\Files_External\Lib\Auth\Password\GlobalAuth;
  24. use OCA\Files_external\Lib\StorageConfig;
  25. use OCP\IL10N;
  26. use OCP\Security\ICredentialsManager;
  27. use Test\TestCase;
  28. class GlobalAuthTest extends TestCase {
  29. /**
  30. * @var \OCP\IL10N|\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. private $l10n;
  33. /**
  34. * @var \OCP\Security\ICredentialsManager|\PHPUnit_Framework_MockObject_MockObject
  35. */
  36. private $credentialsManager;
  37. /**
  38. * @var GlobalAuth
  39. */
  40. private $instance;
  41. protected function setUp() {
  42. parent::setUp();
  43. $this->l10n = $this->createMock(IL10N::class);
  44. $this->credentialsManager = $this->createMock(ICredentialsManager::class);
  45. $this->instance = new GlobalAuth($this->l10n, $this->credentialsManager);
  46. }
  47. private function getStorageConfig($type, $config = []) {
  48. /** @var \OCA\Files_External\Lib\StorageConfig|\PHPUnit_Framework_MockObject_MockObject $storageConfig */
  49. $storageConfig = $this->createMock(StorageConfig::class);
  50. $storageConfig->expects($this->any())
  51. ->method('getType')
  52. ->will($this->returnValue($type));
  53. $storageConfig->expects($this->any())
  54. ->method('getBackendOptions')
  55. ->will($this->returnCallback(function () use (&$config) {
  56. return $config;
  57. }));
  58. $storageConfig->expects($this->any())
  59. ->method('getBackendOption')
  60. ->will($this->returnCallback(function ($key) use (&$config) {
  61. return $config[$key];
  62. }));
  63. $storageConfig->expects($this->any())
  64. ->method('setBackendOption')
  65. ->will($this->returnCallback(function ($key, $value) use (&$config) {
  66. $config[$key] = $value;
  67. }));
  68. return $storageConfig;
  69. }
  70. public function testNoCredentials() {
  71. $this->credentialsManager->expects($this->once())
  72. ->method('retrieve')
  73. ->will($this->returnValue(null));
  74. $storage = $this->getStorageConfig(StorageConfig::MOUNT_TYPE_ADMIN);
  75. $this->instance->manipulateStorageConfig($storage);
  76. $this->assertEquals([], $storage->getBackendOptions());
  77. }
  78. public function testSavedCredentials() {
  79. $this->credentialsManager->expects($this->once())
  80. ->method('retrieve')
  81. ->will($this->returnValue([
  82. 'user' => 'a',
  83. 'password' => 'b'
  84. ]));
  85. $storage = $this->getStorageConfig(StorageConfig::MOUNT_TYPE_ADMIN);
  86. $this->instance->manipulateStorageConfig($storage);
  87. $this->assertEquals([
  88. 'user' => 'a',
  89. 'password' => 'b'
  90. ], $storage->getBackendOptions());
  91. }
  92. /**
  93. * @expectedException \OCA\Files_External\Lib\InsufficientDataForMeaningfulAnswerException
  94. */
  95. public function testNoCredentialsPersonal() {
  96. $this->credentialsManager->expects($this->never())
  97. ->method('retrieve');
  98. $storage = $this->getStorageConfig(StorageConfig::MOUNT_TYPE_PERSONAl);
  99. $this->instance->manipulateStorageConfig($storage);
  100. $this->assertEquals([], $storage->getBackendOptions());
  101. }
  102. }