1 /* tslint:disable:no-unused-expression */
9 generateUserAccessToken,
10 getAccountPlaylistsListWithToken,
15 removeVideoFromPlaylist,
16 reorderVideosPlaylist,
19 setAccessTokensToServers, setDefaultVideoChannel,
21 updateVideoPlaylistElement,
23 } from '../../../../shared/extra-utils'
25 checkBadCountPagination,
26 checkBadSortPagination,
27 checkBadStartPagination
28 } from '../../../../shared/extra-utils/requests/check-api-params'
29 import { VideoPlaylistPrivacy } from '../../../../shared/models/videos/playlist/video-playlist-privacy.model'
30 import { VideoPlaylistType } from '../../../../shared/models/videos/playlist/video-playlist-type.model'
32 describe('Test video playlists API validator', function () {
33 let server: ServerInfo
34 let userAccessToken: string
35 let playlistUUID: string
36 let privatePlaylistUUID: string
37 let watchLaterPlaylistId: number
41 // ---------------------------------------------------------------
43 before(async function () {
48 server = await runServer(1)
50 await setAccessTokensToServers([ server ])
51 await setDefaultVideoChannel([ server ])
53 userAccessToken = await generateUserAccessToken(server, 'user1')
54 videoId = (await uploadVideoAndGetId({ server, videoName: 'video 1' })).id
55 videoId2 = (await uploadVideoAndGetId({ server, videoName: 'video 2' })).id
58 const res = await getAccountPlaylistsListWithToken(server.url, server.accessToken, 'root',0, 5, VideoPlaylistType.WATCH_LATER)
59 watchLaterPlaylistId = res.body.data[0].id
63 const res = await createVideoPlaylist({
65 token: server.accessToken,
67 displayName: 'super playlist',
68 privacy: VideoPlaylistPrivacy.PUBLIC,
69 videoChannelId: server.videoChannel.id
72 playlistUUID = res.body.videoPlaylist.uuid
76 const res = await createVideoPlaylist({
78 token: server.accessToken,
80 displayName: 'private',
81 privacy: VideoPlaylistPrivacy.PRIVATE
84 privatePlaylistUUID = res.body.videoPlaylist.uuid
88 describe('When listing playlists', function () {
89 const globalPath = '/api/v1/video-playlists'
90 const accountPath = '/api/v1/accounts/root/video-playlists'
91 const videoChannelPath = '/api/v1/video-channels/root_channel/video-playlists'
93 it('Should fail with a bad start pagination', async function () {
94 await checkBadStartPagination(server.url, globalPath, server.accessToken)
95 await checkBadStartPagination(server.url, accountPath, server.accessToken)
96 await checkBadStartPagination(server.url, videoChannelPath, server.accessToken)
99 it('Should fail with a bad count pagination', async function () {
100 await checkBadCountPagination(server.url, globalPath, server.accessToken)
101 await checkBadCountPagination(server.url, accountPath, server.accessToken)
102 await checkBadCountPagination(server.url, videoChannelPath, server.accessToken)
105 it('Should fail with an incorrect sort', async function () {
106 await checkBadSortPagination(server.url, globalPath, server.accessToken)
107 await checkBadSortPagination(server.url, accountPath, server.accessToken)
108 await checkBadSortPagination(server.url, videoChannelPath, server.accessToken)
111 it('Should fail with a bad playlist type', async function () {
112 await makeGetRequest({ url: server.url, path: globalPath, query: { playlistType: 3 } })
113 await makeGetRequest({ url: server.url, path: accountPath, query: { playlistType: 3 } })
114 await makeGetRequest({ url: server.url, path: videoChannelPath, query: { playlistType: 3 } })
117 it('Should fail with a bad account parameter', async function () {
118 const accountPath = '/api/v1/accounts/root2/video-playlists'
120 await makeGetRequest({ url: server.url, path: accountPath, statusCodeExpected: 404, token: server.accessToken })
123 it('Should fail with a bad video channel parameter', async function () {
124 const accountPath = '/api/v1/video-channels/bad_channel/video-playlists'
126 await makeGetRequest({ url: server.url, path: accountPath, statusCodeExpected: 404, token: server.accessToken })
129 it('Should success with the correct parameters', async function () {
130 await makeGetRequest({ url: server.url, path: globalPath, statusCodeExpected: 200, token: server.accessToken })
131 await makeGetRequest({ url: server.url, path: accountPath, statusCodeExpected: 200, token: server.accessToken })
132 await makeGetRequest({ url: server.url, path: videoChannelPath, statusCodeExpected: 200, token: server.accessToken })
136 describe('When listing videos of a playlist', function () {
137 const path = '/api/v1/video-playlists'
139 it('Should fail with a bad start pagination', async function () {
140 await checkBadStartPagination(server.url, path, server.accessToken)
143 it('Should fail with a bad count pagination', async function () {
144 await checkBadCountPagination(server.url, path, server.accessToken)
147 it('Should fail with a bad filter', async function () {
148 await checkBadSortPagination(server.url, path, server.accessToken)
152 describe('When getting a video playlist', function () {
153 it('Should fail with a bad id or uuid', async function () {
154 await getVideoPlaylist(server.url, 'toto', 400)
157 it('Should fail with an unknown playlist', async function () {
158 await getVideoPlaylist(server.url, 42, 404)
161 it('Should fail to get an unlisted playlist with the number id', async function () {
162 const res = await createVideoPlaylist({
164 token: server.accessToken,
166 displayName: 'super playlist',
167 privacy: VideoPlaylistPrivacy.UNLISTED
170 const playlist = res.body.videoPlaylist
172 await getVideoPlaylist(server.url, playlist.id, 404)
173 await getVideoPlaylist(server.url, playlist.uuid, 200)
176 it('Should succeed with the correct params', async function () {
177 await getVideoPlaylist(server.url, playlistUUID, 200)
181 describe('When creating/updating a video playlist', function () {
182 const getBase = (playlistAttrs: any = {}, wrapper: any = {}) => {
183 return Object.assign({
186 token: server.accessToken,
187 playlistAttrs: Object.assign({
188 displayName: 'display name',
189 privacy: VideoPlaylistPrivacy.UNLISTED,
190 thumbnailfile: 'thumbnail.jpg',
191 videoChannelId: server.videoChannel.id
195 const getUpdate = (params: any, playlistId: number | string) => {
196 return immutableAssign(params, { playlistId: playlistId })
199 it('Should fail with an unauthenticated user', async function () {
200 const params = getBase({}, { token: null, expectedStatus: 401 })
202 await createVideoPlaylist(params)
203 await updateVideoPlaylist(getUpdate(params, playlistUUID))
206 it('Should fail without displayName', async function () {
207 const params = getBase({ displayName: undefined })
209 await createVideoPlaylist(params)
210 await updateVideoPlaylist(getUpdate(params, playlistUUID))
213 it('Should fail with an incorrect display name', async function () {
214 const params = getBase({ displayName: 's'.repeat(300) })
216 await createVideoPlaylist(params)
217 await updateVideoPlaylist(getUpdate(params, playlistUUID))
220 it('Should fail with an incorrect description', async function () {
221 const params = getBase({ description: 't' })
223 await createVideoPlaylist(params)
224 await updateVideoPlaylist(getUpdate(params, playlistUUID))
227 it('Should fail with an incorrect privacy', async function () {
228 const params = getBase({ privacy: 45 })
230 await createVideoPlaylist(params)
231 await updateVideoPlaylist(getUpdate(params, playlistUUID))
234 it('Should fail with an unknown video channel id', async function () {
235 const params = getBase({ videoChannelId: 42 }, { expectedStatus: 404 })
237 await createVideoPlaylist(params)
238 await updateVideoPlaylist(getUpdate(params, playlistUUID))
241 it('Should fail with an incorrect thumbnail file', async function () {
242 const params = getBase({ thumbnailfile: 'avatar.png' })
244 await createVideoPlaylist(params)
245 await updateVideoPlaylist(getUpdate(params, playlistUUID))
248 it('Should fail to set "public" a playlist not assigned to a channel', async function () {
249 const params = getBase({ privacy: VideoPlaylistPrivacy.PUBLIC, videoChannelId: undefined })
250 const params2 = getBase({ privacy: VideoPlaylistPrivacy.PUBLIC, videoChannelId: 'null' })
251 const params3 = getBase({ privacy: undefined, videoChannelId: 'null' })
253 await createVideoPlaylist(params)
254 await createVideoPlaylist(params2)
255 await updateVideoPlaylist(getUpdate(params, privatePlaylistUUID))
256 await updateVideoPlaylist(getUpdate(params2, playlistUUID))
257 await updateVideoPlaylist(getUpdate(params3, playlistUUID))
260 it('Should fail with an unknown playlist to update', async function () {
261 await updateVideoPlaylist(getUpdate(
262 getBase({}, { expectedStatus: 404 }),
267 it('Should fail to update a playlist of another user', async function () {
268 await updateVideoPlaylist(getUpdate(
269 getBase({}, { token: userAccessToken, expectedStatus: 403 }),
274 it('Should fail to update to private a public/unlisted playlist', async function () {
275 const params = getBase({ privacy: VideoPlaylistPrivacy.PUBLIC }, { expectedStatus: 200 })
277 const res = await createVideoPlaylist(params)
278 const playlist = res.body.videoPlaylist
280 const paramsUpdate = getBase({ privacy: VideoPlaylistPrivacy.PRIVATE }, { expectedStatus: 400 })
282 await updateVideoPlaylist(getUpdate(paramsUpdate, playlist.id))
285 it('Should fail to update the watch later playlist', async function () {
286 await updateVideoPlaylist(getUpdate(
287 getBase({}, { expectedStatus: 400 }),
292 it('Should succeed with the correct params', async function () {
294 const params = getBase({}, { expectedStatus: 200 })
295 await createVideoPlaylist(params)
299 const params = getBase({}, { expectedStatus: 204 })
300 await updateVideoPlaylist(getUpdate(params, playlistUUID))
305 describe('When adding an element in a playlist', function () {
306 const getBase = (elementAttrs: any = {}, wrapper: any = {}) => {
307 return Object.assign({
310 token: server.accessToken,
311 playlistId: playlistUUID,
312 elementAttrs: Object.assign({
320 it('Should fail with an unauthenticated user', async function () {
321 const params = getBase({}, { token: null, expectedStatus: 401 })
322 await addVideoInPlaylist(params)
325 it('Should fail with the playlist of another user', async function () {
326 const params = getBase({}, { token: userAccessToken, expectedStatus: 403 })
327 await addVideoInPlaylist(params)
330 it('Should fail with an unknown or incorrect playlist id', async function () {
332 const params = getBase({}, { playlistId: 'toto' })
333 await addVideoInPlaylist(params)
337 const params = getBase({}, { playlistId: 42, expectedStatus: 404 })
338 await addVideoInPlaylist(params)
342 it('Should fail with an unknown or incorrect video id', async function () {
343 const params = getBase({ videoId: 42 }, { expectedStatus: 404 })
344 await addVideoInPlaylist(params)
347 it('Should fail with a bad start/stop timestamp', async function () {
349 const params = getBase({ startTimestamp: -42 })
350 await addVideoInPlaylist(params)
354 const params = getBase({ stopTimestamp: 'toto' as any })
355 await addVideoInPlaylist(params)
359 it('Succeed with the correct params', async function () {
360 const params = getBase({}, { expectedStatus: 200 })
361 await addVideoInPlaylist(params)
364 it('Should fail if the video was already added in the playlist', async function () {
365 const params = getBase({}, { expectedStatus: 409 })
366 await addVideoInPlaylist(params)
370 describe('When updating an element in a playlist', function () {
371 const getBase = (elementAttrs: any = {}, wrapper: any = {}) => {
372 return Object.assign({
374 token: server.accessToken,
375 elementAttrs: Object.assign({
380 playlistId: playlistUUID,
385 it('Should fail with an unauthenticated user', async function () {
386 const params = getBase({}, { token: null, expectedStatus: 401 })
387 await updateVideoPlaylistElement(params)
390 it('Should fail with the playlist of another user', async function () {
391 const params = getBase({}, { token: userAccessToken, expectedStatus: 403 })
392 await updateVideoPlaylistElement(params)
395 it('Should fail with an unknown or incorrect playlist id', async function () {
397 const params = getBase({}, { playlistId: 'toto' })
398 await updateVideoPlaylistElement(params)
402 const params = getBase({}, { playlistId: 42, expectedStatus: 404 })
403 await updateVideoPlaylistElement(params)
407 it('Should fail with an unknown or incorrect video id', async function () {
409 const params = getBase({}, { videoId: 'toto' })
410 await updateVideoPlaylistElement(params)
414 const params = getBase({}, { videoId: 42, expectedStatus: 404 })
415 await updateVideoPlaylistElement(params)
419 it('Should fail with a bad start/stop timestamp', async function () {
421 const params = getBase({ startTimestamp: 'toto' as any })
422 await updateVideoPlaylistElement(params)
426 const params = getBase({ stopTimestamp: -42 })
427 await updateVideoPlaylistElement(params)
431 it('Should fail with an unknown element', async function () {
432 const params = getBase({}, { videoId: videoId2, expectedStatus: 404 })
433 await updateVideoPlaylistElement(params)
436 it('Succeed with the correct params', async function () {
437 const params = getBase({}, { expectedStatus: 204 })
438 await updateVideoPlaylistElement(params)
442 describe('When reordering elements of a playlist', function () {
446 const getBase = (elementAttrs: any = {}, wrapper: any = {}) => {
447 return Object.assign({
449 token: server.accessToken,
450 playlistId: playlistUUID,
451 elementAttrs: Object.assign({
453 insertAfterPosition: 2,
460 before(async function () {
461 videoId3 = (await uploadVideoAndGetId({ server, videoName: 'video 3' })).id
462 videoId4 = (await uploadVideoAndGetId({ server, videoName: 'video 4' })).id
464 for (let id of [ videoId3, videoId4 ]) {
465 await addVideoInPlaylist({
467 token: server.accessToken,
468 playlistId: playlistUUID,
469 elementAttrs: { videoId: id }
474 it('Should fail with an unauthenticated user', async function () {
475 const params = getBase({}, { token: null, expectedStatus: 401 })
476 await reorderVideosPlaylist(params)
479 it('Should fail with the playlist of another user', async function () {
480 const params = getBase({}, { token: userAccessToken, expectedStatus: 403 })
481 await reorderVideosPlaylist(params)
484 it('Should fail with an invalid playlist', async function () {
486 const params = getBase({}, { playlistId: 'toto' })
487 await reorderVideosPlaylist(params)
491 const params = getBase({}, { playlistId: 42, expectedStatus: 404 })
492 await reorderVideosPlaylist(params)
496 it('Should fail with an invalid start position', async function () {
498 const params = getBase({ startPosition: -1 })
499 await reorderVideosPlaylist(params)
503 const params = getBase({ startPosition: 'toto' as any })
504 await reorderVideosPlaylist(params)
508 const params = getBase({ startPosition: 42 })
509 await reorderVideosPlaylist(params)
513 it('Should fail with an invalid insert after position', async function () {
515 const params = getBase({ insertAfterPosition: 'toto' as any })
516 await reorderVideosPlaylist(params)
520 const params = getBase({ insertAfterPosition: -2 })
521 await reorderVideosPlaylist(params)
525 const params = getBase({ insertAfterPosition: 42 })
526 await reorderVideosPlaylist(params)
530 it('Should fail with an invalid reorder length', async function () {
532 const params = getBase({ reorderLength: 'toto' as any })
533 await reorderVideosPlaylist(params)
537 const params = getBase({ reorderLength: -2 })
538 await reorderVideosPlaylist(params)
542 const params = getBase({ reorderLength: 42 })
543 await reorderVideosPlaylist(params)
547 it('Succeed with the correct params', async function () {
548 const params = getBase({}, { expectedStatus: 204 })
549 await reorderVideosPlaylist(params)
553 describe('When checking exists in playlist endpoint', function () {
554 const path = '/api/v1/users/me/video-playlists/videos-exist'
556 it('Should fail with an unauthenticated user', async function () {
557 await makeGetRequest({
560 query: { videoIds: [ 1, 2 ] },
561 statusCodeExpected: 401
565 it('Should fail with invalid video ids', async function () {
566 await makeGetRequest({
568 token: server.accessToken,
570 query: { videoIds: 'toto' }
573 await makeGetRequest({
575 token: server.accessToken,
577 query: { videoIds: [ 'toto' ] }
580 await makeGetRequest({
582 token: server.accessToken,
584 query: { videoIds: [ 1, 'toto' ] }
588 it('Should succeed with the correct params', async function () {
589 await makeGetRequest({
591 token: server.accessToken,
593 query: { videoIds: [ 1, 2 ] },
594 statusCodeExpected: 200
599 describe('When deleting an element in a playlist', function () {
600 const getBase = (wrapper: any = {}) => {
601 return Object.assign({
603 token: server.accessToken,
605 playlistId: playlistUUID,
610 it('Should fail with an unauthenticated user', async function () {
611 const params = getBase({ token: null, expectedStatus: 401 })
612 await removeVideoFromPlaylist(params)
615 it('Should fail with the playlist of another user', async function () {
616 const params = getBase({ token: userAccessToken, expectedStatus: 403 })
617 await removeVideoFromPlaylist(params)
620 it('Should fail with an unknown or incorrect playlist id', async function () {
622 const params = getBase({ playlistId: 'toto' })
623 await removeVideoFromPlaylist(params)
627 const params = getBase({ playlistId: 42, expectedStatus: 404 })
628 await removeVideoFromPlaylist(params)
632 it('Should fail with an unknown or incorrect video id', async function () {
634 const params = getBase({ videoId: 'toto' })
635 await removeVideoFromPlaylist(params)
639 const params = getBase({ videoId: 42, expectedStatus: 404 })
640 await removeVideoFromPlaylist(params)
644 it('Should fail with an unknown element', async function () {
645 const params = getBase({ videoId: videoId2, expectedStatus: 404 })
646 await removeVideoFromPlaylist(params)
649 it('Succeed with the correct params', async function () {
650 const params = getBase({ expectedStatus: 204 })
651 await removeVideoFromPlaylist(params)
655 describe('When deleting a playlist', function () {
656 it('Should fail with an unknown playlist', async function () {
657 await deleteVideoPlaylist(server.url, server.accessToken, 42, 404)
660 it('Should fail with a playlist of another user', async function () {
661 await deleteVideoPlaylist(server.url, userAccessToken, playlistUUID, 403)
664 it('Should fail with the watch later playlist', async function () {
665 await deleteVideoPlaylist(server.url, server.accessToken, watchLaterPlaylistId, 400)
668 it('Should succeed with the correct params', async function () {
669 await deleteVideoPlaylist(server.url, server.accessToken, playlistUUID)
673 after(async function () {
674 killallServers([ server ])
676 // Keep the logs if the test failed