View source
<?php
namespace Drupal\Tests\cas\Unit\Service;
use Drupal\Tests\UnitTestCase;
use Drupal\cas\Service\CasProxyHelper;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
class CasProxyHelperTest extends UnitTestCase {
protected $session;
protected $casHelper;
protected $database;
protected function setUp() : void {
parent::setUp();
$storage = $this
->getMockBuilder('\\Symfony\\Component\\HttpFoundation\\Session\\Storage\\MockArraySessionStorage')
->setMethods(NULL)
->getMock();
$this->session = $this
->getMockBuilder('\\Symfony\\Component\\HttpFoundation\\Session\\Session')
->setConstructorArgs([
$storage,
])
->setMethods(NULL)
->getMock();
$this->session
->start();
$this->casHelper = $this
->getMockBuilder('\\Drupal\\cas\\Service\\CasHelper')
->disableOriginalConstructor()
->getMock();
$this->database = $this
->getMockBuilder('\\Drupal\\Core\\Database\\Connection')
->disableOriginalConstructor()
->getMock();
}
public function testProxyAuthenticate($target_service, $cookie_domain, $already_proxied) {
$this->session
->set('cas_pgt', $this
->randomMachineName(24));
$cookie_value = $this
->randomMachineName(24);
if ($already_proxied) {
$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']);
}
}
public function proxyAuthenticateDataProvider() {
return [
[
'https://example.com',
'example.com',
FALSE,
],
[
'https://example.com',
'example.com',
TRUE,
],
];
}
public function testProxyAuthenticateException($is_proxy, $pgt_set, $target_service, $response, $client_exception, $exception_type, $exception_message) {
if ($pgt_set) {
$this->session
->set('cas_pgt', $this
->randomMachineName(24));
}
$cookie_value = $this
->randomMachineName(24);
$configFactory = $this
->getConfigFactoryStub([
'cas.settings' => [
'server.hostname' => 'example-server.com',
'server.port' => 443,
'server.path' => '/cas',
'proxy.initialize' => $is_proxy,
],
]);
if ($client_exception == 'server') {
$code = 404;
}
else {
$code = 200;
}
if ($client_exception == 'client') {
$secondResponse = new Response(404);
}
else {
$secondResponse = new Response(200, [
'Content-type' => 'text/html',
'Set-Cookie' => 'SESSION=' . $cookie_value,
]);
}
$mock = new MockHandler([
new Response($code, [], $response),
$secondResponse,
]);
$handler = HandlerStack::create($mock);
$httpClient = new Client([
'handler' => $handler,
]);
$casProxyHelper = new CasProxyHelper($httpClient, $this->casHelper, $this->session, $configFactory, $this->database);
$this
->expectException($exception_type, $exception_message);
$casProxyHelper
->proxyAuthenticate($target_service);
}
public function proxyAuthenticateExceptionDataProvider() {
$target_service = 'https://example.com';
$exception_type = '\\Drupal\\cas\\Exception\\CasProxyException';
$params[] = [
FALSE,
TRUE,
$target_service,
'',
FALSE,
$exception_type,
'Session state not sufficient for proxying.',
];
$params[] = [
TRUE,
FALSE,
$target_service,
'',
FALSE,
$exception_type,
'Session state not sufficient for proxying.',
];
$proxy_ticket = $this
->randomMachineName(24);
$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>";
$params[] = [
TRUE,
TRUE,
$target_service,
$response,
'client',
$exception_type,
'',
];
$proxy_ticket = $this
->randomMachineName(24);
$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>";
$params[] = [
TRUE,
TRUE,
$target_service,
$response,
'server',
$exception_type,
'',
];
$response = "<> </> </ <..";
$params[] = [
TRUE,
TRUE,
$target_service,
$response,
FALSE,
$exception_type,
'CAS Server returned non-XML response.',
];
$response = "<cas:serviceResponse xmlns:cas='http://example.com/cas'>\n <cas:proxyFailure code=\"INVALID_REQUEST\">\n 'pgt' and 'targetService' parameters are both required\n </cas:proxyFailure>\n </cas:serviceResponse>";
$params[] = [
TRUE,
TRUE,
$target_service,
$response,
FALSE,
$exception_type,
'CAS Server rejected proxy request.',
];
$response = "<cas:serviceResponse xmlns:cas='http://example.com/cas'>\n <cas:proxy code=\"INVALID_REQUEST\">\n </cas:proxy>\n </cas:serviceResponse>";
$params[] = [
TRUE,
TRUE,
$target_service,
$response,
FALSE,
$exception_type,
'CAS Server returned malformed response.',
];
$response = "<cas:serviceResponse xmlns:cas='http://example.com/cas'>\n <cas:proxySuccess>\n </cas:proxySuccess>\n </cas:serviceResponse>";
$params[] = [
TRUE,
TRUE,
$target_service,
$response,
FALSE,
$exception_type,
'CAS Server provided invalid or malformed ticket.',
];
return $params;
}
}