|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace casino\Tests\engine\ServiceRouter\Plugin\Authentication; |
|
4
|
|
|
|
|
5
|
|
|
use \PHPUnit_Framework_TestCase; |
|
6
|
|
|
use Vectorface\SnappyRouter\Authentication\CallbackAuthenticator; |
|
7
|
|
|
use Vectorface\SnappyRouter\Di\Di; |
|
8
|
|
|
use Vectorface\SnappyRouter\Exception\InternalErrorException; |
|
9
|
|
|
use Vectorface\SnappyRouter\Exception\UnauthorizedException; |
|
10
|
|
|
use Vectorface\SnappyRouter\Handler\ControllerHandler; |
|
11
|
|
|
use Vectorface\SnappyRouter\Plugin\Authentication\HttpBasicAuthenticationPlugin; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Test for the HttpBasicAuthenticationPlugin class. |
|
15
|
|
|
* @copyright Copyright (c) 2014, VectorFace, Inc. |
|
16
|
|
|
* @author J. Anderson <[email protected]> |
|
17
|
|
|
* @author Dan Bruce <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class HttpBasicAuthenticationPluginTest extends PHPUnit_Framework_TestCase |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Test the HTTPBasicAuthenticationPlugin; All in one test! |
|
23
|
|
|
*/ |
|
24
|
|
|
public function testBasicHTTPAuth() |
|
|
|
|
|
|
25
|
|
|
{ |
|
26
|
|
|
$ignored = new ControllerHandler(array()); |
|
27
|
|
|
|
|
28
|
|
|
/* Configure DI */ |
|
29
|
|
|
$di = new Di(array('MyCustomAuth' => false)); |
|
30
|
|
|
Di::setDefault($di); |
|
31
|
|
|
|
|
32
|
|
|
/* Direct testing. */ |
|
33
|
|
|
$plugin = new HttpBasicAuthenticationPlugin(array( |
|
34
|
|
|
'AuthMechanism' => 'MyCustomAuth', |
|
35
|
|
|
'realm' => 'Authentication Test' |
|
36
|
|
|
)); |
|
37
|
|
|
|
|
38
|
|
|
try { |
|
39
|
|
|
$plugin->afterHandlerSelected($ignored); |
|
40
|
|
|
$this->fail("An invalid authenticator should yield an internal error"); |
|
41
|
|
|
} catch (InternalErrorException $e) { |
|
42
|
|
|
$this->assertEquals(500, $e->getAssociatedStatusCode()); /* HTTP 500 ISE */ |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/* From here on out, use the "Do whatever I say" authenticator. :) */ |
|
46
|
|
|
$bool = false; |
|
47
|
|
|
$auth = new CallbackAuthenticator(function () use (&$bool) { |
|
48
|
|
|
return $bool; |
|
49
|
|
|
}); |
|
50
|
|
|
$di->set('MyCustomAuth', $auth); |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
$_SERVER['PHP_AUTH_USER'] = $_SERVER['PHP_AUTH_PW'] = null; |
|
54
|
|
|
try { |
|
55
|
|
|
$plugin->afterHandlerSelected($ignored); |
|
56
|
|
|
$this->fail("No username and password are available. UnauthorizedException expected."); |
|
57
|
|
|
} catch (UnauthorizedException $e) { |
|
58
|
|
|
$this->assertEquals(401, $e->getAssociatedStatusCode()); /* HTTP 401 Unauthorized */ |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$_SERVER['PHP_AUTH_USER'] = $_SERVER['PHP_AUTH_PW'] = 'ignored'; |
|
62
|
|
|
try { |
|
63
|
|
|
$plugin->afterHandlerSelected($ignored); |
|
64
|
|
|
$this->fail("Callback expected to return false auth result. UnauthorizedException expected."); |
|
65
|
|
|
} catch (UnauthorizedException $e) { |
|
66
|
|
|
// we expect the exception to be thrown |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/* With a true result, preInvoke should pass through. */ |
|
70
|
|
|
$bool = true; |
|
71
|
|
|
$this->assertTrue($bool); |
|
72
|
|
|
$plugin->afterHandlerSelected($ignored); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: