1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com) |
4
|
|
|
* |
5
|
|
|
* Licensed under The MIT License |
6
|
|
|
* Redistributions of files must retain the above copyright notice. |
7
|
|
|
* |
8
|
|
|
* @copyright Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com) |
9
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace CakeDC\Api\Test\TestCase\Auth\Authenticate; |
13
|
|
|
|
14
|
|
|
use CakeDC\Api\Service\Action\CrudIndexAction; |
15
|
|
|
use CakeDC\Api\Service\Auth\Authenticate\TokenAuthenticate; |
16
|
|
|
use CakeDC\Api\Service\FallbackService; |
17
|
|
|
use CakeDC\Api\TestSuite\TestCase; |
18
|
|
|
use CakeDC\Api\Test\ConfigTrait; |
19
|
|
|
use CakeDC\Api\Test\FixturesTrait; |
20
|
|
|
use Cake\Http\Response; |
21
|
|
|
use Cake\Http\ServerRequest; |
22
|
|
|
|
23
|
|
|
class TokenAuthenticateTest extends TestCase |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
use ConfigTrait; |
27
|
|
|
use FixturesTrait; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var TokenAuthenticate |
31
|
|
|
*/ |
32
|
|
|
public $token; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Sets up the fixture, for example, opens a network connection. |
36
|
|
|
* This method is called before a test is executed. |
37
|
|
|
*/ |
38
|
|
|
public function setUp() |
39
|
|
|
{ |
40
|
|
|
$request = new ServerRequest(); |
41
|
|
|
$response = new Response(); |
42
|
|
|
$service = new FallbackService([ |
43
|
|
|
'request' => $request, |
44
|
|
|
'response' => $response |
45
|
|
|
]); |
46
|
|
|
$action = new CrudIndexAction([ |
47
|
|
|
'service' => $service, |
48
|
|
|
'request' => $request, |
49
|
|
|
'response' => $response |
50
|
|
|
]); |
51
|
|
|
$this->token = new TokenAuthenticate($action, ['require_ssl' => false]); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Tears down the fixture, for example, closes a network connection. |
56
|
|
|
* This method is called after a test is executed. |
57
|
|
|
*/ |
58
|
|
|
public function tearDown() |
59
|
|
|
{ |
60
|
|
|
unset($this->token, $this->controller); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* test |
65
|
|
|
* |
66
|
|
|
* @return void |
67
|
|
|
*/ |
68
|
|
|
public function testAuthenticateHappy() |
69
|
|
|
{ |
70
|
|
|
$request = new ServerRequest('/?token=yyy'); |
71
|
|
|
$result = $this->token->authenticate($request, new Response()); |
72
|
|
|
$this->assertEquals('user-1', $result['username']); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* test |
77
|
|
|
* |
78
|
|
|
* @return void |
79
|
|
|
*/ |
80
|
|
|
public function testAuthenticateFail() |
81
|
|
|
{ |
82
|
|
|
$request = new ServerRequest('/'); |
83
|
|
|
$result = $this->token->authenticate($request, new Response()); |
84
|
|
|
$this->assertFalse($result); |
85
|
|
|
|
86
|
|
|
$request = new ServerRequest('/?token=none'); |
87
|
|
|
$result = $this->token->authenticate($request, new Response()); |
88
|
|
|
$this->assertFalse($result); |
89
|
|
|
|
90
|
|
|
$request = new ServerRequest('/?token='); |
91
|
|
|
$result = $this->token->authenticate($request, new Response()); |
92
|
|
|
$this->assertFalse($result); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* test |
97
|
|
|
* |
98
|
|
|
* @expectedException \OutOfBoundsException |
99
|
|
|
* @expectedExceptionMessage Type wrong is not valid |
100
|
|
|
* |
101
|
|
|
*/ |
102
|
|
|
public function testAuthenticateWrongType() |
103
|
|
|
{ |
104
|
|
|
$this->token->setConfig('type', 'wrong'); |
105
|
|
|
$request = new ServerRequest('/'); |
106
|
|
|
$this->token->authenticate($request, new Response()); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* test |
111
|
|
|
* |
112
|
|
|
* @expectedException \Cake\Http\Exception\ForbiddenException |
113
|
|
|
* @expectedExceptionMessage SSL is required for ApiKey Authentication |
114
|
|
|
* |
115
|
|
|
*/ |
116
|
|
|
public function testAuthenticateRequireSSL() |
117
|
|
|
{ |
118
|
|
|
$this->token->setConfig('require_ssl', true); |
119
|
|
|
$request = new ServerRequest('/?token=test'); |
120
|
|
|
$this->token->authenticate($request, new Response()); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* test |
125
|
|
|
* |
126
|
|
|
*/ |
127
|
|
|
public function testAuthenticateRequireSSLNoKey() |
128
|
|
|
{ |
129
|
|
|
$this->token->setConfig('require_ssl', true); |
130
|
|
|
$request = new ServerRequest('/'); |
131
|
|
|
$this->assertFalse($this->token->authenticate($request, new Response())); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* test |
136
|
|
|
* |
137
|
|
|
* @return void |
138
|
|
|
*/ |
139
|
|
|
public function testHeaderHappy() |
140
|
|
|
{ |
141
|
|
|
$request = $this->getMockBuilder('\Cake\Http\ServerRequest') |
142
|
|
|
->setMethods(['getHeader']) |
143
|
|
|
->getMock(); |
144
|
|
|
$request->expects($this->once()) |
145
|
|
|
->method('getHeader') |
146
|
|
|
->with('token') |
147
|
|
|
->will($this->returnValue(['yyy'])); |
148
|
|
|
$this->token->setConfig('type', 'header'); |
149
|
|
|
$result = $this->token->authenticate($request, new Response()); |
150
|
|
|
$this->assertEquals('user-1', $result['username']); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* test |
155
|
|
|
* |
156
|
|
|
* @return void |
157
|
|
|
*/ |
158
|
|
|
public function testAuthenticateHeaderFail() |
159
|
|
|
{ |
160
|
|
|
$request = $this->getMockBuilder('\Cake\Http\ServerRequest') |
161
|
|
|
->setMethods(['getHeader']) |
162
|
|
|
->getMock(); |
163
|
|
|
$request->expects($this->once()) |
164
|
|
|
->method('getHeader') |
165
|
|
|
->with('token') |
166
|
|
|
->will($this->returnValue(['wrong'])); |
167
|
|
|
$this->token->setConfig('type', 'header'); |
168
|
|
|
$result = $this->token->authenticate($request, new Response()); |
169
|
|
|
$this->assertFalse($result); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|