IntegrationTestUserAvatar.php 5.3 KB

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