Skip to content

Commit

Permalink
tests: Restore tests and update them according to latest changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Apr 15, 2022
1 parent 4547417 commit 6e6462f
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 76 deletions.
9 changes: 8 additions & 1 deletion spec/EcPhp/CasLib/CasSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,14 @@ public function it_can_authenticate_a_request(ClientInterface $client, CacheItem

$this
->authenticate($request)
->shouldBeArray();
->shouldBeEqualTo([
'serviceResponse' => [
'authenticationSuccess' => [
'user' => 'username',
'proxyGrantingTicket' => 'pgtId',
],
],
]);

$request = new ServerRequest('GET', UtilsUri::withParams(new Uri('http://from'), ['ticket' => 'ST-ticket-pgt-pgtiou-not-found']));

Expand Down
22 changes: 17 additions & 5 deletions spec/EcPhp/CasLib/Response/Type/ProxyFailureSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

declare(strict_types=1);

namespace spec\EcPhp\CasLib\Introspection;
namespace spec\EcPhp\CasLib\Response\Type;

use Nyholm\Psr7\Response;
use PhpSpec\ObjectBehavior;
Expand All @@ -18,7 +18,19 @@ class ProxyFailureSpec extends ObjectBehavior
{
public function it_can_detect_a_proxy_failure_response()
{
$response = (new Response(200));
$body = [
'serviceResponse' => [
'proxyFailure' => "unrecognized pgt: 'PGT-123'",
],
];

$response = new Response(
200,
[
'Content-Type' => 'application/json',
],
json_encode($body)
);

$parsed = [
'serviceResponse' => [
Expand All @@ -27,14 +39,14 @@ public function it_can_detect_a_proxy_failure_response()
];

$this
->beConstructedWith($parsed, 'XML', $response);
->beConstructedWith($response);

$this
->getMessage()
->shouldReturn("unrecognized pgt: 'PGT-123'");

$this
->getResponse()
->shouldReturn($response);
->toArray()
->shouldReturn($parsed);
}
}
22 changes: 8 additions & 14 deletions spec/EcPhp/CasLib/Response/Type/ProxySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

declare(strict_types=1);

namespace spec\EcPhp\CasLib\Introspection;
namespace spec\EcPhp\CasLib\Response\Type;

use Nyholm\Psr7\Factory\Psr17Factory;
use Nyholm\Psr7\Response;
Expand All @@ -23,11 +23,9 @@ public function it_can_detect_a_proxy_response()

$body = <<< 'EOF'
<?xml version="1.0" encoding="utf-8"?>
<cas:serviceResponse xmlns:cas="https://ecas.ec.europa.eu/cas/schemas"
server="ECAS MOCKUP version 4.6.0.20924 - 09/02/2016 - 14:37"
date="2019-10-18T12:17:53.069+02:00" version="4.5">
<cas:serviceResponse xmlns:cas="https://ecas.ec.europa.eu/cas/schemas">
<cas:proxySuccess>
<cas:proxyTicket>PT-214-A3OoEPNr4Q9kNNuYzmfN8azU31aDUsuW8nk380k7wDExT5PFJpxR1TrNI3q3VGzyDdi0DpZ1LKb8IhPKZKQvavW-8hnfexYjmLCx7qWNsLib1W-DCzzoLVTosAUFzP3XDn5dNzoNtxIXV9KSztF9fYhwHvU0</cas:proxyTicket>
<cas:proxyTicket>PGT-TICKET</cas:proxyTicket>
</cas:proxySuccess>
</cas:serviceResponse>
EOF;
Expand All @@ -39,24 +37,20 @@ public function it_can_detect_a_proxy_response()
$parsed = [
'serviceResponse' => [
'proxySuccess' => [
'proxyTicket' => 'PT-214-A3OoEPNr4Q9kNNuYzmfN8azU31aDUsuW8nk380k7wDExT5PFJpxR1TrNI3q3VGzyDdi0DpZ1LKb8IhPKZKQvavW-8hnfexYjmLCx7qWNsLib1W-DCzzoLVTosAUFzP3XDn5dNzoNtxIXV9KSztF9fYhwHvU0',
'proxyTicket' => 'PGT-TICKET',
],
],
];

$this
->beConstructedWith($parsed, 'XML', $response);
->beConstructedWith($response);

$this
->getProxyTicket()
->shouldReturn('PT-214-A3OoEPNr4Q9kNNuYzmfN8azU31aDUsuW8nk380k7wDExT5PFJpxR1TrNI3q3VGzyDdi0DpZ1LKb8IhPKZKQvavW-8hnfexYjmLCx7qWNsLib1W-DCzzoLVTosAUFzP3XDn5dNzoNtxIXV9KSztF9fYhwHvU0');
->shouldReturn('PGT-TICKET');

$this
->getFormat()
->shouldReturn('XML');

$this
->getResponse()
->shouldReturn($response);
->toArray()
->shouldReturn($parsed);
}
}
89 changes: 33 additions & 56 deletions spec/EcPhp/CasLib/Response/Type/ServiceValidateSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

declare(strict_types=1);

namespace spec\EcPhp\CasLib\Introspection;
namespace spec\EcPhp\CasLib\Response\Type;

