1
0
Fork 0

Add pagination to account video channels endpoint

This commit is contained in:
Chocobozzz 2019-05-29 15:09:38 +02:00
parent b247a13270
commit 91b6631984
No known key found for this signature in database
GPG key ID: 583A612D890159BE
5 changed files with 129 additions and 16 deletions

View file

@ -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))
}

View file

@ -334,14 +334,21 @@ export class VideoChannelModel extends Model<VideoChannelModel> {
})
}
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
}

View file

@ -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
})
})
})

View file

@ -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)

View file

@ -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 (