Nomalize JSON response structure

This commit is contained in:
Pig Fang 2019-04-23 19:14:41 +08:00
parent fd541e7365
commit 6d03e47526
40 changed files with 349 additions and 331 deletions

View File

@ -154,10 +154,7 @@ class AuthController extends Controller
Auth::login($user);
return json([
'code' => 0,
'message' => trans('auth.register.success'),
]);
return json(trans('auth.register.success'), 0);
}
public function forgot()
@ -190,11 +187,7 @@ class AuthController extends Controller
// Rate limit
if ($remain > 0) {
return json([
'code' => 2,
'message' => trans('auth.forgot.frequent-mail'),
'remain' => $remain,
]);
return json(trans('auth.forgot.frequent-mail'), 2);
}
$user = User::where('email', $request->email)->first();

View File

@ -58,9 +58,9 @@ class ClosetController extends Controller
});
$totalPages = ceil($items->count() / $perPage);
return response()->json([
'category' => $category,
'items' => $items,
return json('', 0, [
'category' => $category,
'items' => $items,
'total_pages' => $totalPages,
]);
}

View File

@ -64,10 +64,15 @@ class PlayerController extends Controller
public function listAll()
{
return Auth::user()
->players()
->select('pid', 'name', 'tid_skin', 'tid_cape')
->get();
return json(
'',
0,
Auth::user()
->players()
->select('pid', 'name', 'tid_skin', 'tid_cape')
->get()
->toArray()
);
}
public function add(Request $request)
@ -129,11 +134,6 @@ class PlayerController extends Controller
return json(trans('user.player.delete.success', ['name' => $playerName]), 0);
}
public function show()
{
return response()->json($this->player->toArray());
}
public function rename(Request $request)
{
$this->validate($request, [

View File

@ -42,11 +42,7 @@ class PluginController extends Controller
}
}
return json([
'code' => 1,
'message' => trans('admin.plugins.operations.unsatisfied.notice'),
'reason' => $reason,
]);
return json(trans('admin.plugins.operations.unsatisfied.notice'), 1, compact('reason'));
}
$plugins->enable($name);

View File

@ -119,8 +119,8 @@ class SkinlibController extends Controller
}
}
return response()->json([
'items' => $textures,
return json('', 0, [
'items' => $textures,
'current_uid' => $user ? $user->uid : 0,
'total_pages' => $totalPages,
]);
@ -165,9 +165,9 @@ class SkinlibController extends Controller
public function info($tid)
{
if ($t = Texture::find($tid)) {
return json($t->toArray());
return json('', 0, $t->toArray());
} else {
return json([]);
return abort(404);
}
}
@ -228,9 +228,7 @@ class SkinlibController extends Controller
// if the texture already uploaded was set to private,
// then allow to re-upload it.
if ($result->type == $t->type && $result->public) {
return json(trans('skinlib.upload.repeated'), 0, [
'tid' => $result->tid,
]);
return json(trans('skinlib.upload.repeated'), 0, ['tid' => $result->tid]);
}
}
}
@ -340,11 +338,10 @@ class SkinlibController extends Controller
$t->public = ! $t->public;
$t->save();
return json([
'code' => 0,
'message' => trans('skinlib.privacy.success', ['privacy' => (! $t->public ? trans('general.private') : trans('general.public'))]),
'public' => $t->public,
]);
return json(
trans('skinlib.privacy.success', ['privacy' => (! $t->public ? trans('general.private') : trans('general.public'))]),
0
);
}
// @codeCoverageIgnore

View File

