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
<?php
2

    
3
/**
4
 * @file
5
 * Ldap_authentication simpletests.
6
 */
7

    
8
module_load_include('php', 'ldap_test', 'LdapTestCase.class');
9
module_load_include('module', 'ldap_authentication');
10

    
11
/**
12
 * LdapAuthenticationTestCase.
13
 */
14
class LdapAuthenticationTestCase extends LdapTestCase {
15

    
16
  public $module_name = 'ldap_authentication';
17
  protected $ldap_test_data;
18

    
19
  /**
20
   * Get info.
21
   */
22
  public static function getInfo() {
23
    return [
24
      'name' => 'LDAP Authentication Tests',
25
      'description' => 'Test ldap authentication.',
26
      'group' => 'LDAP Authentication',
27
    ];
28
  }
29

    
30
  /**
31
   * {@inheritdoc}
32
   */
33
  public function __construct($test_id = NULL) {
34
    parent::__construct($test_id);
35
  }
36

    
37
  /**
38
   *
39
   */
40
  public function setUp() {
41
    parent::setUp([
42
      'ldap_authentication',
43
      'ldap_authorization',
44
      'ldap_authorization_drupal_role',
45
      'ldap_test',
46
    // don't need any real servers, configured, just ldap_servers code base.
47
    ]);
48
    variable_set('ldap_simpletest', 2);
49
  }
50

    
51
  /**
52
   *
53
   */
54
  public function tearDown() {
55
    parent::tearDown();
56
    variable_del('ldap_help_watchdog_detail');
57
    variable_del('ldap_simpletest');
58
  }
59

    
60
  /**
61
   * Difficult to test install and uninstall since setUp does module enabling and installing.
62
   */
63
  public function testInstall() {
64
    $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
  /**
74
   * LDAP Authentication Mixed Mode User Logon Test (ids = LDAP_authen.MM.ULT.*)
75
   */
76
  public function testMixedModeUserLogon() {
77
    $sid = 'activedirectory1';
78
    $testid = 'MixedModeUserLogon3';
79
    $sids = [$sid];
80
    $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
    require_once DRUPAL_ROOT . '/includes/password.inc';
98
    $account = [
99
      'name' => $user1->name,
100
      'pass' => user_hash_password(trim($password)),
101
    ];
102
    db_update('users')
103
      ->fields($account)
104
      ->condition('uid', 1)
105
      ->execute();
106

    
107
    $edit = [
108
      'name' => $user1->name,
109
      'pass' => $password,
110
    ];
111

    
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
    $edit = [
119
      'name' => $user1->name,
120
      'pass' => 'mydabpassword',
121
    ];
122

    
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
    $edit = [
132
      'name' => $drupal_user->name,
133
      'pass' => $raw_pass,
134
    ];
135
    $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
    $edit = [
141
      'name' => $drupal_user->name,
142
      'pass' => 'mydabpassword',
143
    ];
144
    $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
    $edit = [
150
      'name' => 'hpotter',
151
      'pass' => 'mydabpassword',
152
    ];
153
    $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
    $edit = [
159
      'name' => 'hpotter',
160
      'pass' => 'goodpwd',
161
    ];
162
    $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
    $edit = [
169
      'name' => 'hpotter',
170
      'pass' => 'mydabpassword',
171
    ];
172
    $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
    $edit = [
178
      'name' => 'hpotter',
179
      'pass' => 'goodpwd',
180
    ];
181
    $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
  /**
189
   * LDAP Authentication Exclusive Mode User Logon Test (ids = LDAP_authen.EM.ULT.*)
190
   */
191
  public function testExclusiveModeUserLogon() {
192

    
193
    $sid = 'activedirectory1';
194
    $testid = 'ExclusiveModeUserLogon3';
195
    $sids = [$sid];
196
    $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
    require_once DRUPAL_ROOT . '/includes/password.inc';
214
    $account = [
215
      'name' => $user1->name,
216
      'pass' => user_hash_password(trim($password)),
217
    ];
218
    db_update('users')
219
      ->fields($account)
220
      ->condition('uid', 1)
221
      ->execute();
222

    
223
    $edit = [
224
      'name' => $user1->name,
225
      'pass' => $password,
226
    ];
227

    
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
    $edit = [
235
      'name' => $user1->name,
236
      'pass' => 'mydabpassword',
237
    ];
238

    
239
    $this->drupalPost('user', $edit, t('Log in'));
240
    $this->assertText(t('Sorry, unrecognized username or password'), 'User 1 failed with bad password', $testid);
241
    $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
    $edit = [
248
      'name' => $drupal_user->name,
249
      'pass' => $raw_pass,
250
    ];
251
    $this->drupalPost('user', $edit, t('Log in'));
252
    $this->assertText(t('Sorry, unrecognized username or password'), 'Drupal user successfully authenticated', $testid);
253
    $this->drupalGet('user/logout');
254

    
255
    /** LDAP_authen.EM.ULT.drupal.badpwd - result: Drupal logon error message. **/
256
    $edit = [
257
      'name' => $drupal_user->name,
258
      'pass' => 'mydabpassword',
259
    ];
260
    $this->drupalPost('user', $edit, t('Log in'));
261
    $this->assertText(t('Sorry, unrecognized username or password'), 'Drupal user with bad password failed to authenticate.', $testid);
262
    $this->drupalGet('user/logout');
263

    
264
    /** LDAP_authen.EM.ULT.ldap.newaccount.badpwd - result: Drupal logon error message. **/
265
    $edit = [
266
      'name' => 'hpotter',
267
      'pass' => 'mydabpassword',
268
    ];
269
    $this->drupalPost('user', $edit, t('Log in'));
270
    $this->assertText(t('Sorry, unrecognized username or password'), 'New Ldap user with bad password failed to authenticate.', $testid);
271
    $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
    $edit = [
275
      'name' => 'hpotter',
276
      'pass' => 'goodpwd',
277
    ];
278
    $this->drupalPost('user', $edit, t('Log in'));
279
    $this->assertText(t('Member for'), 'New Ldap user with good password authenticated.');
280
    $this->assertTrue($this->testFunctions->ldapUserIsAuthmapped('hpotter'), 'Ldap user properly authmapped.', $testid);
281
    $this->drupalGet('user/logout');
282

    
283
    /** LDAP_authen.EM.ULT.existingacct.badpwd - result: Drupal logon error message. **/
284
    $edit = [
285
      'name' => 'hpotter',
286
      'pass' => 'mydabpassword',
287
    ];
288
    $this->drupalPost('user', $edit, t('Log in'));
289
    $this->assertText(t('Sorry, unrecognized username or password'), 'Existing Ldap user with bad password failed to authenticate.', $testid);
290
    $this->drupalGet('user/logout');
291

    
292
    /** LDAP_authen.MM.ULT.existingacct.goodpwd - result: Successful logon. **/
293
    $edit = [
294
      'name' => 'hpotter',
295
      'pass' => 'goodpwd',
296
    ];
297
    $this->drupalPost('user', $edit, t('Log in'));
298
    $this->assertText(t('Member for'), 'Existing Ldap user with good password authenticated.');
299
    $this->assertTrue($this->testFunctions->ldapUserIsAuthmapped('hpotter'), 'Existing Ldap user still properly authmapped.', $testid);
300
    $this->drupalGet('user/logout');
301

    
302
  }
303

    
304
  /**
305
   * Set mock server variables for sso tests.
306
   *
307
   * @param string $server_var_key
308
   * @param string $sso_name
309
   * @param string $ldapImplementation
310
   * @param bool $seamlessLogin
311
   *
312
   * @return object
313
   */
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
    // Clear past environment.
322
    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
    $authenticationConf->ssoExcludedPaths = [];
330
    $authenticationConf->ssoExcludedHosts = [];
331
    $authenticationConf->seamlessLogin = $seamlessLogin;
332
    $authenticationConf->ldapImplementation = $ldapImplementation;
333

    
334
    if ($sso_name !== FALSE) {
335
      if (strpos($sso_name, '@')) {
336
        $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
  /**
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

    
354
    $sid = 'activedirectory1';
355
    $testid = 'SSOUserLogon3';
356
    $sids = [$sid];
357
    $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
    // Just test that the setup works.
369
    $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
    $tests = [
391
      'dontstripnames' => ['sso_name' => 'hpotter'],
392
      'stripnames' => ['sso_name' => 'hpotter@hogwarts'],
393

    
394
    ];
395

    
396
    foreach ($tests as $testid => $conf) {
397
      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
              $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
              // @FIXME: Broken test
409
              // $this->assertText(t('Member for'), 'Successful logon.', $this->ldapTestId);
410
              $this->drupalGet('user/logout');
411
            }
412
          }
413
        }
414
      }
415
    }
416
  }
417

    
418
  /**
419
   *
420
   */
421
  public function testAuthenticationWhitelistTests() {
422
    require_once drupal_get_path('module', 'ldap_authentication') . '/LdapAuthenticationConfAdmin.class.php';
423

    
424
    $sid = 'activedirectory1';
425
    $testid = 'WL3';
426
    $sids = [$sid];
427
    $this->prepTestData(
428
      'hogwarts',
429
      [$sid],
430
      '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
    // These 2 modules are configured in setup, but disabled for most authentication tests.
439
    module_disable(['ldap_authorization_drupal_role', 'ldap_authorization']);
440

    
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
    require_once DRUPAL_ROOT . '/includes/password.inc';
448
    $account = [
449
      'name' => $user1->name,
450
      'pass' => user_hash_password(trim($password)),
451
    ];
452
    db_update('users')
453
      ->fields($account)
454
      ->condition('uid', 1)
455
      ->execute();
456

    
457
    $edit = [
458
      'name' => $user1->name,
459
      'pass' => $password,
460
    ];
461

    
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
    module_enable(['ldap_authorization']);
467
    module_enable(['ldap_authorization_drupal_role']);
468

    
469
    /**
470
     * prep LDAP_authen.WL.allow
471
     */
472
    $authenticationConf = new LdapAuthenticationConfAdmin();
473
    $authenticationConf->allowOnlyIfTextInDn = ['pot'];
474
    $authenticationConf->save();
475
    $authenticationConf = ldap_authentication_get_valid_conf(TRUE);
476

    
477
    /**
478
    * 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
    /**
485
    *  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
    $authenticationConf->allowOnlyIfTextInDn = [];
497
    $authenticationConf->save();
498
    $authenticationConf = ldap_authentication_get_valid_conf(TRUE);
499
    /**
500
    * prep LDAP_authen.WL.exclude
501
    */
502
    $authenticationConf = new LdapAuthenticationConfAdmin();
503
    $authenticationConf->excludeIfTextInDn = ['cn=ssnape'];
504
    $authenticationConf->save();
505
    $authenticationConf = ldap_authentication_get_valid_conf(TRUE);
506

    
507
    /**
508
    * 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
    /**
515
    *  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
    $authenticationConf->excludeIfTextInDn = [];
527
    $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
    /**
547
    * LDAP_authen.WL.php.php disabled -- desired result: authenticate fail with warning the authentication disabled
548
    */
549
    module_disable(['php']);
550
    $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
    module_enable(['php']);
557

    
558
    /**
559
    * 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
    /**
565
    *  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
    /**
576
    * 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
    /**
584
   * 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
    /***  multiple options used in whitelist **/
593

    
594
    /**
595
    * LDAP_authen.WL.allow[match].exclude[match] -- desired result: authenticate fail
596
    */
597

    
598
    /**
599
    *  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
    /**
607
    *  LDAP_authen.WL.exclude[match].php[false] -- desired result: authenticate fail
608
    */
609

    
610
    /**
611
     * LDAP_authen.WL1.excludeIfNoAuthorizations.hasAuthorizations
612
     * test for excludeIfNoAuthorizations set to true and consumer granted authorizations
613
     */
614

    
615
    // These 2 modules are configured in setup, but disabled for most authentication tests.
616
    module_disable(['ldap_authorization_drupal_role', 'ldap_authorization']);
617
    $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
    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

    
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
    module_disable(['ldap_authorization_drupal_role', 'ldap_authorization']);
666

    
667
  }
668

    
669
  /**
670
   * Make sure user admin interface works.
671
   */
672
  public function testUI() {
673

    
674
    // Just to give warning if setup doesn't succeed.  may want to take these out at some point.
675
    $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
    $this->assertTrue($setup_success, ' ldap_authentication UI setup successful', $this->testId('user interface tests'));
682

    
683
    $sid = 'activedirectory1';
684
    $sids = ['activedirectory1'];
685
    $this->prepTestData(LDAP_TEST_LDAP_NAME, $sids, 'provisionToDrupal', 'default');
686

    
687
    $this->privileged_user = $this->drupalCreateUser([
688
      'administer site configuration',
689
    ]);
690

    
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
    $form_tests = [
698
      'authenticationMode' => [
699
        'property' => 'authenticationMode',
700
        'values' => [
701
          LDAP_AUTHENTICATION_MIXED,
702
          LDAP_AUTHENTICATION_EXCLUSIVE,
703
        ],
704
        'required' => TRUE,
705
      ],
706
      'authenticationServers[' . $sid . ']' => [
707
        'property' => 'enabledAuthenticationServers',
708
        'values' => [
709
          TRUE,
710
          TRUE,
711
        ],
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
        'property' => 'allowOnlyIfTextInDn',
747
        'values' => [
748
          'witch\nwarlord\nwisecracker',
749
          "witch\nwarlord\nwisecracker",
750
        ],
751
        'desired_result' => [
752
          ['witch', 'warlord', 'wisecracker'],
753
          ['witch', 'warlord', 'wisecracker'],
754
        ],
755
      ],
756
      'excludeIfTextInDn' => [
757
        'property' => 'excludeIfTextInDn',
758
        'values' => [
759
          "muggle\nmuddle\nmummy",
760
          "muggle\nmuddle\nmummy",
761
        ],
762
        'desired_result' => [
763
          ['muggle', 'muddle', 'mummy'],
764
          ['muggle', 'muddle', 'mummy'],
765
        ],
766
      ],
767
      'excludeIfNoAuthorizations' => [
768
        'property' => 'excludeIfNoAuthorizations',
769
        'values' => [
770
          1,
771
          1,
772
        ],
773
      ],
774
      'emailOption' => [
775
        'property' => 'emailOption',
776
        'values' => [
777
          LDAP_AUTHENTICATION_EMAIL_FIELD_REMOVE,
778
          LDAP_AUTHENTICATION_EMAIL_FIELD_DISABLE,
779
        ],
780
        'required' => TRUE,
781
      ],
782
      'emailUpdate' => [
783
        'property' => 'emailUpdate',
784
        'values' => [
785
          LDAP_AUTHENTICATION_EMAIL_UPDATE_ON_LDAP_CHANGE_ENABLE,
786
          LDAP_AUTHENTICATION_EMAIL_UPDATE_ON_LDAP_CHANGE_DISABLE,
787
        ],
788
        'required' => TRUE,
789
      ],
790
      'allowTestPhp' => [
791
        'property' => 'allowTestPhp',
792
        'values' => [
793
          'pretend php',
794
          'pretend php',
795
        ],
796
      ],
797
    ];
798

    
799
    module_enable(['php']);
800
    foreach ([0, 1] as $i) {
801
      $edit = [];
802
      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
        if (is_array($desired) && is_array($ldap_authentication_conf_post->{$property})) {
815
          $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
}