Welcome to mirror list, hosted at ThFree Co, Russian Federation.

integrationtestuserhome.php « lib « integration « tests « user_ldap « apps - github.com/nextcloud/server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f34fca81c2d170d386339bb8ce035e07c5d478f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
/**
 * Created by PhpStorm.
 * User: blizzz
 * Date: 06.08.15
 * Time: 08:19
 */

namespace OCA\user_ldap\tests\integration\lib;

use OCA\user_ldap\lib\user\Manager as LDAPUserManager;
use OCA\user_ldap\tests\integration\AbstractIntegrationTest;
use OCA\User_LDAP\Mapping\UserMapping;
use OCA\user_ldap\USER_LDAP;

require_once __DIR__  . '/../../../../../lib/base.php';

class IntegrationTestUserHome extends AbstractIntegrationTest {
	/** @var  UserMapping */
	protected $mapping;

	/** @var USER_LDAP */
	protected $backend;

	/**
	 * prepares the LDAP environment and sets up a test configuration for
	 * the LDAP backend.
	 */
	public function init() {
		require(__DIR__ . '/../setup-scripts/createExplicitUsers.php');
		parent::init();

		$this->mapping = new UserMapping(\OC::$server->getDatabaseConnection());
		$this->mapping->clear();
		$this->access->setUserMapper($this->mapping);
		$this->backend = new \OCA\user_ldap\USER_LDAP($this->access, \OC::$server->getConfig());
	}

	/**
	 * sets up the LDAP configuration to be used for the test
	 */
	protected function initConnection() {
		parent::initConnection();
		$this->connection->setConfiguration([
			'homeFolderNamingRule' => 'homeDirectory',
		]);
	}

	/**
	 * initializes an LDAP user manager instance
	 * @return LDAPUserManager
	 */
	protected function initUserManager() {
		$this->userManager = new LDAPUserManager(
			\OC::$server->getConfig(),
			new \OCA\user_ldap\lib\FilesystemHelper(),
			new \OCA\user_ldap\lib\LogWrapper(),
			\OC::$server->getAvatarManager(),
			new \OCP\Image(),
			\OC::$server->getDatabaseConnection()
		);
	}

	/**
	 * homeDirectory on LDAP is empty. Return values of getHome should be
	 * identical to user name, following ownCloud default.
	 *
	 * @return bool
	 */
	protected function case1() {
		\OC::$server->getConfig()->setAppValue('user_ldap', 'enforce_home_folder_naming_rule', false);
		$userManager = \oc::$server->getUserManager();
		$userManager->clearBackends();
		$userManager->registerBackend($this->backend);
		$users = $userManager->search('', 5, 0);

		foreach($users as $user) {
			$home = $user->getHome();
			$uid = $user->getUID();
			$posFound = strpos($home, '/' . $uid);
			$posExpected = strlen($home) - (strlen($uid) + 1);
			if($posFound === false || $posFound !== $posExpected) {
				print('"' . $user->getUID() . '" was not found in "' . $home . '" or does not end with it.' . PHP_EOL);
				return false;
			}
		}

		return true;
	}

	/**
	 * homeDirectory on LDAP is empty. Having the attributes set is enforced.
	 *
	 * @return bool
	 */
	protected function case2() {
		\OC::$server->getConfig()->setAppValue('user_ldap', 'enforce_home_folder_naming_rule', true);
		$userManager = \oc::$server->getUserManager();
		// clearing backends is critical, otherwise the userManager will have
		// the user objects cached and the value from case1 returned
		$userManager->clearBackends();
		$userManager->registerBackend($this->backend);
		$users = $userManager->search('', 5, 0);

		try {
			foreach ($users as $user) {
				$user->getHome();
				print('User home was retrieved without throwing an Exception!' . PHP_EOL);
				return false;
			}
		} catch (\Exception $e) {
			if(strpos($e->getMessage(), 'Home dir attribute') === 0) {
				return true;
			}
		}

		return false;
	}

	/**
	 * homeDirectory on LDAP is set to "attr:" which is effectively empty.
	 * Return values of getHome should be ownCloud default.
	 *
	 * @return bool
	 */
	protected function case3() {
		\OC::$server->getConfig()->setAppValue('user_ldap', 'enforce_home_folder_naming_rule', true);
		$this->connection->setConfiguration([
			'homeFolderNamingRule' => 'attr:',
		]);
		$userManager = \oc::$server->getUserManager();
		$userManager->clearBackends();
		$userManager->registerBackend($this->backend);
		$users = $userManager->search('', 5, 0);

		try {
			foreach ($users as $user) {
				$home = $user->getHome();
				$uid = $user->getUID();
				$posFound = strpos($home, '/' . $uid);
				$posExpected = strlen($home) - (strlen($uid) + 1);
				if ($posFound === false || $posFound !== $posExpected) {
					print('"' . $user->getUID() . '" was not found in "' . $home . '" or does not end with it.' . PHP_EOL);
					return false;
				}
			}
		} catch (\Exception $e) {
			print("Unexpected Exception: " . $e->getMessage() . PHP_EOL);
			return false;
		}

		return true;
	}
}

require_once(__DIR__ . '/../setup-scripts/config.php');
$test = new IntegrationTestUserHome($host, $port, $adn, $apwd, $bdn);
$test->init();
$test->run();