@ -45,7 +45,7 @@ class UserController extends Controller
{
$user = Auth::user();
return [
return json('', 0, [
'user' => [
'score' => $user->score,
'lastSignAt' => $user->last_sign_at,
@ -56,7 +56,7 @@ class UserController extends Controller
],
'signAfterZero' => option('sign_after_zero'),
'signGapTime' => option('sign_gap_time'),
];
]);
}
/**
@ -94,11 +94,9 @@ class UserController extends Controller
$acquiredScore = $user->sign();
$gap = option('sign_gap_time');
return json([
'code' => 0,
'message' => trans('user.sign-success', ['score' => $acquiredScore]),
'score' => $user->score,
'storage' => $this->calculatePercentageUsed($user->getStorageUsed(), option('score_per_storage')),
return json(trans('user.sign-success', ['score' => $acquiredScore]), 0, [
'score' => $user->score,
'storage' => $this->calculatePercentageUsed($user->getStorageUsed(), option('score_per_storage')),
'remaining_time' => $gap > 1 ? round($gap) : $gap,
]);
} else {
@ -130,7 +128,7 @@ class UserController extends Controller
$remain = 60 + session('last_mail_time', 0) - time();
if ($remain > 0) {
return json(trans('user.verification.frequent-mail'));
return json(trans('user.verification.frequent-mail'), 1);
}
$user = Auth::user();
@ -250,11 +248,7 @@ class UserController extends Controller
if ($user->delete()) {
session()->flush();
return response()
->json([
'code' => 0,
'message' => trans('user.profile.delete.success'),
]);
return json(trans('user.profile.delete.success'), 0);
}
break; // @codeCoverageIgnore

View File

@ -12,10 +12,7 @@ class CheckPlayerExist
{
if ($request->has('pid') && $request->isMethod('post')) {
if (is_null(Player::find($request->input('pid')))) {
return response()->json([
'code' => 1,
'message' => trans('general.unexistent-player'),
]);
return json(trans('general.unexistent-player'), 1);
} else {
return $next($request);
}

View File

@ -20,10 +20,7 @@ class CheckPlayerOwner
$player = Player::find($pid);
if ($player->uid != auth()->id()) {
return response()->json([
'code' => 1,
'message' => trans('admin.players.no-permission'),
]);
return json(trans('admin.players.no-permission'), 1);
}
}

View File

@ -50,10 +50,11 @@ if (! function_exists('json')) {
return Response::json($args[0]);
} elseif (count($args) == 3 && is_array($args[2])) {
// The third argument is array of extra fields
return Response::json(array_merge([
return Response::json([
'code' => $args[1],
'message' => $args[0],
], $args[2]));
'data' => $args[2],
]);
} else {
return Response::json([
'code' => Arr::get($args, 1, 1),

View File

@ -71,7 +71,7 @@ export default {
},
methods: {
async fetchList() {
this.players = await this.$http.get('/user/player/list')
this.players = (await this.$http.get('/user/player/list')).data
},
async submit() {
if (!this.selected) {

View File

@ -22,11 +22,11 @@ export default Vue.extend({
}
const {
code, message, reason,
code, message, data: { reason } = { reason: [] },
} = await this.$http.post(
'/admin/plugins/manage',
{ action: 'enable', name }
) as { code: number, message: string, reason: string[] }
) as { code: number, message: string, data: { reason: string[] } }
if (code === 0) {
this.$message.success(message)
this.$set(this.plugins[originalIndex], 'enabled', true)

View File

@ -122,14 +122,14 @@ export default {
},
async resolve(report, action) {
const {
code, message, status,
code, message, data,
} = await this.$http.post(
'/admin/reports',
{ id: report.id, action }
)
if (code === 0) {
this.$message.success(message)
report.status = status
report.status = data.status
} else {
this.$message.warning(message)
}

View File

@ -99,7 +99,7 @@ export default {
this.pending = true
const {
code, message, login_fails: loginFails,
code, message, data: { login_fails: loginFails } = { login_fails: 0 },
} = await this.$http.post(
'/auth/login',
{

View File

@ -176,7 +176,9 @@ export default {
async fetchData() {
this.pending = true
const {
items, total_pages: totalPages, current_uid: currentUid,
data: {
items, total_pages: totalPages, current_uid: currentUid,
},
} = await this.$http.get(
'/skinlib/data',
{

View File

@ -220,7 +220,7 @@ export default {
},
methods: {
async fetchData() {
const data = await this.$http.get(`/skinlib/info/${this.tid}`)
const { data = {} } = await this.$http.get(`/skinlib/info/${this.tid}`)
this.name = data.name
this.type = data.type
this.likes = data.likes

View File

@ -160,7 +160,7 @@ export default {
this.uploading = true
const {
code, message, tid,
code, message, data: { tid } = { tid: 0 },
} = await this.$http.post('/skinlib/upload', data)
if (code === 0) {
this.$message.success(message)

View File

@ -49,7 +49,7 @@ export default {
},
methods: {
async fetchPlayers() {
const players = await this.$http.get('/user/player/list')
const players = (await this.$http.get('/user/player/list')).data
this.players = players.map(player => player.name)
;[this.selected] = this.players
},

View File

@ -200,7 +200,9 @@ export default {
search() {}, // eslint-disable-line no-empty-function
async loadCloset(page = 1) {
const {
items, category, total_pages: totalPages,
data: {
items, category, total_pages: totalPages,
},
} = await this.$http.get(
'/user/closet-data',
{
@ -226,7 +228,7 @@ export default {
this.loadCloset(page)
},
async selectTexture(tid) {
const { type, hash } = await this.$http.get(`/skinlib/info/${tid}`)
const { data: { type, hash } } = await this.$http.get(`/skinlib/info/${tid}`)
if (type === 'cape') {
this.capeUrl = `/textures/${hash}`
this.selectedCape = tid

View File

@ -176,7 +176,7 @@ export default {
},
methods: {
async fetchScoreInfo() {
const data = await this.$http.get('/user/score-info')
const { data } = await this.$http.get('/user/score-info')
this.lastSignAt = new Date(data.user.lastSignAt)
this.signAfterZero = data.signAfterZero
this.signGap = data.signGapTime * 3600 * 1000
@ -187,16 +187,18 @@ export default {
this.score = data.user.score
},
async sign() {
const result = await this.$http.post('/user/sign')
const {
code, message, data,
} = await this.$http.post('/user/sign')
if (result.code === 0) {
this.$message.success(result.message)
this.score = result.score
if (code === 0) {
this.$message.success(message)
this.score = data.score
this.lastSignAt = new Date()
this.storageUsed = result.storage.used
this.storageTotal = result.storage.total
this.storageUsed = data.storage.used
this.storageTotal = data.storage.total
} else {
this.$message.warning(result.message)
this.$message.warning(message)
}
},
},

View File

@ -195,7 +195,7 @@ export default {
},
methods: {
async fetchPlayers() {
this.players = await this.$http.get('/user/player/list')
this.players = (await this.$http.get('/user/player/list')).data
},
togglePreviewer() {
this.using3dPreviewer = !this.using3dPreviewer
@ -207,13 +207,13 @@ export default {
this.preview2d.cape = player.tid_cape
if (player.tid_skin) {
const skin = await this.$http.get(`/skinlib/info/${player.tid_skin}`)
const { data: skin } = await this.$http.get(`/skinlib/info/${player.tid_skin}`)
this.skinUrl = `${this.baseUrl}/textures/${skin.hash}`
} else {
this.skinUrl = ''
}
if (player.tid_cape) {
const cape = await this.$http.get(`/skinlib/info/${player.tid_cape}`)
const { data: cape } = await this.$http.get(`/skinlib/info/${player.tid_cape}`)
this.capeUrl = `${this.baseUrl}/textures/${cape.hash}`
} else {
this.capeUrl = ''

View File

@ -4,7 +4,7 @@ import ApplyToPlayerDialog from '@/components/ApplyToPlayerDialog.vue'
test('submit applying texture', async () => {
window.$ = jest.fn(() => ({ modal() {} }))
Vue.prototype.$http.get.mockResolvedValue([{ pid: 1 }])
Vue.prototype.$http.get.mockResolvedValue({ data: [{ pid: 1 }] })
Vue.prototype.$http.post.mockResolvedValueOnce({ code: 1 })
.mockResolvedValue({ code: 0, message: 'ok' })
const wrapper = mount(ApplyToPlayerDialog)
@ -46,7 +46,7 @@ test('submit applying texture', async () => {
})
test('compute avatar URL', () => {
Vue.prototype.$http.get.mockResolvedValue({})
Vue.prototype.$http.get.mockResolvedValue({ data: {} })
// eslint-disable-next-line camelcase
const wrapper = mount<Vue & { avatarUrl(player: { tid_skin: number }): string }>(ApplyToPlayerDialog)
const { avatarUrl } = wrapper.vm

View File

@ -57,7 +57,7 @@ test('enable plugin', async () => {
])
Vue.prototype.$http.post
.mockResolvedValueOnce({
code: 1, message: '1', reason: ['`a<div></div>`b'],
code: 1, message: '1', data: { reason: ['`a<div></div>`b'] },
})
.mockResolvedValue({ code: 0, message: '0' })
Vue.prototype.$confirm

View File

@ -38,7 +38,7 @@ test('delete texture', async () => {
Vue.prototype.$http.post
.mockResolvedValueOnce({ code: 1, message: 'fail' })
.mockResolvedValue({
code: 0, message: 'ok', status: 1,
code: 0, message: 'ok', data: { status: 1 },
})
const wrapper = mount(Reports)
await wrapper.vm.$nextTick()
@ -62,7 +62,7 @@ test('ban uploader', async () => {
Vue.prototype.$http.get.mockResolvedValue({ data: [{ id: 1, status: 0 }] })
Vue.prototype.$http.post
.mockResolvedValue({
code: 0, message: 'ok', status: 1,
code: 0, message: 'ok', data: { status: 1 },
})
const wrapper = mount(Reports)
await wrapper.vm.$nextTick()
@ -82,7 +82,7 @@ test('reject', async () => {
Vue.prototype.$http.get.mockResolvedValue({ data: [{ id: 1, status: 0 }] })
Vue.prototype.$http.post
.mockResolvedValue({
code: 0, message: 'ok', status: 2,
code: 0, message: 'ok', data: { status: 2 },
})
const wrapper = mount(Reports)
await wrapper.vm.$nextTick()

View File

@ -22,9 +22,9 @@ test('login', async () => {
window.blessing.extra = { tooManyFails: false }
Vue.prototype.$http.post
.mockResolvedValueOnce({ code: 1, message: 'fail' })
.mockResolvedValueOnce({ code: 1, login_fails: 4 })
.mockResolvedValueOnce({ code: 1, login_fails: 4 })
.mockResolvedValueOnce({ code: 1, login_fails: 4 })
.mockResolvedValueOnce({ code: 1, data: { login_fails: 4 } })
.mockResolvedValueOnce({ code: 1, data: { login_fails: 4 } })
.mockResolvedValueOnce({ code: 1, data: { login_fails: 4 } })
.mockResolvedValueOnce({ code: 0, message: 'ok' })
const wrapper = mount(Login, { stubs: { Captcha } })
const form = wrapper.find('form')

View File

@ -45,7 +45,9 @@ jest.mock('element-ui/lib/option', () => ({
test('fetch data before mounting', () => {
Vue.prototype.$http.get.mockResolvedValue({
items: [], total_pages: 0, current_uid: 0,
data: {
items: [], total_pages: 0, current_uid: 0,
},
})
mount(List)
expect(Vue.prototype.$http.get).toBeCalledWith(
@ -58,7 +60,9 @@ test('fetch data before mounting', () => {
test('empty skin library', () => {
Vue.prototype.$http.get.mockResolvedValue({
items: [], total_pages: 0, current_uid: 0,
data: {
items: [], total_pages: 0, current_uid: 0,
},
})
const wrapper = mount(List)
expect(wrapper.text()).toContain('general.noResult')
@ -66,7 +70,9 @@ test('empty skin library', () => {
test('toggle texture type', () => {
Vue.prototype.$http.get.mockResolvedValue({
items: [], total_pages: 0, current_uid: 0,
data: {
items: [], total_pages: 0, current_uid: 0,
},
})
const wrapper = mount(List)
const select = wrapper.find({ name: 'ElSelect' })
@ -104,7 +110,9 @@ test('toggle texture type', () => {
test('check specified uploader', async () => {
Vue.prototype.$http.get.mockResolvedValue({
items: [], total_pages: 0, current_uid: 1,
data: {
items: [], total_pages: 0, current_uid: 1,
},
})
const wrapper = mount(List)
await wrapper.vm.$nextTick()
@ -127,7 +135,9 @@ test('check specified uploader', async () => {
test('sort items', () => {
Vue.prototype.$http.get.mockResolvedValue({
items: [], total_pages: 0, current_uid: 0,
data: {
items: [], total_pages: 0, current_uid: 0,
},
})
const wrapper = mount(List)
const buttons = wrapper.find('.advanced-filter').findAll(Button)
@ -155,7 +165,9 @@ test('sort items', () => {
test('search by keyword', () => {
Vue.prototype.$http.get.mockResolvedValue({
items: [], total_pages: 0, current_uid: 0,
data: {
items: [], total_pages: 0, current_uid: 0,
},
})
const wrapper = mount(List)
@ -180,7 +192,9 @@ test('search by keyword', () => {
test('reset all filters', () => {
Vue.prototype.$http.get.mockResolvedValue({
items: [], total_pages: 0, current_uid: 0,
data: {
items: [], total_pages: 0, current_uid: 0,
},
})
const wrapper = mount(List)
wrapper.findAll('option').at(3)
@ -196,7 +210,9 @@ test('reset all filters', () => {
test('is anonymous', () => {
Vue.prototype.$http.get.mockResolvedValue({
items: [], total_pages: 0, current_uid: 0,
data: {
items: [], total_pages: 0, current_uid: 0,
},
})
const wrapper = mount<Vue & { anonymous: boolean }>(List)
expect(wrapper.vm.anonymous).toBeTrue()
@ -204,7 +220,9 @@ test('is anonymous', () => {
test('on page changed', () => {
Vue.prototype.$http.get.mockResolvedValue({
items: [], total_pages: 0, current_uid: 0,
data: {
items: [], total_pages: 0, current_uid: 0,
},
})
const wrapper = mount<Vue & { pageChanged(page: number): void }>(List)
wrapper.vm.pageChanged(2)
@ -218,11 +236,13 @@ test('on page changed', () => {
test('on like toggled', async () => {
Vue.prototype.$http.get.mockResolvedValue({
items: [{
tid: 1, liked: false, likes: 0,
}],
total_pages: 1,
current_uid: 0,
data: {
items: [{
tid: 1, liked: false, likes: 0,
}],
total_pages: 1,
current_uid: 0,
},
})
const wrapper = mount<Vue & {
onLikeToggled(tid: number, like: boolean): void,

View File

@ -28,7 +28,7 @@ const previewer = Vue.extend({
})
test('button for adding to closet should be disabled if not auth', () => {
Vue.prototype.$http.get.mockResolvedValue({})
Vue.prototype.$http.get.mockResolvedValue({ data: {} })
const wrapper = mount(Show, {
mocks: {
$route: ['/skinlib/show/1', '1'],
@ -39,7 +39,7 @@ test('button for adding to closet should be disabled if not auth', () => {
})
test('button for adding to closet should be disabled if auth', () => {
Vue.prototype.$http.get.mockResolvedValue({})
Vue.prototype.$http.get.mockResolvedValue({ data: {} })
Object.assign(window.blessing.extra, { inCloset: true, currentUid: 1 })
const wrapper = mount(Show, {
mocks: {
@ -51,7 +51,7 @@ test('button for adding to closet should be disabled if auth', () => {
})
test('likes count indicator', async () => {
Vue.prototype.$http.get.mockResolvedValue({ likes: 2 })
Vue.prototype.$http.get.mockResolvedValue({ data: { likes: 2 } })
const wrapper = mount(Show, {
mocks: {
$route: ['/skinlib/show/1', '1'],
@ -65,11 +65,13 @@ test('likes count indicator', async () => {
test('render basic information', async () => {
Vue.prototype.$http.get.mockResolvedValue({
name: 'my-texture',
type: 'alex',
hash: '123',
size: 2,
upload_at: '2018',
data: {
name: 'my-texture',
type: 'alex',
hash: '123',
size: 2,
upload_at: '2018',
},
})
const wrapper = mount(Show, {
mocks: {
@ -88,7 +90,7 @@ test('render basic information', async () => {
test('render action text of editing texture name', async () => {
Object.assign(window.blessing.extra, { admin: true })
Vue.prototype.$http.get.mockResolvedValue({ uploader: 1, name: 'name' })
Vue.prototype.$http.get.mockResolvedValue({ data: { uploader: 1, name: 'name' } })
let wrapper = mount(Show, {
mocks: {
@ -110,7 +112,7 @@ test('render action text of editing texture name', async () => {
test('render nickname of uploader', () => {
Object.assign(window.blessing.extra, { nickname: null })
Vue.prototype.$http.get.mockResolvedValue({})
Vue.prototype.$http.get.mockResolvedValue({ data: {} })
const wrapper = mount(Show, {
mocks: {
$route: ['/skinlib/show/1', '1'],
@ -121,7 +123,7 @@ test('render nickname of uploader', () => {
test('operation panel should not be rendered if not auth', () => {
Object.assign(window.blessing.extra, { currentUid: 0 })
Vue.prototype.$http.get.mockResolvedValue({})
Vue.prototype.$http.get.mockResolvedValue({ data: {} })
const wrapper = mount(Show, {
mocks: {
$route: ['/skinlib/show/1', '1'],
@ -132,7 +134,7 @@ test('operation panel should not be rendered if not auth', () => {
test('download texture', async () => {
Object.assign(window.blessing.extra, { currentUid: 1 })
Vue.prototype.$http.get.mockResolvedValue({ tid: 1, name: 'abc' })
Vue.prototype.$http.get.mockResolvedValue({ data: { tid: 1, name: 'abc' } })
const wrapper = mount(Show, {
mocks: {
$route: ['/skinlib/show/1', '1'],
@ -144,7 +146,7 @@ test('download texture', async () => {
test('link to downloading texture', async () => {
Object.assign(window.blessing.extra, { download: false })
Vue.prototype.$http.get.mockResolvedValue({ hash: '123' })
Vue.prototype.$http.get.mockResolvedValue({ data: { hash: '123' } })
const wrapper = mount(Show, {
mocks: {
$route: ['/skinlib/show/1', '1'],
@ -157,7 +159,7 @@ test('link to downloading texture', async () => {
test('set as avatar', async () => {
Object.assign(window.blessing.extra, { currentUid: 1, inCloset: true })
Vue.prototype.$http.get.mockResolvedValueOnce({ type: 'steve' })
Vue.prototype.$http.get.mockResolvedValueOnce({ data: { type: 'steve' } })
const wrapper = mount(Show, {
mocks: {
$route: ['/skinlib/show/1', '1'],
@ -170,7 +172,7 @@ test('set as avatar', async () => {
})
test('hide "set avatar" button when texture is cape', async () => {
Vue.prototype.$http.get.mockResolvedValueOnce({ type: 'cape' })
Vue.prototype.$http.get.mockResolvedValueOnce({ data: { type: 'cape' } })
const wrapper = mount(Show, {
mocks: {
$route: ['/skinlib/show/1', '1'],
@ -183,7 +185,7 @@ test('hide "set avatar" button when texture is cape', async () => {
test('add to closet', async () => {
Object.assign(window.blessing.extra, { currentUid: 1, inCloset: false })
Vue.prototype.$http.get.mockResolvedValue({ name: 'wow', likes: 2 })
Vue.prototype.$http.get.mockResolvedValue({ data: { name: 'wow', likes: 2 } })
Vue.prototype.$http.post.mockResolvedValue({ code: 0, message: '' })
Vue.prototype.$prompt.mockResolvedValue({ value: 'a' } as MessageBoxData)
const wrapper = mount<Component>(Show, {
@ -200,7 +202,7 @@ test('add to closet', async () => {
test('remove from closet', async () => {
Object.assign(window.blessing.extra, { currentUid: 1, inCloset: true })
Vue.prototype.$http.get.mockResolvedValue({ likes: 2 })
Vue.prototype.$http.get.mockResolvedValue({ data: { likes: 2 } })
Vue.prototype.$http.post.mockResolvedValue({ code: 0 })
const wrapper = mount<Component>(Show, {
mocks: {
@ -216,7 +218,7 @@ test('remove from closet', async () => {
test('change texture name', async () => {
Object.assign(window.blessing.extra, { admin: true })
Vue.prototype.$http.get.mockResolvedValue({ name: 'old-name' })
Vue.prototype.$http.get.mockResolvedValue({ data: { name: 'old-name' } })
Vue.prototype.$http.post
.mockResolvedValueOnce({ code: 1, message: '1' })
.mockResolvedValue({ code: 0, message: '0' })
@ -254,7 +256,7 @@ test('change texture name', async () => {
})
test('change texture model', async () => {
Vue.prototype.$http.get.mockResolvedValue({ type: 'steve' })
Vue.prototype.$http.get.mockResolvedValue({ data: { type: 'steve' } })
Vue.prototype.$http.post
.mockResolvedValueOnce({ code: 1, message: '1' })
.mockResolvedValue({ code: 0, message: '0' })
@ -297,7 +299,7 @@ test('change texture model', async () => {
})
test('toggle privacy', async () => {
Vue.prototype.$http.get.mockResolvedValue({ public: true })
Vue.prototype.$http.get.mockResolvedValue({ data: { public: true } })
Vue.prototype.$http.post
.mockResolvedValueOnce({ code: 1, message: '1' })
.mockResolvedValue({ code: 0, message: '0' })
@ -336,7 +338,7 @@ test('toggle privacy', async () => {
})
test('delete texture', async () => {
Vue.prototype.$http.get.mockResolvedValue({})
Vue.prototype.$http.get.mockResolvedValue({ data: {} })
Vue.prototype.$http.post
.mockResolvedValueOnce({ code: 1, message: '1' })
.mockResolvedValue({ code: 0, message: '0' })
@ -372,7 +374,7 @@ test('delete texture', async () => {
})
test('report texture', async () => {
Vue.prototype.$http.get.mockResolvedValue({ report: 0 })
Vue.prototype.$http.get.mockResolvedValue({ data: { report: 0 } })
Vue.prototype.$http.post
.mockResolvedValueOnce({ code: 1, message: 'duplicated' })
.mockResolvedValue({ code: 0, message: 'success' })
@ -422,7 +424,7 @@ test('report texture', async () => {
test('apply texture to player', () => {
Vue.prototype.$http.get
.mockResolvedValue({})
.mockResolvedValue({ data: {} })
.mockResolvedValue([])
const wrapper = mount(Show, {
mocks: {

View File

@ -116,7 +116,7 @@ test('upload file', async () => {
Vue.prototype.$http.post
.mockResolvedValueOnce({ code: 1, message: '1' })
.mockResolvedValueOnce({
code: 0, message: '0', tid: 1,
code: 0, message: '0', data: { tid: 1 },
})
const wrapper = mount(Upload, {

View File

@ -4,7 +4,7 @@ import Bind from '@/views/user/Bind.vue'
test('list existed players', async () => {
Vue.prototype.$http.get
.mockResolvedValue([{ name: 'a' }, { name: 'b' }])
.mockResolvedValue({ data: [{ name: 'a' }, { name: 'b' }] })
const wrapper = mount(Bind)
await wrapper.vm.$nextTick()
const options = wrapper.findAll('option')
@ -12,7 +12,7 @@ test('list existed players', async () => {
})
test('show input box', async () => {
Vue.prototype.$http.get.mockResolvedValue([])
Vue.prototype.$http.get.mockResolvedValue({ data: [] })
const wrapper = mount(Bind)
await wrapper.vm.$nextTick()
const input = wrapper.find('input')
@ -20,7 +20,7 @@ test('show input box', async () => {
})
test('submit', async () => {
Vue.prototype.$http.get.mockResolvedValue([])
Vue.prototype.$http.get.mockResolvedValue({ data: [] })
Vue.prototype.$http.post
.mockResolvedValueOnce({ code: 1, message: 'fail' })
.mockResolvedValueOnce({ code: 0, message: 'ok' })

View File

@ -7,7 +7,7 @@ import Previewer from '@/components/Previewer.vue'
window.blessing.extra = { unverified: false }
test('fetch closet data before mount', () => {
Vue.prototype.$http.get.mockResolvedValue({})
Vue.prototype.$http.get.mockResolvedValue({ data: {} })
mount(Closet)
jest.runAllTicks()
expect(Vue.prototype.$http.get).toBeCalledWith(
@ -22,13 +22,17 @@ test('fetch closet data before mount', () => {
test('switch tabs', () => {
Vue.prototype.$http.get.mockResolvedValue({
items: [],
category: 'skin',
total_pages: 1,
data: {
items: [],
category: 'skin',
total_pages: 1,
},
}).mockResolvedValueOnce({
items: [],
category: 'cape',
total_pages: 1,
data: {
items: [],
category: 'cape',
total_pages: 1,
},
})
const wrapper = mount(Closet)
@ -58,7 +62,7 @@ test('switch tabs', () => {
})
test('different categories', () => {
Vue.prototype.$http.get.mockResolvedValue({})
Vue.prototype.$http.get.mockResolvedValue({ data: {} })
const wrapper = mount(Closet)
expect(wrapper.findAll('.nav-tabs > li').at(0)
@ -72,7 +76,7 @@ test('different categories', () => {
})
test('search textures', () => {
Vue.prototype.$http.get.mockResolvedValue({})
Vue.prototype.$http.get.mockResolvedValue({ data: {} })
const wrapper = mount(Closet)
const input = wrapper.find('input')
@ -91,7 +95,7 @@ test('search textures', () => {
})
test('empty closet', () => {
Vue.prototype.$http.get.mockResolvedValue({})
Vue.prototype.$http.get.mockResolvedValue({ data: {} })
const wrapper = mount(Closet)
expect(wrapper.find('#skin-category').text()).toContain('user.emptyClosetMsg')
wrapper.setData({ category: 'cape' })
@ -99,7 +103,7 @@ test('empty closet', () => {
})
test('no matched search result', () => {
Vue.prototype.$http.get.mockResolvedValue({})
Vue.prototype.$http.get.mockResolvedValue({ data: {} })
const wrapper = mount(Closet)
wrapper.setData({ query: 'q' })
expect(wrapper.find('#skin-category').text()).toContain('general.noResult')
@ -109,12 +113,14 @@ test('no matched search result', () => {
test('render items', async () => {
Vue.prototype.$http.get.mockResolvedValue({
items: [
{ tid: 1 },
{ tid: 2 },
],
category: 'skin',
total_pages: 1,
data: {
items: [
{ tid: 1 },
{ tid: 2 },
],
category: 'skin',
total_pages: 1,
},
})
const wrapper = mount(Closet)
await wrapper.vm.$nextTick()
@ -122,7 +128,7 @@ test('render items', async () => {
})
test('reload closet when page changed', () => {
Vue.prototype.$http.get.mockResolvedValue({})
Vue.prototype.$http.get.mockResolvedValue({ data: {} })
const wrapper = mount<Vue & { pageChanged(): void }>(Closet)
wrapper.vm.pageChanged()
jest.runAllTicks()
@ -130,7 +136,7 @@ test('reload closet when page changed', () => {
})
test('remove skin item', () => {
Vue.prototype.$http.get.mockResolvedValue({})
Vue.prototype.$http.get.mockResolvedValue({ data: {} })
const wrapper = mount<Vue & { removeSkinItem(tid: number): void }>(Closet)
wrapper.setData({ skinItems: [{ tid: 1 }] })
wrapper.vm.removeSkinItem(0)
@ -138,7 +144,7 @@ test('remove skin item', () => {
})
test('remove cape item', () => {
Vue.prototype.$http.get.mockResolvedValue({})
Vue.prototype.$http.get.mockResolvedValue({ data: {} })
const wrapper = mount<Vue & { removeCapeItem(tid: number): void }>(Closet)
wrapper.setData({ capeItems: [{ tid: 1 }], category: 'cape' })
wrapper.vm.removeCapeItem(0)
@ -147,9 +153,9 @@ test('remove cape item', () => {
test('select texture', async () => {
Vue.prototype.$http.get
.mockResolvedValueOnce({})
.mockResolvedValueOnce({ type: 'steve', hash: 'a' })
.mockResolvedValueOnce({ type: 'cape', hash: 'b' })
.mockResolvedValueOnce({ data: {} })
.mockResolvedValueOnce({ data: { type: 'steve', hash: 'a' } })
.mockResolvedValueOnce({ data: { type: 'cape', hash: 'b' } })
const wrapper = mount<Vue & { skinUrl: string, capeUrl: string }>(Closet)
wrapper.setData({ skinItems: [{ tid: 1 }] })
@ -169,13 +175,15 @@ test('select texture', async () => {
test('apply texture', async () => {
Vue.prototype.$http.get
.mockResolvedValueOnce({})
.mockResolvedValueOnce([])
.mockResolvedValueOnce([
{
pid: 1, name: 'name', tid_skin: 10,
},
])
.mockResolvedValueOnce({ data: {} })
.mockResolvedValueOnce({ data: [] })
.mockResolvedValueOnce({
data: [
{
pid: 1, name: 'name', tid_skin: 10,
},
],
})
const wrapper = mount(Closet)
const button = wrapper.find(Previewer).findAll('button')
@ -193,7 +201,7 @@ test('apply texture', async () => {
})
test('reset selected texture', () => {
Vue.prototype.$http.get.mockResolvedValue({})
Vue.prototype.$http.get.mockResolvedValue({ data: {} })
const wrapper = mount(Closet)
wrapper.setData({
selectedSkin: 1,
@ -217,11 +225,13 @@ test('select specified texture initially', async () => {
}))
Vue.prototype.$http.get
.mockResolvedValueOnce({
items: [],
category: 'skin',
total_pages: 1,
data: {
items: [],
category: 'skin',
total_pages: 1,
},
})
.mockResolvedValueOnce({ type: 'cape', hash: '' })
.mockResolvedValueOnce({ data: { type: 'cape', hash: '' } })
.mockResolvedValueOnce([])
const wrapper = mount(Closet)
jest.runAllTimers()

View File

@ -26,18 +26,20 @@ window.blessing.extra = { unverified: false }
function scoreInfo(data = {}) {
return {
user: { score: 835, lastSignAt: '2018-08-07 16:06:49' },
stats: {
players: {
used: 3, total: 15, percentage: 20,
},
storage: {
used: 5, total: 20, percentage: 25,
data: {
user: { score: 835, lastSignAt: '2018-08-07 16:06:49' },
stats: {
players: {
used: 3, total: 15, percentage: 20,
},
storage: {
used: 5, total: 20, percentage: 25,
},
},
signAfterZero: false,
signGapTime: '24',
...data,
},
signAfterZero: false,
signGapTime: '24',
...data,
}
}
@ -140,11 +142,15 @@ test('sign', async () => {
user: { lastSignAt: Date.now() - 30 * 3600 * 1000 },
}))
Vue.prototype.$http.post
.mockResolvedValueOnce({ code: 1, message: '1' })
.mockResolvedValueOnce({
code: 1, message: '1', data: {},
})
.mockResolvedValueOnce({
code: 0,
score: 233,
storage: { used: 3, total: 4 },
data: {
score: 233,
storage: { used: 3, total: 4 },
},
})
const wrapper = mount(Dashboard)
const button = wrapper.find(Button)

View File

@ -11,6 +11,7 @@ window.blessing.extra = {
}
test('display player name constraints', () => {
Vue.prototype.$http.get.mockResolvedValue({ data: [] })
const wrapper = mount(Players)
const text = wrapper.text()
expect(text).toContain('rule')
@ -18,31 +19,33 @@ test('display player name constraints', () => {
})
test('fetch players data before mount', () => {
Vue.prototype.$http.get.mockResolvedValue([])
Vue.prototype.$http.get.mockResolvedValue({ data: [] })
mount(Players)
expect(Vue.prototype.$http.get).toBeCalledWith('/user/player/list')
})
test('click to preview player', async () => {
Vue.prototype.$http.get
.mockResolvedValueOnce([
{
pid: 1, tid_skin: 1, tid_cape: 3,
},
{
pid: 2, tid_skin: 0, tid_cape: 0,
},
{
pid: 3, tid_skin: 2, tid_cape: 0,
},
{
pid: 4, tid_skin: 0, tid_cape: 5,
},
])
.mockResolvedValueOnce({ hash: 'a' })
.mockResolvedValueOnce({ hash: 'b' })
.mockResolvedValueOnce({ hash: 'c' })
.mockResolvedValueOnce({ hash: 'd' })
.mockResolvedValueOnce({
data: [
{
pid: 1, tid_skin: 1, tid_cape: 3,
},
{
pid: 2, tid_skin: 0, tid_cape: 0,
},
{
pid: 3, tid_skin: 2, tid_cape: 0,
},
{
pid: 4, tid_skin: 0, tid_cape: 5,
},
],
})
.mockResolvedValueOnce({ data: { hash: 'a' } })
.mockResolvedValueOnce({ data: { hash: 'b' } })
.mockResolvedValueOnce({ data: { hash: 'c' } })
.mockResolvedValueOnce({ data: { hash: 'd' } })
const wrapper = mount(Players)
await wrapper.vm.$nextTick()
@ -69,9 +72,11 @@ test('click to preview player', async () => {
test('change player name', async () => {
Vue.prototype.$http.get
.mockResolvedValueOnce([
{ pid: 1, name: 'old' },
])
.mockResolvedValue({
data: [
{ pid: 1, name: 'old' },
],
})
Vue.prototype.$http.post
.mockResolvedValueOnce({ code: 1 })
.mockResolvedValue({ code: 0 })
@ -103,9 +108,11 @@ test('change player name', async () => {
test('delete player', async () => {
Vue.prototype.$http.get
.mockResolvedValueOnce([
{ pid: 1, name: 'to-be-deleted' },
])
.mockResolvedValueOnce({
data: [
{ pid: 1, name: 'to-be-deleted' },
],
})
Vue.prototype.$http.post
.mockResolvedValueOnce({ code: 1 })
.mockResolvedValue({ code: 0 })
@ -128,7 +135,7 @@ test('delete player', async () => {
})
test('toggle preview mode', () => {
Vue.prototype.$http.get.mockResolvedValueOnce([])
Vue.prototype.$http.get.mockResolvedValueOnce({ data: [] })
const wrapper = mount(Players)
wrapper.find('[data-test="to2d"]').trigger('click')
expect(wrapper.text()).toContain('user.player.texture-empty')
@ -136,7 +143,7 @@ test('toggle preview mode', () => {
test('add player', async () => {
window.$ = jest.fn(() => ({ modal() {} }))
Vue.prototype.$http.get.mockResolvedValueOnce([])
Vue.prototype.$http.get.mockResolvedValueOnce({ data: [] })
Vue.prototype.$http.post.mockResolvedValue({ code: 0 })
const wrapper = mount(Players)
const button = wrapper.find('[data-test=addPlayer]')
@ -149,11 +156,13 @@ test('add player', async () => {
test('clear texture', async () => {
window.$ = jest.fn(() => ({ modal() {} }))
Vue.prototype.$http.get.mockResolvedValueOnce([
{
pid: 1, tid_skin: 1, tid_cape: 0,
},
])
Vue.prototype.$http.get.mockResolvedValueOnce({
data: [
{
pid: 1, tid_skin: 1, tid_cape: 0,
},
],
})
Vue.prototype.$http.post
.mockResolvedValueOnce({ code: 1 })
.mockResolvedValue({ code: 0, message: 'ok' })

View File

@ -65,7 +65,6 @@ Route::group([
Route::any('', 'PlayerController@index');
Route::get('/list', 'PlayerController@listAll');
Route::post('/add', 'PlayerController@add');
Route::any('/show', 'PlayerController@show');
Route::post('/set', 'PlayerController@setTexture');
Route::post('/texture/clear', 'PlayerController@clearTexture');
Route::post('/rename', 'PlayerController@rename');

View File

@ -688,6 +688,7 @@ class AdminControllerTest extends BrowserKitTestCase
$this->postJson('/admin/players', [
'pid' => $player->pid,
'action' => 'name',
'name' => 'abc',
])->seeJson(['code' => 0]);
$player->refresh();
$this->assertEquals('abc', $player->user->nickname);

View File

@ -81,7 +81,7 @@ class AuthControllerTest extends TestCase
[
'code' => 1,
'message' => trans('auth.validation.password'),
'login_fails' => 1,
'data' => ['login_fails' => 1],
]
);
$this->assertTrue(Cache::has($loginFailsCacheKey));

View File

@ -37,24 +37,26 @@ class ClosetControllerTest extends TestCase
// Use default query parameters
$this->getJson('/user/closet-data')
->assertJsonStructure([
'category',
'total_pages',
'items' => [['tid', 'name', 'type']],
'data' => [
'category',
'total_pages',
'items' => [['tid', 'name', 'type']],
]
]);
// Responsive
$result = $this->json('get', '/user/closet-data?perPage=0')->json();
$result = $this->json('get', '/user/closet-data?perPage=0')->json()['data'];
$this->assertCount(6, $result['items']);
$result = $this->json('get', '/user/closet-data?perPage=8')->json();
$result = $this->json('get', '/user/closet-data?perPage=8')->json()['data'];
$this->assertCount(8, $result['items']);
$result = $this->json('get', '/user/closet-data?perPage=8&page=2')->json();
$result = $this->json('get', '/user/closet-data?perPage=8&page=2')->json()['data'];
$this->assertCount(2, $result['items']);
// Get capes
$cape = factory(Texture::class, 'cape')->create();
$this->user->closet()->attach($cape->tid, ['item_name' => 'custom_name']);
$this->getJson('/user/closet-data?category=cape')
->assertJson([
->assertJson(['data' => [
'category' => 'cape',
'total_pages' => 1,
'items' => [[
@ -62,12 +64,12 @@ class ClosetControllerTest extends TestCase
'name' => 'custom_name',
'type' => 'cape',
]],
]);
]]);
// Search by keyword
$random = $textures->random();
$this->getJson('/user/closet-data?q='.$random->name)
->assertJson([
->assertJson(['data' => [
'category' => 'skin',
'total_pages' => 1,
'items' => [[
@ -75,7 +77,7 @@ class ClosetControllerTest extends TestCase
'name' => $random->name,
'type' => $random->type,
]],
]);
]]);
}
public function testAdd()

View File

@ -30,12 +30,12 @@ class PlayerControllerTest extends TestCase
$player = factory(Player::class)->create(['uid' => $user->uid]);
$this->actingAs($user)
->get('/user/player/list')
->assertJson([
->assertJson(['data' => [
[
'pid' => $player->pid,
'name' => $player->name,
],
]);
]]);
}
public function testAdd()
@ -153,13 +153,6 @@ class PlayerControllerTest extends TestCase
$this->assertNotNull(Player::find($player->pid));
}
public function testShow()
{
$player = factory(Player::class)->create(['last_modified' => '2017-11-11 22:51:00']);
$this->get('/user/player/show?pid='.$player->pid)
->assertJson($player->toArray());
}
public function testRename()
{
Event::fake();

View File

@ -74,15 +74,17 @@ class PluginControllerTest extends TestCase
])->assertJson([
'code' => 1,
'message' => trans('admin.plugins.operations.unsatisfied.notice'),
'reason' => [
trans('admin.plugins.operations.unsatisfied.version', [
'name' => 'fake-plugin-with-config-view',
'constraint' => '^6.6.6',
]),
trans('admin.plugins.operations.unsatisfied.disabled', [
'name' => 'whatever',
]),
],
'data' => [
'reason' => [
trans('admin.plugins.operations.unsatisfied.version', [
'name' => 'fake-plugin-with-config-view',
'constraint' => '^6.6.6',
]),
trans('admin.plugins.operations.unsatisfied.disabled', [
'name' => 'whatever',
]),
],
]
]);
// Enable a plugin

View File

@ -159,7 +159,7 @@ class ReportControllerTest extends TestCase
->assertJson([
'code' => 0,
'message' => trans('general.op-success'),
'status' => Report::REJECTED,
'data' => ['status' => Report::REJECTED],
]);
$report->refresh();
$reporter->refresh();
@ -186,7 +186,7 @@ class ReportControllerTest extends TestCase
->assertJson([
'code' => 0,
'message' => trans('general.op-success'),
'status' => Report::RESOLVED,
'data' => ['status' => Report::RESOLVED],
]);
$report->refresh();
$reporter->refresh();
@ -207,7 +207,7 @@ class ReportControllerTest extends TestCase
->assertJson([
'code' => 0,
'message' => trans('general.op-success'),
'status' => Report::RESOLVED,
'data' => ['status' => Report::RESOLVED],
]);
$reporter->refresh();
$this->assertEquals(User::BANNED, $uploader->permission);

View File

@ -22,11 +22,11 @@ class SkinlibControllerTest extends TestCase
public function testGetSkinlibFiltered()
{
$this->getJson('/skinlib/data')
->assertJson([
->assertJson(['data' => [
'items' => [],
'current_uid' => 0,
'total_pages' => 0,
]);
]]);
$steves = factory(Texture::class)->times(5)->create();
$alexs = factory(Texture::class, 'alex')->times(5)->create();
@ -35,11 +35,11 @@ class SkinlibControllerTest extends TestCase
// Default arguments
$items = $this->getJson('/skinlib/data')
->assertJson([
->assertJson(['data' => [
'current_uid' => 0,
'total_pages' => 1,
])
->decodeResponseJson('items');
]])
->decodeResponseJson('data')['items'];
$this->assertCount(10, $items);
$this->assertTrue(collect($items)->every(function ($item) {
return $item['type'] == 'steve' || $item['type'] == 'alex';
@ -47,11 +47,11 @@ class SkinlibControllerTest extends TestCase
// Only steve
$items = $this->getJson('/skinlib/data?filter=steve')
->assertJson([
->assertJson(['data' => [
'current_uid' => 0,
'total_pages' => 1,
])
->decodeResponseJson('items');
]])
->decodeResponseJson('data')['items'];
$this->assertCount(5, $items);
$this->assertTrue(collect($items)->every(function ($item) {
return $item['type'] == 'steve';
@ -59,19 +59,19 @@ class SkinlibControllerTest extends TestCase
// Invalid type
$this->getJson('/skinlib/data?filter=what')
->assertJson([
->assertJson(['data' => [
'items' => [],
'current_uid' => 0,
'total_pages' => 0,
]);
]]);
// Only capes
$items = $this->getJson('/skinlib/data?filter=cape')
->assertJson([
->assertJson(['data' => [
'current_uid' => 0,
'total_pages' => 1,
])
->decodeResponseJson('items');
]])
->decodeResponseJson('data')['items'];
$this->assertCount(5, $items);
$this->assertTrue(collect($items)->every(function ($item) {
return $item['type'] == 'cape';
@ -84,11 +84,11 @@ class SkinlibControllerTest extends TestCase
return $texture->uploader == $uid;
});
$items = $this->getJson('/skinlib/data?uploader='.$uid)
->assertJson([
->assertJson(['data' => [
'current_uid' => 0,
'total_pages' => 1,
])
->decodeResponseJson('items');
]])
->decodeResponseJson('data')['items'];
$this->assertCount($owned->count(), $items);
$this->assertTrue(collect($items)->every(function ($item) use ($uid) {
return $item['uploader'] == $uid;
@ -99,11 +99,11 @@ class SkinlibControllerTest extends TestCase
return $skin->tid;
})->values();
$items = $this->getJson('/skinlib/data?sort=tid')
->assertJson([
->assertJson(['data' => [
'current_uid' => 0,
'total_pages' => 1,
])
->decodeResponseJson('items');
]])
->decodeResponseJson('data')['items'];
$items = array_map(function ($item) {
return $item['tid'];
}, $items);
@ -113,11 +113,11 @@ class SkinlibControllerTest extends TestCase
$user = factory(User::class)->create();
$user->closet()->attach($skins->random()->tid, ['item_name' => 'name']);
$items = $this->getJson('/skinlib/data?sort=likes')
->assertJson([
->assertJson(['data' => [
'current_uid' => 0,
'total_pages' => 1,
])
->decodeResponseJson('items');
]])
->decodeResponseJson('data')['items'];
$this->assertEquals(1, $items[0]['likes']);
$this->assertEquals(0, $items[1]['likes']);
@ -129,11 +129,11 @@ class SkinlibControllerTest extends TestCase
Str::contains($texture->name, strtolower($keyword));
});
$items = $this->getJson('/skinlib/data?keyword='.$keyword)
->assertJson([
->assertJson(['data' => [
'current_uid' => 0,
'total_pages' => 1,
])
->decodeResponseJson('items');
]])
->decodeResponseJson('data')['items'];
$this->assertCount($keyworded->count(), $items);
// More than one argument
@ -149,11 +149,11 @@ class SkinlibControllerTest extends TestCase
})
->values();
$items = $this->getJson('/skinlib/data?sort=size&keyword='.$keyword)
->assertJson([
->assertJson(['data' => [
'current_uid' => 0,
'total_pages' => 1,
])
->decodeResponseJson('items');
]])
->decodeResponseJson('data')['items'];
$items = array_map(function ($item) {
return $item['tid'];
}, $items);
@ -167,47 +167,47 @@ class SkinlibControllerTest extends TestCase
->merge($steves);
$skins = $steves->merge($alexs);
$items = $this->getJson('/skinlib/data')
->assertJson([
->assertJson(['data' => [
'current_uid' => 0,
'total_pages' => 2,
])
->decodeResponseJson('items');
]])
->decodeResponseJson('data')['items'];
$this->assertCount(20, $items);
$items = $this->getJson('/skinlib/data?page=-5')
->assertJson([
->assertJson(['data' => [
'current_uid' => 0,
'total_pages' => 2,
])
->decodeResponseJson('items');
]])
->decodeResponseJson('data')['items'];
$this->assertCount(20, $items);
$page2Count = $skins->forPage(2, 20)->count();
$items = $this->getJson('/skinlib/data?page=2')
->assertJson([
->assertJson(['data' => [
'current_uid' => 0,
'total_pages' => 2,
])
->decodeResponseJson('items');
]])
->decodeResponseJson('data')['items'];
$this->assertCount(5, $items);
$this->getJson('/skinlib/data?page=8')
->assertJson([
->assertJson(['data' => [
'items' => [],
'current_uid' => 0,
'total_pages' => 2,
]);
]]);
$items = $this->getJson('/skinlib/data?items_per_page=-6&page=2')
->assertJson([
->assertJson(['data' => [
'current_uid' => 0,
'total_pages' => 2,
])
->decodeResponseJson('items');
]])
->decodeResponseJson('data')['items'];
$this->assertCount($page2Count, $items);
$page3Count = $skins->forPage(3, 8)->count();
$items = $this->getJson('/skinlib/data?page=3&items_per_page=8')
->assertJson([
->assertJson(['data' => [
'current_uid' => 0,
'total_pages' => 4,
])
->decodeResponseJson('items');
]])
->decodeResponseJson('data')['items'];
$this->assertCount($page3Count, $items);
// Add some private textures
@ -219,11 +219,11 @@ class SkinlibControllerTest extends TestCase
// If not logged in, private textures should not be shown
$items = $this->getJson('/skinlib/data')
->assertJson([
->assertJson(['data' => [
'current_uid' => 0,
'total_pages' => 2,
])
->decodeResponseJson('items');
]])
->decodeResponseJson('data')['items'];
$this->assertTrue(collect($items)->every(function ($item) {
return $item['public'] == true;
}));
@ -231,11 +231,11 @@ class SkinlibControllerTest extends TestCase
// Other users should not see someone's private textures
$items = $this->actingAs($otherUser)
->getJson('/skinlib/data')
->assertJson([
->assertJson(['data' => [
'current_uid' => $otherUser->uid,
'total_pages' => 2,
])
->decodeResponseJson('items');
]])
->decodeResponseJson('data')['items'];
$this->assertTrue(collect($items)->every(function ($item) {
return ! $item['liked'];
}));
@ -244,22 +244,22 @@ class SkinlibControllerTest extends TestCase
$texture = $skins->sortByDesc('upload_at')->values()->first();
$otherUser->closet()->attach($texture->tid, ['item_name' => $texture->name]);
$this->getJson('/skinlib/data')
->assertJson([
->assertJson(['data' => [
'items' => [
['tid' => $texture->tid, 'liked' => true],
],
'current_uid' => $otherUser->uid,
'total_pages' => 2,
]);
]]);
// Uploader can see his private textures
$items = $this->actingAs($uploader)
->getJson('/skinlib/data')
->assertJson([
->assertJson(['data' => [
'current_uid' => $uploader->uid,
'total_pages' => 2,
])
->decodeResponseJson('items');
]])
->decodeResponseJson('data')['items'];
$this->assertTrue(collect($items)->contains(function ($item) {
return $item['public'] == false;
}));
@ -268,11 +268,11 @@ class SkinlibControllerTest extends TestCase
$admin = factory(User::class, 'admin')->create();
$items = $this->actingAs($admin)
->getJson('/skinlib/data')
->assertJson([
->assertJson(['data' => [
'current_uid' => $admin->uid,
'total_pages' => 2,
])
->decodeResponseJson('items');
]])
->decodeResponseJson('data')['items'];
$this->assertTrue(collect($items)->contains(function ($item) {
return $item['public'] == false;
}));
@ -335,11 +335,11 @@ class SkinlibControllerTest extends TestCase
public function testInfo()
{
// Non-existed texture
$this->get('/skinlib/info/1')->assertJson([]);
$this->get('/skinlib/info/1')->assertNotFound();
$texture = factory(Texture::class)->create();
$this->get('/skinlib/info/'.$texture->tid)
->assertJson([
->assertJson(['data' => [
'tid' => $texture->tid,
'name' => $texture->name,
'type' => $texture->type,
@ -349,7 +349,7 @@ class SkinlibControllerTest extends TestCase
'uploader' => $texture->uploader,
'public' => $texture->public,
'upload_at' => $texture->upload_at->format('Y-m-d H:i:s'),
]);
]]);
}
public function testUpload()
@ -555,7 +555,7 @@ class SkinlibControllerTest extends TestCase
$response->assertJson([
'code' => 0,
'message' => trans('skinlib.upload.success', ['name' => 'texture']),
'tid' => $t->tid,
'data' => ['tid' => $t->tid],
]);
Storage::disk('textures')->assertExists($t->hash);
$user = User::find($user->uid);
@ -581,7 +581,7 @@ class SkinlibControllerTest extends TestCase
)->assertJson([
'code' => 0,
'message' => trans('skinlib.upload.repeated'),
'tid' => $t->tid,
'data' => ['tid' => $t->tid],
]);
unlink(storage_path('framework/testing/disks/textures/'.$t->hash));
@ -744,7 +744,6 @@ class SkinlibControllerTest extends TestCase
->assertJson([
'code' => 0,
'message' => trans('skinlib.privacy.success', ['privacy' => trans('general.private')]),
'public' => false,
]);
$this->assertEquals(0, Texture::find($texture->tid)->public);
@ -769,7 +768,6 @@ class SkinlibControllerTest extends TestCase
->assertJson([
'code' => 0,
'message' => trans('skinlib.privacy.success', ['privacy' => trans('general.private')]),
'public' => false,
]);
$this->assertEquals(0, User::find($uploader->uid)->score);
@ -785,7 +783,6 @@ class SkinlibControllerTest extends TestCase
->assertJson([
'code' => 0,
'message' => trans('skinlib.privacy.success', ['privacy' => trans('general.private')]),
'public' => false,
]);
$this->assertEquals(0, Player::find($player->pid)->tid_skin);
$this->assertEquals(0, $other->closet()->count());
@ -812,10 +809,7 @@ class SkinlibControllerTest extends TestCase
$other = factory(User::class)->create();
$other->closet()->attach($texture->tid, ['item_name' => 'a']);
$this->postJson('/skinlib/privacy', ['tid' => $texture->tid])
->assertJson([
'code' => 0,
'public' => false,
]);
->assertJson(['code' => 0]);
$this->assertEquals($other->score, User::find($other->uid)->score);
}

View File

@ -38,7 +38,7 @@ class UserControllerTest extends TestCase
$this->actingAs($user)
->get('/user/score-info')
->assertJson([
->assertJson(['data' => [
'user' => [
'score' => $user->score,
'lastSignAt' => $user->last_sign_at,
@ -57,7 +57,7 @@ class UserControllerTest extends TestCase
],
'signAfterZero' => option('sign_after_zero'),
'signGapTime' => option('sign_gap_time'),
]);
]]);
}
public function testSign()
@ -71,13 +71,15 @@ class UserControllerTest extends TestCase
->assertJson([
'code' => 0,
'message' => trans('user.sign-success', ['score' => 50]),
'score' => option('user_initial_score') + 50,
'storage' => [
'percentage' => 0,
'total' => option('user_initial_score') + 50,
'used' => 0,
],
'remaining_time' => (int) option('sign_gap_time'),
'data' => [
'score' => option('user_initial_score') + 50,
'storage' => [
'percentage' => 0,
'total' => option('user_initial_score') + 50,
'used' => 0,
],
'remaining_time' => (int) option('sign_gap_time'),
]
]);
// Remaining time is greater than 0
@ -123,10 +125,7 @@ class UserControllerTest extends TestCase
$user = factory(User::class)->create([
'last_sign_at' => \Carbon\Carbon::today()->toDateTimeString(),
]);
$this->actingAs($user)->postJson('/user/sign')
->assertJson([
'code' => 0,
]);
$this->actingAs($user)->postJson('/user/sign')->assertJson(['code' => 0]);
}
public function testSendVerificationEmail()