Skip to content

Commit

Permalink
add more tests && fix some bug
Browse files Browse the repository at this point in the history
  • Loading branch information
leo108 committed Oct 12, 2016
1 parent fe270bf commit d895636
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 1 deletion.
4 changes: 4 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
7 changes: 7 additions & 0 deletions src/Http/Controllers/SecurityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Repositories/TicketRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
75 changes: 75 additions & 0 deletions tests/Http/Controllers/SecurityControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit d895636

Please sign in to comment.