|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright 2016 - 2017, 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 - 2017, 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\SocialAuthenticate; |
|
16
|
|
|
use CakeDC\Api\Service\Auth\Authenticate\TokenAuthenticate; |
|
17
|
|
|
use CakeDC\Api\Service\FallbackService; |
|
18
|
|
|
use CakeDC\Api\TestSuite\TestCase; |
|
19
|
|
|
use CakeDC\Api\Test\ConfigTrait; |
|
20
|
|
|
use CakeDC\Api\Test\FixturesTrait; |
|
21
|
|
|
use Cake\Network\Request; |
|
22
|
|
|
use Cake\Network\Response; |
|
23
|
|
|
|
|
24
|
|
|
class SocialAuthenticateTest extends TestCase |
|
25
|
|
|
{ |
|
26
|
|
|
|
|
27
|
|
|
use ConfigTrait; |
|
28
|
|
|
use FixturesTrait; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var SocialAuthenticate |
|
32
|
|
|
*/ |
|
33
|
|
|
public $social; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Sets up the fixture, for example, opens a network connection. |
|
37
|
|
|
* This method is called before a test is executed. |
|
38
|
|
|
*/ |
|
39
|
|
|
public function setUp() |
|
40
|
|
|
{ |
|
41
|
|
|
$request = new Request(); |
|
42
|
|
|
$response = new Response(); |
|
43
|
|
|
$service = new FallbackService([ |
|
44
|
|
|
'request' => $request, |
|
45
|
|
|
'response' => $response |
|
46
|
|
|
]); |
|
47
|
|
|
$action = new CrudIndexAction([ |
|
48
|
|
|
'service' => $service, |
|
49
|
|
|
'request' => $request, |
|
50
|
|
|
'response' => $response |
|
51
|
|
|
]); |
|
52
|
|
|
$this->social = new SocialAuthenticate($action, ['require_ssl' => false]); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Tears down the fixture, for example, closes a network connection. |
|
57
|
|
|
* This method is called after a test is executed. |
|
58
|
|
|
*/ |
|
59
|
|
|
public function tearDown() |
|
60
|
|
|
{ |
|
61
|
|
|
unset($this->social, $this->controller); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* test |
|
66
|
|
|
* |
|
67
|
|
|
* @return void |
|
68
|
|
|
*/ |
|
69
|
|
|
public function testAuthenticateHappy() |
|
70
|
|
|
{ |
|
71
|
|
|
$request = new Request('/?provider=Facebook&token=token-1234&token_secret=token-secret'); |
|
72
|
|
|
$result = $this->social->authenticate($request, new Response()); |
|
73
|
|
|
$this->assertEquals('user-1', $result['username']); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* test |
|
78
|
|
|
* |
|
79
|
|
|
* @return void |
|
80
|
|
|
*/ |
|
81
|
|
|
public function testAuthenticateFail() |
|
82
|
|
|
{ |
|
83
|
|
|
$request = new Request('/'); |
|
84
|
|
|
$result = $this->social->authenticate($request, new Response()); |
|
85
|
|
|
$this->assertFalse($result); |
|
86
|
|
|
|
|
87
|
|
|
$request = new Request('/?provider=Facebook&token=none'); |
|
88
|
|
|
$result = $this->social->authenticate($request, new Response()); |
|
89
|
|
|
$this->assertFalse($result); |
|
90
|
|
|
|
|
91
|
|
|
$request = new Request('/?provider=Facebook&token='); |
|
92
|
|
|
$result = $this->social->authenticate($request, new Response()); |
|
93
|
|
|
$this->assertFalse($result); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* test |
|
98
|
|
|
* |
|
99
|
|
|
* @expectedException \OutOfBoundsException |
|
100
|
|
|
* @expectedExceptionMessage Type wrong is not valid |
|
101
|
|
|
* |
|
102
|
|
|
*/ |
|
103
|
|
|
public function testAuthenticateWrongType() |
|
104
|
|
|
{ |
|
105
|
|
|
$this->social->setConfig('type', 'wrong'); |
|
106
|
|
|
$request = new Request('/'); |
|
107
|
|
|
$this->social->authenticate($request, new Response()); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* test |
|
112
|
|
|
* |
|
113
|
|
|
* @expectedException \Cake\Network\Exception\ForbiddenException |
|
114
|
|
|
* @expectedExceptionMessage SSL is required for ApiKey Authentication |
|
115
|
|
|
* |
|
116
|
|
|
*/ |
|
117
|
|
|
public function testAuthenticateRequireSSL() |
|
118
|
|
|
{ |
|
119
|
|
|
$this->social->setConfig('require_ssl', true); |
|
120
|
|
|
$request = new Request('/?token=token-1234&token_secret=token-secret&provider=Facebook'); |
|
121
|
|
|
$this->social->authenticate($request, new Response()); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* test |
|
126
|
|
|
* |
|
127
|
|
|
*/ |
|
128
|
|
|
public function testAuthenticateRequireSSLNoKey() |
|
129
|
|
|
{ |
|
130
|
|
|
$this->social->setConfig('require_ssl', true); |
|
131
|
|
|
$request = new Request('/'); |
|
132
|
|
|
$this->assertFalse($this->social->authenticate($request, new Response())); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* test |
|
137
|
|
|
* |
|
138
|
|
|
* @return void |
|
139
|
|
|
*/ |
|
140
|
|
|
public function testHeaderHappy() |
|
141
|
|
|
{ |
|
142
|
|
|
$request = $this->getMockBuilder('\Cake\Http\ServerRequest') |
|
143
|
|
|
->setMethods(['getHeader']) |
|
144
|
|
|
->getMock(); |
|
145
|
|
|
|
|
146
|
|
|
$request->expects($this->at(0)) |
|
147
|
|
|
->method('getHeader') |
|
148
|
|
|
->with('provider') |
|
149
|
|
|
->will($this->returnValue(['Facebook'])); |
|
150
|
|
|
|
|
151
|
|
|
$request->expects($this->at(1)) |
|
152
|
|
|
->method('getHeader') |
|
153
|
|
|
->with('token') |
|
154
|
|
|
->will($this->returnValue(['token-1234'])); |
|
155
|
|
|
|
|
156
|
|
|
$request->expects($this->at(2)) |
|
157
|
|
|
->method('getHeader') |
|
158
|
|
|
->with('token_secret') |
|
159
|
|
|
->will($this->returnValue(['token-secret'])); |
|
160
|
|
|
$this->social->setConfig('type', 'header'); |
|
161
|
|
|
$result = $this->social->authenticate($request, new Response()); |
|
162
|
|
|
$this->assertEquals('user-1', $result['username']); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* test |
|
167
|
|
|
* |
|
168
|
|
|
* @return void |
|
169
|
|
|
*/ |
|
170
|
|
|
public function testAuthenticateHeaderFail() |
|
171
|
|
|
{ |
|
172
|
|
|
$request = $this->getMockBuilder('\Cake\Http\ServerRequest') |
|
173
|
|
|
->setMethods(['getHeader']) |
|
174
|
|
|
->getMock(); |
|
175
|
|
|
$request->expects($this->at(0)) |
|
176
|
|
|
->method('getHeader') |
|
177
|
|
|
->with('provider') |
|
178
|
|
|
->will($this->returnValue(['wrong'])); |
|
179
|
|
|
|
|
180
|
|
|
$request->expects($this->at(1)) |
|
181
|
|
|
->method('getHeader') |
|
182
|
|
|
->with('token') |
|
183
|
|
|
->will($this->returnValue(['wrong'])); |
|
184
|
|
|
|
|
185
|
|
|
$request->expects($this->at(2)) |
|
186
|
|
|
->method('getHeader') |
|
187
|
|
|
->with('token_secret') |
|
188
|
|
|
->will($this->returnValue(['wrong'])); |
|
189
|
|
|
$this->social->setConfig('type', 'header'); |
|
190
|
|
|
$result = $this->social->authenticate($request, new Response()); |
|
191
|
|
|
$this->assertFalse($result); |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
|