Skip to content

Commit 6095774

Browse files
committed
Add tests for the new middleware authorizing the routes
1 parent 08730b7 commit 6095774

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace JustBetter\Detour\Tests\Http\Middleware;
4+
5+
use JustBetter\Detour\Tests\TestCase;
6+
use PHPUnit\Framework\Attributes\Test;
7+
use Statamic\Facades\Role;
8+
use Statamic\Facades\User;
9+
10+
class AuthorizeDetoursTest extends TestCase
11+
{
12+
#[Test]
13+
public function it_forbids_users_without_the_detours_permission(): void
14+
{
15+
$role = Role::make('cp-only')->permissions(['access cp']);
16+
$role->save();
17+
18+
/** @var \Statamic\Auth\File\User $user */
19+
$user = User::make();
20+
$user
21+
->id('test-user-no-detours')
22+
->email('no-detours@example.com')
23+
->assignRole($role)
24+
->save();
25+
26+
$this->actingAs($user);
27+
28+
$this->get(cp_route('index'))->assertRedirect(cp_route('dashboard'));
29+
$this->get(cp_route('justbetter.detours.index'))->assertForbidden();
30+
}
31+
32+
#[Test]
33+
public function it_can_authorize_detours(): void
34+
{
35+
$permission = config()->string('justbetter.statamic-detour.permissions.access');
36+
37+
$role = Role::make('detours-access')->permissions(['access cp', $permission]);
38+
$role->save();
39+
40+
/** @var \Statamic\Auth\File\User $user */
41+
$user = User::make();
42+
$user
43+
->id('test-user-with-detours')
44+
->email('with-detours@example.com')
45+
->assignRole($role)
46+
->save();
47+
48+
$this->actingAs($user);
49+
50+
$this->get(cp_route('index'))->assertRedirect(cp_route('dashboard'));
51+
$this->get(cp_route('justbetter.detours.index'))->assertOk();
52+
}
53+
}

0 commit comments

Comments
 (0)