Completed
Push — feature/evo-2472-whoami ( 39cc08...5611b4 )
by Jan
14:59
created

SecurityAuthenticatorTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
/**
3
 * main checks for airlock authenticator
4
 */
5
6
namespace Graviton\SecurityBundle\Authentication;
7
8
use Graviton\SecurityBundle\Authentication\Strategies\StrategyInterface;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken;
11
12
/**
13
 * Class AirlockAuthenticationKeyAuthenticatorTest
14
 *
15
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
16
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
17
 * @link     http://swisscom.ch
18
 */
19
class SecurityAuthenticatorTest extends \PHPUnit_Framework_TestCase
20
{
21
    /** @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject logger */
22
    private $logger;
23
24
    /**
25
     * @return void
26
     */
27
    protected function setUp()
28
    {
29
        /** @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject logger */
30
        $this->logger = $this->getMockBuilder('\Psr\Log\LoggerInterface')
31
            ->setMethods(array('warning', 'info'))
32
            ->getMockForAbstractClass();
33
    }
34
35
36
    /**
37
     * @dataProvider stringProvider
38
     *
39
     * @param string $headerFieldValue value to check with
40
     *
41
     * @return void
42
     */
43
    public function testCreateToken($headerFieldValue)
44
    {
45
        $userProviderMock = $this->getMockBuilder('Graviton\SecurityBundle\Authentication\Provider\AuthenticationProvider')
46
            ->disableOriginalConstructor()
47
            ->setMethods(array('loadUserByUsername'))
48
            ->getMock();
49
50
        $strategy = $this->getMockBuilder('\Graviton\SecurityBundle\Authentication\Strategies\StrategyInterface')
51
            ->setMethods(array('apply'))
52
            ->getMockForAbstractClass();
53
        $strategy
54
            ->expects($this->once())
55
            ->method('apply')
56
            ->will($this->returnValue($headerFieldValue));
57
58
        $authenticator = new SecurityAuthenticator(true, true, true, $userProviderMock, $strategy, $this->logger);
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
59
60
        $server = array(
61
            'HTTP_X_IDP_USERNAME' => $headerFieldValue, //"example-authentication-header",
62
        );
63
64
        $request = new Request(array(), array(), array(), array(), array(), $server);
65
66
        $token = $authenticator->createToken($request, 'AirlockProviderKey');
67
68
        $this->assertInstanceOf(
69
            '\Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken',
70
            $token
71
        );
72
73
        $this->assertFalse($token->isAuthenticated());
74
    }
75
76
    /**
77
     * @return array<string>
78
     */
79 View Code Duplication
    public function stringProvider()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
80
    {
81
        return array(
82
            'plain string, no special chars' => array('exampleAuthenticationHeader'),
83
            'string with special chars' => array("$-_.+!*'(),{}|\\^~[]`<>#%;/?:@&=."),
84
            'string with octal chars' => array("a: \141, A: \101"),
85
            'string with hex chars' => array("a: \x61, A: \x41"),
86
            'live example' => array("10N0000188"),
87
        );
88
    }
89
90
    /**
91
     * @return void
92
     */
93
    public function testAuthenticateToken()
94
    {
95
        $providerKey = 'some providerKey';
96
        $apiKey = 'exampleAuthenticationHeader';
97
98
        $securityUserMock =  $this->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface')
99
            ->setMethods(array('getRoles'))
100
            ->getMockForAbstractClass();
101
        $securityUserMock
102
            ->expects($this->once())
103
            ->method('getRoles')
104
            ->will($this->returnValue(array('ROLE_GRAVITON_USER')));
105
106
        $userProviderMock = $this->getProviderMock(array('loadUserByUsername'));
107
        $userProviderMock
108
            ->expects($this->once())
109
            ->method('loadUserByUsername')
110
            ->will($this->returnValue($securityUserMock));
111
112
        $anonymousToken = new PreAuthenticatedToken(
113
            'anon.',
114
            $apiKey,
115
            $providerKey
116
        );
117
118
        $authenticator = new SecurityAuthenticator( true, true, true,
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
119
            $userProviderMock,
0 ignored issues
show
Documentation introduced by
$userProviderMock is of type object<PHPUnit_Framework...\SecurityAuthenticator>, but the function expects a object<Graviton\Security...AuthenticationProvider>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
120
            $this->getStrategyMock(),
121
            $this->logger
122
        );
123
124
        $token = $authenticator->authenticateToken($anonymousToken, $userProviderMock, $providerKey);
0 ignored issues
show
Documentation introduced by
$userProviderMock is of type object<PHPUnit_Framework...\SecurityAuthenticator>, but the function expects a object<Symfony\Component...\UserProviderInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
125
126
        $this->assertInstanceOf(
127
            '\Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken',
128
            $token
129
        );
130
131
        $this->assertTrue($token->isAuthenticated());
132
    }
133
134
    /**
135
     * @return void
136
     */
137
    public function testAuthenticateTokenExpectingException()
138
    {
139
        $providerKey = 'some providerKey';
140
        $apiKey = 'exampleAuthenticationHeader';
141
142
        $userProviderMock = $this->getProviderMock(array('loadUserByUsername'));
143
        $userProviderMock
144
            ->expects($this->once())
145
            ->method('loadUserByUsername')
146
            ->with($this->equalTo($apiKey))
147
            ->will($this->returnValue(false));
148
149
        $anonymousToken = new PreAuthenticatedToken(
150
            'anon.',
151
            $apiKey,
152
            $providerKey
153
        );
154
155
        $authenticator = new SecurityAuthenticator( true, false, false,
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
156
            $userProviderMock,
0 ignored issues
show
Documentation introduced by
$userProviderMock is of type object<PHPUnit_Framework...\SecurityAuthenticator>, but the function expects a object<Graviton\Security...AuthenticationProvider>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
157
            $this->getStrategyMock(),
158
            $this->logger
159
        );
160
161
        $this->setExpectedException('\Symfony\Component\Security\Core\Exception\AuthenticationException');
162
163
        $authenticator->authenticateToken($anonymousToken, $userProviderMock, $providerKey);
0 ignored issues
show
Documentation introduced by
$userProviderMock is of type object<PHPUnit_Framework...\SecurityAuthenticator>, but the function expects a object<Symfony\Component...\UserProviderInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
164
    }
165
166
    /**
167
     * @return void
168
     */
169
    public function testSupportsToken()
170
    {
171
        $providerKey = 'some providerKey';
172
        $apiKey = 'exampleAuthenticationHeader';
173
174
        $anonymousToken = new PreAuthenticatedToken(
175
            'anon.',
176
            $apiKey,
177
            $providerKey
178
        );
179
180
        $authenticator = new SecurityAuthenticator( true, true, true,
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
181
            $this->getProviderMock(),
0 ignored issues
show
Documentation introduced by
$this->getProviderMock() is of type object<PHPUnit_Framework...\SecurityAuthenticator>, but the function expects a object<Graviton\Security...AuthenticationProvider>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
182
            $this->getStrategyMock(),
183
            $this->logger
184
        );
185
186
        $this->assertTrue($authenticator->supportsToken($anonymousToken, $providerKey));
187
    }
188
189
    /**
190
     * @return void
191
     */
192
    public function testOnAuthenticationFailure()
193
    {
194
        $exceptionDouble = $this->getMockBuilder('\Symfony\Component\Security\Core\Exception\AuthenticationException')
195
            ->disableOriginalConstructor()
196
            ->setMethods(array('getMessageKey'))
197
            ->getMock();
198
        $exceptionDouble
199
            ->expects($this->once())
200
            ->method('getMessageKey')
201
            ->will($this->returnValue('test_message'));
202
203
        $authenticator = new SecurityAuthenticator( true, true, true,
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
204
            $this->getProviderMock(),
0 ignored issues
show
Documentation introduced by
$this->getProviderMock() is of type object<PHPUnit_Framework...\SecurityAuthenticator>, but the function expects a object<Graviton\Security...AuthenticationProvider>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
205
            $this->getStrategyMock(),
206
            $this->logger
207
        );
208
209
        $response = $authenticator->onAuthenticationFailure(new Request(), $exceptionDouble);
210
211
        $this->assertEquals('test_message', $response->getContent());
212
        $this->assertEquals(511, $response->getStatusCode());
213
    }
214
215
    /**
216
     * @param string[] $methods methods to mock
217
     *
218
     * @return \PHPUnit_Framework_MockObject_MockObject|SecurityAuthenticator
219
     */
220
    private function getProviderMock(array $methods = array())
221
    {
222
        $userProviderMock = $this->getMockBuilder('Graviton\SecurityBundle\Authentication\Provider\AuthenticationProvider')
223
            ->disableOriginalConstructor()
224
            ->setMethods($methods)
225
            ->getMock();
226
        return $userProviderMock;
227
    }
228
229
    /**
230
     * @param array $methods methods to mock
231
     *
232
     * @return StrategyInterface|\PHPUnit_Framework_MockObject_MockObject
233
     */
234
    private function getStrategyMock(array $methods = array('apply'))
235
    {
236
        return $this->getMockBuilder('\Graviton\SecurityBundle\Authentication\Strategies\StrategyInterface')
237
            ->setMethods($methods)
238
            ->getMockForAbstractClass();
239
    }
240
}
241