Projet

Général

Profil

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

root / drupal7 / sites / all / modules / ldap / ldap_authentication / tests / ldap_authentication.test @ 59ae487e

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
class LdapAuthenticationTestCase extends LdapTestCase {
12
  public static function getInfo() {
13
    return array(
14
      'name' => 'LDAP Authentication Tests',
15
      'description' => 'Test ldap authentication.',
16
      'group' => 'LDAP Authentication'
17
    );
18
  }
19

    
20
  function __construct($test_id = NULL) {
21
    parent::__construct($test_id);
22
  }
23

    
24
  public $module_name = 'ldap_authentication';
25
  protected $ldap_test_data;
26

    
27
  function setUp() {
28
    parent::setUp(array(
29
      'ldap_authentication',
30
      'ldap_authorization',
31
      'ldap_authorization_drupal_role',
32
      'ldap_test',
33
      )); // don't need any real servers, configured, just ldap_servers code base
34
    variable_set('ldap_simpletest', 2);
35
  }
36

    
37
  function tearDown() {
38
    parent::tearDown();
39
    variable_del('ldap_help_watchdog_detail');
40
    variable_del('ldap_simpletest');
41
  }
42

    
43
  /**
44
   * difficult to test install and uninstall since setUp does module enabling and installing.
45
   */
46
  function testInstall() {
47
    $testid = $this->module_name . ': setup success';
48
    $setup_success = (
49
        module_exists('ldap_authentication') &&
50
        module_exists('ldap_servers')
51
      );
52

    
53
    $this->assertTrue($setup_success, ' ldap_authentication setup successful', $testid);
54
  }
55

    
56

    
57
/**
58
 *  LDAP Authentication Mixed Mode User Logon Test (ids = LDAP_authen.MM.ULT.*)
59
 */
60

    
61

    
62
  function testMixedModeUserLogon() {
63
    $sid = 'activedirectory1';
64
    $testid = 'MixedModeUserLogon3';
65
    $sids = array($sid);
66
    $this->prepTestData(
67
      LDAP_TEST_LDAP_NAME,
68
      $sids,
69
      'provisionToDrupal',
70
      'MixedModeUserLogon3',
71
      'drupal_role_authentication_test'
72
    );
73

    
74
    $ldap_servers = ldap_servers_get_servers($sid, 'enabled');
75
    $this->assertTrue(count($ldap_servers) == 1, ' ldap_authentication test server setup successful', $testid);
76

    
77
    /**
78
     * LDAP_authen.MM.ULT.user1.goodpwd -- result: Successful logon as user 1
79
     */
80

    
81
    $user1 = user_load(1);
82
    $password = $this->randomString(20);
83
    require_once(DRUPAL_ROOT . '/includes/password.inc');
84
    $account = array(
85
      'name' => $user1->name,
86
      'pass' => user_hash_password(trim($password)),
87
    );
88
    db_update('users')
89
      ->fields($account)
90
      ->condition('uid', 1)
91
      ->execute();
92

    
93
    $edit = array(
94
      'name' => $user1->name,
95
      'pass' => $password,
96
    );
97

    
98
    $this->drupalPost('user', $edit, t('Log in'));
99
    $this->assertText(t('Member for'), 'User 1 successfully authenticated', $testid);
100
    $this->drupalGet('user/logout');
101

    
102
    /** LDAP_authen.MM.ULT.user1.badpwd  -- result: Drupal logon error message. **/
103

    
104
    $edit = array(
105
      'name' => $user1->name,
106
      'pass' => 'mydabpassword',
107
    );
108

    
109
    $this->drupalPost('user', $edit, t('Log in'));
110
    $this->assertText(t('Sorry, unrecognized username or password'), 'User 1 failed with bad password', $testid);
111
    $this->drupalLogout();
112

    
113
    /** LDAP_authen.MM.ULT.drupal.goodpwd - result: Successful logon **/
114

    
115
    $drupal_user = $this->drupalCreateUser();
116
    $raw_pass = $drupal_user->pass_raw;
117
    $edit = array(
118
      'name' => $drupal_user->name,
119
      'pass' => $raw_pass,
120
    );
121
    $this->drupalPost('user', $edit, t('Log in'));
122
    $this->assertText(t('Member for'), 'Drupal user (not ldap associated) successfully authenticated', $testid);
123
    $this->drupalGet('user/logout');
124

    
125
    /** LDAP_authen.MM.ULT.drupal.badpwd - result: Drupal logon error message. **/
126
    $edit = array(
127
      'name' => $drupal_user->name,
128
      'pass' => 'mydabpassword',
129
    );
130
    $this->drupalPost('user', $edit, t('Log in'));
131
    $this->assertText(t('Sorry, unrecognized username or password'), 'Drupal user  (not ldap associated) with bad password failed to authenticate.', $testid);
132
    $this->drupalGet('user/logout');
133

    
134
    /** LDAP_authen.MM.ULT.ldap.newaccount.badpwd - result: Drupal logon error message. **/
135
    $edit = array(
136
      'name' => 'hpotter',
137
      'pass' => 'mydabpassword',
138
    );
139
    $this->drupalPost('user', $edit, t('Log in'));
140
    $this->assertText(t('Sorry, unrecognized username or password'), 'New Ldap user with bad password failed to authenticate.', $testid);
141
    $this->drupalGet('user/logout');
142

    
143

    
144
    /** LDAP_authen.MM.ULT.ldap.newaccount.goodpwd - result: Successful logon, with user record created and authmapped to ldap **/
145
    $edit = array(
146
      'name' => 'hpotter',
147
      'pass' => 'goodpwd',
148
    );
149
    $this->drupalPost('user', $edit, t('Log in'));
150
    $this->assertText(t('Member for'), 'New Ldap user with good password authenticated.');
151
    $this->assertTrue($this->testFunctions->ldapUserIsAuthmapped('hpotter'), 'Ldap user properly authmapped.', $testid);
152
    $this->drupalGet('user/logout');
153

    
154
    /** LDAP_authen.MM.ULT.existingacct.badpwd - result: Drupal logon error message. **/
155
    $edit = array(
156
      'name' => 'hpotter',
157
      'pass' => 'mydabpassword',
158
    );
159
    $this->drupalPost('user', $edit, t('Log in'));
160
    $this->assertText(t('Sorry, unrecognized username or password'), 'Existing Ldap user with bad password failed to authenticate.', $testid);
161
    $this->drupalGet('user/logout');
162

    
163
    /** LDAP_authen.MM.ULT.existingacct.goodpwd - result: Successful logon. **/
164
    $edit = array(
165
      'name' => 'hpotter',
166
      'pass' => 'goodpwd',
167
    );
168
    $this->drupalPost('user', $edit, t('Log in'));
169
    $this->assertText(t('Member for'), 'Existing Ldap user with good password authenticated.');
170
    $this->assertTrue($this->testFunctions->ldapUserIsAuthmapped('hpotter'), 'Existing Ldap user still properly authmapped.', $testid);
171
    $this->drupalGet('user/logout');
172

    
173
  }
174

    
175

    
176
/**
177
 *  LDAP Authentication Exclusive Mode User Logon Test (ids = LDAP_authen.EM.ULT.*)
178
 */
179
  function testExclusiveModeUserLogon() {
180

    
181
    $sid = 'activedirectory1';
182
    $testid = 'ExclusiveModeUserLogon3';
183
    $sids = array($sid);
184
    $this->prepTestData(
185
      LDAP_TEST_LDAP_NAME,
186
      $sids,
187
      'ad_authentication',
188
      'ExclusiveModeUserLogon3',
189
      'drupal_role_authentication_test'
190
      );
191

    
192
    $ldap_servers = ldap_servers_get_servers($sid, 'enabled');
193
    $this->assertTrue(count($ldap_servers) == 1, ' ldap_authentication test server setup successful', $testid);
194

    
195
    /**
196
     * LDAP_authen.EM.ULT.user1.goodpwd -- result: Successful logon as user 1
197
     */
198

    
199
    $user1 = user_load(1);
200
    $password = $this->randomString(20);
201
    require_once(DRUPAL_ROOT . '/includes/password.inc');
202
    $account = array(
203
      'name' => $user1->name,
204
      'pass' => user_hash_password(trim($password)),
205
    );
206
    db_update('users')
207
      ->fields($account)
208
      ->condition('uid', 1)
209
      ->execute();
210

    
211
    $edit = array(
212
      'name' => $user1->name,
213
      'pass' => $password,
214
    );
215

    
216
    $this->drupalPost('user', $edit, t('Log in'));
217
    $this->assertText(t('Member for'), 'User 1 successfully authenticated', $testid);
218
    $this->drupalGet('user/logout');
219

    
220
    /** LDAP_authen.EM.ULT.user1.badpwd  -- result: Drupal logon error message. **/
221

    
222
    $edit = array(
223
      'name' => $user1->name,
224
      'pass' => 'mydabpassword',
225
    );
226

    
227
    $this->drupalPost('user', $edit, t('Log in'));
228
    $this->assertText(t('Sorry, unrecognized username or password'),  'User 1 failed with bad password', $testid );
229
    $this->drupalLogout();
230

    
231
    /** LDAP_authen.EM.ULT.drupal.goodpwd - result: failed logon **/
232

    
233
    $drupal_user = $this->drupalCreateUser();
234
    $raw_pass = $drupal_user->pass_raw;
235
    $edit = array(
236
      'name' => $drupal_user->name,
237
      'pass' => $raw_pass,
238
    );
239
    $this->drupalPost('user', $edit, t('Log in'));
240
    $this->assertText(t('Sorry, unrecognized username or password'), 'Drupal user successfully authenticated', $testid );
241
    $this->drupalGet('user/logout');
242

    
243
    /** LDAP_authen.EM.ULT.drupal.badpwd - result: Drupal logon error message. **/
244
    $edit = array(
245
      'name' => $drupal_user->name,
246
      'pass' => 'mydabpassword',
247
    );
248
    $this->drupalPost('user', $edit, t('Log in'));
249
    $this->assertText(t('Sorry, unrecognized username or password'), 'Drupal user with bad password failed to authenticate.', $testid );
250
    $this->drupalGet('user/logout');
251

    
252

    
253
    /** LDAP_authen.EM.ULT.ldap.newaccount.badpwd - result: Drupal logon error message. **/
254
    $edit = array(
255
      'name' => 'hpotter',
256
      'pass' => 'mydabpassword',
257
    );
258
    $this->drupalPost('user', $edit, t('Log in'));
259
    $this->assertText(t('Sorry, unrecognized username or password'), 'New Ldap user with bad password failed to authenticate.', $testid );
260
    $this->drupalGet('user/logout');
261

    
262

    
263

    
264
    /** LDAP_authen.EM.ULT.ldap.newaccount.goodpwd - result: Successful logon, with user record created and authmapped to ldap **/
265
    $edit = array(
266
      'name' => 'hpotter',
267
      'pass' => 'goodpwd',
268
    );
269
    $this->drupalPost('user', $edit, t('Log in'));
270
    $this->assertText(t('Member for'), 'New Ldap user with good password authenticated.');
271
    $this->assertTrue($this->testFunctions->ldapUserIsAuthmapped('hpotter'), 'Ldap user properly authmapped.', $testid );
272
    $this->drupalGet('user/logout');
273

    
274

    
275
    /** LDAP_authen.EM.ULT.existingacct.badpwd - result: Drupal logon error message. **/
276
    $edit = array(
277
      'name' => 'hpotter',
278
      'pass' => 'mydabpassword',
279
    );
280
    $this->drupalPost('user', $edit, t('Log in'));
281
    $this->assertText(t('Sorry, unrecognized username or password'), 'Existing Ldap user with bad password failed to authenticate.', $testid );
282
    $this->drupalGet('user/logout');
283

    
284
    /** LDAP_authen.MM.ULT.existingacct.goodpwd - result: Successful logon. **/
285
    $edit = array(
286
      'name' => 'hpotter',
287
      'pass' => 'goodpwd',
288
    );
289
    $this->drupalPost('user', $edit, t('Log in'));
290
    $this->assertText(t('Member for'), 'Existing Ldap user with good password authenticated.');
291
    $this->assertTrue($this->testFunctions->ldapUserIsAuthmapped('hpotter'), 'Existing Ldap user still properly authmapped.', $testid );
292
    $this->drupalGet('user/logout');
293

    
294
  }
295

    
296
  /**
297
   * set mock server variables for sso tests
298
   *
299
   * @param object $drupal_user in drupal stdClass format
300
   * @param array $ldap_user array
301
   * @param enum ldap implementation type
302
   *
303
   */
304
  private function setSsoServerEnvironment(
305
    $server_var_key = 'REMOTE_USER',
306
    $sso_name = 'hpotter',
307
    $ldapImplementation = 'mod_auth_sspi',
308
    $seamlessLogin = TRUE
309
  ) {
310

    
311
    // clear past environment
312
    ldap_servers_delete_globals('_SERVER', 'REMOTE_USER', TRUE);
313
    ldap_servers_delete_globals('_SERVER', 'REDIRECT_REMOTE_USER', TRUE);
314

    
315
    $authenticationConf = new LdapAuthenticationConfAdmin();
316

    
317
    $authenticationConf->ssoEnabled = TRUE;
318
    $authenticationConf->ssoRemoteUserStripDomainName = FALSE;
319
    $authenticationConf->ssoExcludedPaths = array();
320
    $authenticationConf->ssoExcludedHosts = array();
321
    $authenticationConf->seamlessLogin = $seamlessLogin;
322
    $authenticationConf->ldapImplementation = $ldapImplementation;
323

    
324
    if ($sso_name !== FALSE) {
325
      if (strpos($sso_name, '@'))  {
326
        $sso_name_parts = explode('@', $sso_name);
327
        $sso_name = $sso_name_parts[0];
328
        $authenticationConf->ssoRemoteUserStripDomainName = TRUE;
329
      }
330
      ldap_servers_set_globals('_SERVER', $server_var_key, $sso_name);
331
    }
332
    $authenticationConf->save();
333
    return ldap_authentication_get_valid_conf(TRUE);
334

    
335
  }
336
/**
337
 *  LDAP Authentication Exclusive Mode User Logon Test (ids = LDAP_authen.EM.ULT.*)
338
 */
339
  function testSSOUserLogon() {
340

    
341
    module_enable(array('ldap_sso', 'ldap_help'));
342

    
343
    $sid = 'activedirectory1';
344
    $testid = 'SSOUserLogon3';
345
    $sids = array($sid);
346
    $this->prepTestData(
347
      LDAP_TEST_LDAP_NAME,
348
      $sids,
349
      'ad_authentication',
350
      'SSOUserLogon'
351
      );
352

    
353
    $this->setSsoServerEnvironment('REMOTE_USER', 'hpotter', 'mod_auth_sspi', TRUE);
354
    $this->drupalGet('user/logout');
355
    $this->drupalGet('user');
356

    
357
    // just test that the setup works
358
    $authenticationConf = new LdapAuthenticationConfAdmin();
359
    $this->assertTrue(ldap_servers_get_globals('_SERVER', 'REMOTE_USER', TRUE) == 'hpotter',
360
       '$_SERVER[REMOTE_USER] and $_SERVER[REDIRECT_REMOTE_USER] set properly for test with remote server ' . ldap_servers_get_globals('_SERVER', 'REMOTE_ADDR'), $testid);
361

    
362
    $setup_success = ($authenticationConf->ssoEnabled == TRUE &&
363
      $authenticationConf->ssoRemoteUserStripDomainName == FALSE &&
364
      $authenticationConf->seamlessLogin == TRUE &&
365
      $authenticationConf->ldapImplementation == 'mod_auth_sspi');
366

    
367
    $this->assertTrue($setup_success, 'setup ldap sso test worked ', $testid);
368
    if (!$setup_success) {
369
      debug('authenticationConf'); debug($authenticationConf);
370
    }
371

    
372
    $ldap_servers = ldap_servers_get_servers($sid, 'enabled');
373
    $this->assertTrue(count($ldap_servers) == 1, ' ldap_authentication test server setup successful', $testid);
374
    $hpotter_drupal = user_load_by_name('hpotter');
375
    $ldap_user_conf = ldap_user_conf('admin', TRUE);
376
    $hpotter_ldap = $ldap_user_conf->getProvisionRelatedLdapEntry($hpotter_drupal);
377
    debug('hpotter ldap entry'); debug($hpotter_drupal);
378

    
379
    $tests = array(
380
      'dontstripnames' => array('sso_name' => 'hpotter'),
381
      'stripnames' => array('sso_name' => 'hpotter@hogwarts'),
382

    
383
    );
384

    
385
    foreach ($tests as $testid => $conf) {
386
      foreach (array('REMOTE_USER') as $server_var_key) {
387
        foreach (array('user') as $test_path) { // ,'user/login/sso'
388
          foreach (array('mod_auth_sspi', 'mod_auth_kerb') as $ldapImplementation) {  // , 'mod_auth_kerb'
389
            foreach (array(TRUE, FALSE) as $seamlessLogin ) {  // , FALSE
390
              $sso_name = $conf['sso_name'];
391
              $this->ldapTestId = "testSSO._SERVER-key=$server_var_key sso_name=$sso_name path=$test_path ldapImplementation=$ldapImplementation seamlessLogin=$seamlessLogin";
392
              $ldapAuthenticationConf = $this->setSsoServerEnvironment($server_var_key, $sso_name, $ldapImplementation, $seamlessLogin);
393
              $this->drupalGet($test_path);
394
              // @FIXME: Broken test
395
              // $this->assertText(t('Member for'), 'Successful logon.', $this->ldapTestId);
396
              $this->drupalGet('user/logout');
397
            }
398
          }
399
        }
400
      }
401
    }
402
  }
403

    
404

    
405
  function testAuthenticationWhitelistTests() {
406
    require_once(drupal_get_path('module', 'ldap_authentication') . '/LdapAuthenticationConfAdmin.class.php');
407

    
408
    $sid = 'activedirectory1';
409
    $testid = 'WL3';
410
    $sids = array($sid);
411
    $this->prepTestData(
412
      'hogwarts',
413
      array($sid),
414
      'provisionToDrupal',
415
      'WL3',
416
      'drupal_role_authentication_test'
417
      );
418

    
419
    $ldap_servers = ldap_servers_get_servers($sid, 'enabled');
420
    $this->assertTrue(count($ldap_servers) == 1, ' ldap_authentication test server setup successful', $testid);
421

    
422
    // these 2 modules are configured in setup, but disabled for most authentication tests
423
    module_disable(array('ldap_authorization_drupal_role', 'ldap_authorization'));
424

    
425
    /**
426
     * LDAP_authen.WL.user1  test for user 1 being excluded from white and black list tests
427
     */
428

    
429
    $user1 = user_load(1);
430
    $password = $this->randomString(20);
431
    require_once(DRUPAL_ROOT . '/includes/password.inc');
432
    $account = array(
433
      'name' => $user1->name,
434
      'pass' => user_hash_password(trim($password)),
435
    );
436
    db_update('users')
437
      ->fields($account)
438
      ->condition('uid', 1)
439
      ->execute();
440

    
441
    $edit = array(
442
      'name' => $user1->name,
443
      'pass' => $password,
444
    );
445

    
446
    $this->drupalPost('user', $edit, t('Log in'));
447
    $this->assertText(t('Member for'), 'User 1 successfully authenticated in LDAP_authen.WL.user1', $testid);
448
    $this->drupalGet('user/logout');
449

    
450

    
451
    module_enable(array('ldap_authorization'));
452
    module_enable(array('ldap_authorization_drupal_role'));
453

    
454

    
455
    /**
456
     * prep LDAP_authen.WL.allow
457
     */
458
    $authenticationConf = new LdapAuthenticationConfAdmin();
459
    $authenticationConf->allowOnlyIfTextInDn = array('pot');
460
    $authenticationConf->save();
461
    $authenticationConf = ldap_authentication_get_valid_conf(TRUE);
462

    
463
   /**
464
    * LDAP_authen.WL.allow.match -- desirect_result: authenticate success
465
    */
466

    
467
    $this->AttemptLogonNewUser('hpotter');
468
    $this->assertText(t('Member for'), 'User able to authenticate because in white list (allowOnlyIfTextInDn).', $testid);
469

    
470
   /**
471
    *  LDAP_authen.WL.allow.miss -- desirect_result: authenticate fail
472
    */
473

    
474
    $this->AttemptLogonNewUser('ssnape');
475
    $this->assertText(t('User disallowed'), 'User unable to authenticate because not in white list (allowOnlyIfTextInDn).', $testid);
476

    
477

    
478
    /**
479
    * undo LDAP_authen.WL.allow settings
480
    */
481

    
482
    $authenticationConf = new LdapAuthenticationConfAdmin();
483
    $authenticationConf->allowOnlyIfTextInDn = array();
484
    $authenticationConf->save();
485
    $authenticationConf = ldap_authentication_get_valid_conf(TRUE);
486
    /**
487
    * prep LDAP_authen.WL.exclude
488
    */
489
    $authenticationConf = new LdapAuthenticationConfAdmin();
490
    $authenticationConf->excludeIfTextInDn = array('cn=ssnape');
491
    $authenticationConf->save();
492
    $authenticationConf = ldap_authentication_get_valid_conf(TRUE);
493

    
494
   /**
495
    * LDAP_authen.WL.exclude.match -- desirect_result: authenticate fail
496
    */
497

    
498
    $this->AttemptLogonNewUser('ssnape');
499
    $this->assertText(t('User disallowed'), 'User unable to authenticate in exclude list (excludeIfTextInDn).', $testid);
500

    
501
   /**
502
    *  LDAP_authen.WL.exclude.miss-- desirect_result: authenticate success
503
    */
504

    
505
    $this->AttemptLogonNewUser('hpotter');
506
    $this->assertText(t('Member for'), 'Able to authenticate because not in exclude list (allowOnlyIfTextInDn).', $testid);
507

    
508
    /**
509
    * undo LDAP_authen.WL.allow settings
510
    */
511

    
512
    $authenticationConf = new LdapAuthenticationConfAdmin();
513
    $authenticationConf->excludeIfTextInDn = array();
514
    $authenticationConf->save();
515
    $authenticationConf = ldap_authentication_get_valid_conf(TRUE);
516

    
517
    /**
518
    * prep LDAP_authen.WL.php
519
    */
520
    $authenticationConf = new LdapAuthenticationConfAdmin();
521
    $authenticationConf->allowTestPhp = "\n
522
      //exclude users with dumb in email address \n
523
      if (strpos(\$_ldap_user_entry['attr']['mail'][0], 'dumb') === FALSE) {\n
524
        print 1;\n
525
      }\n
526
      else {
527
        print 0;\n
528
      }
529
      ";
530

    
531
    $authenticationConf->save();
532
    $authenticationConf = ldap_authentication_get_valid_conf(TRUE);
533
   /**
534
    * LDAP_authen.WL.php.php disabled -- desired result: authenticate fail with warning the authentication disabled
535
    */
536
    module_disable(array('php'));
537
    $this->AttemptLogonNewUser('adumbledore');
538
    $this->assertText(
539
      LDAP_AUTHENTICATION_DISABLED_FOR_BAD_CONF_MSG,
540
      'With php disabled and php code in whitelist, refuse authentication. (allowTestPhp).',
541
      $testid
542
    );
543
    module_enable(array('php'));
544

    
545

    
546
   /**
547
    * LDAP_authen.WL.php.true -- desired result: authenticate success
548
    */
549
    $this->AttemptLogonNewUser('hpotter');
550
    $this->assertText(t('Member for'), 'Able to authenticate because php returned true (allowTestPhp).', $testid);
551

    
552
   /**
553
    *  LDAP_authen.WL.php.false-- desired result: authenticate fail
554
    */
555

    
556
    $this->AttemptLogonNewUser('adumbledore');
557
    $this->assertText(
558
      t('User disallowed'),
559
      'User unable to authenticate because php returned false (allowTestPhp).',
560
      $testid
561
    );
562

    
563

    
564
  /**
565
    * clear LDAP_authen.WL.php
566
    */
567
    $authenticationConf = new LdapAuthenticationConfAdmin();
568
    $authenticationConf->allowTestPhp = '';
569
    $authenticationConf->save();
570
    $authenticationConf = ldap_authentication_get_valid_conf(TRUE);
571

    
572
  /**
573
   * need to test username changes with PUID
574
   *   - given a user exists
575
   *   - change samaccountname in ldap server
576
   *   - have user logon
577
   *   - make sure old user and new user have same puid
578
   *
579
   */
580

    
581

    
582
  /***  multiple options used in whitelist **/
583

    
584

    
585

    
586
   /**
587
    * LDAP_authen.WL.allow[match].exclude[match] -- desired result: authenticate fail
588
    */
589

    
590

    
591

    
592
   /**
593
    *  LDAP_authen.WL.allow[match].exclude[miss] -- desired result: authenticate success
594
    */
595

    
596

    
597
    /**
598
    * LDAP_authen.WL.exclude[match].*-- desirect_result: authenticate fail
599
    */
600

    
601

    
602

    
603
   /**
604
    *  LDAP_authen.WL.exclude[match].php[false] -- desired result: authenticate fail
605
    */
606

    
607

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

    
613
   // these 2 modules are configured in setup, but disabled for most authentication tests
614
    module_disable(array('ldap_authorization_drupal_role', 'ldap_authorization'));
615
    $authenticationConf = new LdapAuthenticationConfAdmin();
616
    $authenticationConf->excludeIfNoAuthorizations = 1;
617
    $authenticationConf->save();
618
    $authenticationConf = ldap_authentication_get_valid_conf(TRUE);
619
    /**
620
     * LDAP_authen.WL1.excludeIfNoAuthorizations.failsafe
621
     * test for excludeIfNoAuthorizations set to true and ldap_authorization disabled
622
     * to make sure authentication fails completely
623
     */
624

    
625

    
626
    $this->AttemptLogonNewUser('hpotter');
627
    $this->assertText(
628
      LDAP_AUTHENTICATION_DISABLED_FOR_BAD_CONF_MSG,
629
      t('Authentication prohibited when excludeIfNoAuthorizations =
630
        true and LDAP Authorization disabled.
631
        LDAP_authen.WL1.excludeIfNoAuthorizations.failsafe'),
632
      $testid
633
    );
634

    
635
    module_enable(array('ldap_authorization'), TRUE);
636
    module_enable(array('ldap_authorization_drupal_role'), TRUE);
637
    $consumer = ldap_authorization_get_consumers('drupal_role', TRUE, TRUE); // clear static cache
638

    
639
    $this->AttemptLogonNewUser('hpotter');
640
    $this->assertText(
641
      t('Member for'),
642
      'User able to authenticate because of excludeIfNoAuthorizations setting.',
643
      $testid
644
    );
645

    
646
    /**
647
     * LDAP_authen.WL1.excludeIfNoAuthorizations.hasNoAuthorizations
648
     * test for excludeIfNoAuthorizations set to true and No consumer
649
     * granted authorizations
650
     */
651

    
652
    $this->AttemptLogonNewUser('ssnape');
653
    $this->assertText(
654
      t('User disallowed'),
655
      'User unable to authenticate because of excludeIfNoAuthorizations setting.',
656
      $testid
657
    );
658

    
659
    $authenticationConf = new LdapAuthenticationConfAdmin();
660
    $authenticationConf->excludeIfNoAuthorizations = 0;
661
    $authenticationConf->save();
662
    $authenticationConf = ldap_authentication_get_valid_conf(TRUE);
663
    module_disable(array('ldap_authorization_drupal_role', 'ldap_authorization'));
664

    
665

    
666
  }
667

    
668
  /**
669
   * make sure user admin interface works.
670
   */
671
  function testUI() {
672

    
673
    // just to give warning if setup doesn't succeed.  may want to take these out at some point.
674
    $setup_success = (
675
        module_exists('ldap_user') &&
676
        module_exists('ldap_servers') &&
677
        module_exists('ldap_authentication') &&
678
        (variable_get('ldap_simpletest', 2) > 0)
679
      );
680
    $this->assertTrue($setup_success, ' ldap_authentication UI setup successful',  $this->testId('user interface tests'));
681

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

    
686
    $this->privileged_user = $this->drupalCreateUser(array(
687
      'administer site configuration',
688
      ));
689

    
690
    $this->drupalLogin($this->privileged_user);
691

    
692
    $ldap_authentication_conf_pre = ldap_authentication_get_valid_conf();
693

    
694
    $this->drupalGet('admin/config/people/ldap/authentication');
695

    
696
    $form_tests = array(
697
      'authenticationMode' => array(
698
        'property' => 'authenticationMode',
699
        'values' => array(
700
          LDAP_AUTHENTICATION_MIXED,
701
          LDAP_AUTHENTICATION_EXCLUSIVE
702
        ),
703
        'required' => TRUE,
704
      ),
705
      'authenticationServers[' . $sid . ']' => array(
706
        'property' => 'enabledAuthenticationServers',
707
        'values' => array(
708
          TRUE,
709
          TRUE,
710
        ),
711
        'desired_result' => array(
712
          array($sid),
713
          array($sid),
714
        )
715
      ),
716
      'loginUIUsernameTxt' =>
717
        array(
718
          'property' => 'loginUIUsernameTxt',
719
          'values' => array(
720
            '',
721
            'Hogwarts UserID',
722
          ),
723
        ),
724
      'loginUIPasswordTxt' =>
725
        array(
726
          'property' => 'loginUIPasswordTxt',
727
          'values' => array(
728
            '',
729
            'Hogwarts UserID Password',
730
          ),
731
        ),
732
      'ldapUserHelpLinkUrl' =>
733
        array(
734
          'property' => 'ldapUserHelpLinkUrl',
735
          'values' => array(
736
            '',
737
            'http://passwords.hogwarts.edu',
738
          ),
739
        ),
740
      'ldapUserHelpLinkText' =>
741
        array(
742
          'property' => 'ldapUserHelpLinkText',
743
          'values' => array(
744
            'Hogwarts Password Management Page',
745
            'Hogwarts Password Management Page'
746
          ),
747
        ),
748
      'allowOnlyIfTextInDn' => array(
749
        'property' => 'allowOnlyIfTextInDn',
750
        'values' => array(
751
          'witch\nwarlord\nwisecracker',
752
          "witch\nwarlord\nwisecracker"
753
        ),
754
        'desired_result' => array(
755
          array('witch', 'warlord', 'wisecracker'),
756
          array('witch', 'warlord', 'wisecracker'),
757
        )
758
      ),
759
      'excludeIfTextInDn' => array(
760
        'property' => 'excludeIfTextInDn',
761
        'values' => array(
762
          "muggle\nmuddle\nmummy",
763
          "muggle\nmuddle\nmummy"
764
        ),
765
        'desired_result' => array(
766
          array('muggle', 'muddle', 'mummy'),
767
          array('muggle', 'muddle', 'mummy'),
768
        )
769
      ),
770
      'excludeIfNoAuthorizations' => array(
771
        'property' => 'excludeIfNoAuthorizations',
772
        'values' => array(
773
          array(TRUE),
774
          array(TRUE),
775
        ),
776
      ),
777
      'emailOption' => array(
778
        'property' => 'emailOption',
779
        'values' => array(
780
          LDAP_AUTHENTICATION_EMAIL_FIELD_REMOVE,
781
          LDAP_AUTHENTICATION_EMAIL_FIELD_DISABLE,
782
        ),
783
        'required' => TRUE,
784
      ),
785
      'emailUpdate' => array(
786
        'property' => 'emailUpdate',
787
        'values' => array(
788
          LDAP_AUTHENTICATION_EMAIL_UPDATE_ON_LDAP_CHANGE_ENABLE,
789
          LDAP_AUTHENTICATION_EMAIL_UPDATE_ON_LDAP_CHANGE_DISABLE,
790
        ),
791
        'required' => TRUE,
792
      ),
793
      'allowTestPhp' => array(
794
        'property' => 'allowTestPhp',
795
        'values' => array(
796
          'pretend php',
797
          'pretend php',
798
        ),
799
      ),
800
    );
801

    
802
    module_enable(array('php'));
803
    foreach (array(0, 1) as $i) {
804
      $edit = array();
805
      foreach ($form_tests as $field_name => $conf) {
806
        $value = $conf['values'][$i];
807
        $property = isset($conf['property']) ? $conf['property'] : $field_name;
808
        $edit[$field_name] = $value;
809
      }
810
      $this->drupalPost('admin/config/people/ldap/authentication', $edit, t('Save'));
811
      $ldap_authentication_conf_post = ldap_authentication_get_valid_conf(TRUE);
812

    
813
      foreach ($form_tests as $field_name => $conf) {
814
        $property = isset($conf['property']) ? $conf['property'] : $field_name;
815
        $desired = isset($conf['desired_result']) ? isset($conf['desired_result'][$i]) : $conf['values'][$i];
816

    
817
        if (is_array($desired)) {
818
          $success = count($desired) == count($ldap_authentication_conf_post->{$property});
819
        }
820
        else {
821
          $success = ($ldap_authentication_conf_post->{$property} == $desired);
822
        }
823
        $this->assertTrue(
824
          $success,
825
          $property . ' ' . t('field set correctly'),
826
          $this->testId('ldap authentication user interface tests')
827
        );
828
        if (!$success) {
829
          debug("fail $i $property");
830
          debug("desired:"); debug($desired);
831
          debug("actual:");  debug($ldap_authentication_conf_post->{$property});
832
        }
833
      }
834
    }
835
  }
836

    
837
}