|
17 | 17 | use Symfony\Component\Security\Core\Exception\BadCredentialsException;
|
18 | 18 | use Symfony\Component\Security\Core\User\User;
|
19 | 19 | use Symfony\Component\Security\Http\Authenticator\AuthenticatorInterface;
|
| 20 | +use Symfony\Component\Security\Http\Authenticator\Passport\Badge\PasswordUpgradeBadge; |
20 | 21 | use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
|
21 | 22 | use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\CustomCredentials;
|
22 | 23 | use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials;
|
@@ -113,6 +114,54 @@ public function testNoCredentialsBadgeProvided()
|
113 | 114 | $this->listener->checkPassport($event);
|
114 | 115 | }
|
115 | 116 |
|
| 117 | + public function testAddsPasswordUpgradeBadge() |
| 118 | + { |
| 119 | + $encoder = $this->createMock(PasswordEncoderInterface::class); |
| 120 | + $encoder->expects($this->any())->method('isPasswordValid')->with('encoded-password', 'ThePa$$word')->willReturn(true); |
| 121 | + |
| 122 | + $this->encoderFactory->expects($this->any())->method('getEncoder')->with($this->identicalTo($this->user))->willReturn($encoder); |
| 123 | + |
| 124 | + $passport = new Passport(new UserBadge('wouter', function () { return $this->user; }), new PasswordCredentials('ThePa$$word')); |
| 125 | + $this->listener->checkPassport($this->createEvent($passport)); |
| 126 | + |
| 127 | + $this->assertTrue($passport->hasBadge(PasswordUpgradeBadge::class)); |
| 128 | + $this->assertEquals('ThePa$$word', $passport->getBadge(PasswordUpgradeBadge::class)->getAndErasePlaintextPassword()); |
| 129 | + } |
| 130 | + |
| 131 | + public function testAddsNoPasswordUpgradeBadgeIfItAlreadyExists() |
| 132 | + { |
| 133 | + $encoder = $this->createMock(PasswordEncoderInterface::class); |
| 134 | + $encoder->expects($this->any())->method('isPasswordValid')->with('encoded-password', 'ThePa$$word')->willReturn(true); |
| 135 | + |
| 136 | + $this->encoderFactory->expects($this->any())->method('getEncoder')->with($this->identicalTo($this->user))->willReturn($encoder); |
| 137 | + |
| 138 | + $passport = $this->getMockBuilder(Passport::class) |
| 139 | + ->setMethods(['addBadge']) |
| 140 | + ->setConstructorArgs([new UserBadge('wouter', function () { return $this->user; }), new PasswordCredentials('ThePa$$word'), [new PasswordUpgradeBadge('ThePa$$word')]]) |
| 141 | + ->getMock(); |
| 142 | + |
| 143 | + $passport->expects($this->never())->method('addBadge')->with($this->isInstanceOf(PasswordUpgradeBadge::class)); |
| 144 | + |
| 145 | + $this->listener->checkPassport($this->createEvent($passport)); |
| 146 | + } |
| 147 | + |
| 148 | + public function testAddsNoPasswordUpgradeBadgeIfPasswordIsInvalid() |
| 149 | + { |
| 150 | + $encoder = $this->createMock(PasswordEncoderInterface::class); |
| 151 | + $encoder->expects($this->any())->method('isPasswordValid')->with('encoded-password', 'ThePa$$word')->willReturn(false); |
| 152 | + |
| 153 | + $this->encoderFactory->expects($this->any())->method('getEncoder')->with($this->identicalTo($this->user))->willReturn($encoder); |
| 154 | + |
| 155 | + $passport = $this->getMockBuilder(Passport::class) |
| 156 | + ->setMethods(['addBadge']) |
| 157 | + ->setConstructorArgs([new UserBadge('wouter', function () { return $this->user; }), new PasswordCredentials('ThePa$$word'), [new PasswordUpgradeBadge('ThePa$$word')]]) |
| 158 | + ->getMock(); |
| 159 | + |
| 160 | + $passport->expects($this->never())->method('addBadge')->with($this->isInstanceOf(PasswordUpgradeBadge::class)); |
| 161 | + |
| 162 | + $this->listener->checkPassport($this->createEvent($passport)); |
| 163 | + } |
| 164 | + |
116 | 165 | private function createEvent($passport)
|
117 | 166 | {
|
118 | 167 | return new CheckPassportEvent($this->createMock(AuthenticatorInterface::class), $passport);
|
|
0 commit comments