-
Notifications
You must be signed in to change notification settings - Fork 9
/
CasUserProviderSpec.php
147 lines (120 loc) · 4.43 KB
/
CasUserProviderSpec.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?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\CasBundle\Security\Core\User;
use EcPhp\CasBundle\Security\Core\User\CasUser;
use EcPhp\CasBundle\Security\Core\User\CasUserInterface;
use EcPhp\CasBundle\Security\Core\User\CasUserProvider;
use EcPhp\CasLib\Contract\Response\CasResponseBuilderInterface;
use EcPhp\CasLib\Response\Type\AuthenticationFailure;
use EcPhp\CasLib\Response\Type\ServiceValidate;
use Nyholm\Psr7\Factory\Psr17Factory;
use PhpSpec\ObjectBehavior;
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
use Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface;
use Symfony\Component\HttpFoundation\Response as HttpFoundationResponse;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\User\InMemoryUser;
use Symfony\Component\Security\Core\User\User;
class CasUserProviderSpec extends ObjectBehavior
{
public function it_can_check_if_it_is_possible_to_load_a_user_by_username()
{
$this
->shouldThrow(UnsupportedUserException::class)
->during('loadUserByUsername', ['foo']);
}
public function it_can_check_if_the_user_class_is_supported()
{
$this
->supportsClass(CasUser::class)
->shouldReturn(true);
$this
->supportsClass(User::class)
->shouldReturn(false);
}
public function it_can_load_a_user_with_a_response(
CasResponseBuilderInterface $casResponseBuilder,
HttpMessageFactoryInterface $httpMessageFactory
) {
$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 HttpFoundationResponse($body, 200, ['content-type' => 'application/xml']);
$psrResponse = $this->getHttpMessageFactory()->createResponse($response);
$httpMessageFactory
->createResponse($response)
->willReturn($psrResponse);
$casResponseBuilder
->fromResponse($psrResponse)
->willReturn(new ServiceValidate($psrResponse));
$this->beConstructedWith(
$casResponseBuilder,
$httpMessageFactory
);
$this
->loadUserByResponse($response)
->shouldBeAnInstanceOf(CasUserInterface::class);
$body = <<< 'EOF'
<cas:serviceResponse xmlns:cas="http://www.yale.edu/tp/cas">
<cas:authenticationFailure>
</cas:authenticationFailure>
</cas:serviceResponse>
EOF;
$response = new HttpFoundationResponse($body, 200, ['content-type' => 'application/xml']);
$psrResponse = $this->getHttpMessageFactory()->createResponse($response);
$httpMessageFactory
->createResponse($response)
->willReturn($psrResponse);
$casResponseBuilder
->fromResponse($psrResponse)
->willReturn(new AuthenticationFailure($psrResponse));
$this
->shouldThrow(AuthenticationException::class)
->during('loadUserByResponse', [$response]);
}
public function it_can_refresh_the_user()
{
$user = new CasUser([]);
$this
->refreshUser($user)
->shouldReturn($user);
$user = new InMemoryUser('username', 'password');
$this
->shouldThrow(UnsupportedUserException::class)
->during('refreshUser', [$user]);
}
public function it_is_initializable()
{
$this->shouldHaveType(CasUserProvider::class);
}
public function let(
CasResponseBuilderInterface $casResponseBuilder,
HttpMessageFactoryInterface $psrHttpFactory
) {
$this->beConstructedWith(
$casResponseBuilder,
$psrHttpFactory
);
}
private function getHttpMessageFactory(): HttpMessageFactoryInterface
{
$psr17Factory = new Psr17Factory();
return new PsrHttpFactory(
$psr17Factory,
$psr17Factory,
$psr17Factory,
$psr17Factory
);
}
}