Completed
Push — master ( 798ee0...2329f6 )
by Lucas
08:18
created

testAuthenticateTokenExpectingException()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 31
rs 8.8571
cc 1
eloc 22
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 Graviton\SecurityBundle\Entities\SecurityUser;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken;
12
13
/**
14
 * Class AirlockAuthenticationKeyAuthenticatorTest
15
 *
16
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
17
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
18
 * @link     http://swisscom.ch
19
 */
20
class SecurityAuthenticatorTest extends \PHPUnit_Framework_TestCase
21
{
22
    /** @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject logger */
23
    private $logger;
24
25
    /**
26
     * @return void
27
     */
28
    protected function setUp()
29
    {
30
        /** @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject logger */
31
        $this->logger = $this->getMockBuilder('\Psr\Log\LoggerInterface')
32
            ->setMethods(array('warning', 'info'))
33
            ->getMockForAbstractClass();
34
    }
35
36
37
    /**
38
     * @dataProvider stringProvider
39
     *
40
     * @param string $headerFieldValue value to check with
41
     *
42
     * @return void
43
     */
44
    public function testCreateToken($headerFieldValue)
45
    {
46
        $userProviderMock = $this
47
            ->getMockBuilder('Graviton\SecurityBundle\Authentication\Provider\AuthenticationProvider')
48
            ->disableOriginalConstructor()
49
            ->setMethods(array('loadUserByUsername'))
50
            ->getMock();
51
52
        $strategy = $this->getMockBuilder('\Graviton\SecurityBundle\Authentication\Strategies\StrategyInterface')
53
            ->setMethods(array('apply'))
54
            ->getMockForAbstractClass();
55
        $strategy
56
            ->expects($this->once())
57
            ->method('apply')
58
            ->will($this->returnValue($headerFieldValue));
59
60
        $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...
61
62
        $server = array(
63
            'HTTP_X_IDP_USERNAME' => $headerFieldValue, //"example-authentication-header",
64
        );
65
66
        $request = new Request(array(), array(), array(), array(), array(), $server);
67
68
        $token = $authenticator->createToken($request, 'AirlockProviderKey');
69
70
        $this->assertInstanceOf(
71
            '\Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken',
72
            $token
73
        );
74
75
        $this->assertFalse($token->isAuthenticated());
76
    }
77
78
    /**
79
     * @return array<string>
80
     */
81 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...
82
    {
83
        return array(
84
            'plain string, no special chars' => array('exampleAuthenticationHeader'),
85
            'string with special chars' => array("$-_.+!*'(),{}|\\^~[]`<>#%;/?:@&=."),
86
            'string with octal chars' => array("a: \141, A: \101"),
87
            'string with hex chars' => array("a: \x61, A: \x41"),
88
            'live example' => array("10N0000188"),
89
        );
90
    }
91
92
    /**
93
     * @return void
94
     */
95
    public function testAuthenticateToken()
96
    {
97
        $providerKey = 'some providerKey';
98
        $apiKey = 'exampleAuthenticationHeader';
99
100
        $securityUserMock =  $this->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface')
101
            ->setMethods(array('getRoles'))
102
            ->getMockForAbstractClass();
103
        $securityUserMock
104
            ->expects($this->never())
105
            ->method('getRoles')
106
            ->will($this->returnValue(array(SecurityUser::ROLE_USER)));
107
108
        $userProviderMock = $this->getProviderMock(array('loadUserByUsername'));
109
        $userProviderMock
110
            ->expects($this->once())
111
            ->method('loadUserByUsername')
112
            ->will($this->returnValue($securityUserMock));
113
114
        $anonymousToken = new PreAuthenticatedToken(
115
            'anon.',
116
            $apiKey,
117
            $providerKey
118
        );
119
120
        $authenticator = new SecurityAuthenticator(
121
            true,
122
            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...
123
            true,
124
            $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...
125
            $this->getStrategyMock(),
126
            $this->logger
127
        );
128
129
        $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...
130
131
        $this->assertInstanceOf(
132
            '\Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken',
133
            $token
134
        );
135
136
        $this->assertTrue($token->isAuthenticated());
137
    }
138
139
    /**
140
     * @return void
141
     */
142
    public function testAuthenticateTokenExpectingException()
143
    {
144
        $providerKey = 'some providerKey';
145
        $apiKey = 'exampleAuthenticationHeader';
146
147
        $userProviderMock = $this->getProviderMock(array('loadUserByUsername'));
148
        $userProviderMock
149
            ->expects($this->once())
150
            ->method('loadUserByUsername')
151
            ->with($this->equalTo($apiKey))
152
            ->will($this->returnValue(false));
153
154
        $anonymousToken = new PreAuthenticatedToken(
155
            'anon.',
156
            $apiKey,
157
            $providerKey
158
        );
159
160
        $authenticator = new SecurityAuthenticator(
161
            true,
162
            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...
163
            false,
164
            $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...
165
            $this->getStrategyMock(),
166
            $this->logger
167
        );
168
169
        $this->setExpectedException('\Symfony\Component\Security\Core\Exception\AuthenticationException');
170
171
        $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...
172
    }
173
174
    /**
175
     * @return void
176
     */
177
    public function testSupportsToken()
178
    {
179
        $providerKey = 'some providerKey';
180
        $apiKey = 'exampleAuthenticationHeader';
181
182
        $anonymousToken = new PreAuthenticatedToken(
183
            'anon.',
184
            $apiKey,
185
            $providerKey
186
        );
187
188
        $authenticator = new SecurityAuthenticator(
189
            true,
190
            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...
191
            true,
192
            $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...
193
            $this->getStrategyMock(),
194
            $this->logger
195
        );
196
197
        $this->assertTrue($authenticator->supportsToken($anonymousToken, $providerKey));
198
    }
199
200
    /**
201
     * @return void
202
     */
203
    public function testOnAuthenticationFailure()
204
    {
205
        $exceptionDouble = $this->getMockBuilder('\Symfony\Component\Security\Core\Exception\AuthenticationException')
206
            ->disableOriginalConstructor()
207
            ->setMethods(array('getMessageKey'))
208
            ->getMock();
209
        $exceptionDouble
210
            ->expects($this->once())
211
            ->method('getMessageKey')
212
            ->will($this->returnValue('test_message'));
213
214
        $authenticator = new SecurityAuthenticator(
215
            true,
216
            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...
217
            true,
218
            $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...
219
            $this->getStrategyMock(),
220
            $this->logger
221
        );
222
223
        $response = $authenticator->onAuthenticationFailure(new Request(), $exceptionDouble);
224
225
        $this->assertEquals('test_message', $response->getContent());
226
        $this->assertEquals(511, $response->getStatusCode());
227
    }
228
229
    /**
230
     * @param string[] $methods methods to mock
231
     *
232
     * @return \PHPUnit_Framework_MockObject_MockObject|SecurityAuthenticator
233
     */
234
    private function getProviderMock(array $methods = array())
235
    {
236
        $userProviderMock = $this
237
            ->getMockBuilder('Graviton\SecurityBundle\Authentication\Provider\AuthenticationProvider')
238
            ->disableOriginalConstructor()
239
            ->setMethods($methods)
240
            ->getMock();
241
        return $userProviderMock;
242
    }
243
244
    /**
245
     * @param array $methods methods to mock
246
     *
247
     * @return StrategyInterface|\PHPUnit_Framework_MockObject_MockObject
248
     */
249
    private function getStrategyMock(array $methods = array('apply'))
250
    {
251
        return $this->getMockBuilder('\Graviton\SecurityBundle\Authentication\Strategies\StrategyInterface')
252
            ->setMethods($methods)
253
            ->getMockForAbstractClass();
254
    }
255
}
256