ConfigurationTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2018-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\User_LDAP\Tests;
  8. use OCA\User_LDAP\Configuration;
  9. class ConfigurationTest extends \Test\TestCase {
  10. /** @var Configuration */
  11. protected $configuration;
  12. protected function setUp(): void {
  13. parent::setUp();
  14. $this->configuration = new Configuration('t01', false);
  15. }
  16. public function configurationDataProvider() {
  17. $inputWithDN = [
  18. 'cn=someUsers,dc=example,dc=org',
  19. ' ',
  20. ' cn=moreUsers,dc=example,dc=org '
  21. ];
  22. $expectWithDN = [
  23. 'cn=someUsers,dc=example,dc=org',
  24. 'cn=moreUsers,dc=example,dc=org'
  25. ];
  26. $inputNames = [
  27. ' uid ',
  28. 'cn ',
  29. ' ',
  30. '',
  31. ' whats my name',
  32. ' '
  33. ];
  34. $expectedNames = ['uid', 'cn', 'whats my name'];
  35. $inputString = ' alea iacta est ';
  36. $expectedString = 'alea iacta est';
  37. $inputHomeFolder = [
  38. ' homeDirectory ',
  39. ' attr:homeDirectory ',
  40. ' '
  41. ];
  42. $expectedHomeFolder = [
  43. 'attr:homeDirectory', 'attr:homeDirectory', ''
  44. ];
  45. $password = ' such a passw0rd ';
  46. return [
  47. 'set general base' => ['ldapBase', $inputWithDN, $expectWithDN],
  48. 'set user base' => ['ldapBaseUsers', $inputWithDN, $expectWithDN],
  49. 'set group base' => ['ldapBaseGroups', $inputWithDN, $expectWithDN],
  50. 'set search attributes users' => ['ldapAttributesForUserSearch', $inputNames, $expectedNames],
  51. 'set search attributes groups' => ['ldapAttributesForGroupSearch', $inputNames, $expectedNames],
  52. 'set user filter objectclasses' => ['ldapUserFilterObjectclass', $inputNames, $expectedNames],
  53. 'set user filter groups' => ['ldapUserFilterGroups', $inputNames, $expectedNames],
  54. 'set group filter objectclasses' => ['ldapGroupFilterObjectclass', $inputNames, $expectedNames],
  55. 'set group filter groups' => ['ldapGroupFilterGroups', $inputNames, $expectedNames],
  56. 'set login filter attributes' => ['ldapLoginFilterAttributes', $inputNames, $expectedNames],
  57. 'set agent password' => ['ldapAgentPassword', $password, $password],
  58. 'set home folder, variant 1' => ['homeFolderNamingRule', $inputHomeFolder[0], $expectedHomeFolder[0]],
  59. 'set home folder, variant 2' => ['homeFolderNamingRule', $inputHomeFolder[1], $expectedHomeFolder[1]],
  60. 'set home folder, empty' => ['homeFolderNamingRule', $inputHomeFolder[2], $expectedHomeFolder[2]],
  61. // default behaviour, one case is enough, special needs must be tested
  62. // individually
  63. 'set string value' => ['ldapHost', $inputString, $expectedString],
  64. 'set avatar rule, default' => ['ldapUserAvatarRule', 'default', 'default'],
  65. 'set avatar rule, none' => ['ldapUserAvatarRule', 'none', 'none'],
  66. 'set avatar rule, data attribute' => ['ldapUserAvatarRule', 'data:jpegPhoto', 'data:jpegPhoto'],
  67. 'set external storage home attribute' => ['ldapExtStorageHomeAttribute', 'homePath', 'homePath'],
  68. ];
  69. }
  70. /**
  71. * @dataProvider configurationDataProvider
  72. */
  73. public function testSetValue($key, $input, $expected) {
  74. $this->configuration->setConfiguration([$key => $input]);
  75. $this->assertSame($this->configuration->$key, $expected);
  76. }
  77. public function avatarRuleValueProvider() {
  78. return [
  79. ['none', []],
  80. ['data:selfie', ['selfie']],
  81. ['data:sELFie', ['selfie']],
  82. ['data:', ['jpegphoto', 'thumbnailphoto']],
  83. ['default', ['jpegphoto', 'thumbnailphoto']],
  84. ['invalid#', ['jpegphoto', 'thumbnailphoto']],
  85. ];
  86. }
  87. /**
  88. * @dataProvider avatarRuleValueProvider
  89. */
  90. public function testGetAvatarAttributes($setting, $expected) {
  91. $this->configuration->setConfiguration(['ldapUserAvatarRule' => $setting]);
  92. $this->assertSame($expected, $this->configuration->getAvatarAttributes());
  93. }
  94. /**
  95. * @dataProvider avatarRuleValueProvider
  96. */
  97. public function testResolveRule($setting, $expected) {
  98. $this->configuration->setConfiguration(['ldapUserAvatarRule' => $setting]);
  99. // so far the only thing that can get resolved :)
  100. $this->assertSame($expected, $this->configuration->resolveRule('avatar'));
  101. }
  102. }