-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Two separate exception class will thrown
- Loading branch information
1 parent
503baba
commit 2d60c16
Showing
6 changed files
with
98 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Aammui\RolePermission\Exception; | ||
|
||
use Symfony\Component\HttpKernel\Exception\HttpException; | ||
|
||
class RoleDoesNotExistException extends HttpException | ||
{ | ||
public function __construct() | ||
{ | ||
parent::__construct(403, 'User does not have the right roles.', null, []); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Aammui\RolePermission\Exception; | ||
|
||
use Symfony\Component\HttpKernel\Exception\HttpException; | ||
|
||
class UserNotLoginException extends HttpException | ||
{ | ||
public function __construct() | ||
{ | ||
parent::__construct(403, 'User is not logged in.', null, []); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,9 +2,10 @@ | |
|
||
namespace Aammui\RolePermission\Tests; | ||
|
||
use Aammui\RolePermission\Exception\RoleDoesNotExistException; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Response; | ||
use Aammui\RolePermission\Exception\UnauthorizedException; | ||
use Aammui\RolePermission\Exception\UserNotLoginException; | ||
use Aammui\RolePermission\Middleware\Role as RoleMiddleware; | ||
use Illuminate\Support\Facades\Auth; | ||
|
||
|
@@ -19,18 +20,6 @@ public function setUp(): void | |
$this->roleMiddleware = new RoleMiddleware(); | ||
} | ||
|
||
/** @test */ | ||
public function a_guest_cannot_access_a_route_protected_by_rolemiddleware() | ||
{ | ||
$this->assertEquals( | ||
$this->runMiddleware( | ||
$this->roleMiddleware, | ||
'testRole' | ||
), | ||
403 | ||
); | ||
} | ||
|
||
/** @test */ | ||
public function a_user_can_access_a_route_protected_by_role_middleware_if_have_this_role() | ||
{ | ||
|
@@ -51,12 +40,39 @@ public function a_user_can_access_a_route_protected_by_role_middleware_if_have_t | |
|
||
protected function runMiddleware($middleware, $parameter) | ||
{ | ||
try { | ||
return $middleware->handle(new Request(), function () { | ||
return (new Response())->setContent('<html></html>'); | ||
}, $parameter)->status(); | ||
} catch (UnauthorizedException $e) { | ||
return $e->getStatusCode(); | ||
} | ||
return $middleware->handle(new Request(), function () { | ||
return (new Response())->setContent('<html></html>'); | ||
}, $parameter)->status(); | ||
} | ||
|
||
/** | ||
* A guest cannot access a route protected by rolemiddleware | ||
* | ||
* @test | ||
*/ | ||
public function UserNotLogin_exception_is_thrown_when_user_not_login() | ||
{ | ||
$this->expectException(UserNotLoginException::class); | ||
|
||
$this->roleMiddleware->handle(new Request(), function () { | ||
return (new Response())->setContent('<html></html>'); | ||
}, 'testRole')->status(); | ||
} | ||
|
||
/** | ||
* A User cannot access a route protected by rolemiddleware | ||
* | ||
* @test | ||
*/ | ||
public function RoleDoesNotExist_exception_is_thrown_when_user_not_login() | ||
{ | ||
$this->expectException(RoleDoesNotExistException::class); | ||
|
||
$user = User::create(['email' => '[email protected]']); | ||
Auth::login($user); | ||
|
||
$this->roleMiddleware->handle(new Request(), function () { | ||
return (new Response())->setContent('<html></html>'); | ||
}, 'testRole')->status(); | ||
} | ||
} |