1 /* tslint:disable:no-unused-expression */
9 generateUserAccessToken,
10 getAccountPlaylistsListWithToken,
15 removeVideoFromPlaylist,
16 reorderVideosPlaylist,
19 setAccessTokensToServers,
21 updateVideoPlaylistElement,
23 } from '../../../../shared/utils'
25 checkBadCountPagination,
26 checkBadSortPagination,
27 checkBadStartPagination
28 } from '../../../../shared/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 watchLaterPlaylistId: number
40 // ---------------------------------------------------------------
42 before(async function () {
47 server = await runServer(1)
49 await setAccessTokensToServers([ server ])
51 userAccessToken = await generateUserAccessToken(server, 'user1')
52 videoId = (await uploadVideoAndGetId({ server, videoName: 'video 1' })).id
53 videoId2 = (await uploadVideoAndGetId({ server, videoName: 'video 2' })).id
56 const res = await getAccountPlaylistsListWithToken(server.url, server.accessToken, 'root',0, 5, VideoPlaylistType.WATCH_LATER)
57 watchLaterPlaylistId = res.body.data[0].id
61 const res = await createVideoPlaylist({
63 token: server.accessToken,
65 displayName: 'super playlist',
66 privacy: VideoPlaylistPrivacy.PUBLIC
69 playlistUUID = res.body.videoPlaylist.uuid
73 describe('When listing playlists', function () {
74 const globalPath = '/api/v1/video-playlists'
75 const accountPath = '/api/v1/accounts/root/video-playlists'
76 const videoChannelPath = '/api/v1/video-channels/root_channel/video-playlists'
78 it('Should fail with a bad start pagination', async function () {
79 await checkBadStartPagination(server.url, globalPath, server.accessToken)
80 await checkBadStartPagination(server.url, accountPath, server.accessToken)
81 await checkBadStartPagination(server.url, videoChannelPath, server.accessToken)
84 it('Should fail with a bad count pagination', async function () {
85 await checkBadCountPagination(server.url, globalPath, server.accessToken)
86 await checkBadCountPagination(server.url, accountPath, server.accessToken)
87 await checkBadCountPagination(server.url, videoChannelPath, server.accessToken)
90 it('Should fail with an incorrect sort', async function () {
91 await checkBadSortPagination(server.url, globalPath, server.accessToken)
92 await checkBadSortPagination(server.url, accountPath, server.accessToken)
93 await checkBadSortPagination(server.url, videoChannelPath, server.accessToken)
96 it('Should fail with a bad playlist type', async function () {
97 await makeGetRequest({ url: server.url, path: globalPath, query: { playlistType: 3 } })
98 await makeGetRequest({ url: server.url, path: accountPath, query: { playlistType: 3 } })
99 await makeGetRequest({ url: server.url, path: videoChannelPath, query: { playlistType: 3 } })
102 it('Should fail with a bad account parameter', async function () {
103 const accountPath = '/api/v1/accounts/root2/video-playlists'
105 await makeGetRequest({ url: server.url, path: accountPath, statusCodeExpected: 404, token: server.accessToken })
108 it('Should fail with a bad video channel parameter', async function () {
109 const accountPath = '/api/v1/video-channels/bad_channel/video-playlists'
111 await makeGetRequest({ url: server.url, path: accountPath, statusCodeExpected: 404, token: server.accessToken })
114 it('Should success with the correct parameters', async function () {
115 await makeGetRequest({ url: server.url, path: globalPath, statusCodeExpected: 200, token: server.accessToken })
116 await makeGetRequest({ url: server.url, path: accountPath, statusCodeExpected: 200, token: server.accessToken })
117 await makeGetRequest({ url: server.url, path: videoChannelPath, statusCodeExpected: 200, token: server.accessToken })
121 describe('When listing videos of a playlist', function () {
122 const path = '/api/v1/video-playlists'
124 it('Should fail with a bad start pagination', async function () {
125 await checkBadStartPagination(server.url, path, server.accessToken)
128 it('Should fail with a bad count pagination', async function () {
129 await checkBadCountPagination(server.url, path, server.accessToken)
132 it('Should fail with a bad filter', async function () {
133 await checkBadSortPagination(server.url, path, server.accessToken)
137 describe('When getting a video playlist', function () {
138 it('Should fail with a bad id or uuid', async function () {
139 await getVideoPlaylist(server.url, 'toto', 400)
142 it('Should fail with an unknown playlist', async function () {
143 await getVideoPlaylist(server.url, 42, 404)
146 it('Should fail to get an unlisted playlist with the number id', async function () {
147 const res = await createVideoPlaylist({
149 token: server.accessToken,
151 displayName: 'super playlist',
152 privacy: VideoPlaylistPrivacy.UNLISTED
155 const playlist = res.body.videoPlaylist
157 await getVideoPlaylist(server.url, playlist.id, 404)
158 await getVideoPlaylist(server.url, playlist.uuid, 200)
161 it('Should succeed with the correct params', async function () {
162 await getVideoPlaylist(server.url, playlistUUID, 200)
166 describe('When creating/updating a video playlist', function () {
167 const getBase = (playlistAttrs: any = {}, wrapper: any = {}) => {
168 return Object.assign({
171 token: server.accessToken,
172 playlistAttrs: Object.assign({
173 displayName: 'display name',
174 privacy: VideoPlaylistPrivacy.UNLISTED,
175 thumbnailfile: 'thumbnail.jpg'
179 const getUpdate = (params: any, playlistId: number | string) => {
180 return immutableAssign(params, { playlistId: playlistId })
183 it('Should fail with an unauthenticated user', async function () {
184 const params = getBase({}, { token: null, expectedStatus: 401 })
186 await createVideoPlaylist(params)
187 await updateVideoPlaylist(getUpdate(params, playlistUUID))
190 it('Should fail without displayName', async function () {
191 const params = getBase({ displayName: undefined })
193 await createVideoPlaylist(params)
194 await updateVideoPlaylist(getUpdate(params, playlistUUID))
197 it('Should fail with an incorrect display name', async function () {
198 const params = getBase({ displayName: 's'.repeat(300) })
200 await createVideoPlaylist(params)
201 await updateVideoPlaylist(getUpdate(params, playlistUUID))
204 it('Should fail with an incorrect description', async function () {
205 const params = getBase({ description: 't' })
207 await createVideoPlaylist(params)
208 await updateVideoPlaylist(getUpdate(params, playlistUUID))
211 it('Should fail with an incorrect privacy', async function () {
212 const params = getBase({ privacy: 45 })
214 await createVideoPlaylist(params)
215 await updateVideoPlaylist(getUpdate(params, playlistUUID))
218 it('Should fail with an unknown video channel id', async function () {
219 const params = getBase({ videoChannelId: 42 }, { expectedStatus: 404 })
221 await createVideoPlaylist(params)
222 await updateVideoPlaylist(getUpdate(params, playlistUUID))
225 it('Should fail with an incorrect thumbnail file', async function () {
226 const params = getBase({ thumbnailfile: 'avatar.png' })
228 await createVideoPlaylist(params)
229 await updateVideoPlaylist(getUpdate(params, playlistUUID))
232 it('Should fail with an unknown playlist to update', async function () {
233 await updateVideoPlaylist(getUpdate(
234 getBase({}, { expectedStatus: 404 }),
239 it('Should fail to update a playlist of another user', async function () {
240 await updateVideoPlaylist(getUpdate(
241 getBase({}, { token: userAccessToken, expectedStatus: 403 }),
246 it('Should fail to update to private a public/unlisted playlist', async function () {
247 const params = getBase({ privacy: VideoPlaylistPrivacy.PUBLIC }, { expectedStatus: 200 })
249 const res = await createVideoPlaylist(params)
250 const playlist = res.body.videoPlaylist
252 const paramsUpdate = getBase({ privacy: VideoPlaylistPrivacy.PRIVATE }, { expectedStatus: 409 })
254 await updateVideoPlaylist(getUpdate(paramsUpdate, playlist.id))
257 it('Should fail to update the watch later playlist', async function () {
258 await updateVideoPlaylist(getUpdate(
259 getBase({}, { expectedStatus: 409 }),
264 it('Should succeed with the correct params', async function () {
266 const params = getBase({}, { expectedStatus: 200 })
267 await createVideoPlaylist(params)
271 const params = getBase({}, { expectedStatus: 204 })
272 await updateVideoPlaylist(getUpdate(params, playlistUUID))
277 describe('When adding an element in a playlist', function () {
278 const getBase = (elementAttrs: any = {}, wrapper: any = {}) => {
279 return Object.assign({
282 token: server.accessToken,
283 playlistId: playlistUUID,
284 elementAttrs: Object.assign({
292 it('Should fail with an unauthenticated user', async function () {
293 const params = getBase({}, { token: null, expectedStatus: 401 })
294 await addVideoInPlaylist(params)
297 it('Should fail with the playlist of another user', async function () {
298 const params = getBase({}, { token: userAccessToken, expectedStatus: 403 })
299 await addVideoInPlaylist(params)
302 it('Should fail with an unknown or incorrect playlist id', async function () {
304 const params = getBase({}, { playlistId: 'toto' })
305 await addVideoInPlaylist(params)
309 const params = getBase({}, { playlistId: 42, expectedStatus: 404 })
310 await addVideoInPlaylist(params)
314 it('Should fail with an unknown or incorrect video id', async function () {
315 const params = getBase({ videoId: 42 }, { expectedStatus: 404 })
316 await addVideoInPlaylist(params)
319 it('Should fail with a bad start/stop timestamp', async function () {
321 const params = getBase({ startTimestamp: -42 })
322 await addVideoInPlaylist(params)
326 const params = getBase({ stopTimestamp: 'toto' as any })
327 await addVideoInPlaylist(params)
331 it('Succeed with the correct params', async function () {
332 const params = getBase({}, { expectedStatus: 200 })
333 await addVideoInPlaylist(params)
336 it('Should fail if the video was already added in the playlist', async function () {
337 const params = getBase({}, { expectedStatus: 409 })
338 await addVideoInPlaylist(params)
342 describe('When updating an element in a playlist', function () {
343 const getBase = (elementAttrs: any = {}, wrapper: any = {}) => {
344 return Object.assign({
346 token: server.accessToken,
347 elementAttrs: Object.assign({
352 playlistId: playlistUUID,
357 it('Should fail with an unauthenticated user', async function () {
358 const params = getBase({}, { token: null, expectedStatus: 401 })
359 await updateVideoPlaylistElement(params)
362 it('Should fail with the playlist of another user', async function () {
363 const params = getBase({}, { token: userAccessToken, expectedStatus: 403 })
364 await updateVideoPlaylistElement(params)
367 it('Should fail with an unknown or incorrect playlist id', async function () {
369 const params = getBase({}, { playlistId: 'toto' })
370 await updateVideoPlaylistElement(params)
374 const params = getBase({}, { playlistId: 42, expectedStatus: 404 })
375 await updateVideoPlaylistElement(params)
379 it('Should fail with an unknown or incorrect video id', async function () {
381 const params = getBase({}, { videoId: 'toto' })
382 await updateVideoPlaylistElement(params)
386 const params = getBase({}, { videoId: 42, expectedStatus: 404 })
387 await updateVideoPlaylistElement(params)
391 it('Should fail with a bad start/stop timestamp', async function () {
393 const params = getBase({ startTimestamp: 'toto' as any })
394 await updateVideoPlaylistElement(params)
398 const params = getBase({ stopTimestamp: -42 })
399 await updateVideoPlaylistElement(params)
403 it('Should fail with an unknown element', async function () {
404 const params = getBase({}, { videoId: videoId2, expectedStatus: 404 })
405 await updateVideoPlaylistElement(params)
408 it('Succeed with the correct params', async function () {
409 const params = getBase({}, { expectedStatus: 204 })
410 await updateVideoPlaylistElement(params)
414 describe('When reordering elements of a playlist', function () {
418 const getBase = (elementAttrs: any = {}, wrapper: any = {}) => {
419 return Object.assign({
421 token: server.accessToken,
422 playlistId: playlistUUID,
423 elementAttrs: Object.assign({
425 insertAfterPosition: 2,
432 before(async function () {
433 videoId3 = (await uploadVideoAndGetId({ server, videoName: 'video 3' })).id
434 videoId4 = (await uploadVideoAndGetId({ server, videoName: 'video 4' })).id
436 for (let id of [ videoId3, videoId4 ]) {
437 await addVideoInPlaylist({
439 token: server.accessToken,
440 playlistId: playlistUUID,
441 elementAttrs: { videoId: id }
446 it('Should fail with an unauthenticated user', async function () {
447 const params = getBase({}, { token: null, expectedStatus: 401 })
448 await reorderVideosPlaylist(params)
451 it('Should fail with the playlist of another user', async function () {
452 const params = getBase({}, { token: userAccessToken, expectedStatus: 403 })
453 await reorderVideosPlaylist(params)
456 it('Should fail with an invalid playlist', async function () {
458 const params = getBase({}, { playlistId: 'toto' })
459 await reorderVideosPlaylist(params)
463 const params = getBase({}, { playlistId: 42, expectedStatus: 404 })
464 await reorderVideosPlaylist(params)
468 it('Should fail with an invalid start position', async function () {
470 const params = getBase({ startPosition: -1 })
471 await reorderVideosPlaylist(params)
475 const params = getBase({ startPosition: 'toto' as any })
476 await reorderVideosPlaylist(params)
480 const params = getBase({ startPosition: 42 })
481 await reorderVideosPlaylist(params)
485 it('Should fail with an invalid insert after position', async function () {
487 const params = getBase({ insertAfterPosition: 'toto' as any })
488 await reorderVideosPlaylist(params)
492 const params = getBase({ insertAfterPosition: -2 })
493 await reorderVideosPlaylist(params)
497 const params = getBase({ insertAfterPosition: 42 })
498 await reorderVideosPlaylist(params)
502 it('Should fail with an invalid reorder length', async function () {
504 const params = getBase({ reorderLength: 'toto' as any })
505 await reorderVideosPlaylist(params)
509 const params = getBase({ reorderLength: -2 })
510 await reorderVideosPlaylist(params)
514 const params = getBase({ reorderLength: 42 })
515 await reorderVideosPlaylist(params)
519 it('Succeed with the correct params', async function () {
520 const params = getBase({}, { expectedStatus: 204 })
521 await reorderVideosPlaylist(params)
525 describe('When checking exists in playlist endpoint', function () {
526 const path = '/api/v1/users/me/video-playlists/videos-exist'
528 it('Should fail with an unauthenticated user', async function () {
529 await makeGetRequest({
532 query: { videoIds: [ 1, 2 ] },
533 statusCodeExpected: 401
537 it('Should fail with invalid video ids', async function () {
538 await makeGetRequest({
540 token: server.accessToken,
542 query: { videoIds: 'toto' }
545 await makeGetRequest({
547 token: server.accessToken,
549 query: { videoIds: [ 'toto' ] }
552 await makeGetRequest({
554 token: server.accessToken,
556 query: { videoIds: [ 1, 'toto' ] }
560 it('Should succeed with the correct params', async function () {
561 await makeGetRequest({
563 token: server.accessToken,
565 query: { videoIds: [ 1, 2 ] },
566 statusCodeExpected: 200
571 describe('When deleting an element in a playlist', function () {
572 const getBase = (wrapper: any = {}) => {
573 return Object.assign({
575 token: server.accessToken,
577 playlistId: playlistUUID,
582 it('Should fail with an unauthenticated user', async function () {
583 const params = getBase({ token: null, expectedStatus: 401 })
584 await removeVideoFromPlaylist(params)
587 it('Should fail with the playlist of another user', async function () {
588 const params = getBase({ token: userAccessToken, expectedStatus: 403 })
589 await removeVideoFromPlaylist(params)
592 it('Should fail with an unknown or incorrect playlist id', async function () {
594 const params = getBase({ playlistId: 'toto' })
595 await removeVideoFromPlaylist(params)
599 const params = getBase({ playlistId: 42, expectedStatus: 404 })
600 await removeVideoFromPlaylist(params)
604 it('Should fail with an unknown or incorrect video id', async function () {
606 const params = getBase({ videoId: 'toto' })
607 await removeVideoFromPlaylist(params)
611 const params = getBase({ videoId: 42, expectedStatus: 404 })
612 await removeVideoFromPlaylist(params)
616 it('Should fail with an unknown element', async function () {
617 const params = getBase({ videoId: videoId2, expectedStatus: 404 })
618 await removeVideoFromPlaylist(params)
621 it('Succeed with the correct params', async function () {
622 const params = getBase({ expectedStatus: 204 })
623 await removeVideoFromPlaylist(params)
627 describe('When deleting a playlist', function () {
628 it('Should fail with an unknown playlist', async function () {
629 await deleteVideoPlaylist(server.url, server.accessToken, 42, 404)
632 it('Should fail with a playlist of another user', async function () {
633 await deleteVideoPlaylist(server.url, userAccessToken, playlistUUID, 403)
636 it('Should fail with the watch later playlist', async function () {
637 await deleteVideoPlaylist(server.url, server.accessToken, watchLaterPlaylistId, 409)
640 it('Should succeed with the correct params', async function () {
641 await deleteVideoPlaylist(server.url, server.accessToken, playlistUUID)
645 after(async function () {
646 killallServers([ server ])
648 // Keep the logs if the test failed