add api tests
This commit is contained in:
@@ -33,7 +33,13 @@ class RoleSwitcherControllerTest extends TestCase
|
||||
public function test_switch_sets_role(): void
|
||||
{
|
||||
$user = $this->seedUser();
|
||||
$this->seedRole();
|
||||
$roleId = $this->seedRole();
|
||||
|
||||
DB::table('user_roles')->insert([
|
||||
'user_id' => $user->id,
|
||||
'role_id' => $roleId,
|
||||
]);
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$response = $this->postJson('/api/v1/role-switcher/switch', [
|
||||
@@ -52,6 +58,55 @@ class RoleSwitcherControllerTest extends TestCase
|
||||
$response->assertStatus(401);
|
||||
}
|
||||
|
||||
|
||||
public function test_switch_rejects_role_that_user_does_not_have(): void
|
||||
{
|
||||
$user = $this->seedUser();
|
||||
$this->seedRole();
|
||||
$teacherRoleId = DB::table('roles')->insertGetId([
|
||||
'name' => 'teacher',
|
||||
'slug' => 'teacher',
|
||||
'description' => 'Teacher',
|
||||
'dashboard_route' => 'teacher/classes',
|
||||
'priority' => 2,
|
||||
'is_active' => 1,
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
|
||||
DB::table('user_roles')->insert([
|
||||
'user_id' => $user->id,
|
||||
'role_id' => $teacherRoleId,
|
||||
]);
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
$this->postJson('/api/v1/role-switcher/switch', [
|
||||
'role' => 'admin',
|
||||
])
|
||||
->assertForbidden()
|
||||
->assertJsonPath('status', false);
|
||||
}
|
||||
|
||||
public function test_switch_validates_empty_role_payload(): void
|
||||
{
|
||||
$user = $this->seedUser();
|
||||
$this->seedRole();
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$this->postJson('/api/v1/role-switcher/switch', [])
|
||||
->assertUnprocessable();
|
||||
}
|
||||
|
||||
public function test_index_returns_not_found_when_authenticated_user_has_no_roles(): void
|
||||
{
|
||||
$user = $this->seedUser();
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$this->getJson('/api/v1/role-switcher')
|
||||
->assertNotFound()
|
||||
->assertJsonPath('status', false);
|
||||
}
|
||||
|
||||
private function seedUser(): User
|
||||
{
|
||||
$userId = DB::table('users')->insertGetId([
|
||||
|
||||
Reference in New Issue
Block a user