-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f7fbfae
commit 714bdd1
Showing
6 changed files
with
220 additions
and
23 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
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
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,81 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Authentication\Filters; | ||
|
||
use CodeIgniter\Shield\Entities\User; | ||
use CodeIgniter\Shield\Filters\GroupFilter; | ||
use CodeIgniter\Shield\Models\UserModel; | ||
use CodeIgniter\Test\DatabaseTestTrait; | ||
|
||
final class GroupFilterTest extends AbstractFilterTest | ||
{ | ||
use DatabaseTestTrait; | ||
|
||
protected string $alias = 'group'; | ||
protected string $classname = GroupFilter::class; | ||
protected string $routeFilter = 'group:admin'; | ||
|
||
public function testFilterNotAuthorized(): void | ||
{ | ||
$result = $this->call('get', 'protected-route'); | ||
|
||
$result->assertRedirectTo('/login'); | ||
|
||
$result = $this->get('open-route'); | ||
$result->assertStatus(200); | ||
$result->assertSee('Open'); | ||
} | ||
|
||
public function testFilterSuccess() | ||
{ | ||
/** @var User */ | ||
$user = fake(UserModel::class); | ||
$user->addGroup('admin'); | ||
|
||
$result = $this | ||
->actingAs($user) | ||
->get('protected-route'); | ||
|
||
$result->assertStatus(200); | ||
$result->assertSee('Protected'); | ||
|
||
$this->assertSame($user->id, auth('session')->id()); | ||
$this->assertSame($user->id, auth('session')->user()->id); | ||
} | ||
|
||
public function testFilterIncorrectGroupNoPrevious() | ||
{ | ||
/** @var User */ | ||
$user = fake(UserModel::class); | ||
$user->addGroup('beta'); | ||
|
||
$result = $this | ||
->actingAs($user) | ||
->get('protected-route'); | ||
|
||
// Should redirect to home page since previous_url is not set | ||
$result->assertRedirectTo(site_url('/')); | ||
// Should have error message | ||
$result->assertSessionHas('error'); | ||
} | ||
|
||
public function testFilterIncorrectGroupWithPrevious() | ||
{ | ||
/** @var User */ | ||
$user = fake(UserModel::class); | ||
$user->addGroup('beta'); | ||
|
||
$result = $this | ||
->actingAs($user) | ||
->withSession(['_ci_previous_url' => site_url('open-route')]) | ||
->get('protected-route'); | ||
|
||
// Should redirect to home page since previous_url is not set | ||
$result->assertRedirectTo(site_url('open-route')); | ||
|
||
$result->assertSessionHas('error'); | ||
} | ||
|
||
} |
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,81 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Authentication\Filters; | ||
|
||
use CodeIgniter\Shield\Entities\User; | ||
use CodeIgniter\Shield\Filters\PermissionFilter; | ||
use CodeIgniter\Shield\Models\UserModel; | ||
use CodeIgniter\Test\DatabaseTestTrait; | ||
|
||
final class PermissionFilterTest extends AbstractFilterTest | ||
{ | ||
use DatabaseTestTrait; | ||
|
||
protected string $alias = 'permission'; | ||
protected string $classname = PermissionFilter::class; | ||
protected string $routeFilter = 'permission:admin.access'; | ||
|
||
public function testFilterNotAuthorized(): void | ||
{ | ||
$result = $this->call('get', 'protected-route'); | ||
|
||
$result->assertRedirectTo('/login'); | ||
|
||
$result = $this->get('open-route'); | ||
$result->assertStatus(200); | ||
$result->assertSee('Open'); | ||
} | ||
|
||
public function testFilterSuccess() | ||
{ | ||
/** @var User */ | ||
$user = fake(UserModel::class); | ||
$user->addPermission('admin.access'); | ||
|
||
$result = $this | ||
->actingAs($user) | ||
->get('protected-route'); | ||
|
||
$result->assertStatus(200); | ||
$result->assertSee('Protected'); | ||
|
||
$this->assertSame($user->id, auth('session')->id()); | ||
$this->assertSame($user->id, auth('session')->user()->id); | ||
} | ||
|
||
public function testFilterIncorrectGroupNoPrevious() | ||
{ | ||
/** @var User */ | ||
$user = fake(UserModel::class); | ||
$user->addPermission('beta.access'); | ||
|
||
$result = $this | ||
->actingAs($user) | ||
->get('protected-route'); | ||
|
||
// Should redirect to home page since previous_url is not set | ||
$result->assertRedirectTo(site_url('/')); | ||
// Should have error message | ||
$result->assertSessionHas('error'); | ||
} | ||
|
||
public function testFilterIncorrectGroupWithPrevious() | ||
{ | ||
/** @var User */ | ||
$user = fake(UserModel::class); | ||
$user->addPermission('beta.access'); | ||
|
||
$result = $this | ||
->actingAs($user) | ||
->withSession(['_ci_previous_url' => site_url('open-route')]) | ||
->get('protected-route'); | ||
|
||
// Should redirect to home page since previous_url is not set | ||
$result->assertRedirectTo(site_url('open-route')); | ||
|
||
$result->assertSessionHas('error'); | ||
} | ||
|
||
} |