* * 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\User; use PHPUnit\Framework\TestCase; use Symfony\Component\Ldap\Adapter\CollectionInterface; use Symfony\Component\Ldap\Adapter\QueryInterface; use Symfony\Component\Ldap\Entry; use Symfony\Component\Ldap\LdapInterface; use Symfony\Component\Security\Core\User\LdapUserProvider; use Symfony\Component\Ldap\Exception\ConnectionException; /** * @requires extension ldap */ class LdapUserProviderTest extends TestCase { /** * @expectedException \Symfony\Component\Security\Core\Exception\UsernameNotFoundException */ public function testLoadUserByUsernameFailsIfCantConnectToLdap() { $ldap = $this->getMockBuilder(LdapInterface::class)->getMock(); $ldap ->expects($this->once()) ->method('bind') ->will($this->throwException(new ConnectionException())) ; $provider = new LdapUserProvider($ldap, 'ou=MyBusiness,dc=symfony,dc=com'); $provider->loadUserByUsername('foo'); } /** * @expectedException \Symfony\Component\Security\Core\Exception\UsernameNotFoundException */ public function testLoadUserByUsernameFailsIfNoLdapEntries() { $result = $this->getMockBuilder(CollectionInterface::class)->getMock(); $query = $this->getMockBuilder(QueryInterface::class)->getMock(); $query ->expects($this->once()) ->method('execute') ->will($this->returnValue($result)) ; $result ->expects($this->once()) ->method('count') ->will($this->returnValue(0)) ; $ldap = $this->getMockBuilder(LdapInterface::class)->getMock(); $ldap ->expects($this->once()) ->method('escape') ->will($this->returnValue('foo')) ; $ldap ->expects($this->once()) ->method('query') ->will($this->returnValue($query)) ; $provider = new LdapUserProvider($ldap, 'ou=MyBusiness,dc=symfony,dc=com'); $provider->loadUserByUsername('foo'); } /** * @expectedException \Symfony\Component\Security\Core\Exception\UsernameNotFoundException */ public function testLoadUserByUsernameFailsIfMoreThanOneLdapEntry() { $result = $this->getMockBuilder(CollectionInterface::class)->getMock(); $query = $this->getMockBuilder(QueryInterface::class)->getMock(); $query ->expects($this->once()) ->method('execute') ->will($this->returnValue($result)) ; $result ->expects($this->once()) ->method('count') ->will($this->returnValue(2)) ; $ldap = $this->getMockBuilder(LdapInterface::class)->getMock(); $ldap ->expects($this->once()) ->method('escape') ->will($this->returnValue('foo')) ; $ldap ->expects($this->once()) ->method('query') ->will($this->returnValue($query)) ; $provider = new LdapUserProvider($ldap, 'ou=MyBusiness,dc=symfony,dc=com'); $provider->loadUserByUsername('foo'); } /** * @expectedException \Symfony\Component\Security\Core\Exception\InvalidArgumentException */ public function testLoadUserByUsernameFailsIfMoreThanOneLdapPasswordsInEntry() { $result = $this->getMockBuilder(CollectionInterface::class)->getMock(); $query = $this->getMockBuilder(QueryInterface::class)->getMock(); $query ->expects($this->once()) ->method('execute') ->will($this->returnValue($result)) ; $ldap = $this->getMockBuilder(LdapInterface::class)->getMock(); $result ->expects($this->once()) ->method('offsetGet') ->with(0) ->will($this->returnValue(new Entry('foo', array( 'sAMAccountName' => array('foo'), 'userpassword' => array('bar', 'baz'), ) ))) ; $result ->expects($this->once()) ->method('count') ->will($this->returnValue(1)) ; $ldap ->expects($this->once()) ->method('escape') ->will($this->returnValue('foo')) ; $ldap ->expects($this->once()) ->method('query') ->will($this->returnValue($query)) ; $provider = new LdapUserProvider($ldap, 'ou=MyBusiness,dc=symfony,dc=com', null, null, array(), 'sAMAccountName', '({uid_key}={username})', 'userpassword'); $this->assertInstanceOf( 'Symfony\Component\Security\Core\User\User', $provider->loadUserByUsername('foo') ); } public function testLoadUserByUsernameShouldNotFailIfEntryHasNoUidKeyAttribute() { $result = $this->getMockBuilder(CollectionInterface::class)->getMock(); $query = $this->getMockBuilder(QueryInterface::class)->getMock(); $query ->expects($this->once()) ->method('execute') ->will($this->returnValue($result)) ; $ldap = $this->getMockBuilder(LdapInterface::class)->getMock(); $result ->expects($this->once()) ->method('offsetGet') ->with(0) ->will($this->returnValue(new Entry('foo', array()))) ; $result ->expects($this->once()) ->method('count') ->will($this->returnValue(1)) ; $ldap ->expects($this->once()) ->method('escape') ->will($this->returnValue('foo')) ; $ldap ->expects($this->once()) ->method('query') ->will($this->returnValue($query)) ; $provider = new LdapUserProvider($ldap, 'ou=MyBusiness,dc=symfony,dc=com', null, null, array(), 'sAMAccountName', '({uid_key}={username})'); $this->assertInstanceOf( 'Symfony\Component\Security\Core\User\User', $provider->loadUserByUsername('foo') ); } /** * @expectedException \Symfony\Component\Security\Core\Exception\InvalidArgumentException */ public function testLoadUserByUsernameFailsIfEntryHasNoPasswordAttribute() { $result = $this->getMockBuilder(CollectionInterface::class)->getMock(); $query = $this->getMockBuilder(QueryInterface::class)->getMock(); $query ->expects($this->once()) ->method('execute') ->will($this->returnValue($result)) ; $ldap = $this->getMockBuilder(LdapInterface::class)->getMock(); $result ->expects($this->once()) ->method('offsetGet') ->with(0) ->will($this->returnValue(new Entry('foo', array( 'sAMAccountName' => array('foo'), ) ))) ; $result ->expects($this->once()) ->method('count') ->will($this->returnValue(1)) ; $ldap ->expects($this->once()) ->method('escape') ->will($this->returnValue('foo')) ; $ldap ->expects($this->once()) ->method('query') ->will($this->returnValue($query)) ; $provider = new LdapUserProvider($ldap, 'ou=MyBusiness,dc=symfony,dc=com', null, null, array(), 'sAMAccountName', '({uid_key}={username})', 'userpassword'); $this->assertInstanceOf( 'Symfony\Component\Security\Core\User\User', $provider->loadUserByUsername('foo') ); } public function testLoadUserByUsernameIsSuccessfulWithoutPasswordAttribute() { $result = $this->getMockBuilder(CollectionInterface::class)->getMock(); $query = $this->getMockBuilder(QueryInterface::class)->getMock(); $query ->expects($this->once()) ->method('execute') ->will($this->returnValue($result)) ; $ldap = $this->getMockBuilder(LdapInterface::class)->getMock(); $result ->expects($this->once()) ->method('offsetGet') ->with(0) ->will($this->returnValue(new Entry('foo', array( 'sAMAccountName' => array('foo'), ) ))) ; $result ->expects($this->once()) ->method('count') ->will($this->returnValue(1)) ; $ldap ->expects($this->once()) ->method('escape') ->will($this->returnValue('foo')) ; $ldap ->expects($this->once()) ->method('query') ->will($this->returnValue($query)) ; $provider = new LdapUserProvider($ldap, 'ou=MyBusiness,dc=symfony,dc=com'); $this->assertInstanceOf( 'Symfony\Component\Security\Core\User\User', $provider->loadUserByUsername('foo') ); } public function testLoadUserByUsernameIsSuccessfulWithoutPasswordAttributeAndWrongCase() { $result = $this->getMockBuilder(CollectionInterface::class)->getMock(); $query = $this->getMockBuilder(QueryInterface::class)->getMock(); $query ->expects($this->once()) ->method('execute') ->will($this->returnValue($result)) ; $ldap = $this->getMockBuilder(LdapInterface::class)->getMock(); $result ->expects($this->once()) ->method('offsetGet') ->with(0) ->will($this->returnValue(new Entry('foo', array( 'sAMAccountName' => array('foo'), ) ))) ; $result ->expects($this->once()) ->method('count') ->will($this->returnValue(1)) ; $ldap ->expects($this->once()) ->method('escape') ->will($this->returnValue('Foo')) ; $ldap ->expects($this->once()) ->method('query') ->will($this->returnValue($query)) ; $provider = new LdapUserProvider($ldap, 'ou=MyBusiness,dc=symfony,dc=com'); $this->assertSame('foo', $provider->loadUserByUsername('Foo')->getUsername()); } public function testLoadUserByUsernameIsSuccessfulWithPasswordAttribute() { $result = $this->getMockBuilder(CollectionInterface::class)->getMock(); $query = $this->getMockBuilder(QueryInterface::class)->getMock(); $query ->expects($this->once()) ->method('execute') ->will($this->returnValue($result)) ; $ldap = $this->getMockBuilder(LdapInterface::class)->getMock(); $result ->expects($this->once()) ->method('offsetGet') ->with(0) ->will($this->returnValue(new Entry('foo', array( 'sAMAccountName' => array('foo'), 'userpassword' => array('bar'), ) ))) ; $result ->expects($this->once()) ->method('count') ->will($this->returnValue(1)) ; $ldap ->expects($this->once()) ->method('escape') ->will($this->returnValue('foo')) ; $ldap ->expects($this->once()) ->method('query') ->will($this->returnValue($query)) ; $provider = new LdapUserProvider($ldap, 'ou=MyBusiness,dc=symfony,dc=com', null, null, array(), 'sAMAccountName', '({uid_key}={username})', 'userpassword'); $this->assertInstanceOf( 'Symfony\Component\Security\Core\User\User', $provider->loadUserByUsername('foo') ); } } __halt_compiler();----SIGNATURE:----rUU5ucnxyEj3xSgUaGTdVa3irK/Ci4+Hb4jIUKedyxrD1m3nG3mYgAnrmULT78l6PsBO6SyvFuqdat5EPR4gXzOkzoUfCW6cACr+iPbSfsHe1ZbR5ws9QZDjohZ642aUvVi0AJQ2VVhWbrV7hJo2xv9wGoeyBJagqmRY1wrlgL7cedip/1ZBgpYW0W5Y7QcSynHk+UvebegtwTdSRjTDheEdMnFdRVLONVYeflgWzVC3sd9pGFcQh5czzovqjgJ9Zg0UdKbfN1qxIEKMF25KoiKWsUG7Ei0CB1L08Po69kKwt2z7BFeBEj2+iI881ZzmDfzIDsueknecW9jaVmvYILDQQuWZbcAm85CQ/cPMT8gDuORDE2QnUPjDZEk1ujIdng7zoDvrkeVGLFNu9kynX8IbMiVrYluVAo2VWy2towoS6cWLZfNkOdOzMgg754LsUXQAvP/WmnMqDYR8QfIhtsSxhoaj/85w/C+fwFNWxhBJItVbrrObKygCCymN6WCNX2bIF7z5ksDA8XFsG+cX0fcZFNAC8TTmkYfv4lyBlZIaORqfD5ZZDo8s+rW0RKZV7LeO8FKMtU87uSXMdlFzGlJWVLXkeeCgKYkZ9VYQEZ6VrB0oA07NClD4AabXVDuwf6Rua9BObxFiAxhCIe1NcKMoLJxC1pXBmJ6hqZFTJ4w=----ATTACHMENT:----NDUxMzA3MDg0MDQ5MjMzIDcwMTQ4MTI4MDgyMDUzOTkgNjc5Mzc3OTQxODcxMTgyOA==