Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 35 | public static $functions; |
||
| 36 | |||
| 37 | public function setUp() |
||
| 38 | { |
||
| 39 | parent::setUp(); |
||
| 40 | self::$functions = Mockery::mock(); |
||
| 41 | app()->instance(TicketLocker::class, Mockery::mock(TicketLocker::class)); |
||
| 42 | } |
||
| 43 | |||
| 44 | public function testV1ValidateActionWithInvalidRequest() |
||
| 45 | { |
||
| 46 | $request = Mockery::mock(Request::class) |
||
| 47 | ->shouldReceive('get') |
||
| 48 | ->withArgs(['ticket', '']) |
||
| 49 | ->andReturnNull() |
||
| 50 | ->shouldReceive('get') |
||
| 51 | ->withArgs(['service', '']) |
||
| 52 | ->andReturnNull() |
||
| 53 | ->getMock(); |
||
| 54 | $resp = app()->make(ValidateController::class)->v1ValidateAction($request); |
||
| 55 | $this->assertInstanceOf(Response::class, $resp); |
||
| 56 | $this->assertEquals('no', $resp->getOriginalContent()); |
||
| 57 | } |
||
| 58 | |||
| 59 | public function testV1ValidateActionWithLockFailed() |
||
| 60 | { |
||
| 61 | $request = $this->getValidRequest(); |
||
| 62 | $controller = Mockery::mock(ValidateController::class) |
||
| 63 | ->makePartial() |
||
| 64 | ->shouldAllowMockingProtectedMethods() |
||
| 65 | ->shouldReceive('lockTicket') |
||
| 66 | ->andReturn(false) |
||
| 67 | ->getMock(); |
||
| 68 | $resp = $controller->v1ValidateAction($request); |
||
| 69 | $this->assertInstanceOf(Response::class, $resp); |
||
| 70 | $this->assertEquals('no', $resp->getOriginalContent()); |
||
| 71 | } |
||
| 72 | |||
| 73 | public function testV1ValidateActionWithInvalidTicket() |
||
| 74 | { |
||
| 75 | $request = $this->getValidRequest(); |
||
| 76 | $ticketRepository = Mockery::mock(TicketRepository::class) |
||
| 77 | ->shouldReceive('getByTicket') |
||
| 78 | ->andReturnNull() |
||
| 79 | ->getMock(); |
||
| 80 | app()->instance(TicketRepository::class, $ticketRepository); |
||
| 81 | $controller = $this->initController() |
||
| 82 | ->makePartial() |
||
| 83 | ->shouldAllowMockingProtectedMethods() |
||
| 84 | ->shouldReceive('lockTicket') |
||
| 85 | ->andReturn(true) |
||
| 86 | ->shouldReceive('unlockTicket') |
||
| 87 | ->getMock(); |
||
| 88 | $resp = $controller->v1ValidateAction($request); |
||
| 89 | $this->assertInstanceOf(Response::class, $resp); |
||
| 90 | $this->assertEquals('no', $resp->getOriginalContent()); |
||
| 91 | } |
||
| 92 | |||
| 93 | public function testV1ValidateActionWithValidTicketButServiceMismatch() |
||
| 94 | { |
||
| 95 | $request = $this->getValidRequest(); |
||
| 96 | $ticket = Mockery::mock(); |
||
| 97 | $ticket->service_url = 'http//google.com'; |
||
| 98 | $ticketRepository = Mockery::mock(TicketRepository::class) |
||
| 99 | ->shouldReceive('getByTicket') |
||
| 100 | ->andReturn($ticket) |
||
| 101 | ->getMock(); |
||
| 102 | app()->instance(TicketRepository::class, $ticketRepository); |
||
| 103 | $controller = $this->initController() |
||
| 104 | ->makePartial() |
||
| 105 | ->shouldAllowMockingProtectedMethods() |
||
| 106 | ->shouldReceive('lockTicket') |
||
| 107 | ->andReturn(true) |
||
| 108 | ->shouldReceive('unlockTicket') |
||
| 109 | ->getMock(); |
||
| 110 | $resp = $controller->v1ValidateAction($request); |
||
| 111 | $this->assertInstanceOf(Response::class, $resp); |
||
| 112 | $this->assertEquals('no', $resp->getOriginalContent()); |
||
| 113 | } |
||
| 114 | |||
| 115 | public function testV1ValidateActionWithValidTicketAndService() |
||
| 116 | { |
||
| 117 | $request = $this->getValidRequest(); |
||
| 118 | $ticket = Mockery::mock(Ticket::class) |
||
| 119 | ->shouldReceive('getAttribute') |
||
| 120 | ->withArgs(['service_url']) |
||
| 121 | ->andReturn('http://leo108.com') |
||
| 122 | ->getMock(); |
||
| 123 | $ticketRepository = Mockery::mock(TicketRepository::class) |
||
| 124 | ->shouldReceive('getByTicket') |
||
| 125 | ->andReturn($ticket) |
||
| 126 | ->shouldReceive('invalidTicket') |
||
| 127 | ->getMock(); |
||
| 128 | app()->instance(TicketRepository::class, $ticketRepository); |
||
| 129 | $controller = $this->initController() |
||
| 130 | ->makePartial() |
||
| 131 | ->shouldAllowMockingProtectedMethods() |
||
| 132 | ->shouldReceive('lockTicket') |
||
| 133 | ->andReturn(true) |
||
| 134 | ->shouldReceive('unlockTicket') |
||
| 135 | ->getMock(); |
||
| 136 | $resp = $controller->v1ValidateAction($request); |
||
| 137 | $this->assertInstanceOf(Response::class, $resp); |
||
| 138 | $this->assertEquals('yes', $resp->getOriginalContent()); |
||
| 139 | } |
||
| 140 | |||
| 141 | public function testV2ServiceValidateAction() |
||
| 142 | { |
||
| 143 | $controller = Mockery::mock(ValidateController::class) |
||
| 144 | ->makePartial() |
||
| 145 | ->shouldAllowMockingProtectedMethods() |
||
| 146 | ->shouldReceive('casValidate') |
||
| 147 | ->andReturnUsing( |
||
| 148 | function ($request, $returnAttr, $allowProxy) { |
||
| 149 | $this->assertFalse($returnAttr); |
||
| 150 | $this->assertFalse($allowProxy); |
||
| 151 | |||
| 152 | return 'casValidate called'; |
||
| 153 | } |
||
| 154 | ) |
||
| 155 | ->once() |
||
| 156 | ->getMock(); |
||
| 157 | $request = Mockery::mock(Request::class); |
||
| 158 | $this->assertEquals('casValidate called', $controller->v2ServiceValidateAction($request)); |
||
| 159 | } |
||
| 160 | |||
| 161 | public function testV2ProxyValidateAction() |
||
| 162 | { |
||
| 163 | $controller = Mockery::mock(ValidateController::class) |
||
| 164 | ->makePartial() |
||
| 165 | ->shouldAllowMockingProtectedMethods() |
||
| 166 | ->shouldReceive('casValidate') |
||
| 167 | ->andReturnUsing( |
||
| 168 | function ($request, $returnAttr, $allowProxy) { |
||
| 169 | $this->assertFalse($returnAttr); |
||
| 170 | $this->assertTrue($allowProxy); |
||
| 171 | |||
| 172 | return 'casValidate called'; |
||
| 173 | } |
||
| 174 | ) |
||
| 175 | ->once() |
||
| 176 | ->getMock(); |
||
| 177 | $request = Mockery::mock(Request::class); |
||
| 178 | $this->assertEquals('casValidate called', $controller->v2ProxyValidateAction($request)); |
||
| 179 | } |
||
| 180 | |||
| 181 | public function testV3ServiceValidateAction() |
||
| 182 | { |
||
| 183 | $controller = Mockery::mock(ValidateController::class) |
||
| 184 | ->makePartial() |
||
| 185 | ->shouldAllowMockingProtectedMethods() |
||
| 186 | ->shouldReceive('casValidate') |
||
| 187 | ->andReturnUsing( |
||
| 188 | function ($request, $returnAttr, $allowProxy) { |
||
| 189 | $this->assertTrue($returnAttr); |
||
| 190 | $this->assertFalse($allowProxy); |
||
| 191 | |||
| 192 | return 'casValidate called'; |
||
| 193 | } |
||
| 194 | ) |
||
| 195 | ->once() |
||
| 196 | ->getMock(); |
||
| 197 | $request = Mockery::mock(Request::class); |
||
| 198 | $this->assertEquals('casValidate called', $controller->v3ServiceValidateAction($request)); |
||
| 199 | } |
||
| 200 | |||
| 201 | public function testV3ProxyValidateAction() |
||
| 202 | { |
||
| 203 | $controller = Mockery::mock(ValidateController::class) |
||
| 204 | ->makePartial() |
||
| 205 | ->shouldAllowMockingProtectedMethods() |
||
| 206 | ->shouldReceive('casValidate') |
||
| 207 | ->andReturnUsing( |
||
| 208 | function ($request, $returnAttr, $allowProxy) { |
||
| 209 | $this->assertTrue($returnAttr); |
||
| 210 | $this->assertTrue($allowProxy); |
||
| 211 | |||
| 212 | return 'casValidate called'; |
||
| 213 | } |
||
| 214 | ) |
||
| 215 | ->once() |
||
| 216 | ->getMock(); |
||
| 217 | $request = Mockery::mock(Request::class); |
||
| 218 | $this->assertEquals('casValidate called', $controller->v3ProxyValidateAction($request)); |
||
| 219 | } |
||
| 220 | |||
| 221 | public function testProxyActionWithInvalidRequest() |
||
| 222 | { |
||
| 223 | $request = Mockery::mock(Request::class) |
||
| 224 | ->shouldReceive('get') |
||
| 225 | ->with('pgt', '') |
||
| 226 | ->andReturn('') |
||
| 227 | ->once() |
||
| 228 | ->shouldReceive('get') |
||
| 229 | ->with('targetService', '') |
||
| 230 | ->andReturn('') |
||
| 231 | ->once() |
||
| 232 | ->shouldReceive('get') |
||
| 233 | ->with('format', 'XML') |
||
| 234 | ->andReturn('XML') |
||
| 235 | ->once() |
||
| 236 | ->getMock(); |
||
| 237 | $controller = $this->initController() |
||
| 238 | ->makePartial() |
||
| 239 | ->shouldAllowMockingProtectedMethods() |
||
| 240 | ->shouldReceive('proxyFailureResponse') |
||
| 241 | ->andReturnUsing( |
||
| 242 | function ($code, $desc, $format) { |
||
| 243 | $this->assertEquals(CasException::INVALID_REQUEST, $code); |
||
| 244 | $this->assertEquals('param pgt and targetService can not be empty', $desc); |
||
| 245 | $this->assertEquals('XML', $format); |
||
| 246 | |||
| 247 | return 'proxyFailureResponse called'; |
||
| 248 | } |
||
| 249 | ) |
||
| 250 | ->once() |
||
| 251 | ->getMock(); |
||
| 252 | $this->assertEquals('proxyFailureResponse called', $controller->proxyAction($request)); |
||
| 253 | } |
||
| 254 | |||
| 255 | public function testProxyActionWithInvalidTicket() |
||
| 256 | { |
||
| 257 | $request = Mockery::mock(Request::class) |
||
| 258 | ->shouldReceive('get') |
||
| 259 | ->with('pgt', '') |
||
| 260 | ->andReturn('pgt string') |
||
| 261 | ->once() |
||
| 262 | ->shouldReceive('get') |
||
| 263 | ->with('targetService', '') |
||
| 264 | ->andReturn('http://target.com') |
||
| 265 | ->once() |
||
| 266 | ->shouldReceive('get') |
||
| 267 | ->with('format', 'XML') |
||
| 268 | ->andReturn('XML') |
||
| 269 | ->once() |
||
| 270 | ->getMock(); |
||
| 271 | $pgtRepository = Mockery::mock(PGTicketRepository::class) |
||
| 272 | ->shouldReceive('getByTicket') |
||
| 273 | ->andReturn(false) |
||
| 274 | ->once() |
||
| 275 | ->getMock(); |
||
| 276 | app()->instance(PGTicketRepository::class, $pgtRepository); |
||
| 277 | $controller = $this->initController() |
||
| 278 | ->makePartial() |
||
| 279 | ->shouldAllowMockingProtectedMethods() |
||
| 280 | ->shouldReceive('proxyFailureResponse') |
||
| 281 | ->andReturnUsing( |
||
| 282 | function ($code, $desc, $format) { |
||
| 283 | $this->assertEquals(CasException::INVALID_TICKET, $code); |
||
| 284 | $this->assertEquals('ticket is not valid', $desc); |
||
| 285 | $this->assertEquals('XML', $format); |
||
| 286 | |||
| 287 | return 'proxyFailureResponse called'; |
||
| 288 | } |
||
| 289 | ) |
||
| 290 | ->once() |
||
| 291 | ->getMock(); |
||
| 292 | $this->assertEquals('proxyFailureResponse called', $controller->proxyAction($request)); |
||
| 293 | } |
||
| 294 | |||
| 295 | public function testCasValidateWithInvalidRequest() |
||
| 296 | { |
||
| 297 | $request = Mockery::mock(Request::class) |
||
| 298 | ->shouldReceive('get') |
||
| 299 | ->withArgs(['ticket', '']) |
||
| 300 | ->andReturn('') |
||
| 301 | ->once() |
||
| 302 | ->shouldReceive('get') |
||
| 303 | ->withArgs(['service', '']) |
||
| 304 | ->andReturn('') |
||
| 305 | ->once() |
||
| 306 | ->shouldReceive('get') |
||
| 307 | ->withArgs(['format', 'XML']) |
||
| 308 | ->andReturn('JSON') |
||
| 309 | ->once() |
||
| 310 | ->getMock(); |
||
| 311 | |||
| 312 | $controller = $this->initController() |
||
| 313 | ->makePartial() |
||
| 314 | ->shouldAllowMockingProtectedMethods() |
||
| 315 | ->shouldReceive('authFailureResponse') |
||
| 316 | ->andReturnUsing( |
||
| 317 | function ($code, $desc, $format) { |
||
| 318 | $this->assertEquals(CasException::INVALID_REQUEST, $code); |
||
| 319 | $this->assertEquals('param service and ticket can not be empty', $desc); |
||
| 320 | $this->assertEquals('JSON', $format); |
||
| 321 | |||
| 322 | return 'authFailureResponse called'; |
||
| 323 | } |
||
| 324 | ) |
||
| 325 | ->once() |
||
| 326 | ->getMock(); |
||
| 327 | $method = self::getNonPublicMethod($controller, 'casValidate'); |
||
| 328 | $this->assertEquals('authFailureResponse called', $method->invokeArgs($controller, [$request, false, false])); |
||
| 329 | } |
||
| 330 | |||
| 331 | public function testCasValidateAndLockTicketFailed() |
||
| 332 | { |
||
| 333 | $request = $this->getValidRequest(); |
||
| 334 | $controller = $this->initController() |
||
| 335 | ->makePartial() |
||
| 336 | ->shouldAllowMockingProtectedMethods() |
||
| 337 | ->shouldReceive('lockTicket') |
||
| 338 | ->andReturn(false) |
||
| 339 | ->once() |
||
| 340 | ->shouldReceive('authFailureResponse') |
||
| 341 | ->andReturnUsing( |
||
| 342 | function ($code, $desc, $format) { |
||
| 343 | $this->assertEquals(CasException::INTERNAL_ERROR, $code); |
||
| 344 | $this->assertEquals('try to lock ticket failed', $desc); |
||
| 345 | $this->assertEquals('JSON', $format); |
||
| 346 | |||
| 347 | return 'authFailureResponse called'; |
||
| 348 | } |
||
| 349 | ) |
||
| 350 | ->once() |
||
| 351 | ->getMock(); |
||
| 352 | $method = self::getNonPublicMethod($controller, 'casValidate'); |
||
| 353 | $this->assertEquals('authFailureResponse called', $method->invokeArgs($controller, [$request, false, false])); |
||
| 354 | } |
||
| 355 | |||
| 356 | public function testCasValidateWithInvalidTicket() |
||
| 357 | { |
||
| 358 | $request = $this->getValidRequest(); |
||
| 359 | $ticketRepository = Mockery::mock(TicketRepository::class) |
||
| 360 | ->shouldReceive('getByTicket') |
||
| 361 | ->andReturnNull() |
||
| 362 | ->once() |
||
| 363 | ->getMock(); |
||
| 364 | app()->instance(TicketRepository::class, $ticketRepository); |
||
| 365 | $controller = $this->initController() |
||
| 366 | ->makePartial() |
||
| 367 | ->shouldAllowMockingProtectedMethods() |
||
| 368 | ->shouldReceive('unlockTicket') |
||
| 369 | ->once() |
||
| 370 | ->shouldReceive('lockTicket') |
||
| 371 | ->andReturn(true) |
||
| 372 | ->once() |
||
| 373 | ->shouldReceive('authFailureResponse') |
||
| 374 | ->andReturnUsing( |
||
| 375 | function ($code, $desc, $format) { |
||
| 376 | $this->assertEquals(CasException::INVALID_TICKET, $code); |
||
| 377 | $this->assertEquals('ticket is not valid', $desc); |
||
| 378 | $this->assertEquals('JSON', $format); |
||
| 379 | |||
| 380 | return 'authFailureResponse called'; |
||
| 381 | } |
||
| 382 | ) |
||
| 383 | ->once() |
||
| 384 | ->getMock(); |
||
| 385 | $method = self::getNonPublicMethod($controller, 'casValidate'); |
||
| 386 | $this->assertEquals('authFailureResponse called', $method->invokeArgs($controller, [$request, false, false])); |
||
| 387 | } |
||
| 388 | |||
| 389 | public function testCasValidateWithValidTicketButServiceMismatch() |
||
| 390 | { |
||
| 391 | $request = $this->getValidRequest(); |
||
| 392 | $ticket = Mockery::mock(Ticket::class) |
||
| 393 | ->shouldReceive('getAttribute') |
||
| 394 | ->withArgs(['service_url']) |
||
| 395 | ->andReturn('http://github.com') |
||
| 396 | ->once() |
||
| 397 | ->shouldReceive('isProxy') |
||
| 398 | ->andReturn(false) |
||
| 399 | ->once() |
||
| 400 | ->getMock(); |
||
| 401 | $ticketRepository = Mockery::mock(TicketRepository::class) |
||
| 402 | ->shouldReceive('invalidTicket') |
||
| 403 | ->once() |
||
| 404 | ->shouldReceive('getByTicket') |
||
| 405 | ->andReturn($ticket) |
||
| 406 | ->once() |
||
| 407 | ->getMock(); |
||
| 408 | app()->instance(TicketRepository::class, $ticketRepository); |
||
| 409 | $controller = $this->initController() |
||
| 410 | ->makePartial() |
||
| 411 | ->shouldAllowMockingProtectedMethods() |
||
| 412 | ->shouldReceive('unlockTicket') |
||
| 413 | ->once() |
||
| 414 | ->shouldReceive('lockTicket') |
||
| 415 | ->andReturn(true) |
||
| 416 | ->once() |
||
| 417 | ->shouldReceive('authFailureResponse') |
||
| 418 | ->andReturnUsing( |
||
| 419 | function ($code, $desc, $format) { |
||
| 420 | $this->assertEquals(CasException::INVALID_SERVICE, $code); |
||
| 421 | $this->assertEquals('service is not valid', $desc); |
||
| 422 | $this->assertEquals('JSON', $format); |
||
| 423 | |||
| 424 | return 'authFailureResponse called'; |
||
| 425 | } |
||
| 426 | ) |
||
| 427 | ->once() |
||
| 428 | ->getMock(); |
||
| 429 | $method = self::getNonPublicMethod($controller, 'casValidate'); |
||
| 430 | $this->assertEquals('authFailureResponse called', $method->invokeArgs($controller, [$request, false, false])); |
||
| 431 | } |
||
| 432 | |||
| 433 | public function testCasValidateWithValidProxyTicketButNotAllowProxy() |
||
| 434 | { |
||
| 435 | $request = $this->getValidRequest(); |
||
| 436 | $ticket = Mockery::mock(Ticket::class) |
||
| 437 | ->shouldReceive('isProxy') |
||
| 438 | ->andReturn(true) |
||
| 439 | ->once() |
||
| 440 | ->getMock(); |
||
| 441 | $ticketRepository = Mockery::mock(TicketRepository::class) |
||
| 442 | ->shouldReceive('invalidTicket') |
||
| 443 | ->once() |
||
| 444 | ->shouldReceive('getByTicket') |
||
| 445 | ->andReturn($ticket) |
||
| 446 | ->once() |
||
| 447 | ->getMock(); |
||
| 448 | app()->instance(TicketRepository::class, $ticketRepository); |
||
| 449 | $controller = $this->initController() |
||
| 450 | ->makePartial() |
||
| 451 | ->shouldAllowMockingProtectedMethods() |
||
| 452 | ->shouldReceive('unlockTicket') |
||
| 453 | ->once() |
||
| 454 | ->shouldReceive('lockTicket') |
||
| 455 | ->andReturn(true) |
||
| 456 | ->once() |
||
| 457 | ->shouldReceive('authFailureResponse') |
||
| 458 | ->andReturnUsing( |
||
| 459 | function ($code, $desc, $format) { |
||
| 460 | $this->assertEquals(CasException::INVALID_TICKET, $code); |
||
| 461 | $this->assertEquals('ticket is not valid', $desc); |
||
| 462 | $this->assertEquals('JSON', $format); |
||
| 463 | |||
| 464 | return 'authFailureResponse called'; |
||
| 465 | } |
||
| 466 | ) |
||
| 467 | ->once() |
||
| 468 | ->getMock(); |
||
| 469 | $method = self::getNonPublicMethod($controller, 'casValidate'); |
||
| 470 | $this->assertEquals('authFailureResponse called', $method->invokeArgs($controller, [$request, false, false])); |
||
| 471 | } |
||
| 472 | |||
| 473 | public function testCasValidateWithValidProxyTicketAndAllowProxy() |
||
| 474 | { |
||
| 475 | $proxies = ['http://proxy1.com', 'http://proxy2.com']; |
||
| 476 | $request = $this->getValidRequest(''); |
||
| 477 | $user = Mockery::mock(User::class) |
||
| 478 | ->shouldReceive('getName') |
||
| 479 | ->andReturn('test_user') |
||
| 480 | ->once() |
||
| 481 | ->getMock(); |
||
| 482 | $ticket = Mockery::mock(Ticket::class) |
||
| 483 | ->shouldReceive('isProxy') |
||
| 484 | ->andReturn(true) |
||
| 485 | ->once() |
||
| 486 | ->shouldReceive('getAttribute') |
||
| 487 | ->with('proxies') |
||
| 488 | ->andReturn($proxies) |
||
| 489 | ->once() |
||
| 490 | ->shouldReceive('getAttribute') |
||
| 491 | ->withArgs(['service_url']) |
||
| 492 | ->andReturn('http://leo108.com') |
||
| 493 | ->once() |
||
| 494 | ->shouldReceive('getAttribute') |
||
| 495 | ->withArgs(['user']) |
||
| 496 | ->andReturn($user) |
||
| 497 | ->times(2) |
||
| 498 | ->getMock(); |
||
| 499 | $ticketRepository = Mockery::mock(TicketRepository::class) |
||
| 500 | ->shouldReceive('invalidTicket') |
||
| 501 | ->once() |
||
| 502 | ->shouldReceive('getByTicket') |
||
| 503 | ->andReturn($ticket) |
||
| 504 | ->once() |
||
| 505 | ->getMock(); |
||
| 506 | app()->instance(TicketRepository::class, $ticketRepository); |
||
| 507 | $controller = $this->initController() |
||
| 508 | ->makePartial() |
||
| 509 | ->shouldAllowMockingProtectedMethods() |
||
| 510 | ->shouldReceive('lockTicket') |
||
| 511 | ->andReturn(true) |
||
| 512 | ->once() |
||
| 513 | ->shouldReceive('unlockTicket') |
||
| 514 | ->once() |
||
| 515 | ->shouldReceive('authSuccessResponse') |
||
| 516 | ->andReturnUsing( |
||
| 517 | function ($name, $format, $attributes, $proxiesParam, $iou) use ($proxies) { |
||
| 518 | $this->assertEquals('test_user', $name); |
||
| 519 | $this->assertEmpty($attributes); |
||
| 520 | $this->assertEquals('JSON', $format); |
||
| 521 | $this->assertEquals($proxies, $proxiesParam); |
||
| 522 | $this->assertNull($iou); |
||
| 523 | |||
| 524 | return 'authSuccessResponse called'; |
||
| 525 | } |
||
| 526 | ) |
||
| 527 | ->once() |
||
| 528 | ->getMock(); |
||
| 529 | $method = self::getNonPublicMethod($controller, 'casValidate'); |
||
| 530 | $this->assertEquals('authSuccessResponse called', $method->invokeArgs($controller, [$request, false, true])); |
||
| 531 | } |
||
| 532 | |||
| 533 | public function testCasValidateWithValidTicketAndServiceAndNoPgt() |
||
| 534 | { |
||
| 535 | $request = $this->getValidRequest(''); |
||
| 536 | $user = Mockery::mock(User::class) |
||
| 537 | ->shouldReceive('getName') |
||
| 538 | ->andReturn('test_user') |
||
| 539 | ->once() |
||
| 540 | ->getMock(); |
||
| 541 | $ticket = Mockery::mock(Ticket::class) |
||
| 542 | ->shouldReceive('isProxy') |
||
| 543 | ->andReturn(false) |
||
| 544 | ->times(2) |
||
| 545 | ->shouldReceive('getAttribute') |
||
| 546 | ->withArgs(['service_url']) |
||
| 547 | ->andReturn('http://leo108.com') |
||
| 548 | ->once() |
||
| 549 | ->shouldReceive('getAttribute') |
||
| 550 | ->withArgs(['user']) |
||
| 551 | ->andReturn($user) |
||
| 552 | ->times(2) |
||
| 553 | ->getMock(); |
||
| 554 | $ticketRepository = Mockery::mock(TicketRepository::class) |
||
| 555 | ->shouldReceive('getByTicket') |
||
| 556 | ->andReturn($ticket) |
||
| 557 | ->once() |
||
| 558 | ->shouldReceive('invalidTicket') |
||
| 559 | ->once() |
||
| 560 | ->getMock(); |
||
| 561 | app()->instance(TicketRepository::class, $ticketRepository); |
||
| 562 | $controller = $this->initController() |
||
| 563 | ->makePartial() |
||
| 564 | ->shouldAllowMockingProtectedMethods() |
||
| 565 | ->shouldReceive('lockTicket') |
||
| 566 | ->andReturn(true) |
||
| 567 | ->once() |
||
| 568 | ->shouldReceive('unlockTicket') |
||
| 569 | ->once() |
||
| 570 | ->shouldReceive('authSuccessResponse') |
||
| 571 | ->andReturnUsing( |
||
| 572 | function ($name, $format, $attributes, $proxies, $iou) { |
||
| 573 | $this->assertEquals('test_user', $name); |
||
| 574 | $this->assertEmpty($attributes); |
||
| 575 | $this->assertEquals('JSON', $format); |
||
| 576 | $this->assertEmpty($proxies); |
||
| 577 | $this->assertNull($iou); |
||
| 578 | |||
| 579 | return 'authSuccessResponse called'; |
||
| 580 | } |
||
| 581 | ) |
||
| 582 | ->once() |
||
| 583 | ->getMock(); |
||
| 584 | |||
| 585 | $method = self::getNonPublicMethod($controller, 'casValidate'); |
||
| 586 | $this->assertEquals('authSuccessResponse called', $method->invokeArgs($controller, [$request, false, false])); |
||
| 587 | } |
||
| 588 | |||
| 589 | public function testCasValidateWithValidTicketAndServiceAndPgtButApplyPGTFailed() |
||
| 590 | { |
||
| 591 | $request = $this->getValidRequest('http://app1.com/pgtCallback'); |
||
| 592 | $user = Mockery::mock(User::class) |
||
| 593 | ->shouldReceive('getName') |
||
| 594 | ->andReturn('test_user') |
||
| 595 | ->once() |
||
| 596 | ->getMock(); |
||
| 597 | $ticket = Mockery::mock(Ticket::class) |
||
| 598 | ->shouldReceive('isProxy') |
||
| 599 | ->andReturn(false) |
||
| 600 | ->times(2) |
||
| 601 | ->shouldReceive('getAttribute') |
||
| 602 | ->withArgs(['service_url']) |
||
| 603 | ->andReturn('http://leo108.com') |
||
| 604 | ->once() |
||
| 605 | ->shouldReceive('getAttribute') |
||
| 606 | ->withArgs(['user']) |
||
| 607 | ->andReturn($user) |
||
| 608 | ->times(2) |
||
| 609 | ->getMock(); |
||
| 610 | $ticketRepository = Mockery::mock(TicketRepository::class) |
||
| 611 | ->shouldReceive('getByTicket') |
||
| 612 | ->andReturn($ticket) |
||
| 613 | ->once() |
||
| 614 | ->shouldReceive('invalidTicket') |
||
| 615 | ->once() |
||
| 616 | ->getMock(); |
||
| 617 | app()->instance(TicketRepository::class, $ticketRepository); |
||
| 618 | $pgTicketRepository = Mockery::mock(PGTicketRepository::class) |
||
| 619 | ->shouldReceive('applyTicket') |
||
| 620 | ->andThrow(new CasException(CasException::INTERNAL_ERROR)) |
||
| 621 | ->once() |
||
| 622 | ->getMock(); |
||
| 623 | app()->instance(PGTicketRepository::class, $pgTicketRepository); |
||
| 624 | $controller = $this->initController() |
||
| 625 | ->makePartial() |
||
| 626 | ->shouldAllowMockingProtectedMethods() |
||
| 627 | ->shouldReceive('lockTicket') |
||
| 628 | ->andReturn(true) |
||
| 629 | ->once() |
||
| 630 | ->shouldReceive('unlockTicket') |
||
| 631 | ->once() |
||
| 632 | ->shouldReceive('authSuccessResponse') |
||
| 633 | ->andReturnUsing( |
||
| 634 | function ($name, $format, $attributes, $proxies, $iou) { |
||
| 635 | $this->assertEquals('test_user', $name); |
||
| 636 | $this->assertEmpty($attributes); |
||
| 637 | $this->assertEquals('JSON', $format); |
||
| 638 | $this->assertEmpty($proxies); |
||
| 639 | $this->assertNull($iou); |
||
| 640 | |||
| 641 | return 'authSuccessResponse called'; |
||
| 642 | } |
||
| 643 | ) |
||
| 644 | ->once() |
||
| 645 | ->getMock(); |
||
| 646 | |||
| 647 | $method = self::getNonPublicMethod($controller, 'casValidate'); |
||
| 648 | $this->assertEquals('authSuccessResponse called', $method->invokeArgs($controller, [$request, false, false])); |
||
| 649 | } |
||
| 650 | |||
| 651 | public function testCasValidateWithValidTicketAndServiceAndPgtButCallPgtUrlFailed() |
||
| 652 | { |
||
| 653 | $request = $this->getValidRequest('http://app1.com/pgtCallback'); |
||
| 654 | $user = Mockery::mock(User::class) |
||
| 655 | ->shouldReceive('getName') |
||
| 656 | ->andReturn('test_user') |
||
| 657 | ->once() |
||
| 658 | ->getMock(); |
||
| 659 | $ticket = Mockery::mock(Ticket::class) |
||
| 660 | ->shouldReceive('isProxy') |
||
| 661 | ->andReturn(false) |
||
| 662 | ->times(2) |
||
| 663 | ->shouldReceive('getAttribute') |
||
| 664 | ->withArgs(['service_url']) |
||
| 665 | ->andReturn('http://leo108.com') |
||
| 666 | ->once() |
||
| 667 | ->shouldReceive('getAttribute') |
||
| 668 | ->withArgs(['user']) |
||
| 669 | ->andReturn($user) |
||
| 670 | ->times(2) |
||
| 671 | ->getMock(); |
||
| 672 | $ticketRepository = Mockery::mock(TicketRepository::class) |
||
| 673 | ->shouldReceive('getByTicket') |
||
| 674 | ->andReturn($ticket) |
||
| 675 | ->once() |
||
| 676 | ->shouldReceive('invalidTicket') |
||
| 677 | ->once() |
||
| 678 | ->getMock(); |
||
| 679 | app()->instance(TicketRepository::class, $ticketRepository); |
||
| 680 | $pgTicketRepository = Mockery::mock(PGTicketRepository::class) |
||
| 681 | ->shouldReceive('applyTicket') |
||
| 682 | ->andReturn('some string') |
||
| 683 | ->once() |
||
| 684 | ->getMock(); |
||
| 685 | app()->instance(PGTicketRepository::class, $pgTicketRepository); |
||
| 686 | $ticketGenerator = Mockery::mock(TicketGenerator::class) |
||
| 687 | ->shouldReceive('generateOne') |
||
| 688 | ->andReturn('pgtiou string') |
||
| 689 | ->once() |
||
| 690 | ->getMock(); |
||
| 691 | app()->instance(TicketGenerator::class, $ticketGenerator); |
||
| 692 | $pgtCaller = Mockery::mock(PGTCaller::class) |
||
| 693 | ->shouldReceive('call') |
||
| 694 | ->andReturn(false) |
||
| 695 | ->once() |
||
| 696 | ->getMock(); |
||
| 697 | app()->instance(PGTCaller::class, $pgtCaller); |
||
| 698 | $controller = $this->initController() |
||
| 699 | ->makePartial() |
||
| 700 | ->shouldAllowMockingProtectedMethods() |
||
| 701 | ->shouldReceive('lockTicket') |
||
| 702 | ->andReturn(true) |
||
| 703 | ->once() |
||
| 704 | ->shouldReceive('unlockTicket') |
||
| 705 | ->once() |
||
| 706 | ->shouldReceive('authSuccessResponse') |
||
| 707 | ->andReturnUsing( |
||
| 708 | function ($name, $format, $attributes, $proxies, $iou) { |
||
| 709 | $this->assertEquals('test_user', $name); |
||
| 710 | $this->assertEmpty($attributes); |
||
| 711 | $this->assertEquals('JSON', $format); |
||
| 712 | $this->assertEmpty($proxies); |
||
| 713 | $this->assertNull($iou); |
||
| 714 | |||
| 715 | return 'authSuccessResponse called'; |
||
| 716 | } |
||
| 717 | ) |
||
| 718 | ->once() |
||
| 719 | ->getMock(); |
||
| 720 | |||
| 721 | $method = self::getNonPublicMethod($controller, 'casValidate'); |
||
| 722 | $this->assertEquals('authSuccessResponse called', $method->invokeArgs($controller, [$request, false, false])); |
||
| 723 | } |
||
| 724 | |||
| 725 | public function testCasValidateWithValidTicketAndServiceAndPgtButCallPgtUrlSuccess() |
||
| 726 | { |
||
| 727 | $request = $this->getValidRequest('http://app1.com/pgtCallback'); |
||
| 728 | $user = Mockery::mock(User::class) |
||
| 729 | ->shouldReceive('getName') |
||
| 730 | ->andReturn('test_user') |
||
| 731 | ->once() |
||
| 732 | ->getMock(); |
||
| 733 | $ticket = Mockery::mock(Ticket::class) |
||
| 734 | ->shouldReceive('isProxy') |
||
| 735 | ->andReturn(false) |
||
| 736 | ->times(2) |
||
| 737 | ->shouldReceive('getAttribute') |
||
| 738 | ->withArgs(['service_url']) |
||
| 739 | ->andReturn('http://leo108.com') |
||
| 740 | ->once() |
||
| 741 | ->shouldReceive('getAttribute') |
||
| 742 | ->withArgs(['user']) |
||
| 743 | ->andReturn($user) |
||
| 744 | ->times(2) |
||
| 745 | ->getMock(); |
||
| 746 | $ticketRepository = Mockery::mock(TicketRepository::class) |
||
| 747 | ->shouldReceive('getByTicket') |
||
| 748 | ->andReturn($ticket) |
||
| 749 | ->once() |
||
| 750 | ->shouldReceive('invalidTicket') |
||
| 751 | ->once() |
||
| 752 | ->getMock(); |
||
| 753 | app()->instance(TicketRepository::class, $ticketRepository); |
||
| 754 | $pgTicketRepository = Mockery::mock(PGTicketRepository::class) |
||
| 755 | ->shouldReceive('applyTicket') |
||
| 756 | ->andReturn('some string') |
||
| 757 | ->once() |
||
| 758 | ->getMock(); |
||
| 759 | app()->instance(PGTicketRepository::class, $pgTicketRepository); |
||
| 760 | $ticketGenerator = Mockery::mock(TicketGenerator::class) |
||
| 761 | ->shouldReceive('generateOne') |
||
| 762 | ->andReturn('pgtiou string') |
||
| 763 | ->once() |
||
| 764 | ->getMock(); |
||
| 765 | app()->instance(TicketGenerator::class, $ticketGenerator); |
||
| 766 | $pgtCaller = Mockery::mock(PGTCaller::class) |
||
| 767 | ->shouldReceive('call') |
||
| 768 | ->andReturn(true) |
||
| 769 | ->once() |
||
| 770 | ->getMock(); |
||
| 771 | app()->instance(PGTCaller::class, $pgtCaller); |
||
| 772 | $controller = $this->initController() |
||
| 773 | ->makePartial() |
||
| 774 | ->shouldAllowMockingProtectedMethods() |
||
| 775 | ->shouldReceive('lockTicket') |
||
| 776 | ->andReturn(true) |
||
| 777 | ->once() |
||
| 778 | ->shouldReceive('unlockTicket') |
||
| 779 | ->once() |
||
| 780 | ->shouldReceive('authSuccessResponse') |
||
| 781 | ->andReturnUsing( |
||
| 782 | function ($name, $format, $attributes, $proxies, $iou) { |
||
| 783 | $this->assertEquals('test_user', $name); |
||
| 784 | $this->assertEmpty($attributes); |
||
| 785 | $this->assertEquals('JSON', $format); |
||
| 786 | $this->assertEmpty($proxies); |
||
| 787 | $this->assertEquals('pgtiou string', $iou); |
||
| 788 | |||
| 789 | return 'authSuccessResponse called'; |
||
| 790 | } |
||
| 791 | ) |
||
| 792 | ->once() |
||
| 793 | ->getMock(); |
||
| 794 | |||
| 795 | $method = self::getNonPublicMethod($controller, 'casValidate'); |
||
| 796 | $this->assertEquals('authSuccessResponse called', $method->invokeArgs($controller, [$request, false, false])); |
||
| 797 | } |
||
| 798 | |||
| 799 | public function testCasValidateWithValidProxyTicketAndServiceAndPgtButCallPgtUrlSuccess() |
||
| 800 | { |
||
| 801 | $request = $this->getValidRequest('http://app1.com/pgtCallback'); |
||
| 802 | $user = Mockery::mock(User::class) |
||
| 803 | ->shouldReceive('getName') |
||
| 804 | ->andReturn('test_user') |
||
| 805 | ->once() |
||
| 806 | ->getMock(); |
||
| 807 | $ticket = Mockery::mock(Ticket::class) |
||
| 808 | ->shouldReceive('isProxy') |
||
| 809 | ->andReturn(false) |
||
| 810 | ->times(2) |
||
| 811 | ->shouldReceive('getAttribute') |
||
| 812 | ->withArgs(['service_url']) |
||
| 813 | ->andReturn('http://leo108.com') |
||
| 814 | ->once() |
||
| 815 | ->shouldReceive('getAttribute') |
||
| 816 | ->withArgs(['user']) |
||
| 817 | ->andReturn($user) |
||
| 818 | ->times(2) |
||
| 819 | ->getMock(); |
||
| 820 | $ticketRepository = Mockery::mock(TicketRepository::class) |
||
| 821 | ->shouldReceive('getByTicket') |
||
| 822 | ->andReturn($ticket) |
||
| 823 | ->once() |
||
| 824 | ->shouldReceive('invalidTicket') |
||
| 825 | ->once() |
||
| 826 | ->getMock(); |
||
| 827 | app()->instance(TicketRepository::class, $ticketRepository); |
||
| 828 | $pgTicketRepository = Mockery::mock(PGTicketRepository::class) |
||
| 829 | ->shouldReceive('applyTicket') |
||
| 830 | ->andReturn('some string') |
||
| 831 | ->once() |
||
| 832 | ->getMock(); |
||
| 833 | app()->instance(PGTicketRepository::class, $pgTicketRepository); |
||
| 834 | $ticketGenerator = Mockery::mock(TicketGenerator::class) |
||
| 835 | ->shouldReceive('generateOne') |
||
| 836 | ->andReturn('pgtiou string') |
||
| 837 | ->once() |
||
| 838 | ->getMock(); |
||
| 839 | app()->instance(TicketGenerator::class, $ticketGenerator); |
||
| 840 | $pgtCaller = Mockery::mock(PGTCaller::class) |
||
| 841 | ->shouldReceive('call') |
||
| 842 | ->andReturn(true) |
||
| 843 | ->once() |
||
| 844 | ->getMock(); |
||
| 845 | app()->instance(PGTCaller::class, $pgtCaller); |
||
| 846 | $controller = $this->initController() |
||
| 847 | ->makePartial() |
||
| 848 | ->shouldAllowMockingProtectedMethods() |
||
| 849 | ->shouldReceive('lockTicket') |
||
| 850 | ->andReturn(true) |
||
| 851 | ->once() |
||
| 852 | ->shouldReceive('unlockTicket') |
||
| 853 | ->once() |
||
| 854 | ->shouldReceive('authSuccessResponse') |
||
| 855 | ->andReturnUsing( |
||
| 856 | function ($name, $format, $attributes, $proxies, $iou) { |
||
| 857 | $this->assertEquals('test_user', $name); |
||
| 858 | $this->assertEmpty($attributes); |
||
| 859 | $this->assertEquals('JSON', $format); |
||
| 860 | $this->assertEmpty($proxies); |
||
| 861 | $this->assertEquals('pgtiou string', $iou); |
||
| 862 | |||
| 863 | return 'authSuccessResponse called'; |
||
| 864 | } |
||
| 865 | ) |
||
| 866 | ->once() |
||
| 867 | ->getMock(); |
||
| 868 | |||
| 869 | $method = self::getNonPublicMethod($controller, 'casValidate'); |
||
| 870 | $this->assertEquals('authSuccessResponse called', $method->invokeArgs($controller, [$request, false, false])); |
||
| 871 | } |
||
| 872 | |||
| 873 | public function testLockTicket() |
||
| 874 | { |
||
| 875 | $locker = Mockery::mock(TicketLocker::class) |
||
| 876 | ->shouldReceive('acquireLock') |
||
| 877 | ->andReturn('acquireLock called') |
||
| 878 | ->once() |
||
| 879 | ->getMock(); |
||
| 880 | app()->instance(TicketLocker::class, $locker); |
||
| 881 | $controller = $this->initController() |
||
| 882 | ->makePartial(); |
||
| 883 | $method = self::getNonPublicMethod($controller, 'lockTicket'); |
||
| 884 | $this->assertEquals('acquireLock called', $method->invokeArgs($controller, ['str', 30])); |
||
| 885 | } |
||
| 886 | |||
| 887 | public function testUnlockTicket() |
||
| 888 | { |
||
| 889 | $locker = Mockery::mock(TicketLocker::class) |
||
| 890 | ->shouldReceive('releaseLock') |
||
| 891 | ->andReturn('releaseLock called') |
||
| 892 | ->once() |
||
| 893 | ->getMock(); |
||
| 894 | app()->instance(TicketLocker::class, $locker); |
||
| 895 | $controller = $this->initController() |
||
| 896 | ->makePartial(); |
||
| 897 | $method = self::getNonPublicMethod($controller, 'unlockTicket'); |
||
| 898 | $this->assertEquals('releaseLock called', $method->invokeArgs($controller, ['str', 30])); |
||
| 899 | } |
||
| 900 | |||
| 901 | public function testAuthSuccessResponse() |
||
| 902 | { |
||
| 903 | $controller = Mockery::mock(ValidateController::class) |
||
| 904 | ->makePartial(); |
||
| 905 | $method = self::getNonPublicMethod($controller, 'authSuccessResponse'); |
||
| 906 | |||
| 907 | $name = 'test_name'; |
||
| 908 | $attributes = [ |
||
| 909 | 'real_name' => 'real_name', |
||
| 910 | ]; |
||
| 911 | $proxies = ['http://proxy1.com']; |
||
| 912 | $pgt = 'ticket'; |
||
| 913 | $jsonResp = Mockery::mock(JsonAuthenticationSuccessResponse::class) |
||
| 914 | ->shouldReceive('setUser') |
||
| 915 | ->with($name) |
||
| 916 | ->once() |
||
| 917 | ->shouldReceive('setAttributes') |
||
| 918 | ->with($attributes) |
||
| 919 | ->once() |
||
| 920 | ->shouldReceive('setProxies') |
||
| 921 | ->with($proxies) |
||
| 922 | ->once() |
||
| 923 | ->shouldReceive('toResponse') |
||
| 924 | ->once() |
||
| 925 | ->getMock(); |
||
| 926 | app()->instance(JsonAuthenticationSuccessResponse::class, $jsonResp); |
||
| 927 | $method->invokeArgs($controller, ['test_name', 'JSON', $attributes, $proxies, []]); |
||
| 928 | |||
| 929 | $xmlResp = Mockery::mock(XmlAuthenticationSuccessResponse::class) |
||
| 930 | ->shouldReceive('setUser') |
||
| 931 | ->with($name) |
||
| 932 | ->once() |
||
| 933 | ->shouldReceive('setAttributes') |
||
| 934 | ->with($attributes) |
||
| 935 | ->once() |
||
| 936 | ->shouldReceive('setProxies') |
||
| 937 | ->with($proxies) |
||
| 938 | ->once() |
||
| 939 | ->shouldReceive('setProxyGrantingTicket') |
||
| 940 | ->with($pgt) |
||
| 941 | ->once() |
||
| 942 | ->shouldReceive('toResponse') |
||
| 943 | ->once() |
||
| 944 | ->getMock(); |
||
| 945 | app()->instance(XmlAuthenticationSuccessResponse::class, $xmlResp); |
||
| 946 | $method->invokeArgs($controller, ['test_name', 'XML', $attributes, $proxies, $pgt]); |
||
| 947 | } |
||
| 948 | |||
| 949 | public function testAuthFailureResponse() |
||
| 950 | { |
||
| 951 | $controller = Mockery::mock(ValidateController::class) |
||
| 952 | ->makePartial(); |
||
| 953 | $method = self::getNonPublicMethod($controller, 'authFailureResponse'); |
||
| 954 | $code = 'code'; |
||
| 955 | $desc = 'desc'; |
||
| 956 | $jsonResp = Mockery::mock(JsonAuthenticationFailureResponse::class) |
||
| 957 | ->shouldReceive('setFailure') |
||
| 958 | ->withArgs([$code, $desc]) |
||
| 959 | ->once() |
||
| 960 | ->shouldReceive('toResponse') |
||
| 961 | ->once() |
||
| 962 | ->getMock(); |
||
| 963 | app()->instance(JsonAuthenticationFailureResponse::class, $jsonResp); |
||
| 964 | $method->invokeArgs($controller, [$code, $desc, 'JSON']); |
||
| 965 | |||
| 966 | $xmlResp = Mockery::mock(XmlAuthenticationFailureResponse::class) |
||
| 967 | ->shouldReceive('setFailure') |
||
| 968 | ->withArgs([$code, $desc]) |
||
| 969 | ->once() |
||
| 970 | ->shouldReceive('toResponse') |
||
| 971 | ->once() |
||
| 972 | ->getMock(); |
||
| 973 | app()->instance(XmlAuthenticationFailureResponse::class, $xmlResp); |
||
| 974 | $method->invokeArgs($controller, [$code, $desc, 'XML']); |
||
| 975 | } |
||
| 976 | |||
| 977 | protected function getValidRequest($pgt = null) |
||
| 1015 |