aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/utils
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2019-03-05 10:58:44 +0100
committerChocobozzz <chocobozzz@cpy.re>2019-03-18 11:17:59 +0100
commitdf0b219d36bf6852cdf2a7ad09ed4a41c6bccefa (patch)
treec4984e854f5dc18e5c27afd73b843bd52c143034 /shared/utils
parent07b1a18aa678d260009a93e36606c5c5f585723d (diff)
downloadPeerTube-df0b219d36bf6852cdf2a7ad09ed4a41c6bccefa.tar.gz
PeerTube-df0b219d36bf6852cdf2a7ad09ed4a41c6bccefa.tar.zst
PeerTube-df0b219d36bf6852cdf2a7ad09ed4a41c6bccefa.zip
Add playlist rest tests
Diffstat (limited to 'shared/utils')
-rw-r--r--shared/utils/requests/requests.ts2
-rw-r--r--shared/utils/server/servers.ts4
-rw-r--r--shared/utils/users/users.ts9
-rw-r--r--shared/utils/videos/video-channels.ts18
-rw-r--r--shared/utils/videos/video-playlists.ts117
-rw-r--r--shared/utils/videos/videos.ts29
6 files changed, 166 insertions, 13 deletions
diff --git a/shared/utils/requests/requests.ts b/shared/utils/requests/requests.ts
index dc2d4abe5..3532fb429 100644
--- a/shared/utils/requests/requests.ts
+++ b/shared/utils/requests/requests.ts
@@ -77,6 +77,8 @@ function makeUploadRequest (options: {
77 Object.keys(options.fields).forEach(field => { 77 Object.keys(options.fields).forEach(field => {
78 const value = options.fields[field] 78 const value = options.fields[field]
79 79
80 if (value === undefined) return
81
80 if (Array.isArray(value)) { 82 if (Array.isArray(value)) {
81 for (let i = 0; i < value.length; i++) { 83 for (let i = 0; i < value.length; i++) {
82 req.field(field + '[' + i + ']', value[i]) 84 req.field(field + '[' + i + ']', value[i])
diff --git a/shared/utils/server/servers.ts b/shared/utils/server/servers.ts
index bde7dd5c2..5288d253a 100644
--- a/shared/utils/server/servers.ts
+++ b/shared/utils/server/servers.ts
@@ -6,6 +6,7 @@ import { root, wait } from '../miscs/miscs'
6import { readdir, readFile } from 'fs-extra' 6import { readdir, readFile } from 'fs-extra'
7import { existsSync } from 'fs' 7import { existsSync } from 'fs'
8import { expect } from 'chai' 8import { expect } from 'chai'
9import { VideoChannel } from '../../models/videos'
9 10
10interface ServerInfo { 11interface ServerInfo {
11 app: ChildProcess, 12 app: ChildProcess,
@@ -25,6 +26,7 @@ interface ServerInfo {
25 } 26 }
26 27
27 accessToken?: string 28 accessToken?: string
29 videoChannel?: VideoChannel
28 30
29 video?: { 31 video?: {
30 id: number 32 id: number
@@ -39,6 +41,8 @@ interface ServerInfo {
39 id: number 41 id: number
40 uuid: string 42 uuid: string
41 } 43 }
44
45 videos?: { id: number, uuid: string }[]
42} 46}
43 47
44function flushAndRunMultipleServers (totalServers: number, configOverride?: Object) { 48function flushAndRunMultipleServers (totalServers: number, configOverride?: Object) {
diff --git a/shared/utils/users/users.ts b/shared/utils/users/users.ts
index 7191b263e..e3c14a4a3 100644
--- a/shared/utils/users/users.ts
+++ b/shared/utils/users/users.ts
@@ -3,6 +3,7 @@ import { makePostBodyRequest, makePutBodyRequest, updateAvatarRequest } from '..
3 3
4import { UserRole } from '../../index' 4import { UserRole } from '../../index'
5import { NSFWPolicyType } from '../../models/videos/nsfw-policy.type' 5import { NSFWPolicyType } from '../../models/videos/nsfw-policy.type'
6import { ServerInfo, userLogin } from '..'
6 7
7function createUser ( 8function createUser (
8 url: string, 9 url: string,
@@ -32,6 +33,13 @@ function createUser (
32 .expect(specialStatus) 33 .expect(specialStatus)
33} 34}
34 35
36async function generateUserAccessToken (server: ServerInfo, username: string) {
37 const password = 'my super password'
38 await createUser(server.url, server.accessToken, username, password)
39
40 return userLogin(server, { username, password })
41}
42
35function registerUser (url: string, username: string, password: string, specialStatus = 204) { 43function registerUser (url: string, username: string, password: string, specialStatus = 204) {
36 const path = '/api/v1/users/register' 44 const path = '/api/v1/users/register'
37 const body = { 45 const body = {
@@ -300,5 +308,6 @@ export {
300 resetPassword, 308 resetPassword,
301 updateMyAvatar, 309 updateMyAvatar,
302 askSendVerifyEmail, 310 askSendVerifyEmail,
311 generateUserAccessToken,
303 verifyEmail 312 verifyEmail
304} 313}
diff --git a/shared/utils/videos/video-channels.ts b/shared/utils/videos/video-channels.ts
index 3935c261e..93a257bf9 100644
--- a/shared/utils/videos/video-channels.ts
+++ b/shared/utils/videos/video-channels.ts
@@ -1,6 +1,8 @@
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 { updateAvatarRequest } from '../requests/requests'
4import { getMyUserInformation, ServerInfo } from '..'
5import { User } from '../..'
4 6
5function getVideoChannelsList (url: string, start: number, count: number, sort?: string) { 7function getVideoChannelsList (url: string, start: number, count: number, sort?: string) {
6 const path = '/api/v1/video-channels' 8 const path = '/api/v1/video-channels'
@@ -105,6 +107,19 @@ function updateVideoChannelAvatar (options: {
105 return updateAvatarRequest(Object.assign(options, { path })) 107 return updateAvatarRequest(Object.assign(options, { path }))
106} 108}
107 109
110function setDefaultVideoChannel (servers: ServerInfo[]) {
111 const tasks: Promise<any>[] = []
112
113 for (const server of servers) {
114 const p = getMyUserInformation(server.url, server.accessToken)
115 .then(res => server.videoChannel = (res.body as User).videoChannels[0])
116
117 tasks.push(p)
118 }
119
120 return Promise.all(tasks)
121}
122
108// --------------------------------------------------------------------------- 123// ---------------------------------------------------------------------------
109 124
110export { 125export {
@@ -114,5 +129,6 @@ export {
114 addVideoChannel, 129 addVideoChannel,
115 updateVideoChannel, 130 updateVideoChannel,
116 deleteVideoChannel, 131 deleteVideoChannel,
117 getVideoChannel 132 getVideoChannel,
133 setDefaultVideoChannel
118} 134}
diff --git a/shared/utils/videos/video-playlists.ts b/shared/utils/videos/video-playlists.ts
index 21285688a..4af52ec0f 100644
--- a/shared/utils/videos/video-playlists.ts
+++ b/shared/utils/videos/video-playlists.ts
@@ -4,6 +4,12 @@ import { omit } from 'lodash'
4import { VideoPlaylistUpdate } from '../../models/videos/playlist/video-playlist-update.model' 4import { VideoPlaylistUpdate } from '../../models/videos/playlist/video-playlist-update.model'
5import { VideoPlaylistElementCreate } from '../../models/videos/playlist/video-playlist-element-create.model' 5import { VideoPlaylistElementCreate } from '../../models/videos/playlist/video-playlist-element-create.model'
6import { VideoPlaylistElementUpdate } from '../../models/videos/playlist/video-playlist-element-update.model' 6import { VideoPlaylistElementUpdate } from '../../models/videos/playlist/video-playlist-element-update.model'
7import { videoUUIDToId } from './videos'
8import { join } from 'path'
9import { root } from '..'
10import { readdir } from 'fs-extra'
11import { expect } from 'chai'
12import { VideoPlaylistType } from '../../models/videos/playlist/video-playlist-type.model'
7 13
8function getVideoPlaylistsList (url: string, start: number, count: number, sort?: string) { 14function getVideoPlaylistsList (url: string, start: number, count: number, sort?: string) {
9 const path = '/api/v1/video-playlists' 15 const path = '/api/v1/video-playlists'
@@ -17,7 +23,67 @@ function getVideoPlaylistsList (url: string, start: number, count: number, sort?
17 return makeGetRequest({ 23 return makeGetRequest({
18 url, 24 url,
19 path, 25 path,
20 query 26 query,
27 statusCodeExpected: 200
28 })
29}
30
31function getVideoChannelPlaylistsList (url: string, videoChannelName: string, start: number, count: number, sort?: string) {
32 const path = '/api/v1/video-channels/' + videoChannelName + '/video-playlists'
33
34 const query = {
35 start,
36 count,
37 sort
38 }
39
40 return makeGetRequest({
41 url,
42 path,
43 query,
44 statusCodeExpected: 200
45 })
46}
47
48function getAccountPlaylistsList (url: string, accountName: string, start: number, count: number, sort?: string) {
49 const path = '/api/v1/accounts/' + accountName + '/video-playlists'
50
51 const query = {
52 start,
53 count,
54 sort
55 }
56
57 return makeGetRequest({
58 url,
59 path,
60 query,
61 statusCodeExpected: 200
62 })
63}
64
65function getAccountPlaylistsListWithToken (
66 url: string,
67 token: string,
68 accountName: string,
69 start: number,
70 count: number,
71 playlistType?: VideoPlaylistType
72) {
73 const path = '/api/v1/accounts/' + accountName + '/video-playlists'
74
75 const query = {
76 start,
77 count,
78 playlistType
79 }
80
81 return makeGetRequest({
82 url,
83 token,
84 path,
85 query,
86 statusCodeExpected: 200
21 }) 87 })
22} 88}
23 89
@@ -31,6 +97,17 @@ function getVideoPlaylist (url: string, playlistId: number | string, statusCodeE
31 }) 97 })
32} 98}
33 99
100function getVideoPlaylistWithToken (url: string, token: string, playlistId: number | string, statusCodeExpected = 200) {
101 const path = '/api/v1/video-playlists/' + playlistId
102
103 return makeGetRequest({
104 url,
105 token,
106 path,
107 statusCodeExpected
108 })
109}
110
34function deleteVideoPlaylist (url: string, token: string, playlistId: number | string, statusCodeExpected = 204) { 111function deleteVideoPlaylist (url: string, token: string, playlistId: number | string, statusCodeExpected = 204) {
35 const path = '/api/v1/video-playlists/' + playlistId 112 const path = '/api/v1/video-playlists/' + playlistId
36 113
@@ -93,13 +170,15 @@ function updateVideoPlaylist (options: {
93 }) 170 })
94} 171}
95 172
96function addVideoInPlaylist (options: { 173async function addVideoInPlaylist (options: {
97 url: string, 174 url: string,
98 token: string, 175 token: string,
99 playlistId: number | string, 176 playlistId: number | string,
100 elementAttrs: VideoPlaylistElementCreate 177 elementAttrs: VideoPlaylistElementCreate | { videoId: string }
101 expectedStatus?: number 178 expectedStatus?: number
102}) { 179}) {
180 options.elementAttrs.videoId = await videoUUIDToId(options.url, options.elementAttrs.videoId)
181
103 const path = '/api/v1/video-playlists/' + options.playlistId + '/videos' 182 const path = '/api/v1/video-playlists/' + options.playlistId + '/videos'
104 183
105 return makePostBodyRequest({ 184 return makePostBodyRequest({
@@ -135,7 +214,7 @@ function removeVideoFromPlaylist (options: {
135 token: string, 214 token: string,
136 playlistId: number | string, 215 playlistId: number | string,
137 videoId: number | string, 216 videoId: number | string,
138 expectedStatus: number 217 expectedStatus?: number
139}) { 218}) {
140 const path = '/api/v1/video-playlists/' + options.playlistId + '/videos/' + options.videoId 219 const path = '/api/v1/video-playlists/' + options.playlistId + '/videos/' + options.videoId
141 220
@@ -156,7 +235,7 @@ function reorderVideosPlaylist (options: {
156 insertAfterPosition: number, 235 insertAfterPosition: number,
157 reorderLength?: number 236 reorderLength?: number
158 }, 237 },
159 expectedStatus: number 238 expectedStatus?: number
160}) { 239}) {
161 const path = '/api/v1/video-playlists/' + options.playlistId + '/videos/reorder' 240 const path = '/api/v1/video-playlists/' + options.playlistId + '/videos/reorder'
162 241
@@ -165,15 +244,37 @@ function reorderVideosPlaylist (options: {
165 path, 244 path,
166 token: options.token, 245 token: options.token,
167 fields: options.elementAttrs, 246 fields: options.elementAttrs,
168 statusCodeExpected: options.expectedStatus 247 statusCodeExpected: options.expectedStatus || 204
169 }) 248 })
170} 249}
171 250
251async function checkPlaylistFilesWereRemoved (
252 playlistUUID: string,
253 serverNumber: number,
254 directories = [ 'thumbnails' ]
255) {
256 const testDirectory = 'test' + serverNumber
257
258 for (const directory of directories) {
259 const directoryPath = join(root(), testDirectory, directory)
260
261 const files = await readdir(directoryPath)
262 for (const file of files) {
263 expect(file).to.not.contain(playlistUUID)
264 }
265 }
266}
267
172// --------------------------------------------------------------------------- 268// ---------------------------------------------------------------------------
173 269
174export { 270export {
175 getVideoPlaylistsList, 271 getVideoPlaylistsList,
272 getVideoChannelPlaylistsList,
273 getAccountPlaylistsList,
274 getAccountPlaylistsListWithToken,
275
176 getVideoPlaylist, 276 getVideoPlaylist,
277 getVideoPlaylistWithToken,
177 278
178 createVideoPlaylist, 279 createVideoPlaylist,
179 updateVideoPlaylist, 280 updateVideoPlaylist,
@@ -183,5 +284,7 @@ export {
183 updateVideoPlaylistElement, 284 updateVideoPlaylistElement,
184 removeVideoFromPlaylist, 285 removeVideoFromPlaylist,
185 286
186 reorderVideosPlaylist 287 reorderVideosPlaylist,
288
289 checkPlaylistFilesWereRemoved
187} 290}
diff --git a/shared/utils/videos/videos.ts b/shared/utils/videos/videos.ts
index 2c09f0086..16b5165f1 100644
--- a/shared/utils/videos/videos.ts
+++ b/shared/utils/videos/videos.ts
@@ -1,7 +1,7 @@
1/* tslint:disable:no-unused-expression */ 1/* tslint:disable:no-unused-expression */
2 2
3import { expect } from 'chai' 3import { expect } from 'chai'
4import { existsSync, readdir, readFile } from 'fs-extra' 4import { pathExists, readdir, readFile } from 'fs-extra'
5import * as parseTorrent from 'parse-torrent' 5import * as parseTorrent from 'parse-torrent'
6import { extname, join } from 'path' 6import { extname, join } from 'path'
7import * as request from 'supertest' 7import * as request from 'supertest'
@@ -16,7 +16,7 @@ import {
16 ServerInfo, 16 ServerInfo,
17 testImage 17 testImage
18} from '../' 18} from '../'
19 19import * as validator from 'validator'
20import { VideoDetails, VideoPrivacy } from '../../models/videos' 20import { VideoDetails, VideoPrivacy } from '../../models/videos'
21import { VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_PRIVACIES } from '../../../server/initializers/constants' 21import { VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_PRIVACIES } from '../../../server/initializers/constants'
22import { dateIsValid, webtorrentAdd } from '../miscs/miscs' 22import { dateIsValid, webtorrentAdd } from '../miscs/miscs'
@@ -311,8 +311,8 @@ async function checkVideoFilesWereRemoved (
311 for (const directory of directories) { 311 for (const directory of directories) {
312 const directoryPath = join(root(), testDirectory, directory) 312 const directoryPath = join(root(), testDirectory, directory)
313 313
314 const directoryExists = existsSync(directoryPath) 314 const directoryExists = await pathExists(directoryPath)
315 if (!directoryExists) continue 315 if (directoryExists === false) continue
316 316
317 const files = await readdir(directoryPath) 317 const files = await readdir(directoryPath)
318 for (const file of files) { 318 for (const file of files) {
@@ -597,12 +597,30 @@ async function completeVideoCheck (
597 } 597 }
598} 598}
599 599
600async function videoUUIDToId (url: string, id: number | string) {
601 if (validator.isUUID('' + id) === false) return id
602
603 const res = await getVideo(url, id)
604 return res.body.id
605}
606
607async function uploadVideoAndGetId (options: { server: ServerInfo, videoName: string, nsfw?: boolean, token?: string }) {
608 const videoAttrs: any = { name: options.videoName }
609 if (options.nsfw) videoAttrs.nsfw = options.nsfw
610
611
612 const res = await uploadVideo(options.server.url, options.token || options.server.accessToken, videoAttrs)
613
614 return { id: res.body.video.id, uuid: res.body.video.uuid }
615}
616
600// --------------------------------------------------------------------------- 617// ---------------------------------------------------------------------------
601 618
602export { 619export {
603 getVideoDescription, 620 getVideoDescription,
604 getVideoCategories, 621 getVideoCategories,
605 getVideoLicences, 622 getVideoLicences,
623 videoUUIDToId,
606 getVideoPrivacies, 624 getVideoPrivacies,
607 getVideoLanguages, 625 getVideoLanguages,
608 getMyVideos, 626 getMyVideos,
@@ -624,5 +642,6 @@ export {
624 getLocalVideos, 642 getLocalVideos,
625 completeVideoCheck, 643 completeVideoCheck,
626 checkVideoFilesWereRemoved, 644 checkVideoFilesWereRemoved,
627 getPlaylistVideos 645 getPlaylistVideos,
646 uploadVideoAndGetId
628} 647}