From dc634f9b666e6e372f4bb200ef1e19750f7686c7 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              |   97 +++++++++++++++++++++++++++++++++++++++----------
 tests/cas_test.module |    8 ++++-
 3 files changed, 90 insertions(+), 20 deletions(-)

diff --git a/cas.module b/cas.module
index 38d7fbf..70066ad 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 a8a45d5..598650b 100644
--- a/cas.test
+++ b/cas.test
@@ -74,6 +74,12 @@ class CasTestHelper extends DrupalWebTestCase {
       // Extract the files.
       $archiver = new Archive_Tar($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
+        $result = drupal_http_request('http://drupal.org/files/issues/CAS-1.1.3-CAS-client.txt');
+        file_put_contents($cas_library_dir . '/CAS/client.php', $result->data);
+      }
     }
 
     if (function_exists('sem_get')) {
@@ -142,6 +148,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() {
@@ -438,17 +455,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() {
@@ -464,13 +470,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.
@@ -478,27 +486,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->drupalGet($destination);
     $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
@@ -506,14 +518,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();
   }
 
@@ -573,3 +587,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 595c0c7..e48efb0 100644
--- a/tests/cas_test.module
+++ b/tests/cas_test.module
@@ -100,9 +100,15 @@ function cas_test_login() {
     // Generate a ticket and redirect to the service URL with the login ticket.
     $ticket = _cas_test_ticket_generate($service, $cas_name);
     // Force redirection.
-    unset($_GET['destination']);
+    unset($_REQUEST['destination']);
     drupal_goto($service, 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($_REQUEST['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

