IntegrationTestUserAvatar.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Roger Szabo <roger.szabo@web.de>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OCA\User_LDAP\Tests\Integration\Lib\User;
  29. use OCA\User_LDAP\FilesystemHelper;
  30. use OCA\User_LDAP\Mapping\UserMapping;
  31. use OCA\User_LDAP\Tests\Integration\AbstractIntegrationTest;
  32. use OCA\User_LDAP\User\DeletedUsersIndex;
  33. use OCA\User_LDAP\User\Manager;
  34. use OCA\User_LDAP\User\User;
  35. use OCA\User_LDAP\User_LDAP;
  36. use OCA\User_LDAP\UserPluginManager;
  37. use OCP\Image;
  38. use Psr\Log\LoggerInterface;
  39. require_once __DIR__ . '/../../Bootstrap.php';
  40. class IntegrationTestUserAvatar extends AbstractIntegrationTest {
  41. /** @var UserMapping */
  42. protected $mapping;
  43. /**
  44. * prepares the LDAP environment and sets up a test configuration for
  45. * the LDAP backend.
  46. */
  47. public function init() {
  48. require(__DIR__ . '/../../setup-scripts/createExplicitUsers.php');
  49. parent::init();
  50. $this->mapping = new UserMapping(\OC::$server->getDatabaseConnection());
  51. $this->mapping->clear();
  52. $this->access->setUserMapper($this->mapping);
  53. $userBackend = new User_LDAP($this->access, \OC::$server->getConfig(), \OC::$server->getNotificationManager(), \OC::$server->getUserSession(), \OC::$server->get(UserPluginManager::class), \OC::$server->get(LoggerInterface::class), \OC::$server->get(DeletedUsersIndex::class));
  54. \OC_User::useBackend($userBackend);
  55. }
  56. /**
  57. * A method that does the common steps of test cases 1 and 2. The evaluation
  58. * is not happening here.
  59. *
  60. * @param string $dn
  61. * @param string $username
  62. * @param string $image
  63. */
  64. private function execFetchTest($dn, $username, $image) {
  65. $this->setJpegPhotoAttribute($dn, $image);
  66. // assigns our self-picked oc username to the dn
  67. $this->mapping->map($dn, $username, 'fakeUUID-' . $username);
  68. // initialize home folder and make sure that the user will update
  69. // also remove an possibly existing avatar
  70. \OC_Util::tearDownFS();
  71. \OC_Util::setupFS($username);
  72. \OC::$server->getUserFolder($username);
  73. \OC::$server->getConfig()->deleteUserValue($username, 'user_ldap', User::USER_PREFKEY_LASTREFRESH);
  74. if (\OC::$server->getAvatarManager()->getAvatar($username)->exists()) {
  75. \OC::$server->getAvatarManager()->getAvatar($username)->remove();
  76. }
  77. // finally attempt to get the avatar set
  78. $user = $this->userManager->get($dn);
  79. $user->updateAvatar();
  80. }
  81. /**
  82. * tests whether an avatar can be retrieved from LDAP and stored correctly
  83. *
  84. * @return bool
  85. */
  86. protected function case1() {
  87. $image = file_get_contents(__DIR__ . '/../../data/avatar-valid.jpg');
  88. $dn = 'uid=alice,ou=Users,' . $this->base;
  89. $username = 'alice1337';
  90. $this->execFetchTest($dn, $username, $image);
  91. return \OC::$server->getAvatarManager()->getAvatar($username)->exists();
  92. }
  93. /**
  94. * tests whether an image received from LDAP which is of an invalid file
  95. * type is dealt with properly (i.e. not set and not dying).
  96. *
  97. * @return bool
  98. */
  99. protected function case2() {
  100. // gif by Pmspinner from https://commons.wikimedia.org/wiki/File:Avatar2469_3.gif
  101. $image = file_get_contents(__DIR__ . '/../../data/avatar-invalid.gif');
  102. $dn = 'uid=boris,ou=Users,' . $this->base;
  103. $username = 'boris7844';
  104. $this->execFetchTest($dn, $username, $image);
  105. return !\OC::$server->getAvatarManager()->getAvatar($username)->exists();
  106. }
  107. /**
  108. * This writes an image to the 'jpegPhoto' attribute on LDAP.
  109. *
  110. * @param string $dn
  111. * @param string $image An image read via file_get_contents
  112. * @throws \OC\ServerNotAvailableException
  113. */
  114. private function setJpegPhotoAttribute($dn, $image) {
  115. $changeSet = ['jpegphoto' => $image];
  116. ldap_mod_add($this->connection->getConnectionResource(), $dn, $changeSet);
  117. }
  118. protected function initUserManager() {
  119. $this->userManager = new Manager(
  120. \OC::$server->getConfig(),
  121. new FilesystemHelper(),
  122. \OC::$server->get(LoggerInterface::class),
  123. \OC::$server->getAvatarManager(),
  124. new Image(),
  125. \OC::$server->getDatabaseConnection(),
  126. \OC::$server->getUserManager(),
  127. \OC::$server->getNotificationManager()
  128. );
  129. }
  130. /**
  131. * sets up the LDAP configuration to be used for the test
  132. */
  133. protected function initConnection() {
  134. parent::initConnection();
  135. $this->connection->setConfiguration([
  136. 'ldapUserFilter' => 'objectclass=inetOrgPerson',
  137. 'ldapUserDisplayName' => 'displayName',
  138. 'ldapGroupDisplayName' => 'cn',
  139. 'ldapLoginFilter' => 'uid=%uid',
  140. ]);
  141. }
  142. }
  143. /** @var string $host */
  144. /** @var int $port */
  145. /** @var string $adn */
  146. /** @var string $apwd */
  147. /** @var string $bdn */
  148. $test = new IntegrationTestUserAvatar($host, $port, $adn, $apwd, $bdn);
  149. $test->init();
  150. $test->run();