aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2019-05-29 15:09:38 +0200
committerChocobozzz <me@florianbigard.com>2019-05-29 15:09:38 +0200
commit91b6631984fa7097bd60aa013d1cf041d7b95f58 (patch)
tree717cc94c3cba7b02ba404f2536ebc76118adf504
parentb247a132709eb212fef4f77c4912dc0ec108f36b (diff)
downloadPeerTube-91b6631984fa7097bd60aa013d1cf041d7b95f58.tar.gz
PeerTube-91b6631984fa7097bd60aa013d1cf041d7b95f58.tar.zst
PeerTube-91b6631984fa7097bd60aa013d1cf041d7b95f58.zip
Add pagination to account video channels endpoint
-rw-r--r--server/controllers/api/accounts.ts16
-rw-r--r--server/models/video/video-channel.ts13
-rw-r--r--server/tests/api/check-params/video-channels.ts24
-rw-r--r--server/tests/api/videos/video-channels.ts64
-rw-r--r--shared/extra-utils/videos/video-channels.ts28
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 {
16 accountNameWithHostGetValidator, 16 accountNameWithHostGetValidator,
17 accountsSortValidator, 17 accountsSortValidator,
18 ensureAuthUserOwnsAccountValidator, 18 ensureAuthUserOwnsAccountValidator,
19 videosSortValidator 19 videosSortValidator,
20 videoChannelsSortValidator
20} from '../../middlewares/validators' 21} from '../../middlewares/validators'
21import { AccountModel } from '../../models/account/account' 22import { AccountModel } from '../../models/account/account'
22import { AccountVideoRateModel } from '../../models/account/account-video-rate' 23import { AccountVideoRateModel } from '../../models/account/account-video-rate'
@@ -56,6 +57,10 @@ accountsRouter.get('/:accountName/videos',
56 57
57accountsRouter.get('/:accountName/video-channels', 58accountsRouter.get('/:accountName/video-channels',
58 asyncMiddleware(accountNameWithHostGetValidator), 59 asyncMiddleware(accountNameWithHostGetValidator),
60 paginationValidator,
61 videoChannelsSortValidator,
62 setDefaultSort,
63 setDefaultPagination,
59 asyncMiddleware(listAccountChannels) 64 asyncMiddleware(listAccountChannels)
60) 65)
61 66
@@ -108,7 +113,14 @@ async function listAccounts (req: express.Request, res: express.Response) {
108} 113}
109 114
110async function listAccountChannels (req: express.Request, res: express.Response) { 115async function listAccountChannels (req: express.Request, res: express.Response) {
111 const resultList = await VideoChannelModel.listByAccount(res.locals.account.id) 116 const options = {
117 accountId: res.locals.account.id,
118 start: req.query.start,
119 count: req.query.count,
120 sort: req.query.sort,
121 }
122
123 const resultList = await VideoChannelModel.listByAccount(options)
112 124
113 return res.json(getFormattedObjects(resultList.data, resultList.total)) 125 return res.json(getFormattedObjects(resultList.data, resultList.total))
114} 126}
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<VideoChannelModel> {
334 }) 334 })
335 } 335 }
336 336
337 static listByAccount (accountId: number) { 337 static listByAccount (options: {
338 accountId: number,
339 start: number,
340 count: number,
341 sort: string
342 }) {
338 const query = { 343 const query = {
339 order: getSort('createdAt'), 344 offset: options.start,
345 limit: options.count,
346 order: getSort(options.sort),
340 include: [ 347 include: [
341 { 348 {
342 model: AccountModel, 349 model: AccountModel,
343 where: { 350 where: {
344 id: accountId 351 id: options.accountId
345 }, 352 },
346 required: true 353 required: true
347 } 354 }
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 () {
67 }) 67 })
68 68
69 describe('When listing account video channels', function () { 69 describe('When listing account video channels', function () {
70 const accountChannelPath = '/api/v1/accounts/fake/video-channels'
71
72 it('Should fail with a bad start pagination', async function () {
73 await checkBadStartPagination(server.url, accountChannelPath, server.accessToken)
74 })
75
76 it('Should fail with a bad count pagination', async function () {
77 await checkBadCountPagination(server.url, accountChannelPath, server.accessToken)
78 })
79
80 it('Should fail with an incorrect sort', async function () {
81 await checkBadSortPagination(server.url, accountChannelPath, server.accessToken)
82 })
83
70 it('Should fail with a unknown account', async function () { 84 it('Should fail with a unknown account', async function () {
71 await getAccountVideoChannelsList(server.url, 'unknown', 404) 85 await getAccountVideoChannelsList({ url: server.url, accountName: 'unknown', specialStatus: 404 })
86 })
87
88 it('Should succeed with the correct parameters', async function () {
89 await makeGetRequest({
90 url: server.url,
91 path: accountChannelPath,
92 statusCodeExpected: 200
93 })
72 }) 94 })
73 }) 95 })
74 96
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 @@
2 2
3import * as chai from 'chai' 3import * as chai from 'chai'
4import 'mocha' 4import 'mocha'
5import { User, Video } from '../../../../shared/index' 5import { User, Video, VideoChannel } from '../../../../shared/index'
6import { 6import {
7 cleanupTests, 7 cleanupTests,
8 createUser, 8 createUser,
@@ -108,7 +108,11 @@ describe('Test video channels', function () {
108 }) 108 })
109 109
110 it('Should have two video channels when getting account channels on server 1', async function () { 110 it('Should have two video channels when getting account channels on server 1', async function () {
111 const res = await getAccountVideoChannelsList(servers[0].url, userInfo.account.name + '@' + userInfo.account.host) 111 const res = await getAccountVideoChannelsList({
112 url: servers[ 0 ].url,
113 accountName: userInfo.account.name + '@' + userInfo.account.host
114 })
115
112 expect(res.body.total).to.equal(2) 116 expect(res.body.total).to.equal(2)
113 expect(res.body.data).to.be.an('array') 117 expect(res.body.data).to.be.an('array')
114 expect(res.body.data).to.have.lengthOf(2) 118 expect(res.body.data).to.have.lengthOf(2)
@@ -123,8 +127,62 @@ describe('Test video channels', function () {
123 expect(videoChannels[1].support).to.equal('super video channel support text') 127 expect(videoChannels[1].support).to.equal('super video channel support text')
124 }) 128 })
125 129
130 it('Should paginate and sort account channels', async function () {
131 {
132 const res = await getAccountVideoChannelsList({
133 url: servers[ 0 ].url,
134 accountName: userInfo.account.name + '@' + userInfo.account.host,
135 start: 0,
136 count: 1,
137 sort: 'createdAt'
138 })
139
140 expect(res.body.total).to.equal(2)
141 expect(res.body.data).to.have.lengthOf(1)
142
143 const videoChannel: VideoChannel = res.body.data[ 0 ]
144 expect(videoChannel.name).to.equal('root_channel')
145 }
146
147 {
148 const res = await getAccountVideoChannelsList({
149 url: servers[ 0 ].url,
150 accountName: userInfo.account.name + '@' + userInfo.account.host,
151 start: 0,
152 count: 1,
153 sort: '-createdAt'
154 })
155
156 expect(res.body.total).to.equal(2)
157 expect(res.body.data).to.have.lengthOf(1)
158
159 const videoChannel: VideoChannel = res.body.data[ 0 ]
160 expect(videoChannel.name).to.equal('second_video_channel')
161 }
162
163 {
164 const res = await getAccountVideoChannelsList({
165 url: servers[ 0 ].url,
166 accountName: userInfo.account.name + '@' + userInfo.account.host,
167 start: 1,
168 count: 1,
169 sort: '-createdAt'
170 })
171
172 expect(res.body.total).to.equal(2)
173 expect(res.body.data).to.have.lengthOf(1)
174
175 const videoChannel: VideoChannel = res.body.data[ 0 ]
176 expect(videoChannel.name).to.equal('root_channel')
177 }
178 })
179
126 it('Should have one video channel when getting account channels on server 2', async function () { 180 it('Should have one video channel when getting account channels on server 2', async function () {
127 const res = await getAccountVideoChannelsList(servers[1].url, userInfo.account.name + '@' + userInfo.account.host) 181 const res = await getAccountVideoChannelsList({
182 url: servers[ 1 ].url,
183 accountName: userInfo.account.name + '@' + userInfo.account.host
184 })
185
128 expect(res.body.total).to.equal(1) 186 expect(res.body.total).to.equal(1)
129 expect(res.body.data).to.be.an('array') 187 expect(res.body.data).to.be.an('array')
130 expect(res.body.data).to.have.lengthOf(1) 188 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 @@
1import * as request from 'supertest' 1import * as request from 'supertest'
2import { VideoChannelCreate, VideoChannelUpdate } from '../../models/videos' 2import { VideoChannelCreate, VideoChannelUpdate } from '../../models/videos'
3import { updateAvatarRequest } from '../requests/requests' 3import { makeGetRequest, updateAvatarRequest } from '../requests/requests'
4import { getMyUserInformation, ServerInfo } from '..' 4import { getMyUserInformation, ServerInfo } from '..'
5import { User } from '../..' 5import { User } from '../..'
6 6
@@ -19,14 +19,28 @@ function getVideoChannelsList (url: string, start: number, count: number, sort?:
19 .expect('Content-Type', /json/) 19 .expect('Content-Type', /json/)
20} 20}
21 21
22function getAccountVideoChannelsList (url: string, accountName: string, specialStatus = 200) { 22function getAccountVideoChannelsList (parameters: {
23 url: string,
24 accountName: string,
25 start?: number,
26 count?: number,
27 sort?: string,
28 specialStatus?: number
29}) {
30 const { url, accountName, start, count, sort = 'createdAt', specialStatus = 200 } = parameters
31
23 const path = '/api/v1/accounts/' + accountName + '/video-channels' 32 const path = '/api/v1/accounts/' + accountName + '/video-channels'
24 33
25 return request(url) 34 return makeGetRequest({
26 .get(path) 35 url,
27 .set('Accept', 'application/json') 36 path,
28 .expect(specialStatus) 37 query: {
29 .expect('Content-Type', /json/) 38 start,
39 count,
40 sort
41 },
42 statusCodeExpected: specialStatus
43 })
30} 44}
31 45
32function addVideoChannel ( 46function addVideoChannel (