CasTest.php 6.14 KB
Newer Older
1
2
3
4
5
6
7
8
9
<?php
namespace UnicaenAuthTest\Authentication\Adapter;

use CAS_GracefullTerminationException;
use PHPUnit_Framework_TestCase;
use UnicaenApp\Exception;
use UnicaenAuth\Authentication\Adapter\Cas;
use Zend\EventManager\EventManager;
use ZfcUser\Authentication\Adapter\AdapterChainEvent;
10
use Zend\Authentication\Result;
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

define ('__VENDOR_DIR__', __DIR__ . '/../../../../vendor');

require_once __VENDOR_DIR__ . '/gorg/phpcas/CAS.php';
        
/**
 * Description of CasTest
 *
 * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
 */
class CasTest extends PHPUnit_Framework_TestCase
{
    protected $adapter;
    protected $moduleOptions;
    
    /**
     * Sets up the fixture, for example, open a network connection.
     * This method is called before a test is executed.
     */
    protected function setUp()
    {
        $this->moduleOptions = $moduleOptions = new \UnicaenAuth\Options\ModuleOptions(array(
            'cas' => array(
                'connection' => array(
                    'default' => array(
                        'params' => array(
                            'hostname' => 'cas.unicaen.fr',
                            'port' => 443,
                            'version' => "2.0",
                            'uri' => "",
                            'debug' => false,
                        ),
                    ),
                ),
            ),
        ));
        
        $serviceManager = $this->getMock('Zend\ServiceManager\ServiceManager', array('get'));
        $serviceManager->expects($this->any())
                       ->method('get')
                       ->will($this->returnCallback(function($serviceName) use ($moduleOptions) {
                           if ('zfcuser_module_options' === $serviceName) {
                               return new \ZfcUser\Options\ModuleOptions();
                           }
                           if ('unicaen-auth_module_options' === $serviceName) {
                               return $moduleOptions;
                           }
                           if ('router' === $serviceName) {
                               $router = new \Zend\Mvc\Router\Http\TreeRouteStack();
                               $router->setBaseUrl('/appli')->setRequestUri(new \Zend\Uri\Http('/request'));
                               return $router;
                           }
                           return null;
                       }));
                       
        $this->adapter = new Cas();
        $this->adapter->setServiceManager($serviceManager)
                      ->setEventManager(new EventManager());
    }
    
    public function getInvalidCasOptions()
    {
        return array(
            array(array('other' => array())),
            array(array('connection' => array())),
            array(array('connection' => array('default'=> array()))),
            array(array('connection' => array('default'=> array('params' => array())))),
        );
    }
    
    /**
     * @dataProvider getInvalidCasOptions
     * @expectedException Exception
     */
    public function testThrowsExceptionIfNoCasParamSpecified($config)
    {
        $this->moduleOptions->setCas($config);
        $this->adapter->authenticate(new AdapterChainEvent());
    }
    
    public function testAuthenticateReturnsNullIfNoCasConfigSpecified()
    {
        $this->moduleOptions->setCas(array());
        $result = $this->adapter->authenticate(new AdapterChainEvent());
        $this->assertNull($result);
    }
    
    public function testCanRedirectToCasIfNotAuthenticated()
    {
        CAS_GracefullTerminationException::throwInsteadOfExiting();
        
        ob_start();
        try {
            $result = $this->adapter->authenticate(new AdapterChainEvent());
            $this->fail("Exception CAS_GracefullTerminationException non levée.");
        }
        catch (CAS_GracefullTerminationException $e) {
            
        }
        $result = ob_get_clean();
        
        $expected = <<<EOS
<html><head><title>CAS Authentication wanted!</title></head><body><h1>CAS Authentication wanted!</h1><p>You should already have been redirected to the CAS server. Click <a href="https://cas.unicaen.fr/login?service=http%3A%2F%2F%3A">here</a> to continue.</p><hr><address>phpCAS 1.3.1+ using server <a href="https://cas.unicaen.fr/">https://cas.unicaen.fr/</a> (CAS 2.0)</a></address></body></html>
EOS;
        $this->assertEquals($expected, $result);
    }
    
    public function testAuthenticateReturnsTrueWhenAuthenticationSucceeds()
    {
        $casClient = $this->getMock('phpCAS', array('client', 'forceAuthentication', 'getUser'));
        $casClient->staticExpects($this->once())
                  ->method('getUser')
                  ->will($this->returnValue($username = 'username'));
        
        $this->adapter->setCasClient($casClient);
        
        $event = new AdapterChainEvent();
        
        $result = $this->adapter->authenticate($event);
        
        $this->assertTrue($result);
        $this->assertTrue($this->adapter->isSatisfied());
        $this->assertEquals(array('is_satisfied' => true, 'identity' => $username), $this->adapter->getStorage()->read());
        
        $this->assertEquals("userAuthenticated", $event->getName());
136
137
        $this->assertEquals(Result::SUCCESS, $event->getCode());
        $this->assertEquals($username, $event->getIdentity());
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
        $this->assertTrue($event->propagationIsStopped());
    }
    
    public function testLogoutReturnsNullIfNoCasConfigSpecified()
    {
        $this->moduleOptions->setCas(array());
        $result = $this->adapter->logout(new AdapterChainEvent());
        $this->assertNull($result);
    }
    
    public function testCanLogoutFromCasWithRedirectService()
    {
        $casClient = $this->getMock('phpCAS', array('client', 'isAuthenticated', 'logoutWithRedirectService'));
        $casClient->staticExpects($this->once())
                  ->method('isAuthenticated')
                  ->will($this->returnValue(true));
        $casClient->staticExpects($this->once())
                  ->method('logoutWithRedirectService');
        
        $this->adapter->setCasClient($casClient);
        
        $this->adapter->logout(new AdapterChainEvent());
    }
}