From 8523d4e47a213f7aabd2c7a66c83cf318a533f91 Mon Sep 17 00:00:00 2001
From: Bradley M. Froehle <brad.froehle@gmail.com>
Date: Thu, 7 Apr 2011 13:34:12 -0700
Subject: [PATCH] Issue #1120034 - Add test routines for CAS Gateway feature.

---
 cas.module            |    5 +++
 cas.test              |   96 +++++++++++++++++++++++++++++++++++++++----------
 tests/cas_test.module |    6 +++
 3 files changed, 88 insertions(+), 19 deletions(-)

diff --git a/cas.module b/cas.module
index abc3215..cdc9e69 100644
--- a/cas.module
+++ b/cas.module
@@ -23,6 +23,11 @@ define('CAS_EXCLUDE', 'services/*');
  * Traps a page load to see if authentication is required.
  */
 function cas_init() {
+  if (module_exists('cas_test') && arg(0) == 'cas_test') {
+    // We are destined for a page handled by the cas_test module, so do not
+    // do any processing here. Necessary for CAS gateway tests.
+    return;
+  }
   _cas_single_sign_out_check();
   cas_login_check();
 }
diff --git a/cas.test b/cas.test
index 8a34f97..bad87df 100644
--- a/cas.test
+++ b/cas.test
@@ -67,6 +67,11 @@ class CasTestHelper extends DrupalWebTestCase {
       // Extract the files.
       $archiver = archiver_get_archiver($local_archive);
       $archiver->extract($simpletest_cache);
+      if ($version == '1.1.3') {
+        // Work around issue https://issues.jasig.org/browse/PHPCAS-105.
+        // @see http://drupal.org/node/1120034
+        system_retrieve_file('http://drupal.org/files/issues/CAS-1.1.3-CAS-client.txt', $cas_library_dir . '/CAS/client.php', FALSE, FILE_EXISTS_REPLACE);
+      }
     }
     if (function_exists('sem_get')) {
       sem_release($semaphore);
@@ -134,6 +139,17 @@ class CasTestHelper extends DrupalWebTestCase {
   }
 
   /**
+   * Assert that the account been logged in.
+   */
+  function assertLoggedIn($account) {
+    $pass = $this->assertLink(t('Log out'), 0, t('CAS user %cas_name successfully logged in.', array('%cas_name' => $account->cas_name)), t('User login'));
+    if ($pass) {
+      $this->loggedInUser = $account;
+    }
+  }
+
+
+  /**
    * Assert that the user has been logged out.
    */
   function assertLoggedOut() {
@@ -437,17 +453,6 @@ class CasLoginRedirectionTestCase extends CasTestHelper {
   }
 
   /**
-   * Assert that the account just logged in and is at the URL.
-   */
-  function assertLoggedInWithUrl($account, $url) {
-    $pass = $this->assertLink(t('Log out'), 0, t('CAS user %cas_name successfully logged in.', array('%cas_name' => $account->cas_name)), t('User login'));
-    if ($pass) {
-      $this->loggedInUser = $account;
-    }
-    $this->assertUrl($url);
-  }
-
-  /**
    * Verify login redirection for an existing user.
    */
   function testExistingUserLoginRedirection() {
@@ -463,13 +468,15 @@ class CasLoginRedirectionTestCase extends CasTestHelper {
 
     // Test going to 'cas'
     $this->drupalGet('cas');
-    $this->assertLoggedInWithUrl($account, '');
+    $this->assertLoggedIn($account);
+    $this->assertUrl('');
     $this->drupalLogout();
 
     // Test going to 'cas?destination=node/$node1->nid'
     $destination = "node/$node1->nid";
     $this->drupalGet('cas', array('query' => array('destination' => $destination)));
-    $this->assertLoggedInWithUrl($account, $destination);
+    $this->assertLoggedIn($account);
+    $this->assertUrl($destination);
     $this->drupalLogout();
 
     // Use the login block on $node2.
@@ -477,27 +484,31 @@ class CasLoginRedirectionTestCase extends CasTestHelper {
     variable_set('cas_login_form', CAS_ADD_LINK);
     $edit = array('cas_identifier' => TRUE);
     $this->drupalPost($destination, $edit, t('Log in'));
-    $this->assertLoggedInWithUrl($account, $destination);
+    $this->assertLoggedIn($account);
+    $this->assertUrl($destination);
     $this->drupalLogout();
 
     // Use the regular login page, without a destination.
     $edit = array('cas_identifier' => TRUE);
     $this->drupalPost('user/login', $edit, t('Log in'));
-    $this->assertLoggedInWithUrl($account, 'user');
+    $this->assertLoggedIn($account);
+    $this->assertUrl('user');
     $this->drupalLogout();
 
     // Use the regular login page, with a destination.
     $destination = "node/$node3->nid";
     $edit = array('cas_identifier' => TRUE);
     $this->drupalPost('user/login', $edit, t('Log in'), array('query' => array('destination' => $destination)));
-    $this->assertLoggedInWithUrl($account, $destination);
+    $this->assertLoggedIn($account);
+    $this->assertUrl($destination);
     $this->drupalLogout();
 
     // Click on a 'cas' link on node 4.
     $destination = "node/$node4->nid";
     $this->url = url($destination, array('absolute' => TRUE));
     $this->drupalGetWithHttpReferrer('cas');
-    $this->assertLoggedInWithUrl($account, $destination);
+    $this->assertLoggedIn($account);
+    $this->assertUrl($destination);
     $this->drupalLogout();
 
     // Clicking on a 'cas' link from an external host does not redirect back
@@ -505,14 +516,16 @@ class CasLoginRedirectionTestCase extends CasTestHelper {
     // $destination = '';
     // $this->url = 'http://example.com/node/3';
     // $this->drupalGetWithHttpReferrer('cas');
-    // $this->assertLoggedInWithUrl($account, $destination);
+    // $this->assertLoggedIn($account);
+    // $this->assertUrl($destination);
     // $this->drupalLogout();
 
     // The destination parameter overrides the HTTP Referer header.
     $destination = "node/$node2->nid";
     $this->url = url("node/$node3->nid", array('absolute' => TRUE));
     $this->drupalGetWithHttpReferrer('cas', array('query' => array('destination' => $destination)));
-    $this->assertLoggedInWithUrl($account, $destination);
+    $this->assertLoggedIn($account);
+    $this->assertUrl($destination);
     $this->drupalLogout();
   }
 
@@ -572,3 +585,48 @@ class CasSingleSignOutTestCase extends CasTestHelper {
     //   methods coming from cas_pages).
   }
 }
+
+/**
+ * Test case for CAS gateway feature.
+ */
+class CasGatewayTestCase extends CasTestHelper {
+
+  public static function getInfo() {
+    return array(
+      'name' => 'CAS Gateway',
+      'description' => 'Test CAS Gateway ("Check to see if user is already logged in") feature.',
+      'group' => 'Central Authentication Service',
+    );
+  }
+
+  function setUp() {
+    parent::setUp();
+    variable_set('cas_check_first', TRUE);
+  }
+
+  /**
+   * Test the CAS Gateway functionality of the user is not logged in.
+   */
+  function testCasGatewayLoggedOut() {
+    $this->drupalGet('');
+    $this->assertEqual($this->redirect_count, 2, 'Polled CAS server on first request.');
+    $this->drupalGet('');
+    $this->assertEqual($this->redirect_count, 0, 'Did not poll CAS server on second request.');
+    $this->drupalGet('node');
+    $this->assertEqual($this->redirect_count, 0, 'Did not poll CAS server on third request.');
+  }
+
+  /**
+   * Test the CAS Gateway functionality of the user is logged in.
+   */
+  function testCasGatewayLoggedIn() {
+    // Create a user.
+    variable_set('cas_debugfile', '/tmp/casdebug');
+    $cas_name = $this->randomName();
+    $account = $this->casCreateUser($cas_name);
+    variable_set('cas_test_cas_name', $cas_name);
+
+    $this->drupalGet('node');
+    $this->assertLoggedIn($account);
+  }
+}
diff --git a/tests/cas_test.module b/tests/cas_test.module
index 9271ce1..127f921 100644
--- a/tests/cas_test.module
+++ b/tests/cas_test.module
@@ -104,6 +104,12 @@ function cas_test_login() {
     unset($_GET['destination']);
     drupal_goto($service, array('query' => array('ticket' => $ticket)));
   }
+  elseif (!isset($_GET['gateway']) && $_GET['gateway'] == 'true') {
+    // We were not able to log in the user, so redirect to the service URL.
+    // Force redirection.
+    unset($_GET['destination']);
+    drupal_goto($service);
+  }
   else {
     // No CAS name was provided, print an error message.
     print "Warning: No CAS name provided.\n";
-- 
1.7.4.1

