From d895636d0e60ba205e38943937e1e1b5cd4e23c0 Mon Sep 17 00:00:00 2001 From: leo108 Date: Thu, 13 Oct 2016 06:43:26 +0800 Subject: [PATCH] add more tests && fix some bug --- readme.md | 4 + src/Http/Controllers/SecurityController.php | 7 ++ src/Repositories/TicketRepository.php | 2 +- .../Controllers/SecurityControllerTest.php | 75 +++++++++++++++++++ 4 files changed, 87 insertions(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 04be2aa..3ea85f8 100644 --- a/readme.md +++ b/readme.md @@ -25,3 +25,7 @@ Currently this package works for Laravel 5.1/5.2/5.3 . - create a class implements `Leo108\CAS\Contracts\TicketLocker` - create a class implements `Leo108\CAS\Contracts\Interactions\UserLogin` - visit `http://your-domain/cas/login` to see the login page (assume that you didn't change the `router.prefix` value in `config/cas.php`) + +## Example + +If you are looking for an out of box solution of CAS Server powered by PHP, you can refer to [php_cas_server](https://github.com/leo108/php_cas_server) \ No newline at end of file diff --git a/src/Http/Controllers/SecurityController.php b/src/Http/Controllers/SecurityController.php index 1be45c8..f3f9c39 100644 --- a/src/Http/Controllers/SecurityController.php +++ b/src/Http/Controllers/SecurityController.php @@ -91,6 +91,13 @@ public function login(Request $request) public function authenticated(Request $request) { $user = $this->loginInteraction->getCurrentUser($request); + if ($user === null) { + //unreachable code + throw new CasException( + CasException::INTERNAL_ERROR, + 'should call authenticated only after getCurrentUser return not null' + ); + } event(new CasUserLoginEvent($request, $user)); $serviceUrl = $request->get('service', ''); if (!empty($serviceUrl)) { diff --git a/src/Repositories/TicketRepository.php b/src/Repositories/TicketRepository.php index bf61d6a..01d998f 100644 --- a/src/Repositories/TicketRepository.php +++ b/src/Repositories/TicketRepository.php @@ -50,7 +50,7 @@ public function applyTicket(UserModel $user, $serviceUrl) throw new CasException(CasException::INVALID_SERVICE); } $ticket = $this->getAvailableTicket(config('cas.ticket_len', 32)); - if (!$ticket) { + if ($ticket === false) { throw new CasException(CasException::INTERNAL_ERROR, 'apply ticket failed'); } $record = $this->ticket->newInstance( diff --git a/tests/Http/Controllers/SecurityControllerTest.php b/tests/Http/Controllers/SecurityControllerTest.php index 2896c4b..cf2f74e 100644 --- a/tests/Http/Controllers/SecurityControllerTest.php +++ b/tests/Http/Controllers/SecurityControllerTest.php @@ -20,8 +20,22 @@ use Mockery; use User; +//mock function +function cas_route($name, $query) +{ + return SecurityControllerTest::$functions->cas_route($name, $query); +} + class SecurityControllerTest extends TestCase { + public static $functions; + + public function setUp() + { + parent::setUp(); + self::$functions = Mockery::mock(); + } + public function testShowLogin() { //not logged in with valid service url @@ -104,6 +118,67 @@ function ($request, $errors) { ->andReturn('authenticated called') ->getMock(); $this->assertEquals('authenticated called', $controller->showLogin($request)); + + //logged in with valid service url with warn parameter + $serviceRepository = Mockery::mock(ServiceRepository::class) + ->shouldReceive('isUrlValid') + ->andReturn(true) + ->getMock(); + app()->instance(ServiceRepository::class, $serviceRepository); + $ticketRepository = Mockery::mock(TicketRepository::class); + app()->instance(TicketRepository::class, $ticketRepository); + $loginInteraction = Mockery::mock(UserLogin::class) + ->shouldReceive('getCurrentUser') + ->andReturn(true)//just not false is OK + ->shouldReceive('showLoginWarnPage') + ->andReturn('showLoginWarnPage called') + ->getMock(); + app()->instance(UserLogin::class, $loginInteraction); + $request = Mockery::mock(Request::class) + ->shouldReceive('get') + ->withArgs(['service', '']) + ->andReturn('what ever') + ->shouldReceive('get') + ->withArgs(['warn']) + ->andReturn('true') + ->getMock(); + $request->query = Mockery::mock() + ->shouldReceive('all') + ->andReturn([]) + ->getMock(); + self::$functions->shouldReceive('cas_route')->andReturn('some string'); + $controller = app()->make(SecurityController::class); + $this->assertEquals('showLoginWarnPage called', $controller->showLogin($request)); + + //logged in with invalid service url + $serviceRepository = Mockery::mock(ServiceRepository::class) + ->shouldReceive('isUrlValid') + ->andReturn(false) + ->getMock(); + app()->instance(ServiceRepository::class, $serviceRepository); + $ticketRepository = Mockery::mock(TicketRepository::class); + app()->instance(TicketRepository::class, $ticketRepository); + $loginInteraction = Mockery::mock(UserLogin::class) + ->shouldReceive('getCurrentUser') + ->andReturn(true)//just not false is OK + ->shouldReceive('redirectToHome') + ->andReturnUsing( + function ($errors) { + $this->assertNotEmpty($errors); + $this->assertEquals(CasException::INVALID_SERVICE, $errors[0]); + + return 'redirectToHome called'; + } + ) + ->getMock(); + app()->instance(UserLogin::class, $loginInteraction); + $request = Mockery::mock(Request::class) + ->shouldReceive('get') + ->withArgs(['service', '']) + ->andReturn('what ever') + ->getMock(); + $controller = app()->make(SecurityController::class); + $this->assertEquals('redirectToHome called', $controller->showLogin($request)); } public function testAuthenticated()