|
12 | 12 | namespace Symfony\Component\Security\Core\Tests\Authentication\Provider;
|
13 | 13 |
|
14 | 14 | use Symfony\Component\Ldap\LdapInterface;
|
| 15 | +use Symfony\Component\Ldap\Entry; |
| 16 | +use Symfony\Component\Ldap\Adapter\QueryInterface; |
| 17 | +use Symfony\Component\Ldap\Adapter\CollectionInterface; |
15 | 18 | use Symfony\Component\Security\Core\Authentication\Provider\LdapBindAuthenticationProvider;
|
16 | 19 | use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
|
17 | 20 | use Symfony\Component\Security\Core\User\User;
|
@@ -81,4 +84,73 @@ public function testRetrieveUser()
|
81 | 84 |
|
82 | 85 | $reflection->invoke($provider, 'foo', new UsernamePasswordToken('foo', 'bar', 'key'));
|
83 | 86 | }
|
| 87 | + |
| 88 | + public function testQueryForDn() |
| 89 | + { |
| 90 | + $userProvider = $this->getMockBuilder(UserProviderInterface::class)->getMock(); |
| 91 | + |
| 92 | + $collection = new \ArrayIterator(array(new Entry(''))); |
| 93 | + |
| 94 | + $query = $this->getMockBuilder(QueryInterface::class)->getMock(); |
| 95 | + $query |
| 96 | + ->expects($this->once()) |
| 97 | + ->method('execute') |
| 98 | + ->will($this->returnValue($collection)) |
| 99 | + ; |
| 100 | + |
| 101 | + $ldap = $this->getMockBuilder(LdapInterface::class)->getMock(); |
| 102 | + $ldap |
| 103 | + ->expects($this->once()) |
| 104 | + ->method('escape') |
| 105 | + ->with('foo', '') |
| 106 | + ->will($this->returnValue('foo')) |
| 107 | + ; |
| 108 | + $ldap |
| 109 | + ->expects($this->once()) |
| 110 | + ->method('query') |
| 111 | + ->with('{username}', 'foobar') |
| 112 | + ->will($this->returnValue($query)) |
| 113 | + ; |
| 114 | + $userChecker = $this->getMockBuilder(UserCheckerInterface::class)->getMock(); |
| 115 | + |
| 116 | + $provider = new LdapBindAuthenticationProvider($userProvider, $userChecker, 'key', $ldap); |
| 117 | + $provider->setQueryString('{username}bar'); |
| 118 | + $reflection = new \ReflectionMethod($provider, 'checkAuthentication'); |
| 119 | + $reflection->setAccessible(true); |
| 120 | + |
| 121 | + $reflection->invoke($provider, new User('foo', null), new UsernamePasswordToken('foo', 'bar', 'key')); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * @expectedException \Symfony\Component\Security\Core\Exception\BadCredentialsException |
| 126 | + * @expectedExceptionMessage The presented username is invalid. |
| 127 | + */ |
| 128 | + public function testEmptyQueryResultShouldThrowAnException() |
| 129 | + { |
| 130 | + $userProvider = $this->getMockBuilder(UserProviderInterface::class)->getMock(); |
| 131 | + |
| 132 | + $collection = $this->getMockBuilder(CollectionInterface::class)->getMock(); |
| 133 | + |
| 134 | + $query = $this->getMockBuilder(QueryInterface::class)->getMock(); |
| 135 | + $query |
| 136 | + ->expects($this->once()) |
| 137 | + ->method('execute') |
| 138 | + ->will($this->returnValue($collection)) |
| 139 | + ; |
| 140 | + |
| 141 | + $ldap = $this->getMockBuilder(LdapInterface::class)->getMock(); |
| 142 | + $ldap |
| 143 | + ->expects($this->once()) |
| 144 | + ->method('query') |
| 145 | + ->will($this->returnValue($query)) |
| 146 | + ; |
| 147 | + $userChecker = $this->getMockBuilder(UserCheckerInterface::class)->getMock(); |
| 148 | + |
| 149 | + $provider = new LdapBindAuthenticationProvider($userProvider, $userChecker, 'key', $ldap); |
| 150 | + $provider->setQueryString('{username}bar'); |
| 151 | + $reflection = new \ReflectionMethod($provider, 'checkAuthentication'); |
| 152 | + $reflection->setAccessible(true); |
| 153 | + |
| 154 | + $reflection->invoke($provider, new User('foo', null), new UsernamePasswordToken('foo', 'bar', 'key')); |
| 155 | + } |
84 | 156 | }
|
0 commit comments