Projet

Général

Profil

Paste
Télécharger (27 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ldap / ldap_authentication / tests / ldap_authentication.test @ 91af538d

1 85ad3d82 Assos Assos
<?php
2
3
/**
4
 * @file
5 32700c57 Assos Assos
 * Ldap_authentication simpletests.
6 85ad3d82 Assos Assos
 */
7 bc175c27 Assos Assos
8 85ad3d82 Assos Assos
module_load_include('php', 'ldap_test', 'LdapTestCase.class');
9
module_load_include('module', 'ldap_authentication');
10
11 32700c57 Assos Assos
/**
12
 * LdapAuthenticationTestCase.
13
 */
14 85ad3d82 Assos Assos
class LdapAuthenticationTestCase extends LdapTestCase {
15 32700c57 Assos Assos
16
  public $module_name = 'ldap_authentication';
17
  protected $ldap_test_data;
18
19
  /**
20
   * Get info.
21
   */
22 85ad3d82 Assos Assos
  public static function getInfo() {
23 32700c57 Assos Assos
    return [
24 85ad3d82 Assos Assos
      'name' => 'LDAP Authentication Tests',
25
      'description' => 'Test ldap authentication.',
26 32700c57 Assos Assos
      'group' => 'LDAP Authentication',
27
    ];
28 85ad3d82 Assos Assos
  }
29
30 32700c57 Assos Assos
  /**
31
   * {@inheritdoc}
32
   */
33
  public function __construct($test_id = NULL) {
34 85ad3d82 Assos Assos
    parent::__construct($test_id);
35
  }
36
37 32700c57 Assos Assos
  /**
38
   *
39
   */
40
  public function setUp() {
41
    parent::setUp([
42 85ad3d82 Assos Assos
      'ldap_authentication',
43
      'ldap_authorization',
44
      'ldap_authorization_drupal_role',
45
      'ldap_test',
46 32700c57 Assos Assos
    // don't need any real servers, configured, just ldap_servers code base.
47
    ]);
48 85ad3d82 Assos Assos
    variable_set('ldap_simpletest', 2);
49
  }
50
51 32700c57 Assos Assos
  /**
52
   *
53
   */
54
  public function tearDown() {
55 85ad3d82 Assos Assos
    parent::tearDown();
56
    variable_del('ldap_help_watchdog_detail');
57
    variable_del('ldap_simpletest');
58
  }
59
60
  /**
61 32700c57 Assos Assos
   * Difficult to test install and uninstall since setUp does module enabling and installing.
62 85ad3d82 Assos Assos
   */
63 32700c57 Assos Assos
  public function testInstall() {
64 85ad3d82 Assos Assos
    $testid = $this->module_name . ': setup success';
65
    $setup_success = (
66
        module_exists('ldap_authentication') &&
67
        module_exists('ldap_servers')
68
      );
69
70
    $this->assertTrue($setup_success, ' ldap_authentication setup successful', $testid);
71
  }
72
73 32700c57 Assos Assos
  /**
74
   * LDAP Authentication Mixed Mode User Logon Test (ids = LDAP_authen.MM.ULT.*)
75
   */
76
  public function testMixedModeUserLogon() {
77 85ad3d82 Assos Assos
    $sid = 'activedirectory1';
78
    $testid = 'MixedModeUserLogon3';
79 32700c57 Assos Assos
    $sids = [$sid];
80 85ad3d82 Assos Assos
    $this->prepTestData(
81
      LDAP_TEST_LDAP_NAME,
82
      $sids,
83
      'provisionToDrupal',
84
      'MixedModeUserLogon3',
85
      'drupal_role_authentication_test'
86
    );
87
88
    $ldap_servers = ldap_servers_get_servers($sid, 'enabled');
89
    $this->assertTrue(count($ldap_servers) == 1, ' ldap_authentication test server setup successful', $testid);
90
91
    /**
92
     * LDAP_authen.MM.ULT.user1.goodpwd -- result: Successful logon as user 1
93
     */
94
95
    $user1 = user_load(1);
96
    $password = $this->randomString(20);
97 32700c57 Assos Assos
    require_once DRUPAL_ROOT . '/includes/password.inc';
98
    $account = [
99 85ad3d82 Assos Assos
      'name' => $user1->name,
100
      'pass' => user_hash_password(trim($password)),
101 32700c57 Assos Assos
    ];
102 85ad3d82 Assos Assos
    db_update('users')
103
      ->fields($account)
104
      ->condition('uid', 1)
105
      ->execute();
106
107 32700c57 Assos Assos
    $edit = [
108 85ad3d82 Assos Assos
      'name' => $user1->name,
109
      'pass' => $password,
110 32700c57 Assos Assos
    ];
111 85ad3d82 Assos Assos
112
    $this->drupalPost('user', $edit, t('Log in'));
113
    $this->assertText(t('Member for'), 'User 1 successfully authenticated', $testid);
114
    $this->drupalGet('user/logout');
115
116
    /** LDAP_authen.MM.ULT.user1.badpwd  -- result: Drupal logon error message. **/
117
118 32700c57 Assos Assos
    $edit = [
119 85ad3d82 Assos Assos
      'name' => $user1->name,
120
      'pass' => 'mydabpassword',
121 32700c57 Assos Assos
    ];
122 85ad3d82 Assos Assos
123
    $this->drupalPost('user', $edit, t('Log in'));
124
    $this->assertText(t('Sorry, unrecognized username or password'), 'User 1 failed with bad password', $testid);
125
    $this->drupalLogout();
126
127
    /** LDAP_authen.MM.ULT.drupal.goodpwd - result: Successful logon **/
128
129
    $drupal_user = $this->drupalCreateUser();
130
    $raw_pass = $drupal_user->pass_raw;
131 32700c57 Assos Assos
    $edit = [
132 85ad3d82 Assos Assos
      'name' => $drupal_user->name,
133
      'pass' => $raw_pass,
134 32700c57 Assos Assos
    ];
135 85ad3d82 Assos Assos
    $this->drupalPost('user', $edit, t('Log in'));
136
    $this->assertText(t('Member for'), 'Drupal user (not ldap associated) successfully authenticated', $testid);
137
    $this->drupalGet('user/logout');
138
139
    /** LDAP_authen.MM.ULT.drupal.badpwd - result: Drupal logon error message. **/
140 32700c57 Assos Assos
    $edit = [
141 85ad3d82 Assos Assos
      'name' => $drupal_user->name,
142
      'pass' => 'mydabpassword',
143 32700c57 Assos Assos
    ];
144 85ad3d82 Assos Assos
    $this->drupalPost('user', $edit, t('Log in'));
145
    $this->assertText(t('Sorry, unrecognized username or password'), 'Drupal user  (not ldap associated) with bad password failed to authenticate.', $testid);
146
    $this->drupalGet('user/logout');
147
148
    /** LDAP_authen.MM.ULT.ldap.newaccount.badpwd - result: Drupal logon error message. **/
149 32700c57 Assos Assos
    $edit = [
150 85ad3d82 Assos Assos
      'name' => 'hpotter',
151
      'pass' => 'mydabpassword',
152 32700c57 Assos Assos
    ];
153 85ad3d82 Assos Assos
    $this->drupalPost('user', $edit, t('Log in'));
154
    $this->assertText(t('Sorry, unrecognized username or password'), 'New Ldap user with bad password failed to authenticate.', $testid);
155
    $this->drupalGet('user/logout');
156
157
    /** LDAP_authen.MM.ULT.ldap.newaccount.goodpwd - result: Successful logon, with user record created and authmapped to ldap **/
158 32700c57 Assos Assos
    $edit = [
159 85ad3d82 Assos Assos
      'name' => 'hpotter',
160
      'pass' => 'goodpwd',
161 32700c57 Assos Assos
    ];
162 85ad3d82 Assos Assos
    $this->drupalPost('user', $edit, t('Log in'));
163
    $this->assertText(t('Member for'), 'New Ldap user with good password authenticated.');
164
    $this->assertTrue($this->testFunctions->ldapUserIsAuthmapped('hpotter'), 'Ldap user properly authmapped.', $testid);
165
    $this->drupalGet('user/logout');
166
167
    /** LDAP_authen.MM.ULT.existingacct.badpwd - result: Drupal logon error message. **/
168 32700c57 Assos Assos
    $edit = [
169 85ad3d82 Assos Assos
      'name' => 'hpotter',
170
      'pass' => 'mydabpassword',
171 32700c57 Assos Assos
    ];
172 85ad3d82 Assos Assos
    $this->drupalPost('user', $edit, t('Log in'));
173
    $this->assertText(t('Sorry, unrecognized username or password'), 'Existing Ldap user with bad password failed to authenticate.', $testid);
174
    $this->drupalGet('user/logout');
175
176
    /** LDAP_authen.MM.ULT.existingacct.goodpwd - result: Successful logon. **/
177 32700c57 Assos Assos
    $edit = [
178 85ad3d82 Assos Assos
      'name' => 'hpotter',
179
      'pass' => 'goodpwd',
180 32700c57 Assos Assos
    ];
181 85ad3d82 Assos Assos
    $this->drupalPost('user', $edit, t('Log in'));
182
    $this->assertText(t('Member for'), 'Existing Ldap user with good password authenticated.');
183
    $this->assertTrue($this->testFunctions->ldapUserIsAuthmapped('hpotter'), 'Existing Ldap user still properly authmapped.', $testid);
184
    $this->drupalGet('user/logout');
185
186
  }
187
188 32700c57 Assos Assos
  /**
189
   * LDAP Authentication Exclusive Mode User Logon Test (ids = LDAP_authen.EM.ULT.*)
190
   */
191
  public function testExclusiveModeUserLogon() {
192 85ad3d82 Assos Assos
193
    $sid = 'activedirectory1';
194
    $testid = 'ExclusiveModeUserLogon3';
195 32700c57 Assos Assos
    $sids = [$sid];
196 85ad3d82 Assos Assos
    $this->prepTestData(
197
      LDAP_TEST_LDAP_NAME,
198
      $sids,
199
      'ad_authentication',
200
      'ExclusiveModeUserLogon3',
201
      'drupal_role_authentication_test'
202
      );
203
204
    $ldap_servers = ldap_servers_get_servers($sid, 'enabled');
205
    $this->assertTrue(count($ldap_servers) == 1, ' ldap_authentication test server setup successful', $testid);
206
207
    /**
208
     * LDAP_authen.EM.ULT.user1.goodpwd -- result: Successful logon as user 1
209
     */
210
211
    $user1 = user_load(1);
212
    $password = $this->randomString(20);
213 32700c57 Assos Assos
    require_once DRUPAL_ROOT . '/includes/password.inc';
214
    $account = [
215 85ad3d82 Assos Assos
      'name' => $user1->name,
216
      'pass' => user_hash_password(trim($password)),
217 32700c57 Assos Assos
    ];
218 85ad3d82 Assos Assos
    db_update('users')
219
      ->fields($account)
220
      ->condition('uid', 1)
221
      ->execute();
222
223 32700c57 Assos Assos
    $edit = [
224 85ad3d82 Assos Assos
      'name' => $user1->name,
225
      'pass' => $password,
226 32700c57 Assos Assos
    ];
227 85ad3d82 Assos Assos
228
    $this->drupalPost('user', $edit, t('Log in'));
229
    $this->assertText(t('Member for'), 'User 1 successfully authenticated', $testid);
230
    $this->drupalGet('user/logout');
231
232
    /** LDAP_authen.EM.ULT.user1.badpwd  -- result: Drupal logon error message. **/
233
234 32700c57 Assos Assos
    $edit = [
235 85ad3d82 Assos Assos
      'name' => $user1->name,
236
      'pass' => 'mydabpassword',
237 32700c57 Assos Assos
    ];
238 85ad3d82 Assos Assos
239
    $this->drupalPost('user', $edit, t('Log in'));
240 32700c57 Assos Assos
    $this->assertText(t('Sorry, unrecognized username or password'), 'User 1 failed with bad password', $testid);
241 85ad3d82 Assos Assos
    $this->drupalLogout();
242
243
    /** LDAP_authen.EM.ULT.drupal.goodpwd - result: failed logon **/
244
245
    $drupal_user = $this->drupalCreateUser();
246
    $raw_pass = $drupal_user->pass_raw;
247 32700c57 Assos Assos
    $edit = [
248 85ad3d82 Assos Assos
      'name' => $drupal_user->name,
249
      'pass' => $raw_pass,
250 32700c57 Assos Assos
    ];
251 85ad3d82 Assos Assos
    $this->drupalPost('user', $edit, t('Log in'));
252 32700c57 Assos Assos
    $this->assertText(t('Sorry, unrecognized username or password'), 'Drupal user successfully authenticated', $testid);
253 85ad3d82 Assos Assos
    $this->drupalGet('user/logout');
254
255
    /** LDAP_authen.EM.ULT.drupal.badpwd - result: Drupal logon error message. **/
256 32700c57 Assos Assos
    $edit = [
257 85ad3d82 Assos Assos
      'name' => $drupal_user->name,
258
      'pass' => 'mydabpassword',
259 32700c57 Assos Assos
    ];
260 85ad3d82 Assos Assos
    $this->drupalPost('user', $edit, t('Log in'));
261 32700c57 Assos Assos
    $this->assertText(t('Sorry, unrecognized username or password'), 'Drupal user with bad password failed to authenticate.', $testid);
262 85ad3d82 Assos Assos
    $this->drupalGet('user/logout');
263
264
    /** LDAP_authen.EM.ULT.ldap.newaccount.badpwd - result: Drupal logon error message. **/
265 32700c57 Assos Assos
    $edit = [
266 85ad3d82 Assos Assos
      'name' => 'hpotter',
267
      'pass' => 'mydabpassword',
268 32700c57 Assos Assos
    ];
269 85ad3d82 Assos Assos
    $this->drupalPost('user', $edit, t('Log in'));
270 32700c57 Assos Assos
    $this->assertText(t('Sorry, unrecognized username or password'), 'New Ldap user with bad password failed to authenticate.', $testid);
271 85ad3d82 Assos Assos
    $this->drupalGet('user/logout');
272
273
    /** LDAP_authen.EM.ULT.ldap.newaccount.goodpwd - result: Successful logon, with user record created and authmapped to ldap **/
274 32700c57 Assos Assos
    $edit = [
275 85ad3d82 Assos Assos
      'name' => 'hpotter',
276
      'pass' => 'goodpwd',
277 32700c57 Assos Assos
    ];
278 85ad3d82 Assos Assos
    $this->drupalPost('user', $edit, t('Log in'));
279
    $this->assertText(t('Member for'), 'New Ldap user with good password authenticated.');
280 32700c57 Assos Assos
    $this->assertTrue($this->testFunctions->ldapUserIsAuthmapped('hpotter'), 'Ldap user properly authmapped.', $testid);
281 85ad3d82 Assos Assos
    $this->drupalGet('user/logout');
282
283
    /** LDAP_authen.EM.ULT.existingacct.badpwd - result: Drupal logon error message. **/
284 32700c57 Assos Assos
    $edit = [
285 85ad3d82 Assos Assos
      'name' => 'hpotter',
286
      'pass' => 'mydabpassword',
287 32700c57 Assos Assos
    ];
288 85ad3d82 Assos Assos
    $this->drupalPost('user', $edit, t('Log in'));
289 32700c57 Assos Assos
    $this->assertText(t('Sorry, unrecognized username or password'), 'Existing Ldap user with bad password failed to authenticate.', $testid);
290 85ad3d82 Assos Assos
    $this->drupalGet('user/logout');
291
292
    /** LDAP_authen.MM.ULT.existingacct.goodpwd - result: Successful logon. **/
293 32700c57 Assos Assos
    $edit = [
294 85ad3d82 Assos Assos
      'name' => 'hpotter',
295
      'pass' => 'goodpwd',
296 32700c57 Assos Assos
    ];
297 85ad3d82 Assos Assos
    $this->drupalPost('user', $edit, t('Log in'));
298
    $this->assertText(t('Member for'), 'Existing Ldap user with good password authenticated.');
299 32700c57 Assos Assos
    $this->assertTrue($this->testFunctions->ldapUserIsAuthmapped('hpotter'), 'Existing Ldap user still properly authmapped.', $testid);
300 85ad3d82 Assos Assos
    $this->drupalGet('user/logout');
301
302
  }
303
304
  /**
305 32700c57 Assos Assos
   * Set mock server variables for sso tests.
306 85ad3d82 Assos Assos
   *
307 32700c57 Assos Assos
   * @param string $server_var_key
308
   * @param string $sso_name
309
   * @param string $ldapImplementation
310
   * @param bool $seamlessLogin
311 85ad3d82 Assos Assos
   *
312 32700c57 Assos Assos
   * @return object
313 85ad3d82 Assos Assos
   */
314
  private function setSsoServerEnvironment(
315
    $server_var_key = 'REMOTE_USER',
316
    $sso_name = 'hpotter',
317
    $ldapImplementation = 'mod_auth_sspi',
318
    $seamlessLogin = TRUE
319
  ) {
320
321 32700c57 Assos Assos
    // Clear past environment.
322 85ad3d82 Assos Assos
    ldap_servers_delete_globals('_SERVER', 'REMOTE_USER', TRUE);
323
    ldap_servers_delete_globals('_SERVER', 'REDIRECT_REMOTE_USER', TRUE);
324
325
    $authenticationConf = new LdapAuthenticationConfAdmin();
326
327
    $authenticationConf->ssoEnabled = TRUE;
328
    $authenticationConf->ssoRemoteUserStripDomainName = FALSE;
329 32700c57 Assos Assos
    $authenticationConf->ssoExcludedPaths = [];
330
    $authenticationConf->ssoExcludedHosts = [];
331 85ad3d82 Assos Assos
    $authenticationConf->seamlessLogin = $seamlessLogin;
332
    $authenticationConf->ldapImplementation = $ldapImplementation;
333
334
    if ($sso_name !== FALSE) {
335 32700c57 Assos Assos
      if (strpos($sso_name, '@')) {
336 85ad3d82 Assos Assos
        $sso_name_parts = explode('@', $sso_name);
337
        $sso_name = $sso_name_parts[0];
338
        $authenticationConf->ssoRemoteUserStripDomainName = TRUE;
339
      }
340
      ldap_servers_set_globals('_SERVER', $server_var_key, $sso_name);
341
    }
342
    $authenticationConf->save();
343
    return ldap_authentication_get_valid_conf(TRUE);
344
345
  }
346
347 32700c57 Assos Assos
  /**
348
   * LDAP Authentication Exclusive Mode User Logon Test (ids = LDAP_authen.EM.ULT.*)
349
   */
350
  public function testSSOUserLogon() {
351
352
    module_enable(['ldap_sso', 'ldap_help']);
353 85ad3d82 Assos Assos
354
    $sid = 'activedirectory1';
355
    $testid = 'SSOUserLogon3';
356 32700c57 Assos Assos
    $sids = [$sid];
357 85ad3d82 Assos Assos
    $this->prepTestData(
358
      LDAP_TEST_LDAP_NAME,
359
      $sids,
360
      'ad_authentication',
361
      'SSOUserLogon'
362
      );
363
364
    $this->setSsoServerEnvironment('REMOTE_USER', 'hpotter', 'mod_auth_sspi', TRUE);
365
    $this->drupalGet('user/logout');
366
    $this->drupalGet('user');
367
368 32700c57 Assos Assos
    // Just test that the setup works.
369 85ad3d82 Assos Assos
    $authenticationConf = new LdapAuthenticationConfAdmin();
370
    $this->assertTrue(ldap_servers_get_globals('_SERVER', 'REMOTE_USER', TRUE) == 'hpotter',
371
       '$_SERVER[REMOTE_USER] and $_SERVER[REDIRECT_REMOTE_USER] set properly for test with remote server ' . ldap_servers_get_globals('_SERVER', 'REMOTE_ADDR'), $testid);
372
373
    $setup_success = ($authenticationConf->ssoEnabled == TRUE &&
374
      $authenticationConf->ssoRemoteUserStripDomainName == FALSE &&
375
      $authenticationConf->seamlessLogin == TRUE &&
376
      $authenticationConf->ldapImplementation == 'mod_auth_sspi');
377
378
    $this->assertTrue($setup_success, 'setup ldap sso test worked ', $testid);
379
    if (!$setup_success) {
380
      debug('authenticationConf'); debug($authenticationConf);
381
    }
382
383
    $ldap_servers = ldap_servers_get_servers($sid, 'enabled');
384
    $this->assertTrue(count($ldap_servers) == 1, ' ldap_authentication test server setup successful', $testid);
385
    $hpotter_drupal = user_load_by_name('hpotter');
386
    $ldap_user_conf = ldap_user_conf('admin', TRUE);
387
    $hpotter_ldap = $ldap_user_conf->getProvisionRelatedLdapEntry($hpotter_drupal);
388
    debug('hpotter ldap entry'); debug($hpotter_drupal);
389
390 32700c57 Assos Assos
    $tests = [
391
      'dontstripnames' => ['sso_name' => 'hpotter'],
392
      'stripnames' => ['sso_name' => 'hpotter@hogwarts'],
393 85ad3d82 Assos Assos
394 32700c57 Assos Assos
    ];
395 85ad3d82 Assos Assos
396
    foreach ($tests as $testid => $conf) {
397 32700c57 Assos Assos
      foreach (['REMOTE_USER'] as $server_var_key) {
398
        // ,'user/login/sso'.
399
        foreach (['user'] as $test_path) {
400
          // , 'mod_auth_kerb'.
401
          foreach (['mod_auth_sspi', 'mod_auth_kerb'] as $ldapImplementation) {
402
            // , FALSE.
403
            foreach ([TRUE, FALSE] as $seamlessLogin) {
404 85ad3d82 Assos Assos
              $sso_name = $conf['sso_name'];
405
              $this->ldapTestId = "testSSO._SERVER-key=$server_var_key sso_name=$sso_name path=$test_path ldapImplementation=$ldapImplementation seamlessLogin=$seamlessLogin";
406
              $ldapAuthenticationConf = $this->setSsoServerEnvironment($server_var_key, $sso_name, $ldapImplementation, $seamlessLogin);
407
              $this->drupalGet($test_path);
408 7547bb19 Assos Assos
              // @FIXME: Broken test
409
              // $this->assertText(t('Member for'), 'Successful logon.', $this->ldapTestId);
410 85ad3d82 Assos Assos
              $this->drupalGet('user/logout');
411
            }
412
          }
413
        }
414
      }
415
    }
416
  }
417
418 32700c57 Assos Assos
  /**
419
   *
420
   */
421
  public function testAuthenticationWhitelistTests() {
422
    require_once drupal_get_path('module', 'ldap_authentication') . '/LdapAuthenticationConfAdmin.class.php';
423 85ad3d82 Assos Assos
424
    $sid = 'activedirectory1';
425
    $testid = 'WL3';
426 32700c57 Assos Assos
    $sids = [$sid];
427 85ad3d82 Assos Assos
    $this->prepTestData(
428
      'hogwarts',
429 32700c57 Assos Assos
      [$sid],
430 85ad3d82 Assos Assos
      'provisionToDrupal',
431
      'WL3',
432
      'drupal_role_authentication_test'
433
      );
434
435
    $ldap_servers = ldap_servers_get_servers($sid, 'enabled');
436
    $this->assertTrue(count($ldap_servers) == 1, ' ldap_authentication test server setup successful', $testid);
437
438 32700c57 Assos Assos
    // These 2 modules are configured in setup, but disabled for most authentication tests.
439
    module_disable(['ldap_authorization_drupal_role', 'ldap_authorization']);
440 85ad3d82 Assos Assos
441
    /**
442
     * LDAP_authen.WL.user1  test for user 1 being excluded from white and black list tests
443
     */
444
445
    $user1 = user_load(1);
446
    $password = $this->randomString(20);
447 32700c57 Assos Assos
    require_once DRUPAL_ROOT . '/includes/password.inc';
448
    $account = [
449 85ad3d82 Assos Assos
      'name' => $user1->name,
450
      'pass' => user_hash_password(trim($password)),
451 32700c57 Assos Assos
    ];
452 85ad3d82 Assos Assos
    db_update('users')
453
      ->fields($account)
454
      ->condition('uid', 1)
455
      ->execute();
456
457 32700c57 Assos Assos
    $edit = [
458 85ad3d82 Assos Assos
      'name' => $user1->name,
459
      'pass' => $password,
460 32700c57 Assos Assos
    ];
461 85ad3d82 Assos Assos
462
    $this->drupalPost('user', $edit, t('Log in'));
463
    $this->assertText(t('Member for'), 'User 1 successfully authenticated in LDAP_authen.WL.user1', $testid);
464
    $this->drupalGet('user/logout');
465
466 32700c57 Assos Assos
    module_enable(['ldap_authorization']);
467
    module_enable(['ldap_authorization_drupal_role']);
468 85ad3d82 Assos Assos
469
    /**
470
     * prep LDAP_authen.WL.allow
471
     */
472
    $authenticationConf = new LdapAuthenticationConfAdmin();
473 32700c57 Assos Assos
    $authenticationConf->allowOnlyIfTextInDn = ['pot'];
474 85ad3d82 Assos Assos
    $authenticationConf->save();
475
    $authenticationConf = ldap_authentication_get_valid_conf(TRUE);
476
477 32700c57 Assos Assos
    /**
478 85ad3d82 Assos Assos
    * LDAP_authen.WL.allow.match -- desirect_result: authenticate success
479
    */
480
481
    $this->AttemptLogonNewUser('hpotter');
482
    $this->assertText(t('Member for'), 'User able to authenticate because in white list (allowOnlyIfTextInDn).', $testid);
483
484 32700c57 Assos Assos
    /**
485 85ad3d82 Assos Assos
    *  LDAP_authen.WL.allow.miss -- desirect_result: authenticate fail
486
    */
487
488
    $this->AttemptLogonNewUser('ssnape');
489
    $this->assertText(t('User disallowed'), 'User unable to authenticate because not in white list (allowOnlyIfTextInDn).', $testid);
490
491
    /**
492
    * undo LDAP_authen.WL.allow settings
493
    */
494
495
    $authenticationConf = new LdapAuthenticationConfAdmin();
496 32700c57 Assos Assos
    $authenticationConf->allowOnlyIfTextInDn = [];
497 85ad3d82 Assos Assos
    $authenticationConf->save();
498
    $authenticationConf = ldap_authentication_get_valid_conf(TRUE);
499
    /**
500
    * prep LDAP_authen.WL.exclude
501
    */
502
    $authenticationConf = new LdapAuthenticationConfAdmin();
503 32700c57 Assos Assos
    $authenticationConf->excludeIfTextInDn = ['cn=ssnape'];
504 85ad3d82 Assos Assos
    $authenticationConf->save();
505
    $authenticationConf = ldap_authentication_get_valid_conf(TRUE);
506
507 32700c57 Assos Assos
    /**
508 85ad3d82 Assos Assos
    * LDAP_authen.WL.exclude.match -- desirect_result: authenticate fail
509
    */
510
511
    $this->AttemptLogonNewUser('ssnape');
512
    $this->assertText(t('User disallowed'), 'User unable to authenticate in exclude list (excludeIfTextInDn).', $testid);
513
514 32700c57 Assos Assos
    /**
515 85ad3d82 Assos Assos
    *  LDAP_authen.WL.exclude.miss-- desirect_result: authenticate success
516
    */
517
518
    $this->AttemptLogonNewUser('hpotter');
519
    $this->assertText(t('Member for'), 'Able to authenticate because not in exclude list (allowOnlyIfTextInDn).', $testid);
520
521
    /**
522
    * undo LDAP_authen.WL.allow settings
523
    */
524
525
    $authenticationConf = new LdapAuthenticationConfAdmin();
526 32700c57 Assos Assos
    $authenticationConf->excludeIfTextInDn = [];
527 85ad3d82 Assos Assos
    $authenticationConf->save();
528
    $authenticationConf = ldap_authentication_get_valid_conf(TRUE);
529
530
    /**
531
    * prep LDAP_authen.WL.php
532
    */
533
    $authenticationConf = new LdapAuthenticationConfAdmin();
534
    $authenticationConf->allowTestPhp = "\n
535
      //exclude users with dumb in email address \n
536
      if (strpos(\$_ldap_user_entry['attr']['mail'][0], 'dumb') === FALSE) {\n
537
        print 1;\n
538
      }\n
539
      else {
540
        print 0;\n
541
      }
542
      ";
543
544
    $authenticationConf->save();
545
    $authenticationConf = ldap_authentication_get_valid_conf(TRUE);
546 32700c57 Assos Assos
    /**
547 85ad3d82 Assos Assos
    * LDAP_authen.WL.php.php disabled -- desired result: authenticate fail with warning the authentication disabled
548
    */
549 32700c57 Assos Assos
    module_disable(['php']);
550 85ad3d82 Assos Assos
    $this->AttemptLogonNewUser('adumbledore');
551
    $this->assertText(
552
      LDAP_AUTHENTICATION_DISABLED_FOR_BAD_CONF_MSG,
553
      'With php disabled and php code in whitelist, refuse authentication. (allowTestPhp).',
554
      $testid
555
    );
556 32700c57 Assos Assos
    module_enable(['php']);
557 85ad3d82 Assos Assos
558 32700c57 Assos Assos
    /**
559 85ad3d82 Assos Assos
    * LDAP_authen.WL.php.true -- desired result: authenticate success
560
    */
561
    $this->AttemptLogonNewUser('hpotter');
562
    $this->assertText(t('Member for'), 'Able to authenticate because php returned true (allowTestPhp).', $testid);
563
564 32700c57 Assos Assos
    /**
565 85ad3d82 Assos Assos
    *  LDAP_authen.WL.php.false-- desired result: authenticate fail
566
    */
567
568
    $this->AttemptLogonNewUser('adumbledore');
569
    $this->assertText(
570
      t('User disallowed'),
571
      'User unable to authenticate because php returned false (allowTestPhp).',
572
      $testid
573
    );
574
575 32700c57 Assos Assos
    /**
576 85ad3d82 Assos Assos
    * clear LDAP_authen.WL.php
577
    */
578
    $authenticationConf = new LdapAuthenticationConfAdmin();
579
    $authenticationConf->allowTestPhp = '';
580
    $authenticationConf->save();
581
    $authenticationConf = ldap_authentication_get_valid_conf(TRUE);
582
583 32700c57 Assos Assos
    /**
584 85ad3d82 Assos Assos
   * need to test username changes with PUID
585
   *   - given a user exists
586
   *   - change samaccountname in ldap server
587
   *   - have user logon
588
   *   - make sure old user and new user have same puid
589
   *
590
   */
591
592 32700c57 Assos Assos
    /***  multiple options used in whitelist **/
593 85ad3d82 Assos Assos
594 32700c57 Assos Assos
    /**
595 85ad3d82 Assos Assos
    * LDAP_authen.WL.allow[match].exclude[match] -- desired result: authenticate fail
596
    */
597
598 32700c57 Assos Assos
    /**
599 85ad3d82 Assos Assos
    *  LDAP_authen.WL.allow[match].exclude[miss] -- desired result: authenticate success
600
    */
601
602
    /**
603
    * LDAP_authen.WL.exclude[match].*-- desirect_result: authenticate fail
604
    */
605
606 32700c57 Assos Assos
    /**
607 85ad3d82 Assos Assos
    *  LDAP_authen.WL.exclude[match].php[false] -- desired result: authenticate fail
608
    */
609
610 32700c57 Assos Assos
    /**
611 85ad3d82 Assos Assos
     * LDAP_authen.WL1.excludeIfNoAuthorizations.hasAuthorizations
612
     * test for excludeIfNoAuthorizations set to true and consumer granted authorizations
613
     */
614
615 32700c57 Assos Assos
    // These 2 modules are configured in setup, but disabled for most authentication tests.
616
    module_disable(['ldap_authorization_drupal_role', 'ldap_authorization']);
617 85ad3d82 Assos Assos
    $authenticationConf = new LdapAuthenticationConfAdmin();
618
    $authenticationConf->excludeIfNoAuthorizations = 1;
619
    $authenticationConf->save();
620
    $authenticationConf = ldap_authentication_get_valid_conf(TRUE);
621
    /**
622
     * LDAP_authen.WL1.excludeIfNoAuthorizations.failsafe
623
     * test for excludeIfNoAuthorizations set to true and ldap_authorization disabled
624
     * to make sure authentication fails completely
625
     */
626
627
    $this->AttemptLogonNewUser('hpotter');
628
    $this->assertText(
629
      LDAP_AUTHENTICATION_DISABLED_FOR_BAD_CONF_MSG,
630
      t('Authentication prohibited when excludeIfNoAuthorizations =
631
        true and LDAP Authorization disabled.
632
        LDAP_authen.WL1.excludeIfNoAuthorizations.failsafe'),
633
      $testid
634
    );
635
636 32700c57 Assos Assos
    module_enable(['ldap_authorization'], TRUE);
637
    module_enable(['ldap_authorization_drupal_role'], TRUE);
638
    // Clear static cache.
639
    $consumer = ldap_authorization_get_consumers('drupal_role', TRUE, TRUE);
640 85ad3d82 Assos Assos
641
    $this->AttemptLogonNewUser('hpotter');
642
    $this->assertText(
643
      t('Member for'),
644
      'User able to authenticate because of excludeIfNoAuthorizations setting.',
645
      $testid
646
    );
647
648
    /**
649
     * LDAP_authen.WL1.excludeIfNoAuthorizations.hasNoAuthorizations
650
     * test for excludeIfNoAuthorizations set to true and No consumer
651
     * granted authorizations
652
     */
653
654
    $this->AttemptLogonNewUser('ssnape');
655
    $this->assertText(
656
      t('User disallowed'),
657
      'User unable to authenticate because of excludeIfNoAuthorizations setting.',
658
      $testid
659
    );
660
661
    $authenticationConf = new LdapAuthenticationConfAdmin();
662
    $authenticationConf->excludeIfNoAuthorizations = 0;
663
    $authenticationConf->save();
664
    $authenticationConf = ldap_authentication_get_valid_conf(TRUE);
665 32700c57 Assos Assos
    module_disable(['ldap_authorization_drupal_role', 'ldap_authorization']);
666 85ad3d82 Assos Assos
667
  }
668
669
  /**
670 32700c57 Assos Assos
   * Make sure user admin interface works.
671 85ad3d82 Assos Assos
   */
672 32700c57 Assos Assos
  public function testUI() {
673 85ad3d82 Assos Assos
674 32700c57 Assos Assos
    // Just to give warning if setup doesn't succeed.  may want to take these out at some point.
675 85ad3d82 Assos Assos
    $setup_success = (
676
        module_exists('ldap_user') &&
677
        module_exists('ldap_servers') &&
678
        module_exists('ldap_authentication') &&
679
        (variable_get('ldap_simpletest', 2) > 0)
680
      );
681 bc175c27 Assos Assos
    $this->assertTrue($setup_success, ' ldap_authentication UI setup successful', $this->testId('user interface tests'));
682 85ad3d82 Assos Assos
683
    $sid = 'activedirectory1';
684 32700c57 Assos Assos
    $sids = ['activedirectory1'];
685 85ad3d82 Assos Assos
    $this->prepTestData(LDAP_TEST_LDAP_NAME, $sids, 'provisionToDrupal', 'default');
686
687 32700c57 Assos Assos
    $this->privileged_user = $this->drupalCreateUser([
688 85ad3d82 Assos Assos
      'administer site configuration',
689 32700c57 Assos Assos
    ]);
690 85ad3d82 Assos Assos
691
    $this->drupalLogin($this->privileged_user);
692
693
    $ldap_authentication_conf_pre = ldap_authentication_get_valid_conf();
694
695
    $this->drupalGet('admin/config/people/ldap/authentication');
696
697 32700c57 Assos Assos
    $form_tests = [
698
      'authenticationMode' => [
699 85ad3d82 Assos Assos
        'property' => 'authenticationMode',
700 32700c57 Assos Assos
        'values' => [
701 85ad3d82 Assos Assos
          LDAP_AUTHENTICATION_MIXED,
702 32700c57 Assos Assos
          LDAP_AUTHENTICATION_EXCLUSIVE,
703
        ],
704 85ad3d82 Assos Assos
        'required' => TRUE,
705 32700c57 Assos Assos
      ],
706
      'authenticationServers[' . $sid . ']' => [
707 85ad3d82 Assos Assos
        'property' => 'enabledAuthenticationServers',
708 32700c57 Assos Assos
        'values' => [
709 85ad3d82 Assos Assos
          TRUE,
710
          TRUE,
711 32700c57 Assos Assos
        ],
712
        'desired_result' => [
713
          [$sid],
714
          [$sid],
715
        ],
716
      ],
717
      'loginUIUsernameTxt' => [
718
        'property' => 'loginUIUsernameTxt',
719
        'values' => [
720
          '',
721
          'Hogwarts UserID',
722
        ],
723
      ],
724
      'loginUIPasswordTxt' => [
725
        'property' => 'loginUIPasswordTxt',
726
        'values' => [
727
          '',
728
          'Hogwarts UserID Password',
729
        ],
730
      ],
731
      'ldapUserHelpLinkUrl' => [
732
        'property' => 'ldapUserHelpLinkUrl',
733
        'values' => [
734
          '',
735
          'http://passwords.hogwarts.edu',
736
        ],
737
      ],
738
      'ldapUserHelpLinkText' => [
739
        'property' => 'ldapUserHelpLinkText',
740
        'values' => [
741
          'Hogwarts Password Management Page',
742
          'Hogwarts Password Management Page',
743
        ],
744
      ],
745
      'allowOnlyIfTextInDn' => [
746 85ad3d82 Assos Assos
        'property' => 'allowOnlyIfTextInDn',
747 32700c57 Assos Assos
        'values' => [
748 85ad3d82 Assos Assos
          'witch\nwarlord\nwisecracker',
749 32700c57 Assos Assos
          "witch\nwarlord\nwisecracker",
750
        ],
751
        'desired_result' => [
752
          ['witch', 'warlord', 'wisecracker'],
753
          ['witch', 'warlord', 'wisecracker'],
754
        ],
755
      ],
756
      'excludeIfTextInDn' => [
757 85ad3d82 Assos Assos
        'property' => 'excludeIfTextInDn',
758 32700c57 Assos Assos
        'values' => [
759
          "muggle\nmuddle\nmummy",
760 85ad3d82 Assos Assos
          "muggle\nmuddle\nmummy",
761 32700c57 Assos Assos
        ],
762
        'desired_result' => [
763
          ['muggle', 'muddle', 'mummy'],
764
          ['muggle', 'muddle', 'mummy'],
765
        ],
766
      ],
767
      'excludeIfNoAuthorizations' => [
768 85ad3d82 Assos Assos
        'property' => 'excludeIfNoAuthorizations',
769 32700c57 Assos Assos
        'values' => [
770 91af538d Assos Assos
          1,
771
          1,
772 32700c57 Assos Assos
        ],
773
      ],
774
      'emailOption' => [
775 85ad3d82 Assos Assos
        'property' => 'emailOption',
776 32700c57 Assos Assos
        'values' => [
777 85ad3d82 Assos Assos
          LDAP_AUTHENTICATION_EMAIL_FIELD_REMOVE,
778
          LDAP_AUTHENTICATION_EMAIL_FIELD_DISABLE,
779 32700c57 Assos Assos
        ],
780 85ad3d82 Assos Assos
        'required' => TRUE,
781 32700c57 Assos Assos
      ],
782
      'emailUpdate' => [
783 85ad3d82 Assos Assos
        'property' => 'emailUpdate',
784 32700c57 Assos Assos
        'values' => [
785 85ad3d82 Assos Assos
          LDAP_AUTHENTICATION_EMAIL_UPDATE_ON_LDAP_CHANGE_ENABLE,
786
          LDAP_AUTHENTICATION_EMAIL_UPDATE_ON_LDAP_CHANGE_DISABLE,
787 32700c57 Assos Assos
        ],
788 85ad3d82 Assos Assos
        'required' => TRUE,
789 32700c57 Assos Assos
      ],
790
      'allowTestPhp' => [
791 85ad3d82 Assos Assos
        'property' => 'allowTestPhp',
792 32700c57 Assos Assos
        'values' => [
793 85ad3d82 Assos Assos
          'pretend php',
794
          'pretend php',
795 32700c57 Assos Assos
        ],
796
      ],
797
    ];
798 85ad3d82 Assos Assos
799 32700c57 Assos Assos
    module_enable(['php']);
800
    foreach ([0, 1] as $i) {
801
      $edit = [];
802 85ad3d82 Assos Assos
      foreach ($form_tests as $field_name => $conf) {
803
        $value = $conf['values'][$i];
804
        $property = isset($conf['property']) ? $conf['property'] : $field_name;
805
        $edit[$field_name] = $value;
806
      }
807
      $this->drupalPost('admin/config/people/ldap/authentication', $edit, t('Save'));
808
      $ldap_authentication_conf_post = ldap_authentication_get_valid_conf(TRUE);
809
810
      foreach ($form_tests as $field_name => $conf) {
811
        $property = isset($conf['property']) ? $conf['property'] : $field_name;
812
        $desired = isset($conf['desired_result']) ? isset($conf['desired_result'][$i]) : $conf['values'][$i];
813
814 91af538d Assos Assos
        if (is_array($desired) && is_array($ldap_authentication_conf_post->{$property})) {
815 85ad3d82 Assos Assos
          $success = count($desired) == count($ldap_authentication_conf_post->{$property});
816
        }
817
        else {
818
          $success = ($ldap_authentication_conf_post->{$property} == $desired);
819
        }
820
        $this->assertTrue(
821
          $success,
822
          $property . ' ' . t('field set correctly'),
823
          $this->testId('ldap authentication user interface tests')
824
        );
825
        if (!$success) {
826
          debug("fail $i $property");
827
          debug("desired:"); debug($desired);
828
          debug("actual:");  debug($ldap_authentication_conf_post->{$property});
829
        }
830
      }
831
    }
832
  }
833
834
}