You are here

public function CasProxyHelperTest::testProxyAuthenticate in CAS 2.x

Same name and namespace in other branches
  1. 8 tests/src/Unit/Service/CasProxyHelperTest.php \Drupal\Tests\cas\Unit\Service\CasProxyHelperTest::testProxyAuthenticate()

Test proxy authentication to a service.

@covers ::proxyAuthenticate @covers ::getServerProxyURL @covers ::parseProxyTicket

@dataProvider proxyAuthenticateDataProvider

File

tests/src/Unit/Service/CasProxyHelperTest.php, line 76

Class

CasProxyHelperTest
CasHelper unit tests.

Namespace

Drupal\Tests\cas\Unit\Service

Code

public function testProxyAuthenticate($target_service, $cookie_domain, $already_proxied) {

  // Set up the fake pgt in the session.
  $this->session
    ->set('cas_pgt', $this
    ->randomMachineName(24));

  // Set up properties so the http client callback knows about them.
  $cookie_value = $this
    ->randomMachineName(24);
  if ($already_proxied) {

    // Set up the fake session data.
    $session_cas_proxy_helper[$target_service][] = [
      'Name' => 'SESSION',
      'Value' => $cookie_value,
      'Domain' => $cookie_domain,
    ];
    $this->session
      ->set('cas_proxy_helper', $session_cas_proxy_helper);
    $httpClient = new Client();
    $configFactory = $this
      ->getConfigFactoryStub([
      'cas.settings' => [
        'server.hostname' => 'example-server.com',
        'server.port' => 443,
        'server.path' => '/cas',
      ],
    ]);
    $casProxyHelper = new CasProxyHelper($httpClient, $this->casHelper, $this->session, $configFactory, $this->database);
    $jar = $casProxyHelper
      ->proxyAuthenticate($target_service);
    $cookie_array = $jar
      ->toArray();
    $this
      ->assertEquals('SESSION', $cookie_array[0]['Name']);
    $this
      ->assertEquals($cookie_value, $cookie_array[0]['Value']);
    $this
      ->assertEquals($cookie_domain, $cookie_array[0]['Domain']);
  }
  else {
    $proxy_ticket = $this
      ->randomMachineName(24);
    $xml_response = "<cas:serviceResponse xmlns:cas='http://example.com/cas'>\n           <cas:proxySuccess>\n             <cas:proxyTicket>PT-{$proxy_ticket}</cas:proxyTicket>\n            </cas:proxySuccess>\n         </cas:serviceResponse>";
    $mock = new MockHandler([
      new Response(200, [], $xml_response),
      new Response(200, [
        'Content-type' => 'text/html',
        'Set-Cookie' => 'SESSION=' . $cookie_value,
      ]),
    ]);
    $handler = HandlerStack::create($mock);
    $httpClient = new Client([
      'handler' => $handler,
    ]);
    $configFactory = $this
      ->getConfigFactoryStub([
      'cas.settings' => [
        'server.hostname' => 'example-server.com',
        'server.port' => 443,
        'server.path' => '/cas',
        'proxy.initialize' => TRUE,
      ],
    ]);
    $casProxyHelper = new CasProxyHelper($httpClient, $this->casHelper, $this->session, $configFactory, $this->database);
    $jar = $casProxyHelper
      ->proxyAuthenticate($target_service);
    $this
      ->assertEquals('SESSION', $this->session
      ->get('cas_proxy_helper')[$target_service][0]['Name']);
    $this
      ->assertEquals($cookie_value, $this->session
      ->get('cas_proxy_helper')[$target_service][0]['Value']);
    $this
      ->assertEquals($cookie_domain, $this->session
      ->get('cas_proxy_helper')[$target_service][0]['Domain']);
    $cookie_array = $jar
      ->toArray();
    $this
      ->assertEquals('SESSION', $cookie_array[0]['Name']);
    $this
      ->assertEquals($cookie_value, $cookie_array[0]['Value']);
    $this
      ->assertEquals($cookie_domain, $cookie_array[0]['Domain']);
  }
}