/
UserPasswordValidatorTest.php
168 lines (133 loc) · 4.6 KB
/
UserPasswordValidatorTest.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Security\Core\Tests\Validator\Constraints;
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;
use Symfony\Component\Security\Core\SecurityContextInterface;
use Symfony\Component\Security\Core\Validator\Constraints\UserPassword;
use Symfony\Component\Security\Core\Validator\Constraints\UserPasswordValidator;
use Symfony\Component\Validator\Tests\Constraints\AbstractConstraintValidatorTest;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class UserPasswordValidatorTest extends AbstractConstraintValidatorTest
{
const PASSWORD = 's3Cr3t';
const SALT = '^S4lt$';
/**
* @var SecurityContextInterface
*/
protected $securityContext;
/**
* @var PasswordEncoderInterface
*/
protected $encoder;
/**
* @var EncoderFactoryInterface
*/
protected $encoderFactory;
protected function createValidator()
{
return new UserPasswordValidator($this->securityContext, $this->encoderFactory);
}
protected function setUp()
{
$user = $this->createUser();
$this->securityContext = $this->createSecurityContext($user);
$this->encoder = $this->createPasswordEncoder();
$this->encoderFactory = $this->createEncoderFactory($this->encoder);
parent::setUp();
}
public function testPasswordIsValid()
{
$constraint = new UserPassword(array(
'message' => 'myMessage',
));
$this->encoder->expects($this->once())
->method('isPasswordValid')
->with(static::PASSWORD, 'secret', static::SALT)
->will($this->returnValue(true));
$this->validator->validate('secret', $constraint);
$this->assertNoViolation();
}
public function testPasswordIsNotValid()
{
$constraint = new UserPassword(array(
'message' => 'myMessage',
));
$this->encoder->expects($this->once())
->method('isPasswordValid')
->with(static::PASSWORD, 'secret', static::SALT)
->will($this->returnValue(false));
$this->validator->validate('secret', $constraint);
$this->assertViolation('myMessage');
}
/**
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
*/
public function testUserIsNotValid()
{
$user = $this->getMock('Foo\Bar\User');
$this->securityContext = $this->createSecurityContext($user);
$this->validator = $this->createValidator();
$this->validator->initialize($this->context);
$this->validator->validate('secret', new UserPassword());
}
protected function createUser()
{
$mock = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
$mock
->expects($this->any())
->method('getPassword')
->will($this->returnValue(static::PASSWORD))
;
$mock
->expects($this->any())
->method('getSalt')
->will($this->returnValue(static::SALT))
;
return $mock;
}
protected function createPasswordEncoder($isPasswordValid = true)
{
return $this->getMock('Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface');
}
protected function createEncoderFactory($encoder = null)
{
$mock = $this->getMock('Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface');
$mock
->expects($this->any())
->method('getEncoder')
->will($this->returnValue($encoder))
;
return $mock;
}
protected function createSecurityContext($user = null)
{
$token = $this->createAuthenticationToken($user);
$mock = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
$mock
->expects($this->any())
->method('getToken')
->will($this->returnValue($token))
;
return $mock;
}
protected function createAuthenticationToken($user = null)
{
$mock = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
$mock
->expects($this->any())
->method('getUser')
->will($this->returnValue($user))
;
return $mock;
}
}