1
0

IntegrationTestUserAvatar.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017-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\Integration\Lib\User;
  8. use OCA\User_LDAP\FilesystemHelper;
  9. use OCA\User_LDAP\Mapping\UserMapping;
  10. use OCA\User_LDAP\Tests\Integration\AbstractIntegrationTest;
  11. use OCA\User_LDAP\User\DeletedUsersIndex;
  12. use OCA\User_LDAP\User\Manager;
  13. use OCA\User_LDAP\User\User;
  14. use OCA\User_LDAP\User_LDAP;
  15. use OCA\User_LDAP\UserPluginManager;
  16. use OCP\IAvatarManager;
  17. use OCP\Image;
  18. use Psr\Log\LoggerInterface;
  19. require_once __DIR__ . '/../../Bootstrap.php';
  20. class IntegrationTestUserAvatar extends AbstractIntegrationTest {
  21. /** @var UserMapping */
  22. protected $mapping;
  23. /**
  24. * prepares the LDAP environment and sets up a test configuration for
  25. * the LDAP backend.
  26. */
  27. public function init() {
  28. require(__DIR__ . '/../../setup-scripts/createExplicitUsers.php');
  29. parent::init();
  30. $this->mapping = new UserMapping(\OC::$server->getDatabaseConnection());
  31. $this->mapping->clear();
  32. $this->access->setUserMapper($this->mapping);
  33. $userBackend = new User_LDAP($this->access, \OC::$server->getNotificationManager(), \OC::$server->get(UserPluginManager::class), \OC::$server->get(LoggerInterface::class), \OC::$server->get(DeletedUsersIndex::class));
  34. \OC_User::useBackend($userBackend);
  35. }
  36. /**
  37. * A method that does the common steps of test cases 1 and 2. The evaluation
  38. * is not happening here.
  39. *
  40. * @param string $dn
  41. * @param string $username
  42. * @param string $image
  43. */
  44. private function execFetchTest($dn, $username, $image) {
  45. $this->setJpegPhotoAttribute($dn, $image);
  46. // assigns our self-picked oc username to the dn
  47. $this->mapping->map($dn, $username, 'fakeUUID-' . $username);
  48. // initialize home folder and make sure that the user will update
  49. // also remove an possibly existing avatar
  50. \OC_Util::tearDownFS();
  51. \OC_Util::setupFS($username);
  52. \OC::$server->getUserFolder($username);
  53. \OC::$server->getConfig()->deleteUserValue($username, 'user_ldap', User::USER_PREFKEY_LASTREFRESH);
  54. if (\OC::$server->get(IAvatarManager::class)->getAvatar($username)->exists()) {
  55. \OC::$server->get(IAvatarManager::class)->getAvatar($username)->remove();
  56. }
  57. // finally attempt to get the avatar set
  58. $user = $this->userManager->get($dn);
  59. $user->updateAvatar();
  60. }
  61. /**
  62. * tests whether an avatar can be retrieved from LDAP and stored correctly
  63. *
  64. * @return bool
  65. */
  66. protected function case1() {
  67. $image = file_get_contents(__DIR__ . '/../../data/avatar-valid.jpg');
  68. $dn = 'uid=alice,ou=Users,' . $this->base;
  69. $username = 'alice1337';
  70. $this->execFetchTest($dn, $username, $image);
  71. return \OC::$server->get(IAvatarManager::class)->getAvatar($username)->exists();
  72. }
  73. /**
  74. * tests whether an image received from LDAP which is of an invalid file
  75. * type is dealt with properly (i.e. not set and not dying).
  76. *
  77. * @return bool
  78. */
  79. protected function case2() {
  80. // gif by Pmspinner from https://commons.wikimedia.org/wiki/File:Avatar2469_3.gif
  81. $image = file_get_contents(__DIR__ . '/../../data/avatar-invalid.gif');
  82. $dn = 'uid=boris,ou=Users,' . $this->base;
  83. $username = 'boris7844';
  84. $this->execFetchTest($dn, $username, $image);
  85. return !\OC::$server->get(IAvatarManager::class)->getAvatar($username)->exists();
  86. }
  87. /**
  88. * This writes an image to the 'jpegPhoto' attribute on LDAP.
  89. *
  90. * @param string $dn
  91. * @param string $image An image read via file_get_contents
  92. * @throws \OC\ServerNotAvailableException
  93. */
  94. private function setJpegPhotoAttribute($dn, $image) {
  95. $changeSet = ['jpegphoto' => $image];
  96. ldap_mod_add($this->connection->getConnectionResource(), $dn, $changeSet);
  97. }
  98. protected function initUserManager() {
  99. $this->userManager = new Manager(
  100. \OC::$server->getConfig(),
  101. new FilesystemHelper(),
  102. \OC::$server->get(LoggerInterface::class),
  103. \OC::$server->get(IAvatarManager::class),
  104. new Image(),
  105. \OC::$server->getDatabaseConnection(),
  106. \OC::$server->getUserManager(),
  107. \OC::$server->getNotificationManager()
  108. );
  109. }
  110. /**
  111. * sets up the LDAP configuration to be used for the test
  112. */
  113. protected function initConnection() {
  114. parent::initConnection();
  115. $this->connection->setConfiguration([
  116. 'ldapUserFilter' => 'objectclass=inetOrgPerson',
  117. 'ldapUserDisplayName' => 'displayName',
  118. 'ldapGroupDisplayName' => 'cn',
  119. 'ldapLoginFilter' => 'uid=%uid',
  120. ]);
  121. }
  122. }
  123. /** @var string $host */
  124. /** @var int $port */
  125. /** @var string $adn */
  126. /** @var string $apwd */
  127. /** @var string $bdn */
  128. $test = new IntegrationTestUserAvatar($host, $port, $adn, $apwd, $bdn);
  129. $test->init();
  130. $test->run();