test user search and admin promote endpoints

This commit is contained in:
Yisroel Baum 2026-05-06 22:36:10 +03:00
parent ac7295faf3
commit 56136f8bcf
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9

View file

@ -0,0 +1,92 @@
<?php
namespace Tests\Feature\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\Feature\AuthenticatesUsers;
use Tests\TestCase;
class UserSearchAndPromoteTest extends TestCase
{
use AuthenticatesUsers;
use RefreshDatabase;
public function test_search_is_public(): void
{
$this->signupAndLogin(
email: 'alice@example.com',
displayName: 'alice',
password: 'longenoughpassword',
);
$this->signupAndLogin(
email: 'alex@example.com',
displayName: 'alex',
password: 'longenoughpassword',
);
$this->resetClientState();
$response = $this->getJson('/api/users?q=al');
$response->assertStatus(200);
$response->assertJsonCount(2, 'users');
}
public function test_search_with_no_query_returns_empty(): void
{
$response = $this->getJson('/api/users');
$response->assertStatus(200);
$response->assertJsonPath('users', []);
}
public function test_non_admin_cannot_promote(): void
{
$alice = $this->signupAndLogin(
email: 'alice@example.com',
displayName: 'alice',
password: 'longenoughpassword',
);
$bob = $this->signupAndLogin(
email: 'bob@example.com',
displayName: 'bob',
password: 'longenoughpassword',
);
$this->resetClientState();
$this->withCredentials()
->withUnencryptedCookie('auth_token', $alice['cookie'])
->postJson('/api/admin/users/promote', [
'userId' => $bob['user']->getId(),
])
->assertStatus(403);
}
public function test_admin_promotes_user(): void
{
$alice = $this->signupAndLogin(
email: 'alice@example.com',
displayName: 'alice',
password: 'longenoughpassword',
);
$bob = $this->signupAndLogin(
email: 'bob@example.com',
displayName: 'bob',
password: 'longenoughpassword',
);
$this->promoteToAdmin($alice['user']->getId());
$loginResponse = $this->postJson('/api/login', [
'email' => 'alice@example.com',
'password' => 'longenoughpassword',
]);
$aliceCookie = $loginResponse->getCookie('auth_token', false)
->getValue();
$this->resetClientState();
$this->withCredentials()
->withUnencryptedCookie('auth_token', $aliceCookie)
->postJson('/api/admin/users/promote', [
'userId' => $bob['user']->getId(),
])
->assertStatus(200)
->assertJsonPath('user.isAdmin', true);
}
}