Skip to content

Commit

Permalink
Update tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Aug 28, 2020
1 parent 99ede08 commit 41a4f85
Show file tree
Hide file tree
Showing 2 changed files with 299 additions and 82 deletions.
351 changes: 282 additions & 69 deletions spec/EcPhp/EuLoginBundle/Security/Core/User/EuLoginUserProviderSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
use EcPhp\EuLoginBundle\Security\Core\User\EuLoginUser;
use EcPhp\EuLoginBundle\Security\Core\User\EuLoginUserInterface;
use EcPhp\EuLoginBundle\Security\Core\User\EuLoginUserProvider;
use Nyholm\Psr7\Response;
use PhpSpec\ObjectBehavior;
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\User\User;

Expand All @@ -28,93 +28,200 @@ public function it_can_check_if_the_user_class_is_supported()
->shouldReturn(false);
}

public function it_can_load_a_user_from_a_response(ResponseInterface $response)
public function it_can_load_a_user_from_a_response(): void
{
$response
->getStatusCode()
->willReturn(200);

$response
->hasHeader('Content-Type')
->willReturn(true);

$response
->getHeaderLine('Content-Type')
->willReturn('application/xml');

$body = <<<'EOF'
<cas:serviceResponse xmlns:cas="http://www.yale.edu/tp/cas">
<cas:authenticationSuccess>
<cas:user>username</cas:user>
<cas:foo>bar</cas:foo>
<cas:proxies>
<cas:proxy>foo</cas:proxy>
</cas:proxies>
<cas:groups>
<cas:group>group1</cas:group>
<cas:group>group2</cas:group>
</cas:groups>
<cas:extendedAttributes>
<cas:extendedAttribute name="http://stork.eu/motherInLawDogName">
<cas:attributeValue>rex</cas:attributeValue>
<cas:attributeValue>snoopy</cas:attributeValue>
</cas:extendedAttribute>
</cas:extendedAttributes>
</cas:authenticationSuccess>
</cas:serviceResponse>
EOF;
// TestBody1
$response = new Response(200, ['content-type' => 'application/xml'], $this->getTestBody1());

$response
->getBody()
->willReturn($body);

$this
->loadUserByResponse($response)
->shouldReturnAnInstanceOf(EuLoginUser::class);
$user = $this->loadUserByResponse($response);

$this
->loadUserByResponse($response)
$user
->getAttributes()
->shouldEqual([
'foo' => 'bar',
'groups' => [
'group' => [
'group1',
'group2',
->shouldReturn(
[
'departmentNumber' => 'departmentNumber',
'email' => 'email',
'employeeNumber' => 'employeeNumber',
'employeeType' => 'employeeType',
'firstName' => 'firstName',
'lastName' => 'lastName',
'domain' => 'domain',
'domainUsername' => 'domainUsername',
'telephoneNumber' => 'telephoneNumber',
'userManager' => 'userManager',
'timeZone' => 'timeZone',
'locale' => 'locale',
'assuranceLevel' => 'assuranceLevel',
'uid' => 'uid',
'orgId' => 'orgId',
'teleworkingPriority' => 'teleworkingPriority',
'extendedAttributes' => [
'extendedAttribute' => [
[
'attributeValue' => [
'value1',
'value2',
],
'@attributes' => [
'name' => 'extendedAttribute1',
],
],
[
'attributeValue' => [
'value3',
'value4',
],
'@attributes' => [
'name' => 'extendedAttribute2',
],
],
],
],
],
'extendedAttributes' => [
'http://stork.eu/motherInLawDogName' => [
'rex',
'snoopy',
'groups' => [
'group' => [
'group1',
'group2',
],
'@attributes' => [
'number' => '2',
],
],
'strengths' => [
'strength' => [
'strength1',
'strength2',
],
'@attributes' => [
'number' => '2',
],
],
'authenticationFactors' => [
'moniker' => 'moniker1',
'@attributes' => [
'number' => '1',
],
],
'loginDate' => '',
'sso' => 'sso',
'ticketType' => 'ticketType',
'proxyGrantingProtocol' => 'proxyGrantingProtocol',
]
);

$user
->getExtendedAttributes()
->shouldReturn([
'extendedAttribute1' => [
'value1',
'value2',
],
'extendedAttribute2' => [
'value3',
'value4',
],
]);

$this
->loadUserByResponse($response)
->getUsername()
->shouldReturn('username');

$this
->loadUserByResponse($response)
->getRoles()
$user
->getGroups()
->shouldReturn([
'group1',
'group2',
'ROLE_CAS_AUTHENTICATED',
]);

$this->loadUserByResponse($response)
->getExtendedAttributes()
$user
->getStrengths()
->shouldReturn([
'strength1',
'strength2',
]);

// TestBody2
$response = new Response(200, ['content-type' => 'application/xml'], $this->getTestBody2());

$user = $this->loadUserByResponse($response);

$user
->getAttributes()
->shouldReturn(
[
'http://stork.eu/motherInLawDogName' => [
'rex',
'snoopy',
'departmentNumber' => 'departmentNumber',
'email' => 'email',
'employeeNumber' => 'employeeNumber',
'employeeType' => 'employeeType',
'firstName' => 'firstName',
'lastName' => 'lastName',
'domain' => 'domain',
'domainUsername' => 'domainUsername',
'telephoneNumber' => 'telephoneNumber',
'userManager' => 'userManager',
'timeZone' => 'timeZone',
'locale' => 'locale',
'assuranceLevel' => 'assuranceLevel',
'uid' => 'uid',
'orgId' => 'orgId',
'teleworkingPriority' => 'teleworkingPriority',
'extendedAttributes' => [
'extendedAttribute' => [
[
'attributeValue' => [
'value1',
'value2',
],
'@attributes' => [
'name' => 'extendedAttribute1',
],
],
],
],
'groups' => [
'group' => [
'group1',
],
'@attributes' => [
'number' => '1',
],
],
'strengths' => [
'strength' => [
'strength1',
],
'@attributes' => [
'number' => '1',
],
],
'authenticationFactors' => [
'moniker' => 'moniker1',
'@attributes' => [
'number' => '1',
],
],
'loginDate' => '',
'sso' => 'sso',
'ticketType' => 'ticketType',
'proxyGrantingProtocol' => 'proxyGrantingProtocol',
]
);

$user
->getExtendedAttributes()
->shouldReturn([
'extendedAttribute1' => [
'value1',
'value2',
],
]);

$user
->getGroups()
->shouldReturn([
'group1',
]);

$user
->getStrengths()
->shouldReturn([
'strength1',
]);
}

public function it_can_refresh_a_user(EuLoginUserInterface $user)
Expand Down Expand Up @@ -145,4 +252,110 @@ public function let()
$this
->beConstructedWith(new CasUserProvider(new EcasIntrospector(new Introspector())));
}

private function getTestBody1()
{
return <<< 'EOF'
<?xml version="1.0" encoding="utf-8"?>
<cas:serviceResponse xmlns:cas="https://ecas.ec.europa.eu/cas/schemas/" date="" version="">
<cas:authenticationSuccess>
<cas:user>username</cas:user>
<cas:departmentNumber>departmentNumber</cas:departmentNumber>
<cas:email>email</cas:email>
<cas:employeeNumber>employeeNumber</cas:employeeNumber>
<cas:employeeType>employeeType</cas:employeeType>
<cas:firstName>firstName</cas:firstName>
<cas:lastName>lastName</cas:lastName>
<cas:domain>domain</cas:domain>
<cas:domainUsername>domainUsername</cas:domainUsername>
<cas:telephoneNumber>telephoneNumber</cas:telephoneNumber>
<cas:userManager>userManager</cas:userManager>
<cas:timeZone>timeZone</cas:timeZone>
<cas:locale>locale</cas:locale>
<cas:assuranceLevel>assuranceLevel</cas:assuranceLevel>
<cas:uid>uid</cas:uid>
<cas:orgId>orgId</cas:orgId>
<cas:teleworkingPriority>teleworkingPriority</cas:teleworkingPriority>
<cas:extendedAttributes>
<cas:extendedAttribute name="extendedAttribute1">
<cas:attributeValue>value1</cas:attributeValue>
<cas:attributeValue>value2</cas:attributeValue>
</cas:extendedAttribute>
<cas:extendedAttribute name="extendedAttribute2">
<cas:attributeValue>value3</cas:attributeValue>
<cas:attributeValue>value4</cas:attributeValue>
</cas:extendedAttribute>
</cas:extendedAttributes>
<cas:groups number="2">
<cas:group>group1</cas:group>
<cas:group>group2</cas:group>
</cas:groups>
<cas:strengths number="2">
<cas:strength>strength1</cas:strength>
<cas:strength>strength2</cas:strength>
</cas:strengths>
<cas:authenticationFactors number="1">
<cas:moniker>moniker1</cas:moniker>
</cas:authenticationFactors>
<cas:loginDate></cas:loginDate>
<cas:sso>sso</cas:sso>
<cas:ticketType>ticketType</cas:ticketType>
<cas:proxyGrantingProtocol>proxyGrantingProtocol</cas:proxyGrantingProtocol>
<cas:proxies>
<cas:proxy>proxy1</cas:proxy>
</cas:proxies>
</cas:authenticationSuccess>
</cas:serviceResponse>
EOF;
}

private function getTestBody2()
{
return <<< 'EOF'
<?xml version="1.0" encoding="utf-8"?>
<cas:serviceResponse xmlns:cas="https://ecas.ec.europa.eu/cas/schemas/" date="" version="">
<cas:authenticationSuccess>
<cas:user>username</cas:user>
<cas:departmentNumber>departmentNumber</cas:departmentNumber>
<cas:email>email</cas:email>
<cas:employeeNumber>employeeNumber</cas:employeeNumber>
<cas:employeeType>employeeType</cas:employeeType>
<cas:firstName>firstName</cas:firstName>
<cas:lastName>lastName</cas:lastName>
<cas:domain>domain</cas:domain>
<cas:domainUsername>domainUsername</cas:domainUsername>
<cas:telephoneNumber>telephoneNumber</cas:telephoneNumber>
<cas:userManager>userManager</cas:userManager>
<cas:timeZone>timeZone</cas:timeZone>
<cas:locale>locale</cas:locale>
<cas:assuranceLevel>assuranceLevel</cas:assuranceLevel>
<cas:uid>uid</cas:uid>
<cas:orgId>orgId</cas:orgId>
<cas:teleworkingPriority>teleworkingPriority</cas:teleworkingPriority>
<cas:extendedAttributes>
<cas:extendedAttribute name="extendedAttribute1">
<cas:attributeValue>value1</cas:attributeValue>
<cas:attributeValue>value2</cas:attributeValue>
</cas:extendedAttribute>
</cas:extendedAttributes>
<cas:groups number="1">
<cas:group>group1</cas:group>
</cas:groups>
<cas:strengths number="1">
<cas:strength>strength1</cas:strength>
</cas:strengths>
<cas:authenticationFactors number="1">
<cas:moniker>moniker1</cas:moniker>
</cas:authenticationFactors>
<cas:loginDate></cas:loginDate>
<cas:sso>sso</cas:sso>
<cas:ticketType>ticketType</cas:ticketType>
<cas:proxyGrantingProtocol>proxyGrantingProtocol</cas:proxyGrantingProtocol>
<cas:proxies>
<cas:proxy>proxy1</cas:proxy>
</cas:proxies>
</cas:authenticationSuccess>
</cas:serviceResponse>
EOF;
}
}
Loading

0 comments on commit 41a4f85

Please sign in to comment.