256 lines
8.0 KiB
PHP
256 lines
8.0 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Models\Room;
|
|
use App\Models\Sysparam;
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Illuminate\Support\Facades\Redis;
|
|
use Tests\TestCase;
|
|
|
|
class UserControllerTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
Sysparam::updateOrCreate(['alias' => 'superlevel'], ['body' => '100']);
|
|
Sysparam::updateOrCreate(['alias' => 'level_kick'], ['body' => '15']);
|
|
Sysparam::updateOrCreate(['alias' => 'level_mute'], ['body' => '15']);
|
|
Sysparam::updateOrCreate(['alias' => 'level_ban'], ['body' => '15']);
|
|
Sysparam::updateOrCreate(['alias' => 'level_banip'], ['body' => '15']);
|
|
Sysparam::updateOrCreate(['alias' => 'smtp_enabled'], ['body' => '1']); // Allow email changing in tests
|
|
}
|
|
|
|
public function test_can_view_user_profile()
|
|
{
|
|
$user = User::factory()->create([
|
|
'username' => 'testuser',
|
|
'user_level' => 10,
|
|
]);
|
|
|
|
$this->actingAs($user);
|
|
|
|
$response = $this->getJson("/user/{$user->username}");
|
|
|
|
$response->assertStatus(200)
|
|
->assertJsonPath('data.username', 'testuser')
|
|
->assertJsonPath('data.user_level', 10);
|
|
}
|
|
|
|
public function test_can_update_profile_without_email_change()
|
|
{
|
|
$user = User::factory()->create([
|
|
'username' => 'testuser',
|
|
'email' => 'old@example.com',
|
|
'sign' => 'old sign',
|
|
]);
|
|
|
|
$this->actingAs($user);
|
|
|
|
$response = $this->putJson('/user/profile', [
|
|
'email' => 'old@example.com',
|
|
'sign' => 'new sign',
|
|
'sex' => 1,
|
|
'headface' => 'avatar1.png',
|
|
]);
|
|
|
|
$response->assertStatus(200)
|
|
->assertJsonPath('status', 'success');
|
|
|
|
$user->refresh();
|
|
$this->assertEquals('new sign', $user->sign);
|
|
}
|
|
|
|
public function test_cannot_update_email_without_verification_code()
|
|
{
|
|
$user = User::factory()->create([
|
|
'username' => 'testuser',
|
|
'email' => 'old@example.com',
|
|
]);
|
|
|
|
$this->actingAs($user);
|
|
|
|
$response = $this->putJson('/user/profile', [
|
|
'email' => 'new@example.com',
|
|
'sex' => 1,
|
|
'headface' => 'avatar1.png',
|
|
]);
|
|
|
|
$response->assertStatus(422)
|
|
->assertJsonPath('status', 'error')
|
|
->assertJsonPath('message', '新邮箱需要验证码,请先获取并填写验证码。');
|
|
}
|
|
|
|
public function test_can_update_email_with_valid_code()
|
|
{
|
|
$user = User::factory()->create([
|
|
'username' => 'testuser',
|
|
'email' => 'old@example.com',
|
|
]);
|
|
|
|
Cache::put("email_verify_code_{$user->id}_new@example.com", '123456', 5);
|
|
|
|
$this->actingAs($user);
|
|
|
|
$response = $this->putJson('/user/profile', [
|
|
'email' => 'new@example.com',
|
|
'email_code' => '123456',
|
|
'sex' => 1,
|
|
'headface' => 'avatar1.png',
|
|
]);
|
|
|
|
$response->assertStatus(200);
|
|
|
|
$user->refresh();
|
|
$this->assertEquals('new@example.com', $user->email);
|
|
}
|
|
|
|
public function test_can_change_password()
|
|
{
|
|
$user = User::factory()->create([
|
|
'username' => 'testuser',
|
|
'password' => Hash::make('oldpassword'),
|
|
]);
|
|
|
|
$this->actingAs($user);
|
|
|
|
$response = $this->putJson('/user/password', [
|
|
'old_password' => 'oldpassword',
|
|
'new_password' => 'newpassword123',
|
|
'new_password_confirmation' => 'newpassword123',
|
|
]);
|
|
|
|
$response->assertStatus(200)
|
|
->assertJsonPath('status', 'success');
|
|
|
|
$user->refresh();
|
|
$this->assertTrue(Hash::check('newpassword123', $user->password));
|
|
}
|
|
|
|
public function test_admin_can_kick_user()
|
|
{
|
|
$admin = User::factory()->create(['username' => 'admin', 'user_level' => 20]);
|
|
$target = User::factory()->create(['username' => 'target', 'user_level' => 1]);
|
|
$room = Room::create(['id' => 1, 'room_name' => 'Test Room', 'room_owner' => 'someone']);
|
|
|
|
$this->actingAs($admin);
|
|
|
|
$response = $this->postJson("/user/{$target->username}/kick", [
|
|
'room_id' => $room->id,
|
|
]);
|
|
|
|
$response->assertStatus(200)
|
|
->assertJsonPath('status', 'success');
|
|
}
|
|
|
|
public function test_low_level_user_cannot_kick()
|
|
{
|
|
$user = User::factory()->create(['username' => 'user', 'user_level' => 1]);
|
|
$target = User::factory()->create(['username' => 'target', 'user_level' => 1]);
|
|
$room = Room::create(['id' => 1, 'room_name' => 'Test Room', 'room_owner' => 'someone']);
|
|
|
|
$this->actingAs($user);
|
|
|
|
$response = $this->postJson("/user/{$target->username}/kick", [
|
|
'room_id' => $room->id,
|
|
]);
|
|
|
|
$response->assertStatus(403);
|
|
}
|
|
|
|
public function test_room_master_can_kick()
|
|
{
|
|
$user = User::factory()->create(['username' => 'user', 'user_level' => 2]);
|
|
$target = User::factory()->create(['username' => 'target', 'user_level' => 1]);
|
|
$room = Room::create(['id' => 1, 'room_name' => 'Test Room', 'room_owner' => 'user']); // Master is 'user'
|
|
|
|
$this->actingAs($user);
|
|
|
|
$response = $this->postJson("/user/{$target->username}/kick", [
|
|
'room_id' => $room->id,
|
|
]);
|
|
|
|
if ($response->status() !== 200) {
|
|
dump($response->json());
|
|
}
|
|
$response->assertStatus(200);
|
|
}
|
|
|
|
public function test_cannot_kick_higher_level()
|
|
{
|
|
$admin = User::factory()->create(['username' => 'admin', 'user_level' => 20]);
|
|
$superadmin = User::factory()->create(['username' => 'superadmin', 'user_level' => 100]);
|
|
$room = Room::create(['id' => 1, 'room_name' => 'Test Room', 'room_owner' => 'someone']);
|
|
|
|
$this->actingAs($admin);
|
|
|
|
$response = $this->postJson("/user/{$superadmin->username}/kick", [
|
|
'room_id' => $room->id,
|
|
]);
|
|
|
|
$response->assertStatus(403);
|
|
}
|
|
|
|
public function test_admin_can_mute_user()
|
|
{
|
|
$admin = User::factory()->create(['username' => 'admin', 'user_level' => 20]);
|
|
$target = User::factory()->create(['username' => 'target', 'user_level' => 1]);
|
|
$room = Room::create(['id' => 1, 'room_name' => 'Test Room', 'room_owner' => 'someone']);
|
|
|
|
Redis::shouldReceive('setex')->once();
|
|
|
|
$this->actingAs($admin);
|
|
|
|
$response = $this->postJson("/user/{$target->username}/mute", [
|
|
'room_id' => $room->id,
|
|
'duration' => 10,
|
|
]);
|
|
|
|
$response->assertStatus(200);
|
|
}
|
|
|
|
public function test_admin_can_ban_user()
|
|
{
|
|
$admin = User::factory()->create(['username' => 'admin', 'user_level' => 20]);
|
|
$target = User::factory()->create(['username' => 'target', 'user_level' => 1]);
|
|
$room = Room::create(['id' => 1, 'room_name' => 'Test Room', 'room_owner' => 'someone']);
|
|
|
|
$this->actingAs($admin);
|
|
|
|
$response = $this->postJson("/user/{$target->username}/ban", [
|
|
'room_id' => $room->id,
|
|
]);
|
|
|
|
$response->assertStatus(200);
|
|
|
|
$target->refresh();
|
|
$this->assertEquals(-1, $target->user_level);
|
|
}
|
|
|
|
public function test_admin_can_ban_ip()
|
|
{
|
|
$admin = User::factory()->create(['username' => 'admin', 'user_level' => 20]);
|
|
$target = User::factory()->create(['username' => 'target', 'user_level' => 1, 'last_ip' => '192.168.1.100']);
|
|
$room = Room::create(['id' => 1, 'room_name' => 'Test Room', 'room_owner' => 'someone']);
|
|
|
|
Redis::shouldReceive('sadd')->with('banned_ips', '192.168.1.100')->once();
|
|
|
|
$this->actingAs($admin);
|
|
|
|
$response = $this->postJson("/user/{$target->username}/banip", [
|
|
'room_id' => $room->id,
|
|
]);
|
|
|
|
$response->assertStatus(200);
|
|
|
|
$target->refresh();
|
|
$this->assertEquals(-1, $target->user_level);
|
|
}
|
|
}
|