LdapTest.php 8.16 KB
Newer Older
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
<?php
namespace UnicaenAuthTest\Authentication\Adapter;

use PHPUnit_Framework_TestCase;
use UnicaenAuth\Authentication\Adapter\Ldap;
use Zend\Authentication\Result;
use Zend\EventManager\EventManager;
use Zend\Http\Request;
use Zend\Stdlib\Parameters;
use ZfcUser\Authentication\Adapter\AdapterChainEvent;

/**
 * Description of LdapTest
 *
 * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
 */
class LdapTest extends PHPUnit_Framework_TestCase
{
    protected $adapter;
    protected $appModuleOptions;
    protected $authModuleOptions;
    protected $mapper;
    
    /**
     * Sets up the fixture, for example, open a network connection.
     * This method is called before a test is executed.
     */
    protected function setUp()
    {
        $this->appModuleOptions = $appModuleOptions = new \UnicaenApp\Options\ModuleOptions(array(
            'ldap' => array(
                'connection' => array(
                    'default' => array(
                        'params' => array(
                            'host'                => 'host.domain.fr',
                            'username'            => "uid=xxxxxxxxx,ou=xxxxxxxxxx,dc=domain,dc=fr",
                            'password'            => "xxxxxxxxxxxx",
                            'baseDn'              => "ou=xxxxxxxxxxx,dc=domain,dc=fr",
                            'bindRequiresDn'      => true,
                            'accountFilterFormat' => "(&(objectClass=posixAccount)(supannAliasLogin=%s))",
                        )
                    )
                )
            ),
        ));
        $this->authModuleOptions = $authModuleOptions = new \UnicaenAuth\Options\ModuleOptions(array(
            'usurpation_allowed_usernames' => array('usurpateur'),
        ));
        
        $this->mapper = $mapper = $this->getMock('ZfcUser\Mapper\User', array('findByUsername', 'findByEmail'));
        
        $serviceManager = $this->getMock('Zend\ServiceManager\ServiceManager', array('get'));
        $serviceManager->expects($this->any())
                       ->method('get')
                       ->will($this->returnCallback(function($serviceName) use ($authModuleOptions, $appModuleOptions, $mapper) {
                           if ('zfcuser_module_options' === $serviceName) {
                               return new \ZfcUser\Options\ModuleOptions();
                           }
                           if ('unicaen-app_module_options' === $serviceName) {
                               return $appModuleOptions;
                           }
                           if ('unicaen-auth_module_options' === $serviceName) {
                               return $authModuleOptions;
                           }
                           if ('zfcuser_user_mapper' === $serviceName) {
                               return $mapper;
                           }
                           return null;
                       }));
                       
        $this->adapter = new Ldap();
        $this->adapter->setServiceManager($serviceManager)
                      ->setEventManager(new EventManager());
    }
    
    public function testCanProvideDefaultLdapAuthAdapter()
    {
        $adapter = $this->adapter->getLdapAuthAdapter();
        $this->assertInstanceOf('Zend\Authentication\Adapter\Ldap', $adapter);
        
        $appModuleLdapOptions = $this->appModuleOptions->getLdap();
        $connectionNames = array_keys($appModuleLdapOptions['connection']);
        $connectionParams = array_map(function($connection) { return $connection['params']; }, $appModuleLdapOptions['connection']);
        $this->assertEquals(array_combine($connectionNames, $connectionParams), $adapter->getOptions());
    }
    
87
88
89
90
91
92
93
94
    public function testAuthenticatingReturnsNullIfAlreadyStatisfied()
    {
        $event = new AdapterChainEvent();
        $this->adapter->setSatisfied();
        $this->assertNull($this->adapter->authenticate($event));
        $this->assertEquals($event->getCode(), Result::SUCCESS);
    }
    
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
    public function testUsurpationWithAllowedUsernameAndSuccessfulAuthentication()
    {
        $this->authModuleOptions->setUsurpationAllowedUsernames(array('usurpateur'));
        $event = new AdapterChainEvent();
        $result = $this->_authenticateWithUsurpation(Result::SUCCESS, $event);
        
        $this->assertTrue($result);
        $this->assertTrue($this->adapter->isSatisfied());
        $this->assertEquals(array('is_satisfied' => true, 'identity' => 'usurpe'), $this->adapter->getStorage()->read());
        
        $this->assertEquals("userAuthenticated", $event->getName());
        $this->assertEquals(Result::SUCCESS, $event->getCode());
        $this->assertEquals('usurpe', $event->getIdentity());
        $this->assertTrue($event->propagationIsStopped());
    }
    
