Projet

Général

Profil

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

root / drupal7 / sites / all / modules / ldap / ldap_authentication / tests / ldap_authentication.test @ 7547bb19

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
    //debug($this->testFunctions);
420
    $ldap_servers = ldap_servers_get_servers($sid, 'enabled');
421
    $this->assertTrue(count($ldap_servers) == 1, ' ldap_authentication test server setup successful', $testid);
422

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

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

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

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

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

    
451

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

    
455

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

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

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

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

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

    
478

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

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

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

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

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

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

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

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

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

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

    
546

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

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

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

    
564

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

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

    
582

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

    
585

    
586

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

    
591

    
592

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

    
597

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

    
602

    
603

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

    
608

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

    
614
   // these 2 modules are configured in setup, but disabled for most authentication tests
615
    module_disable(array('ldap_authorization_drupal_role', 'ldap_authorization'));
616
    $authenticationConf = new LdapAuthenticationConfAdmin();
617
    $authenticationConf->excludeIfNoAuthorizations = 1;
618
    $authenticationConf->save();
619
    $authenticationConf = ldap_authentication_get_valid_conf(TRUE);
620
    /**
621
     * LDAP_authen.WL1.excludeIfNoAuthorizations.failsafe
622
     * test for excludeIfNoAuthorizations set to true and ldap_authorization disabled
623
     * to make sure authentication fails completely
624
     */
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(array('ldap_authorization'), TRUE);
637
    module_enable(array('ldap_authorization_drupal_role'), TRUE);
638
    $consumer = ldap_authorization_get_consumers('drupal_role', TRUE, TRUE); // clear static cache
639

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

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

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

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

    
666

    
667
  }
668

    
669
  /**
670
   * make sure user admin interface works.
671
   */
672
  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 = array('activedirectory1');
685
    $this->prepTestData(LDAP_TEST_LDAP_NAME, $sids, 'provisionToDrupal', 'default');
686

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

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

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

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

    
840
}