Skip to content

Commit

Permalink
tests: rewrite tests based on upstream ecphp/cas-lib developments.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed May 16, 2023
1 parent 4fbc54b commit 12fa217
Show file tree
Hide file tree
Showing 8 changed files with 305 additions and 257 deletions.
2 changes: 1 addition & 1 deletion spec/EcPhp/CasBundle/Cas.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public static function getTestProperties(): CasProperties

public static function getTestPropertiesWithPgtUrl(): CasProperties
{
$properties = self::getTestProperties()->all();
$properties = self::getTestProperties()->jsonSerialize();

$properties['protocol']['serviceValidate']['default_parameters']['pgtUrl'] = 'https://from/proxyCallback.php';

Expand Down
6 changes: 3 additions & 3 deletions spec/EcPhp/CasBundle/Configuration/SymfonySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function it_is_initializable()

public function it_replace_symfony_routes_into_proper_urls(RouterInterface $router)
{
$testProperties = Cas::getTestProperties()->all();
$testProperties = Cas::getTestProperties()->jsonSerialize();

$properties = [
'cas' => $testProperties,
Expand Down Expand Up @@ -74,14 +74,14 @@ public function it_replace_symfony_routes_into_proper_urls(RouterInterface $rout
);

$this
->all()
->jsonSerialize()
->shouldReturn($updatedProperties);
}

public function let(RouterInterface $router)
{
$properties = [
'cas' => Cas::getTestProperties()->all(),
'cas' => Cas::getTestProperties()->jsonSerialize(),
];

$parameterBag = new ParameterBag($properties);
Expand Down
45 changes: 27 additions & 18 deletions spec/EcPhp/CasBundle/Controller/LoginSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,45 +11,54 @@

namespace spec\EcPhp\CasBundle\Controller;

use EcPhp\CasBundle\Cas\SymfonyCasInterface;
use EcPhp\CasBundle\Controller\Login;
use EcPhp\CasLib\Contract\CasInterface;
use Exception;
use Nyholm\Psr7\ServerRequest;
use PhpSpec\ObjectBehavior;
use Psr\Http\Message\ResponseInterface;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;

class LoginSpec extends ObjectBehavior
{
public function it_can_be_invoked(
SymfonyCasInterface $cas,
public function it_is_initializable()
{
$this->shouldHaveType(Login::class);
}

public function it_return_a_psr_response_in_case_of_success(
CasInterface $cas,
Security $security,
ResponseInterface $casResponse,
) {
$request = Request::create('');
$request = new ServerRequest('GET', '/');

$response = $this->__invoke($request, $cas, $security);
$response->shouldBeAnInstanceOf(RedirectResponse::class);
$response->headers->get('location')->shouldBe('/');
$cas
->login($request)
->willReturn($casResponse);

$casResponse
->getHeaderLine('location')
->willReturn('https://foo.com');

$cas
->login(
$request,
['renew' => false]
)
->willReturn($casResponse);

$response = $this->__invoke($request, $cas, $security);
$response->shouldBeAnInstanceOf(ResponseInterface::class);
$response->getHeaderLine('location')->shouldBe('https://foo.com');
}

public function it_is_initializable()
{
$this->shouldHaveType(Login::class);
public function it_return_a_symfony_redirection_in_case_of_exception(
CasInterface $cas,
Security $security,
) {
$request = new ServerRequest('GET', '/');

$cas
->login($request)
->willThrow(new Exception('Unable to login'));

$response = $this->__invoke($request, $cas, $security);
$response->shouldBeAnInstanceOf(RedirectResponse::class);
$response->headers->get('location')->shouldBe('/');
}
}
68 changes: 38 additions & 30 deletions spec/EcPhp/CasBundle/Controller/LogoutSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,64 +11,72 @@

namespace spec\EcPhp\CasBundle\Controller;

use EcPhp\CasBundle\Cas\SymfonyCasInterface;
use EcPhp\CasBundle\Controller\Logout;
use EcPhp\CasLib\Contract\CasInterface;
use Exception;
use Nyholm\Psr7\ServerRequest;
use PhpSpec\ObjectBehavior;
use Psr\Http\Message\ResponseInterface;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;

class LogoutSpec extends ObjectBehavior
{
public function it_can_be_invoked(
SymfonyCasInterface $cas,
ResponseInterface $casResponse,
public function it_is_initializable()
{
$this->shouldHaveType(Logout::class);
}

public function it_redirects_to_index(
CasInterface $cas,
Security $security,
TokenStorageInterface $tokenStorage,
TokenStorageInterface $tokenStorage
) {
$request = Request::create('');
$request = new ServerRequest('GET', '/');

$casResponse
->getHeaderLine('location')
->willReturn('http://local/cas/logout');
$response = $this->__invoke($request, $cas, $security, $tokenStorage);
$response->shouldBeAnInstanceOf(RedirectResponse::class);
$response->headers->get('location')->shouldReturn('/');
}

public function it_return_a_psr_response_in_case_of_success(
CasInterface $cas,
Security $security,
TokenStorageInterface $tokenStorage,
ResponseInterface $casResponse,
) {
$request = new ServerRequest('GET', '/');

$cas
->logout(
$request,
[]
)
->logout($request)
->willReturn($casResponse);

$response = $this->__invoke($request, $cas, $security, $tokenStorage);

$response
->shouldBeAnInstanceOf(ResponseInterface::class);

$response
$casResponse
->getHeaderLine('location')
->shouldReturn('http://local/cas/logout');
->willReturn('http://local/cas/logout');

$response = $this->__invoke($request, $cas, $security, $tokenStorage);
$response->shouldBeAnInstanceOf(ResponseInterface::class);
$response->getHeaderLine('location')->shouldBe('http://local/cas/logout');
$tokenStorage
->setToken(null)
->shouldBeCalled();
}

public function it_is_initializable()
{
$this->shouldHaveType(Logout::class);
}

public function it_redirects_to_index(
SymfonyCasInterface $cas,
public function it_return_a_symfony_redirection_in_case_of_exception(
CasInterface $cas,
Security $security,
TokenStorageInterface $tokenStorage
) {
$request = Request::create('');
$request = new ServerRequest('GET', '/');

$cas
->logout($request)
->willThrow(new Exception('Unable to login'));

$response = $this->__invoke($request, $cas, $security, $tokenStorage);
$response->shouldBeAnInstanceOf(RedirectResponse::class);
$response->headers->get('location')->shouldReturn('/');
$response->headers->get('location')->shouldBe('/');
}
}
66 changes: 28 additions & 38 deletions spec/EcPhp/CasBundle/Controller/ProxyCallbackSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@

namespace spec\EcPhp\CasBundle\Controller;

use EcPhp\CasBundle\Cas\SymfonyCasInterface;
use EcPhp\CasBundle\Controller\ProxyCallback;
use EcPhp\CasLib\Contract\CasInterface;
use Nyholm\Psr7\ServerRequest;
use PhpSpec\ObjectBehavior;
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\HttpFoundation\Request;

class ProxyCallbackSpec extends ObjectBehavior
{
Expand All @@ -25,10 +25,10 @@ public function it_is_initializable()
}

public function it_returns_a_statuscode_200_when_pgtIou_and_pgtId_are_available(
SymfonyCasInterface $cas,
CasInterface $cas,
ResponseInterface $response
) {
$request = Request::create('http://local/cas/proxy?pgtIou=pgtIou&pgtId=pgtId');
$request = new ServerRequest('GET', 'http://local/cas/proxy?pgtIou=pgtIou&pgtId=pgtId');

$response
->getStatusCode()
Expand All @@ -37,104 +37,94 @@ public function it_returns_a_statuscode_200_when_pgtIou_and_pgtId_are_available(
$cas
->handleProxyCallback(
$request,
[
'pgtIou' => 'pgtIou',
'pgtId' => 'pgtId',
]
)
->willReturn($response);

$this
->__invoke($request, $cas)
$response = $this->__invoke($request, $cas);

$response
->shouldBeAnInstanceOf(ResponseInterface::class);

$this
->__invoke($request, $cas)
$response
->getStatusCode()
->shouldReturn(200);
}

public function it_returns_a_statuscode_500_when_missing_pgtId(
SymfonyCasInterface $cas,
CasInterface $cas,
ResponseInterface $response
) {
$request = Request::create('http://local/cas/proxy?pgtIou=pgtIou');
$request = new ServerRequest('GET', 'http://local/cas/proxy?pgtIou=pgtIou');

$response
->getStatusCode()
->willReturn(500);

$cas
->handleProxyCallback(
$request,
[
'pgtIou' => 'pgtIou',
]
$request
)
->willReturn($response);

$this
->__invoke($request, $cas)
->shouldBeAnInstanceOf(ResponseInterface::class);

$this
->__invoke($request, $cas)
$response = $this->__invoke($request, $cas);

$response
->getStatusCode()
->shouldReturn(500);
}

public function it_returns_a_statuscode_500_when_missing_pgtId_and_pgtIou(
SymfonyCasInterface $cas,
CasInterface $cas,
ResponseInterface $response
) {
$request = Request::create('');
$request = new ServerRequest('GET', 'http://local/cas/proxy');

$response
->getStatusCode()
->willReturn(500);

$cas
->handleProxyCallback(
$request,
[]
$request
)
->willReturn($response);

$this
->__invoke($request, $cas)
$response = $this->__invoke($request, $cas);

$response
->shouldBeAnInstanceOf(ResponseInterface::class);

$this
->__invoke($request, $cas)
$response
->getStatusCode()
->shouldReturn(500);
}

public function it_returns_a_statuscode_500_when_missing_pgtIou(
SymfonyCasInterface $cas,
CasInterface $cas,
ResponseInterface $response
) {
$request = Request::create('http://local/cas/proxy?pgtId=pgtId');
$request = new ServerRequest('GET', 'http://local/cas/proxy?pgtId=pgtId');

$response
->getStatusCode()
->willReturn(500);

$cas
->handleProxyCallback(
$request,
[
'pgtId' => 'pgtId',
]
$request
)
->willReturn($response);

$this
->__invoke($request, $cas)
$response = $this->__invoke($request, $cas);

$response
->shouldBeAnInstanceOf(ResponseInterface::class);

$this
->__invoke($request, $cas)
$response
->getStatusCode()
->shouldReturn(500);
}
Expand Down
Loading

0 comments on commit 12fa217

Please sign in to comment.