blessing-skin-server/tests/AdminControllerTest.php

486 lines
16 KiB
PHP
Raw Normal View History

2017-11-02 16:50:00 +08:00
<?php
2018-08-17 15:25:08 +08:00
namespace Tests;
2017-11-02 16:50:00 +08:00
use App\Models\User;
use App\Models\Player;
use App\Models\Texture;
2019-02-27 23:44:50 +08:00
use Illuminate\Support\Str;
2017-11-02 16:50:00 +08:00
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseTransactions;
2019-07-03 13:19:57 +08:00
class AdminControllerTest extends TestCase
2017-11-02 16:50:00 +08:00
{
use DatabaseTransactions;
2019-02-27 23:44:50 +08:00
protected function setUp(): void
2017-11-02 16:50:00 +08:00
{
// Do not use `WithoutMiddleware` trait
parent::setUp();
2019-02-27 23:44:50 +08:00
$this->actAs('admin');
2017-11-02 16:50:00 +08:00
}
2019-03-19 19:16:03 +08:00
public function testChartData()
2017-11-02 16:50:00 +08:00
{
2019-03-22 11:13:21 +08:00
factory(User::class)->create();
factory(Texture::class)->create();
2019-03-19 23:35:13 +08:00
$this->getJson('/admin/chart')
2019-07-03 13:19:57 +08:00
->assertJson(['labels' => [
2019-03-19 23:35:13 +08:00
trans('admin.index.user-registration'),
2019-04-19 19:36:36 +08:00
trans('admin.index.texture-uploads'),
2019-03-19 23:35:13 +08:00
]])
2019-07-03 13:19:57 +08:00
->assertJsonStructure(['labels', 'xAxis', 'data']);
2017-11-02 16:50:00 +08:00
}
public function testUsers()
{
2019-07-03 13:19:57 +08:00
$this->get('/admin/users')->assertSee(trans('general.user-manage'));
2017-11-02 16:50:00 +08:00
}
public function testGetUserData()
{
2018-08-13 11:08:14 +08:00
$this->getJson('/admin/user-data')
2019-07-03 13:19:57 +08:00
->assertJsonStructure([
2017-11-02 16:50:00 +08:00
'data' => [[
'uid',
'email',
'nickname',
'score',
'permission',
'register_at',
'operations',
'players_count',
]],
2017-11-02 16:50:00 +08:00
]);
$user = factory(User::class)->create();
2018-08-13 11:08:14 +08:00
$this->getJson('/admin/user-data?uid='.$user->uid)
2019-07-03 13:19:57 +08:00
->assertJson([
2017-11-02 16:50:00 +08:00
'data' => [[
'uid' => $user->uid,
'email' => $user->email,
'nickname' => $user->nickname,
'score' => $user->score,
'permission' => $user->permission,
'players_count' => 0,
]],
2017-11-02 16:50:00 +08:00
]);
}
public function testPlayers()
{
2019-07-03 13:19:57 +08:00
$this->get('/admin/players')->assertSee(trans('general.player-manage'));
2017-11-02 16:50:00 +08:00
}
public function testGetPlayerData()
{
$player = factory(Player::class)->create();
2018-07-16 10:22:19 +08:00
$user = $player->user;
2017-11-02 16:50:00 +08:00
2018-08-13 11:08:14 +08:00
$this->getJson('/admin/player-data')
2019-07-03 13:19:57 +08:00
->assertJsonStructure([
2017-11-02 16:50:00 +08:00
'data' => [[
'pid',
'uid',
2019-03-13 13:16:51 +08:00
'name',
'tid_skin',
2017-11-02 16:50:00 +08:00
'tid_cape',
'last_modified',
]],
2017-11-02 16:50:00 +08:00
]);
2018-08-13 11:08:14 +08:00
$this->getJson('/admin/player-data?uid='.$user->uid)
2019-07-03 13:19:57 +08:00
->assertJson([
2017-11-02 16:50:00 +08:00
'data' => [[
'pid' => $player->pid,
'uid' => $user->uid,
2019-03-13 13:16:51 +08:00
'name' => $player->name,
'tid_skin' => $player->tid_skin,
'tid_cape' => $player->tid_cape,
]],
2017-11-02 16:50:00 +08:00
]);
}
public function testUserAjaxHandler()
{
// Operate on an not-existed user
2018-08-17 14:44:22 +08:00
$this->postJson('/admin/users')
2019-07-03 13:19:57 +08:00
->assertJson([
2019-04-23 11:47:45 +08:00
'code' => 1,
'message' => trans('admin.users.operations.non-existent'),
2017-11-02 16:50:00 +08:00
]);
$user = factory(User::class)->create();
// Operate without `action` field
2018-08-17 14:44:22 +08:00
$this->postJson('/admin/users', ['uid' => $user->uid])
2019-07-03 13:19:57 +08:00
->assertJson([
2019-04-23 11:47:45 +08:00
'code' => 1,
'message' => trans('admin.users.operations.invalid'),
2017-11-02 16:50:00 +08:00
]);
// An admin operating on a super admin should be forbidden
$superAdmin = factory(User::class, 'superAdmin')->create();
2018-08-17 14:44:22 +08:00
$this->postJson('/admin/users', ['uid' => $superAdmin->uid])
2019-07-03 13:19:57 +08:00
->assertJson([
2019-04-23 11:47:45 +08:00
'code' => 1,
'message' => trans('admin.users.operations.no-permission'),
2017-11-02 16:50:00 +08:00
]);
// Action is `email` but without `email` field
2018-08-17 14:44:22 +08:00
$this->postJson(
2017-11-02 16:50:00 +08:00
'/admin/users',
2019-07-03 13:19:57 +08:00
['uid' => $user->uid, 'action' => 'email']
)->assertJsonValidationErrors(['email']);
2017-11-02 16:50:00 +08:00
// Action is `email` but with an invalid email address
2018-08-17 14:44:22 +08:00
$this->postJson(
2017-11-02 16:50:00 +08:00
'/admin/users',
2019-07-03 13:19:57 +08:00
['uid' => $user->uid, 'action' => 'email', 'email' => 'invalid']
)->assertJsonValidationErrors(['email']);
2017-11-02 16:50:00 +08:00
// Using an existed email address
2018-08-17 14:44:22 +08:00
$this->postJson(
2017-11-02 16:50:00 +08:00
'/admin/users',
['uid' => $user->uid, 'action' => 'email', 'email' => $superAdmin->email]
2019-07-03 13:19:57 +08:00
)->assertJson([
2019-04-23 11:47:45 +08:00
'code' => 1,
'message' => trans('admin.users.operations.email.existed', ['email' => $superAdmin->email]),
2017-11-02 16:50:00 +08:00
]);
// Set email successfully
2018-08-17 14:44:22 +08:00
$this->postJson(
2017-11-02 16:50:00 +08:00
'/admin/users',
['uid' => $user->uid, 'action' => 'email', 'email' => 'a@b.c']
2019-07-03 13:19:57 +08:00
)->assertJson([
2019-04-23 11:47:45 +08:00
'code' => 0,
'message' => trans('admin.users.operations.email.success'),
2017-11-02 16:50:00 +08:00
]);
2019-07-03 13:19:57 +08:00
$this->assertDatabaseHas('users', [
2017-11-02 16:50:00 +08:00
'uid' => $user->uid,
'email' => 'a@b.c',
2017-11-02 16:50:00 +08:00
]);
2018-08-17 14:44:22 +08:00
// Toggle verification
$this->postJson(
'/admin/users',
['uid' => $user->uid, 'action' => 'verification']
2019-07-03 13:19:57 +08:00
)->assertJson([
2019-04-23 11:47:45 +08:00
'code' => 0,
'message' => trans('admin.users.operations.verification.success'),
2018-08-17 14:44:22 +08:00
]);
2019-07-03 13:19:57 +08:00
$this->assertDatabaseHas('users', [
2018-08-17 14:44:22 +08:00
'uid' => $user->uid,
'verified' => 0,
2018-08-17 14:44:22 +08:00
]);
2017-11-02 16:50:00 +08:00
// Action is `nickname` but without `nickname` field
2018-08-17 14:44:22 +08:00
$this->postJson(
2017-11-02 16:50:00 +08:00
'/admin/users',
2019-07-03 13:19:57 +08:00
['uid' => $user->uid, 'action' => 'nickname']
)->assertJsonValidationErrors(['nickname']);
2017-11-02 16:50:00 +08:00
// Action is `nickname` but with an invalid nickname
2018-08-17 14:44:22 +08:00
$this->postJson(
2017-11-02 16:50:00 +08:00
'/admin/users',
2019-07-03 13:19:57 +08:00
['uid' => $user->uid, 'action' => 'nickname', 'nickname' => '\\']
)->assertJsonValidationErrors(['nickname']);
2017-11-02 16:50:00 +08:00
// Set nickname successfully
2018-08-17 14:44:22 +08:00
$this->postJson(
2017-11-02 16:50:00 +08:00
'/admin/users',
['uid' => $user->uid, 'action' => 'nickname', 'nickname' => 'nickname']
2019-07-03 13:19:57 +08:00
)->assertJson([
2019-04-23 11:47:45 +08:00
'code' => 0,
'message' => trans('admin.users.operations.nickname.success', ['new' => 'nickname']),
2017-11-02 16:50:00 +08:00
]);
2019-07-03 13:19:57 +08:00
$this->assertDatabaseHas('users', [
2017-11-02 16:50:00 +08:00
'uid' => $user->uid,
'nickname' => 'nickname',
2017-11-02 16:50:00 +08:00
]);
// Action is `password` but without `password` field
2018-08-17 14:44:22 +08:00
$this->postJson(
2017-11-02 16:50:00 +08:00
'/admin/users',
2019-07-03 13:19:57 +08:00
['uid' => $user->uid, 'action' => 'password']
)->assertJsonValidationErrors(['password']);
2017-11-02 16:50:00 +08:00
// Set a too short password
2018-08-17 14:44:22 +08:00
$this->postJson(
2017-11-02 16:50:00 +08:00
'/admin/users',
2019-07-03 13:19:57 +08:00
['uid' => $user->uid, 'action' => 'password', 'password' => '1']
)->assertJsonValidationErrors(['password']);
2017-11-02 16:50:00 +08:00
// Set a too long password
2018-08-17 14:44:22 +08:00
$this->postJson(
2017-11-02 16:50:00 +08:00
'/admin/users',
2019-07-03 13:19:57 +08:00
['uid' => $user->uid, 'action' => 'password', 'password' => Str::random(17)]
)->assertJsonValidationErrors(['password']);
2017-11-02 16:50:00 +08:00
// Set password successfully
2018-08-17 14:44:22 +08:00
$this->postJson(
2017-11-02 16:50:00 +08:00
'/admin/users',
['uid' => $user->uid, 'action' => 'password', 'password' => '12345678']
2019-07-03 13:19:57 +08:00
)->assertJson([
2019-04-23 11:47:45 +08:00
'code' => 0,
'message' => trans('admin.users.operations.password.success'),
2017-11-02 16:50:00 +08:00
]);
$user = User::find($user->uid);
$this->assertTrue($user->verifyPassword('12345678'));
// Action is `score` but without `score` field
2018-08-17 14:44:22 +08:00
$this->postJson(
2017-11-02 16:50:00 +08:00
'/admin/users',
2019-07-03 13:19:57 +08:00
['uid' => $user->uid, 'action' => 'score']
)->assertJsonValidationErrors(['score']);
2017-11-02 16:50:00 +08:00
// Action is `score` but with an not-an-integer value
2018-08-17 14:44:22 +08:00
$this->postJson(
2017-11-02 16:50:00 +08:00
'/admin/users',
2019-07-03 13:19:57 +08:00
['uid' => $user->uid, 'action' => 'score', 'score' => 'string']
)->assertJsonValidationErrors(['score']);
2017-11-02 16:50:00 +08:00
// Set score successfully
2018-08-17 14:44:22 +08:00
$this->postJson(
2017-11-02 16:50:00 +08:00
'/admin/users',
['uid' => $user->uid, 'action' => 'score', 'score' => 123]
2019-07-03 13:19:57 +08:00
)->assertJson([
2019-04-23 11:47:45 +08:00
'code' => 0,
'message' => trans('admin.users.operations.score.success'),
2017-11-02 16:50:00 +08:00
]);
2019-07-03 13:19:57 +08:00
$this->assertDatabaseHas('users', [
2017-11-02 16:50:00 +08:00
'uid' => $user->uid,
'score' => 123,
2017-11-02 16:50:00 +08:00
]);
2019-03-18 13:24:03 +08:00
// Invalid permission value
$this->postJson('/admin/users', [
'uid' => $user->uid,
'action' => 'permission',
2019-04-19 19:36:36 +08:00
'permission' => -2,
2019-07-03 13:19:57 +08:00
])->assertJsonValidationErrors(['permission']);
2017-11-02 16:50:00 +08:00
$user = User::find($user->uid);
2019-03-23 00:20:28 +08:00
$this->assertEquals(User::NORMAL, $user->permission);
2017-11-02 16:50:00 +08:00
2019-03-18 13:24:03 +08:00
// Update permission successfully
$this->postJson('/admin/users', [
'uid' => $user->uid,
'action' => 'permission',
2019-04-19 19:36:36 +08:00
'permission' => -1,
2019-07-03 13:19:57 +08:00
])->assertJson([
2019-04-23 11:47:45 +08:00
'code' => 0,
'message' => trans('admin.users.operations.permission'),
2019-03-18 13:24:03 +08:00
]);
2017-11-02 16:50:00 +08:00
$user = User::find($user->uid);
2019-03-23 00:20:28 +08:00
$this->assertEquals(User::BANNED, $user->permission);
2017-11-02 16:50:00 +08:00
// Delete a user
2018-08-17 14:44:22 +08:00
$this->postJson('/admin/users', ['uid' => $user->uid, 'action' => 'delete'])
2019-07-03 13:19:57 +08:00
->assertJson([
2019-04-23 11:47:45 +08:00
'code' => 0,
'message' => trans('admin.users.operations.delete.success'),
2017-11-02 16:50:00 +08:00
]);
$this->assertNull(User::find($user->uid));
}
public function testPlayerAjaxHandler()
{
$player = factory(Player::class)->create();
// Operate on a not-existed player
2018-08-17 14:44:22 +08:00
$this->postJson('/admin/players', ['pid' => -1])
2019-07-03 13:19:57 +08:00
->assertJson([
2019-04-23 11:47:45 +08:00
'code' => 1,
'message' => trans('general.unexistent-player'),
2017-11-02 16:50:00 +08:00
]);
// An admin cannot operate another admin's player
$admin = factory(User::class, 'admin')->create();
2018-08-17 14:44:22 +08:00
$this->postJson(
2017-11-02 16:50:00 +08:00
'/admin/players',
['pid' => factory(Player::class)->create(['uid' => $admin->uid])->pid]
2019-07-03 13:19:57 +08:00
)->assertJson([
2019-04-23 11:47:45 +08:00
'code' => 1,
'message' => trans('admin.players.no-permission'),
2017-11-02 16:50:00 +08:00
]);
$superAdmin = factory(User::class, 'superAdmin')->create();
2018-08-17 14:44:22 +08:00
$this->postJson(
2017-11-02 16:50:00 +08:00
'/admin/players',
['pid' => factory(Player::class)->create(['uid' => $superAdmin->uid])->pid]
2019-07-03 13:19:57 +08:00
)->assertJson([
2019-04-23 11:47:45 +08:00
'code' => 1,
'message' => trans('admin.players.no-permission'),
2017-11-02 16:50:00 +08:00
]);
// For self is OK
2019-04-04 09:50:48 +08:00
$this->actingAs($admin)->postJson(
2017-11-02 16:50:00 +08:00
'/admin/players',
['pid' => factory(Player::class)->create(['uid' => $admin->uid])->pid]
2019-07-03 13:19:57 +08:00
)->assertJson([
2019-04-23 11:47:45 +08:00
'code' => 1,
'message' => trans('admin.users.operations.invalid'),
2017-11-02 16:50:00 +08:00
]);
// Change texture without `type` field
2018-08-17 14:44:22 +08:00
$this->postJson('/admin/players', [
2017-11-02 16:50:00 +08:00
'pid' => $player->pid,
'action' => 'texture',
2019-07-03 13:19:57 +08:00
])->assertJsonValidationErrors(['type']);
2017-11-02 16:50:00 +08:00
// Change texture without `tid` field
2018-08-17 14:44:22 +08:00
$this->postJson('/admin/players', [
2017-11-02 16:50:00 +08:00
'pid' => $player->pid,
'action' => 'texture',
'type' => 'skin',
2019-07-03 13:19:57 +08:00
])->assertJsonValidationErrors(['tid']);
2017-11-02 16:50:00 +08:00
// Change texture with a not-integer value
2018-08-17 14:44:22 +08:00
$this->postJson('/admin/players', [
2017-11-02 16:50:00 +08:00
'pid' => $player->pid,
'action' => 'texture',
'type' => 'skin',
'tid' => 'string',
2019-07-03 13:19:57 +08:00
])->assertJsonValidationErrors(['tid']);
2017-11-02 16:50:00 +08:00
// Invalid texture
2018-08-17 14:44:22 +08:00
$this->postJson('/admin/players', [
2017-11-02 16:50:00 +08:00
'pid' => $player->pid,
'action' => 'texture',
'type' => 'skin',
'tid' => -1,
2019-07-03 13:19:57 +08:00
])->assertJson([
2019-04-23 11:47:45 +08:00
'code' => 1,
'message' => trans('admin.players.textures.non-existent', ['tid' => -1]),
2017-11-02 16:50:00 +08:00
]);
$skin = factory(Texture::class)->create();
2017-11-02 16:50:00 +08:00
$cape = factory(Texture::class, 'cape')->create();
// Skin
2018-08-17 14:44:22 +08:00
$this->postJson('/admin/players', [
2017-11-02 16:50:00 +08:00
'pid' => $player->pid,
'action' => 'texture',
'type' => 'skin',
'tid' => $skin->tid,
2019-07-03 13:19:57 +08:00
])->assertJson([
2019-04-23 11:47:45 +08:00
'code' => 0,
'message' => trans('admin.players.textures.success', ['player' => $player->name]),
2017-11-02 16:50:00 +08:00
]);
$player = Player::find($player->pid);
$this->assertEquals($skin->tid, $player->tid_skin);
2017-11-02 16:50:00 +08:00
// Cape
2018-08-17 14:44:22 +08:00
$this->postJson('/admin/players', [
2017-11-02 16:50:00 +08:00
'pid' => $player->pid,
'action' => 'texture',
'type' => 'cape',
'tid' => $cape->tid,
2019-07-03 13:19:57 +08:00
])->assertJson([
2019-04-23 11:47:45 +08:00
'code' => 0,
'message' => trans('admin.players.textures.success', ['player' => $player->name]),
2017-11-02 16:50:00 +08:00
]);
$player = Player::find($player->pid);
$this->assertEquals($cape->tid, $player->tid_cape);
// Reset texture
2018-08-17 14:44:22 +08:00
$this->postJson('/admin/players', [
2017-11-02 16:50:00 +08:00
'pid' => $player->pid,
'action' => 'texture',
'type' => 'skin',
'tid' => 0,
2019-07-03 13:19:57 +08:00
])->assertJson([
2019-04-23 11:47:45 +08:00
'code' => 0,
'message' => trans('admin.players.textures.success', ['player' => $player->name]),
2017-11-02 16:50:00 +08:00
]);
$player = Player::find($player->pid);
$this->assertEquals(0, $player->tid_skin);
2017-11-02 16:50:00 +08:00
$this->assertNotEquals(0, $player->tid_cape);
2018-08-17 14:44:22 +08:00
$this->postJson('/admin/players', [
2017-11-02 16:50:00 +08:00
'pid' => $player->pid,
'action' => 'texture',
'type' => 'cape',
'tid' => 0,
2019-07-03 13:19:57 +08:00
])->assertJson([
2019-04-23 11:47:45 +08:00
'code' => 0,
'message' => trans('admin.players.textures.success', ['player' => $player->name]),
2017-11-02 16:50:00 +08:00
]);
$player = Player::find($player->pid);
$this->assertEquals(0, $player->tid_cape);
// Change owner without `uid` field
2018-08-17 14:44:22 +08:00
$this->postJson('/admin/players', [
2017-11-02 16:50:00 +08:00
'pid' => $player->pid,
'action' => 'owner',
2019-07-03 13:19:57 +08:00
])->assertJsonValidationErrors(['uid']);
2017-11-02 16:50:00 +08:00
// Change owner with a not-integer `uid` value
2018-08-17 14:44:22 +08:00
$this->postJson('/admin/players', [
2017-11-02 16:50:00 +08:00
'pid' => $player->pid,
'action' => 'owner',
'uid' => 'string',
2019-07-03 13:19:57 +08:00
])->assertJsonValidationErrors(['uid']);
2017-11-02 16:50:00 +08:00
// Change owner to a not-existed user
2018-08-17 14:44:22 +08:00
$this->postJson('/admin/players', [
2017-11-02 16:50:00 +08:00
'pid' => $player->pid,
'action' => 'owner',
'uid' => -1,
2019-07-03 13:19:57 +08:00
])->assertJson([
2019-04-23 11:47:45 +08:00
'code' => 1,
'message' => trans('admin.users.operations.non-existent'),
2017-11-02 16:50:00 +08:00
]);
// Change owner successfully
$user = factory(User::class)->create();
2018-08-17 14:44:22 +08:00
$this->postJson('/admin/players', [
2017-11-02 16:50:00 +08:00
'pid' => $player->pid,
'action' => 'owner',
'uid' => $user->uid,
2019-07-03 13:19:57 +08:00
])->assertJson([
2019-04-23 11:47:45 +08:00
'code' => 0,
'message' => trans(
2017-11-02 16:50:00 +08:00
'admin.players.owner.success',
2019-03-13 13:16:51 +08:00
['player' => $player->name, 'user' => $user->nickname]
),
2017-11-02 16:50:00 +08:00
]);
// Rename a player without `name` field
2018-08-17 14:44:22 +08:00
$this->postJson('/admin/players', [
2017-11-02 16:50:00 +08:00
'pid' => $player->pid,
'action' => 'name',
2019-07-03 13:19:57 +08:00
])->assertJsonValidationErrors(['name']);
2017-11-02 16:50:00 +08:00
// Rename a player successfully
2018-08-17 14:44:22 +08:00
$this->postJson('/admin/players', [
2017-11-02 16:50:00 +08:00
'pid' => $player->pid,
'action' => 'name',
'name' => 'new_name',
2019-07-03 13:19:57 +08:00
])->assertJson([
2019-04-23 11:47:45 +08:00
'code' => 0,
'message' => trans('admin.players.name.success', ['player' => 'new_name']),
2017-11-02 16:50:00 +08:00
]);
2019-03-22 21:40:12 +08:00
// Single player
option(['single_player' => true]);
$this->postJson('/admin/players', [
'pid' => $player->pid,
'action' => 'name',
2019-04-23 19:14:41 +08:00
'name' => 'abc',
2019-07-03 13:19:57 +08:00
])->assertJson(['code' => 0]);
2019-03-22 21:40:12 +08:00
$player->refresh();
$this->assertEquals('abc', $player->user->nickname);
2017-11-02 16:50:00 +08:00
// Delete a player
2018-08-17 14:44:22 +08:00
$this->postJson('/admin/players', [
2017-11-02 16:50:00 +08:00
'pid' => $player->pid,
'action' => 'delete',
2019-07-03 13:19:57 +08:00
])->assertJson([
2019-04-23 11:47:45 +08:00
'code' => 0,
'message' => trans('admin.players.delete.success'),
2017-11-02 16:50:00 +08:00
]);
$this->assertNull(Player::find($player->pid));
}
}