From 91b6631984fa7097bd60aa013d1cf041d7b95f58 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 29 May 2019 15:09:38 +0200 Subject: [PATCH] Add pagination to account video channels endpoint --- server/controllers/api/accounts.ts | 16 ++++- server/models/video/video-channel.ts | 13 +++- .../tests/api/check-params/video-channels.ts | 24 ++++++- server/tests/api/videos/video-channels.ts | 64 ++++++++++++++++++- shared/extra-utils/videos/video-channels.ts | 28 ++++++-- 5 files changed, 129 insertions(+), 16 deletions(-) diff --git a/server/controllers/api/accounts.ts b/server/controllers/api/accounts.ts index 8d4db1e75..9b3489120 100644 --- a/server/controllers/api/accounts.ts +++ b/server/controllers/api/accounts.ts @@ -16,7 +16,8 @@ import { accountNameWithHostGetValidator, accountsSortValidator, ensureAuthUserOwnsAccountValidator, - videosSortValidator + videosSortValidator, + videoChannelsSortValidator } from '../../middlewares/validators' import { AccountModel } from '../../models/account/account' import { AccountVideoRateModel } from '../../models/account/account-video-rate' @@ -56,6 +57,10 @@ accountsRouter.get('/:accountName/videos', accountsRouter.get('/:accountName/video-channels', asyncMiddleware(accountNameWithHostGetValidator), + paginationValidator, + videoChannelsSortValidator, + setDefaultSort, + setDefaultPagination, asyncMiddleware(listAccountChannels) ) @@ -108,7 +113,14 @@ async function listAccounts (req: express.Request, res: express.Response) { } async function listAccountChannels (req: express.Request, res: express.Response) { - const resultList = await VideoChannelModel.listByAccount(res.locals.account.id) + const options = { + accountId: res.locals.account.id, + start: req.query.start, + count: req.query.count, + sort: req.query.sort, + } + + const resultList = await VideoChannelModel.listByAccount(options) return res.json(getFormattedObjects(resultList.data, resultList.total)) } diff --git a/server/models/video/video-channel.ts b/server/models/video/video-channel.ts index fb70e6625..d73be18d6 100644 --- a/server/models/video/video-channel.ts +++ b/server/models/video/video-channel.ts @@ -334,14 +334,21 @@ export class VideoChannelModel extends Model { }) } - static listByAccount (accountId: number) { + static listByAccount (options: { + accountId: number, + start: number, + count: number, + sort: string + }) { const query = { - order: getSort('createdAt'), + offset: options.start, + limit: options.count, + order: getSort(options.sort), include: [ { model: AccountModel, where: { - id: accountId + id: options.accountId }, required: true } diff --git a/server/tests/api/check-params/video-channels.ts b/server/tests/api/check-params/video-channels.ts index 65bc20613..ff04f6b03 100644 --- a/server/tests/api/check-params/video-channels.ts +++ b/server/tests/api/check-params/video-channels.ts @@ -67,8 +67,30 @@ describe('Test video channels API validator', function () { }) describe('When listing account video channels', function () { + const accountChannelPath = '/api/v1/accounts/fake/video-channels' + + it('Should fail with a bad start pagination', async function () { + await checkBadStartPagination(server.url, accountChannelPath, server.accessToken) + }) + + it('Should fail with a bad count pagination', async function () { + await checkBadCountPagination(server.url, accountChannelPath, server.accessToken) + }) + + it('Should fail with an incorrect sort', async function () { + await checkBadSortPagination(server.url, accountChannelPath, server.accessToken) + }) + it('Should fail with a unknown account', async function () { - await getAccountVideoChannelsList(server.url, 'unknown', 404) + await getAccountVideoChannelsList({ url: server.url, accountName: 'unknown', specialStatus: 404 }) + }) + + it('Should succeed with the correct parameters', async function () { + await makeGetRequest({ + url: server.url, + path: accountChannelPath, + statusCodeExpected: 200 + }) }) }) diff --git a/server/tests/api/videos/video-channels.ts b/server/tests/api/videos/video-channels.ts index 41fe3be5c..e98f14ea8 100644 --- a/server/tests/api/videos/video-channels.ts +++ b/server/tests/api/videos/video-channels.ts @@ -2,7 +2,7 @@ import * as chai from 'chai' import 'mocha' -import { User, Video } from '../../../../shared/index' +import { User, Video, VideoChannel } from '../../../../shared/index' import { cleanupTests, createUser, @@ -108,7 +108,11 @@ describe('Test video channels', function () { }) it('Should have two video channels when getting account channels on server 1', async function () { - const res = await getAccountVideoChannelsList(servers[0].url, userInfo.account.name + '@' + userInfo.account.host) + const res = await getAccountVideoChannelsList({ + url: servers[ 0 ].url, + accountName: userInfo.account.name + '@' + userInfo.account.host + }) + expect(res.body.total).to.equal(2) expect(res.body.data).to.be.an('array') expect(res.body.data).to.have.lengthOf(2) @@ -123,8 +127,62 @@ describe('Test video channels', function () { expect(videoChannels[1].support).to.equal('super video channel support text') }) + it('Should paginate and sort account channels', async function () { + { + const res = await getAccountVideoChannelsList({ + url: servers[ 0 ].url, + accountName: userInfo.account.name + '@' + userInfo.account.host, + start: 0, + count: 1, + sort: 'createdAt' + }) + + expect(res.body.total).to.equal(2) + expect(res.body.data).to.have.lengthOf(1) + + const videoChannel: VideoChannel = res.body.data[ 0 ] + expect(videoChannel.name).to.equal('root_channel') + } + + { + const res = await getAccountVideoChannelsList({ + url: servers[ 0 ].url, + accountName: userInfo.account.name + '@' + userInfo.account.host, + start: 0, + count: 1, + sort: '-createdAt' + }) + + expect(res.body.total).to.equal(2) + expect(res.body.data).to.have.lengthOf(1) + + const videoChannel: VideoChannel = res.body.data[ 0 ] + expect(videoChannel.name).to.equal('second_video_channel') + } + + { + const res = await getAccountVideoChannelsList({ + url: servers[ 0 ].url, + accountName: userInfo.account.name + '@' + userInfo.account.host, + start: 1, + count: 1, + sort: '-createdAt' + }) + + expect(res.body.total).to.equal(2) + expect(res.body.data).to.have.lengthOf(1) + + const videoChannel: VideoChannel = res.body.data[ 0 ] + expect(videoChannel.name).to.equal('root_channel') + } + }) + it('Should have one video channel when getting account channels on server 2', async function () { - const res = await getAccountVideoChannelsList(servers[1].url, userInfo.account.name + '@' + userInfo.account.host) + const res = await getAccountVideoChannelsList({ + url: servers[ 1 ].url, + accountName: userInfo.account.name + '@' + userInfo.account.host + }) + expect(res.body.total).to.equal(1) expect(res.body.data).to.be.an('array') expect(res.body.data).to.have.lengthOf(1) diff --git a/shared/extra-utils/videos/video-channels.ts b/shared/extra-utils/videos/video-channels.ts index 93a257bf9..b4755b486 100644 --- a/shared/extra-utils/videos/video-channels.ts +++ b/shared/extra-utils/videos/video-channels.ts @@ -1,6 +1,6 @@ import * as request from 'supertest' import { VideoChannelCreate, VideoChannelUpdate } from '../../models/videos' -import { updateAvatarRequest } from '../requests/requests' +import { makeGetRequest, updateAvatarRequest } from '../requests/requests' import { getMyUserInformation, ServerInfo } from '..' import { User } from '../..' @@ -19,14 +19,28 @@ function getVideoChannelsList (url: string, start: number, count: number, sort?: .expect('Content-Type', /json/) } -function getAccountVideoChannelsList (url: string, accountName: string, specialStatus = 200) { +function getAccountVideoChannelsList (parameters: { + url: string, + accountName: string, + start?: number, + count?: number, + sort?: string, + specialStatus?: number +}) { + const { url, accountName, start, count, sort = 'createdAt', specialStatus = 200 } = parameters + const path = '/api/v1/accounts/' + accountName + '/video-channels' - return request(url) - .get(path) - .set('Accept', 'application/json') - .expect(specialStatus) - .expect('Content-Type', /json/) + return makeGetRequest({ + url, + path, + query: { + start, + count, + sort + }, + statusCodeExpected: specialStatus + }) } function addVideoChannel ( -- 2.25.1