Switch to Laravel's validation expcetion handler

This commit is contained in:
Pig Fang 2019-04-05 08:58:00 +08:00
parent eda7e1ce30
commit 4305b722ca
10 changed files with 122 additions and 385 deletions

View File

@ -17,21 +17,4 @@ class Handler extends ExceptionHandler
ValidationException::class, ValidationException::class,
PrettyPageException::class, PrettyPageException::class,
]; ];
public function render($request, Exception $e)
{
if ($e instanceof ValidationException) {
if ($request->expectsJson()) {
return response()->json([
'errno' => 1,
'msg' => $e->validator->errors()->first(),
]);
} else {
$request->session()->flash('errors', $e->validator->errors());
return redirect()->back();
}
}
return parent::render($request, $e);
}
} }

View File

@ -35,6 +35,19 @@ export async function walkFetch(request: Request): Promise<any> {
? response.json() ? response.json()
: response.text() : response.text()
} }
// Process validation errors from Laravel.
if (response.status === 422) {
const { errors }: {
message: string,
errors: { [field: string]: string[] }
} = await response.json()
return {
errno: 1,
msg: Object.keys(errors).map(field => errors[field][0])[0],
}
}
const res = response.clone() const res = response.clone()
throw new HTTPError(await response.text(), res) throw new HTTPError(await response.text(), res)
} catch (error) { } catch (error) {

View File

@ -122,6 +122,24 @@ test('low level fetch', async () => {
expect(await net.walkFetch(request as Request)).toBe('text') expect(await net.walkFetch(request as Request)).toBe('text')
}) })
test('process Laravel validation errors', async () => {
window.fetch = jest.fn().mockResolvedValue({
status: 422,
json() {
return Promise.resolve({
errors: { name: ['required'] },
})
},
})
const result: {
errno: number,
msg: string
} = await net.walkFetch({ headers: new Headers() } as Request)
expect(result.errno).toBe(1)
expect(result.msg).toBe('required')
})
test('inject to Vue instance', () => { test('inject to Vue instance', () => {
expect(typeof Vue.prototype.$http.get).toBe('function') expect(typeof Vue.prototype.$http.get).toBe('function')
expect(typeof Vue.prototype.$http.post).toBe('function') expect(typeof Vue.prototype.$http.post).toBe('function')

View File

@ -38,10 +38,7 @@ class AdminControllerTest extends BrowserKitTestCase
{ {
// Check if `color_scheme` is existed or not // Check if `color_scheme` is existed or not
$this->getJson('/admin/customize?action=color') $this->getJson('/admin/customize?action=color')
->seeJson([ ->seeJsonStructure(['errors' => ['color_scheme']]);
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'color scheme']),
]);
// Change color // Change color
$this->get('/admin/customize?action=color&color_scheme=purple') $this->get('/admin/customize?action=color&color_scheme=purple')
@ -331,20 +328,14 @@ class AdminControllerTest extends BrowserKitTestCase
'/admin/users', '/admin/users',
['uid' => $user->uid, 'action' => 'email'], ['uid' => $user->uid, 'action' => 'email'],
['Accept' => 'application/json'] ['Accept' => 'application/json']
)->seeJson([ )->seeJsonStructure(['errors' => ['email']]);
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'email']),
]);
// Action is `email` but with an invalid email address // Action is `email` but with an invalid email address
$this->postJson( $this->postJson(
'/admin/users', '/admin/users',
['uid' => $user->uid, 'action' => 'email', 'email' => 'invalid'], ['uid' => $user->uid, 'action' => 'email', 'email' => 'invalid'],
['Accept' => 'application/json'] ['Accept' => 'application/json']
)->seeJson([ )->seeJsonStructure(['errors' => ['email']]);
'errno' => 1,
'msg' => trans('validation.email', ['attribute' => 'email']),
]);
// Using an existed email address // Using an existed email address
$this->postJson( $this->postJson(
@ -386,20 +377,14 @@ class AdminControllerTest extends BrowserKitTestCase
'/admin/users', '/admin/users',
['uid' => $user->uid, 'action' => 'nickname'], ['uid' => $user->uid, 'action' => 'nickname'],
['Accept' => 'application/json'] ['Accept' => 'application/json']
)->seeJson([ )->seeJsonStructure(['errors' => ['nickname']]);
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'nickname']),
]);
// Action is `nickname` but with an invalid nickname // Action is `nickname` but with an invalid nickname
$this->postJson( $this->postJson(
'/admin/users', '/admin/users',
['uid' => $user->uid, 'action' => 'nickname', 'nickname' => '\\'], ['uid' => $user->uid, 'action' => 'nickname', 'nickname' => '\\'],
['Accept' => 'application/json'] ['Accept' => 'application/json']
)->seeJson([ )->seeJsonStructure(['errors' => ['nickname']]);
'errno' => 1,
'msg' => trans('validation.no_special_chars', ['attribute' => 'nickname']),
]);
// Set nickname successfully // Set nickname successfully
$this->postJson( $this->postJson(
@ -419,30 +404,21 @@ class AdminControllerTest extends BrowserKitTestCase
'/admin/users', '/admin/users',
['uid' => $user->uid, 'action' => 'password'], ['uid' => $user->uid, 'action' => 'password'],
['Accept' => 'application/json'] ['Accept' => 'application/json']
)->seeJson([ )->seeJsonStructure(['errors' => ['password']]);
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'password']),
]);
// Set a too short password // Set a too short password
$this->postJson( $this->postJson(
'/admin/users', '/admin/users',
['uid' => $user->uid, 'action' => 'password', 'password' => '1'], ['uid' => $user->uid, 'action' => 'password', 'password' => '1'],
['Accept' => 'application/json'] ['Accept' => 'application/json']
)->seeJson([ )->seeJsonStructure(['errors' => ['password']]);
'errno' => 1,
'msg' => trans('validation.min.string', ['attribute' => 'password', 'min' => 8]),
]);
// Set a too long password // Set a too long password
$this->postJson( $this->postJson(
'/admin/users', '/admin/users',
['uid' => $user->uid, 'action' => 'password', 'password' => Str::random(17)], ['uid' => $user->uid, 'action' => 'password', 'password' => Str::random(17)],
['Accept' => 'application/json'] ['Accept' => 'application/json']
)->seeJson([ )->seeJsonStructure(['errors' => ['password']]);
'errno' => 1,
'msg' => trans('validation.max.string', ['attribute' => 'password', 'max' => 16]),
]);
// Set password successfully // Set password successfully
$this->postJson( $this->postJson(
@ -460,20 +436,14 @@ class AdminControllerTest extends BrowserKitTestCase
'/admin/users', '/admin/users',
['uid' => $user->uid, 'action' => 'score'], ['uid' => $user->uid, 'action' => 'score'],
['Accept' => 'application/json'] ['Accept' => 'application/json']
)->seeJson([ )->seeJsonStructure(['errors' => ['score']]);
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'score']),
]);
// Action is `score` but with an not-an-integer value // Action is `score` but with an not-an-integer value
$this->postJson( $this->postJson(
'/admin/users', '/admin/users',
['uid' => $user->uid, 'action' => 'score', 'score' => 'string'], ['uid' => $user->uid, 'action' => 'score', 'score' => 'string'],
['Accept' => 'application/json'] ['Accept' => 'application/json']
)->seeJson([ )->seeJsonStructure(['errors' => ['score']]);
'errno' => 1,
'msg' => trans('validation.integer', ['attribute' => 'score']),
]);
// Set score successfully // Set score successfully
$this->postJson( $this->postJson(
@ -493,10 +463,7 @@ class AdminControllerTest extends BrowserKitTestCase
'uid' => $user->uid, 'uid' => $user->uid,
'action' => 'permission', 'action' => 'permission',
'permission' => -2 'permission' => -2
])->seeJson([ ])->seeJsonStructure(['errors' => ['permission']]);
'errno' => 1,
'msg' => trans('validation.in', ['attribute' => 'permission']),
]);
$user = User::find($user->uid); $user = User::find($user->uid);
$this->assertEquals(User::NORMAL, $user->permission); $this->assertEquals(User::NORMAL, $user->permission);
@ -564,10 +531,7 @@ class AdminControllerTest extends BrowserKitTestCase
'action' => 'texture', 'action' => 'texture',
], [ ], [
'Accept' => 'application/json', 'Accept' => 'application/json',
])->seeJson([ ])->seeJsonStructure(['errors' => ['type']]);
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'type']),
]);
// Change texture without `tid` field // Change texture without `tid` field
$this->postJson('/admin/players', [ $this->postJson('/admin/players', [
@ -576,10 +540,7 @@ class AdminControllerTest extends BrowserKitTestCase
'type' => 'skin', 'type' => 'skin',
], [ ], [
'Accept' => 'application/json', 'Accept' => 'application/json',
])->seeJson([ ])->seeJsonStructure(['errors' => ['tid']]);
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'tid']),
]);
// Change texture with a not-integer value // Change texture with a not-integer value
$this->postJson('/admin/players', [ $this->postJson('/admin/players', [
@ -589,10 +550,7 @@ class AdminControllerTest extends BrowserKitTestCase
'tid' => 'string', 'tid' => 'string',
], [ ], [
'Accept' => 'application/json', 'Accept' => 'application/json',
])->seeJson([ ])->seeJsonStructure(['errors' => ['tid']]);
'errno' => 1,
'msg' => trans('validation.integer', ['attribute' => 'tid']),
]);
// Invalid texture // Invalid texture
$this->postJson('/admin/players', [ $this->postJson('/admin/players', [
@ -666,10 +624,7 @@ class AdminControllerTest extends BrowserKitTestCase
'action' => 'owner', 'action' => 'owner',
], [ ], [
'Accept' => 'application/json', 'Accept' => 'application/json',
])->seeJson([ ])->seeJsonStructure(['errors' => ['uid']]);
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'uid']),
]);
// Change owner with a not-integer `uid` value // Change owner with a not-integer `uid` value
$this->postJson('/admin/players', [ $this->postJson('/admin/players', [
@ -678,10 +633,7 @@ class AdminControllerTest extends BrowserKitTestCase
'uid' => 'string', 'uid' => 'string',
], [ ], [
'Accept' => 'application/json', 'Accept' => 'application/json',
])->seeJson([ ])->seeJsonStructure(['errors' => ['uid']]);
'errno' => 1,
'msg' => trans('validation.integer', ['attribute' => 'uid']),
]);
// Change owner to a not-existed user // Change owner to a not-existed user
$this->postJson('/admin/players', [ $this->postJson('/admin/players', [
@ -713,10 +665,7 @@ class AdminControllerTest extends BrowserKitTestCase
'action' => 'name', 'action' => 'name',
], [ ], [
'Accept' => 'application/json', 'Accept' => 'application/json',
])->seeJson([ ])->seeJsonStructure(['errors' => ['name']]);
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'name']),
]);
// Rename a player successfully // Rename a player successfully
$this->postJson('/admin/players', [ $this->postJson('/admin/players', [

View File

@ -45,39 +45,26 @@ class AuthControllerTest extends TestCase
$player = factory(Player::class)->create(['uid' => $user->uid]); $player = factory(Player::class)->create(['uid' => $user->uid]);
// Should return a warning if `identification` is empty // Should return a warning if `identification` is empty
$this->postJson('/auth/login') $this->postJson('/auth/login')->assertJsonValidationErrors('identification');
->assertJson([
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => trans('validation.attributes.identification')]),
]);
// Should return a warning if `password` is empty // Should return a warning if `password` is empty
$this->postJson( $this->postJson(
'/auth/login', ['identification' => $user->email] '/auth/login', ['identification' => $user->email]
)->assertJson([ )->assertJsonValidationErrors('password');
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'password']),
]);
// Should return a warning if length of `password` is lower than 6 // Should return a warning if length of `password` is lower than 6
$this->postJson( $this->postJson(
'/auth/login', [ '/auth/login', [
'identification' => $user->email, 'identification' => $user->email,
'password' => '123', 'password' => '123',
])->assertJson([ ])->assertJsonValidationErrors('password');
'errno' => 1,
'msg' => trans('validation.min.string', ['attribute' => 'password', 'min' => 6]),
]);
// Should return a warning if length of `password` is greater than 32 // Should return a warning if length of `password` is greater than 32
$this->postJson( $this->postJson(
'/auth/login', [ '/auth/login', [
'identification' => $user->email, 'identification' => $user->email,
'password' => Str::random(80), 'password' => Str::random(80),
])->assertJson([ ])->assertJsonValidationErrors('password');
'errno' => 1,
'msg' => trans('validation.max.string', ['attribute' => 'password', 'max' => 32]),
]);
$this->flushSession(); $this->flushSession();
@ -105,10 +92,7 @@ class AuthControllerTest extends TestCase
'/auth/login', [ '/auth/login', [
'identification' => $user->email, 'identification' => $user->email,
'password' => '12345678', 'password' => '12345678',
])->assertJson([ ])->assertJsonValidationErrors('captcha');
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'captcha']),
]);
Cache::flush(); Cache::flush();
$this->flushSession(); $this->flushSession();
@ -187,39 +171,26 @@ class AuthControllerTest extends TestCase
$this->expectsEvents(Events\UserRegistered::class); $this->expectsEvents(Events\UserRegistered::class);
// Should return a warning if `email` is empty // Should return a warning if `email` is empty
$this->postJson('/auth/register') $this->postJson('/auth/register')->assertJsonValidationErrors('email');
->assertJson([
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'email']),
]);
// Should return a warning if `email` is invalid // Should return a warning if `email` is invalid
$this->postJson( $this->postJson(
'/auth/register', '/auth/register',
['email' => 'not_an_email'] ['email' => 'not_an_email']
)->assertJson([ )->assertJsonValidationErrors('email');
'errno' => 1,
'msg' => trans('validation.email', ['attribute' => 'email']),
]);
// An existed user // An existed user
$existedUser = factory(User::class)->create(); $existedUser = factory(User::class)->create();
$this->postJson( $this->postJson(
'/auth/register', '/auth/register',
['email' => $existedUser->email] ['email' => $existedUser->email]
)->assertJson([ )->assertJsonValidationErrors('email');
'errno' => 1,
'msg' => trans('validation.unique', ['attribute' => 'email']),
]);
// Should return a warning if `password` is empty // Should return a warning if `password` is empty
$this->postJson( $this->postJson(
'/auth/register', '/auth/register',
['email' => 'a@b.c'] ['email' => 'a@b.c']
)->assertJson([ )->assertJsonValidationErrors('password');
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'password']),
]);
// Should return a warning if length of `password` is lower than 8 // Should return a warning if length of `password` is lower than 8
$this->postJson( $this->postJson(
@ -228,10 +199,7 @@ class AuthControllerTest extends TestCase
'email' => 'a@b.c', 'email' => 'a@b.c',
'password' => '1', 'password' => '1',
] ]
)->assertJson([ )->assertJsonValidationErrors('password');
'errno' => 1,
'msg' => trans('validation.min.string', ['attribute' => 'password', 'min' => 8]),
]);
// Should return a warning if length of `password` is greater than 32 // Should return a warning if length of `password` is greater than 32
$this->postJson( $this->postJson(
@ -240,10 +208,7 @@ class AuthControllerTest extends TestCase
'email' => 'a@b.c', 'email' => 'a@b.c',
'password' => Str::random(33), 'password' => Str::random(33),
] ]
)->assertJson([ )->assertJsonValidationErrors('password');
'errno' => 1,
'msg' => trans('validation.max.string', ['attribute' => 'password', 'max' => 32]),
]);
// The register_with_player_name option is set to true by default. // The register_with_player_name option is set to true by default.
// Should return a warning if `player_name` is empty // Should return a warning if `player_name` is empty
@ -254,10 +219,7 @@ class AuthControllerTest extends TestCase
'password' => '12345678', 'password' => '12345678',
'captcha' => 'a', 'captcha' => 'a',
] ]
)->assertJson([ )->assertJsonValidationErrors('player_name');
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => trans('validation.attributes.player_name')]),
]);
// Should return a warning if `player_name` is invalid // Should return a warning if `player_name` is invalid
option(['player_name_rule' => 'official']); option(['player_name_rule' => 'official']);
@ -269,10 +231,7 @@ class AuthControllerTest extends TestCase
'player_name' => '角色名', 'player_name' => '角色名',
'captcha' => 'a', 'captcha' => 'a',
] ]
)->assertJson([ )->assertJsonValidationErrors('player_name');
'errno' => 1,
'msg' => trans('validation.player_name', ['attribute' => trans('validation.attributes.player_name')]),
]);
// Should return a warning if `player_name` is too long // Should return a warning if `player_name` is too long
$this->postJson( $this->postJson(
@ -283,13 +242,7 @@ class AuthControllerTest extends TestCase
'player_name' => Str::random(option('player_name_length_max') + 10), 'player_name' => Str::random(option('player_name_length_max') + 10),
'captcha' => 'a', 'captcha' => 'a',
] ]
)->assertJson([ )->assertJsonValidationErrors('player_name');
'errno' => 1,
'msg' => trans('validation.max.string', [
'attribute' => trans('validation.attributes.player_name'),
'max' => option('player_name_length_max'),
]),
]);
// Existed player // Existed player
$player = factory(Player::class)->create(); $player = factory(Player::class)->create();
@ -317,10 +270,7 @@ class AuthControllerTest extends TestCase
'password' => '12345678', 'password' => '12345678',
'captcha' => 'a', 'captcha' => 'a',
] ]
)->assertJson([ )->assertJsonValidationErrors('nickname');
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'nickname']),
]);
// Should return a warning if `nickname` is invalid // Should return a warning if `nickname` is invalid
$this->postJson( $this->postJson(
@ -331,10 +281,7 @@ class AuthControllerTest extends TestCase
'nickname' => '\\', 'nickname' => '\\',
'captcha' => 'a', 'captcha' => 'a',
] ]
)->assertJson([ )->assertJsonValidationErrors('nickname');
'errno' => 1,
'msg' => trans('validation.no_special_chars', ['attribute' => 'nickname']),
]);
// Should return a warning if `nickname` is too long // Should return a warning if `nickname` is too long
$this->postJson( $this->postJson(
@ -345,10 +292,7 @@ class AuthControllerTest extends TestCase
'nickname' => Str::random(256), 'nickname' => Str::random(256),
'captcha' => 'a', 'captcha' => 'a',
] ]
)->assertJson([ )->assertJsonValidationErrors('nickname');
'errno' => 1,
'msg' => trans('validation.max.string', ['attribute' => 'nickname', 'max' => 255]),
]);
// Should return a warning if `captcha` is empty // Should return a warning if `captcha` is empty
$this->postJson( $this->postJson(
@ -358,10 +302,7 @@ class AuthControllerTest extends TestCase
'password' => '12345678', 'password' => '12345678',
'nickname' => 'nickname', 'nickname' => 'nickname',
] ]
)->assertJson([ )->assertJsonValidationErrors('captcha');
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'captcha']),
]);
// Should be forbidden if registering is closed // Should be forbidden if registering is closed
Option::set('user_can_register', false); Option::set('user_can_register', false);
@ -533,29 +474,19 @@ class AuthControllerTest extends TestCase
$url = URL::temporarySignedRoute('auth.reset', now()->addHour(), ['uid' => $user->uid]); $url = URL::temporarySignedRoute('auth.reset', now()->addHour(), ['uid' => $user->uid]);
// Should return a warning if `password` is empty // Should return a warning if `password` is empty
$this->postJson($url) $this->postJson($url)->assertJsonValidationErrors('password');
->assertJson([
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'password']),
]);
// Should return a warning if `password` is too short // Should return a warning if `password` is too short
$this->postJson( $this->postJson(
$url, [ $url, [
'password' => '123', 'password' => '123',
])->assertJson([ ])->assertJsonValidationErrors('password');
'errno' => 1,
'msg' => trans('validation.min.string', ['attribute' => 'password', 'min' => 8]),
]);
// Should return a warning if `password` is too long // Should return a warning if `password` is too long
$this->postJson( $this->postJson(
$url, [ $url, [
'password' => Str::random(33), 'password' => Str::random(33),
])->assertJson([ ])->assertJsonValidationErrors('password');
'errno' => 1,
'msg' => trans('validation.max.string', ['attribute' => 'password', 'max' => 32]),
]);
// Success // Success
$this->postJson( $this->postJson(

View File

@ -87,38 +87,25 @@ class ClosetControllerTest extends TestCase
option(['score_per_closet_item' => 10]); option(['score_per_closet_item' => 10]);
// Missing `tid` field // Missing `tid` field
$this->postJson('/user/closet/add') $this->postJson('/user/closet/add')->assertJsonValidationErrors('tid');
->assertJson([
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'tid']),
]);
// `tid` is not a integer // `tid` is not a integer
$this->postJson( $this->postJson(
'/user/closet/add', '/user/closet/add',
['tid' => 'string'] ['tid' => 'string']
)->assertJson([ )->assertJsonValidationErrors('tid');
'errno' => 1,
'msg' => trans('validation.integer', ['attribute' => 'tid']),
]);
// Missing `name` field // Missing `name` field
$this->postJson( $this->postJson(
'/user/closet/add', '/user/closet/add',
['tid' => 0] ['tid' => 0]
)->assertJson([ )->assertJsonValidationErrors('name');
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'name']),
]);
// `name` field has special characters // `name` field has special characters
$this->postJson( $this->postJson(
'/user/closet/add', '/user/closet/add',
['tid' => 0, 'name' => '\\'] ['tid' => 0, 'name' => '\\']
)->assertJson([ )->assertJsonValidationErrors('name');
'errno' => 1,
'msg' => trans('validation.no_special_chars', ['attribute' => 'name']),
]);
// The user doesn't have enough score to add a texture // The user doesn't have enough score to add a texture
$this->user->setScore(0); $this->user->setScore(0);
@ -172,38 +159,25 @@ class ClosetControllerTest extends TestCase
$name = 'new'; $name = 'new';
// Missing `tid` field // Missing `tid` field
$this->postJson('/user/closet/rename') $this->postJson('/user/closet/rename')->assertJsonValidationErrors('tid');
->assertJson([
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'tid']),
]);
// `tid` is not a integer // `tid` is not a integer
$this->postJson( $this->postJson(
'/user/closet/rename', '/user/closet/rename',
['tid' => 'string'] ['tid' => 'string']
)->assertJson([ )->assertJsonValidationErrors('tid');
'errno' => 1,
'msg' => trans('validation.integer', ['attribute' => 'tid']),
]);
// Missing `new_name` field // Missing `new_name` field
$this->postJson( $this->postJson(
'/user/closet/rename', '/user/closet/rename',
['tid' => 0] ['tid' => 0]
)->assertJson([ )->assertJsonValidationErrors('new_name');
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'new name']),
]);
// `new_name` field has special characters // `new_name` field has special characters
$this->postJson( $this->postJson(
'/user/closet/rename', '/user/closet/rename',
['tid' => 0, 'new_name' => '\\'] ['tid' => 0, 'new_name' => '\\']
)->assertJson([ )->assertJsonValidationErrors('new_name');
'errno' => 1,
'msg' => trans('validation.no_special_chars', ['attribute' => 'new name']),
]);
// Rename a not-existed texture // Rename a not-existed texture
$this->postJson( $this->postJson(
@ -233,20 +207,13 @@ class ClosetControllerTest extends TestCase
$likes = $texture->likes; $likes = $texture->likes;
// Missing `tid` field // Missing `tid` field
$this->postJson('/user/closet/remove') $this->postJson('/user/closet/remove')->assertJsonValidationErrors('tid');
->assertJson([
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'tid']),
]);
// `tid` is not a integer // `tid` is not a integer
$this->postJson( $this->postJson(
'/user/closet/remove', '/user/closet/remove',
['tid' => 'string'] ['tid' => 'string']
)->assertJson([ )->assertJsonValidationErrors('tid');
'errno' => 1,
'msg' => trans('validation.integer', ['attribute' => 'tid']),
]);
// Rename a not-existed texture // Rename a not-existed texture
$this->postJson( $this->postJson(

View File

@ -41,21 +41,14 @@ class PlayerControllerTest extends TestCase
public function testAdd() public function testAdd()
{ {
// Without player name // Without player name
$this->postJson('/user/player/add') $this->postJson('/user/player/add')->assertJsonValidationErrors('player_name');
->assertJson([
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => trans('validation.attributes.player_name')]),
]);
// Only A-Za-z0-9_ are allowed // Only A-Za-z0-9_ are allowed
option(['player_name_rule' => 'official']); option(['player_name_rule' => 'official']);
$this->postJson( $this->postJson(
'/user/player/add', '/user/player/add',
['player_name' => '角色名'] ['player_name' => '角色名']
)->assertJson([ )->assertJsonValidationErrors('player_name');
'errno' => 1,
'msg' => trans('validation.player_name', ['attribute' => trans('validation.attributes.player_name')]),
]);
// Custom player name rule (regexp) // Custom player name rule (regexp)
option(['player_name_rule' => 'custom']); option(['player_name_rule' => 'custom']);
@ -63,10 +56,7 @@ class PlayerControllerTest extends TestCase
$this->postJson( $this->postJson(
'/user/player/add', '/user/player/add',
['player_name' => 'yjsnpi'] ['player_name' => 'yjsnpi']
)->assertJson([ )->assertJsonValidationErrors('player_name');
'errno' => 1,
'msg' => trans('validation.player_name', ['attribute' => trans('validation.attributes.player_name')]),
]);
// Lack of score // Lack of score
option(['player_name_rule' => 'official']); option(['player_name_rule' => 'official']);
@ -181,31 +171,21 @@ class PlayerControllerTest extends TestCase
->postJson('/user/player/rename', [ ->postJson('/user/player/rename', [
'pid' => $player->pid, 'pid' => $player->pid,
]) ])
->assertJson([ ->assertJsonValidationErrors('new_player_name');
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => trans('validation.attributes.player_name')]),
]);
// Only A-Za-z0-9_ are allowed // Only A-Za-z0-9_ are allowed
option(['player_name_rule' => 'official']); option(['player_name_rule' => 'official']);
$this->postJson('/user/player/rename', [ $this->postJson('/user/player/rename', [
'pid' => $player->pid, 'pid' => $player->pid,
'new_player_name' => '角色名', 'new_player_name' => '角色名',
])->assertJson([ ])->assertJsonValidationErrors('new_player_name');
'errno' => 1,
'msg' => trans('validation.player_name', ['attribute' => trans('validation.attributes.player_name')]),
]);
// Other invalid characters // Other invalid characters
option(['player_name_rule' => 'cjk']); option(['player_name_rule' => 'cjk']);
$this->postJson('/user/player/rename', [ $this->postJson('/user/player/rename', [
'pid' => $player->pid, 'pid' => $player->pid,
'new_player_name' => '\\', 'new_player_name' => '\\',
]) ])->assertJsonValidationErrors('new_player_name');
->assertJson([
'errno' => 1,
'msg' => trans('validation.player_name', ['attribute' => trans('validation.attributes.player_name')]),
]);
// Use a duplicated player name // Use a duplicated player name
$name = factory(Player::class)->create()->name; $name = factory(Player::class)->create()->name;
@ -318,11 +298,9 @@ class PlayerControllerTest extends TestCase
option(['single_player' => true]); option(['single_player' => true]);
$user = factory(User::class)->create(); $user = factory(User::class)->create();
$this->actingAs($user)->postJson('/user/player/bind') $this->actingAs($user)
->assertJson([ ->postJson('/user/player/bind')
'errno' => 1, ->assertJsonValidationErrors('player');
'msg' => trans('validation.required', ['attribute' => 'player']),
]);
$this->postJson('/user/player/bind', ['player' => 'abc']) $this->postJson('/user/player/bind', ['player' => 'abc'])
->assertJson([ ->assertJson([

View File

@ -19,24 +19,15 @@ class ReportControllerTest extends TestCase
// Without `tid` field // Without `tid` field
$this->actingAs($user) $this->actingAs($user)
->postJson('/skinlib/report') ->postJson('/skinlib/report')
->assertJson([ ->assertJsonValidationErrors('tid');
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'tid'])
]);
// Invalid texture // Invalid texture
$this->postJson('/skinlib/report', ['tid' => $texture->tid - 1]) $this->postJson('/skinlib/report', ['tid' => $texture->tid - 1])
->assertJson([ ->assertJsonValidationErrors('tid');
'errno' => 1,
'msg' => trans('validation.exists', ['attribute' => 'tid'])
]);
// Without `reason` field // Without `reason` field
$this->postJson('/skinlib/report', ['tid' => $texture->tid]) $this->postJson('/skinlib/report', ['tid' => $texture->tid])
->assertJson([ ->assertJsonValidationErrors('reason');
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'reason'])
]);
// Lack of score // Lack of score
$user->score = 0; $user->score = 0;
@ -139,31 +130,19 @@ class ReportControllerTest extends TestCase
// Without `id` field // Without `id` field
$this->actingAs($reporter) $this->actingAs($reporter)
->postJson('/admin/reports') ->postJson('/admin/reports')
->assertJson([ ->assertJsonValidationErrors('id');
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'id'])
]);
// Not existed // Not existed
$this->postJson('/admin/reports', ['id' => $report->id - 1]) $this->postJson('/admin/reports', ['id' => $report->id - 1])
->assertJson([ ->assertJsonValidationErrors('id');
'errno' => 1,
'msg' => trans('validation.exists', ['attribute' => 'id'])
]);
// Without `action` field // Without `action` field
$this->postJson('/admin/reports', ['id' => $report->id]) $this->postJson('/admin/reports', ['id' => $report->id])
->assertJson([ ->assertJsonValidationErrors('action');
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'action'])
]);
// Invalid action // Invalid action
$this->postJson('/admin/reports', ['id' => $report->id, 'action' => 'a']) $this->postJson('/admin/reports', ['id' => $report->id, 'action' => 'a'])
->assertJson([ ->assertJsonValidationErrors('action');
'errno' => 1,
'msg' => trans('validation.in', ['attribute' => 'action'])
]);
// Only process pending report // Only process pending report
$this->postJson('/admin/reports', ['id' => $report->id, 'action' => 'reject']) $this->postJson('/admin/reports', ['id' => $report->id, 'action' => 'reject'])