    public function testUsurpationWithAllowedUsernameAndUnsuccessfulAuthentication()
    {
        $this->authModuleOptions->setUsurpationAllowedUsernames(array('usurpateur'));
        $event = new AdapterChainEvent();
        $result = $this->_authenticateWithUsurpation(Result::FAILURE, $event);
        
        $this->assertFalse($result);
        $this->assertFalse($this->adapter->isSatisfied());
        $this->assertEquals(array('is_satisfied' => false), $this->adapter->getStorage()->read());
        
        $this->assertNull($event->getName());
        $this->assertEquals(Result::FAILURE, $event->getCode());
        $this->assertNull($event->getIdentity());
        $this->assertFalse($event->propagationIsStopped());
    }
    
    public function testUsurpationWithNotAllowedUsernameAndSuccessfulAuthentication()
    {
        $this->authModuleOptions->setUsurpationAllowedUsernames(array());
        $event = new AdapterChainEvent();
        $result = $this->_authenticateWithUsurpation(Result::SUCCESS, $event);
        
        $this->assertTrue($result);
        $this->assertTrue($this->adapter->isSatisfied());
        $this->assertEquals(array('is_satisfied' => true, 'identity' => 'usurpateur'), $this->adapter->getStorage()->read());
        
        $this->assertEquals("userAuthenticated", $event->getName());
        $this->assertTrue($event->propagationIsStopped());
        $this->assertEquals('usurpateur', $event->getIdentity());
    }
    
    public function testUsurpationWithNotAllowedUsernameAndUnsuccessfulAuthentication()
    {
        $this->authModuleOptions->setUsurpationAllowedUsernames(array());
        $event = new AdapterChainEvent();
        $result = $this->_authenticateWithUsurpation(Result::FAILURE, $event);
        
        $this->assertFalse($result);
        $this->assertFalse($this->adapter->isSatisfied());
        $this->assertEquals(array('is_satisfied' => false), $this->adapter->getStorage()->read());
        
        $this->assertNull($event->getName());
        $this->assertEquals(Result::FAILURE, $event->getCode());
        $this->assertNull($event->getIdentity());
        $this->assertFalse($event->propagationIsStopped());
    }
    
    protected function _authenticateWithUsurpation($authenticationResultCode, AdapterChainEvent &$event)
    { 
        $usernameUsurpateur = 'usurpateur';
        $usernameUsurpe     = 'usurpe';
        $username           = $usernameUsurpateur . Ldap::USURPATION_USERNAMES_SEP . $usernameUsurpe;
        
        $ldapAuthAdapter = $this->getMock('Zend\Authentication\Adapter\Ldap', array('setUsername', 'setPassword', 'authenticate'));
        $ldapAuthAdapter->expects($this->once())
                        ->method('setUsername')
                        ->with($usernameUsurpateur)
                        ->will($this->returnSelf());
        $ldapAuthAdapter->expects($this->once())
                        ->method('setPassword')
                        ->will($this->returnSelf());
        $ldapAuthAdapter->expects($this->once())
                        ->method('authenticate')
                        ->will($this->returnValue(new Result($authenticationResultCode, $usernameUsurpateur)));
        $this->adapter->setLdapAuthAdapter($ldapAuthAdapter);
        
        $request = new Request();
        $request->setPost(new Parameters(array('identity' => $username, 'credential' => "xxxxx")));
        $event->setRequest($request);
        
        return $this->adapter->authenticate($event);
    }
}