This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Craft; |
||
4 | |||
5 | use \PHPUnit_Framework_MockObject_MockObject as mock; |
||
6 | |||
7 | /** |
||
8 | * @author Nerds & Company |
||
9 | * @copyright Copyright (c) 2015, Nerds & Company |
||
10 | * @license MIT |
||
11 | * |
||
12 | * @link http://www.nerds.company |
||
13 | * |
||
14 | * @coversDefaultClass Craft\ApiAuthController |
||
15 | * @covers ::<!public> |
||
16 | */ |
||
17 | class ApiAuthControllerTest extends BaseTest |
||
18 | { |
||
19 | /** |
||
20 | * {@inheritdoc} |
||
21 | */ |
||
22 | public static function setUpBeforeClass() |
||
23 | { |
||
24 | parent::setUpBeforeClass(); |
||
25 | //Plugin classes |
||
26 | require_once __DIR__ . '/../../controllers/ApiAuthController.php'; |
||
27 | require_once __DIR__ . '/../../services/ApiAuthService.php'; |
||
28 | } |
||
29 | |||
30 | //============================================================================================================== |
||
31 | //================================================= TESTS ==================================================== |
||
32 | //============================================================================================================== |
||
33 | |||
34 | /** |
||
35 | * @covers ::actionAuthenticate |
||
36 | */ |
||
37 | public function testApiAuthControllerAuthenticateShouldReturnErrorWhenNotPostRequest() |
||
38 | { |
||
39 | $this->setSimpleMockApiAuthService(); |
||
40 | $this->setMockRequestService('GET'); |
||
41 | |||
42 | $apiAuthController = $this->getMockApiAuthController('returnErrorJson', ''); |
||
43 | |||
44 | $apiAuthController->actionAuthenticate(); |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * @covers ::actionAuthenticate |
||
49 | */ |
||
50 | public function testApiAuthControllerAuthenticateShouldReturnBadCredentialsWhenLoginFails() |
||
51 | { |
||
52 | $username = 'username'; |
||
53 | $password = 'test123'; |
||
54 | $errorMessage = Craft::t('Invalid username or password'); |
||
55 | |||
56 | $this->setSimpleMockApiAuthService(); |
||
57 | $this->setMockRequestService('POST', $username, $password); |
||
58 | $this->setMockUserSessionService($username, $password, false); |
||
59 | |||
60 | $apiAuthController = $this->getMockApiAuthController('returnErrorJson', $errorMessage); |
||
61 | |||
62 | $apiAuthController->actionAuthenticate(); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * @covers ::actionAuthenticate |
||
67 | */ |
||
68 | public function testApiAuthControllerAuthenticateShouldReturnInternalServerErrorWhenKeyCannotBeSaved() |
||
69 | { |
||
70 | $username = 'username'; |
||
71 | $password = 'test123'; |
||
72 | $key = 'averynicekey'; |
||
73 | $errorMessage = Craft::t('Something went wrong'); |
||
74 | |||
75 | $mockUser = $this->getMockUser(); |
||
76 | $this->setMockRequestService('POST', $username, $password); |
||
77 | $this->setMockUserSessionService($username, $password, true, $mockUser); |
||
78 | $this->setMockApiAuthService($key, $mockUser, false); |
||
79 | |||
80 | $apiAuthController = $this->getMockApiAuthController('returnErrorJson', $errorMessage); |
||
81 | |||
82 | $apiAuthController->actionAuthenticate(); |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * @covers ::actionAuthenticate |
||
87 | */ |
||
88 | public function testApiAuthControllerAuthenticateShouldReturnKey() |
||
89 | { |
||
90 | $username = 'username'; |
||
91 | $password = 'test123'; |
||
92 | $key = 'averynicekey'; |
||
93 | |||
94 | $mockUser = $this->getMockUser(); |
||
95 | $this->setMockRequestService('POST', $username, $password); |
||
96 | $this->setMockUserSessionService($username, $password, true, $mockUser); |
||
97 | $this->setMockApiAuthService($key, $mockUser, true); |
||
98 | |||
99 | $apiAuthController = $this->getMockApiAuthController('returnJson', array( |
||
100 | 'key' => $key, |
||
101 | 'user' => array( |
||
102 | 'username' => $mockUser->username, |
||
103 | 'photo' => $mockUser->photo, |
||
104 | 'firstName' => $mockUser->firstName, |
||
105 | 'lastName' => $mockUser->lastName, |
||
106 | 'email' => $mockUser->email, |
||
107 | ), |
||
108 | )); |
||
109 | |||
110 | $apiAuthController->actionAuthenticate(); |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * @covers ::actionResetPassword |
||
115 | */ |
||
116 | public function testApiAuthControllerResetPasswordShouldReturnErrorWhenNotPostRequest() |
||
117 | { |
||
118 | $this->setSimpleMockApiAuthService(); |
||
119 | $this->setMockRequestService('GET'); |
||
120 | |||
121 | $apiAuthController = $this->getMockApiAuthController('returnErrorJson', ''); |
||
122 | |||
123 | $apiAuthController->actionResetPassword(); |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * @covers ::actionResetPassword |
||
128 | */ |
||
129 | public function testApiAuthControllerResetPasswordShouldReturnSuccessMessageWhenUserNotFound() |
||
130 | { |
||
131 | $username = 'username'; |
||
132 | $message = array('message' => Craft::t('Email has been sent if address exists')); |
||
133 | |||
134 | $this->setSimpleMockApiAuthService(); |
||
135 | $this->setMockUsersService($username); |
||
136 | $this->setMockRequestService('POST', $username, null, 1); |
||
137 | |||
138 | $apiAuthController = $this->getMockApiAuthController('returnJson', $message); |
||
139 | |||
140 | $apiAuthController->actionResetPassword(); |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * @covers ::actionResetPassword |
||
145 | */ |
||
146 | public function testApiAuthControllerResetPasswordShouldSendMailWhenUserFound() |
||
147 | { |
||
148 | $username = 'username'; |
||
149 | $message = array('message' => Craft::t('Email has been sent if address exists')); |
||
150 | |||
151 | $this->setSimpleMockApiAuthService(); |
||
152 | $this->setMockRequestService('POST', $username, null, 1); |
||
153 | |||
154 | $mockUser = $this->getMockUser(); |
||
155 | $mockUsersService = $this->setMockUsersService($username, $mockUser); |
||
156 | |||
157 | $mockUsersService->expects($this->exactly(1)) |
||
158 | ->method('sendPasswordResetEmail') |
||
159 | ->with($mockUser); |
||
160 | |||
161 | $apiAuthController = $this->getMockApiAuthController('returnJson', $message); |
||
162 | |||
163 | $apiAuthController->actionResetPassword(); |
||
164 | } |
||
165 | |||
166 | //============================================================================================================== |
||
167 | //================================================= MOCKS ==================================================== |
||
168 | //============================================================================================================== |
||
169 | |||
170 | /** |
||
171 | * @param string $method |
||
172 | * @param mixed $param |
||
173 | * @param mixed $return |
||
174 | * @return ApiAuthController|mock |
||
175 | */ |
||
176 | private function getMockApiAuthController($method, $param, $return = null) |
||
177 | { |
||
178 | $apiAuthController = $this->getMockBuilder('Craft\ApiAuthController') |
||
179 | ->disableOriginalConstructor() |
||
180 | ->setMethods(array($method)) |
||
181 | ->getMock(); |
||
182 | |||
183 | $apiAuthController->expects($this->exactly(1)) |
||
184 | ->method($method) |
||
185 | ->with($param) |
||
186 | ->willReturn($return); |
||
187 | |||
188 | return $apiAuthController; |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * @return UserModel|mock |
||
193 | */ |
||
194 | private function getMockUser() |
||
195 | { |
||
196 | $mockUser = $this->getMockBuilder('Craft\UserModel') |
||
197 | ->disableOriginalConstructor() |
||
198 | ->getMock(); |
||
199 | |||
200 | return $mockUser; |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * @param string $requestType |
||
205 | * @param string $username |
||
206 | * @param string $password |
||
207 | * @param int $count |
||
208 | * |
||
209 | * @return UserPermissionsService|mock |
||
210 | */ |
||
211 | private function setMockRequestService($requestType, $username = null, $password = null, $count = 2) |
||
212 | { |
||
213 | $mockRequestService = $this->getMockBuilder('Craft\HttpRequestService') |
||
214 | ->disableOriginalConstructor() |
||
215 | ->getMock(); |
||
216 | |||
217 | $mockRequestService->expects($this->exactly(1)) |
||
218 | ->method('getRequestType') |
||
219 | ->willReturn($requestType); |
||
220 | |||
221 | if ($username !== null || $password !== null) { |
||
222 | $mockRequestService->expects($this->exactly($count)) |
||
223 | ->method('getRequiredPost') |
||
224 | ->willReturnMap(array( |
||
225 | array('username', $username), |
||
226 | array('password', $password), |
||
227 | )); |
||
228 | } |
||
229 | |||
230 | $this->setComponent(craft(), 'request', $mockRequestService); |
||
231 | |||
232 | return $mockRequestService; |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * @param string $username |
||
237 | * @param string $password |
||
238 | * @param bool $success |
||
239 | * @param UserModel $mockUser |
||
240 | * |
||
241 | * @return UserSessionService|mock |
||
242 | */ |
||
243 | private function setMockUserSessionService($username, $password, $success = true, UserModel $mockUser = null) |
||
244 | { |
||
245 | $mockUserSessionService = $this->getMockBuilder('Craft\UserSessionService') |
||
246 | ->disableOriginalConstructor() |
||
247 | ->getMock(); |
||
248 | |||
249 | $mockUserSessionService->expects($this->exactly(1)) |
||
250 | ->method('login') |
||
251 | ->with($username, $password) |
||
252 | ->willReturn($success); |
||
253 | |||
254 | if ($mockUser) { |
||
255 | $mockUserSessionService->expects($this->exactly(1)) |
||
256 | ->method('getUser') |
||
257 | ->willReturn($mockUser); |
||
258 | } |
||
259 | |||
260 | $this->setComponent(craft(), 'userSession', $mockUserSessionService); |
||
261 | |||
262 | return $mockUserSessionService; |
||
263 | } |
||
264 | |||
265 | /** |
||
266 | * @return mock|ApiAuthService |
||
267 | */ |
||
268 | private function setSimpleMockApiAuthService() |
||
269 | { |
||
270 | $mockApiAuthService = $this->getMockBuilder('Craft\ApiAuthService') |
||
271 | ->disableOriginalConstructor() |
||
272 | ->getMock(); |
||
273 | |||
274 | $this->setComponent(craft(), 'apiAuth', $mockApiAuthService); |
||
275 | |||
276 | return $mockApiAuthService; |
||
277 | } |
||
278 | |||
279 | /** |
||
280 | * @param string $key |
||
281 | * @param UserModel $mockUser |
||
282 | * @param bool $success |
||
283 | * |
||
284 | * @return mock|ApiAuthService |
||
285 | */ |
||
286 | private function setMockApiAuthService($key, UserModel $mockUser, $success) |
||
287 | { |
||
288 | $mockApiAuthService = $this->setSimpleMockApiAuthService(); |
||
289 | |||
290 | $mockApiAuthService->expects($this->exactly(1)) |
||
291 | ->method('generateKey') |
||
292 | ->willReturn($key); |
||
293 | |||
294 | $mockApiAuthService->expects($this->exactly(1)) |
||
295 | ->method('saveKey') |
||
296 | ->with($mockUser, $key) |
||
297 | ->willReturn($success); |
||
298 | |||
299 | |||
300 | return $mockApiAuthService; |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * @param string $username |
||
305 | * @param UserModel $user |
||
306 | * @return mock|UsersService |
||
307 | */ |
||
308 | View Code Duplication | private function setMockUsersService($username, UserModel $user = null) |
|
0 ignored issues
–
show
|
|||
309 | { |
||
310 | $mockUsersService = $this->getMockBuilder('Craft\UsersService') |
||
311 | ->disableOriginalConstructor() |
||
312 | ->getMock(); |
||
313 | |||
314 | $mockUsersService->expects($this->exactly(1)) |
||
315 | ->method('getUserByUsernameOrEmail') |
||
316 | ->with($username) |
||
317 | ->willReturn($user); |
||
318 | |||
319 | $this->setComponent(craft(), 'users', $mockUsersService); |
||
320 | |||
321 | return $mockUsersService; |
||
322 | } |
||
323 | } |
||
324 |
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.