View File

@ -384,35 +384,23 @@ class SkinlibControllerTest extends TestCase
]); ]);
// Without `name` field // Without `name` field
$this->postJson('/skinlib/upload')->assertJson([ $this->postJson('/skinlib/upload')->assertJsonValidationErrors('name');
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'name']),
]);
// With some special chars // With some special chars
$this->postJson('/skinlib/upload', ['name' => '\\']) $this->postJson('/skinlib/upload', ['name' => '\\'])
->assertJson([ ->assertJsonValidationErrors('name');
'errno' => 1,
'msg' => trans('validation.no_special_chars', ['attribute' => 'name']),
]);
// Specified regular expression for texture name // Specified regular expression for texture name
option(['texture_name_regexp' => '/\\d+/']); option(['texture_name_regexp' => '/\\d+/']);
$this->postJson('/skinlib/upload', [ $this->postJson('/skinlib/upload', [
'name' => 'abc', 'name' => 'abc',
])->assertJson([ ])->assertJsonValidationErrors('name');
'errno' => 1,
'msg' => trans('validation.regex', ['attribute' => 'name']),
]);
option(['texture_name_regexp' => null]); option(['texture_name_regexp' => null]);
// Without file // Without file
$this->postJson('/skinlib/upload', [ $this->postJson('/skinlib/upload', [
'name' => 'texture', 'name' => 'texture',
])->assertJson([ ])->assertJsonValidationErrors('file');
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'file']),
]);
// Too large file // Too large file
option(['max_upload_file_size' => 2]); option(['max_upload_file_size' => 2]);
@ -420,20 +408,14 @@ class SkinlibControllerTest extends TestCase
$this->postJson('/skinlib/upload', [ $this->postJson('/skinlib/upload', [
'name' => 'texture', 'name' => 'texture',
'file' => $upload, 'file' => $upload,
])->assertJson([ ])->assertJsonValidationErrors('file');
'errno' => 1,
'msg' => trans('validation.max.file', ['attribute' => 'file', 'max' => '2']),
]);
option(['max_upload_file_size' => 1024]); option(['max_upload_file_size' => 1024]);
// Without `public` field // Without `public` field
$this->postJson('/skinlib/upload', [ $this->postJson('/skinlib/upload', [
'name' => 'texture', 'name' => 'texture',
'file' => 'content', // Though it is not a file, it is OK 'file' => 'content', // Though it is not a file, it is OK
])->assertJson([ ])->assertJsonValidationErrors('public');
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'public']),
]);
// Not a PNG image // Not a PNG image
$this->postJson( $this->postJson(
@ -843,38 +825,26 @@ class SkinlibControllerTest extends TestCase
// Without `tid` field // Without `tid` field
$this->actingAs($uploader) $this->actingAs($uploader)
->postJson('/skinlib/rename') ->postJson('/skinlib/rename')
->assertJson([ ->assertJsonValidationErrors('tid');
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'tid']),
]);
// `tid` is not a integer // `tid` is not a integer
$this->postJson('/skinlib/rename', [ $this->postJson('/skinlib/rename', [
'tid' => 'str', 'tid' => 'str',
]) ])
->assertJson([ ->assertJsonValidationErrors('tid');
'errno' => 1,
'msg' => trans('validation.integer', ['attribute' => 'tid']),
]);
// Without `new_name` field // Without `new_name` field
$this->postJson('/skinlib/rename', [ $this->postJson('/skinlib/rename', [
'tid' => $texture->tid, 'tid' => $texture->tid,
]) ])
->assertJson([ ->assertJsonValidationErrors('new_name');
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'new name']),
]);
// `new_name` has special chars // `new_name` has special chars
$this->postJson('/skinlib/rename', [ $this->postJson('/skinlib/rename', [
'tid' => $texture->tid, 'tid' => $texture->tid,
'new_name' => '\\', 'new_name' => '\\',
]) ])
->assertJson([ ->assertJsonValidationErrors('new_name');
'errno' => 1,
'msg' => trans('validation.no_special_chars', ['attribute' => 'new name']),
]);
// Non-existed texture // Non-existed texture
$this->postJson('/skinlib/rename', [ $this->postJson('/skinlib/rename', [

View File

@ -217,28 +217,19 @@ class UserControllerTest extends TestCase
// Change nickname without `new_nickname` field // Change nickname without `new_nickname` field
$this->postJson('/user/profile', ['action' => 'nickname']) $this->postJson('/user/profile', ['action' => 'nickname'])
->assertJson([ ->assertJsonValidationErrors('new_nickname');
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'new nickname']),
]);
// Invalid nickname // Invalid nickname
$this->postJson('/user/profile', [ $this->postJson('/user/profile', [
'action' => 'nickname', 'action' => 'nickname',
'new_nickname' => '\\', 'new_nickname' => '\\',
])->assertJson([ ])->assertJsonValidationErrors('new_nickname');
'errno' => 1,
'msg' => trans('validation.no_special_chars', ['attribute' => 'new nickname']),
]);
// Too long nickname // Too long nickname
$this->postJson('/user/profile', [ $this->postJson('/user/profile', [
'action' => 'nickname', 'action' => 'nickname',
'new_nickname' => Str::random(256), 'new_nickname' => Str::random(256),
])->assertJson([ ])->assertJsonValidationErrors('new_nickname');
'errno' => 1,
'msg' => trans('validation.max.string', ['attribute' => 'new nickname', 'max' => 255]),
]);
// Single player // Single player
option(['single_player' => true]); option(['single_player' => true]);
@ -260,50 +251,35 @@ class UserControllerTest extends TestCase
// Change password without `current_password` field // Change password without `current_password` field
$this->postJson('/user/profile', ['action' => 'password']) $this->postJson('/user/profile', ['action' => 'password'])
->assertJson([ ->assertJsonValidationErrors('current_password');
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'current password']),
]);
// Too short current password // Too short current password
$this->postJson('/user/profile', [ $this->postJson('/user/profile', [
'action' => 'password', 'action' => 'password',
'current_password' => '1', 'current_password' => '1',
'new_password' => '12345678', 'new_password' => '12345678',
])->assertJson([ ])->assertJsonValidationErrors('current_password');
'errno' => 1,
'msg' => trans('validation.min.string', ['attribute' => 'current password', 'min' => 6]),
]);
// Too long current password // Too long current password
$this->postJson('/user/profile', [ $this->postJson('/user/profile', [
'action' => 'password', 'action' => 'password',
'current_password' => Str::random(33), 'current_password' => Str::random(33),
'new_password' => '12345678', 'new_password' => '12345678',
])->assertJson([ ])->assertJsonValidationErrors('current_password');
'errno' => 1,
'msg' => trans('validation.max.string', ['attribute' => 'current password', 'max' => 32]),
]);
// Too short new password // Too short new password
$this->postJson('/user/profile', [ $this->postJson('/user/profile', [
'action' => 'password', 'action' => 'password',
'current_password' => '12345678', 'current_password' => '12345678',
'new_password' => '1', 'new_password' => '1',
])->assertJson([ ])->assertJsonValidationErrors('new_password');
'errno' => 1,
'msg' => trans('validation.min.string', ['attribute' => 'new password', 'min' => 8]),
]);
// Too long new password // Too long new password
$this->postJson('/user/profile', [ $this->postJson('/user/profile', [
'action' => 'password', 'action' => 'password',
'current_password' => '12345678', 'current_password' => '12345678',
'new_password' => Str::random(33), 'new_password' => Str::random(33),
])->assertJson([ ])->assertJsonValidationErrors('new_password');
'errno' => 1,
'msg' => trans('validation.max.string', ['attribute' => 'new password', 'max' => 32]),
]);
// Wrong old password // Wrong old password
$this->postJson('/user/profile', [ $this->postJson('/user/profile', [
@ -336,39 +312,27 @@ class UserControllerTest extends TestCase
'/user/profile', '/user/profile',
['action' => 'email'] ['action' => 'email']
) )
->assertJson([ ->assertJsonValidationErrors('new_email');
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'new email']),
]);
// Invalid email // Invalid email
$this->postJson('/user/profile', [ $this->postJson('/user/profile', [
'action' => 'email', 'action' => 'email',
'new_email' => 'not_an_email', 'new_email' => 'not_an_email',
])->assertJson([ ])->assertJsonValidationErrors('new_email');
'errno' => 1,
'msg' => trans('validation.email', ['attribute' => 'new email']),
]);
// Too short current password // Too short current password
$this->postJson('/user/profile', [ $this->postJson('/user/profile', [
'action' => 'email', 'action' => 'email',
'new_email' => 'a@b.c', 'new_email' => 'a@b.c',
'password' => '1', 'password' => '1',
])->assertJson([ ])->assertJsonValidationErrors('password');
'errno' => 1,
'msg' => trans('validation.min.string', ['attribute' => 'password', 'min' => 6]),
]);
// Too long current password // Too long current password
$this->postJson('/user/profile', [ $this->postJson('/user/profile', [
'action' => 'email', 'action' => 'email',
'new_email' => 'a@b.c', 'new_email' => 'a@b.c',
'password' => Str::random(33), 'password' => Str::random(33),
])->assertJson([ ])->assertJsonValidationErrors('password');
'errno' => 1,
'msg' => trans('validation.max.string', ['attribute' => 'password', 'max' => 32]),
]);
// Use a duplicated email // Use a duplicated email
$this->postJson('/user/profile', [ $this->postJson('/user/profile', [
@ -413,28 +377,19 @@ class UserControllerTest extends TestCase
'/user/profile', '/user/profile',
['action' => 'delete'] ['action' => 'delete']
) )
->assertJson([ ->assertJsonValidationErrors('password');
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'password']),
]);
// Too short current password // Too short current password
$this->postJson('/user/profile', [ $this->postJson('/user/profile', [
'action' => 'delete', 'action' => 'delete',
'password' => '1', 'password' => '1',
])->assertJson([ ])->assertJsonValidationErrors('password');
'errno' => 1,
'msg' => trans('validation.min.string', ['attribute' => 'password', 'min' => 6]),
]);
// Too long current password // Too long current password
$this->postJson('/user/profile', [ $this->postJson('/user/profile', [
'action' => 'delete', 'action' => 'delete',
'password' => Str::random(33), 'password' => Str::random(33),
])->assertJson([ ])->assertJsonValidationErrors('password');
'errno' => 1,
'msg' => trans('validation.max.string', ['attribute' => 'password', 'max' => 32]),
]);
// Wrong password // Wrong password
$this->postJson('/user/profile', [ $this->postJson('/user/profile', [
@ -475,18 +430,12 @@ class UserControllerTest extends TestCase
// Without `tid` field // Without `tid` field
$this->actingAs($user) $this->actingAs($user)
->postJson('/user/profile/avatar') ->postJson('/user/profile/avatar')
->assertJson([ ->assertJsonValidationErrors('tid');
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'tid']),
]);
// TID is not a integer // TID is not a integer
$this->actingAs($user) $this->actingAs($user)
->postJson('/user/profile/avatar', ['tid' => 'string']) ->postJson('/user/profile/avatar', ['tid' => 'string'])
->assertJson([ ->assertJsonValidationErrors('tid');
'errno' => 1,
'msg' => trans('validation.integer', ['attribute' => 'tid']),
]);
// Texture cannot be found // Texture cannot be found
$this->actingAs($user) $this->actingAs($user)