Skip to content

Commit

Permalink
Add missing tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Mar 12, 2020
1 parent 6e68297 commit b3531f1
Show file tree
Hide file tree
Showing 2 changed files with 247 additions and 65 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

declare(strict_types=1);

namespace spec\EcPhp\EuLoginBundle\Security\Core\User;

use EcPhp\EuLoginBundle\Security\Core\User\EuLoginUser;
use EcPhp\EuLoginBundle\Security\Core\User\EuLoginUserInterface;
use EcPhp\EuLoginBundle\Security\Core\User\EuLoginUserProvider;
use PhpSpec\ObjectBehavior;
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\User\User;

class EuLoginUserProviderSpec extends ObjectBehavior
{
public function it_can_check_if_the_user_class_is_supported()
{
$this
->supportsClass(EuLoginUser::class)
->shouldReturn(true);

$this
->supportsClass(User::class)
->shouldReturn(false);
}

public function it_can_load_a_user_from_a_response(ResponseInterface $response)
{
$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:authenticationSuccess>
</cas:serviceResponse>
EOF;

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

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

$this
->loadUserByResponse($response)
->getAttributes()
->shouldEqual([
'foo' => 'bar',
'groups' => [
'group' => [
'group1',
'group2',
],
],
]);

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

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

public function it_can_refresh_a_user(EuLoginUserInterface $user)
{
$this
->shouldThrow(UnsupportedUserException::class)
->during('refreshUser', [new User('foo', 'bar')]);

$this
->refreshUser($user)
->shouldReturn($user);
}

public function it_cannot_load_a_user_by_username()
{
$this
->shouldThrow(UnsupportedUserException::class)
->during('loadUserByUsername', ['foo']);
}

public function it_is_initializable()
{
$this->shouldHaveType(EuLoginUserProvider::class);
}
}
196 changes: 131 additions & 65 deletions spec/EcPhp/EuLoginBundle/Security/Core/User/EuLoginUserSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,29 @@

namespace spec\EcPhp\EuLoginBundle\Security\Core\User;

use EcPhp\CasBundle\Security\Core\User\CasUser;
use EcPhp\CasBundle\Security\Core\User\CasUserInterface;
use EcPhp\EuLoginBundle\Security\Core\User\EuLoginUser;
use PhpSpec\ObjectBehavior;

class EuLoginUserSpec extends ObjectBehavior
{
public function it_can_get_groups_when_no_groups_are_available()
{
$attributes = $this->getAttributesData();
unset($attributes['groups']);

$data = [
'user' => 'user',
'departmentNumber' => 'departmentNumber',
'email' => 'email',
'employeeNumber' => 'employeeNumber',
'employeeType' => 'employeeType',
'firstName' => 'firstName',
'lastName' => 'lastName',
'domain' => 'domain',
'domainUsername' => 'domainUsername',
'telephoneNumber' => 'telephoneNumber',
'locale' => 'locale',
'assuranceLevel' => 'assuranceLevel',
'uid' => 'uid',
'orgId' => 'orgId',
'teleworkingPriority' => 'teleworkingPriority',
'strengths' => [
'bar',
],
'authenticationFactors' => [
'foobar',
],
'loginDate' => 'loginDate',
'sso' => 'sso',
'ticketType' => 'ticketType',
'proxyGrantingProtocol' => 'proxyGrantingProtocol',
'proxyGrantingTicket' => 'proxyGrantingTicket',
'proxies' => [
'proxy1',
],
'attributes' => $attributes,
];

$this
->beConstructedWith($data);
->beConstructedWith(new CasUser($data));

$this
->getGroups()
Expand Down Expand Up @@ -134,52 +116,143 @@ public function it_can_get_specific_attribute()
->shouldReturn('uid');
}

public function it_can_get_the_attributes_only()
public function it_can_get_the_attributes_only(CasUserInterface $user)
{
$data = [
'user' => 'user',
'proxyGrantingTicket' => 'proxyGrantingTicket',
'proxies' => [
'proxy1',
],
'attributes' => $this->getAttributesData(),
];

$user
->getAttributes()
->willReturn($this->getAttributesData());

$user
->beConstructedWith($data);
$this
->beConstructedWith($user);

$this
->getAttributes()
->shouldReturn(
[
'departmentNumber' => 'departmentNumber',
'email' => 'email',
'employeeNumber' => 'employeeNumber',
'employeeType' => 'employeeType',
'firstName' => 'firstName',
'lastName' => 'lastName',
'domain' => 'domain',
'domainUsername' => 'domainUsername',
'telephoneNumber' => 'telephoneNumber',
'locale' => 'locale',
'assuranceLevel' => 'assuranceLevel',
'uid' => 'uid',
'orgId' => 'orgId',
'teleworkingPriority' => 'teleworkingPriority',
'groups' => [
'foo',
],
'strengths' => [
'bar',
],
'authenticationFactors' => [
'foobar',
],
'loginDate' => 'loginDate',
'sso' => 'sso',
'ticketType' => 'ticketType',
'proxyGrantingProtocol' => 'proxyGrantingProtocol',
]
);
->shouldReturn($this->getAttributesData());
}

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

public function let()
public function let(CasUserInterface $user)
{
$data = [
'user' => 'user',
'proxyGrantingTicket' => 'proxyGrantingTicket',
'proxies' => [
'proxy1',
],
'attributes' => $this->getAttributesData(),
];

$user
->beConstructedWith($data);

$user
->getAttribute('assuranceLevel')
->willReturn('assuranceLevel');

$user
->getAttribute('authenticationFactors', [])
->willReturn([
'foobar',
]);

$user
->getAttribute('departmentNumber')
->willReturn('departmentNumber');

$user
->getAttribute('domain')
->willReturn('domain');

$user
->getAttribute('domainUsername')
->willReturn('domainUsername');

$user
->getAttribute('email')
->willReturn('email');

$user
->getAttribute('employeeNumber')
->willReturn('employeeNumber');

$user
->getAttribute('employeeType')
->willReturn('employeeType');

$user
->getAttribute('firstName')
->willReturn('firstName');

$user
->getAttribute('groups', [])
->willReturn([
'foo',
]);

$user
->getAttribute('lastName')
->willReturn('lastName');

$user
->getAttribute('locale')
->willReturn('locale');

$user
->getAttribute('loginDate')
->willReturn('loginDate');

$user
->getAttribute('orgId')
->willReturn('orgId');

$user
->getAttribute('sso')
->willReturn('sso');

$user
->getAttribute('strengths', [])
->willReturn([
'bar',
]);

$user
->getAttribute('telephoneNumber')
->willReturn('telephoneNumber');

$user
->getAttribute('teleworkingPriority')
->willReturn('teleworkingPriority');

$user
->getAttribute('ticketType')
->willReturn('ticketType');

$user
->getAttribute('uid')
->willReturn('uid');

$this
->beConstructedWith($user);
}

private function getAttributesData(): array
{
return [
'departmentNumber' => 'departmentNumber',
'email' => 'email',
'employeeNumber' => 'employeeNumber',
Expand Down Expand Up @@ -207,13 +280,6 @@ public function let()
'sso' => 'sso',
'ticketType' => 'ticketType',
'proxyGrantingProtocol' => 'proxyGrantingProtocol',
'proxyGrantingTicket' => 'proxyGrantingTicket',
'proxies' => [
'proxy1',
],
];

$this
->beConstructedWith($data);
}
}

0 comments on commit b3531f1

Please sign in to comment.