use Nyholm\Psr7\Factory\Psr17Factory;
use Nyholm\Psr7\Response;
Expand All @@ -21,55 +21,45 @@ public function it_can_detect_a_proxy_service_validate_response()
{
$psr17Factory = new Psr17Factory();

$body = <<< 'EOF'
Useless stuff here.
EOF;

$response = (new Response(200))
->withHeader('Content-Type', 'application/xml')
->withBody($psr17Factory->createStream($body));

$credentials = [
'user' => 'user',
'proxyGrantingTicket' => 'proxyGrantingTicket',
'proxies' => [
'proxy' => [
'http://proxy1',
'http://proxy2',
],
],
'extendedAttributes' => [
'extendedAttribute' => [
'attributeValue' => [
0 => 'rex',
1 => 'snoopy',
$bodyArray = [
'serviceResponse' => [
'authenticationSuccess' => [
'user' => 'user',
'proxyGrantingTicket' => 'proxyGrantingTicket',
'proxies' => [
'proxy' => [
'http://proxy1',
'http://proxy2',
],
],
'@attributes' => [
'name' => 'http://stork.eu/motherInLawDogName',
'extendedAttributes' => [
'extendedAttribute' => [
'attributeValue' => [
0 => 'rex',
1 => 'snoopy',
],
'@attributes' => [
'name' => 'http://stork.eu/motherInLawDogName',
],
],
],
],
],
];

$parsed = [
'serviceResponse' => [
'authenticationSuccess' => $credentials,
],
];
$response = (new Response(200))
->withHeader('Content-Type', 'application/json')
->withBody($psr17Factory->createStream(json_encode($bodyArray)));

$this
->beConstructedWith($parsed, 'XML', $response);
->beConstructedWith($response);

$this
->getCredentials()
->shouldReturn(
$credentials
$bodyArray['serviceResponse']['authenticationSuccess']
);

$this
->getFormat()
->shouldReturn('XML');

$this
->getProxies()
->shouldReturn([
Expand All @@ -80,12 +70,8 @@ public function it_can_detect_a_proxy_service_validate_response()
]);

$this
->getResponse()
->shouldReturn($response);

$this
->getParsedResponse()
->shouldReturn($parsed);
->toArray()
->shouldReturn($bodyArray);
}

public function it_can_detect_a_service_validate_response()
Expand All @@ -105,11 +91,6 @@ public function it_can_detect_a_service_validate_response()
->withHeader('Content-Type', 'application/xml')
->withBody($psr17Factory->createStream($body));

$credentials = [
'user' => 'user',
'proxyGrantingTicket' => 'proxyGrantingTicket',
];

$parsed = [
'serviceResponse' => [
'authenticationSuccess' => [
Expand All @@ -120,22 +101,18 @@ public function it_can_detect_a_service_validate_response()
];

$this
->beConstructedWith($parsed, 'XML', $response);

$this
->getCredentials()
->shouldReturn($credentials);
->beConstructedWith($response);

$this
->getFormat()
->shouldReturn('XML');
->toArray()
->shouldReturn($parsed);

$this
->getProxies()
->shouldReturn([]);

$this
->getResponse()
->shouldReturn($response);
->toArray()
->shouldReturn($parsed);
}
}
100 changes: 100 additions & 0 deletions spec/EcPhp/CasLib/Utils/ResponseSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

/**
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* @see https://github.com/ecphp
*/

declare(strict_types=1);

namespace spec\EcPhp\CasLib\Utils;

use EcPhp\CasLib\Exception\CasExceptionInterface;
use Nyholm\Psr7\Response;
use PhpSpec\ObjectBehavior;

class ResponseSpec extends ObjectBehavior
{
public function it_convert_a_json_response()
{
$body = <<< 'EOF'
{
"serviceResponse": {
"authenticationSuccess": {
"user": "username"
}
}
}
EOF;

$response = new Response(
200,
[
'Content-Type' => 'application/json',
],
$body
);

$this
->toArray($response)
->shouldReturn([
'serviceResponse' => [
'authenticationSuccess' => [
'user' => 'username',
],
],
]);
}

public function it_convert_a_xml_response()
{
$body = <<< 'EOF'
<cas:serviceResponse xmlns:cas="http://www.yale.edu/tp/cas">
<cas:authenticationSuccess>
<cas:user>username</cas:user>
</cas:authenticationSuccess>
</cas:serviceResponse>
EOF;

$response = new Response(
200,
[
'Content-Type' => 'application/xml',
],
$body
);

$this
->toArray($response)
->shouldReturn([
'serviceResponse' => [
'authenticationSuccess' => [
'user' => 'username',
],
],
]);
}

public function it_throws_when_content_type_header_is_missing()
{
$body = <<< 'EOF'
<cas:serviceResponse xmlns:cas="http://www.yale.edu/tp/cas">
<cas:authenticationSuccess>
<cas:user>username</cas:user>
</cas:authenticationSuccess>
</cas:serviceResponse>
EOF;

$response = new Response(
200,
[],
$body
);

$this
->shouldThrow(CasExceptionInterface::class)
->during('toArray', [$response]);
}
}

0 comments on commit 6e6462f

Please sign in to comment.