diff --git a/spec/EcPhp/EuLoginBundle/Security/Core/User/EuLoginUserProviderSpec.php b/spec/EcPhp/EuLoginBundle/Security/Core/User/EuLoginUserProviderSpec.php new file mode 100644 index 0000000..6f315b8 --- /dev/null +++ b/spec/EcPhp/EuLoginBundle/Security/Core/User/EuLoginUserProviderSpec.php @@ -0,0 +1,116 @@ +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' + + + username + bar + + foo + + + group1 + group2 + + + +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); + } +} diff --git a/spec/EcPhp/EuLoginBundle/Security/Core/User/EuLoginUserSpec.php b/spec/EcPhp/EuLoginBundle/Security/Core/User/EuLoginUserSpec.php index 151d379..a26e72f 100644 --- a/spec/EcPhp/EuLoginBundle/Security/Core/User/EuLoginUserSpec.php +++ b/spec/EcPhp/EuLoginBundle/Security/Core/User/EuLoginUserSpec.php @@ -4,6 +4,8 @@ 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; @@ -11,40 +13,20 @@ 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() @@ -134,41 +116,29 @@ 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() @@ -176,10 +146,113 @@ 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', @@ -207,13 +280,6 @@ public function let() 'sso' => 'sso', 'ticketType' => 'ticketType', 'proxyGrantingProtocol' => 'proxyGrantingProtocol', - 'proxyGrantingTicket' => 'proxyGrantingTicket', - 'proxies' => [ - 'proxy1', - ], ]; - - $this - ->beConstructedWith($data); } }