aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/api
diff options
context:
space:
mode:
Diffstat (limited to 'server/tests/api')
-rw-r--r--server/tests/api/activitypub/cleaner.ts105
-rw-r--r--server/tests/api/activitypub/client.ts24
-rw-r--r--server/tests/api/activitypub/fetch.ts51
-rw-r--r--server/tests/api/activitypub/helpers.ts5
-rw-r--r--server/tests/api/activitypub/refresher.ts96
-rw-r--r--server/tests/api/activitypub/security.ts46
-rw-r--r--server/tests/api/check-params/abuses.ts205
-rw-r--r--server/tests/api/check-params/accounts.ts19
-rw-r--r--server/tests/api/check-params/blocklist.ts107
-rw-r--r--server/tests/api/check-params/bulk.ts30
-rw-r--r--server/tests/api/check-params/config.ts65
-rw-r--r--server/tests/api/check-params/contact-form.ts84
-rw-r--r--server/tests/api/check-params/custom-pages.ts31
-rw-r--r--server/tests/api/check-params/debug.ts27
-rw-r--r--server/tests/api/check-params/follows.ts112
-rw-r--r--server/tests/api/check-params/jobs.ts33
-rw-r--r--server/tests/api/check-params/live.ts225
-rw-r--r--server/tests/api/check-params/logs.ts35
-rw-r--r--server/tests/api/check-params/plugins.ts95
-rw-r--r--server/tests/api/check-params/redundancy.ts60
-rw-r--r--server/tests/api/check-params/search.ts152
-rw-r--r--server/tests/api/check-params/services.ts49
-rw-r--r--server/tests/api/check-params/upload-quota.ts97
-rw-r--r--server/tests/api/check-params/user-notifications.ts57
-rw-r--r--server/tests/api/check-params/user-subscriptions.ts80
-rw-r--r--server/tests/api/check-params/users.ts425
-rw-r--r--server/tests/api/check-params/video-blacklist.ts132
-rw-r--r--server/tests/api/check-params/video-captions.ts60
-rw-r--r--server/tests/api/check-params/video-channels.ts88
-rw-r--r--server/tests/api/check-params/video-comments.ts107
-rw-r--r--server/tests/api/check-params/video-imports.ts124
-rw-r--r--server/tests/api/check-params/video-playlists.ts394
-rw-r--r--server/tests/api/check-params/videos-filter.ts44
-rw-r--r--server/tests/api/check-params/videos-history.ts43
-rw-r--r--server/tests/api/check-params/videos-overviews.ts13
-rw-r--r--server/tests/api/check-params/videos.ts243
-rw-r--r--server/tests/api/live/live-constraints.ts77
-rw-r--r--server/tests/api/live/live-permanent.ts99
-rw-r--r--server/tests/api/live/live-save-replay.ts112
-rw-r--r--server/tests/api/live/live-socket-messages.ts71
-rw-r--r--server/tests/api/live/live-views.ts51
-rw-r--r--server/tests/api/live/live.ts275
-rw-r--r--server/tests/api/moderation/abuses.ts756
-rw-r--r--server/tests/api/moderation/blocklist-notification.ts121
-rw-r--r--server/tests/api/moderation/blocklist.ts446
-rw-r--r--server/tests/api/moderation/video-blacklist.ts304
-rw-r--r--server/tests/api/notifications/admin-notifications.ts39
-rw-r--r--server/tests/api/notifications/comments-notifications.ts173
-rw-r--r--server/tests/api/notifications/moderation-notifications.ts218
-rw-r--r--server/tests/api/notifications/notifications-api.ts130
-rw-r--r--server/tests/api/notifications/user-notifications.ts115
-rw-r--r--server/tests/api/redundancy/manage-redundancy.ts193
-rw-r--r--server/tests/api/redundancy/redundancy-constraints.ts69
-rw-r--r--server/tests/api/redundancy/redundancy.ts201
-rw-r--r--server/tests/api/search/search-activitypub-video-channels.ts171
-rw-r--r--server/tests/api/search/search-activitypub-video-playlists.ts150
-rw-r--r--server/tests/api/search/search-activitypub-videos.ts124
-rw-r--r--server/tests/api/search/search-channels.ts43
-rw-r--r--server/tests/api/search/search-index.ts206
-rw-r--r--server/tests/api/search/search-playlists.ts83
-rw-r--r--server/tests/api/search/search-videos.ts331
-rw-r--r--server/tests/api/server/auto-follows.ts71
-rw-r--r--server/tests/api/server/bulk.ts123
-rw-r--r--server/tests/api/server/config.ts86
-rw-r--r--server/tests/api/server/contact-form.ts37
-rw-r--r--server/tests/api/server/email.ts111
-rw-r--r--server/tests/api/server/follow-constraints.ts178
-rw-r--r--server/tests/api/server/follows-moderation.ts109
-rw-r--r--server/tests/api/server/follows.ts689
-rw-r--r--server/tests/api/server/handle-down.ts218
-rw-r--r--server/tests/api/server/homepage.ts45
-rw-r--r--server/tests/api/server/jobs.ts61
-rw-r--r--server/tests/api/server/logs.ts88
-rw-r--r--server/tests/api/server/no-client.ts9
-rw-r--r--server/tests/api/server/plugins.ts252
-rw-r--r--server/tests/api/server/reverse-proxy.ts68
-rw-r--r--server/tests/api/server/services.ts56
-rw-r--r--server/tests/api/server/stats.ts170
-rw-r--r--server/tests/api/server/tracker.ts31
-rw-r--r--server/tests/api/users/user-subscriptions.ts230
-rw-r--r--server/tests/api/users/users-multiple-servers.ts132
-rw-r--r--server/tests/api/users/users-verification.ts107
-rw-r--r--server/tests/api/users/users.ts657
-rw-r--r--server/tests/api/videos/audio-only.ts29
-rw-r--r--server/tests/api/videos/multiple-servers.ts459
-rw-r--r--server/tests/api/videos/resumable-upload.ts48
-rw-r--r--server/tests/api/videos/single-server.ts286
-rw-r--r--server/tests/api/videos/video-captions.ts93
-rw-r--r--server/tests/api/videos/video-change-ownership.ts320
-rw-r--r--server/tests/api/videos/video-channels.ts352
-rw-r--r--server/tests/api/videos/video-comments.ts225
-rw-r--r--server/tests/api/videos/video-description.ts47
-rw-r--r--server/tests/api/videos/video-hls.ts101
-rw-r--r--server/tests/api/videos/video-imports.ts172
-rw-r--r--server/tests/api/videos/video-nsfw.ts226
-rw-r--r--server/tests/api/videos/video-playlist-thumbnails.ts144
-rw-r--r--server/tests/api/videos/video-playlists.ts727
-rw-r--r--server/tests/api/videos/video-privacy.ts157
-rw-r--r--server/tests/api/videos/video-schedule-update.ts77
-rw-r--r--server/tests/api/videos/video-transcoder.ts341
-rw-r--r--server/tests/api/videos/videos-filter.ts49
-rw-r--r--server/tests/api/videos/videos-history.ts139
-rw-r--r--server/tests/api/videos/videos-overview.ts97
-rw-r--r--server/tests/api/videos/videos-views-cleaner.ts47
104 files changed, 6986 insertions, 8799 deletions
diff --git a/server/tests/api/activitypub/cleaner.ts b/server/tests/api/activitypub/cleaner.ts
index 75ef56ce3..51cf6e599 100644
--- a/server/tests/api/activitypub/cleaner.ts
+++ b/server/tests/api/activitypub/cleaner.ts
@@ -4,24 +4,18 @@ import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5import { 5import {
6 cleanupTests, 6 cleanupTests,
7 closeAllSequelize, 7 createMultipleServers,
8 deleteAll,
9 doubleFollow, 8 doubleFollow,
10 getCount, 9 PeerTubeServer,
11 selectQuery, 10 setAccessTokensToServers,
12 setVideoField, 11 wait,
13 updateQuery, 12 waitJobs
14 wait 13} from '@shared/extra-utils'
15} from '../../../../shared/extra-utils'
16import { flushAndRunMultipleServers, ServerInfo, setAccessTokensToServers } from '../../../../shared/extra-utils/index'
17import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
18import { addVideoCommentThread, getVideoCommentThreads } from '../../../../shared/extra-utils/videos/video-comments'
19import { getVideo, rateVideo, uploadVideoAndGetId } from '../../../../shared/extra-utils/videos/videos'
20 14
21const expect = chai.expect 15const expect = chai.expect
22 16
23describe('Test AP cleaner', function () { 17describe('Test AP cleaner', function () {
24 let servers: ServerInfo[] = [] 18 let servers: PeerTubeServer[] = []
25 let videoUUID1: string 19 let videoUUID1: string
26 let videoUUID2: string 20 let videoUUID2: string
27 let videoUUID3: string 21 let videoUUID3: string
@@ -36,7 +30,7 @@ describe('Test AP cleaner', function () {
36 videos: { cleanup_remote_interactions: true } 30 videos: { cleanup_remote_interactions: true }
37 } 31 }
38 } 32 }
39 servers = await flushAndRunMultipleServers(3, config) 33 servers = await createMultipleServers(3, config)
40 34
41 // Get the access tokens 35 // Get the access tokens
42 await setAccessTokensToServers(servers) 36 await setAccessTokensToServers(servers)
@@ -52,9 +46,9 @@ describe('Test AP cleaner', function () {
52 // Create 1 comment per video 46 // Create 1 comment per video
53 // Update 1 remote URL and 1 local URL on 47 // Update 1 remote URL and 1 local URL on
54 48
55 videoUUID1 = (await uploadVideoAndGetId({ server: servers[0], videoName: 'server 1' })).uuid 49 videoUUID1 = (await servers[0].videos.quickUpload({ name: 'server 1' })).uuid
56 videoUUID2 = (await uploadVideoAndGetId({ server: servers[1], videoName: 'server 2' })).uuid 50 videoUUID2 = (await servers[1].videos.quickUpload({ name: 'server 2' })).uuid
57 videoUUID3 = (await uploadVideoAndGetId({ server: servers[2], videoName: 'server 3' })).uuid 51 videoUUID3 = (await servers[2].videos.quickUpload({ name: 'server 3' })).uuid
58 52
59 videoUUIDs = [ videoUUID1, videoUUID2, videoUUID3 ] 53 videoUUIDs = [ videoUUID1, videoUUID2, videoUUID3 ]
60 54
@@ -62,8 +56,8 @@ describe('Test AP cleaner', function () {
62 56
63 for (const server of servers) { 57 for (const server of servers) {
64 for (const uuid of videoUUIDs) { 58 for (const uuid of videoUUIDs) {
65 await rateVideo(server.url, server.accessToken, uuid, 'like') 59 await server.videos.rate({ id: uuid, rating: 'like' })
66 await addVideoCommentThread(server.url, server.accessToken, uuid, 'comment') 60 await server.comments.createThread({ videoId: uuid, text: 'comment' })
67 } 61 }
68 } 62 }
69 63
@@ -73,9 +67,10 @@ describe('Test AP cleaner', function () {
73 it('Should have the correct likes', async function () { 67 it('Should have the correct likes', async function () {
74 for (const server of servers) { 68 for (const server of servers) {
75 for (const uuid of videoUUIDs) { 69 for (const uuid of videoUUIDs) {
76 const res = await getVideo(server.url, uuid) 70 const video = await server.videos.get({ id: uuid })
77 expect(res.body.likes).to.equal(3) 71
78 expect(res.body.dislikes).to.equal(0) 72 expect(video.likes).to.equal(3)
73 expect(video.dislikes).to.equal(0)
79 } 74 }
80 } 75 }
81 }) 76 })
@@ -83,9 +78,9 @@ describe('Test AP cleaner', function () {
83 it('Should destroy server 3 internal likes and correctly clean them', async function () { 78 it('Should destroy server 3 internal likes and correctly clean them', async function () {
84 this.timeout(20000) 79 this.timeout(20000)
85 80
86 await deleteAll(servers[2].internalServerNumber, 'accountVideoRate') 81 await servers[2].sql.deleteAll('accountVideoRate')
87 for (const uuid of videoUUIDs) { 82 for (const uuid of videoUUIDs) {
88 await setVideoField(servers[2].internalServerNumber, uuid, 'likes', '0') 83 await servers[2].sql.setVideoField(uuid, 'likes', '0')
89 } 84 }
90 85
91 await wait(5000) 86 await wait(5000)
@@ -93,16 +88,16 @@ describe('Test AP cleaner', function () {
93 88
94 // Updated rates of my video 89 // Updated rates of my video
95 { 90 {
96 const res = await getVideo(servers[0].url, videoUUID1) 91 const video = await servers[0].videos.get({ id: videoUUID1 })
97 expect(res.body.likes).to.equal(2) 92 expect(video.likes).to.equal(2)
98 expect(res.body.dislikes).to.equal(0) 93 expect(video.dislikes).to.equal(0)
99 } 94 }
100 95
101 // Did not update rates of a remote video 96 // Did not update rates of a remote video
102 { 97 {
103 const res = await getVideo(servers[0].url, videoUUID2) 98 const video = await servers[0].videos.get({ id: videoUUID2 })
104 expect(res.body.likes).to.equal(3) 99 expect(video.likes).to.equal(3)
105 expect(res.body.dislikes).to.equal(0) 100 expect(video.dislikes).to.equal(0)
106 } 101 }
107 }) 102 })
108 103
@@ -111,7 +106,7 @@ describe('Test AP cleaner', function () {
111 106
112 for (const server of servers) { 107 for (const server of servers) {
113 for (const uuid of videoUUIDs) { 108 for (const uuid of videoUUIDs) {
114 await rateVideo(server.url, server.accessToken, uuid, 'dislike') 109 await server.videos.rate({ id: uuid, rating: 'dislike' })
115 } 110 }
116 } 111 }
117 112
@@ -119,9 +114,9 @@ describe('Test AP cleaner', function () {
119 114
120 for (const server of servers) { 115 for (const server of servers) {
121 for (const uuid of videoUUIDs) { 116 for (const uuid of videoUUIDs) {
122 const res = await getVideo(server.url, uuid) 117 const video = await server.videos.get({ id: uuid })
123 expect(res.body.likes).to.equal(0) 118 expect(video.likes).to.equal(0)
124 expect(res.body.dislikes).to.equal(3) 119 expect(video.dislikes).to.equal(3)
125 } 120 }
126 } 121 }
127 }) 122 })
@@ -129,10 +124,10 @@ describe('Test AP cleaner', function () {
129 it('Should destroy server 3 internal dislikes and correctly clean them', async function () { 124 it('Should destroy server 3 internal dislikes and correctly clean them', async function () {
130 this.timeout(20000) 125 this.timeout(20000)
131 126
132 await deleteAll(servers[2].internalServerNumber, 'accountVideoRate') 127 await servers[2].sql.deleteAll('accountVideoRate')
133 128
134 for (const uuid of videoUUIDs) { 129 for (const uuid of videoUUIDs) {
135 await setVideoField(servers[2].internalServerNumber, uuid, 'dislikes', '0') 130 await servers[2].sql.setVideoField(uuid, 'dislikes', '0')
136 } 131 }
137 132
138 await wait(5000) 133 await wait(5000)
@@ -140,31 +135,31 @@ describe('Test AP cleaner', function () {
140 135
141 // Updated rates of my video 136 // Updated rates of my video
142 { 137 {
143 const res = await getVideo(servers[0].url, videoUUID1) 138 const video = await servers[0].videos.get({ id: videoUUID1 })
144 expect(res.body.likes).to.equal(0) 139 expect(video.likes).to.equal(0)
145 expect(res.body.dislikes).to.equal(2) 140 expect(video.dislikes).to.equal(2)
146 } 141 }
147 142
148 // Did not update rates of a remote video 143 // Did not update rates of a remote video
149 { 144 {
150 const res = await getVideo(servers[0].url, videoUUID2) 145 const video = await servers[0].videos.get({ id: videoUUID2 })
151 expect(res.body.likes).to.equal(0) 146 expect(video.likes).to.equal(0)
152 expect(res.body.dislikes).to.equal(3) 147 expect(video.dislikes).to.equal(3)
153 } 148 }
154 }) 149 })
155 150
156 it('Should destroy server 3 internal shares and correctly clean them', async function () { 151 it('Should destroy server 3 internal shares and correctly clean them', async function () {
157 this.timeout(20000) 152 this.timeout(20000)
158 153
159 const preCount = await getCount(servers[0].internalServerNumber, 'videoShare') 154 const preCount = await servers[0].sql.getCount('videoShare')
160 expect(preCount).to.equal(6) 155 expect(preCount).to.equal(6)
161 156
162 await deleteAll(servers[2].internalServerNumber, 'videoShare') 157 await servers[2].sql.deleteAll('videoShare')
163 await wait(5000) 158 await wait(5000)
164 await waitJobs(servers) 159 await waitJobs(servers)
165 160
166 // Still 6 because we don't have remote shares on local videos 161 // Still 6 because we don't have remote shares on local videos
167 const postCount = await getCount(servers[0].internalServerNumber, 'videoShare') 162 const postCount = await servers[0].sql.getCount('videoShare')
168 expect(postCount).to.equal(6) 163 expect(postCount).to.equal(6)
169 }) 164 })
170 165
@@ -172,18 +167,18 @@ describe('Test AP cleaner', function () {
172 this.timeout(20000) 167 this.timeout(20000)
173 168
174 { 169 {
175 const res = await getVideoCommentThreads(servers[0].url, videoUUID1, 0, 5) 170 const { total } = await servers[0].comments.listThreads({ videoId: videoUUID1 })
176 expect(res.body.total).to.equal(3) 171 expect(total).to.equal(3)
177 } 172 }
178 173
179 await deleteAll(servers[2].internalServerNumber, 'videoComment') 174 await servers[2].sql.deleteAll('videoComment')
180 175
181 await wait(5000) 176 await wait(5000)
182 await waitJobs(servers) 177 await waitJobs(servers)
183 178
184 { 179 {
185 const res = await getVideoCommentThreads(servers[0].url, videoUUID1, 0, 5) 180 const { total } = await servers[0].comments.listThreads({ videoId: videoUUID1 })
186 expect(res.body.total).to.equal(2) 181 expect(total).to.equal(2)
187 } 182 }
188 }) 183 })
189 184
@@ -193,7 +188,7 @@ describe('Test AP cleaner', function () {
193 async function check (like: string, ofServerUrl: string, urlSuffix: string, remote: 'true' | 'false') { 188 async function check (like: string, ofServerUrl: string, urlSuffix: string, remote: 'true' | 'false') {
194 const query = `SELECT "videoId", "accountVideoRate".url FROM "accountVideoRate" ` + 189 const query = `SELECT "videoId", "accountVideoRate".url FROM "accountVideoRate" ` +
195 `INNER JOIN video ON "accountVideoRate"."videoId" = video.id AND remote IS ${remote} WHERE "accountVideoRate"."url" LIKE '${like}'` 190 `INNER JOIN video ON "accountVideoRate"."videoId" = video.id AND remote IS ${remote} WHERE "accountVideoRate"."url" LIKE '${like}'`
196 const res = await selectQuery(servers[0].internalServerNumber, query) 191 const res = await servers[0].sql.selectQuery(query)
197 192
198 for (const rate of res) { 193 for (const rate of res) {
199 const matcher = new RegExp(`^${ofServerUrl}/accounts/root/dislikes/\\d+${urlSuffix}$`) 194 const matcher = new RegExp(`^${ofServerUrl}/accounts/root/dislikes/\\d+${urlSuffix}$`)
@@ -222,7 +217,7 @@ describe('Test AP cleaner', function () {
222 217
223 { 218 {
224 const query = `UPDATE "accountVideoRate" SET url = url || 'stan'` 219 const query = `UPDATE "accountVideoRate" SET url = url || 'stan'`
225 await updateQuery(servers[1].internalServerNumber, query) 220 await servers[1].sql.updateQuery(query)
226 221
227 await wait(5000) 222 await wait(5000)
228 await waitJobs(servers) 223 await waitJobs(servers)
@@ -239,7 +234,7 @@ describe('Test AP cleaner', function () {
239 const query = `SELECT "videoId", "videoComment".url, uuid as "videoUUID" FROM "videoComment" ` + 234 const query = `SELECT "videoId", "videoComment".url, uuid as "videoUUID" FROM "videoComment" ` +
240 `INNER JOIN video ON "videoComment"."videoId" = video.id AND remote IS ${remote} WHERE "videoComment"."url" LIKE '${like}'` 235 `INNER JOIN video ON "videoComment"."videoId" = video.id AND remote IS ${remote} WHERE "videoComment"."url" LIKE '${like}'`
241 236
242 const res = await selectQuery(servers[0].internalServerNumber, query) 237 const res = await servers[0].sql.selectQuery(query)
243 238
244 for (const comment of res) { 239 for (const comment of res) {
245 const matcher = new RegExp(`${ofServerUrl}/videos/watch/${comment.videoUUID}/comments/\\d+${urlSuffix}`) 240 const matcher = new RegExp(`${ofServerUrl}/videos/watch/${comment.videoUUID}/comments/\\d+${urlSuffix}`)
@@ -265,7 +260,7 @@ describe('Test AP cleaner', function () {
265 260
266 { 261 {
267 const query = `UPDATE "videoComment" SET url = url || 'kyle'` 262 const query = `UPDATE "videoComment" SET url = url || 'kyle'`
268 await updateQuery(servers[1].internalServerNumber, query) 263 await servers[1].sql.updateQuery(query)
269 264
270 await wait(5000) 265 await wait(5000)
271 await waitJobs(servers) 266 await waitJobs(servers)
@@ -277,7 +272,5 @@ describe('Test AP cleaner', function () {
277 272
278 after(async function () { 273 after(async function () {
279 await cleanupTests(servers) 274 await cleanupTests(servers)
280
281 await closeAllSequelize(servers)
282 }) 275 })
283}) 276})
diff --git a/server/tests/api/activitypub/client.ts b/server/tests/api/activitypub/client.ts
index be94e219c..c3e4b7f74 100644
--- a/server/tests/api/activitypub/client.ts
+++ b/server/tests/api/activitypub/client.ts
@@ -2,24 +2,21 @@
2 2
3import 'mocha' 3import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5import { VideoPlaylistPrivacy } from '@shared/models'
6import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
7import { 5import {
8 cleanupTests, 6 cleanupTests,
9 createVideoPlaylist, 7 createMultipleServers,
10 doubleFollow, 8 doubleFollow,
11 flushAndRunMultipleServers,
12 makeActivityPubGetRequest, 9 makeActivityPubGetRequest,
13 ServerInfo, 10 PeerTubeServer,
14 setAccessTokensToServers, 11 setAccessTokensToServers,
15 setDefaultVideoChannel, 12 setDefaultVideoChannel
16 uploadVideoAndGetId 13} from '@shared/extra-utils'
17} from '../../../../shared/extra-utils' 14import { HttpStatusCode, VideoPlaylistPrivacy } from '@shared/models'
18 15
19const expect = chai.expect 16const expect = chai.expect
20 17
21describe('Test activitypub', function () { 18describe('Test activitypub', function () {
22 let servers: ServerInfo[] = [] 19 let servers: PeerTubeServer[] = []
23 let video: { id: number, uuid: string, shortUUID: string } 20 let video: { id: number, uuid: string, shortUUID: string }
24 let playlist: { id: number, uuid: string, shortUUID: string } 21 let playlist: { id: number, uuid: string, shortUUID: string }
25 22
@@ -64,19 +61,18 @@ describe('Test activitypub', function () {
64 before(async function () { 61 before(async function () {
65 this.timeout(30000) 62 this.timeout(30000)
66 63
67 servers = await flushAndRunMultipleServers(2) 64 servers = await createMultipleServers(2)
68 65
69 await setAccessTokensToServers(servers) 66 await setAccessTokensToServers(servers)
70 await setDefaultVideoChannel(servers) 67 await setDefaultVideoChannel(servers)
71 68
72 { 69 {
73 video = await uploadVideoAndGetId({ server: servers[0], videoName: 'video' }) 70 video = await servers[0].videos.quickUpload({ name: 'video' })
74 } 71 }
75 72
76 { 73 {
77 const playlistAttrs = { displayName: 'playlist', privacy: VideoPlaylistPrivacy.PUBLIC, videoChannelId: servers[0].videoChannel.id } 74 const attributes = { displayName: 'playlist', privacy: VideoPlaylistPrivacy.PUBLIC, videoChannelId: servers[0].store.channel.id }
78 const resCreate = await createVideoPlaylist({ url: servers[0].url, token: servers[0].accessToken, playlistAttrs }) 75 playlist = await servers[0].playlists.create({ attributes })
79 playlist = resCreate.body.videoPlaylist
80 } 76 }
81 77
82 await doubleFollow(servers[0], servers[1]) 78 await doubleFollow(servers[0], servers[1])
diff --git a/server/tests/api/activitypub/fetch.ts b/server/tests/api/activitypub/fetch.ts
index 35fd94eed..422a75d6e 100644
--- a/server/tests/api/activitypub/fetch.ts
+++ b/server/tests/api/activitypub/fetch.ts
@@ -1,61 +1,44 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2 2
3import 'mocha' 3import 'mocha'
4
5import {
6 cleanupTests,
7 closeAllSequelize,
8 createUser,
9 doubleFollow,
10 flushAndRunMultipleServers,
11 getVideosListSort,
12 ServerInfo,
13 setAccessTokensToServers,
14 setActorField,
15 setVideoField,
16 uploadVideo,
17 userLogin,
18 waitJobs
19} from '../../../../shared/extra-utils'
20import * as chai from 'chai' 4import * as chai from 'chai'
21import { Video } from '../../../../shared/models/videos' 5import { cleanupTests, createMultipleServers, doubleFollow, PeerTubeServer, setAccessTokensToServers, waitJobs } from '@shared/extra-utils'
22 6
23const expect = chai.expect 7const expect = chai.expect
24 8
25describe('Test ActivityPub fetcher', function () { 9describe('Test ActivityPub fetcher', function () {
26 let servers: ServerInfo[] 10 let servers: PeerTubeServer[]
27 11
28 // --------------------------------------------------------------- 12 // ---------------------------------------------------------------
29 13
30 before(async function () { 14 before(async function () {
31 this.timeout(60000) 15 this.timeout(60000)
32 16
33 servers = await flushAndRunMultipleServers(3) 17 servers = await createMultipleServers(3)
34 18
35 // Get the access tokens 19 // Get the access tokens
36 await setAccessTokensToServers(servers) 20 await setAccessTokensToServers(servers)
37 21
38 const user = { username: 'user1', password: 'password' } 22 const user = { username: 'user1', password: 'password' }
39 for (const server of servers) { 23 for (const server of servers) {
40 await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password }) 24 await server.users.create({ username: user.username, password: user.password })
41 } 25 }
42 26
43 const userAccessToken = await userLogin(servers[0], user) 27 const userAccessToken = await servers[0].login.getAccessToken(user)
44 28
45 await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video root' }) 29 await servers[0].videos.upload({ attributes: { name: 'video root' } })
46 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'bad video root' }) 30 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'bad video root' } })
47 const badVideoUUID = res.body.video.uuid 31 await servers[0].videos.upload({ token: userAccessToken, attributes: { name: 'video user' } })
48 await uploadVideo(servers[0].url, userAccessToken, { name: 'video user' })
49 32
50 { 33 {
51 const to = 'http://localhost:' + servers[0].port + '/accounts/user1' 34 const to = 'http://localhost:' + servers[0].port + '/accounts/user1'
52 const value = 'http://localhost:' + servers[1].port + '/accounts/user1' 35 const value = 'http://localhost:' + servers[1].port + '/accounts/user1'
53 await setActorField(servers[0].internalServerNumber, to, 'url', value) 36 await servers[0].sql.setActorField(to, 'url', value)
54 } 37 }
55 38
56 { 39 {
57 const value = 'http://localhost:' + servers[2].port + '/videos/watch/' + badVideoUUID 40 const value = 'http://localhost:' + servers[2].port + '/videos/watch/' + uuid
58 await setVideoField(servers[0].internalServerNumber, badVideoUUID, 'url', value) 41 await servers[0].sql.setVideoField(uuid, 'url', value)
59 } 42 }
60 }) 43 })
61 44
@@ -66,20 +49,18 @@ describe('Test ActivityPub fetcher', function () {
66 await waitJobs(servers) 49 await waitJobs(servers)
67 50
68 { 51 {
69 const res = await getVideosListSort(servers[0].url, 'createdAt') 52 const { total, data } = await servers[0].videos.list({ sort: 'createdAt' })
70 expect(res.body.total).to.equal(3)
71 53
72 const data: Video[] = res.body.data 54 expect(total).to.equal(3)
73 expect(data[0].name).to.equal('video root') 55 expect(data[0].name).to.equal('video root')
74 expect(data[1].name).to.equal('bad video root') 56 expect(data[1].name).to.equal('bad video root')
75 expect(data[2].name).to.equal('video user') 57 expect(data[2].name).to.equal('video user')
76 } 58 }
77 59
78 { 60 {
79 const res = await getVideosListSort(servers[1].url, 'createdAt') 61 const { total, data } = await servers[1].videos.list({ sort: 'createdAt' })
80 expect(res.body.total).to.equal(1)
81 62
82 const data: Video[] = res.body.data 63 expect(total).to.equal(1)
83 expect(data[0].name).to.equal('video root') 64 expect(data[0].name).to.equal('video root')
84 } 65 }
85 }) 66 })
@@ -88,7 +69,5 @@ describe('Test ActivityPub fetcher', function () {
88 this.timeout(20000) 69 this.timeout(20000)
89 70
90 await cleanupTests(servers) 71 await cleanupTests(servers)
91
92 await closeAllSequelize(servers)
93 }) 72 })
94}) 73})
diff --git a/server/tests/api/activitypub/helpers.ts b/server/tests/api/activitypub/helpers.ts
index 66d7631b7..57b1cab23 100644
--- a/server/tests/api/activitypub/helpers.ts
+++ b/server/tests/api/activitypub/helpers.ts
@@ -2,11 +2,10 @@
2 2
3import 'mocha' 3import 'mocha'
4import { expect } from 'chai' 4import { expect } from 'chai'
5import { buildRequestStub } from '../../../../shared/extra-utils/miscs/stubs'
6import { isHTTPSignatureVerified, isJsonLDSignatureVerified, parseHTTPSignature } from '../../../helpers/peertube-crypto'
7import { cloneDeep } from 'lodash' 5import { cloneDeep } from 'lodash'
6import { buildAbsoluteFixturePath, buildRequestStub } from '@shared/extra-utils'
8import { buildSignedActivity } from '../../../helpers/activitypub' 7import { buildSignedActivity } from '../../../helpers/activitypub'
9import { buildAbsoluteFixturePath } from '@shared/extra-utils' 8import { isHTTPSignatureVerified, isJsonLDSignatureVerified, parseHTTPSignature } from '../../../helpers/peertube-crypto'
10 9
11describe('Test activity pub helpers', function () { 10describe('Test activity pub helpers', function () {
12 describe('When checking the Linked Signature', function () { 11 describe('When checking the Linked Signature', function () {
diff --git a/server/tests/api/activitypub/refresher.ts b/server/tests/api/activitypub/refresher.ts
index c717f1a30..81fee0044 100644
--- a/server/tests/api/activitypub/refresher.ts
+++ b/server/tests/api/activitypub/refresher.ts
@@ -2,32 +2,20 @@
2 2
3import 'mocha' 3import 'mocha'
4import { 4import {
5 cleanupTests, closeAllSequelize, 5 cleanupTests,
6 createVideoPlaylist, 6 createMultipleServers,
7 doubleFollow, 7 doubleFollow,
8 flushAndRunMultipleServers,
9 generateUserAccessToken,
10 getVideo,
11 getVideoPlaylist,
12 killallServers, 8 killallServers,
13 reRunServer, 9 PeerTubeServer,
14 ServerInfo,
15 setAccessTokensToServers, 10 setAccessTokensToServers,
16 setActorField,
17 setDefaultVideoChannel, 11 setDefaultVideoChannel,
18 setPlaylistField,
19 setVideoField,
20 uploadVideo,
21 uploadVideoAndGetId,
22 wait, 12 wait,
23 waitJobs 13 waitJobs
24} from '../../../../shared/extra-utils' 14} from '@shared/extra-utils'
25import { getAccount } from '../../../../shared/extra-utils/users/accounts' 15import { HttpStatusCode, VideoPlaylistPrivacy } from '@shared/models'
26import { VideoPlaylistPrivacy } from '../../../../shared/models/videos'
27import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
28 16
29describe('Test AP refresher', function () { 17describe('Test AP refresher', function () {
30 let servers: ServerInfo[] = [] 18 let servers: PeerTubeServer[] = []
31 let videoUUID1: string 19 let videoUUID1: string
32 let videoUUID2: string 20 let videoUUID2: string
33 let videoUUID3: string 21 let videoUUID3: string
@@ -37,36 +25,36 @@ describe('Test AP refresher', function () {
37 before(async function () { 25 before(async function () {
38 this.timeout(60000) 26 this.timeout(60000)
39 27
40 servers = await flushAndRunMultipleServers(2, { transcoding: { enabled: false } }) 28 servers = await createMultipleServers(2, { transcoding: { enabled: false } })
41 29
42 // Get the access tokens 30 // Get the access tokens
43 await setAccessTokensToServers(servers) 31 await setAccessTokensToServers(servers)
44 await setDefaultVideoChannel(servers) 32 await setDefaultVideoChannel(servers)
45 33
46 { 34 {
47 videoUUID1 = (await uploadVideoAndGetId({ server: servers[1], videoName: 'video1' })).uuid 35 videoUUID1 = (await servers[1].videos.quickUpload({ name: 'video1' })).uuid
48 videoUUID2 = (await uploadVideoAndGetId({ server: servers[1], videoName: 'video2' })).uuid 36 videoUUID2 = (await servers[1].videos.quickUpload({ name: 'video2' })).uuid
49 videoUUID3 = (await uploadVideoAndGetId({ server: servers[1], videoName: 'video3' })).uuid 37 videoUUID3 = (await servers[1].videos.quickUpload({ name: 'video3' })).uuid
50 } 38 }
51 39
52 { 40 {
53 const a1 = await generateUserAccessToken(servers[1], 'user1') 41 const token1 = await servers[1].users.generateUserAndToken('user1')
54 await uploadVideo(servers[1].url, a1, { name: 'video4' }) 42 await servers[1].videos.upload({ token: token1, attributes: { name: 'video4' } })
55 43
56 const a2 = await generateUserAccessToken(servers[1], 'user2') 44 const token2 = await servers[1].users.generateUserAndToken('user2')
57 await uploadVideo(servers[1].url, a2, { name: 'video5' }) 45 await servers[1].videos.upload({ token: token2, attributes: { name: 'video5' } })
58 } 46 }
59 47
60 { 48 {
61 const playlistAttrs = { displayName: 'playlist1', privacy: VideoPlaylistPrivacy.PUBLIC, videoChannelId: servers[1].videoChannel.id } 49 const attributes = { displayName: 'playlist1', privacy: VideoPlaylistPrivacy.PUBLIC, videoChannelId: servers[1].store.channel.id }
62 const res = await createVideoPlaylist({ url: servers[1].url, token: servers[1].accessToken, playlistAttrs }) 50 const created = await servers[1].playlists.create({ attributes })
63 playlistUUID1 = res.body.videoPlaylist.uuid 51 playlistUUID1 = created.uuid
64 } 52 }
65 53
66 { 54 {
67 const playlistAttrs = { displayName: 'playlist2', privacy: VideoPlaylistPrivacy.PUBLIC, videoChannelId: servers[1].videoChannel.id } 55 const attributes = { displayName: 'playlist2', privacy: VideoPlaylistPrivacy.PUBLIC, videoChannelId: servers[1].store.channel.id }
68 const res = await createVideoPlaylist({ url: servers[1].url, token: servers[1].accessToken, playlistAttrs }) 56 const created = await servers[1].playlists.create({ attributes })
69 playlistUUID2 = res.body.videoPlaylist.uuid 57 playlistUUID2 = created.uuid
70 } 58 }
71 59
72 await doubleFollow(servers[0], servers[1]) 60 await doubleFollow(servers[0], servers[1])
@@ -80,34 +68,34 @@ describe('Test AP refresher', function () {
80 await wait(10000) 68 await wait(10000)
81 69
82 // Change UUID so the remote server returns a 404 70 // Change UUID so the remote server returns a 404
83 await setVideoField(servers[1].internalServerNumber, videoUUID1, 'uuid', '304afe4f-39f9-4d49-8ed7-ac57b86b174f') 71 await servers[1].sql.setVideoField(videoUUID1, 'uuid', '304afe4f-39f9-4d49-8ed7-ac57b86b174f')
84 72
85 await getVideo(servers[0].url, videoUUID1) 73 await servers[0].videos.get({ id: videoUUID1 })
86 await getVideo(servers[0].url, videoUUID2) 74 await servers[0].videos.get({ id: videoUUID2 })
87 75
88 await waitJobs(servers) 76 await waitJobs(servers)
89 77
90 await getVideo(servers[0].url, videoUUID1, HttpStatusCode.NOT_FOUND_404) 78 await servers[0].videos.get({ id: videoUUID1, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
91 await getVideo(servers[0].url, videoUUID2, HttpStatusCode.OK_200) 79 await servers[0].videos.get({ id: videoUUID2 })
92 }) 80 })
93 81
94 it('Should not update a remote video if the remote instance is down', async function () { 82 it('Should not update a remote video if the remote instance is down', async function () {
95 this.timeout(70000) 83 this.timeout(70000)
96 84
97 killallServers([ servers[1] ]) 85 await killallServers([ servers[1] ])
98 86
99 await setVideoField(servers[1].internalServerNumber, videoUUID3, 'uuid', '304afe4f-39f9-4d49-8ed7-ac57b86b174e') 87 await servers[1].sql.setVideoField(videoUUID3, 'uuid', '304afe4f-39f9-4d49-8ed7-ac57b86b174e')
100 88
101 // Video will need a refresh 89 // Video will need a refresh
102 await wait(10000) 90 await wait(10000)
103 91
104 await getVideo(servers[0].url, videoUUID3) 92 await servers[0].videos.get({ id: videoUUID3 })
105 // The refresh should fail 93 // The refresh should fail
106 await waitJobs([ servers[0] ]) 94 await waitJobs([ servers[0] ])
107 95
108 await reRunServer(servers[1]) 96 await servers[1].run()
109 97
110 await getVideo(servers[0].url, videoUUID3, HttpStatusCode.OK_200) 98 await servers[0].videos.get({ id: videoUUID3 })
111 }) 99 })
112 }) 100 })
113 101
@@ -116,19 +104,21 @@ describe('Test AP refresher', function () {
116 it('Should remove a deleted actor', async function () { 104 it('Should remove a deleted actor', async function () {
117 this.timeout(60000) 105 this.timeout(60000)
118 106
107 const command = servers[0].accounts
108
119 await wait(10000) 109 await wait(10000)
120 110
121 // Change actor name so the remote server returns a 404 111 // Change actor name so the remote server returns a 404
122 const to = 'http://localhost:' + servers[1].port + '/accounts/user2' 112 const to = 'http://localhost:' + servers[1].port + '/accounts/user2'
123 await setActorField(servers[1].internalServerNumber, to, 'preferredUsername', 'toto') 113 await servers[1].sql.setActorField(to, 'preferredUsername', 'toto')
124 114
125 await getAccount(servers[0].url, 'user1@localhost:' + servers[1].port) 115 await command.get({ accountName: 'user1@localhost:' + servers[1].port })
126 await getAccount(servers[0].url, 'user2@localhost:' + servers[1].port) 116 await command.get({ accountName: 'user2@localhost:' + servers[1].port })
127 117
128 await waitJobs(servers) 118 await waitJobs(servers)
129 119
130 await getAccount(servers[0].url, 'user1@localhost:' + servers[1].port, HttpStatusCode.OK_200) 120 await command.get({ accountName: 'user1@localhost:' + servers[1].port, expectedStatus: HttpStatusCode.OK_200 })
131 await getAccount(servers[0].url, 'user2@localhost:' + servers[1].port, HttpStatusCode.NOT_FOUND_404) 121 await command.get({ accountName: 'user2@localhost:' + servers[1].port, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
132 }) 122 })
133 }) 123 })
134 124
@@ -140,15 +130,15 @@ describe('Test AP refresher', function () {
140 await wait(10000) 130 await wait(10000)
141 131
142 // Change UUID so the remote server returns a 404 132 // Change UUID so the remote server returns a 404
143 await setPlaylistField(servers[1].internalServerNumber, playlistUUID2, 'uuid', '304afe4f-39f9-4d49-8ed7-ac57b86b178e') 133 await servers[1].sql.setPlaylistField(playlistUUID2, 'uuid', '304afe4f-39f9-4d49-8ed7-ac57b86b178e')
144 134
145 await getVideoPlaylist(servers[0].url, playlistUUID1) 135 await servers[0].playlists.get({ playlistId: playlistUUID1 })
146 await getVideoPlaylist(servers[0].url, playlistUUID2) 136 await servers[0].playlists.get({ playlistId: playlistUUID2 })
147 137
148 await waitJobs(servers) 138 await waitJobs(servers)
149 139
150 await getVideoPlaylist(servers[0].url, playlistUUID1, HttpStatusCode.OK_200) 140 await servers[0].playlists.get({ playlistId: playlistUUID1, expectedStatus: HttpStatusCode.OK_200 })
151 await getVideoPlaylist(servers[0].url, playlistUUID2, HttpStatusCode.NOT_FOUND_404) 141 await servers[0].playlists.get({ playlistId: playlistUUID2, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
152 }) 142 })
153 }) 143 })
154 144
@@ -156,7 +146,5 @@ describe('Test AP refresher', function () {
156 this.timeout(10000) 146 this.timeout(10000)
157 147
158 await cleanupTests(servers) 148 await cleanupTests(servers)
159
160 await closeAllSequelize(servers)
161 }) 149 })
162}) 150})
diff --git a/server/tests/api/activitypub/security.ts b/server/tests/api/activitypub/security.ts
index 61db272f6..94d946563 100644
--- a/server/tests/api/activitypub/security.ts
+++ b/server/tests/api/activitypub/security.ts
@@ -2,45 +2,35 @@
2 2
3import 'mocha' 3import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5import { activityPubContextify, buildSignedActivity } from '@server/helpers/activitypub'
5import { buildDigest } from '@server/helpers/peertube-crypto' 6import { buildDigest } from '@server/helpers/peertube-crypto'
6import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes' 7import { HTTP_SIGNATURE } from '@server/initializers/constants'
7import { 8import { buildGlobalHeaders } from '@server/lib/job-queue/handlers/utils/activitypub-http-utils'
8 buildAbsoluteFixturePath, 9import { buildAbsoluteFixturePath, cleanupTests, createMultipleServers, killallServers, PeerTubeServer, wait } from '@shared/extra-utils'
9 cleanupTests, 10import { makeFollowRequest, makePOSTAPRequest } from '@shared/extra-utils/requests/activitypub'
10 closeAllSequelize, 11import { HttpStatusCode } from '@shared/models'
11 flushAndRunMultipleServers,
12 killallServers,
13 reRunServer,
14 ServerInfo,
15 setActorField,
16 wait
17} from '../../../../shared/extra-utils'
18import { makeFollowRequest, makePOSTAPRequest } from '../../../../shared/extra-utils/requests/activitypub'
19import { activityPubContextify, buildSignedActivity } from '../../../helpers/activitypub'
20import { HTTP_SIGNATURE } from '../../../initializers/constants'
21import { buildGlobalHeaders } from '../../../lib/job-queue/handlers/utils/activitypub-http-utils'
22 12
23const expect = chai.expect 13const expect = chai.expect
24 14
25function setKeysOfServer (onServer: ServerInfo, ofServer: ServerInfo, publicKey: string, privateKey: string) { 15function setKeysOfServer (onServer: PeerTubeServer, ofServer: PeerTubeServer, publicKey: string, privateKey: string) {
26 const url = 'http://localhost:' + ofServer.port + '/accounts/peertube' 16 const url = 'http://localhost:' + ofServer.port + '/accounts/peertube'
27 17
28 return Promise.all([ 18 return Promise.all([
29 setActorField(onServer.internalServerNumber, url, 'publicKey', publicKey), 19 onServer.sql.setActorField(url, 'publicKey', publicKey),
30 setActorField(onServer.internalServerNumber, url, 'privateKey', privateKey) 20 onServer.sql.setActorField(url, 'privateKey', privateKey)
31 ]) 21 ])
32} 22}
33 23
34function setUpdatedAtOfServer (onServer: ServerInfo, ofServer: ServerInfo, updatedAt: string) { 24function setUpdatedAtOfServer (onServer: PeerTubeServer, ofServer: PeerTubeServer, updatedAt: string) {
35 const url = 'http://localhost:' + ofServer.port + '/accounts/peertube' 25 const url = 'http://localhost:' + ofServer.port + '/accounts/peertube'
36 26
37 return Promise.all([ 27 return Promise.all([
38 setActorField(onServer.internalServerNumber, url, 'createdAt', updatedAt), 28 onServer.sql.setActorField(url, 'createdAt', updatedAt),
39 setActorField(onServer.internalServerNumber, url, 'updatedAt', updatedAt) 29 onServer.sql.setActorField(url, 'updatedAt', updatedAt)
40 ]) 30 ])
41} 31}
42 32
43function getAnnounceWithoutContext (server: ServerInfo) { 33function getAnnounceWithoutContext (server: PeerTubeServer) {
44 const json = require(buildAbsoluteFixturePath('./ap-json/peertube/announce-without-context.json')) 34 const json = require(buildAbsoluteFixturePath('./ap-json/peertube/announce-without-context.json'))
45 const result: typeof json = {} 35 const result: typeof json = {}
46 36
@@ -56,7 +46,7 @@ function getAnnounceWithoutContext (server: ServerInfo) {
56} 46}
57 47
58describe('Test ActivityPub security', function () { 48describe('Test ActivityPub security', function () {
59 let servers: ServerInfo[] 49 let servers: PeerTubeServer[]
60 let url: string 50 let url: string
61 51
62 const keys = require(buildAbsoluteFixturePath('./ap-json/peertube/keys.json')) 52 const keys = require(buildAbsoluteFixturePath('./ap-json/peertube/keys.json'))
@@ -74,7 +64,7 @@ describe('Test ActivityPub security', function () {
74 before(async function () { 64 before(async function () {
75 this.timeout(60000) 65 this.timeout(60000)
76 66
77 servers = await flushAndRunMultipleServers(3) 67 servers = await createMultipleServers(3)
78 68
79 url = servers[0].url + '/inbox' 69 url = servers[0].url + '/inbox'
80 70
@@ -173,8 +163,8 @@ describe('Test ActivityPub security', function () {
173 await setUpdatedAtOfServer(servers[0], servers[1], '2015-07-17 22:00:00+00') 163 await setUpdatedAtOfServer(servers[0], servers[1], '2015-07-17 22:00:00+00')
174 164
175 // Invalid peertube actor cache 165 // Invalid peertube actor cache
176 killallServers([ servers[1] ]) 166 await killallServers([ servers[1] ])
177 await reRunServer(servers[1]) 167 await servers[1].run()
178 168
179 const body = activityPubContextify(getAnnounceWithoutContext(servers[1])) 169 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]))
180 const headers = buildGlobalHeaders(body) 170 const headers = buildGlobalHeaders(body)
@@ -294,7 +284,5 @@ describe('Test ActivityPub security', function () {
294 this.timeout(10000) 284 this.timeout(10000)
295 285
296 await cleanupTests(servers) 286 await cleanupTests(servers)
297
298 await closeAllSequelize(servers)
299 }) 287 })
300}) 288})
diff --git a/server/tests/api/check-params/abuses.ts b/server/tests/api/check-params/abuses.ts
index 2054776cc..72f2cbd8f 100644
--- a/server/tests/api/check-params/abuses.ts
+++ b/server/tests/api/check-params/abuses.ts
@@ -1,66 +1,49 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2 2
3import 'mocha' 3import 'mocha'
4import { AbuseCreate, AbuseState } from '@shared/models'
5import { 4import {
6 addAbuseMessage, 5 AbusesCommand,
6 checkBadCountPagination,
7 checkBadSortPagination,
8 checkBadStartPagination,
7 cleanupTests, 9 cleanupTests,
8 createUser, 10 createSingleServer,
9 deleteAbuse,
10 deleteAbuseMessage,
11 doubleFollow, 11 doubleFollow,
12 flushAndRunServer,
13 generateUserAccessToken,
14 getAdminAbusesList,
15 getVideoIdFromUUID,
16 listAbuseMessages,
17 makeGetRequest, 12 makeGetRequest,
18 makePostBodyRequest, 13 makePostBodyRequest,
19 reportAbuse, 14 PeerTubeServer,
20 ServerInfo,
21 setAccessTokensToServers, 15 setAccessTokensToServers,
22 updateAbuse,
23 uploadVideo,
24 userLogin,
25 waitJobs 16 waitJobs
26} from '../../../../shared/extra-utils' 17} from '@shared/extra-utils'
27import { 18import { AbuseCreate, AbuseState, HttpStatusCode } from '@shared/models'
28 checkBadCountPagination,
29 checkBadSortPagination,
30 checkBadStartPagination
31} from '../../../../shared/extra-utils/requests/check-api-params'
32import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
33 19
34describe('Test abuses API validators', function () { 20describe('Test abuses API validators', function () {
35 const basePath = '/api/v1/abuses/' 21 const basePath = '/api/v1/abuses/'
36 22
37 let server: ServerInfo 23 let server: PeerTubeServer
38 24
39 let userAccessToken = '' 25 let userToken = ''
40 let userAccessToken2 = '' 26 let userToken2 = ''
41 let abuseId: number 27 let abuseId: number
42 let messageId: number 28 let messageId: number
43 29
30 let command: AbusesCommand
31
44 // --------------------------------------------------------------- 32 // ---------------------------------------------------------------
45 33
46 before(async function () { 34 before(async function () {
47 this.timeout(30000) 35 this.timeout(30000)
48 36
49 server = await flushAndRunServer(1) 37 server = await createSingleServer(1)
50 38
51 await setAccessTokensToServers([ server ]) 39 await setAccessTokensToServers([ server ])
52 40
53 const username = 'user1' 41 userToken = await server.users.generateUserAndToken('user_1')
54 const password = 'my super password' 42 userToken2 = await server.users.generateUserAndToken('user_2')
55 await createUser({ url: server.url, accessToken: server.accessToken, username: username, password: password })
56 userAccessToken = await userLogin(server, { username, password })
57 43
58 { 44 server.store.video = await server.videos.upload()
59 userAccessToken2 = await generateUserAccessToken(server, 'user_2')
60 }
61 45
62 const res = await uploadVideo(server.url, server.accessToken, {}) 46 command = server.abuses
63 server.video = res.body.video
64 }) 47 })
65 48
66 describe('When listing abuses for admins', function () { 49 describe('When listing abuses for admins', function () {
@@ -82,7 +65,7 @@ describe('Test abuses API validators', function () {
82 await makeGetRequest({ 65 await makeGetRequest({
83 url: server.url, 66 url: server.url,
84 path, 67 path,
85 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 68 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
86 }) 69 })
87 }) 70 })
88 71
@@ -90,8 +73,8 @@ describe('Test abuses API validators', function () {
90 await makeGetRequest({ 73 await makeGetRequest({
91 url: server.url, 74 url: server.url,
92 path, 75 path,
93 token: userAccessToken, 76 token: userToken,
94 statusCodeExpected: HttpStatusCode.FORBIDDEN_403 77 expectedStatus: HttpStatusCode.FORBIDDEN_403
95 }) 78 })
96 }) 79 })
97 80
@@ -126,7 +109,7 @@ describe('Test abuses API validators', function () {
126 videoIs: 'deleted' 109 videoIs: 'deleted'
127 } 110 }
128 111
129 await makeGetRequest({ url: server.url, path, token: server.accessToken, query, statusCodeExpected: HttpStatusCode.OK_200 }) 112 await makeGetRequest({ url: server.url, path, token: server.accessToken, query, expectedStatus: HttpStatusCode.OK_200 })
130 }) 113 })
131 }) 114 })
132 115
@@ -134,32 +117,32 @@ describe('Test abuses API validators', function () {
134 const path = '/api/v1/users/me/abuses' 117 const path = '/api/v1/users/me/abuses'
135 118
136 it('Should fail with a bad start pagination', async function () { 119 it('Should fail with a bad start pagination', async function () {
137 await checkBadStartPagination(server.url, path, userAccessToken) 120 await checkBadStartPagination(server.url, path, userToken)
138 }) 121 })
139 122
140 it('Should fail with a bad count pagination', async function () { 123 it('Should fail with a bad count pagination', async function () {
141 await checkBadCountPagination(server.url, path, userAccessToken) 124 await checkBadCountPagination(server.url, path, userToken)
142 }) 125 })
143 126
144 it('Should fail with an incorrect sort', async function () { 127 it('Should fail with an incorrect sort', async function () {
145 await checkBadSortPagination(server.url, path, userAccessToken) 128 await checkBadSortPagination(server.url, path, userToken)
146 }) 129 })
147 130
148 it('Should fail with a non authenticated user', async function () { 131 it('Should fail with a non authenticated user', async function () {
149 await makeGetRequest({ 132 await makeGetRequest({
150 url: server.url, 133 url: server.url,
151 path, 134 path,
152 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 135 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
153 }) 136 })
154 }) 137 })
155 138
156 it('Should fail with a bad id filter', async function () { 139 it('Should fail with a bad id filter', async function () {
157 await makeGetRequest({ url: server.url, path, token: userAccessToken, query: { id: 'toto' } }) 140 await makeGetRequest({ url: server.url, path, token: userToken, query: { id: 'toto' } })
158 }) 141 })
159 142
160 it('Should fail with a bad state filter', async function () { 143 it('Should fail with a bad state filter', async function () {
161 await makeGetRequest({ url: server.url, path, token: userAccessToken, query: { state: 'toto' } }) 144 await makeGetRequest({ url: server.url, path, token: userToken, query: { state: 'toto' } })
162 await makeGetRequest({ url: server.url, path, token: userAccessToken, query: { state: 0 } }) 145 await makeGetRequest({ url: server.url, path, token: userToken, query: { state: 0 } })
163 }) 146 })
164 147
165 it('Should succeed with the correct params', async function () { 148 it('Should succeed with the correct params', async function () {
@@ -168,7 +151,7 @@ describe('Test abuses API validators', function () {
168 state: 2 151 state: 2
169 } 152 }
170 153
171 await makeGetRequest({ url: server.url, path, token: userAccessToken, query, statusCodeExpected: HttpStatusCode.OK_200 }) 154 await makeGetRequest({ url: server.url, path, token: userToken, query, expectedStatus: HttpStatusCode.OK_200 })
172 }) 155 })
173 }) 156 })
174 157
@@ -177,12 +160,12 @@ describe('Test abuses API validators', function () {
177 160
178 it('Should fail with nothing', async function () { 161 it('Should fail with nothing', async function () {
179 const fields = {} 162 const fields = {}
180 await makePostBodyRequest({ url: server.url, path, token: userAccessToken, fields }) 163 await makePostBodyRequest({ url: server.url, path, token: userToken, fields })
181 }) 164 })
182 165
183 it('Should fail with a wrong video', async function () { 166 it('Should fail with a wrong video', async function () {
184 const fields = { video: { id: 'blabla' }, reason: 'my super reason' } 167 const fields = { video: { id: 'blabla' }, reason: 'my super reason' }
185 await makePostBodyRequest({ url: server.url, path: path, token: userAccessToken, fields }) 168 await makePostBodyRequest({ url: server.url, path: path, token: userToken, fields })
186 }) 169 })
187 170
188 it('Should fail with an unknown video', async function () { 171 it('Should fail with an unknown video', async function () {
@@ -190,15 +173,15 @@ describe('Test abuses API validators', function () {
190 await makePostBodyRequest({ 173 await makePostBodyRequest({
191 url: server.url, 174 url: server.url,
192 path, 175 path,
193 token: userAccessToken, 176 token: userToken,
194 fields, 177 fields,
195 statusCodeExpected: HttpStatusCode.NOT_FOUND_404 178 expectedStatus: HttpStatusCode.NOT_FOUND_404
196 }) 179 })
197 }) 180 })
198 181
199 it('Should fail with a wrong comment', async function () { 182 it('Should fail with a wrong comment', async function () {
200 const fields = { comment: { id: 'blabla' }, reason: 'my super reason' } 183 const fields = { comment: { id: 'blabla' }, reason: 'my super reason' }
201 await makePostBodyRequest({ url: server.url, path: path, token: userAccessToken, fields }) 184 await makePostBodyRequest({ url: server.url, path: path, token: userToken, fields })
202 }) 185 })
203 186
204 it('Should fail with an unknown comment', async function () { 187 it('Should fail with an unknown comment', async function () {
@@ -206,15 +189,15 @@ describe('Test abuses API validators', function () {
206 await makePostBodyRequest({ 189 await makePostBodyRequest({
207 url: server.url, 190 url: server.url,
208 path, 191 path,
209 token: userAccessToken, 192 token: userToken,
210 fields, 193 fields,
211 statusCodeExpected: HttpStatusCode.NOT_FOUND_404 194 expectedStatus: HttpStatusCode.NOT_FOUND_404
212 }) 195 })
213 }) 196 })
214 197
215 it('Should fail with a wrong account', async function () { 198 it('Should fail with a wrong account', async function () {
216 const fields = { account: { id: 'blabla' }, reason: 'my super reason' } 199 const fields = { account: { id: 'blabla' }, reason: 'my super reason' }
217 await makePostBodyRequest({ url: server.url, path: path, token: userAccessToken, fields }) 200 await makePostBodyRequest({ url: server.url, path: path, token: userToken, fields })
218 }) 201 })
219 202
220 it('Should fail with an unknown account', async function () { 203 it('Should fail with an unknown account', async function () {
@@ -222,9 +205,9 @@ describe('Test abuses API validators', function () {
222 await makePostBodyRequest({ 205 await makePostBodyRequest({
223 url: server.url, 206 url: server.url,
224 path, 207 path,
225 token: userAccessToken, 208 token: userToken,
226 fields, 209 fields,
227 statusCodeExpected: HttpStatusCode.NOT_FOUND_404 210 expectedStatus: HttpStatusCode.NOT_FOUND_404
228 }) 211 })
229 }) 212 })
230 213
@@ -233,65 +216,65 @@ describe('Test abuses API validators', function () {
233 await makePostBodyRequest({ 216 await makePostBodyRequest({
234 url: server.url, 217 url: server.url,
235 path, 218 path,
236 token: userAccessToken, 219 token: userToken,
237 fields, 220 fields,
238 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 221 expectedStatus: HttpStatusCode.BAD_REQUEST_400
239 }) 222 })
240 }) 223 })
241 224
242 it('Should fail with a non authenticated user', async function () { 225 it('Should fail with a non authenticated user', async function () {
243 const fields = { video: { id: server.video.id }, reason: 'my super reason' } 226 const fields = { video: { id: server.store.video.id }, reason: 'my super reason' }
244 227
245 await makePostBodyRequest({ url: server.url, path, token: 'hello', fields, statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 }) 228 await makePostBodyRequest({ url: server.url, path, token: 'hello', fields, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
246 }) 229 })
247 230
248 it('Should fail with a reason too short', async function () { 231 it('Should fail with a reason too short', async function () {
249 const fields = { video: { id: server.video.id }, reason: 'h' } 232 const fields = { video: { id: server.store.video.id }, reason: 'h' }
250 233
251 await makePostBodyRequest({ url: server.url, path, token: userAccessToken, fields }) 234 await makePostBodyRequest({ url: server.url, path, token: userToken, fields })
252 }) 235 })
253 236
254 it('Should fail with a too big reason', async function () { 237 it('Should fail with a too big reason', async function () {
255 const fields = { video: { id: server.video.id }, reason: 'super'.repeat(605) } 238 const fields = { video: { id: server.store.video.id }, reason: 'super'.repeat(605) }
256 239
257 await makePostBodyRequest({ url: server.url, path, token: userAccessToken, fields }) 240 await makePostBodyRequest({ url: server.url, path, token: userToken, fields })
258 }) 241 })
259 242
260 it('Should succeed with the correct parameters (basic)', async function () { 243 it('Should succeed with the correct parameters (basic)', async function () {
261 const fields: AbuseCreate = { video: { id: server.video.shortUUID }, reason: 'my super reason' } 244 const fields: AbuseCreate = { video: { id: server.store.video.shortUUID }, reason: 'my super reason' }
262 245
263 const res = await makePostBodyRequest({ 246 const res = await makePostBodyRequest({
264 url: server.url, 247 url: server.url,
265 path, 248 path,
266 token: userAccessToken, 249 token: userToken,
267 fields, 250 fields,
268 statusCodeExpected: HttpStatusCode.OK_200 251 expectedStatus: HttpStatusCode.OK_200
269 }) 252 })
270 abuseId = res.body.abuse.id 253 abuseId = res.body.abuse.id
271 }) 254 })
272 255
273 it('Should fail with a wrong predefined reason', async function () { 256 it('Should fail with a wrong predefined reason', async function () {
274 const fields = { video: { id: server.video.id }, reason: 'my super reason', predefinedReasons: [ 'wrongPredefinedReason' ] } 257 const fields = { video: { id: server.store.video.id }, reason: 'my super reason', predefinedReasons: [ 'wrongPredefinedReason' ] }
275 258
276 await makePostBodyRequest({ url: server.url, path, token: userAccessToken, fields }) 259 await makePostBodyRequest({ url: server.url, path, token: userToken, fields })
277 }) 260 })
278 261
279 it('Should fail with negative timestamps', async function () { 262 it('Should fail with negative timestamps', async function () {
280 const fields = { video: { id: server.video.id, startAt: -1 }, reason: 'my super reason' } 263 const fields = { video: { id: server.store.video.id, startAt: -1 }, reason: 'my super reason' }
281 264
282 await makePostBodyRequest({ url: server.url, path, token: userAccessToken, fields }) 265 await makePostBodyRequest({ url: server.url, path, token: userToken, fields })
283 }) 266 })
284 267
285 it('Should fail mith misordered startAt/endAt', async function () { 268 it('Should fail mith misordered startAt/endAt', async function () {
286 const fields = { video: { id: server.video.id, startAt: 5, endAt: 1 }, reason: 'my super reason' } 269 const fields = { video: { id: server.store.video.id, startAt: 5, endAt: 1 }, reason: 'my super reason' }
287 270
288 await makePostBodyRequest({ url: server.url, path, token: userAccessToken, fields }) 271 await makePostBodyRequest({ url: server.url, path, token: userToken, fields })
289 }) 272 })
290 273
291 it('Should succeed with the corret parameters (advanced)', async function () { 274 it('Should succeed with the corret parameters (advanced)', async function () {
292 const fields: AbuseCreate = { 275 const fields: AbuseCreate = {
293 video: { 276 video: {
294 id: server.video.id, 277 id: server.store.video.id,
295 startAt: 1, 278 startAt: 1,
296 endAt: 5 279 endAt: 5
297 }, 280 },
@@ -299,37 +282,37 @@ describe('Test abuses API validators', function () {
299 predefinedReasons: [ 'serverRules' ] 282 predefinedReasons: [ 'serverRules' ]
300 } 283 }
301 284
302 await makePostBodyRequest({ url: server.url, path, token: userAccessToken, fields, statusCodeExpected: HttpStatusCode.OK_200 }) 285 await makePostBodyRequest({ url: server.url, path, token: userToken, fields, expectedStatus: HttpStatusCode.OK_200 })
303 }) 286 })
304 }) 287 })
305 288
306 describe('When updating an abuse', function () { 289 describe('When updating an abuse', function () {
307 290
308 it('Should fail with a non authenticated user', async function () { 291 it('Should fail with a non authenticated user', async function () {
309 await updateAbuse(server.url, 'blabla', abuseId, {}, HttpStatusCode.UNAUTHORIZED_401) 292 await command.update({ token: 'blabla', abuseId, body: {}, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
310 }) 293 })
311 294
312 it('Should fail with a non admin user', async function () { 295 it('Should fail with a non admin user', async function () {
313 await updateAbuse(server.url, userAccessToken, abuseId, {}, HttpStatusCode.FORBIDDEN_403) 296 await command.update({ token: userToken, abuseId, body: {}, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
314 }) 297 })
315 298
316 it('Should fail with a bad abuse id', async function () { 299 it('Should fail with a bad abuse id', async function () {
317 await updateAbuse(server.url, server.accessToken, 45, {}, HttpStatusCode.NOT_FOUND_404) 300 await command.update({ abuseId: 45, body: {}, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
318 }) 301 })
319 302
320 it('Should fail with a bad state', async function () { 303 it('Should fail with a bad state', async function () {
321 const body = { state: 5 } 304 const body = { state: 5 }
322 await updateAbuse(server.url, server.accessToken, abuseId, body, HttpStatusCode.BAD_REQUEST_400) 305 await command.update({ abuseId, body, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
323 }) 306 })
324 307
325 it('Should fail with a bad moderation comment', async function () { 308 it('Should fail with a bad moderation comment', async function () {
326 const body = { moderationComment: 'b'.repeat(3001) } 309 const body = { moderationComment: 'b'.repeat(3001) }
327 await updateAbuse(server.url, server.accessToken, abuseId, body, HttpStatusCode.BAD_REQUEST_400) 310 await command.update({ abuseId, body, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
328 }) 311 })
329 312
330 it('Should succeed with the correct params', async function () { 313 it('Should succeed with the correct params', async function () {
331 const body = { state: AbuseState.ACCEPTED } 314 const body = { state: AbuseState.ACCEPTED }
332 await updateAbuse(server.url, server.accessToken, abuseId, body) 315 await command.update({ abuseId, body })
333 }) 316 })
334 }) 317 })
335 318
@@ -337,23 +320,23 @@ describe('Test abuses API validators', function () {
337 const message = 'my super message' 320 const message = 'my super message'
338 321
339 it('Should fail with an invalid abuse id', async function () { 322 it('Should fail with an invalid abuse id', async function () {
340 await addAbuseMessage(server.url, userAccessToken2, 888, message, HttpStatusCode.NOT_FOUND_404) 323 await command.addMessage({ token: userToken2, abuseId: 888, message, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
341 }) 324 })
342 325
343 it('Should fail with a non authenticated user', async function () { 326 it('Should fail with a non authenticated user', async function () {
344 await addAbuseMessage(server.url, 'fake_token', abuseId, message, HttpStatusCode.UNAUTHORIZED_401) 327 await command.addMessage({ token: 'fake_token', abuseId, message, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
345 }) 328 })
346 329
347 it('Should fail with an invalid logged in user', async function () { 330 it('Should fail with an invalid logged in user', async function () {
348 await addAbuseMessage(server.url, userAccessToken2, abuseId, message, HttpStatusCode.FORBIDDEN_403) 331 await command.addMessage({ token: userToken2, abuseId, message, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
349 }) 332 })
350 333
351 it('Should fail with an invalid message', async function () { 334 it('Should fail with an invalid message', async function () {
352 await addAbuseMessage(server.url, userAccessToken, abuseId, 'a'.repeat(5000), HttpStatusCode.BAD_REQUEST_400) 335 await command.addMessage({ token: userToken, abuseId, message: 'a'.repeat(5000), expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
353 }) 336 })
354 337
355 it('Should suceed with the correct params', async function () { 338 it('Should suceed with the correct params', async function () {
356 const res = await addAbuseMessage(server.url, userAccessToken, abuseId, message) 339 const res = await command.addMessage({ token: userToken, abuseId, message })
357 messageId = res.body.abuseMessage.id 340 messageId = res.body.abuseMessage.id
358 }) 341 })
359 }) 342 })
@@ -361,96 +344,90 @@ describe('Test abuses API validators', function () {
361 describe('When listing abuse messages', function () { 344 describe('When listing abuse messages', function () {
362 345
363 it('Should fail with an invalid abuse id', async function () { 346 it('Should fail with an invalid abuse id', async function () {
364 await listAbuseMessages(server.url, userAccessToken, 888, HttpStatusCode.NOT_FOUND_404) 347 await command.listMessages({ token: userToken, abuseId: 888, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
365 }) 348 })
366 349
367 it('Should fail with a non authenticated user', async function () { 350 it('Should fail with a non authenticated user', async function () {
368 await listAbuseMessages(server.url, 'fake_token', abuseId, HttpStatusCode.UNAUTHORIZED_401) 351 await command.listMessages({ token: 'fake_token', abuseId, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
369 }) 352 })
370 353
371 it('Should fail with an invalid logged in user', async function () { 354 it('Should fail with an invalid logged in user', async function () {
372 await listAbuseMessages(server.url, userAccessToken2, abuseId, HttpStatusCode.FORBIDDEN_403) 355 await command.listMessages({ token: userToken2, abuseId, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
373 }) 356 })
374 357
375 it('Should succeed with the correct params', async function () { 358 it('Should succeed with the correct params', async function () {
376 await listAbuseMessages(server.url, userAccessToken, abuseId) 359 await command.listMessages({ token: userToken, abuseId })
377 }) 360 })
378 }) 361 })
379 362
380 describe('When deleting an abuse message', function () { 363 describe('When deleting an abuse message', function () {
381
382 it('Should fail with an invalid abuse id', async function () { 364 it('Should fail with an invalid abuse id', async function () {
383 await deleteAbuseMessage(server.url, userAccessToken, 888, messageId, HttpStatusCode.NOT_FOUND_404) 365 await command.deleteMessage({ token: userToken, abuseId: 888, messageId, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
384 }) 366 })
385 367
386 it('Should fail with an invalid message id', async function () { 368 it('Should fail with an invalid message id', async function () {
387 await deleteAbuseMessage(server.url, userAccessToken, abuseId, 888, HttpStatusCode.NOT_FOUND_404) 369 await command.deleteMessage({ token: userToken, abuseId, messageId: 888, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
388 }) 370 })
389 371
390 it('Should fail with a non authenticated user', async function () { 372 it('Should fail with a non authenticated user', async function () {
391 await deleteAbuseMessage(server.url, 'fake_token', abuseId, messageId, HttpStatusCode.UNAUTHORIZED_401) 373 await command.deleteMessage({ token: 'fake_token', abuseId, messageId, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
392 }) 374 })
393 375
394 it('Should fail with an invalid logged in user', async function () { 376 it('Should fail with an invalid logged in user', async function () {
395 await deleteAbuseMessage(server.url, userAccessToken2, abuseId, messageId, HttpStatusCode.FORBIDDEN_403) 377 await command.deleteMessage({ token: userToken2, abuseId, messageId, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
396 }) 378 })
397 379
398 it('Should succeed with the correct params', async function () { 380 it('Should succeed with the correct params', async function () {
399 await deleteAbuseMessage(server.url, userAccessToken, abuseId, messageId) 381 await command.deleteMessage({ token: userToken, abuseId, messageId })
400 }) 382 })
401 }) 383 })
402 384
403 describe('When deleting a video abuse', function () { 385 describe('When deleting a video abuse', function () {
404 386
405 it('Should fail with a non authenticated user', async function () { 387 it('Should fail with a non authenticated user', async function () {
406 await deleteAbuse(server.url, 'blabla', abuseId, HttpStatusCode.UNAUTHORIZED_401) 388 await command.delete({ token: 'blabla', abuseId, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
407 }) 389 })
408 390
409 it('Should fail with a non admin user', async function () { 391 it('Should fail with a non admin user', async function () {
410 await deleteAbuse(server.url, userAccessToken, abuseId, HttpStatusCode.FORBIDDEN_403) 392 await command.delete({ token: userToken, abuseId, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
411 }) 393 })
412 394
413 it('Should fail with a bad abuse id', async function () { 395 it('Should fail with a bad abuse id', async function () {
414 await deleteAbuse(server.url, server.accessToken, 45, HttpStatusCode.NOT_FOUND_404) 396 await command.delete({ abuseId: 45, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
415 }) 397 })
416 398
417 it('Should succeed with the correct params', async function () { 399 it('Should succeed with the correct params', async function () {
418 await deleteAbuse(server.url, server.accessToken, abuseId) 400 await command.delete({ abuseId })
419 }) 401 })
420 }) 402 })
421 403
422 describe('When trying to manage messages of a remote abuse', function () { 404 describe('When trying to manage messages of a remote abuse', function () {
423 let remoteAbuseId: number 405 let remoteAbuseId: number
424 let anotherServer: ServerInfo 406 let anotherServer: PeerTubeServer
425 407
426 before(async function () { 408 before(async function () {
427 this.timeout(50000) 409 this.timeout(50000)
428 410
429 anotherServer = await flushAndRunServer(2) 411 anotherServer = await createSingleServer(2)
430 await setAccessTokensToServers([ anotherServer ]) 412 await setAccessTokensToServers([ anotherServer ])
431 413
432 await doubleFollow(anotherServer, server) 414 await doubleFollow(anotherServer, server)
433 415
434 const server2VideoId = await getVideoIdFromUUID(anotherServer.url, server.video.uuid) 416 const server2VideoId = await anotherServer.videos.getId({ uuid: server.store.video.uuid })
435 await reportAbuse({ 417 await anotherServer.abuses.report({ reason: 'remote server', videoId: server2VideoId })
436 url: anotherServer.url,
437 token: anotherServer.accessToken,
438 reason: 'remote server',
439 videoId: server2VideoId
440 })
441 418
442 await waitJobs([ server, anotherServer ]) 419 await waitJobs([ server, anotherServer ])
443 420
444 const res = await getAdminAbusesList({ url: server.url, token: server.accessToken, sort: '-createdAt' }) 421 const body = await command.getAdminList({ sort: '-createdAt' })
445 remoteAbuseId = res.body.data[0].id 422 remoteAbuseId = body.data[0].id
446 }) 423 })
447 424
448 it('Should fail when listing abuse messages of a remote abuse', async function () { 425 it('Should fail when listing abuse messages of a remote abuse', async function () {
449 await listAbuseMessages(server.url, server.accessToken, remoteAbuseId, HttpStatusCode.BAD_REQUEST_400) 426 await command.listMessages({ abuseId: remoteAbuseId, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
450 }) 427 })
451 428
452 it('Should fail when creating abuse message of a remote abuse', async function () { 429 it('Should fail when creating abuse message of a remote abuse', async function () {
453 await addAbuseMessage(server.url, server.accessToken, remoteAbuseId, 'message', HttpStatusCode.BAD_REQUEST_400) 430 await command.addMessage({ abuseId: remoteAbuseId, message: 'message', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
454 }) 431 })
455 432
456 after(async function () { 433 after(async function () {
diff --git a/server/tests/api/check-params/accounts.ts b/server/tests/api/check-params/accounts.ts
index d1712cff6..141d869b7 100644
--- a/server/tests/api/check-params/accounts.ts
+++ b/server/tests/api/check-params/accounts.ts
@@ -1,26 +1,26 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2 2
3import 'mocha' 3import 'mocha'
4
5import { cleanupTests, flushAndRunServer, ServerInfo } from '../../../../shared/extra-utils'
6import { 4import {
7 checkBadCountPagination, 5 checkBadCountPagination,
8 checkBadSortPagination, 6 checkBadSortPagination,
9 checkBadStartPagination 7 checkBadStartPagination,
10} from '../../../../shared/extra-utils/requests/check-api-params' 8 cleanupTests,
11import { getAccount } from '../../../../shared/extra-utils/users/accounts' 9 createSingleServer,
12import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes' 10 PeerTubeServer
11} from '@shared/extra-utils'
12import { HttpStatusCode } from '@shared/models'
13 13
14describe('Test accounts API validators', function () { 14describe('Test accounts API validators', function () {
15 const path = '/api/v1/accounts/' 15 const path = '/api/v1/accounts/'
16 let server: ServerInfo 16 let server: PeerTubeServer
17 17
18 // --------------------------------------------------------------- 18 // ---------------------------------------------------------------
19 19
20 before(async function () { 20 before(async function () {
21 this.timeout(30000) 21 this.timeout(30000)
22 22
23 server = await flushAndRunServer(1) 23 server = await createSingleServer(1)
24 }) 24 })
25 25
26 describe('When listing accounts', function () { 26 describe('When listing accounts', function () {
@@ -38,8 +38,9 @@ describe('Test accounts API validators', function () {
38 }) 38 })
39 39
40 describe('When getting an account', function () { 40 describe('When getting an account', function () {
41
41 it('Should return 404 with a non existing name', async function () { 42 it('Should return 404 with a non existing name', async function () {
42 await getAccount(server.url, 'arfaze', HttpStatusCode.NOT_FOUND_404) 43 await server.accounts.get({ accountName: 'arfaze', expectedStatus: HttpStatusCode.NOT_FOUND_404 })
43 }) 44 })
44 }) 45 })
45 46
diff --git a/server/tests/api/check-params/blocklist.ts b/server/tests/api/check-params/blocklist.ts
index 5ed8810ce..7d5fae5cf 100644
--- a/server/tests/api/check-params/blocklist.ts
+++ b/server/tests/api/check-params/blocklist.ts
@@ -1,43 +1,38 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2 2
3import 'mocha' 3import 'mocha'
4
5import { 4import {
5 checkBadCountPagination,
6 checkBadSortPagination,
7 checkBadStartPagination,
6 cleanupTests, 8 cleanupTests,
7 createUser, 9 createMultipleServers,
8 doubleFollow, 10 doubleFollow,
9 flushAndRunMultipleServers,
10 makeDeleteRequest, 11 makeDeleteRequest,
11 makeGetRequest, 12 makeGetRequest,
12 makePostBodyRequest, 13 makePostBodyRequest,
13 ServerInfo, 14 PeerTubeServer,
14 setAccessTokensToServers, 15 setAccessTokensToServers
15 userLogin 16} from '@shared/extra-utils'
16} from '../../../../shared/extra-utils' 17import { HttpStatusCode } from '@shared/models'
17import {
18 checkBadCountPagination,
19 checkBadSortPagination,
20 checkBadStartPagination
21} from '../../../../shared/extra-utils/requests/check-api-params'
22import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
23 18
24describe('Test blocklist API validators', function () { 19describe('Test blocklist API validators', function () {
25 let servers: ServerInfo[] 20 let servers: PeerTubeServer[]
26 let server: ServerInfo 21 let server: PeerTubeServer
27 let userAccessToken: string 22 let userAccessToken: string
28 23
29 before(async function () { 24 before(async function () {
30 this.timeout(60000) 25 this.timeout(60000)
31 26
32 servers = await flushAndRunMultipleServers(2) 27 servers = await createMultipleServers(2)
33 await setAccessTokensToServers(servers) 28 await setAccessTokensToServers(servers)
34 29
35 server = servers[0] 30 server = servers[0]
36 31
37 const user = { username: 'user1', password: 'password' } 32 const user = { username: 'user1', password: 'password' }
38 await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password }) 33 await server.users.create({ username: user.username, password: user.password })
39 34
40 userAccessToken = await userLogin(server, user) 35 userAccessToken = await server.login.getAccessToken(user)
41 36
42 await doubleFollow(servers[0], servers[1]) 37 await doubleFollow(servers[0], servers[1])
43 }) 38 })
@@ -54,7 +49,7 @@ describe('Test blocklist API validators', function () {
54 await makeGetRequest({ 49 await makeGetRequest({
55 url: server.url, 50 url: server.url,
56 path, 51 path,
57 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 52 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
58 }) 53 })
59 }) 54 })
60 55
@@ -77,7 +72,7 @@ describe('Test blocklist API validators', function () {
77 url: server.url, 72 url: server.url,
78 path, 73 path,
79 fields: { accountName: 'user1' }, 74 fields: { accountName: 'user1' },
80 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 75 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
81 }) 76 })
82 }) 77 })
83 78
@@ -87,7 +82,7 @@ describe('Test blocklist API validators', function () {
87 token: server.accessToken, 82 token: server.accessToken,
88 path, 83 path,
89 fields: { accountName: 'user2' }, 84 fields: { accountName: 'user2' },
90 statusCodeExpected: HttpStatusCode.NOT_FOUND_404 85 expectedStatus: HttpStatusCode.NOT_FOUND_404
91 }) 86 })
92 }) 87 })
93 88
@@ -97,7 +92,7 @@ describe('Test blocklist API validators', function () {
97 token: server.accessToken, 92 token: server.accessToken,
98 path, 93 path,
99 fields: { accountName: 'root' }, 94 fields: { accountName: 'root' },
100 statusCodeExpected: HttpStatusCode.CONFLICT_409 95 expectedStatus: HttpStatusCode.CONFLICT_409
101 }) 96 })
102 }) 97 })
103 98
@@ -107,7 +102,7 @@ describe('Test blocklist API validators', function () {
107 token: server.accessToken, 102 token: server.accessToken,
108 path, 103 path,
109 fields: { accountName: 'user1' }, 104 fields: { accountName: 'user1' },
110 statusCodeExpected: HttpStatusCode.NO_CONTENT_204 105 expectedStatus: HttpStatusCode.NO_CONTENT_204
111 }) 106 })
112 }) 107 })
113 }) 108 })
@@ -117,7 +112,7 @@ describe('Test blocklist API validators', function () {
117 await makeDeleteRequest({ 112 await makeDeleteRequest({
118 url: server.url, 113 url: server.url,
119 path: path + '/user1', 114 path: path + '/user1',
120 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 115 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
121 }) 116 })
122 }) 117 })
123 118
@@ -126,7 +121,7 @@ describe('Test blocklist API validators', function () {
126 url: server.url, 121 url: server.url,
127 path: path + '/user2', 122 path: path + '/user2',
128 token: server.accessToken, 123 token: server.accessToken,
129 statusCodeExpected: HttpStatusCode.NOT_FOUND_404 124 expectedStatus: HttpStatusCode.NOT_FOUND_404
130 }) 125 })
131 }) 126 })
132 127
@@ -135,7 +130,7 @@ describe('Test blocklist API validators', function () {
135 url: server.url, 130 url: server.url,
136 path: path + '/user1', 131 path: path + '/user1',
137 token: server.accessToken, 132 token: server.accessToken,
138 statusCodeExpected: HttpStatusCode.NO_CONTENT_204 133 expectedStatus: HttpStatusCode.NO_CONTENT_204
139 }) 134 })
140 }) 135 })
141 }) 136 })
@@ -149,7 +144,7 @@ describe('Test blocklist API validators', function () {
149 await makeGetRequest({ 144 await makeGetRequest({
150 url: server.url, 145 url: server.url,
151 path, 146 path,
152 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 147 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
153 }) 148 })
154 }) 149 })
155 150
@@ -172,7 +167,7 @@ describe('Test blocklist API validators', function () {
172 url: server.url, 167 url: server.url,
173 path, 168 path,
174 fields: { host: 'localhost:9002' }, 169 fields: { host: 'localhost:9002' },
175 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 170 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
176 }) 171 })
177 }) 172 })
178 173
@@ -182,7 +177,7 @@ describe('Test blocklist API validators', function () {
182 token: server.accessToken, 177 token: server.accessToken,
183 path, 178 path,
184 fields: { host: 'localhost:9003' }, 179 fields: { host: 'localhost:9003' },
185 statusCodeExpected: HttpStatusCode.NO_CONTENT_204 180 expectedStatus: HttpStatusCode.NO_CONTENT_204
186 }) 181 })
187 }) 182 })
188 183
@@ -192,7 +187,7 @@ describe('Test blocklist API validators', function () {
192 token: server.accessToken, 187 token: server.accessToken,
193 path, 188 path,
194 fields: { host: 'localhost:' + server.port }, 189 fields: { host: 'localhost:' + server.port },
195 statusCodeExpected: HttpStatusCode.CONFLICT_409 190 expectedStatus: HttpStatusCode.CONFLICT_409
196 }) 191 })
197 }) 192 })
198 193
@@ -202,7 +197,7 @@ describe('Test blocklist API validators', function () {
202 token: server.accessToken, 197 token: server.accessToken,
203 path, 198 path,
204 fields: { host: 'localhost:' + servers[1].port }, 199 fields: { host: 'localhost:' + servers[1].port },
205 statusCodeExpected: HttpStatusCode.NO_CONTENT_204 200 expectedStatus: HttpStatusCode.NO_CONTENT_204
206 }) 201 })
207 }) 202 })
208 }) 203 })
@@ -212,7 +207,7 @@ describe('Test blocklist API validators', function () {
212 await makeDeleteRequest({ 207 await makeDeleteRequest({
213 url: server.url, 208 url: server.url,
214 path: path + '/localhost:' + servers[1].port, 209 path: path + '/localhost:' + servers[1].port,
215 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 210 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
216 }) 211 })
217 }) 212 })
218 213
@@ -221,7 +216,7 @@ describe('Test blocklist API validators', function () {
221 url: server.url, 216 url: server.url,
222 path: path + '/localhost:9004', 217 path: path + '/localhost:9004',
223 token: server.accessToken, 218 token: server.accessToken,
224 statusCodeExpected: HttpStatusCode.NOT_FOUND_404 219 expectedStatus: HttpStatusCode.NOT_FOUND_404
225 }) 220 })
226 }) 221 })
227 222
@@ -230,7 +225,7 @@ describe('Test blocklist API validators', function () {
230 url: server.url, 225 url: server.url,
231 path: path + '/localhost:' + servers[1].port, 226 path: path + '/localhost:' + servers[1].port,
232 token: server.accessToken, 227 token: server.accessToken,
233 statusCodeExpected: HttpStatusCode.NO_CONTENT_204 228 expectedStatus: HttpStatusCode.NO_CONTENT_204
234 }) 229 })
235 }) 230 })
236 }) 231 })
@@ -247,7 +242,7 @@ describe('Test blocklist API validators', function () {
247 await makeGetRequest({ 242 await makeGetRequest({
248 url: server.url, 243 url: server.url,
249 path, 244 path,
250 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 245 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
251 }) 246 })
252 }) 247 })
253 248
@@ -256,7 +251,7 @@ describe('Test blocklist API validators', function () {
256 url: server.url, 251 url: server.url,
257 token: userAccessToken, 252 token: userAccessToken,
258 path, 253 path,
259 statusCodeExpected: HttpStatusCode.FORBIDDEN_403 254 expectedStatus: HttpStatusCode.FORBIDDEN_403
260 }) 255 })
261 }) 256 })
262 257
@@ -279,7 +274,7 @@ describe('Test blocklist API validators', function () {
279 url: server.url, 274 url: server.url,
280 path, 275 path,
281 fields: { accountName: 'user1' }, 276 fields: { accountName: 'user1' },
282 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 277 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
283 }) 278 })
284 }) 279 })
285 280
@@ -289,7 +284,7 @@ describe('Test blocklist API validators', function () {
289 token: userAccessToken, 284 token: userAccessToken,
290 path, 285 path,
291 fields: { accountName: 'user1' }, 286 fields: { accountName: 'user1' },
292 statusCodeExpected: HttpStatusCode.FORBIDDEN_403 287 expectedStatus: HttpStatusCode.FORBIDDEN_403
293 }) 288 })
294 }) 289 })
295 290
@@ -299,7 +294,7 @@ describe('Test blocklist API validators', function () {
299 token: server.accessToken, 294 token: server.accessToken,
300 path, 295 path,
301 fields: { accountName: 'user2' }, 296 fields: { accountName: 'user2' },
302 statusCodeExpected: HttpStatusCode.NOT_FOUND_404 297 expectedStatus: HttpStatusCode.NOT_FOUND_404
303 }) 298 })
304 }) 299 })
305 300
@@ -309,7 +304,7 @@ describe('Test blocklist API validators', function () {
309 token: server.accessToken, 304 token: server.accessToken,
310 path, 305 path,
311 fields: { accountName: 'root' }, 306 fields: { accountName: 'root' },
312 statusCodeExpected: HttpStatusCode.CONFLICT_409 307 expectedStatus: HttpStatusCode.CONFLICT_409
313 }) 308 })
314 }) 309 })
315 310
@@ -319,7 +314,7 @@ describe('Test blocklist API validators', function () {
319 token: server.accessToken, 314 token: server.accessToken,
320 path, 315 path,
321 fields: { accountName: 'user1' }, 316 fields: { accountName: 'user1' },
322 statusCodeExpected: HttpStatusCode.NO_CONTENT_204 317 expectedStatus: HttpStatusCode.NO_CONTENT_204
323 }) 318 })
324 }) 319 })
325 }) 320 })
@@ -329,7 +324,7 @@ describe('Test blocklist API validators', function () {
329 await makeDeleteRequest({ 324 await makeDeleteRequest({
330 url: server.url, 325 url: server.url,
331 path: path + '/user1', 326 path: path + '/user1',
332 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 327 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
333 }) 328 })
334 }) 329 })
335 330
@@ -338,7 +333,7 @@ describe('Test blocklist API validators', function () {
338 url: server.url, 333 url: server.url,
339 path: path + '/user1', 334 path: path + '/user1',
340 token: userAccessToken, 335 token: userAccessToken,
341 statusCodeExpected: HttpStatusCode.FORBIDDEN_403 336 expectedStatus: HttpStatusCode.FORBIDDEN_403
342 }) 337 })
343 }) 338 })
344 339
@@ -347,7 +342,7 @@ describe('Test blocklist API validators', function () {
347 url: server.url, 342 url: server.url,
348 path: path + '/user2', 343 path: path + '/user2',
349 token: server.accessToken, 344 token: server.accessToken,
350 statusCodeExpected: HttpStatusCode.NOT_FOUND_404 345 expectedStatus: HttpStatusCode.NOT_FOUND_404
351 }) 346 })
352 }) 347 })
353 348
@@ -356,7 +351,7 @@ describe('Test blocklist API validators', function () {
356 url: server.url, 351 url: server.url,
357 path: path + '/user1', 352 path: path + '/user1',
358 token: server.accessToken, 353 token: server.accessToken,
359 statusCodeExpected: HttpStatusCode.NO_CONTENT_204 354 expectedStatus: HttpStatusCode.NO_CONTENT_204
360 }) 355 })
361 }) 356 })
362 }) 357 })
@@ -370,7 +365,7 @@ describe('Test blocklist API validators', function () {
370 await makeGetRequest({ 365 await makeGetRequest({
371 url: server.url, 366 url: server.url,
372 path, 367 path,
373 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 368 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
374 }) 369 })
375 }) 370 })
376 371
@@ -379,7 +374,7 @@ describe('Test blocklist API validators', function () {
379 url: server.url, 374 url: server.url,
380 token: userAccessToken, 375 token: userAccessToken,
381 path, 376 path,
382 statusCodeExpected: HttpStatusCode.FORBIDDEN_403 377 expectedStatus: HttpStatusCode.FORBIDDEN_403
383 }) 378 })
384 }) 379 })
385 380
@@ -402,7 +397,7 @@ describe('Test blocklist API validators', function () {
402 url: server.url, 397 url: server.url,
403 path, 398 path,
404 fields: { host: 'localhost:' + servers[1].port }, 399 fields: { host: 'localhost:' + servers[1].port },
405 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 400 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
406 }) 401 })
407 }) 402 })
408 403
@@ -412,7 +407,7 @@ describe('Test blocklist API validators', function () {
412 token: userAccessToken, 407 token: userAccessToken,
413 path, 408 path,
414 fields: { host: 'localhost:' + servers[1].port }, 409 fields: { host: 'localhost:' + servers[1].port },
415 statusCodeExpected: HttpStatusCode.FORBIDDEN_403 410 expectedStatus: HttpStatusCode.FORBIDDEN_403
416 }) 411 })
417 }) 412 })
418 413
@@ -422,7 +417,7 @@ describe('Test blocklist API validators', function () {
422 token: server.accessToken, 417 token: server.accessToken,
423 path, 418 path,
424 fields: { host: 'localhost:9003' }, 419 fields: { host: 'localhost:9003' },
425 statusCodeExpected: HttpStatusCode.NO_CONTENT_204 420 expectedStatus: HttpStatusCode.NO_CONTENT_204
426 }) 421 })
427 }) 422 })
428 423
@@ -432,7 +427,7 @@ describe('Test blocklist API validators', function () {
432 token: server.accessToken, 427 token: server.accessToken,
433 path, 428 path,
434 fields: { host: 'localhost:' + server.port }, 429 fields: { host: 'localhost:' + server.port },
435 statusCodeExpected: HttpStatusCode.CONFLICT_409 430 expectedStatus: HttpStatusCode.CONFLICT_409
436 }) 431 })
437 }) 432 })
438 433
@@ -442,7 +437,7 @@ describe('Test blocklist API validators', function () {
442 token: server.accessToken, 437 token: server.accessToken,
443 path, 438 path,
444 fields: { host: 'localhost:' + servers[1].port }, 439 fields: { host: 'localhost:' + servers[1].port },
445 statusCodeExpected: HttpStatusCode.NO_CONTENT_204 440 expectedStatus: HttpStatusCode.NO_CONTENT_204
446 }) 441 })
447 }) 442 })
448 }) 443 })
@@ -452,7 +447,7 @@ describe('Test blocklist API validators', function () {
452 await makeDeleteRequest({ 447 await makeDeleteRequest({
453 url: server.url, 448 url: server.url,
454 path: path + '/localhost:' + servers[1].port, 449 path: path + '/localhost:' + servers[1].port,
455 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 450 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
456 }) 451 })
457 }) 452 })
458 453
@@ -461,7 +456,7 @@ describe('Test blocklist API validators', function () {
461 url: server.url, 456 url: server.url,
462 path: path + '/localhost:' + servers[1].port, 457 path: path + '/localhost:' + servers[1].port,
463 token: userAccessToken, 458 token: userAccessToken,
464 statusCodeExpected: HttpStatusCode.FORBIDDEN_403 459 expectedStatus: HttpStatusCode.FORBIDDEN_403
465 }) 460 })
466 }) 461 })
467 462
@@ -470,7 +465,7 @@ describe('Test blocklist API validators', function () {
470 url: server.url, 465 url: server.url,
471 path: path + '/localhost:9004', 466 path: path + '/localhost:9004',
472 token: server.accessToken, 467 token: server.accessToken,
473 statusCodeExpected: HttpStatusCode.NOT_FOUND_404 468 expectedStatus: HttpStatusCode.NOT_FOUND_404
474 }) 469 })
475 }) 470 })
476 471
@@ -479,7 +474,7 @@ describe('Test blocklist API validators', function () {
479 url: server.url, 474 url: server.url,
480 path: path + '/localhost:' + servers[1].port, 475 path: path + '/localhost:' + servers[1].port,
481 token: server.accessToken, 476 token: server.accessToken,
482 statusCodeExpected: HttpStatusCode.NO_CONTENT_204 477 expectedStatus: HttpStatusCode.NO_CONTENT_204
483 }) 478 })
484 }) 479 })
485 }) 480 })
diff --git a/server/tests/api/check-params/bulk.ts b/server/tests/api/check-params/bulk.ts
index 07b920ba7..bc9d7784d 100644
--- a/server/tests/api/check-params/bulk.ts
+++ b/server/tests/api/check-params/bulk.ts
@@ -1,19 +1,11 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2 2
3import 'mocha' 3import 'mocha'
4import { 4import { cleanupTests, createSingleServer, makePostBodyRequest, PeerTubeServer, setAccessTokensToServers } from '@shared/extra-utils'
5 cleanupTests, 5import { HttpStatusCode } from '@shared/models'
6 createUser,
7 flushAndRunServer,
8 ServerInfo,
9 setAccessTokensToServers,
10 userLogin
11} from '../../../../shared/extra-utils'
12import { makePostBodyRequest } from '../../../../shared/extra-utils/requests/requests'
13import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
14 6
15describe('Test bulk API validators', function () { 7describe('Test bulk API validators', function () {
16 let server: ServerInfo 8 let server: PeerTubeServer
17 let userAccessToken: string 9 let userAccessToken: string
18 10
19 // --------------------------------------------------------------- 11 // ---------------------------------------------------------------
@@ -21,13 +13,13 @@ describe('Test bulk API validators', function () {
21 before(async function () { 13 before(async function () {
22 this.timeout(120000) 14 this.timeout(120000)
23 15
24 server = await flushAndRunServer(1) 16 server = await createSingleServer(1)
25 await setAccessTokensToServers([ server ]) 17 await setAccessTokensToServers([ server ])
26 18
27 const user = { username: 'user1', password: 'password' } 19 const user = { username: 'user1', password: 'password' }
28 await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password }) 20 await server.users.create({ username: user.username, password: user.password })
29 21
30 userAccessToken = await userLogin(server, user) 22 userAccessToken = await server.login.getAccessToken(user)
31 }) 23 })
32 24
33 describe('When removing comments of', function () { 25 describe('When removing comments of', function () {
@@ -38,7 +30,7 @@ describe('Test bulk API validators', function () {
38 url: server.url, 30 url: server.url,
39 path, 31 path,
40 fields: { accountName: 'user1', scope: 'my-videos' }, 32 fields: { accountName: 'user1', scope: 'my-videos' },
41 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 33 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
42 }) 34 })
43 }) 35 })
44 36
@@ -48,7 +40,7 @@ describe('Test bulk API validators', function () {
48 token: server.accessToken, 40 token: server.accessToken,
49 path, 41 path,
50 fields: { accountName: 'user2', scope: 'my-videos' }, 42 fields: { accountName: 'user2', scope: 'my-videos' },
51 statusCodeExpected: HttpStatusCode.NOT_FOUND_404 43 expectedStatus: HttpStatusCode.NOT_FOUND_404
52 }) 44 })
53 }) 45 })
54 46
@@ -58,7 +50,7 @@ describe('Test bulk API validators', function () {
58 token: server.accessToken, 50 token: server.accessToken,
59 path, 51 path,
60 fields: { accountName: 'user1', scope: 'my-videoss' }, 52 fields: { accountName: 'user1', scope: 'my-videoss' },
61 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 53 expectedStatus: HttpStatusCode.BAD_REQUEST_400
62 }) 54 })
63 }) 55 })
64 56
@@ -68,7 +60,7 @@ describe('Test bulk API validators', function () {
68 token: userAccessToken, 60 token: userAccessToken,
69 path, 61 path,
70 fields: { accountName: 'user1', scope: 'instance' }, 62 fields: { accountName: 'user1', scope: 'instance' },
71 statusCodeExpected: HttpStatusCode.FORBIDDEN_403 63 expectedStatus: HttpStatusCode.FORBIDDEN_403
72 }) 64 })
73 }) 65 })
74 66
@@ -78,7 +70,7 @@ describe('Test bulk API validators', function () {
78 token: server.accessToken, 70 token: server.accessToken,
79 path, 71 path,
80 fields: { accountName: 'user1', scope: 'instance' }, 72 fields: { accountName: 'user1', scope: 'instance' },
81 statusCodeExpected: HttpStatusCode.NO_CONTENT_204 73 expectedStatus: HttpStatusCode.NO_CONTENT_204
82 }) 74 })
83 }) 75 })
84 }) 76 })
diff --git a/server/tests/api/check-params/config.ts b/server/tests/api/check-params/config.ts
index 9549070ef..87cb2287e 100644
--- a/server/tests/api/check-params/config.ts
+++ b/server/tests/api/check-params/config.ts
@@ -1,26 +1,21 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2 2
3import { omit } from 'lodash'
4import 'mocha' 3import 'mocha'
5import { CustomConfig } from '../../../../shared/models/server/custom-config.model' 4import { omit } from 'lodash'
6
7import { 5import {
8 cleanupTests, 6 cleanupTests,
9 createUser, 7 createSingleServer,
10 flushAndRunServer,
11 immutableAssign,
12 makeDeleteRequest, 8 makeDeleteRequest,
13 makeGetRequest, 9 makeGetRequest,
14 makePutBodyRequest, 10 makePutBodyRequest,
15 ServerInfo, 11 PeerTubeServer,
16 setAccessTokensToServers, 12 setAccessTokensToServers
17 userLogin 13} from '@shared/extra-utils'
18} from '../../../../shared/extra-utils' 14import { CustomConfig, HttpStatusCode } from '@shared/models'
19import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
20 15
21describe('Test config API validators', function () { 16describe('Test config API validators', function () {
22 const path = '/api/v1/config/custom' 17 const path = '/api/v1/config/custom'
23 let server: ServerInfo 18 let server: PeerTubeServer
24 let userAccessToken: string 19 let userAccessToken: string
25 const updateParams: CustomConfig = { 20 const updateParams: CustomConfig = {
26 instance: { 21 instance: {
@@ -201,7 +196,7 @@ describe('Test config API validators', function () {
201 before(async function () { 196 before(async function () {
202 this.timeout(30000) 197 this.timeout(30000)
203 198
204 server = await flushAndRunServer(1) 199 server = await createSingleServer(1)
205 200
206 await setAccessTokensToServers([ server ]) 201 await setAccessTokensToServers([ server ])
207 202
@@ -209,8 +204,8 @@ describe('Test config API validators', function () {
209 username: 'user1', 204 username: 'user1',
210 password: 'password' 205 password: 'password'
211 } 206 }
212 await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password }) 207 await server.users.create({ username: user.username, password: user.password })
213 userAccessToken = await userLogin(server, user) 208 userAccessToken = await server.login.getAccessToken(user)
214 }) 209 })
215 210
216 describe('When getting the configuration', function () { 211 describe('When getting the configuration', function () {
@@ -218,7 +213,7 @@ describe('Test config API validators', function () {
218 await makeGetRequest({ 213 await makeGetRequest({
219 url: server.url, 214 url: server.url,
220 path, 215 path,
221 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 216 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
222 }) 217 })
223 }) 218 })
224 219
@@ -227,7 +222,7 @@ describe('Test config API validators', function () {
227 url: server.url, 222 url: server.url,
228 path, 223 path,
229 token: userAccessToken, 224 token: userAccessToken,
230 statusCodeExpected: HttpStatusCode.FORBIDDEN_403 225 expectedStatus: HttpStatusCode.FORBIDDEN_403
231 }) 226 })
232 }) 227 })
233 }) 228 })
@@ -238,7 +233,7 @@ describe('Test config API validators', function () {
238 url: server.url, 233 url: server.url,
239 path, 234 path,
240 fields: updateParams, 235 fields: updateParams,
241 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 236 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
242 }) 237 })
243 }) 238 })
244 239
@@ -248,7 +243,7 @@ describe('Test config API validators', function () {
248 path, 243 path,
249 fields: updateParams, 244 fields: updateParams,
250 token: userAccessToken, 245 token: userAccessToken,
251 statusCodeExpected: HttpStatusCode.FORBIDDEN_403 246 expectedStatus: HttpStatusCode.FORBIDDEN_403
252 }) 247 })
253 }) 248 })
254 249
@@ -260,47 +255,53 @@ describe('Test config API validators', function () {
260 path, 255 path,
261 fields: newUpdateParams, 256 fields: newUpdateParams,
262 token: server.accessToken, 257 token: server.accessToken,
263 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 258 expectedStatus: HttpStatusCode.BAD_REQUEST_400
264 }) 259 })
265 }) 260 })
266 261
267 it('Should fail with a bad default NSFW policy', async function () { 262 it('Should fail with a bad default NSFW policy', async function () {
268 const newUpdateParams = immutableAssign(updateParams, { 263 const newUpdateParams = {
264 ...updateParams,
265
269 instance: { 266 instance: {
270 defaultNSFWPolicy: 'hello' 267 defaultNSFWPolicy: 'hello'
271 } 268 }
272 }) 269 }
273 270
274 await makePutBodyRequest({ 271 await makePutBodyRequest({
275 url: server.url, 272 url: server.url,
276 path, 273 path,
277 fields: newUpdateParams, 274 fields: newUpdateParams,
278 token: server.accessToken, 275 token: server.accessToken,
279 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 276 expectedStatus: HttpStatusCode.BAD_REQUEST_400
280 }) 277 })
281 }) 278 })
282 279
283 it('Should fail if email disabled and signup requires email verification', async function () { 280 it('Should fail if email disabled and signup requires email verification', async function () {
284 // opposite scenario - success when enable enabled - covered via tests/api/users/user-verification.ts 281 // opposite scenario - success when enable enabled - covered via tests/api/users/user-verification.ts
285 const newUpdateParams = immutableAssign(updateParams, { 282 const newUpdateParams = {
283 ...updateParams,
284
286 signup: { 285 signup: {
287 enabled: true, 286 enabled: true,
288 limit: 5, 287 limit: 5,
289 requiresEmailVerification: true 288 requiresEmailVerification: true
290 } 289 }
291 }) 290 }
292 291
293 await makePutBodyRequest({ 292 await makePutBodyRequest({
294 url: server.url, 293 url: server.url,
295 path, 294 path,
296 fields: newUpdateParams, 295 fields: newUpdateParams,
297 token: server.accessToken, 296 token: server.accessToken,
298 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 297 expectedStatus: HttpStatusCode.BAD_REQUEST_400
299 }) 298 })
300 }) 299 })
301 300
302 it('Should fail with a disabled webtorrent & hls transcoding', async function () { 301 it('Should fail with a disabled webtorrent & hls transcoding', async function () {
303 const newUpdateParams = immutableAssign(updateParams, { 302 const newUpdateParams = {
303 ...updateParams,
304
304 transcoding: { 305 transcoding: {
305 hls: { 306 hls: {
306 enabled: false 307 enabled: false
@@ -309,14 +310,14 @@ describe('Test config API validators', function () {
309 enabled: false 310 enabled: false
310 } 311 }
311 } 312 }
312 }) 313 }
313 314
314 await makePutBodyRequest({ 315 await makePutBodyRequest({
315 url: server.url, 316 url: server.url,
316 path, 317 path,
317 fields: newUpdateParams, 318 fields: newUpdateParams,
318 token: server.accessToken, 319 token: server.accessToken,
319 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 320 expectedStatus: HttpStatusCode.BAD_REQUEST_400
320 }) 321 })
321 }) 322 })
322 323
@@ -326,7 +327,7 @@ describe('Test config API validators', function () {
326 path, 327 path,
327 fields: updateParams, 328 fields: updateParams,
328 token: server.accessToken, 329 token: server.accessToken,
329 statusCodeExpected: HttpStatusCode.OK_200 330 expectedStatus: HttpStatusCode.OK_200
330 }) 331 })
331 }) 332 })
332 }) 333 })
@@ -336,7 +337,7 @@ describe('Test config API validators', function () {
336 await makeDeleteRequest({ 337 await makeDeleteRequest({
337 url: server.url, 338 url: server.url,
338 path, 339 path,
339 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 340 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
340 }) 341 })
341 }) 342 })
342 343
@@ -345,7 +346,7 @@ describe('Test config API validators', function () {
345 url: server.url, 346 url: server.url,
346 path, 347 path,
347 token: userAccessToken, 348 token: userAccessToken,
348 statusCodeExpected: HttpStatusCode.FORBIDDEN_403 349 expectedStatus: HttpStatusCode.FORBIDDEN_403
349 }) 350 })
350 }) 351 })
351 }) 352 })
diff --git a/server/tests/api/check-params/contact-form.ts b/server/tests/api/check-params/contact-form.ts
index c7f9c1b47..1df9993da 100644
--- a/server/tests/api/check-params/contact-form.ts
+++ b/server/tests/api/check-params/contact-form.ts
@@ -1,14 +1,12 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2 2
3import 'mocha' 3import 'mocha'
4 4import { cleanupTests, createSingleServer, killallServers, MockSmtpServer, PeerTubeServer } from '@shared/extra-utils'
5import { cleanupTests, flushAndRunServer, immutableAssign, killallServers, reRunServer, ServerInfo } from '../../../../shared/extra-utils' 5import { ContactFormCommand } from '@shared/extra-utils/server'
6import { sendContactForm } from '../../../../shared/extra-utils/server/contact-form' 6import { HttpStatusCode } from '@shared/models'
7import { MockSmtpServer } from '../../../../shared/extra-utils/miscs/email'
8import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
9 7
10describe('Test contact form API validators', function () { 8describe('Test contact form API validators', function () {
11 let server: ServerInfo 9 let server: PeerTubeServer
12 const emails: object[] = [] 10 const emails: object[] = []
13 const defaultBody = { 11 const defaultBody = {
14 fromName: 'super name', 12 fromName: 'super name',
@@ -17,6 +15,7 @@ describe('Test contact form API validators', function () {
17 body: 'Hello, how are you?' 15 body: 'Hello, how are you?'
18 } 16 }
19 let emailPort: number 17 let emailPort: number
18 let command: ContactFormCommand
20 19
21 // --------------------------------------------------------------- 20 // ---------------------------------------------------------------
22 21
@@ -26,86 +25,51 @@ describe('Test contact form API validators', function () {
26 emailPort = await MockSmtpServer.Instance.collectEmails(emails) 25 emailPort = await MockSmtpServer.Instance.collectEmails(emails)
27 26
28 // Email is disabled 27 // Email is disabled
29 server = await flushAndRunServer(1) 28 server = await createSingleServer(1)
29 command = server.contactForm
30 }) 30 })
31 31
32 it('Should not accept a contact form if emails are disabled', async function () { 32 it('Should not accept a contact form if emails are disabled', async function () {
33 await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: HttpStatusCode.CONFLICT_409 })) 33 await command.send({ ...defaultBody, expectedStatus: HttpStatusCode.CONFLICT_409 })
34 }) 34 })
35 35
36 it('Should not accept a contact form if it is disabled in the configuration', async function () { 36 it('Should not accept a contact form if it is disabled in the configuration', async function () {
37 this.timeout(10000) 37 this.timeout(10000)
38 38
39 killallServers([ server ]) 39 await killallServers([ server ])
40 40
41 // Contact form is disabled 41 // Contact form is disabled
42 await reRunServer(server, { smtp: { hostname: 'localhost', port: emailPort }, contact_form: { enabled: false } }) 42 await server.run({ smtp: { hostname: 'localhost', port: emailPort }, contact_form: { enabled: false } })
43 await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: HttpStatusCode.CONFLICT_409 })) 43 await command.send({ ...defaultBody, expectedStatus: HttpStatusCode.CONFLICT_409 })
44 }) 44 })
45 45
46 it('Should not accept a contact form if from email is invalid', async function () { 46 it('Should not accept a contact form if from email is invalid', async function () {
47 this.timeout(10000) 47 this.timeout(10000)
48 48
49 killallServers([ server ]) 49 await killallServers([ server ])
50 50
51 // Email & contact form enabled 51 // Email & contact form enabled
52 await reRunServer(server, { smtp: { hostname: 'localhost', port: emailPort } }) 52 await server.run({ smtp: { hostname: 'localhost', port: emailPort } })
53 53
54 await sendContactForm(immutableAssign(defaultBody, { 54 await command.send({ ...defaultBody, fromEmail: 'badEmail', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
55 url: server.url, 55 await command.send({ ...defaultBody, fromEmail: 'badEmail@', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
56 expectedStatus: HttpStatusCode.BAD_REQUEST_400, 56 await command.send({ ...defaultBody, fromEmail: undefined, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
57 fromEmail: 'badEmail'
58 }))
59 await sendContactForm(immutableAssign(defaultBody, {
60 url: server.url,
61 expectedStatus: HttpStatusCode.BAD_REQUEST_400,
62 fromEmail: 'badEmail@'
63 }))
64 await sendContactForm(immutableAssign(defaultBody, {
65 url: server.url,
66 expectedStatus: HttpStatusCode.BAD_REQUEST_400,
67 fromEmail: undefined
68 }))
69 }) 57 })
70 58
71 it('Should not accept a contact form if from name is invalid', async function () { 59 it('Should not accept a contact form if from name is invalid', async function () {
72 await sendContactForm(immutableAssign(defaultBody, { 60 await command.send({ ...defaultBody, fromName: 'name'.repeat(100), expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
73 url: server.url, 61 await command.send({ ...defaultBody, fromName: '', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
74 expectedStatus: HttpStatusCode.BAD_REQUEST_400, 62 await command.send({ ...defaultBody, fromName: undefined, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
75 fromName: 'name'.repeat(100)
76 }))
77 await sendContactForm(immutableAssign(defaultBody, {
78 url: server.url,
79 expectedStatus: HttpStatusCode.BAD_REQUEST_400,
80 fromName: ''
81 }))
82 await sendContactForm(immutableAssign(defaultBody, {
83 url: server.url,
84 expectedStatus: HttpStatusCode.BAD_REQUEST_400,
85 fromName: undefined
86 }))
87 }) 63 })
88 64
89 it('Should not accept a contact form if body is invalid', async function () { 65 it('Should not accept a contact form if body is invalid', async function () {
90 await sendContactForm(immutableAssign(defaultBody, { 66 await command.send({ ...defaultBody, body: 'body'.repeat(5000), expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
91 url: server.url, 67 await command.send({ ...defaultBody, body: 'a', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
92 expectedStatus: HttpStatusCode.BAD_REQUEST_400, 68 await command.send({ ...defaultBody, body: undefined, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
93 body: 'body'.repeat(5000)
94 }))
95 await sendContactForm(immutableAssign(defaultBody, {
96 url: server.url,
97 expectedStatus: HttpStatusCode.BAD_REQUEST_400,
98 body: 'a'
99 }))
100 await sendContactForm(immutableAssign(defaultBody, {
101 url: server.url,
102 expectedStatus: HttpStatusCode.BAD_REQUEST_400,
103 body: undefined
104 }))
105 }) 69 })
106 70
107 it('Should accept a contact form with the correct parameters', async function () { 71 it('Should accept a contact form with the correct parameters', async function () {
108 await sendContactForm(immutableAssign(defaultBody, { url: server.url })) 72 await command.send(defaultBody)
109 }) 73 })
110 74
111 after(async function () { 75 after(async function () {
diff --git a/server/tests/api/check-params/custom-pages.ts b/server/tests/api/check-params/custom-pages.ts
index 74ca3384c..9fbbea315 100644
--- a/server/tests/api/check-params/custom-pages.ts
+++ b/server/tests/api/check-params/custom-pages.ts
@@ -1,21 +1,20 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2 2
3import 'mocha' 3import 'mocha'
4import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
5import { 4import {
6 cleanupTests, 5 cleanupTests,
7 createUser, 6 createSingleServer,
8 flushAndRunServer, 7 makeGetRequest,
9 ServerInfo, 8 makePutBodyRequest,
10 setAccessTokensToServers, 9 PeerTubeServer,
11 userLogin 10 setAccessTokensToServers
12} from '../../../../shared/extra-utils' 11} from '@shared/extra-utils'
13import { makeGetRequest, makePutBodyRequest } from '../../../../shared/extra-utils/requests/requests' 12import { HttpStatusCode } from '@shared/models'
14 13
15describe('Test custom pages validators', function () { 14describe('Test custom pages validators', function () {
16 const path = '/api/v1/custom-pages/homepage/instance' 15 const path = '/api/v1/custom-pages/homepage/instance'
17 16
18 let server: ServerInfo 17 let server: PeerTubeServer
19 let userAccessToken: string 18 let userAccessToken: string
20 19
21 // --------------------------------------------------------------- 20 // ---------------------------------------------------------------
@@ -23,13 +22,13 @@ describe('Test custom pages validators', function () {
23 before(async function () { 22 before(async function () {
24 this.timeout(120000) 23 this.timeout(120000)
25 24
26 server = await flushAndRunServer(1) 25 server = await createSingleServer(1)
27 await setAccessTokensToServers([ server ]) 26 await setAccessTokensToServers([ server ])
28 27
29 const user = { username: 'user1', password: 'password' } 28 const user = { username: 'user1', password: 'password' }
30 await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password }) 29 await server.users.create({ username: user.username, password: user.password })
31 30
32 userAccessToken = await userLogin(server, user) 31 userAccessToken = await server.login.getAccessToken(user)
33 }) 32 })
34 33
35 describe('When updating instance homepage', function () { 34 describe('When updating instance homepage', function () {
@@ -39,7 +38,7 @@ describe('Test custom pages validators', function () {
39 url: server.url, 38 url: server.url,
40 path, 39 path,
41 fields: { content: 'super content' }, 40 fields: { content: 'super content' },
42 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 41 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
43 }) 42 })
44 }) 43 })
45 44
@@ -49,7 +48,7 @@ describe('Test custom pages validators', function () {
49 path, 48 path,
50 token: userAccessToken, 49 token: userAccessToken,
51 fields: { content: 'super content' }, 50 fields: { content: 'super content' },
52 statusCodeExpected: HttpStatusCode.FORBIDDEN_403 51 expectedStatus: HttpStatusCode.FORBIDDEN_403
53 }) 52 })
54 }) 53 })
55 54
@@ -59,7 +58,7 @@ describe('Test custom pages validators', function () {
59 path, 58 path,
60 token: server.accessToken, 59 token: server.accessToken,
61 fields: { content: 'super content' }, 60 fields: { content: 'super content' },
62 statusCodeExpected: HttpStatusCode.NO_CONTENT_204 61 expectedStatus: HttpStatusCode.NO_CONTENT_204
63 }) 62 })
64 }) 63 })
65 }) 64 })
@@ -70,7 +69,7 @@ describe('Test custom pages validators', function () {
70 await makeGetRequest({ 69 await makeGetRequest({
71 url: server.url, 70 url: server.url,
72 path, 71 path,
73 statusCodeExpected: HttpStatusCode.OK_200 72 expectedStatus: HttpStatusCode.OK_200
74 }) 73 })
75 }) 74 })
76 }) 75 })
diff --git a/server/tests/api/check-params/debug.ts b/server/tests/api/check-params/debug.ts
index 37bf0f99b..a55786359 100644
--- a/server/tests/api/check-params/debug.ts
+++ b/server/tests/api/check-params/debug.ts
@@ -1,21 +1,12 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2 2
3import 'mocha' 3import 'mocha'
4 4import { cleanupTests, createSingleServer, makeGetRequest, PeerTubeServer, setAccessTokensToServers } from '@shared/extra-utils'
5import { 5import { HttpStatusCode } from '@shared/models'
6 cleanupTests,
7 createUser,
8 flushAndRunServer,
9 ServerInfo,
10 setAccessTokensToServers,
11 userLogin
12} from '../../../../shared/extra-utils'
13import { makeGetRequest } from '../../../../shared/extra-utils/requests/requests'
14import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
15 6
16describe('Test debug API validators', function () { 7describe('Test debug API validators', function () {
17 const path = '/api/v1/server/debug' 8 const path = '/api/v1/server/debug'
18 let server: ServerInfo 9 let server: PeerTubeServer
19 let userAccessToken = '' 10 let userAccessToken = ''
20 11
21 // --------------------------------------------------------------- 12 // ---------------------------------------------------------------
@@ -23,7 +14,7 @@ describe('Test debug API validators', function () {
23 before(async function () { 14 before(async function () {
24 this.timeout(120000) 15 this.timeout(120000)
25 16
26 server = await flushAndRunServer(1) 17 server = await createSingleServer(1)
27 18
28 await setAccessTokensToServers([ server ]) 19 await setAccessTokensToServers([ server ])
29 20
@@ -31,8 +22,8 @@ describe('Test debug API validators', function () {
31 username: 'user1', 22 username: 'user1',
32 password: 'my super password' 23 password: 'my super password'
33 } 24 }
34 await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password }) 25 await server.users.create({ username: user.username, password: user.password })
35 userAccessToken = await userLogin(server, user) 26 userAccessToken = await server.login.getAccessToken(user)
36 }) 27 })
37 28
38 describe('When getting debug endpoint', function () { 29 describe('When getting debug endpoint', function () {
@@ -41,7 +32,7 @@ describe('Test debug API validators', function () {
41 await makeGetRequest({ 32 await makeGetRequest({
42 url: server.url, 33 url: server.url,
43 path, 34 path,
44 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 35 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
45 }) 36 })
46 }) 37 })
47 38
@@ -50,7 +41,7 @@ describe('Test debug API validators', function () {
50 url: server.url, 41 url: server.url,
51 path, 42 path,
52 token: userAccessToken, 43 token: userAccessToken,
53 statusCodeExpected: HttpStatusCode.FORBIDDEN_403 44 expectedStatus: HttpStatusCode.FORBIDDEN_403
54 }) 45 })
55 }) 46 })
56 47
@@ -60,7 +51,7 @@ describe('Test debug API validators', function () {
60 path, 51 path,
61 token: server.accessToken, 52 token: server.accessToken,
62 query: { startDate: new Date().toISOString() }, 53 query: { startDate: new Date().toISOString() },
63 statusCodeExpected: HttpStatusCode.OK_200 54 expectedStatus: HttpStatusCode.OK_200
64 }) 55 })
65 }) 56 })
66 }) 57 })
diff --git a/server/tests/api/check-params/follows.ts b/server/tests/api/check-params/follows.ts
index c03dd5c9c..2bc9f6b96 100644
--- a/server/tests/api/check-params/follows.ts
+++ b/server/tests/api/check-params/follows.ts
@@ -1,33 +1,29 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2 2
3import 'mocha' 3import 'mocha'
4
5import {
6 cleanupTests,
7 createUser,
8 flushAndRunServer,
9 makeDeleteRequest, makeGetRequest,
10 makePostBodyRequest,
11 ServerInfo,
12 setAccessTokensToServers,
13 userLogin
14} from '../../../../shared/extra-utils'
15import { 4import {
16 checkBadCountPagination, 5 checkBadCountPagination,
17 checkBadSortPagination, 6 checkBadSortPagination,
18 checkBadStartPagination 7 checkBadStartPagination,
19} from '../../../../shared/extra-utils/requests/check-api-params' 8 cleanupTests,
20import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes' 9 createSingleServer,
10 makeDeleteRequest,
11 makeGetRequest,
12 makePostBodyRequest,
13 PeerTubeServer,
14 setAccessTokensToServers
15} from '@shared/extra-utils'
16import { HttpStatusCode } from '@shared/models'
21 17
22describe('Test server follows API validators', function () { 18describe('Test server follows API validators', function () {
23 let server: ServerInfo 19 let server: PeerTubeServer
24 20
25 // --------------------------------------------------------------- 21 // ---------------------------------------------------------------
26 22
27 before(async function () { 23 before(async function () {
28 this.timeout(30000) 24 this.timeout(30000)
29 25
30 server = await flushAndRunServer(1) 26 server = await createSingleServer(1)
31 27
32 await setAccessTokensToServers([ server ]) 28 await setAccessTokensToServers([ server ])
33 }) 29 })
@@ -36,64 +32,68 @@ describe('Test server follows API validators', function () {
36 let userAccessToken = null 32 let userAccessToken = null
37 33
38 before(async function () { 34 before(async function () {
39 const user = { 35 userAccessToken = await server.users.generateUserAndToken('user1')
40 username: 'user1',
41 password: 'password'
42 }
43
44 await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
45 userAccessToken = await userLogin(server, user)
46 }) 36 })
47 37
48 describe('When adding follows', function () { 38 describe('When adding follows', function () {
49 const path = '/api/v1/server/following' 39 const path = '/api/v1/server/following'
50 40
51 it('Should fail without hosts', async function () { 41 it('Should fail with nothing', async function () {
52 await makePostBodyRequest({ 42 await makePostBodyRequest({
53 url: server.url, 43 url: server.url,
54 path, 44 path,
55 token: server.accessToken, 45 token: server.accessToken,
56 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 46 expectedStatus: HttpStatusCode.BAD_REQUEST_400
57 }) 47 })
58 }) 48 })
59 49
60 it('Should fail if hosts is not an array', async function () { 50 it('Should fail if hosts is not composed by hosts', async function () {
61 await makePostBodyRequest({ 51 await makePostBodyRequest({
62 url: server.url, 52 url: server.url,
63 path, 53 path,
54 fields: { hosts: [ 'localhost:9002', 'localhost:coucou' ] },
64 token: server.accessToken, 55 token: server.accessToken,
65 fields: { hosts: 'localhost:9002' }, 56 expectedStatus: HttpStatusCode.BAD_REQUEST_400
66 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
67 }) 57 })
68 }) 58 })
69 59
70 it('Should fail if the array is not composed by hosts', async function () { 60 it('Should fail if hosts is composed with http schemes', async function () {
71 await makePostBodyRequest({ 61 await makePostBodyRequest({
72 url: server.url, 62 url: server.url,
73 path, 63 path,
74 fields: { hosts: [ 'localhost:9002', 'localhost:coucou' ] }, 64 fields: { hosts: [ 'localhost:9002', 'http://localhost:9003' ] },
65 token: server.accessToken,
66 expectedStatus: HttpStatusCode.BAD_REQUEST_400
67 })
68 })
69
70 it('Should fail if hosts are not unique', async function () {
71 await makePostBodyRequest({
72 url: server.url,
73 path,
74 fields: { urls: [ 'localhost:9002', 'localhost:9002' ] },
75 token: server.accessToken, 75 token: server.accessToken,
76 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 76 expectedStatus: HttpStatusCode.BAD_REQUEST_400
77 }) 77 })
78 }) 78 })
79 79
80 it('Should fail if the array is composed with http schemes', async function () { 80 it('Should fail if handles is not composed by handles', async function () {
81 await makePostBodyRequest({ 81 await makePostBodyRequest({
82 url: server.url, 82 url: server.url,
83 path, 83 path,
84 fields: { hosts: [ 'localhost:9002', 'http://localhost:9003' ] }, 84 fields: { handles: [ 'hello@example.com', 'localhost:9001' ] },
85 token: server.accessToken, 85 token: server.accessToken,
86 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 86 expectedStatus: HttpStatusCode.BAD_REQUEST_400
87 }) 87 })
88 }) 88 })
89 89
90 it('Should fail if hosts are not unique', async function () { 90 it('Should fail if handles are not unique', async function () {
91 await makePostBodyRequest({ 91 await makePostBodyRequest({
92 url: server.url, 92 url: server.url,
93 path, 93 path,
94 fields: { urls: [ 'localhost:9002', 'localhost:9002' ] }, 94 fields: { urls: [ 'hello@example.com', 'hello@example.com' ] },
95 token: server.accessToken, 95 token: server.accessToken,
96 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 96 expectedStatus: HttpStatusCode.BAD_REQUEST_400
97 }) 97 })
98 }) 98 })
99 99
@@ -103,7 +103,7 @@ describe('Test server follows API validators', function () {
103 path, 103 path,
104 fields: { hosts: [ 'localhost:9002' ] }, 104 fields: { hosts: [ 'localhost:9002' ] },
105 token: 'fake_token', 105 token: 'fake_token',
106 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 106 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
107 }) 107 })
108 }) 108 })
109 109
@@ -113,7 +113,7 @@ describe('Test server follows API validators', function () {
113 path, 113 path,
114 fields: { hosts: [ 'localhost:9002' ] }, 114 fields: { hosts: [ 'localhost:9002' ] },
115 token: userAccessToken, 115 token: userAccessToken,
116 statusCodeExpected: HttpStatusCode.FORBIDDEN_403 116 expectedStatus: HttpStatusCode.FORBIDDEN_403
117 }) 117 })
118 }) 118 })
119 }) 119 })
@@ -157,7 +157,7 @@ describe('Test server follows API validators', function () {
157 await makeGetRequest({ 157 await makeGetRequest({
158 url: server.url, 158 url: server.url,
159 path, 159 path,
160 statusCodeExpected: HttpStatusCode.OK_200, 160 expectedStatus: HttpStatusCode.OK_200,
161 query: { 161 query: {
162 state: 'accepted', 162 state: 'accepted',
163 actorType: 'Application' 163 actorType: 'Application'
@@ -206,7 +206,7 @@ describe('Test server follows API validators', function () {
206 await makeGetRequest({ 206 await makeGetRequest({
207 url: server.url, 207 url: server.url,
208 path, 208 path,
209 statusCodeExpected: HttpStatusCode.OK_200, 209 expectedStatus: HttpStatusCode.OK_200,
210 query: { 210 query: {
211 state: 'accepted' 211 state: 'accepted'
212 } 212 }
@@ -222,7 +222,7 @@ describe('Test server follows API validators', function () {
222 url: server.url, 222 url: server.url,
223 path: path + '/toto@localhost:9002', 223 path: path + '/toto@localhost:9002',
224 token: 'fake_token', 224 token: 'fake_token',
225 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 225 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
226 }) 226 })
227 }) 227 })
228 228
@@ -231,7 +231,7 @@ describe('Test server follows API validators', function () {
231 url: server.url, 231 url: server.url,
232 path: path + '/toto@localhost:9002', 232 path: path + '/toto@localhost:9002',
233 token: userAccessToken, 233 token: userAccessToken,
234 statusCodeExpected: HttpStatusCode.FORBIDDEN_403 234 expectedStatus: HttpStatusCode.FORBIDDEN_403
235 }) 235 })
236 }) 236 })
237 237
@@ -240,7 +240,7 @@ describe('Test server follows API validators', function () {
240 url: server.url, 240 url: server.url,
241 path: path + '/toto', 241 path: path + '/toto',
242 token: server.accessToken, 242 token: server.accessToken,
243 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 243 expectedStatus: HttpStatusCode.BAD_REQUEST_400
244 }) 244 })
245 }) 245 })
246 246
@@ -249,7 +249,7 @@ describe('Test server follows API validators', function () {
249 url: server.url, 249 url: server.url,
250 path: path + '/toto@localhost:9003', 250 path: path + '/toto@localhost:9003',
251 token: server.accessToken, 251 token: server.accessToken,
252 statusCodeExpected: HttpStatusCode.NOT_FOUND_404 252 expectedStatus: HttpStatusCode.NOT_FOUND_404
253 }) 253 })
254 }) 254 })
255 }) 255 })
@@ -262,7 +262,7 @@ describe('Test server follows API validators', function () {
262 url: server.url, 262 url: server.url,
263 path: path + '/toto@localhost:9002/accept', 263 path: path + '/toto@localhost:9002/accept',
264 token: 'fake_token', 264 token: 'fake_token',
265 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 265 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
266 }) 266 })
267 }) 267 })
268 268
@@ -271,7 +271,7 @@ describe('Test server follows API validators', function () {
271 url: server.url, 271 url: server.url,
272 path: path + '/toto@localhost:9002/accept', 272 path: path + '/toto@localhost:9002/accept',
273 token: userAccessToken, 273 token: userAccessToken,
274 statusCodeExpected: HttpStatusCode.FORBIDDEN_403 274 expectedStatus: HttpStatusCode.FORBIDDEN_403
275 }) 275 })
276 }) 276 })
277 277
@@ -280,7 +280,7 @@ describe('Test server follows API validators', function () {
280 url: server.url, 280 url: server.url,
281 path: path + '/toto/accept', 281 path: path + '/toto/accept',
282 token: server.accessToken, 282 token: server.accessToken,
283 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 283 expectedStatus: HttpStatusCode.BAD_REQUEST_400
284 }) 284 })
285 }) 285 })
286 286
@@ -289,7 +289,7 @@ describe('Test server follows API validators', function () {
289 url: server.url, 289 url: server.url,
290 path: path + '/toto@localhost:9003/accept', 290 path: path + '/toto@localhost:9003/accept',
291 token: server.accessToken, 291 token: server.accessToken,
292 statusCodeExpected: HttpStatusCode.NOT_FOUND_404 292 expectedStatus: HttpStatusCode.NOT_FOUND_404
293 }) 293 })
294 }) 294 })
295 }) 295 })
@@ -302,7 +302,7 @@ describe('Test server follows API validators', function () {
302 url: server.url, 302 url: server.url,
303 path: path + '/toto@localhost:9002/reject', 303 path: path + '/toto@localhost:9002/reject',
304 token: 'fake_token', 304 token: 'fake_token',
305 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 305 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
306 }) 306 })
307 }) 307 })
308 308
@@ -311,7 +311,7 @@ describe('Test server follows API validators', function () {
311 url: server.url, 311 url: server.url,
312 path: path + '/toto@localhost:9002/reject', 312 path: path + '/toto@localhost:9002/reject',
313 token: userAccessToken, 313 token: userAccessToken,
314 statusCodeExpected: HttpStatusCode.FORBIDDEN_403 314 expectedStatus: HttpStatusCode.FORBIDDEN_403
315 }) 315 })
316 }) 316 })
317 317
@@ -320,7 +320,7 @@ describe('Test server follows API validators', function () {
320 url: server.url, 320 url: server.url,
321 path: path + '/toto/reject', 321 path: path + '/toto/reject',
322 token: server.accessToken, 322 token: server.accessToken,
323 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 323 expectedStatus: HttpStatusCode.BAD_REQUEST_400
324 }) 324 })
325 }) 325 })
326 326
@@ -329,7 +329,7 @@ describe('Test server follows API validators', function () {
329 url: server.url, 329 url: server.url,
330 path: path + '/toto@localhost:9003/reject', 330 path: path + '/toto@localhost:9003/reject',
331 token: server.accessToken, 331 token: server.accessToken,
332 statusCodeExpected: HttpStatusCode.NOT_FOUND_404 332 expectedStatus: HttpStatusCode.NOT_FOUND_404
333 }) 333 })
334 }) 334 })
335 }) 335 })
@@ -342,7 +342,7 @@ describe('Test server follows API validators', function () {
342 url: server.url, 342 url: server.url,
343 path: path + '/localhost:9002', 343 path: path + '/localhost:9002',
344 token: 'fake_token', 344 token: 'fake_token',
345 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 345 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
346 }) 346 })
347 }) 347 })
348 348
@@ -351,7 +351,7 @@ describe('Test server follows API validators', function () {
351 url: server.url, 351 url: server.url,
352 path: path + '/localhost:9002', 352 path: path + '/localhost:9002',
353 token: userAccessToken, 353 token: userAccessToken,
354 statusCodeExpected: HttpStatusCode.FORBIDDEN_403 354 expectedStatus: HttpStatusCode.FORBIDDEN_403
355 }) 355 })
356 }) 356 })
357 357
@@ -360,7 +360,7 @@ describe('Test server follows API validators', function () {
360 url: server.url, 360 url: server.url,
361 path: path + '/example.com', 361 path: path + '/example.com',
362 token: server.accessToken, 362 token: server.accessToken,
363 statusCodeExpected: HttpStatusCode.NOT_FOUND_404 363 expectedStatus: HttpStatusCode.NOT_FOUND_404
364 }) 364 })
365 }) 365 })
366 }) 366 })
diff --git a/server/tests/api/check-params/jobs.ts b/server/tests/api/check-params/jobs.ts
index 3c1d2049b..23d95d8e4 100644
--- a/server/tests/api/check-params/jobs.ts
+++ b/server/tests/api/check-params/jobs.ts
@@ -1,26 +1,21 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2 2
3import 'mocha' 3import 'mocha'
4
5import {
6 cleanupTests,
7 createUser,
8 flushAndRunServer,
9 ServerInfo,
10 setAccessTokensToServers,
11 userLogin
12} from '../../../../shared/extra-utils'
13import { 4import {
14 checkBadCountPagination, 5 checkBadCountPagination,
15 checkBadSortPagination, 6 checkBadSortPagination,
16 checkBadStartPagination 7 checkBadStartPagination,
17} from '../../../../shared/extra-utils/requests/check-api-params' 8 cleanupTests,
18import { makeGetRequest } from '../../../../shared/extra-utils/requests/requests' 9 createSingleServer,
19import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes' 10 makeGetRequest,
11 PeerTubeServer,
12 setAccessTokensToServers
13} from '@shared/extra-utils'
14import { HttpStatusCode } from '@shared/models'
20 15
21describe('Test jobs API validators', function () { 16describe('Test jobs API validators', function () {
22 const path = '/api/v1/jobs/failed' 17 const path = '/api/v1/jobs/failed'
23 let server: ServerInfo 18 let server: PeerTubeServer
24 let userAccessToken = '' 19 let userAccessToken = ''
25 20
26 // --------------------------------------------------------------- 21 // ---------------------------------------------------------------
@@ -28,7 +23,7 @@ describe('Test jobs API validators', function () {
28 before(async function () { 23 before(async function () {
29 this.timeout(120000) 24 this.timeout(120000)
30 25
31 server = await flushAndRunServer(1) 26 server = await createSingleServer(1)
32 27
33 await setAccessTokensToServers([ server ]) 28 await setAccessTokensToServers([ server ])
34 29
@@ -36,8 +31,8 @@ describe('Test jobs API validators', function () {
36 username: 'user1', 31 username: 'user1',
37 password: 'my super password' 32 password: 'my super password'
38 } 33 }
39 await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password }) 34 await server.users.create({ username: user.username, password: user.password })
40 userAccessToken = await userLogin(server, user) 35 userAccessToken = await server.login.getAccessToken(user)
41 }) 36 })
42 37
43 describe('When listing jobs', function () { 38 describe('When listing jobs', function () {
@@ -77,7 +72,7 @@ describe('Test jobs API validators', function () {
77 await makeGetRequest({ 72 await makeGetRequest({
78 url: server.url, 73 url: server.url,
79 path, 74 path,
80 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 75 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
81 }) 76 })
82 }) 77 })
83 78
@@ -86,7 +81,7 @@ describe('Test jobs API validators', function () {
86 url: server.url, 81 url: server.url,
87 path, 82 path,
88 token: userAccessToken, 83 token: userAccessToken,
89 statusCodeExpected: HttpStatusCode.FORBIDDEN_403 84 expectedStatus: HttpStatusCode.FORBIDDEN_403
90 }) 85 })
91 }) 86 })
92 87
diff --git a/server/tests/api/check-params/live.ts b/server/tests/api/check-params/live.ts
index 933d8abf2..700b4724d 100644
--- a/server/tests/api/check-params/live.ts
+++ b/server/tests/api/check-params/live.ts
@@ -2,69 +2,64 @@
2 2
3import 'mocha' 3import 'mocha'
4import { omit } from 'lodash' 4import { omit } from 'lodash'
5import { LiveVideo, VideoCreateResult, VideoPrivacy } from '@shared/models'
6import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
7import { 5import {
8 buildAbsoluteFixturePath, 6 buildAbsoluteFixturePath,
9 cleanupTests, 7 cleanupTests,
10 createUser, 8 createSingleServer,
11 flushAndRunServer, 9 LiveCommand,
12 getLive,
13 getMyUserInformation,
14 immutableAssign,
15 makePostBodyRequest, 10 makePostBodyRequest,
16 makeUploadRequest, 11 makeUploadRequest,
17 runAndTestFfmpegStreamError, 12 PeerTubeServer,
18 sendRTMPStream, 13 sendRTMPStream,
19 ServerInfo,
20 setAccessTokensToServers, 14 setAccessTokensToServers,
21 stopFfmpeg, 15 stopFfmpeg
22 updateCustomSubConfig, 16} from '@shared/extra-utils'
23 updateLive, 17import { HttpStatusCode, VideoCreateResult, VideoPrivacy } from '@shared/models'
24 uploadVideoAndGetId,
25 userLogin,
26 waitUntilLivePublished
27} from '../../../../shared/extra-utils'
28 18
29describe('Test video lives API validator', function () { 19describe('Test video lives API validator', function () {
30 const path = '/api/v1/videos/live' 20 const path = '/api/v1/videos/live'
31 let server: ServerInfo 21 let server: PeerTubeServer
32 let userAccessToken = '' 22 let userAccessToken = ''
33 let channelId: number 23 let channelId: number
34 let video: VideoCreateResult 24 let video: VideoCreateResult
35 let videoIdNotLive: number 25 let videoIdNotLive: number
26 let command: LiveCommand
36 27
37 // --------------------------------------------------------------- 28 // ---------------------------------------------------------------
38 29
39 before(async function () { 30 before(async function () {
40 this.timeout(30000) 31 this.timeout(30000)
41 32
42 server = await flushAndRunServer(1) 33 server = await createSingleServer(1)
43 34
44 await setAccessTokensToServers([ server ]) 35 await setAccessTokensToServers([ server ])
45 36
46 await updateCustomSubConfig(server.url, server.accessToken, { 37 await server.config.updateCustomSubConfig({
47 live: { 38 newConfig: {
48 enabled: true, 39 live: {
49 maxInstanceLives: 20, 40 enabled: true,
50 maxUserLives: 20, 41 maxInstanceLives: 20,
51 allowReplay: true 42 maxUserLives: 20,
43 allowReplay: true
44 }
52 } 45 }
53 }) 46 })
54 47
55 const username = 'user1' 48 const username = 'user1'
56 const password = 'my super password' 49 const password = 'my super password'
57 await createUser({ url: server.url, accessToken: server.accessToken, username: username, password: password }) 50 await server.users.create({ username: username, password: password })
58 userAccessToken = await userLogin(server, { username, password }) 51 userAccessToken = await server.login.getAccessToken({ username, password })
59 52
60 { 53 {
61 const res = await getMyUserInformation(server.url, server.accessToken) 54 const { videoChannels } = await server.users.getMyInfo()
62 channelId = res.body.videoChannels[0].id 55 channelId = videoChannels[0].id
63 } 56 }
64 57
65 { 58 {
66 videoIdNotLive = (await uploadVideoAndGetId({ server, videoName: 'not live' })).id 59 videoIdNotLive = (await server.videos.quickUpload({ name: 'not live' })).id
67 } 60 }
61
62 command = server.live
68 }) 63 })
69 64
70 describe('When creating a live', function () { 65 describe('When creating a live', function () {
@@ -96,37 +91,37 @@ describe('Test video lives API validator', function () {
96 }) 91 })
97 92
98 it('Should fail with a long name', async function () { 93 it('Should fail with a long name', async function () {
99 const fields = immutableAssign(baseCorrectParams, { name: 'super'.repeat(65) }) 94 const fields = { ...baseCorrectParams, name: 'super'.repeat(65) }
100 95
101 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) 96 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
102 }) 97 })
103 98
104 it('Should fail with a bad category', async function () { 99 it('Should fail with a bad category', async function () {
105 const fields = immutableAssign(baseCorrectParams, { category: 125 }) 100 const fields = { ...baseCorrectParams, category: 125 }
106 101
107 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) 102 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
108 }) 103 })
109 104
110 it('Should fail with a bad licence', async function () { 105 it('Should fail with a bad licence', async function () {
111 const fields = immutableAssign(baseCorrectParams, { licence: 125 }) 106 const fields = { ...baseCorrectParams, licence: 125 }
112 107
113 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) 108 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
114 }) 109 })
115 110
116 it('Should fail with a bad language', async function () { 111 it('Should fail with a bad language', async function () {
117 const fields = immutableAssign(baseCorrectParams, { language: 'a'.repeat(15) }) 112 const fields = { ...baseCorrectParams, language: 'a'.repeat(15) }
118 113
119 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) 114 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
120 }) 115 })
121 116
122 it('Should fail with a long description', async function () { 117 it('Should fail with a long description', async function () {
123 const fields = immutableAssign(baseCorrectParams, { description: 'super'.repeat(2500) }) 118 const fields = { ...baseCorrectParams, description: 'super'.repeat(2500) }
124 119
125 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) 120 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
126 }) 121 })
127 122
128 it('Should fail with a long support text', async function () { 123 it('Should fail with a long support text', async function () {
129 const fields = immutableAssign(baseCorrectParams, { support: 'super'.repeat(201) }) 124 const fields = { ...baseCorrectParams, support: 'super'.repeat(201) }
130 125
131 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) 126 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
132 }) 127 })
@@ -138,7 +133,7 @@ describe('Test video lives API validator', function () {
138 }) 133 })
139 134
140 it('Should fail with a bad channel', async function () { 135 it('Should fail with a bad channel', async function () {
141 const fields = immutableAssign(baseCorrectParams, { channelId: 545454 }) 136 const fields = { ...baseCorrectParams, channelId: 545454 }
142 137
143 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) 138 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
144 }) 139 })
@@ -148,31 +143,31 @@ describe('Test video lives API validator', function () {
148 username: 'fake', 143 username: 'fake',
149 password: 'fake_password' 144 password: 'fake_password'
150 } 145 }
151 await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password }) 146 await server.users.create({ username: user.username, password: user.password })
152 147
153 const accessTokenUser = await userLogin(server, user) 148 const accessTokenUser = await server.login.getAccessToken(user)
154 const res = await getMyUserInformation(server.url, accessTokenUser) 149 const { videoChannels } = await server.users.getMyInfo({ token: accessTokenUser })
155 const customChannelId = res.body.videoChannels[0].id 150 const customChannelId = videoChannels[0].id
156 151
157 const fields = immutableAssign(baseCorrectParams, { channelId: customChannelId }) 152 const fields = { ...baseCorrectParams, channelId: customChannelId }
158 153
159 await makePostBodyRequest({ url: server.url, path, token: userAccessToken, fields }) 154 await makePostBodyRequest({ url: server.url, path, token: userAccessToken, fields })
160 }) 155 })
161 156
162 it('Should fail with too many tags', async function () { 157 it('Should fail with too many tags', async function () {
163 const fields = immutableAssign(baseCorrectParams, { tags: [ 'tag1', 'tag2', 'tag3', 'tag4', 'tag5', 'tag6' ] }) 158 const fields = { ...baseCorrectParams, tags: [ 'tag1', 'tag2', 'tag3', 'tag4', 'tag5', 'tag6' ] }
164 159
165 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) 160 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
166 }) 161 })
167 162
168 it('Should fail with a tag length too low', async function () { 163 it('Should fail with a tag length too low', async function () {
169 const fields = immutableAssign(baseCorrectParams, { tags: [ 'tag1', 't' ] }) 164 const fields = { ...baseCorrectParams, tags: [ 'tag1', 't' ] }
170 165
171 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) 166 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
172 }) 167 })
173 168
174 it('Should fail with a tag length too big', async function () { 169 it('Should fail with a tag length too big', async function () {
175 const fields = immutableAssign(baseCorrectParams, { tags: [ 'tag1', 'my_super_tag_too_long_long_long_long_long_long' ] }) 170 const fields = { ...baseCorrectParams, tags: [ 'tag1', 'my_super_tag_too_long_long_long_long_long_long' ] }
176 171
177 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) 172 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
178 }) 173 })
@@ -214,7 +209,7 @@ describe('Test video lives API validator', function () {
214 }) 209 })
215 210
216 it('Should fail with save replay and permanent live set to true', async function () { 211 it('Should fail with save replay and permanent live set to true', async function () {
217 const fields = immutableAssign(baseCorrectParams, { saveReplay: true, permanentLive: true }) 212 const fields = { ...baseCorrectParams, saveReplay: true, permanentLive: true }
218 213
219 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) 214 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
220 }) 215 })
@@ -227,16 +222,18 @@ describe('Test video lives API validator', function () {
227 path, 222 path,
228 token: server.accessToken, 223 token: server.accessToken,
229 fields: baseCorrectParams, 224 fields: baseCorrectParams,
230 statusCodeExpected: HttpStatusCode.OK_200 225 expectedStatus: HttpStatusCode.OK_200
231 }) 226 })
232 227
233 video = res.body.video 228 video = res.body.video
234 }) 229 })
235 230
236 it('Should forbid if live is disabled', async function () { 231 it('Should forbid if live is disabled', async function () {
237 await updateCustomSubConfig(server.url, server.accessToken, { 232 await server.config.updateCustomSubConfig({
238 live: { 233 newConfig: {
239 enabled: false 234 live: {
235 enabled: false
236 }
240 } 237 }
241 }) 238 })
242 239
@@ -245,17 +242,19 @@ describe('Test video lives API validator', function () {
245 path, 242 path,
246 token: server.accessToken, 243 token: server.accessToken,
247 fields: baseCorrectParams, 244 fields: baseCorrectParams,
248 statusCodeExpected: HttpStatusCode.FORBIDDEN_403 245 expectedStatus: HttpStatusCode.FORBIDDEN_403
249 }) 246 })
250 }) 247 })
251 248
252 it('Should forbid to save replay if not enabled by the admin', async function () { 249 it('Should forbid to save replay if not enabled by the admin', async function () {
253 const fields = immutableAssign(baseCorrectParams, { saveReplay: true }) 250 const fields = { ...baseCorrectParams, saveReplay: true }
254 251
255 await updateCustomSubConfig(server.url, server.accessToken, { 252 await server.config.updateCustomSubConfig({
256 live: { 253 newConfig: {
257 enabled: true, 254 live: {
258 allowReplay: false 255 enabled: true,
256 allowReplay: false
257 }
259 } 258 }
260 }) 259 })
261 260
@@ -264,17 +263,19 @@ describe('Test video lives API validator', function () {
264 path, 263 path,
265 token: server.accessToken, 264 token: server.accessToken,
266 fields, 265 fields,
267 statusCodeExpected: HttpStatusCode.FORBIDDEN_403 266 expectedStatus: HttpStatusCode.FORBIDDEN_403
268 }) 267 })
269 }) 268 })
270 269
271 it('Should allow to save replay if enabled by the admin', async function () { 270 it('Should allow to save replay if enabled by the admin', async function () {
272 const fields = immutableAssign(baseCorrectParams, { saveReplay: true }) 271 const fields = { ...baseCorrectParams, saveReplay: true }
273 272
274 await updateCustomSubConfig(server.url, server.accessToken, { 273 await server.config.updateCustomSubConfig({
275 live: { 274 newConfig: {
276 enabled: true, 275 live: {
277 allowReplay: true 276 enabled: true,
277 allowReplay: true
278 }
278 } 279 }
279 }) 280 })
280 281
@@ -283,15 +284,17 @@ describe('Test video lives API validator', function () {
283 path, 284 path,
284 token: server.accessToken, 285 token: server.accessToken,
285 fields, 286 fields,
286 statusCodeExpected: HttpStatusCode.OK_200 287 expectedStatus: HttpStatusCode.OK_200
287 }) 288 })
288 }) 289 })
289 290
290 it('Should not allow live if max instance lives is reached', async function () { 291 it('Should not allow live if max instance lives is reached', async function () {
291 await updateCustomSubConfig(server.url, server.accessToken, { 292 await server.config.updateCustomSubConfig({
292 live: { 293 newConfig: {
293 enabled: true, 294 live: {
294 maxInstanceLives: 1 295 enabled: true,
296 maxInstanceLives: 1
297 }
295 } 298 }
296 }) 299 })
297 300
@@ -300,16 +303,18 @@ describe('Test video lives API validator', function () {
300 path, 303 path,
301 token: server.accessToken, 304 token: server.accessToken,
302 fields: baseCorrectParams, 305 fields: baseCorrectParams,
303 statusCodeExpected: HttpStatusCode.FORBIDDEN_403 306 expectedStatus: HttpStatusCode.FORBIDDEN_403
304 }) 307 })
305 }) 308 })
306 309
307 it('Should not allow live if max user lives is reached', async function () { 310 it('Should not allow live if max user lives is reached', async function () {
308 await updateCustomSubConfig(server.url, server.accessToken, { 311 await server.config.updateCustomSubConfig({
309 live: { 312 newConfig: {
310 enabled: true, 313 live: {
311 maxInstanceLives: 20, 314 enabled: true,
312 maxUserLives: 1 315 maxInstanceLives: 20,
316 maxUserLives: 1
317 }
313 } 318 }
314 }) 319 })
315 320
@@ -318,7 +323,7 @@ describe('Test video lives API validator', function () {
318 path, 323 path,
319 token: server.accessToken, 324 token: server.accessToken,
320 fields: baseCorrectParams, 325 fields: baseCorrectParams,
321 statusCodeExpected: HttpStatusCode.FORBIDDEN_403 326 expectedStatus: HttpStatusCode.FORBIDDEN_403
322 }) 327 })
323 }) 328 })
324 }) 329 })
@@ -326,110 +331,112 @@ describe('Test video lives API validator', function () {
326 describe('When getting live information', function () { 331 describe('When getting live information', function () {
327 332
328 it('Should fail without access token', async function () { 333 it('Should fail without access token', async function () {
329 await getLive(server.url, '', video.id, HttpStatusCode.UNAUTHORIZED_401) 334 await command.get({ token: '', videoId: video.id, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
330 }) 335 })
331 336
332 it('Should fail with a bad access token', async function () { 337 it('Should fail with a bad access token', async function () {
333 await getLive(server.url, 'toto', video.id, HttpStatusCode.UNAUTHORIZED_401) 338 await command.get({ token: 'toto', videoId: video.id, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
334 }) 339 })
335 340
336 it('Should fail with access token of another user', async function () { 341 it('Should fail with access token of another user', async function () {
337 await getLive(server.url, userAccessToken, video.id, HttpStatusCode.FORBIDDEN_403) 342 await command.get({ token: userAccessToken, videoId: video.id, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
338 }) 343 })
339 344
340 it('Should fail with a bad video id', async function () { 345 it('Should fail with a bad video id', async function () {
341 await getLive(server.url, server.accessToken, 'toto', HttpStatusCode.BAD_REQUEST_400) 346 await command.get({ videoId: 'toto', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
342 }) 347 })
343 348
344 it('Should fail with an unknown video id', async function () { 349 it('Should fail with an unknown video id', async function () {
345 await getLive(server.url, server.accessToken, 454555, HttpStatusCode.NOT_FOUND_404) 350 await command.get({ videoId: 454555, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
346 }) 351 })
347 352
348 it('Should fail with a non live video', async function () { 353 it('Should fail with a non live video', async function () {
349 await getLive(server.url, server.accessToken, videoIdNotLive, HttpStatusCode.NOT_FOUND_404) 354 await command.get({ videoId: videoIdNotLive, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
350 }) 355 })
351 356
352 it('Should succeed with the correct params', async function () { 357 it('Should succeed with the correct params', async function () {
353 await getLive(server.url, server.accessToken, video.id) 358 await command.get({ videoId: video.id })
354 await getLive(server.url, server.accessToken, video.shortUUID) 359 await command.get({ videoId: video.uuid })
360 await command.get({ videoId: video.shortUUID })
355 }) 361 })
356 }) 362 })
357 363
358 describe('When updating live information', async function () { 364 describe('When updating live information', async function () {
359 365
360 it('Should fail without access token', async function () { 366 it('Should fail without access token', async function () {
361 await updateLive(server.url, '', video.id, {}, HttpStatusCode.UNAUTHORIZED_401) 367 await command.update({ token: '', videoId: video.id, fields: {}, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
362 }) 368 })
363 369
364 it('Should fail with a bad access token', async function () { 370 it('Should fail with a bad access token', async function () {
365 await updateLive(server.url, 'toto', video.id, {}, HttpStatusCode.UNAUTHORIZED_401) 371 await command.update({ token: 'toto', videoId: video.id, fields: {}, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
366 }) 372 })
367 373
368 it('Should fail with access token of another user', async function () { 374 it('Should fail with access token of another user', async function () {
369 await updateLive(server.url, userAccessToken, video.id, {}, HttpStatusCode.FORBIDDEN_403) 375 await command.update({ token: userAccessToken, videoId: video.id, fields: {}, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
370 }) 376 })
371 377
372 it('Should fail with a bad video id', async function () { 378 it('Should fail with a bad video id', async function () {
373 await updateLive(server.url, server.accessToken, 'toto', {}, HttpStatusCode.BAD_REQUEST_400) 379 await command.update({ videoId: 'toto', fields: {}, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
374 }) 380 })
375 381
376 it('Should fail with an unknown video id', async function () { 382 it('Should fail with an unknown video id', async function () {
377 await updateLive(server.url, server.accessToken, 454555, {}, HttpStatusCode.NOT_FOUND_404) 383 await command.update({ videoId: 454555, fields: {}, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
378 }) 384 })
379 385
380 it('Should fail with a non live video', async function () { 386 it('Should fail with a non live video', async function () {
381 await updateLive(server.url, server.accessToken, videoIdNotLive, {}, HttpStatusCode.NOT_FOUND_404) 387 await command.update({ videoId: videoIdNotLive, fields: {}, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
382 }) 388 })
383 389
384 it('Should fail with save replay and permanent live set to true', async function () { 390 it('Should fail with save replay and permanent live set to true', async function () {
385 const fields = { saveReplay: true, permanentLive: true } 391 const fields = { saveReplay: true, permanentLive: true }
386 392
387 await updateLive(server.url, server.accessToken, video.id, fields, HttpStatusCode.BAD_REQUEST_400) 393 await command.update({ videoId: video.id, fields, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
388 }) 394 })
389 395
390 it('Should succeed with the correct params', async function () { 396 it('Should succeed with the correct params', async function () {
391 await updateLive(server.url, server.accessToken, video.id, { saveReplay: false }) 397 await command.update({ videoId: video.id, fields: { saveReplay: false } })
392 await updateLive(server.url, server.accessToken, video.shortUUID, { saveReplay: false }) 398 await command.update({ videoId: video.uuid, fields: { saveReplay: false } })
399 await command.update({ videoId: video.shortUUID, fields: { saveReplay: false } })
393 }) 400 })
394 401
395 it('Should fail to update replay status if replay is not allowed on the instance', async function () { 402 it('Should fail to update replay status if replay is not allowed on the instance', async function () {
396 await updateCustomSubConfig(server.url, server.accessToken, { 403 await server.config.updateCustomSubConfig({
397 live: { 404 newConfig: {
398 enabled: true, 405 live: {
399 allowReplay: false 406 enabled: true,
407 allowReplay: false
408 }
400 } 409 }
401 }) 410 })
402 411
403 await updateLive(server.url, server.accessToken, video.id, { saveReplay: true }, HttpStatusCode.FORBIDDEN_403) 412 await command.update({ videoId: video.id, fields: { saveReplay: true }, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
404 }) 413 })
405 414
406 it('Should fail to update a live if it has already started', async function () { 415 it('Should fail to update a live if it has already started', async function () {
407 this.timeout(40000) 416 this.timeout(40000)
408 417
409 const resLive = await getLive(server.url, server.accessToken, video.id) 418 const live = await command.get({ videoId: video.id })
410 const live: LiveVideo = resLive.body
411 419
412 const command = sendRTMPStream(live.rtmpUrl, live.streamKey) 420 const ffmpegCommand = sendRTMPStream(live.rtmpUrl, live.streamKey)
413 421
414 await waitUntilLivePublished(server.url, server.accessToken, video.id) 422 await command.waitUntilPublished({ videoId: video.id })
415 await updateLive(server.url, server.accessToken, video.id, {}, HttpStatusCode.BAD_REQUEST_400) 423 await command.update({ videoId: video.id, fields: {}, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
416 424
417 await stopFfmpeg(command) 425 await stopFfmpeg(ffmpegCommand)
418 }) 426 })
419 427
420 it('Should fail to stream twice in the save live', async function () { 428 it('Should fail to stream twice in the save live', async function () {
421 this.timeout(40000) 429 this.timeout(40000)
422 430
423 const resLive = await getLive(server.url, server.accessToken, video.id) 431 const live = await command.get({ videoId: video.id })
424 const live: LiveVideo = resLive.body
425 432
426 const command = sendRTMPStream(live.rtmpUrl, live.streamKey) 433 const ffmpegCommand = sendRTMPStream(live.rtmpUrl, live.streamKey)
427 434
428 await waitUntilLivePublished(server.url, server.accessToken, video.id) 435 await command.waitUntilPublished({ videoId: video.id })
429 436
430 await runAndTestFfmpegStreamError(server.url, server.accessToken, video.id, true) 437 await command.runAndTestStreamError({ videoId: video.id, shouldHaveError: true })
431 438
432 await stopFfmpeg(command) 439 await stopFfmpeg(ffmpegCommand)
433 }) 440 })
434 }) 441 })
435 442
diff --git a/server/tests/api/check-params/logs.ts b/server/tests/api/check-params/logs.ts
index dac1e6b98..05372257a 100644
--- a/server/tests/api/check-params/logs.ts
+++ b/server/tests/api/check-params/logs.ts
@@ -1,21 +1,12 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2 2
3import 'mocha' 3import 'mocha'
4 4import { cleanupTests, createSingleServer, makeGetRequest, PeerTubeServer, setAccessTokensToServers } from '@shared/extra-utils'
5import { 5import { HttpStatusCode } from '@shared/models'
6 cleanupTests,
7 createUser,
8 flushAndRunServer,
9 ServerInfo,
10 setAccessTokensToServers,
11 userLogin
12} from '../../../../shared/extra-utils'
13import { makeGetRequest } from '../../../../shared/extra-utils/requests/requests'
14import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
15 6
16describe('Test logs API validators', function () { 7describe('Test logs API validators', function () {
17 const path = '/api/v1/server/logs' 8 const path = '/api/v1/server/logs'
18 let server: ServerInfo 9 let server: PeerTubeServer
19 let userAccessToken = '' 10 let userAccessToken = ''
20 11
21 // --------------------------------------------------------------- 12 // ---------------------------------------------------------------
@@ -23,7 +14,7 @@ describe('Test logs API validators', function () {
23 before(async function () { 14 before(async function () {
24 this.timeout(120000) 15 this.timeout(120000)
25 16
26 server = await flushAndRunServer(1) 17 server = await createSingleServer(1)
27 18
28 await setAccessTokensToServers([ server ]) 19 await setAccessTokensToServers([ server ])
29 20
@@ -31,8 +22,8 @@ describe('Test logs API validators', function () {
31 username: 'user1', 22 username: 'user1',
32 password: 'my super password' 23 password: 'my super password'
33 } 24 }
34 await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password }) 25 await server.users.create({ username: user.username, password: user.password })
35 userAccessToken = await userLogin(server, user) 26 userAccessToken = await server.login.getAccessToken(user)
36 }) 27 })
37 28
38 describe('When getting logs', function () { 29 describe('When getting logs', function () {
@@ -41,7 +32,7 @@ describe('Test logs API validators', function () {
41 await makeGetRequest({ 32 await makeGetRequest({
42 url: server.url, 33 url: server.url,
43 path, 34 path,
44 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 35 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
45 }) 36 })
46 }) 37 })
47 38
@@ -50,7 +41,7 @@ describe('Test logs API validators', function () {
50 url: server.url, 41 url: server.url,
51 path, 42 path,
52 token: userAccessToken, 43 token: userAccessToken,
53 statusCodeExpected: HttpStatusCode.FORBIDDEN_403 44 expectedStatus: HttpStatusCode.FORBIDDEN_403
54 }) 45 })
55 }) 46 })
56 47
@@ -59,7 +50,7 @@ describe('Test logs API validators', function () {
59 url: server.url, 50 url: server.url,
60 path, 51 path,
61 token: server.accessToken, 52 token: server.accessToken,
62 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 53 expectedStatus: HttpStatusCode.BAD_REQUEST_400
63 }) 54 })
64 }) 55 })
65 56
@@ -69,7 +60,7 @@ describe('Test logs API validators', function () {
69 path, 60 path,
70 token: server.accessToken, 61 token: server.accessToken,
71 query: { startDate: 'toto' }, 62 query: { startDate: 'toto' },
72 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 63 expectedStatus: HttpStatusCode.BAD_REQUEST_400
73 }) 64 })
74 }) 65 })
75 66
@@ -79,7 +70,7 @@ describe('Test logs API validators', function () {
79 path, 70 path,
80 token: server.accessToken, 71 token: server.accessToken,
81 query: { startDate: new Date().toISOString(), endDate: 'toto' }, 72 query: { startDate: new Date().toISOString(), endDate: 'toto' },
82 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 73 expectedStatus: HttpStatusCode.BAD_REQUEST_400
83 }) 74 })
84 }) 75 })
85 76
@@ -89,7 +80,7 @@ describe('Test logs API validators', function () {
89 path, 80 path,
90 token: server.accessToken, 81 token: server.accessToken,
91 query: { startDate: new Date().toISOString(), level: 'toto' }, 82 query: { startDate: new Date().toISOString(), level: 'toto' },
92 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 83 expectedStatus: HttpStatusCode.BAD_REQUEST_400
93 }) 84 })
94 }) 85 })
95 86
@@ -99,7 +90,7 @@ describe('Test logs API validators', function () {
99 path, 90 path,
100 token: server.accessToken, 91 token: server.accessToken,
101 query: { startDate: new Date().toISOString() }, 92 query: { startDate: new Date().toISOString() },
102 statusCodeExpected: HttpStatusCode.OK_200 93 expectedStatus: HttpStatusCode.OK_200
103 }) 94 })
104 }) 95 })
105 }) 96 })
diff --git a/server/tests/api/check-params/plugins.ts b/server/tests/api/check-params/plugins.ts
index a833fe6ff..33f84ecbc 100644
--- a/server/tests/api/check-params/plugins.ts
+++ b/server/tests/api/check-params/plugins.ts
@@ -1,27 +1,22 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2 2
3import 'mocha' 3import 'mocha'
4import { HttpStatusCode } from '@shared/core-utils'
5import { 4import {
6 checkBadCountPagination, 5 checkBadCountPagination,
7 checkBadSortPagination, 6 checkBadSortPagination,
8 checkBadStartPagination, 7 checkBadStartPagination,
9 cleanupTests, 8 cleanupTests,
10 createUser, 9 createSingleServer,
11 flushAndRunServer,
12 immutableAssign,
13 installPlugin,
14 makeGetRequest, 10 makeGetRequest,
15 makePostBodyRequest, 11 makePostBodyRequest,
16 makePutBodyRequest, 12 makePutBodyRequest,
17 ServerInfo, 13 PeerTubeServer,
18 setAccessTokensToServers, 14 setAccessTokensToServers
19 userLogin
20} from '@shared/extra-utils' 15} from '@shared/extra-utils'
21import { PeerTubePlugin, PluginType } from '@shared/models' 16import { HttpStatusCode, PeerTubePlugin, PluginType } from '@shared/models'
22 17
23describe('Test server plugins API validators', function () { 18describe('Test server plugins API validators', function () {
24 let server: ServerInfo 19 let server: PeerTubeServer
25 let userAccessToken = null 20 let userAccessToken = null
26 21
27 const npmPlugin = 'peertube-plugin-hello-world' 22 const npmPlugin = 'peertube-plugin-hello-world'
@@ -37,7 +32,7 @@ describe('Test server plugins API validators', function () {
37 before(async function () { 32 before(async function () {
38 this.timeout(30000) 33 this.timeout(30000)
39 34
40 server = await flushAndRunServer(1) 35 server = await createSingleServer(1)
41 36
42 await setAccessTokensToServers([ server ]) 37 await setAccessTokensToServers([ server ])
43 38
@@ -46,17 +41,17 @@ describe('Test server plugins API validators', function () {
46 password: 'password' 41 password: 'password'
47 } 42 }
48 43
49 await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password }) 44 await server.users.create({ username: user.username, password: user.password })
50 userAccessToken = await userLogin(server, user) 45 userAccessToken = await server.login.getAccessToken(user)
51 46
52 { 47 {
53 const res = await installPlugin({ url: server.url, accessToken: server.accessToken, npmName: npmPlugin }) 48 const res = await server.plugins.install({ npmName: npmPlugin })
54 const plugin = res.body as PeerTubePlugin 49 const plugin = res.body as PeerTubePlugin
55 npmVersion = plugin.version 50 npmVersion = plugin.version
56 } 51 }
57 52
58 { 53 {
59 const res = await installPlugin({ url: server.url, accessToken: server.accessToken, npmName: themePlugin }) 54 const res = await server.plugins.install({ npmName: themePlugin })
60 const plugin = res.body as PeerTubePlugin 55 const plugin = res.body as PeerTubePlugin
61 themeVersion = plugin.version 56 themeVersion = plugin.version
62 } 57 }
@@ -74,7 +69,7 @@ describe('Test server plugins API validators', function () {
74 ] 69 ]
75 70
76 for (const p of paths) { 71 for (const p of paths) {
77 await makeGetRequest({ url: server.url, path: p, statusCodeExpected: HttpStatusCode.NOT_FOUND_404 }) 72 await makeGetRequest({ url: server.url, path: p, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
78 } 73 }
79 }) 74 })
80 75
@@ -82,7 +77,7 @@ describe('Test server plugins API validators', function () {
82 await makeGetRequest({ 77 await makeGetRequest({
83 url: server.url, 78 url: server.url,
84 path: '/themes/' + pluginName + '/' + npmVersion + '/static/images/chocobo.png', 79 path: '/themes/' + pluginName + '/' + npmVersion + '/static/images/chocobo.png',
85 statusCodeExpected: HttpStatusCode.NOT_FOUND_404 80 expectedStatus: HttpStatusCode.NOT_FOUND_404
86 }) 81 })
87 }) 82 })
88 83
@@ -97,7 +92,7 @@ describe('Test server plugins API validators', function () {
97 ] 92 ]
98 93
99 for (const p of paths) { 94 for (const p of paths) {
100 await makeGetRequest({ url: server.url, path: p, statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 }) 95 await makeGetRequest({ url: server.url, path: p, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
101 } 96 }
102 }) 97 })
103 98
@@ -111,14 +106,14 @@ describe('Test server plugins API validators', function () {
111 ] 106 ]
112 107
113 for (const p of paths) { 108 for (const p of paths) {
114 await makeGetRequest({ url: server.url, path: p, statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 }) 109 await makeGetRequest({ url: server.url, path: p, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
115 } 110 }
116 }) 111 })
117 112
118 it('Should fail with an unknown auth name', async function () { 113 it('Should fail with an unknown auth name', async function () {
119 const path = '/plugins/' + pluginName + '/' + npmVersion + '/auth/bad-auth' 114 const path = '/plugins/' + pluginName + '/' + npmVersion + '/auth/bad-auth'
120 115
121 await makeGetRequest({ url: server.url, path, statusCodeExpected: HttpStatusCode.NOT_FOUND_404 }) 116 await makeGetRequest({ url: server.url, path, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
122 }) 117 })
123 118
124 it('Should fail with an unknown static file', async function () { 119 it('Should fail with an unknown static file', async function () {
@@ -130,7 +125,7 @@ describe('Test server plugins API validators', function () {
130 ] 125 ]
131 126
132 for (const p of paths) { 127 for (const p of paths) {
133 await makeGetRequest({ url: server.url, path: p, statusCodeExpected: HttpStatusCode.NOT_FOUND_404 }) 128 await makeGetRequest({ url: server.url, path: p, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
134 } 129 }
135 }) 130 })
136 131
@@ -138,7 +133,7 @@ describe('Test server plugins API validators', function () {
138 await makeGetRequest({ 133 await makeGetRequest({
139 url: server.url, 134 url: server.url,
140 path: '/themes/' + themeName + '/' + themeVersion + '/css/assets/fake.css', 135 path: '/themes/' + themeName + '/' + themeVersion + '/css/assets/fake.css',
141 statusCodeExpected: HttpStatusCode.NOT_FOUND_404 136 expectedStatus: HttpStatusCode.NOT_FOUND_404
142 }) 137 })
143 }) 138 })
144 139
@@ -152,11 +147,11 @@ describe('Test server plugins API validators', function () {
152 ] 147 ]
153 148
154 for (const p of paths) { 149 for (const p of paths) {
155 await makeGetRequest({ url: server.url, path: p, statusCodeExpected: HttpStatusCode.OK_200 }) 150 await makeGetRequest({ url: server.url, path: p, expectedStatus: HttpStatusCode.OK_200 })
156 } 151 }
157 152
158 const authPath = '/plugins/' + pluginName + '/' + npmVersion + '/auth/fake-auth' 153 const authPath = '/plugins/' + pluginName + '/' + npmVersion + '/auth/fake-auth'
159 await makeGetRequest({ url: server.url, path: authPath, statusCodeExpected: HttpStatusCode.FOUND_302 }) 154 await makeGetRequest({ url: server.url, path: authPath, expectedStatus: HttpStatusCode.FOUND_302 })
160 }) 155 })
161 }) 156 })
162 157
@@ -174,7 +169,7 @@ describe('Test server plugins API validators', function () {
174 path, 169 path,
175 token: 'fake_token', 170 token: 'fake_token',
176 query: baseQuery, 171 query: baseQuery,
177 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 172 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
178 }) 173 })
179 }) 174 })
180 175
@@ -184,7 +179,7 @@ describe('Test server plugins API validators', function () {
184 path, 179 path,
185 token: userAccessToken, 180 token: userAccessToken,
186 query: baseQuery, 181 query: baseQuery,
187 statusCodeExpected: HttpStatusCode.FORBIDDEN_403 182 expectedStatus: HttpStatusCode.FORBIDDEN_403
188 }) 183 })
189 }) 184 })
190 185
@@ -201,7 +196,7 @@ describe('Test server plugins API validators', function () {
201 }) 196 })
202 197
203 it('Should fail with an invalid plugin type', async function () { 198 it('Should fail with an invalid plugin type', async function () {
204 const query = immutableAssign(baseQuery, { pluginType: 5 }) 199 const query = { ...baseQuery, pluginType: 5 }
205 200
206 await makeGetRequest({ 201 await makeGetRequest({
207 url: server.url, 202 url: server.url,
@@ -212,7 +207,7 @@ describe('Test server plugins API validators', function () {
212 }) 207 })
213 208
214 it('Should fail with an invalid current peertube engine', async function () { 209 it('Should fail with an invalid current peertube engine', async function () {
215 const query = immutableAssign(baseQuery, { currentPeerTubeEngine: '1.0' }) 210 const query = { ...baseQuery, currentPeerTubeEngine: '1.0' }
216 211
217 await makeGetRequest({ 212 await makeGetRequest({
218 url: server.url, 213 url: server.url,
@@ -228,7 +223,7 @@ describe('Test server plugins API validators', function () {
228 path, 223 path,
229 token: server.accessToken, 224 token: server.accessToken,
230 query: baseQuery, 225 query: baseQuery,
231 statusCodeExpected: HttpStatusCode.OK_200 226 expectedStatus: HttpStatusCode.OK_200
232 }) 227 })
233 }) 228 })
234 }) 229 })
@@ -245,7 +240,7 @@ describe('Test server plugins API validators', function () {
245 path, 240 path,
246 token: 'fake_token', 241 token: 'fake_token',
247 query: baseQuery, 242 query: baseQuery,
248 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 243 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
249 }) 244 })
250 }) 245 })
251 246
@@ -255,7 +250,7 @@ describe('Test server plugins API validators', function () {
255 path, 250 path,
256 token: userAccessToken, 251 token: userAccessToken,
257 query: baseQuery, 252 query: baseQuery,
258 statusCodeExpected: HttpStatusCode.FORBIDDEN_403 253 expectedStatus: HttpStatusCode.FORBIDDEN_403
259 }) 254 })
260 }) 255 })
261 256
@@ -272,7 +267,7 @@ describe('Test server plugins API validators', function () {
272 }) 267 })
273 268
274 it('Should fail with an invalid plugin type', async function () { 269 it('Should fail with an invalid plugin type', async function () {
275 const query = immutableAssign(baseQuery, { pluginType: 5 }) 270 const query = { ...baseQuery, pluginType: 5 }
276 271
277 await makeGetRequest({ 272 await makeGetRequest({
278 url: server.url, 273 url: server.url,
@@ -288,7 +283,7 @@ describe('Test server plugins API validators', function () {
288 path, 283 path,
289 token: server.accessToken, 284 token: server.accessToken,
290 query: baseQuery, 285 query: baseQuery,
291 statusCodeExpected: HttpStatusCode.OK_200 286 expectedStatus: HttpStatusCode.OK_200
292 }) 287 })
293 }) 288 })
294 }) 289 })
@@ -302,7 +297,7 @@ describe('Test server plugins API validators', function () {
302 url: server.url, 297 url: server.url,
303 path: path + suffix, 298 path: path + suffix,
304 token: 'fake_token', 299 token: 'fake_token',
305 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 300 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
306 }) 301 })
307 } 302 }
308 }) 303 })
@@ -313,7 +308,7 @@ describe('Test server plugins API validators', function () {
313 url: server.url, 308 url: server.url,
314 path: path + suffix, 309 path: path + suffix,
315 token: userAccessToken, 310 token: userAccessToken,
316 statusCodeExpected: HttpStatusCode.FORBIDDEN_403 311 expectedStatus: HttpStatusCode.FORBIDDEN_403
317 }) 312 })
318 } 313 }
319 }) 314 })
@@ -324,7 +319,7 @@ describe('Test server plugins API validators', function () {
324 url: server.url, 319 url: server.url,
325 path: path + suffix, 320 path: path + suffix,
326 token: server.accessToken, 321 token: server.accessToken,
327 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 322 expectedStatus: HttpStatusCode.BAD_REQUEST_400
328 }) 323 })
329 } 324 }
330 325
@@ -333,7 +328,7 @@ describe('Test server plugins API validators', function () {
333 url: server.url, 328 url: server.url,
334 path: path + suffix, 329 path: path + suffix,
335 token: server.accessToken, 330 token: server.accessToken,
336 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 331 expectedStatus: HttpStatusCode.BAD_REQUEST_400
337 }) 332 })
338 } 333 }
339 }) 334 })
@@ -344,7 +339,7 @@ describe('Test server plugins API validators', function () {
344 url: server.url, 339 url: server.url,
345 path: path + suffix, 340 path: path + suffix,
346 token: server.accessToken, 341 token: server.accessToken,
347 statusCodeExpected: HttpStatusCode.NOT_FOUND_404 342 expectedStatus: HttpStatusCode.NOT_FOUND_404
348 }) 343 })
349 } 344 }
350 }) 345 })
@@ -355,7 +350,7 @@ describe('Test server plugins API validators', function () {
355 url: server.url, 350 url: server.url,
356 path: path + suffix, 351 path: path + suffix,
357 token: server.accessToken, 352 token: server.accessToken,
358 statusCodeExpected: HttpStatusCode.OK_200 353 expectedStatus: HttpStatusCode.OK_200
359 }) 354 })
360 } 355 }
361 }) 356 })
@@ -371,7 +366,7 @@ describe('Test server plugins API validators', function () {
371 path: path + npmPlugin + '/settings', 366 path: path + npmPlugin + '/settings',
372 fields: { settings }, 367 fields: { settings },
373 token: 'fake_token', 368 token: 'fake_token',
374 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 369 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
375 }) 370 })
376 }) 371 })
377 372
@@ -381,7 +376,7 @@ describe('Test server plugins API validators', function () {
381 path: path + npmPlugin + '/settings', 376 path: path + npmPlugin + '/settings',
382 fields: { settings }, 377 fields: { settings },
383 token: userAccessToken, 378 token: userAccessToken,
384 statusCodeExpected: HttpStatusCode.FORBIDDEN_403 379 expectedStatus: HttpStatusCode.FORBIDDEN_403
385 }) 380 })
386 }) 381 })
387 382
@@ -391,7 +386,7 @@ describe('Test server plugins API validators', function () {
391 path: path + 'toto/settings', 386 path: path + 'toto/settings',
392 fields: { settings }, 387 fields: { settings },
393 token: server.accessToken, 388 token: server.accessToken,
394 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 389 expectedStatus: HttpStatusCode.BAD_REQUEST_400
395 }) 390 })
396 391
397 await makePutBodyRequest({ 392 await makePutBodyRequest({
@@ -399,7 +394,7 @@ describe('Test server plugins API validators', function () {
399 path: path + 'peertube-plugin-TOTO/settings', 394 path: path + 'peertube-plugin-TOTO/settings',
400 fields: { settings }, 395 fields: { settings },
401 token: server.accessToken, 396 token: server.accessToken,
402 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 397 expectedStatus: HttpStatusCode.BAD_REQUEST_400
403 }) 398 })
404 }) 399 })
405 400
@@ -409,7 +404,7 @@ describe('Test server plugins API validators', function () {
409 path: path + 'peertube-plugin-toto/settings', 404 path: path + 'peertube-plugin-toto/settings',
410 fields: { settings }, 405 fields: { settings },
411 token: server.accessToken, 406 token: server.accessToken,
412 statusCodeExpected: HttpStatusCode.NOT_FOUND_404 407 expectedStatus: HttpStatusCode.NOT_FOUND_404
413 }) 408 })
414 }) 409 })
415 410
@@ -419,7 +414,7 @@ describe('Test server plugins API validators', function () {
419 path: path + npmPlugin + '/settings', 414 path: path + npmPlugin + '/settings',
420 fields: { settings }, 415 fields: { settings },
421 token: server.accessToken, 416 token: server.accessToken,
422 statusCodeExpected: HttpStatusCode.NO_CONTENT_204 417 expectedStatus: HttpStatusCode.NO_CONTENT_204
423 }) 418 })
424 }) 419 })
425 }) 420 })
@@ -434,7 +429,7 @@ describe('Test server plugins API validators', function () {
434 path: path + suffix, 429 path: path + suffix,
435 fields: { npmName: npmPlugin }, 430 fields: { npmName: npmPlugin },
436 token: 'fake_token', 431 token: 'fake_token',
437 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 432 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
438 }) 433 })
439 } 434 }
440 }) 435 })
@@ -446,7 +441,7 @@ describe('Test server plugins API validators', function () {
446 path: path + suffix, 441 path: path + suffix,
447 fields: { npmName: npmPlugin }, 442 fields: { npmName: npmPlugin },
448 token: userAccessToken, 443 token: userAccessToken,
449 statusCodeExpected: HttpStatusCode.FORBIDDEN_403 444 expectedStatus: HttpStatusCode.FORBIDDEN_403
450 }) 445 })
451 } 446 }
452 }) 447 })
@@ -458,7 +453,7 @@ describe('Test server plugins API validators', function () {
458 path: path + suffix, 453 path: path + suffix,
459 fields: { npmName: 'toto' }, 454 fields: { npmName: 'toto' },
460 token: server.accessToken, 455 token: server.accessToken,
461 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 456 expectedStatus: HttpStatusCode.BAD_REQUEST_400
462 }) 457 })
463 } 458 }
464 459
@@ -468,7 +463,7 @@ describe('Test server plugins API validators', function () {
468 path: path + suffix, 463 path: path + suffix,
469 fields: { npmName: 'peertube-plugin-TOTO' }, 464 fields: { npmName: 'peertube-plugin-TOTO' },
470 token: server.accessToken, 465 token: server.accessToken,
471 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 466 expectedStatus: HttpStatusCode.BAD_REQUEST_400
472 }) 467 })
473 } 468 }
474 }) 469 })
@@ -488,7 +483,7 @@ describe('Test server plugins API validators', function () {
488 path: path + obj.suffix, 483 path: path + obj.suffix,
489 fields: { npmName: npmPlugin }, 484 fields: { npmName: npmPlugin },
490 token: server.accessToken, 485 token: server.accessToken,
491 statusCodeExpected: obj.status 486 expectedStatus: obj.status
492 }) 487 })
493 } 488 }
494 }) 489 })
diff --git a/server/tests/api/check-params/redundancy.ts b/server/tests/api/check-params/redundancy.ts
index dac6938de..d9f905549 100644
--- a/server/tests/api/check-params/redundancy.ts
+++ b/server/tests/api/check-params/redundancy.ts
@@ -1,30 +1,25 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2 2
3import 'mocha' 3import 'mocha'
4import { VideoCreateResult } from '@shared/models'
5import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
6import { 4import {
7 checkBadCountPagination, 5 checkBadCountPagination,
8 checkBadSortPagination, 6 checkBadSortPagination,
9 checkBadStartPagination, 7 checkBadStartPagination,
10 cleanupTests, 8 cleanupTests,
11 createUser, 9 createMultipleServers,
12 doubleFollow, 10 doubleFollow,
13 flushAndRunMultipleServers,
14 getVideo,
15 makeDeleteRequest, 11 makeDeleteRequest,
16 makeGetRequest, 12 makeGetRequest,
17 makePostBodyRequest, 13 makePostBodyRequest,
18 makePutBodyRequest, 14 makePutBodyRequest,
19 ServerInfo, 15 PeerTubeServer,
20 setAccessTokensToServers, 16 setAccessTokensToServers,
21 uploadVideoAndGetId,
22 userLogin,
23 waitJobs 17 waitJobs
24} from '../../../../shared/extra-utils' 18} from '@shared/extra-utils'
19import { HttpStatusCode, VideoCreateResult } from '@shared/models'
25 20
26describe('Test server redundancy API validators', function () { 21describe('Test server redundancy API validators', function () {
27 let servers: ServerInfo[] 22 let servers: PeerTubeServer[]
28 let userAccessToken = null 23 let userAccessToken = null
29 let videoIdLocal: number 24 let videoIdLocal: number
30 let videoRemote: VideoCreateResult 25 let videoRemote: VideoCreateResult
@@ -34,7 +29,7 @@ describe('Test server redundancy API validators', function () {
34 before(async function () { 29 before(async function () {
35 this.timeout(80000) 30 this.timeout(80000)
36 31
37 servers = await flushAndRunMultipleServers(2) 32 servers = await createMultipleServers(2)
38 33
39 await setAccessTokensToServers(servers) 34 await setAccessTokensToServers(servers)
40 await doubleFollow(servers[0], servers[1]) 35 await doubleFollow(servers[0], servers[1])
@@ -44,17 +39,16 @@ describe('Test server redundancy API validators', function () {
44 password: 'password' 39 password: 'password'
45 } 40 }
46 41
47 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: user.username, password: user.password }) 42 await servers[0].users.create({ username: user.username, password: user.password })
48 userAccessToken = await userLogin(servers[0], user) 43 userAccessToken = await servers[0].login.getAccessToken(user)
49 44
50 videoIdLocal = (await uploadVideoAndGetId({ server: servers[0], videoName: 'video' })).id 45 videoIdLocal = (await servers[0].videos.quickUpload({ name: 'video' })).id
51 46
52 const remoteUUID = (await uploadVideoAndGetId({ server: servers[1], videoName: 'video' })).uuid 47 const remoteUUID = (await servers[1].videos.quickUpload({ name: 'video' })).uuid
53 48
54 await waitJobs(servers) 49 await waitJobs(servers)
55 50
56 const resVideo = await getVideo(servers[0].url, remoteUUID) 51 videoRemote = await servers[0].videos.get({ id: remoteUUID })
57 videoRemote = resVideo.body
58 }) 52 })
59 53
60 describe('When listing redundancies', function () { 54 describe('When listing redundancies', function () {
@@ -69,11 +63,11 @@ describe('Test server redundancy API validators', function () {
69 }) 63 })
70 64
71 it('Should fail with an invalid token', async function () { 65 it('Should fail with an invalid token', async function () {
72 await makeGetRequest({ url, path, token: 'fake_token', statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 }) 66 await makeGetRequest({ url, path, token: 'fake_token', expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
73 }) 67 })
74 68
75 it('Should fail if the user is not an administrator', async function () { 69 it('Should fail if the user is not an administrator', async function () {
76 await makeGetRequest({ url, path, token: userAccessToken, statusCodeExpected: HttpStatusCode.FORBIDDEN_403 }) 70 await makeGetRequest({ url, path, token: userAccessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
77 }) 71 })
78 72
79 it('Should fail with a bad start pagination', async function () { 73 it('Should fail with a bad start pagination', async function () {
@@ -97,7 +91,7 @@ describe('Test server redundancy API validators', function () {
97 }) 91 })
98 92
99 it('Should succeed with the correct params', async function () { 93 it('Should succeed with the correct params', async function () {
100 await makeGetRequest({ url, path, token, query: { target: 'my-videos' }, statusCodeExpected: HttpStatusCode.OK_200 }) 94 await makeGetRequest({ url, path, token, query: { target: 'my-videos' }, expectedStatus: HttpStatusCode.OK_200 })
101 }) 95 })
102 }) 96 })
103 97
@@ -113,11 +107,11 @@ describe('Test server redundancy API validators', function () {
113 }) 107 })
114 108
115 it('Should fail with an invalid token', async function () { 109 it('Should fail with an invalid token', async function () {
116 await makePostBodyRequest({ url, path, token: 'fake_token', statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 }) 110 await makePostBodyRequest({ url, path, token: 'fake_token', expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
117 }) 111 })
118 112
119 it('Should fail if the user is not an administrator', async function () { 113 it('Should fail if the user is not an administrator', async function () {
120 await makePostBodyRequest({ url, path, token: userAccessToken, statusCodeExpected: HttpStatusCode.FORBIDDEN_403 }) 114 await makePostBodyRequest({ url, path, token: userAccessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
121 }) 115 })
122 116
123 it('Should fail without a video id', async function () { 117 it('Should fail without a video id', async function () {
@@ -129,7 +123,7 @@ describe('Test server redundancy API validators', function () {
129 }) 123 })
130 124
131 it('Should fail with a not found video id', async function () { 125 it('Should fail with a not found video id', async function () {
132 await makePostBodyRequest({ url, path, token, fields: { videoId: 6565 }, statusCodeExpected: HttpStatusCode.NOT_FOUND_404 }) 126 await makePostBodyRequest({ url, path, token, fields: { videoId: 6565 }, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
133 }) 127 })
134 128
135 it('Should fail with a local a video id', async function () { 129 it('Should fail with a local a video id', async function () {
@@ -142,7 +136,7 @@ describe('Test server redundancy API validators', function () {
142 path, 136 path,
143 token, 137 token,
144 fields: { videoId: videoRemote.shortUUID }, 138 fields: { videoId: videoRemote.shortUUID },
145 statusCodeExpected: HttpStatusCode.NO_CONTENT_204 139 expectedStatus: HttpStatusCode.NO_CONTENT_204
146 }) 140 })
147 }) 141 })
148 142
@@ -156,7 +150,7 @@ describe('Test server redundancy API validators', function () {
156 path, 150 path,
157 token, 151 token,
158 fields: { videoId: videoRemote.uuid }, 152 fields: { videoId: videoRemote.uuid },
159 statusCodeExpected: HttpStatusCode.CONFLICT_409 153 expectedStatus: HttpStatusCode.CONFLICT_409
160 }) 154 })
161 }) 155 })
162 }) 156 })
@@ -173,11 +167,11 @@ describe('Test server redundancy API validators', function () {
173 }) 167 })
174 168
175 it('Should fail with an invalid token', async function () { 169 it('Should fail with an invalid token', async function () {
176 await makeDeleteRequest({ url, path: path + '1', token: 'fake_token', statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 }) 170 await makeDeleteRequest({ url, path: path + '1', token: 'fake_token', expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
177 }) 171 })
178 172
179 it('Should fail if the user is not an administrator', async function () { 173 it('Should fail if the user is not an administrator', async function () {
180 await makeDeleteRequest({ url, path: path + '1', token: userAccessToken, statusCodeExpected: HttpStatusCode.FORBIDDEN_403 }) 174 await makeDeleteRequest({ url, path: path + '1', token: userAccessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
181 }) 175 })
182 176
183 it('Should fail with an incorrect video id', async function () { 177 it('Should fail with an incorrect video id', async function () {
@@ -185,7 +179,7 @@ describe('Test server redundancy API validators', function () {
185 }) 179 })
186 180
187 it('Should fail with a not found video redundancy', async function () { 181 it('Should fail with a not found video redundancy', async function () {
188 await makeDeleteRequest({ url, path: path + '454545', token, statusCodeExpected: HttpStatusCode.NOT_FOUND_404 }) 182 await makeDeleteRequest({ url, path: path + '454545', token, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
189 }) 183 })
190 }) 184 })
191 185
@@ -198,7 +192,7 @@ describe('Test server redundancy API validators', function () {
198 path: path + '/localhost:' + servers[1].port, 192 path: path + '/localhost:' + servers[1].port,
199 fields: { redundancyAllowed: true }, 193 fields: { redundancyAllowed: true },
200 token: 'fake_token', 194 token: 'fake_token',
201 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 195 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
202 }) 196 })
203 }) 197 })
204 198
@@ -208,7 +202,7 @@ describe('Test server redundancy API validators', function () {
208 path: path + '/localhost:' + servers[1].port, 202 path: path + '/localhost:' + servers[1].port,
209 fields: { redundancyAllowed: true }, 203 fields: { redundancyAllowed: true },
210 token: userAccessToken, 204 token: userAccessToken,
211 statusCodeExpected: HttpStatusCode.FORBIDDEN_403 205 expectedStatus: HttpStatusCode.FORBIDDEN_403
212 }) 206 })
213 }) 207 })
214 208
@@ -218,7 +212,7 @@ describe('Test server redundancy API validators', function () {
218 path: path + '/example.com', 212 path: path + '/example.com',
219 fields: { redundancyAllowed: true }, 213 fields: { redundancyAllowed: true },
220 token: servers[0].accessToken, 214 token: servers[0].accessToken,
221 statusCodeExpected: HttpStatusCode.NOT_FOUND_404 215 expectedStatus: HttpStatusCode.NOT_FOUND_404
222 }) 216 })
223 }) 217 })
224 218
@@ -228,7 +222,7 @@ describe('Test server redundancy API validators', function () {
228 path: path + '/localhost:' + servers[1].port, 222 path: path + '/localhost:' + servers[1].port,
229 fields: { blabla: true }, 223 fields: { blabla: true },
230 token: servers[0].accessToken, 224 token: servers[0].accessToken,
231 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 225 expectedStatus: HttpStatusCode.BAD_REQUEST_400
232 }) 226 })
233 }) 227 })
234 228
@@ -238,7 +232,7 @@ describe('Test server redundancy API validators', function () {
238 path: path + '/localhost:' + servers[1].port, 232 path: path + '/localhost:' + servers[1].port,
239 fields: { redundancyAllowed: true }, 233 fields: { redundancyAllowed: true },
240 token: servers[0].accessToken, 234 token: servers[0].accessToken,
241 statusCodeExpected: HttpStatusCode.NO_CONTENT_204 235 expectedStatus: HttpStatusCode.NO_CONTENT_204
242 }) 236 })
243 }) 237 })
244 }) 238 })
diff --git a/server/tests/api/check-params/search.ts b/server/tests/api/check-params/search.ts
index 20ad46cff..a3da54e1f 100644
--- a/server/tests/api/check-params/search.ts
+++ b/server/tests/api/check-params/search.ts
@@ -2,41 +2,39 @@
2 2
3import 'mocha' 3import 'mocha'
4import { 4import {
5 checkBadCountPagination,
6 checkBadSortPagination,
7 checkBadStartPagination,
5 cleanupTests, 8 cleanupTests,
6 flushAndRunServer, 9 createSingleServer,
7 immutableAssign,
8 makeGetRequest, 10 makeGetRequest,
9 ServerInfo, 11 PeerTubeServer,
10 updateCustomSubConfig,
11 setAccessTokensToServers 12 setAccessTokensToServers
12} from '../../../../shared/extra-utils' 13} from '@shared/extra-utils'
13import { 14import { HttpStatusCode } from '@shared/models'
14 checkBadCountPagination, 15
15 checkBadSortPagination, 16function updateSearchIndex (server: PeerTubeServer, enabled: boolean, disableLocalSearch = false) {
16 checkBadStartPagination 17 return server.config.updateCustomSubConfig({
17} from '../../../../shared/extra-utils/requests/check-api-params' 18 newConfig: {
18import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes' 19 search: {
19 20 searchIndex: {
20function updateSearchIndex (server: ServerInfo, enabled: boolean, disableLocalSearch = false) { 21 enabled,
21 return updateCustomSubConfig(server.url, server.accessToken, { 22 disableLocalSearch
22 search: { 23 }
23 searchIndex: {
24 enabled,
25 disableLocalSearch
26 } 24 }
27 } 25 }
28 }) 26 })
29} 27}
30 28
31describe('Test videos API validator', function () { 29describe('Test videos API validator', function () {
32 let server: ServerInfo 30 let server: PeerTubeServer
33 31
34 // --------------------------------------------------------------- 32 // ---------------------------------------------------------------
35 33
36 before(async function () { 34 before(async function () {
37 this.timeout(30000) 35 this.timeout(30000)
38 36
39 server = await flushAndRunServer(1) 37 server = await createSingleServer(1)
40 await setAccessTokensToServers([ server ]) 38 await setAccessTokensToServers([ server ])
41 }) 39 })
42 40
@@ -60,83 +58,83 @@ describe('Test videos API validator', function () {
60 }) 58 })
61 59
62 it('Should success with the correct parameters', async function () { 60 it('Should success with the correct parameters', async function () {
63 await makeGetRequest({ url: server.url, path, query, statusCodeExpected: HttpStatusCode.OK_200 }) 61 await makeGetRequest({ url: server.url, path, query, expectedStatus: HttpStatusCode.OK_200 })
64 }) 62 })
65 63
66 it('Should fail with an invalid category', async function () { 64 it('Should fail with an invalid category', async function () {
67 const customQuery1 = immutableAssign(query, { categoryOneOf: [ 'aa', 'b' ] }) 65 const customQuery1 = { ...query, categoryOneOf: [ 'aa', 'b' ] }
68 await makeGetRequest({ url: server.url, path, query: customQuery1, statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 }) 66 await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
69 67
70 const customQuery2 = immutableAssign(query, { categoryOneOf: 'a' }) 68 const customQuery2 = { ...query, categoryOneOf: 'a' }
71 await makeGetRequest({ url: server.url, path, query: customQuery2, statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 }) 69 await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
72 }) 70 })
73 71
74 it('Should succeed with a valid category', async function () { 72 it('Should succeed with a valid category', async function () {
75 const customQuery1 = immutableAssign(query, { categoryOneOf: [ 1, 7 ] }) 73 const customQuery1 = { ...query, categoryOneOf: [ 1, 7 ] }
76 await makeGetRequest({ url: server.url, path, query: customQuery1, statusCodeExpected: HttpStatusCode.OK_200 }) 74 await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.OK_200 })
77 75
78 const customQuery2 = immutableAssign(query, { categoryOneOf: 1 }) 76 const customQuery2 = { ...query, categoryOneOf: 1 }
79 await makeGetRequest({ url: server.url, path, query: customQuery2, statusCodeExpected: HttpStatusCode.OK_200 }) 77 await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.OK_200 })
80 }) 78 })
81 79
82 it('Should fail with an invalid licence', async function () { 80 it('Should fail with an invalid licence', async function () {
83 const customQuery1 = immutableAssign(query, { licenceOneOf: [ 'aa', 'b' ] }) 81 const customQuery1 = { ...query, licenceOneOf: [ 'aa', 'b' ] }
84 await makeGetRequest({ url: server.url, path, query: customQuery1, statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 }) 82 await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
85 83
86 const customQuery2 = immutableAssign(query, { licenceOneOf: 'a' }) 84 const customQuery2 = { ...query, licenceOneOf: 'a' }
87 await makeGetRequest({ url: server.url, path, query: customQuery2, statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 }) 85 await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
88 }) 86 })
89 87
90 it('Should succeed with a valid licence', async function () { 88 it('Should succeed with a valid licence', async function () {
91 const customQuery1 = immutableAssign(query, { licenceOneOf: [ 1, 2 ] }) 89 const customQuery1 = { ...query, licenceOneOf: [ 1, 2 ] }
92 await makeGetRequest({ url: server.url, path, query: customQuery1, statusCodeExpected: HttpStatusCode.OK_200 }) 90 await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.OK_200 })
93 91
94 const customQuery2 = immutableAssign(query, { licenceOneOf: 1 }) 92 const customQuery2 = { ...query, licenceOneOf: 1 }
95 await makeGetRequest({ url: server.url, path, query: customQuery2, statusCodeExpected: HttpStatusCode.OK_200 }) 93 await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.OK_200 })
96 }) 94 })
97 95
98 it('Should succeed with a valid language', async function () { 96 it('Should succeed with a valid language', async function () {
99 const customQuery1 = immutableAssign(query, { languageOneOf: [ 'fr', 'en' ] }) 97 const customQuery1 = { ...query, languageOneOf: [ 'fr', 'en' ] }
100 await makeGetRequest({ url: server.url, path, query: customQuery1, statusCodeExpected: HttpStatusCode.OK_200 }) 98 await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.OK_200 })
101 99
102 const customQuery2 = immutableAssign(query, { languageOneOf: 'fr' }) 100 const customQuery2 = { ...query, languageOneOf: 'fr' }
103 await makeGetRequest({ url: server.url, path, query: customQuery2, statusCodeExpected: HttpStatusCode.OK_200 }) 101 await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.OK_200 })
104 }) 102 })
105 103
106 it('Should succeed with valid tags', async function () { 104 it('Should succeed with valid tags', async function () {
107 const customQuery1 = immutableAssign(query, { tagsOneOf: [ 'tag1', 'tag2' ] }) 105 const customQuery1 = { ...query, tagsOneOf: [ 'tag1', 'tag2' ] }
108 await makeGetRequest({ url: server.url, path, query: customQuery1, statusCodeExpected: HttpStatusCode.OK_200 }) 106 await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.OK_200 })
109 107
110 const customQuery2 = immutableAssign(query, { tagsOneOf: 'tag1' }) 108 const customQuery2 = { ...query, tagsOneOf: 'tag1' }
111 await makeGetRequest({ url: server.url, path, query: customQuery2, statusCodeExpected: HttpStatusCode.OK_200 }) 109 await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.OK_200 })
112 110
113 const customQuery3 = immutableAssign(query, { tagsAllOf: [ 'tag1', 'tag2' ] }) 111 const customQuery3 = { ...query, tagsAllOf: [ 'tag1', 'tag2' ] }
114 await makeGetRequest({ url: server.url, path, query: customQuery3, statusCodeExpected: HttpStatusCode.OK_200 }) 112 await makeGetRequest({ url: server.url, path, query: customQuery3, expectedStatus: HttpStatusCode.OK_200 })
115 113
116 const customQuery4 = immutableAssign(query, { tagsAllOf: 'tag1' }) 114 const customQuery4 = { ...query, tagsAllOf: 'tag1' }
117 await makeGetRequest({ url: server.url, path, query: customQuery4, statusCodeExpected: HttpStatusCode.OK_200 }) 115 await makeGetRequest({ url: server.url, path, query: customQuery4, expectedStatus: HttpStatusCode.OK_200 })
118 }) 116 })
119 117
120 it('Should fail with invalid durations', async function () { 118 it('Should fail with invalid durations', async function () {
121 const customQuery1 = immutableAssign(query, { durationMin: 'hello' }) 119 const customQuery1 = { ...query, durationMin: 'hello' }
122 await makeGetRequest({ url: server.url, path, query: customQuery1, statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 }) 120 await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
123 121
124 const customQuery2 = immutableAssign(query, { durationMax: 'hello' }) 122 const customQuery2 = { ...query, durationMax: 'hello' }
125 await makeGetRequest({ url: server.url, path, query: customQuery2, statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 }) 123 await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
126 }) 124 })
127 125
128 it('Should fail with invalid dates', async function () { 126 it('Should fail with invalid dates', async function () {
129 const customQuery1 = immutableAssign(query, { startDate: 'hello' }) 127 const customQuery1 = { ...query, startDate: 'hello' }
130 await makeGetRequest({ url: server.url, path, query: customQuery1, statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 }) 128 await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
131 129
132 const customQuery2 = immutableAssign(query, { endDate: 'hello' }) 130 const customQuery2 = { ...query, endDate: 'hello' }
133 await makeGetRequest({ url: server.url, path, query: customQuery2, statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 }) 131 await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
134 132
135 const customQuery3 = immutableAssign(query, { originallyPublishedStartDate: 'hello' }) 133 const customQuery3 = { ...query, originallyPublishedStartDate: 'hello' }
136 await makeGetRequest({ url: server.url, path, query: customQuery3, statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 }) 134 await makeGetRequest({ url: server.url, path, query: customQuery3, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
137 135
138 const customQuery4 = immutableAssign(query, { originallyPublishedEndDate: 'hello' }) 136 const customQuery4 = { ...query, originallyPublishedEndDate: 'hello' }
139 await makeGetRequest({ url: server.url, path, query: customQuery4, statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 }) 137 await makeGetRequest({ url: server.url, path, query: customQuery4, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
140 }) 138 })
141 }) 139 })
142 140
@@ -160,7 +158,7 @@ describe('Test videos API validator', function () {
160 }) 158 })
161 159
162 it('Should success with the correct parameters', async function () { 160 it('Should success with the correct parameters', async function () {
163 await makeGetRequest({ url: server.url, path, query, statusCodeExpected: HttpStatusCode.OK_200 }) 161 await makeGetRequest({ url: server.url, path, query, expectedStatus: HttpStatusCode.OK_200 })
164 }) 162 })
165 }) 163 })
166 164
@@ -184,7 +182,7 @@ describe('Test videos API validator', function () {
184 }) 182 })
185 183
186 it('Should success with the correct parameters', async function () { 184 it('Should success with the correct parameters', async function () {
187 await makeGetRequest({ url: server.url, path, query, statusCodeExpected: HttpStatusCode.OK_200 }) 185 await makeGetRequest({ url: server.url, path, query, expectedStatus: HttpStatusCode.OK_200 })
188 }) 186 })
189 }) 187 })
190 188
@@ -202,42 +200,42 @@ describe('Test videos API validator', function () {
202 200
203 for (const path of paths) { 201 for (const path of paths) {
204 { 202 {
205 const customQuery = immutableAssign(query, { searchTarget: 'hello' }) 203 const customQuery = { ...query, searchTarget: 'hello' }
206 await makeGetRequest({ url: server.url, path, query: customQuery, statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 }) 204 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
207 } 205 }
208 206
209 { 207 {
210 const customQuery = immutableAssign(query, { searchTarget: undefined }) 208 const customQuery = { ...query, searchTarget: undefined }
211 await makeGetRequest({ url: server.url, path, query: customQuery, statusCodeExpected: HttpStatusCode.OK_200 }) 209 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.OK_200 })
212 } 210 }
213 211
214 { 212 {
215 const customQuery = immutableAssign(query, { searchTarget: 'local' }) 213 const customQuery = { ...query, searchTarget: 'local' }
216 await makeGetRequest({ url: server.url, path, query: customQuery, statusCodeExpected: HttpStatusCode.OK_200 }) 214 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.OK_200 })
217 } 215 }
218 216
219 { 217 {
220 const customQuery = immutableAssign(query, { searchTarget: 'search-index' }) 218 const customQuery = { ...query, searchTarget: 'search-index' }
221 await makeGetRequest({ url: server.url, path, query: customQuery, statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 }) 219 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
222 } 220 }
223 221
224 await updateSearchIndex(server, true, true) 222 await updateSearchIndex(server, true, true)
225 223
226 { 224 {
227 const customQuery = immutableAssign(query, { searchTarget: 'local' }) 225 const customQuery = { ...query, searchTarget: 'local' }
228 await makeGetRequest({ url: server.url, path, query: customQuery, statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 }) 226 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
229 } 227 }
230 228
231 { 229 {
232 const customQuery = immutableAssign(query, { searchTarget: 'search-index' }) 230 const customQuery = { ...query, searchTarget: 'search-index' }
233 await makeGetRequest({ url: server.url, path, query: customQuery, statusCodeExpected: HttpStatusCode.OK_200 }) 231 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.OK_200 })
234 } 232 }
235 233
236 await updateSearchIndex(server, true, false) 234 await updateSearchIndex(server, true, false)
237 235
238 { 236 {
239 const customQuery = immutableAssign(query, { searchTarget: 'local' }) 237 const customQuery = { ...query, searchTarget: 'local' }
240 await makeGetRequest({ url: server.url, path, query: customQuery, statusCodeExpected: HttpStatusCode.OK_200 }) 238 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.OK_200 })
241 } 239 }
242 240
243 await updateSearchIndex(server, false, false) 241 await updateSearchIndex(server, false, false)
diff --git a/server/tests/api/check-params/services.ts b/server/tests/api/check-params/services.ts
index 514e3da70..4c4a5cade 100644
--- a/server/tests/api/check-params/services.ts
+++ b/server/tests/api/check-params/services.ts
@@ -1,22 +1,18 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2 2
3import 'mocha' 3import 'mocha'
4
5import { 4import {
6 cleanupTests, 5 cleanupTests,
7 flushAndRunServer, 6 createSingleServer,
8 makeGetRequest, 7 makeGetRequest,
9 ServerInfo, 8 PeerTubeServer,
10 setAccessTokensToServers, 9 setAccessTokensToServers,
11 uploadVideo,
12 createVideoPlaylist,
13 setDefaultVideoChannel 10 setDefaultVideoChannel
14} from '../../../../shared/extra-utils' 11} from '@shared/extra-utils'
15import { VideoPlaylistPrivacy } from '@shared/models' 12import { HttpStatusCode, VideoPlaylistPrivacy } from '@shared/models'
16import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
17 13
18describe('Test services API validators', function () { 14describe('Test services API validators', function () {
19 let server: ServerInfo 15 let server: PeerTubeServer
20 let playlistUUID: string 16 let playlistUUID: string
21 17
22 // --------------------------------------------------------------- 18 // ---------------------------------------------------------------
@@ -24,27 +20,22 @@ describe('Test services API validators', function () {
24 before(async function () { 20 before(async function () {
25 this.timeout(60000) 21 this.timeout(60000)
26 22
27 server = await flushAndRunServer(1) 23 server = await createSingleServer(1)
28 await setAccessTokensToServers([ server ]) 24 await setAccessTokensToServers([ server ])
29 await setDefaultVideoChannel([ server ]) 25 await setDefaultVideoChannel([ server ])
30 26
31 { 27 server.store.video = await server.videos.upload({ attributes: { name: 'my super name' } })
32 const res = await uploadVideo(server.url, server.accessToken, { name: 'my super name' })
33 server.video = res.body.video
34 }
35 28
36 { 29 {
37 const res = await createVideoPlaylist({ 30 const created = await server.playlists.create({
38 url: server.url, 31 attributes: {
39 token: server.accessToken,
40 playlistAttrs: {
41 displayName: 'super playlist', 32 displayName: 'super playlist',
42 privacy: VideoPlaylistPrivacy.PUBLIC, 33 privacy: VideoPlaylistPrivacy.PUBLIC,
43 videoChannelId: server.videoChannel.id 34 videoChannelId: server.store.channel.id
44 } 35 }
45 }) 36 })
46 37
47 playlistUUID = res.body.videoPlaylist.uuid 38 playlistUUID = created.uuid
48 } 39 }
49 }) 40 })
50 41
@@ -56,7 +47,7 @@ describe('Test services API validators', function () {
56 }) 47 })
57 48
58 it('Should fail with an invalid host', async function () { 49 it('Should fail with an invalid host', async function () {
59 const embedUrl = 'http://hello.com/videos/watch/' + server.video.uuid 50 const embedUrl = 'http://hello.com/videos/watch/' + server.store.video.uuid
60 await checkParamEmbed(server, embedUrl) 51 await checkParamEmbed(server, embedUrl)
61 }) 52 })
62 53
@@ -71,37 +62,37 @@ describe('Test services API validators', function () {
71 }) 62 })
72 63
73 it('Should fail with an invalid path', async function () { 64 it('Should fail with an invalid path', async function () {
74 const embedUrl = `http://localhost:${server.port}/videos/watchs/${server.video.uuid}` 65 const embedUrl = `http://localhost:${server.port}/videos/watchs/${server.store.video.uuid}`
75 66
76 await checkParamEmbed(server, embedUrl) 67 await checkParamEmbed(server, embedUrl)
77 }) 68 })
78 69
79 it('Should fail with an invalid max height', async function () { 70 it('Should fail with an invalid max height', async function () {
80 const embedUrl = `http://localhost:${server.port}/videos/watch/${server.video.uuid}` 71 const embedUrl = `http://localhost:${server.port}/videos/watch/${server.store.video.uuid}`
81 72
82 await checkParamEmbed(server, embedUrl, HttpStatusCode.BAD_REQUEST_400, { maxheight: 'hello' }) 73 await checkParamEmbed(server, embedUrl, HttpStatusCode.BAD_REQUEST_400, { maxheight: 'hello' })
83 }) 74 })
84 75
85 it('Should fail with an invalid max width', async function () { 76 it('Should fail with an invalid max width', async function () {
86 const embedUrl = `http://localhost:${server.port}/videos/watch/${server.video.uuid}` 77 const embedUrl = `http://localhost:${server.port}/videos/watch/${server.store.video.uuid}`
87 78
88 await checkParamEmbed(server, embedUrl, HttpStatusCode.BAD_REQUEST_400, { maxwidth: 'hello' }) 79 await checkParamEmbed(server, embedUrl, HttpStatusCode.BAD_REQUEST_400, { maxwidth: 'hello' })
89 }) 80 })
90 81
91 it('Should fail with an invalid format', async function () { 82 it('Should fail with an invalid format', async function () {
92 const embedUrl = `http://localhost:${server.port}/videos/watch/${server.video.uuid}` 83 const embedUrl = `http://localhost:${server.port}/videos/watch/${server.store.video.uuid}`
93 84
94 await checkParamEmbed(server, embedUrl, HttpStatusCode.BAD_REQUEST_400, { format: 'blabla' }) 85 await checkParamEmbed(server, embedUrl, HttpStatusCode.BAD_REQUEST_400, { format: 'blabla' })
95 }) 86 })
96 87
97 it('Should fail with a non supported format', async function () { 88 it('Should fail with a non supported format', async function () {
98 const embedUrl = `http://localhost:${server.port}/videos/watch/${server.video.uuid}` 89 const embedUrl = `http://localhost:${server.port}/videos/watch/${server.store.video.uuid}`
99 90
100 await checkParamEmbed(server, embedUrl, HttpStatusCode.NOT_IMPLEMENTED_501, { format: 'xml' }) 91 await checkParamEmbed(server, embedUrl, HttpStatusCode.NOT_IMPLEMENTED_501, { format: 'xml' })
101 }) 92 })
102 93
103 it('Should succeed with the correct params with a video', async function () { 94 it('Should succeed with the correct params with a video', async function () {
104 const embedUrl = `http://localhost:${server.port}/videos/watch/${server.video.uuid}` 95 const embedUrl = `http://localhost:${server.port}/videos/watch/${server.store.video.uuid}`
105 const query = { 96 const query = {
106 format: 'json', 97 format: 'json',
107 maxheight: 400, 98 maxheight: 400,
@@ -128,13 +119,13 @@ describe('Test services API validators', function () {
128 }) 119 })
129}) 120})
130 121
131function checkParamEmbed (server: ServerInfo, embedUrl: string, statusCodeExpected = HttpStatusCode.BAD_REQUEST_400, query = {}) { 122function checkParamEmbed (server: PeerTubeServer, embedUrl: string, expectedStatus = HttpStatusCode.BAD_REQUEST_400, query = {}) {
132 const path = '/services/oembed' 123 const path = '/services/oembed'
133 124
134 return makeGetRequest({ 125 return makeGetRequest({
135 url: server.url, 126 url: server.url,
136 path, 127 path,
137 query: Object.assign(query, { url: embedUrl }), 128 query: Object.assign(query, { url: embedUrl }),
138 statusCodeExpected 129 expectedStatus
139 }) 130 })
140} 131}
diff --git a/server/tests/api/check-params/upload-quota.ts b/server/tests/api/check-params/upload-quota.ts
index d0fbec415..322e93d0d 100644
--- a/server/tests/api/check-params/upload-quota.ts
+++ b/server/tests/api/check-params/upload-quota.ts
@@ -2,46 +2,39 @@
2 2
3import 'mocha' 3import 'mocha'
4import { expect } from 'chai' 4import { expect } from 'chai'
5import { HttpStatusCode, randomInt } from '@shared/core-utils' 5import { randomInt } from '@shared/core-utils'
6import { getGoodVideoUrl, getMagnetURI, getMyVideoImports, importVideo } from '@shared/extra-utils/videos/video-imports'
7import { MyUser, VideoImport, VideoImportState, VideoPrivacy } from '@shared/models'
8import { 6import {
9 cleanupTests, 7 cleanupTests,
10 flushAndRunServer, 8 createSingleServer,
11 getMyUserInformation, 9 FIXTURE_URLS,
12 immutableAssign, 10 PeerTubeServer,
13 registerUser,
14 ServerInfo,
15 setAccessTokensToServers, 11 setAccessTokensToServers,
16 setDefaultVideoChannel, 12 setDefaultVideoChannel,
17 updateUser, 13 VideosCommand,
18 uploadVideo,
19 userLogin,
20 waitJobs 14 waitJobs
21} from '../../../../shared/extra-utils' 15} from '@shared/extra-utils'
16import { HttpStatusCode, VideoImportState, VideoPrivacy } from '@shared/models'
22 17
23describe('Test upload quota', function () { 18describe('Test upload quota', function () {
24 let server: ServerInfo 19 let server: PeerTubeServer
25 let rootId: number 20 let rootId: number
21 let command: VideosCommand
26 22
27 // --------------------------------------------------------------- 23 // ---------------------------------------------------------------
28 24
29 before(async function () { 25 before(async function () {
30 this.timeout(30000) 26 this.timeout(30000)
31 27
32 server = await flushAndRunServer(1) 28 server = await createSingleServer(1)
33 await setAccessTokensToServers([ server ]) 29 await setAccessTokensToServers([ server ])
34 await setDefaultVideoChannel([ server ]) 30 await setDefaultVideoChannel([ server ])
35 31
36 const res = await getMyUserInformation(server.url, server.accessToken) 32 const user = await server.users.getMyInfo()
37 rootId = (res.body as MyUser).id 33 rootId = user.id
38 34
39 await updateUser({ 35 await server.users.update({ userId: rootId, videoQuota: 42 })
40 url: server.url, 36
41 userId: rootId, 37 command = server.videos
42 accessToken: server.accessToken,
43 videoQuota: 42
44 })
45 }) 38 })
46 39
47 describe('When having a video quota', function () { 40 describe('When having a video quota', function () {
@@ -50,49 +43,48 @@ describe('Test upload quota', function () {
50 this.timeout(30000) 43 this.timeout(30000)
51 44
52 const user = { username: 'registered' + randomInt(1, 1500), password: 'password' } 45 const user = { username: 'registered' + randomInt(1, 1500), password: 'password' }
53 await registerUser(server.url, user.username, user.password) 46 await server.users.register(user)
54 const userAccessToken = await userLogin(server, user) 47 const userToken = await server.login.getAccessToken(user)
55 48
56 const videoAttributes = { fixture: 'video_short2.webm' } 49 const attributes = { fixture: 'video_short2.webm' }
57 for (let i = 0; i < 5; i++) { 50 for (let i = 0; i < 5; i++) {
58 await uploadVideo(server.url, userAccessToken, videoAttributes) 51 await command.upload({ token: userToken, attributes })
59 } 52 }
60 53
61 await uploadVideo(server.url, userAccessToken, videoAttributes, HttpStatusCode.PAYLOAD_TOO_LARGE_413, 'legacy') 54 await command.upload({ token: userToken, attributes, expectedStatus: HttpStatusCode.PAYLOAD_TOO_LARGE_413, mode: 'legacy' })
62 }) 55 })
63 56
64 it('Should fail with a registered user having too many videos with resumable upload', async function () { 57 it('Should fail with a registered user having too many videos with resumable upload', async function () {
65 this.timeout(30000) 58 this.timeout(30000)
66 59
67 const user = { username: 'registered' + randomInt(1, 1500), password: 'password' } 60 const user = { username: 'registered' + randomInt(1, 1500), password: 'password' }
68 await registerUser(server.url, user.username, user.password) 61 await server.users.register(user)
69 const userAccessToken = await userLogin(server, user) 62 const userToken = await server.login.getAccessToken(user)
70 63
71 const videoAttributes = { fixture: 'video_short2.webm' } 64 const attributes = { fixture: 'video_short2.webm' }
72 for (let i = 0; i < 5; i++) { 65 for (let i = 0; i < 5; i++) {
73 await uploadVideo(server.url, userAccessToken, videoAttributes) 66 await command.upload({ token: userToken, attributes })
74 } 67 }
75 68
76 await uploadVideo(server.url, userAccessToken, videoAttributes, HttpStatusCode.PAYLOAD_TOO_LARGE_413, 'resumable') 69 await command.upload({ token: userToken, attributes, expectedStatus: HttpStatusCode.PAYLOAD_TOO_LARGE_413, mode: 'resumable' })
77 }) 70 })
78 71
79 it('Should fail to import with HTTP/Torrent/magnet', async function () { 72 it('Should fail to import with HTTP/Torrent/magnet', async function () {
80 this.timeout(120000) 73 this.timeout(120000)
81 74
82 const baseAttributes = { 75 const baseAttributes = {
83 channelId: server.videoChannel.id, 76 channelId: server.store.channel.id,
84 privacy: VideoPrivacy.PUBLIC 77 privacy: VideoPrivacy.PUBLIC
85 } 78 }
86 await importVideo(server.url, server.accessToken, immutableAssign(baseAttributes, { targetUrl: getGoodVideoUrl() })) 79 await server.imports.importVideo({ attributes: { ...baseAttributes, targetUrl: FIXTURE_URLS.goodVideo } })
87 await importVideo(server.url, server.accessToken, immutableAssign(baseAttributes, { magnetUri: getMagnetURI() })) 80 await server.imports.importVideo({ attributes: { ...baseAttributes, magnetUri: FIXTURE_URLS.magnet } })
88 await importVideo(server.url, server.accessToken, immutableAssign(baseAttributes, { torrentfile: 'video-720p.torrent' as any })) 81 await server.imports.importVideo({ attributes: { ...baseAttributes, torrentfile: 'video-720p.torrent' as any } })
89 82
90 await waitJobs([ server ]) 83 await waitJobs([ server ])
91 84
92 const res = await getMyVideoImports(server.url, server.accessToken) 85 const { total, data: videoImports } = await server.imports.getMyVideoImports()
86 expect(total).to.equal(3)
93 87
94 expect(res.body.total).to.equal(3)
95 const videoImports: VideoImport[] = res.body.data
96 expect(videoImports).to.have.lengthOf(3) 88 expect(videoImports).to.have.lengthOf(3)
97 89
98 for (const videoImport of videoImports) { 90 for (const videoImport of videoImports) {
@@ -106,43 +98,34 @@ describe('Test upload quota', function () {
106 describe('When having a daily video quota', function () { 98 describe('When having a daily video quota', function () {
107 99
108 it('Should fail with a user having too many videos daily', async function () { 100 it('Should fail with a user having too many videos daily', async function () {
109 await updateUser({ 101 await server.users.update({ userId: rootId, videoQuotaDaily: 42 })
110 url: server.url,
111 userId: rootId,
112 accessToken: server.accessToken,
113 videoQuotaDaily: 42
114 })
115 102
116 await uploadVideo(server.url, server.accessToken, {}, HttpStatusCode.PAYLOAD_TOO_LARGE_413, 'legacy') 103 await command.upload({ expectedStatus: HttpStatusCode.PAYLOAD_TOO_LARGE_413, mode: 'legacy' })
117 await uploadVideo(server.url, server.accessToken, {}, HttpStatusCode.PAYLOAD_TOO_LARGE_413, 'resumable') 104 await command.upload({ expectedStatus: HttpStatusCode.PAYLOAD_TOO_LARGE_413, mode: 'resumable' })
118 }) 105 })
119 }) 106 })
120 107
121 describe('When having an absolute and daily video quota', function () { 108 describe('When having an absolute and daily video quota', function () {
122 it('Should fail if exceeding total quota', async function () { 109 it('Should fail if exceeding total quota', async function () {
123 await updateUser({ 110 await server.users.update({
124 url: server.url,
125 userId: rootId, 111 userId: rootId,
126 accessToken: server.accessToken,
127 videoQuota: 42, 112 videoQuota: 42,
128 videoQuotaDaily: 1024 * 1024 * 1024 113 videoQuotaDaily: 1024 * 1024 * 1024
129 }) 114 })
130 115
131 await uploadVideo(server.url, server.accessToken, {}, HttpStatusCode.PAYLOAD_TOO_LARGE_413, 'legacy') 116 await command.upload({ expectedStatus: HttpStatusCode.PAYLOAD_TOO_LARGE_413, mode: 'legacy' })
132 await uploadVideo(server.url, server.accessToken, {}, HttpStatusCode.PAYLOAD_TOO_LARGE_413, 'resumable') 117 await command.upload({ expectedStatus: HttpStatusCode.PAYLOAD_TOO_LARGE_413, mode: 'resumable' })
133 }) 118 })
134 119
135 it('Should fail if exceeding daily quota', async function () { 120 it('Should fail if exceeding daily quota', async function () {
136 await updateUser({ 121 await server.users.update({
137 url: server.url,
138 userId: rootId, 122 userId: rootId,
139 accessToken: server.accessToken,
140 videoQuota: 1024 * 1024 * 1024, 123 videoQuota: 1024 * 1024 * 1024,
141 videoQuotaDaily: 42 124 videoQuotaDaily: 42
142 }) 125 })
143 126
144 await uploadVideo(server.url, server.accessToken, {}, HttpStatusCode.PAYLOAD_TOO_LARGE_413, 'legacy') 127 await command.upload({ expectedStatus: HttpStatusCode.PAYLOAD_TOO_LARGE_413, mode: 'legacy' })
145 await uploadVideo(server.url, server.accessToken, {}, HttpStatusCode.PAYLOAD_TOO_LARGE_413, 'resumable') 128 await command.upload({ expectedStatus: HttpStatusCode.PAYLOAD_TOO_LARGE_413, mode: 'resumable' })
146 }) 129 })
147 }) 130 })
148 131
diff --git a/server/tests/api/check-params/user-notifications.ts b/server/tests/api/check-params/user-notifications.ts
index 26d4423f9..17edf5aa1 100644
--- a/server/tests/api/check-params/user-notifications.ts
+++ b/server/tests/api/check-params/user-notifications.ts
@@ -2,35 +2,30 @@
2 2
3import 'mocha' 3import 'mocha'
4import { io } from 'socket.io-client' 4import { io } from 'socket.io-client'
5
6import { 5import {
6 checkBadCountPagination,
7 checkBadSortPagination,
8 checkBadStartPagination,
7 cleanupTests, 9 cleanupTests,
8 flushAndRunServer, 10 createSingleServer,
9 immutableAssign,
10 makeGetRequest, 11 makeGetRequest,
11 makePostBodyRequest, 12 makePostBodyRequest,
12 makePutBodyRequest, 13 makePutBodyRequest,
13 ServerInfo, 14 PeerTubeServer,
14 setAccessTokensToServers, 15 setAccessTokensToServers,
15 wait 16 wait
16} from '../../../../shared/extra-utils' 17} from '@shared/extra-utils'
17import { 18import { HttpStatusCode, UserNotificationSetting, UserNotificationSettingValue } from '@shared/models'
18 checkBadCountPagination,
19 checkBadSortPagination,
20 checkBadStartPagination
21} from '../../../../shared/extra-utils/requests/check-api-params'
22import { UserNotificationSetting, UserNotificationSettingValue } from '../../../../shared/models/users'
23import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
24 19
25describe('Test user notifications API validators', function () { 20describe('Test user notifications API validators', function () {
26 let server: ServerInfo 21 let server: PeerTubeServer
27 22
28 // --------------------------------------------------------------- 23 // ---------------------------------------------------------------
29 24
30 before(async function () { 25 before(async function () {
31 this.timeout(30000) 26 this.timeout(30000)
32 27
33 server = await flushAndRunServer(1) 28 server = await createSingleServer(1)
34 29
35 await setAccessTokensToServers([ server ]) 30 await setAccessTokensToServers([ server ])
36 }) 31 })
@@ -58,7 +53,7 @@ describe('Test user notifications API validators', function () {
58 unread: 'toto' 53 unread: 'toto'
59 }, 54 },
60 token: server.accessToken, 55 token: server.accessToken,
61 statusCodeExpected: HttpStatusCode.OK_200 56 expectedStatus: HttpStatusCode.OK_200
62 }) 57 })
63 }) 58 })
64 59
@@ -66,7 +61,7 @@ describe('Test user notifications API validators', function () {
66 await makeGetRequest({ 61 await makeGetRequest({
67 url: server.url, 62 url: server.url,
68 path, 63 path,
69 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 64 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
70 }) 65 })
71 }) 66 })
72 67
@@ -75,7 +70,7 @@ describe('Test user notifications API validators', function () {
75 url: server.url, 70 url: server.url,
76 path, 71 path,
77 token: server.accessToken, 72 token: server.accessToken,
78 statusCodeExpected: HttpStatusCode.OK_200 73 expectedStatus: HttpStatusCode.OK_200
79 }) 74 })
80 }) 75 })
81 }) 76 })
@@ -91,7 +86,7 @@ describe('Test user notifications API validators', function () {
91 ids: [ 'hello' ] 86 ids: [ 'hello' ]
92 }, 87 },
93 token: server.accessToken, 88 token: server.accessToken,
94 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 89 expectedStatus: HttpStatusCode.BAD_REQUEST_400
95 }) 90 })
96 91
97 await makePostBodyRequest({ 92 await makePostBodyRequest({
@@ -101,7 +96,7 @@ describe('Test user notifications API validators', function () {
101 ids: [ ] 96 ids: [ ]
102 }, 97 },
103 token: server.accessToken, 98 token: server.accessToken,
104 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 99 expectedStatus: HttpStatusCode.BAD_REQUEST_400
105 }) 100 })
106 101
107 await makePostBodyRequest({ 102 await makePostBodyRequest({
@@ -111,7 +106,7 @@ describe('Test user notifications API validators', function () {
111 ids: 5 106 ids: 5
112 }, 107 },
113 token: server.accessToken, 108 token: server.accessToken,
114 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 109 expectedStatus: HttpStatusCode.BAD_REQUEST_400
115 }) 110 })
116 }) 111 })
117 112
@@ -122,7 +117,7 @@ describe('Test user notifications API validators', function () {
122 fields: { 117 fields: {
123 ids: [ 5 ] 118 ids: [ 5 ]
124 }, 119 },
125 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 120 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
126 }) 121 })
127 }) 122 })
128 123
@@ -134,7 +129,7 @@ describe('Test user notifications API validators', function () {
134 ids: [ 5 ] 129 ids: [ 5 ]
135 }, 130 },
136 token: server.accessToken, 131 token: server.accessToken,
137 statusCodeExpected: HttpStatusCode.NO_CONTENT_204 132 expectedStatus: HttpStatusCode.NO_CONTENT_204
138 }) 133 })
139 }) 134 })
140 }) 135 })
@@ -146,7 +141,7 @@ describe('Test user notifications API validators', function () {
146 await makePostBodyRequest({ 141 await makePostBodyRequest({
147 url: server.url, 142 url: server.url,
148 path, 143 path,
149 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 144 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
150 }) 145 })
151 }) 146 })
152 147
@@ -155,7 +150,7 @@ describe('Test user notifications API validators', function () {
155 url: server.url, 150 url: server.url,
156 path, 151 path,
157 token: server.accessToken, 152 token: server.accessToken,
158 statusCodeExpected: HttpStatusCode.NO_CONTENT_204 153 expectedStatus: HttpStatusCode.NO_CONTENT_204
159 }) 154 })
160 }) 155 })
161 }) 156 })
@@ -187,32 +182,32 @@ describe('Test user notifications API validators', function () {
187 path, 182 path,
188 token: server.accessToken, 183 token: server.accessToken,
189 fields: { newVideoFromSubscription: UserNotificationSettingValue.WEB }, 184 fields: { newVideoFromSubscription: UserNotificationSettingValue.WEB },
190 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 185 expectedStatus: HttpStatusCode.BAD_REQUEST_400
191 }) 186 })
192 }) 187 })
193 188
194 it('Should fail with incorrect field values', async function () { 189 it('Should fail with incorrect field values', async function () {
195 { 190 {
196 const fields = immutableAssign(correctFields, { newCommentOnMyVideo: 15 }) 191 const fields = { ...correctFields, newCommentOnMyVideo: 15 }
197 192
198 await makePutBodyRequest({ 193 await makePutBodyRequest({
199 url: server.url, 194 url: server.url,
200 path, 195 path,
201 token: server.accessToken, 196 token: server.accessToken,
202 fields, 197 fields,
203 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 198 expectedStatus: HttpStatusCode.BAD_REQUEST_400
204 }) 199 })
205 } 200 }
206 201
207 { 202 {
208 const fields = immutableAssign(correctFields, { newCommentOnMyVideo: 'toto' }) 203 const fields = { ...correctFields, newCommentOnMyVideo: 'toto' }
209 204
210 await makePutBodyRequest({ 205 await makePutBodyRequest({
211 url: server.url, 206 url: server.url,
212 path, 207 path,
213 fields, 208 fields,
214 token: server.accessToken, 209 token: server.accessToken,
215 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 210 expectedStatus: HttpStatusCode.BAD_REQUEST_400
216 }) 211 })
217 } 212 }
218 }) 213 })
@@ -222,7 +217,7 @@ describe('Test user notifications API validators', function () {
222 url: server.url, 217 url: server.url,
223 path, 218 path,
224 fields: correctFields, 219 fields: correctFields,
225 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 220 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
226 }) 221 })
227 }) 222 })
228 223
@@ -232,7 +227,7 @@ describe('Test user notifications API validators', function () {
232 path, 227 path,
233 token: server.accessToken, 228 token: server.accessToken,
234 fields: correctFields, 229 fields: correctFields,
235 statusCodeExpected: HttpStatusCode.NO_CONTENT_204 230 expectedStatus: HttpStatusCode.NO_CONTENT_204
236 }) 231 })
237 }) 232 })
238 }) 233 })
diff --git a/server/tests/api/check-params/user-subscriptions.ts b/server/tests/api/check-params/user-subscriptions.ts
index 538201647..624069c80 100644
--- a/server/tests/api/check-params/user-subscriptions.ts
+++ b/server/tests/api/check-params/user-subscriptions.ts
@@ -1,30 +1,24 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2 2
3import 'mocha' 3import 'mocha'
4
5import { 4import {
5 checkBadCountPagination,
6 checkBadSortPagination,
7 checkBadStartPagination,
6 cleanupTests, 8 cleanupTests,
7 createUser, 9 createSingleServer,
8 flushAndRunServer,
9 makeDeleteRequest, 10 makeDeleteRequest,
10 makeGetRequest, 11 makeGetRequest,
11 makePostBodyRequest, 12 makePostBodyRequest,
12 ServerInfo, 13 PeerTubeServer,
13 setAccessTokensToServers, 14 setAccessTokensToServers,
14 userLogin 15 waitJobs
15} from '../../../../shared/extra-utils' 16} from '@shared/extra-utils'
16 17import { HttpStatusCode } from '@shared/models'
17import {
18 checkBadCountPagination,
19 checkBadSortPagination,
20 checkBadStartPagination
21} from '../../../../shared/extra-utils/requests/check-api-params'
22import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
23import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
24 18
25describe('Test user subscriptions API validators', function () { 19describe('Test user subscriptions API validators', function () {
26 const path = '/api/v1/users/me/subscriptions' 20 const path = '/api/v1/users/me/subscriptions'
27 let server: ServerInfo 21 let server: PeerTubeServer
28 let userAccessToken = '' 22 let userAccessToken = ''
29 23
30 // --------------------------------------------------------------- 24 // ---------------------------------------------------------------
@@ -32,7 +26,7 @@ describe('Test user subscriptions API validators', function () {
32 before(async function () { 26 before(async function () {
33 this.timeout(30000) 27 this.timeout(30000)
34 28
35 server = await flushAndRunServer(1) 29 server = await createSingleServer(1)
36 30
37 await setAccessTokensToServers([ server ]) 31 await setAccessTokensToServers([ server ])
38 32
@@ -40,8 +34,8 @@ describe('Test user subscriptions API validators', function () {
40 username: 'user1', 34 username: 'user1',
41 password: 'my super password' 35 password: 'my super password'
42 } 36 }
43 await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password }) 37 await server.users.create({ username: user.username, password: user.password })
44 userAccessToken = await userLogin(server, user) 38 userAccessToken = await server.login.getAccessToken(user)
45 }) 39 })
46 40
47 describe('When listing my subscriptions', function () { 41 describe('When listing my subscriptions', function () {
@@ -61,7 +55,7 @@ describe('Test user subscriptions API validators', function () {
61 await makeGetRequest({ 55 await makeGetRequest({
62 url: server.url, 56 url: server.url,
63 path, 57 path,
64 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 58 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
65 }) 59 })
66 }) 60 })
67 61
@@ -70,7 +64,7 @@ describe('Test user subscriptions API validators', function () {
70 url: server.url, 64 url: server.url,
71 path, 65 path,
72 token: userAccessToken, 66 token: userAccessToken,
73 statusCodeExpected: HttpStatusCode.OK_200 67 expectedStatus: HttpStatusCode.OK_200
74 }) 68 })
75 }) 69 })
76 }) 70 })
@@ -94,7 +88,7 @@ describe('Test user subscriptions API validators', function () {
94 await makeGetRequest({ 88 await makeGetRequest({
95 url: server.url, 89 url: server.url,
96 path, 90 path,
97 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 91 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
98 }) 92 })
99 }) 93 })
100 94
@@ -103,7 +97,7 @@ describe('Test user subscriptions API validators', function () {
103 url: server.url, 97 url: server.url,
104 path, 98 path,
105 token: userAccessToken, 99 token: userAccessToken,
106 statusCodeExpected: HttpStatusCode.OK_200 100 expectedStatus: HttpStatusCode.OK_200
107 }) 101 })
108 }) 102 })
109 }) 103 })
@@ -114,7 +108,7 @@ describe('Test user subscriptions API validators', function () {
114 url: server.url, 108 url: server.url,
115 path, 109 path,
116 fields: { uri: 'user1_channel@localhost:' + server.port }, 110 fields: { uri: 'user1_channel@localhost:' + server.port },
117 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 111 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
118 }) 112 })
119 }) 113 })
120 114
@@ -124,7 +118,7 @@ describe('Test user subscriptions API validators', function () {
124 path, 118 path,
125 token: server.accessToken, 119 token: server.accessToken,
126 fields: { uri: 'root' }, 120 fields: { uri: 'root' },
127 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 121 expectedStatus: HttpStatusCode.BAD_REQUEST_400
128 }) 122 })
129 123
130 await makePostBodyRequest({ 124 await makePostBodyRequest({
@@ -132,7 +126,7 @@ describe('Test user subscriptions API validators', function () {
132 path, 126 path,
133 token: server.accessToken, 127 token: server.accessToken,
134 fields: { uri: 'root@' }, 128 fields: { uri: 'root@' },
135 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 129 expectedStatus: HttpStatusCode.BAD_REQUEST_400
136 }) 130 })
137 131
138 await makePostBodyRequest({ 132 await makePostBodyRequest({
@@ -140,7 +134,7 @@ describe('Test user subscriptions API validators', function () {
140 path, 134 path,
141 token: server.accessToken, 135 token: server.accessToken,
142 fields: { uri: 'root@hello@' }, 136 fields: { uri: 'root@hello@' },
143 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 137 expectedStatus: HttpStatusCode.BAD_REQUEST_400
144 }) 138 })
145 }) 139 })
146 140
@@ -152,7 +146,7 @@ describe('Test user subscriptions API validators', function () {
152 path, 146 path,
153 token: server.accessToken, 147 token: server.accessToken,
154 fields: { uri: 'user1_channel@localhost:' + server.port }, 148 fields: { uri: 'user1_channel@localhost:' + server.port },
155 statusCodeExpected: HttpStatusCode.NO_CONTENT_204 149 expectedStatus: HttpStatusCode.NO_CONTENT_204
156 }) 150 })
157 151
158 await waitJobs([ server ]) 152 await waitJobs([ server ])
@@ -164,7 +158,7 @@ describe('Test user subscriptions API validators', function () {
164 await makeGetRequest({ 158 await makeGetRequest({
165 url: server.url, 159 url: server.url,
166 path: path + '/user1_channel@localhost:' + server.port, 160 path: path + '/user1_channel@localhost:' + server.port,
167 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 161 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
168 }) 162 })
169 }) 163 })
170 164
@@ -173,21 +167,21 @@ describe('Test user subscriptions API validators', function () {
173 url: server.url, 167 url: server.url,
174 path: path + '/root', 168 path: path + '/root',
175 token: server.accessToken, 169 token: server.accessToken,
176 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 170 expectedStatus: HttpStatusCode.BAD_REQUEST_400
177 }) 171 })
178 172
179 await makeGetRequest({ 173 await makeGetRequest({
180 url: server.url, 174 url: server.url,
181 path: path + '/root@', 175 path: path + '/root@',
182 token: server.accessToken, 176 token: server.accessToken,
183 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 177 expectedStatus: HttpStatusCode.BAD_REQUEST_400
184 }) 178 })
185 179
186 await makeGetRequest({ 180 await makeGetRequest({
187 url: server.url, 181 url: server.url,
188 path: path + '/root@hello@', 182 path: path + '/root@hello@',
189 token: server.accessToken, 183 token: server.accessToken,
190 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 184 expectedStatus: HttpStatusCode.BAD_REQUEST_400
191 }) 185 })
192 }) 186 })
193 187
@@ -196,7 +190,7 @@ describe('Test user subscriptions API validators', function () {
196 url: server.url, 190 url: server.url,
197 path: path + '/root1@localhost:' + server.port, 191 path: path + '/root1@localhost:' + server.port,
198 token: server.accessToken, 192 token: server.accessToken,
199 statusCodeExpected: HttpStatusCode.NOT_FOUND_404 193 expectedStatus: HttpStatusCode.NOT_FOUND_404
200 }) 194 })
201 }) 195 })
202 196
@@ -205,7 +199,7 @@ describe('Test user subscriptions API validators', function () {
205 url: server.url, 199 url: server.url,
206 path: path + '/user1_channel@localhost:' + server.port, 200 path: path + '/user1_channel@localhost:' + server.port,
207 token: server.accessToken, 201 token: server.accessToken,
208 statusCodeExpected: HttpStatusCode.OK_200 202 expectedStatus: HttpStatusCode.OK_200
209 }) 203 })
210 }) 204 })
211 }) 205 })
@@ -217,7 +211,7 @@ describe('Test user subscriptions API validators', function () {
217 await makeGetRequest({ 211 await makeGetRequest({
218 url: server.url, 212 url: server.url,
219 path: existPath, 213 path: existPath,
220 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 214 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
221 }) 215 })
222 }) 216 })
223 217
@@ -227,7 +221,7 @@ describe('Test user subscriptions API validators', function () {
227 path: existPath, 221 path: existPath,
228 query: { uris: 'toto' }, 222 query: { uris: 'toto' },
229 token: server.accessToken, 223 token: server.accessToken,
230 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 224 expectedStatus: HttpStatusCode.BAD_REQUEST_400
231 }) 225 })
232 226
233 await makeGetRequest({ 227 await makeGetRequest({
@@ -235,7 +229,7 @@ describe('Test user subscriptions API validators', function () {
235 path: existPath, 229 path: existPath,
236 query: { 'uris[]': 1 }, 230 query: { 'uris[]': 1 },
237 token: server.accessToken, 231 token: server.accessToken,
238 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 232 expectedStatus: HttpStatusCode.BAD_REQUEST_400
239 }) 233 })
240 }) 234 })
241 235
@@ -245,7 +239,7 @@ describe('Test user subscriptions API validators', function () {
245 path: existPath, 239 path: existPath,
246 query: { 'uris[]': 'coucou@localhost:' + server.port }, 240 query: { 'uris[]': 'coucou@localhost:' + server.port },
247 token: server.accessToken, 241 token: server.accessToken,
248 statusCodeExpected: HttpStatusCode.OK_200 242 expectedStatus: HttpStatusCode.OK_200
249 }) 243 })
250 }) 244 })
251 }) 245 })
@@ -255,7 +249,7 @@ describe('Test user subscriptions API validators', function () {
255 await makeDeleteRequest({ 249 await makeDeleteRequest({
256 url: server.url, 250 url: server.url,
257 path: path + '/user1_channel@localhost:' + server.port, 251 path: path + '/user1_channel@localhost:' + server.port,
258 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 252 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
259 }) 253 })
260 }) 254 })
261 255
@@ -264,21 +258,21 @@ describe('Test user subscriptions API validators', function () {
264 url: server.url, 258 url: server.url,
265 path: path + '/root', 259 path: path + '/root',
266 token: server.accessToken, 260 token: server.accessToken,
267 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 261 expectedStatus: HttpStatusCode.BAD_REQUEST_400
268 }) 262 })
269 263
270 await makeDeleteRequest({ 264 await makeDeleteRequest({
271 url: server.url, 265 url: server.url,
272 path: path + '/root@', 266 path: path + '/root@',
273 token: server.accessToken, 267 token: server.accessToken,
274 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 268 expectedStatus: HttpStatusCode.BAD_REQUEST_400
275 }) 269 })
276 270
277 await makeDeleteRequest({ 271 await makeDeleteRequest({
278 url: server.url, 272 url: server.url,
279 path: path + '/root@hello@', 273 path: path + '/root@hello@',
280 token: server.accessToken, 274 token: server.accessToken,
281 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 275 expectedStatus: HttpStatusCode.BAD_REQUEST_400
282 }) 276 })
283 }) 277 })
284 278
@@ -287,7 +281,7 @@ describe('Test user subscriptions API validators', function () {
287 url: server.url, 281 url: server.url,
288 path: path + '/root1@localhost:' + server.port, 282 path: path + '/root1@localhost:' + server.port,
289 token: server.accessToken, 283 token: server.accessToken,
290 statusCodeExpected: HttpStatusCode.NOT_FOUND_404 284 expectedStatus: HttpStatusCode.NOT_FOUND_404
291 }) 285 })
292 }) 286 })
293 287
@@ -296,7 +290,7 @@ describe('Test user subscriptions API validators', function () {
296 url: server.url, 290 url: server.url,
297 path: path + '/user1_channel@localhost:' + server.port, 291 path: path + '/user1_channel@localhost:' + server.port,
298 token: server.accessToken, 292 token: server.accessToken,
299 statusCodeExpected: HttpStatusCode.NO_CONTENT_204 293 expectedStatus: HttpStatusCode.NO_CONTENT_204
300 }) 294 })
301 }) 295 })
302 }) 296 })
diff --git a/server/tests/api/check-params/users.ts b/server/tests/api/check-params/users.ts
index 70a872ce5..9d8f933db 100644
--- a/server/tests/api/check-params/users.ts
+++ b/server/tests/api/check-params/users.ts
@@ -2,43 +2,24 @@
2 2
3import 'mocha' 3import 'mocha'
4import { omit } from 'lodash' 4import { omit } from 'lodash'
5import { User, UserRole, VideoCreateResult } from '../../../../shared'
6import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
7import { 5import {
8 addVideoChannel,
9 blockUser,
10 buildAbsoluteFixturePath, 6 buildAbsoluteFixturePath,
7 checkBadCountPagination,
8 checkBadSortPagination,
9 checkBadStartPagination,
11 cleanupTests, 10 cleanupTests,
12 createUser, 11 createSingleServer,
13 deleteMe,
14 flushAndRunServer,
15 getMyUserInformation,
16 getMyUserVideoRating,
17 getUserScopedTokens,
18 getUsersList,
19 immutableAssign,
20 killallServers, 12 killallServers,
21 makeGetRequest, 13 makeGetRequest,
22 makePostBodyRequest, 14 makePostBodyRequest,
23 makePutBodyRequest, 15 makePutBodyRequest,
24 makeUploadRequest, 16 makeUploadRequest,
25 registerUser, 17 MockSmtpServer,
26 removeUser, 18 PeerTubeServer,
27 renewUserScopedTokens,
28 reRunServer,
29 ServerInfo,
30 setAccessTokensToServers, 19 setAccessTokensToServers,
31 unblockUser, 20 UsersCommand
32 uploadVideo, 21} from '@shared/extra-utils'
33 userLogin 22import { HttpStatusCode, UserAdminFlag, UserRole, VideoCreateResult } from '@shared/models'
34} from '../../../../shared/extra-utils'
35import { MockSmtpServer } from '../../../../shared/extra-utils/miscs/email'
36import {
37 checkBadCountPagination,
38 checkBadSortPagination,
39 checkBadStartPagination
40} from '../../../../shared/extra-utils/requests/check-api-params'
41import { UserAdminFlag } from '../../../../shared/models/users/user-flag.model'
42 23
43describe('Test users API validators', function () { 24describe('Test users API validators', function () {
44 const path = '/api/v1/users/' 25 const path = '/api/v1/users/'
@@ -46,10 +27,10 @@ describe('Test users API validators', function () {
46 let rootId: number 27 let rootId: number
47 let moderatorId: number 28 let moderatorId: number
48 let video: VideoCreateResult 29 let video: VideoCreateResult
49 let server: ServerInfo 30 let server: PeerTubeServer
50 let serverWithRegistrationDisabled: ServerInfo 31 let serverWithRegistrationDisabled: PeerTubeServer
51 let userAccessToken = '' 32 let userToken = ''
52 let moderatorAccessToken = '' 33 let moderatorToken = ''
53 let emailPort: number 34 let emailPort: number
54 let overrideConfig: Object 35 let overrideConfig: Object
55 36
@@ -65,8 +46,8 @@ describe('Test users API validators', function () {
65 46
66 { 47 {
67 const res = await Promise.all([ 48 const res = await Promise.all([
68 flushAndRunServer(1, overrideConfig), 49 createSingleServer(1, overrideConfig),
69 flushAndRunServer(2) 50 createSingleServer(2)
70 ]) 51 ])
71 52
72 server = res[0] 53 server = res[0]
@@ -76,66 +57,31 @@ describe('Test users API validators', function () {
76 } 57 }
77 58
78 { 59 {
79 const user = { 60 const user = { username: 'user1' }
80 username: 'user1', 61 await server.users.create({ ...user })
81 password: 'my super password' 62 userToken = await server.login.getAccessToken(user)
82 }
83
84 const videoQuota = 42000000
85 await createUser({
86 url: server.url,
87 accessToken: server.accessToken,
88 username: user.username,
89 password: user.password,
90 videoQuota: videoQuota
91 })
92 userAccessToken = await userLogin(server, user)
93 } 63 }
94 64
95 { 65 {
96 const moderator = { 66 const moderator = { username: 'moderator1' }
97 username: 'moderator1', 67 await server.users.create({ ...moderator, role: UserRole.MODERATOR })
98 password: 'super password' 68 moderatorToken = await server.login.getAccessToken(moderator)
99 }
100
101 await createUser({
102 url: server.url,
103 accessToken: server.accessToken,
104 username: moderator.username,
105 password: moderator.password,
106 role: UserRole.MODERATOR
107 })
108
109 moderatorAccessToken = await userLogin(server, moderator)
110 } 69 }
111 70
112 { 71 {
113 const moderator = { 72 const moderator = { username: 'moderator2' }
114 username: 'moderator2', 73 await server.users.create({ ...moderator, role: UserRole.MODERATOR })
115 password: 'super password'
116 }
117
118 await createUser({
119 url: server.url,
120 accessToken: server.accessToken,
121 username: moderator.username,
122 password: moderator.password,
123 role: UserRole.MODERATOR
124 })
125 } 74 }
126 75
127 { 76 {
128 const res = await uploadVideo(server.url, server.accessToken, {}) 77 video = await server.videos.upload()
129 video = res.body.video
130 } 78 }
131 79
132 { 80 {
133 const res = await getUsersList(server.url, server.accessToken) 81 const { data } = await server.users.list()
134 const users: User[] = res.body.data 82 userId = data.find(u => u.username === 'user1').id
135 83 rootId = data.find(u => u.username === 'root').id
136 userId = users.find(u => u.username === 'user1').id 84 moderatorId = data.find(u => u.username === 'moderator2').id
137 rootId = users.find(u => u.username === 'root').id
138 moderatorId = users.find(u => u.username === 'moderator2').id
139 } 85 }
140 }) 86 })
141 87
@@ -156,7 +102,7 @@ describe('Test users API validators', function () {
156 await makeGetRequest({ 102 await makeGetRequest({
157 url: server.url, 103 url: server.url,
158 path, 104 path,
159 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 105 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
160 }) 106 })
161 }) 107 })
162 108
@@ -164,8 +110,8 @@ describe('Test users API validators', function () {
164 await makeGetRequest({ 110 await makeGetRequest({
165 url: server.url, 111 url: server.url,
166 path, 112 path,
167 token: userAccessToken, 113 token: userToken,
168 statusCodeExpected: HttpStatusCode.FORBIDDEN_403 114 expectedStatus: HttpStatusCode.FORBIDDEN_403
169 }) 115 })
170 }) 116 })
171 }) 117 })
@@ -182,25 +128,25 @@ describe('Test users API validators', function () {
182 } 128 }
183 129
184 it('Should fail with a too small username', async function () { 130 it('Should fail with a too small username', async function () {
185 const fields = immutableAssign(baseCorrectParams, { username: '' }) 131 const fields = { ...baseCorrectParams, username: '' }
186 132
187 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) 133 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
188 }) 134 })
189 135
190 it('Should fail with a too long username', async function () { 136 it('Should fail with a too long username', async function () {
191 const fields = immutableAssign(baseCorrectParams, { username: 'super'.repeat(50) }) 137 const fields = { ...baseCorrectParams, username: 'super'.repeat(50) }
192 138
193 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) 139 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
194 }) 140 })
195 141
196 it('Should fail with a not lowercase username', async function () { 142 it('Should fail with a not lowercase username', async function () {
197 const fields = immutableAssign(baseCorrectParams, { username: 'Toto' }) 143 const fields = { ...baseCorrectParams, username: 'Toto' }
198 144
199 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) 145 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
200 }) 146 })
201 147
202 it('Should fail with an incorrect username', async function () { 148 it('Should fail with an incorrect username', async function () {
203 const fields = immutableAssign(baseCorrectParams, { username: 'my username' }) 149 const fields = { ...baseCorrectParams, username: 'my username' }
204 150
205 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) 151 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
206 }) 152 })
@@ -212,25 +158,25 @@ describe('Test users API validators', function () {
212 }) 158 })
213 159
214 it('Should fail with an invalid email', async function () { 160 it('Should fail with an invalid email', async function () {
215 const fields = immutableAssign(baseCorrectParams, { email: 'test_example.com' }) 161 const fields = { ...baseCorrectParams, email: 'test_example.com' }
216 162
217 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) 163 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
218 }) 164 })
219 165
220 it('Should fail with a too small password', async function () { 166 it('Should fail with a too small password', async function () {
221 const fields = immutableAssign(baseCorrectParams, { password: 'bla' }) 167 const fields = { ...baseCorrectParams, password: 'bla' }
222 168
223 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) 169 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
224 }) 170 })
225 171
226 it('Should fail with a too long password', async function () { 172 it('Should fail with a too long password', async function () {
227 const fields = immutableAssign(baseCorrectParams, { password: 'super'.repeat(61) }) 173 const fields = { ...baseCorrectParams, password: 'super'.repeat(61) }
228 174
229 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) 175 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
230 }) 176 })
231 177
232 it('Should fail with empty password and no smtp configured', async function () { 178 it('Should fail with empty password and no smtp configured', async function () {
233 const fields = immutableAssign(baseCorrectParams, { password: '' }) 179 const fields = { ...baseCorrectParams, password: '' }
234 180
235 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) 181 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
236 }) 182 })
@@ -238,33 +184,37 @@ describe('Test users API validators', function () {
238 it('Should succeed with no password on a server with smtp enabled', async function () { 184 it('Should succeed with no password on a server with smtp enabled', async function () {
239 this.timeout(20000) 185 this.timeout(20000)
240 186
241 killallServers([ server ]) 187 await killallServers([ server ])
188
189 const config = {
190 ...overrideConfig,
242 191
243 const config = immutableAssign(overrideConfig, {
244 smtp: { 192 smtp: {
245 hostname: 'localhost', 193 hostname: 'localhost',
246 port: emailPort 194 port: emailPort
247 } 195 }
248 }) 196 }
249 await reRunServer(server, config) 197 await server.run(config)
198
199 const fields = {
200 ...baseCorrectParams,
250 201
251 const fields = immutableAssign(baseCorrectParams, {
252 password: '', 202 password: '',
253 username: 'create_password', 203 username: 'create_password',
254 email: 'create_password@example.com' 204 email: 'create_password@example.com'
255 }) 205 }
256 206
257 await makePostBodyRequest({ 207 await makePostBodyRequest({
258 url: server.url, 208 url: server.url,
259 path: path, 209 path: path,
260 token: server.accessToken, 210 token: server.accessToken,
261 fields, 211 fields,
262 statusCodeExpected: HttpStatusCode.OK_200 212 expectedStatus: HttpStatusCode.OK_200
263 }) 213 })
264 }) 214 })
265 215
266 it('Should fail with invalid admin flags', async function () { 216 it('Should fail with invalid admin flags', async function () {
267 const fields = immutableAssign(baseCorrectParams, { adminFlags: 'toto' }) 217 const fields = { ...baseCorrectParams, adminFlags: 'toto' }
268 218
269 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) 219 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
270 }) 220 })
@@ -275,31 +225,31 @@ describe('Test users API validators', function () {
275 path, 225 path,
276 token: 'super token', 226 token: 'super token',
277 fields: baseCorrectParams, 227 fields: baseCorrectParams,
278 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 228 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
279 }) 229 })
280 }) 230 })
281 231
282 it('Should fail if we add a user with the same username', async function () { 232 it('Should fail if we add a user with the same username', async function () {
283 const fields = immutableAssign(baseCorrectParams, { username: 'user1' }) 233 const fields = { ...baseCorrectParams, username: 'user1' }
284 234
285 await makePostBodyRequest({ 235 await makePostBodyRequest({
286 url: server.url, 236 url: server.url,
287 path, 237 path,
288 token: server.accessToken, 238 token: server.accessToken,
289 fields, 239 fields,
290 statusCodeExpected: HttpStatusCode.CONFLICT_409 240 expectedStatus: HttpStatusCode.CONFLICT_409
291 }) 241 })
292 }) 242 })
293 243
294 it('Should fail if we add a user with the same email', async function () { 244 it('Should fail if we add a user with the same email', async function () {
295 const fields = immutableAssign(baseCorrectParams, { email: 'user1@example.com' }) 245 const fields = { ...baseCorrectParams, email: 'user1@example.com' }
296 246
297 await makePostBodyRequest({ 247 await makePostBodyRequest({
298 url: server.url, 248 url: server.url,
299 path, 249 path,
300 token: server.accessToken, 250 token: server.accessToken,
301 fields, 251 fields,
302 statusCodeExpected: HttpStatusCode.CONFLICT_409 252 expectedStatus: HttpStatusCode.CONFLICT_409
303 }) 253 })
304 }) 254 })
305 255
@@ -316,13 +266,13 @@ describe('Test users API validators', function () {
316 }) 266 })
317 267
318 it('Should fail with an invalid videoQuota', async function () { 268 it('Should fail with an invalid videoQuota', async function () {
319 const fields = immutableAssign(baseCorrectParams, { videoQuota: -5 }) 269 const fields = { ...baseCorrectParams, videoQuota: -5 }
320 270
321 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) 271 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
322 }) 272 })
323 273
324 it('Should fail with an invalid videoQuotaDaily', async function () { 274 it('Should fail with an invalid videoQuotaDaily', async function () {
325 const fields = immutableAssign(baseCorrectParams, { videoQuotaDaily: -7 }) 275 const fields = { ...baseCorrectParams, videoQuotaDaily: -7 }
326 276
327 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) 277 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
328 }) 278 })
@@ -334,46 +284,46 @@ describe('Test users API validators', function () {
334 }) 284 })
335 285
336 it('Should fail with an invalid user role', async function () { 286 it('Should fail with an invalid user role', async function () {
337 const fields = immutableAssign(baseCorrectParams, { role: 88989 }) 287 const fields = { ...baseCorrectParams, role: 88989 }
338 288
339 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) 289 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
340 }) 290 })
341 291
342 it('Should fail with a "peertube" username', async function () { 292 it('Should fail with a "peertube" username', async function () {
343 const fields = immutableAssign(baseCorrectParams, { username: 'peertube' }) 293 const fields = { ...baseCorrectParams, username: 'peertube' }
344 294
345 await makePostBodyRequest({ 295 await makePostBodyRequest({
346 url: server.url, 296 url: server.url,
347 path, 297 path,
348 token: server.accessToken, 298 token: server.accessToken,
349 fields, 299 fields,
350 statusCodeExpected: HttpStatusCode.CONFLICT_409 300 expectedStatus: HttpStatusCode.CONFLICT_409
351 }) 301 })
352 }) 302 })
353 303
354 it('Should fail to create a moderator or an admin with a moderator', async function () { 304 it('Should fail to create a moderator or an admin with a moderator', async function () {
355 for (const role of [ UserRole.MODERATOR, UserRole.ADMINISTRATOR ]) { 305 for (const role of [ UserRole.MODERATOR, UserRole.ADMINISTRATOR ]) {
356 const fields = immutableAssign(baseCorrectParams, { role }) 306 const fields = { ...baseCorrectParams, role }
357 307
358 await makePostBodyRequest({ 308 await makePostBodyRequest({
359 url: server.url, 309 url: server.url,
360 path, 310 path,
361 token: moderatorAccessToken, 311 token: moderatorToken,
362 fields, 312 fields,
363 statusCodeExpected: HttpStatusCode.FORBIDDEN_403 313 expectedStatus: HttpStatusCode.FORBIDDEN_403
364 }) 314 })
365 } 315 }
366 }) 316 })
367 317
368 it('Should succeed to create a user with a moderator', async function () { 318 it('Should succeed to create a user with a moderator', async function () {
369 const fields = immutableAssign(baseCorrectParams, { username: 'a4656', email: 'a4656@example.com', role: UserRole.USER }) 319 const fields = { ...baseCorrectParams, username: 'a4656', email: 'a4656@example.com', role: UserRole.USER }
370 320
371 await makePostBodyRequest({ 321 await makePostBodyRequest({
372 url: server.url, 322 url: server.url,
373 path, 323 path,
374 token: moderatorAccessToken, 324 token: moderatorToken,
375 fields, 325 fields,
376 statusCodeExpected: HttpStatusCode.OK_200 326 expectedStatus: HttpStatusCode.OK_200
377 }) 327 })
378 }) 328 })
379 329
@@ -383,16 +333,13 @@ describe('Test users API validators', function () {
383 path, 333 path,
384 token: server.accessToken, 334 token: server.accessToken,
385 fields: baseCorrectParams, 335 fields: baseCorrectParams,
386 statusCodeExpected: HttpStatusCode.OK_200 336 expectedStatus: HttpStatusCode.OK_200
387 }) 337 })
388 }) 338 })
389 339
390 it('Should fail with a non admin user', async function () { 340 it('Should fail with a non admin user', async function () {
391 const user = { 341 const user = { username: 'user1' }
392 username: 'user1', 342 userToken = await server.login.getAccessToken(user)
393 password: 'my super password'
394 }
395 userAccessToken = await userLogin(server, user)
396 343
397 const fields = { 344 const fields = {
398 username: 'user3', 345 username: 'user3',
@@ -400,11 +347,12 @@ describe('Test users API validators', function () {
400 password: 'my super password', 347 password: 'my super password',
401 videoQuota: 42000000 348 videoQuota: 42000000
402 } 349 }
403 await makePostBodyRequest({ url: server.url, path, token: userAccessToken, fields, statusCodeExpected: HttpStatusCode.FORBIDDEN_403 }) 350 await makePostBodyRequest({ url: server.url, path, token: userToken, fields, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
404 }) 351 })
405 }) 352 })
406 353
407 describe('When updating my account', function () { 354 describe('When updating my account', function () {
355
408 it('Should fail with an invalid email attribute', async function () { 356 it('Should fail with an invalid email attribute', async function () {
409 const fields = { 357 const fields = {
410 email: 'blabla' 358 email: 'blabla'
@@ -415,29 +363,29 @@ describe('Test users API validators', function () {
415 363
416 it('Should fail with a too small password', async function () { 364 it('Should fail with a too small password', async function () {
417 const fields = { 365 const fields = {
418 currentPassword: 'my super password', 366 currentPassword: 'password',
419 password: 'bla' 367 password: 'bla'
420 } 368 }
421 369
422 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userAccessToken, fields }) 370 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
423 }) 371 })
424 372
425 it('Should fail with a too long password', async function () { 373 it('Should fail with a too long password', async function () {
426 const fields = { 374 const fields = {
427 currentPassword: 'my super password', 375 currentPassword: 'password',
428 password: 'super'.repeat(61) 376 password: 'super'.repeat(61)
429 } 377 }
430 378
431 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userAccessToken, fields }) 379 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
432 }) 380 })
433 381
434 it('Should fail without the current password', async function () { 382 it('Should fail without the current password', async function () {
435 const fields = { 383 const fields = {
436 currentPassword: 'my super password', 384 currentPassword: 'password',
437 password: 'super'.repeat(61) 385 password: 'super'.repeat(61)
438 } 386 }
439 387
440 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userAccessToken, fields }) 388 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
441 }) 389 })
442 390
443 it('Should fail with an invalid current password', async function () { 391 it('Should fail with an invalid current password', async function () {
@@ -449,9 +397,9 @@ describe('Test users API validators', function () {
449 await makePutBodyRequest({ 397 await makePutBodyRequest({
450 url: server.url, 398 url: server.url,
451 path: path + 'me', 399 path: path + 'me',
452 token: userAccessToken, 400 token: userToken,
453 fields, 401 fields,
454 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 402 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
455 }) 403 })
456 }) 404 })
457 405
@@ -460,7 +408,7 @@ describe('Test users API validators', function () {
460 nsfwPolicy: 'hello' 408 nsfwPolicy: 'hello'
461 } 409 }
462 410
463 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userAccessToken, fields }) 411 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
464 }) 412 })
465 413
466 it('Should fail with an invalid autoPlayVideo attribute', async function () { 414 it('Should fail with an invalid autoPlayVideo attribute', async function () {
@@ -468,7 +416,7 @@ describe('Test users API validators', function () {
468 autoPlayVideo: -1 416 autoPlayVideo: -1
469 } 417 }
470 418
471 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userAccessToken, fields }) 419 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
472 }) 420 })
473 421
474 it('Should fail with an invalid autoPlayNextVideo attribute', async function () { 422 it('Should fail with an invalid autoPlayNextVideo attribute', async function () {
@@ -476,7 +424,7 @@ describe('Test users API validators', function () {
476 autoPlayNextVideo: -1 424 autoPlayNextVideo: -1
477 } 425 }
478 426
479 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userAccessToken, fields }) 427 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
480 }) 428 })
481 429
482 it('Should fail with an invalid videosHistoryEnabled attribute', async function () { 430 it('Should fail with an invalid videosHistoryEnabled attribute', async function () {
@@ -484,12 +432,12 @@ describe('Test users API validators', function () {
484 videosHistoryEnabled: -1 432 videosHistoryEnabled: -1
485 } 433 }
486 434
487 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userAccessToken, fields }) 435 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
488 }) 436 })
489 437
490 it('Should fail with an non authenticated user', async function () { 438 it('Should fail with an non authenticated user', async function () {
491 const fields = { 439 const fields = {
492 currentPassword: 'my super password', 440 currentPassword: 'password',
493 password: 'my super password' 441 password: 'my super password'
494 } 442 }
495 443
@@ -498,7 +446,7 @@ describe('Test users API validators', function () {
498 path: path + 'me', 446 path: path + 'me',
499 token: 'super token', 447 token: 'super token',
500 fields, 448 fields,
501 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 449 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
502 }) 450 })
503 }) 451 })
504 452
@@ -507,7 +455,7 @@ describe('Test users API validators', function () {
507 description: 'super'.repeat(201) 455 description: 'super'.repeat(201)
508 } 456 }
509 457
510 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userAccessToken, fields }) 458 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
511 }) 459 })
512 460
513 it('Should fail with an invalid videoLanguages attribute', async function () { 461 it('Should fail with an invalid videoLanguages attribute', async function () {
@@ -516,7 +464,7 @@ describe('Test users API validators', function () {
516 videoLanguages: 'toto' 464 videoLanguages: 'toto'
517 } 465 }
518 466
519 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userAccessToken, fields }) 467 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
520 } 468 }
521 469
522 { 470 {
@@ -529,18 +477,18 @@ describe('Test users API validators', function () {
529 videoLanguages: languages 477 videoLanguages: languages
530 } 478 }
531 479
532 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userAccessToken, fields }) 480 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
533 } 481 }
534 }) 482 })
535 483
536 it('Should fail with an invalid theme', async function () { 484 it('Should fail with an invalid theme', async function () {
537 const fields = { theme: 'invalid' } 485 const fields = { theme: 'invalid' }
538 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userAccessToken, fields }) 486 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
539 }) 487 })
540 488
541 it('Should fail with an unknown theme', async function () { 489 it('Should fail with an unknown theme', async function () {
542 const fields = { theme: 'peertube-theme-unknown' } 490 const fields = { theme: 'peertube-theme-unknown' }
543 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userAccessToken, fields }) 491 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
544 }) 492 })
545 493
546 it('Should fail with an invalid noInstanceConfigWarningModal attribute', async function () { 494 it('Should fail with an invalid noInstanceConfigWarningModal attribute', async function () {
@@ -548,7 +496,7 @@ describe('Test users API validators', function () {
548 noInstanceConfigWarningModal: -1 496 noInstanceConfigWarningModal: -1
549 } 497 }
550 498
551 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userAccessToken, fields }) 499 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
552 }) 500 })
553 501
554 it('Should fail with an invalid noWelcomeModal attribute', async function () { 502 it('Should fail with an invalid noWelcomeModal attribute', async function () {
@@ -556,12 +504,12 @@ describe('Test users API validators', function () {
556 noWelcomeModal: -1 504 noWelcomeModal: -1
557 } 505 }
558 506
559 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userAccessToken, fields }) 507 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields })
560 }) 508 })
561 509
562 it('Should succeed to change password with the correct params', async function () { 510 it('Should succeed to change password with the correct params', async function () {
563 const fields = { 511 const fields = {
564 currentPassword: 'my super password', 512 currentPassword: 'password',
565 password: 'my super password', 513 password: 'my super password',
566 nsfwPolicy: 'blur', 514 nsfwPolicy: 'blur',
567 autoPlayVideo: false, 515 autoPlayVideo: false,
@@ -574,9 +522,9 @@ describe('Test users API validators', function () {
574 await makePutBodyRequest({ 522 await makePutBodyRequest({
575 url: server.url, 523 url: server.url,
576 path: path + 'me', 524 path: path + 'me',
577 token: userAccessToken, 525 token: userToken,
578 fields, 526 fields,
579 statusCodeExpected: HttpStatusCode.NO_CONTENT_204 527 expectedStatus: HttpStatusCode.NO_CONTENT_204
580 }) 528 })
581 }) 529 })
582 530
@@ -589,9 +537,9 @@ describe('Test users API validators', function () {
589 await makePutBodyRequest({ 537 await makePutBodyRequest({
590 url: server.url, 538 url: server.url,
591 path: path + 'me', 539 path: path + 'me',
592 token: userAccessToken, 540 token: userToken,
593 fields, 541 fields,
594 statusCodeExpected: HttpStatusCode.NO_CONTENT_204 542 expectedStatus: HttpStatusCode.NO_CONTENT_204
595 }) 543 })
596 }) 544 })
597 }) 545 })
@@ -623,7 +571,7 @@ describe('Test users API validators', function () {
623 path: path + '/me/avatar/pick', 571 path: path + '/me/avatar/pick',
624 fields, 572 fields,
625 attaches, 573 attaches,
626 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 574 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
627 }) 575 })
628 }) 576 })
629 577
@@ -638,7 +586,7 @@ describe('Test users API validators', function () {
638 token: server.accessToken, 586 token: server.accessToken,
639 fields, 587 fields,
640 attaches, 588 attaches,
641 statusCodeExpected: HttpStatusCode.OK_200 589 expectedStatus: HttpStatusCode.OK_200
642 }) 590 })
643 }) 591 })
644 }) 592 })
@@ -646,28 +594,28 @@ describe('Test users API validators', function () {
646 describe('When managing my scoped tokens', function () { 594 describe('When managing my scoped tokens', function () {
647 595
648 it('Should fail to get my scoped tokens with an non authenticated user', async function () { 596 it('Should fail to get my scoped tokens with an non authenticated user', async function () {
649 await getUserScopedTokens(server.url, null, HttpStatusCode.UNAUTHORIZED_401) 597 await server.users.getMyScopedTokens({ token: null, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
650 }) 598 })
651 599
652 it('Should fail to get my scoped tokens with a bad token', async function () { 600 it('Should fail to get my scoped tokens with a bad token', async function () {
653 await getUserScopedTokens(server.url, 'bad', HttpStatusCode.UNAUTHORIZED_401) 601 await server.users.getMyScopedTokens({ token: 'bad', expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
654 602
655 }) 603 })
656 604
657 it('Should succeed to get my scoped tokens', async function () { 605 it('Should succeed to get my scoped tokens', async function () {
658 await getUserScopedTokens(server.url, server.accessToken) 606 await server.users.getMyScopedTokens()
659 }) 607 })
660 608
661 it('Should fail to renew my scoped tokens with an non authenticated user', async function () { 609 it('Should fail to renew my scoped tokens with an non authenticated user', async function () {
662 await renewUserScopedTokens(server.url, null, HttpStatusCode.UNAUTHORIZED_401) 610 await server.users.renewMyScopedTokens({ token: null, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
663 }) 611 })
664 612
665 it('Should fail to renew my scoped tokens with a bad token', async function () { 613 it('Should fail to renew my scoped tokens with a bad token', async function () {
666 await renewUserScopedTokens(server.url, 'bad', HttpStatusCode.UNAUTHORIZED_401) 614 await server.users.renewMyScopedTokens({ token: 'bad', expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
667 }) 615 })
668 616
669 it('Should succeed to renew my scoped tokens', async function () { 617 it('Should succeed to renew my scoped tokens', async function () {
670 await renewUserScopedTokens(server.url, server.accessToken) 618 await server.users.renewMyScopedTokens()
671 }) 619 })
672 }) 620 })
673 621
@@ -678,16 +626,16 @@ describe('Test users API validators', function () {
678 url: server.url, 626 url: server.url,
679 path: path + userId, 627 path: path + userId,
680 token: 'super token', 628 token: 'super token',
681 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 629 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
682 }) 630 })
683 }) 631 })
684 632
685 it('Should fail with a non admin user', async function () { 633 it('Should fail with a non admin user', async function () {
686 await makeGetRequest({ url: server.url, path, token: userAccessToken, statusCodeExpected: HttpStatusCode.FORBIDDEN_403 }) 634 await makeGetRequest({ url: server.url, path, token: userToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
687 }) 635 })
688 636
689 it('Should succeed with the correct params', async function () { 637 it('Should succeed with the correct params', async function () {
690 await makeGetRequest({ url: server.url, path: path + userId, token: server.accessToken, statusCodeExpected: HttpStatusCode.OK_200 }) 638 await makeGetRequest({ url: server.url, path: path + userId, token: server.accessToken, expectedStatus: HttpStatusCode.OK_200 })
691 }) 639 })
692 }) 640 })
693 641
@@ -727,7 +675,7 @@ describe('Test users API validators', function () {
727 675
728 it('Should fail with a too small password', async function () { 676 it('Should fail with a too small password', async function () {
729 const fields = { 677 const fields = {
730 currentPassword: 'my super password', 678 currentPassword: 'password',
731 password: 'bla' 679 password: 'bla'
732 } 680 }
733 681
@@ -736,7 +684,7 @@ describe('Test users API validators', function () {
736 684
737 it('Should fail with a too long password', async function () { 685 it('Should fail with a too long password', async function () {
738 const fields = { 686 const fields = {
739 currentPassword: 'my super password', 687 currentPassword: 'password',
740 password: 'super'.repeat(61) 688 password: 'super'.repeat(61)
741 } 689 }
742 690
@@ -753,7 +701,7 @@ describe('Test users API validators', function () {
753 path: path + userId, 701 path: path + userId,
754 token: 'super token', 702 token: 'super token',
755 fields, 703 fields,
756 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 704 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
757 }) 705 })
758 }) 706 })
759 707
@@ -779,9 +727,9 @@ describe('Test users API validators', function () {
779 await makePutBodyRequest({ 727 await makePutBodyRequest({
780 url: server.url, 728 url: server.url,
781 path: path + moderatorId, 729 path: path + moderatorId,
782 token: moderatorAccessToken, 730 token: moderatorToken,
783 fields, 731 fields,
784 statusCodeExpected: HttpStatusCode.FORBIDDEN_403 732 expectedStatus: HttpStatusCode.FORBIDDEN_403
785 }) 733 })
786 }) 734 })
787 735
@@ -793,9 +741,9 @@ describe('Test users API validators', function () {
793 await makePutBodyRequest({ 741 await makePutBodyRequest({
794 url: server.url, 742 url: server.url,
795 path: path + userId, 743 path: path + userId,
796 token: moderatorAccessToken, 744 token: moderatorToken,
797 fields, 745 fields,
798 statusCodeExpected: HttpStatusCode.NO_CONTENT_204 746 expectedStatus: HttpStatusCode.NO_CONTENT_204
799 }) 747 })
800 }) 748 })
801 749
@@ -812,38 +760,44 @@ describe('Test users API validators', function () {
812 path: path + userId, 760 path: path + userId,
813 token: server.accessToken, 761 token: server.accessToken,
814 fields, 762 fields,
815 statusCodeExpected: HttpStatusCode.NO_CONTENT_204 763 expectedStatus: HttpStatusCode.NO_CONTENT_204
816 }) 764 })
817 }) 765 })
818 }) 766 })
819 767
820 describe('When getting my information', function () { 768 describe('When getting my information', function () {
821 it('Should fail with a non authenticated user', async function () { 769 it('Should fail with a non authenticated user', async function () {
822 await getMyUserInformation(server.url, 'fake_token', HttpStatusCode.UNAUTHORIZED_401) 770 await server.users.getMyInfo({ token: 'fake_token', expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
823 }) 771 })
824 772
825 it('Should success with the correct parameters', async function () { 773 it('Should success with the correct parameters', async function () {
826 await getMyUserInformation(server.url, userAccessToken) 774 await server.users.getMyInfo({ token: userToken })
827 }) 775 })
828 }) 776 })
829 777
830 describe('When getting my video rating', function () { 778 describe('When getting my video rating', function () {
779 let command: UsersCommand
780
781 before(function () {
782 command = server.users
783 })
784
831 it('Should fail with a non authenticated user', async function () { 785 it('Should fail with a non authenticated user', async function () {
832 await getMyUserVideoRating(server.url, 'fake_token', video.id, HttpStatusCode.UNAUTHORIZED_401) 786 await command.getMyRating({ token: 'fake_token', videoId: video.id, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
833 }) 787 })
834 788
835 it('Should fail with an incorrect video uuid', async function () { 789 it('Should fail with an incorrect video uuid', async function () {
836 await getMyUserVideoRating(server.url, server.accessToken, 'blabla', HttpStatusCode.BAD_REQUEST_400) 790 await command.getMyRating({ videoId: 'blabla', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
837 }) 791 })
838 792
839 it('Should fail with an unknown video', async function () { 793 it('Should fail with an unknown video', async function () {
840 await getMyUserVideoRating(server.url, server.accessToken, '4da6fde3-88f7-4d16-b119-108df5630b06', HttpStatusCode.NOT_FOUND_404) 794 await command.getMyRating({ videoId: '4da6fde3-88f7-4d16-b119-108df5630b06', expectedStatus: HttpStatusCode.NOT_FOUND_404 })
841 }) 795 })
842 796
843 it('Should succeed with the correct parameters', async function () { 797 it('Should succeed with the correct parameters', async function () {
844 await getMyUserVideoRating(server.url, server.accessToken, video.id) 798 await command.getMyRating({ videoId: video.id })
845 await getMyUserVideoRating(server.url, server.accessToken, video.uuid) 799 await command.getMyRating({ videoId: video.uuid })
846 await getMyUserVideoRating(server.url, server.accessToken, video.shortUUID) 800 await command.getMyRating({ videoId: video.shortUUID })
847 }) 801 })
848 }) 802 })
849 803
@@ -851,80 +805,93 @@ describe('Test users API validators', function () {
851 const path = '/api/v1/accounts/user1/ratings' 805 const path = '/api/v1/accounts/user1/ratings'
852 806
853 it('Should fail with a bad start pagination', async function () { 807 it('Should fail with a bad start pagination', async function () {
854 await checkBadStartPagination(server.url, path, userAccessToken) 808 await checkBadStartPagination(server.url, path, userToken)
855 }) 809 })
856 810
857 it('Should fail with a bad count pagination', async function () { 811 it('Should fail with a bad count pagination', async function () {
858 await checkBadCountPagination(server.url, path, userAccessToken) 812 await checkBadCountPagination(server.url, path, userToken)
859 }) 813 })
860 814
861 it('Should fail with an incorrect sort', async function () { 815 it('Should fail with an incorrect sort', async function () {
862 await checkBadSortPagination(server.url, path, userAccessToken) 816 await checkBadSortPagination(server.url, path, userToken)
863 }) 817 })
864 818
865 it('Should fail with a unauthenticated user', async function () { 819 it('Should fail with a unauthenticated user', async function () {
866 await makeGetRequest({ url: server.url, path, statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 }) 820 await makeGetRequest({ url: server.url, path, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
867 }) 821 })
868 822
869 it('Should fail with a another user', async function () { 823 it('Should fail with a another user', async function () {
870 await makeGetRequest({ url: server.url, path, token: server.accessToken, statusCodeExpected: HttpStatusCode.FORBIDDEN_403 }) 824 await makeGetRequest({ url: server.url, path, token: server.accessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
871 }) 825 })
872 826
873 it('Should fail with a bad type', async function () { 827 it('Should fail with a bad type', async function () {
874 await makeGetRequest({ 828 await makeGetRequest({
875 url: server.url, 829 url: server.url,
876 path, 830 path,
877 token: userAccessToken, 831 token: userToken,
878 query: { rating: 'toto ' }, 832 query: { rating: 'toto ' },
879 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 833 expectedStatus: HttpStatusCode.BAD_REQUEST_400
880 }) 834 })
881 }) 835 })
882 836
883 it('Should succeed with the correct params', async function () { 837 it('Should succeed with the correct params', async function () {
884 await makeGetRequest({ url: server.url, path, token: userAccessToken, statusCodeExpected: HttpStatusCode.OK_200 }) 838 await makeGetRequest({ url: server.url, path, token: userToken, expectedStatus: HttpStatusCode.OK_200 })
885 }) 839 })
886 }) 840 })
887 841
888 describe('When blocking/unblocking/removing user', function () { 842 describe('When blocking/unblocking/removing user', function () {
843
889 it('Should fail with an incorrect id', async function () { 844 it('Should fail with an incorrect id', async function () {
890 await removeUser(server.url, 'blabla', server.accessToken, HttpStatusCode.BAD_REQUEST_400) 845 const options = { userId: 'blabla' as any, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }
891 await blockUser(server.url, 'blabla', server.accessToken, HttpStatusCode.BAD_REQUEST_400) 846
892 await unblockUser(server.url, 'blabla', server.accessToken, HttpStatusCode.BAD_REQUEST_400) 847 await server.users.remove(options)
848 await server.users.banUser({ userId: 'blabla' as any, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
849 await server.users.unbanUser({ userId: 'blabla' as any, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
893 }) 850 })
894 851
895 it('Should fail with the root user', async function () { 852 it('Should fail with the root user', async function () {
896 await removeUser(server.url, rootId, server.accessToken, HttpStatusCode.BAD_REQUEST_400) 853 const options = { userId: rootId, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }
897 await blockUser(server.url, rootId, server.accessToken, HttpStatusCode.BAD_REQUEST_400) 854
898 await unblockUser(server.url, rootId, server.accessToken, HttpStatusCode.BAD_REQUEST_400) 855 await server.users.remove(options)
856 await server.users.banUser(options)
857 await server.users.unbanUser(options)
899 }) 858 })
900 859
901 it('Should return 404 with a non existing id', async function () { 860 it('Should return 404 with a non existing id', async function () {
902 await removeUser(server.url, 4545454, server.accessToken, HttpStatusCode.NOT_FOUND_404) 861 const options = { userId: 4545454, expectedStatus: HttpStatusCode.NOT_FOUND_404 }
903 await blockUser(server.url, 4545454, server.accessToken, HttpStatusCode.NOT_FOUND_404) 862
904 await unblockUser(server.url, 4545454, server.accessToken, HttpStatusCode.NOT_FOUND_404) 863 await server.users.remove(options)
864 await server.users.banUser(options)
865 await server.users.unbanUser(options)
905 }) 866 })
906 867
907 it('Should fail with a non admin user', async function () { 868 it('Should fail with a non admin user', async function () {
908 await removeUser(server.url, userId, userAccessToken, HttpStatusCode.FORBIDDEN_403) 869 const options = { userId, token: userToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 }
909 await blockUser(server.url, userId, userAccessToken, HttpStatusCode.FORBIDDEN_403) 870
910 await unblockUser(server.url, userId, userAccessToken, HttpStatusCode.FORBIDDEN_403) 871 await server.users.remove(options)
872 await server.users.banUser(options)
873 await server.users.unbanUser(options)
911 }) 874 })
912 875
913 it('Should fail on a moderator with a moderator', async function () { 876 it('Should fail on a moderator with a moderator', async function () {
914 await removeUser(server.url, moderatorId, moderatorAccessToken, HttpStatusCode.FORBIDDEN_403) 877 const options = { userId: moderatorId, token: moderatorToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 }
915 await blockUser(server.url, moderatorId, moderatorAccessToken, HttpStatusCode.FORBIDDEN_403) 878
916 await unblockUser(server.url, moderatorId, moderatorAccessToken, HttpStatusCode.FORBIDDEN_403) 879 await server.users.remove(options)
880 await server.users.banUser(options)
881 await server.users.unbanUser(options)
917 }) 882 })
918 883
919 it('Should succeed on a user with a moderator', async function () { 884 it('Should succeed on a user with a moderator', async function () {
920 await blockUser(server.url, userId, moderatorAccessToken) 885 const options = { userId, token: moderatorToken }
921 await unblockUser(server.url, userId, moderatorAccessToken) 886
887 await server.users.banUser(options)
888 await server.users.unbanUser(options)
922 }) 889 })
923 }) 890 })
924 891
925 describe('When deleting our account', function () { 892 describe('When deleting our account', function () {
926 it('Should fail with with the root account', async function () { 893 it('Should fail with with the root account', async function () {
927 await deleteMe(server.url, server.accessToken, HttpStatusCode.BAD_REQUEST_400) 894 await server.users.deleteMe({ expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
928 }) 895 })
929 }) 896 })
930 897
@@ -938,19 +905,19 @@ describe('Test users API validators', function () {
938 } 905 }
939 906
940 it('Should fail with a too small username', async function () { 907 it('Should fail with a too small username', async function () {
941 const fields = immutableAssign(baseCorrectParams, { username: '' }) 908 const fields = { ...baseCorrectParams, username: '' }
942 909
943 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields }) 910 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
944 }) 911 })
945 912
946 it('Should fail with a too long username', async function () { 913 it('Should fail with a too long username', async function () {
947 const fields = immutableAssign(baseCorrectParams, { username: 'super'.repeat(50) }) 914 const fields = { ...baseCorrectParams, username: 'super'.repeat(50) }
948 915
949 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields }) 916 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
950 }) 917 })
951 918
952 it('Should fail with an incorrect username', async function () { 919 it('Should fail with an incorrect username', async function () {
953 const fields = immutableAssign(baseCorrectParams, { username: 'my username' }) 920 const fields = { ...baseCorrectParams, username: 'my username' }
954 921
955 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields }) 922 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
956 }) 923 })
@@ -962,108 +929,108 @@ describe('Test users API validators', function () {
962 }) 929 })
963 930
964 it('Should fail with an invalid email', async function () { 931 it('Should fail with an invalid email', async function () {
965 const fields = immutableAssign(baseCorrectParams, { email: 'test_example.com' }) 932 const fields = { ...baseCorrectParams, email: 'test_example.com' }
966 933
967 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields }) 934 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
968 }) 935 })
969 936
970 it('Should fail with a too small password', async function () { 937 it('Should fail with a too small password', async function () {
971 const fields = immutableAssign(baseCorrectParams, { password: 'bla' }) 938 const fields = { ...baseCorrectParams, password: 'bla' }
972 939
973 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields }) 940 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
974 }) 941 })
975 942
976 it('Should fail with a too long password', async function () { 943 it('Should fail with a too long password', async function () {
977 const fields = immutableAssign(baseCorrectParams, { password: 'super'.repeat(61) }) 944 const fields = { ...baseCorrectParams, password: 'super'.repeat(61) }
978 945
979 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields }) 946 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
980 }) 947 })
981 948
982 it('Should fail if we register a user with the same username', async function () { 949 it('Should fail if we register a user with the same username', async function () {
983 const fields = immutableAssign(baseCorrectParams, { username: 'root' }) 950 const fields = { ...baseCorrectParams, username: 'root' }
984 951
985 await makePostBodyRequest({ 952 await makePostBodyRequest({
986 url: server.url, 953 url: server.url,
987 path: registrationPath, 954 path: registrationPath,
988 token: server.accessToken, 955 token: server.accessToken,
989 fields, 956 fields,
990 statusCodeExpected: HttpStatusCode.CONFLICT_409 957 expectedStatus: HttpStatusCode.CONFLICT_409
991 }) 958 })
992 }) 959 })
993 960
994 it('Should fail with a "peertube" username', async function () { 961 it('Should fail with a "peertube" username', async function () {
995 const fields = immutableAssign(baseCorrectParams, { username: 'peertube' }) 962 const fields = { ...baseCorrectParams, username: 'peertube' }
996 963
997 await makePostBodyRequest({ 964 await makePostBodyRequest({
998 url: server.url, 965 url: server.url,
999 path: registrationPath, 966 path: registrationPath,
1000 token: server.accessToken, 967 token: server.accessToken,
1001 fields, 968 fields,
1002 statusCodeExpected: HttpStatusCode.CONFLICT_409 969 expectedStatus: HttpStatusCode.CONFLICT_409
1003 }) 970 })
1004 }) 971 })
1005 972
1006 it('Should fail if we register a user with the same email', async function () { 973 it('Should fail if we register a user with the same email', async function () {
1007 const fields = immutableAssign(baseCorrectParams, { email: 'admin' + server.internalServerNumber + '@example.com' }) 974 const fields = { ...baseCorrectParams, email: 'admin' + server.internalServerNumber + '@example.com' }
1008 975
1009 await makePostBodyRequest({ 976 await makePostBodyRequest({
1010 url: server.url, 977 url: server.url,
1011 path: registrationPath, 978 path: registrationPath,
1012 token: server.accessToken, 979 token: server.accessToken,
1013 fields, 980 fields,
1014 statusCodeExpected: HttpStatusCode.CONFLICT_409 981 expectedStatus: HttpStatusCode.CONFLICT_409
1015 }) 982 })
1016 }) 983 })
1017 984
1018 it('Should fail with a bad display name', async function () { 985 it('Should fail with a bad display name', async function () {
1019 const fields = immutableAssign(baseCorrectParams, { displayName: 'a'.repeat(150) }) 986 const fields = { ...baseCorrectParams, displayName: 'a'.repeat(150) }
1020 987
1021 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields }) 988 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
1022 }) 989 })
1023 990
1024 it('Should fail with a bad channel name', async function () { 991 it('Should fail with a bad channel name', async function () {
1025 const fields = immutableAssign(baseCorrectParams, { channel: { name: '[]azf', displayName: 'toto' } }) 992 const fields = { ...baseCorrectParams, channel: { name: '[]azf', displayName: 'toto' } }
1026 993
1027 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields }) 994 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
1028 }) 995 })
1029 996
1030 it('Should fail with a bad channel display name', async function () { 997 it('Should fail with a bad channel display name', async function () {
1031 const fields = immutableAssign(baseCorrectParams, { channel: { name: 'toto', displayName: '' } }) 998 const fields = { ...baseCorrectParams, channel: { name: 'toto', displayName: '' } }
1032 999
1033 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields }) 1000 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
1034 }) 1001 })
1035 1002
1036 it('Should fail with a channel name that is the same as username', async function () { 1003 it('Should fail with a channel name that is the same as username', async function () {
1037 const source = { username: 'super_user', channel: { name: 'super_user', displayName: 'display name' } } 1004 const source = { username: 'super_user', channel: { name: 'super_user', displayName: 'display name' } }
1038 const fields = immutableAssign(baseCorrectParams, source) 1005 const fields = { ...baseCorrectParams, ...source }
1039 1006
1040 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields }) 1007 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
1041 }) 1008 })
1042 1009
1043 it('Should fail with an existing channel', async function () { 1010 it('Should fail with an existing channel', async function () {
1044 const videoChannelAttributesArg = { name: 'existing_channel', displayName: 'hello', description: 'super description' } 1011 const attributes = { name: 'existing_channel', displayName: 'hello', description: 'super description' }
1045 await addVideoChannel(server.url, server.accessToken, videoChannelAttributesArg) 1012 await server.channels.create({ attributes })
1046 1013
1047 const fields = immutableAssign(baseCorrectParams, { channel: { name: 'existing_channel', displayName: 'toto' } }) 1014 const fields = { ...baseCorrectParams, channel: { name: 'existing_channel', displayName: 'toto' } }
1048 1015
1049 await makePostBodyRequest({ 1016 await makePostBodyRequest({
1050 url: server.url, 1017 url: server.url,
1051 path: registrationPath, 1018 path: registrationPath,
1052 token: server.accessToken, 1019 token: server.accessToken,
1053 fields, 1020 fields,
1054 statusCodeExpected: HttpStatusCode.CONFLICT_409 1021 expectedStatus: HttpStatusCode.CONFLICT_409
1055 }) 1022 })
1056 }) 1023 })
1057 1024
1058 it('Should succeed with the correct params', async function () { 1025 it('Should succeed with the correct params', async function () {
1059 const fields = immutableAssign(baseCorrectParams, { channel: { name: 'super_channel', displayName: 'toto' } }) 1026 const fields = { ...baseCorrectParams, channel: { name: 'super_channel', displayName: 'toto' } }
1060 1027
1061 await makePostBodyRequest({ 1028 await makePostBodyRequest({
1062 url: server.url, 1029 url: server.url,
1063 path: registrationPath, 1030 path: registrationPath,
1064 token: server.accessToken, 1031 token: server.accessToken,
1065 fields: fields, 1032 fields: fields,
1066 statusCodeExpected: HttpStatusCode.NO_CONTENT_204 1033 expectedStatus: HttpStatusCode.NO_CONTENT_204
1067 }) 1034 })
1068 }) 1035 })
1069 1036
@@ -1079,14 +1046,14 @@ describe('Test users API validators', function () {
1079 path: registrationPath, 1046 path: registrationPath,
1080 token: serverWithRegistrationDisabled.accessToken, 1047 token: serverWithRegistrationDisabled.accessToken,
1081 fields, 1048 fields,
1082 statusCodeExpected: HttpStatusCode.FORBIDDEN_403 1049 expectedStatus: HttpStatusCode.FORBIDDEN_403
1083 }) 1050 })
1084 }) 1051 })
1085 }) 1052 })
1086 1053
1087 describe('When registering multiple users on a server with users limit', function () { 1054 describe('When registering multiple users on a server with users limit', function () {
1088 it('Should fail when after 3 registrations', async function () { 1055 it('Should fail when after 3 registrations', async function () {
1089 await registerUser(server.url, 'user42', 'super password', HttpStatusCode.FORBIDDEN_403) 1056 await server.users.register({ username: 'user42', expectedStatus: HttpStatusCode.FORBIDDEN_403 })
1090 }) 1057 })
1091 }) 1058 })
1092 1059
@@ -1113,7 +1080,7 @@ describe('Test users API validators', function () {
1113 path, 1080 path,
1114 token: server.accessToken, 1081 token: server.accessToken,
1115 fields, 1082 fields,
1116 statusCodeExpected: HttpStatusCode.NO_CONTENT_204 1083 expectedStatus: HttpStatusCode.NO_CONTENT_204
1117 }) 1084 })
1118 }) 1085 })
1119 }) 1086 })
@@ -1141,7 +1108,7 @@ describe('Test users API validators', function () {
1141 path, 1108 path,
1142 token: server.accessToken, 1109 token: server.accessToken,
1143 fields, 1110 fields,
1144 statusCodeExpected: HttpStatusCode.NO_CONTENT_204 1111 expectedStatus: HttpStatusCode.NO_CONTENT_204
1145 }) 1112 })
1146 }) 1113 })
1147 }) 1114 })
diff --git a/server/tests/api/check-params/video-blacklist.ts b/server/tests/api/check-params/video-blacklist.ts
index ce7f5fa17..d28c6a952 100644
--- a/server/tests/api/check-params/video-blacklist.ts
+++ b/server/tests/api/check-params/video-blacklist.ts
@@ -1,46 +1,37 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2 2
3import 'mocha' 3import 'mocha'
4 4import { expect } from 'chai'
5import { 5import {
6 BlacklistCommand,
7 checkBadCountPagination,
8 checkBadSortPagination,
9 checkBadStartPagination,
6 cleanupTests, 10 cleanupTests,
7 createUser, 11 createMultipleServers,
8 doubleFollow, 12 doubleFollow,
9 flushAndRunMultipleServers,
10 getBlacklistedVideosList,
11 getVideo,
12 getVideoWithToken,
13 makePostBodyRequest, 13 makePostBodyRequest,
14 makePutBodyRequest, 14 makePutBodyRequest,
15 removeVideoFromBlacklist, 15 PeerTubeServer,
16 ServerInfo,
17 setAccessTokensToServers, 16 setAccessTokensToServers,
18 uploadVideo,
19 userLogin,
20 waitJobs 17 waitJobs
21} from '../../../../shared/extra-utils' 18} from '@shared/extra-utils'
22import { 19import { HttpStatusCode, VideoBlacklistType } from '@shared/models'
23 checkBadCountPagination,
24 checkBadSortPagination,
25 checkBadStartPagination
26} from '../../../../shared/extra-utils/requests/check-api-params'
27import { VideoBlacklistType, VideoDetails } from '../../../../shared/models/videos'
28import { expect } from 'chai'
29import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
30 20
31describe('Test video blacklist API validators', function () { 21describe('Test video blacklist API validators', function () {
32 let servers: ServerInfo[] 22 let servers: PeerTubeServer[]
33 let notBlacklistedVideoId: number 23 let notBlacklistedVideoId: string
34 let remoteVideoUUID: string 24 let remoteVideoUUID: string
35 let userAccessToken1 = '' 25 let userAccessToken1 = ''
36 let userAccessToken2 = '' 26 let userAccessToken2 = ''
27 let command: BlacklistCommand
37 28
38 // --------------------------------------------------------------- 29 // ---------------------------------------------------------------
39 30
40 before(async function () { 31 before(async function () {
41 this.timeout(120000) 32 this.timeout(120000)
42 33
43 servers = await flushAndRunMultipleServers(2) 34 servers = await createMultipleServers(2)
44 35
45 await setAccessTokensToServers(servers) 36 await setAccessTokensToServers(servers)
46 await doubleFollow(servers[0], servers[1]) 37 await doubleFollow(servers[0], servers[1])
@@ -48,40 +39,41 @@ describe('Test video blacklist API validators', function () {
48 { 39 {
49 const username = 'user1' 40 const username = 'user1'
50 const password = 'my super password' 41 const password = 'my super password'
51 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: username, password: password }) 42 await servers[0].users.create({ username: username, password: password })
52 userAccessToken1 = await userLogin(servers[0], { username, password }) 43 userAccessToken1 = await servers[0].login.getAccessToken({ username, password })
53 } 44 }
54 45
55 { 46 {
56 const username = 'user2' 47 const username = 'user2'
57 const password = 'my super password' 48 const password = 'my super password'
58 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: username, password: password }) 49 await servers[0].users.create({ username: username, password: password })
59 userAccessToken2 = await userLogin(servers[0], { username, password }) 50 userAccessToken2 = await servers[0].login.getAccessToken({ username, password })
60 } 51 }
61 52
62 { 53 {
63 const res = await uploadVideo(servers[0].url, userAccessToken1, {}) 54 servers[0].store.video = await servers[0].videos.upload({ token: userAccessToken1 })
64 servers[0].video = res.body.video
65 } 55 }
66 56
67 { 57 {
68 const res = await uploadVideo(servers[0].url, servers[0].accessToken, {}) 58 const { uuid } = await servers[0].videos.upload()
69 notBlacklistedVideoId = res.body.video.uuid 59 notBlacklistedVideoId = uuid
70 } 60 }
71 61
72 { 62 {
73 const res = await uploadVideo(servers[1].url, servers[1].accessToken, {}) 63 const { uuid } = await servers[1].videos.upload()
74 remoteVideoUUID = res.body.video.uuid 64 remoteVideoUUID = uuid
75 } 65 }
76 66
77 await waitJobs(servers) 67 await waitJobs(servers)
68
69 command = servers[0].blacklist
78 }) 70 })
79 71
80 describe('When adding a video in blacklist', function () { 72 describe('When adding a video in blacklist', function () {
81 const basePath = '/api/v1/videos/' 73 const basePath = '/api/v1/videos/'
82 74
83 it('Should fail with nothing', async function () { 75 it('Should fail with nothing', async function () {
84 const path = basePath + servers[0].video + '/blacklist' 76 const path = basePath + servers[0].store.video + '/blacklist'
85 const fields = {} 77 const fields = {}
86 await makePostBodyRequest({ url: servers[0].url, path, token: servers[0].accessToken, fields }) 78 await makePostBodyRequest({ url: servers[0].url, path, token: servers[0].accessToken, fields })
87 }) 79 })
@@ -93,25 +85,25 @@ describe('Test video blacklist API validators', function () {
93 }) 85 })
94 86
95 it('Should fail with a non authenticated user', async function () { 87 it('Should fail with a non authenticated user', async function () {
96 const path = basePath + servers[0].video + '/blacklist' 88 const path = basePath + servers[0].store.video + '/blacklist'
97 const fields = {} 89 const fields = {}
98 await makePostBodyRequest({ url: servers[0].url, path, token: 'hello', fields, statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 }) 90 await makePostBodyRequest({ url: servers[0].url, path, token: 'hello', fields, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
99 }) 91 })
100 92
101 it('Should fail with a non admin user', async function () { 93 it('Should fail with a non admin user', async function () {
102 const path = basePath + servers[0].video + '/blacklist' 94 const path = basePath + servers[0].store.video + '/blacklist'
103 const fields = {} 95 const fields = {}
104 await makePostBodyRequest({ 96 await makePostBodyRequest({
105 url: servers[0].url, 97 url: servers[0].url,
106 path, 98 path,
107 token: userAccessToken2, 99 token: userAccessToken2,
108 fields, 100 fields,
109 statusCodeExpected: HttpStatusCode.FORBIDDEN_403 101 expectedStatus: HttpStatusCode.FORBIDDEN_403
110 }) 102 })
111 }) 103 })
112 104
113 it('Should fail with an invalid reason', async function () { 105 it('Should fail with an invalid reason', async function () {
114 const path = basePath + servers[0].video.uuid + '/blacklist' 106 const path = basePath + servers[0].store.video.uuid + '/blacklist'
115 const fields = { reason: 'a'.repeat(305) } 107 const fields = { reason: 'a'.repeat(305) }
116 108
117 await makePostBodyRequest({ url: servers[0].url, path, token: servers[0].accessToken, fields }) 109 await makePostBodyRequest({ url: servers[0].url, path, token: servers[0].accessToken, fields })
@@ -126,12 +118,12 @@ describe('Test video blacklist API validators', function () {
126 path, 118 path,
127 token: servers[0].accessToken, 119 token: servers[0].accessToken,
128 fields, 120 fields,
129 statusCodeExpected: HttpStatusCode.CONFLICT_409 121 expectedStatus: HttpStatusCode.CONFLICT_409
130 }) 122 })
131 }) 123 })
132 124
133 it('Should succeed with the correct params', async function () { 125 it('Should succeed with the correct params', async function () {
134 const path = basePath + servers[0].video.uuid + '/blacklist' 126 const path = basePath + servers[0].store.video.uuid + '/blacklist'
135 const fields = {} 127 const fields = {}
136 128
137 await makePostBodyRequest({ 129 await makePostBodyRequest({
@@ -139,7 +131,7 @@ describe('Test video blacklist API validators', function () {
139 path, 131 path,
140 token: servers[0].accessToken, 132 token: servers[0].accessToken,
141 fields, 133 fields,
142 statusCodeExpected: HttpStatusCode.NO_CONTENT_204 134 expectedStatus: HttpStatusCode.NO_CONTENT_204
143 }) 135 })
144 }) 136 })
145 }) 137 })
@@ -161,37 +153,37 @@ describe('Test video blacklist API validators', function () {
161 path, 153 path,
162 token: servers[0].accessToken, 154 token: servers[0].accessToken,
163 fields, 155 fields,
164 statusCodeExpected: HttpStatusCode.NOT_FOUND_404 156 expectedStatus: HttpStatusCode.NOT_FOUND_404
165 }) 157 })
166 }) 158 })
167 159
168 it('Should fail with a non authenticated user', async function () { 160 it('Should fail with a non authenticated user', async function () {
169 const path = basePath + servers[0].video + '/blacklist' 161 const path = basePath + servers[0].store.video + '/blacklist'
170 const fields = {} 162 const fields = {}
171 await makePutBodyRequest({ url: servers[0].url, path, token: 'hello', fields, statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 }) 163 await makePutBodyRequest({ url: servers[0].url, path, token: 'hello', fields, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
172 }) 164 })
173 165
174 it('Should fail with a non admin user', async function () { 166 it('Should fail with a non admin user', async function () {
175 const path = basePath + servers[0].video + '/blacklist' 167 const path = basePath + servers[0].store.video + '/blacklist'
176 const fields = {} 168 const fields = {}
177 await makePutBodyRequest({ 169 await makePutBodyRequest({
178 url: servers[0].url, 170 url: servers[0].url,
179 path, 171 path,
180 token: userAccessToken2, 172 token: userAccessToken2,
181 fields, 173 fields,
182 statusCodeExpected: HttpStatusCode.FORBIDDEN_403 174 expectedStatus: HttpStatusCode.FORBIDDEN_403
183 }) 175 })
184 }) 176 })
185 177
186 it('Should fail with an invalid reason', async function () { 178 it('Should fail with an invalid reason', async function () {
187 const path = basePath + servers[0].video.uuid + '/blacklist' 179 const path = basePath + servers[0].store.video.uuid + '/blacklist'
188 const fields = { reason: 'a'.repeat(305) } 180 const fields = { reason: 'a'.repeat(305) }
189 181
190 await makePutBodyRequest({ url: servers[0].url, path, token: servers[0].accessToken, fields }) 182 await makePutBodyRequest({ url: servers[0].url, path, token: servers[0].accessToken, fields })
191 }) 183 })
192 184
193 it('Should succeed with the correct params', async function () { 185 it('Should succeed with the correct params', async function () {
194 const path = basePath + servers[0].video.shortUUID + '/blacklist' 186 const path = basePath + servers[0].store.video.shortUUID + '/blacklist'
195 const fields = { reason: 'hello' } 187 const fields = { reason: 'hello' }
196 188
197 await makePutBodyRequest({ 189 await makePutBodyRequest({
@@ -199,7 +191,7 @@ describe('Test video blacklist API validators', function () {
199 path, 191 path,
200 token: servers[0].accessToken, 192 token: servers[0].accessToken,
201 fields, 193 fields,
202 statusCodeExpected: HttpStatusCode.NO_CONTENT_204 194 expectedStatus: HttpStatusCode.NO_CONTENT_204
203 }) 195 })
204 }) 196 })
205 }) 197 })
@@ -207,52 +199,53 @@ describe('Test video blacklist API validators', function () {
207 describe('When getting blacklisted video', function () { 199 describe('When getting blacklisted video', function () {
208 200
209 it('Should fail with a non authenticated user', async function () { 201 it('Should fail with a non authenticated user', async function () {
210 await getVideo(servers[0].url, servers[0].video.uuid, HttpStatusCode.UNAUTHORIZED_401) 202 await servers[0].videos.get({ id: servers[0].store.video.uuid, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
211 }) 203 })
212 204
213 it('Should fail with another user', async function () { 205 it('Should fail with another user', async function () {
214 await getVideoWithToken(servers[0].url, userAccessToken2, servers[0].video.uuid, HttpStatusCode.FORBIDDEN_403) 206 await servers[0].videos.getWithToken({
207 token: userAccessToken2,
208 id: servers[0].store.video.uuid,
209 expectedStatus: HttpStatusCode.FORBIDDEN_403
210 })
215 }) 211 })
216 212
217 it('Should succeed with the owner authenticated user', async function () { 213 it('Should succeed with the owner authenticated user', async function () {
218 const res = await getVideoWithToken(servers[0].url, userAccessToken1, servers[0].video.uuid, HttpStatusCode.OK_200) 214 const video = await servers[0].videos.getWithToken({ token: userAccessToken1, id: servers[0].store.video.uuid })
219 const video: VideoDetails = res.body
220
221 expect(video.blacklisted).to.be.true 215 expect(video.blacklisted).to.be.true
222 }) 216 })
223 217
224 it('Should succeed with an admin', async function () { 218 it('Should succeed with an admin', async function () {
225 const video = servers[0].video 219 const video = servers[0].store.video
226 220
227 for (const id of [ video.id, video.uuid, video.shortUUID ]) { 221 for (const id of [ video.id, video.uuid, video.shortUUID ]) {
228 const res = await getVideoWithToken(servers[0].url, servers[0].accessToken, id, HttpStatusCode.OK_200) 222 const video = await servers[0].videos.getWithToken({ id, expectedStatus: HttpStatusCode.OK_200 })
229 const video: VideoDetails = res.body
230
231 expect(video.blacklisted).to.be.true 223 expect(video.blacklisted).to.be.true
232 } 224 }
233 }) 225 })
234 }) 226 })
235 227
236 describe('When removing a video in blacklist', function () { 228 describe('When removing a video in blacklist', function () {
229
237 it('Should fail with a non authenticated user', async function () { 230 it('Should fail with a non authenticated user', async function () {
238 await removeVideoFromBlacklist(servers[0].url, 'fake token', servers[0].video.uuid, HttpStatusCode.UNAUTHORIZED_401) 231 await command.remove({ token: 'fake token', videoId: servers[0].store.video.uuid, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
239 }) 232 })
240 233
241 it('Should fail with a non admin user', async function () { 234 it('Should fail with a non admin user', async function () {
242 await removeVideoFromBlacklist(servers[0].url, userAccessToken2, servers[0].video.uuid, HttpStatusCode.FORBIDDEN_403) 235 await command.remove({ token: userAccessToken2, videoId: servers[0].store.video.uuid, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
243 }) 236 })
244 237
245 it('Should fail with an incorrect id', async function () { 238 it('Should fail with an incorrect id', async function () {
246 await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, 'hello', HttpStatusCode.BAD_REQUEST_400) 239 await command.remove({ videoId: 'hello', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
247 }) 240 })
248 241
249 it('Should fail with a not blacklisted video', async function () { 242 it('Should fail with a not blacklisted video', async function () {
250 // The video was not added to the blacklist so it should fail 243 // The video was not added to the blacklist so it should fail
251 await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, notBlacklistedVideoId, HttpStatusCode.NOT_FOUND_404) 244 await command.remove({ videoId: notBlacklistedVideoId, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
252 }) 245 })
253 246
254 it('Should succeed with the correct params', async function () { 247 it('Should succeed with the correct params', async function () {
255 await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, servers[0].video.uuid, HttpStatusCode.NO_CONTENT_204) 248 await command.remove({ videoId: servers[0].store.video.uuid, expectedStatus: HttpStatusCode.NO_CONTENT_204 })
256 }) 249 })
257 }) 250 })
258 251
@@ -260,11 +253,11 @@ describe('Test video blacklist API validators', function () {
260 const basePath = '/api/v1/videos/blacklist/' 253 const basePath = '/api/v1/videos/blacklist/'
261 254
262 it('Should fail with a non authenticated user', async function () { 255 it('Should fail with a non authenticated user', async function () {
263 await getBlacklistedVideosList({ url: servers[0].url, token: 'fake token', specialStatus: HttpStatusCode.UNAUTHORIZED_401 }) 256 await servers[0].blacklist.list({ token: 'fake token', expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
264 }) 257 })
265 258
266 it('Should fail with a non admin user', async function () { 259 it('Should fail with a non admin user', async function () {
267 await getBlacklistedVideosList({ url: servers[0].url, token: userAccessToken2, specialStatus: HttpStatusCode.FORBIDDEN_403 }) 260 await servers[0].blacklist.list({ token: userAccessToken2, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
268 }) 261 })
269 262
270 it('Should fail with a bad start pagination', async function () { 263 it('Should fail with a bad start pagination', async function () {
@@ -280,16 +273,11 @@ describe('Test video blacklist API validators', function () {
280 }) 273 })
281 274
282 it('Should fail with an invalid type', async function () { 275 it('Should fail with an invalid type', async function () {
283 await getBlacklistedVideosList({ 276 await servers[0].blacklist.list({ type: 0, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
284 url: servers[0].url,
285 token: servers[0].accessToken,
286 type: 0,
287 specialStatus: HttpStatusCode.BAD_REQUEST_400
288 })
289 }) 277 })
290 278
291 it('Should succeed with the correct parameters', async function () { 279 it('Should succeed with the correct parameters', async function () {
292 await getBlacklistedVideosList({ url: servers[0].url, token: servers[0].accessToken, type: VideoBlacklistType.MANUAL }) 280 await servers[0].blacklist.list({ type: VideoBlacklistType.MANUAL })
293 }) 281 })
294 }) 282 })
295 283
diff --git a/server/tests/api/check-params/video-captions.ts b/server/tests/api/check-params/video-captions.ts
index c0595c04d..90f429314 100644
--- a/server/tests/api/check-params/video-captions.ts
+++ b/server/tests/api/check-params/video-captions.ts
@@ -1,27 +1,22 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2 2
3import 'mocha' 3import 'mocha'
4import { VideoCreateResult } from '@shared/models'
5import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
6import { 4import {
7 buildAbsoluteFixturePath, 5 buildAbsoluteFixturePath,
8 cleanupTests, 6 cleanupTests,
9 createUser, 7 createSingleServer,
10 flushAndRunServer,
11 makeDeleteRequest, 8 makeDeleteRequest,
12 makeGetRequest, 9 makeGetRequest,
13 makeUploadRequest, 10 makeUploadRequest,
14 ServerInfo, 11 PeerTubeServer,
15 setAccessTokensToServers, 12 setAccessTokensToServers
16 uploadVideo, 13} from '@shared/extra-utils'
17 userLogin 14import { HttpStatusCode, VideoCreateResult } from '@shared/models'
18} from '../../../../shared/extra-utils'
19import { createVideoCaption } from '../../../../shared/extra-utils/videos/video-captions'
20 15
21describe('Test video captions API validator', function () { 16describe('Test video captions API validator', function () {
22 const path = '/api/v1/videos/' 17 const path = '/api/v1/videos/'
23 18
24 let server: ServerInfo 19 let server: PeerTubeServer
25 let userAccessToken: string 20 let userAccessToken: string
26 let video: VideoCreateResult 21 let video: VideoCreateResult
27 22
@@ -30,22 +25,19 @@ describe('Test video captions API validator', function () {
30 before(async function () { 25 before(async function () {
31 this.timeout(30000) 26 this.timeout(30000)
32 27
33 server = await flushAndRunServer(1) 28 server = await createSingleServer(1)
34 29
35 await setAccessTokensToServers([ server ]) 30 await setAccessTokensToServers([ server ])
36 31
37 { 32 video = await server.videos.upload()
38 const res = await uploadVideo(server.url, server.accessToken, {})
39 video = res.body.video
40 }
41 33
42 { 34 {
43 const user = { 35 const user = {
44 username: 'user1', 36 username: 'user1',
45 password: 'my super password' 37 password: 'my super password'
46 } 38 }
47 await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password }) 39 await server.users.create({ username: user.username, password: user.password })
48 userAccessToken = await userLogin(server, user) 40 userAccessToken = await server.login.getAccessToken(user)
49 } 41 }
50 }) 42 })
51 43
@@ -74,7 +66,7 @@ describe('Test video captions API validator', function () {
74 token: server.accessToken, 66 token: server.accessToken,
75 fields, 67 fields,
76 attaches, 68 attaches,
77 statusCodeExpected: 404 69 expectedStatus: 404
78 }) 70 })
79 }) 71 })
80 72
@@ -110,7 +102,7 @@ describe('Test video captions API validator', function () {
110 path: captionPath, 102 path: captionPath,
111 fields, 103 fields,
112 attaches, 104 attaches,
113 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 105 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
114 }) 106 })
115 }) 107 })
116 108
@@ -123,7 +115,7 @@ describe('Test video captions API validator', function () {
123 token: 'blabla', 115 token: 'blabla',
124 fields, 116 fields,
125 attaches, 117 attaches,
126 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 118 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
127 }) 119 })
128 }) 120 })
129 121
@@ -141,7 +133,7 @@ describe('Test video captions API validator', function () {
141 // token: server.accessToken, 133 // token: server.accessToken,
142 // fields, 134 // fields,
143 // attaches, 135 // attaches,
144 // statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 136 // expectedStatus: HttpStatusCode.BAD_REQUEST_400
145 // }) 137 // })
146 // }) 138 // })
147 139
@@ -154,14 +146,12 @@ describe('Test video captions API validator', function () {
154 // videoId: video.uuid, 146 // videoId: video.uuid,
155 // fixture: 'subtitle-bad.txt', 147 // fixture: 'subtitle-bad.txt',
156 // mimeType: 'application/octet-stream', 148 // mimeType: 'application/octet-stream',
157 // statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 149 // expectedStatus: HttpStatusCode.BAD_REQUEST_400
158 // }) 150 // })
159 // }) 151 // })
160 152
161 it('Should succeed with a valid captionfile extension and octet-stream mime type', async function () { 153 it('Should succeed with a valid captionfile extension and octet-stream mime type', async function () {
162 await createVideoCaption({ 154 await server.captions.add({
163 url: server.url,
164 accessToken: server.accessToken,
165 language: 'zh', 155 language: 'zh',
166 videoId: video.uuid, 156 videoId: video.uuid,
167 fixture: 'subtitle-good.srt', 157 fixture: 'subtitle-good.srt',
@@ -183,7 +173,7 @@ describe('Test video captions API validator', function () {
183 // token: server.accessToken, 173 // token: server.accessToken,
184 // fields, 174 // fields,
185 // attaches, 175 // attaches,
186 // statusCodeExpected: HttpStatusCode.INTERNAL_SERVER_ERROR_500 176 // expectedStatus: HttpStatusCode.INTERNAL_SERVER_ERROR_500
187 // }) 177 // })
188 // }) 178 // })
189 179
@@ -196,7 +186,7 @@ describe('Test video captions API validator', function () {
196 token: server.accessToken, 186 token: server.accessToken,
197 fields, 187 fields,
198 attaches, 188 attaches,
199 statusCodeExpected: HttpStatusCode.NO_CONTENT_204 189 expectedStatus: HttpStatusCode.NO_CONTENT_204
200 }) 190 })
201 }) 191 })
202 }) 192 })
@@ -210,12 +200,12 @@ describe('Test video captions API validator', function () {
210 await makeGetRequest({ 200 await makeGetRequest({
211 url: server.url, 201 url: server.url,
212 path: path + '4da6fde3-88f7-4d16-b119-108df5630b06/captions', 202 path: path + '4da6fde3-88f7-4d16-b119-108df5630b06/captions',
213 statusCodeExpected: HttpStatusCode.NOT_FOUND_404 203 expectedStatus: HttpStatusCode.NOT_FOUND_404
214 }) 204 })
215 }) 205 })
216 206
217 it('Should success with the correct parameters', async function () { 207 it('Should success with the correct parameters', async function () {
218 await makeGetRequest({ url: server.url, path: path + video.shortUUID + '/captions', statusCodeExpected: HttpStatusCode.OK_200 }) 208 await makeGetRequest({ url: server.url, path: path + video.shortUUID + '/captions', expectedStatus: HttpStatusCode.OK_200 })
219 }) 209 })
220 }) 210 })
221 211
@@ -233,7 +223,7 @@ describe('Test video captions API validator', function () {
233 url: server.url, 223 url: server.url,
234 path: path + '4da6fde3-88f7-4d16-b119-108df5630b06/captions/fr', 224 path: path + '4da6fde3-88f7-4d16-b119-108df5630b06/captions/fr',
235 token: server.accessToken, 225 token: server.accessToken,
236 statusCodeExpected: HttpStatusCode.NOT_FOUND_404 226 expectedStatus: HttpStatusCode.NOT_FOUND_404
237 }) 227 })
238 }) 228 })
239 229
@@ -257,12 +247,12 @@ describe('Test video captions API validator', function () {
257 247
258 it('Should fail without access token', async function () { 248 it('Should fail without access token', async function () {
259 const captionPath = path + video.shortUUID + '/captions/fr' 249 const captionPath = path + video.shortUUID + '/captions/fr'
260 await makeDeleteRequest({ url: server.url, path: captionPath, statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 }) 250 await makeDeleteRequest({ url: server.url, path: captionPath, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
261 }) 251 })
262 252
263 it('Should fail with a bad access token', async function () { 253 it('Should fail with a bad access token', async function () {
264 const captionPath = path + video.shortUUID + '/captions/fr' 254 const captionPath = path + video.shortUUID + '/captions/fr'
265 await makeDeleteRequest({ url: server.url, path: captionPath, token: 'coucou', statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 }) 255 await makeDeleteRequest({ url: server.url, path: captionPath, token: 'coucou', expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
266 }) 256 })
267 257
268 it('Should fail with another user', async function () { 258 it('Should fail with another user', async function () {
@@ -271,7 +261,7 @@ describe('Test video captions API validator', function () {
271 url: server.url, 261 url: server.url,
272 path: captionPath, 262 path: captionPath,
273 token: userAccessToken, 263 token: userAccessToken,
274 statusCodeExpected: HttpStatusCode.FORBIDDEN_403 264 expectedStatus: HttpStatusCode.FORBIDDEN_403
275 }) 265 })
276 }) 266 })
277 267
@@ -281,7 +271,7 @@ describe('Test video captions API validator', function () {
281 url: server.url, 271 url: server.url,
282 path: captionPath, 272 path: captionPath,
283 token: server.accessToken, 273 token: server.accessToken,
284 statusCodeExpected: HttpStatusCode.NO_CONTENT_204 274 expectedStatus: HttpStatusCode.NO_CONTENT_204
285 }) 275 })
286 }) 276 })
287 }) 277 })
diff --git a/server/tests/api/check-params/video-channels.ts b/server/tests/api/check-params/video-channels.ts
index 5c02afd31..2e63916d4 100644
--- a/server/tests/api/check-params/video-channels.ts
+++ b/server/tests/api/check-params/video-channels.ts
@@ -3,43 +3,37 @@
3import 'mocha' 3import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5import { omit } from 'lodash' 5import { omit } from 'lodash'
6import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
7import { 6import {
8 buildAbsoluteFixturePath, 7 buildAbsoluteFixturePath,
8 ChannelsCommand,
9 checkBadCountPagination,
10 checkBadSortPagination,
11 checkBadStartPagination,
9 cleanupTests, 12 cleanupTests,
10 createUser, 13 createSingleServer,
11 deleteVideoChannel,
12 flushAndRunServer,
13 getAccountVideoChannelsList,
14 immutableAssign,
15 makeGetRequest, 14 makeGetRequest,
16 makePostBodyRequest, 15 makePostBodyRequest,
17 makePutBodyRequest, 16 makePutBodyRequest,
18 makeUploadRequest, 17 makeUploadRequest,
19 ServerInfo, 18 PeerTubeServer,
20 setAccessTokensToServers, 19 setAccessTokensToServers
21 userLogin 20} from '@shared/extra-utils'
22} from '../../../../shared/extra-utils' 21import { HttpStatusCode, VideoChannelUpdate } from '@shared/models'
23import {
24 checkBadCountPagination,
25 checkBadSortPagination,
26 checkBadStartPagination
27} from '../../../../shared/extra-utils/requests/check-api-params'
28import { VideoChannelUpdate } from '../../../../shared/models/videos'
29 22
30const expect = chai.expect 23const expect = chai.expect
31 24
32describe('Test video channels API validator', function () { 25describe('Test video channels API validator', function () {
33 const videoChannelPath = '/api/v1/video-channels' 26 const videoChannelPath = '/api/v1/video-channels'
34 let server: ServerInfo 27 let server: PeerTubeServer
35 let accessTokenUser: string 28 let accessTokenUser: string
29 let command: ChannelsCommand
36 30
37 // --------------------------------------------------------------- 31 // ---------------------------------------------------------------
38 32
39 before(async function () { 33 before(async function () {
40 this.timeout(30000) 34 this.timeout(30000)
41 35
42 server = await flushAndRunServer(1) 36 server = await createSingleServer(1)
43 37
44 await setAccessTokensToServers([ server ]) 38 await setAccessTokensToServers([ server ])
45 39
@@ -49,9 +43,11 @@ describe('Test video channels API validator', function () {
49 } 43 }
50 44
51 { 45 {
52 await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password }) 46 await server.users.create({ username: user.username, password: user.password })
53 accessTokenUser = await userLogin(server, user) 47 accessTokenUser = await server.login.getAccessToken(user)
54 } 48 }
49
50 command = server.channels
55 }) 51 })
56 52
57 describe('When listing a video channels', function () { 53 describe('When listing a video channels', function () {
@@ -84,14 +80,14 @@ describe('Test video channels API validator', function () {
84 }) 80 })
85 81
86 it('Should fail with a unknown account', async function () { 82 it('Should fail with a unknown account', async function () {
87 await getAccountVideoChannelsList({ url: server.url, accountName: 'unknown', specialStatus: HttpStatusCode.NOT_FOUND_404 }) 83 await server.channels.listByAccount({ accountName: 'unknown', expectedStatus: HttpStatusCode.NOT_FOUND_404 })
88 }) 84 })
89 85
90 it('Should succeed with the correct parameters', async function () { 86 it('Should succeed with the correct parameters', async function () {
91 await makeGetRequest({ 87 await makeGetRequest({
92 url: server.url, 88 url: server.url,
93 path: accountChannelPath, 89 path: accountChannelPath,
94 statusCodeExpected: HttpStatusCode.OK_200 90 expectedStatus: HttpStatusCode.OK_200
95 }) 91 })
96 }) 92 })
97 }) 93 })
@@ -110,7 +106,7 @@ describe('Test video channels API validator', function () {
110 path: videoChannelPath, 106 path: videoChannelPath,
111 token: 'none', 107 token: 'none',
112 fields: baseCorrectParams, 108 fields: baseCorrectParams,
113 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 109 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
114 }) 110 })
115 }) 111 })
116 112
@@ -125,7 +121,7 @@ describe('Test video channels API validator', function () {
125 }) 121 })
126 122
127 it('Should fail with a bad name', async function () { 123 it('Should fail with a bad name', async function () {
128 const fields = immutableAssign(baseCorrectParams, { name: 'super name' }) 124 const fields = { ...baseCorrectParams, name: 'super name' }
129 await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields }) 125 await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
130 }) 126 })
131 127
@@ -135,17 +131,17 @@ describe('Test video channels API validator', function () {
135 }) 131 })
136 132
137 it('Should fail with a long name', async function () { 133 it('Should fail with a long name', async function () {
138 const fields = immutableAssign(baseCorrectParams, { displayName: 'super'.repeat(25) }) 134 const fields = { ...baseCorrectParams, displayName: 'super'.repeat(25) }
139 await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields }) 135 await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
140 }) 136 })
141 137
142 it('Should fail with a long description', async function () { 138 it('Should fail with a long description', async function () {
143 const fields = immutableAssign(baseCorrectParams, { description: 'super'.repeat(201) }) 139 const fields = { ...baseCorrectParams, description: 'super'.repeat(201) }
144 await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields }) 140 await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
145 }) 141 })
146 142
147 it('Should fail with a long support text', async function () { 143 it('Should fail with a long support text', async function () {
148 const fields = immutableAssign(baseCorrectParams, { support: 'super'.repeat(201) }) 144 const fields = { ...baseCorrectParams, support: 'super'.repeat(201) }
149 await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields }) 145 await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
150 }) 146 })
151 147
@@ -155,7 +151,7 @@ describe('Test video channels API validator', function () {
155 path: videoChannelPath, 151 path: videoChannelPath,
156 token: server.accessToken, 152 token: server.accessToken,
157 fields: baseCorrectParams, 153 fields: baseCorrectParams,
158 statusCodeExpected: HttpStatusCode.OK_200 154 expectedStatus: HttpStatusCode.OK_200
159 }) 155 })
160 }) 156 })
161 157
@@ -165,7 +161,7 @@ describe('Test video channels API validator', function () {
165 path: videoChannelPath, 161 path: videoChannelPath,
166 token: server.accessToken, 162 token: server.accessToken,
167 fields: baseCorrectParams, 163 fields: baseCorrectParams,
168 statusCodeExpected: HttpStatusCode.CONFLICT_409 164 expectedStatus: HttpStatusCode.CONFLICT_409
169 }) 165 })
170 }) 166 })
171 }) 167 })
@@ -189,7 +185,7 @@ describe('Test video channels API validator', function () {
189 path, 185 path,
190 token: 'hi', 186 token: 'hi',
191 fields: baseCorrectParams, 187 fields: baseCorrectParams,
192 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 188 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
193 }) 189 })
194 }) 190 })
195 191
@@ -199,27 +195,27 @@ describe('Test video channels API validator', function () {
199 path, 195 path,
200 token: accessTokenUser, 196 token: accessTokenUser,
201 fields: baseCorrectParams, 197 fields: baseCorrectParams,
202 statusCodeExpected: HttpStatusCode.FORBIDDEN_403 198 expectedStatus: HttpStatusCode.FORBIDDEN_403
203 }) 199 })
204 }) 200 })
205 201
206 it('Should fail with a long name', async function () { 202 it('Should fail with a long name', async function () {
207 const fields = immutableAssign(baseCorrectParams, { displayName: 'super'.repeat(25) }) 203 const fields = { ...baseCorrectParams, displayName: 'super'.repeat(25) }
208 await makePutBodyRequest({ url: server.url, path, token: server.accessToken, fields }) 204 await makePutBodyRequest({ url: server.url, path, token: server.accessToken, fields })
209 }) 205 })
210 206
211 it('Should fail with a long description', async function () { 207 it('Should fail with a long description', async function () {
212 const fields = immutableAssign(baseCorrectParams, { description: 'super'.repeat(201) }) 208 const fields = { ...baseCorrectParams, description: 'super'.repeat(201) }
213 await makePutBodyRequest({ url: server.url, path, token: server.accessToken, fields }) 209 await makePutBodyRequest({ url: server.url, path, token: server.accessToken, fields })
214 }) 210 })
215 211
216 it('Should fail with a long support text', async function () { 212 it('Should fail with a long support text', async function () {
217 const fields = immutableAssign(baseCorrectParams, { support: 'super'.repeat(201) }) 213 const fields = { ...baseCorrectParams, support: 'super'.repeat(201) }
218 await makePutBodyRequest({ url: server.url, path, token: server.accessToken, fields }) 214 await makePutBodyRequest({ url: server.url, path, token: server.accessToken, fields })
219 }) 215 })
220 216
221 it('Should fail with a bad bulkVideosSupportUpdate field', async function () { 217 it('Should fail with a bad bulkVideosSupportUpdate field', async function () {
222 const fields = immutableAssign(baseCorrectParams, { bulkVideosSupportUpdate: 'super' }) 218 const fields = { ...baseCorrectParams, bulkVideosSupportUpdate: 'super' }
223 await makePutBodyRequest({ url: server.url, path, token: server.accessToken, fields }) 219 await makePutBodyRequest({ url: server.url, path, token: server.accessToken, fields })
224 }) 220 })
225 221
@@ -229,7 +225,7 @@ describe('Test video channels API validator', function () {
229 path, 225 path,
230 token: server.accessToken, 226 token: server.accessToken,
231 fields: baseCorrectParams, 227 fields: baseCorrectParams,
232 statusCodeExpected: HttpStatusCode.NO_CONTENT_204 228 expectedStatus: HttpStatusCode.NO_CONTENT_204
233 }) 229 })
234 }) 230 })
235 }) 231 })
@@ -274,7 +270,7 @@ describe('Test video channels API validator', function () {
274 path: `${path}/${type}/pick`, 270 path: `${path}/${type}/pick`,
275 fields, 271 fields,
276 attaches, 272 attaches,
277 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 273 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
278 }) 274 })
279 } 275 }
280 }) 276 })
@@ -291,7 +287,7 @@ describe('Test video channels API validator', function () {
291 token: server.accessToken, 287 token: server.accessToken,
292 fields, 288 fields,
293 attaches, 289 attaches,
294 statusCodeExpected: HttpStatusCode.OK_200 290 expectedStatus: HttpStatusCode.OK_200
295 }) 291 })
296 } 292 }
297 }) 293 })
@@ -302,7 +298,7 @@ describe('Test video channels API validator', function () {
302 const res = await makeGetRequest({ 298 const res = await makeGetRequest({
303 url: server.url, 299 url: server.url,
304 path: videoChannelPath, 300 path: videoChannelPath,
305 statusCodeExpected: HttpStatusCode.OK_200 301 expectedStatus: HttpStatusCode.OK_200
306 }) 302 })
307 303
308 expect(res.body.data).to.be.an('array') 304 expect(res.body.data).to.be.an('array')
@@ -312,7 +308,7 @@ describe('Test video channels API validator', function () {
312 await makeGetRequest({ 308 await makeGetRequest({
313 url: server.url, 309 url: server.url,
314 path: videoChannelPath + '/super_channel2', 310 path: videoChannelPath + '/super_channel2',
315 statusCodeExpected: HttpStatusCode.NOT_FOUND_404 311 expectedStatus: HttpStatusCode.NOT_FOUND_404
316 }) 312 })
317 }) 313 })
318 314
@@ -320,30 +316,30 @@ describe('Test video channels API validator', function () {
320 await makeGetRequest({ 316 await makeGetRequest({
321 url: server.url, 317 url: server.url,
322 path: videoChannelPath + '/super_channel', 318 path: videoChannelPath + '/super_channel',
323 statusCodeExpected: HttpStatusCode.OK_200 319 expectedStatus: HttpStatusCode.OK_200
324 }) 320 })
325 }) 321 })
326 }) 322 })
327 323
328 describe('When deleting a video channel', function () { 324 describe('When deleting a video channel', function () {
329 it('Should fail with a non authenticated user', async function () { 325 it('Should fail with a non authenticated user', async function () {
330 await deleteVideoChannel(server.url, 'coucou', 'super_channel', HttpStatusCode.UNAUTHORIZED_401) 326 await command.delete({ token: 'coucou', channelName: 'super_channel', expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
331 }) 327 })
332 328
333 it('Should fail with another authenticated user', async function () { 329 it('Should fail with another authenticated user', async function () {
334 await deleteVideoChannel(server.url, accessTokenUser, 'super_channel', HttpStatusCode.FORBIDDEN_403) 330 await command.delete({ token: accessTokenUser, channelName: 'super_channel', expectedStatus: HttpStatusCode.FORBIDDEN_403 })
335 }) 331 })
336 332
337 it('Should fail with an unknown video channel id', async function () { 333 it('Should fail with an unknown video channel id', async function () {
338 await deleteVideoChannel(server.url, server.accessToken, 'super_channel2', HttpStatusCode.NOT_FOUND_404) 334 await command.delete({ channelName: 'super_channel2', expectedStatus: HttpStatusCode.NOT_FOUND_404 })
339 }) 335 })
340 336
341 it('Should succeed with the correct parameters', async function () { 337 it('Should succeed with the correct parameters', async function () {
342 await deleteVideoChannel(server.url, server.accessToken, 'super_channel') 338 await command.delete({ channelName: 'super_channel' })
343 }) 339 })
344 340
345 it('Should fail to delete the last user video channel', async function () { 341 it('Should fail to delete the last user video channel', async function () {
346 await deleteVideoChannel(server.url, server.accessToken, 'root_channel', HttpStatusCode.CONFLICT_409) 342 await command.delete({ channelName: 'root_channel', expectedStatus: HttpStatusCode.CONFLICT_409 })
347 }) 343 })
348 }) 344 })
349 345
diff --git a/server/tests/api/check-params/video-comments.ts b/server/tests/api/check-params/video-comments.ts
index a38420851..2d9ee1e0d 100644
--- a/server/tests/api/check-params/video-comments.ts
+++ b/server/tests/api/check-params/video-comments.ts
@@ -2,33 +2,26 @@
2 2
3import 'mocha' 3import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5import { VideoCreateResult } from '@shared/models'
6import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
7import { 5import {
6 checkBadCountPagination,
7 checkBadSortPagination,
8 checkBadStartPagination,
8 cleanupTests, 9 cleanupTests,
9 createUser, 10 createSingleServer,
10 flushAndRunServer,
11 makeDeleteRequest, 11 makeDeleteRequest,
12 makeGetRequest, 12 makeGetRequest,
13 makePostBodyRequest, 13 makePostBodyRequest,
14 ServerInfo, 14 PeerTubeServer,
15 setAccessTokensToServers, 15 setAccessTokensToServers
16 uploadVideo, 16} from '@shared/extra-utils'
17 userLogin 17import { HttpStatusCode, VideoCreateResult } from '@shared/models'
18} from '../../../../shared/extra-utils'
19import {
20 checkBadCountPagination,
21 checkBadSortPagination,
22 checkBadStartPagination
23} from '../../../../shared/extra-utils/requests/check-api-params'
24import { addVideoCommentThread } from '../../../../shared/extra-utils/videos/video-comments'
25 18
26const expect = chai.expect 19const expect = chai.expect
27 20
28describe('Test video comments API validator', function () { 21describe('Test video comments API validator', function () {
29 let pathThread: string 22 let pathThread: string
30 let pathComment: string 23 let pathComment: string
31 let server: ServerInfo 24 let server: PeerTubeServer
32 let video: VideoCreateResult 25 let video: VideoCreateResult
33 let userAccessToken: string 26 let userAccessToken: string
34 let userAccessToken2: string 27 let userAccessToken2: string
@@ -39,32 +32,31 @@ describe('Test video comments API validator', function () {
39 before(async function () { 32 before(async function () {
40 this.timeout(30000) 33 this.timeout(30000)
41 34
42 server = await flushAndRunServer(1) 35 server = await createSingleServer(1)
43 36
44 await setAccessTokensToServers([ server ]) 37 await setAccessTokensToServers([ server ])
45 38
46 { 39 {
47 const res = await uploadVideo(server.url, server.accessToken, {}) 40 video = await server.videos.upload({ attributes: {} })
48 video = res.body.video
49 pathThread = '/api/v1/videos/' + video.uuid + '/comment-threads' 41 pathThread = '/api/v1/videos/' + video.uuid + '/comment-threads'
50 } 42 }
51 43
52 { 44 {
53 const res = await addVideoCommentThread(server.url, server.accessToken, video.uuid, 'coucou') 45 const created = await server.comments.createThread({ videoId: video.uuid, text: 'coucou' })
54 commentId = res.body.comment.id 46 commentId = created.id
55 pathComment = '/api/v1/videos/' + video.uuid + '/comments/' + commentId 47 pathComment = '/api/v1/videos/' + video.uuid + '/comments/' + commentId
56 } 48 }
57 49
58 { 50 {
59 const user = { username: 'user1', password: 'my super password' } 51 const user = { username: 'user1', password: 'my super password' }
60 await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password }) 52 await server.users.create({ username: user.username, password: user.password })
61 userAccessToken = await userLogin(server, user) 53 userAccessToken = await server.login.getAccessToken(user)
62 } 54 }
63 55
64 { 56 {
65 const user = { username: 'user2', password: 'my super password' } 57 const user = { username: 'user2', password: 'my super password' }
66 await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password }) 58 await server.users.create({ username: user.username, password: user.password })
67 userAccessToken2 = await userLogin(server, user) 59 userAccessToken2 = await server.login.getAccessToken(user)
68 } 60 }
69 }) 61 })
70 62
@@ -85,7 +77,7 @@ describe('Test video comments API validator', function () {
85 await makeGetRequest({ 77 await makeGetRequest({
86 url: server.url, 78 url: server.url,
87 path: '/api/v1/videos/ba708d62-e3d7-45d9-9d73-41b9097cc02d/comment-threads', 79 path: '/api/v1/videos/ba708d62-e3d7-45d9-9d73-41b9097cc02d/comment-threads',
88 statusCodeExpected: HttpStatusCode.NOT_FOUND_404 80 expectedStatus: HttpStatusCode.NOT_FOUND_404
89 }) 81 })
90 }) 82 })
91 }) 83 })
@@ -95,7 +87,7 @@ describe('Test video comments API validator', function () {
95 await makeGetRequest({ 87 await makeGetRequest({
96 url: server.url, 88 url: server.url,
97 path: '/api/v1/videos/ba708d62-e3d7-45d9-9d73-41b9097cc02d/comment-threads/' + commentId, 89 path: '/api/v1/videos/ba708d62-e3d7-45d9-9d73-41b9097cc02d/comment-threads/' + commentId,
98 statusCodeExpected: HttpStatusCode.NOT_FOUND_404 90 expectedStatus: HttpStatusCode.NOT_FOUND_404
99 }) 91 })
100 }) 92 })
101 93
@@ -103,7 +95,7 @@ describe('Test video comments API validator', function () {
103 await makeGetRequest({ 95 await makeGetRequest({
104 url: server.url, 96 url: server.url,
105 path: '/api/v1/videos/' + video.shortUUID + '/comment-threads/156', 97 path: '/api/v1/videos/' + video.shortUUID + '/comment-threads/156',
106 statusCodeExpected: HttpStatusCode.NOT_FOUND_404 98 expectedStatus: HttpStatusCode.NOT_FOUND_404
107 }) 99 })
108 }) 100 })
109 101
@@ -111,7 +103,7 @@ describe('Test video comments API validator', function () {
111 await makeGetRequest({ 103 await makeGetRequest({
112 url: server.url, 104 url: server.url,
113 path: '/api/v1/videos/' + video.shortUUID + '/comment-threads/' + commentId, 105 path: '/api/v1/videos/' + video.shortUUID + '/comment-threads/' + commentId,
114 statusCodeExpected: HttpStatusCode.OK_200 106 expectedStatus: HttpStatusCode.OK_200
115 }) 107 })
116 }) 108 })
117 }) 109 })
@@ -127,7 +119,7 @@ describe('Test video comments API validator', function () {
127 path: pathThread, 119 path: pathThread,
128 token: 'none', 120 token: 'none',
129 fields, 121 fields,
130 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 122 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
131 }) 123 })
132 }) 124 })
133 125
@@ -160,7 +152,7 @@ describe('Test video comments API validator', function () {
160 path, 152 path,
161 token: server.accessToken, 153 token: server.accessToken,
162 fields, 154 fields,
163 statusCodeExpected: HttpStatusCode.NOT_FOUND_404 155 expectedStatus: HttpStatusCode.NOT_FOUND_404
164 }) 156 })
165 }) 157 })
166 158
@@ -173,7 +165,7 @@ describe('Test video comments API validator', function () {
173 path: pathThread, 165 path: pathThread,
174 token: server.accessToken, 166 token: server.accessToken,
175 fields, 167 fields,
176 statusCodeExpected: HttpStatusCode.OK_200 168 expectedStatus: HttpStatusCode.OK_200
177 }) 169 })
178 }) 170 })
179 }) 171 })
@@ -188,7 +180,7 @@ describe('Test video comments API validator', function () {
188 path: pathComment, 180 path: pathComment,
189 token: 'none', 181 token: 'none',
190 fields, 182 fields,
191 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 183 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
192 }) 184 })
193 }) 185 })
194 186
@@ -221,7 +213,7 @@ describe('Test video comments API validator', function () {
221 path, 213 path,
222 token: server.accessToken, 214 token: server.accessToken,
223 fields, 215 fields,
224 statusCodeExpected: HttpStatusCode.NOT_FOUND_404 216 expectedStatus: HttpStatusCode.NOT_FOUND_404
225 }) 217 })
226 }) 218 })
227 219
@@ -235,7 +227,7 @@ describe('Test video comments API validator', function () {
235 path, 227 path,
236 token: server.accessToken, 228 token: server.accessToken,
237 fields, 229 fields,
238 statusCodeExpected: HttpStatusCode.NOT_FOUND_404 230 expectedStatus: HttpStatusCode.NOT_FOUND_404
239 }) 231 })
240 }) 232 })
241 233
@@ -248,14 +240,14 @@ describe('Test video comments API validator', function () {
248 path: pathComment, 240 path: pathComment,
249 token: server.accessToken, 241 token: server.accessToken,
250 fields, 242 fields,
251 statusCodeExpected: HttpStatusCode.OK_200 243 expectedStatus: HttpStatusCode.OK_200
252 }) 244 })
253 }) 245 })
254 }) 246 })
255 247
256 describe('When removing video comments', function () { 248 describe('When removing video comments', function () {
257 it('Should fail with a non authenticated user', async function () { 249 it('Should fail with a non authenticated user', async function () {
258 await makeDeleteRequest({ url: server.url, path: pathComment, token: 'none', statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 }) 250 await makeDeleteRequest({ url: server.url, path: pathComment, token: 'none', expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
259 }) 251 })
260 252
261 it('Should fail with another user', async function () { 253 it('Should fail with another user', async function () {
@@ -263,32 +255,32 @@ describe('Test video comments API validator', function () {
263 url: server.url, 255 url: server.url,
264 path: pathComment, 256 path: pathComment,
265 token: userAccessToken, 257 token: userAccessToken,
266 statusCodeExpected: HttpStatusCode.FORBIDDEN_403 258 expectedStatus: HttpStatusCode.FORBIDDEN_403
267 }) 259 })
268 }) 260 })
269 261
270 it('Should fail with an incorrect video', async function () { 262 it('Should fail with an incorrect video', async function () {
271 const path = '/api/v1/videos/ba708d62-e3d7-45d9-9d73-41b9097cc02d/comments/' + commentId 263 const path = '/api/v1/videos/ba708d62-e3d7-45d9-9d73-41b9097cc02d/comments/' + commentId
272 await makeDeleteRequest({ url: server.url, path, token: server.accessToken, statusCodeExpected: HttpStatusCode.NOT_FOUND_404 }) 264 await makeDeleteRequest({ url: server.url, path, token: server.accessToken, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
273 }) 265 })
274 266
275 it('Should fail with an incorrect comment', async function () { 267 it('Should fail with an incorrect comment', async function () {
276 const path = '/api/v1/videos/' + video.uuid + '/comments/124' 268 const path = '/api/v1/videos/' + video.uuid + '/comments/124'
277 await makeDeleteRequest({ url: server.url, path, token: server.accessToken, statusCodeExpected: HttpStatusCode.NOT_FOUND_404 }) 269 await makeDeleteRequest({ url: server.url, path, token: server.accessToken, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
278 }) 270 })
279 271
280 it('Should succeed with the same user', async function () { 272 it('Should succeed with the same user', async function () {
281 let commentToDelete: number 273 let commentToDelete: number
282 274
283 { 275 {
284 const res = await addVideoCommentThread(server.url, userAccessToken, video.uuid, 'hello') 276 const created = await server.comments.createThread({ videoId: video.uuid, token: userAccessToken, text: 'hello' })
285 commentToDelete = res.body.comment.id 277 commentToDelete = created.id
286 } 278 }
287 279
288 const path = '/api/v1/videos/' + video.uuid + '/comments/' + commentToDelete 280 const path = '/api/v1/videos/' + video.uuid + '/comments/' + commentToDelete
289 281
290 await makeDeleteRequest({ url: server.url, path, token: userAccessToken2, statusCodeExpected: HttpStatusCode.FORBIDDEN_403 }) 282 await makeDeleteRequest({ url: server.url, path, token: userAccessToken2, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
291 await makeDeleteRequest({ url: server.url, path, token: userAccessToken, statusCodeExpected: HttpStatusCode.NO_CONTENT_204 }) 283 await makeDeleteRequest({ url: server.url, path, token: userAccessToken, expectedStatus: HttpStatusCode.NO_CONTENT_204 })
292 }) 284 })
293 285
294 it('Should succeed with the owner of the video', async function () { 286 it('Should succeed with the owner of the video', async function () {
@@ -296,19 +288,19 @@ describe('Test video comments API validator', function () {
296 let anotherVideoUUID: string 288 let anotherVideoUUID: string
297 289
298 { 290 {
299 const res = await uploadVideo(server.url, userAccessToken, { name: 'video' }) 291 const { uuid } = await server.videos.upload({ token: userAccessToken, attributes: { name: 'video' } })
300 anotherVideoUUID = res.body.video.uuid 292 anotherVideoUUID = uuid
301 } 293 }
302 294
303 { 295 {
304 const res = await addVideoCommentThread(server.url, server.accessToken, anotherVideoUUID, 'hello') 296 const created = await server.comments.createThread({ videoId: anotherVideoUUID, text: 'hello' })
305 commentToDelete = res.body.comment.id 297 commentToDelete = created.id
306 } 298 }
307 299
308 const path = '/api/v1/videos/' + anotherVideoUUID + '/comments/' + commentToDelete 300 const path = '/api/v1/videos/' + anotherVideoUUID + '/comments/' + commentToDelete
309 301
310 await makeDeleteRequest({ url: server.url, path, token: userAccessToken2, statusCodeExpected: HttpStatusCode.FORBIDDEN_403 }) 302 await makeDeleteRequest({ url: server.url, path, token: userAccessToken2, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
311 await makeDeleteRequest({ url: server.url, path, token: userAccessToken, statusCodeExpected: HttpStatusCode.NO_CONTENT_204 }) 303 await makeDeleteRequest({ url: server.url, path, token: userAccessToken, expectedStatus: HttpStatusCode.NO_CONTENT_204 })
312 }) 304 })
313 305
314 it('Should succeed with the correct parameters', async function () { 306 it('Should succeed with the correct parameters', async function () {
@@ -316,15 +308,14 @@ describe('Test video comments API validator', function () {
316 url: server.url, 308 url: server.url,
317 path: pathComment, 309 path: pathComment,
318 token: server.accessToken, 310 token: server.accessToken,
319 statusCodeExpected: HttpStatusCode.NO_CONTENT_204 311 expectedStatus: HttpStatusCode.NO_CONTENT_204
320 }) 312 })
321 }) 313 })
322 }) 314 })
323 315
324 describe('When a video has comments disabled', function () { 316 describe('When a video has comments disabled', function () {
325 before(async function () { 317 before(async function () {
326 const res = await uploadVideo(server.url, server.accessToken, { commentsEnabled: false }) 318 video = await server.videos.upload({ attributes: { commentsEnabled: false } })
327 video = res.body.video
328 pathThread = '/api/v1/videos/' + video.uuid + '/comment-threads' 319 pathThread = '/api/v1/videos/' + video.uuid + '/comment-threads'
329 }) 320 })
330 321
@@ -332,7 +323,7 @@ describe('Test video comments API validator', function () {
332 const res = await makeGetRequest({ 323 const res = await makeGetRequest({
333 url: server.url, 324 url: server.url,
334 path: pathThread, 325 path: pathThread,
335 statusCodeExpected: HttpStatusCode.OK_200 326 expectedStatus: HttpStatusCode.OK_200
336 }) 327 })
337 expect(res.body.total).to.equal(0) 328 expect(res.body.total).to.equal(0)
338 expect(res.body.data).to.have.lengthOf(0) 329 expect(res.body.data).to.have.lengthOf(0)
@@ -349,7 +340,7 @@ describe('Test video comments API validator', function () {
349 path: pathThread, 340 path: pathThread,
350 token: server.accessToken, 341 token: server.accessToken,
351 fields, 342 fields,
352 statusCodeExpected: HttpStatusCode.CONFLICT_409 343 expectedStatus: HttpStatusCode.CONFLICT_409
353 }) 344 })
354 }) 345 })
355 346
@@ -375,7 +366,7 @@ describe('Test video comments API validator', function () {
375 await makeGetRequest({ 366 await makeGetRequest({
376 url: server.url, 367 url: server.url,
377 path, 368 path,
378 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 369 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
379 }) 370 })
380 }) 371 })
381 372
@@ -384,7 +375,7 @@ describe('Test video comments API validator', function () {
384 url: server.url, 375 url: server.url,
385 path, 376 path,
386 token: userAccessToken, 377 token: userAccessToken,
387 statusCodeExpected: HttpStatusCode.FORBIDDEN_403 378 expectedStatus: HttpStatusCode.FORBIDDEN_403
388 }) 379 })
389 }) 380 })
390 381
@@ -399,7 +390,7 @@ describe('Test video comments API validator', function () {
399 searchAccount: 'toto', 390 searchAccount: 'toto',
400 searchVideo: 'toto' 391 searchVideo: 'toto'
401 }, 392 },
402 statusCodeExpected: HttpStatusCode.OK_200 393 expectedStatus: HttpStatusCode.OK_200
403 }) 394 })
404 }) 395 })
405 }) 396 })
diff --git a/server/tests/api/check-params/video-imports.ts b/server/tests/api/check-params/video-imports.ts
index a27b624d0..d6d745488 100644
--- a/server/tests/api/check-params/video-imports.ts
+++ b/server/tests/api/check-params/video-imports.ts
@@ -2,33 +2,25 @@
2 2
3import 'mocha' 3import 'mocha'
4import { omit } from 'lodash' 4import { omit } from 'lodash'
5import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
6import { 5import {
7 buildAbsoluteFixturePath, 6 buildAbsoluteFixturePath,
7 checkBadCountPagination,
8 checkBadSortPagination,
9 checkBadStartPagination,
8 cleanupTests, 10 cleanupTests,
9 createUser, 11 createSingleServer,
10 flushAndRunServer, 12 FIXTURE_URLS,
11 getMyUserInformation,
12 immutableAssign,
13 makeGetRequest, 13 makeGetRequest,
14 makePostBodyRequest, 14 makePostBodyRequest,
15 makeUploadRequest, 15 makeUploadRequest,
16 ServerInfo, 16 PeerTubeServer,
17 setAccessTokensToServers, 17 setAccessTokensToServers
18 updateCustomSubConfig, 18} from '@shared/extra-utils'
19 userLogin 19import { HttpStatusCode, VideoPrivacy } from '@shared/models'
20} from '../../../../shared/extra-utils'
21import {
22 checkBadCountPagination,
23 checkBadSortPagination,
24 checkBadStartPagination
25} from '../../../../shared/extra-utils/requests/check-api-params'
26import { getGoodVideoUrl, getMagnetURI } from '../../../../shared/extra-utils/videos/video-imports'
27import { VideoPrivacy } from '../../../../shared/models/videos/video-privacy.enum'
28 20
29describe('Test video imports API validator', function () { 21describe('Test video imports API validator', function () {
30 const path = '/api/v1/videos/imports' 22 const path = '/api/v1/videos/imports'
31 let server: ServerInfo 23 let server: PeerTubeServer
32 let userAccessToken = '' 24 let userAccessToken = ''
33 let channelId: number 25 let channelId: number
34 26
@@ -37,18 +29,18 @@ describe('Test video imports API validator', function () {
37 before(async function () { 29 before(async function () {
38 this.timeout(30000) 30 this.timeout(30000)
39 31
40 server = await flushAndRunServer(1) 32 server = await createSingleServer(1)
41 33
42 await setAccessTokensToServers([ server ]) 34 await setAccessTokensToServers([ server ])
43 35
44 const username = 'user1' 36 const username = 'user1'
45 const password = 'my super password' 37 const password = 'my super password'
46 await createUser({ url: server.url, accessToken: server.accessToken, username: username, password: password }) 38 await server.users.create({ username: username, password: password })
47 userAccessToken = await userLogin(server, { username, password }) 39 userAccessToken = await server.login.getAccessToken({ username, password })
48 40
49 { 41 {
50 const res = await getMyUserInformation(server.url, server.accessToken) 42 const { videoChannels } = await server.users.getMyInfo()
51 channelId = res.body.videoChannels[0].id 43 channelId = videoChannels[0].id
52 } 44 }
53 }) 45 })
54 46
@@ -68,7 +60,7 @@ describe('Test video imports API validator', function () {
68 }) 60 })
69 61
70 it('Should success with the correct parameters', async function () { 62 it('Should success with the correct parameters', async function () {
71 await makeGetRequest({ url: server.url, path: myPath, statusCodeExpected: HttpStatusCode.OK_200, token: server.accessToken }) 63 await makeGetRequest({ url: server.url, path: myPath, expectedStatus: HttpStatusCode.OK_200, token: server.accessToken })
72 }) 64 })
73 }) 65 })
74 66
@@ -77,7 +69,7 @@ describe('Test video imports API validator', function () {
77 69
78 before(function () { 70 before(function () {
79 baseCorrectParams = { 71 baseCorrectParams = {
80 targetUrl: getGoodVideoUrl(), 72 targetUrl: FIXTURE_URLS.goodVideo,
81 name: 'my super name', 73 name: 'my super name',
82 category: 5, 74 category: 5,
83 licence: 1, 75 licence: 1,
@@ -106,48 +98,48 @@ describe('Test video imports API validator', function () {
106 path, 98 path,
107 token: server.accessToken, 99 token: server.accessToken,
108 fields, 100 fields,
109 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 101 expectedStatus: HttpStatusCode.BAD_REQUEST_400
110 }) 102 })
111 }) 103 })
112 104
113 it('Should fail with a bad target url', async function () { 105 it('Should fail with a bad target url', async function () {
114 const fields = immutableAssign(baseCorrectParams, { targetUrl: 'htt://hello' }) 106 const fields = { ...baseCorrectParams, targetUrl: 'htt://hello' }
115 107
116 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) 108 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
117 }) 109 })
118 110
119 it('Should fail with a long name', async function () { 111 it('Should fail with a long name', async function () {
120 const fields = immutableAssign(baseCorrectParams, { name: 'super'.repeat(65) }) 112 const fields = { ...baseCorrectParams, name: 'super'.repeat(65) }
121 113
122 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) 114 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
123 }) 115 })
124 116
125 it('Should fail with a bad category', async function () { 117 it('Should fail with a bad category', async function () {
126 const fields = immutableAssign(baseCorrectParams, { category: 125 }) 118 const fields = { ...baseCorrectParams, category: 125 }
127 119
128 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) 120 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
129 }) 121 })
130 122
131 it('Should fail with a bad licence', async function () { 123 it('Should fail with a bad licence', async function () {
132 const fields = immutableAssign(baseCorrectParams, { licence: 125 }) 124 const fields = { ...baseCorrectParams, licence: 125 }
133 125
134 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) 126 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
135 }) 127 })
136 128
137 it('Should fail with a bad language', async function () { 129 it('Should fail with a bad language', async function () {
138 const fields = immutableAssign(baseCorrectParams, { language: 'a'.repeat(15) }) 130 const fields = { ...baseCorrectParams, language: 'a'.repeat(15) }
139 131
140 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) 132 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
141 }) 133 })
142 134
143 it('Should fail with a long description', async function () { 135 it('Should fail with a long description', async function () {
144 const fields = immutableAssign(baseCorrectParams, { description: 'super'.repeat(2500) }) 136 const fields = { ...baseCorrectParams, description: 'super'.repeat(2500) }
145 137
146 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) 138 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
147 }) 139 })
148 140
149 it('Should fail with a long support text', async function () { 141 it('Should fail with a long support text', async function () {
150 const fields = immutableAssign(baseCorrectParams, { support: 'super'.repeat(201) }) 142 const fields = { ...baseCorrectParams, support: 'super'.repeat(201) }
151 143
152 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) 144 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
153 }) 145 })
@@ -159,7 +151,7 @@ describe('Test video imports API validator', function () {
159 }) 151 })
160 152
161 it('Should fail with a bad channel', async function () { 153 it('Should fail with a bad channel', async function () {
162 const fields = immutableAssign(baseCorrectParams, { channelId: 545454 }) 154 const fields = { ...baseCorrectParams, channelId: 545454 }
163 155
164 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) 156 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
165 }) 157 })
@@ -169,31 +161,31 @@ describe('Test video imports API validator', function () {
169 username: 'fake', 161 username: 'fake',
170 password: 'fake_password' 162 password: 'fake_password'
171 } 163 }
172 await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password }) 164 await server.users.create({ username: user.username, password: user.password })
173 165
174 const accessTokenUser = await userLogin(server, user) 166 const accessTokenUser = await server.login.getAccessToken(user)
175 const res = await getMyUserInformation(server.url, accessTokenUser) 167 const { videoChannels } = await server.users.getMyInfo({ token: accessTokenUser })
176 const customChannelId = res.body.videoChannels[0].id 168 const customChannelId = videoChannels[0].id
177 169
178 const fields = immutableAssign(baseCorrectParams, { channelId: customChannelId }) 170 const fields = { ...baseCorrectParams, channelId: customChannelId }
179 171
180 await makePostBodyRequest({ url: server.url, path, token: userAccessToken, fields }) 172 await makePostBodyRequest({ url: server.url, path, token: userAccessToken, fields })
181 }) 173 })
182 174
183 it('Should fail with too many tags', async function () { 175 it('Should fail with too many tags', async function () {
184 const fields = immutableAssign(baseCorrectParams, { tags: [ 'tag1', 'tag2', 'tag3', 'tag4', 'tag5', 'tag6' ] }) 176 const fields = { ...baseCorrectParams, tags: [ 'tag1', 'tag2', 'tag3', 'tag4', 'tag5', 'tag6' ] }
185 177
186 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) 178 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
187 }) 179 })
188 180
189 it('Should fail with a tag length too low', async function () { 181 it('Should fail with a tag length too low', async function () {
190 const fields = immutableAssign(baseCorrectParams, { tags: [ 'tag1', 't' ] }) 182 const fields = { ...baseCorrectParams, tags: [ 'tag1', 't' ] }
191 183
192 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) 184 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
193 }) 185 })
194 186
195 it('Should fail with a tag length too big', async function () { 187 it('Should fail with a tag length too big', async function () {
196 const fields = immutableAssign(baseCorrectParams, { tags: [ 'tag1', 'my_super_tag_too_long_long_long_long_long_long' ] }) 188 const fields = { ...baseCorrectParams, tags: [ 'tag1', 'my_super_tag_too_long_long_long_long_long_long' ] }
197 189
198 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) 190 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
199 }) 191 })
@@ -245,7 +237,7 @@ describe('Test video imports API validator', function () {
245 237
246 it('Should fail with an invalid magnet URI', async function () { 238 it('Should fail with an invalid magnet URI', async function () {
247 let fields = omit(baseCorrectParams, 'targetUrl') 239 let fields = omit(baseCorrectParams, 'targetUrl')
248 fields = immutableAssign(fields, { magnetUri: 'blabla' }) 240 fields = { ...fields, magnetUri: 'blabla' }
249 241
250 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) 242 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
251 }) 243 })
@@ -258,19 +250,21 @@ describe('Test video imports API validator', function () {
258 path, 250 path,
259 token: server.accessToken, 251 token: server.accessToken,
260 fields: baseCorrectParams, 252 fields: baseCorrectParams,
261 statusCodeExpected: HttpStatusCode.OK_200 253 expectedStatus: HttpStatusCode.OK_200
262 }) 254 })
263 }) 255 })
264 256
265 it('Should forbid to import http videos', async function () { 257 it('Should forbid to import http videos', async function () {
266 await updateCustomSubConfig(server.url, server.accessToken, { 258 await server.config.updateCustomSubConfig({
267 import: { 259 newConfig: {
268 videos: { 260 import: {
269 http: { 261 videos: {
270 enabled: false 262 http: {
271 }, 263 enabled: false
272 torrent: { 264 },
273 enabled: true 265 torrent: {
266 enabled: true
267 }
274 } 268 }
275 } 269 }
276 } 270 }
@@ -281,33 +275,35 @@ describe('Test video imports API validator', function () {
281 path, 275 path,
282 token: server.accessToken, 276 token: server.accessToken,
283 fields: baseCorrectParams, 277 fields: baseCorrectParams,
284 statusCodeExpected: HttpStatusCode.CONFLICT_409 278 expectedStatus: HttpStatusCode.CONFLICT_409
285 }) 279 })
286 }) 280 })
287 281
288 it('Should forbid to import torrent videos', async function () { 282 it('Should forbid to import torrent videos', async function () {
289 await updateCustomSubConfig(server.url, server.accessToken, { 283 await server.config.updateCustomSubConfig({
290 import: { 284 newConfig: {
291 videos: { 285 import: {
292 http: { 286 videos: {
293 enabled: true 287 http: {
294 }, 288 enabled: true
295 torrent: { 289 },
296 enabled: false 290 torrent: {
291 enabled: false
292 }
297 } 293 }
298 } 294 }
299 } 295 }
300 }) 296 })
301 297
302 let fields = omit(baseCorrectParams, 'targetUrl') 298 let fields = omit(baseCorrectParams, 'targetUrl')
303 fields = immutableAssign(fields, { magnetUri: getMagnetURI() }) 299 fields = { ...fields, magnetUri: FIXTURE_URLS.magnet }
304 300
305 await makePostBodyRequest({ 301 await makePostBodyRequest({
306 url: server.url, 302 url: server.url,
307 path, 303 path,
308 token: server.accessToken, 304 token: server.accessToken,
309 fields, 305 fields,
310 statusCodeExpected: HttpStatusCode.CONFLICT_409 306 expectedStatus: HttpStatusCode.CONFLICT_409
311 }) 307 })
312 308
313 fields = omit(fields, 'magnetUri') 309 fields = omit(fields, 'magnetUri')
@@ -321,7 +317,7 @@ describe('Test video imports API validator', function () {
321 token: server.accessToken, 317 token: server.accessToken,
322 fields, 318 fields,
323 attaches, 319 attaches,
324 statusCodeExpected: HttpStatusCode.CONFLICT_409 320 expectedStatus: HttpStatusCode.CONFLICT_409
325 }) 321 })
326 }) 322 })
327 }) 323 })
diff --git a/server/tests/api/check-params/video-playlists.ts b/server/tests/api/check-params/video-playlists.ts
index 18253d11a..e4d541b48 100644
--- a/server/tests/api/check-params/video-playlists.ts
+++ b/server/tests/api/check-params/video-playlists.ts
@@ -1,34 +1,31 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2 2
3import 'mocha' 3import 'mocha'
4import { VideoPlaylistCreateResult, VideoPlaylistPrivacy, VideoPlaylistType } from '@shared/models'
5import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
6import { 4import {
7 addVideoInPlaylist,
8 checkBadCountPagination, 5 checkBadCountPagination,
9 checkBadSortPagination, 6 checkBadSortPagination,
10 checkBadStartPagination, 7 checkBadStartPagination,
11 cleanupTests, 8 cleanupTests,
12 createVideoPlaylist, 9 createSingleServer,
13 deleteVideoPlaylist,
14 flushAndRunServer,
15 generateUserAccessToken,
16 getAccountPlaylistsListWithToken,
17 getVideoPlaylist,
18 immutableAssign,
19 makeGetRequest, 10 makeGetRequest,
20 removeVideoFromPlaylist, 11 PeerTubeServer,
21 reorderVideosPlaylist, 12 PlaylistsCommand,
22 ServerInfo,
23 setAccessTokensToServers, 13 setAccessTokensToServers,
24 setDefaultVideoChannel, 14 setDefaultVideoChannel
25 updateVideoPlaylist, 15} from '@shared/extra-utils'
26 updateVideoPlaylistElement, 16import {
27 uploadVideoAndGetId 17 HttpStatusCode,
28} from '../../../../shared/extra-utils' 18 VideoPlaylistCreate,
19 VideoPlaylistCreateResult,
20 VideoPlaylistElementCreate,
21 VideoPlaylistElementUpdate,
22 VideoPlaylistPrivacy,
23 VideoPlaylistReorder,
24 VideoPlaylistType
25} from '@shared/models'
29 26
30describe('Test video playlists API validator', function () { 27describe('Test video playlists API validator', function () {
31 let server: ServerInfo 28 let server: PeerTubeServer
32 let userAccessToken: string 29 let userAccessToken: string
33 30
34 let playlist: VideoPlaylistCreateResult 31 let playlist: VideoPlaylistCreateResult
@@ -36,49 +33,54 @@ describe('Test video playlists API validator', function () {
36 33
37 let watchLaterPlaylistId: number 34 let watchLaterPlaylistId: number
38 let videoId: number 35 let videoId: number
39 let playlistElementId: number 36 let elementId: number
37
38 let command: PlaylistsCommand
40 39
41 // --------------------------------------------------------------- 40 // ---------------------------------------------------------------
42 41
43 before(async function () { 42 before(async function () {
44 this.timeout(30000) 43 this.timeout(30000)
45 44
46 server = await flushAndRunServer(1) 45 server = await createSingleServer(1)
47 46
48 await setAccessTokensToServers([ server ]) 47 await setAccessTokensToServers([ server ])
49 await setDefaultVideoChannel([ server ]) 48 await setDefaultVideoChannel([ server ])
50 49
51 userAccessToken = await generateUserAccessToken(server, 'user1') 50 userAccessToken = await server.users.generateUserAndToken('user1')
52 videoId = (await uploadVideoAndGetId({ server, videoName: 'video 1' })).id 51 videoId = (await server.videos.quickUpload({ name: 'video 1' })).id
52
53 command = server.playlists
53 54
54 { 55 {
55 const res = await getAccountPlaylistsListWithToken(server.url, server.accessToken, 'root', 0, 5, VideoPlaylistType.WATCH_LATER) 56 const { data } = await command.listByAccount({
56 watchLaterPlaylistId = res.body.data[0].id 57 token: server.accessToken,
58 handle: 'root',
59 start: 0,
60 count: 5,
61 playlistType: VideoPlaylistType.WATCH_LATER
62 })
63 watchLaterPlaylistId = data[0].id
57 } 64 }
58 65
59 { 66 {
60 const res = await createVideoPlaylist({ 67 playlist = await command.create({
61 url: server.url, 68 attributes: {
62 token: server.accessToken,
63 playlistAttrs: {
64 displayName: 'super playlist', 69 displayName: 'super playlist',
65 privacy: VideoPlaylistPrivacy.PUBLIC, 70 privacy: VideoPlaylistPrivacy.PUBLIC,
66 videoChannelId: server.videoChannel.id 71 videoChannelId: server.store.channel.id
67 } 72 }
68 }) 73 })
69 playlist = res.body.videoPlaylist
70 } 74 }
71 75
72 { 76 {
73 const res = await createVideoPlaylist({ 77 const created = await command.create({
74 url: server.url, 78 attributes: {
75 token: server.accessToken,
76 playlistAttrs: {
77 displayName: 'private', 79 displayName: 'private',
78 privacy: VideoPlaylistPrivacy.PRIVATE 80 privacy: VideoPlaylistPrivacy.PRIVATE
79 } 81 }
80 }) 82 })
81 privatePlaylistUUID = res.body.videoPlaylist.uuid 83 privatePlaylistUUID = created.uuid
82 } 84 }
83 }) 85 })
84 86
@@ -117,7 +119,7 @@ describe('Test video playlists API validator', function () {
117 await makeGetRequest({ 119 await makeGetRequest({
118 url: server.url, 120 url: server.url,
119 path: accountPath, 121 path: accountPath,
120 statusCodeExpected: HttpStatusCode.NOT_FOUND_404, 122 expectedStatus: HttpStatusCode.NOT_FOUND_404,
121 token: server.accessToken 123 token: server.accessToken
122 }) 124 })
123 }) 125 })
@@ -128,18 +130,18 @@ describe('Test video playlists API validator', function () {
128 await makeGetRequest({ 130 await makeGetRequest({
129 url: server.url, 131 url: server.url,
130 path: accountPath, 132 path: accountPath,
131 statusCodeExpected: HttpStatusCode.NOT_FOUND_404, 133 expectedStatus: HttpStatusCode.NOT_FOUND_404,
132 token: server.accessToken 134 token: server.accessToken
133 }) 135 })
134 }) 136 })
135 137
136 it('Should success with the correct parameters', async function () { 138 it('Should success with the correct parameters', async function () {
137 await makeGetRequest({ url: server.url, path: globalPath, statusCodeExpected: HttpStatusCode.OK_200, token: server.accessToken }) 139 await makeGetRequest({ url: server.url, path: globalPath, expectedStatus: HttpStatusCode.OK_200, token: server.accessToken })
138 await makeGetRequest({ url: server.url, path: accountPath, statusCodeExpected: HttpStatusCode.OK_200, token: server.accessToken }) 140 await makeGetRequest({ url: server.url, path: accountPath, expectedStatus: HttpStatusCode.OK_200, token: server.accessToken })
139 await makeGetRequest({ 141 await makeGetRequest({
140 url: server.url, 142 url: server.url,
141 path: videoChannelPath, 143 path: videoChannelPath,
142 statusCodeExpected: HttpStatusCode.OK_200, 144 expectedStatus: HttpStatusCode.OK_200,
143 token: server.accessToken 145 token: server.accessToken
144 }) 146 })
145 }) 147 })
@@ -157,141 +159,144 @@ describe('Test video playlists API validator', function () {
157 }) 159 })
158 160
159 it('Should success with the correct parameters', async function () { 161 it('Should success with the correct parameters', async function () {
160 await makeGetRequest({ url: server.url, path: path + playlist.shortUUID + '/videos', statusCodeExpected: HttpStatusCode.OK_200 }) 162 await makeGetRequest({ url: server.url, path: path + playlist.shortUUID + '/videos', expectedStatus: HttpStatusCode.OK_200 })
161 }) 163 })
162 }) 164 })
163 165
164 describe('When getting a video playlist', function () { 166 describe('When getting a video playlist', function () {
165 it('Should fail with a bad id or uuid', async function () { 167 it('Should fail with a bad id or uuid', async function () {
166 await getVideoPlaylist(server.url, 'toto', HttpStatusCode.BAD_REQUEST_400) 168 await command.get({ playlistId: 'toto', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
167 }) 169 })
168 170
169 it('Should fail with an unknown playlist', async function () { 171 it('Should fail with an unknown playlist', async function () {
170 await getVideoPlaylist(server.url, 42, HttpStatusCode.NOT_FOUND_404) 172 await command.get({ playlistId: 42, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
171 }) 173 })
172 174
173 it('Should fail to get an unlisted playlist with the number id', async function () { 175 it('Should fail to get an unlisted playlist with the number id', async function () {
174 const res = await createVideoPlaylist({ 176 const playlist = await command.create({
175 url: server.url, 177 attributes: {
176 token: server.accessToken,
177 playlistAttrs: {
178 displayName: 'super playlist', 178 displayName: 'super playlist',
179 videoChannelId: server.videoChannel.id, 179 videoChannelId: server.store.channel.id,
180 privacy: VideoPlaylistPrivacy.UNLISTED 180 privacy: VideoPlaylistPrivacy.UNLISTED
181 } 181 }
182 }) 182 })
183 const playlist = res.body.videoPlaylist
184 183
185 await getVideoPlaylist(server.url, playlist.id, HttpStatusCode.NOT_FOUND_404) 184 await command.get({ playlistId: playlist.id, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
186 await getVideoPlaylist(server.url, playlist.uuid, HttpStatusCode.OK_200) 185 await command.get({ playlistId: playlist.uuid, expectedStatus: HttpStatusCode.OK_200 })
187 }) 186 })
188 187
189 it('Should succeed with the correct params', async function () { 188 it('Should succeed with the correct params', async function () {
190 await getVideoPlaylist(server.url, playlist.uuid, HttpStatusCode.OK_200) 189 await command.get({ playlistId: playlist.uuid, expectedStatus: HttpStatusCode.OK_200 })
191 }) 190 })
192 }) 191 })
193 192
194 describe('When creating/updating a video playlist', function () { 193 describe('When creating/updating a video playlist', function () {
195 const getBase = (playlistAttrs: any = {}, wrapper: any = {}) => { 194 const getBase = (
196 return Object.assign({ 195 attributes?: Partial<VideoPlaylistCreate>,
197 expectedStatus: HttpStatusCode.BAD_REQUEST_400, 196 wrapper?: Partial<Parameters<PlaylistsCommand['create']>[0]>
198 url: server.url, 197 ) => {
199 token: server.accessToken, 198 return {
200 playlistAttrs: Object.assign({ 199 attributes: {
201 displayName: 'display name', 200 displayName: 'display name',
202 privacy: VideoPlaylistPrivacy.UNLISTED, 201 privacy: VideoPlaylistPrivacy.UNLISTED,
203 thumbnailfile: 'thumbnail.jpg', 202 thumbnailfile: 'thumbnail.jpg',
204 videoChannelId: server.videoChannel.id 203 videoChannelId: server.store.channel.id,
205 }, playlistAttrs) 204
206 }, wrapper) 205 ...attributes
206 },
207
208 expectedStatus: HttpStatusCode.BAD_REQUEST_400,
209
210 ...wrapper
211 }
207 } 212 }
208 const getUpdate = (params: any, playlistId: number | string) => { 213 const getUpdate = (params: any, playlistId: number | string) => {
209 return immutableAssign(params, { playlistId: playlistId }) 214 return { ...params, playlistId: playlistId }
210 } 215 }
211 216
212 it('Should fail with an unauthenticated user', async function () { 217 it('Should fail with an unauthenticated user', async function () {
213 const params = getBase({}, { token: null, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 }) 218 const params = getBase({}, { token: null, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
214 219
215 await createVideoPlaylist(params) 220 await command.create(params)
216 await updateVideoPlaylist(getUpdate(params, playlist.shortUUID)) 221 await command.update(getUpdate(params, playlist.shortUUID))
217 }) 222 })
218 223
219 it('Should fail without displayName', async function () { 224 it('Should fail without displayName', async function () {
220 const params = getBase({ displayName: undefined }) 225 const params = getBase({ displayName: undefined })
221 226
222 await createVideoPlaylist(params) 227 await command.create(params)
223 }) 228 })
224 229
225 it('Should fail with an incorrect display name', async function () { 230 it('Should fail with an incorrect display name', async function () {
226 const params = getBase({ displayName: 's'.repeat(300) }) 231 const params = getBase({ displayName: 's'.repeat(300) })
227 232
228 await createVideoPlaylist(params) 233 await command.create(params)
229 await updateVideoPlaylist(getUpdate(params, playlist.shortUUID)) 234 await command.update(getUpdate(params, playlist.shortUUID))
230 }) 235 })
231 236
232 it('Should fail with an incorrect description', async function () { 237 it('Should fail with an incorrect description', async function () {
233 const params = getBase({ description: 't' }) 238 const params = getBase({ description: 't' })
234 239
235 await createVideoPlaylist(params) 240 await command.create(params)
236 await updateVideoPlaylist(getUpdate(params, playlist.shortUUID)) 241 await command.update(getUpdate(params, playlist.shortUUID))
237 }) 242 })
238 243
239 it('Should fail with an incorrect privacy', async function () { 244 it('Should fail with an incorrect privacy', async function () {
240 const params = getBase({ privacy: 45 }) 245 const params = getBase({ privacy: 45 })
241 246
242 await createVideoPlaylist(params) 247 await command.create(params)
243 await updateVideoPlaylist(getUpdate(params, playlist.shortUUID)) 248 await command.update(getUpdate(params, playlist.shortUUID))
244 }) 249 })
245 250
246 it('Should fail with an unknown video channel id', async function () { 251 it('Should fail with an unknown video channel id', async function () {
247 const params = getBase({ videoChannelId: 42 }, { expectedStatus: HttpStatusCode.NOT_FOUND_404 }) 252 const params = getBase({ videoChannelId: 42 }, { expectedStatus: HttpStatusCode.NOT_FOUND_404 })
248 253
249 await createVideoPlaylist(params) 254 await command.create(params)
250 await updateVideoPlaylist(getUpdate(params, playlist.shortUUID)) 255 await command.update(getUpdate(params, playlist.shortUUID))
251 }) 256 })
252 257
253 it('Should fail with an incorrect thumbnail file', async function () { 258 it('Should fail with an incorrect thumbnail file', async function () {
254 const params = getBase({ thumbnailfile: 'video_short.mp4' }) 259 const params = getBase({ thumbnailfile: 'video_short.mp4' })
255 260
256 await createVideoPlaylist(params) 261 await command.create(params)
257 await updateVideoPlaylist(getUpdate(params, playlist.shortUUID)) 262 await command.update(getUpdate(params, playlist.shortUUID))
258 }) 263 })
259 264
260 it('Should fail with a thumbnail file too big', async function () { 265 it('Should fail with a thumbnail file too big', async function () {
261 const params = getBase({ thumbnailfile: 'preview-big.png' }) 266 const params = getBase({ thumbnailfile: 'preview-big.png' })
262 267
263 await createVideoPlaylist(params) 268 await command.create(params)
264 await updateVideoPlaylist(getUpdate(params, playlist.shortUUID)) 269 await command.update(getUpdate(params, playlist.shortUUID))
265 }) 270 })
266 271
267 it('Should fail to set "public" a playlist not assigned to a channel', async function () { 272 it('Should fail to set "public" a playlist not assigned to a channel', async function () {
268 const params = getBase({ privacy: VideoPlaylistPrivacy.PUBLIC, videoChannelId: undefined }) 273 const params = getBase({ privacy: VideoPlaylistPrivacy.PUBLIC, videoChannelId: undefined })
269 const params2 = getBase({ privacy: VideoPlaylistPrivacy.PUBLIC, videoChannelId: 'null' }) 274 const params2 = getBase({ privacy: VideoPlaylistPrivacy.PUBLIC, videoChannelId: 'null' as any })
270 const params3 = getBase({ privacy: undefined, videoChannelId: 'null' }) 275 const params3 = getBase({ privacy: undefined, videoChannelId: 'null' as any })
271 276
272 await createVideoPlaylist(params) 277 await command.create(params)
273 await createVideoPlaylist(params2) 278 await command.create(params2)
274 await updateVideoPlaylist(getUpdate(params, privatePlaylistUUID)) 279 await command.update(getUpdate(params, privatePlaylistUUID))
275 await updateVideoPlaylist(getUpdate(params2, playlist.shortUUID)) 280 await command.update(getUpdate(params2, playlist.shortUUID))
276 await updateVideoPlaylist(getUpdate(params3, playlist.shortUUID)) 281 await command.update(getUpdate(params3, playlist.shortUUID))
277 }) 282 })
278 283
279 it('Should fail with an unknown playlist to update', async function () { 284 it('Should fail with an unknown playlist to update', async function () {
280 await updateVideoPlaylist(getUpdate( 285 await command.update(getUpdate(
281 getBase({}, { expectedStatus: HttpStatusCode.NOT_FOUND_404 }), 286 getBase({}, { expectedStatus: HttpStatusCode.NOT_FOUND_404 }),
282 42 287 42
283 )) 288 ))
284 }) 289 })
285 290
286 it('Should fail to update a playlist of another user', async function () { 291 it('Should fail to update a playlist of another user', async function () {
287 await updateVideoPlaylist(getUpdate( 292 await command.update(getUpdate(
288 getBase({}, { token: userAccessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 }), 293 getBase({}, { token: userAccessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 }),
289 playlist.shortUUID 294 playlist.shortUUID
290 )) 295 ))
291 }) 296 })
292 297
293 it('Should fail to update the watch later playlist', async function () { 298 it('Should fail to update the watch later playlist', async function () {
294 await updateVideoPlaylist(getUpdate( 299 await command.update(getUpdate(
295 getBase({}, { expectedStatus: HttpStatusCode.BAD_REQUEST_400 }), 300 getBase({}, { expectedStatus: HttpStatusCode.BAD_REQUEST_400 }),
296 watchLaterPlaylistId 301 watchLaterPlaylistId
297 )) 302 ))
@@ -300,146 +305,158 @@ describe('Test video playlists API validator', function () {
300 it('Should succeed with the correct params', async function () { 305 it('Should succeed with the correct params', async function () {
301 { 306 {
302 const params = getBase({}, { expectedStatus: HttpStatusCode.OK_200 }) 307 const params = getBase({}, { expectedStatus: HttpStatusCode.OK_200 })
303 await createVideoPlaylist(params) 308 await command.create(params)
304 } 309 }
305 310
306 { 311 {
307 const params = getBase({}, { expectedStatus: HttpStatusCode.NO_CONTENT_204 }) 312 const params = getBase({}, { expectedStatus: HttpStatusCode.NO_CONTENT_204 })
308 await updateVideoPlaylist(getUpdate(params, playlist.shortUUID)) 313 await command.update(getUpdate(params, playlist.shortUUID))
309 } 314 }
310 }) 315 })
311 }) 316 })
312 317
313 describe('When adding an element in a playlist', function () { 318 describe('When adding an element in a playlist', function () {
314 const getBase = (elementAttrs: any = {}, wrapper: any = {}) => { 319 const getBase = (
315 return Object.assign({ 320 attributes?: Partial<VideoPlaylistElementCreate>,
316 expectedStatus: HttpStatusCode.BAD_REQUEST_400, 321 wrapper?: Partial<Parameters<PlaylistsCommand['addElement']>[0]>
317 url: server.url, 322 ) => {
318 token: server.accessToken, 323 return {
319 playlistId: playlist.id, 324 attributes: {
320 elementAttrs: Object.assign({
321 videoId, 325 videoId,
322 startTimestamp: 2, 326 startTimestamp: 2,
323 stopTimestamp: 3 327 stopTimestamp: 3,
324 }, elementAttrs) 328
325 }, wrapper) 329 ...attributes
330 },
331
332 expectedStatus: HttpStatusCode.BAD_REQUEST_400,
333 playlistId: playlist.id,
334
335 ...wrapper
336 }
326 } 337 }
327 338
328 it('Should fail with an unauthenticated user', async function () { 339 it('Should fail with an unauthenticated user', async function () {
329 const params = getBase({}, { token: null, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 }) 340 const params = getBase({}, { token: null, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
330 await addVideoInPlaylist(params) 341 await command.addElement(params)
331 }) 342 })
332 343
333 it('Should fail with the playlist of another user', async function () { 344 it('Should fail with the playlist of another user', async function () {
334 const params = getBase({}, { token: userAccessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 }) 345 const params = getBase({}, { token: userAccessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
335 await addVideoInPlaylist(params) 346 await command.addElement(params)
336 }) 347 })
337 348
338 it('Should fail with an unknown or incorrect playlist id', async function () { 349 it('Should fail with an unknown or incorrect playlist id', async function () {
339 { 350 {
340 const params = getBase({}, { playlistId: 'toto' }) 351 const params = getBase({}, { playlistId: 'toto' })
341 await addVideoInPlaylist(params) 352 await command.addElement(params)
342 } 353 }
343 354
344 { 355 {
345 const params = getBase({}, { playlistId: 42, expectedStatus: HttpStatusCode.NOT_FOUND_404 }) 356 const params = getBase({}, { playlistId: 42, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
346 await addVideoInPlaylist(params) 357 await command.addElement(params)
347 } 358 }
348 }) 359 })
349 360
350 it('Should fail with an unknown or incorrect video id', async function () { 361 it('Should fail with an unknown or incorrect video id', async function () {
351 const params = getBase({ videoId: 42 }, { expectedStatus: HttpStatusCode.NOT_FOUND_404 }) 362 const params = getBase({ videoId: 42 }, { expectedStatus: HttpStatusCode.NOT_FOUND_404 })
352 await addVideoInPlaylist(params) 363 await command.addElement(params)
353 }) 364 })
354 365
355 it('Should fail with a bad start/stop timestamp', async function () { 366 it('Should fail with a bad start/stop timestamp', async function () {
356 { 367 {
357 const params = getBase({ startTimestamp: -42 }) 368 const params = getBase({ startTimestamp: -42 })
358 await addVideoInPlaylist(params) 369 await command.addElement(params)
359 } 370 }
360 371
361 { 372 {
362 const params = getBase({ stopTimestamp: 'toto' as any }) 373 const params = getBase({ stopTimestamp: 'toto' as any })
363 await addVideoInPlaylist(params) 374 await command.addElement(params)
364 } 375 }
365 }) 376 })
366 377
367 it('Succeed with the correct params', async function () { 378 it('Succeed with the correct params', async function () {
368 const params = getBase({}, { expectedStatus: HttpStatusCode.OK_200 }) 379 const params = getBase({}, { expectedStatus: HttpStatusCode.OK_200 })
369 const res = await addVideoInPlaylist(params) 380 const created = await command.addElement(params)
370 playlistElementId = res.body.videoPlaylistElement.id 381 elementId = created.id
371 }) 382 })
372 }) 383 })
373 384
374 describe('When updating an element in a playlist', function () { 385 describe('When updating an element in a playlist', function () {
375 const getBase = (elementAttrs: any = {}, wrapper: any = {}) => { 386 const getBase = (
376 return Object.assign({ 387 attributes?: Partial<VideoPlaylistElementUpdate>,
377 url: server.url, 388 wrapper?: Partial<Parameters<PlaylistsCommand['updateElement']>[0]>
378 token: server.accessToken, 389 ) => {
379 elementAttrs: Object.assign({ 390 return {
391 attributes: {
380 startTimestamp: 1, 392 startTimestamp: 1,
381 stopTimestamp: 2 393 stopTimestamp: 2,
382 }, elementAttrs), 394
383 playlistElementId, 395 ...attributes
396 },
397
398 elementId,
384 playlistId: playlist.id, 399 playlistId: playlist.id,
385 expectedStatus: HttpStatusCode.BAD_REQUEST_400 400 expectedStatus: HttpStatusCode.BAD_REQUEST_400,
386 }, wrapper) 401
402 ...wrapper
403 }
387 } 404 }
388 405
389 it('Should fail with an unauthenticated user', async function () { 406 it('Should fail with an unauthenticated user', async function () {
390 const params = getBase({}, { token: null, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 }) 407 const params = getBase({}, { token: null, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
391 await updateVideoPlaylistElement(params) 408 await command.updateElement(params)
392 }) 409 })
393 410
394 it('Should fail with the playlist of another user', async function () { 411 it('Should fail with the playlist of another user', async function () {
395 const params = getBase({}, { token: userAccessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 }) 412 const params = getBase({}, { token: userAccessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
396 await updateVideoPlaylistElement(params) 413 await command.updateElement(params)
397 }) 414 })
398 415
399 it('Should fail with an unknown or incorrect playlist id', async function () { 416 it('Should fail with an unknown or incorrect playlist id', async function () {
400 { 417 {
401 const params = getBase({}, { playlistId: 'toto' }) 418 const params = getBase({}, { playlistId: 'toto' })
402 await updateVideoPlaylistElement(params) 419 await command.updateElement(params)
403 } 420 }
404 421
405 { 422 {
406 const params = getBase({}, { playlistId: 42, expectedStatus: HttpStatusCode.NOT_FOUND_404 }) 423 const params = getBase({}, { playlistId: 42, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
407 await updateVideoPlaylistElement(params) 424 await command.updateElement(params)
408 } 425 }
409 }) 426 })
410 427
411 it('Should fail with an unknown or incorrect playlistElement id', async function () { 428 it('Should fail with an unknown or incorrect playlistElement id', async function () {
412 { 429 {
413 const params = getBase({}, { playlistElementId: 'toto' }) 430 const params = getBase({}, { elementId: 'toto' })
414 await updateVideoPlaylistElement(params) 431 await command.updateElement(params)
415 } 432 }
416 433
417 { 434 {
418 const params = getBase({}, { playlistElementId: 42, expectedStatus: HttpStatusCode.NOT_FOUND_404 }) 435 const params = getBase({}, { elementId: 42, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
419 await updateVideoPlaylistElement(params) 436 await command.updateElement(params)
420 } 437 }
421 }) 438 })
422 439
423 it('Should fail with a bad start/stop timestamp', async function () { 440 it('Should fail with a bad start/stop timestamp', async function () {
424 { 441 {
425 const params = getBase({ startTimestamp: 'toto' as any }) 442 const params = getBase({ startTimestamp: 'toto' as any })
426 await updateVideoPlaylistElement(params) 443 await command.updateElement(params)
427 } 444 }
428 445
429 { 446 {
430 const params = getBase({ stopTimestamp: -42 }) 447 const params = getBase({ stopTimestamp: -42 })
431 await updateVideoPlaylistElement(params) 448 await command.updateElement(params)
432 } 449 }
433 }) 450 })
434 451
435 it('Should fail with an unknown element', async function () { 452 it('Should fail with an unknown element', async function () {
436 const params = getBase({}, { playlistElementId: 888, expectedStatus: HttpStatusCode.NOT_FOUND_404 }) 453 const params = getBase({}, { elementId: 888, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
437 await updateVideoPlaylistElement(params) 454 await command.updateElement(params)
438 }) 455 })
439 456
440 it('Succeed with the correct params', async function () { 457 it('Succeed with the correct params', async function () {
441 const params = getBase({}, { expectedStatus: HttpStatusCode.NO_CONTENT_204 }) 458 const params = getBase({}, { expectedStatus: HttpStatusCode.NO_CONTENT_204 })
442 await updateVideoPlaylistElement(params) 459 await command.updateElement(params)
443 }) 460 })
444 }) 461 })
445 462
@@ -447,110 +464,111 @@ describe('Test video playlists API validator', function () {
447 let videoId3: number 464 let videoId3: number
448 let videoId4: number 465 let videoId4: number
449 466
450 const getBase = (elementAttrs: any = {}, wrapper: any = {}) => { 467 const getBase = (
451 return Object.assign({ 468 attributes?: Partial<VideoPlaylistReorder>,
452 url: server.url, 469 wrapper?: Partial<Parameters<PlaylistsCommand['reorderElements']>[0]>
453 token: server.accessToken, 470 ) => {
454 playlistId: playlist.shortUUID, 471 return {
455 elementAttrs: Object.assign({ 472 attributes: {
456 startPosition: 1, 473 startPosition: 1,
457 insertAfterPosition: 2, 474 insertAfterPosition: 2,
458 reorderLength: 3 475 reorderLength: 3,
459 }, elementAttrs), 476
460 expectedStatus: HttpStatusCode.BAD_REQUEST_400 477 ...attributes
461 }, wrapper) 478 },
479
480 playlistId: playlist.shortUUID,
481 expectedStatus: HttpStatusCode.BAD_REQUEST_400,
482
483 ...wrapper
484 }
462 } 485 }
463 486
464 before(async function () { 487 before(async function () {
465 videoId3 = (await uploadVideoAndGetId({ server, videoName: 'video 3' })).id 488 videoId3 = (await server.videos.quickUpload({ name: 'video 3' })).id
466 videoId4 = (await uploadVideoAndGetId({ server, videoName: 'video 4' })).id 489 videoId4 = (await server.videos.quickUpload({ name: 'video 4' })).id
467 490
468 for (const id of [ videoId3, videoId4 ]) { 491 for (const id of [ videoId3, videoId4 ]) {
469 await addVideoInPlaylist({ 492 await command.addElement({ playlistId: playlist.shortUUID, attributes: { videoId: id } })
470 url: server.url,
471 token: server.accessToken,
472 playlistId: playlist.shortUUID,
473 elementAttrs: { videoId: id }
474 })
475 } 493 }
476 }) 494 })
477 495
478 it('Should fail with an unauthenticated user', async function () { 496 it('Should fail with an unauthenticated user', async function () {
479 const params = getBase({}, { token: null, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 }) 497 const params = getBase({}, { token: null, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
480 await reorderVideosPlaylist(params) 498 await command.reorderElements(params)
481 }) 499 })
482 500
483 it('Should fail with the playlist of another user', async function () { 501 it('Should fail with the playlist of another user', async function () {
484 const params = getBase({}, { token: userAccessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 }) 502 const params = getBase({}, { token: userAccessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
485 await reorderVideosPlaylist(params) 503 await command.reorderElements(params)
486 }) 504 })
487 505
488 it('Should fail with an invalid playlist', async function () { 506 it('Should fail with an invalid playlist', async function () {
489 { 507 {
490 const params = getBase({}, { playlistId: 'toto' }) 508 const params = getBase({}, { playlistId: 'toto' })
491 await reorderVideosPlaylist(params) 509 await command.reorderElements(params)
492 } 510 }
493 511
494 { 512 {
495 const params = getBase({}, { playlistId: 42, expectedStatus: HttpStatusCode.NOT_FOUND_404 }) 513 const params = getBase({}, { playlistId: 42, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
496 await reorderVideosPlaylist(params) 514 await command.reorderElements(params)
497 } 515 }
498 }) 516 })
499 517
500 it('Should fail with an invalid start position', async function () { 518 it('Should fail with an invalid start position', async function () {
501 { 519 {
502 const params = getBase({ startPosition: -1 }) 520 const params = getBase({ startPosition: -1 })
503 await reorderVideosPlaylist(params) 521 await command.reorderElements(params)
504 } 522 }
505 523
506 { 524 {
507 const params = getBase({ startPosition: 'toto' as any }) 525 const params = getBase({ startPosition: 'toto' as any })
508 await reorderVideosPlaylist(params) 526 await command.reorderElements(params)
509 } 527 }
510 528
511 { 529 {
512 const params = getBase({ startPosition: 42 }) 530 const params = getBase({ startPosition: 42 })
513 await reorderVideosPlaylist(params) 531 await command.reorderElements(params)
514 } 532 }
515 }) 533 })
516 534
517 it('Should fail with an invalid insert after position', async function () { 535 it('Should fail with an invalid insert after position', async function () {
518 { 536 {
519 const params = getBase({ insertAfterPosition: 'toto' as any }) 537 const params = getBase({ insertAfterPosition: 'toto' as any })
520 await reorderVideosPlaylist(params) 538 await command.reorderElements(params)
521 } 539 }
522 540
523 { 541 {
524 const params = getBase({ insertAfterPosition: -2 }) 542 const params = getBase({ insertAfterPosition: -2 })
525 await reorderVideosPlaylist(params) 543 await command.reorderElements(params)
526 } 544 }
527 545
528 { 546 {
529 const params = getBase({ insertAfterPosition: 42 }) 547 const params = getBase({ insertAfterPosition: 42 })
530 await reorderVideosPlaylist(params) 548 await command.reorderElements(params)
531 } 549 }
532 }) 550 })
533 551
534 it('Should fail with an invalid reorder length', async function () { 552 it('Should fail with an invalid reorder length', async function () {
535 { 553 {
536 const params = getBase({ reorderLength: 'toto' as any }) 554 const params = getBase({ reorderLength: 'toto' as any })
537 await reorderVideosPlaylist(params) 555 await command.reorderElements(params)
538 } 556 }
539 557
540 { 558 {
541 const params = getBase({ reorderLength: -2 }) 559 const params = getBase({ reorderLength: -2 })
542 await reorderVideosPlaylist(params) 560 await command.reorderElements(params)
543 } 561 }
544 562
545 { 563 {
546 const params = getBase({ reorderLength: 42 }) 564 const params = getBase({ reorderLength: 42 })
547 await reorderVideosPlaylist(params) 565 await command.reorderElements(params)
548 } 566 }
549 }) 567 })
550 568
551 it('Succeed with the correct params', async function () { 569 it('Succeed with the correct params', async function () {
552 const params = getBase({}, { expectedStatus: HttpStatusCode.NO_CONTENT_204 }) 570 const params = getBase({}, { expectedStatus: HttpStatusCode.NO_CONTENT_204 })
553 await reorderVideosPlaylist(params) 571 await command.reorderElements(params)
554 }) 572 })
555 }) 573 })
556 574
@@ -562,7 +580,7 @@ describe('Test video playlists API validator', function () {
562 url: server.url, 580 url: server.url,
563 path, 581 path,
564 query: { videoIds: [ 1, 2 ] }, 582 query: { videoIds: [ 1, 2 ] },
565 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 583 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
566 }) 584 })
567 }) 585 })
568 586
@@ -595,82 +613,82 @@ describe('Test video playlists API validator', function () {
595 token: server.accessToken, 613 token: server.accessToken,
596 path, 614 path,
597 query: { videoIds: [ 1, 2 ] }, 615 query: { videoIds: [ 1, 2 ] },
598 statusCodeExpected: HttpStatusCode.OK_200 616 expectedStatus: HttpStatusCode.OK_200
599 }) 617 })
600 }) 618 })
601 }) 619 })
602 620
603 describe('When deleting an element in a playlist', function () { 621 describe('When deleting an element in a playlist', function () {
604 const getBase = (wrapper: any = {}) => { 622 const getBase = (wrapper: Partial<Parameters<PlaylistsCommand['removeElement']>[0]>) => {
605 return Object.assign({ 623 return {
606 url: server.url, 624 elementId,
607 token: server.accessToken,
608 playlistElementId,
609 playlistId: playlist.uuid, 625 playlistId: playlist.uuid,
610 expectedStatus: HttpStatusCode.BAD_REQUEST_400 626 expectedStatus: HttpStatusCode.BAD_REQUEST_400,
611 }, wrapper) 627
628 ...wrapper
629 }
612 } 630 }
613 631
614 it('Should fail with an unauthenticated user', async function () { 632 it('Should fail with an unauthenticated user', async function () {
615 const params = getBase({ token: null, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 }) 633 const params = getBase({ token: null, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
616 await removeVideoFromPlaylist(params) 634 await command.removeElement(params)
617 }) 635 })
618 636
619 it('Should fail with the playlist of another user', async function () { 637 it('Should fail with the playlist of another user', async function () {
620 const params = getBase({ token: userAccessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 }) 638 const params = getBase({ token: userAccessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
621 await removeVideoFromPlaylist(params) 639 await command.removeElement(params)
622 }) 640 })
623 641
624 it('Should fail with an unknown or incorrect playlist id', async function () { 642 it('Should fail with an unknown or incorrect playlist id', async function () {
625 { 643 {
626 const params = getBase({ playlistId: 'toto' }) 644 const params = getBase({ playlistId: 'toto' })
627 await removeVideoFromPlaylist(params) 645 await command.removeElement(params)
628 } 646 }
629 647
630 { 648 {
631 const params = getBase({ playlistId: 42, expectedStatus: HttpStatusCode.NOT_FOUND_404 }) 649 const params = getBase({ playlistId: 42, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
632 await removeVideoFromPlaylist(params) 650 await command.removeElement(params)
633 } 651 }
634 }) 652 })
635 653
636 it('Should fail with an unknown or incorrect video id', async function () { 654 it('Should fail with an unknown or incorrect video id', async function () {
637 { 655 {
638 const params = getBase({ playlistElementId: 'toto' }) 656 const params = getBase({ elementId: 'toto' as any })
639 await removeVideoFromPlaylist(params) 657 await command.removeElement(params)
640 } 658 }
641 659
642 { 660 {
643 const params = getBase({ playlistElementId: 42, expectedStatus: HttpStatusCode.NOT_FOUND_404 }) 661 const params = getBase({ elementId: 42, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
644 await removeVideoFromPlaylist(params) 662 await command.removeElement(params)
645 } 663 }
646 }) 664 })
647 665
648 it('Should fail with an unknown element', async function () { 666 it('Should fail with an unknown element', async function () {
649 const params = getBase({ playlistElementId: 888, expectedStatus: HttpStatusCode.NOT_FOUND_404 }) 667 const params = getBase({ elementId: 888, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
650 await removeVideoFromPlaylist(params) 668 await command.removeElement(params)
651 }) 669 })
652 670
653 it('Succeed with the correct params', async function () { 671 it('Succeed with the correct params', async function () {
654 const params = getBase({ expectedStatus: HttpStatusCode.NO_CONTENT_204 }) 672 const params = getBase({ expectedStatus: HttpStatusCode.NO_CONTENT_204 })
655 await removeVideoFromPlaylist(params) 673 await command.removeElement(params)
656 }) 674 })
657 }) 675 })
658 676
659 describe('When deleting a playlist', function () { 677 describe('When deleting a playlist', function () {
660 it('Should fail with an unknown playlist', async function () { 678 it('Should fail with an unknown playlist', async function () {
661 await deleteVideoPlaylist(server.url, server.accessToken, 42, HttpStatusCode.NOT_FOUND_404) 679 await command.delete({ playlistId: 42, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
662 }) 680 })
663 681
664 it('Should fail with a playlist of another user', async function () { 682 it('Should fail with a playlist of another user', async function () {
665 await deleteVideoPlaylist(server.url, userAccessToken, playlist.uuid, HttpStatusCode.FORBIDDEN_403) 683 await command.delete({ token: userAccessToken, playlistId: playlist.uuid, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
666 }) 684 })
667 685
668 it('Should fail with the watch later playlist', async function () { 686 it('Should fail with the watch later playlist', async function () {
669 await deleteVideoPlaylist(server.url, server.accessToken, watchLaterPlaylistId, HttpStatusCode.BAD_REQUEST_400) 687 await command.delete({ playlistId: watchLaterPlaylistId, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
670 }) 688 })
671 689
672 it('Should succeed with the correct params', async function () { 690 it('Should succeed with the correct params', async function () {
673 await deleteVideoPlaylist(server.url, server.accessToken, playlist.uuid) 691 await command.delete({ playlistId: playlist.uuid })
674 }) 692 })
675 }) 693 })
676 694
diff --git a/server/tests/api/check-params/videos-filter.ts b/server/tests/api/check-params/videos-filter.ts
index 4d54a4fd0..d08570bbe 100644
--- a/server/tests/api/check-params/videos-filter.ts
+++ b/server/tests/api/check-params/videos-filter.ts
@@ -3,18 +3,15 @@
3import 'mocha' 3import 'mocha'
4import { 4import {
5 cleanupTests, 5 cleanupTests,
6 createUser, 6 createSingleServer,
7 flushAndRunServer,
8 makeGetRequest, 7 makeGetRequest,
9 ServerInfo, 8 PeerTubeServer,
10 setAccessTokensToServers, 9 setAccessTokensToServers,
11 setDefaultVideoChannel, 10 setDefaultVideoChannel
12 userLogin 11} from '@shared/extra-utils'
13} from '../../../../shared/extra-utils' 12import { HttpStatusCode, UserRole } from '@shared/models'
14import { UserRole } from '../../../../shared/models/users'
15import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
16 13
17async function testEndpoints (server: ServerInfo, token: string, filter: string, statusCodeExpected: HttpStatusCode) { 14async function testEndpoints (server: PeerTubeServer, token: string, filter: string, expectedStatus: HttpStatusCode) {
18 const paths = [ 15 const paths = [
19 '/api/v1/video-channels/root_channel/videos', 16 '/api/v1/video-channels/root_channel/videos',
20 '/api/v1/accounts/root/videos', 17 '/api/v1/accounts/root/videos',
@@ -30,13 +27,13 @@ async function testEndpoints (server: ServerInfo, token: string, filter: string,
30 query: { 27 query: {
31 filter 28 filter
32 }, 29 },
33 statusCodeExpected 30 expectedStatus
34 }) 31 })
35 } 32 }
36} 33}
37 34
38describe('Test video filters validators', function () { 35describe('Test video filters validators', function () {
39 let server: ServerInfo 36 let server: PeerTubeServer
40 let userAccessToken: string 37 let userAccessToken: string
41 let moderatorAccessToken: string 38 let moderatorAccessToken: string
42 39
@@ -45,28 +42,19 @@ describe('Test video filters validators', function () {
45 before(async function () { 42 before(async function () {
46 this.timeout(30000) 43 this.timeout(30000)
47 44
48 server = await flushAndRunServer(1) 45 server = await createSingleServer(1)
49 46
50 await setAccessTokensToServers([ server ]) 47 await setAccessTokensToServers([ server ])
51 await setDefaultVideoChannel([ server ]) 48 await setDefaultVideoChannel([ server ])
52 49
53 const user = { username: 'user1', password: 'my super password' } 50 const user = { username: 'user1', password: 'my super password' }
54 await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password }) 51 await server.users.create({ username: user.username, password: user.password })
55 userAccessToken = await userLogin(server, user) 52 userAccessToken = await server.login.getAccessToken(user)
56 53
57 const moderator = { username: 'moderator', password: 'my super password' } 54 const moderator = { username: 'moderator', password: 'my super password' }
58 await createUser( 55 await server.users.create({ username: moderator.username, password: moderator.password, role: UserRole.MODERATOR })
59 { 56
60 url: server.url, 57 moderatorAccessToken = await server.login.getAccessToken(moderator)
61 accessToken: server.accessToken,
62 username: moderator.username,
63 password: moderator.password,
64 videoQuota: undefined,
65 videoQuotaDaily: undefined,
66 role: UserRole.MODERATOR
67 }
68 )
69 moderatorAccessToken = await userLogin(server, moderator)
70 }) 58 })
71 59
72 describe('When setting a video filter', function () { 60 describe('When setting a video filter', function () {
@@ -100,7 +88,7 @@ describe('Test video filters validators', function () {
100 await makeGetRequest({ 88 await makeGetRequest({
101 url: server.url, 89 url: server.url,
102 path: '/feeds/videos.json', 90 path: '/feeds/videos.json',
103 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401, 91 expectedStatus: HttpStatusCode.UNAUTHORIZED_401,
104 query: { 92 query: {
105 filter 93 filter
106 } 94 }
@@ -112,7 +100,7 @@ describe('Test video filters validators', function () {
112 await makeGetRequest({ 100 await makeGetRequest({
113 url: server.url, 101 url: server.url,
114 path: '/feeds/videos.json', 102 path: '/feeds/videos.json',
115 statusCodeExpected: HttpStatusCode.OK_200, 103 expectedStatus: HttpStatusCode.OK_200,
116 query: { 104 query: {
117 filter: 'local' 105 filter: 'local'
118 } 106 }
diff --git a/server/tests/api/check-params/videos-history.ts b/server/tests/api/check-params/videos-history.ts
index 0e91fe0a8..c3c309ed2 100644
--- a/server/tests/api/check-params/videos-history.ts
+++ b/server/tests/api/check-params/videos-history.ts
@@ -5,42 +5,39 @@ import {
5 checkBadCountPagination, 5 checkBadCountPagination,
6 checkBadStartPagination, 6 checkBadStartPagination,
7 cleanupTests, 7 cleanupTests,
8 flushAndRunServer, 8 createSingleServer,
9 makeGetRequest, 9 makeGetRequest,
10 makePostBodyRequest, 10 makePostBodyRequest,
11 makePutBodyRequest, 11 makePutBodyRequest,
12 ServerInfo, 12 PeerTubeServer,
13 setAccessTokensToServers, 13 setAccessTokensToServers
14 uploadVideo 14} from '@shared/extra-utils'
15} from '../../../../shared/extra-utils' 15import { HttpStatusCode } from '@shared/models'
16import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
17 16
18describe('Test videos history API validator', function () { 17describe('Test videos history API validator', function () {
19 const myHistoryPath = '/api/v1/users/me/history/videos' 18 const myHistoryPath = '/api/v1/users/me/history/videos'
20 const myHistoryRemove = myHistoryPath + '/remove' 19 const myHistoryRemove = myHistoryPath + '/remove'
21 let watchingPath: string 20 let watchingPath: string
22 let server: ServerInfo 21 let server: PeerTubeServer
23 22
24 // --------------------------------------------------------------- 23 // ---------------------------------------------------------------
25 24
26 before(async function () { 25 before(async function () {
27 this.timeout(30000) 26 this.timeout(30000)
28 27
29 server = await flushAndRunServer(1) 28 server = await createSingleServer(1)
30 29
31 await setAccessTokensToServers([ server ]) 30 await setAccessTokensToServers([ server ])
32 31
33 const res = await uploadVideo(server.url, server.accessToken, {}) 32 const { uuid } = await server.videos.upload()
34 const videoUUID = res.body.video.uuid 33 watchingPath = '/api/v1/videos/' + uuid + '/watching'
35
36 watchingPath = '/api/v1/videos/' + videoUUID + '/watching'
37 }) 34 })
38 35
39 describe('When notifying a user is watching a video', function () { 36 describe('When notifying a user is watching a video', function () {
40 37
41 it('Should fail with an unauthenticated user', async function () { 38 it('Should fail with an unauthenticated user', async function () {
42 const fields = { currentTime: 5 } 39 const fields = { currentTime: 5 }
43 await makePutBodyRequest({ url: server.url, path: watchingPath, fields, statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 }) 40 await makePutBodyRequest({ url: server.url, path: watchingPath, fields, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
44 }) 41 })
45 42
46 it('Should fail with an incorrect video id', async function () { 43 it('Should fail with an incorrect video id', async function () {
@@ -51,7 +48,7 @@ describe('Test videos history API validator', function () {
51 path, 48 path,
52 fields, 49 fields,
53 token: server.accessToken, 50 token: server.accessToken,
54 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 51 expectedStatus: HttpStatusCode.BAD_REQUEST_400
55 }) 52 })
56 }) 53 })
57 54
@@ -64,7 +61,7 @@ describe('Test videos history API validator', function () {
64 path, 61 path,
65 fields, 62 fields,
66 token: server.accessToken, 63 token: server.accessToken,
67 statusCodeExpected: HttpStatusCode.NOT_FOUND_404 64 expectedStatus: HttpStatusCode.NOT_FOUND_404
68 }) 65 })
69 }) 66 })
70 67
@@ -75,7 +72,7 @@ describe('Test videos history API validator', function () {
75 path: watchingPath, 72 path: watchingPath,
76 fields, 73 fields,
77 token: server.accessToken, 74 token: server.accessToken,
78 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 75 expectedStatus: HttpStatusCode.BAD_REQUEST_400
79 }) 76 })
80 }) 77 })
81 78
@@ -87,7 +84,7 @@ describe('Test videos history API validator', function () {
87 path: watchingPath, 84 path: watchingPath,
88 fields, 85 fields,
89 token: server.accessToken, 86 token: server.accessToken,
90 statusCodeExpected: HttpStatusCode.NO_CONTENT_204 87 expectedStatus: HttpStatusCode.NO_CONTENT_204
91 }) 88 })
92 }) 89 })
93 }) 90 })
@@ -102,17 +99,17 @@ describe('Test videos history API validator', function () {
102 }) 99 })
103 100
104 it('Should fail with an unauthenticated user', async function () { 101 it('Should fail with an unauthenticated user', async function () {
105 await makeGetRequest({ url: server.url, path: myHistoryPath, statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 }) 102 await makeGetRequest({ url: server.url, path: myHistoryPath, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
106 }) 103 })
107 104
108 it('Should succeed with the correct params', async function () { 105 it('Should succeed with the correct params', async function () {
109 await makeGetRequest({ url: server.url, token: server.accessToken, path: myHistoryPath, statusCodeExpected: HttpStatusCode.OK_200 }) 106 await makeGetRequest({ url: server.url, token: server.accessToken, path: myHistoryPath, expectedStatus: HttpStatusCode.OK_200 })
110 }) 107 })
111 }) 108 })
112 109
113 describe('When removing user videos history', function () { 110 describe('When removing user videos history', function () {
114 it('Should fail with an unauthenticated user', async function () { 111 it('Should fail with an unauthenticated user', async function () {
115 await makePostBodyRequest({ url: server.url, path: myHistoryPath + '/remove', statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 }) 112 await makePostBodyRequest({ url: server.url, path: myHistoryPath + '/remove', expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
116 }) 113 })
117 114
118 it('Should fail with a bad beforeDate parameter', async function () { 115 it('Should fail with a bad beforeDate parameter', async function () {
@@ -122,7 +119,7 @@ describe('Test videos history API validator', function () {
122 token: server.accessToken, 119 token: server.accessToken,
123 path: myHistoryRemove, 120 path: myHistoryRemove,
124 fields: body, 121 fields: body,
125 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 122 expectedStatus: HttpStatusCode.BAD_REQUEST_400
126 }) 123 })
127 }) 124 })
128 125
@@ -133,7 +130,7 @@ describe('Test videos history API validator', function () {
133 token: server.accessToken, 130 token: server.accessToken,
134 path: myHistoryRemove, 131 path: myHistoryRemove,
135 fields: body, 132 fields: body,
136 statusCodeExpected: HttpStatusCode.NO_CONTENT_204 133 expectedStatus: HttpStatusCode.NO_CONTENT_204
137 }) 134 })
138 }) 135 })
139 136
@@ -142,7 +139,7 @@ describe('Test videos history API validator', function () {
142 url: server.url, 139 url: server.url,
143 token: server.accessToken, 140 token: server.accessToken,
144 path: myHistoryRemove, 141 path: myHistoryRemove,
145 statusCodeExpected: HttpStatusCode.NO_CONTENT_204 142 expectedStatus: HttpStatusCode.NO_CONTENT_204
146 }) 143 })
147 }) 144 })
148 }) 145 })
diff --git a/server/tests/api/check-params/videos-overviews.ts b/server/tests/api/check-params/videos-overviews.ts
index 69d7fc471..c2139d74b 100644
--- a/server/tests/api/check-params/videos-overviews.ts
+++ b/server/tests/api/check-params/videos-overviews.ts
@@ -1,29 +1,28 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2 2
3import 'mocha' 3import 'mocha'
4import { cleanupTests, flushAndRunServer, ServerInfo } from '../../../../shared/extra-utils' 4import { cleanupTests, createSingleServer, PeerTubeServer } from '@shared/extra-utils'
5import { getVideosOverview } from '@shared/extra-utils/overviews/overviews'
6 5
7describe('Test videos overview', function () { 6describe('Test videos overview', function () {
8 let server: ServerInfo 7 let server: PeerTubeServer
9 8
10 // --------------------------------------------------------------- 9 // ---------------------------------------------------------------
11 10
12 before(async function () { 11 before(async function () {
13 this.timeout(30000) 12 this.timeout(30000)
14 13
15 server = await flushAndRunServer(1) 14 server = await createSingleServer(1)
16 }) 15 })
17 16
18 describe('When getting videos overview', function () { 17 describe('When getting videos overview', function () {
19 18
20 it('Should fail with a bad pagination', async function () { 19 it('Should fail with a bad pagination', async function () {
21 await getVideosOverview(server.url, 0, 400) 20 await server.overviews.getVideos({ page: 0, expectedStatus: 400 })
22 await getVideosOverview(server.url, 100, 400) 21 await server.overviews.getVideos({ page: 100, expectedStatus: 400 })
23 }) 22 })
24 23
25 it('Should succeed with a good pagination', async function () { 24 it('Should succeed with a good pagination', async function () {
26 await getVideosOverview(server.url, 1) 25 await server.overviews.getVideos({ page: 1 })
27 }) 26 })
28 }) 27 })
29 28
diff --git a/server/tests/api/check-params/videos.ts b/server/tests/api/check-params/videos.ts
index 4d7a9a23b..e11ca0c82 100644
--- a/server/tests/api/check-params/videos.ts
+++ b/server/tests/api/check-params/videos.ts
@@ -5,39 +5,28 @@ import * as chai from 'chai'
5import { omit } from 'lodash' 5import { omit } from 'lodash'
6import { join } from 'path' 6import { join } from 'path'
7import { randomInt } from '@shared/core-utils' 7import { randomInt } from '@shared/core-utils'
8import { PeerTubeProblemDocument, VideoCreateResult } from '@shared/models'
9import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
10import { 8import {
9 checkBadCountPagination,
10 checkBadSortPagination,
11 checkBadStartPagination,
11 checkUploadVideoParam, 12 checkUploadVideoParam,
12 cleanupTests, 13 cleanupTests,
13 createUser, 14 createSingleServer,
14 flushAndRunServer,
15 getMyUserInformation,
16 getVideo,
17 getVideosList,
18 immutableAssign,
19 makeDeleteRequest, 15 makeDeleteRequest,
20 makeGetRequest, 16 makeGetRequest,
21 makePutBodyRequest, 17 makePutBodyRequest,
22 makeUploadRequest, 18 makeUploadRequest,
23 removeVideo, 19 PeerTubeServer,
24 root, 20 root,
25 ServerInfo, 21 setAccessTokensToServers
26 setAccessTokensToServers, 22} from '@shared/extra-utils'
27 userLogin 23import { HttpStatusCode, PeerTubeProblemDocument, VideoCreateResult, VideoPrivacy } from '@shared/models'
28} from '../../../../shared/extra-utils'
29import {
30 checkBadCountPagination,
31 checkBadSortPagination,
32 checkBadStartPagination
33} from '../../../../shared/extra-utils/requests/check-api-params'
34import { VideoPrivacy } from '../../../../shared/models/videos/video-privacy.enum'
35 24
36const expect = chai.expect 25const expect = chai.expect
37 26
38describe('Test videos API validator', function () { 27describe('Test videos API validator', function () {
39 const path = '/api/v1/videos/' 28 const path = '/api/v1/videos/'
40 let server: ServerInfo 29 let server: PeerTubeServer
41 let userAccessToken = '' 30 let userAccessToken = ''
42 let accountName: string 31 let accountName: string
43 let channelId: number 32 let channelId: number
@@ -49,20 +38,20 @@ describe('Test videos API validator', function () {
49 before(async function () { 38 before(async function () {
50 this.timeout(30000) 39 this.timeout(30000)
51 40
52 server = await flushAndRunServer(1) 41 server = await createSingleServer(1)
53 42
54 await setAccessTokensToServers([ server ]) 43 await setAccessTokensToServers([ server ])
55 44
56 const username = 'user1' 45 const username = 'user1'
57 const password = 'my super password' 46 const password = 'my super password'
58 await createUser({ url: server.url, accessToken: server.accessToken, username: username, password: password }) 47 await server.users.create({ username: username, password: password })
59 userAccessToken = await userLogin(server, { username, password }) 48 userAccessToken = await server.login.getAccessToken({ username, password })
60 49
61 { 50 {
62 const res = await getMyUserInformation(server.url, server.accessToken) 51 const body = await server.users.getMyInfo()
63 channelId = res.body.videoChannels[0].id 52 channelId = body.videoChannels[0].id
64 channelName = res.body.videoChannels[0].name 53 channelName = body.videoChannels[0].name
65 accountName = res.body.account.name + '@' + res.body.account.host 54 accountName = body.account.name + '@' + body.account.host
66 } 55 }
67 }) 56 })
68 57
@@ -80,11 +69,11 @@ describe('Test videos API validator', function () {
80 }) 69 })
81 70
82 it('Should fail with a bad skipVideos query', async function () { 71 it('Should fail with a bad skipVideos query', async function () {
83 await makeGetRequest({ url: server.url, path, statusCodeExpected: HttpStatusCode.OK_200, query: { skipCount: 'toto' } }) 72 await makeGetRequest({ url: server.url, path, expectedStatus: HttpStatusCode.OK_200, query: { skipCount: 'toto' } })
84 }) 73 })
85 74
86 it('Should success with the correct parameters', async function () { 75 it('Should success with the correct parameters', async function () {
87 await makeGetRequest({ url: server.url, path, statusCodeExpected: HttpStatusCode.OK_200, query: { skipCount: false } }) 76 await makeGetRequest({ url: server.url, path, expectedStatus: HttpStatusCode.OK_200, query: { skipCount: false } })
88 }) 77 })
89 }) 78 })
90 79
@@ -94,7 +83,7 @@ describe('Test videos API validator', function () {
94 await makeGetRequest({ 83 await makeGetRequest({
95 url: server.url, 84 url: server.url,
96 path: join(path, 'search'), 85 path: join(path, 'search'),
97 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 86 expectedStatus: HttpStatusCode.BAD_REQUEST_400
98 }) 87 })
99 }) 88 })
100 89
@@ -111,7 +100,7 @@ describe('Test videos API validator', function () {
111 }) 100 })
112 101
113 it('Should success with the correct parameters', async function () { 102 it('Should success with the correct parameters', async function () {
114 await makeGetRequest({ url: server.url, path, statusCodeExpected: HttpStatusCode.OK_200 }) 103 await makeGetRequest({ url: server.url, path, expectedStatus: HttpStatusCode.OK_200 })
115 }) 104 })
116 }) 105 })
117 106
@@ -131,7 +120,7 @@ describe('Test videos API validator', function () {
131 }) 120 })
132 121
133 it('Should success with the correct parameters', async function () { 122 it('Should success with the correct parameters', async function () {
134 await makeGetRequest({ url: server.url, token: server.accessToken, path, statusCodeExpected: HttpStatusCode.OK_200 }) 123 await makeGetRequest({ url: server.url, token: server.accessToken, path, expectedStatus: HttpStatusCode.OK_200 })
135 }) 124 })
136 }) 125 })
137 126
@@ -155,7 +144,7 @@ describe('Test videos API validator', function () {
155 }) 144 })
156 145
157 it('Should success with the correct parameters', async function () { 146 it('Should success with the correct parameters', async function () {
158 await makeGetRequest({ url: server.url, path, statusCodeExpected: HttpStatusCode.OK_200 }) 147 await makeGetRequest({ url: server.url, path, expectedStatus: HttpStatusCode.OK_200 })
159 }) 148 })
160 }) 149 })
161 150
@@ -179,7 +168,7 @@ describe('Test videos API validator', function () {
179 }) 168 })
180 169
181 it('Should success with the correct parameters', async function () { 170 it('Should success with the correct parameters', async function () {
182 await makeGetRequest({ url: server.url, path, statusCodeExpected: HttpStatusCode.OK_200 }) 171 await makeGetRequest({ url: server.url, path, expectedStatus: HttpStatusCode.OK_200 })
183 }) 172 })
184 }) 173 })
185 174
@@ -214,70 +203,70 @@ describe('Test videos API validator', function () {
214 it('Should fail with nothing', async function () { 203 it('Should fail with nothing', async function () {
215 const fields = {} 204 const fields = {}
216 const attaches = {} 205 const attaches = {}
217 await checkUploadVideoParam(server.url, server.accessToken, { ...fields, ...attaches }, HttpStatusCode.BAD_REQUEST_400, mode) 206 await checkUploadVideoParam(server, server.accessToken, { ...fields, ...attaches }, HttpStatusCode.BAD_REQUEST_400, mode)
218 }) 207 })
219 208
220 it('Should fail without name', async function () { 209 it('Should fail without name', async function () {
221 const fields = omit(baseCorrectParams, 'name') 210 const fields = omit(baseCorrectParams, 'name')
222 const attaches = baseCorrectAttaches 211 const attaches = baseCorrectAttaches
223 212
224 await checkUploadVideoParam(server.url, server.accessToken, { ...fields, ...attaches }, HttpStatusCode.BAD_REQUEST_400, mode) 213 await checkUploadVideoParam(server, server.accessToken, { ...fields, ...attaches }, HttpStatusCode.BAD_REQUEST_400, mode)
225 }) 214 })
226 215
227 it('Should fail with a long name', async function () { 216 it('Should fail with a long name', async function () {
228 const fields = immutableAssign(baseCorrectParams, { name: 'super'.repeat(65) }) 217 const fields = { ...baseCorrectParams, name: 'super'.repeat(65) }
229 const attaches = baseCorrectAttaches 218 const attaches = baseCorrectAttaches
230 219
231 await checkUploadVideoParam(server.url, server.accessToken, { ...fields, ...attaches }, HttpStatusCode.BAD_REQUEST_400, mode) 220 await checkUploadVideoParam(server, server.accessToken, { ...fields, ...attaches }, HttpStatusCode.BAD_REQUEST_400, mode)
232 }) 221 })
233 222
234 it('Should fail with a bad category', async function () { 223 it('Should fail with a bad category', async function () {
235 const fields = immutableAssign(baseCorrectParams, { category: 125 }) 224 const fields = { ...baseCorrectParams, category: 125 }
236 const attaches = baseCorrectAttaches 225 const attaches = baseCorrectAttaches
237 226
238 await checkUploadVideoParam(server.url, server.accessToken, { ...fields, ...attaches }, HttpStatusCode.BAD_REQUEST_400, mode) 227 await checkUploadVideoParam(server, server.accessToken, { ...fields, ...attaches }, HttpStatusCode.BAD_REQUEST_400, mode)
239 }) 228 })
240 229
241 it('Should fail with a bad licence', async function () { 230 it('Should fail with a bad licence', async function () {
242 const fields = immutableAssign(baseCorrectParams, { licence: 125 }) 231 const fields = { ...baseCorrectParams, licence: 125 }
243 const attaches = baseCorrectAttaches 232 const attaches = baseCorrectAttaches
244 233
245 await checkUploadVideoParam(server.url, server.accessToken, { ...fields, ...attaches }, HttpStatusCode.BAD_REQUEST_400, mode) 234 await checkUploadVideoParam(server, server.accessToken, { ...fields, ...attaches }, HttpStatusCode.BAD_REQUEST_400, mode)
246 }) 235 })
247 236
248 it('Should fail with a bad language', async function () { 237 it('Should fail with a bad language', async function () {
249 const fields = immutableAssign(baseCorrectParams, { language: 'a'.repeat(15) }) 238 const fields = { ...baseCorrectParams, language: 'a'.repeat(15) }
250 const attaches = baseCorrectAttaches 239 const attaches = baseCorrectAttaches
251 240
252 await checkUploadVideoParam(server.url, server.accessToken, { ...fields, ...attaches }, HttpStatusCode.BAD_REQUEST_400, mode) 241 await checkUploadVideoParam(server, server.accessToken, { ...fields, ...attaches }, HttpStatusCode.BAD_REQUEST_400, mode)
253 }) 242 })
254 243
255 it('Should fail with a long description', async function () { 244 it('Should fail with a long description', async function () {
256 const fields = immutableAssign(baseCorrectParams, { description: 'super'.repeat(2500) }) 245 const fields = { ...baseCorrectParams, description: 'super'.repeat(2500) }
257 const attaches = baseCorrectAttaches 246 const attaches = baseCorrectAttaches
258 247
259 await checkUploadVideoParam(server.url, server.accessToken, { ...fields, ...attaches }, HttpStatusCode.BAD_REQUEST_400, mode) 248 await checkUploadVideoParam(server, server.accessToken, { ...fields, ...attaches }, HttpStatusCode.BAD_REQUEST_400, mode)
260 }) 249 })
261 250
262 it('Should fail with a long support text', async function () { 251 it('Should fail with a long support text', async function () {
263 const fields = immutableAssign(baseCorrectParams, { support: 'super'.repeat(201) }) 252 const fields = { ...baseCorrectParams, support: 'super'.repeat(201) }
264 const attaches = baseCorrectAttaches 253 const attaches = baseCorrectAttaches
265 254
266 await checkUploadVideoParam(server.url, server.accessToken, { ...fields, ...attaches }, HttpStatusCode.BAD_REQUEST_400, mode) 255 await checkUploadVideoParam(server, server.accessToken, { ...fields, ...attaches }, HttpStatusCode.BAD_REQUEST_400, mode)
267 }) 256 })
268 257
269 it('Should fail without a channel', async function () { 258 it('Should fail without a channel', async function () {
270 const fields = omit(baseCorrectParams, 'channelId') 259 const fields = omit(baseCorrectParams, 'channelId')
271 const attaches = baseCorrectAttaches 260 const attaches = baseCorrectAttaches
272 261
273 await checkUploadVideoParam(server.url, server.accessToken, { ...fields, ...attaches }, HttpStatusCode.BAD_REQUEST_400, mode) 262 await checkUploadVideoParam(server, server.accessToken, { ...fields, ...attaches }, HttpStatusCode.BAD_REQUEST_400, mode)
274 }) 263 })
275 264
276 it('Should fail with a bad channel', async function () { 265 it('Should fail with a bad channel', async function () {
277 const fields = immutableAssign(baseCorrectParams, { channelId: 545454 }) 266 const fields = { ...baseCorrectParams, channelId: 545454 }
278 const attaches = baseCorrectAttaches 267 const attaches = baseCorrectAttaches
279 268
280 await checkUploadVideoParam(server.url, server.accessToken, { ...fields, ...attaches }, HttpStatusCode.BAD_REQUEST_400, mode) 269 await checkUploadVideoParam(server, server.accessToken, { ...fields, ...attaches }, HttpStatusCode.BAD_REQUEST_400, mode)
281 }) 270 })
282 271
283 it('Should fail with another user channel', async function () { 272 it('Should fail with another user channel', async function () {
@@ -285,69 +274,71 @@ describe('Test videos API validator', function () {
285 username: 'fake' + randomInt(0, 1500), 274 username: 'fake' + randomInt(0, 1500),
286 password: 'fake_password' 275 password: 'fake_password'
287 } 276 }
288 await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password }) 277 await server.users.create({ username: user.username, password: user.password })
289 278
290 const accessTokenUser = await userLogin(server, user) 279 const accessTokenUser = await server.login.getAccessToken(user)
291 const res = await getMyUserInformation(server.url, accessTokenUser) 280 const { videoChannels } = await server.users.getMyInfo({ token: accessTokenUser })
292 const customChannelId = res.body.videoChannels[0].id 281 const customChannelId = videoChannels[0].id
293 282
294 const fields = immutableAssign(baseCorrectParams, { channelId: customChannelId }) 283 const fields = { ...baseCorrectParams, channelId: customChannelId }
295 const attaches = baseCorrectAttaches 284 const attaches = baseCorrectAttaches
296 285
297 await checkUploadVideoParam(server.url, userAccessToken, { ...fields, ...attaches }, HttpStatusCode.BAD_REQUEST_400, mode) 286 await checkUploadVideoParam(server, userAccessToken, { ...fields, ...attaches }, HttpStatusCode.BAD_REQUEST_400, mode)
298 }) 287 })
299 288
300 it('Should fail with too many tags', async function () { 289 it('Should fail with too many tags', async function () {
301 const fields = immutableAssign(baseCorrectParams, { tags: [ 'tag1', 'tag2', 'tag3', 'tag4', 'tag5', 'tag6' ] }) 290 const fields = { ...baseCorrectParams, tags: [ 'tag1', 'tag2', 'tag3', 'tag4', 'tag5', 'tag6' ] }
302 const attaches = baseCorrectAttaches 291 const attaches = baseCorrectAttaches
303 292
304 await checkUploadVideoParam(server.url, server.accessToken, { ...fields, ...attaches }, HttpStatusCode.BAD_REQUEST_400, mode) 293 await checkUploadVideoParam(server, server.accessToken, { ...fields, ...attaches }, HttpStatusCode.BAD_REQUEST_400, mode)
305 }) 294 })
306 295
307 it('Should fail with a tag length too low', async function () { 296 it('Should fail with a tag length too low', async function () {
308 const fields = immutableAssign(baseCorrectParams, { tags: [ 'tag1', 't' ] }) 297 const fields = { ...baseCorrectParams, tags: [ 'tag1', 't' ] }
309 const attaches = baseCorrectAttaches 298 const attaches = baseCorrectAttaches
310 299
311 await checkUploadVideoParam(server.url, server.accessToken, { ...fields, ...attaches }, HttpStatusCode.BAD_REQUEST_400, mode) 300 await checkUploadVideoParam(server, server.accessToken, { ...fields, ...attaches }, HttpStatusCode.BAD_REQUEST_400, mode)
312 }) 301 })
313 302
314 it('Should fail with a tag length too big', async function () { 303 it('Should fail with a tag length too big', async function () {
315 const fields = immutableAssign(baseCorrectParams, { tags: [ 'tag1', 'my_super_tag_too_long_long_long_long_long_long' ] }) 304 const fields = { ...baseCorrectParams, tags: [ 'tag1', 'my_super_tag_too_long_long_long_long_long_long' ] }
316 const attaches = baseCorrectAttaches 305 const attaches = baseCorrectAttaches
317 306
318 await checkUploadVideoParam(server.url, server.accessToken, { ...fields, ...attaches }, HttpStatusCode.BAD_REQUEST_400, mode) 307 await checkUploadVideoParam(server, server.accessToken, { ...fields, ...attaches }, HttpStatusCode.BAD_REQUEST_400, mode)
319 }) 308 })
320 309
321 it('Should fail with a bad schedule update (miss updateAt)', async function () { 310 it('Should fail with a bad schedule update (miss updateAt)', async function () {
322 const fields = immutableAssign(baseCorrectParams, { scheduleUpdate: { privacy: VideoPrivacy.PUBLIC } }) 311 const fields = { ...baseCorrectParams, scheduleUpdate: { privacy: VideoPrivacy.PUBLIC } }
323 const attaches = baseCorrectAttaches 312 const attaches = baseCorrectAttaches
324 313
325 await checkUploadVideoParam(server.url, server.accessToken, { ...fields, ...attaches }, HttpStatusCode.BAD_REQUEST_400, mode) 314 await checkUploadVideoParam(server, server.accessToken, { ...fields, ...attaches }, HttpStatusCode.BAD_REQUEST_400, mode)
326 }) 315 })
327 316
328 it('Should fail with a bad schedule update (wrong updateAt)', async function () { 317 it('Should fail with a bad schedule update (wrong updateAt)', async function () {
329 const fields = immutableAssign(baseCorrectParams, { 318 const fields = {
319 ...baseCorrectParams,
320
330 scheduleUpdate: { 321 scheduleUpdate: {
331 privacy: VideoPrivacy.PUBLIC, 322 privacy: VideoPrivacy.PUBLIC,
332 updateAt: 'toto' 323 updateAt: 'toto'
333 } 324 }
334 }) 325 }
335 const attaches = baseCorrectAttaches 326 const attaches = baseCorrectAttaches
336 327
337 await checkUploadVideoParam(server.url, server.accessToken, { ...fields, ...attaches }, HttpStatusCode.BAD_REQUEST_400, mode) 328 await checkUploadVideoParam(server, server.accessToken, { ...fields, ...attaches }, HttpStatusCode.BAD_REQUEST_400, mode)
338 }) 329 })
339 330
340 it('Should fail with a bad originally published at attribute', async function () { 331 it('Should fail with a bad originally published at attribute', async function () {
341 const fields = immutableAssign(baseCorrectParams, { originallyPublishedAt: 'toto' }) 332 const fields = { ...baseCorrectParams, originallyPublishedAt: 'toto' }
342 const attaches = baseCorrectAttaches 333 const attaches = baseCorrectAttaches
343 334
344 await checkUploadVideoParam(server.url, server.accessToken, { ...fields, ...attaches }, HttpStatusCode.BAD_REQUEST_400, mode) 335 await checkUploadVideoParam(server, server.accessToken, { ...fields, ...attaches }, HttpStatusCode.BAD_REQUEST_400, mode)
345 }) 336 })
346 337
347 it('Should fail without an input file', async function () { 338 it('Should fail without an input file', async function () {
348 const fields = baseCorrectParams 339 const fields = baseCorrectParams
349 const attaches = {} 340 const attaches = {}
350 await checkUploadVideoParam(server.url, server.accessToken, { ...fields, ...attaches }, HttpStatusCode.BAD_REQUEST_400, mode) 341 await checkUploadVideoParam(server, server.accessToken, { ...fields, ...attaches }, HttpStatusCode.BAD_REQUEST_400, mode)
351 }) 342 })
352 343
353 it('Should fail with an incorrect input file', async function () { 344 it('Should fail with an incorrect input file', async function () {
@@ -355,7 +346,7 @@ describe('Test videos API validator', function () {
355 let attaches = { fixture: join(root(), 'server', 'tests', 'fixtures', 'video_short_fake.webm') } 346 let attaches = { fixture: join(root(), 'server', 'tests', 'fixtures', 'video_short_fake.webm') }
356 347
357 await checkUploadVideoParam( 348 await checkUploadVideoParam(
358 server.url, 349 server,
359 server.accessToken, 350 server.accessToken,
360 { ...fields, ...attaches }, 351 { ...fields, ...attaches },
361 HttpStatusCode.UNPROCESSABLE_ENTITY_422, 352 HttpStatusCode.UNPROCESSABLE_ENTITY_422,
@@ -364,7 +355,7 @@ describe('Test videos API validator', function () {
364 355
365 attaches = { fixture: join(root(), 'server', 'tests', 'fixtures', 'video_short.mkv') } 356 attaches = { fixture: join(root(), 'server', 'tests', 'fixtures', 'video_short.mkv') }
366 await checkUploadVideoParam( 357 await checkUploadVideoParam(
367 server.url, 358 server,
368 server.accessToken, 359 server.accessToken,
369 { ...fields, ...attaches }, 360 { ...fields, ...attaches },
370 HttpStatusCode.UNSUPPORTED_MEDIA_TYPE_415, 361 HttpStatusCode.UNSUPPORTED_MEDIA_TYPE_415,
@@ -379,7 +370,7 @@ describe('Test videos API validator', function () {
379 fixture: join(root(), 'server', 'tests', 'fixtures', 'video_short.mp4') 370 fixture: join(root(), 'server', 'tests', 'fixtures', 'video_short.mp4')
380 } 371 }
381 372
382 await checkUploadVideoParam(server.url, server.accessToken, { ...fields, ...attaches }, HttpStatusCode.BAD_REQUEST_400, mode) 373 await checkUploadVideoParam(server, server.accessToken, { ...fields, ...attaches }, HttpStatusCode.BAD_REQUEST_400, mode)
383 }) 374 })
384 375
385 it('Should fail with a big thumbnail file', async function () { 376 it('Should fail with a big thumbnail file', async function () {
@@ -389,7 +380,7 @@ describe('Test videos API validator', function () {
389 fixture: join(root(), 'server', 'tests', 'fixtures', 'video_short.mp4') 380 fixture: join(root(), 'server', 'tests', 'fixtures', 'video_short.mp4')
390 } 381 }
391 382
392 await checkUploadVideoParam(server.url, server.accessToken, { ...fields, ...attaches }, HttpStatusCode.BAD_REQUEST_400, mode) 383 await checkUploadVideoParam(server, server.accessToken, { ...fields, ...attaches }, HttpStatusCode.BAD_REQUEST_400, mode)
393 }) 384 })
394 385
395 it('Should fail with an incorrect preview file', async function () { 386 it('Should fail with an incorrect preview file', async function () {
@@ -399,7 +390,7 @@ describe('Test videos API validator', function () {
399 fixture: join(root(), 'server', 'tests', 'fixtures', 'video_short.mp4') 390 fixture: join(root(), 'server', 'tests', 'fixtures', 'video_short.mp4')
400 } 391 }
401 392
402 await checkUploadVideoParam(server.url, server.accessToken, { ...fields, ...attaches }, HttpStatusCode.BAD_REQUEST_400, mode) 393 await checkUploadVideoParam(server, server.accessToken, { ...fields, ...attaches }, HttpStatusCode.BAD_REQUEST_400, mode)
403 }) 394 })
404 395
405 it('Should fail with a big preview file', async function () { 396 it('Should fail with a big preview file', async function () {
@@ -409,17 +400,17 @@ describe('Test videos API validator', function () {
409 fixture: join(root(), 'server', 'tests', 'fixtures', 'video_short.mp4') 400 fixture: join(root(), 'server', 'tests', 'fixtures', 'video_short.mp4')
410 } 401 }
411 402
412 await checkUploadVideoParam(server.url, server.accessToken, { ...fields, ...attaches }, HttpStatusCode.BAD_REQUEST_400, mode) 403 await checkUploadVideoParam(server, server.accessToken, { ...fields, ...attaches }, HttpStatusCode.BAD_REQUEST_400, mode)
413 }) 404 })
414 405
415 it('Should report the appropriate error', async function () { 406 it('Should report the appropriate error', async function () {
416 const fields = immutableAssign(baseCorrectParams, { language: 'a'.repeat(15) }) 407 const fields = { ...baseCorrectParams, language: 'a'.repeat(15) }
417 const attaches = baseCorrectAttaches 408 const attaches = baseCorrectAttaches
418 409
419 const attributes = { ...fields, ...attaches } 410 const attributes = { ...fields, ...attaches }
420 const res = await checkUploadVideoParam(server.url, server.accessToken, attributes, HttpStatusCode.BAD_REQUEST_400, mode) 411 const body = await checkUploadVideoParam(server, server.accessToken, attributes, HttpStatusCode.BAD_REQUEST_400, mode)
421 412
422 const error = res.body as PeerTubeProblemDocument 413 const error = body as unknown as PeerTubeProblemDocument
423 414
424 if (mode === 'legacy') { 415 if (mode === 'legacy') {
425 expect(error.docs).to.equal('https://docs.joinpeertube.org/api-rest-reference.html#operation/uploadLegacy') 416 expect(error.docs).to.equal('https://docs.joinpeertube.org/api-rest-reference.html#operation/uploadLegacy')
@@ -444,23 +435,27 @@ describe('Test videos API validator', function () {
444 435
445 { 436 {
446 const attaches = baseCorrectAttaches 437 const attaches = baseCorrectAttaches
447 await checkUploadVideoParam(server.url, server.accessToken, { ...fields, ...attaches }, HttpStatusCode.OK_200, mode) 438 await checkUploadVideoParam(server, server.accessToken, { ...fields, ...attaches }, HttpStatusCode.OK_200, mode)
448 } 439 }
449 440
450 { 441 {
451 const attaches = immutableAssign(baseCorrectAttaches, { 442 const attaches = {
443 ...baseCorrectAttaches,
444
452 videofile: join(root(), 'server', 'tests', 'fixtures', 'video_short.mp4') 445 videofile: join(root(), 'server', 'tests', 'fixtures', 'video_short.mp4')
453 }) 446 }
454 447
455 await checkUploadVideoParam(server.url, server.accessToken, { ...fields, ...attaches }, HttpStatusCode.OK_200, mode) 448 await checkUploadVideoParam(server, server.accessToken, { ...fields, ...attaches }, HttpStatusCode.OK_200, mode)
456 } 449 }
457 450
458 { 451 {
459 const attaches = immutableAssign(baseCorrectAttaches, { 452 const attaches = {
453 ...baseCorrectAttaches,
454
460 videofile: join(root(), 'server', 'tests', 'fixtures', 'video_short.ogv') 455 videofile: join(root(), 'server', 'tests', 'fixtures', 'video_short.ogv')
461 }) 456 }
462 457
463 await checkUploadVideoParam(server.url, server.accessToken, { ...fields, ...attaches }, HttpStatusCode.OK_200, mode) 458 await checkUploadVideoParam(server, server.accessToken, { ...fields, ...attaches }, HttpStatusCode.OK_200, mode)
464 } 459 }
465 }) 460 })
466 } 461 }
@@ -489,8 +484,8 @@ describe('Test videos API validator', function () {
489 } 484 }
490 485
491 before(async function () { 486 before(async function () {
492 const res = await getVideosList(server.url) 487 const { data } = await server.videos.list()
493 video = res.body.data[0] 488 video = data[0]
494 }) 489 })
495 490
496 it('Should fail with nothing', async function () { 491 it('Should fail with nothing', async function () {
@@ -511,84 +506,84 @@ describe('Test videos API validator', function () {
511 path: path + '4da6fde3-88f7-4d16-b119-108df5630b06', 506 path: path + '4da6fde3-88f7-4d16-b119-108df5630b06',
512 token: server.accessToken, 507 token: server.accessToken,
513 fields, 508 fields,
514 statusCodeExpected: HttpStatusCode.NOT_FOUND_404 509 expectedStatus: HttpStatusCode.NOT_FOUND_404
515 }) 510 })
516 }) 511 })
517 512
518 it('Should fail with a long name', async function () { 513 it('Should fail with a long name', async function () {
519 const fields = immutableAssign(baseCorrectParams, { name: 'super'.repeat(65) }) 514 const fields = { ...baseCorrectParams, name: 'super'.repeat(65) }
520 515
521 await makePutBodyRequest({ url: server.url, path: path + video.shortUUID, token: server.accessToken, fields }) 516 await makePutBodyRequest({ url: server.url, path: path + video.shortUUID, token: server.accessToken, fields })
522 }) 517 })
523 518
524 it('Should fail with a bad category', async function () { 519 it('Should fail with a bad category', async function () {
525 const fields = immutableAssign(baseCorrectParams, { category: 125 }) 520 const fields = { ...baseCorrectParams, category: 125 }
526 521
527 await makePutBodyRequest({ url: server.url, path: path + video.shortUUID, token: server.accessToken, fields }) 522 await makePutBodyRequest({ url: server.url, path: path + video.shortUUID, token: server.accessToken, fields })
528 }) 523 })
529 524
530 it('Should fail with a bad licence', async function () { 525 it('Should fail with a bad licence', async function () {
531 const fields = immutableAssign(baseCorrectParams, { licence: 125 }) 526 const fields = { ...baseCorrectParams, licence: 125 }
532 527
533 await makePutBodyRequest({ url: server.url, path: path + video.shortUUID, token: server.accessToken, fields }) 528 await makePutBodyRequest({ url: server.url, path: path + video.shortUUID, token: server.accessToken, fields })
534 }) 529 })
535 530
536 it('Should fail with a bad language', async function () { 531 it('Should fail with a bad language', async function () {
537 const fields = immutableAssign(baseCorrectParams, { language: 'a'.repeat(15) }) 532 const fields = { ...baseCorrectParams, language: 'a'.repeat(15) }
538 533
539 await makePutBodyRequest({ url: server.url, path: path + video.shortUUID, token: server.accessToken, fields }) 534 await makePutBodyRequest({ url: server.url, path: path + video.shortUUID, token: server.accessToken, fields })
540 }) 535 })
541 536
542 it('Should fail with a long description', async function () { 537 it('Should fail with a long description', async function () {
543 const fields = immutableAssign(baseCorrectParams, { description: 'super'.repeat(2500) }) 538 const fields = { ...baseCorrectParams, description: 'super'.repeat(2500) }
544 539
545 await makePutBodyRequest({ url: server.url, path: path + video.shortUUID, token: server.accessToken, fields }) 540 await makePutBodyRequest({ url: server.url, path: path + video.shortUUID, token: server.accessToken, fields })
546 }) 541 })
547 542
548 it('Should fail with a long support text', async function () { 543 it('Should fail with a long support text', async function () {
549 const fields = immutableAssign(baseCorrectParams, { support: 'super'.repeat(201) }) 544 const fields = { ...baseCorrectParams, support: 'super'.repeat(201) }
550 545
551 await makePutBodyRequest({ url: server.url, path: path + video.shortUUID, token: server.accessToken, fields }) 546 await makePutBodyRequest({ url: server.url, path: path + video.shortUUID, token: server.accessToken, fields })
552 }) 547 })
553 548
554 it('Should fail with a bad channel', async function () { 549 it('Should fail with a bad channel', async function () {
555 const fields = immutableAssign(baseCorrectParams, { channelId: 545454 }) 550 const fields = { ...baseCorrectParams, channelId: 545454 }
556 551
557 await makePutBodyRequest({ url: server.url, path: path + video.shortUUID, token: server.accessToken, fields }) 552 await makePutBodyRequest({ url: server.url, path: path + video.shortUUID, token: server.accessToken, fields })
558 }) 553 })
559 554
560 it('Should fail with too many tags', async function () { 555 it('Should fail with too many tags', async function () {
561 const fields = immutableAssign(baseCorrectParams, { tags: [ 'tag1', 'tag2', 'tag3', 'tag4', 'tag5', 'tag6' ] }) 556 const fields = { ...baseCorrectParams, tags: [ 'tag1', 'tag2', 'tag3', 'tag4', 'tag5', 'tag6' ] }
562 557
563 await makePutBodyRequest({ url: server.url, path: path + video.shortUUID, token: server.accessToken, fields }) 558 await makePutBodyRequest({ url: server.url, path: path + video.shortUUID, token: server.accessToken, fields })
564 }) 559 })
565 560
566 it('Should fail with a tag length too low', async function () { 561 it('Should fail with a tag length too low', async function () {
567 const fields = immutableAssign(baseCorrectParams, { tags: [ 'tag1', 't' ] }) 562 const fields = { ...baseCorrectParams, tags: [ 'tag1', 't' ] }
568 563
569 await makePutBodyRequest({ url: server.url, path: path + video.shortUUID, token: server.accessToken, fields }) 564 await makePutBodyRequest({ url: server.url, path: path + video.shortUUID, token: server.accessToken, fields })
570 }) 565 })
571 566
572 it('Should fail with a tag length too big', async function () { 567 it('Should fail with a tag length too big', async function () {
573 const fields = immutableAssign(baseCorrectParams, { tags: [ 'tag1', 'my_super_tag_too_long_long_long_long_long_long' ] }) 568 const fields = { ...baseCorrectParams, tags: [ 'tag1', 'my_super_tag_too_long_long_long_long_long_long' ] }
574 569
575 await makePutBodyRequest({ url: server.url, path: path + video.shortUUID, token: server.accessToken, fields }) 570 await makePutBodyRequest({ url: server.url, path: path + video.shortUUID, token: server.accessToken, fields })
576 }) 571 })
577 572
578 it('Should fail with a bad schedule update (miss updateAt)', async function () { 573 it('Should fail with a bad schedule update (miss updateAt)', async function () {
579 const fields = immutableAssign(baseCorrectParams, { scheduleUpdate: { privacy: VideoPrivacy.PUBLIC } }) 574 const fields = { ...baseCorrectParams, scheduleUpdate: { privacy: VideoPrivacy.PUBLIC } }
580 575
581 await makePutBodyRequest({ url: server.url, path: path + video.shortUUID, token: server.accessToken, fields }) 576 await makePutBodyRequest({ url: server.url, path: path + video.shortUUID, token: server.accessToken, fields })
582 }) 577 })
583 578
584 it('Should fail with a bad schedule update (wrong updateAt)', async function () { 579 it('Should fail with a bad schedule update (wrong updateAt)', async function () {
585 const fields = immutableAssign(baseCorrectParams, { scheduleUpdate: { updateAt: 'toto', privacy: VideoPrivacy.PUBLIC } }) 580 const fields = { ...baseCorrectParams, scheduleUpdate: { updateAt: 'toto', privacy: VideoPrivacy.PUBLIC } }
586 581
587 await makePutBodyRequest({ url: server.url, path: path + video.shortUUID, token: server.accessToken, fields }) 582 await makePutBodyRequest({ url: server.url, path: path + video.shortUUID, token: server.accessToken, fields })
588 }) 583 })
589 584
590 it('Should fail with a bad originally published at param', async function () { 585 it('Should fail with a bad originally published at param', async function () {
591 const fields = immutableAssign(baseCorrectParams, { originallyPublishedAt: 'toto' }) 586 const fields = { ...baseCorrectParams, originallyPublishedAt: 'toto' }
592 587
593 await makePutBodyRequest({ url: server.url, path: path + video.shortUUID, token: server.accessToken, fields }) 588 await makePutBodyRequest({ url: server.url, path: path + video.shortUUID, token: server.accessToken, fields })
594 }) 589 })
@@ -665,14 +660,14 @@ describe('Test videos API validator', function () {
665 path: path + video.shortUUID, 660 path: path + video.shortUUID,
666 token: userAccessToken, 661 token: userAccessToken,
667 fields, 662 fields,
668 statusCodeExpected: HttpStatusCode.FORBIDDEN_403 663 expectedStatus: HttpStatusCode.FORBIDDEN_403
669 }) 664 })
670 }) 665 })
671 666
672 it('Should fail with a video of another server') 667 it('Should fail with a video of another server')
673 668
674 it('Shoud report the appropriate error', async function () { 669 it('Shoud report the appropriate error', async function () {
675 const fields = immutableAssign(baseCorrectParams, { licence: 125 }) 670 const fields = { ...baseCorrectParams, licence: 125 }
676 671
677 const res = await makePutBodyRequest({ url: server.url, path: path + video.shortUUID, token: server.accessToken, fields }) 672 const res = await makePutBodyRequest({ url: server.url, path: path + video.shortUUID, token: server.accessToken, fields })
678 const error = res.body as PeerTubeProblemDocument 673 const error = res.body as PeerTubeProblemDocument
@@ -697,7 +692,7 @@ describe('Test videos API validator', function () {
697 path: path + video.shortUUID, 692 path: path + video.shortUUID,
698 token: server.accessToken, 693 token: server.accessToken,
699 fields, 694 fields,
700 statusCodeExpected: HttpStatusCode.NO_CONTENT_204 695 expectedStatus: HttpStatusCode.NO_CONTENT_204
701 }) 696 })
702 }) 697 })
703 }) 698 })
@@ -707,7 +702,7 @@ describe('Test videos API validator', function () {
707 const res = await makeGetRequest({ 702 const res = await makeGetRequest({
708 url: server.url, 703 url: server.url,
709 path, 704 path,
710 statusCodeExpected: HttpStatusCode.OK_200 705 expectedStatus: HttpStatusCode.OK_200
711 }) 706 })
712 707
713 expect(res.body.data).to.be.an('array') 708 expect(res.body.data).to.be.an('array')
@@ -715,16 +710,16 @@ describe('Test videos API validator', function () {
715 }) 710 })
716 711
717 it('Should fail without a correct uuid', async function () { 712 it('Should fail without a correct uuid', async function () {
718 await getVideo(server.url, 'coucou', HttpStatusCode.BAD_REQUEST_400) 713 await server.videos.get({ id: 'coucou', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
719 }) 714 })
720 715
721 it('Should return 404 with an incorrect video', async function () { 716 it('Should return 404 with an incorrect video', async function () {
722 await getVideo(server.url, '4da6fde3-88f7-4d16-b119-108df5630b06', HttpStatusCode.NOT_FOUND_404) 717 await server.videos.get({ id: '4da6fde3-88f7-4d16-b119-108df5630b06', expectedStatus: HttpStatusCode.NOT_FOUND_404 })
723 }) 718 })
724 719
725 it('Shoud report the appropriate error', async function () { 720 it('Shoud report the appropriate error', async function () {
726 const res = await getVideo(server.url, 'hi', HttpStatusCode.BAD_REQUEST_400) 721 const body = await server.videos.get({ id: 'hi', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
727 const error = res.body as PeerTubeProblemDocument 722 const error = body as unknown as PeerTubeProblemDocument
728 723
729 expect(error.docs).to.equal('https://docs.joinpeertube.org/api-rest-reference.html#operation/getVideo') 724 expect(error.docs).to.equal('https://docs.joinpeertube.org/api-rest-reference.html#operation/getVideo')
730 725
@@ -739,16 +734,16 @@ describe('Test videos API validator', function () {
739 }) 734 })
740 735
741 it('Should succeed with the correct parameters', async function () { 736 it('Should succeed with the correct parameters', async function () {
742 await getVideo(server.url, video.shortUUID) 737 await server.videos.get({ id: video.shortUUID })
743 }) 738 })
744 }) 739 })
745 740
746 describe('When rating a video', function () { 741 describe('When rating a video', function () {
747 let videoId 742 let videoId: number
748 743
749 before(async function () { 744 before(async function () {
750 const res = await getVideosList(server.url) 745 const { data } = await server.videos.list()
751 videoId = res.body.data[0].id 746 videoId = data[0].id
752 }) 747 })
753 748
754 it('Should fail without a valid uuid', async function () { 749 it('Should fail without a valid uuid', async function () {
@@ -767,7 +762,7 @@ describe('Test videos API validator', function () {
767 path: path + '4da6fde3-88f7-4d16-b119-108df5630b06/rate', 762 path: path + '4da6fde3-88f7-4d16-b119-108df5630b06/rate',
768 token: server.accessToken, 763 token: server.accessToken,
769 fields, 764 fields,
770 statusCodeExpected: HttpStatusCode.NOT_FOUND_404 765 expectedStatus: HttpStatusCode.NOT_FOUND_404
771 }) 766 })
772 }) 767 })
773 768
@@ -787,7 +782,7 @@ describe('Test videos API validator', function () {
787 path: path + videoId + '/rate', 782 path: path + videoId + '/rate',
788 token: server.accessToken, 783 token: server.accessToken,
789 fields, 784 fields,
790 statusCodeExpected: HttpStatusCode.NO_CONTENT_204 785 expectedStatus: HttpStatusCode.NO_CONTENT_204
791 }) 786 })
792 }) 787 })
793 }) 788 })
@@ -797,27 +792,27 @@ describe('Test videos API validator', function () {
797 await makeDeleteRequest({ 792 await makeDeleteRequest({
798 url: server.url, 793 url: server.url,
799 path, 794 path,
800 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 795 expectedStatus: HttpStatusCode.BAD_REQUEST_400
801 }) 796 })
802 }) 797 })
803 798
804 it('Should fail without a correct uuid', async function () { 799 it('Should fail without a correct uuid', async function () {
805 await removeVideo(server.url, server.accessToken, 'hello', HttpStatusCode.BAD_REQUEST_400) 800 await server.videos.remove({ id: 'hello', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
806 }) 801 })
807 802
808 it('Should fail with a video which does not exist', async function () { 803 it('Should fail with a video which does not exist', async function () {
809 await removeVideo(server.url, server.accessToken, '4da6fde3-88f7-4d16-b119-108df5630b06', HttpStatusCode.NOT_FOUND_404) 804 await server.videos.remove({ id: '4da6fde3-88f7-4d16-b119-108df5630b06', expectedStatus: HttpStatusCode.NOT_FOUND_404 })
810 }) 805 })
811 806
812 it('Should fail with a video of another user without the appropriate right', async function () { 807 it('Should fail with a video of another user without the appropriate right', async function () {
813 await removeVideo(server.url, userAccessToken, video.uuid, HttpStatusCode.FORBIDDEN_403) 808 await server.videos.remove({ token: userAccessToken, id: video.uuid, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
814 }) 809 })
815 810
816 it('Should fail with a video of another server') 811 it('Should fail with a video of another server')
817 812
818 it('Shoud report the appropriate error', async function () { 813 it('Shoud report the appropriate error', async function () {
819 const res = await removeVideo(server.url, server.accessToken, 'hello', HttpStatusCode.BAD_REQUEST_400) 814 const body = await server.videos.remove({ id: 'hello', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
820 const error = res.body as PeerTubeProblemDocument 815 const error = body as PeerTubeProblemDocument
821 816
822 expect(error.docs).to.equal('https://docs.joinpeertube.org/api-rest-reference.html#operation/delVideo') 817 expect(error.docs).to.equal('https://docs.joinpeertube.org/api-rest-reference.html#operation/delVideo')
823 818
@@ -832,7 +827,7 @@ describe('Test videos API validator', function () {
832 }) 827 })
833 828
834 it('Should succeed with the correct parameters', async function () { 829 it('Should succeed with the correct parameters', async function () {
835 await removeVideo(server.url, server.accessToken, video.uuid) 830 await server.videos.remove({ id: video.uuid })
836 }) 831 })
837 }) 832 })
838 833
diff --git a/server/tests/api/live/live-constraints.ts b/server/tests/api/live/live-constraints.ts
index cc635de33..20346113d 100644
--- a/server/tests/api/live/live-constraints.ts
+++ b/server/tests/api/live/live-constraints.ts
@@ -2,31 +2,24 @@
2 2
3import 'mocha' 3import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5import { VideoDetails, VideoPrivacy } from '@shared/models' 5import { VideoPrivacy } from '@shared/models'
6import { 6import {
7 checkLiveCleanup, 7 checkLiveCleanup,
8 cleanupTests, 8 cleanupTests,
9 createLive, 9 ConfigCommand,
10 createMultipleServers,
10 doubleFollow, 11 doubleFollow,
11 flushAndRunMultipleServers, 12 PeerTubeServer,
12 generateUser,
13 getCustomConfigResolutions,
14 getVideo,
15 runAndTestFfmpegStreamError,
16 ServerInfo,
17 setAccessTokensToServers, 13 setAccessTokensToServers,
18 setDefaultVideoChannel, 14 setDefaultVideoChannel,
19 updateCustomSubConfig,
20 updateUser,
21 wait, 15 wait,
22 waitJobs, 16 waitJobs
23 waitUntilLivePublished
24} from '../../../../shared/extra-utils' 17} from '../../../../shared/extra-utils'
25 18
26const expect = chai.expect 19const expect = chai.expect
27 20
28describe('Test live constraints', function () { 21describe('Test live constraints', function () {
29 let servers: ServerInfo[] = [] 22 let servers: PeerTubeServer[] = []
30 let userId: number 23 let userId: number
31 let userAccessToken: string 24 let userAccessToken: string
32 let userChannelId: number 25 let userChannelId: number
@@ -39,15 +32,13 @@ describe('Test live constraints', function () {
39 saveReplay 32 saveReplay
40 } 33 }
41 34
42 const res = await createLive(servers[0].url, userAccessToken, liveAttributes) 35 const { uuid } = await servers[0].live.create({ token: userAccessToken, fields: liveAttributes })
43 return res.body.video.uuid as string 36 return uuid
44 } 37 }
45 38
46 async function checkSaveReplay (videoId: string, resolutions = [ 720 ]) { 39 async function checkSaveReplay (videoId: string, resolutions = [ 720 ]) {
47 for (const server of servers) { 40 for (const server of servers) {
48 const res = await getVideo(server.url, videoId) 41 const video = await server.videos.get({ id: videoId })
49
50 const video: VideoDetails = res.body
51 expect(video.isLive).to.be.false 42 expect(video.isLive).to.be.false
52 expect(video.duration).to.be.greaterThan(0) 43 expect(video.duration).to.be.greaterThan(0)
53 } 44 }
@@ -57,14 +48,12 @@ describe('Test live constraints', function () {
57 48
58 async function waitUntilLivePublishedOnAllServers (videoId: string) { 49 async function waitUntilLivePublishedOnAllServers (videoId: string) {
59 for (const server of servers) { 50 for (const server of servers) {
60 await waitUntilLivePublished(server.url, server.accessToken, videoId) 51 await server.live.waitUntilPublished({ videoId })
61 } 52 }
62 } 53 }
63 54
64 function updateQuota (options: { total: number, daily: number }) { 55 function updateQuota (options: { total: number, daily: number }) {
65 return updateUser({ 56 return servers[0].users.update({
66 url: servers[0].url,
67 accessToken: servers[0].accessToken,
68 userId, 57 userId,
69 videoQuota: options.total, 58 videoQuota: options.total,
70 videoQuotaDaily: options.daily 59 videoQuotaDaily: options.daily
@@ -74,24 +63,26 @@ describe('Test live constraints', function () {
74 before(async function () { 63 before(async function () {
75 this.timeout(120000) 64 this.timeout(120000)
76 65
77 servers = await flushAndRunMultipleServers(2) 66 servers = await createMultipleServers(2)
78 67
79 // Get the access tokens 68 // Get the access tokens
80 await setAccessTokensToServers(servers) 69 await setAccessTokensToServers(servers)
81 await setDefaultVideoChannel(servers) 70 await setDefaultVideoChannel(servers)
82 71
83 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, { 72 await servers[0].config.updateCustomSubConfig({
84 live: { 73 newConfig: {
85 enabled: true, 74 live: {
86 allowReplay: true, 75 enabled: true,
87 transcoding: { 76 allowReplay: true,
88 enabled: false 77 transcoding: {
78 enabled: false
79 }
89 } 80 }
90 } 81 }
91 }) 82 })
92 83
93 { 84 {
94 const res = await generateUser(servers[0], 'user1') 85 const res = await servers[0].users.generate('user1')
95 userId = res.userId 86 userId = res.userId
96 userChannelId = res.userChannelId 87 userChannelId = res.userChannelId
97 userAccessToken = res.token 88 userAccessToken = res.token
@@ -107,7 +98,7 @@ describe('Test live constraints', function () {
107 this.timeout(60000) 98 this.timeout(60000)
108 99
109 const userVideoLiveoId = await createLiveWrapper(false) 100 const userVideoLiveoId = await createLiveWrapper(false)
110 await runAndTestFfmpegStreamError(servers[0].url, userAccessToken, userVideoLiveoId, false) 101 await servers[0].live.runAndTestStreamError({ token: userAccessToken, videoId: userVideoLiveoId, shouldHaveError: false })
111 }) 102 })
112 103
113 it('Should have size limit depending on user global quota if save replay is enabled', async function () { 104 it('Should have size limit depending on user global quota if save replay is enabled', async function () {
@@ -117,7 +108,7 @@ describe('Test live constraints', function () {
117 await wait(5000) 108 await wait(5000)
118 109
119 const userVideoLiveoId = await createLiveWrapper(true) 110 const userVideoLiveoId = await createLiveWrapper(true)
120 await runAndTestFfmpegStreamError(servers[0].url, userAccessToken, userVideoLiveoId, true) 111 await servers[0].live.runAndTestStreamError({ token: userAccessToken, videoId: userVideoLiveoId, shouldHaveError: true })
121 112
122 await waitUntilLivePublishedOnAllServers(userVideoLiveoId) 113 await waitUntilLivePublishedOnAllServers(userVideoLiveoId)
123 await waitJobs(servers) 114 await waitJobs(servers)
@@ -134,7 +125,7 @@ describe('Test live constraints', function () {
134 await updateQuota({ total: -1, daily: 1 }) 125 await updateQuota({ total: -1, daily: 1 })
135 126
136 const userVideoLiveoId = await createLiveWrapper(true) 127 const userVideoLiveoId = await createLiveWrapper(true)
137 await runAndTestFfmpegStreamError(servers[0].url, userAccessToken, userVideoLiveoId, true) 128 await servers[0].live.runAndTestStreamError({ token: userAccessToken, videoId: userVideoLiveoId, shouldHaveError: true })
138 129
139 await waitUntilLivePublishedOnAllServers(userVideoLiveoId) 130 await waitUntilLivePublishedOnAllServers(userVideoLiveoId)
140 await waitJobs(servers) 131 await waitJobs(servers)
@@ -151,26 +142,28 @@ describe('Test live constraints', function () {
151 await updateQuota({ total: 10 * 1000 * 1000, daily: -1 }) 142 await updateQuota({ total: 10 * 1000 * 1000, daily: -1 })
152 143
153 const userVideoLiveoId = await createLiveWrapper(true) 144 const userVideoLiveoId = await createLiveWrapper(true)
154 await runAndTestFfmpegStreamError(servers[0].url, userAccessToken, userVideoLiveoId, false) 145 await servers[0].live.runAndTestStreamError({ token: userAccessToken, videoId: userVideoLiveoId, shouldHaveError: false })
155 }) 146 })
156 147
157 it('Should have max duration limit', async function () { 148 it('Should have max duration limit', async function () {
158 this.timeout(60000) 149 this.timeout(60000)
159 150
160 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, { 151 await servers[0].config.updateCustomSubConfig({
161 live: { 152 newConfig: {
162 enabled: true, 153 live: {
163 allowReplay: true,
164 maxDuration: 1,
165 transcoding: {
166 enabled: true, 154 enabled: true,
167 resolutions: getCustomConfigResolutions(true) 155 allowReplay: true,
156 maxDuration: 1,
157 transcoding: {
158 enabled: true,
159 resolutions: ConfigCommand.getCustomConfigResolutions(true)
160 }
168 } 161 }
169 } 162 }
170 }) 163 })
171 164
172 const userVideoLiveoId = await createLiveWrapper(true) 165 const userVideoLiveoId = await createLiveWrapper(true)
173 await runAndTestFfmpegStreamError(servers[0].url, userAccessToken, userVideoLiveoId, true) 166 await servers[0].live.runAndTestStreamError({ token: userAccessToken, videoId: userVideoLiveoId, shouldHaveError: true })
174 167
175 await waitUntilLivePublishedOnAllServers(userVideoLiveoId) 168 await waitUntilLivePublishedOnAllServers(userVideoLiveoId)
176 await waitJobs(servers) 169 await waitJobs(servers)
diff --git a/server/tests/api/live/live-permanent.ts b/server/tests/api/live/live-permanent.ts
index 71b7d28a8..f07d4cfec 100644
--- a/server/tests/api/live/live-permanent.ts
+++ b/server/tests/api/live/live-permanent.ts
@@ -2,59 +2,50 @@
2 2
3import 'mocha' 3import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5import { LiveVideoCreate, VideoDetails, VideoPrivacy, VideoState } from '@shared/models' 5import { LiveVideoCreate, VideoPrivacy, VideoState } from '@shared/models'
6import { 6import {
7 cleanupTests, 7 cleanupTests,
8 createLive, 8 ConfigCommand,
9 createMultipleServers,
9 doubleFollow, 10 doubleFollow,
10 flushAndRunMultipleServers, 11 PeerTubeServer,
11 getCustomConfigResolutions,
12 getLive,
13 getPlaylistsCount,
14 getVideo,
15 sendRTMPStreamInVideo,
16 ServerInfo,
17 setAccessTokensToServers, 12 setAccessTokensToServers,
18 setDefaultVideoChannel, 13 setDefaultVideoChannel,
19 stopFfmpeg, 14 stopFfmpeg,
20 updateCustomSubConfig,
21 updateLive,
22 wait, 15 wait,
23 waitJobs, 16 waitJobs
24 waitUntilLivePublished,
25 waitUntilLiveWaiting
26} from '../../../../shared/extra-utils' 17} from '../../../../shared/extra-utils'
27 18
28const expect = chai.expect 19const expect = chai.expect
29 20
30describe('Permanent live', function () { 21describe('Permanent live', function () {
31 let servers: ServerInfo[] = [] 22 let servers: PeerTubeServer[] = []
32 let videoUUID: string 23 let videoUUID: string
33 24
34 async function createLiveWrapper (permanentLive: boolean) { 25 async function createLiveWrapper (permanentLive: boolean) {
35 const attributes: LiveVideoCreate = { 26 const attributes: LiveVideoCreate = {
36 channelId: servers[0].videoChannel.id, 27 channelId: servers[0].store.channel.id,
37 privacy: VideoPrivacy.PUBLIC, 28 privacy: VideoPrivacy.PUBLIC,
38 name: 'my super live', 29 name: 'my super live',
39 saveReplay: false, 30 saveReplay: false,
40 permanentLive 31 permanentLive
41 } 32 }
42 33
43 const res = await createLive(servers[0].url, servers[0].accessToken, attributes) 34 const { uuid } = await servers[0].live.create({ fields: attributes })
44 return res.body.video.uuid 35 return uuid
45 } 36 }
46 37
47 async function checkVideoState (videoId: string, state: VideoState) { 38 async function checkVideoState (videoId: string, state: VideoState) {
48 for (const server of servers) { 39 for (const server of servers) {
49 const res = await getVideo(server.url, videoId) 40 const video = await server.videos.get({ id: videoId })
50 expect((res.body as VideoDetails).state.id).to.equal(state) 41 expect(video.state.id).to.equal(state)
51 } 42 }
52 } 43 }
53 44
54 before(async function () { 45 before(async function () {
55 this.timeout(120000) 46 this.timeout(120000)
56 47
57 servers = await flushAndRunMultipleServers(2) 48 servers = await createMultipleServers(2)
58 49
59 // Get the access tokens 50 // Get the access tokens
60 await setAccessTokensToServers(servers) 51 await setAccessTokensToServers(servers)
@@ -63,14 +54,16 @@ describe('Permanent live', function () {
63 // Server 1 and server 2 follow each other 54 // Server 1 and server 2 follow each other
64 await doubleFollow(servers[0], servers[1]) 55 await doubleFollow(servers[0], servers[1])
65 56
66 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, { 57 await servers[0].config.updateCustomSubConfig({
67 live: { 58 newConfig: {
68 enabled: true, 59 live: {
69 allowReplay: true,
70 maxDuration: -1,
71 transcoding: {
72 enabled: true, 60 enabled: true,
73 resolutions: getCustomConfigResolutions(true) 61 allowReplay: true,
62 maxDuration: -1,
63 transcoding: {
64 enabled: true,
65 resolutions: ConfigCommand.getCustomConfigResolutions(true)
66 }
74 } 67 }
75 } 68 }
76 }) 69 })
@@ -82,15 +75,15 @@ describe('Permanent live', function () {
82 const videoUUID = await createLiveWrapper(false) 75 const videoUUID = await createLiveWrapper(false)
83 76
84 { 77 {
85 const res = await getLive(servers[0].url, servers[0].accessToken, videoUUID) 78 const live = await servers[0].live.get({ videoId: videoUUID })
86 expect(res.body.permanentLive).to.be.false 79 expect(live.permanentLive).to.be.false
87 } 80 }
88 81
89 await updateLive(servers[0].url, servers[0].accessToken, videoUUID, { permanentLive: true }) 82 await servers[0].live.update({ videoId: videoUUID, fields: { permanentLive: true } })
90 83
91 { 84 {
92 const res = await getLive(servers[0].url, servers[0].accessToken, videoUUID) 85 const live = await servers[0].live.get({ videoId: videoUUID })
93 expect(res.body.permanentLive).to.be.true 86 expect(live.permanentLive).to.be.true
94 } 87 }
95 }) 88 })
96 89
@@ -99,8 +92,8 @@ describe('Permanent live', function () {
99 92
100 videoUUID = await createLiveWrapper(true) 93 videoUUID = await createLiveWrapper(true)
101 94
102 const res = await getLive(servers[0].url, servers[0].accessToken, videoUUID) 95 const live = await servers[0].live.get({ videoId: videoUUID })
103 expect(res.body.permanentLive).to.be.true 96 expect(live.permanentLive).to.be.true
104 97
105 await waitJobs(servers) 98 await waitJobs(servers)
106 }) 99 })
@@ -108,16 +101,16 @@ describe('Permanent live', function () {
108 it('Should stream into this permanent live', async function () { 101 it('Should stream into this permanent live', async function () {
109 this.timeout(120000) 102 this.timeout(120000)
110 103
111 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, videoUUID) 104 const ffmpegCommand = await servers[0].live.sendRTMPStreamInVideo({ videoId: videoUUID })
112 105
113 for (const server of servers) { 106 for (const server of servers) {
114 await waitUntilLivePublished(server.url, server.accessToken, videoUUID) 107 await server.live.waitUntilPublished({ videoId: videoUUID })
115 } 108 }
116 109
117 await checkVideoState(videoUUID, VideoState.PUBLISHED) 110 await checkVideoState(videoUUID, VideoState.PUBLISHED)
118 111
119 await stopFfmpeg(command) 112 await stopFfmpeg(ffmpegCommand)
120 await waitUntilLiveWaiting(servers[0].url, servers[0].accessToken, videoUUID) 113 await servers[0].live.waitUntilWaiting({ videoId: videoUUID })
121 114
122 await waitJobs(servers) 115 await waitJobs(servers)
123 }) 116 })
@@ -129,9 +122,7 @@ describe('Permanent live', function () {
129 await waitJobs(servers) 122 await waitJobs(servers)
130 123
131 for (const server of servers) { 124 for (const server of servers) {
132 const res = await getVideo(server.url, videoUUID) 125 const videoDetails = await server.videos.get({ id: videoUUID })
133
134 const videoDetails = res.body as VideoDetails
135 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1) 126 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
136 } 127 }
137 }) 128 })
@@ -145,31 +136,33 @@ describe('Permanent live', function () {
145 it('Should be able to stream again in the permanent live', async function () { 136 it('Should be able to stream again in the permanent live', async function () {
146 this.timeout(20000) 137 this.timeout(20000)
147 138
148 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, { 139 await servers[0].config.updateCustomSubConfig({
149 live: { 140 newConfig: {
150 enabled: true, 141 live: {
151 allowReplay: true,
152 maxDuration: -1,
153 transcoding: {
154 enabled: true, 142 enabled: true,
155 resolutions: getCustomConfigResolutions(false) 143 allowReplay: true,
144 maxDuration: -1,
145 transcoding: {
146 enabled: true,
147 resolutions: ConfigCommand.getCustomConfigResolutions(false)
148 }
156 } 149 }
157 } 150 }
158 }) 151 })
159 152
160 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, videoUUID) 153 const ffmpegCommand = await servers[0].live.sendRTMPStreamInVideo({ videoId: videoUUID })
161 154
162 for (const server of servers) { 155 for (const server of servers) {
163 await waitUntilLivePublished(server.url, server.accessToken, videoUUID) 156 await server.live.waitUntilPublished({ videoId: videoUUID })
164 } 157 }
165 158
166 await checkVideoState(videoUUID, VideoState.PUBLISHED) 159 await checkVideoState(videoUUID, VideoState.PUBLISHED)
167 160
168 const count = await getPlaylistsCount(servers[0], videoUUID) 161 const count = await servers[0].live.countPlaylists({ videoUUID })
169 // master playlist and 720p playlist 162 // master playlist and 720p playlist
170 expect(count).to.equal(2) 163 expect(count).to.equal(2)
171 164
172 await stopFfmpeg(command) 165 await stopFfmpeg(ffmpegCommand)
173 }) 166 })
174 167
175 after(async function () { 168 after(async function () {
diff --git a/server/tests/api/live/live-save-replay.ts b/server/tests/api/live/live-save-replay.ts
index 3d4736c8f..bd15396ec 100644
--- a/server/tests/api/live/live-save-replay.ts
+++ b/server/tests/api/live/live-save-replay.ts
@@ -3,97 +3,85 @@
3import 'mocha' 3import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5import { FfmpegCommand } from 'fluent-ffmpeg' 5import { FfmpegCommand } from 'fluent-ffmpeg'
6import { LiveVideoCreate, VideoDetails, VideoPrivacy, VideoState } from '@shared/models'
7import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
8import { 6import {
9 addVideoToBlacklist,
10 checkLiveCleanup, 7 checkLiveCleanup,
11 cleanupTests, 8 cleanupTests,
12 createLive, 9 ConfigCommand,
10 createMultipleServers,
13 doubleFollow, 11 doubleFollow,
14 flushAndRunMultipleServers, 12 PeerTubeServer,
15 getCustomConfigResolutions,
16 getVideo,
17 getVideosList,
18 removeVideo,
19 sendRTMPStreamInVideo,
20 ServerInfo,
21 setAccessTokensToServers, 13 setAccessTokensToServers,
22 setDefaultVideoChannel, 14 setDefaultVideoChannel,
23 stopFfmpeg, 15 stopFfmpeg,
24 testFfmpegStreamError, 16 testFfmpegStreamError,
25 updateCustomSubConfig,
26 updateVideo,
27 wait, 17 wait,
28 waitJobs, 18 waitJobs
29 waitUntilLiveEnded, 19} from '@shared/extra-utils'
30 waitUntilLivePublished, 20import { HttpStatusCode, LiveVideoCreate, VideoPrivacy, VideoState } from '@shared/models'
31 waitUntilLiveSaved
32} from '../../../../shared/extra-utils'
33 21
34const expect = chai.expect 22const expect = chai.expect
35 23
36describe('Save replay setting', function () { 24describe('Save replay setting', function () {
37 let servers: ServerInfo[] = [] 25 let servers: PeerTubeServer[] = []
38 let liveVideoUUID: string 26 let liveVideoUUID: string
39 let ffmpegCommand: FfmpegCommand 27 let ffmpegCommand: FfmpegCommand
40 28
41 async function createLiveWrapper (saveReplay: boolean) { 29 async function createLiveWrapper (saveReplay: boolean) {
42 if (liveVideoUUID) { 30 if (liveVideoUUID) {
43 try { 31 try {
44 await removeVideo(servers[0].url, servers[0].accessToken, liveVideoUUID) 32 await servers[0].videos.remove({ id: liveVideoUUID })
45 await waitJobs(servers) 33 await waitJobs(servers)
46 } catch {} 34 } catch {}
47 } 35 }
48 36
49 const attributes: LiveVideoCreate = { 37 const attributes: LiveVideoCreate = {
50 channelId: servers[0].videoChannel.id, 38 channelId: servers[0].store.channel.id,
51 privacy: VideoPrivacy.PUBLIC, 39 privacy: VideoPrivacy.PUBLIC,
52 name: 'my super live', 40 name: 'my super live',
53 saveReplay 41 saveReplay
54 } 42 }
55 43
56 const res = await createLive(servers[0].url, servers[0].accessToken, attributes) 44 const { uuid } = await servers[0].live.create({ fields: attributes })
57 return res.body.video.uuid 45 return uuid
58 } 46 }
59 47
60 async function checkVideosExist (videoId: string, existsInList: boolean, getStatus?: number) { 48 async function checkVideosExist (videoId: string, existsInList: boolean, expectedStatus?: number) {
61 for (const server of servers) { 49 for (const server of servers) {
62 const length = existsInList ? 1 : 0 50 const length = existsInList ? 1 : 0
63 51
64 const resVideos = await getVideosList(server.url) 52 const { data, total } = await server.videos.list()
65 expect(resVideos.body.data).to.have.lengthOf(length) 53 expect(data).to.have.lengthOf(length)
66 expect(resVideos.body.total).to.equal(length) 54 expect(total).to.equal(length)
67 55
68 if (getStatus) { 56 if (expectedStatus) {
69 await getVideo(server.url, videoId, getStatus) 57 await server.videos.get({ id: videoId, expectedStatus })
70 } 58 }
71 } 59 }
72 } 60 }
73 61
74 async function checkVideoState (videoId: string, state: VideoState) { 62 async function checkVideoState (videoId: string, state: VideoState) {
75 for (const server of servers) { 63 for (const server of servers) {
76 const res = await getVideo(server.url, videoId) 64 const video = await server.videos.get({ id: videoId })
77 expect((res.body as VideoDetails).state.id).to.equal(state) 65 expect(video.state.id).to.equal(state)
78 } 66 }
79 } 67 }
80 68
81 async function waitUntilLivePublishedOnAllServers (videoId: string) { 69 async function waitUntilLivePublishedOnAllServers (videoId: string) {
82 for (const server of servers) { 70 for (const server of servers) {
83 await waitUntilLivePublished(server.url, server.accessToken, videoId) 71 await server.live.waitUntilPublished({ videoId })
84 } 72 }
85 } 73 }
86 74
87 async function waitUntilLiveSavedOnAllServers (videoId: string) { 75 async function waitUntilLiveSavedOnAllServers (videoId: string) {
88 for (const server of servers) { 76 for (const server of servers) {
89 await waitUntilLiveSaved(server.url, server.accessToken, videoId) 77 await server.live.waitUntilSaved({ videoId })
90 } 78 }
91 } 79 }
92 80
93 before(async function () { 81 before(async function () {
94 this.timeout(120000) 82 this.timeout(120000)
95 83
96 servers = await flushAndRunMultipleServers(2) 84 servers = await createMultipleServers(2)
97 85
98 // Get the access tokens 86 // Get the access tokens
99 await setAccessTokensToServers(servers) 87 await setAccessTokensToServers(servers)
@@ -102,14 +90,16 @@ describe('Save replay setting', function () {
102 // Server 1 and server 2 follow each other 90 // Server 1 and server 2 follow each other
103 await doubleFollow(servers[0], servers[1]) 91 await doubleFollow(servers[0], servers[1])
104 92
105 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, { 93 await servers[0].config.updateCustomSubConfig({
106 live: { 94 newConfig: {
107 enabled: true, 95 live: {
108 allowReplay: true, 96 enabled: true,
109 maxDuration: -1, 97 allowReplay: true,
110 transcoding: { 98 maxDuration: -1,
111 enabled: false, 99 transcoding: {
112 resolutions: getCustomConfigResolutions(true) 100 enabled: false,
101 resolutions: ConfigCommand.getCustomConfigResolutions(true)
102 }
113 } 103 }
114 } 104 }
115 }) 105 })
@@ -135,7 +125,7 @@ describe('Save replay setting', function () {
135 it('Should correctly have updated the live and federated it when streaming in the live', async function () { 125 it('Should correctly have updated the live and federated it when streaming in the live', async function () {
136 this.timeout(30000) 126 this.timeout(30000)
137 127
138 ffmpegCommand = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID) 128 ffmpegCommand = await servers[0].live.sendRTMPStreamInVideo({ videoId: liveVideoUUID })
139 129
140 await waitUntilLivePublishedOnAllServers(liveVideoUUID) 130 await waitUntilLivePublishedOnAllServers(liveVideoUUID)
141 131
@@ -151,7 +141,7 @@ describe('Save replay setting', function () {
151 await stopFfmpeg(ffmpegCommand) 141 await stopFfmpeg(ffmpegCommand)
152 142
153 for (const server of servers) { 143 for (const server of servers) {
154 await waitUntilLiveEnded(server.url, server.accessToken, liveVideoUUID) 144 await server.live.waitUntilEnded({ videoId: liveVideoUUID })
155 } 145 }
156 await waitJobs(servers) 146 await waitJobs(servers)
157 147
@@ -168,7 +158,7 @@ describe('Save replay setting', function () {
168 158
169 liveVideoUUID = await createLiveWrapper(false) 159 liveVideoUUID = await createLiveWrapper(false)
170 160
171 ffmpegCommand = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID) 161 ffmpegCommand = await servers[0].live.sendRTMPStreamInVideo({ videoId: liveVideoUUID })
172 162
173 await waitUntilLivePublishedOnAllServers(liveVideoUUID) 163 await waitUntilLivePublishedOnAllServers(liveVideoUUID)
174 164
@@ -176,7 +166,7 @@ describe('Save replay setting', function () {
176 await checkVideosExist(liveVideoUUID, true, HttpStatusCode.OK_200) 166 await checkVideosExist(liveVideoUUID, true, HttpStatusCode.OK_200)
177 167
178 await Promise.all([ 168 await Promise.all([
179 addVideoToBlacklist(servers[0].url, servers[0].accessToken, liveVideoUUID, 'bad live', true), 169 servers[0].blacklist.add({ videoId: liveVideoUUID, reason: 'bad live', unfederate: true }),
180 testFfmpegStreamError(ffmpegCommand, true) 170 testFfmpegStreamError(ffmpegCommand, true)
181 ]) 171 ])
182 172
@@ -184,8 +174,8 @@ describe('Save replay setting', function () {
184 174
185 await checkVideosExist(liveVideoUUID, false) 175 await checkVideosExist(liveVideoUUID, false)
186 176
187 await getVideo(servers[0].url, liveVideoUUID, HttpStatusCode.UNAUTHORIZED_401) 177 await servers[0].videos.get({ id: liveVideoUUID, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
188 await getVideo(servers[1].url, liveVideoUUID, HttpStatusCode.NOT_FOUND_404) 178 await servers[1].videos.get({ id: liveVideoUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
189 179
190 await wait(5000) 180 await wait(5000)
191 await waitJobs(servers) 181 await waitJobs(servers)
@@ -197,7 +187,7 @@ describe('Save replay setting', function () {
197 187
198 liveVideoUUID = await createLiveWrapper(false) 188 liveVideoUUID = await createLiveWrapper(false)
199 189
200 ffmpegCommand = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID) 190 ffmpegCommand = await servers[0].live.sendRTMPStreamInVideo({ videoId: liveVideoUUID })
201 191
202 await waitUntilLivePublishedOnAllServers(liveVideoUUID) 192 await waitUntilLivePublishedOnAllServers(liveVideoUUID)
203 193
@@ -206,7 +196,7 @@ describe('Save replay setting', function () {
206 196
207 await Promise.all([ 197 await Promise.all([
208 testFfmpegStreamError(ffmpegCommand, true), 198 testFfmpegStreamError(ffmpegCommand, true),
209 removeVideo(servers[0].url, servers[0].accessToken, liveVideoUUID) 199 servers[0].videos.remove({ id: liveVideoUUID })
210 ]) 200 ])
211 201
212 await wait(5000) 202 await wait(5000)
@@ -233,7 +223,7 @@ describe('Save replay setting', function () {
233 it('Should correctly have updated the live and federated it when streaming in the live', async function () { 223 it('Should correctly have updated the live and federated it when streaming in the live', async function () {
234 this.timeout(20000) 224 this.timeout(20000)
235 225
236 ffmpegCommand = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID) 226 ffmpegCommand = await servers[0].live.sendRTMPStreamInVideo({ videoId: liveVideoUUID })
237 await waitUntilLivePublishedOnAllServers(liveVideoUUID) 227 await waitUntilLivePublishedOnAllServers(liveVideoUUID)
238 228
239 await waitJobs(servers) 229 await waitJobs(servers)
@@ -258,13 +248,13 @@ describe('Save replay setting', function () {
258 it('Should update the saved live and correctly federate the updated attributes', async function () { 248 it('Should update the saved live and correctly federate the updated attributes', async function () {
259 this.timeout(30000) 249 this.timeout(30000)
260 250
261 await updateVideo(servers[0].url, servers[0].accessToken, liveVideoUUID, { name: 'video updated' }) 251 await servers[0].videos.update({ id: liveVideoUUID, attributes: { name: 'video updated' } })
262 await waitJobs(servers) 252 await waitJobs(servers)
263 253
264 for (const server of servers) { 254 for (const server of servers) {
265 const res = await getVideo(server.url, liveVideoUUID) 255 const video = await server.videos.get({ id: liveVideoUUID })
266 expect(res.body.name).to.equal('video updated') 256 expect(video.name).to.equal('video updated')
267 expect(res.body.isLive).to.be.false 257 expect(video.isLive).to.be.false
268 } 258 }
269 }) 259 })
270 260
@@ -277,14 +267,14 @@ describe('Save replay setting', function () {
277 267
278 liveVideoUUID = await createLiveWrapper(true) 268 liveVideoUUID = await createLiveWrapper(true)
279 269
280 ffmpegCommand = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID) 270 ffmpegCommand = await servers[0].live.sendRTMPStreamInVideo({ videoId: liveVideoUUID })
281 await waitUntilLivePublishedOnAllServers(liveVideoUUID) 271 await waitUntilLivePublishedOnAllServers(liveVideoUUID)
282 272
283 await waitJobs(servers) 273 await waitJobs(servers)
284 await checkVideosExist(liveVideoUUID, true, HttpStatusCode.OK_200) 274 await checkVideosExist(liveVideoUUID, true, HttpStatusCode.OK_200)
285 275
286 await Promise.all([ 276 await Promise.all([
287 addVideoToBlacklist(servers[0].url, servers[0].accessToken, liveVideoUUID, 'bad live', true), 277 servers[0].blacklist.add({ videoId: liveVideoUUID, reason: 'bad live', unfederate: true }),
288 testFfmpegStreamError(ffmpegCommand, true) 278 testFfmpegStreamError(ffmpegCommand, true)
289 ]) 279 ])
290 280
@@ -292,8 +282,8 @@ describe('Save replay setting', function () {
292 282
293 await checkVideosExist(liveVideoUUID, false) 283 await checkVideosExist(liveVideoUUID, false)
294 284
295 await getVideo(servers[0].url, liveVideoUUID, HttpStatusCode.UNAUTHORIZED_401) 285 await servers[0].videos.get({ id: liveVideoUUID, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
296 await getVideo(servers[1].url, liveVideoUUID, HttpStatusCode.NOT_FOUND_404) 286 await servers[1].videos.get({ id: liveVideoUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
297 287
298 await wait(5000) 288 await wait(5000)
299 await waitJobs(servers) 289 await waitJobs(servers)
@@ -305,14 +295,14 @@ describe('Save replay setting', function () {
305 295
306 liveVideoUUID = await createLiveWrapper(true) 296 liveVideoUUID = await createLiveWrapper(true)
307 297
308 ffmpegCommand = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID) 298 ffmpegCommand = await servers[0].live.sendRTMPStreamInVideo({ videoId: liveVideoUUID })
309 await waitUntilLivePublishedOnAllServers(liveVideoUUID) 299 await waitUntilLivePublishedOnAllServers(liveVideoUUID)
310 300
311 await waitJobs(servers) 301 await waitJobs(servers)
312 await checkVideosExist(liveVideoUUID, true, HttpStatusCode.OK_200) 302 await checkVideosExist(liveVideoUUID, true, HttpStatusCode.OK_200)
313 303
314 await Promise.all([ 304 await Promise.all([
315 removeVideo(servers[0].url, servers[0].accessToken, liveVideoUUID), 305 servers[0].videos.remove({ id: liveVideoUUID }),
316 testFfmpegStreamError(ffmpegCommand, true) 306 testFfmpegStreamError(ffmpegCommand, true)
317 ]) 307 ])
318 308
diff --git a/server/tests/api/live/live-socket-messages.ts b/server/tests/api/live/live-socket-messages.ts
index e00909ade..2a1f9f108 100644
--- a/server/tests/api/live/live-socket-messages.ts
+++ b/server/tests/api/live/live-socket-messages.ts
@@ -2,47 +2,42 @@
2 2
3import 'mocha' 3import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5import { getLiveNotificationSocket } from '@shared/extra-utils/socket/socket-io'
6import { VideoPrivacy, VideoState } from '@shared/models' 5import { VideoPrivacy, VideoState } from '@shared/models'
7import { 6import {
8 cleanupTests, 7 cleanupTests,
9 createLive, 8 createMultipleServers,
10 doubleFollow, 9 doubleFollow,
11 flushAndRunMultipleServers, 10 PeerTubeServer,
12 getVideoIdFromUUID,
13 sendRTMPStreamInVideo,
14 ServerInfo,
15 setAccessTokensToServers, 11 setAccessTokensToServers,
16 setDefaultVideoChannel, 12 setDefaultVideoChannel,
17 stopFfmpeg, 13 stopFfmpeg,
18 updateCustomSubConfig,
19 viewVideo,
20 wait, 14 wait,
21 waitJobs, 15 waitJobs,
22 waitUntilLiveEnded,
23 waitUntilLivePublishedOnAllServers 16 waitUntilLivePublishedOnAllServers
24} from '../../../../shared/extra-utils' 17} from '../../../../shared/extra-utils'
25 18
26const expect = chai.expect 19const expect = chai.expect
27 20
28describe('Test live', function () { 21describe('Test live', function () {
29 let servers: ServerInfo[] = [] 22 let servers: PeerTubeServer[] = []
30 23
31 before(async function () { 24 before(async function () {
32 this.timeout(120000) 25 this.timeout(120000)
33 26
34 servers = await flushAndRunMultipleServers(2) 27 servers = await createMultipleServers(2)
35 28
36 // Get the access tokens 29 // Get the access tokens
37 await setAccessTokensToServers(servers) 30 await setAccessTokensToServers(servers)
38 await setDefaultVideoChannel(servers) 31 await setDefaultVideoChannel(servers)
39 32
40 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, { 33 await servers[0].config.updateCustomSubConfig({
41 live: { 34 newConfig: {
42 enabled: true, 35 live: {
43 allowReplay: true, 36 enabled: true,
44 transcoding: { 37 allowReplay: true,
45 enabled: false 38 transcoding: {
39 enabled: false
40 }
46 } 41 }
47 } 42 }
48 }) 43 })
@@ -56,12 +51,12 @@ describe('Test live', function () {
56 async function createLiveWrapper () { 51 async function createLiveWrapper () {
57 const liveAttributes = { 52 const liveAttributes = {
58 name: 'live video', 53 name: 'live video',
59 channelId: servers[0].videoChannel.id, 54 channelId: servers[0].store.channel.id,
60 privacy: VideoPrivacy.PUBLIC 55 privacy: VideoPrivacy.PUBLIC
61 } 56 }
62 57
63 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes) 58 const { uuid } = await servers[0].live.create({ fields: liveAttributes })
64 return res.body.video.uuid 59 return uuid
65 } 60 }
66 61
67 it('Should correctly send a message when the live starts and ends', async function () { 62 it('Should correctly send a message when the live starts and ends', async function () {
@@ -74,22 +69,22 @@ describe('Test live', function () {
74 await waitJobs(servers) 69 await waitJobs(servers)
75 70
76 { 71 {
77 const videoId = await getVideoIdFromUUID(servers[0].url, liveVideoUUID) 72 const videoId = await servers[0].videos.getId({ uuid: liveVideoUUID })
78 73
79 const localSocket = getLiveNotificationSocket(servers[0].url) 74 const localSocket = servers[0].socketIO.getLiveNotificationSocket()
80 localSocket.on('state-change', data => localStateChanges.push(data.state)) 75 localSocket.on('state-change', data => localStateChanges.push(data.state))
81 localSocket.emit('subscribe', { videoId }) 76 localSocket.emit('subscribe', { videoId })
82 } 77 }
83 78
84 { 79 {
85 const videoId = await getVideoIdFromUUID(servers[1].url, liveVideoUUID) 80 const videoId = await servers[1].videos.getId({ uuid: liveVideoUUID })
86 81
87 const remoteSocket = getLiveNotificationSocket(servers[1].url) 82 const remoteSocket = servers[1].socketIO.getLiveNotificationSocket()
88 remoteSocket.on('state-change', data => remoteStateChanges.push(data.state)) 83 remoteSocket.on('state-change', data => remoteStateChanges.push(data.state))
89 remoteSocket.emit('subscribe', { videoId }) 84 remoteSocket.emit('subscribe', { videoId })
90 } 85 }
91 86
92 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID) 87 const ffmpegCommand = await servers[0].live.sendRTMPStreamInVideo({ videoId: liveVideoUUID })
93 88
94 await waitUntilLivePublishedOnAllServers(servers, liveVideoUUID) 89 await waitUntilLivePublishedOnAllServers(servers, liveVideoUUID)
95 await waitJobs(servers) 90 await waitJobs(servers)
@@ -99,10 +94,10 @@ describe('Test live', function () {
99 expect(stateChanges[stateChanges.length - 1]).to.equal(VideoState.PUBLISHED) 94 expect(stateChanges[stateChanges.length - 1]).to.equal(VideoState.PUBLISHED)
100 } 95 }
101 96
102 await stopFfmpeg(command) 97 await stopFfmpeg(ffmpegCommand)
103 98
104 for (const server of servers) { 99 for (const server of servers) {
105 await waitUntilLiveEnded(server.url, server.accessToken, liveVideoUUID) 100 await server.live.waitUntilEnded({ videoId: liveVideoUUID })
106 } 101 }
107 await waitJobs(servers) 102 await waitJobs(servers)
108 103
@@ -122,22 +117,22 @@ describe('Test live', function () {
122 await waitJobs(servers) 117 await waitJobs(servers)
123 118
124 { 119 {
125 const videoId = await getVideoIdFromUUID(servers[0].url, liveVideoUUID) 120 const videoId = await servers[0].videos.getId({ uuid: liveVideoUUID })
126 121
127 const localSocket = getLiveNotificationSocket(servers[0].url) 122 const localSocket = servers[0].socketIO.getLiveNotificationSocket()
128 localSocket.on('views-change', data => { localLastVideoViews = data.views }) 123 localSocket.on('views-change', data => { localLastVideoViews = data.views })
129 localSocket.emit('subscribe', { videoId }) 124 localSocket.emit('subscribe', { videoId })
130 } 125 }
131 126
132 { 127 {
133 const videoId = await getVideoIdFromUUID(servers[1].url, liveVideoUUID) 128 const videoId = await servers[1].videos.getId({ uuid: liveVideoUUID })
134 129
135 const remoteSocket = getLiveNotificationSocket(servers[1].url) 130 const remoteSocket = servers[1].socketIO.getLiveNotificationSocket()
136 remoteSocket.on('views-change', data => { remoteLastVideoViews = data.views }) 131 remoteSocket.on('views-change', data => { remoteLastVideoViews = data.views })
137 remoteSocket.emit('subscribe', { videoId }) 132 remoteSocket.emit('subscribe', { videoId })
138 } 133 }
139 134
140 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID) 135 const ffmpegCommand = await servers[0].live.sendRTMPStreamInVideo({ videoId: liveVideoUUID })
141 136
142 await waitUntilLivePublishedOnAllServers(servers, liveVideoUUID) 137 await waitUntilLivePublishedOnAllServers(servers, liveVideoUUID)
143 await waitJobs(servers) 138 await waitJobs(servers)
@@ -145,8 +140,8 @@ describe('Test live', function () {
145 expect(localLastVideoViews).to.equal(0) 140 expect(localLastVideoViews).to.equal(0)
146 expect(remoteLastVideoViews).to.equal(0) 141 expect(remoteLastVideoViews).to.equal(0)
147 142
148 await viewVideo(servers[0].url, liveVideoUUID) 143 await servers[0].videos.view({ id: liveVideoUUID })
149 await viewVideo(servers[1].url, liveVideoUUID) 144 await servers[1].videos.view({ id: liveVideoUUID })
150 145
151 await waitJobs(servers) 146 await waitJobs(servers)
152 await wait(5000) 147 await wait(5000)
@@ -155,7 +150,7 @@ describe('Test live', function () {
155 expect(localLastVideoViews).to.equal(2) 150 expect(localLastVideoViews).to.equal(2)
156 expect(remoteLastVideoViews).to.equal(2) 151 expect(remoteLastVideoViews).to.equal(2)
157 152
158 await stopFfmpeg(command) 153 await stopFfmpeg(ffmpegCommand)
159 }) 154 })
160 155
161 it('Should not receive a notification after unsubscribe', async function () { 156 it('Should not receive a notification after unsubscribe', async function () {
@@ -166,13 +161,13 @@ describe('Test live', function () {
166 const liveVideoUUID = await createLiveWrapper() 161 const liveVideoUUID = await createLiveWrapper()
167 await waitJobs(servers) 162 await waitJobs(servers)
168 163
169 const videoId = await getVideoIdFromUUID(servers[0].url, liveVideoUUID) 164 const videoId = await servers[0].videos.getId({ uuid: liveVideoUUID })
170 165
171 const socket = getLiveNotificationSocket(servers[0].url) 166 const socket = servers[0].socketIO.getLiveNotificationSocket()
172 socket.on('state-change', data => stateChanges.push(data.state)) 167 socket.on('state-change', data => stateChanges.push(data.state))
173 socket.emit('subscribe', { videoId }) 168 socket.emit('subscribe', { videoId })
174 169
175 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID) 170 const command = await servers[0].live.sendRTMPStreamInVideo({ videoId: liveVideoUUID })
176 171
177 await waitUntilLivePublishedOnAllServers(servers, liveVideoUUID) 172 await waitUntilLivePublishedOnAllServers(servers, liveVideoUUID)
178 await waitJobs(servers) 173 await waitJobs(servers)
diff --git a/server/tests/api/live/live-views.ts b/server/tests/api/live/live-views.ts
index a44d21ffa..5e3a79c64 100644
--- a/server/tests/api/live/live-views.ts
+++ b/server/tests/api/live/live-views.ts
@@ -3,20 +3,15 @@
3import 'mocha' 3import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5import { FfmpegCommand } from 'fluent-ffmpeg' 5import { FfmpegCommand } from 'fluent-ffmpeg'
6import { VideoDetails, VideoPrivacy } from '@shared/models' 6import { VideoPrivacy } from '@shared/models'
7import { 7import {
8 cleanupTests, 8 cleanupTests,
9 createLive, 9 createMultipleServers,
10 doubleFollow, 10 doubleFollow,
11 flushAndRunMultipleServers, 11 PeerTubeServer,
12 getVideo,
13 sendRTMPStreamInVideo,
14 ServerInfo,
15 setAccessTokensToServers, 12 setAccessTokensToServers,
16 setDefaultVideoChannel, 13 setDefaultVideoChannel,
17 stopFfmpeg, 14 stopFfmpeg,
18 updateCustomSubConfig,
19 viewVideo,
20 wait, 15 wait,
21 waitJobs, 16 waitJobs,
22 waitUntilLivePublishedOnAllServers 17 waitUntilLivePublishedOnAllServers
@@ -25,23 +20,25 @@ import {
25const expect = chai.expect 20const expect = chai.expect
26 21
27describe('Test live', function () { 22describe('Test live', function () {
28 let servers: ServerInfo[] = [] 23 let servers: PeerTubeServer[] = []
29 24
30 before(async function () { 25 before(async function () {
31 this.timeout(120000) 26 this.timeout(120000)
32 27
33 servers = await flushAndRunMultipleServers(2) 28 servers = await createMultipleServers(2)
34 29
35 // Get the access tokens 30 // Get the access tokens
36 await setAccessTokensToServers(servers) 31 await setAccessTokensToServers(servers)
37 await setDefaultVideoChannel(servers) 32 await setDefaultVideoChannel(servers)
38 33
39 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, { 34 await servers[0].config.updateCustomSubConfig({
40 live: { 35 newConfig: {
41 enabled: true, 36 live: {
42 allowReplay: true, 37 enabled: true,
43 transcoding: { 38 allowReplay: true,
44 enabled: false 39 transcoding: {
40 enabled: false
41 }
45 } 42 }
46 } 43 }
47 }) 44 })
@@ -56,9 +53,7 @@ describe('Test live', function () {
56 53
57 async function countViews (expected: number) { 54 async function countViews (expected: number) {
58 for (const server of servers) { 55 for (const server of servers) {
59 const res = await getVideo(server.url, liveVideoId) 56 const video = await server.videos.get({ id: liveVideoId })
60 const video: VideoDetails = res.body
61
62 expect(video.views).to.equal(expected) 57 expect(video.views).to.equal(expected)
63 } 58 }
64 } 59 }
@@ -68,14 +63,14 @@ describe('Test live', function () {
68 63
69 const liveAttributes = { 64 const liveAttributes = {
70 name: 'live video', 65 name: 'live video',
71 channelId: servers[0].videoChannel.id, 66 channelId: servers[0].store.channel.id,
72 privacy: VideoPrivacy.PUBLIC 67 privacy: VideoPrivacy.PUBLIC
73 } 68 }
74 69
75 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes) 70 const live = await servers[0].live.create({ fields: liveAttributes })
76 liveVideoId = res.body.video.uuid 71 liveVideoId = live.uuid
77 72
78 command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId) 73 command = await servers[0].live.sendRTMPStreamInVideo({ videoId: liveVideoId })
79 await waitUntilLivePublishedOnAllServers(servers, liveVideoId) 74 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
80 await waitJobs(servers) 75 await waitJobs(servers)
81 }) 76 })
@@ -87,8 +82,8 @@ describe('Test live', function () {
87 it('Should view a live twice and display 1 view', async function () { 82 it('Should view a live twice and display 1 view', async function () {
88 this.timeout(30000) 83 this.timeout(30000)
89 84
90 await viewVideo(servers[0].url, liveVideoId) 85 await servers[0].videos.view({ id: liveVideoId })
91 await viewVideo(servers[0].url, liveVideoId) 86 await servers[0].videos.view({ id: liveVideoId })
92 87
93 await wait(7000) 88 await wait(7000)
94 89
@@ -109,9 +104,9 @@ describe('Test live', function () {
109 it('Should view a live on a remote and on local and display 2 views', async function () { 104 it('Should view a live on a remote and on local and display 2 views', async function () {
110 this.timeout(30000) 105 this.timeout(30000)
111 106
112 await viewVideo(servers[0].url, liveVideoId) 107 await servers[0].videos.view({ id: liveVideoId })
113 await viewVideo(servers[1].url, liveVideoId) 108 await servers[1].videos.view({ id: liveVideoId })
114 await viewVideo(servers[1].url, liveVideoId) 109 await servers[1].videos.view({ id: liveVideoId })
115 110
116 await wait(7000) 111 await wait(7000)
117 await waitJobs(servers) 112 await waitJobs(servers)
diff --git a/server/tests/api/live/live.ts b/server/tests/api/live/live.ts
index 50397924e..4676a840a 100644
--- a/server/tests/api/live/live.ts
+++ b/server/tests/api/live/live.ts
@@ -4,73 +4,68 @@ import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5import { join } from 'path' 5import { join } from 'path'
6import { ffprobePromise, getVideoStreamFromFile } from '@server/helpers/ffprobe-utils' 6import { ffprobePromise, getVideoStreamFromFile } from '@server/helpers/ffprobe-utils'
7import { LiveVideo, LiveVideoCreate, Video, VideoDetails, VideoPrivacy, VideoState, VideoStreamingPlaylistType } from '@shared/models'
8import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
9import { 7import {
10 addVideoToBlacklist,
11 buildServerDirectory,
12 checkLiveCleanup, 8 checkLiveCleanup,
13 checkLiveSegmentHash, 9 checkLiveSegmentHash,
14 checkResolutionsInMasterPlaylist, 10 checkResolutionsInMasterPlaylist,
15 cleanupTests, 11 cleanupTests,
16 createLive, 12 createMultipleServers,
17 doubleFollow, 13 doubleFollow,
18 flushAndRunMultipleServers,
19 getLive,
20 getMyVideosWithFilter,
21 getPlaylist,
22 getVideo,
23 getVideosList,
24 getVideosWithFilters,
25 killallServers, 14 killallServers,
15 LiveCommand,
26 makeRawRequest, 16 makeRawRequest,
27 removeVideo, 17 PeerTubeServer,
28 reRunServer,
29 sendRTMPStream, 18 sendRTMPStream,
30 sendRTMPStreamInVideo,
31 ServerInfo,
32 setAccessTokensToServers, 19 setAccessTokensToServers,
33 setDefaultVideoChannel, 20 setDefaultVideoChannel,
34 stopFfmpeg, 21 stopFfmpeg,
35 testFfmpegStreamError, 22 testFfmpegStreamError,
36 testImage, 23 testImage,
37 updateCustomSubConfig,
38 updateLive,
39 uploadVideoAndGetId,
40 wait, 24 wait,
41 waitJobs, 25 waitJobs,
42 waitUntilLiveEnded, 26 waitUntilLivePublishedOnAllServers
43 waitUntilLivePublished, 27} from '@shared/extra-utils'
44 waitUntilLivePublishedOnAllServers, 28import {
45 waitUntilLiveSegmentGeneration 29 HttpStatusCode,
46} from '../../../../shared/extra-utils' 30 LiveVideo,
31 LiveVideoCreate,
32 VideoDetails,
33 VideoPrivacy,
34 VideoState,
35 VideoStreamingPlaylistType
36} from '@shared/models'
47 37
48const expect = chai.expect 38const expect = chai.expect
49 39
50describe('Test live', function () { 40describe('Test live', function () {
51 let servers: ServerInfo[] = [] 41 let servers: PeerTubeServer[] = []
42 let commands: LiveCommand[]
52 43
53 before(async function () { 44 before(async function () {
54 this.timeout(120000) 45 this.timeout(120000)
55 46
56 servers = await flushAndRunMultipleServers(2) 47 servers = await createMultipleServers(2)
57 48
58 // Get the access tokens 49 // Get the access tokens
59 await setAccessTokensToServers(servers) 50 await setAccessTokensToServers(servers)
60 await setDefaultVideoChannel(servers) 51 await setDefaultVideoChannel(servers)
61 52
62 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, { 53 await servers[0].config.updateCustomSubConfig({
63 live: { 54 newConfig: {
64 enabled: true, 55 live: {
65 allowReplay: true, 56 enabled: true,
66 transcoding: { 57 allowReplay: true,
67 enabled: false 58 transcoding: {
59 enabled: false
60 }
68 } 61 }
69 } 62 }
70 }) 63 })
71 64
72 // Server 1 and server 2 follow each other 65 // Server 1 and server 2 follow each other
73 await doubleFollow(servers[0], servers[1]) 66 await doubleFollow(servers[0], servers[1])
67
68 commands = servers.map(s => s.live)
74 }) 69 })
75 70
76 describe('Live creation, update and delete', function () { 71 describe('Live creation, update and delete', function () {
@@ -85,7 +80,7 @@ describe('Test live', function () {
85 language: 'fr', 80 language: 'fr',
86 description: 'super live description', 81 description: 'super live description',
87 support: 'support field', 82 support: 'support field',
88 channelId: servers[0].videoChannel.id, 83 channelId: servers[0].store.channel.id,
89 nsfw: false, 84 nsfw: false,
90 waitTranscoding: false, 85 waitTranscoding: false,
91 name: 'my super live', 86 name: 'my super live',
@@ -98,14 +93,13 @@ describe('Test live', function () {
98 thumbnailfile: 'video_short1.webm.jpg' 93 thumbnailfile: 'video_short1.webm.jpg'
99 } 94 }
100 95
101 const res = await createLive(servers[0].url, servers[0].accessToken, attributes) 96 const live = await commands[0].create({ fields: attributes })
102 liveVideoUUID = res.body.video.uuid 97 liveVideoUUID = live.uuid
103 98
104 await waitJobs(servers) 99 await waitJobs(servers)
105 100
106 for (const server of servers) { 101 for (const server of servers) {
107 const resVideo = await getVideo(server.url, liveVideoUUID) 102 const video = await server.videos.get({ id: liveVideoUUID })
108 const video: VideoDetails = resVideo.body
109 103
110 expect(video.category.id).to.equal(1) 104 expect(video.category.id).to.equal(1)
111 expect(video.licence.id).to.equal(2) 105 expect(video.licence.id).to.equal(2)
@@ -113,8 +107,8 @@ describe('Test live', function () {
113 expect(video.description).to.equal('super live description') 107 expect(video.description).to.equal('super live description')
114 expect(video.support).to.equal('support field') 108 expect(video.support).to.equal('support field')
115 109
116 expect(video.channel.name).to.equal(servers[0].videoChannel.name) 110 expect(video.channel.name).to.equal(servers[0].store.channel.name)
117 expect(video.channel.host).to.equal(servers[0].videoChannel.host) 111 expect(video.channel.host).to.equal(servers[0].store.channel.host)
118 112
119 expect(video.isLive).to.be.true 113 expect(video.isLive).to.be.true
120 114
@@ -129,8 +123,7 @@ describe('Test live', function () {
129 await testImage(server.url, 'video_short1-preview.webm', video.previewPath) 123 await testImage(server.url, 'video_short1-preview.webm', video.previewPath)
130 await testImage(server.url, 'video_short1.webm', video.thumbnailPath) 124 await testImage(server.url, 'video_short1.webm', video.thumbnailPath)
131 125
132 const resLive = await getLive(server.url, server.accessToken, liveVideoUUID) 126 const live = await server.live.get({ videoId: liveVideoUUID })
133 const live: LiveVideo = resLive.body
134 127
135 if (server.url === servers[0].url) { 128 if (server.url === servers[0].url) {
136 expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':' + servers[0].rtmpPort + '/live') 129 expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':' + servers[0].rtmpPort + '/live')
@@ -149,20 +142,18 @@ describe('Test live', function () {
149 142
150 const attributes: LiveVideoCreate = { 143 const attributes: LiveVideoCreate = {
151 name: 'default live thumbnail', 144 name: 'default live thumbnail',
152 channelId: servers[0].videoChannel.id, 145 channelId: servers[0].store.channel.id,
153 privacy: VideoPrivacy.UNLISTED, 146 privacy: VideoPrivacy.UNLISTED,
154 nsfw: true 147 nsfw: true
155 } 148 }
156 149
157 const res = await createLive(servers[0].url, servers[0].accessToken, attributes) 150 const live = await commands[0].create({ fields: attributes })
158 const videoId = res.body.video.uuid 151 const videoId = live.uuid
159 152
160 await waitJobs(servers) 153 await waitJobs(servers)
161 154
162 for (const server of servers) { 155 for (const server of servers) {
163 const resVideo = await getVideo(server.url, videoId) 156 const video = await server.videos.get({ id: videoId })
164 const video: VideoDetails = resVideo.body
165
166 expect(video.privacy.id).to.equal(VideoPrivacy.UNLISTED) 157 expect(video.privacy.id).to.equal(VideoPrivacy.UNLISTED)
167 expect(video.nsfw).to.be.true 158 expect(video.nsfw).to.be.true
168 159
@@ -173,28 +164,27 @@ describe('Test live', function () {
173 164
174 it('Should not have the live listed since nobody streams into', async function () { 165 it('Should not have the live listed since nobody streams into', async function () {
175 for (const server of servers) { 166 for (const server of servers) {
176 const res = await getVideosList(server.url) 167 const { total, data } = await server.videos.list()
177 168
178 expect(res.body.total).to.equal(0) 169 expect(total).to.equal(0)
179 expect(res.body.data).to.have.lengthOf(0) 170 expect(data).to.have.lengthOf(0)
180 } 171 }
181 }) 172 })
182 173
183 it('Should not be able to update a live of another server', async function () { 174 it('Should not be able to update a live of another server', async function () {
184 await updateLive(servers[1].url, servers[1].accessToken, liveVideoUUID, { saveReplay: false }, HttpStatusCode.FORBIDDEN_403) 175 await commands[1].update({ videoId: liveVideoUUID, fields: { saveReplay: false }, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
185 }) 176 })
186 177
187 it('Should update the live', async function () { 178 it('Should update the live', async function () {
188 this.timeout(10000) 179 this.timeout(10000)
189 180
190 await updateLive(servers[0].url, servers[0].accessToken, liveVideoUUID, { saveReplay: false }) 181 await commands[0].update({ videoId: liveVideoUUID, fields: { saveReplay: false } })
191 await waitJobs(servers) 182 await waitJobs(servers)
192 }) 183 })
193 184
194 it('Have the live updated', async function () { 185 it('Have the live updated', async function () {
195 for (const server of servers) { 186 for (const server of servers) {
196 const res = await getLive(server.url, server.accessToken, liveVideoUUID) 187 const live = await server.live.get({ videoId: liveVideoUUID })
197 const live: LiveVideo = res.body
198 188
199 if (server.url === servers[0].url) { 189 if (server.url === servers[0].url) {
200 expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':' + servers[0].rtmpPort + '/live') 190 expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':' + servers[0].rtmpPort + '/live')
@@ -211,77 +201,75 @@ describe('Test live', function () {
211 it('Delete the live', async function () { 201 it('Delete the live', async function () {
212 this.timeout(10000) 202 this.timeout(10000)
213 203
214 await removeVideo(servers[0].url, servers[0].accessToken, liveVideoUUID) 204 await servers[0].videos.remove({ id: liveVideoUUID })
215 await waitJobs(servers) 205 await waitJobs(servers)
216 }) 206 })
217 207
218 it('Should have the live deleted', async function () { 208 it('Should have the live deleted', async function () {
219 for (const server of servers) { 209 for (const server of servers) {
220 await getVideo(server.url, liveVideoUUID, HttpStatusCode.NOT_FOUND_404) 210 await server.videos.get({ id: liveVideoUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
221 await getLive(server.url, server.accessToken, liveVideoUUID, HttpStatusCode.NOT_FOUND_404) 211 await server.live.get({ videoId: liveVideoUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
222 } 212 }
223 }) 213 })
224 }) 214 })
225 215
226 describe('Live filters', function () { 216 describe('Live filters', function () {
227 let command: any 217 let ffmpegCommand: any
228 let liveVideoId: string 218 let liveVideoId: string
229 let vodVideoId: string 219 let vodVideoId: string
230 220
231 before(async function () { 221 before(async function () {
232 this.timeout(120000) 222 this.timeout(120000)
233 223
234 vodVideoId = (await uploadVideoAndGetId({ server: servers[0], videoName: 'vod video' })).uuid 224 vodVideoId = (await servers[0].videos.quickUpload({ name: 'vod video' })).uuid
235 225
236 const liveOptions = { name: 'live', privacy: VideoPrivacy.PUBLIC, channelId: servers[0].videoChannel.id } 226 const liveOptions = { name: 'live', privacy: VideoPrivacy.PUBLIC, channelId: servers[0].store.channel.id }
237 const resLive = await createLive(servers[0].url, servers[0].accessToken, liveOptions) 227 const live = await commands[0].create({ fields: liveOptions })
238 liveVideoId = resLive.body.video.uuid 228 liveVideoId = live.uuid
239 229
240 command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId) 230 ffmpegCommand = await servers[0].live.sendRTMPStreamInVideo({ videoId: liveVideoId })
241 await waitUntilLivePublishedOnAllServers(servers, liveVideoId) 231 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
242 await waitJobs(servers) 232 await waitJobs(servers)
243 }) 233 })
244 234
245 it('Should only display lives', async function () { 235 it('Should only display lives', async function () {
246 const res = await getVideosWithFilters(servers[0].url, { isLive: true }) 236 const { data, total } = await servers[0].videos.list({ isLive: true })
247 237
248 expect(res.body.total).to.equal(1) 238 expect(total).to.equal(1)
249 expect(res.body.data).to.have.lengthOf(1) 239 expect(data).to.have.lengthOf(1)
250 expect(res.body.data[0].name).to.equal('live') 240 expect(data[0].name).to.equal('live')
251 }) 241 })
252 242
253 it('Should not display lives', async function () { 243 it('Should not display lives', async function () {
254 const res = await getVideosWithFilters(servers[0].url, { isLive: false }) 244 const { data, total } = await servers[0].videos.list({ isLive: false })
255 245
256 expect(res.body.total).to.equal(1) 246 expect(total).to.equal(1)
257 expect(res.body.data).to.have.lengthOf(1) 247 expect(data).to.have.lengthOf(1)
258 expect(res.body.data[0].name).to.equal('vod video') 248 expect(data[0].name).to.equal('vod video')
259 }) 249 })
260 250
261 it('Should display my lives', async function () { 251 it('Should display my lives', async function () {
262 this.timeout(60000) 252 this.timeout(60000)
263 253
264 await stopFfmpeg(command) 254 await stopFfmpeg(ffmpegCommand)
265 await waitJobs(servers) 255 await waitJobs(servers)
266 256
267 const res = await getMyVideosWithFilter(servers[0].url, servers[0].accessToken, { isLive: true }) 257 const { data } = await servers[0].videos.listMyVideos({ isLive: true })
268 const videos = res.body.data as Video[]
269 258
270 const result = videos.every(v => v.isLive) 259 const result = data.every(v => v.isLive)
271 expect(result).to.be.true 260 expect(result).to.be.true
272 }) 261 })
273 262
274 it('Should not display my lives', async function () { 263 it('Should not display my lives', async function () {
275 const res = await getMyVideosWithFilter(servers[0].url, servers[0].accessToken, { isLive: false }) 264 const { data } = await servers[0].videos.listMyVideos({ isLive: false })
276 const videos = res.body.data as Video[]
277 265
278 const result = videos.every(v => !v.isLive) 266 const result = data.every(v => !v.isLive)
279 expect(result).to.be.true 267 expect(result).to.be.true
280 }) 268 })
281 269
282 after(async function () { 270 after(async function () {
283 await removeVideo(servers[0].url, servers[0].accessToken, vodVideoId) 271 await servers[0].videos.remove({ id: vodVideoId })
284 await removeVideo(servers[0].url, servers[0].accessToken, liveVideoId) 272 await servers[0].videos.remove({ id: liveVideoId })
285 }) 273 })
286 }) 274 })
287 275
@@ -296,18 +284,17 @@ describe('Test live', function () {
296 async function createLiveWrapper () { 284 async function createLiveWrapper () {
297 const liveAttributes = { 285 const liveAttributes = {
298 name: 'user live', 286 name: 'user live',
299 channelId: servers[0].videoChannel.id, 287 channelId: servers[0].store.channel.id,
300 privacy: VideoPrivacy.PUBLIC, 288 privacy: VideoPrivacy.PUBLIC,
301 saveReplay: false 289 saveReplay: false
302 } 290 }
303 291
304 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes) 292 const { uuid } = await commands[0].create({ fields: liveAttributes })
305 const uuid = res.body.video.uuid
306 293
307 const resLive = await getLive(servers[0].url, servers[0].accessToken, uuid) 294 const live = await commands[0].get({ videoId: uuid })
308 const resVideo = await getVideo(servers[0].url, uuid) 295 const video = await servers[0].videos.get({ id: uuid })
309 296
310 return Object.assign(resVideo.body, resLive.body) as LiveVideo & VideoDetails 297 return Object.assign(video, live)
311 } 298 }
312 299
313 it('Should not allow a stream without the appropriate path', async function () { 300 it('Should not allow a stream without the appropriate path', async function () {
@@ -335,13 +322,12 @@ describe('Test live', function () {
335 322
336 it('Should list this live now someone stream into it', async function () { 323 it('Should list this live now someone stream into it', async function () {
337 for (const server of servers) { 324 for (const server of servers) {
338 const res = await getVideosList(server.url) 325 const { total, data } = await server.videos.list()
339
340 expect(res.body.total).to.equal(1)
341 expect(res.body.data).to.have.lengthOf(1)
342 326
343 const video: Video = res.body.data[0] 327 expect(total).to.equal(1)
328 expect(data).to.have.lengthOf(1)
344 329
330 const video = data[0]
345 expect(video.name).to.equal('user live') 331 expect(video.name).to.equal('user live')
346 expect(video.isLive).to.be.true 332 expect(video.isLive).to.be.true
347 } 333 }
@@ -352,7 +338,7 @@ describe('Test live', function () {
352 338
353 liveVideo = await createLiveWrapper() 339 liveVideo = await createLiveWrapper()
354 340
355 await addVideoToBlacklist(servers[0].url, servers[0].accessToken, liveVideo.uuid) 341 await servers[0].blacklist.add({ videoId: liveVideo.uuid })
356 342
357 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey) 343 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
358 await testFfmpegStreamError(command, true) 344 await testFfmpegStreamError(command, true)
@@ -363,7 +349,7 @@ describe('Test live', function () {
363 349
364 liveVideo = await createLiveWrapper() 350 liveVideo = await createLiveWrapper()
365 351
366 await removeVideo(servers[0].url, servers[0].accessToken, liveVideo.uuid) 352 await servers[0].videos.remove({ id: liveVideo.uuid })
367 353
368 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey) 354 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
369 await testFfmpegStreamError(command, true) 355 await testFfmpegStreamError(command, true)
@@ -376,24 +362,21 @@ describe('Test live', function () {
376 async function createLiveWrapper (saveReplay: boolean) { 362 async function createLiveWrapper (saveReplay: boolean) {
377 const liveAttributes = { 363 const liveAttributes = {
378 name: 'live video', 364 name: 'live video',
379 channelId: servers[0].videoChannel.id, 365 channelId: servers[0].store.channel.id,
380 privacy: VideoPrivacy.PUBLIC, 366 privacy: VideoPrivacy.PUBLIC,
381 saveReplay 367 saveReplay
382 } 368 }
383 369
384 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes) 370 const { uuid } = await commands[0].create({ fields: liveAttributes })
385 return res.body.video.uuid 371 return uuid
386 } 372 }
387 373
388 async function testVideoResolutions (liveVideoId: string, resolutions: number[]) { 374 async function testVideoResolutions (liveVideoId: string, resolutions: number[]) {
389 for (const server of servers) { 375 for (const server of servers) {
390 const resList = await getVideosList(server.url) 376 const { data } = await server.videos.list()
391 const videos: Video[] = resList.body.data 377 expect(data.find(v => v.uuid === liveVideoId)).to.exist
392 378
393 expect(videos.find(v => v.uuid === liveVideoId)).to.exist 379 const video = await server.videos.get({ id: liveVideoId })
394
395 const resVideo = await getVideo(server.url, liveVideoId)
396 const video: VideoDetails = resVideo.body
397 380
398 expect(video.streamingPlaylists).to.have.lengthOf(1) 381 expect(video.streamingPlaylists).to.have.lengthOf(1)
399 382
@@ -403,39 +386,48 @@ describe('Test live', function () {
403 // Only finite files are displayed 386 // Only finite files are displayed
404 expect(hlsPlaylist.files).to.have.lengthOf(0) 387 expect(hlsPlaylist.files).to.have.lengthOf(0)
405 388
406 await checkResolutionsInMasterPlaylist(hlsPlaylist.playlistUrl, resolutions) 389 await checkResolutionsInMasterPlaylist({ server, playlistUrl: hlsPlaylist.playlistUrl, resolutions })
407 390
408 for (let i = 0; i < resolutions.length; i++) { 391 for (let i = 0; i < resolutions.length; i++) {
409 const segmentNum = 3 392 const segmentNum = 3
410 const segmentName = `${i}-00000${segmentNum}.ts` 393 const segmentName = `${i}-00000${segmentNum}.ts`
411 await waitUntilLiveSegmentGeneration(servers[0], video.uuid, i, segmentNum) 394 await commands[0].waitUntilSegmentGeneration({ videoUUID: video.uuid, resolution: i, segment: segmentNum })
412 395
413 const res = await getPlaylist(`${servers[0].url}/static/streaming-playlists/hls/${video.uuid}/${i}.m3u8`) 396 const subPlaylist = await servers[0].streamingPlaylists.get({
414 const subPlaylist = res.text 397 url: `${servers[0].url}/static/streaming-playlists/hls/${video.uuid}/${i}.m3u8`
398 })
415 399
416 expect(subPlaylist).to.contain(segmentName) 400 expect(subPlaylist).to.contain(segmentName)
417 401
418 const baseUrlAndPath = servers[0].url + '/static/streaming-playlists/hls' 402 const baseUrlAndPath = servers[0].url + '/static/streaming-playlists/hls'
419 await checkLiveSegmentHash(baseUrlAndPath, video.uuid, segmentName, hlsPlaylist) 403 await checkLiveSegmentHash({
404 server,
405 baseUrlSegment: baseUrlAndPath,
406 videoUUID: video.uuid,
407 segmentName,
408 hlsPlaylist
409 })
420 } 410 }
421 } 411 }
422 } 412 }
423 413
424 function updateConf (resolutions: number[]) { 414 function updateConf (resolutions: number[]) {
425 return updateCustomSubConfig(servers[0].url, servers[0].accessToken, { 415 return servers[0].config.updateCustomSubConfig({
426 live: { 416 newConfig: {
427 enabled: true, 417 live: {
428 allowReplay: true,
429 maxDuration: -1,
430 transcoding: {
431 enabled: true, 418 enabled: true,
432 resolutions: { 419 allowReplay: true,
433 '240p': resolutions.includes(240), 420 maxDuration: -1,
434 '360p': resolutions.includes(360), 421 transcoding: {
435 '480p': resolutions.includes(480), 422 enabled: true,
436 '720p': resolutions.includes(720), 423 resolutions: {
437 '1080p': resolutions.includes(1080), 424 '240p': resolutions.includes(240),
438 '2160p': resolutions.includes(2160) 425 '360p': resolutions.includes(360),
426 '480p': resolutions.includes(480),
427 '720p': resolutions.includes(720),
428 '1080p': resolutions.includes(1080),
429 '2160p': resolutions.includes(2160)
430 }
439 } 431 }
440 } 432 }
441 } 433 }
@@ -451,13 +443,13 @@ describe('Test live', function () {
451 443
452 liveVideoId = await createLiveWrapper(false) 444 liveVideoId = await createLiveWrapper(false)
453 445
454 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId) 446 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId })
455 await waitUntilLivePublishedOnAllServers(servers, liveVideoId) 447 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
456 await waitJobs(servers) 448 await waitJobs(servers)
457 449
458 await testVideoResolutions(liveVideoId, [ 720 ]) 450 await testVideoResolutions(liveVideoId, [ 720 ])
459 451
460 await stopFfmpeg(command) 452 await stopFfmpeg(ffmpegCommand)
461 }) 453 })
462 454
463 it('Should enable transcoding with some resolutions', async function () { 455 it('Should enable transcoding with some resolutions', async function () {
@@ -467,13 +459,13 @@ describe('Test live', function () {
467 await updateConf(resolutions) 459 await updateConf(resolutions)
468 liveVideoId = await createLiveWrapper(false) 460 liveVideoId = await createLiveWrapper(false)
469 461
470 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId) 462 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId })
471 await waitUntilLivePublishedOnAllServers(servers, liveVideoId) 463 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
472 await waitJobs(servers) 464 await waitJobs(servers)
473 465
474 await testVideoResolutions(liveVideoId, resolutions) 466 await testVideoResolutions(liveVideoId, resolutions)
475 467
476 await stopFfmpeg(command) 468 await stopFfmpeg(ffmpegCommand)
477 }) 469 })
478 470
479 it('Should enable transcoding with some resolutions and correctly save them', async function () { 471 it('Should enable transcoding with some resolutions and correctly save them', async function () {
@@ -484,14 +476,14 @@ describe('Test live', function () {
484 await updateConf(resolutions) 476 await updateConf(resolutions)
485 liveVideoId = await createLiveWrapper(true) 477 liveVideoId = await createLiveWrapper(true)
486 478
487 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId, 'video_short2.webm') 479 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId, fixtureName: 'video_short2.webm' })
488 await waitUntilLivePublishedOnAllServers(servers, liveVideoId) 480 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
489 await waitJobs(servers) 481 await waitJobs(servers)
490 482
491 await testVideoResolutions(liveVideoId, resolutions) 483 await testVideoResolutions(liveVideoId, resolutions)
492 484
493 await stopFfmpeg(command) 485 await stopFfmpeg(ffmpegCommand)
494 await waitUntilLiveEnded(servers[0].url, servers[0].accessToken, liveVideoId) 486 await commands[0].waitUntilEnded({ videoId: liveVideoId })
495 487
496 await waitJobs(servers) 488 await waitJobs(servers)
497 489
@@ -504,8 +496,7 @@ describe('Test live', function () {
504 } 496 }
505 497
506 for (const server of servers) { 498 for (const server of servers) {
507 const resVideo = await getVideo(server.url, liveVideoId) 499 const video = await server.videos.get({ id: liveVideoId })
508 const video: VideoDetails = resVideo.body
509 500
510 expect(video.state.id).to.equal(VideoState.PUBLISHED) 501 expect(video.state.id).to.equal(VideoState.PUBLISHED)
511 expect(video.duration).to.be.greaterThan(1) 502 expect(video.duration).to.be.greaterThan(1)
@@ -530,7 +521,7 @@ describe('Test live', function () {
530 } 521 }
531 522
532 const filename = `${video.uuid}-${resolution}-fragmented.mp4` 523 const filename = `${video.uuid}-${resolution}-fragmented.mp4`
533 const segmentPath = buildServerDirectory(servers[0], join('streaming-playlists', 'hls', video.uuid, filename)) 524 const segmentPath = servers[0].servers.buildDirectory(join('streaming-playlists', 'hls', video.uuid, filename))
534 525
535 const probe = await ffprobePromise(segmentPath) 526 const probe = await ffprobePromise(segmentPath)
536 const videoStream = await getVideoStreamFromFile(segmentPath, probe) 527 const videoStream = await getVideoStreamFromFile(segmentPath, probe)
@@ -557,13 +548,13 @@ describe('Test live', function () {
557 async function createLiveWrapper (saveReplay: boolean) { 548 async function createLiveWrapper (saveReplay: boolean) {
558 const liveAttributes = { 549 const liveAttributes = {
559 name: 'live video', 550 name: 'live video',
560 channelId: servers[0].videoChannel.id, 551 channelId: servers[0].store.channel.id,
561 privacy: VideoPrivacy.PUBLIC, 552 privacy: VideoPrivacy.PUBLIC,
562 saveReplay 553 saveReplay
563 } 554 }
564 555
565 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes) 556 const { uuid } = await commands[0].create({ fields: liveAttributes })
566 return res.body.video.uuid 557 return uuid
567 } 558 }
568 559
569 before(async function () { 560 before(async function () {
@@ -573,20 +564,20 @@ describe('Test live', function () {
573 liveVideoReplayId = await createLiveWrapper(true) 564 liveVideoReplayId = await createLiveWrapper(true)
574 565
575 await Promise.all([ 566 await Promise.all([
576 sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId), 567 commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId }),
577 sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoReplayId) 568 commands[0].sendRTMPStreamInVideo({ videoId: liveVideoReplayId })
578 ]) 569 ])
579 570
580 await Promise.all([ 571 await Promise.all([
581 waitUntilLivePublished(servers[0].url, servers[0].accessToken, liveVideoId), 572 commands[0].waitUntilPublished({ videoId: liveVideoId }),
582 waitUntilLivePublished(servers[0].url, servers[0].accessToken, liveVideoReplayId) 573 commands[0].waitUntilPublished({ videoId: liveVideoReplayId })
583 ]) 574 ])
584 575
585 await waitUntilLiveSegmentGeneration(servers[0], liveVideoId, 0, 2) 576 await commands[0].waitUntilSegmentGeneration({ videoUUID: liveVideoId, resolution: 0, segment: 2 })
586 await waitUntilLiveSegmentGeneration(servers[0], liveVideoReplayId, 0, 2) 577 await commands[0].waitUntilSegmentGeneration({ videoUUID: liveVideoReplayId, resolution: 0, segment: 2 })
587 578
588 await killallServers([ servers[0] ]) 579 await killallServers([ servers[0] ])
589 await reRunServer(servers[0]) 580 await servers[0].run()
590 581
591 await wait(5000) 582 await wait(5000)
592 }) 583 })
@@ -594,13 +585,13 @@ describe('Test live', function () {
594 it('Should cleanup lives', async function () { 585 it('Should cleanup lives', async function () {
595 this.timeout(60000) 586 this.timeout(60000)
596 587
597 await waitUntilLiveEnded(servers[0].url, servers[0].accessToken, liveVideoId) 588 await commands[0].waitUntilEnded({ videoId: liveVideoId })
598 }) 589 })
599 590
600 it('Should save a live replay', async function () { 591 it('Should save a live replay', async function () {
601 this.timeout(120000) 592 this.timeout(120000)
602 593
603 await waitUntilLivePublished(servers[0].url, servers[0].accessToken, liveVideoReplayId) 594 await commands[0].waitUntilPublished({ videoId: liveVideoReplayId })
604 }) 595 })
605 }) 596 })
606 597
diff --git a/server/tests/api/moderation/abuses.ts b/server/tests/api/moderation/abuses.ts
index fb765e7e3..8d6360eb3 100644
--- a/server/tests/api/moderation/abuses.ts
+++ b/server/tests/api/moderation/abuses.ts
@@ -3,70 +3,37 @@
3import 'mocha' 3import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5import { 5import {
6 AbuseFilter, 6 AbusesCommand,
7 AbuseMessage,
8 AbusePredefinedReasonsString,
9 AbuseState,
10 Account,
11 AdminAbuse,
12 UserAbuse,
13 VideoComment
14} from '@shared/models'
15import {
16 addAbuseMessage,
17 addVideoCommentThread,
18 cleanupTests, 7 cleanupTests,
19 createUser, 8 createMultipleServers,
20 deleteAbuse, 9 doubleFollow,
21 deleteAbuseMessage, 10 PeerTubeServer,
22 deleteVideoComment,
23 flushAndRunMultipleServers,
24 generateUserAccessToken,
25 getAccount,
26 getAdminAbusesList,
27 getUserAbusesList,
28 getVideoCommentThreads,
29 getVideoIdFromUUID,
30 getVideosList,
31 immutableAssign,
32 listAbuseMessages,
33 removeUser,
34 removeVideo,
35 reportAbuse,
36 ServerInfo,
37 setAccessTokensToServers, 11 setAccessTokensToServers,
38 updateAbuse, 12 waitJobs
39 uploadVideo, 13} from '@shared/extra-utils'
40 uploadVideoAndGetId, 14import { AbuseMessage, AbusePredefinedReasonsString, AbuseState, AdminAbuse, UserAbuse } from '@shared/models'
41 userLogin
42} from '../../../../shared/extra-utils/index'
43import { doubleFollow } from '../../../../shared/extra-utils/server/follows'
44import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
45import {
46 addAccountToServerBlocklist,
47 addServerToServerBlocklist,
48 removeAccountFromServerBlocklist,
49 removeServerFromServerBlocklist
50} from '../../../../shared/extra-utils/users/blocklist'
51 15
52const expect = chai.expect 16const expect = chai.expect
53 17
54describe('Test abuses', function () { 18describe('Test abuses', function () {
55 let servers: ServerInfo[] = [] 19 let servers: PeerTubeServer[] = []
56 let abuseServer1: AdminAbuse 20 let abuseServer1: AdminAbuse
57 let abuseServer2: AdminAbuse 21 let abuseServer2: AdminAbuse
22 let commands: AbusesCommand[]
58 23
59 before(async function () { 24 before(async function () {
60 this.timeout(50000) 25 this.timeout(50000)
61 26
62 // Run servers 27 // Run servers
63 servers = await flushAndRunMultipleServers(2) 28 servers = await createMultipleServers(2)
64 29
65 // Get the access tokens 30 // Get the access tokens
66 await setAccessTokensToServers(servers) 31 await setAccessTokensToServers(servers)
67 32
68 // Server 1 and server 2 follow each other 33 // Server 1 and server 2 follow each other
69 await doubleFollow(servers[0], servers[1]) 34 await doubleFollow(servers[0], servers[1])
35
36 commands = servers.map(s => s.abuses)
70 }) 37 })
71 38
72 describe('Video abuses', function () { 39 describe('Video abuses', function () {
@@ -75,179 +42,189 @@ describe('Test abuses', function () {
75 this.timeout(50000) 42 this.timeout(50000)
76 43
77 // Upload some videos on each servers 44 // Upload some videos on each servers
78 const video1Attributes = { 45 {
79 name: 'my super name for server 1', 46 const attributes = {
80 description: 'my super description for server 1' 47 name: 'my super name for server 1',
48 description: 'my super description for server 1'
49 }
50 await servers[0].videos.upload({ attributes })
81 } 51 }
82 await uploadVideo(servers[0].url, servers[0].accessToken, video1Attributes)
83 52
84 const video2Attributes = { 53 {
85 name: 'my super name for server 2', 54 const attributes = {
86 description: 'my super description for server 2' 55 name: 'my super name for server 2',
56 description: 'my super description for server 2'
57 }
58 await servers[1].videos.upload({ attributes })
87 } 59 }
88 await uploadVideo(servers[1].url, servers[1].accessToken, video2Attributes)
89 60
90 // Wait videos propagation, server 2 has transcoding enabled 61 // Wait videos propagation, server 2 has transcoding enabled
91 await waitJobs(servers) 62 await waitJobs(servers)
92 63
93 const res = await getVideosList(servers[0].url) 64 const { data } = await servers[0].videos.list()
94 const videos = res.body.data 65 expect(data.length).to.equal(2)
95 66
96 expect(videos.length).to.equal(2) 67 servers[0].store.video = data.find(video => video.name === 'my super name for server 1')
97 68 servers[1].store.video = data.find(video => video.name === 'my super name for server 2')
98 servers[0].video = videos.find(video => video.name === 'my super name for server 1')
99 servers[1].video = videos.find(video => video.name === 'my super name for server 2')
100 }) 69 })
101 70
102 it('Should not have abuses', async function () { 71 it('Should not have abuses', async function () {
103 const res = await getAdminAbusesList({ url: servers[0].url, token: servers[0].accessToken }) 72 const body = await commands[0].getAdminList()
104 73
105 expect(res.body.total).to.equal(0) 74 expect(body.total).to.equal(0)
106 expect(res.body.data).to.be.an('array') 75 expect(body.data).to.be.an('array')
107 expect(res.body.data.length).to.equal(0) 76 expect(body.data.length).to.equal(0)
108 }) 77 })
109 78
110 it('Should report abuse on a local video', async function () { 79 it('Should report abuse on a local video', async function () {
111 this.timeout(15000) 80 this.timeout(15000)
112 81
113 const reason = 'my super bad reason' 82 const reason = 'my super bad reason'
114 await reportAbuse({ url: servers[0].url, token: servers[0].accessToken, videoId: servers[0].video.id, reason }) 83 await commands[0].report({ videoId: servers[0].store.video.id, reason })
115 84
116 // We wait requests propagation, even if the server 1 is not supposed to make a request to server 2 85 // We wait requests propagation, even if the server 1 is not supposed to make a request to server 2
117 await waitJobs(servers) 86 await waitJobs(servers)
118 }) 87 })
119 88
120 it('Should have 1 video abuses on server 1 and 0 on server 2', async function () { 89 it('Should have 1 video abuses on server 1 and 0 on server 2', async function () {
121 const res1 = await getAdminAbusesList({ url: servers[0].url, token: servers[0].accessToken }) 90 {
91 const body = await commands[0].getAdminList()
122 92
123 expect(res1.body.total).to.equal(1) 93 expect(body.total).to.equal(1)
124 expect(res1.body.data).to.be.an('array') 94 expect(body.data).to.be.an('array')
125 expect(res1.body.data.length).to.equal(1) 95 expect(body.data.length).to.equal(1)
126 96
127 const abuse: AdminAbuse = res1.body.data[0] 97 const abuse = body.data[0]
128 expect(abuse.reason).to.equal('my super bad reason') 98 expect(abuse.reason).to.equal('my super bad reason')
129 99
130 expect(abuse.reporterAccount.name).to.equal('root') 100 expect(abuse.reporterAccount.name).to.equal('root')
131 expect(abuse.reporterAccount.host).to.equal(servers[0].host) 101 expect(abuse.reporterAccount.host).to.equal(servers[0].host)
132 102
133 expect(abuse.video.id).to.equal(servers[0].video.id) 103 expect(abuse.video.id).to.equal(servers[0].store.video.id)
134 expect(abuse.video.channel).to.exist 104 expect(abuse.video.channel).to.exist
135 105
136 expect(abuse.comment).to.be.null 106 expect(abuse.comment).to.be.null
137 107
138 expect(abuse.flaggedAccount.name).to.equal('root') 108 expect(abuse.flaggedAccount.name).to.equal('root')
139 expect(abuse.flaggedAccount.host).to.equal(servers[0].host) 109 expect(abuse.flaggedAccount.host).to.equal(servers[0].host)
140 110
141 expect(abuse.video.countReports).to.equal(1) 111 expect(abuse.video.countReports).to.equal(1)
142 expect(abuse.video.nthReport).to.equal(1) 112 expect(abuse.video.nthReport).to.equal(1)
143 113
144 expect(abuse.countReportsForReporter).to.equal(1) 114 expect(abuse.countReportsForReporter).to.equal(1)
145 expect(abuse.countReportsForReportee).to.equal(1) 115 expect(abuse.countReportsForReportee).to.equal(1)
116 }
146 117
147 const res2 = await getAdminAbusesList({ url: servers[1].url, token: servers[1].accessToken }) 118 {
148 expect(res2.body.total).to.equal(0) 119 const body = await commands[1].getAdminList()
149 expect(res2.body.data).to.be.an('array') 120 expect(body.total).to.equal(0)
150 expect(res2.body.data.length).to.equal(0) 121 expect(body.data).to.be.an('array')
122 expect(body.data.length).to.equal(0)
123 }
151 }) 124 })
152 125
153 it('Should report abuse on a remote video', async function () { 126 it('Should report abuse on a remote video', async function () {
154 this.timeout(10000) 127 this.timeout(10000)
155 128
156 const reason = 'my super bad reason 2' 129 const reason = 'my super bad reason 2'
157 const videoId = await getVideoIdFromUUID(servers[0].url, servers[1].video.uuid) 130 const videoId = await servers[0].videos.getId({ uuid: servers[1].store.video.uuid })
158 await reportAbuse({ url: servers[0].url, token: servers[0].accessToken, videoId, reason }) 131 await commands[0].report({ videoId, reason })
159 132
160 // We wait requests propagation 133 // We wait requests propagation
161 await waitJobs(servers) 134 await waitJobs(servers)
162 }) 135 })
163 136
164 it('Should have 2 video abuses on server 1 and 1 on server 2', async function () { 137 it('Should have 2 video abuses on server 1 and 1 on server 2', async function () {
165 const res1 = await getAdminAbusesList({ url: servers[0].url, token: servers[0].accessToken }) 138 {
139 const body = await commands[0].getAdminList()
166 140
167 expect(res1.body.total).to.equal(2) 141 expect(body.total).to.equal(2)
168 expect(res1.body.data.length).to.equal(2) 142 expect(body.data.length).to.equal(2)
169 143
170 const abuse1: AdminAbuse = res1.body.data[0] 144 const abuse1 = body.data[0]
171 expect(abuse1.reason).to.equal('my super bad reason') 145 expect(abuse1.reason).to.equal('my super bad reason')
172 expect(abuse1.reporterAccount.name).to.equal('root') 146 expect(abuse1.reporterAccount.name).to.equal('root')
173 expect(abuse1.reporterAccount.host).to.equal(servers[0].host) 147 expect(abuse1.reporterAccount.host).to.equal(servers[0].host)
174 148
175 expect(abuse1.video.id).to.equal(servers[0].video.id) 149 expect(abuse1.video.id).to.equal(servers[0].store.video.id)
176 expect(abuse1.video.countReports).to.equal(1) 150 expect(abuse1.video.countReports).to.equal(1)
177 expect(abuse1.video.nthReport).to.equal(1) 151 expect(abuse1.video.nthReport).to.equal(1)
178 152
179 expect(abuse1.comment).to.be.null 153 expect(abuse1.comment).to.be.null
180 154
181 expect(abuse1.flaggedAccount.name).to.equal('root') 155 expect(abuse1.flaggedAccount.name).to.equal('root')
182 expect(abuse1.flaggedAccount.host).to.equal(servers[0].host) 156 expect(abuse1.flaggedAccount.host).to.equal(servers[0].host)
183 157
184 expect(abuse1.state.id).to.equal(AbuseState.PENDING) 158 expect(abuse1.state.id).to.equal(AbuseState.PENDING)
185 expect(abuse1.state.label).to.equal('Pending') 159 expect(abuse1.state.label).to.equal('Pending')
186 expect(abuse1.moderationComment).to.be.null 160 expect(abuse1.moderationComment).to.be.null
187 161
188 const abuse2: AdminAbuse = res1.body.data[1] 162 const abuse2 = body.data[1]
189 expect(abuse2.reason).to.equal('my super bad reason 2') 163 expect(abuse2.reason).to.equal('my super bad reason 2')
190 164
191 expect(abuse2.reporterAccount.name).to.equal('root') 165 expect(abuse2.reporterAccount.name).to.equal('root')
192 expect(abuse2.reporterAccount.host).to.equal(servers[0].host) 166 expect(abuse2.reporterAccount.host).to.equal(servers[0].host)
193 167
194 expect(abuse2.video.id).to.equal(servers[1].video.id) 168 expect(abuse2.video.id).to.equal(servers[1].store.video.id)
195 169
196 expect(abuse2.comment).to.be.null 170 expect(abuse2.comment).to.be.null
197 171
198 expect(abuse2.flaggedAccount.name).to.equal('root') 172 expect(abuse2.flaggedAccount.name).to.equal('root')
199 expect(abuse2.flaggedAccount.host).to.equal(servers[1].host) 173 expect(abuse2.flaggedAccount.host).to.equal(servers[1].host)
200 174
201 expect(abuse2.state.id).to.equal(AbuseState.PENDING) 175 expect(abuse2.state.id).to.equal(AbuseState.PENDING)
202 expect(abuse2.state.label).to.equal('Pending') 176 expect(abuse2.state.label).to.equal('Pending')
203 expect(abuse2.moderationComment).to.be.null 177 expect(abuse2.moderationComment).to.be.null
178 }
204 179
205 const res2 = await getAdminAbusesList({ url: servers[1].url, token: servers[1].accessToken }) 180 {
206 expect(res2.body.total).to.equal(1) 181 const body = await commands[1].getAdminList()
207 expect(res2.body.data.length).to.equal(1) 182 expect(body.total).to.equal(1)
183 expect(body.data.length).to.equal(1)
208 184
209 abuseServer2 = res2.body.data[0] 185 abuseServer2 = body.data[0]
210 expect(abuseServer2.reason).to.equal('my super bad reason 2') 186 expect(abuseServer2.reason).to.equal('my super bad reason 2')
211 expect(abuseServer2.reporterAccount.name).to.equal('root') 187 expect(abuseServer2.reporterAccount.name).to.equal('root')
212 expect(abuseServer2.reporterAccount.host).to.equal(servers[0].host) 188 expect(abuseServer2.reporterAccount.host).to.equal(servers[0].host)
213 189
214 expect(abuse2.flaggedAccount.name).to.equal('root') 190 expect(abuseServer2.flaggedAccount.name).to.equal('root')
215 expect(abuse2.flaggedAccount.host).to.equal(servers[1].host) 191 expect(abuseServer2.flaggedAccount.host).to.equal(servers[1].host)
216 192
217 expect(abuseServer2.state.id).to.equal(AbuseState.PENDING) 193 expect(abuseServer2.state.id).to.equal(AbuseState.PENDING)
218 expect(abuseServer2.state.label).to.equal('Pending') 194 expect(abuseServer2.state.label).to.equal('Pending')
219 expect(abuseServer2.moderationComment).to.be.null 195 expect(abuseServer2.moderationComment).to.be.null
196 }
220 }) 197 })
221 198
222 it('Should hide video abuses from blocked accounts', async function () { 199 it('Should hide video abuses from blocked accounts', async function () {
223 this.timeout(10000) 200 this.timeout(10000)
224 201
225 { 202 {
226 const videoId = await getVideoIdFromUUID(servers[1].url, servers[0].video.uuid) 203 const videoId = await servers[1].videos.getId({ uuid: servers[0].store.video.uuid })
227 await reportAbuse({ url: servers[1].url, token: servers[1].accessToken, videoId, reason: 'will mute this' }) 204 await commands[1].report({ videoId, reason: 'will mute this' })
228 await waitJobs(servers) 205 await waitJobs(servers)
229 206
230 const res = await getAdminAbusesList({ url: servers[0].url, token: servers[0].accessToken }) 207 const body = await commands[0].getAdminList()
231 expect(res.body.total).to.equal(3) 208 expect(body.total).to.equal(3)
232 } 209 }
233 210
234 const accountToBlock = 'root@' + servers[1].host 211 const accountToBlock = 'root@' + servers[1].host
235 212
236 { 213 {
237 await addAccountToServerBlocklist(servers[0].url, servers[0].accessToken, accountToBlock) 214 await servers[0].blocklist.addToServerBlocklist({ account: accountToBlock })
238 215
239 const res = await getAdminAbusesList({ url: servers[0].url, token: servers[0].accessToken }) 216 const body = await commands[0].getAdminList()
240 expect(res.body.total).to.equal(2) 217 expect(body.total).to.equal(2)
241 218
242 const abuse = res.body.data.find(a => a.reason === 'will mute this') 219 const abuse = body.data.find(a => a.reason === 'will mute this')
243 expect(abuse).to.be.undefined 220 expect(abuse).to.be.undefined
244 } 221 }
245 222
246 { 223 {
247 await removeAccountFromServerBlocklist(servers[0].url, servers[0].accessToken, accountToBlock) 224 await servers[0].blocklist.removeFromServerBlocklist({ account: accountToBlock })
248 225
249 const res = await getAdminAbusesList({ url: servers[0].url, token: servers[0].accessToken }) 226 const body = await commands[0].getAdminList()
250 expect(res.body.total).to.equal(3) 227 expect(body.total).to.equal(3)
251 } 228 }
252 }) 229 })
253 230
@@ -255,35 +232,35 @@ describe('Test abuses', function () {
255 const serverToBlock = servers[1].host 232 const serverToBlock = servers[1].host
256 233
257 { 234 {
258 await addServerToServerBlocklist(servers[0].url, servers[0].accessToken, servers[1].host) 235 await servers[0].blocklist.addToServerBlocklist({ server: serverToBlock })
259 236
260 const res = await getAdminAbusesList({ url: servers[0].url, token: servers[0].accessToken }) 237 const body = await commands[0].getAdminList()
261 expect(res.body.total).to.equal(2) 238 expect(body.total).to.equal(2)
262 239
263 const abuse = res.body.data.find(a => a.reason === 'will mute this') 240 const abuse = body.data.find(a => a.reason === 'will mute this')
264 expect(abuse).to.be.undefined 241 expect(abuse).to.be.undefined
265 } 242 }
266 243
267 { 244 {
268 await removeServerFromServerBlocklist(servers[0].url, servers[0].accessToken, serverToBlock) 245 await servers[0].blocklist.removeFromServerBlocklist({ server: serverToBlock })
269 246
270 const res = await getAdminAbusesList({ url: servers[0].url, token: servers[0].accessToken }) 247 const body = await commands[0].getAdminList()
271 expect(res.body.total).to.equal(3) 248 expect(body.total).to.equal(3)
272 } 249 }
273 }) 250 })
274 251
275 it('Should keep the video abuse when deleting the video', async function () { 252 it('Should keep the video abuse when deleting the video', async function () {
276 this.timeout(10000) 253 this.timeout(10000)
277 254
278 await removeVideo(servers[1].url, servers[1].accessToken, abuseServer2.video.uuid) 255 await servers[1].videos.remove({ id: abuseServer2.video.uuid })
279 256
280 await waitJobs(servers) 257 await waitJobs(servers)
281 258
282 const res = await getAdminAbusesList({ url: servers[1].url, token: servers[1].accessToken }) 259 const body = await commands[1].getAdminList()
283 expect(res.body.total).to.equal(2, "wrong number of videos returned") 260 expect(body.total).to.equal(2, "wrong number of videos returned")
284 expect(res.body.data).to.have.lengthOf(2, "wrong number of videos returned") 261 expect(body.data).to.have.lengthOf(2, "wrong number of videos returned")
285 262
286 const abuse: AdminAbuse = res.body.data[0] 263 const abuse = body.data[0]
287 expect(abuse.id).to.equal(abuseServer2.id, "wrong origin server id for first video") 264 expect(abuse.id).to.equal(abuseServer2.id, "wrong origin server id for first video")
288 expect(abuse.video.id).to.equal(abuseServer2.video.id, "wrong video id") 265 expect(abuse.video.id).to.equal(abuseServer2.video.id, "wrong video id")
289 expect(abuse.video.channel).to.exist 266 expect(abuse.video.channel).to.exist
@@ -295,39 +272,36 @@ describe('Test abuses', function () {
295 272
296 // register a second user to have two reporters/reportees 273 // register a second user to have two reporters/reportees
297 const user = { username: 'user2', password: 'password' } 274 const user = { username: 'user2', password: 'password' }
298 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, ...user }) 275 await servers[0].users.create({ ...user })
299 const userAccessToken = await userLogin(servers[0], user) 276 const userAccessToken = await servers[0].login.getAccessToken(user)
300 277
301 // upload a third video via this user 278 // upload a third video via this user
302 const video3Attributes = { 279 const attributes = {
303 name: 'my second super name for server 1', 280 name: 'my second super name for server 1',
304 description: 'my second super description for server 1' 281 description: 'my second super description for server 1'
305 } 282 }
306 await uploadVideo(servers[0].url, userAccessToken, video3Attributes) 283 const { id } = await servers[0].videos.upload({ token: userAccessToken, attributes })
307 284 const video3Id = id
308 const res1 = await getVideosList(servers[0].url)
309 const videos = res1.body.data
310 const video3 = videos.find(video => video.name === 'my second super name for server 1')
311 285
312 // resume with the test 286 // resume with the test
313 const reason3 = 'my super bad reason 3' 287 const reason3 = 'my super bad reason 3'
314 await reportAbuse({ url: servers[0].url, token: servers[0].accessToken, videoId: video3.id, reason: reason3 }) 288 await commands[0].report({ videoId: video3Id, reason: reason3 })
315 289
316 const reason4 = 'my super bad reason 4' 290 const reason4 = 'my super bad reason 4'
317 await reportAbuse({ url: servers[0].url, token: userAccessToken, videoId: servers[0].video.id, reason: reason4 }) 291 await commands[0].report({ token: userAccessToken, videoId: servers[0].store.video.id, reason: reason4 })
318 292
319 { 293 {
320 const res2 = await getAdminAbusesList({ url: servers[0].url, token: servers[0].accessToken }) 294 const body = await commands[0].getAdminList()
321 const abuses = res2.body.data as AdminAbuse[] 295 const abuses = body.data
322 296
323 const abuseVideo3 = res2.body.data.find(a => a.video.id === video3.id) 297 const abuseVideo3 = body.data.find(a => a.video.id === video3Id)
324 expect(abuseVideo3).to.not.be.undefined 298 expect(abuseVideo3).to.not.be.undefined
325 expect(abuseVideo3.video.countReports).to.equal(1, "wrong reports count for video 3") 299 expect(abuseVideo3.video.countReports).to.equal(1, "wrong reports count for video 3")
326 expect(abuseVideo3.video.nthReport).to.equal(1, "wrong report position in report list for video 3") 300 expect(abuseVideo3.video.nthReport).to.equal(1, "wrong report position in report list for video 3")
327 expect(abuseVideo3.countReportsForReportee).to.equal(1, "wrong reports count for reporter on video 3 abuse") 301 expect(abuseVideo3.countReportsForReportee).to.equal(1, "wrong reports count for reporter on video 3 abuse")
328 expect(abuseVideo3.countReportsForReporter).to.equal(3, "wrong reports count for reportee on video 3 abuse") 302 expect(abuseVideo3.countReportsForReporter).to.equal(3, "wrong reports count for reportee on video 3 abuse")
329 303
330 const abuseServer1 = abuses.find(a => a.video.id === servers[0].video.id) 304 const abuseServer1 = abuses.find(a => a.video.id === servers[0].store.video.id)
331 expect(abuseServer1.countReportsForReportee).to.equal(3, "wrong reports count for reporter on video 1 abuse") 305 expect(abuseServer1.countReportsForReportee).to.equal(3, "wrong reports count for reporter on video 1 abuse")
332 } 306 }
333 }) 307 })
@@ -337,20 +311,18 @@ describe('Test abuses', function () {
337 311
338 const reason5 = 'my super bad reason 5' 312 const reason5 = 'my super bad reason 5'
339 const predefinedReasons5: AbusePredefinedReasonsString[] = [ 'violentOrRepulsive', 'captions' ] 313 const predefinedReasons5: AbusePredefinedReasonsString[] = [ 'violentOrRepulsive', 'captions' ]
340 const createdAbuse = (await reportAbuse({ 314 const createRes = await commands[0].report({
341 url: servers[0].url, 315 videoId: servers[0].store.video.id,
342 token: servers[0].accessToken,
343 videoId: servers[0].video.id,
344 reason: reason5, 316 reason: reason5,
345 predefinedReasons: predefinedReasons5, 317 predefinedReasons: predefinedReasons5,
346 startAt: 1, 318 startAt: 1,
347 endAt: 5 319 endAt: 5
348 })).body.abuse 320 })
349 321
350 const res = await getAdminAbusesList({ url: servers[0].url, token: servers[0].accessToken }) 322 const body = await commands[0].getAdminList()
351 323
352 { 324 {
353 const abuse = (res.body.data as AdminAbuse[]).find(a => a.id === createdAbuse.id) 325 const abuse = body.data.find(a => a.id === createRes.abuse.id)
354 expect(abuse.reason).to.equals(reason5) 326 expect(abuse.reason).to.equals(reason5)
355 expect(abuse.predefinedReasons).to.deep.equals(predefinedReasons5, "predefined reasons do not match the one reported") 327 expect(abuse.predefinedReasons).to.deep.equals(predefinedReasons5, "predefined reasons do not match the one reported")
356 expect(abuse.video.startAt).to.equal(1, "starting timestamp doesn't match the one reported") 328 expect(abuse.video.startAt).to.equal(1, "starting timestamp doesn't match the one reported")
@@ -361,37 +333,30 @@ describe('Test abuses', function () {
361 it('Should delete the video abuse', async function () { 333 it('Should delete the video abuse', async function () {
362 this.timeout(10000) 334 this.timeout(10000)
363 335
364 await deleteAbuse(servers[1].url, servers[1].accessToken, abuseServer2.id) 336 await commands[1].delete({ abuseId: abuseServer2.id })
365 337
366 await waitJobs(servers) 338 await waitJobs(servers)
367 339
368 { 340 {
369 const res = await getAdminAbusesList({ url: servers[1].url, token: servers[1].accessToken }) 341 const body = await commands[1].getAdminList()
370 expect(res.body.total).to.equal(1) 342 expect(body.total).to.equal(1)
371 expect(res.body.data.length).to.equal(1) 343 expect(body.data.length).to.equal(1)
372 expect(res.body.data[0].id).to.not.equal(abuseServer2.id) 344 expect(body.data[0].id).to.not.equal(abuseServer2.id)
373 } 345 }
374 346
375 { 347 {
376 const res = await getAdminAbusesList({ url: servers[0].url, token: servers[0].accessToken }) 348 const body = await commands[0].getAdminList()
377 expect(res.body.total).to.equal(6) 349 expect(body.total).to.equal(6)
378 } 350 }
379 }) 351 })
380 352
381 it('Should list and filter video abuses', async function () { 353 it('Should list and filter video abuses', async function () {
382 this.timeout(10000) 354 this.timeout(10000)
383 355
384 async function list (query: Omit<Parameters<typeof getAdminAbusesList>[0], 'url' | 'token'>) { 356 async function list (query: Parameters<AbusesCommand['getAdminList']>[0]) {
385 const options = { 357 const body = await commands[0].getAdminList(query)
386 url: servers[0].url,
387 token: servers[0].accessToken
388 }
389
390 Object.assign(options, query)
391 358
392 const res = await getAdminAbusesList(options) 359 return body.data
393
394 return res.body.data as AdminAbuse[]
395 } 360 }
396 361
397 expect(await list({ id: 56 })).to.have.lengthOf(0) 362 expect(await list({ id: 56 })).to.have.lengthOf(0)
@@ -424,24 +389,24 @@ describe('Test abuses', function () {
424 389
425 describe('Comment abuses', function () { 390 describe('Comment abuses', function () {
426 391
427 async function getComment (url: string, videoIdArg: number | string) { 392 async function getComment (server: PeerTubeServer, videoIdArg: number | string) {
428 const videoId = typeof videoIdArg === 'string' 393 const videoId = typeof videoIdArg === 'string'
429 ? await getVideoIdFromUUID(url, videoIdArg) 394 ? await server.videos.getId({ uuid: videoIdArg })
430 : videoIdArg 395 : videoIdArg
431 396
432 const res = await getVideoCommentThreads(url, videoId, 0, 5) 397 const { data } = await server.comments.listThreads({ videoId })
433 398
434 return res.body.data[0] as VideoComment 399 return data[0]
435 } 400 }
436 401
437 before(async function () { 402 before(async function () {
438 this.timeout(50000) 403 this.timeout(50000)
439 404
440 servers[0].video = await uploadVideoAndGetId({ server: servers[0], videoName: 'server 1' }) 405 servers[0].store.video = await servers[0].videos.quickUpload({ name: 'server 1' })
441 servers[1].video = await uploadVideoAndGetId({ server: servers[1], videoName: 'server 2' }) 406 servers[1].store.video = await servers[1].videos.quickUpload({ name: 'server 2' })
442 407
443 await addVideoCommentThread(servers[0].url, servers[0].accessToken, servers[0].video.id, 'comment server 1') 408 await servers[0].comments.createThread({ videoId: servers[0].store.video.id, text: 'comment server 1' })
444 await addVideoCommentThread(servers[1].url, servers[1].accessToken, servers[1].video.id, 'comment server 2') 409 await servers[1].comments.createThread({ videoId: servers[1].store.video.id, text: 'comment server 2' })
445 410
446 await waitJobs(servers) 411 await waitJobs(servers)
447 }) 412 })
@@ -449,23 +414,23 @@ describe('Test abuses', function () {
449 it('Should report abuse on a comment', async function () { 414 it('Should report abuse on a comment', async function () {
450 this.timeout(15000) 415 this.timeout(15000)
451 416
452 const comment = await getComment(servers[0].url, servers[0].video.id) 417 const comment = await getComment(servers[0], servers[0].store.video.id)
453 418
454 const reason = 'it is a bad comment' 419 const reason = 'it is a bad comment'
455 await reportAbuse({ url: servers[0].url, token: servers[0].accessToken, commentId: comment.id, reason }) 420 await commands[0].report({ commentId: comment.id, reason })
456 421
457 await waitJobs(servers) 422 await waitJobs(servers)
458 }) 423 })
459 424
460 it('Should have 1 comment abuse on server 1 and 0 on server 2', async function () { 425 it('Should have 1 comment abuse on server 1 and 0 on server 2', async function () {
461 { 426 {
462 const comment = await getComment(servers[0].url, servers[0].video.id) 427 const comment = await getComment(servers[0], servers[0].store.video.id)
463 const res = await getAdminAbusesList({ url: servers[0].url, token: servers[0].accessToken, filter: 'comment' }) 428 const body = await commands[0].getAdminList({ filter: 'comment' })
464 429
465 expect(res.body.total).to.equal(1) 430 expect(body.total).to.equal(1)
466 expect(res.body.data).to.have.lengthOf(1) 431 expect(body.data).to.have.lengthOf(1)
467 432
468 const abuse: AdminAbuse = res.body.data[0] 433 const abuse = body.data[0]
469 expect(abuse.reason).to.equal('it is a bad comment') 434 expect(abuse.reason).to.equal('it is a bad comment')
470 435
471 expect(abuse.reporterAccount.name).to.equal('root') 436 expect(abuse.reporterAccount.name).to.equal('root')
@@ -477,98 +442,102 @@ describe('Test abuses', function () {
477 expect(abuse.comment.id).to.equal(comment.id) 442 expect(abuse.comment.id).to.equal(comment.id)
478 expect(abuse.comment.text).to.equal(comment.text) 443 expect(abuse.comment.text).to.equal(comment.text)
479 expect(abuse.comment.video.name).to.equal('server 1') 444 expect(abuse.comment.video.name).to.equal('server 1')
480 expect(abuse.comment.video.id).to.equal(servers[0].video.id) 445 expect(abuse.comment.video.id).to.equal(servers[0].store.video.id)
481 expect(abuse.comment.video.uuid).to.equal(servers[0].video.uuid) 446 expect(abuse.comment.video.uuid).to.equal(servers[0].store.video.uuid)
482 447
483 expect(abuse.countReportsForReporter).to.equal(5) 448 expect(abuse.countReportsForReporter).to.equal(5)
484 expect(abuse.countReportsForReportee).to.equal(5) 449 expect(abuse.countReportsForReportee).to.equal(5)
485 } 450 }
486 451
487 { 452 {
488 const res = await getAdminAbusesList({ url: servers[1].url, token: servers[1].accessToken, filter: 'comment' }) 453 const body = await commands[1].getAdminList({ filter: 'comment' })
489 expect(res.body.total).to.equal(0) 454 expect(body.total).to.equal(0)
490 expect(res.body.data.length).to.equal(0) 455 expect(body.data.length).to.equal(0)
491 } 456 }
492 }) 457 })
493 458
494 it('Should report abuse on a remote comment', async function () { 459 it('Should report abuse on a remote comment', async function () {
495 this.timeout(10000) 460 this.timeout(10000)
496 461
497 const comment = await getComment(servers[0].url, servers[1].video.uuid) 462 const comment = await getComment(servers[0], servers[1].store.video.uuid)
498 463
499 const reason = 'it is a really bad comment' 464 const reason = 'it is a really bad comment'
500 await reportAbuse({ url: servers[0].url, token: servers[0].accessToken, commentId: comment.id, reason }) 465 await commands[0].report({ commentId: comment.id, reason })
501 466
502 await waitJobs(servers) 467 await waitJobs(servers)
503 }) 468 })
504 469
505 it('Should have 2 comment abuses on server 1 and 1 on server 2', async function () { 470 it('Should have 2 comment abuses on server 1 and 1 on server 2', async function () {
506 const commentServer2 = await getComment(servers[0].url, servers[1].video.id) 471 const commentServer2 = await getComment(servers[0], servers[1].store.video.id)
507 472
508 const res1 = await getAdminAbusesList({ url: servers[0].url, token: servers[0].accessToken, filter: 'comment' }) 473 {
509 expect(res1.body.total).to.equal(2) 474 const body = await commands[0].getAdminList({ filter: 'comment' })
510 expect(res1.body.data.length).to.equal(2) 475 expect(body.total).to.equal(2)
476 expect(body.data.length).to.equal(2)
511 477
512 const abuse: AdminAbuse = res1.body.data[0] 478 const abuse = body.data[0]
513 expect(abuse.reason).to.equal('it is a bad comment') 479 expect(abuse.reason).to.equal('it is a bad comment')
514 expect(abuse.countReportsForReporter).to.equal(6) 480 expect(abuse.countReportsForReporter).to.equal(6)
515 expect(abuse.countReportsForReportee).to.equal(5) 481 expect(abuse.countReportsForReportee).to.equal(5)
516 482
517 const abuse2: AdminAbuse = res1.body.data[1] 483 const abuse2 = body.data[1]
518 484
519 expect(abuse2.reason).to.equal('it is a really bad comment') 485 expect(abuse2.reason).to.equal('it is a really bad comment')
520 486
521 expect(abuse2.reporterAccount.name).to.equal('root') 487 expect(abuse2.reporterAccount.name).to.equal('root')
522 expect(abuse2.reporterAccount.host).to.equal(servers[0].host) 488 expect(abuse2.reporterAccount.host).to.equal(servers[0].host)
523 489
524 expect(abuse2.video).to.be.null 490 expect(abuse2.video).to.be.null
525 491
526 expect(abuse2.comment.deleted).to.be.false 492 expect(abuse2.comment.deleted).to.be.false
527 expect(abuse2.comment.id).to.equal(commentServer2.id) 493 expect(abuse2.comment.id).to.equal(commentServer2.id)
528 expect(abuse2.comment.text).to.equal(commentServer2.text) 494 expect(abuse2.comment.text).to.equal(commentServer2.text)
529 expect(abuse2.comment.video.name).to.equal('server 2') 495 expect(abuse2.comment.video.name).to.equal('server 2')
530 expect(abuse2.comment.video.uuid).to.equal(servers[1].video.uuid) 496 expect(abuse2.comment.video.uuid).to.equal(servers[1].store.video.uuid)
531 497
532 expect(abuse2.state.id).to.equal(AbuseState.PENDING) 498 expect(abuse2.state.id).to.equal(AbuseState.PENDING)
533 expect(abuse2.state.label).to.equal('Pending') 499 expect(abuse2.state.label).to.equal('Pending')
534 500
535 expect(abuse2.moderationComment).to.be.null 501 expect(abuse2.moderationComment).to.be.null
536 502
537 expect(abuse2.countReportsForReporter).to.equal(6) 503 expect(abuse2.countReportsForReporter).to.equal(6)
538 expect(abuse2.countReportsForReportee).to.equal(2) 504 expect(abuse2.countReportsForReportee).to.equal(2)
505 }
539 506
540 const res2 = await getAdminAbusesList({ url: servers[1].url, token: servers[1].accessToken, filter: 'comment' }) 507 {
541 expect(res2.body.total).to.equal(1) 508 const body = await commands[1].getAdminList({ filter: 'comment' })
542 expect(res2.body.data.length).to.equal(1) 509 expect(body.total).to.equal(1)
510 expect(body.data.length).to.equal(1)
543 511
544 abuseServer2 = res2.body.data[0] 512 abuseServer2 = body.data[0]
545 expect(abuseServer2.reason).to.equal('it is a really bad comment') 513 expect(abuseServer2.reason).to.equal('it is a really bad comment')
546 expect(abuseServer2.reporterAccount.name).to.equal('root') 514 expect(abuseServer2.reporterAccount.name).to.equal('root')
547 expect(abuseServer2.reporterAccount.host).to.equal(servers[0].host) 515 expect(abuseServer2.reporterAccount.host).to.equal(servers[0].host)
548 516
549 expect(abuseServer2.state.id).to.equal(AbuseState.PENDING) 517 expect(abuseServer2.state.id).to.equal(AbuseState.PENDING)
550 expect(abuseServer2.state.label).to.equal('Pending') 518 expect(abuseServer2.state.label).to.equal('Pending')
551 519
552 expect(abuseServer2.moderationComment).to.be.null 520 expect(abuseServer2.moderationComment).to.be.null
553 521
554 expect(abuseServer2.countReportsForReporter).to.equal(1) 522 expect(abuseServer2.countReportsForReporter).to.equal(1)
555 expect(abuseServer2.countReportsForReportee).to.equal(1) 523 expect(abuseServer2.countReportsForReportee).to.equal(1)
524 }
556 }) 525 })
557 526
558 it('Should keep the comment abuse when deleting the comment', async function () { 527 it('Should keep the comment abuse when deleting the comment', async function () {
559 this.timeout(10000) 528 this.timeout(10000)
560 529
561 const commentServer2 = await getComment(servers[0].url, servers[1].video.id) 530 const commentServer2 = await getComment(servers[0], servers[1].store.video.id)
562 531
563 await deleteVideoComment(servers[0].url, servers[0].accessToken, servers[1].video.uuid, commentServer2.id) 532 await servers[0].comments.delete({ videoId: servers[1].store.video.uuid, commentId: commentServer2.id })
564 533
565 await waitJobs(servers) 534 await waitJobs(servers)
566 535
567 const res = await getAdminAbusesList({ url: servers[0].url, token: servers[0].accessToken, filter: 'comment' }) 536 const body = await commands[0].getAdminList({ filter: 'comment' })
568 expect(res.body.total).to.equal(2) 537 expect(body.total).to.equal(2)
569 expect(res.body.data).to.have.lengthOf(2) 538 expect(body.data).to.have.lengthOf(2)
570 539
571 const abuse = (res.body.data as AdminAbuse[]).find(a => a.comment?.id === commentServer2.id) 540 const abuse = body.data.find(a => a.comment?.id === commentServer2.id)
572 expect(abuse).to.not.be.undefined 541 expect(abuse).to.not.be.undefined
573 542
574 expect(abuse.comment.text).to.be.empty 543 expect(abuse.comment.text).to.be.empty
@@ -579,72 +548,60 @@ describe('Test abuses', function () {
579 it('Should delete the comment abuse', async function () { 548 it('Should delete the comment abuse', async function () {
580 this.timeout(10000) 549 this.timeout(10000)
581 550
582 await deleteAbuse(servers[1].url, servers[1].accessToken, abuseServer2.id) 551 await commands[1].delete({ abuseId: abuseServer2.id })
583 552
584 await waitJobs(servers) 553 await waitJobs(servers)
585 554
586 { 555 {
587 const res = await getAdminAbusesList({ url: servers[1].url, token: servers[1].accessToken, filter: 'comment' }) 556 const body = await commands[1].getAdminList({ filter: 'comment' })
588 expect(res.body.total).to.equal(0) 557 expect(body.total).to.equal(0)
589 expect(res.body.data.length).to.equal(0) 558 expect(body.data.length).to.equal(0)
590 } 559 }
591 560
592 { 561 {
593 const res = await getAdminAbusesList({ url: servers[0].url, token: servers[0].accessToken, filter: 'comment' }) 562 const body = await commands[0].getAdminList({ filter: 'comment' })
594 expect(res.body.total).to.equal(2) 563 expect(body.total).to.equal(2)
595 } 564 }
596 }) 565 })
597 566
598 it('Should list and filter video abuses', async function () { 567 it('Should list and filter video abuses', async function () {
599 { 568 {
600 const res = await getAdminAbusesList({ 569 const body = await commands[0].getAdminList({ filter: 'comment', searchReportee: 'foo' })
601 url: servers[0].url, 570 expect(body.total).to.equal(0)
602 token: servers[0].accessToken,
603 filter: 'comment',
604 searchReportee: 'foo'
605 })
606 expect(res.body.total).to.equal(0)
607 } 571 }
608 572
609 { 573 {
610 const res = await getAdminAbusesList({ 574 const body = await commands[0].getAdminList({ filter: 'comment', searchReportee: 'ot' })
611 url: servers[0].url, 575 expect(body.total).to.equal(2)
612 token: servers[0].accessToken,
613 filter: 'comment',
614 searchReportee: 'ot'
615 })
616 expect(res.body.total).to.equal(2)
617 } 576 }
618 577
619 { 578 {
620 const baseParams = { url: servers[0].url, token: servers[0].accessToken, filter: 'comment' as AbuseFilter, start: 1, count: 1 } 579 const body = await commands[0].getAdminList({ filter: 'comment', start: 1, count: 1, sort: 'createdAt' })
621 580 expect(body.data).to.have.lengthOf(1)
622 const res1 = await getAdminAbusesList(immutableAssign(baseParams, { sort: 'createdAt' })) 581 expect(body.data[0].comment.text).to.be.empty
623 expect(res1.body.data).to.have.lengthOf(1) 582 }
624 expect(res1.body.data[0].comment.text).to.be.empty
625 583
626 const res2 = await getAdminAbusesList(immutableAssign(baseParams, { sort: '-createdAt' })) 584 {
627 expect(res2.body.data).to.have.lengthOf(1) 585 const body = await commands[0].getAdminList({ filter: 'comment', start: 1, count: 1, sort: '-createdAt' })
628 expect(res2.body.data[0].comment.text).to.equal('comment server 1') 586 expect(body.data).to.have.lengthOf(1)
587 expect(body.data[0].comment.text).to.equal('comment server 1')
629 } 588 }
630 }) 589 })
631 }) 590 })
632 591
633 describe('Account abuses', function () { 592 describe('Account abuses', function () {
634 593
635 async function getAccountFromServer (url: string, name: string, server: ServerInfo) { 594 function getAccountFromServer (server: PeerTubeServer, targetName: string, targetServer: PeerTubeServer) {
636 const res = await getAccount(url, name + '@' + server.host) 595 return server.accounts.get({ accountName: targetName + '@' + targetServer.host })
637
638 return res.body as Account
639 } 596 }
640 597
641 before(async function () { 598 before(async function () {
642 this.timeout(50000) 599 this.timeout(50000)
643 600
644 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: 'user_1', password: 'donald' }) 601 await servers[0].users.create({ username: 'user_1', password: 'donald' })
645 602
646 const token = await generateUserAccessToken(servers[1], 'user_2') 603 const token = await servers[1].users.generateUserAndToken('user_2')
647 await uploadVideo(servers[1].url, token, { name: 'super video' }) 604 await servers[1].videos.upload({ token, attributes: { name: 'super video' } })
648 605
649 await waitJobs(servers) 606 await waitJobs(servers)
650 }) 607 })
@@ -652,22 +609,22 @@ describe('Test abuses', function () {
652 it('Should report abuse on an account', async function () { 609 it('Should report abuse on an account', async function () {
653 this.timeout(15000) 610 this.timeout(15000)
654 611
655 const account = await getAccountFromServer(servers[0].url, 'user_1', servers[0]) 612 const account = await getAccountFromServer(servers[0], 'user_1', servers[0])
656 613
657 const reason = 'it is a bad account' 614 const reason = 'it is a bad account'
658 await reportAbuse({ url: servers[0].url, token: servers[0].accessToken, accountId: account.id, reason }) 615 await commands[0].report({ accountId: account.id, reason })
659 616
660 await waitJobs(servers) 617 await waitJobs(servers)
661 }) 618 })
662 619
663 it('Should have 1 account abuse on server 1 and 0 on server 2', async function () { 620 it('Should have 1 account abuse on server 1 and 0 on server 2', async function () {
664 { 621 {
665 const res = await getAdminAbusesList({ url: servers[0].url, token: servers[0].accessToken, filter: 'account' }) 622 const body = await commands[0].getAdminList({ filter: 'account' })
666 623
667 expect(res.body.total).to.equal(1) 624 expect(body.total).to.equal(1)
668 expect(res.body.data).to.have.lengthOf(1) 625 expect(body.data).to.have.lengthOf(1)
669 626
670 const abuse: AdminAbuse = res.body.data[0] 627 const abuse = body.data[0]
671 expect(abuse.reason).to.equal('it is a bad account') 628 expect(abuse.reason).to.equal('it is a bad account')
672 629
673 expect(abuse.reporterAccount.name).to.equal('root') 630 expect(abuse.reporterAccount.name).to.equal('root')
@@ -681,96 +638,100 @@ describe('Test abuses', function () {
681 } 638 }
682 639
683 { 640 {
684 const res = await getAdminAbusesList({ url: servers[1].url, token: servers[1].accessToken, filter: 'comment' }) 641 const body = await commands[1].getAdminList({ filter: 'comment' })
685 expect(res.body.total).to.equal(0) 642 expect(body.total).to.equal(0)
686 expect(res.body.data.length).to.equal(0) 643 expect(body.data.length).to.equal(0)
687 } 644 }
688 }) 645 })
689 646
690 it('Should report abuse on a remote account', async function () { 647 it('Should report abuse on a remote account', async function () {
691 this.timeout(10000) 648 this.timeout(10000)
692 649
693 const account = await getAccountFromServer(servers[0].url, 'user_2', servers[1]) 650 const account = await getAccountFromServer(servers[0], 'user_2', servers[1])
694 651
695 const reason = 'it is a really bad account' 652 const reason = 'it is a really bad account'
696 await reportAbuse({ url: servers[0].url, token: servers[0].accessToken, accountId: account.id, reason }) 653 await commands[0].report({ accountId: account.id, reason })
697 654
698 await waitJobs(servers) 655 await waitJobs(servers)
699 }) 656 })
700 657
701 it('Should have 2 comment abuses on server 1 and 1 on server 2', async function () { 658 it('Should have 2 comment abuses on server 1 and 1 on server 2', async function () {
702 const res1 = await getAdminAbusesList({ url: servers[0].url, token: servers[0].accessToken, filter: 'account' }) 659 {
703 expect(res1.body.total).to.equal(2) 660 const body = await commands[0].getAdminList({ filter: 'account' })
704 expect(res1.body.data.length).to.equal(2) 661 expect(body.total).to.equal(2)
662 expect(body.data.length).to.equal(2)
705 663
706 const abuse: AdminAbuse = res1.body.data[0] 664 const abuse: AdminAbuse = body.data[0]
707 expect(abuse.reason).to.equal('it is a bad account') 665 expect(abuse.reason).to.equal('it is a bad account')
708 666
709 const abuse2: AdminAbuse = res1.body.data[1] 667 const abuse2: AdminAbuse = body.data[1]
710 expect(abuse2.reason).to.equal('it is a really bad account') 668 expect(abuse2.reason).to.equal('it is a really bad account')
711 669
712 expect(abuse2.reporterAccount.name).to.equal('root') 670 expect(abuse2.reporterAccount.name).to.equal('root')
713 expect(abuse2.reporterAccount.host).to.equal(servers[0].host) 671 expect(abuse2.reporterAccount.host).to.equal(servers[0].host)
714 672
715 expect(abuse2.video).to.be.null 673 expect(abuse2.video).to.be.null
716 expect(abuse2.comment).to.be.null 674 expect(abuse2.comment).to.be.null
717 675
718 expect(abuse2.state.id).to.equal(AbuseState.PENDING) 676 expect(abuse2.state.id).to.equal(AbuseState.PENDING)
719 expect(abuse2.state.label).to.equal('Pending') 677 expect(abuse2.state.label).to.equal('Pending')
720 678
721 expect(abuse2.moderationComment).to.be.null 679 expect(abuse2.moderationComment).to.be.null
680 }
722 681
723 const res2 = await getAdminAbusesList({ url: servers[1].url, token: servers[1].accessToken, filter: 'account' }) 682 {
724 expect(res2.body.total).to.equal(1) 683 const body = await commands[1].getAdminList({ filter: 'account' })
725 expect(res2.body.data.length).to.equal(1) 684 expect(body.total).to.equal(1)
685 expect(body.data.length).to.equal(1)
726 686
727 abuseServer2 = res2.body.data[0] 687 abuseServer2 = body.data[0]
728 688
729 expect(abuseServer2.reason).to.equal('it is a really bad account') 689 expect(abuseServer2.reason).to.equal('it is a really bad account')
730 690
731 expect(abuseServer2.reporterAccount.name).to.equal('root') 691 expect(abuseServer2.reporterAccount.name).to.equal('root')
732 expect(abuseServer2.reporterAccount.host).to.equal(servers[0].host) 692 expect(abuseServer2.reporterAccount.host).to.equal(servers[0].host)
733 693
734 expect(abuseServer2.state.id).to.equal(AbuseState.PENDING) 694 expect(abuseServer2.state.id).to.equal(AbuseState.PENDING)
735 expect(abuseServer2.state.label).to.equal('Pending') 695 expect(abuseServer2.state.label).to.equal('Pending')
736 696
737 expect(abuseServer2.moderationComment).to.be.null 697 expect(abuseServer2.moderationComment).to.be.null
698 }
738 }) 699 })
739 700
740 it('Should keep the account abuse when deleting the account', async function () { 701 it('Should keep the account abuse when deleting the account', async function () {
741 this.timeout(10000) 702 this.timeout(10000)
742 703
743 const account = await getAccountFromServer(servers[1].url, 'user_2', servers[1]) 704 const account = await getAccountFromServer(servers[1], 'user_2', servers[1])
744 await removeUser(servers[1].url, account.userId, servers[1].accessToken) 705 await servers[1].users.remove({ userId: account.userId })
745 706
746 await waitJobs(servers) 707 await waitJobs(servers)
747 708
748 const res = await getAdminAbusesList({ url: servers[0].url, token: servers[0].accessToken, filter: 'account' }) 709 const body = await commands[0].getAdminList({ filter: 'account' })
749 expect(res.body.total).to.equal(2) 710 expect(body.total).to.equal(2)
750 expect(res.body.data).to.have.lengthOf(2) 711 expect(body.data).to.have.lengthOf(2)
751 712
752 const abuse = (res.body.data as AdminAbuse[]).find(a => a.reason === 'it is a really bad account') 713 const abuse = body.data.find(a => a.reason === 'it is a really bad account')
753 expect(abuse).to.not.be.undefined 714 expect(abuse).to.not.be.undefined
754 }) 715 })
755 716
756 it('Should delete the account abuse', async function () { 717 it('Should delete the account abuse', async function () {
757 this.timeout(10000) 718 this.timeout(10000)
758 719
759 await deleteAbuse(servers[1].url, servers[1].accessToken, abuseServer2.id) 720 await commands[1].delete({ abuseId: abuseServer2.id })
760 721
761 await waitJobs(servers) 722 await waitJobs(servers)
762 723
763 { 724 {
764 const res = await getAdminAbusesList({ url: servers[1].url, token: servers[1].accessToken, filter: 'account' }) 725 const body = await commands[1].getAdminList({ filter: 'account' })
765 expect(res.body.total).to.equal(0) 726 expect(body.total).to.equal(0)
766 expect(res.body.data.length).to.equal(0) 727 expect(body.data.length).to.equal(0)
767 } 728 }
768 729
769 { 730 {
770 const res = await getAdminAbusesList({ url: servers[0].url, token: servers[0].accessToken, filter: 'account' }) 731 const body = await commands[0].getAdminList({ filter: 'account' })
771 expect(res.body.total).to.equal(2) 732 expect(body.total).to.equal(2)
772 733
773 abuseServer1 = res.body.data[0] 734 abuseServer1 = body.data[0]
774 } 735 }
775 }) 736 })
776 }) 737 })
@@ -778,20 +739,18 @@ describe('Test abuses', function () {
778 describe('Common actions on abuses', function () { 739 describe('Common actions on abuses', function () {
779 740
780 it('Should update the state of an abuse', async function () { 741 it('Should update the state of an abuse', async function () {
781 const body = { state: AbuseState.REJECTED } 742 await commands[0].update({ abuseId: abuseServer1.id, body: { state: AbuseState.REJECTED } })
782 await updateAbuse(servers[0].url, servers[0].accessToken, abuseServer1.id, body)
783 743
784 const res = await getAdminAbusesList({ url: servers[0].url, token: servers[0].accessToken, id: abuseServer1.id }) 744 const body = await commands[0].getAdminList({ id: abuseServer1.id })
785 expect(res.body.data[0].state.id).to.equal(AbuseState.REJECTED) 745 expect(body.data[0].state.id).to.equal(AbuseState.REJECTED)
786 }) 746 })
787 747
788 it('Should add a moderation comment', async function () { 748 it('Should add a moderation comment', async function () {
789 const body = { state: AbuseState.ACCEPTED, moderationComment: 'It is valid' } 749 await commands[0].update({ abuseId: abuseServer1.id, body: { state: AbuseState.ACCEPTED, moderationComment: 'Valid' } })
790 await updateAbuse(servers[0].url, servers[0].accessToken, abuseServer1.id, body)
791 750
792 const res = await getAdminAbusesList({ url: servers[0].url, token: servers[0].accessToken, id: abuseServer1.id }) 751 const body = await commands[0].getAdminList({ id: abuseServer1.id })
793 expect(res.body.data[0].state.id).to.equal(AbuseState.ACCEPTED) 752 expect(body.data[0].state.id).to.equal(AbuseState.ACCEPTED)
794 expect(res.body.data[0].moderationComment).to.equal('It is valid') 753 expect(body.data[0].moderationComment).to.equal('Valid')
795 }) 754 })
796 }) 755 })
797 756
@@ -800,20 +759,20 @@ describe('Test abuses', function () {
800 let userAccessToken: string 759 let userAccessToken: string
801 760
802 before(async function () { 761 before(async function () {
803 userAccessToken = await generateUserAccessToken(servers[0], 'user_42') 762 userAccessToken = await servers[0].users.generateUserAndToken('user_42')
804 763
805 await reportAbuse({ url: servers[0].url, token: userAccessToken, videoId: servers[0].video.id, reason: 'user reason 1' }) 764 await commands[0].report({ token: userAccessToken, videoId: servers[0].store.video.id, reason: 'user reason 1' })
806 765
807 const videoId = await getVideoIdFromUUID(servers[0].url, servers[1].video.uuid) 766 const videoId = await servers[0].videos.getId({ uuid: servers[1].store.video.uuid })
808 await reportAbuse({ url: servers[0].url, token: userAccessToken, videoId, reason: 'user reason 2' }) 767 await commands[0].report({ token: userAccessToken, videoId, reason: 'user reason 2' })
809 }) 768 })
810 769
811 it('Should correctly list my abuses', async function () { 770 it('Should correctly list my abuses', async function () {
812 { 771 {
813 const res = await getUserAbusesList({ url: servers[0].url, token: userAccessToken, start: 0, count: 5, sort: 'createdAt' }) 772 const body = await commands[0].getUserList({ token: userAccessToken, start: 0, count: 5, sort: 'createdAt' })
814 expect(res.body.total).to.equal(2) 773 expect(body.total).to.equal(2)
815 774
816 const abuses: UserAbuse[] = res.body.data 775 const abuses = body.data
817 expect(abuses[0].reason).to.equal('user reason 1') 776 expect(abuses[0].reason).to.equal('user reason 1')
818 expect(abuses[1].reason).to.equal('user reason 2') 777 expect(abuses[1].reason).to.equal('user reason 2')
819 778
@@ -821,95 +780,77 @@ describe('Test abuses', function () {
821 } 780 }
822 781
823 { 782 {
824 const res = await getUserAbusesList({ url: servers[0].url, token: userAccessToken, start: 1, count: 1, sort: 'createdAt' }) 783 const body = await commands[0].getUserList({ token: userAccessToken, start: 1, count: 1, sort: 'createdAt' })
825 expect(res.body.total).to.equal(2) 784 expect(body.total).to.equal(2)
826 785
827 const abuses: UserAbuse[] = res.body.data 786 const abuses: UserAbuse[] = body.data
828 expect(abuses[0].reason).to.equal('user reason 2') 787 expect(abuses[0].reason).to.equal('user reason 2')
829 } 788 }
830 789
831 { 790 {
832 const res = await getUserAbusesList({ url: servers[0].url, token: userAccessToken, start: 1, count: 1, sort: '-createdAt' }) 791 const body = await commands[0].getUserList({ token: userAccessToken, start: 1, count: 1, sort: '-createdAt' })
833 expect(res.body.total).to.equal(2) 792 expect(body.total).to.equal(2)
834 793
835 const abuses: UserAbuse[] = res.body.data 794 const abuses: UserAbuse[] = body.data
836 expect(abuses[0].reason).to.equal('user reason 1') 795 expect(abuses[0].reason).to.equal('user reason 1')
837 } 796 }
838 }) 797 })
839 798
840 it('Should correctly filter my abuses by id', async function () { 799 it('Should correctly filter my abuses by id', async function () {
841 const res = await getUserAbusesList({ url: servers[0].url, token: userAccessToken, id: abuseId1 }) 800 const body = await commands[0].getUserList({ token: userAccessToken, id: abuseId1 })
801 expect(body.total).to.equal(1)
842 802
843 expect(res.body.total).to.equal(1) 803 const abuses: UserAbuse[] = body.data
844
845 const abuses: UserAbuse[] = res.body.data
846 expect(abuses[0].reason).to.equal('user reason 1') 804 expect(abuses[0].reason).to.equal('user reason 1')
847 }) 805 })
848 806
849 it('Should correctly filter my abuses by search', async function () { 807 it('Should correctly filter my abuses by search', async function () {
850 const res = await getUserAbusesList({ 808 const body = await commands[0].getUserList({ token: userAccessToken, search: 'server 2' })
851 url: servers[0].url, 809 expect(body.total).to.equal(1)
852 token: userAccessToken,
853 search: 'server 2'
854 })
855
856 expect(res.body.total).to.equal(1)
857 810
858 const abuses: UserAbuse[] = res.body.data 811 const abuses: UserAbuse[] = body.data
859 expect(abuses[0].reason).to.equal('user reason 2') 812 expect(abuses[0].reason).to.equal('user reason 2')
860 }) 813 })
861 814
862 it('Should correctly filter my abuses by state', async function () { 815 it('Should correctly filter my abuses by state', async function () {
863 const body = { state: AbuseState.REJECTED } 816 await commands[0].update({ abuseId: abuseId1, body: { state: AbuseState.REJECTED } })
864 await updateAbuse(servers[0].url, servers[0].accessToken, abuseId1, body)
865 817
866 const res = await getUserAbusesList({ 818 const body = await commands[0].getUserList({ token: userAccessToken, state: AbuseState.REJECTED })
867 url: servers[0].url, 819 expect(body.total).to.equal(1)
868 token: userAccessToken,
869 state: AbuseState.REJECTED
870 })
871
872 expect(res.body.total).to.equal(1)
873 820
874 const abuses: UserAbuse[] = res.body.data 821 const abuses: UserAbuse[] = body.data
875 expect(abuses[0].reason).to.equal('user reason 1') 822 expect(abuses[0].reason).to.equal('user reason 1')
876 }) 823 })
877 }) 824 })
878 825
879 describe('Abuse messages', async function () { 826 describe('Abuse messages', async function () {
880 let abuseId: number 827 let abuseId: number
881 let userAccessToken: string 828 let userToken: string
882 let abuseMessageUserId: number 829 let abuseMessageUserId: number
883 let abuseMessageModerationId: number 830 let abuseMessageModerationId: number
884 831
885 before(async function () { 832 before(async function () {
886 userAccessToken = await generateUserAccessToken(servers[0], 'user_43') 833 userToken = await servers[0].users.generateUserAndToken('user_43')
887 834
888 const res = await reportAbuse({ 835 const body = await commands[0].report({ token: userToken, videoId: servers[0].store.video.id, reason: 'user 43 reason 1' })
889 url: servers[0].url, 836 abuseId = body.abuse.id
890 token: userAccessToken,
891 videoId: servers[0].video.id,
892 reason: 'user 43 reason 1'
893 })
894
895 abuseId = res.body.abuse.id
896 }) 837 })
897 838
898 it('Should create some messages on the abuse', async function () { 839 it('Should create some messages on the abuse', async function () {
899 await addAbuseMessage(servers[0].url, userAccessToken, abuseId, 'message 1') 840 await commands[0].addMessage({ token: userToken, abuseId, message: 'message 1' })
900 await addAbuseMessage(servers[0].url, servers[0].accessToken, abuseId, 'message 2') 841 await commands[0].addMessage({ abuseId, message: 'message 2' })
901 await addAbuseMessage(servers[0].url, servers[0].accessToken, abuseId, 'message 3') 842 await commands[0].addMessage({ abuseId, message: 'message 3' })
902 await addAbuseMessage(servers[0].url, userAccessToken, abuseId, 'message 4') 843 await commands[0].addMessage({ token: userToken, abuseId, message: 'message 4' })
903 }) 844 })
904 845
905 it('Should have the correct messages count when listing abuses', async function () { 846 it('Should have the correct messages count when listing abuses', async function () {
906 const results = await Promise.all([ 847 const results = await Promise.all([
907 getAdminAbusesList({ url: servers[0].url, token: servers[0].accessToken, start: 0, count: 50 }), 848 commands[0].getAdminList({ start: 0, count: 50 }),
908 getUserAbusesList({ url: servers[0].url, token: userAccessToken, start: 0, count: 50 }) 849 commands[0].getUserList({ token: userToken, start: 0, count: 50 })
909 ]) 850 ])
910 851
911 for (const res of results) { 852 for (const body of results) {
912 const abuses: AdminAbuse[] = res.body.data 853 const abuses = body.data
913 const abuse = abuses.find(a => a.id === abuseId) 854 const abuse = abuses.find(a => a.id === abuseId)
914 expect(abuse.countMessages).to.equal(4) 855 expect(abuse.countMessages).to.equal(4)
915 } 856 }
@@ -917,14 +858,14 @@ describe('Test abuses', function () {
917 858
918 it('Should correctly list messages of this abuse', async function () { 859 it('Should correctly list messages of this abuse', async function () {
919 const results = await Promise.all([ 860 const results = await Promise.all([
920 listAbuseMessages(servers[0].url, servers[0].accessToken, abuseId), 861 commands[0].listMessages({ abuseId }),
921 listAbuseMessages(servers[0].url, userAccessToken, abuseId) 862 commands[0].listMessages({ token: userToken, abuseId })
922 ]) 863 ])
923 864
924 for (const res of results) { 865 for (const body of results) {
925 expect(res.body.total).to.equal(4) 866 expect(body.total).to.equal(4)
926 867
927 const abuseMessages: AbuseMessage[] = res.body.data 868 const abuseMessages: AbuseMessage[] = body.data
928 869
929 expect(abuseMessages[0].message).to.equal('message 1') 870 expect(abuseMessages[0].message).to.equal('message 1')
930 expect(abuseMessages[0].byModerator).to.be.false 871 expect(abuseMessages[0].byModerator).to.be.false
@@ -948,19 +889,18 @@ describe('Test abuses', function () {
948 }) 889 })
949 890
950 it('Should delete messages', async function () { 891 it('Should delete messages', async function () {
951 await deleteAbuseMessage(servers[0].url, servers[0].accessToken, abuseId, abuseMessageModerationId) 892 await commands[0].deleteMessage({ abuseId, messageId: abuseMessageModerationId })
952 await deleteAbuseMessage(servers[0].url, userAccessToken, abuseId, abuseMessageUserId) 893 await commands[0].deleteMessage({ token: userToken, abuseId, messageId: abuseMessageUserId })
953 894
954 const results = await Promise.all([ 895 const results = await Promise.all([
955 listAbuseMessages(servers[0].url, servers[0].accessToken, abuseId), 896 commands[0].listMessages({ abuseId }),
956 listAbuseMessages(servers[0].url, userAccessToken, abuseId) 897 commands[0].listMessages({ token: userToken, abuseId })
957 ]) 898 ])
958 899
959 for (const res of results) { 900 for (const body of results) {
960 expect(res.body.total).to.equal(2) 901 expect(body.total).to.equal(2)
961
962 const abuseMessages: AbuseMessage[] = res.body.data
963 902
903 const abuseMessages: AbuseMessage[] = body.data
964 expect(abuseMessages[0].message).to.equal('message 2') 904 expect(abuseMessages[0].message).to.equal('message 2')
965 expect(abuseMessages[1].message).to.equal('message 4') 905 expect(abuseMessages[1].message).to.equal('message 4')
966 } 906 }
diff --git a/server/tests/api/moderation/blocklist-notification.ts b/server/tests/api/moderation/blocklist-notification.ts
index 4fb3c95f2..75b15c298 100644
--- a/server/tests/api/moderation/blocklist-notification.ts
+++ b/server/tests/api/moderation/blocklist-notification.ts
@@ -2,47 +2,22 @@
2 2
3import 'mocha' 3import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5import { getUserNotifications, markAsReadAllNotifications } from '@shared/extra-utils/users/user-notifications' 5import { cleanupTests, createMultipleServers, doubleFollow, PeerTubeServer, setAccessTokensToServers, waitJobs } from '@shared/extra-utils'
6import { addUserSubscription, removeUserSubscription } from '@shared/extra-utils/users/user-subscriptions' 6import { UserNotificationType } from '@shared/models'
7import { UserNotification, UserNotificationType } from '@shared/models'
8import {
9 cleanupTests,
10 createUser,
11 doubleFollow,
12 flushAndRunMultipleServers,
13 ServerInfo,
14 uploadVideo,
15 userLogin
16} from '../../../../shared/extra-utils/index'
17import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
18import {
19 addAccountToAccountBlocklist,
20 addAccountToServerBlocklist,
21 addServerToAccountBlocklist,
22 addServerToServerBlocklist,
23 removeAccountFromAccountBlocklist,
24 removeAccountFromServerBlocklist,
25 removeServerFromAccountBlocklist
26} from '../../../../shared/extra-utils/users/blocklist'
27import { setAccessTokensToServers } from '../../../../shared/extra-utils/users/login'
28import { addVideoCommentThread } from '../../../../shared/extra-utils/videos/video-comments'
29 7
30const expect = chai.expect 8const expect = chai.expect
31 9
32async function checkNotifications (url: string, token: string, expected: UserNotificationType[]) { 10async function checkNotifications (server: PeerTubeServer, token: string, expected: UserNotificationType[]) {
33 const res = await getUserNotifications(url, token, 0, 10, true) 11 const { data } = await server.notifications.list({ token, start: 0, count: 10, unread: true })
34 12 expect(data).to.have.lengthOf(expected.length)
35 const notifications: UserNotification[] = res.body.data
36
37 expect(notifications).to.have.lengthOf(expected.length)
38 13
39 for (const type of expected) { 14 for (const type of expected) {
40 expect(notifications.find(n => n.type === type)).to.exist 15 expect(data.find(n => n.type === type)).to.exist
41 } 16 }
42} 17}
43 18
44describe('Test blocklist', function () { 19describe('Test blocklist', function () {
45 let servers: ServerInfo[] 20 let servers: PeerTubeServer[]
46 let videoUUID: string 21 let videoUUID: string
47 22
48 let userToken1: string 23 let userToken1: string
@@ -51,30 +26,34 @@ describe('Test blocklist', function () {
51 26
52 async function resetState () { 27 async function resetState () {
53 try { 28 try {
54 await removeUserSubscription(servers[1].url, remoteUserToken, 'user1_channel@' + servers[0].host) 29 await servers[1].subscriptions.remove({ token: remoteUserToken, uri: 'user1_channel@' + servers[0].host })
55 await removeUserSubscription(servers[1].url, remoteUserToken, 'user2_channel@' + servers[0].host) 30 await servers[1].subscriptions.remove({ token: remoteUserToken, uri: 'user2_channel@' + servers[0].host })
56 } catch {} 31 } catch {}
57 32
58 await waitJobs(servers) 33 await waitJobs(servers)
59 34
60 await markAsReadAllNotifications(servers[0].url, userToken1) 35 await servers[0].notifications.markAsReadAll({ token: userToken1 })
61 await markAsReadAllNotifications(servers[0].url, userToken2) 36 await servers[0].notifications.markAsReadAll({ token: userToken2 })
62 37
63 { 38 {
64 const res = await uploadVideo(servers[0].url, userToken1, { name: 'video' }) 39 const { uuid } = await servers[0].videos.upload({ token: userToken1, attributes: { name: 'video' } })
65 videoUUID = res.body.video.uuid 40 videoUUID = uuid
66 41
67 await waitJobs(servers) 42 await waitJobs(servers)
68 } 43 }
69 44
70 { 45 {
71 await addVideoCommentThread(servers[1].url, remoteUserToken, videoUUID, '@user2@' + servers[0].host + ' hello') 46 await servers[1].comments.createThread({
47 token: remoteUserToken,
48 videoId: videoUUID,
49 text: '@user2@' + servers[0].host + ' hello'
50 })
72 } 51 }
73 52
74 { 53 {
75 54
76 await addUserSubscription(servers[1].url, remoteUserToken, 'user1_channel@' + servers[0].host) 55 await servers[1].subscriptions.add({ token: remoteUserToken, targetUri: 'user1_channel@' + servers[0].host })
77 await addUserSubscription(servers[1].url, remoteUserToken, 'user2_channel@' + servers[0].host) 56 await servers[1].subscriptions.add({ token: remoteUserToken, targetUri: 'user2_channel@' + servers[0].host })
78 } 57 }
79 58
80 await waitJobs(servers) 59 await waitJobs(servers)
@@ -83,36 +62,34 @@ describe('Test blocklist', function () {
83 before(async function () { 62 before(async function () {
84 this.timeout(60000) 63 this.timeout(60000)
85 64
86 servers = await flushAndRunMultipleServers(2) 65 servers = await createMultipleServers(2)
87 await setAccessTokensToServers(servers) 66 await setAccessTokensToServers(servers)
88 67
89 { 68 {
90 const user = { username: 'user1', password: 'password' } 69 const user = { username: 'user1', password: 'password' }
91 await createUser({ 70 await servers[0].users.create({
92 url: servers[0].url,
93 accessToken: servers[0].accessToken,
94 username: user.username, 71 username: user.username,
95 password: user.password, 72 password: user.password,
96 videoQuota: -1, 73 videoQuota: -1,
97 videoQuotaDaily: -1 74 videoQuotaDaily: -1
98 }) 75 })
99 76
100 userToken1 = await userLogin(servers[0], user) 77 userToken1 = await servers[0].login.getAccessToken(user)
101 await uploadVideo(servers[0].url, userToken1, { name: 'video user 1' }) 78 await servers[0].videos.upload({ token: userToken1, attributes: { name: 'video user 1' } })
102 } 79 }
103 80
104 { 81 {
105 const user = { username: 'user2', password: 'password' } 82 const user = { username: 'user2', password: 'password' }
106 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: user.username, password: user.password }) 83 await servers[0].users.create({ username: user.username, password: user.password })
107 84
108 userToken2 = await userLogin(servers[0], user) 85 userToken2 = await servers[0].login.getAccessToken(user)
109 } 86 }
110 87
111 { 88 {
112 const user = { username: 'user3', password: 'password' } 89 const user = { username: 'user3', password: 'password' }
113 await createUser({ url: servers[1].url, accessToken: servers[1].accessToken, username: user.username, password: user.password }) 90 await servers[1].users.create({ username: user.username, password: user.password })
114 91
115 remoteUserToken = await userLogin(servers[1], user) 92 remoteUserToken = await servers[1].login.getAccessToken(user)
116 } 93 }
117 94
118 await doubleFollow(servers[0], servers[1]) 95 await doubleFollow(servers[0], servers[1])
@@ -128,26 +105,26 @@ describe('Test blocklist', function () {
128 105
129 it('Should have appropriate notifications', async function () { 106 it('Should have appropriate notifications', async function () {
130 const notifs = [ UserNotificationType.NEW_COMMENT_ON_MY_VIDEO, UserNotificationType.NEW_FOLLOW ] 107 const notifs = [ UserNotificationType.NEW_COMMENT_ON_MY_VIDEO, UserNotificationType.NEW_FOLLOW ]
131 await checkNotifications(servers[0].url, userToken1, notifs) 108 await checkNotifications(servers[0], userToken1, notifs)
132 }) 109 })
133 110
134 it('Should block an account', async function () { 111 it('Should block an account', async function () {
135 this.timeout(10000) 112 this.timeout(10000)
136 113
137 await addAccountToAccountBlocklist(servers[0].url, userToken1, 'user3@' + servers[1].host) 114 await servers[0].blocklist.addToMyBlocklist({ token: userToken1, account: 'user3@' + servers[1].host })
138 await waitJobs(servers) 115 await waitJobs(servers)
139 }) 116 })
140 117
141 it('Should not have notifications from this account', async function () { 118 it('Should not have notifications from this account', async function () {
142 await checkNotifications(servers[0].url, userToken1, []) 119 await checkNotifications(servers[0], userToken1, [])
143 }) 120 })
144 121
145 it('Should have notifications of this account on user 2', async function () { 122 it('Should have notifications of this account on user 2', async function () {
146 const notifs = [ UserNotificationType.COMMENT_MENTION, UserNotificationType.NEW_FOLLOW ] 123 const notifs = [ UserNotificationType.COMMENT_MENTION, UserNotificationType.NEW_FOLLOW ]
147 124
148 await checkNotifications(servers[0].url, userToken2, notifs) 125 await checkNotifications(servers[0], userToken2, notifs)
149 126
150 await removeAccountFromAccountBlocklist(servers[0].url, userToken1, 'user3@' + servers[1].host) 127 await servers[0].blocklist.removeFromMyBlocklist({ token: userToken1, account: 'user3@' + servers[1].host })
151 }) 128 })
152 }) 129 })
153 130
@@ -161,26 +138,26 @@ describe('Test blocklist', function () {
161 138
162 it('Should have appropriate notifications', async function () { 139 it('Should have appropriate notifications', async function () {
163 const notifs = [ UserNotificationType.NEW_COMMENT_ON_MY_VIDEO, UserNotificationType.NEW_FOLLOW ] 140 const notifs = [ UserNotificationType.NEW_COMMENT_ON_MY_VIDEO, UserNotificationType.NEW_FOLLOW ]
164 await checkNotifications(servers[0].url, userToken1, notifs) 141 await checkNotifications(servers[0], userToken1, notifs)
165 }) 142 })
166 143
167 it('Should block an account', async function () { 144 it('Should block an account', async function () {
168 this.timeout(10000) 145 this.timeout(10000)
169 146
170 await addServerToAccountBlocklist(servers[0].url, userToken1, servers[1].host) 147 await servers[0].blocklist.addToMyBlocklist({ token: userToken1, server: servers[1].host })
171 await waitJobs(servers) 148 await waitJobs(servers)
172 }) 149 })
173 150
174 it('Should not have notifications from this account', async function () { 151 it('Should not have notifications from this account', async function () {
175 await checkNotifications(servers[0].url, userToken1, []) 152 await checkNotifications(servers[0], userToken1, [])
176 }) 153 })
177 154
178 it('Should have notifications of this account on user 2', async function () { 155 it('Should have notifications of this account on user 2', async function () {
179 const notifs = [ UserNotificationType.COMMENT_MENTION, UserNotificationType.NEW_FOLLOW ] 156 const notifs = [ UserNotificationType.COMMENT_MENTION, UserNotificationType.NEW_FOLLOW ]
180 157
181 await checkNotifications(servers[0].url, userToken2, notifs) 158 await checkNotifications(servers[0], userToken2, notifs)
182 159
183 await removeServerFromAccountBlocklist(servers[0].url, userToken1, servers[1].host) 160 await servers[0].blocklist.removeFromMyBlocklist({ token: userToken1, server: servers[1].host })
184 }) 161 })
185 }) 162 })
186 163
@@ -195,27 +172,27 @@ describe('Test blocklist', function () {
195 it('Should have appropriate notifications', async function () { 172 it('Should have appropriate notifications', async function () {
196 { 173 {
197 const notifs = [ UserNotificationType.NEW_COMMENT_ON_MY_VIDEO, UserNotificationType.NEW_FOLLOW ] 174 const notifs = [ UserNotificationType.NEW_COMMENT_ON_MY_VIDEO, UserNotificationType.NEW_FOLLOW ]
198 await checkNotifications(servers[0].url, userToken1, notifs) 175 await checkNotifications(servers[0], userToken1, notifs)
199 } 176 }
200 177
201 { 178 {
202 const notifs = [ UserNotificationType.COMMENT_MENTION, UserNotificationType.NEW_FOLLOW ] 179 const notifs = [ UserNotificationType.COMMENT_MENTION, UserNotificationType.NEW_FOLLOW ]
203 await checkNotifications(servers[0].url, userToken2, notifs) 180 await checkNotifications(servers[0], userToken2, notifs)
204 } 181 }
205 }) 182 })
206 183
207 it('Should block an account', async function () { 184 it('Should block an account', async function () {
208 this.timeout(10000) 185 this.timeout(10000)
209 186
210 await addAccountToServerBlocklist(servers[0].url, servers[0].accessToken, 'user3@' + servers[1].host) 187 await servers[0].blocklist.addToServerBlocklist({ account: 'user3@' + servers[1].host })
211 await waitJobs(servers) 188 await waitJobs(servers)
212 }) 189 })
213 190
214 it('Should not have notifications from this account', async function () { 191 it('Should not have notifications from this account', async function () {
215 await checkNotifications(servers[0].url, userToken1, []) 192 await checkNotifications(servers[0], userToken1, [])
216 await checkNotifications(servers[0].url, userToken2, []) 193 await checkNotifications(servers[0], userToken2, [])
217 194
218 await removeAccountFromServerBlocklist(servers[0].url, servers[0].accessToken, 'user3@' + servers[1].host) 195 await servers[0].blocklist.removeFromServerBlocklist({ account: 'user3@' + servers[1].host })
219 }) 196 })
220 }) 197 })
221 198
@@ -230,25 +207,25 @@ describe('Test blocklist', function () {
230 it('Should have appropriate notifications', async function () { 207 it('Should have appropriate notifications', async function () {
231 { 208 {
232 const notifs = [ UserNotificationType.NEW_COMMENT_ON_MY_VIDEO, UserNotificationType.NEW_FOLLOW ] 209 const notifs = [ UserNotificationType.NEW_COMMENT_ON_MY_VIDEO, UserNotificationType.NEW_FOLLOW ]
233 await checkNotifications(servers[0].url, userToken1, notifs) 210 await checkNotifications(servers[0], userToken1, notifs)
234 } 211 }
235 212
236 { 213 {
237 const notifs = [ UserNotificationType.COMMENT_MENTION, UserNotificationType.NEW_FOLLOW ] 214 const notifs = [ UserNotificationType.COMMENT_MENTION, UserNotificationType.NEW_FOLLOW ]
238 await checkNotifications(servers[0].url, userToken2, notifs) 215 await checkNotifications(servers[0], userToken2, notifs)
239 } 216 }
240 }) 217 })
241 218
242 it('Should block an account', async function () { 219 it('Should block an account', async function () {
243 this.timeout(10000) 220 this.timeout(10000)
244 221
245 await addServerToServerBlocklist(servers[0].url, servers[0].accessToken, servers[1].host) 222 await servers[0].blocklist.addToServerBlocklist({ server: servers[1].host })
246 await waitJobs(servers) 223 await waitJobs(servers)
247 }) 224 })
248 225
249 it('Should not have notifications from this account', async function () { 226 it('Should not have notifications from this account', async function () {
250 await checkNotifications(servers[0].url, userToken1, []) 227 await checkNotifications(servers[0], userToken1, [])
251 await checkNotifications(servers[0].url, userToken2, []) 228 await checkNotifications(servers[0], userToken2, [])
252 }) 229 })
253 }) 230 })
254 231
diff --git a/server/tests/api/moderation/blocklist.ts b/server/tests/api/moderation/blocklist.ts
index 793abbcb4..089af8b15 100644
--- a/server/tests/api/moderation/blocklist.ts
+++ b/server/tests/api/moderation/blocklist.ts
@@ -3,106 +3,67 @@
3import 'mocha' 3import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5import { 5import {
6 addAccountToAccountBlocklist, 6 BlocklistCommand,
7 addAccountToServerBlocklist,
8 addServerToAccountBlocklist,
9 addServerToServerBlocklist,
10 addVideoCommentReply,
11 addVideoCommentThread,
12 cleanupTests, 7 cleanupTests,
13 createUser, 8 CommentsCommand,
14 deleteVideoComment, 9 createMultipleServers,
15 doubleFollow, 10 doubleFollow,
16 findCommentId, 11 PeerTubeServer,
17 flushAndRunMultipleServers,
18 follow,
19 getAccountBlocklistByAccount,
20 getAccountBlocklistByServer,
21 getServerBlocklistByAccount,
22 getServerBlocklistByServer,
23 getUserNotifications,
24 getVideoCommentThreads,
25 getVideosList,
26 getVideosListWithToken,
27 getVideoThreadComments,
28 removeAccountFromAccountBlocklist,
29 removeAccountFromServerBlocklist,
30 removeServerFromAccountBlocklist,
31 removeServerFromServerBlocklist,
32 ServerInfo,
33 setAccessTokensToServers, 12 setAccessTokensToServers,
34 unfollow,
35 uploadVideo,
36 userLogin,
37 waitJobs 13 waitJobs
38} from '@shared/extra-utils' 14} from '@shared/extra-utils'
39import { 15import { UserNotificationType } from '@shared/models'
40 AccountBlock,
41 ServerBlock,
42 UserNotification,
43 UserNotificationType,
44 Video,
45 VideoComment,
46 VideoCommentThreadTree
47} from '@shared/models'
48 16
49const expect = chai.expect 17const expect = chai.expect
50 18
51async function checkAllVideos (url: string, token: string) { 19async function checkAllVideos (server: PeerTubeServer, token: string) {
52 { 20 {
53 const res = await getVideosListWithToken(url, token) 21 const { data } = await server.videos.listWithToken({ token })
54 22 expect(data).to.have.lengthOf(5)
55 expect(res.body.data).to.have.lengthOf(5)
56 } 23 }
57 24
58 { 25 {
59 const res = await getVideosList(url) 26 const { data } = await server.videos.list()
60 27 expect(data).to.have.lengthOf(5)
61 expect(res.body.data).to.have.lengthOf(5)
62 } 28 }
63} 29}
64 30
65async function checkAllComments (url: string, token: string, videoUUID: string) { 31async function checkAllComments (server: PeerTubeServer, token: string, videoUUID: string) {
66 const resThreads = await getVideoCommentThreads(url, videoUUID, 0, 25, '-createdAt', token) 32 const { data } = await server.comments.listThreads({ videoId: videoUUID, start: 0, count: 25, sort: '-createdAt', token })
67 33
68 const allThreads: VideoComment[] = resThreads.body.data 34 const threads = data.filter(t => t.isDeleted === false)
69 const threads = allThreads.filter(t => t.isDeleted === false)
70 expect(threads).to.have.lengthOf(2) 35 expect(threads).to.have.lengthOf(2)
71 36
72 for (const thread of threads) { 37 for (const thread of threads) {
73 const res = await getVideoThreadComments(url, videoUUID, thread.id, token) 38 const tree = await server.comments.getThread({ videoId: videoUUID, threadId: thread.id, token })
74
75 const tree: VideoCommentThreadTree = res.body
76 expect(tree.children).to.have.lengthOf(1) 39 expect(tree.children).to.have.lengthOf(1)
77 } 40 }
78} 41}
79 42
80async function checkCommentNotification ( 43async function checkCommentNotification (
81 mainServer: ServerInfo, 44 mainServer: PeerTubeServer,
82 comment: { server: ServerInfo, token: string, videoUUID: string, text: string }, 45 comment: { server: PeerTubeServer, token: string, videoUUID: string, text: string },
83 check: 'presence' | 'absence' 46 check: 'presence' | 'absence'
84) { 47) {
85 const resComment = await addVideoCommentThread(comment.server.url, comment.token, comment.videoUUID, comment.text) 48 const command = comment.server.comments
86 const created = resComment.body.comment as VideoComment 49
87 const threadId = created.id 50 const { threadId, createdAt } = await command.createThread({ token: comment.token, videoId: comment.videoUUID, text: comment.text })
88 const createdAt = created.createdAt
89 51
90 await waitJobs([ mainServer, comment.server ]) 52 await waitJobs([ mainServer, comment.server ])
91 53
92 const res = await getUserNotifications(mainServer.url, mainServer.accessToken, 0, 30) 54 const { data } = await mainServer.notifications.list({ start: 0, count: 30 })
93 const commentNotifications = (res.body.data as UserNotification[]) 55 const commentNotifications = data.filter(n => n.comment && n.comment.video.uuid === comment.videoUUID && n.createdAt >= createdAt)
94 .filter(n => n.comment && n.comment.video.uuid === comment.videoUUID && n.createdAt >= createdAt)
95 56
96 if (check === 'presence') expect(commentNotifications).to.have.lengthOf(1) 57 if (check === 'presence') expect(commentNotifications).to.have.lengthOf(1)
97 else expect(commentNotifications).to.have.lengthOf(0) 58 else expect(commentNotifications).to.have.lengthOf(0)
98 59
99 await deleteVideoComment(comment.server.url, comment.token, comment.videoUUID, threadId) 60 await command.delete({ token: comment.token, videoId: comment.videoUUID, commentId: threadId })
100 61
101 await waitJobs([ mainServer, comment.server ]) 62 await waitJobs([ mainServer, comment.server ])
102} 63}
103 64
104describe('Test blocklist', function () { 65describe('Test blocklist', function () {
105 let servers: ServerInfo[] 66 let servers: PeerTubeServer[]
106 let videoUUID1: string 67 let videoUUID1: string
107 let videoUUID2: string 68 let videoUUID2: string
108 let videoUUID3: string 69 let videoUUID3: string
@@ -110,62 +71,73 @@ describe('Test blocklist', function () {
110 let userModeratorToken: string 71 let userModeratorToken: string
111 let userToken2: string 72 let userToken2: string
112 73
74 let command: BlocklistCommand
75 let commentsCommand: CommentsCommand[]
76
113 before(async function () { 77 before(async function () {
114 this.timeout(120000) 78 this.timeout(120000)
115 79
116 servers = await flushAndRunMultipleServers(3) 80 servers = await createMultipleServers(3)
117 await setAccessTokensToServers(servers) 81 await setAccessTokensToServers(servers)
118 82
83 command = servers[0].blocklist
84 commentsCommand = servers.map(s => s.comments)
85
119 { 86 {
120 const user = { username: 'user1', password: 'password' } 87 const user = { username: 'user1', password: 'password' }
121 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: user.username, password: user.password }) 88 await servers[0].users.create({ username: user.username, password: user.password })
122 89
123 userToken1 = await userLogin(servers[0], user) 90 userToken1 = await servers[0].login.getAccessToken(user)
124 await uploadVideo(servers[0].url, userToken1, { name: 'video user 1' }) 91 await servers[0].videos.upload({ token: userToken1, attributes: { name: 'video user 1' } })
125 } 92 }
126 93
127 { 94 {
128 const user = { username: 'moderator', password: 'password' } 95 const user = { username: 'moderator', password: 'password' }
129 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: user.username, password: user.password }) 96 await servers[0].users.create({ username: user.username, password: user.password })
130 97
131 userModeratorToken = await userLogin(servers[0], user) 98 userModeratorToken = await servers[0].login.getAccessToken(user)
132 } 99 }
133 100
134 { 101 {
135 const user = { username: 'user2', password: 'password' } 102 const user = { username: 'user2', password: 'password' }
136 await createUser({ url: servers[1].url, accessToken: servers[1].accessToken, username: user.username, password: user.password }) 103 await servers[1].users.create({ username: user.username, password: user.password })
137 104
138 userToken2 = await userLogin(servers[1], user) 105 userToken2 = await servers[1].login.getAccessToken(user)
139 await uploadVideo(servers[1].url, userToken2, { name: 'video user 2' }) 106 await servers[1].videos.upload({ token: userToken2, attributes: { name: 'video user 2' } })
140 } 107 }
141 108
142 { 109 {
143 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video server 1' }) 110 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video server 1' } })
144 videoUUID1 = res.body.video.uuid 111 videoUUID1 = uuid
145 } 112 }
146 113
147 { 114 {
148 const res = await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'video server 2' }) 115 const { uuid } = await servers[1].videos.upload({ attributes: { name: 'video server 2' } })
149 videoUUID2 = res.body.video.uuid 116 videoUUID2 = uuid
150 } 117 }
151 118
152 { 119 {
153 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video 2 server 1' }) 120 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video 2 server 1' } })
154 videoUUID3 = res.body.video.uuid 121 videoUUID3 = uuid
155 } 122 }
156 123
157 await doubleFollow(servers[0], servers[1]) 124 await doubleFollow(servers[0], servers[1])
158 await doubleFollow(servers[0], servers[2]) 125 await doubleFollow(servers[0], servers[2])
159 126
160 { 127 {
161 const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoUUID1, 'comment root 1') 128 const created = await commentsCommand[0].createThread({ videoId: videoUUID1, text: 'comment root 1' })
162 const resReply = await addVideoCommentReply(servers[0].url, userToken1, videoUUID1, resComment.body.comment.id, 'comment user 1') 129 const reply = await commentsCommand[0].addReply({
163 await addVideoCommentReply(servers[0].url, servers[0].accessToken, videoUUID1, resReply.body.comment.id, 'comment root 1') 130 token: userToken1,
131 videoId: videoUUID1,
132 toCommentId: created.id,
133 text: 'comment user 1'
134 })
135 await commentsCommand[0].addReply({ videoId: videoUUID1, toCommentId: reply.id, text: 'comment root 1' })
164 } 136 }
165 137
166 { 138 {
167 const resComment = await addVideoCommentThread(servers[0].url, userToken1, videoUUID1, 'comment user 1') 139 const created = await commentsCommand[0].createThread({ token: userToken1, videoId: videoUUID1, text: 'comment user 1' })
168 await addVideoCommentReply(servers[0].url, servers[0].accessToken, videoUUID1, resComment.body.comment.id, 'comment root 1') 140 await commentsCommand[0].addReply({ videoId: videoUUID1, toCommentId: created.id, text: 'comment root 1' })
169 } 141 }
170 142
171 await waitJobs(servers) 143 await waitJobs(servers)
@@ -175,55 +147,60 @@ describe('Test blocklist', function () {
175 147
176 describe('When managing account blocklist', function () { 148 describe('When managing account blocklist', function () {
177 it('Should list all videos', function () { 149 it('Should list all videos', function () {
178 return checkAllVideos(servers[0].url, servers[0].accessToken) 150 return checkAllVideos(servers[0], servers[0].accessToken)
179 }) 151 })
180 152
181 it('Should list the comments', function () { 153 it('Should list the comments', function () {
182 return checkAllComments(servers[0].url, servers[0].accessToken, videoUUID1) 154 return checkAllComments(servers[0], servers[0].accessToken, videoUUID1)
183 }) 155 })
184 156
185 it('Should block a remote account', async function () { 157 it('Should block a remote account', async function () {
186 await addAccountToAccountBlocklist(servers[0].url, servers[0].accessToken, 'user2@localhost:' + servers[1].port) 158 await command.addToMyBlocklist({ account: 'user2@localhost:' + servers[1].port })
187 }) 159 })
188 160
189 it('Should hide its videos', async function () { 161 it('Should hide its videos', async function () {
190 const res = await getVideosListWithToken(servers[0].url, servers[0].accessToken) 162 const { data } = await servers[0].videos.listWithToken()
191 163
192 const videos: Video[] = res.body.data 164 expect(data).to.have.lengthOf(4)
193 expect(videos).to.have.lengthOf(4)
194 165
195 const v = videos.find(v => v.name === 'video user 2') 166 const v = data.find(v => v.name === 'video user 2')
196 expect(v).to.be.undefined 167 expect(v).to.be.undefined
197 }) 168 })
198 169
199 it('Should block a local account', async function () { 170 it('Should block a local account', async function () {
200 await addAccountToAccountBlocklist(servers[0].url, servers[0].accessToken, 'user1') 171 await command.addToMyBlocklist({ account: 'user1' })
201 }) 172 })
202 173
203 it('Should hide its videos', async function () { 174 it('Should hide its videos', async function () {
204 const res = await getVideosListWithToken(servers[0].url, servers[0].accessToken) 175 const { data } = await servers[0].videos.listWithToken()
205 176
206 const videos: Video[] = res.body.data 177 expect(data).to.have.lengthOf(3)
207 expect(videos).to.have.lengthOf(3)
208 178
209 const v = videos.find(v => v.name === 'video user 1') 179 const v = data.find(v => v.name === 'video user 1')
210 expect(v).to.be.undefined 180 expect(v).to.be.undefined
211 }) 181 })
212 182
213 it('Should hide its comments', async function () { 183 it('Should hide its comments', async function () {
214 const resThreads = await getVideoCommentThreads(servers[0].url, videoUUID1, 0, 25, '-createdAt', servers[0].accessToken) 184 const { data } = await commentsCommand[0].listThreads({
215 185 token: servers[0].accessToken,
216 const threads: VideoComment[] = resThreads.body.data 186 videoId: videoUUID1,
217 expect(threads).to.have.lengthOf(1) 187 start: 0,
218 expect(threads[0].totalReplies).to.equal(1) 188 count: 25,
219 189 sort: '-createdAt'
220 const t = threads.find(t => t.text === 'comment user 1') 190 })
191
192 expect(data).to.have.lengthOf(1)
193 expect(data[0].totalReplies).to.equal(1)
194
195 const t = data.find(t => t.text === 'comment user 1')
221 expect(t).to.be.undefined 196 expect(t).to.be.undefined
222 197
223 for (const thread of threads) { 198 for (const thread of data) {
224 const res = await getVideoThreadComments(servers[0].url, videoUUID1, thread.id, servers[0].accessToken) 199 const tree = await commentsCommand[0].getThread({
225 200 videoId: videoUUID1,
226 const tree: VideoCommentThreadTree = res.body 201 threadId: thread.id,
202 token: servers[0].accessToken
203 })
227 expect(tree.children).to.have.lengthOf(0) 204 expect(tree.children).to.have.lengthOf(0)
228 } 205 }
229 }) 206 })
@@ -248,17 +225,15 @@ describe('Test blocklist', function () {
248 }) 225 })
249 226
250 it('Should list all the videos with another user', async function () { 227 it('Should list all the videos with another user', async function () {
251 return checkAllVideos(servers[0].url, userToken1) 228 return checkAllVideos(servers[0], userToken1)
252 }) 229 })
253 230
254 it('Should list blocked accounts', async function () { 231 it('Should list blocked accounts', async function () {
255 { 232 {
256 const res = await getAccountBlocklistByAccount(servers[0].url, servers[0].accessToken, 0, 1, 'createdAt') 233 const body = await command.listMyAccountBlocklist({ start: 0, count: 1, sort: 'createdAt' })
257 const blocks: AccountBlock[] = res.body.data 234 expect(body.total).to.equal(2)
258 235
259 expect(res.body.total).to.equal(2) 236 const block = body.data[0]
260
261 const block = blocks[0]
262 expect(block.byAccount.displayName).to.equal('root') 237 expect(block.byAccount.displayName).to.equal('root')
263 expect(block.byAccount.name).to.equal('root') 238 expect(block.byAccount.name).to.equal('root')
264 expect(block.blockedAccount.displayName).to.equal('user2') 239 expect(block.blockedAccount.displayName).to.equal('user2')
@@ -267,12 +242,10 @@ describe('Test blocklist', function () {
267 } 242 }
268 243
269 { 244 {
270 const res = await getAccountBlocklistByAccount(servers[0].url, servers[0].accessToken, 1, 2, 'createdAt') 245 const body = await command.listMyAccountBlocklist({ start: 1, count: 2, sort: 'createdAt' })
271 const blocks: AccountBlock[] = res.body.data 246 expect(body.total).to.equal(2)
272
273 expect(res.body.total).to.equal(2)
274 247
275 const block = blocks[0] 248 const block = body.data[0]
276 expect(block.byAccount.displayName).to.equal('root') 249 expect(block.byAccount.displayName).to.equal('root')
277 expect(block.byAccount.name).to.equal('root') 250 expect(block.byAccount.name).to.equal('root')
278 expect(block.blockedAccount.displayName).to.equal('user1') 251 expect(block.blockedAccount.displayName).to.equal('user1')
@@ -285,32 +258,29 @@ describe('Test blocklist', function () {
285 this.timeout(60000) 258 this.timeout(60000)
286 259
287 { 260 {
288 await addVideoCommentThread(servers[1].url, userToken2, videoUUID3, 'comment user 2') 261 await commentsCommand[1].createThread({ token: userToken2, videoId: videoUUID3, text: 'comment user 2' })
289 await waitJobs(servers) 262 await waitJobs(servers)
290 263
291 await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoUUID3, 'uploader') 264 await commentsCommand[0].createThread({ token: servers[0].accessToken, videoId: videoUUID3, text: 'uploader' })
292 await waitJobs(servers) 265 await waitJobs(servers)
293 266
294 const commentId = await findCommentId(servers[1].url, videoUUID3, 'uploader') 267 const commentId = await commentsCommand[1].findCommentId({ videoId: videoUUID3, text: 'uploader' })
295 const message = 'reply by user 2' 268 const message = 'reply by user 2'
296 const resReply = await addVideoCommentReply(servers[1].url, userToken2, videoUUID3, commentId, message) 269 const reply = await commentsCommand[1].addReply({ token: userToken2, videoId: videoUUID3, toCommentId: commentId, text: message })
297 await addVideoCommentReply(servers[1].url, servers[1].accessToken, videoUUID3, resReply.body.comment.id, 'another reply') 270 await commentsCommand[1].addReply({ videoId: videoUUID3, toCommentId: reply.id, text: 'another reply' })
298 271
299 await waitJobs(servers) 272 await waitJobs(servers)
300 } 273 }
301 274
302 // Server 2 has all the comments 275 // Server 2 has all the comments
303 { 276 {
304 const resThreads = await getVideoCommentThreads(servers[1].url, videoUUID3, 0, 25, '-createdAt') 277 const { data } = await commentsCommand[1].listThreads({ videoId: videoUUID3, count: 25, sort: '-createdAt' })
305 const threads: VideoComment[] = resThreads.body.data
306
307 expect(threads).to.have.lengthOf(2)
308 expect(threads[0].text).to.equal('uploader')
309 expect(threads[1].text).to.equal('comment user 2')
310 278
311 const resReplies = await getVideoThreadComments(servers[1].url, videoUUID3, threads[0].id) 279 expect(data).to.have.lengthOf(2)
280 expect(data[0].text).to.equal('uploader')
281 expect(data[1].text).to.equal('comment user 2')
312 282
313 const tree: VideoCommentThreadTree = resReplies.body 283 const tree = await commentsCommand[1].getThread({ videoId: videoUUID3, threadId: data[0].id })
314 expect(tree.children).to.have.lengthOf(1) 284 expect(tree.children).to.have.lengthOf(1)
315 expect(tree.children[0].comment.text).to.equal('reply by user 2') 285 expect(tree.children[0].comment.text).to.equal('reply by user 2')
316 expect(tree.children[0].children).to.have.lengthOf(1) 286 expect(tree.children[0].children).to.have.lengthOf(1)
@@ -319,55 +289,45 @@ describe('Test blocklist', function () {
319 289
320 // Server 1 and 3 should only have uploader comments 290 // Server 1 and 3 should only have uploader comments
321 for (const server of [ servers[0], servers[2] ]) { 291 for (const server of [ servers[0], servers[2] ]) {
322 const resThreads = await getVideoCommentThreads(server.url, videoUUID3, 0, 25, '-createdAt') 292 const { data } = await server.comments.listThreads({ videoId: videoUUID3, count: 25, sort: '-createdAt' })
323 const threads: VideoComment[] = resThreads.body.data
324 293
325 expect(threads).to.have.lengthOf(1) 294 expect(data).to.have.lengthOf(1)
326 expect(threads[0].text).to.equal('uploader') 295 expect(data[0].text).to.equal('uploader')
327 296
328 const resReplies = await getVideoThreadComments(server.url, videoUUID3, threads[0].id) 297 const tree = await server.comments.getThread({ videoId: videoUUID3, threadId: data[0].id })
329 298
330 const tree: VideoCommentThreadTree = resReplies.body 299 if (server.serverNumber === 1) expect(tree.children).to.have.lengthOf(0)
331 if (server.serverNumber === 1) { 300 else expect(tree.children).to.have.lengthOf(1)
332 expect(tree.children).to.have.lengthOf(0)
333 } else {
334 expect(tree.children).to.have.lengthOf(1)
335 }
336 } 301 }
337 }) 302 })
338 303
339 it('Should unblock the remote account', async function () { 304 it('Should unblock the remote account', async function () {
340 await removeAccountFromAccountBlocklist(servers[0].url, servers[0].accessToken, 'user2@localhost:' + servers[1].port) 305 await command.removeFromMyBlocklist({ account: 'user2@localhost:' + servers[1].port })
341 }) 306 })
342 307
343 it('Should display its videos', async function () { 308 it('Should display its videos', async function () {
344 const res = await getVideosListWithToken(servers[0].url, servers[0].accessToken) 309 const { data } = await servers[0].videos.listWithToken()
345 310 expect(data).to.have.lengthOf(4)
346 const videos: Video[] = res.body.data
347 expect(videos).to.have.lengthOf(4)
348 311
349 const v = videos.find(v => v.name === 'video user 2') 312 const v = data.find(v => v.name === 'video user 2')
350 expect(v).not.to.be.undefined 313 expect(v).not.to.be.undefined
351 }) 314 })
352 315
353 it('Should display its comments on my video', async function () { 316 it('Should display its comments on my video', async function () {
354 for (const server of servers) { 317 for (const server of servers) {
355 const resThreads = await getVideoCommentThreads(server.url, videoUUID3, 0, 25, '-createdAt') 318 const { data } = await server.comments.listThreads({ videoId: videoUUID3, count: 25, sort: '-createdAt' })
356 const threads: VideoComment[] = resThreads.body.data
357 319
358 // Server 3 should not have 2 comment threads, because server 1 did not forward the server 2 comment 320 // Server 3 should not have 2 comment threads, because server 1 did not forward the server 2 comment
359 if (server.serverNumber === 3) { 321 if (server.serverNumber === 3) {
360 expect(threads).to.have.lengthOf(1) 322 expect(data).to.have.lengthOf(1)
361 continue 323 continue
362 } 324 }
363 325
364 expect(threads).to.have.lengthOf(2) 326 expect(data).to.have.lengthOf(2)
365 expect(threads[0].text).to.equal('uploader') 327 expect(data[0].text).to.equal('uploader')
366 expect(threads[1].text).to.equal('comment user 2') 328 expect(data[1].text).to.equal('comment user 2')
367 329
368 const resReplies = await getVideoThreadComments(server.url, videoUUID3, threads[0].id) 330 const tree = await server.comments.getThread({ videoId: videoUUID3, threadId: data[0].id })
369
370 const tree: VideoCommentThreadTree = resReplies.body
371 expect(tree.children).to.have.lengthOf(1) 331 expect(tree.children).to.have.lengthOf(1)
372 expect(tree.children[0].comment.text).to.equal('reply by user 2') 332 expect(tree.children[0].comment.text).to.equal('reply by user 2')
373 expect(tree.children[0].children).to.have.lengthOf(1) 333 expect(tree.children[0].children).to.have.lengthOf(1)
@@ -376,11 +336,11 @@ describe('Test blocklist', function () {
376 }) 336 })
377 337
378 it('Should unblock the local account', async function () { 338 it('Should unblock the local account', async function () {
379 await removeAccountFromAccountBlocklist(servers[0].url, servers[0].accessToken, 'user1') 339 await command.removeFromMyBlocklist({ account: 'user1' })
380 }) 340 })
381 341
382 it('Should display its comments', function () { 342 it('Should display its comments', function () {
383 return checkAllComments(servers[0].url, servers[0].accessToken, videoUUID1) 343 return checkAllComments(servers[0], servers[0].accessToken, videoUUID1)
384 }) 344 })
385 345
386 it('Should have a notification from a non blocked account', async function () { 346 it('Should have a notification from a non blocked account', async function () {
@@ -404,46 +364,45 @@ describe('Test blocklist', function () {
404 }) 364 })
405 365
406 describe('When managing server blocklist', function () { 366 describe('When managing server blocklist', function () {
367
407 it('Should list all videos', function () { 368 it('Should list all videos', function () {
408 return checkAllVideos(servers[0].url, servers[0].accessToken) 369 return checkAllVideos(servers[0], servers[0].accessToken)
409 }) 370 })
410 371
411 it('Should list the comments', function () { 372 it('Should list the comments', function () {
412 return checkAllComments(servers[0].url, servers[0].accessToken, videoUUID1) 373 return checkAllComments(servers[0], servers[0].accessToken, videoUUID1)
413 }) 374 })
414 375
415 it('Should block a remote server', async function () { 376 it('Should block a remote server', async function () {
416 await addServerToAccountBlocklist(servers[0].url, servers[0].accessToken, 'localhost:' + servers[1].port) 377 await command.addToMyBlocklist({ server: 'localhost:' + servers[1].port })
417 }) 378 })
418 379
419 it('Should hide its videos', async function () { 380 it('Should hide its videos', async function () {
420 const res = await getVideosListWithToken(servers[0].url, servers[0].accessToken) 381 const { data } = await servers[0].videos.listWithToken()
421 382
422 const videos: Video[] = res.body.data 383 expect(data).to.have.lengthOf(3)
423 expect(videos).to.have.lengthOf(3)
424 384
425 const v1 = videos.find(v => v.name === 'video user 2') 385 const v1 = data.find(v => v.name === 'video user 2')
426 const v2 = videos.find(v => v.name === 'video server 2') 386 const v2 = data.find(v => v.name === 'video server 2')
427 387
428 expect(v1).to.be.undefined 388 expect(v1).to.be.undefined
429 expect(v2).to.be.undefined 389 expect(v2).to.be.undefined
430 }) 390 })
431 391
432 it('Should list all the videos with another user', async function () { 392 it('Should list all the videos with another user', async function () {
433 return checkAllVideos(servers[0].url, userToken1) 393 return checkAllVideos(servers[0], userToken1)
434 }) 394 })
435 395
436 it('Should hide its comments', async function () { 396 it('Should hide its comments', async function () {
437 this.timeout(10000) 397 this.timeout(10000)
438 398
439 const resThreads = await addVideoCommentThread(servers[1].url, userToken2, videoUUID1, 'hidden comment 2') 399 const { id } = await commentsCommand[1].createThread({ token: userToken2, videoId: videoUUID1, text: 'hidden comment 2' })
440 const threadId = resThreads.body.comment.id
441 400
442 await waitJobs(servers) 401 await waitJobs(servers)
443 402
444 await checkAllComments(servers[0].url, servers[0].accessToken, videoUUID1) 403 await checkAllComments(servers[0], servers[0].accessToken, videoUUID1)
445 404
446 await deleteVideoComment(servers[1].url, userToken2, videoUUID1, threadId) 405 await commentsCommand[1].delete({ token: userToken2, videoId: videoUUID1, commentId: id })
447 }) 406 })
448 407
449 it('Should not have notifications from blocked server', async function () { 408 it('Should not have notifications from blocked server', async function () {
@@ -466,27 +425,25 @@ describe('Test blocklist', function () {
466 }) 425 })
467 426
468 it('Should list blocked servers', async function () { 427 it('Should list blocked servers', async function () {
469 const res = await getServerBlocklistByAccount(servers[0].url, servers[0].accessToken, 0, 1, 'createdAt') 428 const body = await command.listMyServerBlocklist({ start: 0, count: 1, sort: 'createdAt' })
470 const blocks: ServerBlock[] = res.body.data 429 expect(body.total).to.equal(1)
471
472 expect(res.body.total).to.equal(1)
473 430
474 const block = blocks[0] 431 const block = body.data[0]
475 expect(block.byAccount.displayName).to.equal('root') 432 expect(block.byAccount.displayName).to.equal('root')
476 expect(block.byAccount.name).to.equal('root') 433 expect(block.byAccount.name).to.equal('root')
477 expect(block.blockedServer.host).to.equal('localhost:' + servers[1].port) 434 expect(block.blockedServer.host).to.equal('localhost:' + servers[1].port)
478 }) 435 })
479 436
480 it('Should unblock the remote server', async function () { 437 it('Should unblock the remote server', async function () {
481 await removeServerFromAccountBlocklist(servers[0].url, servers[0].accessToken, 'localhost:' + servers[1].port) 438 await command.removeFromMyBlocklist({ server: 'localhost:' + servers[1].port })
482 }) 439 })
483 440
484 it('Should display its videos', function () { 441 it('Should display its videos', function () {
485 return checkAllVideos(servers[0].url, servers[0].accessToken) 442 return checkAllVideos(servers[0], servers[0].accessToken)
486 }) 443 })
487 444
488 it('Should display its comments', function () { 445 it('Should display its comments', function () {
489 return checkAllComments(servers[0].url, servers[0].accessToken, videoUUID1) 446 return checkAllComments(servers[0], servers[0].accessToken, videoUUID1)
490 }) 447 })
491 448
492 it('Should have notification from unblocked server', async function () { 449 it('Should have notification from unblocked server', async function () {
@@ -515,54 +472,50 @@ describe('Test blocklist', function () {
515 describe('When managing account blocklist', function () { 472 describe('When managing account blocklist', function () {
516 it('Should list all videos', async function () { 473 it('Should list all videos', async function () {
517 for (const token of [ userModeratorToken, servers[0].accessToken ]) { 474 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
518 await checkAllVideos(servers[0].url, token) 475 await checkAllVideos(servers[0], token)
519 } 476 }
520 }) 477 })
521 478
522 it('Should list the comments', async function () { 479 it('Should list the comments', async function () {
523 for (const token of [ userModeratorToken, servers[0].accessToken ]) { 480 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
524 await checkAllComments(servers[0].url, token, videoUUID1) 481 await checkAllComments(servers[0], token, videoUUID1)
525 } 482 }
526 }) 483 })
527 484
528 it('Should block a remote account', async function () { 485 it('Should block a remote account', async function () {
529 await addAccountToServerBlocklist(servers[0].url, servers[0].accessToken, 'user2@localhost:' + servers[1].port) 486 await command.addToServerBlocklist({ account: 'user2@localhost:' + servers[1].port })
530 }) 487 })
531 488
532 it('Should hide its videos', async function () { 489 it('Should hide its videos', async function () {
533 for (const token of [ userModeratorToken, servers[0].accessToken ]) { 490 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
534 const res = await getVideosListWithToken(servers[0].url, token) 491 const { data } = await servers[0].videos.listWithToken({ token })
535 492
536 const videos: Video[] = res.body.data 493 expect(data).to.have.lengthOf(4)
537 expect(videos).to.have.lengthOf(4)
538 494
539 const v = videos.find(v => v.name === 'video user 2') 495 const v = data.find(v => v.name === 'video user 2')
540 expect(v).to.be.undefined 496 expect(v).to.be.undefined
541 } 497 }
542 }) 498 })
543 499
544 it('Should block a local account', async function () { 500 it('Should block a local account', async function () {
545 await addAccountToServerBlocklist(servers[0].url, servers[0].accessToken, 'user1') 501 await command.addToServerBlocklist({ account: 'user1' })
546 }) 502 })
547 503
548 it('Should hide its videos', async function () { 504 it('Should hide its videos', async function () {
549 for (const token of [ userModeratorToken, servers[0].accessToken ]) { 505 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
550 const res = await getVideosListWithToken(servers[0].url, token) 506 const { data } = await servers[0].videos.listWithToken({ token })
551 507
552 const videos: Video[] = res.body.data 508 expect(data).to.have.lengthOf(3)
553 expect(videos).to.have.lengthOf(3)
554 509
555 const v = videos.find(v => v.name === 'video user 1') 510 const v = data.find(v => v.name === 'video user 1')
556 expect(v).to.be.undefined 511 expect(v).to.be.undefined
557 } 512 }
558 }) 513 })
559 514
560 it('Should hide its comments', async function () { 515 it('Should hide its comments', async function () {
561 for (const token of [ userModeratorToken, servers[0].accessToken ]) { 516 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
562 const resThreads = await getVideoCommentThreads(servers[0].url, videoUUID1, 0, 20, '-createdAt', token) 517 const { data } = await commentsCommand[0].listThreads({ videoId: videoUUID1, count: 20, sort: '-createdAt', token })
563 518 const threads = data.filter(t => t.isDeleted === false)
564 let threads: VideoComment[] = resThreads.body.data
565 threads = threads.filter(t => t.isDeleted === false)
566 519
567 expect(threads).to.have.lengthOf(1) 520 expect(threads).to.have.lengthOf(1)
568 expect(threads[0].totalReplies).to.equal(1) 521 expect(threads[0].totalReplies).to.equal(1)
@@ -571,9 +524,7 @@ describe('Test blocklist', function () {
571 expect(t).to.be.undefined 524 expect(t).to.be.undefined
572 525
573 for (const thread of threads) { 526 for (const thread of threads) {
574 const res = await getVideoThreadComments(servers[0].url, videoUUID1, thread.id, token) 527 const tree = await commentsCommand[0].getThread({ videoId: videoUUID1, threadId: thread.id, token })
575
576 const tree: VideoCommentThreadTree = res.body
577 expect(tree.children).to.have.lengthOf(0) 528 expect(tree.children).to.have.lengthOf(0)
578 } 529 }
579 } 530 }
@@ -600,12 +551,10 @@ describe('Test blocklist', function () {
600 551
601 it('Should list blocked accounts', async function () { 552 it('Should list blocked accounts', async function () {
602 { 553 {
603 const res = await getAccountBlocklistByServer(servers[0].url, servers[0].accessToken, 0, 1, 'createdAt') 554 const body = await command.listServerAccountBlocklist({ start: 0, count: 1, sort: 'createdAt' })
604 const blocks: AccountBlock[] = res.body.data 555 expect(body.total).to.equal(2)
605
606 expect(res.body.total).to.equal(2)
607 556
608 const block = blocks[0] 557 const block = body.data[0]
609 expect(block.byAccount.displayName).to.equal('peertube') 558 expect(block.byAccount.displayName).to.equal('peertube')
610 expect(block.byAccount.name).to.equal('peertube') 559 expect(block.byAccount.name).to.equal('peertube')
611 expect(block.blockedAccount.displayName).to.equal('user2') 560 expect(block.blockedAccount.displayName).to.equal('user2')
@@ -614,12 +563,10 @@ describe('Test blocklist', function () {
614 } 563 }
615 564
616 { 565 {
617 const res = await getAccountBlocklistByServer(servers[0].url, servers[0].accessToken, 1, 2, 'createdAt') 566 const body = await command.listServerAccountBlocklist({ start: 1, count: 2, sort: 'createdAt' })
618 const blocks: AccountBlock[] = res.body.data 567 expect(body.total).to.equal(2)
619 568
620 expect(res.body.total).to.equal(2) 569 const block = body.data[0]
621
622 const block = blocks[0]
623 expect(block.byAccount.displayName).to.equal('peertube') 570 expect(block.byAccount.displayName).to.equal('peertube')
624 expect(block.byAccount.name).to.equal('peertube') 571 expect(block.byAccount.name).to.equal('peertube')
625 expect(block.blockedAccount.displayName).to.equal('user1') 572 expect(block.blockedAccount.displayName).to.equal('user1')
@@ -629,28 +576,26 @@ describe('Test blocklist', function () {
629 }) 576 })
630 577
631 it('Should unblock the remote account', async function () { 578 it('Should unblock the remote account', async function () {
632 await removeAccountFromServerBlocklist(servers[0].url, servers[0].accessToken, 'user2@localhost:' + servers[1].port) 579 await command.removeFromServerBlocklist({ account: 'user2@localhost:' + servers[1].port })
633 }) 580 })
634 581
635 it('Should display its videos', async function () { 582 it('Should display its videos', async function () {
636 for (const token of [ userModeratorToken, servers[0].accessToken ]) { 583 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
637 const res = await getVideosListWithToken(servers[0].url, token) 584 const { data } = await servers[0].videos.listWithToken({ token })
638 585 expect(data).to.have.lengthOf(4)
639 const videos: Video[] = res.body.data
640 expect(videos).to.have.lengthOf(4)
641 586
642 const v = videos.find(v => v.name === 'video user 2') 587 const v = data.find(v => v.name === 'video user 2')
643 expect(v).not.to.be.undefined 588 expect(v).not.to.be.undefined
644 } 589 }
645 }) 590 })
646 591
647 it('Should unblock the local account', async function () { 592 it('Should unblock the local account', async function () {
648 await removeAccountFromServerBlocklist(servers[0].url, servers[0].accessToken, 'user1') 593 await command.removeFromServerBlocklist({ account: 'user1' })
649 }) 594 })
650 595
651 it('Should display its comments', async function () { 596 it('Should display its comments', async function () {
652 for (const token of [ userModeratorToken, servers[0].accessToken ]) { 597 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
653 await checkAllComments(servers[0].url, token, videoUUID1) 598 await checkAllComments(servers[0], token, videoUUID1)
654 } 599 }
655 }) 600 })
656 601
@@ -677,31 +622,33 @@ describe('Test blocklist', function () {
677 describe('When managing server blocklist', function () { 622 describe('When managing server blocklist', function () {
678 it('Should list all videos', async function () { 623 it('Should list all videos', async function () {
679 for (const token of [ userModeratorToken, servers[0].accessToken ]) { 624 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
680 await checkAllVideos(servers[0].url, token) 625 await checkAllVideos(servers[0], token)
681 } 626 }
682 }) 627 })
683 628
684 it('Should list the comments', async function () { 629 it('Should list the comments', async function () {
685 for (const token of [ userModeratorToken, servers[0].accessToken ]) { 630 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
686 await checkAllComments(servers[0].url, token, videoUUID1) 631 await checkAllComments(servers[0], token, videoUUID1)
687 } 632 }
688 }) 633 })
689 634
690 it('Should block a remote server', async function () { 635 it('Should block a remote server', async function () {
691 await addServerToServerBlocklist(servers[0].url, servers[0].accessToken, 'localhost:' + servers[1].port) 636 await command.addToServerBlocklist({ server: 'localhost:' + servers[1].port })
692 }) 637 })
693 638
694 it('Should hide its videos', async function () { 639 it('Should hide its videos', async function () {
695 for (const token of [ userModeratorToken, servers[0].accessToken ]) { 640 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
696 const res1 = await getVideosList(servers[0].url) 641 const requests = [
697 const res2 = await getVideosListWithToken(servers[0].url, token) 642 servers[0].videos.list(),
643 servers[0].videos.listWithToken({ token })
644 ]
698 645
699 for (const res of [ res1, res2 ]) { 646 for (const req of requests) {
700 const videos: Video[] = res.body.data 647 const { data } = await req
701 expect(videos).to.have.lengthOf(3) 648 expect(data).to.have.lengthOf(3)
702 649
703 const v1 = videos.find(v => v.name === 'video user 2') 650 const v1 = data.find(v => v.name === 'video user 2')
704 const v2 = videos.find(v => v.name === 'video server 2') 651 const v2 = data.find(v => v.name === 'video server 2')
705 652
706 expect(v1).to.be.undefined 653 expect(v1).to.be.undefined
707 expect(v2).to.be.undefined 654 expect(v2).to.be.undefined
@@ -712,14 +659,13 @@ describe('Test blocklist', function () {
712 it('Should hide its comments', async function () { 659 it('Should hide its comments', async function () {
713 this.timeout(10000) 660 this.timeout(10000)
714 661
715 const resThreads = await addVideoCommentThread(servers[1].url, userToken2, videoUUID1, 'hidden comment 2') 662 const { id } = await commentsCommand[1].createThread({ token: userToken2, videoId: videoUUID1, text: 'hidden comment 2' })
716 const threadId = resThreads.body.comment.id
717 663
718 await waitJobs(servers) 664 await waitJobs(servers)
719 665
720 await checkAllComments(servers[0].url, servers[0].accessToken, videoUUID1) 666 await checkAllComments(servers[0], servers[0].accessToken, videoUUID1)
721 667
722 await deleteVideoComment(servers[1].url, userToken2, videoUUID1, threadId) 668 await commentsCommand[1].delete({ token: userToken2, videoId: videoUUID1, commentId: id })
723 }) 669 })
724 670
725 it('Should not have notification from blocked instances by instance', async function () { 671 it('Should not have notification from blocked instances by instance', async function () {
@@ -742,48 +688,44 @@ describe('Test blocklist', function () {
742 688
743 { 689 {
744 const now = new Date() 690 const now = new Date()
745 await unfollow(servers[1].url, servers[1].accessToken, servers[0]) 691 await servers[1].follows.unfollow({ target: servers[0] })
746 await waitJobs(servers) 692 await waitJobs(servers)
747 await follow(servers[1].url, [ servers[0].host ], servers[1].accessToken) 693 await servers[1].follows.follow({ hosts: [ servers[0].host ] })
748 694
749 await waitJobs(servers) 695 await waitJobs(servers)
750 696
751 const res = await getUserNotifications(servers[0].url, servers[0].accessToken, 0, 30) 697 const { data } = await servers[0].notifications.list({ start: 0, count: 30 })
752 const commentNotifications = (res.body.data as UserNotification[]) 698 const commentNotifications = data.filter(n => {
753 .filter(n => { 699 return n.type === UserNotificationType.NEW_INSTANCE_FOLLOWER && n.createdAt >= now.toISOString()
754 return n.type === UserNotificationType.NEW_INSTANCE_FOLLOWER && 700 })
755 n.createdAt >= now.toISOString()
756 })
757 701
758 expect(commentNotifications).to.have.lengthOf(0) 702 expect(commentNotifications).to.have.lengthOf(0)
759 } 703 }
760 }) 704 })
761 705
762 it('Should list blocked servers', async function () { 706 it('Should list blocked servers', async function () {
763 const res = await getServerBlocklistByServer(servers[0].url, servers[0].accessToken, 0, 1, 'createdAt') 707 const body = await command.listServerServerBlocklist({ start: 0, count: 1, sort: 'createdAt' })
764 const blocks: ServerBlock[] = res.body.data 708 expect(body.total).to.equal(1)
765
766 expect(res.body.total).to.equal(1)
767 709
768 const block = blocks[0] 710 const block = body.data[0]
769 expect(block.byAccount.displayName).to.equal('peertube') 711 expect(block.byAccount.displayName).to.equal('peertube')
770 expect(block.byAccount.name).to.equal('peertube') 712 expect(block.byAccount.name).to.equal('peertube')
771 expect(block.blockedServer.host).to.equal('localhost:' + servers[1].port) 713 expect(block.blockedServer.host).to.equal('localhost:' + servers[1].port)
772 }) 714 })
773 715
774 it('Should unblock the remote server', async function () { 716 it('Should unblock the remote server', async function () {
775 await removeServerFromServerBlocklist(servers[0].url, servers[0].accessToken, 'localhost:' + servers[1].port) 717 await command.removeFromServerBlocklist({ server: 'localhost:' + servers[1].port })
776 }) 718 })
777 719
778 it('Should list all videos', async function () { 720 it('Should list all videos', async function () {
779 for (const token of [ userModeratorToken, servers[0].accessToken ]) { 721 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
780 await checkAllVideos(servers[0].url, token) 722 await checkAllVideos(servers[0], token)
781 } 723 }
782 }) 724 })
783 725
784 it('Should list the comments', async function () { 726 it('Should list the comments', async function () {
785 for (const token of [ userModeratorToken, servers[0].accessToken ]) { 727 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
786 await checkAllComments(servers[0].url, token, videoUUID1) 728 await checkAllComments(servers[0], token, videoUUID1)
787 } 729 }
788 }) 730 })
789 731
@@ -807,18 +749,16 @@ describe('Test blocklist', function () {
807 749
808 { 750 {
809 const now = new Date() 751 const now = new Date()
810 await unfollow(servers[1].url, servers[1].accessToken, servers[0]) 752 await servers[1].follows.unfollow({ target: servers[0] })
811 await waitJobs(servers) 753 await waitJobs(servers)
812 await follow(servers[1].url, [ servers[0].host ], servers[1].accessToken) 754 await servers[1].follows.follow({ hosts: [ servers[0].host ] })
813 755
814 await waitJobs(servers) 756 await waitJobs(servers)
815 757
816 const res = await getUserNotifications(servers[0].url, servers[0].accessToken, 0, 30) 758 const { data } = await servers[0].notifications.list({ start: 0, count: 30 })
817 const commentNotifications = (res.body.data as UserNotification[]) 759 const commentNotifications = data.filter(n => {
818 .filter(n => { 760 return n.type === UserNotificationType.NEW_INSTANCE_FOLLOWER && n.createdAt >= now.toISOString()
819 return n.type === UserNotificationType.NEW_INSTANCE_FOLLOWER && 761 })
820 n.createdAt >= now.toISOString()
821 })
822 762
823 expect(commentNotifications).to.have.lengthOf(1) 763 expect(commentNotifications).to.have.lengthOf(1)
824 } 764 }
diff --git a/server/tests/api/moderation/video-blacklist.ts b/server/tests/api/moderation/video-blacklist.ts
index 52cac20d9..d5838191a 100644
--- a/server/tests/api/moderation/video-blacklist.ts
+++ b/server/tests/api/moderation/video-blacklist.ts
@@ -4,44 +4,30 @@ import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5import { orderBy } from 'lodash' 5import { orderBy } from 'lodash'
6import { 6import {
7 addVideoToBlacklist, 7 BlacklistCommand,
8 cleanupTests, 8 cleanupTests,
9 createUser, 9 createMultipleServers,
10 flushAndRunMultipleServers, 10 doubleFollow,
11 getBlacklistedVideosList, 11 FIXTURE_URLS,
12 getMyUserInformation,
13 getMyVideos,
14 getVideosList,
15 killallServers, 12 killallServers,
16 removeVideoFromBlacklist, 13 PeerTubeServer,
17 reRunServer,
18 searchVideo,
19 ServerInfo,
20 setAccessTokensToServers, 14 setAccessTokensToServers,
21 updateVideo, 15 waitJobs
22 updateVideoBlacklist, 16} from '@shared/extra-utils'
23 uploadVideo, 17import { UserAdminFlag, UserRole, VideoBlacklist, VideoBlacklistType } from '@shared/models'
24 userLogin
25} from '../../../../shared/extra-utils/index'
26import { doubleFollow } from '../../../../shared/extra-utils/server/follows'
27import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
28import { getGoodVideoUrl, getMagnetURI, importVideo } from '../../../../shared/extra-utils/videos/video-imports'
29import { User, UserRole } from '../../../../shared/models/users'
30import { UserAdminFlag } from '../../../../shared/models/users/user-flag.model'
31import { VideoBlacklist, VideoBlacklistType } from '../../../../shared/models/videos'
32 18
33const expect = chai.expect 19const expect = chai.expect
34 20
35describe('Test video blacklist', function () { 21describe('Test video blacklist', function () {
36 let servers: ServerInfo[] = [] 22 let servers: PeerTubeServer[] = []
37 let videoId: number 23 let videoId: number
24 let command: BlacklistCommand
38 25
39 async function blacklistVideosOnServer (server: ServerInfo) { 26 async function blacklistVideosOnServer (server: PeerTubeServer) {
40 const res = await getVideosList(server.url) 27 const { data } = await server.videos.list()
41 28
42 const videos = res.body.data 29 for (const video of data) {
43 for (const video of videos) { 30 await server.blacklist.add({ videoId: video.id, reason: 'super reason' })
44 await addVideoToBlacklist(server.url, server.accessToken, video.id, 'super reason')
45 } 31 }
46 } 32 }
47 33
@@ -49,7 +35,7 @@ describe('Test video blacklist', function () {
49 this.timeout(50000) 35 this.timeout(50000)
50 36
51 // Run servers 37 // Run servers
52 servers = await flushAndRunMultipleServers(2) 38 servers = await createMultipleServers(2)
53 39
54 // Get the access tokens 40 // Get the access tokens
55 await setAccessTokensToServers(servers) 41 await setAccessTokensToServers(servers)
@@ -58,12 +44,14 @@ describe('Test video blacklist', function () {
58 await doubleFollow(servers[0], servers[1]) 44 await doubleFollow(servers[0], servers[1])
59 45
60 // Upload 2 videos on server 2 46 // Upload 2 videos on server 2
61 await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'My 1st video', description: 'A video on server 2' }) 47 await servers[1].videos.upload({ attributes: { name: 'My 1st video', description: 'A video on server 2' } })
62 await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'My 2nd video', description: 'A video on server 2' }) 48 await servers[1].videos.upload({ attributes: { name: 'My 2nd video', description: 'A video on server 2' } })
63 49
64 // Wait videos propagation, server 2 has transcoding enabled 50 // Wait videos propagation, server 2 has transcoding enabled
65 await waitJobs(servers) 51 await waitJobs(servers)
66 52
53 command = servers[0].blacklist
54
67 // Blacklist the two videos on server 1 55 // Blacklist the two videos on server 1
68 await blacklistVideosOnServer(servers[0]) 56 await blacklistVideosOnServer(servers[0])
69 }) 57 })
@@ -72,48 +60,47 @@ describe('Test video blacklist', function () {
72 60
73 it('Should not have the video blacklisted in videos list/search on server 1', async function () { 61 it('Should not have the video blacklisted in videos list/search on server 1', async function () {
74 { 62 {
75 const res = await getVideosList(servers[0].url) 63 const { total, data } = await servers[0].videos.list()
76 64
77 expect(res.body.total).to.equal(0) 65 expect(total).to.equal(0)
78 expect(res.body.data).to.be.an('array') 66 expect(data).to.be.an('array')
79 expect(res.body.data.length).to.equal(0) 67 expect(data.length).to.equal(0)
80 } 68 }
81 69
82 { 70 {
83 const res = await searchVideo(servers[0].url, 'name') 71 const body = await servers[0].search.searchVideos({ search: 'video' })
84 72
85 expect(res.body.total).to.equal(0) 73 expect(body.total).to.equal(0)
86 expect(res.body.data).to.be.an('array') 74 expect(body.data).to.be.an('array')
87 expect(res.body.data.length).to.equal(0) 75 expect(body.data.length).to.equal(0)
88 } 76 }
89 }) 77 })
90 78
91 it('Should have the blacklisted video in videos list/search on server 2', async function () { 79 it('Should have the blacklisted video in videos list/search on server 2', async function () {
92 { 80 {
93 const res = await getVideosList(servers[1].url) 81 const { total, data } = await servers[1].videos.list()
94 82
95 expect(res.body.total).to.equal(2) 83 expect(total).to.equal(2)
96 expect(res.body.data).to.be.an('array') 84 expect(data).to.be.an('array')
97 expect(res.body.data.length).to.equal(2) 85 expect(data.length).to.equal(2)
98 } 86 }
99 87
100 { 88 {
101 const res = await searchVideo(servers[1].url, 'video') 89 const body = await servers[1].search.searchVideos({ search: 'video' })
102 90
103 expect(res.body.total).to.equal(2) 91 expect(body.total).to.equal(2)
104 expect(res.body.data).to.be.an('array') 92 expect(body.data).to.be.an('array')
105 expect(res.body.data.length).to.equal(2) 93 expect(body.data.length).to.equal(2)
106 } 94 }
107 }) 95 })
108 }) 96 })
109 97
110 describe('When listing manually blacklisted videos', function () { 98 describe('When listing manually blacklisted videos', function () {
111 it('Should display all the blacklisted videos', async function () { 99 it('Should display all the blacklisted videos', async function () {
112 const res = await getBlacklistedVideosList({ url: servers[0].url, token: servers[0].accessToken }) 100 const body = await command.list()
113 101 expect(body.total).to.equal(2)
114 expect(res.body.total).to.equal(2)
115 102
116 const blacklistedVideos = res.body.data 103 const blacklistedVideos = body.data
117 expect(blacklistedVideos).to.be.an('array') 104 expect(blacklistedVideos).to.be.an('array')
118 expect(blacklistedVideos.length).to.equal(2) 105 expect(blacklistedVideos.length).to.equal(2)
119 106
@@ -124,79 +111,66 @@ describe('Test video blacklist', function () {
124 }) 111 })
125 112
126 it('Should display all the blacklisted videos when applying manual type filter', async function () { 113 it('Should display all the blacklisted videos when applying manual type filter', async function () {
127 const res = await getBlacklistedVideosList({ 114 const body = await command.list({ type: VideoBlacklistType.MANUAL })
128 url: servers[0].url, 115 expect(body.total).to.equal(2)
129 token: servers[0].accessToken,
130 type: VideoBlacklistType.MANUAL
131 })
132 116
133 expect(res.body.total).to.equal(2) 117 const blacklistedVideos = body.data
134
135 const blacklistedVideos = res.body.data
136 expect(blacklistedVideos).to.be.an('array') 118 expect(blacklistedVideos).to.be.an('array')
137 expect(blacklistedVideos.length).to.equal(2) 119 expect(blacklistedVideos.length).to.equal(2)
138 }) 120 })
139 121
140 it('Should display nothing when applying automatic type filter', async function () { 122 it('Should display nothing when applying automatic type filter', async function () {
141 const res = await getBlacklistedVideosList({ 123 const body = await command.list({ type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED })
142 url: servers[0].url, 124 expect(body.total).to.equal(0)
143 token: servers[0].accessToken,
144 type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED
145 })
146
147 expect(res.body.total).to.equal(0)
148 125
149 const blacklistedVideos = res.body.data 126 const blacklistedVideos = body.data
150 expect(blacklistedVideos).to.be.an('array') 127 expect(blacklistedVideos).to.be.an('array')
151 expect(blacklistedVideos.length).to.equal(0) 128 expect(blacklistedVideos.length).to.equal(0)
152 }) 129 })
153 130
154 it('Should get the correct sort when sorting by descending id', async function () { 131 it('Should get the correct sort when sorting by descending id', async function () {
155 const res = await getBlacklistedVideosList({ url: servers[0].url, token: servers[0].accessToken, sort: '-id' }) 132 const body = await command.list({ sort: '-id' })
156 expect(res.body.total).to.equal(2) 133 expect(body.total).to.equal(2)
157 134
158 const blacklistedVideos = res.body.data 135 const blacklistedVideos = body.data
159 expect(blacklistedVideos).to.be.an('array') 136 expect(blacklistedVideos).to.be.an('array')
160 expect(blacklistedVideos.length).to.equal(2) 137 expect(blacklistedVideos.length).to.equal(2)
161 138
162 const result = orderBy(res.body.data, [ 'id' ], [ 'desc' ]) 139 const result = orderBy(body.data, [ 'id' ], [ 'desc' ])
163
164 expect(blacklistedVideos).to.deep.equal(result) 140 expect(blacklistedVideos).to.deep.equal(result)
165 }) 141 })
166 142
167 it('Should get the correct sort when sorting by descending video name', async function () { 143 it('Should get the correct sort when sorting by descending video name', async function () {
168 const res = await getBlacklistedVideosList({ url: servers[0].url, token: servers[0].accessToken, sort: '-name' }) 144 const body = await command.list({ sort: '-name' })
169 expect(res.body.total).to.equal(2) 145 expect(body.total).to.equal(2)
170 146
171 const blacklistedVideos = res.body.data 147 const blacklistedVideos = body.data
172 expect(blacklistedVideos).to.be.an('array') 148 expect(blacklistedVideos).to.be.an('array')
173 expect(blacklistedVideos.length).to.equal(2) 149 expect(blacklistedVideos.length).to.equal(2)
174 150
175 const result = orderBy(res.body.data, [ 'name' ], [ 'desc' ]) 151 const result = orderBy(body.data, [ 'name' ], [ 'desc' ])
176
177 expect(blacklistedVideos).to.deep.equal(result) 152 expect(blacklistedVideos).to.deep.equal(result)
178 }) 153 })
179 154
180 it('Should get the correct sort when sorting by ascending creation date', async function () { 155 it('Should get the correct sort when sorting by ascending creation date', async function () {
181 const res = await getBlacklistedVideosList({ url: servers[0].url, token: servers[0].accessToken, sort: 'createdAt' }) 156 const body = await command.list({ sort: 'createdAt' })
182 expect(res.body.total).to.equal(2) 157 expect(body.total).to.equal(2)
183 158
184 const blacklistedVideos = res.body.data 159 const blacklistedVideos = body.data
185 expect(blacklistedVideos).to.be.an('array') 160 expect(blacklistedVideos).to.be.an('array')
186 expect(blacklistedVideos.length).to.equal(2) 161 expect(blacklistedVideos.length).to.equal(2)
187 162
188 const result = orderBy(res.body.data, [ 'createdAt' ]) 163 const result = orderBy(body.data, [ 'createdAt' ])
189
190 expect(blacklistedVideos).to.deep.equal(result) 164 expect(blacklistedVideos).to.deep.equal(result)
191 }) 165 })
192 }) 166 })
193 167
194 describe('When updating blacklisted videos', function () { 168 describe('When updating blacklisted videos', function () {
195 it('Should change the reason', async function () { 169 it('Should change the reason', async function () {
196 await updateVideoBlacklist(servers[0].url, servers[0].accessToken, videoId, 'my super reason updated') 170 await command.update({ videoId, reason: 'my super reason updated' })
197 171
198 const res = await getBlacklistedVideosList({ url: servers[0].url, token: servers[0].accessToken, sort: '-name' }) 172 const body = await command.list({ sort: '-name' })
199 const video = res.body.data.find(b => b.video.id === videoId) 173 const video = body.data.find(b => b.video.id === videoId)
200 174
201 expect(video.reason).to.equal('my super reason updated') 175 expect(video.reason).to.equal('my super reason updated')
202 }) 176 })
@@ -206,12 +180,12 @@ describe('Test video blacklist', function () {
206 it('Should display blacklisted videos', async function () { 180 it('Should display blacklisted videos', async function () {
207 await blacklistVideosOnServer(servers[1]) 181 await blacklistVideosOnServer(servers[1])
208 182
209 const res = await getMyVideos(servers[1].url, servers[1].accessToken, 0, 5) 183 const { total, data } = await servers[1].videos.listMyVideos()
210 184
211 expect(res.body.total).to.equal(2) 185 expect(total).to.equal(2)
212 expect(res.body.data).to.have.lengthOf(2) 186 expect(data).to.have.lengthOf(2)
213 187
214 for (const video of res.body.data) { 188 for (const video of data) {
215 expect(video.blacklisted).to.be.true 189 expect(video.blacklisted).to.be.true
216 expect(video.blacklistedReason).to.equal('super reason') 190 expect(video.blacklistedReason).to.equal('super reason')
217 } 191 }
@@ -223,39 +197,38 @@ describe('Test video blacklist', function () {
223 let blacklist = [] 197 let blacklist = []
224 198
225 it('Should not have any video in videos list on server 1', async function () { 199 it('Should not have any video in videos list on server 1', async function () {
226 const res = await getVideosList(servers[0].url) 200 const { total, data } = await servers[0].videos.list()
227 expect(res.body.total).to.equal(0) 201 expect(total).to.equal(0)
228 expect(res.body.data).to.be.an('array') 202 expect(data).to.be.an('array')
229 expect(res.body.data.length).to.equal(0) 203 expect(data.length).to.equal(0)
230 }) 204 })
231 205
232 it('Should remove a video from the blacklist on server 1', async function () { 206 it('Should remove a video from the blacklist on server 1', async function () {
233 // Get one video in the blacklist 207 // Get one video in the blacklist
234 const res = await getBlacklistedVideosList({ url: servers[0].url, token: servers[0].accessToken, sort: '-name' }) 208 const body = await command.list({ sort: '-name' })
235 videoToRemove = res.body.data[0] 209 videoToRemove = body.data[0]
236 blacklist = res.body.data.slice(1) 210 blacklist = body.data.slice(1)
237 211
238 // Remove it 212 // Remove it
239 await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, videoToRemove.video.id) 213 await command.remove({ videoId: videoToRemove.video.id })
240 }) 214 })
241 215
242 it('Should have the ex-blacklisted video in videos list on server 1', async function () { 216 it('Should have the ex-blacklisted video in videos list on server 1', async function () {
243 const res = await getVideosList(servers[0].url) 217 const { total, data } = await servers[0].videos.list()
244 expect(res.body.total).to.equal(1) 218 expect(total).to.equal(1)
245 219
246 const videos = res.body.data 220 expect(data).to.be.an('array')
247 expect(videos).to.be.an('array') 221 expect(data.length).to.equal(1)
248 expect(videos.length).to.equal(1)
249 222
250 expect(videos[0].name).to.equal(videoToRemove.video.name) 223 expect(data[0].name).to.equal(videoToRemove.video.name)
251 expect(videos[0].id).to.equal(videoToRemove.video.id) 224 expect(data[0].id).to.equal(videoToRemove.video.id)
252 }) 225 })
253 226
254 it('Should not have the ex-blacklisted video in videos blacklist list on server 1', async function () { 227 it('Should not have the ex-blacklisted video in videos blacklist list on server 1', async function () {
255 const res = await getBlacklistedVideosList({ url: servers[0].url, token: servers[0].accessToken, sort: '-name' }) 228 const body = await command.list({ sort: '-name' })
256 expect(res.body.total).to.equal(1) 229 expect(body.total).to.equal(1)
257 230
258 const videos = res.body.data 231 const videos = body.data
259 expect(videos).to.be.an('array') 232 expect(videos).to.be.an('array')
260 expect(videos.length).to.equal(1) 233 expect(videos.length).to.equal(1)
261 expect(videos).to.deep.equal(blacklist) 234 expect(videos).to.deep.equal(blacklist)
@@ -270,12 +243,12 @@ describe('Test video blacklist', function () {
270 this.timeout(10000) 243 this.timeout(10000)
271 244
272 { 245 {
273 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'Video 3' }) 246 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'Video 3' } })
274 video3UUID = res.body.video.uuid 247 video3UUID = uuid
275 } 248 }
276 { 249 {
277 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'Video 4' }) 250 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'Video 4' } })
278 video4UUID = res.body.video.uuid 251 video4UUID = uuid
279 } 252 }
280 253
281 await waitJobs(servers) 254 await waitJobs(servers)
@@ -284,51 +257,51 @@ describe('Test video blacklist', function () {
284 it('Should blacklist video 3 and keep it federated', async function () { 257 it('Should blacklist video 3 and keep it federated', async function () {
285 this.timeout(10000) 258 this.timeout(10000)
286 259
287 await addVideoToBlacklist(servers[0].url, servers[0].accessToken, video3UUID, 'super reason', false) 260 await command.add({ videoId: video3UUID, reason: 'super reason', unfederate: false })
288 261
289 await waitJobs(servers) 262 await waitJobs(servers)
290 263
291 { 264 {
292 const res = await getVideosList(servers[0].url) 265 const { data } = await servers[0].videos.list()
293 expect(res.body.data.find(v => v.uuid === video3UUID)).to.be.undefined 266 expect(data.find(v => v.uuid === video3UUID)).to.be.undefined
294 } 267 }
295 268
296 { 269 {
297 const res = await getVideosList(servers[1].url) 270 const { data } = await servers[1].videos.list()
298 expect(res.body.data.find(v => v.uuid === video3UUID)).to.not.be.undefined 271 expect(data.find(v => v.uuid === video3UUID)).to.not.be.undefined
299 } 272 }
300 }) 273 })
301 274
302 it('Should unfederate the video', async function () { 275 it('Should unfederate the video', async function () {
303 this.timeout(10000) 276 this.timeout(10000)
304 277
305 await addVideoToBlacklist(servers[0].url, servers[0].accessToken, video4UUID, 'super reason', true) 278 await command.add({ videoId: video4UUID, reason: 'super reason', unfederate: true })
306 279
307 await waitJobs(servers) 280 await waitJobs(servers)
308 281
309 for (const server of servers) { 282 for (const server of servers) {
310 const res = await getVideosList(server.url) 283 const { data } = await server.videos.list()
311 expect(res.body.data.find(v => v.uuid === video4UUID)).to.be.undefined 284 expect(data.find(v => v.uuid === video4UUID)).to.be.undefined
312 } 285 }
313 }) 286 })
314 287
315 it('Should have the video unfederated even after an Update AP message', async function () { 288 it('Should have the video unfederated even after an Update AP message', async function () {
316 this.timeout(10000) 289 this.timeout(10000)
317 290
318 await updateVideo(servers[0].url, servers[0].accessToken, video4UUID, { description: 'super description' }) 291 await servers[0].videos.update({ id: video4UUID, attributes: { description: 'super description' } })
319 292
320 await waitJobs(servers) 293 await waitJobs(servers)
321 294
322 for (const server of servers) { 295 for (const server of servers) {
323 const res = await getVideosList(server.url) 296 const { data } = await server.videos.list()
324 expect(res.body.data.find(v => v.uuid === video4UUID)).to.be.undefined 297 expect(data.find(v => v.uuid === video4UUID)).to.be.undefined
325 } 298 }
326 }) 299 })
327 300
328 it('Should have the correct video blacklist unfederate attribute', async function () { 301 it('Should have the correct video blacklist unfederate attribute', async function () {
329 const res = await getBlacklistedVideosList({ url: servers[0].url, token: servers[0].accessToken, sort: 'createdAt' }) 302 const body = await command.list({ sort: 'createdAt' })
330 303
331 const blacklistedVideos: VideoBlacklist[] = res.body.data 304 const blacklistedVideos = body.data
332 const video3Blacklisted = blacklistedVideos.find(b => b.video.uuid === video3UUID) 305 const video3Blacklisted = blacklistedVideos.find(b => b.video.uuid === video3UUID)
333 const video4Blacklisted = blacklistedVideos.find(b => b.video.uuid === video4UUID) 306 const video4Blacklisted = blacklistedVideos.find(b => b.video.uuid === video4UUID)
334 307
@@ -339,13 +312,13 @@ describe('Test video blacklist', function () {
339 it('Should remove the video from blacklist and refederate the video', async function () { 312 it('Should remove the video from blacklist and refederate the video', async function () {
340 this.timeout(10000) 313 this.timeout(10000)
341 314
342 await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, video4UUID) 315 await command.remove({ videoId: video4UUID })
343 316
344 await waitJobs(servers) 317 await waitJobs(servers)
345 318
346 for (const server of servers) { 319 for (const server of servers) {
347 const res = await getVideosList(server.url) 320 const { data } = await server.videos.list()
348 expect(res.body.data.find(v => v.uuid === video4UUID)).to.not.be.undefined 321 expect(data.find(v => v.uuid === video4UUID)).to.not.be.undefined
349 } 322 }
350 }) 323 })
351 324
@@ -359,7 +332,7 @@ describe('Test video blacklist', function () {
359 before(async function () { 332 before(async function () {
360 this.timeout(20000) 333 this.timeout(20000)
361 334
362 killallServers([ servers[0] ]) 335 await killallServers([ servers[0] ])
363 336
364 const config = { 337 const config = {
365 auto_blacklist: { 338 auto_blacklist: {
@@ -370,106 +343,79 @@ describe('Test video blacklist', function () {
370 } 343 }
371 } 344 }
372 } 345 }
373 await reRunServer(servers[0], config) 346 await servers[0].run(config)
374 347
375 { 348 {
376 const user = { username: 'user_without_flag', password: 'password' } 349 const user = { username: 'user_without_flag', password: 'password' }
377 await createUser({ 350 await servers[0].users.create({
378 url: servers[0].url,
379 accessToken: servers[0].accessToken,
380 username: user.username, 351 username: user.username,
381 adminFlags: UserAdminFlag.NONE, 352 adminFlags: UserAdminFlag.NONE,
382 password: user.password, 353 password: user.password,
383 role: UserRole.USER 354 role: UserRole.USER
384 }) 355 })
385 356
386 userWithoutFlag = await userLogin(servers[0], user) 357 userWithoutFlag = await servers[0].login.getAccessToken(user)
387 358
388 const res = await getMyUserInformation(servers[0].url, userWithoutFlag) 359 const { videoChannels } = await servers[0].users.getMyInfo({ token: userWithoutFlag })
389 const body: User = res.body 360 channelOfUserWithoutFlag = videoChannels[0].id
390 channelOfUserWithoutFlag = body.videoChannels[0].id
391 } 361 }
392 362
393 { 363 {
394 const user = { username: 'user_with_flag', password: 'password' } 364 const user = { username: 'user_with_flag', password: 'password' }
395 await createUser({ 365 await servers[0].users.create({
396 url: servers[0].url,
397 accessToken: servers[0].accessToken,
398 username: user.username, 366 username: user.username,
399 adminFlags: UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST, 367 adminFlags: UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST,
400 password: user.password, 368 password: user.password,
401 role: UserRole.USER 369 role: UserRole.USER
402 }) 370 })
403 371
404 userWithFlag = await userLogin(servers[0], user) 372 userWithFlag = await servers[0].login.getAccessToken(user)
405 } 373 }
406 374
407 await waitJobs(servers) 375 await waitJobs(servers)
408 }) 376 })
409 377
410 it('Should auto blacklist a video on upload', async function () { 378 it('Should auto blacklist a video on upload', async function () {
411 await uploadVideo(servers[0].url, userWithoutFlag, { name: 'blacklisted' }) 379 await servers[0].videos.upload({ token: userWithoutFlag, attributes: { name: 'blacklisted' } })
412
413 const res = await getBlacklistedVideosList({
414 url: servers[0].url,
415 token: servers[0].accessToken,
416 type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED
417 })
418 380
419 expect(res.body.total).to.equal(1) 381 const body = await command.list({ type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED })
420 expect(res.body.data[0].video.name).to.equal('blacklisted') 382 expect(body.total).to.equal(1)
383 expect(body.data[0].video.name).to.equal('blacklisted')
421 }) 384 })
422 385
423 it('Should auto blacklist a video on URL import', async function () { 386 it('Should auto blacklist a video on URL import', async function () {
424 this.timeout(15000) 387 this.timeout(15000)
425 388
426 const attributes = { 389 const attributes = {
427 targetUrl: getGoodVideoUrl(), 390 targetUrl: FIXTURE_URLS.goodVideo,
428 name: 'URL import', 391 name: 'URL import',
429 channelId: channelOfUserWithoutFlag 392 channelId: channelOfUserWithoutFlag
430 } 393 }
431 await importVideo(servers[0].url, userWithoutFlag, attributes) 394 await servers[0].imports.importVideo({ token: userWithoutFlag, attributes })
432 395
433 const res = await getBlacklistedVideosList({ 396 const body = await command.list({ sort: 'createdAt', type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED })
434 url: servers[0].url, 397 expect(body.total).to.equal(2)
435 token: servers[0].accessToken, 398 expect(body.data[1].video.name).to.equal('URL import')
436 sort: 'createdAt',
437 type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED
438 })
439
440 expect(res.body.total).to.equal(2)
441 expect(res.body.data[1].video.name).to.equal('URL import')
442 }) 399 })
443 400
444 it('Should auto blacklist a video on torrent import', async function () { 401 it('Should auto blacklist a video on torrent import', async function () {
445 const attributes = { 402 const attributes = {
446 magnetUri: getMagnetURI(), 403 magnetUri: FIXTURE_URLS.magnet,
447 name: 'Torrent import', 404 name: 'Torrent import',
448 channelId: channelOfUserWithoutFlag 405 channelId: channelOfUserWithoutFlag
449 } 406 }
450 await importVideo(servers[0].url, userWithoutFlag, attributes) 407 await servers[0].imports.importVideo({ token: userWithoutFlag, attributes })
451
452 const res = await getBlacklistedVideosList({
453 url: servers[0].url,
454 token: servers[0].accessToken,
455 sort: 'createdAt',
456 type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED
457 })
458 408
459 expect(res.body.total).to.equal(3) 409 const body = await command.list({ sort: 'createdAt', type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED })
460 expect(res.body.data[2].video.name).to.equal('Torrent import') 410 expect(body.total).to.equal(3)
411 expect(body.data[2].video.name).to.equal('Torrent import')
461 }) 412 })
462 413
463 it('Should not auto blacklist a video on upload if the user has the bypass blacklist flag', async function () { 414 it('Should not auto blacklist a video on upload if the user has the bypass blacklist flag', async function () {
464 await uploadVideo(servers[0].url, userWithFlag, { name: 'not blacklisted' }) 415 await servers[0].videos.upload({ token: userWithFlag, attributes: { name: 'not blacklisted' } })
465
466 const res = await getBlacklistedVideosList({
467 url: servers[0].url,
468 token: servers[0].accessToken,
469 type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED
470 })
471 416
472 expect(res.body.total).to.equal(3) 417 const body = await command.list({ type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED })
418 expect(body.total).to.equal(3)
473 }) 419 })
474 }) 420 })
475 421
diff --git a/server/tests/api/notifications/admin-notifications.ts b/server/tests/api/notifications/admin-notifications.ts
index cfe0bd2bb..b36ba11a9 100644
--- a/server/tests/api/notifications/admin-notifications.ts
+++ b/server/tests/api/notifications/admin-notifications.ts
@@ -2,21 +2,21 @@
2 2
3import 'mocha' 3import 'mocha'
4import { expect } from 'chai' 4import { expect } from 'chai'
5import { MockJoinPeerTubeVersions } from '@shared/extra-utils/mock-servers/joinpeertube-versions'
6import { PluginType } from '@shared/models'
7import { cleanupTests, installPlugin, setPluginLatestVersion, setPluginVersion, wait } from '../../../../shared/extra-utils'
8import { ServerInfo } from '../../../../shared/extra-utils/index'
9import { MockSmtpServer } from '../../../../shared/extra-utils/miscs/email'
10import { 5import {
11 CheckerBaseParams, 6 CheckerBaseParams,
12 checkNewPeerTubeVersion, 7 checkNewPeerTubeVersion,
13 checkNewPluginVersion, 8 checkNewPluginVersion,
14 prepareNotificationsTest 9 cleanupTests,
15} from '../../../../shared/extra-utils/users/user-notifications' 10 MockJoinPeerTubeVersions,
16import { UserNotification, UserNotificationType } from '../../../../shared/models/users' 11 MockSmtpServer,
12 PeerTubeServer,
13 prepareNotificationsTest,
14 wait
15} from '@shared/extra-utils'
16import { PluginType, UserNotification, UserNotificationType } from '@shared/models'
17 17
18describe('Test admin notifications', function () { 18describe('Test admin notifications', function () {
19 let server: ServerInfo 19 let server: PeerTubeServer
20 let userNotifications: UserNotification[] = [] 20 let userNotifications: UserNotification[] = []
21 let adminNotifications: UserNotification[] = [] 21 let adminNotifications: UserNotification[] = []
22 let emails: object[] = [] 22 let emails: object[] = []
@@ -58,17 +58,8 @@ describe('Test admin notifications', function () {
58 token: server.accessToken 58 token: server.accessToken
59 } 59 }
60 60
61 await installPlugin({ 61 await server.plugins.install({ npmName: 'peertube-plugin-hello-world' })
62 url: server.url, 62 await server.plugins.install({ npmName: 'peertube-theme-background-red' })
63 accessToken: server.accessToken,
64 npmName: 'peertube-plugin-hello-world'
65 })
66
67 await installPlugin({
68 url: server.url,
69 accessToken: server.accessToken,
70 npmName: 'peertube-theme-background-red'
71 })
72 }) 63 })
73 64
74 describe('Latest PeerTube version notification', function () { 65 describe('Latest PeerTube version notification', function () {
@@ -127,8 +118,8 @@ describe('Test admin notifications', function () {
127 it('Should send a notification to admins on new plugin version', async function () { 118 it('Should send a notification to admins on new plugin version', async function () {
128 this.timeout(30000) 119 this.timeout(30000)
129 120
130 await setPluginVersion(server.internalServerNumber, 'hello-world', '0.0.1') 121 await server.sql.setPluginVersion('hello-world', '0.0.1')
131 await setPluginLatestVersion(server.internalServerNumber, 'hello-world', '0.0.1') 122 await server.sql.setPluginLatestVersion('hello-world', '0.0.1')
132 await wait(6000) 123 await wait(6000)
133 124
134 await checkNewPluginVersion(baseParams, PluginType.PLUGIN, 'hello-world', 'presence') 125 await checkNewPluginVersion(baseParams, PluginType.PLUGIN, 'hello-world', 'presence')
@@ -149,8 +140,8 @@ describe('Test admin notifications', function () {
149 it('Should send a new notification after a new plugin release', async function () { 140 it('Should send a new notification after a new plugin release', async function () {
150 this.timeout(30000) 141 this.timeout(30000)
151 142
152 await setPluginVersion(server.internalServerNumber, 'hello-world', '0.0.1') 143 await server.sql.setPluginVersion('hello-world', '0.0.1')
153 await setPluginLatestVersion(server.internalServerNumber, 'hello-world', '0.0.1') 144 await server.sql.setPluginLatestVersion('hello-world', '0.0.1')
154 await wait(6000) 145 await wait(6000)
155 146
156 expect(adminNotifications.filter(n => n.type === UserNotificationType.NEW_PEERTUBE_VERSION)).to.have.lengthOf(2) 147 expect(adminNotifications.filter(n => n.type === UserNotificationType.NEW_PEERTUBE_VERSION)).to.have.lengthOf(2)
diff --git a/server/tests/api/notifications/comments-notifications.ts b/server/tests/api/notifications/comments-notifications.ts
index d2badf237..cbb46e510 100644
--- a/server/tests/api/notifications/comments-notifications.ts
+++ b/server/tests/api/notifications/comments-notifications.ts
@@ -3,30 +3,22 @@
3import 'mocha' 3import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5import { 5import {
6 addAccountToAccountBlocklist,
7 addVideoCommentReply,
8 addVideoCommentThread,
9 checkCommentMention, 6 checkCommentMention,
10 CheckerBaseParams, 7 CheckerBaseParams,
11 checkNewCommentOnMyVideo, 8 checkNewCommentOnMyVideo,
12 cleanupTests, 9 cleanupTests,
13 getVideoCommentThreads,
14 getVideoThreadComments,
15 MockSmtpServer, 10 MockSmtpServer,
11 PeerTubeServer,
16 prepareNotificationsTest, 12 prepareNotificationsTest,
17 removeAccountFromAccountBlocklist,
18 ServerInfo,
19 updateMyUser,
20 uploadVideo,
21 waitJobs 13 waitJobs
22} from '@shared/extra-utils' 14} from '@shared/extra-utils'
23import { UserNotification, VideoCommentThreadTree } from '@shared/models' 15import { UserNotification } from '@shared/models'
24 16
25const expect = chai.expect 17const expect = chai.expect
26 18
27describe('Test comments notifications', function () { 19describe('Test comments notifications', function () {
28 let servers: ServerInfo[] = [] 20 let servers: PeerTubeServer[] = []
29 let userAccessToken: string 21 let userToken: string
30 let userNotifications: UserNotification[] = [] 22 let userNotifications: UserNotification[] = []
31 let emails: object[] = [] 23 let emails: object[] = []
32 24
@@ -40,7 +32,7 @@ describe('Test comments notifications', function () {
40 32
41 const res = await prepareNotificationsTest(2) 33 const res = await prepareNotificationsTest(2)
42 emails = res.emails 34 emails = res.emails
43 userAccessToken = res.userAccessToken 35 userToken = res.userAccessToken
44 servers = res.servers 36 servers = res.servers
45 userNotifications = res.userNotifications 37 userNotifications = res.userNotifications
46 }) 38 })
@@ -53,18 +45,17 @@ describe('Test comments notifications', function () {
53 server: servers[0], 45 server: servers[0],
54 emails, 46 emails,
55 socketNotifications: userNotifications, 47 socketNotifications: userNotifications,
56 token: userAccessToken 48 token: userToken
57 } 49 }
58 }) 50 })
59 51
60 it('Should not send a new comment notification after a comment on another video', async function () { 52 it('Should not send a new comment notification after a comment on another video', async function () {
61 this.timeout(20000) 53 this.timeout(20000)
62 54
63 const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' }) 55 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'super video' } })
64 const uuid = resVideo.body.video.uuid
65 56
66 const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, 'comment') 57 const created = await servers[0].comments.createThread({ videoId: uuid, text: 'comment' })
67 const commentId = resComment.body.comment.id 58 const commentId = created.id
68 59
69 await waitJobs(servers) 60 await waitJobs(servers)
70 await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'absence') 61 await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'absence')
@@ -73,11 +64,10 @@ describe('Test comments notifications', function () {
73 it('Should not send a new comment notification if I comment my own video', async function () { 64 it('Should not send a new comment notification if I comment my own video', async function () {
74 this.timeout(20000) 65 this.timeout(20000)
75 66
76 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' }) 67 const { uuid } = await servers[0].videos.upload({ token: userToken, attributes: { name: 'super video' } })
77 const uuid = resVideo.body.video.uuid
78 68
79 const resComment = await addVideoCommentThread(servers[0].url, userAccessToken, uuid, 'comment') 69 const created = await servers[0].comments.createThread({ token: userToken, videoId: uuid, text: 'comment' })
80 const commentId = resComment.body.comment.id 70 const commentId = created.id
81 71
82 await waitJobs(servers) 72 await waitJobs(servers)
83 await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'absence') 73 await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'absence')
@@ -86,28 +76,26 @@ describe('Test comments notifications', function () {
86 it('Should not send a new comment notification if the account is muted', async function () { 76 it('Should not send a new comment notification if the account is muted', async function () {
87 this.timeout(20000) 77 this.timeout(20000)
88 78
89 await addAccountToAccountBlocklist(servers[0].url, userAccessToken, 'root') 79 await servers[0].blocklist.addToMyBlocklist({ token: userToken, account: 'root' })
90 80
91 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' }) 81 const { uuid } = await servers[0].videos.upload({ token: userToken, attributes: { name: 'super video' } })
92 const uuid = resVideo.body.video.uuid
93 82
94 const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, 'comment') 83 const created = await servers[0].comments.createThread({ videoId: uuid, text: 'comment' })
95 const commentId = resComment.body.comment.id 84 const commentId = created.id
96 85
97 await waitJobs(servers) 86 await waitJobs(servers)
98 await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'absence') 87 await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'absence')
99 88
100 await removeAccountFromAccountBlocklist(servers[0].url, userAccessToken, 'root') 89 await servers[0].blocklist.removeFromMyBlocklist({ token: userToken, account: 'root' })
101 }) 90 })
102 91
103 it('Should send a new comment notification after a local comment on my video', async function () { 92 it('Should send a new comment notification after a local comment on my video', async function () {
104 this.timeout(20000) 93 this.timeout(20000)
105 94
106 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' }) 95 const { uuid } = await servers[0].videos.upload({ token: userToken, attributes: { name: 'super video' } })
107 const uuid = resVideo.body.video.uuid
108 96
109 const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, 'comment') 97 const created = await servers[0].comments.createThread({ videoId: uuid, text: 'comment' })
110 const commentId = resComment.body.comment.id 98 const commentId = created.id
111 99
112 await waitJobs(servers) 100 await waitJobs(servers)
113 await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'presence') 101 await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'presence')
@@ -116,33 +104,29 @@ describe('Test comments notifications', function () {
116 it('Should send a new comment notification after a remote comment on my video', async function () { 104 it('Should send a new comment notification after a remote comment on my video', async function () {
117 this.timeout(20000) 105 this.timeout(20000)
118 106
119 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' }) 107 const { uuid } = await servers[0].videos.upload({ token: userToken, attributes: { name: 'super video' } })
120 const uuid = resVideo.body.video.uuid
121 108
122 await waitJobs(servers) 109 await waitJobs(servers)
123 110
124 await addVideoCommentThread(servers[1].url, servers[1].accessToken, uuid, 'comment') 111 await servers[1].comments.createThread({ videoId: uuid, text: 'comment' })
125 112
126 await waitJobs(servers) 113 await waitJobs(servers)
127 114
128 const resComment = await getVideoCommentThreads(servers[0].url, uuid, 0, 5) 115 const { data } = await servers[0].comments.listThreads({ videoId: uuid })
129 expect(resComment.body.data).to.have.lengthOf(1) 116 expect(data).to.have.lengthOf(1)
130 const commentId = resComment.body.data[0].id
131 117
118 const commentId = data[0].id
132 await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'presence') 119 await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'presence')
133 }) 120 })
134 121
135 it('Should send a new comment notification after a local reply on my video', async function () { 122 it('Should send a new comment notification after a local reply on my video', async function () {
136 this.timeout(20000) 123 this.timeout(20000)
137 124
138 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' }) 125 const { uuid } = await servers[0].videos.upload({ token: userToken, attributes: { name: 'super video' } })
139 const uuid = resVideo.body.video.uuid
140 126
141 const resThread = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, 'comment') 127 const { id: threadId } = await servers[0].comments.createThread({ videoId: uuid, text: 'comment' })
142 const threadId = resThread.body.comment.id
143 128
144 const resComment = await addVideoCommentReply(servers[0].url, servers[0].accessToken, uuid, threadId, 'reply') 129 const { id: commentId } = await servers[0].comments.addReply({ videoId: uuid, toCommentId: threadId, text: 'reply' })
145 const commentId = resComment.body.comment.id
146 130
147 await waitJobs(servers) 131 await waitJobs(servers)
148 await checkNewCommentOnMyVideo(baseParams, uuid, commentId, threadId, 'presence') 132 await checkNewCommentOnMyVideo(baseParams, uuid, commentId, threadId, 'presence')
@@ -151,24 +135,22 @@ describe('Test comments notifications', function () {
151 it('Should send a new comment notification after a remote reply on my video', async function () { 135 it('Should send a new comment notification after a remote reply on my video', async function () {
152 this.timeout(20000) 136 this.timeout(20000)
153 137
154 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' }) 138 const { uuid } = await servers[0].videos.upload({ token: userToken, attributes: { name: 'super video' } })
155 const uuid = resVideo.body.video.uuid
156 await waitJobs(servers) 139 await waitJobs(servers)
157 140
158 { 141 {
159 const resThread = await addVideoCommentThread(servers[1].url, servers[1].accessToken, uuid, 'comment') 142 const created = await servers[1].comments.createThread({ videoId: uuid, text: 'comment' })
160 const threadId = resThread.body.comment.id 143 const threadId = created.id
161 await addVideoCommentReply(servers[1].url, servers[1].accessToken, uuid, threadId, 'reply') 144 await servers[1].comments.addReply({ videoId: uuid, toCommentId: threadId, text: 'reply' })
162 } 145 }
163 146
164 await waitJobs(servers) 147 await waitJobs(servers)
165 148
166 const resThread = await getVideoCommentThreads(servers[0].url, uuid, 0, 5) 149 const { data } = await servers[0].comments.listThreads({ videoId: uuid })
167 expect(resThread.body.data).to.have.lengthOf(1) 150 expect(data).to.have.lengthOf(1)
168 const threadId = resThread.body.data[0].id
169 151
170 const resComments = await getVideoThreadComments(servers[0].url, uuid, threadId) 152 const threadId = data[0].id
171 const tree = resComments.body as VideoCommentThreadTree 153 const tree = await servers[0].comments.getThread({ videoId: uuid, threadId })
172 154
173 expect(tree.children).to.have.lengthOf(1) 155 expect(tree.children).to.have.lengthOf(1)
174 const commentId = tree.children[0].comment.id 156 const commentId = tree.children[0].comment.id
@@ -179,10 +161,9 @@ describe('Test comments notifications', function () {
179 it('Should convert markdown in comment to html', async function () { 161 it('Should convert markdown in comment to html', async function () {
180 this.timeout(20000) 162 this.timeout(20000)
181 163
182 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'cool video' }) 164 const { uuid } = await servers[0].videos.upload({ token: userToken, attributes: { name: 'cool video' } })
183 const uuid = resVideo.body.video.uuid
184 165
185 await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, commentText) 166 await servers[0].comments.createThread({ videoId: uuid, text: commentText })
186 167
187 await waitJobs(servers) 168 await waitJobs(servers)
188 169
@@ -199,30 +180,19 @@ describe('Test comments notifications', function () {
199 server: servers[0], 180 server: servers[0],
200 emails, 181 emails,
201 socketNotifications: userNotifications, 182 socketNotifications: userNotifications,
202 token: userAccessToken 183 token: userToken
203 } 184 }
204 185
205 await updateMyUser({ 186 await servers[0].users.updateMe({ displayName: 'super root name' })
206 url: servers[0].url, 187 await servers[1].users.updateMe({ displayName: 'super root 2 name' })
207 accessToken: servers[0].accessToken,
208 displayName: 'super root name'
209 })
210
211 await updateMyUser({
212 url: servers[1].url,
213 accessToken: servers[1].accessToken,
214 displayName: 'super root 2 name'
215 })
216 }) 188 })
217 189
218 it('Should not send a new mention comment notification if I mention the video owner', async function () { 190 it('Should not send a new mention comment notification if I mention the video owner', async function () {
219 this.timeout(10000) 191 this.timeout(10000)
220 192
221 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' }) 193 const { uuid } = await servers[0].videos.upload({ token: userToken, attributes: { name: 'super video' } })
222 const uuid = resVideo.body.video.uuid
223 194
224 const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, '@user_1 hello') 195 const { id: commentId } = await servers[0].comments.createThread({ videoId: uuid, text: '@user_1 hello' })
225 const commentId = resComment.body.comment.id
226 196
227 await waitJobs(servers) 197 await waitJobs(servers)
228 await checkCommentMention(baseParams, uuid, commentId, commentId, 'super root name', 'absence') 198 await checkCommentMention(baseParams, uuid, commentId, commentId, 'super root name', 'absence')
@@ -231,11 +201,9 @@ describe('Test comments notifications', function () {
231 it('Should not send a new mention comment notification if I mention myself', async function () { 201 it('Should not send a new mention comment notification if I mention myself', async function () {
232 this.timeout(10000) 202 this.timeout(10000)
233 203
234 const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' }) 204 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'super video' } })
235 const uuid = resVideo.body.video.uuid
236 205
237 const resComment = await addVideoCommentThread(servers[0].url, userAccessToken, uuid, '@user_1 hello') 206 const { id: commentId } = await servers[0].comments.createThread({ token: userToken, videoId: uuid, text: '@user_1 hello' })
238 const commentId = resComment.body.comment.id
239 207
240 await waitJobs(servers) 208 await waitJobs(servers)
241 await checkCommentMention(baseParams, uuid, commentId, commentId, 'super root name', 'absence') 209 await checkCommentMention(baseParams, uuid, commentId, commentId, 'super root name', 'absence')
@@ -244,29 +212,25 @@ describe('Test comments notifications', function () {
244 it('Should not send a new mention notification if the account is muted', async function () { 212 it('Should not send a new mention notification if the account is muted', async function () {
245 this.timeout(10000) 213 this.timeout(10000)
246 214
247 await addAccountToAccountBlocklist(servers[0].url, userAccessToken, 'root') 215 await servers[0].blocklist.addToMyBlocklist({ token: userToken, account: 'root' })
248 216
249 const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' }) 217 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'super video' } })
250 const uuid = resVideo.body.video.uuid
251 218
252 const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, '@user_1 hello') 219 const { id: commentId } = await servers[0].comments.createThread({ videoId: uuid, text: '@user_1 hello' })
253 const commentId = resComment.body.comment.id
254 220
255 await waitJobs(servers) 221 await waitJobs(servers)
256 await checkCommentMention(baseParams, uuid, commentId, commentId, 'super root name', 'absence') 222 await checkCommentMention(baseParams, uuid, commentId, commentId, 'super root name', 'absence')
257 223
258 await removeAccountFromAccountBlocklist(servers[0].url, userAccessToken, 'root') 224 await servers[0].blocklist.removeFromMyBlocklist({ token: userToken, account: 'root' })
259 }) 225 })
260 226
261 it('Should not send a new mention notification if the remote account mention a local account', async function () { 227 it('Should not send a new mention notification if the remote account mention a local account', async function () {
262 this.timeout(20000) 228 this.timeout(20000)
263 229
264 const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' }) 230 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'super video' } })
265 const uuid = resVideo.body.video.uuid
266 231
267 await waitJobs(servers) 232 await waitJobs(servers)
268 const resThread = await addVideoCommentThread(servers[1].url, servers[1].accessToken, uuid, '@user_1 hello') 233 const { id: threadId } = await servers[1].comments.createThread({ videoId: uuid, text: '@user_1 hello' })
269 const threadId = resThread.body.comment.id
270 234
271 await waitJobs(servers) 235 await waitJobs(servers)
272 await checkCommentMention(baseParams, uuid, threadId, threadId, 'super root 2 name', 'absence') 236 await checkCommentMention(baseParams, uuid, threadId, threadId, 'super root 2 name', 'absence')
@@ -275,17 +239,14 @@ describe('Test comments notifications', function () {
275 it('Should send a new mention notification after local comments', async function () { 239 it('Should send a new mention notification after local comments', async function () {
276 this.timeout(10000) 240 this.timeout(10000)
277 241
278 const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' }) 242 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'super video' } })
279 const uuid = resVideo.body.video.uuid
280 243
281 const resThread = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, '@user_1 hello 1') 244 const { id: threadId } = await servers[0].comments.createThread({ videoId: uuid, text: '@user_1 hellotext: 1' })
282 const threadId = resThread.body.comment.id
283 245
284 await waitJobs(servers) 246 await waitJobs(servers)
285 await checkCommentMention(baseParams, uuid, threadId, threadId, 'super root name', 'presence') 247 await checkCommentMention(baseParams, uuid, threadId, threadId, 'super root name', 'presence')
286 248
287 const resComment = await addVideoCommentReply(servers[0].url, servers[0].accessToken, uuid, threadId, 'hello 2 @user_1') 249 const { id: commentId } = await servers[0].comments.addReply({ videoId: uuid, toCommentId: threadId, text: 'hello 2 @user_1' })
288 const commentId = resComment.body.comment.id
289 250
290 await waitJobs(servers) 251 await waitJobs(servers)
291 await checkCommentMention(baseParams, uuid, commentId, threadId, 'super root name', 'presence') 252 await checkCommentMention(baseParams, uuid, commentId, threadId, 'super root name', 'presence')
@@ -294,29 +255,27 @@ describe('Test comments notifications', function () {
294 it('Should send a new mention notification after remote comments', async function () { 255 it('Should send a new mention notification after remote comments', async function () {
295 this.timeout(20000) 256 this.timeout(20000)
296 257
297 const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' }) 258 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'super video' } })
298 const uuid = resVideo.body.video.uuid
299 259
300 await waitJobs(servers) 260 await waitJobs(servers)
301 261
302 const text1 = `hello @user_1@localhost:${servers[0].port} 1` 262 const text1 = `hello @user_1@localhost:${servers[0].port} 1`
303 const resThread = await addVideoCommentThread(servers[1].url, servers[1].accessToken, uuid, text1) 263 const { id: server2ThreadId } = await servers[1].comments.createThread({ videoId: uuid, text: text1 })
304 const server2ThreadId = resThread.body.comment.id
305 264
306 await waitJobs(servers) 265 await waitJobs(servers)
307 266
308 const resThread2 = await getVideoCommentThreads(servers[0].url, uuid, 0, 5) 267 const { data } = await servers[0].comments.listThreads({ videoId: uuid })
309 expect(resThread2.body.data).to.have.lengthOf(1) 268 expect(data).to.have.lengthOf(1)
310 const server1ThreadId = resThread2.body.data[0].id 269
270 const server1ThreadId = data[0].id
311 await checkCommentMention(baseParams, uuid, server1ThreadId, server1ThreadId, 'super root 2 name', 'presence') 271 await checkCommentMention(baseParams, uuid, server1ThreadId, server1ThreadId, 'super root 2 name', 'presence')
312 272
313 const text2 = `@user_1@localhost:${servers[0].port} hello 2 @root@localhost:${servers[0].port}` 273 const text2 = `@user_1@localhost:${servers[0].port} hello 2 @root@localhost:${servers[0].port}`
314 await addVideoCommentReply(servers[1].url, servers[1].accessToken, uuid, server2ThreadId, text2) 274 await servers[1].comments.addReply({ videoId: uuid, toCommentId: server2ThreadId, text: text2 })
315 275
316 await waitJobs(servers) 276 await waitJobs(servers)
317 277
318 const resComments = await getVideoThreadComments(servers[0].url, uuid, server1ThreadId) 278 const tree = await servers[0].comments.getThread({ videoId: uuid, threadId: server1ThreadId })
319 const tree = resComments.body as VideoCommentThreadTree
320 279
321 expect(tree.children).to.have.lengthOf(1) 280 expect(tree.children).to.have.lengthOf(1)
322 const commentId = tree.children[0].comment.id 281 const commentId = tree.children[0].comment.id
@@ -327,13 +286,11 @@ describe('Test comments notifications', function () {
327 it('Should convert markdown in comment to html', async function () { 286 it('Should convert markdown in comment to html', async function () {
328 this.timeout(10000) 287 this.timeout(10000)
329 288
330 const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' }) 289 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'super video' } })
331 const uuid = resVideo.body.video.uuid
332 290
333 const resThread = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, '@user_1 hello 1') 291 const { id: threadId } = await servers[0].comments.createThread({ videoId: uuid, text: '@user_1 hello 1' })
334 const threadId = resThread.body.comment.id
335 292
336 await addVideoCommentReply(servers[0].url, servers[0].accessToken, uuid, threadId, '@user_1 ' + commentText) 293 await servers[0].comments.addReply({ videoId: uuid, toCommentId: threadId, text: '@user_1 ' + commentText })
337 294
338 await waitJobs(servers) 295 await waitJobs(servers)
339 296
diff --git a/server/tests/api/notifications/moderation-notifications.ts b/server/tests/api/notifications/moderation-notifications.ts
index 3425480ae..6f74709b3 100644
--- a/server/tests/api/notifications/moderation-notifications.ts
+++ b/server/tests/api/notifications/moderation-notifications.ts
@@ -2,33 +2,6 @@
2 2
3import 'mocha' 3import 'mocha'
4import { buildUUID } from '@server/helpers/uuid' 4import { buildUUID } from '@server/helpers/uuid'
5import { AbuseState } from '@shared/models'
6import {
7 addAbuseMessage,
8 addVideoCommentThread,
9 addVideoToBlacklist,
10 cleanupTests,
11 createUser,
12 follow,
13 generateUserAccessToken,
14 getAccount,
15 getCustomConfig,
16 getVideoCommentThreads,
17 getVideoIdFromUUID,
18 immutableAssign,
19 MockInstancesIndex,
20 registerUser,
21 removeVideoFromBlacklist,
22 reportAbuse,
23 unfollow,
24 updateAbuse,
25 updateCustomConfig,
26 updateCustomSubConfig,
27 wait
28} from '../../../../shared/extra-utils'
29import { ServerInfo, uploadVideo } from '../../../../shared/extra-utils/index'
30import { MockSmtpServer } from '../../../../shared/extra-utils/miscs/email'
31import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
32import { 5import {
33 checkAbuseStateChange, 6 checkAbuseStateChange,
34 checkAutoInstanceFollowing, 7 checkAutoInstanceFollowing,
@@ -43,15 +16,18 @@ import {
43 checkUserRegistered, 16 checkUserRegistered,
44 checkVideoAutoBlacklistForModerators, 17 checkVideoAutoBlacklistForModerators,
45 checkVideoIsPublished, 18 checkVideoIsPublished,
46 prepareNotificationsTest 19 cleanupTests,
47} from '../../../../shared/extra-utils/users/user-notifications' 20 MockInstancesIndex,
48import { addUserSubscription, removeUserSubscription } from '../../../../shared/extra-utils/users/user-subscriptions' 21 MockSmtpServer,
49import { CustomConfig } from '../../../../shared/models/server' 22 PeerTubeServer,
50import { UserNotification } from '../../../../shared/models/users' 23 prepareNotificationsTest,
51import { VideoPrivacy } from '../../../../shared/models/videos' 24 wait,
25 waitJobs
26} from '@shared/extra-utils'
27import { AbuseState, CustomConfig, UserNotification, VideoPrivacy } from '@shared/models'
52 28
53describe('Test moderation notifications', function () { 29describe('Test moderation notifications', function () {
54 let servers: ServerInfo[] = [] 30 let servers: PeerTubeServer[] = []
55 let userAccessToken: string 31 let userAccessToken: string
56 let userNotifications: UserNotification[] = [] 32 let userNotifications: UserNotification[] = []
57 let adminNotifications: UserNotification[] = [] 33 let adminNotifications: UserNotification[] = []
@@ -86,10 +62,9 @@ describe('Test moderation notifications', function () {
86 this.timeout(20000) 62 this.timeout(20000)
87 63
88 const name = 'video for abuse ' + buildUUID() 64 const name = 'video for abuse ' + buildUUID()
89 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name }) 65 const video = await servers[0].videos.upload({ token: userAccessToken, attributes: { name } })
90 const video = resVideo.body.video
91 66
92 await reportAbuse({ url: servers[0].url, token: servers[0].accessToken, videoId: video.id, reason: 'super reason' }) 67 await servers[0].abuses.report({ videoId: video.id, reason: 'super reason' })
93 68
94 await waitJobs(servers) 69 await waitJobs(servers)
95 await checkNewVideoAbuseForModerators(baseParams, video.uuid, name, 'presence') 70 await checkNewVideoAbuseForModerators(baseParams, video.uuid, name, 'presence')
@@ -99,13 +74,12 @@ describe('Test moderation notifications', function () {
99 this.timeout(20000) 74 this.timeout(20000)
100 75
101 const name = 'video for abuse ' + buildUUID() 76 const name = 'video for abuse ' + buildUUID()
102 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name }) 77 const video = await servers[0].videos.upload({ token: userAccessToken, attributes: { name } })
103 const video = resVideo.body.video
104 78
105 await waitJobs(servers) 79 await waitJobs(servers)
106 80
107 const videoId = await getVideoIdFromUUID(servers[1].url, video.uuid) 81 const videoId = await servers[1].videos.getId({ uuid: video.uuid })
108 await reportAbuse({ url: servers[1].url, token: servers[1].accessToken, videoId, reason: 'super reason' }) 82 await servers[1].abuses.report({ videoId, reason: 'super reason' })
109 83
110 await waitJobs(servers) 84 await waitJobs(servers)
111 await checkNewVideoAbuseForModerators(baseParams, video.uuid, name, 'presence') 85 await checkNewVideoAbuseForModerators(baseParams, video.uuid, name, 'presence')
@@ -115,14 +89,16 @@ describe('Test moderation notifications', function () {
115 this.timeout(20000) 89 this.timeout(20000)
116 90
117 const name = 'video for abuse ' + buildUUID() 91 const name = 'video for abuse ' + buildUUID()
118 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name }) 92 const video = await servers[0].videos.upload({ token: userAccessToken, attributes: { name } })
119 const video = resVideo.body.video 93 const comment = await servers[0].comments.createThread({
120 const resComment = await addVideoCommentThread(servers[0].url, userAccessToken, video.id, 'comment abuse ' + buildUUID()) 94 token: userAccessToken,
121 const comment = resComment.body.comment 95 videoId: video.id,
96 text: 'comment abuse ' + buildUUID()
97 })
122 98
123 await waitJobs(servers) 99 await waitJobs(servers)
124 100
125 await reportAbuse({ url: servers[0].url, token: servers[0].accessToken, commentId: comment.id, reason: 'super reason' }) 101 await servers[0].abuses.report({ commentId: comment.id, reason: 'super reason' })
126 102
127 await waitJobs(servers) 103 await waitJobs(servers)
128 await checkNewCommentAbuseForModerators(baseParams, video.uuid, name, 'presence') 104 await checkNewCommentAbuseForModerators(baseParams, video.uuid, name, 'presence')
@@ -132,15 +108,19 @@ describe('Test moderation notifications', function () {
132 this.timeout(20000) 108 this.timeout(20000)
133 109
134 const name = 'video for abuse ' + buildUUID() 110 const name = 'video for abuse ' + buildUUID()
135 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name }) 111 const video = await servers[0].videos.upload({ token: userAccessToken, attributes: { name } })
136 const video = resVideo.body.video 112
137 await addVideoCommentThread(servers[0].url, userAccessToken, video.id, 'comment abuse ' + buildUUID()) 113 await servers[0].comments.createThread({
114 token: userAccessToken,
115 videoId: video.id,
116 text: 'comment abuse ' + buildUUID()
117 })
138 118
139 await waitJobs(servers) 119 await waitJobs(servers)
140 120
141 const resComments = await getVideoCommentThreads(servers[1].url, video.uuid, 0, 5) 121 const { data } = await servers[1].comments.listThreads({ videoId: video.uuid })
142 const commentId = resComments.body.data[0].id 122 const commentId = data[0].id
143 await reportAbuse({ url: servers[1].url, token: servers[1].accessToken, commentId, reason: 'super reason' }) 123 await servers[1].abuses.report({ commentId, reason: 'super reason' })
144 124
145 await waitJobs(servers) 125 await waitJobs(servers)
146 await checkNewCommentAbuseForModerators(baseParams, video.uuid, name, 'presence') 126 await checkNewCommentAbuseForModerators(baseParams, video.uuid, name, 'presence')
@@ -150,10 +130,10 @@ describe('Test moderation notifications', function () {
150 this.timeout(20000) 130 this.timeout(20000)
151 131
152 const username = 'user' + new Date().getTime() 132 const username = 'user' + new Date().getTime()
153 const resUser = await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username, password: 'donald' }) 133 const { account } = await servers[0].users.create({ username, password: 'donald' })
154 const accountId = resUser.body.user.account.id 134 const accountId = account.id
155 135
156 await reportAbuse({ url: servers[0].url, token: servers[0].accessToken, accountId, reason: 'super reason' }) 136 await servers[0].abuses.report({ accountId, reason: 'super reason' })
157 137
158 await waitJobs(servers) 138 await waitJobs(servers)
159 await checkNewAccountAbuseForModerators(baseParams, username, 'presence') 139 await checkNewAccountAbuseForModerators(baseParams, username, 'presence')
@@ -163,13 +143,13 @@ describe('Test moderation notifications', function () {
163 this.timeout(20000) 143 this.timeout(20000)
164 144
165 const username = 'user' + new Date().getTime() 145 const username = 'user' + new Date().getTime()
166 const tmpToken = await generateUserAccessToken(servers[0], username) 146 const tmpToken = await servers[0].users.generateUserAndToken(username)
167 await uploadVideo(servers[0].url, tmpToken, { name: 'super video' }) 147 await servers[0].videos.upload({ token: tmpToken, attributes: { name: 'super video' } })
168 148
169 await waitJobs(servers) 149 await waitJobs(servers)
170 150
171 const resAccount = await getAccount(servers[1].url, username + '@' + servers[0].host) 151 const account = await servers[1].accounts.get({ accountName: username + '@' + servers[0].host })
172 await reportAbuse({ url: servers[1].url, token: servers[1].accessToken, accountId: resAccount.body.id, reason: 'super reason' }) 152 await servers[1].abuses.report({ accountId: account.id, reason: 'super reason' })
173 153
174 await waitJobs(servers) 154 await waitJobs(servers)
175 await checkNewAccountAbuseForModerators(baseParams, username, 'presence') 155 await checkNewAccountAbuseForModerators(baseParams, username, 'presence')
@@ -189,17 +169,16 @@ describe('Test moderation notifications', function () {
189 } 169 }
190 170
191 const name = 'abuse ' + buildUUID() 171 const name = 'abuse ' + buildUUID()
192 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name }) 172 const video = await servers[0].videos.upload({ token: userAccessToken, attributes: { name } })
193 const video = resVideo.body.video
194 173
195 const res = await reportAbuse({ url: servers[0].url, token: userAccessToken, videoId: video.id, reason: 'super reason' }) 174 const body = await servers[0].abuses.report({ token: userAccessToken, videoId: video.id, reason: 'super reason' })
196 abuseId = res.body.abuse.id 175 abuseId = body.abuse.id
197 }) 176 })
198 177
199 it('Should send a notification to reporter if the abuse has been accepted', async function () { 178 it('Should send a notification to reporter if the abuse has been accepted', async function () {
200 this.timeout(10000) 179 this.timeout(10000)
201 180
202 await updateAbuse(servers[0].url, servers[0].accessToken, abuseId, { state: AbuseState.ACCEPTED }) 181 await servers[0].abuses.update({ abuseId, body: { state: AbuseState.ACCEPTED } })
203 await waitJobs(servers) 182 await waitJobs(servers)
204 183
205 await checkAbuseStateChange(baseParams, abuseId, AbuseState.ACCEPTED, 'presence') 184 await checkAbuseStateChange(baseParams, abuseId, AbuseState.ACCEPTED, 'presence')
@@ -208,7 +187,7 @@ describe('Test moderation notifications', function () {
208 it('Should send a notification to reporter if the abuse has been rejected', async function () { 187 it('Should send a notification to reporter if the abuse has been rejected', async function () {
209 this.timeout(10000) 188 this.timeout(10000)
210 189
211 await updateAbuse(servers[0].url, servers[0].accessToken, abuseId, { state: AbuseState.REJECTED }) 190 await servers[0].abuses.update({ abuseId, body: { state: AbuseState.REJECTED } })
212 await waitJobs(servers) 191 await waitJobs(servers)
213 192
214 await checkAbuseStateChange(baseParams, abuseId, AbuseState.REJECTED, 'presence') 193 await checkAbuseStateChange(baseParams, abuseId, AbuseState.REJECTED, 'presence')
@@ -237,17 +216,16 @@ describe('Test moderation notifications', function () {
237 } 216 }
238 217
239 const name = 'abuse ' + buildUUID() 218 const name = 'abuse ' + buildUUID()
240 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name }) 219 const video = await servers[0].videos.upload({ token: userAccessToken, attributes: { name } })
241 const video = resVideo.body.video
242 220
243 { 221 {
244 const res = await reportAbuse({ url: servers[0].url, token: userAccessToken, videoId: video.id, reason: 'super reason' }) 222 const body = await servers[0].abuses.report({ token: userAccessToken, videoId: video.id, reason: 'super reason' })
245 abuseId = res.body.abuse.id 223 abuseId = body.abuse.id
246 } 224 }
247 225
248 { 226 {
249 const res = await reportAbuse({ url: servers[0].url, token: userAccessToken, videoId: video.id, reason: 'super reason 2' }) 227 const body = await servers[0].abuses.report({ token: userAccessToken, videoId: video.id, reason: 'super reason 2' })
250 abuseId2 = res.body.abuse.id 228 abuseId2 = body.abuse.id
251 } 229 }
252 }) 230 })
253 231
@@ -255,7 +233,7 @@ describe('Test moderation notifications', function () {
255 this.timeout(10000) 233 this.timeout(10000)
256 234
257 const message = 'my super message to users' 235 const message = 'my super message to users'
258 await addAbuseMessage(servers[0].url, servers[0].accessToken, abuseId, message) 236 await servers[0].abuses.addMessage({ abuseId, message })
259 await waitJobs(servers) 237 await waitJobs(servers)
260 238
261 await checkNewAbuseMessage(baseParamsUser, abuseId, message, 'user_1@example.com', 'presence') 239 await checkNewAbuseMessage(baseParamsUser, abuseId, message, 'user_1@example.com', 'presence')
@@ -265,7 +243,7 @@ describe('Test moderation notifications', function () {
265 this.timeout(10000) 243 this.timeout(10000)
266 244
267 const message = 'my super message that should not be sent to the admin' 245 const message = 'my super message that should not be sent to the admin'
268 await addAbuseMessage(servers[0].url, servers[0].accessToken, abuseId, message) 246 await servers[0].abuses.addMessage({ abuseId, message })
269 await waitJobs(servers) 247 await waitJobs(servers)
270 248
271 await checkNewAbuseMessage(baseParamsAdmin, abuseId, message, 'admin' + servers[0].internalServerNumber + '@example.com', 'absence') 249 await checkNewAbuseMessage(baseParamsAdmin, abuseId, message, 'admin' + servers[0].internalServerNumber + '@example.com', 'absence')
@@ -275,7 +253,7 @@ describe('Test moderation notifications', function () {
275 this.timeout(10000) 253 this.timeout(10000)
276 254
277 const message = 'my super message to moderators' 255 const message = 'my super message to moderators'
278 await addAbuseMessage(servers[0].url, userAccessToken, abuseId2, message) 256 await servers[0].abuses.addMessage({ token: userAccessToken, abuseId: abuseId2, message })
279 await waitJobs(servers) 257 await waitJobs(servers)
280 258
281 await checkNewAbuseMessage(baseParamsAdmin, abuseId2, message, 'admin' + servers[0].internalServerNumber + '@example.com', 'presence') 259 await checkNewAbuseMessage(baseParamsAdmin, abuseId2, message, 'admin' + servers[0].internalServerNumber + '@example.com', 'presence')
@@ -285,7 +263,7 @@ describe('Test moderation notifications', function () {
285 this.timeout(10000) 263 this.timeout(10000)
286 264
287 const message = 'my super message that should not be sent to reporter' 265 const message = 'my super message that should not be sent to reporter'
288 await addAbuseMessage(servers[0].url, userAccessToken, abuseId2, message) 266 await servers[0].abuses.addMessage({ token: userAccessToken, abuseId: abuseId2, message })
289 await waitJobs(servers) 267 await waitJobs(servers)
290 268
291 await checkNewAbuseMessage(baseParamsUser, abuseId2, message, 'user_1@example.com', 'absence') 269 await checkNewAbuseMessage(baseParamsUser, abuseId2, message, 'user_1@example.com', 'absence')
@@ -308,10 +286,9 @@ describe('Test moderation notifications', function () {
308 this.timeout(10000) 286 this.timeout(10000)
309 287
310 const name = 'video for abuse ' + buildUUID() 288 const name = 'video for abuse ' + buildUUID()
311 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name }) 289 const { uuid } = await servers[0].videos.upload({ token: userAccessToken, attributes: { name } })
312 const uuid = resVideo.body.video.uuid
313 290
314 await addVideoToBlacklist(servers[0].url, servers[0].accessToken, uuid) 291 await servers[0].blacklist.add({ videoId: uuid })
315 292
316 await waitJobs(servers) 293 await waitJobs(servers)
317 await checkNewBlacklistOnMyVideo(baseParams, uuid, name, 'blacklist') 294 await checkNewBlacklistOnMyVideo(baseParams, uuid, name, 'blacklist')
@@ -321,13 +298,12 @@ describe('Test moderation notifications', function () {
321 this.timeout(10000) 298 this.timeout(10000)
322 299
323 const name = 'video for abuse ' + buildUUID() 300 const name = 'video for abuse ' + buildUUID()
324 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name }) 301 const { uuid } = await servers[0].videos.upload({ token: userAccessToken, attributes: { name } })
325 const uuid = resVideo.body.video.uuid
326 302
327 await addVideoToBlacklist(servers[0].url, servers[0].accessToken, uuid) 303 await servers[0].blacklist.add({ videoId: uuid })
328 304
329 await waitJobs(servers) 305 await waitJobs(servers)
330 await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, uuid) 306 await servers[0].blacklist.remove({ videoId: uuid })
331 await waitJobs(servers) 307 await waitJobs(servers)
332 308
333 await wait(500) 309 await wait(500)
@@ -350,14 +326,14 @@ describe('Test moderation notifications', function () {
350 it('Should send a notification only to moderators when a user registers on the instance', async function () { 326 it('Should send a notification only to moderators when a user registers on the instance', async function () {
351 this.timeout(10000) 327 this.timeout(10000)
352 328
353 await registerUser(servers[0].url, 'user_45', 'password') 329 await servers[0].users.register({ username: 'user_45' })
354 330
355 await waitJobs(servers) 331 await waitJobs(servers)
356 332
357 await checkUserRegistered(baseParams, 'user_45', 'presence') 333 await checkUserRegistered(baseParams, 'user_45', 'presence')
358 334
359 const userOverride = { socketNotifications: userNotifications, token: userAccessToken, check: { web: true, mail: false } } 335 const userOverride = { socketNotifications: userNotifications, token: userAccessToken, check: { web: true, mail: false } }
360 await checkUserRegistered(immutableAssign(baseParams, userOverride), 'user_45', 'absence') 336 await checkUserRegistered({ ...baseParams, ...userOverride }, 'user_45', 'absence')
361 }) 337 })
362 }) 338 })
363 339
@@ -392,20 +368,20 @@ describe('Test moderation notifications', function () {
392 it('Should send a notification only to admin when there is a new instance follower', async function () { 368 it('Should send a notification only to admin when there is a new instance follower', async function () {
393 this.timeout(20000) 369 this.timeout(20000)
394 370
395 await follow(servers[2].url, [ servers[0].url ], servers[2].accessToken) 371 await servers[2].follows.follow({ hosts: [ servers[0].url ] })
396 372
397 await waitJobs(servers) 373 await waitJobs(servers)
398 374
399 await checkNewInstanceFollower(baseParams, 'localhost:' + servers[2].port, 'presence') 375 await checkNewInstanceFollower(baseParams, 'localhost:' + servers[2].port, 'presence')
400 376
401 const userOverride = { socketNotifications: userNotifications, token: userAccessToken, check: { web: true, mail: false } } 377 const userOverride = { socketNotifications: userNotifications, token: userAccessToken, check: { web: true, mail: false } }
402 await checkNewInstanceFollower(immutableAssign(baseParams, userOverride), 'localhost:' + servers[2].port, 'absence') 378 await checkNewInstanceFollower({ ...baseParams, ...userOverride }, 'localhost:' + servers[2].port, 'absence')
403 }) 379 })
404 380
405 it('Should send a notification on auto follow back', async function () { 381 it('Should send a notification on auto follow back', async function () {
406 this.timeout(40000) 382 this.timeout(40000)
407 383
408 await unfollow(servers[2].url, servers[2].accessToken, servers[0]) 384 await servers[2].follows.unfollow({ target: servers[0] })
409 await waitJobs(servers) 385 await waitJobs(servers)
410 386
411 const config = { 387 const config = {
@@ -415,9 +391,9 @@ describe('Test moderation notifications', function () {
415 } 391 }
416 } 392 }
417 } 393 }
418 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, config) 394 await servers[0].config.updateCustomSubConfig({ newConfig: config })
419 395
420 await follow(servers[2].url, [ servers[0].url ], servers[2].accessToken) 396 await servers[2].follows.follow({ hosts: [ servers[0].url ] })
421 397
422 await waitJobs(servers) 398 await waitJobs(servers)
423 399
@@ -426,19 +402,19 @@ describe('Test moderation notifications', function () {
426 await checkAutoInstanceFollowing(baseParams, followerHost, followingHost, 'presence') 402 await checkAutoInstanceFollowing(baseParams, followerHost, followingHost, 'presence')
427 403
428 const userOverride = { socketNotifications: userNotifications, token: userAccessToken, check: { web: true, mail: false } } 404 const userOverride = { socketNotifications: userNotifications, token: userAccessToken, check: { web: true, mail: false } }
429 await checkAutoInstanceFollowing(immutableAssign(baseParams, userOverride), followerHost, followingHost, 'absence') 405 await checkAutoInstanceFollowing({ ...baseParams, ...userOverride }, followerHost, followingHost, 'absence')
430 406
431 config.followings.instance.autoFollowBack.enabled = false 407 config.followings.instance.autoFollowBack.enabled = false
432 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, config) 408 await servers[0].config.updateCustomSubConfig({ newConfig: config })
433 await unfollow(servers[0].url, servers[0].accessToken, servers[2]) 409 await servers[0].follows.unfollow({ target: servers[2] })
434 await unfollow(servers[2].url, servers[2].accessToken, servers[0]) 410 await servers[2].follows.unfollow({ target: servers[0] })
435 }) 411 })
436 412
437 it('Should send a notification on auto instances index follow', async function () { 413 it('Should send a notification on auto instances index follow', async function () {
438 this.timeout(30000) 414 this.timeout(30000)
439 await unfollow(servers[0].url, servers[0].accessToken, servers[1]) 415 await servers[0].follows.unfollow({ target: servers[1] })
440 416
441 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, config) 417 await servers[0].config.updateCustomSubConfig({ newConfig: config })
442 418
443 await wait(5000) 419 await wait(5000)
444 await waitJobs(servers) 420 await waitJobs(servers)
@@ -448,8 +424,8 @@ describe('Test moderation notifications', function () {
448 await checkAutoInstanceFollowing(baseParams, followerHost, followingHost, 'presence') 424 await checkAutoInstanceFollowing(baseParams, followerHost, followingHost, 'presence')
449 425
450 config.followings.instance.autoFollowIndex.enabled = false 426 config.followings.instance.autoFollowIndex.enabled = false
451 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, config) 427 await servers[0].config.updateCustomSubConfig({ newConfig: config })
452 await unfollow(servers[0].url, servers[0].accessToken, servers[1]) 428 await servers[0].follows.unfollow({ target: servers[1] })
453 }) 429 })
454 }) 430 })
455 431
@@ -484,9 +460,11 @@ describe('Test moderation notifications', function () {
484 token: userAccessToken 460 token: userAccessToken
485 } 461 }
486 462
487 const resCustomConfig = await getCustomConfig(servers[0].url, servers[0].accessToken) 463 currentCustomConfig = await servers[0].config.getCustomConfig()
488 currentCustomConfig = resCustomConfig.body 464
489 const autoBlacklistTestsCustomConfig = immutableAssign(currentCustomConfig, { 465 const autoBlacklistTestsCustomConfig = {
466 ...currentCustomConfig,
467
490 autoBlacklist: { 468 autoBlacklist: {
491 videos: { 469 videos: {
492 ofUsers: { 470 ofUsers: {
@@ -494,13 +472,14 @@ describe('Test moderation notifications', function () {
494 } 472 }
495 } 473 }
496 } 474 }
497 }) 475 }
476
498 // enable transcoding otherwise own publish notification after transcoding not expected 477 // enable transcoding otherwise own publish notification after transcoding not expected
499 autoBlacklistTestsCustomConfig.transcoding.enabled = true 478 autoBlacklistTestsCustomConfig.transcoding.enabled = true
500 await updateCustomConfig(servers[0].url, servers[0].accessToken, autoBlacklistTestsCustomConfig) 479 await servers[0].config.updateCustomConfig({ newCustomConfig: autoBlacklistTestsCustomConfig })
501 480
502 await addUserSubscription(servers[0].url, servers[0].accessToken, 'user_1_channel@localhost:' + servers[0].port) 481 await servers[0].subscriptions.add({ targetUri: 'user_1_channel@localhost:' + servers[0].port })
503 await addUserSubscription(servers[1].url, servers[1].accessToken, 'user_1_channel@localhost:' + servers[0].port) 482 await servers[1].subscriptions.add({ targetUri: 'user_1_channel@localhost:' + servers[0].port })
504 483
505 }) 484 })
506 485
@@ -508,8 +487,8 @@ describe('Test moderation notifications', function () {
508 this.timeout(40000) 487 this.timeout(40000)
509 488
510 videoName = 'video with auto-blacklist ' + buildUUID() 489 videoName = 'video with auto-blacklist ' + buildUUID()
511 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: videoName }) 490 const { uuid } = await servers[0].videos.upload({ token: userAccessToken, attributes: { name: videoName } })
512 videoUUID = resVideo.body.video.uuid 491 videoUUID = uuid
513 492
514 await waitJobs(servers) 493 await waitJobs(servers)
515 await checkVideoAutoBlacklistForModerators(adminBaseParamsServer1, videoUUID, videoName, 'presence') 494 await checkVideoAutoBlacklistForModerators(adminBaseParamsServer1, videoUUID, videoName, 'presence')
@@ -530,7 +509,7 @@ describe('Test moderation notifications', function () {
530 it('Should send video published and unblacklist after video unblacklisted', async function () { 509 it('Should send video published and unblacklist after video unblacklisted', async function () {
531 this.timeout(40000) 510 this.timeout(40000)
532 511
533 await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, videoUUID) 512 await servers[0].blacklist.remove({ videoId: videoUUID })
534 513
535 await waitJobs(servers) 514 await waitJobs(servers)
536 515
@@ -555,19 +534,18 @@ describe('Test moderation notifications', function () {
555 534
556 const name = 'video with auto-blacklist and future schedule ' + buildUUID() 535 const name = 'video with auto-blacklist and future schedule ' + buildUUID()
557 536
558 const data = { 537 const attributes = {
559 name, 538 name,
560 privacy: VideoPrivacy.PRIVATE, 539 privacy: VideoPrivacy.PRIVATE,
561 scheduleUpdate: { 540 scheduleUpdate: {
562 updateAt: updateAt.toISOString(), 541 updateAt: updateAt.toISOString(),
563 privacy: VideoPrivacy.PUBLIC 542 privacy: VideoPrivacy.PUBLIC as VideoPrivacy.PUBLIC
564 } 543 }
565 } 544 }
566 545
567 const resVideo = await uploadVideo(servers[0].url, userAccessToken, data) 546 const { uuid } = await servers[0].videos.upload({ token: userAccessToken, attributes })
568 const uuid = resVideo.body.video.uuid
569 547
570 await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, uuid) 548 await servers[0].blacklist.remove({ videoId: uuid })
571 549
572 await waitJobs(servers) 550 await waitJobs(servers)
573 await checkNewBlacklistOnMyVideo(userBaseParams, uuid, name, 'unblacklist') 551 await checkNewBlacklistOnMyVideo(userBaseParams, uuid, name, 'unblacklist')
@@ -588,17 +566,16 @@ describe('Test moderation notifications', function () {
588 566
589 const name = 'video with schedule done and still auto-blacklisted ' + buildUUID() 567 const name = 'video with schedule done and still auto-blacklisted ' + buildUUID()
590 568
591 const data = { 569 const attributes = {
592 name, 570 name,
593 privacy: VideoPrivacy.PRIVATE, 571 privacy: VideoPrivacy.PRIVATE,
594 scheduleUpdate: { 572 scheduleUpdate: {
595 updateAt: updateAt.toISOString(), 573 updateAt: updateAt.toISOString(),
596 privacy: VideoPrivacy.PUBLIC 574 privacy: VideoPrivacy.PUBLIC as VideoPrivacy.PUBLIC
597 } 575 }
598 } 576 }
599 577
600 const resVideo = await uploadVideo(servers[0].url, userAccessToken, data) 578 const { uuid } = await servers[0].videos.upload({ token: userAccessToken, attributes })
601 const uuid = resVideo.body.video.uuid
602 579
603 await wait(6000) 580 await wait(6000)
604 await checkVideoIsPublished(userBaseParams, name, uuid, 'absence') 581 await checkVideoIsPublished(userBaseParams, name, uuid, 'absence')
@@ -612,18 +589,17 @@ describe('Test moderation notifications', function () {
612 const name = 'video without auto-blacklist ' + buildUUID() 589 const name = 'video without auto-blacklist ' + buildUUID()
613 590
614 // admin with blacklist right will not be auto-blacklisted 591 // admin with blacklist right will not be auto-blacklisted
615 const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name }) 592 const { uuid } = await servers[0].videos.upload({ attributes: { name } })
616 const uuid = resVideo.body.video.uuid
617 593
618 await waitJobs(servers) 594 await waitJobs(servers)
619 await checkVideoAutoBlacklistForModerators(adminBaseParamsServer1, uuid, name, 'absence') 595 await checkVideoAutoBlacklistForModerators(adminBaseParamsServer1, uuid, name, 'absence')
620 }) 596 })
621 597
622 after(async () => { 598 after(async () => {
623 await updateCustomConfig(servers[0].url, servers[0].accessToken, currentCustomConfig) 599 await servers[0].config.updateCustomConfig({ newCustomConfig: currentCustomConfig })
624 600
625 await removeUserSubscription(servers[0].url, servers[0].accessToken, 'user_1_channel@localhost:' + servers[0].port) 601 await servers[0].subscriptions.remove({ uri: 'user_1_channel@localhost:' + servers[0].port })
626 await removeUserSubscription(servers[1].url, servers[1].accessToken, 'user_1_channel@localhost:' + servers[0].port) 602 await servers[1].subscriptions.remove({ uri: 'user_1_channel@localhost:' + servers[0].port })
627 }) 603 })
628 }) 604 })
629 605
diff --git a/server/tests/api/notifications/notifications-api.ts b/server/tests/api/notifications/notifications-api.ts
index b81995449..fa4b53db6 100644
--- a/server/tests/api/notifications/notifications-api.ts
+++ b/server/tests/api/notifications/notifications-api.ts
@@ -2,28 +2,24 @@
2 2
3import 'mocha' 3import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5import { addUserSubscription } from '@shared/extra-utils/users/user-subscriptions'
6import { cleanupTests, getMyUserInformation, immutableAssign, uploadRandomVideo, waitJobs } from '../../../../shared/extra-utils'
7import { ServerInfo } from '../../../../shared/extra-utils/index'
8import { MockSmtpServer } from '../../../../shared/extra-utils/miscs/email'
9import { 5import {
10 CheckerBaseParams, 6 CheckerBaseParams,
11 checkNewVideoFromSubscription, 7 checkNewVideoFromSubscription,
8 cleanupTests,
12 getAllNotificationsSettings, 9 getAllNotificationsSettings,
13 getUserNotifications, 10 MockSmtpServer,
14 markAsReadAllNotifications, 11 PeerTubeServer,
15 markAsReadNotifications,
16 prepareNotificationsTest, 12 prepareNotificationsTest,
17 updateMyNotificationSettings 13 waitJobs
18} from '../../../../shared/extra-utils/users/user-notifications' 14} from '@shared/extra-utils'
19import { User, UserNotification, UserNotificationSettingValue } from '../../../../shared/models/users' 15import { UserNotification, UserNotificationSettingValue } from '@shared/models'
20 16
21const expect = chai.expect 17const expect = chai.expect
22 18
23describe('Test notifications API', function () { 19describe('Test notifications API', function () {
24 let server: ServerInfo 20 let server: PeerTubeServer
25 let userNotifications: UserNotification[] = [] 21 let userNotifications: UserNotification[] = []
26 let userAccessToken: string 22 let userToken: string
27 let emails: object[] = [] 23 let emails: object[] = []
28 24
29 before(async function () { 25 before(async function () {
@@ -31,14 +27,14 @@ describe('Test notifications API', function () {
31 27
32 const res = await prepareNotificationsTest(1) 28 const res = await prepareNotificationsTest(1)
33 emails = res.emails 29 emails = res.emails
34 userAccessToken = res.userAccessToken 30 userToken = res.userAccessToken
35 userNotifications = res.userNotifications 31 userNotifications = res.userNotifications
36 server = res.servers[0] 32 server = res.servers[0]
37 33
38 await addUserSubscription(server.url, userAccessToken, 'root_channel@localhost:' + server.port) 34 await server.subscriptions.add({ token: userToken, targetUri: 'root_channel@localhost:' + server.port })
39 35
40 for (let i = 0; i < 10; i++) { 36 for (let i = 0; i < 10; i++) {
41 await uploadRandomVideo(server, false) 37 await server.videos.randomUpload({ wait: false })
42 } 38 }
43 39
44 await waitJobs([ server ]) 40 await waitJobs([ server ])
@@ -47,49 +43,46 @@ describe('Test notifications API', function () {
47 describe('Mark as read', function () { 43 describe('Mark as read', function () {
48 44
49 it('Should mark as read some notifications', async function () { 45 it('Should mark as read some notifications', async function () {
50 const res = await getUserNotifications(server.url, userAccessToken, 2, 3) 46 const { data } = await server.notifications.list({ token: userToken, start: 2, count: 3 })
51 const ids = res.body.data.map(n => n.id) 47 const ids = data.map(n => n.id)
52 48
53 await markAsReadNotifications(server.url, userAccessToken, ids) 49 await server.notifications.markAsRead({ token: userToken, ids })
54 }) 50 })
55 51
56 it('Should have the notifications marked as read', async function () { 52 it('Should have the notifications marked as read', async function () {
57 const res = await getUserNotifications(server.url, userAccessToken, 0, 10) 53 const { data } = await server.notifications.list({ token: userToken, start: 0, count: 10 })
58 54
59 const notifications = res.body.data as UserNotification[] 55 expect(data[0].read).to.be.false
60 expect(notifications[0].read).to.be.false 56 expect(data[1].read).to.be.false
61 expect(notifications[1].read).to.be.false 57 expect(data[2].read).to.be.true
62 expect(notifications[2].read).to.be.true 58 expect(data[3].read).to.be.true
63 expect(notifications[3].read).to.be.true 59 expect(data[4].read).to.be.true
64 expect(notifications[4].read).to.be.true 60 expect(data[5].read).to.be.false
65 expect(notifications[5].read).to.be.false
66 }) 61 })
67 62
68 it('Should only list read notifications', async function () { 63 it('Should only list read notifications', async function () {
69 const res = await getUserNotifications(server.url, userAccessToken, 0, 10, false) 64 const { data } = await server.notifications.list({ token: userToken, start: 0, count: 10, unread: false })
70 65
71 const notifications = res.body.data as UserNotification[] 66 for (const notification of data) {
72 for (const notification of notifications) {
73 expect(notification.read).to.be.true 67 expect(notification.read).to.be.true
74 } 68 }
75 }) 69 })
76 70
77 it('Should only list unread notifications', async function () { 71 it('Should only list unread notifications', async function () {
78 const res = await getUserNotifications(server.url, userAccessToken, 0, 10, true) 72 const { data } = await server.notifications.list({ token: userToken, start: 0, count: 10, unread: true })
79 73
80 const notifications = res.body.data as UserNotification[] 74 for (const notification of data) {
81 for (const notification of notifications) {
82 expect(notification.read).to.be.false 75 expect(notification.read).to.be.false
83 } 76 }
84 }) 77 })
85 78
86 it('Should mark as read all notifications', async function () { 79 it('Should mark as read all notifications', async function () {
87 await markAsReadAllNotifications(server.url, userAccessToken) 80 await server.notifications.markAsReadAll({ token: userToken })
88 81
89 const res = await getUserNotifications(server.url, userAccessToken, 0, 10, true) 82 const body = await server.notifications.list({ token: userToken, start: 0, count: 10, unread: true })
90 83
91 expect(res.body.total).to.equal(0) 84 expect(body.total).to.equal(0)
92 expect(res.body.data).to.have.lengthOf(0) 85 expect(body.data).to.have.lengthOf(0)
93 }) 86 })
94 }) 87 })
95 88
@@ -101,97 +94,100 @@ describe('Test notifications API', function () {
101 server: server, 94 server: server,
102 emails, 95 emails,
103 socketNotifications: userNotifications, 96 socketNotifications: userNotifications,
104 token: userAccessToken 97 token: userToken
105 } 98 }
106 }) 99 })
107 100
108 it('Should not have notifications', async function () { 101 it('Should not have notifications', async function () {
109 this.timeout(20000) 102 this.timeout(20000)
110 103
111 await updateMyNotificationSettings(server.url, userAccessToken, immutableAssign(getAllNotificationsSettings(), { 104 await server.notifications.updateMySettings({
112 newVideoFromSubscription: UserNotificationSettingValue.NONE 105 token: userToken,
113 })) 106 settings: { ...getAllNotificationsSettings(), newVideoFromSubscription: UserNotificationSettingValue.NONE }
107 })
114 108
115 { 109 {
116 const res = await getMyUserInformation(server.url, userAccessToken) 110 const info = await server.users.getMyInfo({ token: userToken })
117 const info = res.body as User
118 expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.NONE) 111 expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.NONE)
119 } 112 }
120 113
121 const { name, uuid } = await uploadRandomVideo(server) 114 const { name, uuid } = await server.videos.randomUpload()
122 115
123 const check = { web: true, mail: true } 116 const check = { web: true, mail: true }
124 await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'absence') 117 await checkNewVideoFromSubscription({ ...baseParams, check }, name, uuid, 'absence')
125 }) 118 })
126 119
127 it('Should only have web notifications', async function () { 120 it('Should only have web notifications', async function () {
128 this.timeout(20000) 121 this.timeout(20000)
129 122
130 await updateMyNotificationSettings(server.url, userAccessToken, immutableAssign(getAllNotificationsSettings(), { 123 await server.notifications.updateMySettings({
131 newVideoFromSubscription: UserNotificationSettingValue.WEB 124 token: userToken,
132 })) 125 settings: { ...getAllNotificationsSettings(), newVideoFromSubscription: UserNotificationSettingValue.WEB }
126 })
133 127
134 { 128 {
135 const res = await getMyUserInformation(server.url, userAccessToken) 129 const info = await server.users.getMyInfo({ token: userToken })
136 const info = res.body as User
137 expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.WEB) 130 expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.WEB)
138 } 131 }
139 132
140 const { name, uuid } = await uploadRandomVideo(server) 133 const { name, uuid } = await server.videos.randomUpload()
141 134
142 { 135 {
143 const check = { mail: true, web: false } 136 const check = { mail: true, web: false }
144 await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'absence') 137 await checkNewVideoFromSubscription({ ...baseParams, check }, name, uuid, 'absence')
145 } 138 }
146 139
147 { 140 {
148 const check = { mail: false, web: true } 141 const check = { mail: false, web: true }
149 await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'presence') 142 await checkNewVideoFromSubscription({ ...baseParams, check }, name, uuid, 'presence')
150 } 143 }
151 }) 144 })
152 145
153 it('Should only have mail notifications', async function () { 146 it('Should only have mail notifications', async function () {
154 this.timeout(20000) 147 this.timeout(20000)
155 148
156 await updateMyNotificationSettings(server.url, userAccessToken, immutableAssign(getAllNotificationsSettings(), { 149 await server.notifications.updateMySettings({
157 newVideoFromSubscription: UserNotificationSettingValue.EMAIL 150 token: userToken,
158 })) 151 settings: { ...getAllNotificationsSettings(), newVideoFromSubscription: UserNotificationSettingValue.EMAIL }
152 })
159 153
160 { 154 {
161 const res = await getMyUserInformation(server.url, userAccessToken) 155 const info = await server.users.getMyInfo({ token: userToken })
162 const info = res.body as User
163 expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.EMAIL) 156 expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.EMAIL)
164 } 157 }
165 158
166 const { name, uuid } = await uploadRandomVideo(server) 159 const { name, uuid } = await server.videos.randomUpload()
167 160
168 { 161 {
169 const check = { mail: false, web: true } 162 const check = { mail: false, web: true }
170 await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'absence') 163 await checkNewVideoFromSubscription({ ...baseParams, check }, name, uuid, 'absence')
171 } 164 }
172 165
173 { 166 {
174 const check = { mail: true, web: false } 167 const check = { mail: true, web: false }
175 await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'presence') 168 await checkNewVideoFromSubscription({ ...baseParams, check }, name, uuid, 'presence')
176 } 169 }
177 }) 170 })
178 171
179 it('Should have email and web notifications', async function () { 172 it('Should have email and web notifications', async function () {
180 this.timeout(20000) 173 this.timeout(20000)
181 174
182 await updateMyNotificationSettings(server.url, userAccessToken, immutableAssign(getAllNotificationsSettings(), { 175 await server.notifications.updateMySettings({
183 newVideoFromSubscription: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL 176 token: userToken,
184 })) 177 settings: {
178 ...getAllNotificationsSettings(),
179 newVideoFromSubscription: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL
180 }
181 })
185 182
186 { 183 {
187 const res = await getMyUserInformation(server.url, userAccessToken) 184 const info = await server.users.getMyInfo({ token: userToken })
188 const info = res.body as User
189 expect(info.notificationSettings.newVideoFromSubscription).to.equal( 185 expect(info.notificationSettings.newVideoFromSubscription).to.equal(
190 UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL 186 UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL
191 ) 187 )
192 } 188 }
193 189
194 const { name, uuid } = await uploadRandomVideo(server) 190 const { name, uuid } = await server.videos.randomUpload()
195 191
196 await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence') 192 await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence')
197 }) 193 })
diff --git a/server/tests/api/notifications/user-notifications.ts b/server/tests/api/notifications/user-notifications.ts
index e981c1718..ca592d466 100644
--- a/server/tests/api/notifications/user-notifications.ts
+++ b/server/tests/api/notifications/user-notifications.ts
@@ -4,34 +4,26 @@ import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5import { buildUUID } from '@server/helpers/uuid' 5import { buildUUID } from '@server/helpers/uuid'
6import { 6import {
7 cleanupTests,
8 updateMyUser,
9 updateVideo,
10 updateVideoChannel,
11 uploadRandomVideoOnServers,
12 wait
13} from '../../../../shared/extra-utils'
14import { ServerInfo } from '../../../../shared/extra-utils/index'
15import { MockSmtpServer } from '../../../../shared/extra-utils/miscs/email'
16import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
17import {
18 CheckerBaseParams, 7 CheckerBaseParams,
19 checkMyVideoImportIsFinished, 8 checkMyVideoImportIsFinished,
20 checkNewActorFollow, 9 checkNewActorFollow,
21 checkNewVideoFromSubscription, 10 checkNewVideoFromSubscription,
22 checkVideoIsPublished, 11 checkVideoIsPublished,
23 getLastNotification, 12 cleanupTests,
24 prepareNotificationsTest 13 FIXTURE_URLS,
25} from '../../../../shared/extra-utils/users/user-notifications' 14 MockSmtpServer,
26import { addUserSubscription, removeUserSubscription } from '../../../../shared/extra-utils/users/user-subscriptions' 15 PeerTubeServer,
27import { getBadVideoUrl, getGoodVideoUrl, importVideo } from '../../../../shared/extra-utils/videos/video-imports' 16 prepareNotificationsTest,
28import { UserNotification, UserNotificationType } from '../../../../shared/models/users' 17 uploadRandomVideoOnServers,
29import { VideoPrivacy } from '../../../../shared/models/videos' 18 wait,
19 waitJobs
20} from '@shared/extra-utils'
21import { UserNotification, UserNotificationType, VideoPrivacy } from '@shared/models'
30 22
31const expect = chai.expect 23const expect = chai.expect
32 24
33describe('Test user notifications', function () { 25describe('Test user notifications', function () {
34 let servers: ServerInfo[] = [] 26 let servers: PeerTubeServer[] = []
35 let userAccessToken: string 27 let userAccessToken: string
36 let userNotifications: UserNotification[] = [] 28 let userNotifications: UserNotification[] = []
37 let adminNotifications: UserNotification[] = [] 29 let adminNotifications: UserNotification[] = []
@@ -69,7 +61,7 @@ describe('Test user notifications', function () {
69 61
70 await uploadRandomVideoOnServers(servers, 1) 62 await uploadRandomVideoOnServers(servers, 1)
71 63
72 const notification = await getLastNotification(servers[0].url, userAccessToken) 64 const notification = await servers[0].notifications.getLastest({ token: userAccessToken })
73 expect(notification).to.be.undefined 65 expect(notification).to.be.undefined
74 66
75 expect(emails).to.have.lengthOf(0) 67 expect(emails).to.have.lengthOf(0)
@@ -79,7 +71,7 @@ describe('Test user notifications', function () {
79 it('Should send a new video notification if the user follows the local video publisher', async function () { 71 it('Should send a new video notification if the user follows the local video publisher', async function () {
80 this.timeout(15000) 72 this.timeout(15000)
81 73
82 await addUserSubscription(servers[0].url, userAccessToken, 'root_channel@localhost:' + servers[0].port) 74 await servers[0].subscriptions.add({ token: userAccessToken, targetUri: 'root_channel@localhost:' + servers[0].port })
83 await waitJobs(servers) 75 await waitJobs(servers)
84 76
85 const { name, uuid } = await uploadRandomVideoOnServers(servers, 1) 77 const { name, uuid } = await uploadRandomVideoOnServers(servers, 1)
@@ -89,7 +81,7 @@ describe('Test user notifications', function () {
89 it('Should send a new video notification from a remote account', async function () { 81 it('Should send a new video notification from a remote account', async function () {
90 this.timeout(150000) // Server 2 has transcoding enabled 82 this.timeout(150000) // Server 2 has transcoding enabled
91 83
92 await addUserSubscription(servers[0].url, userAccessToken, 'root_channel@localhost:' + servers[1].port) 84 await servers[0].subscriptions.add({ token: userAccessToken, targetUri: 'root_channel@localhost:' + servers[1].port })
93 await waitJobs(servers) 85 await waitJobs(servers)
94 86
95 const { name, uuid } = await uploadRandomVideoOnServers(servers, 2) 87 const { name, uuid } = await uploadRandomVideoOnServers(servers, 2)
@@ -106,7 +98,7 @@ describe('Test user notifications', function () {
106 privacy: VideoPrivacy.PRIVATE, 98 privacy: VideoPrivacy.PRIVATE,
107 scheduleUpdate: { 99 scheduleUpdate: {
108 updateAt: updateAt.toISOString(), 100 updateAt: updateAt.toISOString(),
109 privacy: VideoPrivacy.PUBLIC 101 privacy: VideoPrivacy.PUBLIC as VideoPrivacy.PUBLIC
110 } 102 }
111 } 103 }
112 const { name, uuid } = await uploadRandomVideoOnServers(servers, 1, data) 104 const { name, uuid } = await uploadRandomVideoOnServers(servers, 1, data)
@@ -125,7 +117,7 @@ describe('Test user notifications', function () {
125 privacy: VideoPrivacy.PRIVATE, 117 privacy: VideoPrivacy.PRIVATE,
126 scheduleUpdate: { 118 scheduleUpdate: {
127 updateAt: updateAt.toISOString(), 119 updateAt: updateAt.toISOString(),
128 privacy: VideoPrivacy.PUBLIC 120 privacy: VideoPrivacy.PUBLIC as VideoPrivacy.PUBLIC
129 } 121 }
130 } 122 }
131 const { name, uuid } = await uploadRandomVideoOnServers(servers, 2, data) 123 const { name, uuid } = await uploadRandomVideoOnServers(servers, 2, data)
@@ -144,7 +136,7 @@ describe('Test user notifications', function () {
144 privacy: VideoPrivacy.PRIVATE, 136 privacy: VideoPrivacy.PRIVATE,
145 scheduleUpdate: { 137 scheduleUpdate: {
146 updateAt: updateAt.toISOString(), 138 updateAt: updateAt.toISOString(),
147 privacy: VideoPrivacy.PUBLIC 139 privacy: VideoPrivacy.PUBLIC as VideoPrivacy.PUBLIC
148 } 140 }
149 } 141 }
150 const { name, uuid } = await uploadRandomVideoOnServers(servers, 1, data) 142 const { name, uuid } = await uploadRandomVideoOnServers(servers, 1, data)
@@ -161,7 +153,7 @@ describe('Test user notifications', function () {
161 153
162 await checkNewVideoFromSubscription(baseParams, name, uuid, 'absence') 154 await checkNewVideoFromSubscription(baseParams, name, uuid, 'absence')
163 155
164 await updateVideo(servers[0].url, servers[0].accessToken, uuid, { privacy: VideoPrivacy.PUBLIC }) 156 await servers[0].videos.update({ id: uuid, attributes: { privacy: VideoPrivacy.PUBLIC } })
165 157
166 await waitJobs(servers) 158 await waitJobs(servers)
167 await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence') 159 await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence')
@@ -175,7 +167,7 @@ describe('Test user notifications', function () {
175 167
176 await checkNewVideoFromSubscription(baseParams, name, uuid, 'absence') 168 await checkNewVideoFromSubscription(baseParams, name, uuid, 'absence')
177 169
178 await updateVideo(servers[1].url, servers[1].accessToken, uuid, { privacy: VideoPrivacy.PUBLIC }) 170 await servers[1].videos.update({ id: uuid, attributes: { privacy: VideoPrivacy.PUBLIC } })
179 171
180 await waitJobs(servers) 172 await waitJobs(servers)
181 await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence') 173 await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence')
@@ -187,7 +179,7 @@ describe('Test user notifications', function () {
187 const data = { privacy: VideoPrivacy.PRIVATE } 179 const data = { privacy: VideoPrivacy.PRIVATE }
188 const { name, uuid } = await uploadRandomVideoOnServers(servers, 1, data) 180 const { name, uuid } = await uploadRandomVideoOnServers(servers, 1, data)
189 181
190 await updateVideo(servers[0].url, servers[0].accessToken, uuid, { privacy: VideoPrivacy.UNLISTED }) 182 await servers[0].videos.update({ id: uuid, attributes: { privacy: VideoPrivacy.UNLISTED } })
191 183
192 await checkNewVideoFromSubscription(baseParams, name, uuid, 'absence') 184 await checkNewVideoFromSubscription(baseParams, name, uuid, 'absence')
193 }) 185 })
@@ -198,7 +190,7 @@ describe('Test user notifications', function () {
198 const data = { privacy: VideoPrivacy.PRIVATE } 190 const data = { privacy: VideoPrivacy.PRIVATE }
199 const { name, uuid } = await uploadRandomVideoOnServers(servers, 2, data) 191 const { name, uuid } = await uploadRandomVideoOnServers(servers, 2, data)
200 192
201 await updateVideo(servers[1].url, servers[1].accessToken, uuid, { privacy: VideoPrivacy.UNLISTED }) 193 await servers[1].videos.update({ id: uuid, attributes: { privacy: VideoPrivacy.UNLISTED } })
202 194
203 await waitJobs(servers) 195 await waitJobs(servers)
204 await checkNewVideoFromSubscription(baseParams, name, uuid, 'absence') 196 await checkNewVideoFromSubscription(baseParams, name, uuid, 'absence')
@@ -213,14 +205,13 @@ describe('Test user notifications', function () {
213 name, 205 name,
214 channelId, 206 channelId,
215 privacy: VideoPrivacy.PUBLIC, 207 privacy: VideoPrivacy.PUBLIC,
216 targetUrl: getGoodVideoUrl() 208 targetUrl: FIXTURE_URLS.goodVideo
217 } 209 }
218 const res = await importVideo(servers[0].url, servers[0].accessToken, attributes) 210 const { video } = await servers[0].imports.importVideo({ attributes })
219 const uuid = res.body.video.uuid
220 211
221 await waitJobs(servers) 212 await waitJobs(servers)
222 213
223 await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence') 214 await checkNewVideoFromSubscription(baseParams, name, video.uuid, 'presence')
224 }) 215 })
225 }) 216 })
226 217
@@ -251,7 +242,7 @@ describe('Test user notifications', function () {
251 await uploadRandomVideoOnServers(servers, 2, { waitTranscoding: false }) 242 await uploadRandomVideoOnServers(servers, 2, { waitTranscoding: false })
252 await waitJobs(servers) 243 await waitJobs(servers)
253 244
254 const notification = await getLastNotification(servers[0].url, userAccessToken) 245 const notification = await servers[0].notifications.getLastest({ token: userAccessToken })
255 if (notification) { 246 if (notification) {
256 expect(notification.type).to.not.equal(UserNotificationType.MY_VIDEO_PUBLISHED) 247 expect(notification.type).to.not.equal(UserNotificationType.MY_VIDEO_PUBLISHED)
257 } 248 }
@@ -284,14 +275,13 @@ describe('Test user notifications', function () {
284 name, 275 name,
285 channelId, 276 channelId,
286 privacy: VideoPrivacy.PUBLIC, 277 privacy: VideoPrivacy.PUBLIC,
287 targetUrl: getGoodVideoUrl(), 278 targetUrl: FIXTURE_URLS.goodVideo,
288 waitTranscoding: true 279 waitTranscoding: true
289 } 280 }
290 const res = await importVideo(servers[1].url, servers[1].accessToken, attributes) 281 const { video } = await servers[1].imports.importVideo({ attributes })
291 const uuid = res.body.video.uuid
292 282
293 await waitJobs(servers) 283 await waitJobs(servers)
294 await checkVideoIsPublished(baseParams, name, uuid, 'presence') 284 await checkVideoIsPublished(baseParams, name, video.uuid, 'presence')
295 }) 285 })
296 286
297 it('Should send a notification when the scheduled update has been proceeded', async function () { 287 it('Should send a notification when the scheduled update has been proceeded', async function () {
@@ -304,7 +294,7 @@ describe('Test user notifications', function () {
304 privacy: VideoPrivacy.PRIVATE, 294 privacy: VideoPrivacy.PRIVATE,
305 scheduleUpdate: { 295 scheduleUpdate: {
306 updateAt: updateAt.toISOString(), 296 updateAt: updateAt.toISOString(),
307 privacy: VideoPrivacy.PUBLIC 297 privacy: VideoPrivacy.PUBLIC as VideoPrivacy.PUBLIC
308 } 298 }
309 } 299 }
310 const { name, uuid } = await uploadRandomVideoOnServers(servers, 2, data) 300 const { name, uuid } = await uploadRandomVideoOnServers(servers, 2, data)
@@ -322,7 +312,7 @@ describe('Test user notifications', function () {
322 privacy: VideoPrivacy.PRIVATE, 312 privacy: VideoPrivacy.PRIVATE,
323 scheduleUpdate: { 313 scheduleUpdate: {
324 updateAt: updateAt.toISOString(), 314 updateAt: updateAt.toISOString(),
325 privacy: VideoPrivacy.PUBLIC 315 privacy: VideoPrivacy.PUBLIC as VideoPrivacy.PUBLIC
326 } 316 }
327 } 317 }
328 const { name, uuid } = await uploadRandomVideoOnServers(servers, 2, data) 318 const { name, uuid } = await uploadRandomVideoOnServers(servers, 2, data)
@@ -353,13 +343,12 @@ describe('Test user notifications', function () {
353 name, 343 name,
354 channelId, 344 channelId,
355 privacy: VideoPrivacy.PRIVATE, 345 privacy: VideoPrivacy.PRIVATE,
356 targetUrl: getBadVideoUrl() 346 targetUrl: FIXTURE_URLS.badVideo
357 } 347 }
358 const res = await importVideo(servers[0].url, servers[0].accessToken, attributes) 348 const { video } = await servers[0].imports.importVideo({ attributes })
359 const uuid = res.body.video.uuid
360 349
361 await waitJobs(servers) 350 await waitJobs(servers)
362 await checkMyVideoImportIsFinished(baseParams, name, uuid, getBadVideoUrl(), false, 'presence') 351 await checkMyVideoImportIsFinished(baseParams, name, video.uuid, FIXTURE_URLS.badVideo, false, 'presence')
363 }) 352 })
364 353
365 it('Should send a notification when the video import succeeded', async function () { 354 it('Should send a notification when the video import succeeded', async function () {
@@ -371,13 +360,12 @@ describe('Test user notifications', function () {
371 name, 360 name,
372 channelId, 361 channelId,
373 privacy: VideoPrivacy.PRIVATE, 362 privacy: VideoPrivacy.PRIVATE,
374 targetUrl: getGoodVideoUrl() 363 targetUrl: FIXTURE_URLS.goodVideo
375 } 364 }
376 const res = await importVideo(servers[0].url, servers[0].accessToken, attributes) 365 const { video } = await servers[0].imports.importVideo({ attributes })
377 const uuid = res.body.video.uuid
378 366
379 await waitJobs(servers) 367 await waitJobs(servers)
380 await checkMyVideoImportIsFinished(baseParams, name, uuid, getGoodVideoUrl(), true, 'presence') 368 await checkMyVideoImportIsFinished(baseParams, name, video.uuid, FIXTURE_URLS.goodVideo, true, 'presence')
381 }) 369 })
382 }) 370 })
383 371
@@ -394,47 +382,42 @@ describe('Test user notifications', function () {
394 token: userAccessToken 382 token: userAccessToken
395 } 383 }
396 384
397 await updateMyUser({ 385 await servers[0].users.updateMe({ displayName: 'super root name' })
398 url: servers[0].url,
399 accessToken: servers[0].accessToken,
400 displayName: 'super root name'
401 })
402 386
403 await updateMyUser({ 387 await servers[0].users.updateMe({
404 url: servers[0].url, 388 token: userAccessToken,
405 accessToken: userAccessToken,
406 displayName: myUserName 389 displayName: myUserName
407 }) 390 })
408 391
409 await updateMyUser({ 392 await servers[1].users.updateMe({ displayName: 'super root 2 name' })
410 url: servers[1].url,
411 accessToken: servers[1].accessToken,
412 displayName: 'super root 2 name'
413 })
414 393
415 await updateVideoChannel(servers[0].url, userAccessToken, 'user_1_channel', { displayName: myChannelName }) 394 await servers[0].channels.update({
395 token: userAccessToken,
396 channelName: 'user_1_channel',
397 attributes: { displayName: myChannelName }
398 })
416 }) 399 })
417 400
418 it('Should notify when a local channel is following one of our channel', async function () { 401 it('Should notify when a local channel is following one of our channel', async function () {
419 this.timeout(50000) 402 this.timeout(50000)
420 403
421 await addUserSubscription(servers[0].url, servers[0].accessToken, 'user_1_channel@localhost:' + servers[0].port) 404 await servers[0].subscriptions.add({ targetUri: 'user_1_channel@localhost:' + servers[0].port })
422 await waitJobs(servers) 405 await waitJobs(servers)
423 406
424 await checkNewActorFollow(baseParams, 'channel', 'root', 'super root name', myChannelName, 'presence') 407 await checkNewActorFollow(baseParams, 'channel', 'root', 'super root name', myChannelName, 'presence')
425 408
426 await removeUserSubscription(servers[0].url, servers[0].accessToken, 'user_1_channel@localhost:' + servers[0].port) 409 await servers[0].subscriptions.remove({ uri: 'user_1_channel@localhost:' + servers[0].port })
427 }) 410 })
428 411
429 it('Should notify when a remote channel is following one of our channel', async function () { 412 it('Should notify when a remote channel is following one of our channel', async function () {
430 this.timeout(50000) 413 this.timeout(50000)
431 414
432 await addUserSubscription(servers[1].url, servers[1].accessToken, 'user_1_channel@localhost:' + servers[0].port) 415 await servers[1].subscriptions.add({ targetUri: 'user_1_channel@localhost:' + servers[0].port })
433 await waitJobs(servers) 416 await waitJobs(servers)
434 417
435 await checkNewActorFollow(baseParams, 'channel', 'root', 'super root 2 name', myChannelName, 'presence') 418 await checkNewActorFollow(baseParams, 'channel', 'root', 'super root 2 name', myChannelName, 'presence')
436 419
437 await removeUserSubscription(servers[1].url, servers[1].accessToken, 'user_1_channel@localhost:' + servers[0].port) 420 await servers[1].subscriptions.remove({ uri: 'user_1_channel@localhost:' + servers[0].port })
438 }) 421 })
439 422
440 // PeerTube does not support accout -> account follows 423 // PeerTube does not support accout -> account follows
diff --git a/server/tests/api/redundancy/manage-redundancy.ts b/server/tests/api/redundancy/manage-redundancy.ts
index 4253124c8..5fd464ded 100644
--- a/server/tests/api/redundancy/manage-redundancy.ts
+++ b/server/tests/api/redundancy/manage-redundancy.ts
@@ -1,32 +1,30 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2 2
3import * as chai from 'chai'
4import 'mocha' 3import 'mocha'
4import * as chai from 'chai'
5import { 5import {
6 cleanupTests, 6 cleanupTests,
7 createMultipleServers,
7 doubleFollow, 8 doubleFollow,
8 flushAndRunMultipleServers, 9 PeerTubeServer,
9 getLocalIdByUUID, 10 RedundancyCommand,
10 ServerInfo,
11 setAccessTokensToServers, 11 setAccessTokensToServers,
12 uploadVideo, 12 waitJobs
13 uploadVideoAndGetId, 13} from '@shared/extra-utils'
14 waitUntilLog 14import { VideoPrivacy, VideoRedundanciesTarget } from '@shared/models'
15} from '../../../../shared/extra-utils'
16import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
17import { addVideoRedundancy, listVideoRedundancies, removeVideoRedundancy, updateRedundancy } from '@shared/extra-utils/server/redundancy'
18import { VideoPrivacy, VideoRedundanciesTarget, VideoRedundancy } from '@shared/models'
19 15
20const expect = chai.expect 16const expect = chai.expect
21 17
22describe('Test manage videos redundancy', function () { 18describe('Test manage videos redundancy', function () {
23 const targets: VideoRedundanciesTarget[] = [ 'my-videos', 'remote-videos' ] 19 const targets: VideoRedundanciesTarget[] = [ 'my-videos', 'remote-videos' ]
24 20
25 let servers: ServerInfo[] 21 let servers: PeerTubeServer[]
26 let video1Server2UUID: string 22 let video1Server2UUID: string
27 let video2Server2UUID: string 23 let video2Server2UUID: string
28 let redundanciesToRemove: number[] = [] 24 let redundanciesToRemove: number[] = []
29 25
26 let commands: RedundancyCommand[]
27
30 before(async function () { 28 before(async function () {
31 this.timeout(120000) 29 this.timeout(120000)
32 30
@@ -50,40 +48,38 @@ describe('Test manage videos redundancy', function () {
50 } 48 }
51 } 49 }
52 } 50 }
53 servers = await flushAndRunMultipleServers(3, config) 51 servers = await createMultipleServers(3, config)
54 52
55 // Get the access tokens 53 // Get the access tokens
56 await setAccessTokensToServers(servers) 54 await setAccessTokensToServers(servers)
57 55
56 commands = servers.map(s => s.redundancy)
57
58 { 58 {
59 const res = await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'video 1 server 2' }) 59 const { uuid } = await servers[1].videos.upload({ attributes: { name: 'video 1 server 2' } })
60 video1Server2UUID = res.body.video.uuid 60 video1Server2UUID = uuid
61 } 61 }
62 62
63 { 63 {
64 const res = await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'video 2 server 2' }) 64 const { uuid } = await servers[1].videos.upload({ attributes: { name: 'video 2 server 2' } })
65 video2Server2UUID = res.body.video.uuid 65 video2Server2UUID = uuid
66 } 66 }
67 67
68 await waitJobs(servers) 68 await waitJobs(servers)
69 69
70 // Server 1 and server 2 follow each other 70 // Server 1 and server 2 follow each other
71 await doubleFollow(servers[0], servers[1]) 71 await doubleFollow(servers[0], servers[1])
72 await updateRedundancy(servers[0].url, servers[0].accessToken, servers[1].host, true) 72 await commands[0].updateRedundancy({ host: servers[1].host, redundancyAllowed: true })
73 73
74 await waitJobs(servers) 74 await waitJobs(servers)
75 }) 75 })
76 76
77 it('Should not have redundancies on server 3', async function () { 77 it('Should not have redundancies on server 3', async function () {
78 for (const target of targets) { 78 for (const target of targets) {
79 const res = await listVideoRedundancies({ 79 const body = await commands[2].listVideos({ target })
80 url: servers[2].url,
81 accessToken: servers[2].accessToken,
82 target
83 })
84 80
85 expect(res.body.total).to.equal(0) 81 expect(body.total).to.equal(0)
86 expect(res.body.data).to.have.lengthOf(0) 82 expect(body.data).to.have.lengthOf(0)
87 } 83 }
88 }) 84 })
89 85
@@ -91,31 +87,22 @@ describe('Test manage videos redundancy', function () {
91 this.timeout(120000) 87 this.timeout(120000)
92 88
93 await waitJobs(servers) 89 await waitJobs(servers)
94 await waitUntilLog(servers[0], 'Duplicated ', 10) 90 await servers[0].servers.waitUntilLog('Duplicated ', 10)
95 await waitJobs(servers) 91 await waitJobs(servers)
96 92
97 const res = await listVideoRedundancies({ 93 const body = await commands[1].listVideos({ target: 'remote-videos' })
98 url: servers[1].url,
99 accessToken: servers[1].accessToken,
100 target: 'remote-videos'
101 })
102 94
103 expect(res.body.total).to.equal(0) 95 expect(body.total).to.equal(0)
104 expect(res.body.data).to.have.lengthOf(0) 96 expect(body.data).to.have.lengthOf(0)
105 }) 97 })
106 98
107 it('Should have "my-videos" redundancies on server 2', async function () { 99 it('Should have "my-videos" redundancies on server 2', async function () {
108 this.timeout(120000) 100 this.timeout(120000)
109 101
110 const res = await listVideoRedundancies({ 102 const body = await commands[1].listVideos({ target: 'my-videos' })
111 url: servers[1].url, 103 expect(body.total).to.equal(2)
112 accessToken: servers[1].accessToken,
113 target: 'my-videos'
114 })
115
116 expect(res.body.total).to.equal(2)
117 104
118 const videos = res.body.data as VideoRedundancy[] 105 const videos = body.data
119 expect(videos).to.have.lengthOf(2) 106 expect(videos).to.have.lengthOf(2)
120 107
121 const videos1 = videos.find(v => v.uuid === video1Server2UUID) 108 const videos1 = videos.find(v => v.uuid === video1Server2UUID)
@@ -139,28 +126,19 @@ describe('Test manage videos redundancy', function () {
139 }) 126 })
140 127
141 it('Should not have "my-videos" redundancies on server 1', async function () { 128 it('Should not have "my-videos" redundancies on server 1', async function () {
142 const res = await listVideoRedundancies({ 129 const body = await commands[0].listVideos({ target: 'my-videos' })
143 url: servers[0].url,
144 accessToken: servers[0].accessToken,
145 target: 'my-videos'
146 })
147 130
148 expect(res.body.total).to.equal(0) 131 expect(body.total).to.equal(0)
149 expect(res.body.data).to.have.lengthOf(0) 132 expect(body.data).to.have.lengthOf(0)
150 }) 133 })
151 134
152 it('Should have "remote-videos" redundancies on server 1', async function () { 135 it('Should have "remote-videos" redundancies on server 1', async function () {
153 this.timeout(120000) 136 this.timeout(120000)
154 137
155 const res = await listVideoRedundancies({ 138 const body = await commands[0].listVideos({ target: 'remote-videos' })
156 url: servers[0].url, 139 expect(body.total).to.equal(2)
157 accessToken: servers[0].accessToken,
158 target: 'remote-videos'
159 })
160 140
161 expect(res.body.total).to.equal(2) 141 const videos = body.data
162
163 const videos = res.body.data as VideoRedundancy[]
164 expect(videos).to.have.lengthOf(2) 142 expect(videos).to.have.lengthOf(2)
165 143
166 const videos1 = videos.find(v => v.uuid === video1Server2UUID) 144 const videos1 = videos.find(v => v.uuid === video1Server2UUID)
@@ -185,81 +163,67 @@ describe('Test manage videos redundancy', function () {
185 163
186 it('Should correctly paginate and sort results', async function () { 164 it('Should correctly paginate and sort results', async function () {
187 { 165 {
188 const res = await listVideoRedundancies({ 166 const body = await commands[0].listVideos({
189 url: servers[0].url,
190 accessToken: servers[0].accessToken,
191 target: 'remote-videos', 167 target: 'remote-videos',
192 sort: 'name', 168 sort: 'name',
193 start: 0, 169 start: 0,
194 count: 2 170 count: 2
195 }) 171 })
196 172
197 const videos = res.body.data 173 const videos = body.data
198 expect(videos[0].name).to.equal('video 1 server 2') 174 expect(videos[0].name).to.equal('video 1 server 2')
199 expect(videos[1].name).to.equal('video 2 server 2') 175 expect(videos[1].name).to.equal('video 2 server 2')
200 } 176 }
201 177
202 { 178 {
203 const res = await listVideoRedundancies({ 179 const body = await commands[0].listVideos({
204 url: servers[0].url,
205 accessToken: servers[0].accessToken,
206 target: 'remote-videos', 180 target: 'remote-videos',
207 sort: '-name', 181 sort: '-name',
208 start: 0, 182 start: 0,
209 count: 2 183 count: 2
210 }) 184 })
211 185
212 const videos = res.body.data 186 const videos = body.data
213 expect(videos[0].name).to.equal('video 2 server 2') 187 expect(videos[0].name).to.equal('video 2 server 2')
214 expect(videos[1].name).to.equal('video 1 server 2') 188 expect(videos[1].name).to.equal('video 1 server 2')
215 } 189 }
216 190
217 { 191 {
218 const res = await listVideoRedundancies({ 192 const body = await commands[0].listVideos({
219 url: servers[0].url,
220 accessToken: servers[0].accessToken,
221 target: 'remote-videos', 193 target: 'remote-videos',
222 sort: '-name', 194 sort: '-name',
223 start: 1, 195 start: 1,
224 count: 1 196 count: 1
225 }) 197 })
226 198
227 const videos = res.body.data 199 expect(body.data[0].name).to.equal('video 1 server 2')
228 expect(videos[0].name).to.equal('video 1 server 2')
229 } 200 }
230 }) 201 })
231 202
232 it('Should manually add a redundancy and list it', async function () { 203 it('Should manually add a redundancy and list it', async function () {
233 this.timeout(120000) 204 this.timeout(120000)
234 205
235 const uuid = (await uploadVideoAndGetId({ server: servers[1], videoName: 'video 3 server 2', privacy: VideoPrivacy.UNLISTED })).uuid 206 const uuid = (await servers[1].videos.quickUpload({ name: 'video 3 server 2', privacy: VideoPrivacy.UNLISTED })).uuid
236 await waitJobs(servers) 207 await waitJobs(servers)
237 const videoId = await getLocalIdByUUID(servers[0].url, uuid) 208 const videoId = await servers[0].videos.getId({ uuid })
238 209
239 await addVideoRedundancy({ 210 await commands[0].addVideo({ videoId })
240 url: servers[0].url,
241 accessToken: servers[0].accessToken,
242 videoId
243 })
244 211
245 await waitJobs(servers) 212 await waitJobs(servers)
246 await waitUntilLog(servers[0], 'Duplicated ', 15) 213 await servers[0].servers.waitUntilLog('Duplicated ', 15)
247 await waitJobs(servers) 214 await waitJobs(servers)
248 215
249 { 216 {
250 const res = await listVideoRedundancies({ 217 const body = await commands[0].listVideos({
251 url: servers[0].url,
252 accessToken: servers[0].accessToken,
253 target: 'remote-videos', 218 target: 'remote-videos',
254 sort: '-name', 219 sort: '-name',
255 start: 0, 220 start: 0,
256 count: 5 221 count: 5
257 }) 222 })
258 223
259 const videos = res.body.data 224 const video = body.data[0]
260 expect(videos[0].name).to.equal('video 3 server 2')
261 225
262 const video = videos[0] 226 expect(video.name).to.equal('video 3 server 2')
263 expect(video.redundancies.files).to.have.lengthOf(4) 227 expect(video.redundancies.files).to.have.lengthOf(4)
264 expect(video.redundancies.streamingPlaylists).to.have.lengthOf(1) 228 expect(video.redundancies.streamingPlaylists).to.have.lengthOf(1)
265 229
@@ -276,19 +240,15 @@ describe('Test manage videos redundancy', function () {
276 } 240 }
277 } 241 }
278 242
279 const res = await listVideoRedundancies({ 243 const body = await commands[1].listVideos({
280 url: servers[1].url,
281 accessToken: servers[1].accessToken,
282 target: 'my-videos', 244 target: 'my-videos',
283 sort: '-name', 245 sort: '-name',
284 start: 0, 246 start: 0,
285 count: 5 247 count: 5
286 }) 248 })
287 249
288 const videos = res.body.data 250 const video = body.data[0]
289 expect(videos[0].name).to.equal('video 3 server 2') 251 expect(video.name).to.equal('video 3 server 2')
290
291 const video = videos[0]
292 expect(video.redundancies.files).to.have.lengthOf(4) 252 expect(video.redundancies.files).to.have.lengthOf(4)
293 expect(video.redundancies.streamingPlaylists).to.have.lengthOf(1) 253 expect(video.redundancies.streamingPlaylists).to.have.lengthOf(1)
294 254
@@ -307,64 +267,47 @@ describe('Test manage videos redundancy', function () {
307 this.timeout(120000) 267 this.timeout(120000)
308 268
309 for (const redundancyId of redundanciesToRemove) { 269 for (const redundancyId of redundanciesToRemove) {
310 await removeVideoRedundancy({ 270 await commands[0].removeVideo({ redundancyId })
311 url: servers[0].url,
312 accessToken: servers[0].accessToken,
313 redundancyId
314 })
315 } 271 }
316 272
317 { 273 {
318 const res = await listVideoRedundancies({ 274 const body = await commands[0].listVideos({
319 url: servers[0].url,
320 accessToken: servers[0].accessToken,
321 target: 'remote-videos', 275 target: 'remote-videos',
322 sort: '-name', 276 sort: '-name',
323 start: 0, 277 start: 0,
324 count: 5 278 count: 5
325 }) 279 })
326 280
327 const videos = res.body.data 281 const videos = body.data
328 expect(videos).to.have.lengthOf(2)
329 282
330 expect(videos[0].name).to.equal('video 2 server 2') 283 expect(videos).to.have.lengthOf(2)
331 284
332 redundanciesToRemove = []
333 const video = videos[0] 285 const video = videos[0]
286 expect(video.name).to.equal('video 2 server 2')
334 expect(video.redundancies.files).to.have.lengthOf(4) 287 expect(video.redundancies.files).to.have.lengthOf(4)
335 expect(video.redundancies.streamingPlaylists).to.have.lengthOf(1) 288 expect(video.redundancies.streamingPlaylists).to.have.lengthOf(1)
336 289
337 const redundancies = video.redundancies.files.concat(video.redundancies.streamingPlaylists) 290 const redundancies = video.redundancies.files.concat(video.redundancies.streamingPlaylists)
338 291
339 for (const r of redundancies) { 292 redundanciesToRemove = redundancies.map(r => r.id)
340 redundanciesToRemove.push(r.id)
341 }
342 } 293 }
343 }) 294 })
344 295
345 it('Should remove another (auto) redundancy', async function () { 296 it('Should remove another (auto) redundancy', async function () {
346 { 297 for (const redundancyId of redundanciesToRemove) {
347 for (const redundancyId of redundanciesToRemove) { 298 await commands[0].removeVideo({ redundancyId })
348 await removeVideoRedundancy({ 299 }
349 url: servers[0].url,
350 accessToken: servers[0].accessToken,
351 redundancyId
352 })
353 }
354 300
355 const res = await listVideoRedundancies({ 301 const body = await commands[0].listVideos({
356 url: servers[0].url, 302 target: 'remote-videos',
357 accessToken: servers[0].accessToken, 303 sort: '-name',
358 target: 'remote-videos', 304 start: 0,
359 sort: '-name', 305 count: 5
360 start: 0, 306 })
361 count: 5
362 })
363 307
364 const videos = res.body.data 308 const videos = body.data
365 expect(videos[0].name).to.equal('video 1 server 2') 309 expect(videos).to.have.lengthOf(1)
366 expect(videos).to.have.lengthOf(1) 310 expect(videos[0].name).to.equal('video 1 server 2')
367 }
368 }) 311 })
369 312
370 after(async function () { 313 after(async function () {
diff --git a/server/tests/api/redundancy/redundancy-constraints.ts b/server/tests/api/redundancy/redundancy-constraints.ts
index 1cb1603bc..933a2c776 100644
--- a/server/tests/api/redundancy/redundancy-constraints.ts
+++ b/server/tests/api/redundancy/redundancy-constraints.ts
@@ -1,29 +1,14 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2 2
3import 'mocha' 3import 'mocha'
4import * as chai from 'chai' 4import { expect } from 'chai'
5import { listVideoRedundancies, updateRedundancy } from '@shared/extra-utils/server/redundancy' 5import { cleanupTests, createSingleServer, killallServers, PeerTubeServer, setAccessTokensToServers, waitJobs } from '@shared/extra-utils'
6import { VideoPrivacy } from '@shared/models' 6import { VideoPrivacy } from '@shared/models'
7import {
8 cleanupTests,
9 flushAndRunServer,
10 follow,
11 killallServers,
12 reRunServer,
13 ServerInfo,
14 setAccessTokensToServers,
15 updateVideo,
16 uploadVideo,
17 waitUntilLog
18} from '../../../../shared/extra-utils'
19import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
20
21const expect = chai.expect
22 7
23describe('Test redundancy constraints', function () { 8describe('Test redundancy constraints', function () {
24 let remoteServer: ServerInfo 9 let remoteServer: PeerTubeServer
25 let localServer: ServerInfo 10 let localServer: PeerTubeServer
26 let servers: ServerInfo[] 11 let servers: PeerTubeServer[]
27 12
28 const remoteServerConfig = { 13 const remoteServerConfig = {
29 redundancy: { 14 redundancy: {
@@ -43,38 +28,30 @@ describe('Test redundancy constraints', function () {
43 28
44 async function uploadWrapper (videoName: string) { 29 async function uploadWrapper (videoName: string) {
45 // Wait for transcoding 30 // Wait for transcoding
46 const res = await uploadVideo(localServer.url, localServer.accessToken, { name: 'to transcode', privacy: VideoPrivacy.PRIVATE }) 31 const { id } = await localServer.videos.upload({ attributes: { name: 'to transcode', privacy: VideoPrivacy.PRIVATE } })
47 await waitJobs([ localServer ]) 32 await waitJobs([ localServer ])
48 33
49 // Update video to schedule a federation 34 // Update video to schedule a federation
50 await updateVideo(localServer.url, localServer.accessToken, res.body.video.id, { name: videoName, privacy: VideoPrivacy.PUBLIC }) 35 await localServer.videos.update({ id, attributes: { name: videoName, privacy: VideoPrivacy.PUBLIC } })
51 } 36 }
52 37
53 async function getTotalRedundanciesLocalServer () { 38 async function getTotalRedundanciesLocalServer () {
54 const res = await listVideoRedundancies({ 39 const body = await localServer.redundancy.listVideos({ target: 'my-videos' })
55 url: localServer.url,
56 accessToken: localServer.accessToken,
57 target: 'my-videos'
58 })
59 40
60 return res.body.total 41 return body.total
61 } 42 }
62 43
63 async function getTotalRedundanciesRemoteServer () { 44 async function getTotalRedundanciesRemoteServer () {
64 const res = await listVideoRedundancies({ 45 const body = await remoteServer.redundancy.listVideos({ target: 'remote-videos' })
65 url: remoteServer.url,
66 accessToken: remoteServer.accessToken,
67 target: 'remote-videos'
68 })
69 46
70 return res.body.total 47 return body.total
71 } 48 }
72 49
73 before(async function () { 50 before(async function () {
74 this.timeout(120000) 51 this.timeout(120000)
75 52
76 { 53 {
77 remoteServer = await flushAndRunServer(1, remoteServerConfig) 54 remoteServer = await createSingleServer(1, remoteServerConfig)
78 } 55 }
79 56
80 { 57 {
@@ -85,7 +62,7 @@ describe('Test redundancy constraints', function () {
85 } 62 }
86 } 63 }
87 } 64 }
88 localServer = await flushAndRunServer(2, config) 65 localServer = await createSingleServer(2, config)
89 } 66 }
90 67
91 servers = [ remoteServer, localServer ] 68 servers = [ remoteServer, localServer ]
@@ -93,14 +70,14 @@ describe('Test redundancy constraints', function () {
93 // Get the access tokens 70 // Get the access tokens
94 await setAccessTokensToServers(servers) 71 await setAccessTokensToServers(servers)
95 72
96 await uploadVideo(localServer.url, localServer.accessToken, { name: 'video 1 server 2' }) 73 await localServer.videos.upload({ attributes: { name: 'video 1 server 2' } })
97 74
98 await waitJobs(servers) 75 await waitJobs(servers)
99 76
100 // Server 1 and server 2 follow each other 77 // Server 1 and server 2 follow each other
101 await follow(remoteServer.url, [ localServer.url ], remoteServer.accessToken) 78 await remoteServer.follows.follow({ hosts: [ localServer.url ] })
102 await waitJobs(servers) 79 await waitJobs(servers)
103 await updateRedundancy(remoteServer.url, remoteServer.accessToken, localServer.host, true) 80 await remoteServer.redundancy.updateRedundancy({ host: localServer.host, redundancyAllowed: true })
104 81
105 await waitJobs(servers) 82 await waitJobs(servers)
106 }) 83 })
@@ -109,7 +86,7 @@ describe('Test redundancy constraints', function () {
109 this.timeout(120000) 86 this.timeout(120000)
110 87
111 await waitJobs(servers) 88 await waitJobs(servers)
112 await waitUntilLog(remoteServer, 'Duplicated ', 5) 89 await remoteServer.servers.waitUntilLog('Duplicated ', 5)
113 await waitJobs(servers) 90 await waitJobs(servers)
114 91
115 { 92 {
@@ -134,11 +111,11 @@ describe('Test redundancy constraints', function () {
134 } 111 }
135 } 112 }
136 await killallServers([ localServer ]) 113 await killallServers([ localServer ])
137 await reRunServer(localServer, config) 114 await localServer.run(config)
138 115
139 await uploadWrapper('video 2 server 2') 116 await uploadWrapper('video 2 server 2')
140 117
141 await waitUntilLog(remoteServer, 'Duplicated ', 10) 118 await remoteServer.servers.waitUntilLog('Duplicated ', 10)
142 await waitJobs(servers) 119 await waitJobs(servers)
143 120
144 { 121 {
@@ -163,11 +140,11 @@ describe('Test redundancy constraints', function () {
163 } 140 }
164 } 141 }
165 await killallServers([ localServer ]) 142 await killallServers([ localServer ])
166 await reRunServer(localServer, config) 143 await localServer.run(config)
167 144
168 await uploadWrapper('video 3 server 2') 145 await uploadWrapper('video 3 server 2')
169 146
170 await waitUntilLog(remoteServer, 'Duplicated ', 15) 147 await remoteServer.servers.waitUntilLog('Duplicated ', 15)
171 await waitJobs(servers) 148 await waitJobs(servers)
172 149
173 { 150 {
@@ -184,11 +161,11 @@ describe('Test redundancy constraints', function () {
184 it('Should have redundancy on server 1 and on server 2 with followings filter now server 2 follows server 1', async function () { 161 it('Should have redundancy on server 1 and on server 2 with followings filter now server 2 follows server 1', async function () {
185 this.timeout(120000) 162 this.timeout(120000)
186 163
187 await follow(localServer.url, [ remoteServer.url ], localServer.accessToken) 164 await localServer.follows.follow({ hosts: [ remoteServer.url ] })
188 await waitJobs(servers) 165 await waitJobs(servers)
189 166
190 await uploadWrapper('video 4 server 2') 167 await uploadWrapper('video 4 server 2')
191 await waitUntilLog(remoteServer, 'Duplicated ', 20) 168 await remoteServer.servers.waitUntilLog('Duplicated ', 20)
192 await waitJobs(servers) 169 await waitJobs(servers)
193 170
194 { 171 {
diff --git a/server/tests/api/redundancy/redundancy.ts b/server/tests/api/redundancy/redundancy.ts
index 0e0a73b9d..a6559d304 100644
--- a/server/tests/api/redundancy/redundancy.ts
+++ b/server/tests/api/redundancy/redundancy.ts
@@ -5,51 +5,29 @@ import * as chai from 'chai'
5import { readdir } from 'fs-extra' 5import { readdir } from 'fs-extra'
6import * as magnetUtil from 'magnet-uri' 6import * as magnetUtil from 'magnet-uri'
7import { join } from 'path' 7import { join } from 'path'
8import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
9import { 8import {
10 checkSegmentHash, 9 checkSegmentHash,
11 checkVideoFilesWereRemoved, 10 checkVideoFilesWereRemoved,
12 cleanupTests, 11 cleanupTests,
12 createMultipleServers,
13 doubleFollow, 13 doubleFollow,
14 flushAndRunMultipleServers,
15 getFollowingListPaginationAndSort,
16 getVideo,
17 getVideoWithToken,
18 immutableAssign,
19 killallServers, 14 killallServers,
20 makeGetRequest, 15 makeGetRequest,
21 removeVideo, 16 PeerTubeServer,
22 reRunServer,
23 root, 17 root,
24 ServerInfo,
25 setAccessTokensToServers, 18 setAccessTokensToServers,
26 unfollow,
27 updateVideo,
28 uploadVideo,
29 viewVideo,
30 wait, 19 wait,
31 waitUntilLog 20 waitJobs
32} from '../../../../shared/extra-utils' 21} from '@shared/extra-utils'
33import { waitJobs } from '../../../../shared/extra-utils/server/jobs' 22import { HttpStatusCode, VideoPrivacy, VideoRedundancyStrategy, VideoRedundancyStrategyWithManual } from '@shared/models'
34import {
35 addVideoRedundancy,
36 listVideoRedundancies,
37 removeVideoRedundancy,
38 updateRedundancy
39} from '../../../../shared/extra-utils/server/redundancy'
40import { getStats } from '../../../../shared/extra-utils/server/stats'
41import { ActorFollow } from '../../../../shared/models/actors'
42import { VideoRedundancy, VideoRedundancyStrategy, VideoRedundancyStrategyWithManual } from '../../../../shared/models/redundancy'
43import { ServerStats } from '../../../../shared/models/server/server-stats.model'
44import { VideoDetails, VideoPrivacy } from '../../../../shared/models/videos'
45 23
46const expect = chai.expect 24const expect = chai.expect
47 25
48let servers: ServerInfo[] = [] 26let servers: PeerTubeServer[] = []
49let video1Server2UUID: string 27let video1Server2UUID: string
50let video1Server2Id: number 28let video1Server2Id: number
51 29
52function checkMagnetWebseeds (file: { magnetUri: string, resolution: { id: number } }, baseWebseeds: string[], server: ServerInfo) { 30function checkMagnetWebseeds (file: { magnetUri: string, resolution: { id: number } }, baseWebseeds: string[], server: PeerTubeServer) {
53 const parsed = magnetUtil.decode(file.magnetUri) 31 const parsed = magnetUtil.decode(file.magnetUri)
54 32
55 for (const ws of baseWebseeds) { 33 for (const ws of baseWebseeds) {
@@ -60,16 +38,18 @@ function checkMagnetWebseeds (file: { magnetUri: string, resolution: { id: numbe
60 expect(parsed.urlList).to.have.lengthOf(baseWebseeds.length) 38 expect(parsed.urlList).to.have.lengthOf(baseWebseeds.length)
61} 39}
62 40
63async function flushAndRunServers (strategy: VideoRedundancyStrategy | null, additionalParams: any = {}, withWebtorrent = true) { 41async function createSingleServers (strategy: VideoRedundancyStrategy | null, additionalParams: any = {}, withWebtorrent = true) {
64 const strategies: any[] = [] 42 const strategies: any[] = []
65 43
66 if (strategy !== null) { 44 if (strategy !== null) {
67 strategies.push( 45 strategies.push(
68 immutableAssign({ 46 {
69 min_lifetime: '1 hour', 47 min_lifetime: '1 hour',
70 strategy: strategy, 48 strategy: strategy,
71 size: '400KB' 49 size: '400KB',
72 }, additionalParams) 50
51 ...additionalParams
52 }
73 ) 53 )
74 } 54 }
75 55
@@ -90,17 +70,17 @@ async function flushAndRunServers (strategy: VideoRedundancyStrategy | null, add
90 } 70 }
91 } 71 }
92 72
93 servers = await flushAndRunMultipleServers(3, config) 73 servers = await createMultipleServers(3, config)
94 74
95 // Get the access tokens 75 // Get the access tokens
96 await setAccessTokensToServers(servers) 76 await setAccessTokensToServers(servers)
97 77
98 { 78 {
99 const res = await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'video 1 server 2' }) 79 const { uuid, id } = await servers[1].videos.upload({ attributes: { name: 'video 1 server 2' } })
100 video1Server2UUID = res.body.video.uuid 80 video1Server2UUID = uuid
101 video1Server2Id = res.body.video.id 81 video1Server2Id = id
102 82
103 await viewVideo(servers[1].url, video1Server2UUID) 83 await servers[1].videos.view({ id: video1Server2UUID })
104 } 84 }
105 85
106 await waitJobs(servers) 86 await waitJobs(servers)
@@ -124,9 +104,8 @@ async function check1WebSeed (videoUUID?: string) {
124 104
125 for (const server of servers) { 105 for (const server of servers) {
126 // With token to avoid issues with video follow constraints 106 // With token to avoid issues with video follow constraints
127 const res = await getVideoWithToken(server.url, server.accessToken, videoUUID) 107 const video = await server.videos.getWithToken({ id: videoUUID })
128 108
129 const video: VideoDetails = res.body
130 for (const f of video.files) { 109 for (const f of video.files) {
131 checkMagnetWebseeds(f, webseeds, server) 110 checkMagnetWebseeds(f, webseeds, server)
132 } 111 }
@@ -142,22 +121,20 @@ async function check2Webseeds (videoUUID?: string) {
142 ] 121 ]
143 122
144 for (const server of servers) { 123 for (const server of servers) {
145 const res = await getVideo(server.url, videoUUID) 124 const video = await server.videos.get({ id: videoUUID })
146
147 const video: VideoDetails = res.body
148 125
149 for (const file of video.files) { 126 for (const file of video.files) {
150 checkMagnetWebseeds(file, webseeds, server) 127 checkMagnetWebseeds(file, webseeds, server)
151 128
152 await makeGetRequest({ 129 await makeGetRequest({
153 url: servers[0].url, 130 url: servers[0].url,
154 statusCodeExpected: HttpStatusCode.OK_200, 131 expectedStatus: HttpStatusCode.OK_200,
155 path: '/static/redundancy/' + `${videoUUID}-${file.resolution.id}.mp4`, 132 path: '/static/redundancy/' + `${videoUUID}-${file.resolution.id}.mp4`,
156 contentType: null 133 contentType: null
157 }) 134 })
158 await makeGetRequest({ 135 await makeGetRequest({
159 url: servers[1].url, 136 url: servers[1].url,
160 statusCodeExpected: HttpStatusCode.OK_200, 137 expectedStatus: HttpStatusCode.OK_200,
161 path: `/static/webseed/${videoUUID}-${file.resolution.id}.mp4`, 138 path: `/static/webseed/${videoUUID}-${file.resolution.id}.mp4`,
162 contentType: null 139 contentType: null
163 }) 140 })
@@ -184,8 +161,7 @@ async function check0PlaylistRedundancies (videoUUID?: string) {
184 161
185 for (const server of servers) { 162 for (const server of servers) {
186 // With token to avoid issues with video follow constraints 163 // With token to avoid issues with video follow constraints
187 const res = await getVideoWithToken(server.url, server.accessToken, videoUUID) 164 const video = await server.videos.getWithToken({ id: videoUUID })
188 const video: VideoDetails = res.body
189 165
190 expect(video.streamingPlaylists).to.be.an('array') 166 expect(video.streamingPlaylists).to.be.an('array')
191 expect(video.streamingPlaylists).to.have.lengthOf(1) 167 expect(video.streamingPlaylists).to.have.lengthOf(1)
@@ -197,8 +173,7 @@ async function check1PlaylistRedundancies (videoUUID?: string) {
197 if (!videoUUID) videoUUID = video1Server2UUID 173 if (!videoUUID) videoUUID = video1Server2UUID
198 174
199 for (const server of servers) { 175 for (const server of servers) {
200 const res = await getVideo(server.url, videoUUID) 176 const video = await server.videos.get({ id: videoUUID })
201 const video: VideoDetails = res.body
202 177
203 expect(video.streamingPlaylists).to.have.lengthOf(1) 178 expect(video.streamingPlaylists).to.have.lengthOf(1)
204 expect(video.streamingPlaylists[0].redundancies).to.have.lengthOf(1) 179 expect(video.streamingPlaylists[0].redundancies).to.have.lengthOf(1)
@@ -211,11 +186,11 @@ async function check1PlaylistRedundancies (videoUUID?: string) {
211 const baseUrlPlaylist = servers[1].url + '/static/streaming-playlists/hls' 186 const baseUrlPlaylist = servers[1].url + '/static/streaming-playlists/hls'
212 const baseUrlSegment = servers[0].url + '/static/redundancy/hls' 187 const baseUrlSegment = servers[0].url + '/static/redundancy/hls'
213 188
214 const res = await getVideo(servers[0].url, videoUUID) 189 const video = await servers[0].videos.get({ id: videoUUID })
215 const hlsPlaylist = (res.body as VideoDetails).streamingPlaylists[0] 190 const hlsPlaylist = video.streamingPlaylists[0]
216 191
217 for (const resolution of [ 240, 360, 480, 720 ]) { 192 for (const resolution of [ 240, 360, 480, 720 ]) {
218 await checkSegmentHash(baseUrlPlaylist, baseUrlSegment, videoUUID, resolution, hlsPlaylist) 193 await checkSegmentHash({ server: servers[1], baseUrlPlaylist, baseUrlSegment, videoUUID, resolution, hlsPlaylist })
219 } 194 }
220 195
221 const directories = [ 196 const directories = [
@@ -244,9 +219,7 @@ async function checkStatsGlobal (strategy: VideoRedundancyStrategyWithManual) {
244 statsLength = 2 219 statsLength = 2
245 } 220 }
246 221
247 const res = await getStats(servers[0].url) 222 const data = await servers[0].stats.get()
248 const data: ServerStats = res.body
249
250 expect(data.videosRedundancy).to.have.lengthOf(statsLength) 223 expect(data.videosRedundancy).to.have.lengthOf(statsLength)
251 224
252 const stat = data.videosRedundancy[0] 225 const stat = data.videosRedundancy[0]
@@ -272,14 +245,20 @@ async function checkStatsWithoutRedundancy (strategy: VideoRedundancyStrategyWit
272 expect(stat.totalVideos).to.equal(0) 245 expect(stat.totalVideos).to.equal(0)
273} 246}
274 247
275async function enableRedundancyOnServer1 () { 248async function findServerFollows () {
276 await updateRedundancy(servers[0].url, servers[0].accessToken, servers[1].host, true) 249 const body = await servers[0].follows.getFollowings({ start: 0, count: 5, sort: '-createdAt' })
277 250 const follows = body.data
278 const res = await getFollowingListPaginationAndSort({ url: servers[0].url, start: 0, count: 5, sort: '-createdAt' })
279 const follows: ActorFollow[] = res.body.data
280 const server2 = follows.find(f => f.following.host === `localhost:${servers[1].port}`) 251 const server2 = follows.find(f => f.following.host === `localhost:${servers[1].port}`)
281 const server3 = follows.find(f => f.following.host === `localhost:${servers[2].port}`) 252 const server3 = follows.find(f => f.following.host === `localhost:${servers[2].port}`)
282 253
254 return { server2, server3 }
255}
256
257async function enableRedundancyOnServer1 () {
258 await servers[0].redundancy.updateRedundancy({ host: servers[1].host, redundancyAllowed: true })
259
260 const { server2, server3 } = await findServerFollows()
261
283 expect(server3).to.not.be.undefined 262 expect(server3).to.not.be.undefined
284 expect(server3.following.hostRedundancyAllowed).to.be.false 263 expect(server3.following.hostRedundancyAllowed).to.be.false
285 264
@@ -288,12 +267,9 @@ async function enableRedundancyOnServer1 () {
288} 267}
289 268
290async function disableRedundancyOnServer1 () { 269async function disableRedundancyOnServer1 () {
291 await updateRedundancy(servers[0].url, servers[0].accessToken, servers[1].host, false) 270 await servers[0].redundancy.updateRedundancy({ host: servers[1].host, redundancyAllowed: false })
292 271
293 const res = await getFollowingListPaginationAndSort({ url: servers[0].url, start: 0, count: 5, sort: '-createdAt' }) 272 const { server2, server3 } = await findServerFollows()
294 const follows: ActorFollow[] = res.body.data
295 const server2 = follows.find(f => f.following.host === `localhost:${servers[1].port}`)
296 const server3 = follows.find(f => f.following.host === `localhost:${servers[2].port}`)
297 273
298 expect(server3).to.not.be.undefined 274 expect(server3).to.not.be.undefined
299 expect(server3.following.hostRedundancyAllowed).to.be.false 275 expect(server3.following.hostRedundancyAllowed).to.be.false
@@ -310,7 +286,7 @@ describe('Test videos redundancy', function () {
310 before(function () { 286 before(function () {
311 this.timeout(120000) 287 this.timeout(120000)
312 288
313 return flushAndRunServers(strategy) 289 return createSingleServers(strategy)
314 }) 290 })
315 291
316 it('Should have 1 webseed on the first video', async function () { 292 it('Should have 1 webseed on the first video', async function () {
@@ -327,7 +303,7 @@ describe('Test videos redundancy', function () {
327 this.timeout(80000) 303 this.timeout(80000)
328 304
329 await waitJobs(servers) 305 await waitJobs(servers)
330 await waitUntilLog(servers[0], 'Duplicated ', 5) 306 await servers[0].servers.waitUntilLog('Duplicated ', 5)
331 await waitJobs(servers) 307 await waitJobs(servers)
332 308
333 await check2Webseeds() 309 await check2Webseeds()
@@ -346,7 +322,7 @@ describe('Test videos redundancy', function () {
346 await check1WebSeed() 322 await check1WebSeed()
347 await check0PlaylistRedundancies() 323 await check0PlaylistRedundancies()
348 324
349 await checkVideoFilesWereRemoved(video1Server2UUID, servers[0].internalServerNumber, [ 'videos', join('playlists', 'hls') ]) 325 await checkVideoFilesWereRemoved(video1Server2UUID, servers[0], [ 'videos', join('playlists', 'hls') ])
350 }) 326 })
351 327
352 after(async function () { 328 after(async function () {
@@ -360,7 +336,7 @@ describe('Test videos redundancy', function () {
360 before(function () { 336 before(function () {
361 this.timeout(120000) 337 this.timeout(120000)
362 338
363 return flushAndRunServers(strategy) 339 return createSingleServers(strategy)
364 }) 340 })
365 341
366 it('Should have 1 webseed on the first video', async function () { 342 it('Should have 1 webseed on the first video', async function () {
@@ -377,7 +353,7 @@ describe('Test videos redundancy', function () {
377 this.timeout(80000) 353 this.timeout(80000)
378 354
379 await waitJobs(servers) 355 await waitJobs(servers)
380 await waitUntilLog(servers[0], 'Duplicated ', 5) 356 await servers[0].servers.waitUntilLog('Duplicated ', 5)
381 await waitJobs(servers) 357 await waitJobs(servers)
382 358
383 await check2Webseeds() 359 await check2Webseeds()
@@ -388,7 +364,7 @@ describe('Test videos redundancy', function () {
388 it('Should unfollow on server 1 and remove duplicated videos', async function () { 364 it('Should unfollow on server 1 and remove duplicated videos', async function () {
389 this.timeout(80000) 365 this.timeout(80000)
390 366
391 await unfollow(servers[0].url, servers[0].accessToken, servers[1]) 367 await servers[0].follows.unfollow({ target: servers[1] })
392 368
393 await waitJobs(servers) 369 await waitJobs(servers)
394 await wait(5000) 370 await wait(5000)
@@ -396,7 +372,7 @@ describe('Test videos redundancy', function () {
396 await check1WebSeed() 372 await check1WebSeed()
397 await check0PlaylistRedundancies() 373 await check0PlaylistRedundancies()
398 374
399 await checkVideoFilesWereRemoved(video1Server2UUID, servers[0].internalServerNumber, [ 'videos' ]) 375 await checkVideoFilesWereRemoved(video1Server2UUID, servers[0], [ 'videos' ])
400 }) 376 })
401 377
402 after(async function () { 378 after(async function () {
@@ -410,7 +386,7 @@ describe('Test videos redundancy', function () {
410 before(function () { 386 before(function () {
411 this.timeout(120000) 387 this.timeout(120000)
412 388
413 return flushAndRunServers(strategy, { min_views: 3 }) 389 return createSingleServers(strategy, { min_views: 3 })
414 }) 390 })
415 391
416 it('Should have 1 webseed on the first video', async function () { 392 it('Should have 1 webseed on the first video', async function () {
@@ -438,8 +414,8 @@ describe('Test videos redundancy', function () {
438 it('Should view 2 times the first video to have > min_views config', async function () { 414 it('Should view 2 times the first video to have > min_views config', async function () {
439 this.timeout(80000) 415 this.timeout(80000)
440 416
441 await viewVideo(servers[0].url, video1Server2UUID) 417 await servers[0].videos.view({ id: video1Server2UUID })
442 await viewVideo(servers[2].url, video1Server2UUID) 418 await servers[2].videos.view({ id: video1Server2UUID })
443 419
444 await wait(10000) 420 await wait(10000)
445 await waitJobs(servers) 421 await waitJobs(servers)
@@ -449,7 +425,7 @@ describe('Test videos redundancy', function () {
449 this.timeout(80000) 425 this.timeout(80000)
450 426
451 await waitJobs(servers) 427 await waitJobs(servers)
452 await waitUntilLog(servers[0], 'Duplicated ', 5) 428 await servers[0].servers.waitUntilLog('Duplicated ', 5)
453 await waitJobs(servers) 429 await waitJobs(servers)
454 430
455 await check2Webseeds() 431 await check2Webseeds()
@@ -460,12 +436,12 @@ describe('Test videos redundancy', function () {
460 it('Should remove the video and the redundancy files', async function () { 436 it('Should remove the video and the redundancy files', async function () {
461 this.timeout(20000) 437 this.timeout(20000)
462 438
463 await removeVideo(servers[1].url, servers[1].accessToken, video1Server2UUID) 439 await servers[1].videos.remove({ id: video1Server2UUID })
464 440
465 await waitJobs(servers) 441 await waitJobs(servers)
466 442
467 for (const server of servers) { 443 for (const server of servers) {
468 await checkVideoFilesWereRemoved(video1Server2UUID, server.internalServerNumber) 444 await checkVideoFilesWereRemoved(video1Server2UUID, server)
469 } 445 }
470 }) 446 })
471 447
@@ -480,7 +456,7 @@ describe('Test videos redundancy', function () {
480 before(async function () { 456 before(async function () {
481 this.timeout(120000) 457 this.timeout(120000)
482 458
483 await flushAndRunServers(strategy, { min_views: 3 }, false) 459 await createSingleServers(strategy, { min_views: 3 }, false)
484 }) 460 })
485 461
486 it('Should have 0 playlist redundancy on the first video', async function () { 462 it('Should have 0 playlist redundancy on the first video', async function () {
@@ -506,14 +482,14 @@ describe('Test videos redundancy', function () {
506 it('Should have 1 redundancy on the first video', async function () { 482 it('Should have 1 redundancy on the first video', async function () {
507 this.timeout(160000) 483 this.timeout(160000)
508 484
509 await viewVideo(servers[0].url, video1Server2UUID) 485 await servers[0].videos.view({ id: video1Server2UUID })
510 await viewVideo(servers[2].url, video1Server2UUID) 486 await servers[2].videos.view({ id: video1Server2UUID })
511 487
512 await wait(10000) 488 await wait(10000)
513 await waitJobs(servers) 489 await waitJobs(servers)
514 490
515 await waitJobs(servers) 491 await waitJobs(servers)
516 await waitUntilLog(servers[0], 'Duplicated ', 1) 492 await servers[0].servers.waitUntilLog('Duplicated ', 1)
517 await waitJobs(servers) 493 await waitJobs(servers)
518 494
519 await check1PlaylistRedundancies() 495 await check1PlaylistRedundancies()
@@ -523,12 +499,12 @@ describe('Test videos redundancy', function () {
523 it('Should remove the video and the redundancy files', async function () { 499 it('Should remove the video and the redundancy files', async function () {
524 this.timeout(20000) 500 this.timeout(20000)
525 501
526 await removeVideo(servers[1].url, servers[1].accessToken, video1Server2UUID) 502 await servers[1].videos.remove({ id: video1Server2UUID })
527 503
528 await waitJobs(servers) 504 await waitJobs(servers)
529 505
530 for (const server of servers) { 506 for (const server of servers) {
531 await checkVideoFilesWereRemoved(video1Server2UUID, server.internalServerNumber) 507 await checkVideoFilesWereRemoved(video1Server2UUID, server)
532 } 508 }
533 }) 509 })
534 510
@@ -541,7 +517,7 @@ describe('Test videos redundancy', function () {
541 before(function () { 517 before(function () {
542 this.timeout(120000) 518 this.timeout(120000)
543 519
544 return flushAndRunServers(null) 520 return createSingleServers(null)
545 }) 521 })
546 522
547 it('Should have 1 webseed on the first video', async function () { 523 it('Should have 1 webseed on the first video', async function () {
@@ -551,18 +527,14 @@ describe('Test videos redundancy', function () {
551 }) 527 })
552 528
553 it('Should create a redundancy on first video', async function () { 529 it('Should create a redundancy on first video', async function () {
554 await addVideoRedundancy({ 530 await servers[0].redundancy.addVideo({ videoId: video1Server2Id })
555 url: servers[0].url,
556 accessToken: servers[0].accessToken,
557 videoId: video1Server2Id
558 })
559 }) 531 })
560 532
561 it('Should have 2 webseeds on the first video', async function () { 533 it('Should have 2 webseeds on the first video', async function () {
562 this.timeout(80000) 534 this.timeout(80000)
563 535
564 await waitJobs(servers) 536 await waitJobs(servers)
565 await waitUntilLog(servers[0], 'Duplicated ', 5) 537 await servers[0].servers.waitUntilLog('Duplicated ', 5)
566 await waitJobs(servers) 538 await waitJobs(servers)
567 539
568 await check2Webseeds() 540 await check2Webseeds()
@@ -573,22 +545,15 @@ describe('Test videos redundancy', function () {
573 it('Should manually remove redundancies on server 1 and remove duplicated videos', async function () { 545 it('Should manually remove redundancies on server 1 and remove duplicated videos', async function () {
574 this.timeout(80000) 546 this.timeout(80000)
575 547
576 const res = await listVideoRedundancies({ 548 const body = await servers[0].redundancy.listVideos({ target: 'remote-videos' })
577 url: servers[0].url,
578 accessToken: servers[0].accessToken,
579 target: 'remote-videos'
580 })
581 549
582 const videos = res.body.data as VideoRedundancy[] 550 const videos = body.data
583 expect(videos).to.have.lengthOf(1) 551 expect(videos).to.have.lengthOf(1)
584 552
585 const video = videos[0] 553 const video = videos[0]
554
586 for (const r of video.redundancies.files.concat(video.redundancies.streamingPlaylists)) { 555 for (const r of video.redundancies.files.concat(video.redundancies.streamingPlaylists)) {
587 await removeVideoRedundancy({ 556 await servers[0].redundancy.removeVideo({ redundancyId: r.id })
588 url: servers[0].url,
589 accessToken: servers[0].accessToken,
590 redundancyId: r.id
591 })
592 } 557 }
593 558
594 await waitJobs(servers) 559 await waitJobs(servers)
@@ -597,7 +562,7 @@ describe('Test videos redundancy', function () {
597 await check1WebSeed() 562 await check1WebSeed()
598 await check0PlaylistRedundancies() 563 await check0PlaylistRedundancies()
599 564
600 await checkVideoFilesWereRemoved(video1Server2UUID, servers[0].serverNumber, [ 'videos' ]) 565 await checkVideoFilesWereRemoved(video1Server2UUID, servers[0], [ 'videos' ])
601 }) 566 })
602 567
603 after(async function () { 568 after(async function () {
@@ -608,10 +573,9 @@ describe('Test videos redundancy', function () {
608 describe('Test expiration', function () { 573 describe('Test expiration', function () {
609 const strategy = 'recently-added' 574 const strategy = 'recently-added'
610 575
611 async function checkContains (servers: ServerInfo[], str: string) { 576 async function checkContains (servers: PeerTubeServer[], str: string) {
612 for (const server of servers) { 577 for (const server of servers) {
613 const res = await getVideo(server.url, video1Server2UUID) 578 const video = await server.videos.get({ id: video1Server2UUID })
614 const video: VideoDetails = res.body
615 579
616 for (const f of video.files) { 580 for (const f of video.files) {
617 expect(f.magnetUri).to.contain(str) 581 expect(f.magnetUri).to.contain(str)
@@ -619,10 +583,9 @@ describe('Test videos redundancy', function () {
619 } 583 }
620 } 584 }
621 585
622 async function checkNotContains (servers: ServerInfo[], str: string) { 586 async function checkNotContains (servers: PeerTubeServer[], str: string) {
623 for (const server of servers) { 587 for (const server of servers) {
624 const res = await getVideo(server.url, video1Server2UUID) 588 const video = await server.videos.get({ id: video1Server2UUID })
625 const video: VideoDetails = res.body
626 589
627 for (const f of video.files) { 590 for (const f of video.files) {
628 expect(f.magnetUri).to.not.contain(str) 591 expect(f.magnetUri).to.not.contain(str)
@@ -633,7 +596,7 @@ describe('Test videos redundancy', function () {
633 before(async function () { 596 before(async function () {
634 this.timeout(120000) 597 this.timeout(120000)
635 598
636 await flushAndRunServers(strategy, { min_lifetime: '7 seconds', min_views: 0 }) 599 await createSingleServers(strategy, { min_lifetime: '7 seconds', min_views: 0 })
637 600
638 await enableRedundancyOnServer1() 601 await enableRedundancyOnServer1()
639 }) 602 })
@@ -656,7 +619,7 @@ describe('Test videos redundancy', function () {
656 it('Should stop server 1 and expire video redundancy', async function () { 619 it('Should stop server 1 and expire video redundancy', async function () {
657 this.timeout(80000) 620 this.timeout(80000)
658 621
659 killallServers([ servers[0] ]) 622 await killallServers([ servers[0] ])
660 623
661 await wait(15000) 624 await wait(15000)
662 625
@@ -675,25 +638,25 @@ describe('Test videos redundancy', function () {
675 before(async function () { 638 before(async function () {
676 this.timeout(120000) 639 this.timeout(120000)
677 640
678 await flushAndRunServers(strategy, { min_lifetime: '7 seconds', min_views: 0 }) 641 await createSingleServers(strategy, { min_lifetime: '7 seconds', min_views: 0 })
679 642
680 await enableRedundancyOnServer1() 643 await enableRedundancyOnServer1()
681 644
682 await waitJobs(servers) 645 await waitJobs(servers)
683 await waitUntilLog(servers[0], 'Duplicated ', 5) 646 await servers[0].servers.waitUntilLog('Duplicated ', 5)
684 await waitJobs(servers) 647 await waitJobs(servers)
685 648
686 await check2Webseeds(video1Server2UUID) 649 await check2Webseeds(video1Server2UUID)
687 await check1PlaylistRedundancies(video1Server2UUID) 650 await check1PlaylistRedundancies(video1Server2UUID)
688 await checkStatsWith1Redundancy(strategy) 651 await checkStatsWith1Redundancy(strategy)
689 652
690 const res = await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'video 2 server 2', privacy: VideoPrivacy.PRIVATE }) 653 const { uuid } = await servers[1].videos.upload({ attributes: { name: 'video 2 server 2', privacy: VideoPrivacy.PRIVATE } })
691 video2Server2UUID = res.body.video.uuid 654 video2Server2UUID = uuid
692 655
693 // Wait transcoding before federation 656 // Wait transcoding before federation
694 await waitJobs(servers) 657 await waitJobs(servers)
695 658
696 await updateVideo(servers[1].url, servers[1].accessToken, video2Server2UUID, { privacy: VideoPrivacy.PUBLIC }) 659 await servers[1].videos.update({ id: video2Server2UUID, attributes: { privacy: VideoPrivacy.PUBLIC } })
697 }) 660 })
698 661
699 it('Should cache video 2 webseeds on the first video', async function () { 662 it('Should cache video 2 webseeds on the first video', async function () {
@@ -725,8 +688,8 @@ describe('Test videos redundancy', function () {
725 688
726 await waitJobs(servers) 689 await waitJobs(servers)
727 690
728 killallServers([ servers[0] ]) 691 await killallServers([ servers[0] ])
729 await reRunServer(servers[0], { 692 await servers[0].run({
730 redundancy: { 693 redundancy: {
731 videos: { 694 videos: {
732 check_interval: '1 second', 695 check_interval: '1 second',
@@ -737,7 +700,7 @@ describe('Test videos redundancy', function () {
737 700
738 await waitJobs(servers) 701 await waitJobs(servers)
739 702
740 await checkVideoFilesWereRemoved(video1Server2UUID, servers[0].internalServerNumber, [ join('redundancy', 'hls') ]) 703 await checkVideoFilesWereRemoved(video1Server2UUID, servers[0], [ join('redundancy', 'hls') ])
741 }) 704 })
742 705
743 after(async function () { 706 after(async function () {
diff --git a/server/tests/api/search/search-activitypub-video-channels.ts b/server/tests/api/search/search-activitypub-video-channels.ts
index e83eb7171..426cbc8e1 100644
--- a/server/tests/api/search/search-activitypub-video-channels.ts
+++ b/server/tests/api/search/search-activitypub-video-channels.ts
@@ -1,69 +1,63 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2 2
3import * as chai from 'chai'
4import 'mocha' 3import 'mocha'
4import * as chai from 'chai'
5import { 5import {
6 addVideoChannel,
7 cleanupTests, 6 cleanupTests,
8 createUser, 7 createMultipleServers,
9 deleteVideoChannel, 8 PeerTubeServer,
10 flushAndRunMultipleServers, 9 SearchCommand,
11 getVideoChannelsList,
12 getVideoChannelVideos,
13 ServerInfo,
14 setAccessTokensToServers, 10 setAccessTokensToServers,
15 updateMyUser, 11 wait,
16 updateVideo, 12 waitJobs
17 updateVideoChannel, 13} from '@shared/extra-utils'
18 uploadVideo, 14import { VideoChannel } from '@shared/models'
19 userLogin,
20 wait
21} from '../../../../shared/extra-utils'
22import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
23import { VideoChannel } from '../../../../shared/models/videos'
24import { searchVideoChannel } from '../../../../shared/extra-utils/search/video-channels'
25 15
26const expect = chai.expect 16const expect = chai.expect
27 17
28describe('Test ActivityPub video channels search', function () { 18describe('Test ActivityPub video channels search', function () {
29 let servers: ServerInfo[] 19 let servers: PeerTubeServer[]
30 let userServer2Token: string 20 let userServer2Token: string
31 let videoServer2UUID: string 21 let videoServer2UUID: string
32 let channelIdServer2: number 22 let channelIdServer2: number
23 let command: SearchCommand
33 24
34 before(async function () { 25 before(async function () {
35 this.timeout(120000) 26 this.timeout(120000)
36 27
37 servers = await flushAndRunMultipleServers(2) 28 servers = await createMultipleServers(2)
38 29
39 await setAccessTokensToServers(servers) 30 await setAccessTokensToServers(servers)
40 31
41 { 32 {
42 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: 'user1_server1', password: 'password' }) 33 await servers[0].users.create({ username: 'user1_server1', password: 'password' })
43 const channel = { 34 const channel = {
44 name: 'channel1_server1', 35 name: 'channel1_server1',
45 displayName: 'Channel 1 server 1' 36 displayName: 'Channel 1 server 1'
46 } 37 }
47 await addVideoChannel(servers[0].url, servers[0].accessToken, channel) 38 await servers[0].channels.create({ attributes: channel })
48 } 39 }
49 40
50 { 41 {
51 const user = { username: 'user1_server2', password: 'password' } 42 const user = { username: 'user1_server2', password: 'password' }
52 await createUser({ url: servers[1].url, accessToken: servers[1].accessToken, username: user.username, password: user.password }) 43 await servers[1].users.create({ username: user.username, password: user.password })
53 userServer2Token = await userLogin(servers[1], user) 44 userServer2Token = await servers[1].login.getAccessToken(user)
54 45
55 const channel = { 46 const channel = {
56 name: 'channel1_server2', 47 name: 'channel1_server2',
57 displayName: 'Channel 1 server 2' 48 displayName: 'Channel 1 server 2'
58 } 49 }
59 const resChannel = await addVideoChannel(servers[1].url, userServer2Token, channel) 50 const created = await servers[1].channels.create({ token: userServer2Token, attributes: channel })
60 channelIdServer2 = resChannel.body.videoChannel.id 51 channelIdServer2 = created.id
61 52
62 const res = await uploadVideo(servers[1].url, userServer2Token, { name: 'video 1 server 2', channelId: channelIdServer2 }) 53 const attributes = { name: 'video 1 server 2', channelId: channelIdServer2 }
63 videoServer2UUID = res.body.video.uuid 54 const { uuid } = await servers[1].videos.upload({ token: userServer2Token, attributes })
55 videoServer2UUID = uuid
64 } 56 }
65 57
66 await waitJobs(servers) 58 await waitJobs(servers)
59
60 command = servers[0].search
67 }) 61 })
68 62
69 it('Should not find a remote video channel', async function () { 63 it('Should not find a remote video channel', async function () {
@@ -71,21 +65,21 @@ describe('Test ActivityPub video channels search', function () {
71 65
72 { 66 {
73 const search = 'http://localhost:' + servers[1].port + '/video-channels/channel1_server3' 67 const search = 'http://localhost:' + servers[1].port + '/video-channels/channel1_server3'
74 const res = await searchVideoChannel(servers[0].url, search, servers[0].accessToken) 68 const body = await command.searchChannels({ search, token: servers[0].accessToken })
75 69
76 expect(res.body.total).to.equal(0) 70 expect(body.total).to.equal(0)
77 expect(res.body.data).to.be.an('array') 71 expect(body.data).to.be.an('array')
78 expect(res.body.data).to.have.lengthOf(0) 72 expect(body.data).to.have.lengthOf(0)
79 } 73 }
80 74
81 { 75 {
82 // Without token 76 // Without token
83 const search = 'http://localhost:' + servers[1].port + '/video-channels/channel1_server2' 77 const search = 'http://localhost:' + servers[1].port + '/video-channels/channel1_server2'
84 const res = await searchVideoChannel(servers[0].url, search) 78 const body = await command.searchChannels({ search })
85 79
86 expect(res.body.total).to.equal(0) 80 expect(body.total).to.equal(0)
87 expect(res.body.data).to.be.an('array') 81 expect(body.data).to.be.an('array')
88 expect(res.body.data).to.have.lengthOf(0) 82 expect(body.data).to.have.lengthOf(0)
89 } 83 }
90 }) 84 })
91 85
@@ -96,13 +90,13 @@ describe('Test ActivityPub video channels search', function () {
96 ] 90 ]
97 91
98 for (const search of searches) { 92 for (const search of searches) {
99 const res = await searchVideoChannel(servers[0].url, search) 93 const body = await command.searchChannels({ search })
100 94
101 expect(res.body.total).to.equal(1) 95 expect(body.total).to.equal(1)
102 expect(res.body.data).to.be.an('array') 96 expect(body.data).to.be.an('array')
103 expect(res.body.data).to.have.lengthOf(1) 97 expect(body.data).to.have.lengthOf(1)
104 expect(res.body.data[0].name).to.equal('channel1_server1') 98 expect(body.data[0].name).to.equal('channel1_server1')
105 expect(res.body.data[0].displayName).to.equal('Channel 1 server 1') 99 expect(body.data[0].displayName).to.equal('Channel 1 server 1')
106 } 100 }
107 }) 101 })
108 102
@@ -110,13 +104,13 @@ describe('Test ActivityPub video channels search', function () {
110 const search = 'http://localhost:' + servers[0].port + '/c/channel1_server1' 104 const search = 'http://localhost:' + servers[0].port + '/c/channel1_server1'
111 105
112 for (const token of [ undefined, servers[0].accessToken ]) { 106 for (const token of [ undefined, servers[0].accessToken ]) {
113 const res = await searchVideoChannel(servers[0].url, search, token) 107 const body = await command.searchChannels({ search, token })
114 108
115 expect(res.body.total).to.equal(1) 109 expect(body.total).to.equal(1)
116 expect(res.body.data).to.be.an('array') 110 expect(body.data).to.be.an('array')
117 expect(res.body.data).to.have.lengthOf(1) 111 expect(body.data).to.have.lengthOf(1)
118 expect(res.body.data[0].name).to.equal('channel1_server1') 112 expect(body.data[0].name).to.equal('channel1_server1')
119 expect(res.body.data[0].displayName).to.equal('Channel 1 server 1') 113 expect(body.data[0].displayName).to.equal('Channel 1 server 1')
120 } 114 }
121 }) 115 })
122 116
@@ -129,23 +123,23 @@ describe('Test ActivityPub video channels search', function () {
129 ] 123 ]
130 124
131 for (const search of searches) { 125 for (const search of searches) {
132 const res = await searchVideoChannel(servers[0].url, search, servers[0].accessToken) 126 const body = await command.searchChannels({ search, token: servers[0].accessToken })
133 127
134 expect(res.body.total).to.equal(1) 128 expect(body.total).to.equal(1)
135 expect(res.body.data).to.be.an('array') 129 expect(body.data).to.be.an('array')
136 expect(res.body.data).to.have.lengthOf(1) 130 expect(body.data).to.have.lengthOf(1)
137 expect(res.body.data[0].name).to.equal('channel1_server2') 131 expect(body.data[0].name).to.equal('channel1_server2')
138 expect(res.body.data[0].displayName).to.equal('Channel 1 server 2') 132 expect(body.data[0].displayName).to.equal('Channel 1 server 2')
139 } 133 }
140 }) 134 })
141 135
142 it('Should not list this remote video channel', async function () { 136 it('Should not list this remote video channel', async function () {
143 const res = await getVideoChannelsList(servers[0].url, 0, 5) 137 const body = await servers[0].channels.list()
144 expect(res.body.total).to.equal(3) 138 expect(body.total).to.equal(3)
145 expect(res.body.data).to.have.lengthOf(3) 139 expect(body.data).to.have.lengthOf(3)
146 expect(res.body.data[0].name).to.equal('channel1_server1') 140 expect(body.data[0].name).to.equal('channel1_server1')
147 expect(res.body.data[1].name).to.equal('user1_server1_channel') 141 expect(body.data[1].name).to.equal('user1_server1_channel')
148 expect(res.body.data[2].name).to.equal('root_channel') 142 expect(body.data[2].name).to.equal('root_channel')
149 }) 143 })
150 144
151 it('Should list video channel videos of server 2 without token', async function () { 145 it('Should list video channel videos of server 2 without token', async function () {
@@ -153,34 +147,43 @@ describe('Test ActivityPub video channels search', function () {
153 147
154 await waitJobs(servers) 148 await waitJobs(servers)
155 149
156 const res = await getVideoChannelVideos(servers[0].url, null, 'channel1_server2@localhost:' + servers[1].port, 0, 5) 150 const { total, data } = await servers[0].videos.listByChannel({
157 expect(res.body.total).to.equal(0) 151 token: null,
158 expect(res.body.data).to.have.lengthOf(0) 152 handle: 'channel1_server2@localhost:' + servers[1].port
153 })
154 expect(total).to.equal(0)
155 expect(data).to.have.lengthOf(0)
159 }) 156 })
160 157
161 it('Should list video channel videos of server 2 with token', async function () { 158 it('Should list video channel videos of server 2 with token', async function () {
162 const res = await getVideoChannelVideos(servers[0].url, servers[0].accessToken, 'channel1_server2@localhost:' + servers[1].port, 0, 5) 159 const { total, data } = await servers[0].videos.listByChannel({
160 handle: 'channel1_server2@localhost:' + servers[1].port
161 })
163 162
164 expect(res.body.total).to.equal(1) 163 expect(total).to.equal(1)
165 expect(res.body.data[0].name).to.equal('video 1 server 2') 164 expect(data[0].name).to.equal('video 1 server 2')
166 }) 165 })
167 166
168 it('Should update video channel of server 2, and refresh it on server 1', async function () { 167 it('Should update video channel of server 2, and refresh it on server 1', async function () {
169 this.timeout(60000) 168 this.timeout(60000)
170 169
171 await updateVideoChannel(servers[1].url, userServer2Token, 'channel1_server2', { displayName: 'channel updated' }) 170 await servers[1].channels.update({
172 await updateMyUser({ url: servers[1].url, accessToken: userServer2Token, displayName: 'user updated' }) 171 token: userServer2Token,
172 channelName: 'channel1_server2',
173 attributes: { displayName: 'channel updated' }
174 })
175 await servers[1].users.updateMe({ token: userServer2Token, displayName: 'user updated' })
173 176
174 await waitJobs(servers) 177 await waitJobs(servers)
175 // Expire video channel 178 // Expire video channel
176 await wait(10000) 179 await wait(10000)
177 180
178 const search = 'http://localhost:' + servers[1].port + '/video-channels/channel1_server2' 181 const search = 'http://localhost:' + servers[1].port + '/video-channels/channel1_server2'
179 const res = await searchVideoChannel(servers[0].url, search, servers[0].accessToken) 182 const body = await command.searchChannels({ search, token: servers[0].accessToken })
180 expect(res.body.total).to.equal(1) 183 expect(body.total).to.equal(1)
181 expect(res.body.data).to.have.lengthOf(1) 184 expect(body.data).to.have.lengthOf(1)
182 185
183 const videoChannel: VideoChannel = res.body.data[0] 186 const videoChannel: VideoChannel = body.data[0]
184 expect(videoChannel.displayName).to.equal('channel updated') 187 expect(videoChannel.displayName).to.equal('channel updated')
185 188
186 // We don't return the owner account for now 189 // We don't return the owner account for now
@@ -190,8 +193,8 @@ describe('Test ActivityPub video channels search', function () {
190 it('Should update and add a video on server 2, and update it on server 1 after a search', async function () { 193 it('Should update and add a video on server 2, and update it on server 1 after a search', async function () {
191 this.timeout(60000) 194 this.timeout(60000)
192 195
193 await updateVideo(servers[1].url, userServer2Token, videoServer2UUID, { name: 'video 1 updated' }) 196 await servers[1].videos.update({ token: userServer2Token, id: videoServer2UUID, attributes: { name: 'video 1 updated' } })
194 await uploadVideo(servers[1].url, userServer2Token, { name: 'video 2 server 2', channelId: channelIdServer2 }) 197 await servers[1].videos.upload({ token: userServer2Token, attributes: { name: 'video 2 server 2', channelId: channelIdServer2 } })
195 198
196 await waitJobs(servers) 199 await waitJobs(servers)
197 200
@@ -199,31 +202,31 @@ describe('Test ActivityPub video channels search', function () {
199 await wait(10000) 202 await wait(10000)
200 203
201 const search = 'http://localhost:' + servers[1].port + '/video-channels/channel1_server2' 204 const search = 'http://localhost:' + servers[1].port + '/video-channels/channel1_server2'
202 await searchVideoChannel(servers[0].url, search, servers[0].accessToken) 205 await command.searchChannels({ search, token: servers[0].accessToken })
203 206
204 await waitJobs(servers) 207 await waitJobs(servers)
205 208
206 const videoChannelName = 'channel1_server2@localhost:' + servers[1].port 209 const handle = 'channel1_server2@localhost:' + servers[1].port
207 const res = await getVideoChannelVideos(servers[0].url, servers[0].accessToken, videoChannelName, 0, 5, '-createdAt') 210 const { total, data } = await servers[0].videos.listByChannel({ handle, sort: '-createdAt' })
208 211
209 expect(res.body.total).to.equal(2) 212 expect(total).to.equal(2)
210 expect(res.body.data[0].name).to.equal('video 2 server 2') 213 expect(data[0].name).to.equal('video 2 server 2')
211 expect(res.body.data[1].name).to.equal('video 1 updated') 214 expect(data[1].name).to.equal('video 1 updated')
212 }) 215 })
213 216
214 it('Should delete video channel of server 2, and delete it on server 1', async function () { 217 it('Should delete video channel of server 2, and delete it on server 1', async function () {
215 this.timeout(60000) 218 this.timeout(60000)
216 219
217 await deleteVideoChannel(servers[1].url, userServer2Token, 'channel1_server2') 220 await servers[1].channels.delete({ token: userServer2Token, channelName: 'channel1_server2' })
218 221
219 await waitJobs(servers) 222 await waitJobs(servers)
220 // Expire video 223 // Expire video
221 await wait(10000) 224 await wait(10000)
222 225
223 const search = 'http://localhost:' + servers[1].port + '/video-channels/channel1_server2' 226 const search = 'http://localhost:' + servers[1].port + '/video-channels/channel1_server2'
224 const res = await searchVideoChannel(servers[0].url, search, servers[0].accessToken) 227 const body = await command.searchChannels({ search, token: servers[0].accessToken })
225 expect(res.body.total).to.equal(0) 228 expect(body.total).to.equal(0)
226 expect(res.body.data).to.have.lengthOf(0) 229 expect(body.data).to.have.lengthOf(0)
227 }) 230 })
228 231
229 after(async function () { 232 after(async function () {
diff --git a/server/tests/api/search/search-activitypub-video-playlists.ts b/server/tests/api/search/search-activitypub-video-playlists.ts
index 4c08e9548..33ca7be12 100644
--- a/server/tests/api/search/search-activitypub-video-playlists.ts
+++ b/server/tests/api/search/search-activitypub-video-playlists.ts
@@ -3,113 +3,102 @@
3import 'mocha' 3import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5import { 5import {
6 addVideoInPlaylist,
7 cleanupTests, 6 cleanupTests,
8 createVideoPlaylist, 7 createMultipleServers,
9 deleteVideoPlaylist, 8 PeerTubeServer,
10 flushAndRunMultipleServers, 9 SearchCommand,
11 getVideoPlaylistsList,
12 searchVideoPlaylists,
13 ServerInfo,
14 setAccessTokensToServers, 10 setAccessTokensToServers,
15 setDefaultVideoChannel, 11 setDefaultVideoChannel,
16 uploadVideoAndGetId, 12 wait,
17 wait 13 waitJobs
18} from '../../../../shared/extra-utils' 14} from '@shared/extra-utils'
19import { waitJobs } from '../../../../shared/extra-utils/server/jobs' 15import { VideoPlaylistPrivacy } from '@shared/models'
20import { VideoPlaylist, VideoPlaylistPrivacy } from '../../../../shared/models/videos'
21 16
22const expect = chai.expect 17const expect = chai.expect
23 18
24describe('Test ActivityPub playlists search', function () { 19describe('Test ActivityPub playlists search', function () {
25 let servers: ServerInfo[] 20 let servers: PeerTubeServer[]
26 let playlistServer1UUID: string 21 let playlistServer1UUID: string
27 let playlistServer2UUID: string 22 let playlistServer2UUID: string
28 let video2Server2: string 23 let video2Server2: string
29 24
25 let command: SearchCommand
26
30 before(async function () { 27 before(async function () {
31 this.timeout(120000) 28 this.timeout(120000)
32 29
33 servers = await flushAndRunMultipleServers(2) 30 servers = await createMultipleServers(2)
34 31
35 await setAccessTokensToServers(servers) 32 await setAccessTokensToServers(servers)
36 await setDefaultVideoChannel(servers) 33 await setDefaultVideoChannel(servers)
37 34
38 { 35 {
39 const video1 = (await uploadVideoAndGetId({ server: servers[0], videoName: 'video 1' })).uuid 36 const video1 = (await servers[0].videos.quickUpload({ name: 'video 1' })).uuid
40 const video2 = (await uploadVideoAndGetId({ server: servers[0], videoName: 'video 2' })).uuid 37 const video2 = (await servers[0].videos.quickUpload({ name: 'video 2' })).uuid
41 38
42 const attributes = { 39 const attributes = {
43 displayName: 'playlist 1 on server 1', 40 displayName: 'playlist 1 on server 1',
44 privacy: VideoPlaylistPrivacy.PUBLIC, 41 privacy: VideoPlaylistPrivacy.PUBLIC,
45 videoChannelId: servers[0].videoChannel.id 42 videoChannelId: servers[0].store.channel.id
46 } 43 }
47 const res = await createVideoPlaylist({ url: servers[0].url, token: servers[0].accessToken, playlistAttrs: attributes }) 44 const created = await servers[0].playlists.create({ attributes })
48 playlistServer1UUID = res.body.videoPlaylist.uuid 45 playlistServer1UUID = created.uuid
49 46
50 for (const videoId of [ video1, video2 ]) { 47 for (const videoId of [ video1, video2 ]) {
51 await addVideoInPlaylist({ 48 await servers[0].playlists.addElement({ playlistId: playlistServer1UUID, attributes: { videoId } })
52 url: servers[0].url,
53 token: servers[0].accessToken,
54 playlistId: playlistServer1UUID,
55 elementAttrs: { videoId }
56 })
57 } 49 }
58 } 50 }
59 51
60 { 52 {
61 const videoId = (await uploadVideoAndGetId({ server: servers[1], videoName: 'video 1' })).uuid 53 const videoId = (await servers[1].videos.quickUpload({ name: 'video 1' })).uuid
62 video2Server2 = (await uploadVideoAndGetId({ server: servers[1], videoName: 'video 2' })).uuid 54 video2Server2 = (await servers[1].videos.quickUpload({ name: 'video 2' })).uuid
63 55
64 const attributes = { 56 const attributes = {
65 displayName: 'playlist 1 on server 2', 57 displayName: 'playlist 1 on server 2',
66 privacy: VideoPlaylistPrivacy.PUBLIC, 58 privacy: VideoPlaylistPrivacy.PUBLIC,
67 videoChannelId: servers[1].videoChannel.id 59 videoChannelId: servers[1].store.channel.id
68 } 60 }
69 const res = await createVideoPlaylist({ url: servers[1].url, token: servers[1].accessToken, playlistAttrs: attributes }) 61 const created = await servers[1].playlists.create({ attributes })
70 playlistServer2UUID = res.body.videoPlaylist.uuid 62 playlistServer2UUID = created.uuid
71 63
72 await addVideoInPlaylist({ 64 await servers[1].playlists.addElement({ playlistId: playlistServer2UUID, attributes: { videoId } })
73 url: servers[1].url,
74 token: servers[1].accessToken,
75 playlistId: playlistServer2UUID,
76 elementAttrs: { videoId }
77 })
78 } 65 }
79 66
80 await waitJobs(servers) 67 await waitJobs(servers)
68
69 command = servers[0].search
81 }) 70 })
82 71
83 it('Should not find a remote playlist', async function () { 72 it('Should not find a remote playlist', async function () {
84 { 73 {
85 const search = 'http://localhost:' + servers[1].port + '/video-playlists/43' 74 const search = 'http://localhost:' + servers[1].port + '/video-playlists/43'
86 const res = await searchVideoPlaylists(servers[0].url, search, servers[0].accessToken) 75 const body = await command.searchPlaylists({ search, token: servers[0].accessToken })
87 76
88 expect(res.body.total).to.equal(0) 77 expect(body.total).to.equal(0)
89 expect(res.body.data).to.be.an('array') 78 expect(body.data).to.be.an('array')
90 expect(res.body.data).to.have.lengthOf(0) 79 expect(body.data).to.have.lengthOf(0)
91 } 80 }
92 81
93 { 82 {
94 // Without token 83 // Without token
95 const search = 'http://localhost:' + servers[1].port + '/video-playlists/' + playlistServer2UUID 84 const search = 'http://localhost:' + servers[1].port + '/video-playlists/' + playlistServer2UUID
96 const res = await searchVideoPlaylists(servers[0].url, search) 85 const body = await command.searchPlaylists({ search })
97 86
98 expect(res.body.total).to.equal(0) 87 expect(body.total).to.equal(0)
99 expect(res.body.data).to.be.an('array') 88 expect(body.data).to.be.an('array')
100 expect(res.body.data).to.have.lengthOf(0) 89 expect(body.data).to.have.lengthOf(0)
101 } 90 }
102 }) 91 })
103 92
104 it('Should search a local playlist', async function () { 93 it('Should search a local playlist', async function () {
105 const search = 'http://localhost:' + servers[0].port + '/video-playlists/' + playlistServer1UUID 94 const search = 'http://localhost:' + servers[0].port + '/video-playlists/' + playlistServer1UUID
106 const res = await searchVideoPlaylists(servers[0].url, search) 95 const body = await command.searchPlaylists({ search })
107 96
108 expect(res.body.total).to.equal(1) 97 expect(body.total).to.equal(1)
109 expect(res.body.data).to.be.an('array') 98 expect(body.data).to.be.an('array')
110 expect(res.body.data).to.have.lengthOf(1) 99 expect(body.data).to.have.lengthOf(1)
111 expect(res.body.data[0].displayName).to.equal('playlist 1 on server 1') 100 expect(body.data[0].displayName).to.equal('playlist 1 on server 1')
112 expect(res.body.data[0].videosLength).to.equal(2) 101 expect(body.data[0].videosLength).to.equal(2)
113 }) 102 })
114 103
115 it('Should search a local playlist with an alternative URL', async function () { 104 it('Should search a local playlist with an alternative URL', async function () {
@@ -120,13 +109,13 @@ describe('Test ActivityPub playlists search', function () {
120 109
121 for (const search of searches) { 110 for (const search of searches) {
122 for (const token of [ undefined, servers[0].accessToken ]) { 111 for (const token of [ undefined, servers[0].accessToken ]) {
123 const res = await searchVideoPlaylists(servers[0].url, search, token) 112 const body = await command.searchPlaylists({ search, token })
124 113
125 expect(res.body.total).to.equal(1) 114 expect(body.total).to.equal(1)
126 expect(res.body.data).to.be.an('array') 115 expect(body.data).to.be.an('array')
127 expect(res.body.data).to.have.lengthOf(1) 116 expect(body.data).to.have.lengthOf(1)
128 expect(res.body.data[0].displayName).to.equal('playlist 1 on server 1') 117 expect(body.data[0].displayName).to.equal('playlist 1 on server 1')
129 expect(res.body.data[0].videosLength).to.equal(2) 118 expect(body.data[0].videosLength).to.equal(2)
130 } 119 }
131 } 120 }
132 }) 121 })
@@ -139,32 +128,27 @@ describe('Test ActivityPub playlists search', function () {
139 ] 128 ]
140 129
141 for (const search of searches) { 130 for (const search of searches) {
142 const res = await searchVideoPlaylists(servers[0].url, search, servers[0].accessToken) 131 const body = await command.searchPlaylists({ search, token: servers[0].accessToken })
143 132
144 expect(res.body.total).to.equal(1) 133 expect(body.total).to.equal(1)
145 expect(res.body.data).to.be.an('array') 134 expect(body.data).to.be.an('array')
146 expect(res.body.data).to.have.lengthOf(1) 135 expect(body.data).to.have.lengthOf(1)
147 expect(res.body.data[0].displayName).to.equal('playlist 1 on server 2') 136 expect(body.data[0].displayName).to.equal('playlist 1 on server 2')
148 expect(res.body.data[0].videosLength).to.equal(1) 137 expect(body.data[0].videosLength).to.equal(1)
149 } 138 }
150 }) 139 })
151 140
152 it('Should not list this remote playlist', async function () { 141 it('Should not list this remote playlist', async function () {
153 const res = await getVideoPlaylistsList(servers[0].url, 0, 10) 142 const body = await servers[0].playlists.list({ start: 0, count: 10 })
154 expect(res.body.total).to.equal(1) 143 expect(body.total).to.equal(1)
155 expect(res.body.data).to.have.lengthOf(1) 144 expect(body.data).to.have.lengthOf(1)
156 expect(res.body.data[0].displayName).to.equal('playlist 1 on server 1') 145 expect(body.data[0].displayName).to.equal('playlist 1 on server 1')
157 }) 146 })
158 147
159 it('Should update the playlist of server 2, and refresh it on server 1', async function () { 148 it('Should update the playlist of server 2, and refresh it on server 1', async function () {
160 this.timeout(60000) 149 this.timeout(60000)
161 150
162 await addVideoInPlaylist({ 151 await servers[1].playlists.addElement({ playlistId: playlistServer2UUID, attributes: { videoId: video2Server2 } })
163 url: servers[1].url,
164 token: servers[1].accessToken,
165 playlistId: playlistServer2UUID,
166 elementAttrs: { videoId: video2Server2 }
167 })
168 152
169 await waitJobs(servers) 153 await waitJobs(servers)
170 // Expire playlist 154 // Expire playlist
@@ -172,23 +156,23 @@ describe('Test ActivityPub playlists search', function () {
172 156
173 // Will run refresh async 157 // Will run refresh async
174 const search = 'http://localhost:' + servers[1].port + '/video-playlists/' + playlistServer2UUID 158 const search = 'http://localhost:' + servers[1].port + '/video-playlists/' + playlistServer2UUID
175 await searchVideoPlaylists(servers[0].url, search, servers[0].accessToken) 159 await command.searchPlaylists({ search, token: servers[0].accessToken })
176 160
177 // Wait refresh 161 // Wait refresh
178 await wait(5000) 162 await wait(5000)
179 163
180 const res = await searchVideoPlaylists(servers[0].url, search, servers[0].accessToken) 164 const body = await command.searchPlaylists({ search, token: servers[0].accessToken })
181 expect(res.body.total).to.equal(1) 165 expect(body.total).to.equal(1)
182 expect(res.body.data).to.have.lengthOf(1) 166 expect(body.data).to.have.lengthOf(1)
183 167
184 const playlist: VideoPlaylist = res.body.data[0] 168 const playlist = body.data[0]
185 expect(playlist.videosLength).to.equal(2) 169 expect(playlist.videosLength).to.equal(2)
186 }) 170 })
187 171
188 it('Should delete playlist of server 2, and delete it on server 1', async function () { 172 it('Should delete playlist of server 2, and delete it on server 1', async function () {
189 this.timeout(60000) 173 this.timeout(60000)
190 174
191 await deleteVideoPlaylist(servers[1].url, servers[1].accessToken, playlistServer2UUID) 175 await servers[1].playlists.delete({ playlistId: playlistServer2UUID })
192 176
193 await waitJobs(servers) 177 await waitJobs(servers)
194 // Expiration 178 // Expiration
@@ -196,14 +180,14 @@ describe('Test ActivityPub playlists search', function () {
196 180
197 // Will run refresh async 181 // Will run refresh async
198 const search = 'http://localhost:' + servers[1].port + '/video-playlists/' + playlistServer2UUID 182 const search = 'http://localhost:' + servers[1].port + '/video-playlists/' + playlistServer2UUID
199 await searchVideoPlaylists(servers[0].url, search, servers[0].accessToken) 183 await command.searchPlaylists({ search, token: servers[0].accessToken })
200 184
201 // Wait refresh 185 // Wait refresh
202 await wait(5000) 186 await wait(5000)
203 187
204 const res = await searchVideoPlaylists(servers[0].url, search, servers[0].accessToken) 188 const body = await command.searchPlaylists({ search, token: servers[0].accessToken })
205 expect(res.body.total).to.equal(0) 189 expect(body.total).to.equal(0)
206 expect(res.body.data).to.have.lengthOf(0) 190 expect(body.data).to.have.lengthOf(0)
207 }) 191 })
208 192
209 after(async function () { 193 after(async function () {
diff --git a/server/tests/api/search/search-activitypub-videos.ts b/server/tests/api/search/search-activitypub-videos.ts
index e9b4978da..b3cfcacca 100644
--- a/server/tests/api/search/search-activitypub-videos.ts
+++ b/server/tests/api/search/search-activitypub-videos.ts
@@ -1,92 +1,90 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2 2
3import * as chai from 'chai'
4import 'mocha' 3import 'mocha'
4import * as chai from 'chai'
5import { 5import {
6 addVideoChannel,
7 cleanupTests, 6 cleanupTests,
8 flushAndRunMultipleServers, 7 createMultipleServers,
9 getVideosList, 8 PeerTubeServer,
10 removeVideo, 9 SearchCommand,
11 searchVideo,
12 searchVideoWithToken,
13 ServerInfo,
14 setAccessTokensToServers, 10 setAccessTokensToServers,
15 updateVideo, 11 wait,
16 uploadVideo, 12 waitJobs
17 wait 13} from '@shared/extra-utils'
18} from '../../../../shared/extra-utils' 14import { VideoPrivacy } from '@shared/models'
19import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
20import { Video, VideoPrivacy } from '../../../../shared/models/videos'
21 15
22const expect = chai.expect 16const expect = chai.expect
23 17
24describe('Test ActivityPub videos search', function () { 18describe('Test ActivityPub videos search', function () {
25 let servers: ServerInfo[] 19 let servers: PeerTubeServer[]
26 let videoServer1UUID: string 20 let videoServer1UUID: string
27 let videoServer2UUID: string 21 let videoServer2UUID: string
28 22
23 let command: SearchCommand
24
29 before(async function () { 25 before(async function () {
30 this.timeout(120000) 26 this.timeout(120000)
31 27
32 servers = await flushAndRunMultipleServers(2) 28 servers = await createMultipleServers(2)
33 29
34 await setAccessTokensToServers(servers) 30 await setAccessTokensToServers(servers)
35 31
36 { 32 {
37 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video 1 on server 1' }) 33 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video 1 on server 1' } })
38 videoServer1UUID = res.body.video.uuid 34 videoServer1UUID = uuid
39 } 35 }
40 36
41 { 37 {
42 const res = await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'video 1 on server 2' }) 38 const { uuid } = await servers[1].videos.upload({ attributes: { name: 'video 1 on server 2' } })
43 videoServer2UUID = res.body.video.uuid 39 videoServer2UUID = uuid
44 } 40 }
45 41
46 await waitJobs(servers) 42 await waitJobs(servers)
43
44 command = servers[0].search
47 }) 45 })
48 46
49 it('Should not find a remote video', async function () { 47 it('Should not find a remote video', async function () {
50 { 48 {
51 const search = 'http://localhost:' + servers[1].port + '/videos/watch/43' 49 const search = 'http://localhost:' + servers[1].port + '/videos/watch/43'
52 const res = await searchVideoWithToken(servers[0].url, search, servers[0].accessToken) 50 const body = await command.searchVideos({ search, token: servers[0].accessToken })
53 51
54 expect(res.body.total).to.equal(0) 52 expect(body.total).to.equal(0)
55 expect(res.body.data).to.be.an('array') 53 expect(body.data).to.be.an('array')
56 expect(res.body.data).to.have.lengthOf(0) 54 expect(body.data).to.have.lengthOf(0)
57 } 55 }
58 56
59 { 57 {
60 // Without token 58 // Without token
61 const search = 'http://localhost:' + servers[1].port + '/videos/watch/' + videoServer2UUID 59 const search = 'http://localhost:' + servers[1].port + '/videos/watch/' + videoServer2UUID
62 const res = await searchVideo(servers[0].url, search) 60 const body = await command.searchVideos({ search })
63 61
64 expect(res.body.total).to.equal(0) 62 expect(body.total).to.equal(0)
65 expect(res.body.data).to.be.an('array') 63 expect(body.data).to.be.an('array')
66 expect(res.body.data).to.have.lengthOf(0) 64 expect(body.data).to.have.lengthOf(0)
67 } 65 }
68 }) 66 })
69 67
70 it('Should search a local video', async function () { 68 it('Should search a local video', async function () {
71 const search = 'http://localhost:' + servers[0].port + '/videos/watch/' + videoServer1UUID 69 const search = 'http://localhost:' + servers[0].port + '/videos/watch/' + videoServer1UUID
72 const res = await searchVideo(servers[0].url, search) 70 const body = await command.searchVideos({ search })
73 71
74 expect(res.body.total).to.equal(1) 72 expect(body.total).to.equal(1)
75 expect(res.body.data).to.be.an('array') 73 expect(body.data).to.be.an('array')
76 expect(res.body.data).to.have.lengthOf(1) 74 expect(body.data).to.have.lengthOf(1)
77 expect(res.body.data[0].name).to.equal('video 1 on server 1') 75 expect(body.data[0].name).to.equal('video 1 on server 1')
78 }) 76 })
79 77
80 it('Should search a local video with an alternative URL', async function () { 78 it('Should search a local video with an alternative URL', async function () {
81 const search = 'http://localhost:' + servers[0].port + '/w/' + videoServer1UUID 79 const search = 'http://localhost:' + servers[0].port + '/w/' + videoServer1UUID
82 const res1 = await searchVideo(servers[0].url, search) 80 const body1 = await command.searchVideos({ search })
83 const res2 = await searchVideoWithToken(servers[0].url, search, servers[0].accessToken) 81 const body2 = await command.searchVideos({ search, token: servers[0].accessToken })
84 82
85 for (const res of [ res1, res2 ]) { 83 for (const body of [ body1, body2 ]) {
86 expect(res.body.total).to.equal(1) 84 expect(body.total).to.equal(1)
87 expect(res.body.data).to.be.an('array') 85 expect(body.data).to.be.an('array')
88 expect(res.body.data).to.have.lengthOf(1) 86 expect(body.data).to.have.lengthOf(1)
89 expect(res.body.data[0].name).to.equal('video 1 on server 1') 87 expect(body.data[0].name).to.equal('video 1 on server 1')
90 } 88 }
91 }) 89 })
92 90
@@ -97,20 +95,20 @@ describe('Test ActivityPub videos search', function () {
97 ] 95 ]
98 96
99 for (const search of searches) { 97 for (const search of searches) {
100 const res = await searchVideoWithToken(servers[0].url, search, servers[0].accessToken) 98 const body = await command.searchVideos({ search, token: servers[0].accessToken })
101 99
102 expect(res.body.total).to.equal(1) 100 expect(body.total).to.equal(1)
103 expect(res.body.data).to.be.an('array') 101 expect(body.data).to.be.an('array')
104 expect(res.body.data).to.have.lengthOf(1) 102 expect(body.data).to.have.lengthOf(1)
105 expect(res.body.data[0].name).to.equal('video 1 on server 2') 103 expect(body.data[0].name).to.equal('video 1 on server 2')
106 } 104 }
107 }) 105 })
108 106
109 it('Should not list this remote video', async function () { 107 it('Should not list this remote video', async function () {
110 const res = await getVideosList(servers[0].url) 108 const { total, data } = await servers[0].videos.list()
111 expect(res.body.total).to.equal(1) 109 expect(total).to.equal(1)
112 expect(res.body.data).to.have.lengthOf(1) 110 expect(data).to.have.lengthOf(1)
113 expect(res.body.data[0].name).to.equal('video 1 on server 1') 111 expect(data[0].name).to.equal('video 1 on server 1')
114 }) 112 })
115 113
116 it('Should update video of server 2, and refresh it on server 1', async function () { 114 it('Should update video of server 2, and refresh it on server 1', async function () {
@@ -120,8 +118,8 @@ describe('Test ActivityPub videos search', function () {
120 name: 'super_channel', 118 name: 'super_channel',
121 displayName: 'super channel' 119 displayName: 'super channel'
122 } 120 }
123 const resChannel = await addVideoChannel(servers[1].url, servers[1].accessToken, channelAttributes) 121 const created = await servers[1].channels.create({ attributes: channelAttributes })
124 const videoChannelId = resChannel.body.videoChannel.id 122 const videoChannelId = created.id
125 123
126 const attributes = { 124 const attributes = {
127 name: 'updated', 125 name: 'updated',
@@ -129,7 +127,7 @@ describe('Test ActivityPub videos search', function () {
129 privacy: VideoPrivacy.UNLISTED, 127 privacy: VideoPrivacy.UNLISTED,
130 channelId: videoChannelId 128 channelId: videoChannelId
131 } 129 }
132 await updateVideo(servers[1].url, servers[1].accessToken, videoServer2UUID, attributes) 130 await servers[1].videos.update({ id: videoServer2UUID, attributes })
133 131
134 await waitJobs(servers) 132 await waitJobs(servers)
135 // Expire video 133 // Expire video
@@ -137,16 +135,16 @@ describe('Test ActivityPub videos search', function () {
137 135
138 // Will run refresh async 136 // Will run refresh async
139 const search = 'http://localhost:' + servers[1].port + '/videos/watch/' + videoServer2UUID 137 const search = 'http://localhost:' + servers[1].port + '/videos/watch/' + videoServer2UUID
140 await searchVideoWithToken(servers[0].url, search, servers[0].accessToken) 138 await command.searchVideos({ search, token: servers[0].accessToken })
141 139
142 // Wait refresh 140 // Wait refresh
143 await wait(5000) 141 await wait(5000)
144 142
145 const res = await searchVideoWithToken(servers[0].url, search, servers[0].accessToken) 143 const body = await command.searchVideos({ search, token: servers[0].accessToken })
146 expect(res.body.total).to.equal(1) 144 expect(body.total).to.equal(1)
147 expect(res.body.data).to.have.lengthOf(1) 145 expect(body.data).to.have.lengthOf(1)
148 146
149 const video: Video = res.body.data[0] 147 const video = body.data[0]
150 expect(video.name).to.equal('updated') 148 expect(video.name).to.equal('updated')
151 expect(video.channel.name).to.equal('super_channel') 149 expect(video.channel.name).to.equal('super_channel')
152 expect(video.privacy.id).to.equal(VideoPrivacy.UNLISTED) 150 expect(video.privacy.id).to.equal(VideoPrivacy.UNLISTED)
@@ -155,7 +153,7 @@ describe('Test ActivityPub videos search', function () {
155 it('Should delete video of server 2, and delete it on server 1', async function () { 153 it('Should delete video of server 2, and delete it on server 1', async function () {
156 this.timeout(120000) 154 this.timeout(120000)
157 155
158 await removeVideo(servers[1].url, servers[1].accessToken, videoServer2UUID) 156 await servers[1].videos.remove({ id: videoServer2UUID })
159 157
160 await waitJobs(servers) 158 await waitJobs(servers)
161 // Expire video 159 // Expire video
@@ -163,14 +161,14 @@ describe('Test ActivityPub videos search', function () {
163 161
164 // Will run refresh async 162 // Will run refresh async
165 const search = 'http://localhost:' + servers[1].port + '/videos/watch/' + videoServer2UUID 163 const search = 'http://localhost:' + servers[1].port + '/videos/watch/' + videoServer2UUID
166 await searchVideoWithToken(servers[0].url, search, servers[0].accessToken) 164 await command.searchVideos({ search, token: servers[0].accessToken })
167 165
168 // Wait refresh 166 // Wait refresh
169 await wait(5000) 167 await wait(5000)
170 168
171 const res = await searchVideoWithToken(servers[0].url, search, servers[0].accessToken) 169 const body = await command.searchVideos({ search, token: servers[0].accessToken })
172 expect(res.body.total).to.equal(0) 170 expect(body.total).to.equal(0)
173 expect(res.body.data).to.have.lengthOf(0) 171 expect(body.data).to.have.lengthOf(0)
174 }) 172 })
175 173
176 after(async function () { 174 after(async function () {
diff --git a/server/tests/api/search/search-channels.ts b/server/tests/api/search/search-channels.ts
index daca2aebe..4da2d0ece 100644
--- a/server/tests/api/search/search-channels.ts
+++ b/server/tests/api/search/search-channels.ts
@@ -2,44 +2,39 @@
2 2
3import 'mocha' 3import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5import { searchVideoChannel, advancedVideoChannelSearch } from '@shared/extra-utils/search/video-channels' 5import { cleanupTests, createSingleServer, PeerTubeServer, SearchCommand, setAccessTokensToServers } from '@shared/extra-utils'
6import {
7 addVideoChannel,
8 cleanupTests,
9 createUser,
10 flushAndRunServer,
11 ServerInfo,
12 setAccessTokensToServers
13} from '../../../../shared/extra-utils'
14import { VideoChannel } from '@shared/models' 6import { VideoChannel } from '@shared/models'
15 7
16const expect = chai.expect 8const expect = chai.expect
17 9
18describe('Test channels search', function () { 10describe('Test channels search', function () {
19 let server: ServerInfo = null 11 let server: PeerTubeServer = null
12 let command: SearchCommand
20 13
21 before(async function () { 14 before(async function () {
22 this.timeout(30000) 15 this.timeout(30000)
23 16
24 server = await flushAndRunServer(1) 17 server = await createSingleServer(1)
25 18
26 await setAccessTokensToServers([ server ]) 19 await setAccessTokensToServers([ server ])
27 20
28 { 21 {
29 await createUser({ url: server.url, accessToken: server.accessToken, username: 'user1', password: 'password' }) 22 await server.users.create({ username: 'user1', password: 'password' })
30 const channel = { 23 const channel = {
31 name: 'squall_channel', 24 name: 'squall_channel',
32 displayName: 'Squall channel' 25 displayName: 'Squall channel'
33 } 26 }
34 await addVideoChannel(server.url, server.accessToken, channel) 27 await server.channels.create({ attributes: channel })
35 } 28 }
29
30 command = server.search
36 }) 31 })
37 32
38 it('Should make a simple search and not have results', async function () { 33 it('Should make a simple search and not have results', async function () {
39 const res = await searchVideoChannel(server.url, 'abc') 34 const body = await command.searchChannels({ search: 'abc' })
40 35
41 expect(res.body.total).to.equal(0) 36 expect(body.total).to.equal(0)
42 expect(res.body.data).to.have.lengthOf(0) 37 expect(body.data).to.have.lengthOf(0)
43 }) 38 })
44 39
45 it('Should make a search and have results', async function () { 40 it('Should make a search and have results', async function () {
@@ -49,11 +44,11 @@ describe('Test channels search', function () {
49 start: 0, 44 start: 0,
50 count: 1 45 count: 1
51 } 46 }
52 const res = await advancedVideoChannelSearch(server.url, search) 47 const body = await command.advancedChannelSearch({ search })
53 expect(res.body.total).to.equal(1) 48 expect(body.total).to.equal(1)
54 expect(res.body.data).to.have.lengthOf(1) 49 expect(body.data).to.have.lengthOf(1)
55 50
56 const channel: VideoChannel = res.body.data[0] 51 const channel: VideoChannel = body.data[0]
57 expect(channel.name).to.equal('squall_channel') 52 expect(channel.name).to.equal('squall_channel')
58 expect(channel.displayName).to.equal('Squall channel') 53 expect(channel.displayName).to.equal('Squall channel')
59 } 54 }
@@ -65,11 +60,9 @@ describe('Test channels search', function () {
65 count: 1 60 count: 1
66 } 61 }
67 62
68 const res = await advancedVideoChannelSearch(server.url, search) 63 const body = await command.advancedChannelSearch({ search })
69 64 expect(body.total).to.equal(1)
70 expect(res.body.total).to.equal(1) 65 expect(body.data).to.have.lengthOf(0)
71
72 expect(res.body.data).to.have.lengthOf(0)
73 } 66 }
74 }) 67 })
75 68
diff --git a/server/tests/api/search/search-index.ts b/server/tests/api/search/search-index.ts
index 00f79232a..feb35411f 100644
--- a/server/tests/api/search/search-index.ts
+++ b/server/tests/api/search/search-index.ts
@@ -2,36 +2,27 @@
2 2
3import 'mocha' 3import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5import { advancedVideoChannelSearch, searchVideoChannel } from '@shared/extra-utils/search/video-channels' 5import { cleanupTests, createSingleServer, PeerTubeServer, SearchCommand, setAccessTokensToServers } from '@shared/extra-utils'
6import { Video, VideoChannel, VideoPlaylist, VideoPlaylistPrivacy, VideoPlaylistType, VideosSearchQuery } from '@shared/models' 6import { BooleanBothQuery, VideoPlaylistPrivacy, VideoPlaylistType, VideosSearchQuery } from '@shared/models'
7import {
8 advancedVideoPlaylistSearch,
9 advancedVideosSearch,
10 cleanupTests,
11 flushAndRunServer,
12 immutableAssign,
13 searchVideo,
14 searchVideoPlaylists,
15 ServerInfo,
16 setAccessTokensToServers,
17 updateCustomSubConfig,
18 uploadVideo
19} from '../../../../shared/extra-utils'
20 7
21const expect = chai.expect 8const expect = chai.expect
22 9
23describe('Test videos search', function () { 10describe('Test videos search', function () {
24 let server: ServerInfo = null
25 const localVideoName = 'local video' + new Date().toISOString() 11 const localVideoName = 'local video' + new Date().toISOString()
26 12
13 let server: PeerTubeServer = null
14 let command: SearchCommand
15
27 before(async function () { 16 before(async function () {
28 this.timeout(30000) 17 this.timeout(30000)
29 18
30 server = await flushAndRunServer(1) 19 server = await createSingleServer(1)
31 20
32 await setAccessTokensToServers([ server ]) 21 await setAccessTokensToServers([ server ])
33 22
34 await uploadVideo(server.url, server.accessToken, { name: localVideoName }) 23 await server.videos.upload({ attributes: { name: localVideoName } })
24
25 command = server.search
35 }) 26 })
36 27
37 describe('Default search', async function () { 28 describe('Default search', async function () {
@@ -39,101 +30,107 @@ describe('Test videos search', function () {
39 it('Should make a local videos search by default', async function () { 30 it('Should make a local videos search by default', async function () {
40 this.timeout(10000) 31 this.timeout(10000)
41 32
42 await updateCustomSubConfig(server.url, server.accessToken, { 33 await server.config.updateCustomSubConfig({
43 search: { 34 newConfig: {
44 searchIndex: { 35 search: {
45 enabled: true, 36 searchIndex: {
46 isDefaultSearch: false, 37 enabled: true,
47 disableLocalSearch: false 38 isDefaultSearch: false,
39 disableLocalSearch: false
40 }
48 } 41 }
49 } 42 }
50 }) 43 })
51 44
52 const res = await searchVideo(server.url, 'local video') 45 const body = await command.searchVideos({ search: 'local video' })
53 46
54 expect(res.body.total).to.equal(1) 47 expect(body.total).to.equal(1)
55 expect(res.body.data[0].name).to.equal(localVideoName) 48 expect(body.data[0].name).to.equal(localVideoName)
56 }) 49 })
57 50
58 it('Should make a local channels search by default', async function () { 51 it('Should make a local channels search by default', async function () {
59 const res = await searchVideoChannel(server.url, 'root') 52 const body = await command.searchChannels({ search: 'root' })
60 53
61 expect(res.body.total).to.equal(1) 54 expect(body.total).to.equal(1)
62 expect(res.body.data[0].name).to.equal('root_channel') 55 expect(body.data[0].name).to.equal('root_channel')
63 expect(res.body.data[0].host).to.equal('localhost:' + server.port) 56 expect(body.data[0].host).to.equal('localhost:' + server.port)
64 }) 57 })
65 58
66 it('Should make an index videos search by default', async function () { 59 it('Should make an index videos search by default', async function () {
67 await updateCustomSubConfig(server.url, server.accessToken, { 60 await server.config.updateCustomSubConfig({
68 search: { 61 newConfig: {
69 searchIndex: { 62 search: {
70 enabled: true, 63 searchIndex: {
71 isDefaultSearch: true, 64 enabled: true,
72 disableLocalSearch: false 65 isDefaultSearch: true,
66 disableLocalSearch: false
67 }
73 } 68 }
74 } 69 }
75 }) 70 })
76 71
77 const res = await searchVideo(server.url, 'local video') 72 const body = await command.searchVideos({ search: 'local video' })
78 expect(res.body.total).to.be.greaterThan(2) 73 expect(body.total).to.be.greaterThan(2)
79 }) 74 })
80 75
81 it('Should make an index channels search by default', async function () { 76 it('Should make an index channels search by default', async function () {
82 const res = await searchVideoChannel(server.url, 'root') 77 const body = await command.searchChannels({ search: 'root' })
83 expect(res.body.total).to.be.greaterThan(2) 78 expect(body.total).to.be.greaterThan(2)
84 }) 79 })
85 80
86 it('Should make an index videos search if local search is disabled', async function () { 81 it('Should make an index videos search if local search is disabled', async function () {
87 await updateCustomSubConfig(server.url, server.accessToken, { 82 await server.config.updateCustomSubConfig({
88 search: { 83 newConfig: {
89 searchIndex: { 84 search: {
90 enabled: true, 85 searchIndex: {
91 isDefaultSearch: false, 86 enabled: true,
92 disableLocalSearch: true 87 isDefaultSearch: false,
88 disableLocalSearch: true
89 }
93 } 90 }
94 } 91 }
95 }) 92 })
96 93
97 const res = await searchVideo(server.url, 'local video') 94 const body = await command.searchVideos({ search: 'local video' })
98 expect(res.body.total).to.be.greaterThan(2) 95 expect(body.total).to.be.greaterThan(2)
99 }) 96 })
100 97
101 it('Should make an index channels search if local search is disabled', async function () { 98 it('Should make an index channels search if local search is disabled', async function () {
102 const res = await searchVideoChannel(server.url, 'root') 99 const body = await command.searchChannels({ search: 'root' })
103 expect(res.body.total).to.be.greaterThan(2) 100 expect(body.total).to.be.greaterThan(2)
104 }) 101 })
105 }) 102 })
106 103
107 describe('Videos search', async function () { 104 describe('Videos search', async function () {
108 105
109 it('Should make a simple search and not have results', async function () { 106 it('Should make a simple search and not have results', async function () {
110 const res = await searchVideo(server.url, 'djidane'.repeat(50)) 107 const body = await command.searchVideos({ search: 'djidane'.repeat(50) })
111 108
112 expect(res.body.total).to.equal(0) 109 expect(body.total).to.equal(0)
113 expect(res.body.data).to.have.lengthOf(0) 110 expect(body.data).to.have.lengthOf(0)
114 }) 111 })
115 112
116 it('Should make a simple search and have results', async function () { 113 it('Should make a simple search and have results', async function () {
117 const res = await searchVideo(server.url, 'What is PeerTube') 114 const body = await command.searchVideos({ search: 'What is PeerTube' })
118 115
119 expect(res.body.total).to.be.greaterThan(1) 116 expect(body.total).to.be.greaterThan(1)
120 }) 117 })
121 118
122 it('Should make a complex search', async function () { 119 it('Should make a complex search', async function () {
123 120
124 async function check (search: VideosSearchQuery, exists = true) { 121 async function check (search: VideosSearchQuery, exists = true) {
125 const res = await advancedVideosSearch(server.url, search) 122 const body = await command.advancedVideoSearch({ search })
126 123
127 if (exists === false) { 124 if (exists === false) {
128 expect(res.body.total).to.equal(0) 125 expect(body.total).to.equal(0)
129 expect(res.body.data).to.have.lengthOf(0) 126 expect(body.data).to.have.lengthOf(0)
130 return 127 return
131 } 128 }
132 129
133 expect(res.body.total).to.equal(1) 130 expect(body.total).to.equal(1)
134 expect(res.body.data).to.have.lengthOf(1) 131 expect(body.data).to.have.lengthOf(1)
135 132
136 const video: Video = res.body.data[0] 133 const video = body.data[0]
137 134
138 expect(video.name).to.equal('What is PeerTube?') 135 expect(video.name).to.equal('What is PeerTube?')
139 expect(video.category.label).to.equal('Science & Technology') 136 expect(video.category.label).to.equal('Science & Technology')
@@ -169,32 +166,32 @@ describe('Test videos search', function () {
169 } 166 }
170 167
171 { 168 {
172 const search = immutableAssign(baseSearch, { startDate: '2018-10-01T10:54:46.396Z' }) 169 const search = { ...baseSearch, startDate: '2018-10-01T10:54:46.396Z' }
173 await check(search, false) 170 await check(search, false)
174 } 171 }
175 172
176 { 173 {
177 const search = immutableAssign(baseSearch, { tagsAllOf: [ 'toto', 'framasoft' ] }) 174 const search = { ...baseSearch, tagsAllOf: [ 'toto', 'framasoft' ] }
178 await check(search, false) 175 await check(search, false)
179 } 176 }
180 177
181 { 178 {
182 const search = immutableAssign(baseSearch, { durationMin: 2000 }) 179 const search = { ...baseSearch, durationMin: 2000 }
183 await check(search, false) 180 await check(search, false)
184 } 181 }
185 182
186 { 183 {
187 const search = immutableAssign(baseSearch, { nsfw: 'true' }) 184 const search = { ...baseSearch, nsfw: 'true' as BooleanBothQuery }
188 await check(search, false) 185 await check(search, false)
189 } 186 }
190 187
191 { 188 {
192 const search = immutableAssign(baseSearch, { nsfw: 'false' }) 189 const search = { ...baseSearch, nsfw: 'false' as BooleanBothQuery }
193 await check(search, true) 190 await check(search, true)
194 } 191 }
195 192
196 { 193 {
197 const search = immutableAssign(baseSearch, { nsfw: 'both' }) 194 const search = { ...baseSearch, nsfw: 'both' as BooleanBothQuery }
198 await check(search, true) 195 await check(search, true)
199 } 196 }
200 }) 197 })
@@ -206,37 +203,44 @@ describe('Test videos search', function () {
206 count: 5 203 count: 5
207 } 204 }
208 205
209 const res = await advancedVideosSearch(server.url, search) 206 const body = await command.advancedVideoSearch({ search })
210 207
211 expect(res.body.total).to.be.greaterThan(5) 208 expect(body.total).to.be.greaterThan(5)
212 expect(res.body.data).to.have.lengthOf(5) 209 expect(body.data).to.have.lengthOf(5)
213 }) 210 })
214 211
215 it('Should use the nsfw instance policy as default', async function () { 212 it('Should use the nsfw instance policy as default', async function () {
216 let nsfwUUID: string 213 let nsfwUUID: string
217 214
218 { 215 {
219 await updateCustomSubConfig(server.url, server.accessToken, { instance: { defaultNSFWPolicy: 'display' } }) 216 await server.config.updateCustomSubConfig({
217 newConfig: {
218 instance: { defaultNSFWPolicy: 'display' }
219 }
220 })
220 221
221 const res = await searchVideo(server.url, 'NSFW search index', '-match') 222 const body = await command.searchVideos({ search: 'NSFW search index', sort: '-match' })
222 const video = res.body.data[0] as Video 223 expect(body.data).to.have.length.greaterThan(0)
223 224
224 expect(res.body.data).to.have.length.greaterThan(0) 225 const video = body.data[0]
225 expect(video.nsfw).to.be.true 226 expect(video.nsfw).to.be.true
226 227
227 nsfwUUID = video.uuid 228 nsfwUUID = video.uuid
228 } 229 }
229 230
230 { 231 {
231 await updateCustomSubConfig(server.url, server.accessToken, { instance: { defaultNSFWPolicy: 'do_not_list' } }) 232 await server.config.updateCustomSubConfig({
233 newConfig: {
234 instance: { defaultNSFWPolicy: 'do_not_list' }
235 }
236 })
232 237
233 const res = await searchVideo(server.url, 'NSFW search index', '-match') 238 const body = await command.searchVideos({ search: 'NSFW search index', sort: '-match' })
234 239
235 try { 240 try {
236 expect(res.body.data).to.have.lengthOf(0) 241 expect(body.data).to.have.lengthOf(0)
237 } catch (err) { 242 } catch {
238 // 243 const video = body.data[0]
239 const video = res.body.data[0] as Video
240 244
241 expect(video.uuid).not.equal(nsfwUUID) 245 expect(video.uuid).not.equal(nsfwUUID)
242 } 246 }
@@ -247,19 +251,19 @@ describe('Test videos search', function () {
247 describe('Channels search', async function () { 251 describe('Channels search', async function () {
248 252
249 it('Should make a simple search and not have results', async function () { 253 it('Should make a simple search and not have results', async function () {
250 const res = await searchVideoChannel(server.url, 'a'.repeat(500)) 254 const body = await command.searchChannels({ search: 'a'.repeat(500) })
251 255
252 expect(res.body.total).to.equal(0) 256 expect(body.total).to.equal(0)
253 expect(res.body.data).to.have.lengthOf(0) 257 expect(body.data).to.have.lengthOf(0)
254 }) 258 })
255 259
256 it('Should make a search and have results', async function () { 260 it('Should make a search and have results', async function () {
257 const res = await advancedVideoChannelSearch(server.url, { search: 'Framasoft', sort: 'createdAt' }) 261 const body = await command.advancedChannelSearch({ search: { search: 'Framasoft', sort: 'createdAt' } })
258 262
259 expect(res.body.total).to.be.greaterThan(0) 263 expect(body.total).to.be.greaterThan(0)
260 expect(res.body.data).to.have.length.greaterThan(0) 264 expect(body.data).to.have.length.greaterThan(0)
261 265
262 const videoChannel: VideoChannel = res.body.data[0] 266 const videoChannel = body.data[0]
263 expect(videoChannel.url).to.equal('https://framatube.org/video-channels/bf54d359-cfad-4935-9d45-9d6be93f63e8') 267 expect(videoChannel.url).to.equal('https://framatube.org/video-channels/bf54d359-cfad-4935-9d45-9d6be93f63e8')
264 expect(videoChannel.host).to.equal('framatube.org') 268 expect(videoChannel.host).to.equal('framatube.org')
265 expect(videoChannel.avatar).to.exist 269 expect(videoChannel.avatar).to.exist
@@ -272,29 +276,29 @@ describe('Test videos search', function () {
272 }) 276 })
273 277
274 it('Should have a correct pagination', async function () { 278 it('Should have a correct pagination', async function () {
275 const res = await advancedVideoChannelSearch(server.url, { search: 'root', start: 0, count: 2 }) 279 const body = await command.advancedChannelSearch({ search: { search: 'root', start: 0, count: 2 } })
276 280
277 expect(res.body.total).to.be.greaterThan(2) 281 expect(body.total).to.be.greaterThan(2)
278 expect(res.body.data).to.have.lengthOf(2) 282 expect(body.data).to.have.lengthOf(2)
279 }) 283 })
280 }) 284 })
281 285
282 describe('Playlists search', async function () { 286 describe('Playlists search', async function () {
283 287
284 it('Should make a simple search and not have results', async function () { 288 it('Should make a simple search and not have results', async function () {
285 const res = await searchVideoPlaylists(server.url, 'a'.repeat(500)) 289 const body = await command.searchPlaylists({ search: 'a'.repeat(500) })
286 290
287 expect(res.body.total).to.equal(0) 291 expect(body.total).to.equal(0)
288 expect(res.body.data).to.have.lengthOf(0) 292 expect(body.data).to.have.lengthOf(0)
289 }) 293 })
290 294
291 it('Should make a search and have results', async function () { 295 it('Should make a search and have results', async function () {
292 const res = await advancedVideoPlaylistSearch(server.url, { search: 'E2E playlist', sort: '-match' }) 296 const body = await command.advancedPlaylistSearch({ search: { search: 'E2E playlist', sort: '-match' } })
293 297
294 expect(res.body.total).to.be.greaterThan(0) 298 expect(body.total).to.be.greaterThan(0)
295 expect(res.body.data).to.have.length.greaterThan(0) 299 expect(body.data).to.have.length.greaterThan(0)
296 300
297 const videoPlaylist: VideoPlaylist = res.body.data[0] 301 const videoPlaylist = body.data[0]
298 302
299 expect(videoPlaylist.url).to.equal('https://peertube2.cpy.re/videos/watch/playlist/73804a40-da9a-40c2-b1eb-2c6d9eec8f0a') 303 expect(videoPlaylist.url).to.equal('https://peertube2.cpy.re/videos/watch/playlist/73804a40-da9a-40c2-b1eb-2c6d9eec8f0a')
300 expect(videoPlaylist.thumbnailUrl).to.exist 304 expect(videoPlaylist.thumbnailUrl).to.exist
@@ -322,10 +326,10 @@ describe('Test videos search', function () {
322 }) 326 })
323 327
324 it('Should have a correct pagination', async function () { 328 it('Should have a correct pagination', async function () {
325 const res = await advancedVideoChannelSearch(server.url, { search: 'root', start: 0, count: 2 }) 329 const body = await command.advancedChannelSearch({ search: { search: 'root', start: 0, count: 2 } })
326 330
327 expect(res.body.total).to.be.greaterThan(2) 331 expect(body.total).to.be.greaterThan(2)
328 expect(res.body.data).to.have.lengthOf(2) 332 expect(body.data).to.have.lengthOf(2)
329 }) 333 })
330 }) 334 })
331 335
diff --git a/server/tests/api/search/search-playlists.ts b/server/tests/api/search/search-playlists.ts
index ab17d55e9..22e9b8fca 100644
--- a/server/tests/api/search/search-playlists.ts
+++ b/server/tests/api/search/search-playlists.ts
@@ -2,82 +2,71 @@
2 2
3import 'mocha' 3import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5import { VideoPlaylist, VideoPlaylistPrivacy } from '@shared/models'
6import { 5import {
7 addVideoInPlaylist,
8 advancedVideoPlaylistSearch,
9 cleanupTests, 6 cleanupTests,
10 createVideoPlaylist, 7 createSingleServer,
11 flushAndRunServer, 8 PeerTubeServer,
12 searchVideoPlaylists, 9 SearchCommand,
13 ServerInfo,
14 setAccessTokensToServers, 10 setAccessTokensToServers,
15 setDefaultVideoChannel, 11 setDefaultVideoChannel
16 uploadVideoAndGetId 12} from '@shared/extra-utils'
17} from '../../../../shared/extra-utils' 13import { VideoPlaylistPrivacy } from '@shared/models'
18 14
19const expect = chai.expect 15const expect = chai.expect
20 16
21describe('Test playlists search', function () { 17describe('Test playlists search', function () {
22 let server: ServerInfo = null 18 let server: PeerTubeServer = null
19 let command: SearchCommand
23 20
24 before(async function () { 21 before(async function () {
25 this.timeout(30000) 22 this.timeout(30000)
26 23
27 server = await flushAndRunServer(1) 24 server = await createSingleServer(1)
28 25
29 await setAccessTokensToServers([ server ]) 26 await setAccessTokensToServers([ server ])
30 await setDefaultVideoChannel([ server ]) 27 await setDefaultVideoChannel([ server ])
31 28
32 const videoId = (await uploadVideoAndGetId({ server: server, videoName: 'video' })).uuid 29 const videoId = (await server.videos.quickUpload({ name: 'video' })).uuid
33 30
34 { 31 {
35 const attributes = { 32 const attributes = {
36 displayName: 'Dr. Kenzo Tenma hospital videos', 33 displayName: 'Dr. Kenzo Tenma hospital videos',
37 privacy: VideoPlaylistPrivacy.PUBLIC, 34 privacy: VideoPlaylistPrivacy.PUBLIC,
38 videoChannelId: server.videoChannel.id 35 videoChannelId: server.store.channel.id
39 } 36 }
40 const res = await createVideoPlaylist({ url: server.url, token: server.accessToken, playlistAttrs: attributes }) 37 const created = await server.playlists.create({ attributes })
41 38
42 await addVideoInPlaylist({ 39 await server.playlists.addElement({ playlistId: created.id, attributes: { videoId } })
43 url: server.url,
44 token: server.accessToken,
45 playlistId: res.body.videoPlaylist.id,
46 elementAttrs: { videoId }
47 })
48 } 40 }
49 41
50 { 42 {
51 const attributes = { 43 const attributes = {
52 displayName: 'Johan & Anna Libert musics', 44 displayName: 'Johan & Anna Libert musics',
53 privacy: VideoPlaylistPrivacy.PUBLIC, 45 privacy: VideoPlaylistPrivacy.PUBLIC,
54 videoChannelId: server.videoChannel.id 46 videoChannelId: server.store.channel.id
55 } 47 }
56 const res = await createVideoPlaylist({ url: server.url, token: server.accessToken, playlistAttrs: attributes }) 48 const created = await server.playlists.create({ attributes })
57 49
58 await addVideoInPlaylist({ 50 await server.playlists.addElement({ playlistId: created.id, attributes: { videoId } })
59 url: server.url,
60 token: server.accessToken,
61 playlistId: res.body.videoPlaylist.id,
62 elementAttrs: { videoId }
63 })
64 } 51 }
65 52
66 { 53 {
67 const attributes = { 54 const attributes = {
68 displayName: 'Inspector Lunge playlist', 55 displayName: 'Inspector Lunge playlist',
69 privacy: VideoPlaylistPrivacy.PUBLIC, 56 privacy: VideoPlaylistPrivacy.PUBLIC,
70 videoChannelId: server.videoChannel.id 57 videoChannelId: server.store.channel.id
71 } 58 }
72 await createVideoPlaylist({ url: server.url, token: server.accessToken, playlistAttrs: attributes }) 59 await server.playlists.create({ attributes })
73 } 60 }
61
62 command = server.search
74 }) 63 })
75 64
76 it('Should make a simple search and not have results', async function () { 65 it('Should make a simple search and not have results', async function () {
77 const res = await searchVideoPlaylists(server.url, 'abc') 66 const body = await command.searchPlaylists({ search: 'abc' })
78 67
79 expect(res.body.total).to.equal(0) 68 expect(body.total).to.equal(0)
80 expect(res.body.data).to.have.lengthOf(0) 69 expect(body.data).to.have.lengthOf(0)
81 }) 70 })
82 71
83 it('Should make a search and have results', async function () { 72 it('Should make a search and have results', async function () {
@@ -87,11 +76,11 @@ describe('Test playlists search', function () {
87 start: 0, 76 start: 0,
88 count: 1 77 count: 1
89 } 78 }
90 const res = await advancedVideoPlaylistSearch(server.url, search) 79 const body = await command.advancedPlaylistSearch({ search })
91 expect(res.body.total).to.equal(1) 80 expect(body.total).to.equal(1)
92 expect(res.body.data).to.have.lengthOf(1) 81 expect(body.data).to.have.lengthOf(1)
93 82
94 const playlist: VideoPlaylist = res.body.data[0] 83 const playlist = body.data[0]
95 expect(playlist.displayName).to.equal('Dr. Kenzo Tenma hospital videos') 84 expect(playlist.displayName).to.equal('Dr. Kenzo Tenma hospital videos')
96 expect(playlist.url).to.equal(server.url + '/video-playlists/' + playlist.uuid) 85 expect(playlist.url).to.equal(server.url + '/video-playlists/' + playlist.uuid)
97 } 86 }
@@ -102,11 +91,11 @@ describe('Test playlists search', function () {
102 start: 0, 91 start: 0,
103 count: 1 92 count: 1
104 } 93 }
105 const res = await advancedVideoPlaylistSearch(server.url, search) 94 const body = await command.advancedPlaylistSearch({ search })
106 expect(res.body.total).to.equal(1) 95 expect(body.total).to.equal(1)
107 expect(res.body.data).to.have.lengthOf(1) 96 expect(body.data).to.have.lengthOf(1)
108 97
109 const playlist: VideoPlaylist = res.body.data[0] 98 const playlist = body.data[0]
110 expect(playlist.displayName).to.equal('Johan & Anna Libert musics') 99 expect(playlist.displayName).to.equal('Johan & Anna Libert musics')
111 } 100 }
112 }) 101 })
@@ -117,9 +106,9 @@ describe('Test playlists search', function () {
117 start: 0, 106 start: 0,
118 count: 1 107 count: 1
119 } 108 }
120 const res = await advancedVideoPlaylistSearch(server.url, search) 109 const body = await command.advancedPlaylistSearch({ search })
121 expect(res.body.total).to.equal(0) 110 expect(body.total).to.equal(0)
122 expect(res.body.data).to.have.lengthOf(0) 111 expect(body.data).to.have.lengthOf(0)
123 }) 112 })
124 113
125 after(async function () { 114 after(async function () {
diff --git a/server/tests/api/search/search-videos.ts b/server/tests/api/search/search-videos.ts
index 5b8907961..965766742 100644
--- a/server/tests/api/search/search-videos.ts
+++ b/server/tests/api/search/search-videos.ts
@@ -2,37 +2,31 @@
2 2
3import 'mocha' 3import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5import { VideoPrivacy } from '@shared/models'
6import { 5import {
7 advancedVideosSearch,
8 cleanupTests, 6 cleanupTests,
9 createLive, 7 createSingleServer,
10 flushAndRunServer, 8 PeerTubeServer,
11 immutableAssign, 9 SearchCommand,
12 searchVideo,
13 sendRTMPStreamInVideo,
14 ServerInfo,
15 setAccessTokensToServers, 10 setAccessTokensToServers,
16 setDefaultVideoChannel, 11 setDefaultVideoChannel,
17 stopFfmpeg, 12 stopFfmpeg,
18 updateCustomSubConfig, 13 wait
19 uploadVideo, 14} from '@shared/extra-utils'
20 wait, 15import { VideoPrivacy } from '@shared/models'
21 waitUntilLivePublished
22} from '../../../../shared/extra-utils'
23import { createVideoCaption } from '../../../../shared/extra-utils/videos/video-captions'
24 16
25const expect = chai.expect 17const expect = chai.expect
26 18
27describe('Test videos search', function () { 19describe('Test videos search', function () {
28 let server: ServerInfo = null 20 let server: PeerTubeServer = null
29 let startDate: string 21 let startDate: string
30 let videoUUID: string 22 let videoUUID: string
31 23
24 let command: SearchCommand
25
32 before(async function () { 26 before(async function () {
33 this.timeout(60000) 27 this.timeout(60000)
34 28
35 server = await flushAndRunServer(1) 29 server = await createSingleServer(1)
36 30
37 await setAccessTokensToServers([ server ]) 31 await setAccessTokensToServers([ server ])
38 await setDefaultVideoChannel([ server ]) 32 await setDefaultVideoChannel([ server ])
@@ -46,57 +40,49 @@ describe('Test videos search', function () {
46 nsfw: false, 40 nsfw: false,
47 language: 'fr' 41 language: 'fr'
48 } 42 }
49 await uploadVideo(server.url, server.accessToken, attributes1) 43 await server.videos.upload({ attributes: attributes1 })
50 44
51 const attributes2 = immutableAssign(attributes1, { name: attributes1.name + ' - 2', fixture: 'video_short.mp4' }) 45 const attributes2 = { ...attributes1, name: attributes1.name + ' - 2', fixture: 'video_short.mp4' }
52 await uploadVideo(server.url, server.accessToken, attributes2) 46 await server.videos.upload({ attributes: attributes2 })
53 47
54 { 48 {
55 const attributes3 = immutableAssign(attributes1, { name: attributes1.name + ' - 3', language: undefined }) 49 const attributes3 = { ...attributes1, name: attributes1.name + ' - 3', language: undefined }
56 const res = await uploadVideo(server.url, server.accessToken, attributes3) 50 const { id, uuid } = await server.videos.upload({ attributes: attributes3 })
57 const videoId = res.body.video.id 51 videoUUID = uuid
58 videoUUID = res.body.video.uuid 52
59 53 await server.captions.add({
60 await createVideoCaption({
61 url: server.url,
62 accessToken: server.accessToken,
63 language: 'en', 54 language: 'en',
64 videoId, 55 videoId: id,
65 fixture: 'subtitle-good2.vtt', 56 fixture: 'subtitle-good2.vtt',
66 mimeType: 'application/octet-stream' 57 mimeType: 'application/octet-stream'
67 }) 58 })
68 59
69 await createVideoCaption({ 60 await server.captions.add({
70 url: server.url,
71 accessToken: server.accessToken,
72 language: 'aa', 61 language: 'aa',
73 videoId, 62 videoId: id,
74 fixture: 'subtitle-good2.vtt', 63 fixture: 'subtitle-good2.vtt',
75 mimeType: 'application/octet-stream' 64 mimeType: 'application/octet-stream'
76 }) 65 })
77 } 66 }
78 67
79 const attributes4 = immutableAssign(attributes1, { name: attributes1.name + ' - 4', language: 'pl', nsfw: true }) 68 const attributes4 = { ...attributes1, name: attributes1.name + ' - 4', language: 'pl', nsfw: true }
80 await uploadVideo(server.url, server.accessToken, attributes4) 69 await server.videos.upload({ attributes: attributes4 })
81 70
82 await wait(1000) 71 await wait(1000)
83 72
84 startDate = new Date().toISOString() 73 startDate = new Date().toISOString()
85 74
86 const attributes5 = immutableAssign(attributes1, { name: attributes1.name + ' - 5', licence: 2, language: undefined }) 75 const attributes5 = { ...attributes1, name: attributes1.name + ' - 5', licence: 2, language: undefined }
87 await uploadVideo(server.url, server.accessToken, attributes5) 76 await server.videos.upload({ attributes: attributes5 })
88 77
89 const attributes6 = immutableAssign(attributes1, { name: attributes1.name + ' - 6', tags: [ 't1', 't2' ] }) 78 const attributes6 = { ...attributes1, name: attributes1.name + ' - 6', tags: [ 't1', 't2' ] }
90 await uploadVideo(server.url, server.accessToken, attributes6) 79 await server.videos.upload({ attributes: attributes6 })
91 80
92 const attributes7 = immutableAssign(attributes1, { 81 const attributes7 = { ...attributes1, name: attributes1.name + ' - 7', originallyPublishedAt: '2019-02-12T09:58:08.286Z' }
93 name: attributes1.name + ' - 7', 82 await server.videos.upload({ attributes: attributes7 })
94 originallyPublishedAt: '2019-02-12T09:58:08.286Z'
95 })
96 await uploadVideo(server.url, server.accessToken, attributes7)
97 83
98 const attributes8 = immutableAssign(attributes1, { name: attributes1.name + ' - 8', licence: 4 }) 84 const attributes8 = { ...attributes1, name: attributes1.name + ' - 8', licence: 4 }
99 await uploadVideo(server.url, server.accessToken, attributes8) 85 await server.videos.upload({ attributes: attributes8 })
100 } 86 }
101 87
102 { 88 {
@@ -107,9 +93,9 @@ describe('Test videos search', function () {
107 licence: 2, 93 licence: 2,
108 language: 'en' 94 language: 'en'
109 } 95 }
110 await uploadVideo(server.url, server.accessToken, attributes) 96 await server.videos.upload({ attributes: attributes })
111 97
112 await uploadVideo(server.url, server.accessToken, immutableAssign(attributes, { name: attributes.name + ' duplicate' })) 98 await server.videos.upload({ attributes: { ...attributes, name: attributes.name + ' duplicate' } })
113 } 99 }
114 100
115 { 101 {
@@ -120,7 +106,7 @@ describe('Test videos search', function () {
120 licence: 3, 106 licence: 3,
121 language: 'pl' 107 language: 'pl'
122 } 108 }
123 await uploadVideo(server.url, server.accessToken, attributes) 109 await server.videos.upload({ attributes: attributes })
124 } 110 }
125 111
126 { 112 {
@@ -129,11 +115,11 @@ describe('Test videos search', function () {
129 tags: [ 'aaaa', 'bbbb', 'cccc' ], 115 tags: [ 'aaaa', 'bbbb', 'cccc' ],
130 category: 1 116 category: 1
131 } 117 }
132 await uploadVideo(server.url, server.accessToken, attributes1) 118 await server.videos.upload({ attributes: attributes1 })
133 await uploadVideo(server.url, server.accessToken, immutableAssign(attributes1, { category: 2 })) 119 await server.videos.upload({ attributes: { ...attributes1, category: 2 } })
134 120
135 await uploadVideo(server.url, server.accessToken, immutableAssign(attributes1, { tags: [ 'cccc', 'dddd' ] })) 121 await server.videos.upload({ attributes: { ...attributes1, tags: [ 'cccc', 'dddd' ] } })
136 await uploadVideo(server.url, server.accessToken, immutableAssign(attributes1, { tags: [ 'eeee', 'ffff' ] })) 122 await server.videos.upload({ attributes: { ...attributes1, tags: [ 'eeee', 'ffff' ] } })
137 } 123 }
138 124
139 { 125 {
@@ -141,24 +127,26 @@ describe('Test videos search', function () {
141 name: 'aaaa 2', 127 name: 'aaaa 2',
142 category: 1 128 category: 1
143 } 129 }
144 await uploadVideo(server.url, server.accessToken, attributes1) 130 await server.videos.upload({ attributes: attributes1 })
145 await uploadVideo(server.url, server.accessToken, immutableAssign(attributes1, { category: 2 })) 131 await server.videos.upload({ attributes: { ...attributes1, category: 2 } })
146 } 132 }
133
134 command = server.search
147 }) 135 })
148 136
149 it('Should make a simple search and not have results', async function () { 137 it('Should make a simple search and not have results', async function () {
150 const res = await searchVideo(server.url, 'abc') 138 const body = await command.searchVideos({ search: 'abc' })
151 139
152 expect(res.body.total).to.equal(0) 140 expect(body.total).to.equal(0)
153 expect(res.body.data).to.have.lengthOf(0) 141 expect(body.data).to.have.lengthOf(0)
154 }) 142 })
155 143
156 it('Should make a simple search and have results', async function () { 144 it('Should make a simple search and have results', async function () {
157 const res = await searchVideo(server.url, '4444 5555 duplicate') 145 const body = await command.searchVideos({ search: '4444 5555 duplicate' })
158 146
159 expect(res.body.total).to.equal(2) 147 expect(body.total).to.equal(2)
160 148
161 const videos = res.body.data 149 const videos = body.data
162 expect(videos).to.have.lengthOf(2) 150 expect(videos).to.have.lengthOf(2)
163 151
164 // bestmatch 152 // bestmatch
@@ -167,15 +155,15 @@ describe('Test videos search', function () {
167 }) 155 })
168 156
169 it('Should make a search on tags too, and have results', async function () { 157 it('Should make a search on tags too, and have results', async function () {
170 const query = { 158 const search = {
171 search: 'aaaa', 159 search: 'aaaa',
172 categoryOneOf: [ 1 ] 160 categoryOneOf: [ 1 ]
173 } 161 }
174 const res = await advancedVideosSearch(server.url, query) 162 const body = await command.advancedVideoSearch({ search })
175 163
176 expect(res.body.total).to.equal(2) 164 expect(body.total).to.equal(2)
177 165
178 const videos = res.body.data 166 const videos = body.data
179 expect(videos).to.have.lengthOf(2) 167 expect(videos).to.have.lengthOf(2)
180 168
181 // bestmatch 169 // bestmatch
@@ -184,14 +172,14 @@ describe('Test videos search', function () {
184 }) 172 })
185 173
186 it('Should filter on tags without a search', async function () { 174 it('Should filter on tags without a search', async function () {
187 const query = { 175 const search = {
188 tagsAllOf: [ 'bbbb' ] 176 tagsAllOf: [ 'bbbb' ]
189 } 177 }
190 const res = await advancedVideosSearch(server.url, query) 178 const body = await command.advancedVideoSearch({ search })
191 179
192 expect(res.body.total).to.equal(2) 180 expect(body.total).to.equal(2)
193 181
194 const videos = res.body.data 182 const videos = body.data
195 expect(videos).to.have.lengthOf(2) 183 expect(videos).to.have.lengthOf(2)
196 184
197 expect(videos[0].name).to.equal('9999') 185 expect(videos[0].name).to.equal('9999')
@@ -199,14 +187,14 @@ describe('Test videos search', function () {
199 }) 187 })
200 188
201 it('Should filter on category without a search', async function () { 189 it('Should filter on category without a search', async function () {
202 const query = { 190 const search = {
203 categoryOneOf: [ 3 ] 191 categoryOneOf: [ 3 ]
204 } 192 }
205 const res = await advancedVideosSearch(server.url, query) 193 const body = await command.advancedVideoSearch({ search: search })
206 194
207 expect(res.body.total).to.equal(1) 195 expect(body.total).to.equal(1)
208 196
209 const videos = res.body.data 197 const videos = body.data
210 expect(videos).to.have.lengthOf(1) 198 expect(videos).to.have.lengthOf(1)
211 199
212 expect(videos[0].name).to.equal('6666 7777 8888') 200 expect(videos[0].name).to.equal('6666 7777 8888')
@@ -218,11 +206,16 @@ describe('Test videos search', function () {
218 categoryOneOf: [ 1 ], 206 categoryOneOf: [ 1 ],
219 tagsOneOf: [ 'aAaa', 'ffff' ] 207 tagsOneOf: [ 'aAaa', 'ffff' ]
220 } 208 }
221 const res1 = await advancedVideosSearch(server.url, query)
222 expect(res1.body.total).to.equal(2)
223 209
224 const res2 = await advancedVideosSearch(server.url, immutableAssign(query, { tagsOneOf: [ 'blabla' ] })) 210 {
225 expect(res2.body.total).to.equal(0) 211 const body = await command.advancedVideoSearch({ search: query })
212 expect(body.total).to.equal(2)
213 }
214
215 {
216 const body = await command.advancedVideoSearch({ search: { ...query, tagsOneOf: [ 'blabla' ] } })
217 expect(body.total).to.equal(0)
218 }
226 }) 219 })
227 220
228 it('Should search by tags (all of)', async function () { 221 it('Should search by tags (all of)', async function () {
@@ -231,14 +224,21 @@ describe('Test videos search', function () {
231 categoryOneOf: [ 1 ], 224 categoryOneOf: [ 1 ],
232 tagsAllOf: [ 'CCcc' ] 225 tagsAllOf: [ 'CCcc' ]
233 } 226 }
234 const res1 = await advancedVideosSearch(server.url, query)
235 expect(res1.body.total).to.equal(2)
236 227
237 const res2 = await advancedVideosSearch(server.url, immutableAssign(query, { tagsAllOf: [ 'blAbla' ] })) 228 {
238 expect(res2.body.total).to.equal(0) 229 const body = await command.advancedVideoSearch({ search: query })
230 expect(body.total).to.equal(2)
231 }
239 232
240 const res3 = await advancedVideosSearch(server.url, immutableAssign(query, { tagsAllOf: [ 'bbbb', 'CCCC' ] })) 233 {
241 expect(res3.body.total).to.equal(1) 234 const body = await command.advancedVideoSearch({ search: { ...query, tagsAllOf: [ 'blAbla' ] } })
235 expect(body.total).to.equal(0)
236 }
237
238 {
239 const body = await command.advancedVideoSearch({ search: { ...query, tagsAllOf: [ 'bbbb', 'CCCC' ] } })
240 expect(body.total).to.equal(1)
241 }
242 }) 242 })
243 243
244 it('Should search by category', async function () { 244 it('Should search by category', async function () {
@@ -246,12 +246,17 @@ describe('Test videos search', function () {
246 search: '6666', 246 search: '6666',
247 categoryOneOf: [ 3 ] 247 categoryOneOf: [ 3 ]
248 } 248 }
249 const res1 = await advancedVideosSearch(server.url, query)
250 expect(res1.body.total).to.equal(1)
251 expect(res1.body.data[0].name).to.equal('6666 7777 8888')
252 249
253 const res2 = await advancedVideosSearch(server.url, immutableAssign(query, { categoryOneOf: [ 2 ] })) 250 {
254 expect(res2.body.total).to.equal(0) 251 const body = await command.advancedVideoSearch({ search: query })
252 expect(body.total).to.equal(1)
253 expect(body.data[0].name).to.equal('6666 7777 8888')
254 }
255
256 {
257 const body = await command.advancedVideoSearch({ search: { ...query, categoryOneOf: [ 2 ] } })
258 expect(body.total).to.equal(0)
259 }
255 }) 260 })
256 261
257 it('Should search by licence', async function () { 262 it('Should search by licence', async function () {
@@ -259,13 +264,18 @@ describe('Test videos search', function () {
259 search: '4444 5555', 264 search: '4444 5555',
260 licenceOneOf: [ 2 ] 265 licenceOneOf: [ 2 ]
261 } 266 }
262 const res1 = await advancedVideosSearch(server.url, query)
263 expect(res1.body.total).to.equal(2)
264 expect(res1.body.data[0].name).to.equal('3333 4444 5555')
265 expect(res1.body.data[1].name).to.equal('3333 4444 5555 duplicate')
266 267
267 const res2 = await advancedVideosSearch(server.url, immutableAssign(query, { licenceOneOf: [ 3 ] })) 268 {
268 expect(res2.body.total).to.equal(0) 269 const body = await command.advancedVideoSearch({ search: query })
270 expect(body.total).to.equal(2)
271 expect(body.data[0].name).to.equal('3333 4444 5555')
272 expect(body.data[1].name).to.equal('3333 4444 5555 duplicate')
273 }
274
275 {
276 const body = await command.advancedVideoSearch({ search: { ...query, licenceOneOf: [ 3 ] } })
277 expect(body.total).to.equal(0)
278 }
269 }) 279 })
270 280
271 it('Should search by languages', async function () { 281 it('Should search by languages', async function () {
@@ -275,23 +285,23 @@ describe('Test videos search', function () {
275 } 285 }
276 286
277 { 287 {
278 const res = await advancedVideosSearch(server.url, query) 288 const body = await command.advancedVideoSearch({ search: query })
279 expect(res.body.total).to.equal(2) 289 expect(body.total).to.equal(2)
280 expect(res.body.data[0].name).to.equal('1111 2222 3333 - 3') 290 expect(body.data[0].name).to.equal('1111 2222 3333 - 3')
281 expect(res.body.data[1].name).to.equal('1111 2222 3333 - 4') 291 expect(body.data[1].name).to.equal('1111 2222 3333 - 4')
282 } 292 }
283 293
284 { 294 {
285 const res = await advancedVideosSearch(server.url, immutableAssign(query, { languageOneOf: [ 'pl', 'en', '_unknown' ] })) 295 const body = await command.advancedVideoSearch({ search: { ...query, languageOneOf: [ 'pl', 'en', '_unknown' ] } })
286 expect(res.body.total).to.equal(3) 296 expect(body.total).to.equal(3)
287 expect(res.body.data[0].name).to.equal('1111 2222 3333 - 3') 297 expect(body.data[0].name).to.equal('1111 2222 3333 - 3')
288 expect(res.body.data[1].name).to.equal('1111 2222 3333 - 4') 298 expect(body.data[1].name).to.equal('1111 2222 3333 - 4')
289 expect(res.body.data[2].name).to.equal('1111 2222 3333 - 5') 299 expect(body.data[2].name).to.equal('1111 2222 3333 - 5')
290 } 300 }
291 301
292 { 302 {
293 const res = await advancedVideosSearch(server.url, immutableAssign(query, { languageOneOf: [ 'eo' ] })) 303 const body = await command.advancedVideoSearch({ search: { ...query, languageOneOf: [ 'eo' ] } })
294 expect(res.body.total).to.equal(0) 304 expect(body.total).to.equal(0)
295 } 305 }
296 }) 306 })
297 307
@@ -301,10 +311,10 @@ describe('Test videos search', function () {
301 startDate 311 startDate
302 } 312 }
303 313
304 const res = await advancedVideosSearch(server.url, query) 314 const body = await command.advancedVideoSearch({ search: query })
305 expect(res.body.total).to.equal(4) 315 expect(body.total).to.equal(4)
306 316
307 const videos = res.body.data 317 const videos = body.data
308 expect(videos[0].name).to.equal('1111 2222 3333 - 5') 318 expect(videos[0].name).to.equal('1111 2222 3333 - 5')
309 expect(videos[1].name).to.equal('1111 2222 3333 - 6') 319 expect(videos[1].name).to.equal('1111 2222 3333 - 6')
310 expect(videos[2].name).to.equal('1111 2222 3333 - 7') 320 expect(videos[2].name).to.equal('1111 2222 3333 - 7')
@@ -320,10 +330,10 @@ describe('Test videos search', function () {
320 licenceOneOf: [ 1, 4 ] 330 licenceOneOf: [ 1, 4 ]
321 } 331 }
322 332
323 const res = await advancedVideosSearch(server.url, query) 333 const body = await command.advancedVideoSearch({ search: query })
324 expect(res.body.total).to.equal(4) 334 expect(body.total).to.equal(4)
325 335
326 const videos = res.body.data 336 const videos = body.data
327 expect(videos[0].name).to.equal('1111 2222 3333') 337 expect(videos[0].name).to.equal('1111 2222 3333')
328 expect(videos[1].name).to.equal('1111 2222 3333 - 6') 338 expect(videos[1].name).to.equal('1111 2222 3333 - 6')
329 expect(videos[2].name).to.equal('1111 2222 3333 - 7') 339 expect(videos[2].name).to.equal('1111 2222 3333 - 7')
@@ -340,10 +350,10 @@ describe('Test videos search', function () {
340 sort: '-name' 350 sort: '-name'
341 } 351 }
342 352
343 const res = await advancedVideosSearch(server.url, query) 353 const body = await command.advancedVideoSearch({ search: query })
344 expect(res.body.total).to.equal(4) 354 expect(body.total).to.equal(4)
345 355
346 const videos = res.body.data 356 const videos = body.data
347 expect(videos[0].name).to.equal('1111 2222 3333 - 8') 357 expect(videos[0].name).to.equal('1111 2222 3333 - 8')
348 expect(videos[1].name).to.equal('1111 2222 3333 - 7') 358 expect(videos[1].name).to.equal('1111 2222 3333 - 7')
349 expect(videos[2].name).to.equal('1111 2222 3333 - 6') 359 expect(videos[2].name).to.equal('1111 2222 3333 - 6')
@@ -362,10 +372,10 @@ describe('Test videos search', function () {
362 count: 1 372 count: 1
363 } 373 }
364 374
365 const res = await advancedVideosSearch(server.url, query) 375 const body = await command.advancedVideoSearch({ search: query })
366 expect(res.body.total).to.equal(4) 376 expect(body.total).to.equal(4)
367 377
368 const videos = res.body.data 378 const videos = body.data
369 expect(videos[0].name).to.equal('1111 2222 3333 - 8') 379 expect(videos[0].name).to.equal('1111 2222 3333 - 8')
370 }) 380 })
371 381
@@ -381,10 +391,10 @@ describe('Test videos search', function () {
381 count: 1 391 count: 1
382 } 392 }
383 393
384 const res = await advancedVideosSearch(server.url, query) 394 const body = await command.advancedVideoSearch({ search: query })
385 expect(res.body.total).to.equal(4) 395 expect(body.total).to.equal(4)
386 396
387 const videos = res.body.data 397 const videos = body.data
388 expect(videos[0].name).to.equal('1111 2222 3333') 398 expect(videos[0].name).to.equal('1111 2222 3333')
389 }) 399 })
390 400
@@ -398,99 +408,102 @@ describe('Test videos search', function () {
398 } 408 }
399 409
400 { 410 {
401 const query = immutableAssign(baseQuery, { originallyPublishedStartDate: '2019-02-11T09:58:08.286Z' }) 411 const query = { ...baseQuery, originallyPublishedStartDate: '2019-02-11T09:58:08.286Z' }
402 const res = await advancedVideosSearch(server.url, query) 412 const body = await command.advancedVideoSearch({ search: query })
403 413
404 expect(res.body.total).to.equal(1) 414 expect(body.total).to.equal(1)
405 expect(res.body.data[0].name).to.equal('1111 2222 3333 - 7') 415 expect(body.data[0].name).to.equal('1111 2222 3333 - 7')
406 } 416 }
407 417
408 { 418 {
409 const query = immutableAssign(baseQuery, { originallyPublishedEndDate: '2019-03-11T09:58:08.286Z' }) 419 const query = { ...baseQuery, originallyPublishedEndDate: '2019-03-11T09:58:08.286Z' }
410 const res = await advancedVideosSearch(server.url, query) 420 const body = await command.advancedVideoSearch({ search: query })
411 421
412 expect(res.body.total).to.equal(1) 422 expect(body.total).to.equal(1)
413 expect(res.body.data[0].name).to.equal('1111 2222 3333 - 7') 423 expect(body.data[0].name).to.equal('1111 2222 3333 - 7')
414 } 424 }
415 425
416 { 426 {
417 const query = immutableAssign(baseQuery, { originallyPublishedEndDate: '2019-01-11T09:58:08.286Z' }) 427 const query = { ...baseQuery, originallyPublishedEndDate: '2019-01-11T09:58:08.286Z' }
418 const res = await advancedVideosSearch(server.url, query) 428 const body = await command.advancedVideoSearch({ search: query })
419 429
420 expect(res.body.total).to.equal(0) 430 expect(body.total).to.equal(0)
421 } 431 }
422 432
423 { 433 {
424 const query = immutableAssign(baseQuery, { originallyPublishedStartDate: '2019-03-11T09:58:08.286Z' }) 434 const query = { ...baseQuery, originallyPublishedStartDate: '2019-03-11T09:58:08.286Z' }
425 const res = await advancedVideosSearch(server.url, query) 435 const body = await command.advancedVideoSearch({ search: query })
426 436
427 expect(res.body.total).to.equal(0) 437 expect(body.total).to.equal(0)
428 } 438 }
429 439
430 { 440 {
431 const query = immutableAssign(baseQuery, { 441 const query = {
442 ...baseQuery,
432 originallyPublishedStartDate: '2019-01-11T09:58:08.286Z', 443 originallyPublishedStartDate: '2019-01-11T09:58:08.286Z',
433 originallyPublishedEndDate: '2019-01-10T09:58:08.286Z' 444 originallyPublishedEndDate: '2019-01-10T09:58:08.286Z'
434 }) 445 }
435 const res = await advancedVideosSearch(server.url, query) 446 const body = await command.advancedVideoSearch({ search: query })
436 447
437 expect(res.body.total).to.equal(0) 448 expect(body.total).to.equal(0)
438 } 449 }
439 450
440 { 451 {
441 const query = immutableAssign(baseQuery, { 452 const query = {
453 ...baseQuery,
442 originallyPublishedStartDate: '2019-01-11T09:58:08.286Z', 454 originallyPublishedStartDate: '2019-01-11T09:58:08.286Z',
443 originallyPublishedEndDate: '2019-04-11T09:58:08.286Z' 455 originallyPublishedEndDate: '2019-04-11T09:58:08.286Z'
444 }) 456 }
445 const res = await advancedVideosSearch(server.url, query) 457 const body = await command.advancedVideoSearch({ search: query })
446 458
447 expect(res.body.total).to.equal(1) 459 expect(body.total).to.equal(1)
448 expect(res.body.data[0].name).to.equal('1111 2222 3333 - 7') 460 expect(body.data[0].name).to.equal('1111 2222 3333 - 7')
449 } 461 }
450 }) 462 })
451 463
452 it('Should search by UUID', async function () { 464 it('Should search by UUID', async function () {
453 const search = videoUUID 465 const search = videoUUID
454 const res = await advancedVideosSearch(server.url, { search }) 466 const body = await command.advancedVideoSearch({ search: { search } })
455 467
456 expect(res.body.total).to.equal(1) 468 expect(body.total).to.equal(1)
457 expect(res.body.data[0].name).to.equal('1111 2222 3333 - 3') 469 expect(body.data[0].name).to.equal('1111 2222 3333 - 3')
458 }) 470 })
459 471
460 it('Should search by live', async function () { 472 it('Should search by live', async function () {
461 this.timeout(30000) 473 this.timeout(30000)
462 474
463 { 475 {
464 const options = { 476 const newConfig = {
465 search: { 477 search: {
466 searchIndex: { enabled: false } 478 searchIndex: { enabled: false }
467 }, 479 },
468 live: { enabled: true } 480 live: { enabled: true }
469 } 481 }
470 await updateCustomSubConfig(server.url, server.accessToken, options) 482 await server.config.updateCustomSubConfig({ newConfig })
471 } 483 }
472 484
473 { 485 {
474 const res = await advancedVideosSearch(server.url, { isLive: true }) 486 const body = await command.advancedVideoSearch({ search: { isLive: true } })
475 487
476 expect(res.body.total).to.equal(0) 488 expect(body.total).to.equal(0)
477 expect(res.body.data).to.have.lengthOf(0) 489 expect(body.data).to.have.lengthOf(0)
478 } 490 }
479 491
480 { 492 {
481 const liveOptions = { name: 'live', privacy: VideoPrivacy.PUBLIC, channelId: server.videoChannel.id } 493 const liveCommand = server.live
482 const resLive = await createLive(server.url, server.accessToken, liveOptions) 494
483 const liveVideoId = resLive.body.video.uuid 495 const liveAttributes = { name: 'live', privacy: VideoPrivacy.PUBLIC, channelId: server.store.channel.id }
496 const live = await liveCommand.create({ fields: liveAttributes })
484 497
485 const command = await sendRTMPStreamInVideo(server.url, server.accessToken, liveVideoId) 498 const ffmpegCommand = await liveCommand.sendRTMPStreamInVideo({ videoId: live.id })
486 await waitUntilLivePublished(server.url, server.accessToken, liveVideoId) 499 await liveCommand.waitUntilPublished({ videoId: live.id })
487 500
488 const res = await advancedVideosSearch(server.url, { isLive: true }) 501 const body = await command.advancedVideoSearch({ search: { isLive: true } })
489 502
490 expect(res.body.total).to.equal(1) 503 expect(body.total).to.equal(1)
491 expect(res.body.data[0].name).to.equal('live') 504 expect(body.data[0].name).to.equal('live')
492 505
493 await stopFfmpeg(command) 506 await stopFfmpeg(ffmpegCommand)
494 } 507 }
495 }) 508 })
496 509
diff --git a/server/tests/api/server/auto-follows.ts b/server/tests/api/server/auto-follows.ts
index 1519b263f..ce7b51925 100644
--- a/server/tests/api/server/auto-follows.ts
+++ b/server/tests/api/server/auto-follows.ts
@@ -3,64 +3,45 @@
3import 'mocha' 3import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5import { 5import {
6 acceptFollower,
7 cleanupTests, 6 cleanupTests,
8 flushAndRunMultipleServers, 7 createMultipleServers,
9 MockInstancesIndex, 8 MockInstancesIndex,
10 ServerInfo, 9 PeerTubeServer,
11 setAccessTokensToServers, 10 setAccessTokensToServers,
12 unfollow, 11 wait,
13 updateCustomSubConfig, 12 waitJobs
14 wait 13} from '@shared/extra-utils'
15} from '../../../../shared/extra-utils/index'
16import { follow, getFollowersListPaginationAndSort, getFollowingListPaginationAndSort } from '../../../../shared/extra-utils/server/follows'
17import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
18import { ActorFollow } from '../../../../shared/models/actors'
19 14
20const expect = chai.expect 15const expect = chai.expect
21 16
22async function checkFollow (follower: ServerInfo, following: ServerInfo, exists: boolean) { 17async function checkFollow (follower: PeerTubeServer, following: PeerTubeServer, exists: boolean) {
23 { 18 {
24 const res = await getFollowersListPaginationAndSort({ url: following.url, start: 0, count: 5, sort: '-createdAt' }) 19 const body = await following.follows.getFollowers({ start: 0, count: 5, sort: '-createdAt' })
25 const follows = res.body.data as ActorFollow[] 20 const follow = body.data.find(f => f.follower.host === follower.host && f.state === 'accepted')
26 21
27 const follow = follows.find(f => { 22 if (exists === true) expect(follow).to.exist
28 return f.follower.host === follower.host && f.state === 'accepted' 23 else expect(follow).to.be.undefined
29 })
30
31 if (exists === true) {
32 expect(follow).to.exist
33 } else {
34 expect(follow).to.be.undefined
35 }
36 } 24 }
37 25
38 { 26 {
39 const res = await getFollowingListPaginationAndSort({ url: follower.url, start: 0, count: 5, sort: '-createdAt' }) 27 const body = await follower.follows.getFollowings({ start: 0, count: 5, sort: '-createdAt' })
40 const follows = res.body.data as ActorFollow[] 28 const follow = body.data.find(f => f.following.host === following.host && f.state === 'accepted')
41
42 const follow = follows.find(f => {
43 return f.following.host === following.host && f.state === 'accepted'
44 })
45 29
46 if (exists === true) { 30 if (exists === true) expect(follow).to.exist
47 expect(follow).to.exist 31 else expect(follow).to.be.undefined
48 } else {
49 expect(follow).to.be.undefined
50 }
51 } 32 }
52} 33}
53 34
54async function server1Follows2 (servers: ServerInfo[]) { 35async function server1Follows2 (servers: PeerTubeServer[]) {
55 await follow(servers[0].url, [ servers[1].host ], servers[0].accessToken) 36 await servers[0].follows.follow({ hosts: [ servers[1].host ] })
56 37
57 await waitJobs(servers) 38 await waitJobs(servers)
58} 39}
59 40
60async function resetFollows (servers: ServerInfo[]) { 41async function resetFollows (servers: PeerTubeServer[]) {
61 try { 42 try {
62 await unfollow(servers[0].url, servers[0].accessToken, servers[1]) 43 await servers[0].follows.unfollow({ target: servers[1] })
63 await unfollow(servers[1].url, servers[1].accessToken, servers[0]) 44 await servers[1].follows.unfollow({ target: servers[0] })
64 } catch { /* empty */ 45 } catch { /* empty */
65 } 46 }
66 47
@@ -71,12 +52,12 @@ async function resetFollows (servers: ServerInfo[]) {
71} 52}
72 53
73describe('Test auto follows', function () { 54describe('Test auto follows', function () {
74 let servers: ServerInfo[] = [] 55 let servers: PeerTubeServer[] = []
75 56
76 before(async function () { 57 before(async function () {
77 this.timeout(30000) 58 this.timeout(30000)
78 59
79 servers = await flushAndRunMultipleServers(3) 60 servers = await createMultipleServers(3)
80 61
81 // Get the access tokens 62 // Get the access tokens
82 await setAccessTokensToServers(servers) 63 await setAccessTokensToServers(servers)
@@ -105,7 +86,7 @@ describe('Test auto follows', function () {
105 } 86 }
106 } 87 }
107 } 88 }
108 await updateCustomSubConfig(servers[1].url, servers[1].accessToken, config) 89 await servers[1].config.updateCustomSubConfig({ newConfig: config })
109 90
110 await server1Follows2(servers) 91 await server1Follows2(servers)
111 92
@@ -130,14 +111,14 @@ describe('Test auto follows', function () {
130 } 111 }
131 } 112 }
132 } 113 }
133 await updateCustomSubConfig(servers[1].url, servers[1].accessToken, config) 114 await servers[1].config.updateCustomSubConfig({ newConfig: config })
134 115
135 await server1Follows2(servers) 116 await server1Follows2(servers)
136 117
137 await checkFollow(servers[0], servers[1], false) 118 await checkFollow(servers[0], servers[1], false)
138 await checkFollow(servers[1], servers[0], false) 119 await checkFollow(servers[1], servers[0], false)
139 120
140 await acceptFollower(servers[1].url, servers[1].accessToken, 'peertube@' + servers[0].host) 121 await servers[1].follows.acceptFollower({ follower: 'peertube@' + servers[0].host })
141 await waitJobs(servers) 122 await waitJobs(servers)
142 123
143 await checkFollow(servers[0], servers[1], true) 124 await checkFollow(servers[0], servers[1], true)
@@ -147,7 +128,7 @@ describe('Test auto follows', function () {
147 128
148 config.followings.instance.autoFollowBack.enabled = false 129 config.followings.instance.autoFollowBack.enabled = false
149 config.followers.instance.manualApproval = false 130 config.followers.instance.manualApproval = false
150 await updateCustomSubConfig(servers[1].url, servers[1].accessToken, config) 131 await servers[1].config.updateCustomSubConfig({ newConfig: config })
151 }) 132 })
152 }) 133 })
153 134
@@ -184,7 +165,7 @@ describe('Test auto follows', function () {
184 } 165 }
185 } 166 }
186 } 167 }
187 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, config) 168 await servers[0].config.updateCustomSubConfig({ newConfig: config })
188 169
189 await wait(5000) 170 await wait(5000)
190 await waitJobs(servers) 171 await waitJobs(servers)
diff --git a/server/tests/api/server/bulk.ts b/server/tests/api/server/bulk.ts
index 80fa7fce6..16cbcd5c3 100644
--- a/server/tests/api/server/bulk.ts
+++ b/server/tests/api/server/bulk.ts
@@ -2,91 +2,83 @@
2 2
3import 'mocha' 3import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5import { Video, VideoComment } from '@shared/models'
6import { 5import {
7 addVideoCommentReply, 6 BulkCommand,
8 addVideoCommentThread,
9 bulkRemoveCommentsOf,
10 cleanupTests, 7 cleanupTests,
11 createUser, 8 createMultipleServers,
12 doubleFollow, 9 doubleFollow,
13 flushAndRunMultipleServers, 10 PeerTubeServer,
14 getVideoCommentThreads,
15 getVideosList,
16 ServerInfo,
17 setAccessTokensToServers, 11 setAccessTokensToServers,
18 uploadVideo,
19 userLogin,
20 waitJobs 12 waitJobs
21} from '../../../../shared/extra-utils/index' 13} from '@shared/extra-utils'
22 14
23const expect = chai.expect 15const expect = chai.expect
24 16
25describe('Test bulk actions', function () { 17describe('Test bulk actions', function () {
26 const commentsUser3: { videoId: number, commentId: number }[] = [] 18 const commentsUser3: { videoId: number, commentId: number }[] = []
27 19
28 let servers: ServerInfo[] = [] 20 let servers: PeerTubeServer[] = []
29 let user1AccessToken: string 21 let user1Token: string
30 let user2AccessToken: string 22 let user2Token: string
31 let user3AccessToken: string 23 let user3Token: string
24
25 let bulkCommand: BulkCommand
32 26
33 before(async function () { 27 before(async function () {
34 this.timeout(30000) 28 this.timeout(30000)
35 29
36 servers = await flushAndRunMultipleServers(2) 30 servers = await createMultipleServers(2)
37 31
38 // Get the access tokens 32 // Get the access tokens
39 await setAccessTokensToServers(servers) 33 await setAccessTokensToServers(servers)
40 34
41 { 35 {
42 const user = { username: 'user1', password: 'password' } 36 const user = { username: 'user1', password: 'password' }
43 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: user.username, password: user.password }) 37 await servers[0].users.create({ username: user.username, password: user.password })
44 38
45 user1AccessToken = await userLogin(servers[0], user) 39 user1Token = await servers[0].login.getAccessToken(user)
46 } 40 }
47 41
48 { 42 {
49 const user = { username: 'user2', password: 'password' } 43 const user = { username: 'user2', password: 'password' }
50 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: user.username, password: user.password }) 44 await servers[0].users.create({ username: user.username, password: user.password })
51 45
52 user2AccessToken = await userLogin(servers[0], user) 46 user2Token = await servers[0].login.getAccessToken(user)
53 } 47 }
54 48
55 { 49 {
56 const user = { username: 'user3', password: 'password' } 50 const user = { username: 'user3', password: 'password' }
57 await createUser({ url: servers[1].url, accessToken: servers[1].accessToken, username: user.username, password: user.password }) 51 await servers[1].users.create({ username: user.username, password: user.password })
58 52
59 user3AccessToken = await userLogin(servers[1], user) 53 user3Token = await servers[1].login.getAccessToken(user)
60 } 54 }
61 55
62 await doubleFollow(servers[0], servers[1]) 56 await doubleFollow(servers[0], servers[1])
57
58 bulkCommand = new BulkCommand(servers[0])
63 }) 59 })
64 60
65 describe('Bulk remove comments', function () { 61 describe('Bulk remove comments', function () {
66 async function checkInstanceCommentsRemoved () { 62 async function checkInstanceCommentsRemoved () {
67 { 63 {
68 const res = await getVideosList(servers[0].url) 64 const { data } = await servers[0].videos.list()
69 const videos = res.body.data as Video[]
70 65
71 // Server 1 should not have these comments anymore 66 // Server 1 should not have these comments anymore
72 for (const video of videos) { 67 for (const video of data) {
73 const resThreads = await getVideoCommentThreads(servers[0].url, video.id, 0, 10) 68 const { data } = await servers[0].comments.listThreads({ videoId: video.id })
74 const comments = resThreads.body.data as VideoComment[] 69 const comment = data.find(c => c.text === 'comment by user 3')
75 const comment = comments.find(c => c.text === 'comment by user 3')
76 70
77 expect(comment).to.not.exist 71 expect(comment).to.not.exist
78 } 72 }
79 } 73 }
80 74
81 { 75 {
82 const res = await getVideosList(servers[1].url) 76 const { data } = await servers[1].videos.list()
83 const videos = res.body.data as Video[]
84 77
85 // Server 1 should not have these comments on videos of server 1 78 // Server 1 should not have these comments on videos of server 1
86 for (const video of videos) { 79 for (const video of data) {
87 const resThreads = await getVideoCommentThreads(servers[1].url, video.id, 0, 10) 80 const { data } = await servers[1].comments.listThreads({ videoId: video.id })
88 const comments = resThreads.body.data as VideoComment[] 81 const comment = data.find(c => c.text === 'comment by user 3')
89 const comment = comments.find(c => c.text === 'comment by user 3')
90 82
91 if (video.account.host === 'localhost:' + servers[0].port) { 83 if (video.account.host === 'localhost:' + servers[0].port) {
92 expect(comment).to.not.exist 84 expect(comment).to.not.exist
@@ -100,30 +92,31 @@ describe('Test bulk actions', function () {
100 before(async function () { 92 before(async function () {
101 this.timeout(120000) 93 this.timeout(120000)
102 94
103 await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video 1 server 1' }) 95 await servers[0].videos.upload({ attributes: { name: 'video 1 server 1' } })
104 await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video 2 server 1' }) 96 await servers[0].videos.upload({ attributes: { name: 'video 2 server 1' } })
105 await uploadVideo(servers[0].url, user1AccessToken, { name: 'video 3 server 1' }) 97 await servers[0].videos.upload({ token: user1Token, attributes: { name: 'video 3 server 1' } })
106 98
107 await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'video 1 server 2' }) 99 await servers[1].videos.upload({ attributes: { name: 'video 1 server 2' } })
108 100
109 await waitJobs(servers) 101 await waitJobs(servers)
110 102
111 { 103 {
112 const res = await getVideosList(servers[0].url) 104 const { data } = await servers[0].videos.list()
113 for (const video of res.body.data) { 105 for (const video of data) {
114 await addVideoCommentThread(servers[0].url, servers[0].accessToken, video.id, 'comment by root server 1') 106 await servers[0].comments.createThread({ videoId: video.id, text: 'comment by root server 1' })
115 await addVideoCommentThread(servers[0].url, user1AccessToken, video.id, 'comment by user 1') 107 await servers[0].comments.createThread({ token: user1Token, videoId: video.id, text: 'comment by user 1' })
116 await addVideoCommentThread(servers[0].url, user2AccessToken, video.id, 'comment by user 2') 108 await servers[0].comments.createThread({ token: user2Token, videoId: video.id, text: 'comment by user 2' })
117 } 109 }
118 } 110 }
119 111
120 { 112 {
121 const res = await getVideosList(servers[1].url) 113 const { data } = await servers[1].videos.list()
122 for (const video of res.body.data) {
123 await addVideoCommentThread(servers[1].url, servers[1].accessToken, video.id, 'comment by root server 2')
124 114
125 const res = await addVideoCommentThread(servers[1].url, user3AccessToken, video.id, 'comment by user 3') 115 for (const video of data) {
126 commentsUser3.push({ videoId: video.id, commentId: res.body.comment.id }) 116 await servers[1].comments.createThread({ videoId: video.id, text: 'comment by root server 2' })
117
118 const comment = await servers[1].comments.createThread({ token: user3Token, videoId: video.id, text: 'comment by user 3' })
119 commentsUser3.push({ videoId: video.id, commentId: comment.id })
127 } 120 }
128 } 121 }
129 122
@@ -133,9 +126,8 @@ describe('Test bulk actions', function () {
133 it('Should delete comments of an account on my videos', async function () { 126 it('Should delete comments of an account on my videos', async function () {
134 this.timeout(60000) 127 this.timeout(60000)
135 128
136 await bulkRemoveCommentsOf({ 129 await bulkCommand.removeCommentsOf({
137 url: servers[0].url, 130 token: user1Token,
138 token: user1AccessToken,
139 attributes: { 131 attributes: {
140 accountName: 'user2', 132 accountName: 'user2',
141 scope: 'my-videos' 133 scope: 'my-videos'
@@ -145,18 +137,14 @@ describe('Test bulk actions', function () {
145 await waitJobs(servers) 137 await waitJobs(servers)
146 138
147 for (const server of servers) { 139 for (const server of servers) {
148 const res = await getVideosList(server.url) 140 const { data } = await server.videos.list()
149 141
150 for (const video of res.body.data) { 142 for (const video of data) {
151 const resThreads = await getVideoCommentThreads(server.url, video.id, 0, 10) 143 const { data } = await server.comments.listThreads({ videoId: video.id })
152 const comments = resThreads.body.data as VideoComment[] 144 const comment = data.find(c => c.text === 'comment by user 2')
153 const comment = comments.find(c => c.text === 'comment by user 2')
154 145
155 if (video.name === 'video 3 server 1') { 146 if (video.name === 'video 3 server 1') expect(comment).to.not.exist
156 expect(comment).to.not.exist 147 else expect(comment).to.exist
157 } else {
158 expect(comment).to.exist
159 }
160 } 148 }
161 } 149 }
162 }) 150 })
@@ -164,9 +152,7 @@ describe('Test bulk actions', function () {
164 it('Should delete comments of an account on the instance', async function () { 152 it('Should delete comments of an account on the instance', async function () {
165 this.timeout(60000) 153 this.timeout(60000)
166 154
167 await bulkRemoveCommentsOf({ 155 await bulkCommand.removeCommentsOf({
168 url: servers[0].url,
169 token: servers[0].accessToken,
170 attributes: { 156 attributes: {
171 accountName: 'user3@localhost:' + servers[1].port, 157 accountName: 'user3@localhost:' + servers[1].port,
172 scope: 'instance' 158 scope: 'instance'
@@ -182,7 +168,12 @@ describe('Test bulk actions', function () {
182 this.timeout(60000) 168 this.timeout(60000)
183 169
184 for (const obj of commentsUser3) { 170 for (const obj of commentsUser3) {
185 await addVideoCommentReply(servers[1].url, user3AccessToken, obj.videoId, obj.commentId, 'comment by user 3 bis') 171 await servers[1].comments.addReply({
172 token: user3Token,
173 videoId: obj.videoId,
174 toCommentId: obj.commentId,
175 text: 'comment by user 3 bis'
176 })
186 } 177 }
187 178
188 await waitJobs(servers) 179 await waitJobs(servers)
diff --git a/server/tests/api/server/config.ts b/server/tests/api/server/config.ts
index 19bf9582c..fd61e95df 100644
--- a/server/tests/api/server/config.ts
+++ b/server/tests/api/server/config.ts
@@ -2,31 +2,20 @@
2 2
3import 'mocha' 3import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5import { About } from '../../../../shared/models/server/about.model'
6import { CustomConfig } from '../../../../shared/models/server/custom-config.model'
7import { 5import {
8 cleanupTests, 6 cleanupTests,
9 deleteCustomConfig, 7 createSingleServer,
10 flushAndRunServer,
11 getAbout,
12 getConfig,
13 getCustomConfig,
14 killallServers, 8 killallServers,
15 makeGetRequest, 9 makeGetRequest,
16 parallelTests, 10 parallelTests,
17 registerUser, 11 PeerTubeServer,
18 reRunServer, 12 setAccessTokensToServers
19 ServerInfo, 13} from '@shared/extra-utils'
20 setAccessTokensToServers, 14import { CustomConfig, HttpStatusCode } from '@shared/models'
21 updateCustomConfig,
22 uploadVideo
23} from '../../../../shared/extra-utils'
24import { ServerConfig } from '../../../../shared/models'
25import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
26 15
27const expect = chai.expect 16const expect = chai.expect
28 17
29function checkInitialConfig (server: ServerInfo, data: CustomConfig) { 18function checkInitialConfig (server: PeerTubeServer, data: CustomConfig) {
30 expect(data.instance.name).to.equal('PeerTube') 19 expect(data.instance.name).to.equal('PeerTube')
31 expect(data.instance.shortDescription).to.equal( 20 expect(data.instance.shortDescription).to.equal(
32 'PeerTube, an ActivityPub-federated video streaming platform using P2P directly in your web browser.' 21 'PeerTube, an ActivityPub-federated video streaming platform using P2P directly in your web browser.'
@@ -213,18 +202,17 @@ function checkUpdatedConfig (data: CustomConfig) {
213} 202}
214 203
215describe('Test config', function () { 204describe('Test config', function () {
216 let server = null 205 let server: PeerTubeServer = null
217 206
218 before(async function () { 207 before(async function () {
219 this.timeout(30000) 208 this.timeout(30000)
220 209
221 server = await flushAndRunServer(1) 210 server = await createSingleServer(1)
222 await setAccessTokensToServers([ server ]) 211 await setAccessTokensToServers([ server ])
223 }) 212 })
224 213
225 it('Should have a correct config on a server with registration enabled', async function () { 214 it('Should have a correct config on a server with registration enabled', async function () {
226 const res = await getConfig(server.url) 215 const data = await server.config.getConfig()
227 const data: ServerConfig = res.body
228 216
229 expect(data.signup.allowed).to.be.true 217 expect(data.signup.allowed).to.be.true
230 }) 218 })
@@ -233,35 +221,32 @@ describe('Test config', function () {
233 this.timeout(5000) 221 this.timeout(5000)
234 222
235 await Promise.all([ 223 await Promise.all([
236 registerUser(server.url, 'user1', 'super password'), 224 server.users.register({ username: 'user1' }),
237 registerUser(server.url, 'user2', 'super password'), 225 server.users.register({ username: 'user2' }),
238 registerUser(server.url, 'user3', 'super password') 226 server.users.register({ username: 'user3' })
239 ]) 227 ])
240 228
241 const res = await getConfig(server.url) 229 const data = await server.config.getConfig()
242 const data: ServerConfig = res.body
243 230
244 expect(data.signup.allowed).to.be.false 231 expect(data.signup.allowed).to.be.false
245 }) 232 })
246 233
247 it('Should have the correct video allowed extensions', async function () { 234 it('Should have the correct video allowed extensions', async function () {
248 const res = await getConfig(server.url) 235 const data = await server.config.getConfig()
249 const data: ServerConfig = res.body
250 236
251 expect(data.video.file.extensions).to.have.lengthOf(3) 237 expect(data.video.file.extensions).to.have.lengthOf(3)
252 expect(data.video.file.extensions).to.contain('.mp4') 238 expect(data.video.file.extensions).to.contain('.mp4')
253 expect(data.video.file.extensions).to.contain('.webm') 239 expect(data.video.file.extensions).to.contain('.webm')
254 expect(data.video.file.extensions).to.contain('.ogv') 240 expect(data.video.file.extensions).to.contain('.ogv')
255 241
256 await uploadVideo(server.url, server.accessToken, { fixture: 'video_short.mkv' }, HttpStatusCode.UNSUPPORTED_MEDIA_TYPE_415) 242 await server.videos.upload({ attributes: { fixture: 'video_short.mkv' }, expectedStatus: HttpStatusCode.UNSUPPORTED_MEDIA_TYPE_415 })
257 await uploadVideo(server.url, server.accessToken, { fixture: 'sample.ogg' }, HttpStatusCode.UNSUPPORTED_MEDIA_TYPE_415) 243 await server.videos.upload({ attributes: { fixture: 'sample.ogg' }, expectedStatus: HttpStatusCode.UNSUPPORTED_MEDIA_TYPE_415 })
258 244
259 expect(data.contactForm.enabled).to.be.true 245 expect(data.contactForm.enabled).to.be.true
260 }) 246 })
261 247
262 it('Should get the customized configuration', async function () { 248 it('Should get the customized configuration', async function () {
263 const res = await getCustomConfig(server.url, server.accessToken) 249 const data = await server.config.getCustomConfig()
264 const data = res.body as CustomConfig
265 250
266 checkInitialConfig(server, data) 251 checkInitialConfig(server, data)
267 }) 252 })
@@ -438,19 +423,16 @@ describe('Test config', function () {
438 } 423 }
439 } 424 }
440 } 425 }
441 await updateCustomConfig(server.url, server.accessToken, newCustomConfig) 426 await server.config.updateCustomConfig({ newCustomConfig })
442
443 const res = await getCustomConfig(server.url, server.accessToken)
444 const data = res.body
445 427
428 const data = await server.config.getCustomConfig()
446 checkUpdatedConfig(data) 429 checkUpdatedConfig(data)
447 }) 430 })
448 431
449 it('Should have the correct updated video allowed extensions', async function () { 432 it('Should have the correct updated video allowed extensions', async function () {
450 this.timeout(10000) 433 this.timeout(10000)
451 434
452 const res = await getConfig(server.url) 435 const data = await server.config.getConfig()
453 const data: ServerConfig = res.body
454 436
455 expect(data.video.file.extensions).to.have.length.above(4) 437 expect(data.video.file.extensions).to.have.length.above(4)
456 expect(data.video.file.extensions).to.contain('.mp4') 438 expect(data.video.file.extensions).to.contain('.mp4')
@@ -463,26 +445,24 @@ describe('Test config', function () {
463 expect(data.video.file.extensions).to.contain('.ogg') 445 expect(data.video.file.extensions).to.contain('.ogg')
464 expect(data.video.file.extensions).to.contain('.flac') 446 expect(data.video.file.extensions).to.contain('.flac')
465 447
466 await uploadVideo(server.url, server.accessToken, { fixture: 'video_short.mkv' }, HttpStatusCode.OK_200) 448 await server.videos.upload({ attributes: { fixture: 'video_short.mkv' }, expectedStatus: HttpStatusCode.OK_200 })
467 await uploadVideo(server.url, server.accessToken, { fixture: 'sample.ogg' }, HttpStatusCode.OK_200) 449 await server.videos.upload({ attributes: { fixture: 'sample.ogg' }, expectedStatus: HttpStatusCode.OK_200 })
468 }) 450 })
469 451
470 it('Should have the configuration updated after a restart', async function () { 452 it('Should have the configuration updated after a restart', async function () {
471 this.timeout(10000) 453 this.timeout(10000)
472 454
473 killallServers([ server ]) 455 await killallServers([ server ])
474 456
475 await reRunServer(server) 457 await server.run()
476 458
477 const res = await getCustomConfig(server.url, server.accessToken) 459 const data = await server.config.getCustomConfig()
478 const data = res.body
479 460
480 checkUpdatedConfig(data) 461 checkUpdatedConfig(data)
481 }) 462 })
482 463
483 it('Should fetch the about information', async function () { 464 it('Should fetch the about information', async function () {
484 const res = await getAbout(server.url) 465 const data = await server.config.getAbout()
485 const data: About = res.body
486 466
487 expect(data.instance.name).to.equal('PeerTube updated') 467 expect(data.instance.name).to.equal('PeerTube updated')
488 expect(data.instance.shortDescription).to.equal('my short description') 468 expect(data.instance.shortDescription).to.equal('my short description')
@@ -504,11 +484,9 @@ describe('Test config', function () {
504 it('Should remove the custom configuration', async function () { 484 it('Should remove the custom configuration', async function () {
505 this.timeout(10000) 485 this.timeout(10000)
506 486
507 await deleteCustomConfig(server.url, server.accessToken) 487 await server.config.deleteCustomConfig()
508
509 const res = await getCustomConfig(server.url, server.accessToken)
510 const data = res.body
511 488
489 const data = await server.config.getCustomConfig()
512 checkInitialConfig(server, data) 490 checkInitialConfig(server, data)
513 }) 491 })
514 492
@@ -519,26 +497,26 @@ describe('Test config', function () {
519 const res = await makeGetRequest({ 497 const res = await makeGetRequest({
520 url: server.url, 498 url: server.url,
521 path: '/api/v1/config', 499 path: '/api/v1/config',
522 statusCodeExpected: 200 500 expectedStatus: 200
523 }) 501 })
524 502
525 expect(res.headers['x-frame-options']).to.exist 503 expect(res.headers['x-frame-options']).to.exist
526 } 504 }
527 505
528 killallServers([ server ]) 506 await killallServers([ server ])
529 507
530 const config = { 508 const config = {
531 security: { 509 security: {
532 frameguard: { enabled: false } 510 frameguard: { enabled: false }
533 } 511 }
534 } 512 }
535 server = await reRunServer(server, config) 513 await server.run(config)
536 514
537 { 515 {
538 const res = await makeGetRequest({ 516 const res = await makeGetRequest({
539 url: server.url, 517 url: server.url,
540 path: '/api/v1/config', 518 path: '/api/v1/config',
541 statusCodeExpected: 200 519 expectedStatus: 200
542 }) 520 })
543 521
544 expect(res.headers['x-frame-options']).to.not.exist 522 expect(res.headers['x-frame-options']).to.not.exist
diff --git a/server/tests/api/server/contact-form.ts b/server/tests/api/server/contact-form.ts
index 8851ad55e..c555661ad 100644
--- a/server/tests/api/server/contact-form.ts
+++ b/server/tests/api/server/contact-form.ts
@@ -1,18 +1,25 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2 2
3import * as chai from 'chai'
4import 'mocha' 3import 'mocha'
5import { cleanupTests, flushAndRunServer, ServerInfo, setAccessTokensToServers, wait } from '../../../../shared/extra-utils' 4import * as chai from 'chai'
6import { MockSmtpServer } from '../../../../shared/extra-utils/miscs/email' 5import {
7import { waitJobs } from '../../../../shared/extra-utils/server/jobs' 6 cleanupTests,
8import { sendContactForm } from '../../../../shared/extra-utils/server/contact-form' 7 ContactFormCommand,
9import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes' 8 createSingleServer,
9 MockSmtpServer,
10 PeerTubeServer,
11 setAccessTokensToServers,
12 wait,
13 waitJobs
14} from '@shared/extra-utils'
15import { HttpStatusCode } from '@shared/models'
10 16
11const expect = chai.expect 17const expect = chai.expect
12 18
13describe('Test contact form', function () { 19describe('Test contact form', function () {
14 let server: ServerInfo 20 let server: PeerTubeServer
15 const emails: object[] = [] 21 const emails: object[] = []
22 let command: ContactFormCommand
16 23
17 before(async function () { 24 before(async function () {
18 this.timeout(30000) 25 this.timeout(30000)
@@ -25,15 +32,16 @@ describe('Test contact form', function () {
25 port 32 port
26 } 33 }
27 } 34 }
28 server = await flushAndRunServer(1, overrideConfig) 35 server = await createSingleServer(1, overrideConfig)
29 await setAccessTokensToServers([ server ]) 36 await setAccessTokensToServers([ server ])
37
38 command = server.contactForm
30 }) 39 })
31 40
32 it('Should send a contact form', async function () { 41 it('Should send a contact form', async function () {
33 this.timeout(10000) 42 this.timeout(10000)
34 43
35 await sendContactForm({ 44 await command.send({
36 url: server.url,
37 fromEmail: 'toto@example.com', 45 fromEmail: 'toto@example.com',
38 body: 'my super message', 46 body: 'my super message',
39 subject: 'my subject', 47 subject: 'my subject',
@@ -58,16 +66,14 @@ describe('Test contact form', function () {
58 66
59 await wait(1000) 67 await wait(1000)
60 68
61 await sendContactForm({ 69 await command.send({
62 url: server.url,
63 fromEmail: 'toto@example.com', 70 fromEmail: 'toto@example.com',
64 body: 'my super message', 71 body: 'my super message',
65 subject: 'my subject', 72 subject: 'my subject',
66 fromName: 'Super toto' 73 fromName: 'Super toto'
67 }) 74 })
68 75
69 await sendContactForm({ 76 await command.send({
70 url: server.url,
71 fromEmail: 'toto@example.com', 77 fromEmail: 'toto@example.com',
72 body: 'my super message', 78 body: 'my super message',
73 fromName: 'Super toto', 79 fromName: 'Super toto',
@@ -79,8 +85,7 @@ describe('Test contact form', function () {
79 it('Should be able to send another contact form after a while', async function () { 85 it('Should be able to send another contact form after a while', async function () {
80 await wait(1000) 86 await wait(1000)
81 87
82 await sendContactForm({ 88 await command.send({
83 url: server.url,
84 fromEmail: 'toto@example.com', 89 fromEmail: 'toto@example.com',
85 fromName: 'Super toto', 90 fromName: 'Super toto',
86 subject: 'my subject', 91 subject: 'my subject',
diff --git a/server/tests/api/server/email.ts b/server/tests/api/server/email.ts
index 92768d9df..ae86b380f 100644
--- a/server/tests/api/server/email.ts
+++ b/server/tests/api/server/email.ts
@@ -2,32 +2,13 @@
2 2
3import 'mocha' 3import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5import { 5import { cleanupTests, createSingleServer, MockSmtpServer, PeerTubeServer, setAccessTokensToServers, waitJobs } from '@shared/extra-utils'
6 addVideoToBlacklist, 6import { HttpStatusCode } from '@shared/models'
7 askResetPassword,
8 askSendVerifyEmail,
9 blockUser,
10 cleanupTests,
11 createUser,
12 flushAndRunServer,
13 removeVideoFromBlacklist,
14 reportAbuse,
15 resetPassword,
16 ServerInfo,
17 setAccessTokensToServers,
18 unblockUser,
19 uploadVideo,
20 userLogin,
21 verifyEmail
22} from '../../../../shared/extra-utils'
23import { MockSmtpServer } from '../../../../shared/extra-utils/miscs/email'
24import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
25import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
26 7
27const expect = chai.expect 8const expect = chai.expect
28 9
29describe('Test emails', function () { 10describe('Test emails', function () {
30 let server: ServerInfo 11 let server: PeerTubeServer
31 let userId: number 12 let userId: number
32 let userId2: number 13 let userId2: number
33 let userAccessToken: string 14 let userAccessToken: string
@@ -58,31 +39,29 @@ describe('Test emails', function () {
58 port: emailPort 39 port: emailPort
59 } 40 }
60 } 41 }
61 server = await flushAndRunServer(1, overrideConfig) 42 server = await createSingleServer(1, overrideConfig)
62 await setAccessTokensToServers([ server ]) 43 await setAccessTokensToServers([ server ])
63 44
64 { 45 {
65 const res = await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password }) 46 const created = await server.users.create({ username: user.username, password: user.password })
66 userId = res.body.user.id 47 userId = created.id
67 48
68 userAccessToken = await userLogin(server, user) 49 userAccessToken = await server.login.getAccessToken(user)
69 } 50 }
70 51
71 { 52 {
72 const attributes = { 53 const attributes = { name: 'my super user video' }
73 name: 'my super user video' 54 const { uuid } = await server.videos.upload({ token: userAccessToken, attributes })
74 } 55 videoUserUUID = uuid
75 const res = await uploadVideo(server.url, userAccessToken, attributes)
76 videoUserUUID = res.body.video.uuid
77 } 56 }
78 57
79 { 58 {
80 const attributes = { 59 const attributes = {
81 name: 'my super name' 60 name: 'my super name'
82 } 61 }
83 const res = await uploadVideo(server.url, server.accessToken, attributes) 62 const { uuid, id } = await server.videos.upload({ attributes })
84 videoUUID = res.body.video.uuid 63 videoUUID = uuid
85 videoId = res.body.video.id 64 videoId = id
86 } 65 }
87 }) 66 })
88 67
@@ -91,7 +70,7 @@ describe('Test emails', function () {
91 it('Should ask to reset the password', async function () { 70 it('Should ask to reset the password', async function () {
92 this.timeout(10000) 71 this.timeout(10000)
93 72
94 await askResetPassword(server.url, 'user_1@example.com') 73 await server.users.askResetPassword({ email: 'user_1@example.com' })
95 74
96 await waitJobs(server) 75 await waitJobs(server)
97 expect(emails).to.have.lengthOf(1) 76 expect(emails).to.have.lengthOf(1)
@@ -117,34 +96,40 @@ describe('Test emails', function () {
117 }) 96 })
118 97
119 it('Should not reset the password with an invalid verification string', async function () { 98 it('Should not reset the password with an invalid verification string', async function () {
120 await resetPassword(server.url, userId, verificationString + 'b', 'super_password2', HttpStatusCode.FORBIDDEN_403) 99 await server.users.resetPassword({
100 userId,
101 verificationString: verificationString + 'b',
102 password: 'super_password2',
103 expectedStatus: HttpStatusCode.FORBIDDEN_403
104 })
121 }) 105 })
122 106
123 it('Should reset the password', async function () { 107 it('Should reset the password', async function () {
124 await resetPassword(server.url, userId, verificationString, 'super_password2') 108 await server.users.resetPassword({ userId, verificationString, password: 'super_password2' })
125 }) 109 })
126 110
127 it('Should not reset the password with the same verification string', async function () { 111 it('Should not reset the password with the same verification string', async function () {
128 await resetPassword(server.url, userId, verificationString, 'super_password3', HttpStatusCode.FORBIDDEN_403) 112 await server.users.resetPassword({
113 userId,
114 verificationString,
115 password: 'super_password3',
116 expectedStatus: HttpStatusCode.FORBIDDEN_403
117 })
129 }) 118 })
130 119
131 it('Should login with this new password', async function () { 120 it('Should login with this new password', async function () {
132 user.password = 'super_password2' 121 user.password = 'super_password2'
133 122
134 await userLogin(server, user) 123 await server.login.getAccessToken(user)
135 }) 124 })
136 }) 125 })
137 126
138 describe('When creating a user without password', function () { 127 describe('When creating a user without password', function () {
128
139 it('Should send a create password email', async function () { 129 it('Should send a create password email', async function () {
140 this.timeout(10000) 130 this.timeout(10000)
141 131
142 await createUser({ 132 await server.users.create({ username: 'create_password', password: '' })
143 url: server.url,
144 accessToken: server.accessToken,
145 username: 'create_password',
146 password: ''
147 })
148 133
149 await waitJobs(server) 134 await waitJobs(server)
150 expect(emails).to.have.lengthOf(2) 135 expect(emails).to.have.lengthOf(2)
@@ -170,15 +155,24 @@ describe('Test emails', function () {
170 }) 155 })
171 156
172 it('Should not reset the password with an invalid verification string', async function () { 157 it('Should not reset the password with an invalid verification string', async function () {
173 await resetPassword(server.url, userId2, verificationString2 + 'c', 'newly_created_password', HttpStatusCode.FORBIDDEN_403) 158 await server.users.resetPassword({
159 userId: userId2,
160 verificationString: verificationString2 + 'c',
161 password: 'newly_created_password',
162 expectedStatus: HttpStatusCode.FORBIDDEN_403
163 })
174 }) 164 })
175 165
176 it('Should reset the password', async function () { 166 it('Should reset the password', async function () {
177 await resetPassword(server.url, userId2, verificationString2, 'newly_created_password') 167 await server.users.resetPassword({
168 userId: userId2,
169 verificationString: verificationString2,
170 password: 'newly_created_password'
171 })
178 }) 172 })
179 173
180 it('Should login with this new password', async function () { 174 it('Should login with this new password', async function () {
181 await userLogin(server, { 175 await server.login.getAccessToken({
182 username: 'create_password', 176 username: 'create_password',
183 password: 'newly_created_password' 177 password: 'newly_created_password'
184 }) 178 })
@@ -190,7 +184,7 @@ describe('Test emails', function () {
190 this.timeout(10000) 184 this.timeout(10000)
191 185
192 const reason = 'my super bad reason' 186 const reason = 'my super bad reason'
193 await reportAbuse({ url: server.url, token: server.accessToken, videoId, reason }) 187 await server.abuses.report({ videoId, reason })
194 188
195 await waitJobs(server) 189 await waitJobs(server)
196 expect(emails).to.have.lengthOf(3) 190 expect(emails).to.have.lengthOf(3)
@@ -211,7 +205,7 @@ describe('Test emails', function () {
211 this.timeout(10000) 205 this.timeout(10000)
212 206
213 const reason = 'my super bad reason' 207 const reason = 'my super bad reason'
214 await blockUser(server.url, userId, server.accessToken, HttpStatusCode.NO_CONTENT_204, reason) 208 await server.users.banUser({ userId, reason })
215 209
216 await waitJobs(server) 210 await waitJobs(server)
217 expect(emails).to.have.lengthOf(4) 211 expect(emails).to.have.lengthOf(4)
@@ -229,7 +223,7 @@ describe('Test emails', function () {
229 it('Should send the notification email when unblocking a user', async function () { 223 it('Should send the notification email when unblocking a user', async function () {
230 this.timeout(10000) 224 this.timeout(10000)
231 225
232 await unblockUser(server.url, userId, server.accessToken, HttpStatusCode.NO_CONTENT_204) 226 await server.users.unbanUser({ userId })
233 227
234 await waitJobs(server) 228 await waitJobs(server)
235 expect(emails).to.have.lengthOf(5) 229 expect(emails).to.have.lengthOf(5)
@@ -249,7 +243,7 @@ describe('Test emails', function () {
249 this.timeout(10000) 243 this.timeout(10000)
250 244
251 const reason = 'my super reason' 245 const reason = 'my super reason'
252 await addVideoToBlacklist(server.url, server.accessToken, videoUserUUID, reason) 246 await server.blacklist.add({ videoId: videoUserUUID, reason })
253 247
254 await waitJobs(server) 248 await waitJobs(server)
255 expect(emails).to.have.lengthOf(6) 249 expect(emails).to.have.lengthOf(6)
@@ -267,7 +261,7 @@ describe('Test emails', function () {
267 it('Should send the notification email', async function () { 261 it('Should send the notification email', async function () {
268 this.timeout(10000) 262 this.timeout(10000)
269 263
270 await removeVideoFromBlacklist(server.url, server.accessToken, videoUserUUID) 264 await server.blacklist.remove({ videoId: videoUserUUID })
271 265
272 await waitJobs(server) 266 await waitJobs(server)
273 expect(emails).to.have.lengthOf(7) 267 expect(emails).to.have.lengthOf(7)
@@ -292,7 +286,7 @@ describe('Test emails', function () {
292 it('Should ask to send the verification email', async function () { 286 it('Should ask to send the verification email', async function () {
293 this.timeout(10000) 287 this.timeout(10000)
294 288
295 await askSendVerifyEmail(server.url, 'user_1@example.com') 289 await server.users.askSendVerifyEmail({ email: 'user_1@example.com' })
296 290
297 await waitJobs(server) 291 await waitJobs(server)
298 expect(emails).to.have.lengthOf(8) 292 expect(emails).to.have.lengthOf(8)
@@ -318,11 +312,16 @@ describe('Test emails', function () {
318 }) 312 })
319 313
320 it('Should not verify the email with an invalid verification string', async function () { 314 it('Should not verify the email with an invalid verification string', async function () {
321 await verifyEmail(server.url, userId, verificationString + 'b', false, HttpStatusCode.FORBIDDEN_403) 315 await server.users.verifyEmail({
316 userId,
317 verificationString: verificationString + 'b',
318 isPendingEmail: false,
319 expectedStatus: HttpStatusCode.FORBIDDEN_403
320 })
322 }) 321 })
323 322
324 it('Should verify the email', async function () { 323 it('Should verify the email', async function () {
325 await verifyEmail(server.url, userId, verificationString) 324 await server.users.verifyEmail({ userId, verificationString })
326 }) 325 })
327 }) 326 })
328 327
diff --git a/server/tests/api/server/follow-constraints.ts b/server/tests/api/server/follow-constraints.ts
index 3f2f71f46..471f5d8d0 100644
--- a/server/tests/api/server/follow-constraints.ts
+++ b/server/tests/api/server/follow-constraints.ts
@@ -1,56 +1,41 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2 2
3import * as chai from 'chai'
4import 'mocha' 3import 'mocha'
5import { 4import * as chai from 'chai'
6 cleanupTests, 5import { cleanupTests, createMultipleServers, doubleFollow, PeerTubeServer, setAccessTokensToServers } from '@shared/extra-utils'
7 doubleFollow, 6import { HttpStatusCode, PeerTubeProblemDocument, ServerErrorCode } from '@shared/models'
8 flushAndRunMultipleServers,
9 getAccountVideos,
10 getVideo,
11 getVideoChannelVideos,
12 getVideoWithToken,
13 ServerInfo,
14 setAccessTokensToServers,
15 uploadVideo
16} from '../../../../shared/extra-utils'
17import { unfollow } from '../../../../shared/extra-utils/server/follows'
18import { userLogin } from '../../../../shared/extra-utils/users/login'
19import { createUser } from '../../../../shared/extra-utils/users/users'
20import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
21import { PeerTubeProblemDocument, ServerErrorCode } from '@shared/models'
22 7
23const expect = chai.expect 8const expect = chai.expect
24 9
25describe('Test follow constraints', function () { 10describe('Test follow constraints', function () {
26 let servers: ServerInfo[] = [] 11 let servers: PeerTubeServer[] = []
27 let video1UUID: string 12 let video1UUID: string
28 let video2UUID: string 13 let video2UUID: string
29 let userAccessToken: string 14 let userToken: string
30 15
31 before(async function () { 16 before(async function () {
32 this.timeout(90000) 17 this.timeout(90000)
33 18
34 servers = await flushAndRunMultipleServers(2) 19 servers = await createMultipleServers(2)
35 20
36 // Get the access tokens 21 // Get the access tokens
37 await setAccessTokensToServers(servers) 22 await setAccessTokensToServers(servers)
38 23
39 { 24 {
40 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video server 1' }) 25 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video server 1' } })
41 video1UUID = res.body.video.uuid 26 video1UUID = uuid
42 } 27 }
43 { 28 {
44 const res = await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'video server 2' }) 29 const { uuid } = await servers[1].videos.upload({ attributes: { name: 'video server 2' } })
45 video2UUID = res.body.video.uuid 30 video2UUID = uuid
46 } 31 }
47 32
48 const user = { 33 const user = {
49 username: 'user1', 34 username: 'user1',
50 password: 'super_password' 35 password: 'super_password'
51 } 36 }
52 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: user.username, password: user.password }) 37 await servers[0].users.create({ username: user.username, password: user.password })
53 userAccessToken = await userLogin(servers[0], user) 38 userToken = await servers[0].login.getAccessToken(user)
54 39
55 await doubleFollow(servers[0], servers[1]) 40 await doubleFollow(servers[0], servers[1])
56 }) 41 })
@@ -60,81 +45,81 @@ describe('Test follow constraints', function () {
60 describe('With an unlogged user', function () { 45 describe('With an unlogged user', function () {
61 46
62 it('Should get the local video', async function () { 47 it('Should get the local video', async function () {
63 await getVideo(servers[0].url, video1UUID, HttpStatusCode.OK_200) 48 await servers[0].videos.get({ id: video1UUID })
64 }) 49 })
65 50
66 it('Should get the remote video', async function () { 51 it('Should get the remote video', async function () {
67 await getVideo(servers[0].url, video2UUID, HttpStatusCode.OK_200) 52 await servers[0].videos.get({ id: video2UUID })
68 }) 53 })
69 54
70 it('Should list local account videos', async function () { 55 it('Should list local account videos', async function () {
71 const res = await getAccountVideos(servers[0].url, undefined, 'root@localhost:' + servers[0].port, 0, 5) 56 const { total, data } = await servers[0].videos.listByAccount({ handle: 'root@localhost:' + servers[0].port })
72 57
73 expect(res.body.total).to.equal(1) 58 expect(total).to.equal(1)
74 expect(res.body.data).to.have.lengthOf(1) 59 expect(data).to.have.lengthOf(1)
75 }) 60 })
76 61
77 it('Should list remote account videos', async function () { 62 it('Should list remote account videos', async function () {
78 const res = await getAccountVideos(servers[0].url, undefined, 'root@localhost:' + servers[1].port, 0, 5) 63 const { total, data } = await servers[0].videos.listByAccount({ handle: 'root@localhost:' + servers[1].port })
79 64
80 expect(res.body.total).to.equal(1) 65 expect(total).to.equal(1)
81 expect(res.body.data).to.have.lengthOf(1) 66 expect(data).to.have.lengthOf(1)
82 }) 67 })
83 68
84 it('Should list local channel videos', async function () { 69 it('Should list local channel videos', async function () {
85 const videoChannelName = 'root_channel@localhost:' + servers[0].port 70 const handle = 'root_channel@localhost:' + servers[0].port
86 const res = await getVideoChannelVideos(servers[0].url, undefined, videoChannelName, 0, 5) 71 const { total, data } = await servers[0].videos.listByChannel({ handle })
87 72
88 expect(res.body.total).to.equal(1) 73 expect(total).to.equal(1)
89 expect(res.body.data).to.have.lengthOf(1) 74 expect(data).to.have.lengthOf(1)
90 }) 75 })
91 76
92 it('Should list remote channel videos', async function () { 77 it('Should list remote channel videos', async function () {
93 const videoChannelName = 'root_channel@localhost:' + servers[1].port 78 const handle = 'root_channel@localhost:' + servers[1].port
94 const res = await getVideoChannelVideos(servers[0].url, undefined, videoChannelName, 0, 5) 79 const { total, data } = await servers[0].videos.listByChannel({ handle })
95 80
96 expect(res.body.total).to.equal(1) 81 expect(total).to.equal(1)
97 expect(res.body.data).to.have.lengthOf(1) 82 expect(data).to.have.lengthOf(1)
98 }) 83 })
99 }) 84 })
100 85
101 describe('With a logged user', function () { 86 describe('With a logged user', function () {
102 it('Should get the local video', async function () { 87 it('Should get the local video', async function () {
103 await getVideoWithToken(servers[0].url, userAccessToken, video1UUID, HttpStatusCode.OK_200) 88 await servers[0].videos.getWithToken({ token: userToken, id: video1UUID })
104 }) 89 })
105 90
106 it('Should get the remote video', async function () { 91 it('Should get the remote video', async function () {
107 await getVideoWithToken(servers[0].url, userAccessToken, video2UUID, HttpStatusCode.OK_200) 92 await servers[0].videos.getWithToken({ token: userToken, id: video2UUID })
108 }) 93 })
109 94
110 it('Should list local account videos', async function () { 95 it('Should list local account videos', async function () {
111 const res = await getAccountVideos(servers[0].url, userAccessToken, 'root@localhost:' + servers[0].port, 0, 5) 96 const { total, data } = await servers[0].videos.listByAccount({ token: userToken, handle: 'root@localhost:' + servers[0].port })
112 97
113 expect(res.body.total).to.equal(1) 98 expect(total).to.equal(1)
114 expect(res.body.data).to.have.lengthOf(1) 99 expect(data).to.have.lengthOf(1)
115 }) 100 })
116 101
117 it('Should list remote account videos', async function () { 102 it('Should list remote account videos', async function () {
118 const res = await getAccountVideos(servers[0].url, userAccessToken, 'root@localhost:' + servers[1].port, 0, 5) 103 const { total, data } = await servers[0].videos.listByAccount({ token: userToken, handle: 'root@localhost:' + servers[1].port })
119 104
120 expect(res.body.total).to.equal(1) 105 expect(total).to.equal(1)
121 expect(res.body.data).to.have.lengthOf(1) 106 expect(data).to.have.lengthOf(1)
122 }) 107 })
123 108
124 it('Should list local channel videos', async function () { 109 it('Should list local channel videos', async function () {
125 const videoChannelName = 'root_channel@localhost:' + servers[0].port 110 const handle = 'root_channel@localhost:' + servers[0].port
126 const res = await getVideoChannelVideos(servers[0].url, userAccessToken, videoChannelName, 0, 5) 111 const { total, data } = await servers[0].videos.listByChannel({ token: userToken, handle })
127 112
128 expect(res.body.total).to.equal(1) 113 expect(total).to.equal(1)
129 expect(res.body.data).to.have.lengthOf(1) 114 expect(data).to.have.lengthOf(1)
130 }) 115 })
131 116
132 it('Should list remote channel videos', async function () { 117 it('Should list remote channel videos', async function () {
133 const videoChannelName = 'root_channel@localhost:' + servers[1].port 118 const handle = 'root_channel@localhost:' + servers[1].port
134 const res = await getVideoChannelVideos(servers[0].url, userAccessToken, videoChannelName, 0, 5) 119 const { total, data } = await servers[0].videos.listByChannel({ token: userToken, handle })
135 120
136 expect(res.body.total).to.equal(1) 121 expect(total).to.equal(1)
137 expect(res.body.data).to.have.lengthOf(1) 122 expect(data).to.have.lengthOf(1)
138 }) 123 })
139 }) 124 })
140 }) 125 })
@@ -144,19 +129,18 @@ describe('Test follow constraints', function () {
144 before(async function () { 129 before(async function () {
145 this.timeout(30000) 130 this.timeout(30000)
146 131
147 await unfollow(servers[0].url, servers[0].accessToken, servers[1]) 132 await servers[0].follows.unfollow({ target: servers[1] })
148 }) 133 })
149 134
150 describe('With an unlogged user', function () { 135 describe('With an unlogged user', function () {
151 136
152 it('Should get the local video', async function () { 137 it('Should get the local video', async function () {
153 await getVideo(servers[0].url, video1UUID, HttpStatusCode.OK_200) 138 await servers[0].videos.get({ id: video1UUID })
154 }) 139 })
155 140
156 it('Should not get the remote video', async function () { 141 it('Should not get the remote video', async function () {
157 const res = await getVideo(servers[0].url, video2UUID, HttpStatusCode.FORBIDDEN_403) 142 const body = await servers[0].videos.get({ id: video2UUID, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
158 143 const error = body as unknown as PeerTubeProblemDocument
159 const error = res.body as PeerTubeProblemDocument
160 144
161 const doc = 'https://docs.joinpeertube.org/api-rest-reference.html#section/Errors/does_not_respect_follow_constraints' 145 const doc = 'https://docs.joinpeertube.org/api-rest-reference.html#section/Errors/does_not_respect_follow_constraints'
162 expect(error.type).to.equal(doc) 146 expect(error.type).to.equal(doc)
@@ -171,73 +155,79 @@ describe('Test follow constraints', function () {
171 }) 155 })
172 156
173 it('Should list local account videos', async function () { 157 it('Should list local account videos', async function () {
174 const res = await getAccountVideos(servers[0].url, undefined, 'root@localhost:' + servers[0].port, 0, 5) 158 const { total, data } = await servers[0].videos.listByAccount({
159 token: null,
160 handle: 'root@localhost:' + servers[0].port
161 })
175 162
176 expect(res.body.total).to.equal(1) 163 expect(total).to.equal(1)
177 expect(res.body.data).to.have.lengthOf(1) 164 expect(data).to.have.lengthOf(1)
178 }) 165 })
179 166
180 it('Should not list remote account videos', async function () { 167 it('Should not list remote account videos', async function () {
181 const res = await getAccountVideos(servers[0].url, undefined, 'root@localhost:' + servers[1].port, 0, 5) 168 const { total, data } = await servers[0].videos.listByAccount({
169 token: null,
170 handle: 'root@localhost:' + servers[1].port
171 })
182 172
183 expect(res.body.total).to.equal(0) 173 expect(total).to.equal(0)
184 expect(res.body.data).to.have.lengthOf(0) 174 expect(data).to.have.lengthOf(0)
185 }) 175 })
186 176
187 it('Should list local channel videos', async function () { 177 it('Should list local channel videos', async function () {
188 const videoChannelName = 'root_channel@localhost:' + servers[0].port 178 const handle = 'root_channel@localhost:' + servers[0].port
189 const res = await getVideoChannelVideos(servers[0].url, undefined, videoChannelName, 0, 5) 179 const { total, data } = await servers[0].videos.listByChannel({ token: null, handle })
190 180
191 expect(res.body.total).to.equal(1) 181 expect(total).to.equal(1)
192 expect(res.body.data).to.have.lengthOf(1) 182 expect(data).to.have.lengthOf(1)
193 }) 183 })
194 184
195 it('Should not list remote channel videos', async function () { 185 it('Should not list remote channel videos', async function () {
196 const videoChannelName = 'root_channel@localhost:' + servers[1].port 186 const handle = 'root_channel@localhost:' + servers[1].port
197 const res = await getVideoChannelVideos(servers[0].url, undefined, videoChannelName, 0, 5) 187 const { total, data } = await servers[0].videos.listByChannel({ token: null, handle })
198 188
199 expect(res.body.total).to.equal(0) 189 expect(total).to.equal(0)
200 expect(res.body.data).to.have.lengthOf(0) 190 expect(data).to.have.lengthOf(0)
201 }) 191 })
202 }) 192 })
203 193
204 describe('With a logged user', function () { 194 describe('With a logged user', function () {
205 it('Should get the local video', async function () { 195 it('Should get the local video', async function () {
206 await getVideoWithToken(servers[0].url, userAccessToken, video1UUID, HttpStatusCode.OK_200) 196 await servers[0].videos.getWithToken({ token: userToken, id: video1UUID })
207 }) 197 })
208 198
209 it('Should get the remote video', async function () { 199 it('Should get the remote video', async function () {
210 await getVideoWithToken(servers[0].url, userAccessToken, video2UUID, HttpStatusCode.OK_200) 200 await servers[0].videos.getWithToken({ token: userToken, id: video2UUID })
211 }) 201 })
212 202
213 it('Should list local account videos', async function () { 203 it('Should list local account videos', async function () {
214 const res = await getAccountVideos(servers[0].url, userAccessToken, 'root@localhost:' + servers[0].port, 0, 5) 204 const { total, data } = await servers[0].videos.listByAccount({ token: userToken, handle: 'root@localhost:' + servers[0].port })
215 205
216 expect(res.body.total).to.equal(1) 206 expect(total).to.equal(1)
217 expect(res.body.data).to.have.lengthOf(1) 207 expect(data).to.have.lengthOf(1)
218 }) 208 })
219 209
220 it('Should list remote account videos', async function () { 210 it('Should list remote account videos', async function () {
221 const res = await getAccountVideos(servers[0].url, userAccessToken, 'root@localhost:' + servers[1].port, 0, 5) 211 const { total, data } = await servers[0].videos.listByAccount({ token: userToken, handle: 'root@localhost:' + servers[1].port })
222 212
223 expect(res.body.total).to.equal(1) 213 expect(total).to.equal(1)
224 expect(res.body.data).to.have.lengthOf(1) 214 expect(data).to.have.lengthOf(1)
225 }) 215 })
226 216
227 it('Should list local channel videos', async function () { 217 it('Should list local channel videos', async function () {
228 const videoChannelName = 'root_channel@localhost:' + servers[0].port 218 const handle = 'root_channel@localhost:' + servers[0].port
229 const res = await getVideoChannelVideos(servers[0].url, userAccessToken, videoChannelName, 0, 5) 219 const { total, data } = await servers[0].videos.listByChannel({ token: userToken, handle })
230 220
231 expect(res.body.total).to.equal(1) 221 expect(total).to.equal(1)
232 expect(res.body.data).to.have.lengthOf(1) 222 expect(data).to.have.lengthOf(1)
233 }) 223 })
234 224
235 it('Should list remote channel videos', async function () { 225 it('Should list remote channel videos', async function () {
236 const videoChannelName = 'root_channel@localhost:' + servers[1].port 226 const handle = 'root_channel@localhost:' + servers[1].port
237 const res = await getVideoChannelVideos(servers[0].url, userAccessToken, videoChannelName, 0, 5) 227 const { total, data } = await servers[0].videos.listByChannel({ token: userToken, handle })
238 228
239 expect(res.body.total).to.equal(1) 229 expect(total).to.equal(1)
240 expect(res.body.data).to.have.lengthOf(1) 230 expect(data).to.have.lengthOf(1)
241 }) 231 })
242 }) 232 })
243 }) 233 })
diff --git a/server/tests/api/server/follows-moderation.ts b/server/tests/api/server/follows-moderation.ts
index 73c212a32..921f51043 100644
--- a/server/tests/api/server/follows-moderation.ts
+++ b/server/tests/api/server/follows-moderation.ts
@@ -1,77 +1,66 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2 2
3import * as chai from 'chai'
4import 'mocha' 3import 'mocha'
4import * as chai from 'chai'
5import { 5import {
6 acceptFollower,
7 cleanupTests, 6 cleanupTests,
8 flushAndRunMultipleServers, 7 createMultipleServers,
9 ServerInfo, 8 FollowsCommand,
9 PeerTubeServer,
10 setAccessTokensToServers, 10 setAccessTokensToServers,
11 updateCustomSubConfig 11 waitJobs
12} from '../../../../shared/extra-utils/index' 12} from '@shared/extra-utils'
13import {
14 follow,
15 getFollowersListPaginationAndSort,
16 getFollowingListPaginationAndSort,
17 rejectFollower,
18 removeFollower
19} from '../../../../shared/extra-utils/server/follows'
20import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
21import { ActorFollow } from '../../../../shared/models/actors'
22 13
23const expect = chai.expect 14const expect = chai.expect
24 15
25async function checkServer1And2HasFollowers (servers: ServerInfo[], state = 'accepted') { 16async function checkServer1And2HasFollowers (servers: PeerTubeServer[], state = 'accepted') {
26 { 17 const fns = [
27 const res = await getFollowingListPaginationAndSort({ url: servers[0].url, start: 0, count: 5, sort: 'createdAt' }) 18 servers[0].follows.getFollowings.bind(servers[0].follows),
28 expect(res.body.total).to.equal(1) 19 servers[1].follows.getFollowers.bind(servers[1].follows)
29 20 ]
30 const follow = res.body.data[0] as ActorFollow
31 expect(follow.state).to.equal(state)
32 expect(follow.follower.url).to.equal('http://localhost:' + servers[0].port + '/accounts/peertube')
33 expect(follow.following.url).to.equal('http://localhost:' + servers[1].port + '/accounts/peertube')
34 }
35 21
36 { 22 for (const fn of fns) {
37 const res = await getFollowersListPaginationAndSort({ url: servers[1].url, start: 0, count: 5, sort: 'createdAt' }) 23 const body = await fn({ start: 0, count: 5, sort: 'createdAt' })
38 expect(res.body.total).to.equal(1) 24 expect(body.total).to.equal(1)
39 25
40 const follow = res.body.data[0] as ActorFollow 26 const follow = body.data[0]
41 expect(follow.state).to.equal(state) 27 expect(follow.state).to.equal(state)
42 expect(follow.follower.url).to.equal('http://localhost:' + servers[0].port + '/accounts/peertube') 28 expect(follow.follower.url).to.equal('http://localhost:' + servers[0].port + '/accounts/peertube')
43 expect(follow.following.url).to.equal('http://localhost:' + servers[1].port + '/accounts/peertube') 29 expect(follow.following.url).to.equal('http://localhost:' + servers[1].port + '/accounts/peertube')
44 } 30 }
45} 31}
46 32
47async function checkNoFollowers (servers: ServerInfo[]) { 33async function checkNoFollowers (servers: PeerTubeServer[]) {
48 { 34 const fns = [
49 const res = await getFollowingListPaginationAndSort({ url: servers[0].url, start: 0, count: 5, sort: 'createdAt' }) 35 servers[0].follows.getFollowings.bind(servers[0].follows),
50 expect(res.body.total).to.equal(0) 36 servers[1].follows.getFollowers.bind(servers[1].follows)
51 } 37 ]
52 38
53 { 39 for (const fn of fns) {
54 const res = await getFollowersListPaginationAndSort({ url: servers[1].url, start: 0, count: 5, sort: 'createdAt' }) 40 const body = await fn({ start: 0, count: 5, sort: 'createdAt' })
55 expect(res.body.total).to.equal(0) 41 expect(body.total).to.equal(0)
56 } 42 }
57} 43}
58 44
59describe('Test follows moderation', function () { 45describe('Test follows moderation', function () {
60 let servers: ServerInfo[] = [] 46 let servers: PeerTubeServer[] = []
47 let commands: FollowsCommand[]
61 48
62 before(async function () { 49 before(async function () {
63 this.timeout(30000) 50 this.timeout(30000)
64 51
65 servers = await flushAndRunMultipleServers(3) 52 servers = await createMultipleServers(3)
66 53
67 // Get the access tokens 54 // Get the access tokens
68 await setAccessTokensToServers(servers) 55 await setAccessTokensToServers(servers)
56
57 commands = servers.map(s => s.follows)
69 }) 58 })
70 59
71 it('Should have server 1 following server 2', async function () { 60 it('Should have server 1 following server 2', async function () {
72 this.timeout(30000) 61 this.timeout(30000)
73 62
74 await follow(servers[0].url, [ servers[1].url ], servers[0].accessToken) 63 await commands[0].follow({ hosts: [ servers[1].url ] })
75 64
76 await waitJobs(servers) 65 await waitJobs(servers)
77 }) 66 })
@@ -83,7 +72,7 @@ describe('Test follows moderation', function () {
83 it('Should remove follower on server 2', async function () { 72 it('Should remove follower on server 2', async function () {
84 this.timeout(10000) 73 this.timeout(10000)
85 74
86 await removeFollower(servers[1].url, servers[1].accessToken, servers[0]) 75 await commands[1].removeFollower({ follower: servers[0] })
87 76
88 await waitJobs(servers) 77 await waitJobs(servers)
89 }) 78 })
@@ -104,9 +93,9 @@ describe('Test follows moderation', function () {
104 } 93 }
105 } 94 }
106 95
107 await updateCustomSubConfig(servers[1].url, servers[1].accessToken, subConfig) 96 await servers[1].config.updateCustomSubConfig({ newConfig: subConfig })
108 97
109 await follow(servers[0].url, [ servers[1].url ], servers[0].accessToken) 98 await commands[0].follow({ hosts: [ servers[1].url ] })
110 await waitJobs(servers) 99 await waitJobs(servers)
111 100
112 await checkNoFollowers(servers) 101 await checkNoFollowers(servers)
@@ -124,9 +113,9 @@ describe('Test follows moderation', function () {
124 } 113 }
125 } 114 }
126 115
127 await updateCustomSubConfig(servers[1].url, servers[1].accessToken, subConfig) 116 await servers[1].config.updateCustomSubConfig({ newConfig: subConfig })
128 117
129 await follow(servers[0].url, [ servers[1].url ], servers[0].accessToken) 118 await commands[0].follow({ hosts: [ servers[1].url ] })
130 await waitJobs(servers) 119 await waitJobs(servers)
131 120
132 await checkServer1And2HasFollowers(servers) 121 await checkServer1And2HasFollowers(servers)
@@ -135,7 +124,7 @@ describe('Test follows moderation', function () {
135 it('Should manually approve followers', async function () { 124 it('Should manually approve followers', async function () {
136 this.timeout(20000) 125 this.timeout(20000)
137 126
138 await removeFollower(servers[1].url, servers[1].accessToken, servers[0]) 127 await commands[1].removeFollower({ follower: servers[0] })
139 await waitJobs(servers) 128 await waitJobs(servers)
140 129
141 const subConfig = { 130 const subConfig = {
@@ -147,10 +136,10 @@ describe('Test follows moderation', function () {
147 } 136 }
148 } 137 }
149 138
150 await updateCustomSubConfig(servers[1].url, servers[1].accessToken, subConfig) 139 await servers[1].config.updateCustomSubConfig({ newConfig: subConfig })
151 await updateCustomSubConfig(servers[2].url, servers[2].accessToken, subConfig) 140 await servers[2].config.updateCustomSubConfig({ newConfig: subConfig })
152 141
153 await follow(servers[0].url, [ servers[1].url ], servers[0].accessToken) 142 await commands[0].follow({ hosts: [ servers[1].url ] })
154 await waitJobs(servers) 143 await waitJobs(servers)
155 144
156 await checkServer1And2HasFollowers(servers, 'pending') 145 await checkServer1And2HasFollowers(servers, 'pending')
@@ -159,7 +148,7 @@ describe('Test follows moderation', function () {
159 it('Should accept a follower', async function () { 148 it('Should accept a follower', async function () {
160 this.timeout(10000) 149 this.timeout(10000)
161 150
162 await acceptFollower(servers[1].url, servers[1].accessToken, 'peertube@localhost:' + servers[0].port) 151 await commands[1].acceptFollower({ follower: 'peertube@localhost:' + servers[0].port })
163 await waitJobs(servers) 152 await waitJobs(servers)
164 153
165 await checkServer1And2HasFollowers(servers) 154 await checkServer1And2HasFollowers(servers)
@@ -168,32 +157,32 @@ describe('Test follows moderation', function () {
168 it('Should reject another follower', async function () { 157 it('Should reject another follower', async function () {
169 this.timeout(20000) 158 this.timeout(20000)
170 159
171 await follow(servers[0].url, [ servers[2].url ], servers[0].accessToken) 160 await commands[0].follow({ hosts: [ servers[2].url ] })
172 await waitJobs(servers) 161 await waitJobs(servers)
173 162
174 { 163 {
175 const res = await getFollowingListPaginationAndSort({ url: servers[0].url, start: 0, count: 5, sort: 'createdAt' }) 164 const body = await commands[0].getFollowings({ start: 0, count: 5, sort: 'createdAt' })
176 expect(res.body.total).to.equal(2) 165 expect(body.total).to.equal(2)
177 } 166 }
178 167
179 { 168 {
180 const res = await getFollowersListPaginationAndSort({ url: servers[1].url, start: 0, count: 5, sort: 'createdAt' }) 169 const body = await commands[1].getFollowers({ start: 0, count: 5, sort: 'createdAt' })
181 expect(res.body.total).to.equal(1) 170 expect(body.total).to.equal(1)
182 } 171 }
183 172
184 { 173 {
185 const res = await getFollowersListPaginationAndSort({ url: servers[2].url, start: 0, count: 5, sort: 'createdAt' }) 174 const body = await commands[2].getFollowers({ start: 0, count: 5, sort: 'createdAt' })
186 expect(res.body.total).to.equal(1) 175 expect(body.total).to.equal(1)
187 } 176 }
188 177
189 await rejectFollower(servers[2].url, servers[2].accessToken, 'peertube@localhost:' + servers[0].port) 178 await commands[2].rejectFollower({ follower: 'peertube@localhost:' + servers[0].port })
190 await waitJobs(servers) 179 await waitJobs(servers)
191 180
192 await checkServer1And2HasFollowers(servers) 181 await checkServer1And2HasFollowers(servers)
193 182
194 { 183 {
195 const res = await getFollowersListPaginationAndSort({ url: servers[2].url, start: 0, count: 5, sort: 'createdAt' }) 184 const body = await commands[2].getFollowers({ start: 0, count: 5, sort: 'createdAt' })
196 expect(res.body.total).to.equal(0) 185 expect(body.total).to.equal(0)
197 } 186 }
198 }) 187 })
199 188
diff --git a/server/tests/api/server/follows.ts b/server/tests/api/server/follows.ts
index 9e5aa00c7..832ba561a 100644
--- a/server/tests/api/server/follows.ts
+++ b/server/tests/api/server/follows.ts
@@ -3,325 +3,374 @@
3import 'mocha' 3import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5import { 5import {
6 addVideoCommentReply,
7 addVideoCommentThread,
8 cleanupTests, 6 cleanupTests,
9 completeVideoCheck, 7 completeVideoCheck,
10 createUser, 8 createMultipleServers,
11 createVideoCaption,
12 dateIsValid, 9 dateIsValid,
13 deleteVideoComment,
14 expectAccountFollows, 10 expectAccountFollows,
15 flushAndRunMultipleServers, 11 expectChannelsFollows,
16 follow, 12 PeerTubeServer,
17 getFollowersListPaginationAndSort,
18 getFollowingListPaginationAndSort,
19 getVideoCommentThreads,
20 getVideosList,
21 getVideoThreadComments,
22 listVideoCaptions,
23 rateVideo,
24 ServerInfo,
25 setAccessTokensToServers, 13 setAccessTokensToServers,
26 testCaptionFile, 14 testCaptionFile,
27 unfollow,
28 uploadVideo,
29 userLogin,
30 waitJobs 15 waitJobs
31} from '@shared/extra-utils' 16} from '@shared/extra-utils'
32import { Video, VideoCaption, VideoComment, VideoCommentThreadTree, VideoPrivacy } from '@shared/models' 17import { VideoCreateResult, VideoPrivacy } from '@shared/models'
33 18
34const expect = chai.expect 19const expect = chai.expect
35 20
36describe('Test follows', function () { 21describe('Test follows', function () {
37 let servers: ServerInfo[] = [] 22 let servers: PeerTubeServer[] = []
38 23
39 before(async function () { 24 before(async function () {
40 this.timeout(30000) 25 this.timeout(30000)
41 26
42 servers = await flushAndRunMultipleServers(3) 27 servers = await createMultipleServers(3)
43 28
44 // Get the access tokens 29 // Get the access tokens
45 await setAccessTokensToServers(servers) 30 await setAccessTokensToServers(servers)
46 }) 31 })
47 32
48 it('Should not have followers', async function () { 33 describe('Data propagation after follow', function () {
49 for (const server of servers) {
50 const res = await getFollowersListPaginationAndSort({ url: server.url, start: 0, count: 5, sort: 'createdAt' })
51 const follows = res.body.data
52 34
53 expect(res.body.total).to.equal(0) 35 it('Should not have followers/followings', async function () {
54 expect(follows).to.be.an('array') 36 for (const server of servers) {
55 expect(follows.length).to.equal(0) 37 const bodies = await Promise.all([
56 } 38 server.follows.getFollowers({ start: 0, count: 5, sort: 'createdAt' }),
57 }) 39 server.follows.getFollowings({ start: 0, count: 5, sort: 'createdAt' })
58 40 ])
59 it('Should not have following', async function () {
60 for (const server of servers) {
61 const res = await getFollowingListPaginationAndSort({ url: server.url, start: 0, count: 5, sort: 'createdAt' })
62 const follows = res.body.data
63 41
64 expect(res.body.total).to.equal(0) 42 for (const body of bodies) {
65 expect(follows).to.be.an('array') 43 expect(body.total).to.equal(0)
66 expect(follows.length).to.equal(0)
67 }
68 })
69 44
70 it('Should have server 1 following server 2 and 3', async function () { 45 const follows = body.data
71 this.timeout(30000) 46 expect(follows).to.be.an('array')
47 expect(follows).to.have.lengthOf(0)
48 }
49 }
50 })
72 51
73 await follow(servers[0].url, [ servers[1].url, servers[2].url ], servers[0].accessToken) 52 it('Should have server 1 following root account of server 2 and server 3', async function () {
53 this.timeout(30000)
74 54
75 await waitJobs(servers) 55 await servers[0].follows.follow({
76 }) 56 hosts: [ servers[2].url ],
57 handles: [ 'root@' + servers[1].host ]
58 })
77 59
78 it('Should have 2 followings on server 1', async function () { 60 await waitJobs(servers)
79 let res = await getFollowingListPaginationAndSort({ url: servers[0].url, start: 0, count: 1, sort: 'createdAt' }) 61 })
80 let follows = res.body.data
81 62
82 expect(res.body.total).to.equal(2) 63 it('Should have 2 followings on server 1', async function () {
83 expect(follows).to.be.an('array') 64 const body = await servers[0].follows.getFollowings({ start: 0, count: 1, sort: 'createdAt' })
84 expect(follows.length).to.equal(1) 65 expect(body.total).to.equal(2)
85 66
86 res = await getFollowingListPaginationAndSort({ url: servers[0].url, start: 1, count: 1, sort: 'createdAt' }) 67 let follows = body.data
87 follows = follows.concat(res.body.data) 68 expect(follows).to.be.an('array')
69 expect(follows).to.have.lengthOf(1)
88 70
89 const server2Follow = follows.find(f => f.following.host === 'localhost:' + servers[1].port) 71 const body2 = await servers[0].follows.getFollowings({ start: 1, count: 1, sort: 'createdAt' })
90 const server3Follow = follows.find(f => f.following.host === 'localhost:' + servers[2].port) 72 follows = follows.concat(body2.data)
91 73
92 expect(server2Follow).to.not.be.undefined 74 const server2Follow = follows.find(f => f.following.host === servers[1].host)
93 expect(server3Follow).to.not.be.undefined 75 const server3Follow = follows.find(f => f.following.host === servers[2].host)
94 expect(server2Follow.state).to.equal('accepted')
95 expect(server3Follow.state).to.equal('accepted')
96 })
97 76
98 it('Should search/filter followings on server 1', async function () { 77 expect(server2Follow).to.not.be.undefined
99 const sort = 'createdAt' 78 expect(server2Follow.following.name).to.equal('root')
100 const start = 0 79 expect(server2Follow.state).to.equal('accepted')
101 const count = 1
102 const url = servers[0].url
103 80
104 { 81 expect(server3Follow).to.not.be.undefined
105 const search = ':' + servers[1].port 82 expect(server3Follow.following.name).to.equal('peertube')
83 expect(server3Follow.state).to.equal('accepted')
84 })
106 85
107 { 86 it('Should have 0 followings on server 2 and 3', async function () {
108 const res = await getFollowingListPaginationAndSort({ url, start, count, sort, search }) 87 for (const server of [ servers[1], servers[2] ]) {
109 const follows = res.body.data 88 const body = await server.follows.getFollowings({ start: 0, count: 5, sort: 'createdAt' })
89 expect(body.total).to.equal(0)
110 90
111 expect(res.body.total).to.equal(1) 91 const follows = body.data
112 expect(follows.length).to.equal(1) 92 expect(follows).to.be.an('array')
113 expect(follows[0].following.host).to.equal('localhost:' + servers[1].port) 93 expect(follows).to.have.lengthOf(0)
114 } 94 }
95 })
115 96
116 { 97 it('Should have 1 followers on server 3', async function () {
117 const res = await getFollowingListPaginationAndSort({ url, start, count, sort, search, state: 'accepted' }) 98 const body = await servers[2].follows.getFollowers({ start: 0, count: 1, sort: 'createdAt' })
118 expect(res.body.total).to.equal(1) 99 expect(body.total).to.equal(1)
119 expect(res.body.data).to.have.lengthOf(1) 100
101 const follows = body.data
102 expect(follows).to.be.an('array')
103 expect(follows).to.have.lengthOf(1)
104 expect(follows[0].follower.host).to.equal('localhost:' + servers[0].port)
105 })
106
107 it('Should have 0 followers on server 1 and 2', async function () {
108 for (const server of [ servers[0], servers[1] ]) {
109 const body = await server.follows.getFollowers({ start: 0, count: 5, sort: 'createdAt' })
110 expect(body.total).to.equal(0)
111
112 const follows = body.data
113 expect(follows).to.be.an('array')
114 expect(follows).to.have.lengthOf(0)
120 } 115 }
116 })
117
118 it('Should search/filter followings on server 1', async function () {
119 const sort = 'createdAt'
120 const start = 0
121 const count = 1
121 122
122 { 123 {
123 const res = await getFollowingListPaginationAndSort({ url, start, count, sort, search, state: 'accepted', actorType: 'Person' }) 124 const search = ':' + servers[1].port
124 expect(res.body.total).to.equal(0) 125
125 expect(res.body.data).to.have.lengthOf(0) 126 {
127 const body = await servers[0].follows.getFollowings({ start, count, sort, search })
128 expect(body.total).to.equal(1)
129
130 const follows = body.data
131 expect(follows).to.have.lengthOf(1)
132 expect(follows[0].following.host).to.equal(servers[1].host)
133 }
134
135 {
136 const body = await servers[0].follows.getFollowings({ start, count, sort, search, state: 'accepted' })
137 expect(body.total).to.equal(1)
138 expect(body.data).to.have.lengthOf(1)
139 }
140
141 {
142 const body = await servers[0].follows.getFollowings({ start, count, sort, search, state: 'accepted', actorType: 'Person' })
143 expect(body.total).to.equal(1)
144 expect(body.data).to.have.lengthOf(1)
145 }
146
147 {
148 const body = await servers[0].follows.getFollowings({
149 start,
150 count,
151 sort,
152 search,
153 state: 'accepted',
154 actorType: 'Application'
155 })
156 expect(body.total).to.equal(0)
157 expect(body.data).to.have.lengthOf(0)
158 }
159
160 {
161 const body = await servers[0].follows.getFollowings({ start, count, sort, search, state: 'pending' })
162 expect(body.total).to.equal(0)
163 expect(body.data).to.have.lengthOf(0)
164 }
126 } 165 }
127 166
128 { 167 {
129 const res = await getFollowingListPaginationAndSort({ 168 const body = await servers[0].follows.getFollowings({ start, count, sort, search: 'root' })
130 url, 169 expect(body.total).to.equal(1)
131 start, 170 expect(body.data).to.have.lengthOf(1)
132 count,
133 sort,
134 search,
135 state: 'accepted',
136 actorType: 'Application'
137 })
138 expect(res.body.total).to.equal(1)
139 expect(res.body.data).to.have.lengthOf(1)
140 } 171 }
141 172
142 { 173 {
143 const res = await getFollowingListPaginationAndSort({ url, start, count, sort, search, state: 'pending' }) 174 const body = await servers[0].follows.getFollowings({ start, count, sort, search: 'bla' })
144 expect(res.body.total).to.equal(0) 175 expect(body.total).to.equal(0)
145 expect(res.body.data).to.have.lengthOf(0) 176
177 expect(body.data).to.have.lengthOf(0)
146 } 178 }
147 } 179 })
148 180
149 { 181 it('Should search/filter followers on server 2', async function () {
150 const res = await getFollowingListPaginationAndSort({ url, start, count, sort, search: 'bla' }) 182 const start = 0
151 const follows = res.body.data 183 const count = 5
184 const sort = 'createdAt'
152 185
153 expect(res.body.total).to.equal(0) 186 {
154 expect(follows.length).to.equal(0) 187 const search = servers[0].port + ''
155 }
156 })
157 188
158 it('Should have 0 followings on server 2 and 3', async function () { 189 {
159 for (const server of [ servers[1], servers[2] ]) { 190 const body = await servers[2].follows.getFollowers({ start, count, sort, search })
160 const res = await getFollowingListPaginationAndSort({ url: server.url, start: 0, count: 5, sort: 'createdAt' }) 191 expect(body.total).to.equal(1)
161 const follows = res.body.data
162 192
163 expect(res.body.total).to.equal(0) 193 const follows = body.data
164 expect(follows).to.be.an('array') 194 expect(follows).to.have.lengthOf(1)
165 expect(follows.length).to.equal(0) 195 expect(follows[0].following.host).to.equal(servers[2].host)
166 } 196 }
167 })
168 197
169 it('Should have 1 followers on server 2 and 3', async function () { 198 {
170 for (const server of [ servers[1], servers[2] ]) { 199 const body = await servers[2].follows.getFollowers({ start, count, sort, search, state: 'accepted' })
171 const res = await getFollowersListPaginationAndSort({ url: server.url, start: 0, count: 1, sort: 'createdAt' }) 200 expect(body.total).to.equal(1)
201 expect(body.data).to.have.lengthOf(1)
202 }
172 203
173 const follows = res.body.data 204 {
174 expect(res.body.total).to.equal(1) 205 const body = await servers[2].follows.getFollowers({ start, count, sort, search, state: 'accepted', actorType: 'Person' })
175 expect(follows).to.be.an('array') 206 expect(body.total).to.equal(0)
176 expect(follows.length).to.equal(1) 207 expect(body.data).to.have.lengthOf(0)
177 expect(follows[0].follower.host).to.equal('localhost:' + servers[0].port) 208 }
178 }
179 })
180 209
181 it('Should search/filter followers on server 2', async function () { 210 {
182 const url = servers[2].url 211 const body = await servers[2].follows.getFollowers({
183 const start = 0 212 start,
184 const count = 5 213 count,
185 const sort = 'createdAt' 214 sort,
215 search,
216 state: 'accepted',
217 actorType: 'Application'
218 })
219 expect(body.total).to.equal(1)
220 expect(body.data).to.have.lengthOf(1)
221 }
186 222
187 { 223 {
188 const search = servers[0].port + '' 224 const body = await servers[2].follows.getFollowers({ start, count, sort, search, state: 'pending' })
225 expect(body.total).to.equal(0)
226 expect(body.data).to.have.lengthOf(0)
227 }
228 }
189 229
190 { 230 {
191 const res = await getFollowersListPaginationAndSort({ url, start, count, sort, search }) 231 const body = await servers[2].follows.getFollowers({ start, count, sort, search: 'bla' })
192 const follows = res.body.data 232 expect(body.total).to.equal(0)
193 233
194 expect(res.body.total).to.equal(1) 234 const follows = body.data
195 expect(follows.length).to.equal(1) 235 expect(follows).to.have.lengthOf(0)
196 expect(follows[0].following.host).to.equal('localhost:' + servers[2].port)
197 } 236 }
237 })
198 238
199 { 239 it('Should have the correct follows counts', async function () {
200 const res = await getFollowersListPaginationAndSort({ url, start, count, sort, search, state: 'accepted' }) 240 await expectAccountFollows({ server: servers[0], handle: 'peertube@' + servers[0].host, followers: 0, following: 2 })
201 expect(res.body.total).to.equal(1) 241 await expectAccountFollows({ server: servers[0], handle: 'root@' + servers[1].host, followers: 1, following: 0 })
202 expect(res.body.data).to.have.lengthOf(1) 242 await expectAccountFollows({ server: servers[0], handle: 'peertube@' + servers[2].host, followers: 1, following: 0 })
203 }
204 243
205 { 244 // Server 2 and 3 does not know server 1 follow another server (there was not a refresh)
206 const res = await getFollowersListPaginationAndSort({ url, start, count, sort, search, state: 'accepted', actorType: 'Person' }) 245 await expectAccountFollows({ server: servers[1], handle: 'peertube@' + servers[0].host, followers: 0, following: 1 })
207 expect(res.body.total).to.equal(0) 246 await expectAccountFollows({ server: servers[1], handle: 'root@' + servers[1].host, followers: 1, following: 0 })
208 expect(res.body.data).to.have.lengthOf(0) 247 await expectAccountFollows({ server: servers[1], handle: 'peertube@' + servers[1].host, followers: 0, following: 0 })
209 }
210 248
211 { 249 await expectAccountFollows({ server: servers[2], handle: 'peertube@' + servers[0].host, followers: 0, following: 1 })
212 const res = await getFollowersListPaginationAndSort({ 250 await expectAccountFollows({ server: servers[2], handle: 'peertube@' + servers[2].host, followers: 1, following: 0 })
213 url, 251 })
214 start,
215 count,
216 sort,
217 search,
218 state: 'accepted',
219 actorType: 'Application'
220 })
221 expect(res.body.total).to.equal(1)
222 expect(res.body.data).to.have.lengthOf(1)
223 }
224 252
225 { 253 it('Should unfollow server 3 on server 1', async function () {
226 const res = await getFollowersListPaginationAndSort({ url, start, count, sort, search, state: 'pending' }) 254 this.timeout(15000)
227 expect(res.body.total).to.equal(0)
228 expect(res.body.data).to.have.lengthOf(0)
229 }
230 }
231 255
232 { 256 await servers[0].follows.unfollow({ target: servers[2] })
233 const res = await getFollowersListPaginationAndSort({ url, start, count, sort, search: 'bla' })
234 const follows = res.body.data
235 257
236 expect(res.body.total).to.equal(0) 258 await waitJobs(servers)
237 expect(follows.length).to.equal(0) 259 })
238 }
239 })
240 260
241 it('Should have 0 followers on server 1', async function () { 261 it('Should not follow server 3 on server 1 anymore', async function () {
242 const res = await getFollowersListPaginationAndSort({ url: servers[0].url, start: 0, count: 5, sort: 'createdAt' }) 262 const body = await servers[0].follows.getFollowings({ start: 0, count: 2, sort: 'createdAt' })
243 const follows = res.body.data 263 expect(body.total).to.equal(1)
244 264
245 expect(res.body.total).to.equal(0) 265 const follows = body.data
246 expect(follows).to.be.an('array') 266 expect(follows).to.be.an('array')
247 expect(follows.length).to.equal(0) 267 expect(follows).to.have.lengthOf(1)
248 })
249 268
250 it('Should have the correct follows counts', async function () { 269 expect(follows[0].following.host).to.equal(servers[1].host)
251 await expectAccountFollows(servers[0].url, 'peertube@localhost:' + servers[0].port, 0, 2) 270 })
252 await expectAccountFollows(servers[0].url, 'peertube@localhost:' + servers[1].port, 1, 0)
253 await expectAccountFollows(servers[0].url, 'peertube@localhost:' + servers[2].port, 1, 0)
254 271
255 // Server 2 and 3 does not know server 1 follow another server (there was not a refresh) 272 it('Should not have server 1 as follower on server 3 anymore', async function () {
256 await expectAccountFollows(servers[1].url, 'peertube@localhost:' + servers[0].port, 0, 1) 273 const body = await servers[2].follows.getFollowers({ start: 0, count: 1, sort: 'createdAt' })
257 await expectAccountFollows(servers[1].url, 'peertube@localhost:' + servers[1].port, 1, 0) 274 expect(body.total).to.equal(0)
258 275
259 await expectAccountFollows(servers[2].url, 'peertube@localhost:' + servers[0].port, 0, 1) 276 const follows = body.data
260 await expectAccountFollows(servers[2].url, 'peertube@localhost:' + servers[2].port, 1, 0) 277 expect(follows).to.be.an('array')
261 }) 278 expect(follows).to.have.lengthOf(0)
279 })
262 280
263 it('Should unfollow server 3 on server 1', async function () { 281 it('Should have the correct follows counts after the unfollow', async function () {
264 this.timeout(5000) 282 await expectAccountFollows({ server: servers[0], handle: 'peertube@' + servers[0].host, followers: 0, following: 1 })
283 await expectAccountFollows({ server: servers[0], handle: 'root@' + servers[1].host, followers: 1, following: 0 })
284 await expectAccountFollows({ server: servers[0], handle: 'peertube@' + servers[2].host, followers: 0, following: 0 })
265 285
266 await unfollow(servers[0].url, servers[0].accessToken, servers[2]) 286 await expectAccountFollows({ server: servers[1], handle: 'peertube@' + servers[0].host, followers: 0, following: 1 })
287 await expectAccountFollows({ server: servers[1], handle: 'root@' + servers[1].host, followers: 1, following: 0 })
288 await expectAccountFollows({ server: servers[1], handle: 'peertube@' + servers[1].host, followers: 0, following: 0 })
267 289
268 await waitJobs(servers) 290 await expectAccountFollows({ server: servers[2], handle: 'peertube@' + servers[0].host, followers: 0, following: 0 })
269 }) 291 await expectAccountFollows({ server: servers[2], handle: 'peertube@' + servers[2].host, followers: 0, following: 0 })
292 })
270 293
271 it('Should not follow server 3 on server 1 anymore', async function () { 294 it('Should upload a video on server 2 and 3 and propagate only the video of server 2', async function () {
272 const res = await getFollowingListPaginationAndSort({ url: servers[0].url, start: 0, count: 2, sort: 'createdAt' }) 295 this.timeout(60000)
273 const follows = res.body.data
274 296
275 expect(res.body.total).to.equal(1) 297 await servers[1].videos.upload({ attributes: { name: 'server2' } })
276 expect(follows).to.be.an('array') 298 await servers[2].videos.upload({ attributes: { name: 'server3' } })
277 expect(follows.length).to.equal(1)
278 299
279 expect(follows[0].following.host).to.equal('localhost:' + servers[1].port) 300 await waitJobs(servers)
280 })
281 301
282 it('Should not have server 1 as follower on server 3 anymore', async function () { 302 {
283 const res = await getFollowersListPaginationAndSort({ url: servers[2].url, start: 0, count: 1, sort: 'createdAt' }) 303 const { total, data } = await servers[0].videos.list()
304 expect(total).to.equal(1)
305 expect(data[0].name).to.equal('server2')
306 }
284 307
285 const follows = res.body.data 308 {
286 expect(res.body.total).to.equal(0) 309 const { total, data } = await servers[1].videos.list()
287 expect(follows).to.be.an('array') 310 expect(total).to.equal(1)
288 expect(follows.length).to.equal(0) 311 expect(data[0].name).to.equal('server2')
289 }) 312 }
313
314 {
315 const { total, data } = await servers[2].videos.list()
316 expect(total).to.equal(1)
317 expect(data[0].name).to.equal('server3')
318 }
319 })
290 320
291 it('Should have the correct follows counts 2', async function () { 321 it('Should remove account follow', async function () {
292 await expectAccountFollows(servers[0].url, 'peertube@localhost:' + servers[0].port, 0, 1) 322 this.timeout(15000)
293 await expectAccountFollows(servers[0].url, 'peertube@localhost:' + servers[1].port, 1, 0)
294 323
295 await expectAccountFollows(servers[1].url, 'peertube@localhost:' + servers[0].port, 0, 1) 324 await servers[0].follows.unfollow({ target: 'root@' + servers[1].host })
296 await expectAccountFollows(servers[1].url, 'peertube@localhost:' + servers[1].port, 1, 0)
297 325
298 await expectAccountFollows(servers[2].url, 'peertube@localhost:' + servers[0].port, 0, 0) 326 await waitJobs(servers)
299 await expectAccountFollows(servers[2].url, 'peertube@localhost:' + servers[2].port, 0, 0) 327 })
300 })
301 328
302 it('Should upload a video on server 2 and 3 and propagate only the video of server 2', async function () { 329 it('Should have removed the account follow', async function () {
303 this.timeout(60000) 330 await expectAccountFollows({ server: servers[0], handle: 'root@' + servers[1].host, followers: 0, following: 0 })
331 await expectAccountFollows({ server: servers[1], handle: 'root@' + servers[1].host, followers: 0, following: 0 })
304 332
305 await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'server2' }) 333 {
306 await uploadVideo(servers[2].url, servers[2].accessToken, { name: 'server3' }) 334 const { total, data } = await servers[0].follows.getFollowings()
335 expect(total).to.equal(0)
336 expect(data).to.have.lengthOf(0)
337 }
307 338
308 await waitJobs(servers) 339 {
340 const { total, data } = await servers[0].videos.list()
341 expect(total).to.equal(0)
342 expect(data).to.have.lengthOf(0)
343 }
344 })
309 345
310 let res = await getVideosList(servers[0].url) 346 it('Should follow a channel', async function () {
311 expect(res.body.total).to.equal(1) 347 this.timeout(15000)
312 expect(res.body.data[0].name).to.equal('server2')
313 348
314 res = await getVideosList(servers[1].url) 349 await servers[0].follows.follow({
315 expect(res.body.total).to.equal(1) 350 handles: [ 'root_channel@' + servers[1].host ]
316 expect(res.body.data[0].name).to.equal('server2') 351 })
317 352
318 res = await getVideosList(servers[2].url) 353 await waitJobs(servers)
319 expect(res.body.total).to.equal(1) 354
320 expect(res.body.data[0].name).to.equal('server3') 355 await expectChannelsFollows({ server: servers[0], handle: 'root_channel@' + servers[1].host, followers: 1, following: 0 })
356 await expectChannelsFollows({ server: servers[1], handle: 'root_channel@' + servers[1].host, followers: 1, following: 0 })
357
358 {
359 const { total, data } = await servers[0].follows.getFollowings()
360 expect(total).to.equal(1)
361 expect(data).to.have.lengthOf(1)
362 }
363
364 {
365 const { total, data } = await servers[0].videos.list()
366 expect(total).to.equal(1)
367 expect(data).to.have.lengthOf(1)
368 }
369 })
321 }) 370 })
322 371
323 describe('Should propagate data on a new following', function () { 372 describe('Should propagate data on a new server follow', function () {
324 let video4: Video 373 let video4: VideoCreateResult
325 374
326 before(async function () { 375 before(async function () {
327 this.timeout(50000) 376 this.timeout(50000)
@@ -334,100 +383,75 @@ describe('Test follows', function () {
334 tags: [ 'tag1', 'tag2', 'tag3' ] 383 tags: [ 'tag1', 'tag2', 'tag3' ]
335 } 384 }
336 385
337 await uploadVideo(servers[2].url, servers[2].accessToken, { name: 'server3-2' }) 386 await servers[2].videos.upload({ attributes: { name: 'server3-2' } })
338 await uploadVideo(servers[2].url, servers[2].accessToken, { name: 'server3-3' }) 387 await servers[2].videos.upload({ attributes: { name: 'server3-3' } })
339 await uploadVideo(servers[2].url, servers[2].accessToken, video4Attributes) 388 video4 = await servers[2].videos.upload({ attributes: video4Attributes })
340 await uploadVideo(servers[2].url, servers[2].accessToken, { name: 'server3-5' }) 389 await servers[2].videos.upload({ attributes: { name: 'server3-5' } })
341 await uploadVideo(servers[2].url, servers[2].accessToken, { name: 'server3-6' }) 390 await servers[2].videos.upload({ attributes: { name: 'server3-6' } })
342 391
343 { 392 {
344 const user = { username: 'captain', password: 'password' } 393 const userAccessToken = await servers[2].users.generateUserAndToken('captain')
345 await createUser({ url: servers[2].url, accessToken: servers[2].accessToken, username: user.username, password: user.password })
346 const userAccessToken = await userLogin(servers[2], user)
347
348 const resVideos = await getVideosList(servers[2].url)
349 video4 = resVideos.body.data.find(v => v.name === 'server3-4')
350 394
351 { 395 await servers[2].videos.rate({ id: video4.id, rating: 'like' })
352 await rateVideo(servers[2].url, servers[2].accessToken, video4.id, 'like') 396 await servers[2].videos.rate({ token: userAccessToken, id: video4.id, rating: 'dislike' })
353 await rateVideo(servers[2].url, userAccessToken, video4.id, 'dislike') 397 }
354 }
355
356 {
357 {
358 const text = 'my super first comment'
359 const res = await addVideoCommentThread(servers[2].url, servers[2].accessToken, video4.id, text)
360 const threadId = res.body.comment.id
361
362 const text1 = 'my super answer to thread 1'
363 const childCommentRes = await addVideoCommentReply(servers[2].url, servers[2].accessToken, video4.id, threadId, text1)
364 const childCommentId = childCommentRes.body.comment.id
365
366 const text2 = 'my super answer to answer of thread 1'
367 await addVideoCommentReply(servers[2].url, servers[2].accessToken, video4.id, childCommentId, text2)
368
369 const text3 = 'my second answer to thread 1'
370 await addVideoCommentReply(servers[2].url, servers[2].accessToken, video4.id, threadId, text3)
371 }
372 398
373 { 399 {
374 const text = 'will be deleted' 400 await servers[2].comments.createThread({ videoId: video4.id, text: 'my super first comment' })
375 const res = await addVideoCommentThread(servers[2].url, servers[2].accessToken, video4.id, text)
376 const threadId = res.body.comment.id
377 401
378 const text1 = 'answer to deleted' 402 await servers[2].comments.addReplyToLastThread({ text: 'my super answer to thread 1' })
379 await addVideoCommentReply(servers[2].url, servers[2].accessToken, video4.id, threadId, text1) 403 await servers[2].comments.addReplyToLastReply({ text: 'my super answer to answer of thread 1' })
404 await servers[2].comments.addReplyToLastThread({ text: 'my second answer to thread 1' })
405 }
380 406
381 const text2 = 'will also be deleted' 407 {
382 const childCommentRes = await addVideoCommentReply(servers[2].url, servers[2].accessToken, video4.id, threadId, text2) 408 const { id: threadId } = await servers[2].comments.createThread({ videoId: video4.id, text: 'will be deleted' })
383 const childCommentId = childCommentRes.body.comment.id 409 await servers[2].comments.addReplyToLastThread({ text: 'answer to deleted' })
384 410
385 const text3 = 'my second answer to deleted' 411 const { id: replyId } = await servers[2].comments.addReplyToLastThread({ text: 'will also be deleted' })
386 await addVideoCommentReply(servers[2].url, servers[2].accessToken, video4.id, childCommentId, text3)
387 412
388 await deleteVideoComment(servers[2].url, servers[2].accessToken, video4.id, threadId) 413 await servers[2].comments.addReplyToLastReply({ text: 'my second answer to deleted' })
389 await deleteVideoComment(servers[2].url, servers[2].accessToken, video4.id, childCommentId)
390 }
391 }
392 414
393 { 415 await servers[2].comments.delete({ videoId: video4.id, commentId: threadId })
394 await createVideoCaption({ 416 await servers[2].comments.delete({ videoId: video4.id, commentId: replyId })
395 url: servers[2].url,
396 accessToken: servers[2].accessToken,
397 language: 'ar',
398 videoId: video4.id,
399 fixture: 'subtitle-good2.vtt'
400 })
401 }
402 } 417 }
403 418
419 await servers[2].captions.add({
420 language: 'ar',
421 videoId: video4.id,
422 fixture: 'subtitle-good2.vtt'
423 })
424
404 await waitJobs(servers) 425 await waitJobs(servers)
405 426
406 // Server 1 follows server 3 427 // Server 1 follows server 3
407 await follow(servers[0].url, [ servers[2].url ], servers[0].accessToken) 428 await servers[0].follows.follow({ hosts: [ servers[2].url ] })
408 429
409 await waitJobs(servers) 430 await waitJobs(servers)
410 }) 431 })
411 432
412 it('Should have the correct follows counts 3', async function () { 433 it('Should have the correct follows counts', async function () {
413 await expectAccountFollows(servers[0].url, 'peertube@localhost:' + servers[0].port, 0, 2) 434 await expectAccountFollows({ server: servers[0], handle: 'peertube@' + servers[0].host, followers: 0, following: 2 })
414 await expectAccountFollows(servers[0].url, 'peertube@localhost:' + servers[1].port, 1, 0) 435 await expectAccountFollows({ server: servers[0], handle: 'root@' + servers[1].host, followers: 0, following: 0 })
415 await expectAccountFollows(servers[0].url, 'peertube@localhost:' + servers[2].port, 1, 0) 436 await expectChannelsFollows({ server: servers[0], handle: 'root_channel@' + servers[1].host, followers: 1, following: 0 })
437 await expectAccountFollows({ server: servers[0], handle: 'peertube@' + servers[2].host, followers: 1, following: 0 })
416 438
417 await expectAccountFollows(servers[1].url, 'peertube@localhost:' + servers[0].port, 0, 1) 439 await expectAccountFollows({ server: servers[1], handle: 'peertube@' + servers[0].host, followers: 0, following: 1 })
418 await expectAccountFollows(servers[1].url, 'peertube@localhost:' + servers[1].port, 1, 0) 440 await expectAccountFollows({ server: servers[1], handle: 'peertube@' + servers[1].host, followers: 0, following: 0 })
441 await expectAccountFollows({ server: servers[1], handle: 'root@' + servers[1].host, followers: 0, following: 0 })
442 await expectChannelsFollows({ server: servers[1], handle: 'root_channel@' + servers[1].host, followers: 1, following: 0 })
419 443
420 await expectAccountFollows(servers[2].url, 'peertube@localhost:' + servers[0].port, 0, 1) 444 await expectAccountFollows({ server: servers[2], handle: 'peertube@' + servers[0].host, followers: 0, following: 1 })
421 await expectAccountFollows(servers[2].url, 'peertube@localhost:' + servers[2].port, 1, 0) 445 await expectAccountFollows({ server: servers[2], handle: 'peertube@' + servers[2].host, followers: 1, following: 0 })
422 }) 446 })
423 447
424 it('Should have propagated videos', async function () { 448 it('Should have propagated videos', async function () {
425 const res = await getVideosList(servers[0].url) 449 const { total, data } = await servers[0].videos.list()
426 expect(res.body.total).to.equal(7) 450 expect(total).to.equal(7)
427 451
428 const video2 = res.body.data.find(v => v.name === 'server3-2') 452 const video2 = data.find(v => v.name === 'server3-2')
429 video4 = res.body.data.find(v => v.name === 'server3-4') 453 video4 = data.find(v => v.name === 'server3-4')
430 const video6 = res.body.data.find(v => v.name === 'server3-6') 454 const video6 = data.find(v => v.name === 'server3-6')
431 455
432 expect(video2).to.not.be.undefined 456 expect(video2).to.not.be.undefined
433 expect(video4).to.not.be.undefined 457 expect(video4).to.not.be.undefined
@@ -444,7 +468,7 @@ describe('Test follows', function () {
444 support: 'my super support text', 468 support: 'my super support text',
445 account: { 469 account: {
446 name: 'root', 470 name: 'root',
447 host: 'localhost:' + servers[2].port 471 host: servers[2].host
448 }, 472 },
449 isLocal, 473 isLocal,
450 commentsEnabled: true, 474 commentsEnabled: true,
@@ -468,33 +492,31 @@ describe('Test follows', function () {
468 } 492 }
469 ] 493 ]
470 } 494 }
471 await completeVideoCheck(servers[0].url, video4, checkAttributes) 495 await completeVideoCheck(servers[0], video4, checkAttributes)
472 }) 496 })
473 497
474 it('Should have propagated comments', async function () { 498 it('Should have propagated comments', async function () {
475 const res1 = await getVideoCommentThreads(servers[0].url, video4.id, 0, 5, 'createdAt') 499 const { total, data } = await servers[0].comments.listThreads({ videoId: video4.id, sort: 'createdAt' })
476 500
477 expect(res1.body.total).to.equal(2) 501 expect(total).to.equal(2)
478 expect(res1.body.data).to.be.an('array') 502 expect(data).to.be.an('array')
479 expect(res1.body.data).to.have.lengthOf(2) 503 expect(data).to.have.lengthOf(2)
480 504
481 { 505 {
482 const comment: VideoComment = res1.body.data[0] 506 const comment = data[0]
483 expect(comment.inReplyToCommentId).to.be.null 507 expect(comment.inReplyToCommentId).to.be.null
484 expect(comment.text).equal('my super first comment') 508 expect(comment.text).equal('my super first comment')
485 expect(comment.videoId).to.equal(video4.id) 509 expect(comment.videoId).to.equal(video4.id)
486 expect(comment.id).to.equal(comment.threadId) 510 expect(comment.id).to.equal(comment.threadId)
487 expect(comment.account.name).to.equal('root') 511 expect(comment.account.name).to.equal('root')
488 expect(comment.account.host).to.equal('localhost:' + servers[2].port) 512 expect(comment.account.host).to.equal(servers[2].host)
489 expect(comment.totalReplies).to.equal(3) 513 expect(comment.totalReplies).to.equal(3)
490 expect(dateIsValid(comment.createdAt as string)).to.be.true 514 expect(dateIsValid(comment.createdAt as string)).to.be.true
491 expect(dateIsValid(comment.updatedAt as string)).to.be.true 515 expect(dateIsValid(comment.updatedAt as string)).to.be.true
492 516
493 const threadId = comment.threadId 517 const threadId = comment.threadId
494 518
495 const res2 = await getVideoThreadComments(servers[0].url, video4.id, threadId) 519 const tree = await servers[0].comments.getThread({ videoId: video4.id, threadId })
496
497 const tree: VideoCommentThreadTree = res2.body
498 expect(tree.comment.text).equal('my super first comment') 520 expect(tree.comment.text).equal('my super first comment')
499 expect(tree.children).to.have.lengthOf(2) 521 expect(tree.children).to.have.lengthOf(2)
500 522
@@ -512,7 +534,7 @@ describe('Test follows', function () {
512 } 534 }
513 535
514 { 536 {
515 const deletedComment: VideoComment = res1.body.data[1] 537 const deletedComment = data[1]
516 expect(deletedComment).to.not.be.undefined 538 expect(deletedComment).to.not.be.undefined
517 expect(deletedComment.isDeleted).to.be.true 539 expect(deletedComment.isDeleted).to.be.true
518 expect(deletedComment.deletedAt).to.not.be.null 540 expect(deletedComment.deletedAt).to.not.be.null
@@ -522,9 +544,7 @@ describe('Test follows', function () {
522 expect(deletedComment.totalReplies).to.equal(2) 544 expect(deletedComment.totalReplies).to.equal(2)
523 expect(dateIsValid(deletedComment.deletedAt as string)).to.be.true 545 expect(dateIsValid(deletedComment.deletedAt as string)).to.be.true
524 546
525 const res2 = await getVideoThreadComments(servers[0].url, video4.id, deletedComment.threadId) 547 const tree = await servers[0].comments.getThread({ videoId: video4.id, threadId: deletedComment.threadId })
526
527 const tree: VideoCommentThreadTree = res2.body
528 const [ commentRoot, deletedChildRoot ] = tree.children 548 const [ commentRoot, deletedChildRoot ] = tree.children
529 549
530 expect(deletedChildRoot).to.not.be.undefined 550 expect(deletedChildRoot).to.not.be.undefined
@@ -549,11 +569,11 @@ describe('Test follows', function () {
549 }) 569 })
550 570
551 it('Should have propagated captions', async function () { 571 it('Should have propagated captions', async function () {
552 const res = await listVideoCaptions(servers[0].url, video4.id) 572 const body = await servers[0].captions.list({ videoId: video4.id })
553 expect(res.body.total).to.equal(1) 573 expect(body.total).to.equal(1)
554 expect(res.body.data).to.have.lengthOf(1) 574 expect(body.data).to.have.lengthOf(1)
555 575
556 const caption1: VideoCaption = res.body.data[0] 576 const caption1 = body.data[0]
557 expect(caption1.language.id).to.equal('ar') 577 expect(caption1.language.id).to.equal('ar')
558 expect(caption1.language.label).to.equal('Arabic') 578 expect(caption1.language.label).to.equal('Arabic')
559 expect(caption1.captionPath).to.match(new RegExp('^/lazy-static/video-captions/.+-ar.vtt$')) 579 expect(caption1.captionPath).to.match(new RegExp('^/lazy-static/video-captions/.+-ar.vtt$'))
@@ -563,14 +583,39 @@ describe('Test follows', function () {
563 it('Should unfollow server 3 on server 1 and does not list server 3 videos', async function () { 583 it('Should unfollow server 3 on server 1 and does not list server 3 videos', async function () {
564 this.timeout(5000) 584 this.timeout(5000)
565 585
566 await unfollow(servers[0].url, servers[0].accessToken, servers[2]) 586 await servers[0].follows.unfollow({ target: servers[2] })
587
588 await waitJobs(servers)
589
590 const { total } = await servers[0].videos.list()
591 expect(total).to.equal(1)
592 })
593 })
594
595 describe('Should propagate data on a new channel follow', function () {
596
597 before(async function () {
598 this.timeout(60000)
599
600 await servers[2].videos.upload({ attributes: { name: 'server3-7' } })
567 601
568 await waitJobs(servers) 602 await waitJobs(servers)
569 603
570 const res = await getVideosList(servers[0].url) 604 const video = await servers[0].videos.find({ name: 'server3-7' })
571 expect(res.body.total).to.equal(1) 605 expect(video).to.not.exist
572 }) 606 })
573 607
608 it('Should have propagated channel video', async function () {
609 this.timeout(60000)
610
611 await servers[0].follows.follow({ handles: [ 'root_channel@' + servers[2].host ] })
612
613 await waitJobs(servers)
614
615 const video = await servers[0].videos.find({ name: 'server3-7' })
616
617 expect(video).to.exist
618 })
574 }) 619 })
575 620
576 after(async function () { 621 after(async function () {
diff --git a/server/tests/api/server/handle-down.ts b/server/tests/api/server/handle-down.ts
index d57d72f5e..2f3950354 100644
--- a/server/tests/api/server/handle-down.ts
+++ b/server/tests/api/server/handle-down.ts
@@ -1,51 +1,31 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2 2
3import * as chai from 'chai'
4import 'mocha' 3import 'mocha'
5import { JobState, Video } from '../../../../shared/models' 4import * as chai from 'chai'
6import { VideoPrivacy } from '../../../../shared/models/videos'
7import { VideoCommentThreadTree } from '../../../../shared/models/videos/comment/video-comment.model'
8
9import { 5import {
10 cleanupTests, 6 cleanupTests,
11 closeAllSequelize, 7 CommentsCommand,
12 completeVideoCheck, 8 completeVideoCheck,
13 flushAndRunMultipleServers, 9 createMultipleServers,
14 getVideo,
15 getVideosList,
16 immutableAssign,
17 killallServers, 10 killallServers,
18 reRunServer, 11 PeerTubeServer,
19 ServerInfo,
20 setAccessTokensToServers, 12 setAccessTokensToServers,
21 setActorFollowScores, 13 wait,
22 unfollow, 14 waitJobs
23 updateVideo, 15} from '@shared/extra-utils'
24 uploadVideo, 16import { HttpStatusCode, JobState, VideoCreateResult, VideoPrivacy } from '@shared/models'
25 uploadVideoAndGetId,
26 wait
27} from '../../../../shared/extra-utils'
28import { follow, getFollowersListPaginationAndSort } from '../../../../shared/extra-utils/server/follows'
29import { getJobsListPaginationAndSort, waitJobs } from '../../../../shared/extra-utils/server/jobs'
30import {
31 addVideoCommentReply,
32 addVideoCommentThread,
33 getVideoCommentThreads,
34 getVideoThreadComments
35} from '../../../../shared/extra-utils/videos/video-comments'
36import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
37 17
38const expect = chai.expect 18const expect = chai.expect
39 19
40describe('Test handle downs', function () { 20describe('Test handle downs', function () {
41 let servers: ServerInfo[] = [] 21 let servers: PeerTubeServer[] = []
42 let threadIdServer1: number 22 let threadIdServer1: number
43 let threadIdServer2: number 23 let threadIdServer2: number
44 let commentIdServer1: number 24 let commentIdServer1: number
45 let commentIdServer2: number 25 let commentIdServer2: number
46 let missedVideo1: Video 26 let missedVideo1: VideoCreateResult
47 let missedVideo2: Video 27 let missedVideo2: VideoCreateResult
48 let unlistedVideo: Video 28 let unlistedVideo: VideoCreateResult
49 29
50 const videoIdsServer1: string[] = [] 30 const videoIdsServer1: string[] = []
51 31
@@ -62,17 +42,18 @@ describe('Test handle downs', function () {
62 fixture: 'video_short1.webm' 42 fixture: 'video_short1.webm'
63 } 43 }
64 44
65 const unlistedVideoAttributes = immutableAssign(videoAttributes, { 45 const unlistedVideoAttributes = { ...videoAttributes, privacy: VideoPrivacy.UNLISTED }
66 privacy: VideoPrivacy.UNLISTED
67 })
68 46
69 let checkAttributes: any 47 let checkAttributes: any
70 let unlistedCheckAttributes: any 48 let unlistedCheckAttributes: any
71 49
50 let commentCommands: CommentsCommand[]
51
72 before(async function () { 52 before(async function () {
73 this.timeout(30000) 53 this.timeout(30000)
74 54
75 servers = await flushAndRunMultipleServers(3) 55 servers = await createMultipleServers(3)
56 commentCommands = servers.map(s => s.comments)
76 57
77 checkAttributes = { 58 checkAttributes = {
78 name: 'my super name for server 1', 59 name: 'my super name for server 1',
@@ -106,9 +87,7 @@ describe('Test handle downs', function () {
106 } 87 }
107 ] 88 ]
108 } 89 }
109 unlistedCheckAttributes = immutableAssign(checkAttributes, { 90 unlistedCheckAttributes = { ...checkAttributes, privacy: VideoPrivacy.UNLISTED }
110 privacy: VideoPrivacy.UNLISTED
111 })
112 91
113 // Get the access tokens 92 // Get the access tokens
114 await setAccessTokensToServers(servers) 93 await setAccessTokensToServers(servers)
@@ -118,58 +97,53 @@ describe('Test handle downs', function () {
118 this.timeout(240000) 97 this.timeout(240000)
119 98
120 // Server 2 and 3 follow server 1 99 // Server 2 and 3 follow server 1
121 await follow(servers[1].url, [ servers[0].url ], servers[1].accessToken) 100 await servers[1].follows.follow({ hosts: [ servers[0].url ] })
122 await follow(servers[2].url, [ servers[0].url ], servers[2].accessToken) 101 await servers[2].follows.follow({ hosts: [ servers[0].url ] })
123 102
124 await waitJobs(servers) 103 await waitJobs(servers)
125 104
126 // Upload a video to server 1 105 // Upload a video to server 1
127 await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes) 106 await servers[0].videos.upload({ attributes: videoAttributes })
128 107
129 await waitJobs(servers) 108 await waitJobs(servers)
130 109
131 // And check all servers have this video 110 // And check all servers have this video
132 for (const server of servers) { 111 for (const server of servers) {
133 const res = await getVideosList(server.url) 112 const { data } = await server.videos.list()
134 expect(res.body.data).to.be.an('array') 113 expect(data).to.be.an('array')
135 expect(res.body.data).to.have.lengthOf(1) 114 expect(data).to.have.lengthOf(1)
136 } 115 }
137 116
138 // Kill server 2 117 // Kill server 2
139 killallServers([ servers[1] ]) 118 await killallServers([ servers[1] ])
140 119
141 // Remove server 2 follower 120 // Remove server 2 follower
142 for (let i = 0; i < 10; i++) { 121 for (let i = 0; i < 10; i++) {
143 await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes) 122 await servers[0].videos.upload({ attributes: videoAttributes })
144 } 123 }
145 124
146 await waitJobs([ servers[0], servers[2] ]) 125 await waitJobs([ servers[0], servers[2] ])
147 126
148 // Kill server 3 127 // Kill server 3
149 killallServers([ servers[2] ]) 128 await killallServers([ servers[2] ])
150 129
151 const resLastVideo1 = await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes) 130 missedVideo1 = await servers[0].videos.upload({ attributes: videoAttributes })
152 missedVideo1 = resLastVideo1.body.video
153 131
154 const resLastVideo2 = await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes) 132 missedVideo2 = await servers[0].videos.upload({ attributes: videoAttributes })
155 missedVideo2 = resLastVideo2.body.video
156 133
157 // Unlisted video 134 // Unlisted video
158 const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, unlistedVideoAttributes) 135 unlistedVideo = await servers[0].videos.upload({ attributes: unlistedVideoAttributes })
159 unlistedVideo = resVideo.body.video
160 136
161 // Add comments to video 2 137 // Add comments to video 2
162 { 138 {
163 const text = 'thread 1' 139 const text = 'thread 1'
164 let resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, missedVideo2.uuid, text) 140 let comment = await commentCommands[0].createThread({ videoId: missedVideo2.uuid, text })
165 let comment = resComment.body.comment
166 threadIdServer1 = comment.id 141 threadIdServer1 = comment.id
167 142
168 resComment = await addVideoCommentReply(servers[0].url, servers[0].accessToken, missedVideo2.uuid, comment.id, 'comment 1-1') 143 comment = await commentCommands[0].addReply({ videoId: missedVideo2.uuid, toCommentId: comment.id, text: 'comment 1-1' })
169 comment = resComment.body.comment
170 144
171 resComment = await addVideoCommentReply(servers[0].url, servers[0].accessToken, missedVideo2.uuid, comment.id, 'comment 1-2') 145 const created = await commentCommands[0].addReply({ videoId: missedVideo2.uuid, toCommentId: comment.id, text: 'comment 1-2' })
172 commentIdServer1 = resComment.body.comment.id 146 commentIdServer1 = created.id
173 } 147 }
174 148
175 await waitJobs(servers[0]) 149 await waitJobs(servers[0])
@@ -177,90 +151,87 @@ describe('Test handle downs', function () {
177 await wait(11000) 151 await wait(11000)
178 152
179 // Only server 3 is still a follower of server 1 153 // Only server 3 is still a follower of server 1
180 const res = await getFollowersListPaginationAndSort({ url: servers[0].url, start: 0, count: 2, sort: 'createdAt' }) 154 const body = await servers[0].follows.getFollowers({ start: 0, count: 2, sort: 'createdAt' })
181 expect(res.body.data).to.be.an('array') 155 expect(body.data).to.be.an('array')
182 expect(res.body.data).to.have.lengthOf(1) 156 expect(body.data).to.have.lengthOf(1)
183 expect(res.body.data[0].follower.host).to.equal('localhost:' + servers[2].port) 157 expect(body.data[0].follower.host).to.equal('localhost:' + servers[2].port)
184 }) 158 })
185 159
186 it('Should not have pending/processing jobs anymore', async function () { 160 it('Should not have pending/processing jobs anymore', async function () {
187 const states: JobState[] = [ 'waiting', 'active' ] 161 const states: JobState[] = [ 'waiting', 'active' ]
188 162
189 for (const state of states) { 163 for (const state of states) {
190 const res = await getJobsListPaginationAndSort({ 164 const body = await servers[0].jobs.getJobsList({
191 url: servers[0].url,
192 accessToken: servers[0].accessToken,
193 state: state, 165 state: state,
194 start: 0, 166 start: 0,
195 count: 50, 167 count: 50,
196 sort: '-createdAt' 168 sort: '-createdAt'
197 }) 169 })
198 expect(res.body.data).to.have.length(0) 170 expect(body.data).to.have.length(0)
199 } 171 }
200 }) 172 })
201 173
202 it('Should re-follow server 1', async function () { 174 it('Should re-follow server 1', async function () {
203 this.timeout(35000) 175 this.timeout(35000)
204 176
205 await reRunServer(servers[1]) 177 await servers[1].run()
206 await reRunServer(servers[2]) 178 await servers[2].run()
207 179
208 await unfollow(servers[1].url, servers[1].accessToken, servers[0]) 180 await servers[1].follows.unfollow({ target: servers[0] })
209 await waitJobs(servers) 181 await waitJobs(servers)
210 182
211 await follow(servers[1].url, [ servers[0].url ], servers[1].accessToken) 183 await servers[1].follows.follow({ hosts: [ servers[0].url ] })
212 184
213 await waitJobs(servers) 185 await waitJobs(servers)
214 186
215 const res = await getFollowersListPaginationAndSort({ url: servers[0].url, start: 0, count: 2, sort: 'createdAt' }) 187 const body = await servers[0].follows.getFollowers({ start: 0, count: 2, sort: 'createdAt' })
216 expect(res.body.data).to.be.an('array') 188 expect(body.data).to.be.an('array')
217 expect(res.body.data).to.have.lengthOf(2) 189 expect(body.data).to.have.lengthOf(2)
218 }) 190 })
219 191
220 it('Should send an update to server 3, and automatically fetch the video', async function () { 192 it('Should send an update to server 3, and automatically fetch the video', async function () {
221 this.timeout(15000) 193 this.timeout(15000)
222 194
223 const res1 = await getVideosList(servers[2].url) 195 {
224 expect(res1.body.data).to.be.an('array') 196 const { data } = await servers[2].videos.list()
225 expect(res1.body.data).to.have.lengthOf(11) 197 expect(data).to.be.an('array')
198 expect(data).to.have.lengthOf(11)
199 }
226 200
227 await updateVideo(servers[0].url, servers[0].accessToken, missedVideo1.uuid, {}) 201 await servers[0].videos.update({ id: missedVideo1.uuid })
228 await updateVideo(servers[0].url, servers[0].accessToken, unlistedVideo.uuid, {}) 202 await servers[0].videos.update({ id: unlistedVideo.uuid })
229 203
230 await waitJobs(servers) 204 await waitJobs(servers)
231 205
232 const res = await getVideosList(servers[2].url) 206 {
233 expect(res.body.data).to.be.an('array') 207 const { data } = await servers[2].videos.list()
234 // 1 video is unlisted 208 expect(data).to.be.an('array')
235 expect(res.body.data).to.have.lengthOf(12) 209 // 1 video is unlisted
210 expect(data).to.have.lengthOf(12)
211 }
236 212
237 // Check unlisted video 213 // Check unlisted video
238 const resVideo = await getVideo(servers[2].url, unlistedVideo.uuid) 214 const video = await servers[2].videos.get({ id: unlistedVideo.uuid })
239 expect(resVideo.body).not.to.be.undefined 215 await completeVideoCheck(servers[2], video, unlistedCheckAttributes)
240
241 await completeVideoCheck(servers[2].url, resVideo.body, unlistedCheckAttributes)
242 }) 216 })
243 217
244 it('Should send comments on a video to server 3, and automatically fetch the video', async function () { 218 it('Should send comments on a video to server 3, and automatically fetch the video', async function () {
245 this.timeout(25000) 219 this.timeout(25000)
246 220
247 await addVideoCommentReply(servers[0].url, servers[0].accessToken, missedVideo2.uuid, commentIdServer1, 'comment 1-3') 221 await commentCommands[0].addReply({ videoId: missedVideo2.uuid, toCommentId: commentIdServer1, text: 'comment 1-3' })
248 222
249 await waitJobs(servers) 223 await waitJobs(servers)
250 224
251 const resVideo = await getVideo(servers[2].url, missedVideo2.uuid) 225 await servers[2].videos.get({ id: missedVideo2.uuid })
252 expect(resVideo.body).not.to.be.undefined
253 226
254 { 227 {
255 let resComment = await getVideoCommentThreads(servers[2].url, missedVideo2.uuid, 0, 5) 228 const { data } = await servers[2].comments.listThreads({ videoId: missedVideo2.uuid })
256 expect(resComment.body.data).to.be.an('array') 229 expect(data).to.be.an('array')
257 expect(resComment.body.data).to.have.lengthOf(1) 230 expect(data).to.have.lengthOf(1)
258
259 threadIdServer2 = resComment.body.data[0].id
260 231
261 resComment = await getVideoThreadComments(servers[2].url, missedVideo2.uuid, threadIdServer2) 232 threadIdServer2 = data[0].id
262 233
263 const tree: VideoCommentThreadTree = resComment.body 234 const tree = await servers[2].comments.getThread({ videoId: missedVideo2.uuid, threadId: threadIdServer2 })
264 expect(tree.comment.text).equal('thread 1') 235 expect(tree.comment.text).equal('thread 1')
265 expect(tree.children).to.have.lengthOf(1) 236 expect(tree.children).to.have.lengthOf(1)
266 237
@@ -283,57 +254,54 @@ describe('Test handle downs', function () {
283 it('Should correctly reply to the comment', async function () { 254 it('Should correctly reply to the comment', async function () {
284 this.timeout(15000) 255 this.timeout(15000)
285 256
286 await addVideoCommentReply(servers[2].url, servers[2].accessToken, missedVideo2.uuid, commentIdServer2, 'comment 1-4') 257 await servers[2].comments.addReply({ videoId: missedVideo2.uuid, toCommentId: commentIdServer2, text: 'comment 1-4' })
287 258
288 await waitJobs(servers) 259 await waitJobs(servers)
289 260
290 { 261 const tree = await commentCommands[0].getThread({ videoId: missedVideo2.uuid, threadId: threadIdServer1 })
291 const resComment = await getVideoThreadComments(servers[0].url, missedVideo2.uuid, threadIdServer1)
292 262
293 const tree: VideoCommentThreadTree = resComment.body 263 expect(tree.comment.text).equal('thread 1')
294 expect(tree.comment.text).equal('thread 1') 264 expect(tree.children).to.have.lengthOf(1)
295 expect(tree.children).to.have.lengthOf(1)
296 265
297 const firstChild = tree.children[0] 266 const firstChild = tree.children[0]
298 expect(firstChild.comment.text).to.equal('comment 1-1') 267 expect(firstChild.comment.text).to.equal('comment 1-1')
299 expect(firstChild.children).to.have.lengthOf(1) 268 expect(firstChild.children).to.have.lengthOf(1)
300 269
301 const childOfFirstChild = firstChild.children[0] 270 const childOfFirstChild = firstChild.children[0]
302 expect(childOfFirstChild.comment.text).to.equal('comment 1-2') 271 expect(childOfFirstChild.comment.text).to.equal('comment 1-2')
303 expect(childOfFirstChild.children).to.have.lengthOf(1) 272 expect(childOfFirstChild.children).to.have.lengthOf(1)
304 273
305 const childOfChildFirstChild = childOfFirstChild.children[0] 274 const childOfChildFirstChild = childOfFirstChild.children[0]
306 expect(childOfChildFirstChild.comment.text).to.equal('comment 1-3') 275 expect(childOfChildFirstChild.comment.text).to.equal('comment 1-3')
307 expect(childOfChildFirstChild.children).to.have.lengthOf(1) 276 expect(childOfChildFirstChild.children).to.have.lengthOf(1)
308 277
309 const childOfChildOfChildOfFirstChild = childOfChildFirstChild.children[0] 278 const childOfChildOfChildOfFirstChild = childOfChildFirstChild.children[0]
310 expect(childOfChildOfChildOfFirstChild.comment.text).to.equal('comment 1-4') 279 expect(childOfChildOfChildOfFirstChild.comment.text).to.equal('comment 1-4')
311 expect(childOfChildOfChildOfFirstChild.children).to.have.lengthOf(0) 280 expect(childOfChildOfChildOfFirstChild.children).to.have.lengthOf(0)
312 }
313 }) 281 })
314 282
315 it('Should upload many videos on server 1', async function () { 283 it('Should upload many videos on server 1', async function () {
316 this.timeout(120000) 284 this.timeout(120000)
317 285
318 for (let i = 0; i < 10; i++) { 286 for (let i = 0; i < 10; i++) {
319 const uuid = (await uploadVideoAndGetId({ server: servers[0], videoName: 'video ' + i })).uuid 287 const uuid = (await servers[0].videos.quickUpload({ name: 'video ' + i })).uuid
320 videoIdsServer1.push(uuid) 288 videoIdsServer1.push(uuid)
321 } 289 }
322 290
323 await waitJobs(servers) 291 await waitJobs(servers)
324 292
325 for (const id of videoIdsServer1) { 293 for (const id of videoIdsServer1) {
326 await getVideo(servers[1].url, id) 294 await servers[1].videos.get({ id })
327 } 295 }
328 296
329 await waitJobs(servers) 297 await waitJobs(servers)
330 await setActorFollowScores(servers[1].internalServerNumber, 20) 298 await servers[1].sql.setActorFollowScores(20)
331 299
332 // Wait video expiration 300 // Wait video expiration
333 await wait(11000) 301 await wait(11000)
334 302
335 // Refresh video -> score + 10 = 30 303 // Refresh video -> score + 10 = 30
336 await getVideo(servers[1].url, videoIdsServer1[0]) 304 await servers[1].videos.get({ id: videoIdsServer1[0] })
337 305
338 await waitJobs(servers) 306 await waitJobs(servers)
339 }) 307 })
@@ -341,27 +309,25 @@ describe('Test handle downs', function () {
341 it('Should remove followings that are down', async function () { 309 it('Should remove followings that are down', async function () {
342 this.timeout(120000) 310 this.timeout(120000)
343 311
344 killallServers([ servers[0] ]) 312 await killallServers([ servers[0] ])
345 313
346 // Wait video expiration 314 // Wait video expiration
347 await wait(11000) 315 await wait(11000)
348 316
349 for (let i = 0; i < 5; i++) { 317 for (let i = 0; i < 5; i++) {
350 try { 318 try {
351 await getVideo(servers[1].url, videoIdsServer1[i]) 319 await servers[1].videos.get({ id: videoIdsServer1[i] })
352 await waitJobs([ servers[1] ]) 320 await waitJobs([ servers[1] ])
353 await wait(1500) 321 await wait(1500)
354 } catch {} 322 } catch {}
355 } 323 }
356 324
357 for (const id of videoIdsServer1) { 325 for (const id of videoIdsServer1) {
358 await getVideo(servers[1].url, id, HttpStatusCode.FORBIDDEN_403) 326 await servers[1].videos.get({ id, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
359 } 327 }
360 }) 328 })
361 329
362 after(async function () { 330 after(async function () {
363 await closeAllSequelize([ servers[1] ])
364
365 await cleanupTests(servers) 331 await cleanupTests(servers)
366 }) 332 })
367}) 333})
diff --git a/server/tests/api/server/homepage.ts b/server/tests/api/server/homepage.ts
index e8ba89ca6..cb3ba5677 100644
--- a/server/tests/api/server/homepage.ts
+++ b/server/tests/api/server/homepage.ts
@@ -2,51 +2,48 @@
2 2
3import 'mocha' 3import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5import { HttpStatusCode } from '@shared/core-utils' 5import { HttpStatusCode } from '@shared/models'
6import { CustomPage, ServerConfig } from '@shared/models'
7import { 6import {
8 cleanupTests, 7 cleanupTests,
9 flushAndRunServer, 8 createSingleServer,
10 getConfig, 9 CustomPagesCommand,
11 getInstanceHomepage,
12 killallServers, 10 killallServers,
13 reRunServer, 11 PeerTubeServer,
14 ServerInfo, 12 setAccessTokensToServers
15 setAccessTokensToServers,
16 updateInstanceHomepage
17} from '../../../../shared/extra-utils/index' 13} from '../../../../shared/extra-utils/index'
18 14
19const expect = chai.expect 15const expect = chai.expect
20 16
21async function getHomepageState (server: ServerInfo) { 17async function getHomepageState (server: PeerTubeServer) {
22 const res = await getConfig(server.url) 18 const config = await server.config.getConfig()
23 19
24 const config = res.body as ServerConfig
25 return config.homepage.enabled 20 return config.homepage.enabled
26} 21}
27 22
28describe('Test instance homepage actions', function () { 23describe('Test instance homepage actions', function () {
29 let server: ServerInfo 24 let server: PeerTubeServer
25 let command: CustomPagesCommand
30 26
31 before(async function () { 27 before(async function () {
32 this.timeout(30000) 28 this.timeout(30000)
33 29
34 server = await flushAndRunServer(1) 30 server = await createSingleServer(1)
35 await setAccessTokensToServers([ server ]) 31 await setAccessTokensToServers([ server ])
32
33 command = server.customPage
36 }) 34 })
37 35
38 it('Should not have a homepage', async function () { 36 it('Should not have a homepage', async function () {
39 const state = await getHomepageState(server) 37 const state = await getHomepageState(server)
40 expect(state).to.be.false 38 expect(state).to.be.false
41 39
42 await getInstanceHomepage(server.url, HttpStatusCode.NOT_FOUND_404) 40 await command.getInstanceHomepage({ expectedStatus: HttpStatusCode.NOT_FOUND_404 })
43 }) 41 })
44 42
45 it('Should set a homepage', async function () { 43 it('Should set a homepage', async function () {
46 await updateInstanceHomepage(server.url, server.accessToken, '<picsou-magazine></picsou-magazine>') 44 await command.updateInstanceHomepage({ content: '<picsou-magazine></picsou-magazine>' })
47 45
48 const res = await getInstanceHomepage(server.url) 46 const page = await command.getInstanceHomepage()
49 const page: CustomPage = res.body
50 expect(page.content).to.equal('<picsou-magazine></picsou-magazine>') 47 expect(page.content).to.equal('<picsou-magazine></picsou-magazine>')
51 48
52 const state = await getHomepageState(server) 49 const state = await getHomepageState(server)
@@ -56,12 +53,11 @@ describe('Test instance homepage actions', function () {
56 it('Should have the same homepage after a restart', async function () { 53 it('Should have the same homepage after a restart', async function () {
57 this.timeout(30000) 54 this.timeout(30000)
58 55
59 killallServers([ server ]) 56 await killallServers([ server ])
60 57
61 await reRunServer(server) 58 await server.run()
62 59
63 const res = await getInstanceHomepage(server.url) 60 const page = await command.getInstanceHomepage()
64 const page: CustomPage = res.body
65 expect(page.content).to.equal('<picsou-magazine></picsou-magazine>') 61 expect(page.content).to.equal('<picsou-magazine></picsou-magazine>')
66 62
67 const state = await getHomepageState(server) 63 const state = await getHomepageState(server)
@@ -69,10 +65,9 @@ describe('Test instance homepage actions', function () {
69 }) 65 })
70 66
71 it('Should empty the homepage', async function () { 67 it('Should empty the homepage', async function () {
72 await updateInstanceHomepage(server.url, server.accessToken, '') 68 await command.updateInstanceHomepage({ content: '' })
73 69
74 const res = await getInstanceHomepage(server.url) 70 const page = await command.getInstanceHomepage()
75 const page: CustomPage = res.body
76 expect(page.content).to.be.empty 71 expect(page.content).to.be.empty
77 72
78 const state = await getHomepageState(server) 73 const state = await getHomepageState(server)
diff --git a/server/tests/api/server/jobs.ts b/server/tests/api/server/jobs.ts
index d0e222997..c10c154c2 100644
--- a/server/tests/api/server/jobs.ts
+++ b/server/tests/api/server/jobs.ts
@@ -1,24 +1,26 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2 2
3import * as chai from 'chai'
4import 'mocha' 3import 'mocha'
5import { cleanupTests, ServerInfo, setAccessTokensToServers } from '../../../../shared/extra-utils/index' 4import * as chai from 'chai'
6import { doubleFollow } from '../../../../shared/extra-utils/server/follows' 5import {
7import { getJobsList, getJobsListPaginationAndSort, waitJobs } from '../../../../shared/extra-utils/server/jobs' 6 cleanupTests,
8import { flushAndRunMultipleServers } from '../../../../shared/extra-utils/server/servers' 7 createMultipleServers,
9import { uploadVideo } from '../../../../shared/extra-utils/videos/videos' 8 dateIsValid,
10import { dateIsValid } from '../../../../shared/extra-utils/miscs/miscs' 9 doubleFollow,
11import { Job } from '../../../../shared/models/server' 10 PeerTubeServer,
11 setAccessTokensToServers,
12 waitJobs
13} from '@shared/extra-utils'
12 14
13const expect = chai.expect 15const expect = chai.expect
14 16
15describe('Test jobs', function () { 17describe('Test jobs', function () {
16 let servers: ServerInfo[] 18 let servers: PeerTubeServer[]
17 19
18 before(async function () { 20 before(async function () {
19 this.timeout(30000) 21 this.timeout(30000)
20 22
21 servers = await flushAndRunMultipleServers(2) 23 servers = await createMultipleServers(2)
22 24
23 await setAccessTokensToServers(servers) 25 await setAccessTokensToServers(servers)
24 26
@@ -27,36 +29,34 @@ describe('Test jobs', function () {
27 }) 29 })
28 30
29 it('Should create some jobs', async function () { 31 it('Should create some jobs', async function () {
30 this.timeout(60000) 32 this.timeout(120000)
31 33
32 await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'video1' }) 34 await servers[1].videos.upload({ attributes: { name: 'video1' } })
33 await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'video2' }) 35 await servers[1].videos.upload({ attributes: { name: 'video2' } })
34 36
35 await waitJobs(servers) 37 await waitJobs(servers)
36 }) 38 })
37 39
38 it('Should list jobs', async function () { 40 it('Should list jobs', async function () {
39 const res = await getJobsList(servers[1].url, servers[1].accessToken, 'completed') 41 const body = await servers[1].jobs.getJobsList({ state: 'completed' })
40 expect(res.body.total).to.be.above(2) 42 expect(body.total).to.be.above(2)
41 expect(res.body.data).to.have.length.above(2) 43 expect(body.data).to.have.length.above(2)
42 }) 44 })
43 45
44 it('Should list jobs with sort, pagination and job type', async function () { 46 it('Should list jobs with sort, pagination and job type', async function () {
45 { 47 {
46 const res = await getJobsListPaginationAndSort({ 48 const body = await servers[1].jobs.getJobsList({
47 url: servers[1].url,
48 accessToken: servers[1].accessToken,
49 state: 'completed', 49 state: 'completed',
50 start: 1, 50 start: 1,
51 count: 2, 51 count: 2,
52 sort: 'createdAt' 52 sort: 'createdAt'
53 }) 53 })
54 expect(res.body.total).to.be.above(2) 54 expect(body.total).to.be.above(2)
55 expect(res.body.data).to.have.lengthOf(2) 55 expect(body.data).to.have.lengthOf(2)
56 56
57 let job: Job = res.body.data[0] 57 let job = body.data[0]
58 // Skip repeat jobs 58 // Skip repeat jobs
59 if (job.type === 'videos-views') job = res.body.data[1] 59 if (job.type === 'videos-views') job = body.data[1]
60 60
61 expect(job.state).to.equal('completed') 61 expect(job.state).to.equal('completed')
62 expect(job.type.startsWith('activitypub-')).to.be.true 62 expect(job.type.startsWith('activitypub-')).to.be.true
@@ -66,29 +66,26 @@ describe('Test jobs', function () {
66 } 66 }
67 67
68 { 68 {
69 const res = await getJobsListPaginationAndSort({ 69 const body = await servers[1].jobs.getJobsList({
70 url: servers[1].url,
71 accessToken: servers[1].accessToken,
72 state: 'completed', 70 state: 'completed',
73 start: 0, 71 start: 0,
74 count: 100, 72 count: 100,
75 sort: 'createdAt', 73 sort: 'createdAt',
76 jobType: 'activitypub-http-broadcast' 74 jobType: 'activitypub-http-broadcast'
77 }) 75 })
78 expect(res.body.total).to.be.above(2) 76 expect(body.total).to.be.above(2)
79 77
80 for (const j of res.body.data as Job[]) { 78 for (const j of body.data) {
81 expect(j.type).to.equal('activitypub-http-broadcast') 79 expect(j.type).to.equal('activitypub-http-broadcast')
82 } 80 }
83 } 81 }
84 }) 82 })
85 83
86 it('Should list all jobs', async function () { 84 it('Should list all jobs', async function () {
87 const res = await getJobsList(servers[1].url, servers[1].accessToken) 85 const body = await servers[1].jobs.getJobsList()
88 86 expect(body.total).to.be.above(2)
89 const jobs = res.body.data as Job[]
90 87
91 expect(res.body.total).to.be.above(2) 88 const jobs = body.data
92 expect(jobs).to.have.length.above(2) 89 expect(jobs).to.have.length.above(2)
93 90
94 // We know there are a least 1 delayed job (video views) and 1 completed job (broadcast) 91 // We know there are a least 1 delayed job (video views) and 1 completed job (broadcast)
diff --git a/server/tests/api/server/logs.ts b/server/tests/api/server/logs.ts
index bc398ea73..bcd94dda3 100644
--- a/server/tests/api/server/logs.ts
+++ b/server/tests/api/server/logs.ts
@@ -4,27 +4,27 @@ import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5import { 5import {
6 cleanupTests, 6 cleanupTests,
7 flushAndRunServer, 7 createSingleServer,
8 killallServers, 8 killallServers,
9 makePingRequest, 9 LogsCommand,
10 reRunServer, 10 PeerTubeServer,
11 ServerInfo, 11 setAccessTokensToServers,
12 setAccessTokensToServers 12 waitJobs
13} from '../../../../shared/extra-utils/index' 13} from '@shared/extra-utils'
14import { getAuditLogs, getLogs } from '../../../../shared/extra-utils/logs/logs'
15import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
16import { uploadVideo } from '../../../../shared/extra-utils/videos/videos'
17 14
18const expect = chai.expect 15const expect = chai.expect
19 16
20describe('Test logs', function () { 17describe('Test logs', function () {
21 let server: ServerInfo 18 let server: PeerTubeServer
19 let logsCommand: LogsCommand
22 20
23 before(async function () { 21 before(async function () {
24 this.timeout(30000) 22 this.timeout(30000)
25 23
26 server = await flushAndRunServer(1) 24 server = await createSingleServer(1)
27 await setAccessTokensToServers([ server ]) 25 await setAccessTokensToServers([ server ])
26
27 logsCommand = server.logs
28 }) 28 })
29 29
30 describe('With the standard log file', function () { 30 describe('With the standard log file', function () {
@@ -32,16 +32,16 @@ describe('Test logs', function () {
32 it('Should get logs with a start date', async function () { 32 it('Should get logs with a start date', async function () {
33 this.timeout(20000) 33 this.timeout(20000)
34 34
35 await uploadVideo(server.url, server.accessToken, { name: 'video 1' }) 35 await server.videos.upload({ attributes: { name: 'video 1' } })
36 await waitJobs([ server ]) 36 await waitJobs([ server ])
37 37
38 const now = new Date() 38 const now = new Date()
39 39
40 await uploadVideo(server.url, server.accessToken, { name: 'video 2' }) 40 await server.videos.upload({ attributes: { name: 'video 2' } })
41 await waitJobs([ server ]) 41 await waitJobs([ server ])
42 42
43 const res = await getLogs(server.url, server.accessToken, now) 43 const body = await logsCommand.getLogs({ startDate: now })
44 const logsString = JSON.stringify(res.body) 44 const logsString = JSON.stringify(body)
45 45
46 expect(logsString.includes('video 1')).to.be.false 46 expect(logsString.includes('video 1')).to.be.false
47 expect(logsString.includes('video 2')).to.be.true 47 expect(logsString.includes('video 2')).to.be.true
@@ -50,21 +50,21 @@ describe('Test logs', function () {
50 it('Should get logs with an end date', async function () { 50 it('Should get logs with an end date', async function () {
51 this.timeout(30000) 51 this.timeout(30000)
52 52
53 await uploadVideo(server.url, server.accessToken, { name: 'video 3' }) 53 await server.videos.upload({ attributes: { name: 'video 3' } })
54 await waitJobs([ server ]) 54 await waitJobs([ server ])
55 55
56 const now1 = new Date() 56 const now1 = new Date()
57 57
58 await uploadVideo(server.url, server.accessToken, { name: 'video 4' }) 58 await server.videos.upload({ attributes: { name: 'video 4' } })
59 await waitJobs([ server ]) 59 await waitJobs([ server ])
60 60
61 const now2 = new Date() 61 const now2 = new Date()
62 62
63 await uploadVideo(server.url, server.accessToken, { name: 'video 5' }) 63 await server.videos.upload({ attributes: { name: 'video 5' } })
64 await waitJobs([ server ]) 64 await waitJobs([ server ])
65 65
66 const res = await getLogs(server.url, server.accessToken, now1, now2) 66 const body = await logsCommand.getLogs({ startDate: now1, endDate: now2 })
67 const logsString = JSON.stringify(res.body) 67 const logsString = JSON.stringify(body)
68 68
69 expect(logsString.includes('video 3')).to.be.false 69 expect(logsString.includes('video 3')).to.be.false
70 expect(logsString.includes('video 4')).to.be.true 70 expect(logsString.includes('video 4')).to.be.true
@@ -76,19 +76,19 @@ describe('Test logs', function () {
76 76
77 const now = new Date() 77 const now = new Date()
78 78
79 await uploadVideo(server.url, server.accessToken, { name: 'video 6' }) 79 await server.videos.upload({ attributes: { name: 'video 6' } })
80 await waitJobs([ server ]) 80 await waitJobs([ server ])
81 81
82 { 82 {
83 const res = await getLogs(server.url, server.accessToken, now, undefined, 'info') 83 const body = await logsCommand.getLogs({ startDate: now, level: 'info' })
84 const logsString = JSON.stringify(res.body) 84 const logsString = JSON.stringify(body)
85 85
86 expect(logsString.includes('video 6')).to.be.true 86 expect(logsString.includes('video 6')).to.be.true
87 } 87 }
88 88
89 { 89 {
90 const res = await getLogs(server.url, server.accessToken, now, undefined, 'warn') 90 const body = await logsCommand.getLogs({ startDate: now, level: 'warn' })
91 const logsString = JSON.stringify(res.body) 91 const logsString = JSON.stringify(body)
92 92
93 expect(logsString.includes('video 6')).to.be.false 93 expect(logsString.includes('video 6')).to.be.false
94 } 94 }
@@ -99,10 +99,10 @@ describe('Test logs', function () {
99 99
100 const now = new Date() 100 const now = new Date()
101 101
102 await makePingRequest(server) 102 await server.servers.ping()
103 103
104 const res = await getLogs(server.url, server.accessToken, now, undefined, 'info') 104 const body = await logsCommand.getLogs({ startDate: now, level: 'info' })
105 const logsString = JSON.stringify(res.body) 105 const logsString = JSON.stringify(body)
106 106
107 expect(logsString.includes('/api/v1/ping')).to.be.true 107 expect(logsString.includes('/api/v1/ping')).to.be.true
108 }) 108 })
@@ -110,16 +110,16 @@ describe('Test logs', function () {
110 it('Should not log ping requests', async function () { 110 it('Should not log ping requests', async function () {
111 this.timeout(30000) 111 this.timeout(30000)
112 112
113 killallServers([ server ]) 113 await killallServers([ server ])
114 114
115 await reRunServer(server, { log: { log_ping_requests: false } }) 115 await server.run({ log: { log_ping_requests: false } })
116 116
117 const now = new Date() 117 const now = new Date()
118 118
119 await makePingRequest(server) 119 await server.servers.ping()
120 120
121 const res = await getLogs(server.url, server.accessToken, now, undefined, 'info') 121 const body = await logsCommand.getLogs({ startDate: now, level: 'info' })
122 const logsString = JSON.stringify(res.body) 122 const logsString = JSON.stringify(body)
123 123
124 expect(logsString.includes('/api/v1/ping')).to.be.false 124 expect(logsString.includes('/api/v1/ping')).to.be.false
125 }) 125 })
@@ -129,23 +129,23 @@ describe('Test logs', function () {
129 it('Should get logs with a start date', async function () { 129 it('Should get logs with a start date', async function () {
130 this.timeout(20000) 130 this.timeout(20000)
131 131
132 await uploadVideo(server.url, server.accessToken, { name: 'video 7' }) 132 await server.videos.upload({ attributes: { name: 'video 7' } })
133 await waitJobs([ server ]) 133 await waitJobs([ server ])
134 134
135 const now = new Date() 135 const now = new Date()
136 136
137 await uploadVideo(server.url, server.accessToken, { name: 'video 8' }) 137 await server.videos.upload({ attributes: { name: 'video 8' } })
138 await waitJobs([ server ]) 138 await waitJobs([ server ])
139 139
140 const res = await getAuditLogs(server.url, server.accessToken, now) 140 const body = await logsCommand.getAuditLogs({ startDate: now })
141 const logsString = JSON.stringify(res.body) 141 const logsString = JSON.stringify(body)
142 142
143 expect(logsString.includes('video 7')).to.be.false 143 expect(logsString.includes('video 7')).to.be.false
144 expect(logsString.includes('video 8')).to.be.true 144 expect(logsString.includes('video 8')).to.be.true
145 145
146 expect(res.body).to.have.lengthOf(1) 146 expect(body).to.have.lengthOf(1)
147 147
148 const item = res.body[0] 148 const item = body[0]
149 149
150 const message = JSON.parse(item.message) 150 const message = JSON.parse(item.message)
151 expect(message.domain).to.equal('videos') 151 expect(message.domain).to.equal('videos')
@@ -155,21 +155,21 @@ describe('Test logs', function () {
155 it('Should get logs with an end date', async function () { 155 it('Should get logs with an end date', async function () {
156 this.timeout(30000) 156 this.timeout(30000)
157 157
158 await uploadVideo(server.url, server.accessToken, { name: 'video 9' }) 158 await server.videos.upload({ attributes: { name: 'video 9' } })
159 await waitJobs([ server ]) 159 await waitJobs([ server ])
160 160
161 const now1 = new Date() 161 const now1 = new Date()
162 162
163 await uploadVideo(server.url, server.accessToken, { name: 'video 10' }) 163 await server.videos.upload({ attributes: { name: 'video 10' } })
164 await waitJobs([ server ]) 164 await waitJobs([ server ])
165 165
166 const now2 = new Date() 166 const now2 = new Date()
167 167
168 await uploadVideo(server.url, server.accessToken, { name: 'video 11' }) 168 await server.videos.upload({ attributes: { name: 'video 11' } })
169 await waitJobs([ server ]) 169 await waitJobs([ server ])
170 170
171 const res = await getAuditLogs(server.url, server.accessToken, now1, now2) 171 const body = await logsCommand.getAuditLogs({ startDate: now1, endDate: now2 })
172 const logsString = JSON.stringify(res.body) 172 const logsString = JSON.stringify(body)
173 173
174 expect(logsString.includes('video 9')).to.be.false 174 expect(logsString.includes('video 9')).to.be.false
175 expect(logsString.includes('video 10')).to.be.true 175 expect(logsString.includes('video 10')).to.be.true
diff --git a/server/tests/api/server/no-client.ts b/server/tests/api/server/no-client.ts
index d589f51f3..efa890ad4 100644
--- a/server/tests/api/server/no-client.ts
+++ b/server/tests/api/server/no-client.ts
@@ -1,16 +1,15 @@
1import 'mocha' 1import 'mocha'
2import * as request from 'supertest' 2import * as request from 'supertest'
3import { ServerInfo } from '../../../../shared/extra-utils' 3import { cleanupTests, createSingleServer, PeerTubeServer } from '@shared/extra-utils'
4import { cleanupTests, flushAndRunServer } from '../../../../shared/extra-utils/server/servers' 4import { HttpStatusCode } from '@shared/models'
5import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
6 5
7describe('Start and stop server without web client routes', function () { 6describe('Start and stop server without web client routes', function () {
8 let server: ServerInfo 7 let server: PeerTubeServer
9 8
10 before(async function () { 9 before(async function () {
11 this.timeout(30000) 10 this.timeout(30000)
12 11
13 server = await flushAndRunServer(1, {}, [ '--no-client' ]) 12 server = await createSingleServer(1, {}, [ '--no-client' ])
14 }) 13 })
15 14
16 it('Should fail getting the client', function () { 15 it('Should fail getting the client', function () {
diff --git a/server/tests/api/server/plugins.ts b/server/tests/api/server/plugins.ts
index 6b61c7c33..5f9f4ffdd 100644
--- a/server/tests/api/server/plugins.ts
+++ b/server/tests/api/server/plugins.ts
@@ -2,41 +2,23 @@
2 2
3import 'mocha' 3import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5import { HttpStatusCode } from '@shared/core-utils'
6import { 5import {
7 cleanupTests, 6 cleanupTests,
8 closeAllSequelize, 7 createSingleServer,
9 flushAndRunServer,
10 getConfig,
11 getMyUserInformation,
12 getPlugin,
13 getPluginPackageJSON,
14 getPluginTestPath,
15 getPublicSettings,
16 installPlugin,
17 killallServers, 8 killallServers,
18 listAvailablePlugins, 9 PeerTubeServer,
19 listPlugins, 10 PluginsCommand,
20 reRunServer,
21 ServerInfo,
22 setAccessTokensToServers, 11 setAccessTokensToServers,
23 setPluginVersion,
24 testHelloWorldRegisteredSettings, 12 testHelloWorldRegisteredSettings,
25 uninstallPlugin, 13 wait
26 updateCustomSubConfig,
27 updateMyUser,
28 updatePlugin,
29 updatePluginPackageJSON,
30 updatePluginSettings,
31 wait,
32 waitUntilLog
33} from '@shared/extra-utils' 14} from '@shared/extra-utils'
34import { PeerTubePlugin, PeerTubePluginIndex, PluginPackageJson, PluginType, PublicServerSetting, ServerConfig, User } from '@shared/models' 15import { HttpStatusCode, PluginType } from '@shared/models'
35 16
36const expect = chai.expect 17const expect = chai.expect
37 18
38describe('Test plugins', function () { 19describe('Test plugins', function () {
39 let server: ServerInfo = null 20 let server: PeerTubeServer = null
21 let command: PluginsCommand
40 22
41 before(async function () { 23 before(async function () {
42 this.timeout(30000) 24 this.timeout(30000)
@@ -46,68 +28,61 @@ describe('Test plugins', function () {
46 index: { check_latest_versions_interval: '5 seconds' } 28 index: { check_latest_versions_interval: '5 seconds' }
47 } 29 }
48 } 30 }
49 server = await flushAndRunServer(1, configOverride) 31 server = await createSingleServer(1, configOverride)
50 await setAccessTokensToServers([ server ]) 32 await setAccessTokensToServers([ server ])
33
34 command = server.plugins
51 }) 35 })
52 36
53 it('Should list and search available plugins and themes', async function () { 37 it('Should list and search available plugins and themes', async function () {
54 this.timeout(30000) 38 this.timeout(30000)
55 39
56 { 40 {
57 const res = await listAvailablePlugins({ 41 const body = await command.listAvailable({
58 url: server.url,
59 accessToken: server.accessToken,
60 count: 1, 42 count: 1,
61 start: 0, 43 start: 0,
62 pluginType: PluginType.THEME, 44 pluginType: PluginType.THEME,
63 search: 'background-red' 45 search: 'background-red'
64 }) 46 })
65 47
66 expect(res.body.total).to.be.at.least(1) 48 expect(body.total).to.be.at.least(1)
67 expect(res.body.data).to.have.lengthOf(1) 49 expect(body.data).to.have.lengthOf(1)
68 } 50 }
69 51
70 { 52 {
71 const res1 = await listAvailablePlugins({ 53 const body1 = await command.listAvailable({
72 url: server.url,
73 accessToken: server.accessToken,
74 count: 2, 54 count: 2,
75 start: 0, 55 start: 0,
76 sort: 'npmName' 56 sort: 'npmName'
77 }) 57 })
78 const data1: PeerTubePluginIndex[] = res1.body.data 58 expect(body1.total).to.be.at.least(2)
79 59
80 expect(res1.body.total).to.be.at.least(2) 60 const data1 = body1.data
81 expect(data1).to.have.lengthOf(2) 61 expect(data1).to.have.lengthOf(2)
82 62
83 const res2 = await listAvailablePlugins({ 63 const body2 = await command.listAvailable({
84 url: server.url,
85 accessToken: server.accessToken,
86 count: 2, 64 count: 2,
87 start: 0, 65 start: 0,
88 sort: '-npmName' 66 sort: '-npmName'
89 }) 67 })
90 const data2: PeerTubePluginIndex[] = res2.body.data 68 expect(body2.total).to.be.at.least(2)
91 69
92 expect(res2.body.total).to.be.at.least(2) 70 const data2 = body2.data
93 expect(data2).to.have.lengthOf(2) 71 expect(data2).to.have.lengthOf(2)
94 72
95 expect(data1[0].npmName).to.not.equal(data2[0].npmName) 73 expect(data1[0].npmName).to.not.equal(data2[0].npmName)
96 } 74 }
97 75
98 { 76 {
99 const res = await listAvailablePlugins({ 77 const body = await command.listAvailable({
100 url: server.url,
101 accessToken: server.accessToken,
102 count: 10, 78 count: 10,
103 start: 0, 79 start: 0,
104 pluginType: PluginType.THEME, 80 pluginType: PluginType.THEME,
105 search: 'background-red', 81 search: 'background-red',
106 currentPeerTubeEngine: '1.0.0' 82 currentPeerTubeEngine: '1.0.0'
107 }) 83 })
108 const data: PeerTubePluginIndex[] = res.body.data
109 84
110 const p = data.find(p => p.npmName === 'peertube-theme-background-red') 85 const p = body.data.find(p => p.npmName === 'peertube-theme-background-red')
111 expect(p).to.be.undefined 86 expect(p).to.be.undefined
112 } 87 }
113 }) 88 })
@@ -115,22 +90,12 @@ describe('Test plugins', function () {
115 it('Should install a plugin and a theme', async function () { 90 it('Should install a plugin and a theme', async function () {
116 this.timeout(30000) 91 this.timeout(30000)
117 92
118 await installPlugin({ 93 await command.install({ npmName: 'peertube-plugin-hello-world' })
119 url: server.url, 94 await command.install({ npmName: 'peertube-theme-background-red' })
120 accessToken: server.accessToken,
121 npmName: 'peertube-plugin-hello-world'
122 })
123
124 await installPlugin({
125 url: server.url,
126 accessToken: server.accessToken,
127 npmName: 'peertube-theme-background-red'
128 })
129 }) 95 })
130 96
131 it('Should have the plugin loaded in the configuration', async function () { 97 it('Should have the plugin loaded in the configuration', async function () {
132 const res = await getConfig(server.url) 98 const config = await server.config.getConfig()
133 const config: ServerConfig = res.body
134 99
135 const theme = config.theme.registered.find(r => r.name === 'background-red') 100 const theme = config.theme.registered.find(r => r.name === 'background-red')
136 expect(theme).to.not.be.undefined 101 expect(theme).to.not.be.undefined
@@ -140,66 +105,56 @@ describe('Test plugins', function () {
140 }) 105 })
141 106
142 it('Should update the default theme in the configuration', async function () { 107 it('Should update the default theme in the configuration', async function () {
143 await updateCustomSubConfig(server.url, server.accessToken, { theme: { default: 'background-red' } }) 108 await server.config.updateCustomSubConfig({
144 109 newConfig: {
145 const res = await getConfig(server.url) 110 theme: { default: 'background-red' }
146 const config: ServerConfig = res.body 111 }
112 })
147 113
114 const config = await server.config.getConfig()
148 expect(config.theme.default).to.equal('background-red') 115 expect(config.theme.default).to.equal('background-red')
149 }) 116 })
150 117
151 it('Should update my default theme', async function () { 118 it('Should update my default theme', async function () {
152 await updateMyUser({ 119 await server.users.updateMe({ theme: 'background-red' })
153 url: server.url,
154 accessToken: server.accessToken,
155 theme: 'background-red'
156 })
157 120
158 const res = await getMyUserInformation(server.url, server.accessToken) 121 const user = await server.users.getMyInfo()
159 expect((res.body as User).theme).to.equal('background-red') 122 expect(user.theme).to.equal('background-red')
160 }) 123 })
161 124
162 it('Should list plugins and themes', async function () { 125 it('Should list plugins and themes', async function () {
163 { 126 {
164 const res = await listPlugins({ 127 const body = await command.list({
165 url: server.url,
166 accessToken: server.accessToken,
167 count: 1, 128 count: 1,
168 start: 0, 129 start: 0,
169 pluginType: PluginType.THEME 130 pluginType: PluginType.THEME
170 }) 131 })
171 const data: PeerTubePlugin[] = res.body.data 132 expect(body.total).to.be.at.least(1)
172 133
173 expect(res.body.total).to.be.at.least(1) 134 const data = body.data
174 expect(data).to.have.lengthOf(1) 135 expect(data).to.have.lengthOf(1)
175 expect(data[0].name).to.equal('background-red') 136 expect(data[0].name).to.equal('background-red')
176 } 137 }
177 138
178 { 139 {
179 const res = await listPlugins({ 140 const { data } = await command.list({
180 url: server.url,
181 accessToken: server.accessToken,
182 count: 2, 141 count: 2,
183 start: 0, 142 start: 0,
184 sort: 'name' 143 sort: 'name'
185 }) 144 })
186 const data: PeerTubePlugin[] = res.body.data
187 145
188 expect(data[0].name).to.equal('background-red') 146 expect(data[0].name).to.equal('background-red')
189 expect(data[1].name).to.equal('hello-world') 147 expect(data[1].name).to.equal('hello-world')
190 } 148 }
191 149
192 { 150 {
193 const res = await listPlugins({ 151 const body = await command.list({
194 url: server.url,
195 accessToken: server.accessToken,
196 count: 2, 152 count: 2,
197 start: 1, 153 start: 1,
198 sort: 'name' 154 sort: 'name'
199 }) 155 })
200 const data: PeerTubePlugin[] = res.body.data
201 156
202 expect(data[0].name).to.equal('hello-world') 157 expect(body.data[0].name).to.equal('hello-world')
203 } 158 }
204 }) 159 })
205 160
@@ -208,9 +163,8 @@ describe('Test plugins', function () {
208 }) 163 })
209 164
210 it('Should get public settings', async function () { 165 it('Should get public settings', async function () {
211 const res = await getPublicSettings({ url: server.url, npmName: 'peertube-plugin-hello-world' }) 166 const body = await command.getPublicSettings({ npmName: 'peertube-plugin-hello-world' })
212 167 const publicSettings = body.publicSettings
213 const publicSettings = (res.body as PublicServerSetting).publicSettings
214 168
215 expect(Object.keys(publicSettings)).to.have.lengthOf(1) 169 expect(Object.keys(publicSettings)).to.have.lengthOf(1)
216 expect(Object.keys(publicSettings)).to.deep.equal([ 'user-name' ]) 170 expect(Object.keys(publicSettings)).to.deep.equal([ 'user-name' ])
@@ -222,9 +176,7 @@ describe('Test plugins', function () {
222 'admin-name': 'Cid' 176 'admin-name': 'Cid'
223 } 177 }
224 178
225 await updatePluginSettings({ 179 await command.updateSettings({
226 url: server.url,
227 accessToken: server.accessToken,
228 npmName: 'peertube-plugin-hello-world', 180 npmName: 'peertube-plugin-hello-world',
229 settings 181 settings
230 }) 182 })
@@ -233,18 +185,12 @@ describe('Test plugins', function () {
233 it('Should have watched settings changes', async function () { 185 it('Should have watched settings changes', async function () {
234 this.timeout(10000) 186 this.timeout(10000)
235 187
236 await waitUntilLog(server, 'Settings changed!') 188 await server.servers.waitUntilLog('Settings changed!')
237 }) 189 })
238 190
239 it('Should get a plugin and a theme', async function () { 191 it('Should get a plugin and a theme', async function () {
240 { 192 {
241 const res = await getPlugin({ 193 const plugin = await command.get({ npmName: 'peertube-plugin-hello-world' })
242 url: server.url,
243 accessToken: server.accessToken,
244 npmName: 'peertube-plugin-hello-world'
245 })
246
247 const plugin: PeerTubePlugin = res.body
248 194
249 expect(plugin.type).to.equal(PluginType.PLUGIN) 195 expect(plugin.type).to.equal(PluginType.PLUGIN)
250 expect(plugin.name).to.equal('hello-world') 196 expect(plugin.name).to.equal('hello-world')
@@ -262,13 +208,7 @@ describe('Test plugins', function () {
262 } 208 }
263 209
264 { 210 {
265 const res = await getPlugin({ 211 const plugin = await command.get({ npmName: 'peertube-theme-background-red' })
266 url: server.url,
267 accessToken: server.accessToken,
268 npmName: 'peertube-theme-background-red'
269 })
270
271 const plugin: PeerTubePlugin = res.body
272 212
273 expect(plugin.type).to.equal(PluginType.THEME) 213 expect(plugin.type).to.equal(PluginType.THEME)
274 expect(plugin.name).to.equal('background-red') 214 expect(plugin.name).to.equal('background-red')
@@ -292,101 +232,66 @@ describe('Test plugins', function () {
292 await wait(6000) 232 await wait(6000)
293 233
294 // Fake update our plugin version 234 // Fake update our plugin version
295 await setPluginVersion(server.internalServerNumber, 'hello-world', '0.0.1') 235 await server.sql.setPluginVersion('hello-world', '0.0.1')
296 236
297 // Fake update package.json 237 // Fake update package.json
298 const packageJSON: PluginPackageJson = await getPluginPackageJSON(server, 'peertube-plugin-hello-world') 238 const packageJSON = await command.getPackageJSON('peertube-plugin-hello-world')
299 const oldVersion = packageJSON.version 239 const oldVersion = packageJSON.version
300 240
301 packageJSON.version = '0.0.1' 241 packageJSON.version = '0.0.1'
302 await updatePluginPackageJSON(server, 'peertube-plugin-hello-world', packageJSON) 242 await command.updatePackageJSON('peertube-plugin-hello-world', packageJSON)
303 243
304 // Restart the server to take into account this change 244 // Restart the server to take into account this change
305 killallServers([ server ]) 245 await killallServers([ server ])
306 await reRunServer(server) 246 await server.run()
307 247
308 { 248 {
309 const res = await listPlugins({ 249 const body = await command.list({ pluginType: PluginType.PLUGIN })
310 url: server.url,
311 accessToken: server.accessToken,
312 pluginType: PluginType.PLUGIN
313 })
314
315 const plugin: PeerTubePlugin = res.body.data[0]
316 250
251 const plugin = body.data[0]
317 expect(plugin.version).to.equal('0.0.1') 252 expect(plugin.version).to.equal('0.0.1')
318 expect(plugin.latestVersion).to.exist 253 expect(plugin.latestVersion).to.exist
319 expect(plugin.latestVersion).to.not.equal('0.0.1') 254 expect(plugin.latestVersion).to.not.equal('0.0.1')
320 } 255 }
321 256
322 { 257 {
323 await updatePlugin({ 258 await command.update({ npmName: 'peertube-plugin-hello-world' })
324 url: server.url,
325 accessToken: server.accessToken,
326 npmName: 'peertube-plugin-hello-world'
327 })
328
329 const res = await listPlugins({
330 url: server.url,
331 accessToken: server.accessToken,
332 pluginType: PluginType.PLUGIN
333 })
334 259
335 const plugin: PeerTubePlugin = res.body.data[0] 260 const body = await command.list({ pluginType: PluginType.PLUGIN })
336 261
262 const plugin = body.data[0]
337 expect(plugin.version).to.equal(oldVersion) 263 expect(plugin.version).to.equal(oldVersion)
338 264
339 const updatedPackageJSON: PluginPackageJson = await getPluginPackageJSON(server, 'peertube-plugin-hello-world') 265 const updatedPackageJSON = await command.getPackageJSON('peertube-plugin-hello-world')
340 expect(updatedPackageJSON.version).to.equal(oldVersion) 266 expect(updatedPackageJSON.version).to.equal(oldVersion)
341 } 267 }
342 }) 268 })
343 269
344 it('Should uninstall the plugin', async function () { 270 it('Should uninstall the plugin', async function () {
345 await uninstallPlugin({ 271 await command.uninstall({ npmName: 'peertube-plugin-hello-world' })
346 url: server.url,
347 accessToken: server.accessToken,
348 npmName: 'peertube-plugin-hello-world'
349 })
350
351 const res = await listPlugins({
352 url: server.url,
353 accessToken: server.accessToken,
354 pluginType: PluginType.PLUGIN
355 })
356 272
357 expect(res.body.total).to.equal(0) 273 const body = await command.list({ pluginType: PluginType.PLUGIN })
358 expect(res.body.data).to.have.lengthOf(0) 274 expect(body.total).to.equal(0)
275 expect(body.data).to.have.lengthOf(0)
359 }) 276 })
360 277
361 it('Should list uninstalled plugins', async function () { 278 it('Should list uninstalled plugins', async function () {
362 const res = await listPlugins({ 279 const body = await command.list({ pluginType: PluginType.PLUGIN, uninstalled: true })
363 url: server.url, 280 expect(body.total).to.equal(1)
364 accessToken: server.accessToken, 281 expect(body.data).to.have.lengthOf(1)
365 pluginType: PluginType.PLUGIN,
366 uninstalled: true
367 })
368
369 expect(res.body.total).to.equal(1)
370 expect(res.body.data).to.have.lengthOf(1)
371 282
372 const plugin: PeerTubePlugin = res.body.data[0] 283 const plugin = body.data[0]
373 expect(plugin.name).to.equal('hello-world') 284 expect(plugin.name).to.equal('hello-world')
374 expect(plugin.enabled).to.be.false 285 expect(plugin.enabled).to.be.false
375 expect(plugin.uninstalled).to.be.true 286 expect(plugin.uninstalled).to.be.true
376 }) 287 })
377 288
378 it('Should uninstall the theme', async function () { 289 it('Should uninstall the theme', async function () {
379 await uninstallPlugin({ 290 await command.uninstall({ npmName: 'peertube-theme-background-red' })
380 url: server.url,
381 accessToken: server.accessToken,
382 npmName: 'peertube-theme-background-red'
383 })
384 }) 291 })
385 292
386 it('Should have updated the configuration', async function () { 293 it('Should have updated the configuration', async function () {
387 // get /config (default theme + registered themes + registered plugins) 294 const config = await server.config.getConfig()
388 const res = await getConfig(server.url)
389 const config: ServerConfig = res.body
390 295
391 expect(config.theme.default).to.equal('default') 296 expect(config.theme.default).to.equal('default')
392 297
@@ -398,42 +303,33 @@ describe('Test plugins', function () {
398 }) 303 })
399 304
400 it('Should have updated the user theme', async function () { 305 it('Should have updated the user theme', async function () {
401 const res = await getMyUserInformation(server.url, server.accessToken) 306 const user = await server.users.getMyInfo()
402 expect((res.body as User).theme).to.equal('instance-default') 307 expect(user.theme).to.equal('instance-default')
403 }) 308 })
404 309
405 it('Should not install a broken plugin', async function () { 310 it('Should not install a broken plugin', async function () {
406 this.timeout(60000) 311 this.timeout(60000)
407 312
408 async function check () { 313 async function check () {
409 const res = await listPlugins({ 314 const body = await command.list({ pluginType: PluginType.PLUGIN })
410 url: server.url, 315 const plugins = body.data
411 accessToken: server.accessToken,
412 pluginType: PluginType.PLUGIN
413 })
414
415 const plugins: PeerTubePlugin[] = res.body.data
416
417 expect(plugins.find(p => p.name === 'test-broken')).to.not.exist 316 expect(plugins.find(p => p.name === 'test-broken')).to.not.exist
418 } 317 }
419 318
420 await installPlugin({ 319 await command.install({
421 url: server.url, 320 path: PluginsCommand.getPluginTestPath('-broken'),
422 accessToken: server.accessToken,
423 path: getPluginTestPath('-broken'),
424 expectedStatus: HttpStatusCode.BAD_REQUEST_400 321 expectedStatus: HttpStatusCode.BAD_REQUEST_400
425 }) 322 })
426 323
427 await check() 324 await check()
428 325
429 killallServers([ server ]) 326 await killallServers([ server ])
430 await reRunServer(server) 327 await server.run()
431 328
432 await check() 329 await check()
433 }) 330 })
434 331
435 after(async function () { 332 after(async function () {
436 await closeAllSequelize([ server ])
437 await cleanupTests([ server ]) 333 await cleanupTests([ server ])
438 }) 334 })
439}) 335})
diff --git a/server/tests/api/server/reverse-proxy.ts b/server/tests/api/server/reverse-proxy.ts
index 17d1ee4a5..484f88d67 100644
--- a/server/tests/api/server/reverse-proxy.ts
+++ b/server/tests/api/server/reverse-proxy.ts
@@ -1,16 +1,12 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2 2
3import 'mocha' 3import { expect } from 'chai'
4import * as chai from 'chai' 4import { cleanupTests, createSingleServer, PeerTubeServer, setAccessTokensToServers, wait } from '@shared/extra-utils'
5import { cleanupTests, getVideo, registerUser, uploadVideo, userLogin, viewVideo, wait } from '../../../../shared/extra-utils' 5import { HttpStatusCode } from '@shared/models'
6import { flushAndRunServer, setAccessTokensToServers } from '../../../../shared/extra-utils/index'
7import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
8
9const expect = chai.expect
10 6
11describe('Test application behind a reverse proxy', function () { 7describe('Test application behind a reverse proxy', function () {
12 let server = null 8 let server: PeerTubeServer
13 let videoId 9 let videoId: string
14 10
15 before(async function () { 11 before(async function () {
16 this.timeout(30000) 12 this.timeout(30000)
@@ -34,85 +30,85 @@ describe('Test application behind a reverse proxy', function () {
34 } 30 }
35 } 31 }
36 32
37 server = await flushAndRunServer(1, config) 33 server = await createSingleServer(1, config)
38 await setAccessTokensToServers([ server ]) 34 await setAccessTokensToServers([ server ])
39 35
40 const { body } = await uploadVideo(server.url, server.accessToken, {}) 36 const { uuid } = await server.videos.upload()
41 videoId = body.video.uuid 37 videoId = uuid
42 }) 38 })
43 39
44 it('Should view a video only once with the same IP by default', async function () { 40 it('Should view a video only once with the same IP by default', async function () {
45 this.timeout(20000) 41 this.timeout(20000)
46 42
47 await viewVideo(server.url, videoId) 43 await server.videos.view({ id: videoId })
48 await viewVideo(server.url, videoId) 44 await server.videos.view({ id: videoId })
49 45
50 // Wait the repeatable job 46 // Wait the repeatable job
51 await wait(8000) 47 await wait(8000)
52 48
53 const { body } = await getVideo(server.url, videoId) 49 const video = await server.videos.get({ id: videoId })
54 expect(body.views).to.equal(1) 50 expect(video.views).to.equal(1)
55 }) 51 })
56 52
57 it('Should view a video 2 times with the X-Forwarded-For header set', async function () { 53 it('Should view a video 2 times with the X-Forwarded-For header set', async function () {
58 this.timeout(20000) 54 this.timeout(20000)
59 55
60 await viewVideo(server.url, videoId, HttpStatusCode.NO_CONTENT_204, '0.0.0.1,127.0.0.1') 56 await server.videos.view({ id: videoId, xForwardedFor: '0.0.0.1,127.0.0.1' })
61 await viewVideo(server.url, videoId, HttpStatusCode.NO_CONTENT_204, '0.0.0.2,127.0.0.1') 57 await server.videos.view({ id: videoId, xForwardedFor: '0.0.0.2,127.0.0.1' })
62 58
63 // Wait the repeatable job 59 // Wait the repeatable job
64 await wait(8000) 60 await wait(8000)
65 61
66 const { body } = await getVideo(server.url, videoId) 62 const video = await server.videos.get({ id: videoId })
67 expect(body.views).to.equal(3) 63 expect(video.views).to.equal(3)
68 }) 64 })
69 65
70 it('Should view a video only once with the same client IP in the X-Forwarded-For header', async function () { 66 it('Should view a video only once with the same client IP in the X-Forwarded-For header', async function () {
71 this.timeout(20000) 67 this.timeout(20000)
72 68
73 await viewVideo(server.url, videoId, HttpStatusCode.NO_CONTENT_204, '0.0.0.4,0.0.0.3,::ffff:127.0.0.1') 69 await server.videos.view({ id: videoId, xForwardedFor: '0.0.0.4,0.0.0.3,::ffff:127.0.0.1' })
74 await viewVideo(server.url, videoId, HttpStatusCode.NO_CONTENT_204, '0.0.0.5,0.0.0.3,127.0.0.1') 70 await server.videos.view({ id: videoId, xForwardedFor: '0.0.0.5,0.0.0.3,127.0.0.1' })
75 71
76 // Wait the repeatable job 72 // Wait the repeatable job
77 await wait(8000) 73 await wait(8000)
78 74
79 const { body } = await getVideo(server.url, videoId) 75 const video = await server.videos.get({ id: videoId })
80 expect(body.views).to.equal(4) 76 expect(video.views).to.equal(4)
81 }) 77 })
82 78
83 it('Should view a video two times with a different client IP in the X-Forwarded-For header', async function () { 79 it('Should view a video two times with a different client IP in the X-Forwarded-For header', async function () {
84 this.timeout(20000) 80 this.timeout(20000)
85 81
86 await viewVideo(server.url, videoId, HttpStatusCode.NO_CONTENT_204, '0.0.0.8,0.0.0.6,127.0.0.1') 82 await server.videos.view({ id: videoId, xForwardedFor: '0.0.0.8,0.0.0.6,127.0.0.1' })
87 await viewVideo(server.url, videoId, HttpStatusCode.NO_CONTENT_204, '0.0.0.8,0.0.0.7,127.0.0.1') 83 await server.videos.view({ id: videoId, xForwardedFor: '0.0.0.8,0.0.0.7,127.0.0.1' })
88 84
89 // Wait the repeatable job 85 // Wait the repeatable job
90 await wait(8000) 86 await wait(8000)
91 87
92 const { body } = await getVideo(server.url, videoId) 88 const video = await server.videos.get({ id: videoId })
93 expect(body.views).to.equal(6) 89 expect(video.views).to.equal(6)
94 }) 90 })
95 91
96 it('Should rate limit logins', async function () { 92 it('Should rate limit logins', async function () {
97 const user = { username: 'root', password: 'fail' } 93 const user = { username: 'root', password: 'fail' }
98 94
99 for (let i = 0; i < 19; i++) { 95 for (let i = 0; i < 19; i++) {
100 await userLogin(server, user, HttpStatusCode.BAD_REQUEST_400) 96 await server.login.login({ user, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
101 } 97 }
102 98
103 await userLogin(server, user, HttpStatusCode.TOO_MANY_REQUESTS_429) 99 await server.login.login({ user, expectedStatus: HttpStatusCode.TOO_MANY_REQUESTS_429 })
104 }) 100 })
105 101
106 it('Should rate limit signup', async function () { 102 it('Should rate limit signup', async function () {
107 for (let i = 0; i < 10; i++) { 103 for (let i = 0; i < 10; i++) {
108 try { 104 try {
109 await registerUser(server.url, 'test' + i, 'password') 105 await server.users.register({ username: 'test' + i })
110 } catch { 106 } catch {
111 // empty 107 // empty
112 } 108 }
113 } 109 }
114 110
115 await registerUser(server.url, 'test42', 'password', HttpStatusCode.TOO_MANY_REQUESTS_429) 111 await server.users.register({ username: 'test42', expectedStatus: HttpStatusCode.TOO_MANY_REQUESTS_429 })
116 }) 112 })
117 113
118 it('Should not rate limit failed signup', async function () { 114 it('Should not rate limit failed signup', async function () {
@@ -121,10 +117,10 @@ describe('Test application behind a reverse proxy', function () {
121 await wait(7000) 117 await wait(7000)
122 118
123 for (let i = 0; i < 3; i++) { 119 for (let i = 0; i < 3; i++) {
124 await registerUser(server.url, 'test' + i, 'password', HttpStatusCode.CONFLICT_409) 120 await server.users.register({ username: 'test' + i, expectedStatus: HttpStatusCode.CONFLICT_409 })
125 } 121 }
126 122
127 await registerUser(server.url, 'test43', 'password', HttpStatusCode.NO_CONTENT_204) 123 await server.users.register({ username: 'test43', expectedStatus: HttpStatusCode.NO_CONTENT_204 })
128 124
129 }) 125 })
130 126
@@ -135,13 +131,13 @@ describe('Test application behind a reverse proxy', function () {
135 131
136 for (let i = 0; i < 100; i++) { 132 for (let i = 0; i < 100; i++) {
137 try { 133 try {
138 await getVideo(server.url, videoId) 134 await server.videos.get({ id: videoId })
139 } catch { 135 } catch {
140 // don't care if it fails 136 // don't care if it fails
141 } 137 }
142 } 138 }
143 139
144 await getVideo(server.url, videoId, HttpStatusCode.TOO_MANY_REQUESTS_429) 140 await server.videos.get({ id: videoId, expectedStatus: HttpStatusCode.TOO_MANY_REQUESTS_429 })
145 }) 141 })
146 142
147 after(async function () { 143 after(async function () {
diff --git a/server/tests/api/server/services.ts b/server/tests/api/server/services.ts
index ea64e4040..69d030dbb 100644
--- a/server/tests/api/server/services.ts
+++ b/server/tests/api/server/services.ts
@@ -2,23 +2,13 @@
2 2
3import 'mocha' 3import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5import { cleanupTests, createSingleServer, PeerTubeServer, setAccessTokensToServers, setDefaultVideoChannel } from '@shared/extra-utils'
5import { Video, VideoPlaylistPrivacy } from '@shared/models' 6import { Video, VideoPlaylistPrivacy } from '@shared/models'
6import {
7 addVideoInPlaylist,
8 createVideoPlaylist,
9 getOEmbed,
10 getVideosList,
11 ServerInfo,
12 setAccessTokensToServers,
13 setDefaultVideoChannel,
14 uploadVideo
15} from '../../../../shared/extra-utils'
16import { cleanupTests, flushAndRunServer } from '../../../../shared/extra-utils/server/servers'
17 7
18const expect = chai.expect 8const expect = chai.expect
19 9
20describe('Test services', function () { 10describe('Test services', function () {
21 let server: ServerInfo = null 11 let server: PeerTubeServer = null
22 let playlistUUID: string 12 let playlistUUID: string
23 let playlistDisplayName: string 13 let playlistDisplayName: string
24 let video: Video 14 let video: Video
@@ -26,40 +16,34 @@ describe('Test services', function () {
26 before(async function () { 16 before(async function () {
27 this.timeout(30000) 17 this.timeout(30000)
28 18
29 server = await flushAndRunServer(1) 19 server = await createSingleServer(1)
30 20
31 await setAccessTokensToServers([ server ]) 21 await setAccessTokensToServers([ server ])
32 await setDefaultVideoChannel([ server ]) 22 await setDefaultVideoChannel([ server ])
33 23
34 { 24 {
35 const videoAttributes = { 25 const attributes = { name: 'my super name' }
36 name: 'my super name' 26 await server.videos.upload({ attributes })
37 }
38 await uploadVideo(server.url, server.accessToken, videoAttributes)
39 27
40 const res = await getVideosList(server.url) 28 const { data } = await server.videos.list()
41 video = res.body.data[0] 29 video = data[0]
42 } 30 }
43 31
44 { 32 {
45 const res = await createVideoPlaylist({ 33 const created = await server.playlists.create({
46 url: server.url, 34 attributes: {
47 token: server.accessToken,
48 playlistAttrs: {
49 displayName: 'The Life and Times of Scrooge McDuck', 35 displayName: 'The Life and Times of Scrooge McDuck',
50 privacy: VideoPlaylistPrivacy.PUBLIC, 36 privacy: VideoPlaylistPrivacy.PUBLIC,
51 videoChannelId: server.videoChannel.id 37 videoChannelId: server.store.channel.id
52 } 38 }
53 }) 39 })
54 40
55 playlistUUID = res.body.videoPlaylist.uuid 41 playlistUUID = created.uuid
56 playlistDisplayName = 'The Life and Times of Scrooge McDuck' 42 playlistDisplayName = 'The Life and Times of Scrooge McDuck'
57 43
58 await addVideoInPlaylist({ 44 await server.playlists.addElement({
59 url: server.url, 45 playlistId: created.id,
60 token: server.accessToken, 46 attributes: {
61 playlistId: res.body.videoPlaylist.id,
62 elementAttrs: {
63 videoId: video.id 47 videoId: video.id
64 } 48 }
65 }) 49 })
@@ -70,7 +54,7 @@ describe('Test services', function () {
70 for (const basePath of [ '/videos/watch/', '/w/' ]) { 54 for (const basePath of [ '/videos/watch/', '/w/' ]) {
71 const oembedUrl = 'http://localhost:' + server.port + basePath + video.uuid 55 const oembedUrl = 'http://localhost:' + server.port + basePath + video.uuid
72 56
73 const res = await getOEmbed(server.url, oembedUrl) 57 const res = await server.services.getOEmbed({ oembedUrl })
74 const expectedHtml = '<iframe width="560" height="315" sandbox="allow-same-origin allow-scripts" ' + 58 const expectedHtml = '<iframe width="560" height="315" sandbox="allow-same-origin allow-scripts" ' +
75 `title="${video.name}" src="http://localhost:${server.port}/videos/embed/${video.uuid}" ` + 59 `title="${video.name}" src="http://localhost:${server.port}/videos/embed/${video.uuid}" ` +
76 'frameborder="0" allowfullscreen></iframe>' 60 'frameborder="0" allowfullscreen></iframe>'
@@ -78,7 +62,7 @@ describe('Test services', function () {
78 62
79 expect(res.body.html).to.equal(expectedHtml) 63 expect(res.body.html).to.equal(expectedHtml)
80 expect(res.body.title).to.equal(video.name) 64 expect(res.body.title).to.equal(video.name)
81 expect(res.body.author_name).to.equal(server.videoChannel.displayName) 65 expect(res.body.author_name).to.equal(server.store.channel.displayName)
82 expect(res.body.width).to.equal(560) 66 expect(res.body.width).to.equal(560)
83 expect(res.body.height).to.equal(315) 67 expect(res.body.height).to.equal(315)
84 expect(res.body.thumbnail_url).to.equal(expectedThumbnailUrl) 68 expect(res.body.thumbnail_url).to.equal(expectedThumbnailUrl)
@@ -91,14 +75,14 @@ describe('Test services', function () {
91 for (const basePath of [ '/videos/watch/playlist/', '/w/p/' ]) { 75 for (const basePath of [ '/videos/watch/playlist/', '/w/p/' ]) {
92 const oembedUrl = 'http://localhost:' + server.port + basePath + playlistUUID 76 const oembedUrl = 'http://localhost:' + server.port + basePath + playlistUUID
93 77
94 const res = await getOEmbed(server.url, oembedUrl) 78 const res = await server.services.getOEmbed({ oembedUrl })
95 const expectedHtml = '<iframe width="560" height="315" sandbox="allow-same-origin allow-scripts" ' + 79 const expectedHtml = '<iframe width="560" height="315" sandbox="allow-same-origin allow-scripts" ' +
96 `title="${playlistDisplayName}" src="http://localhost:${server.port}/video-playlists/embed/${playlistUUID}" ` + 80 `title="${playlistDisplayName}" src="http://localhost:${server.port}/video-playlists/embed/${playlistUUID}" ` +
97 'frameborder="0" allowfullscreen></iframe>' 81 'frameborder="0" allowfullscreen></iframe>'
98 82
99 expect(res.body.html).to.equal(expectedHtml) 83 expect(res.body.html).to.equal(expectedHtml)
100 expect(res.body.title).to.equal('The Life and Times of Scrooge McDuck') 84 expect(res.body.title).to.equal('The Life and Times of Scrooge McDuck')
101 expect(res.body.author_name).to.equal(server.videoChannel.displayName) 85 expect(res.body.author_name).to.equal(server.store.channel.displayName)
102 expect(res.body.width).to.equal(560) 86 expect(res.body.width).to.equal(560)
103 expect(res.body.height).to.equal(315) 87 expect(res.body.height).to.equal(315)
104 expect(res.body.thumbnail_url).exist 88 expect(res.body.thumbnail_url).exist
@@ -114,14 +98,14 @@ describe('Test services', function () {
114 const maxHeight = 50 98 const maxHeight = 50
115 const maxWidth = 50 99 const maxWidth = 50
116 100
117 const res = await getOEmbed(server.url, oembedUrl, format, maxHeight, maxWidth) 101 const res = await server.services.getOEmbed({ oembedUrl, format, maxHeight, maxWidth })
118 const expectedHtml = '<iframe width="50" height="50" sandbox="allow-same-origin allow-scripts" ' + 102 const expectedHtml = '<iframe width="50" height="50" sandbox="allow-same-origin allow-scripts" ' +
119 `title="${video.name}" src="http://localhost:${server.port}/videos/embed/${video.uuid}" ` + 103 `title="${video.name}" src="http://localhost:${server.port}/videos/embed/${video.uuid}" ` +
120 'frameborder="0" allowfullscreen></iframe>' 104 'frameborder="0" allowfullscreen></iframe>'
121 105
122 expect(res.body.html).to.equal(expectedHtml) 106 expect(res.body.html).to.equal(expectedHtml)
123 expect(res.body.title).to.equal(video.name) 107 expect(res.body.title).to.equal(video.name)
124 expect(res.body.author_name).to.equal(server.videoChannel.displayName) 108 expect(res.body.author_name).to.equal(server.store.channel.displayName)
125 expect(res.body.height).to.equal(50) 109 expect(res.body.height).to.equal(50)
126 expect(res.body.width).to.equal(50) 110 expect(res.body.width).to.equal(50)
127 expect(res.body).to.not.have.property('thumbnail_url') 111 expect(res.body).to.not.have.property('thumbnail_url')
diff --git a/server/tests/api/server/stats.ts b/server/tests/api/server/stats.ts
index 304181a6d..5ec771429 100644
--- a/server/tests/api/server/stats.ts
+++ b/server/tests/api/server/stats.ts
@@ -3,33 +3,20 @@
3import 'mocha' 3import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5import { 5import {
6 addVideoChannel,
7 cleanupTests, 6 cleanupTests,
8 createUser, 7 createMultipleServers,
9 createVideoPlaylist,
10 doubleFollow, 8 doubleFollow,
11 flushAndRunMultipleServers, 9 PeerTubeServer,
12 follow, 10 setAccessTokensToServers,
13 ServerInfo, 11 wait,
14 unfollow, 12 waitJobs
15 updateCustomSubConfig, 13} from '@shared/extra-utils'
16 uploadVideo, 14import { ActivityType, VideoPlaylistPrivacy } from '@shared/models'
17 userLogin,
18 viewVideo,
19 wait
20} from '../../../../shared/extra-utils'
21import { setAccessTokensToServers } from '../../../../shared/extra-utils/index'
22import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
23import { getStats } from '../../../../shared/extra-utils/server/stats'
24import { addVideoCommentThread } from '../../../../shared/extra-utils/videos/video-comments'
25import { ServerStats } from '../../../../shared/models/server/server-stats.model'
26import { VideoPlaylistPrivacy } from '../../../../shared/models/videos/playlist/video-playlist-privacy.model'
27import { ActivityType } from '@shared/models'
28 15
29const expect = chai.expect 16const expect = chai.expect
30 17
31describe('Test stats (excluding redundancy)', function () { 18describe('Test stats (excluding redundancy)', function () {
32 let servers: ServerInfo[] = [] 19 let servers: PeerTubeServer[] = []
33 let channelId 20 let channelId
34 const user = { 21 const user = {
35 username: 'user1', 22 username: 'user1',
@@ -39,31 +26,29 @@ describe('Test stats (excluding redundancy)', function () {
39 before(async function () { 26 before(async function () {
40 this.timeout(60000) 27 this.timeout(60000)
41 28
42 servers = await flushAndRunMultipleServers(3) 29 servers = await createMultipleServers(3)
43 30
44 await setAccessTokensToServers(servers) 31 await setAccessTokensToServers(servers)
45 32
46 await doubleFollow(servers[0], servers[1]) 33 await doubleFollow(servers[0], servers[1])
47 34
48 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: user.username, password: user.password }) 35 await servers[0].users.create({ username: user.username, password: user.password })
49 36
50 const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { fixture: 'video_short.webm' }) 37 const { uuid } = await servers[0].videos.upload({ attributes: { fixture: 'video_short.webm' } })
51 const videoUUID = resVideo.body.video.uuid
52 38
53 await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoUUID, 'comment') 39 await servers[0].comments.createThread({ videoId: uuid, text: 'comment' })
54 40
55 await viewVideo(servers[0].url, videoUUID) 41 await servers[0].videos.view({ id: uuid })
56 42
57 // Wait the video views repeatable job 43 // Wait the video views repeatable job
58 await wait(8000) 44 await wait(8000)
59 45
60 await follow(servers[2].url, [ servers[0].url ], servers[2].accessToken) 46 await servers[2].follows.follow({ hosts: [ servers[0].url ] })
61 await waitJobs(servers) 47 await waitJobs(servers)
62 }) 48 })
63 49
64 it('Should have the correct stats on instance 1', async function () { 50 it('Should have the correct stats on instance 1', async function () {
65 const res = await getStats(servers[0].url) 51 const data = await servers[0].stats.get()
66 const data: ServerStats = res.body
67 52
68 expect(data.totalLocalVideoComments).to.equal(1) 53 expect(data.totalLocalVideoComments).to.equal(1)
69 expect(data.totalLocalVideos).to.equal(1) 54 expect(data.totalLocalVideos).to.equal(1)
@@ -78,8 +63,7 @@ describe('Test stats (excluding redundancy)', function () {
78 }) 63 })
79 64
80 it('Should have the correct stats on instance 2', async function () { 65 it('Should have the correct stats on instance 2', async function () {
81 const res = await getStats(servers[1].url) 66 const data = await servers[1].stats.get()
82 const data: ServerStats = res.body
83 67
84 expect(data.totalLocalVideoComments).to.equal(0) 68 expect(data.totalLocalVideoComments).to.equal(0)
85 expect(data.totalLocalVideos).to.equal(0) 69 expect(data.totalLocalVideos).to.equal(0)
@@ -94,8 +78,7 @@ describe('Test stats (excluding redundancy)', function () {
94 }) 78 })
95 79
96 it('Should have the correct stats on instance 3', async function () { 80 it('Should have the correct stats on instance 3', async function () {
97 const res = await getStats(servers[2].url) 81 const data = await servers[2].stats.get()
98 const data: ServerStats = res.body
99 82
100 expect(data.totalLocalVideoComments).to.equal(0) 83 expect(data.totalLocalVideoComments).to.equal(0)
101 expect(data.totalLocalVideos).to.equal(0) 84 expect(data.totalLocalVideos).to.equal(0)
@@ -111,11 +94,10 @@ describe('Test stats (excluding redundancy)', function () {
111 it('Should have the correct total videos stats after an unfollow', async function () { 94 it('Should have the correct total videos stats after an unfollow', async function () {
112 this.timeout(15000) 95 this.timeout(15000)
113 96
114 await unfollow(servers[2].url, servers[2].accessToken, servers[0]) 97 await servers[2].follows.unfollow({ target: servers[0] })
115 await waitJobs(servers) 98 await waitJobs(servers)
116 99
117 const res = await getStats(servers[2].url) 100 const data = await servers[2].stats.get()
118 const data: ServerStats = res.body
119 101
120 expect(data.totalVideos).to.equal(0) 102 expect(data.totalVideos).to.equal(0)
121 }) 103 })
@@ -124,18 +106,18 @@ describe('Test stats (excluding redundancy)', function () {
124 const server = servers[0] 106 const server = servers[0]
125 107
126 { 108 {
127 const res = await getStats(server.url) 109 const data = await server.stats.get()
128 const data: ServerStats = res.body 110
129 expect(data.totalDailyActiveUsers).to.equal(1) 111 expect(data.totalDailyActiveUsers).to.equal(1)
130 expect(data.totalWeeklyActiveUsers).to.equal(1) 112 expect(data.totalWeeklyActiveUsers).to.equal(1)
131 expect(data.totalMonthlyActiveUsers).to.equal(1) 113 expect(data.totalMonthlyActiveUsers).to.equal(1)
132 } 114 }
133 115
134 { 116 {
135 await userLogin(server, user) 117 await server.login.getAccessToken(user)
118
119 const data = await server.stats.get()
136 120
137 const res = await getStats(server.url)
138 const data: ServerStats = res.body
139 expect(data.totalDailyActiveUsers).to.equal(2) 121 expect(data.totalDailyActiveUsers).to.equal(2)
140 expect(data.totalWeeklyActiveUsers).to.equal(2) 122 expect(data.totalWeeklyActiveUsers).to.equal(2)
141 expect(data.totalMonthlyActiveUsers).to.equal(2) 123 expect(data.totalMonthlyActiveUsers).to.equal(2)
@@ -146,33 +128,33 @@ describe('Test stats (excluding redundancy)', function () {
146 const server = servers[0] 128 const server = servers[0]
147 129
148 { 130 {
149 const res = await getStats(server.url) 131 const data = await server.stats.get()
150 const data: ServerStats = res.body 132
151 expect(data.totalLocalDailyActiveVideoChannels).to.equal(1) 133 expect(data.totalLocalDailyActiveVideoChannels).to.equal(1)
152 expect(data.totalLocalWeeklyActiveVideoChannels).to.equal(1) 134 expect(data.totalLocalWeeklyActiveVideoChannels).to.equal(1)
153 expect(data.totalLocalMonthlyActiveVideoChannels).to.equal(1) 135 expect(data.totalLocalMonthlyActiveVideoChannels).to.equal(1)
154 } 136 }
155 137
156 { 138 {
157 const channelAttributes = { 139 const attributes = {
158 name: 'stats_channel', 140 name: 'stats_channel',
159 displayName: 'My stats channel' 141 displayName: 'My stats channel'
160 } 142 }
161 const resChannel = await addVideoChannel(server.url, server.accessToken, channelAttributes) 143 const created = await server.channels.create({ attributes })
162 channelId = resChannel.body.videoChannel.id 144 channelId = created.id
145
146 const data = await server.stats.get()
163 147
164 const res = await getStats(server.url)
165 const data: ServerStats = res.body
166 expect(data.totalLocalDailyActiveVideoChannels).to.equal(1) 148 expect(data.totalLocalDailyActiveVideoChannels).to.equal(1)
167 expect(data.totalLocalWeeklyActiveVideoChannels).to.equal(1) 149 expect(data.totalLocalWeeklyActiveVideoChannels).to.equal(1)
168 expect(data.totalLocalMonthlyActiveVideoChannels).to.equal(1) 150 expect(data.totalLocalMonthlyActiveVideoChannels).to.equal(1)
169 } 151 }
170 152
171 { 153 {
172 await uploadVideo(server.url, server.accessToken, { fixture: 'video_short.webm', channelId }) 154 await server.videos.upload({ attributes: { fixture: 'video_short.webm', channelId } })
155
156 const data = await server.stats.get()
173 157
174 const res = await getStats(server.url)
175 const data: ServerStats = res.body
176 expect(data.totalLocalDailyActiveVideoChannels).to.equal(2) 158 expect(data.totalLocalDailyActiveVideoChannels).to.equal(2)
177 expect(data.totalLocalWeeklyActiveVideoChannels).to.equal(2) 159 expect(data.totalLocalWeeklyActiveVideoChannels).to.equal(2)
178 expect(data.totalLocalMonthlyActiveVideoChannels).to.equal(2) 160 expect(data.totalLocalMonthlyActiveVideoChannels).to.equal(2)
@@ -183,66 +165,62 @@ describe('Test stats (excluding redundancy)', function () {
183 const server = servers[0] 165 const server = servers[0]
184 166
185 { 167 {
186 const resStats = await getStats(server.url) 168 const data = await server.stats.get()
187 const dataStats: ServerStats = resStats.body 169 expect(data.totalLocalPlaylists).to.equal(0)
188 expect(dataStats.totalLocalPlaylists).to.equal(0)
189 } 170 }
190 171
191 { 172 {
192 await createVideoPlaylist({ 173 await server.playlists.create({
193 url: server.url, 174 attributes: {
194 token: server.accessToken,
195 playlistAttrs: {
196 displayName: 'playlist for count', 175 displayName: 'playlist for count',
197 privacy: VideoPlaylistPrivacy.PUBLIC, 176 privacy: VideoPlaylistPrivacy.PUBLIC,
198 videoChannelId: channelId 177 videoChannelId: channelId
199 } 178 }
200 }) 179 })
201 180
202 const resStats = await getStats(server.url) 181 const data = await server.stats.get()
203 const dataStats: ServerStats = resStats.body 182 expect(data.totalLocalPlaylists).to.equal(1)
204 expect(dataStats.totalLocalPlaylists).to.equal(1)
205 } 183 }
206 }) 184 })
207 185
208 it('Should correctly count video file sizes if transcoding is enabled', async function () { 186 it('Should correctly count video file sizes if transcoding is enabled', async function () {
209 this.timeout(60000) 187 this.timeout(60000)
210 188
211 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, { 189 await servers[0].config.updateCustomSubConfig({
212 transcoding: { 190 newConfig: {
213 enabled: true, 191 transcoding: {
214 webtorrent: { 192 enabled: true,
215 enabled: true 193 webtorrent: {
216 }, 194 enabled: true
217 hls: { 195 },
218 enabled: true 196 hls: {
219 }, 197 enabled: true
220 resolutions: { 198 },
221 '0p': false, 199 resolutions: {
222 '240p': false, 200 '0p': false,
223 '360p': false, 201 '240p': false,
224 '480p': false, 202 '360p': false,
225 '720p': false, 203 '480p': false,
226 '1080p': false, 204 '720p': false,
227 '1440p': false, 205 '1080p': false,
228 '2160p': false 206 '1440p': false,
207 '2160p': false
208 }
229 } 209 }
230 } 210 }
231 }) 211 })
232 212
233 await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video', fixture: 'video_short.webm' }) 213 await servers[0].videos.upload({ attributes: { name: 'video', fixture: 'video_short.webm' } })
234 214
235 await waitJobs(servers) 215 await waitJobs(servers)
236 216
237 { 217 {
238 const res = await getStats(servers[1].url) 218 const data = await servers[1].stats.get()
239 const data: ServerStats = res.body
240 expect(data.totalLocalVideoFilesSize).to.equal(0) 219 expect(data.totalLocalVideoFilesSize).to.equal(0)
241 } 220 }
242 221
243 { 222 {
244 const res = await getStats(servers[0].url) 223 const data = await servers[0].stats.get()
245 const data: ServerStats = res.body
246 expect(data.totalLocalVideoFilesSize).to.be.greaterThan(500000) 224 expect(data.totalLocalVideoFilesSize).to.be.greaterThan(500000)
247 expect(data.totalLocalVideoFilesSize).to.be.lessThan(600000) 225 expect(data.totalLocalVideoFilesSize).to.be.lessThan(600000)
248 } 226 }
@@ -251,27 +229,27 @@ describe('Test stats (excluding redundancy)', function () {
251 it('Should have the correct AP stats', async function () { 229 it('Should have the correct AP stats', async function () {
252 this.timeout(60000) 230 this.timeout(60000)
253 231
254 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, { 232 await servers[0].config.updateCustomSubConfig({
255 transcoding: { 233 newConfig: {
256 enabled: false 234 transcoding: {
235 enabled: false
236 }
257 } 237 }
258 }) 238 })
259 239
260 const res1 = await getStats(servers[1].url) 240 const first = await servers[1].stats.get()
261 const first = res1.body as ServerStats
262 241
263 for (let i = 0; i < 10; i++) { 242 for (let i = 0; i < 10; i++) {
264 await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video' }) 243 await servers[0].videos.upload({ attributes: { name: 'video' } })
265 } 244 }
266 245
267 await waitJobs(servers) 246 await waitJobs(servers)
268 247
269 await wait(6000) 248 await wait(6000)
270 249
271 const res2 = await getStats(servers[1].url) 250 const second = await servers[1].stats.get()
272 const second: ServerStats = res2.body
273
274 expect(second.totalActivityPubMessagesProcessed).to.be.greaterThan(first.totalActivityPubMessagesProcessed) 251 expect(second.totalActivityPubMessagesProcessed).to.be.greaterThan(first.totalActivityPubMessagesProcessed)
252
275 const apTypes: ActivityType[] = [ 253 const apTypes: ActivityType[] = [
276 'Create', 'Update', 'Delete', 'Follow', 'Accept', 'Announce', 'Undo', 'Like', 'Reject', 'View', 'Dislike', 'Flag' 254 'Create', 'Update', 'Delete', 'Follow', 'Accept', 'Announce', 'Undo', 'Like', 'Reject', 'View', 'Dislike', 'Flag'
277 ] 255 ]
@@ -291,9 +269,7 @@ describe('Test stats (excluding redundancy)', function () {
291 269
292 await wait(6000) 270 await wait(6000)
293 271
294 const res3 = await getStats(servers[1].url) 272 const third = await servers[1].stats.get()
295 const third: ServerStats = res3.body
296
297 expect(third.totalActivityPubMessagesWaiting).to.equal(0) 273 expect(third.totalActivityPubMessagesWaiting).to.equal(0)
298 expect(third.activityPubMessagesProcessedPerSecond).to.be.lessThan(second.activityPubMessagesProcessedPerSecond) 274 expect(third.activityPubMessagesProcessedPerSecond).to.be.lessThan(second.activityPubMessagesProcessedPerSecond)
299 }) 275 })
diff --git a/server/tests/api/server/tracker.ts b/server/tests/api/server/tracker.ts
index 4b86e0b90..f597ac60c 100644
--- a/server/tests/api/server/tracker.ts
+++ b/server/tests/api/server/tracker.ts
@@ -1,36 +1,23 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await,@typescript-eslint/no-floating-promises */ 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await,@typescript-eslint/no-floating-promises */
2 2
3import * as magnetUtil from 'magnet-uri'
4import 'mocha' 3import 'mocha'
5import { 4import * as magnetUtil from 'magnet-uri'
6 cleanupTests,
7 flushAndRunServer,
8 getVideo,
9 killallServers,
10 reRunServer,
11 ServerInfo,
12 uploadVideo
13} from '../../../../shared/extra-utils'
14import { setAccessTokensToServers } from '../../../../shared/extra-utils/index'
15import { VideoDetails } from '../../../../shared/models/videos'
16import * as WebTorrent from 'webtorrent' 5import * as WebTorrent from 'webtorrent'
6import { cleanupTests, createSingleServer, killallServers, PeerTubeServer, setAccessTokensToServers } from '@shared/extra-utils'
17 7
18describe('Test tracker', function () { 8describe('Test tracker', function () {
19 let server: ServerInfo 9 let server: PeerTubeServer
20 let badMagnet: string 10 let badMagnet: string
21 let goodMagnet: string 11 let goodMagnet: string
22 12
23 before(async function () { 13 before(async function () {
24 this.timeout(60000) 14 this.timeout(60000)
25 server = await flushAndRunServer(1) 15 server = await createSingleServer(1)
26 await setAccessTokensToServers([ server ]) 16 await setAccessTokensToServers([ server ])
27 17
28 { 18 {
29 const res = await uploadVideo(server.url, server.accessToken, {}) 19 const { uuid } = await server.videos.upload()
30 const videoUUID = res.body.video.uuid 20 const video = await server.videos.get({ id: uuid })
31
32 const resGet = await getVideo(server.url, videoUUID)
33 const video: VideoDetails = resGet.body
34 goodMagnet = video.files[0].magnetUri 21 goodMagnet = video.files[0].magnetUri
35 22
36 const parsed = magnetUtil.decode(goodMagnet) 23 const parsed = magnetUtil.decode(goodMagnet)
@@ -61,8 +48,7 @@ describe('Test tracker', function () {
61 const errCb = () => done(new Error('Tracker is enabled')) 48 const errCb = () => done(new Error('Tracker is enabled'))
62 49
63 killallServers([ server ]) 50 killallServers([ server ])
64 51 .then(() => server.run({ tracker: { enabled: false } }))
65 reRunServer(server, { tracker: { enabled: false } })
66 .then(() => { 52 .then(() => {
67 const webtorrent = new WebTorrent() 53 const webtorrent = new WebTorrent()
68 54
@@ -86,8 +72,7 @@ describe('Test tracker', function () {
86 this.timeout(20000) 72 this.timeout(20000)
87 73
88 killallServers([ server ]) 74 killallServers([ server ])
89 75 .then(() => server.run())
90 reRunServer(server)
91 .then(() => { 76 .then(() => {
92 const webtorrent = new WebTorrent() 77 const webtorrent = new WebTorrent()
93 78
diff --git a/server/tests/api/users/user-subscriptions.ts b/server/tests/api/users/user-subscriptions.ts
index 60676a37b..77b99886d 100644
--- a/server/tests/api/users/user-subscriptions.ts
+++ b/server/tests/api/users/user-subscriptions.ts
@@ -1,42 +1,30 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2 2
3import * as chai from 'chai'
4import 'mocha' 3import 'mocha'
4import * as chai from 'chai'
5import { 5import {
6 cleanupTests, 6 cleanupTests,
7 createUser, 7 createMultipleServers,
8 doubleFollow, 8 doubleFollow,
9 flushAndRunMultipleServers, 9 PeerTubeServer,
10 follow, 10 setAccessTokensToServers,
11 getVideosList, 11 SubscriptionsCommand,
12 unfollow, 12 waitJobs
13 updateVideo, 13} from '@shared/extra-utils'
14 userLogin
15} from '../../../../shared/extra-utils'
16import { ServerInfo, uploadVideo } from '../../../../shared/extra-utils/index'
17import { setAccessTokensToServers } from '../../../../shared/extra-utils/users/login'
18import { Video, VideoChannel } from '../../../../shared/models/videos'
19import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
20import {
21 addUserSubscription,
22 areSubscriptionsExist,
23 getUserSubscription,
24 listUserSubscriptions,
25 listUserSubscriptionVideos,
26 removeUserSubscription
27} from '../../../../shared/extra-utils/users/user-subscriptions'
28 14
29const expect = chai.expect 15const expect = chai.expect
30 16
31describe('Test users subscriptions', function () { 17describe('Test users subscriptions', function () {
32 let servers: ServerInfo[] = [] 18 let servers: PeerTubeServer[] = []
33 const users: { accessToken: string }[] = [] 19 const users: { accessToken: string }[] = []
34 let video3UUID: string 20 let video3UUID: string
35 21
22 let command: SubscriptionsCommand
23
36 before(async function () { 24 before(async function () {
37 this.timeout(120000) 25 this.timeout(120000)
38 26
39 servers = await flushAndRunMultipleServers(3) 27 servers = await createMultipleServers(3)
40 28
41 // Get the access tokens 29 // Get the access tokens
42 await setAccessTokensToServers(servers) 30 await setAccessTokensToServers(servers)
@@ -47,47 +35,50 @@ describe('Test users subscriptions', function () {
47 { 35 {
48 for (const server of servers) { 36 for (const server of servers) {
49 const user = { username: 'user' + server.serverNumber, password: 'password' } 37 const user = { username: 'user' + server.serverNumber, password: 'password' }
50 await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password }) 38 await server.users.create({ username: user.username, password: user.password })
51 39
52 const accessToken = await userLogin(server, user) 40 const accessToken = await server.login.getAccessToken(user)
53 users.push({ accessToken }) 41 users.push({ accessToken })
54 42
55 const videoName1 = 'video 1-' + server.serverNumber 43 const videoName1 = 'video 1-' + server.serverNumber
56 await uploadVideo(server.url, accessToken, { name: videoName1 }) 44 await server.videos.upload({ token: accessToken, attributes: { name: videoName1 } })
57 45
58 const videoName2 = 'video 2-' + server.serverNumber 46 const videoName2 = 'video 2-' + server.serverNumber
59 await uploadVideo(server.url, accessToken, { name: videoName2 }) 47 await server.videos.upload({ token: accessToken, attributes: { name: videoName2 } })
60 } 48 }
61 } 49 }
62 50
63 await waitJobs(servers) 51 await waitJobs(servers)
52
53 command = servers[0].subscriptions
64 }) 54 })
65 55
66 it('Should display videos of server 2 on server 1', async function () { 56 it('Should display videos of server 2 on server 1', async function () {
67 const res = await getVideosList(servers[0].url) 57 const { total } = await servers[0].videos.list()
68 58
69 expect(res.body.total).to.equal(4) 59 expect(total).to.equal(4)
70 }) 60 })
71 61
72 it('User of server 1 should follow user of server 3 and root of server 1', async function () { 62 it('User of server 1 should follow user of server 3 and root of server 1', async function () {
73 this.timeout(60000) 63 this.timeout(60000)
74 64
75 await addUserSubscription(servers[0].url, users[0].accessToken, 'user3_channel@localhost:' + servers[2].port) 65 await command.add({ token: users[0].accessToken, targetUri: 'user3_channel@localhost:' + servers[2].port })
76 await addUserSubscription(servers[0].url, users[0].accessToken, 'root_channel@localhost:' + servers[0].port) 66 await command.add({ token: users[0].accessToken, targetUri: 'root_channel@localhost:' + servers[0].port })
77 67
78 await waitJobs(servers) 68 await waitJobs(servers)
79 69
80 const res = await uploadVideo(servers[2].url, users[2].accessToken, { name: 'video server 3 added after follow' }) 70 const attributes = { name: 'video server 3 added after follow' }
81 video3UUID = res.body.video.uuid 71 const { uuid } = await servers[2].videos.upload({ token: users[2].accessToken, attributes })
72 video3UUID = uuid
82 73
83 await waitJobs(servers) 74 await waitJobs(servers)
84 }) 75 })
85 76
86 it('Should not display videos of server 3 on server 1', async function () { 77 it('Should not display videos of server 3 on server 1', async function () {
87 const res = await getVideosList(servers[0].url) 78 const { total, data } = await servers[0].videos.list()
79 expect(total).to.equal(4)
88 80
89 expect(res.body.total).to.equal(4) 81 for (const video of data) {
90 for (const video of res.body.data) {
91 expect(video.name).to.not.contain('1-3') 82 expect(video.name).to.not.contain('1-3')
92 expect(video.name).to.not.contain('2-3') 83 expect(video.name).to.not.contain('2-3')
93 expect(video.name).to.not.contain('video server 3 added after follow') 84 expect(video.name).to.not.contain('video server 3 added after follow')
@@ -96,17 +87,17 @@ describe('Test users subscriptions', function () {
96 87
97 it('Should list subscriptions', async function () { 88 it('Should list subscriptions', async function () {
98 { 89 {
99 const res = await listUserSubscriptions({ url: servers[0].url, token: servers[0].accessToken }) 90 const body = await command.list()
100 expect(res.body.total).to.equal(0) 91 expect(body.total).to.equal(0)
101 expect(res.body.data).to.be.an('array') 92 expect(body.data).to.be.an('array')
102 expect(res.body.data).to.have.lengthOf(0) 93 expect(body.data).to.have.lengthOf(0)
103 } 94 }
104 95
105 { 96 {
106 const res = await listUserSubscriptions({ url: servers[0].url, token: users[0].accessToken, sort: 'createdAt' }) 97 const body = await command.list({ token: users[0].accessToken, sort: 'createdAt' })
107 expect(res.body.total).to.equal(2) 98 expect(body.total).to.equal(2)
108 99
109 const subscriptions: VideoChannel[] = res.body.data 100 const subscriptions = body.data
110 expect(subscriptions).to.be.an('array') 101 expect(subscriptions).to.be.an('array')
111 expect(subscriptions).to.have.lengthOf(2) 102 expect(subscriptions).to.have.lengthOf(2)
112 103
@@ -117,8 +108,7 @@ describe('Test users subscriptions', function () {
117 108
118 it('Should get subscription', async function () { 109 it('Should get subscription', async function () {
119 { 110 {
120 const res = await getUserSubscription(servers[0].url, users[0].accessToken, 'user3_channel@localhost:' + servers[2].port) 111 const videoChannel = await command.get({ token: users[0].accessToken, uri: 'user3_channel@localhost:' + servers[2].port })
121 const videoChannel: VideoChannel = res.body
122 112
123 expect(videoChannel.name).to.equal('user3_channel') 113 expect(videoChannel.name).to.equal('user3_channel')
124 expect(videoChannel.host).to.equal('localhost:' + servers[2].port) 114 expect(videoChannel.host).to.equal('localhost:' + servers[2].port)
@@ -128,8 +118,7 @@ describe('Test users subscriptions', function () {
128 } 118 }
129 119
130 { 120 {
131 const res = await getUserSubscription(servers[0].url, users[0].accessToken, 'root_channel@localhost:' + servers[0].port) 121 const videoChannel = await command.get({ token: users[0].accessToken, uri: 'root_channel@localhost:' + servers[0].port })
132 const videoChannel: VideoChannel = res.body
133 122
134 expect(videoChannel.name).to.equal('root_channel') 123 expect(videoChannel.name).to.equal('root_channel')
135 expect(videoChannel.host).to.equal('localhost:' + servers[0].port) 124 expect(videoChannel.host).to.equal('localhost:' + servers[0].port)
@@ -147,8 +136,7 @@ describe('Test users subscriptions', function () {
147 'user3_channel@localhost:' + servers[0].port 136 'user3_channel@localhost:' + servers[0].port
148 ] 137 ]
149 138
150 const res = await areSubscriptionsExist(servers[0].url, users[0].accessToken, uris) 139 const body = await command.exist({ token: users[0].accessToken, uris })
151 const body = res.body
152 140
153 expect(body['user3_channel@localhost:' + servers[2].port]).to.be.true 141 expect(body['user3_channel@localhost:' + servers[2].port]).to.be.true
154 expect(body['root2_channel@localhost:' + servers[0].port]).to.be.false 142 expect(body['root2_channel@localhost:' + servers[0].port]).to.be.false
@@ -158,45 +146,31 @@ describe('Test users subscriptions', function () {
158 146
159 it('Should search among subscriptions', async function () { 147 it('Should search among subscriptions', async function () {
160 { 148 {
161 const res = await listUserSubscriptions({ 149 const body = await command.list({ token: users[0].accessToken, sort: '-createdAt', search: 'user3_channel' })
162 url: servers[0].url, 150 expect(body.total).to.equal(1)
163 token: users[0].accessToken, 151 expect(body.data).to.have.lengthOf(1)
164 sort: '-createdAt',
165 search: 'user3_channel'
166 })
167 expect(res.body.total).to.equal(1)
168
169 const subscriptions = res.body.data
170 expect(subscriptions).to.have.lengthOf(1)
171 } 152 }
172 153
173 { 154 {
174 const res = await listUserSubscriptions({ 155 const body = await command.list({ token: users[0].accessToken, sort: '-createdAt', search: 'toto' })
175 url: servers[0].url, 156 expect(body.total).to.equal(0)
176 token: users[0].accessToken, 157 expect(body.data).to.have.lengthOf(0)
177 sort: '-createdAt',
178 search: 'toto'
179 })
180 expect(res.body.total).to.equal(0)
181
182 const subscriptions = res.body.data
183 expect(subscriptions).to.have.lengthOf(0)
184 } 158 }
185 }) 159 })
186 160
187 it('Should list subscription videos', async function () { 161 it('Should list subscription videos', async function () {
188 { 162 {
189 const res = await listUserSubscriptionVideos(servers[0].url, servers[0].accessToken) 163 const body = await command.listVideos()
190 expect(res.body.total).to.equal(0) 164 expect(body.total).to.equal(0)
191 expect(res.body.data).to.be.an('array') 165 expect(body.data).to.be.an('array')
192 expect(res.body.data).to.have.lengthOf(0) 166 expect(body.data).to.have.lengthOf(0)
193 } 167 }
194 168
195 { 169 {
196 const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt') 170 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
197 expect(res.body.total).to.equal(3) 171 expect(body.total).to.equal(3)
198 172
199 const videos: Video[] = res.body.data 173 const videos = body.data
200 expect(videos).to.be.an('array') 174 expect(videos).to.be.an('array')
201 expect(videos).to.have.lengthOf(3) 175 expect(videos).to.have.lengthOf(3)
202 176
@@ -210,22 +184,22 @@ describe('Test users subscriptions', function () {
210 this.timeout(60000) 184 this.timeout(60000)
211 185
212 const videoName = 'video server 1 added after follow' 186 const videoName = 'video server 1 added after follow'
213 await uploadVideo(servers[0].url, servers[0].accessToken, { name: videoName }) 187 await servers[0].videos.upload({ attributes: { name: videoName } })
214 188
215 await waitJobs(servers) 189 await waitJobs(servers)
216 190
217 { 191 {
218 const res = await listUserSubscriptionVideos(servers[0].url, servers[0].accessToken) 192 const body = await command.listVideos()
219 expect(res.body.total).to.equal(0) 193 expect(body.total).to.equal(0)
220 expect(res.body.data).to.be.an('array') 194 expect(body.data).to.be.an('array')
221 expect(res.body.data).to.have.lengthOf(0) 195 expect(body.data).to.have.lengthOf(0)
222 } 196 }
223 197
224 { 198 {
225 const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt') 199 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
226 expect(res.body.total).to.equal(4) 200 expect(body.total).to.equal(4)
227 201
228 const videos: Video[] = res.body.data 202 const videos = body.data
229 expect(videos).to.be.an('array') 203 expect(videos).to.be.an('array')
230 expect(videos).to.have.lengthOf(4) 204 expect(videos).to.have.lengthOf(4)
231 205
@@ -236,10 +210,10 @@ describe('Test users subscriptions', function () {
236 } 210 }
237 211
238 { 212 {
239 const res = await getVideosList(servers[0].url) 213 const { data, total } = await servers[0].videos.list()
214 expect(total).to.equal(5)
240 215
241 expect(res.body.total).to.equal(5) 216 for (const video of data) {
242 for (const video of res.body.data) {
243 expect(video.name).to.not.contain('1-3') 217 expect(video.name).to.not.contain('1-3')
244 expect(video.name).to.not.contain('2-3') 218 expect(video.name).to.not.contain('2-3')
245 expect(video.name).to.not.contain('video server 3 added after follow') 219 expect(video.name).to.not.contain('video server 3 added after follow')
@@ -250,17 +224,16 @@ describe('Test users subscriptions', function () {
250 it('Should have server 1 follow server 3 and display server 3 videos', async function () { 224 it('Should have server 1 follow server 3 and display server 3 videos', async function () {
251 this.timeout(60000) 225 this.timeout(60000)
252 226
253 await follow(servers[0].url, [ servers[2].url ], servers[0].accessToken) 227 await servers[0].follows.follow({ hosts: [ servers[2].url ] })
254 228
255 await waitJobs(servers) 229 await waitJobs(servers)
256 230
257 const res = await getVideosList(servers[0].url) 231 const { data, total } = await servers[0].videos.list()
258 232 expect(total).to.equal(8)
259 expect(res.body.total).to.equal(8)
260 233
261 const names = [ '1-3', '2-3', 'video server 3 added after follow' ] 234 const names = [ '1-3', '2-3', 'video server 3 added after follow' ]
262 for (const name of names) { 235 for (const name of names) {
263 const video = res.body.data.find(v => v.name.indexOf(name) === -1) 236 const video = data.find(v => v.name.includes(name))
264 expect(video).to.not.be.undefined 237 expect(video).to.not.be.undefined
265 } 238 }
266 }) 239 })
@@ -268,14 +241,14 @@ describe('Test users subscriptions', function () {
268 it('Should remove follow server 1 -> server 3 and hide server 3 videos', async function () { 241 it('Should remove follow server 1 -> server 3 and hide server 3 videos', async function () {
269 this.timeout(60000) 242 this.timeout(60000)
270 243
271 await unfollow(servers[0].url, servers[0].accessToken, servers[2]) 244 await servers[0].follows.unfollow({ target: servers[2] })
272 245
273 await waitJobs(servers) 246 await waitJobs(servers)
274 247
275 const res = await getVideosList(servers[0].url) 248 const { total, data } = await servers[0].videos.list()
249 expect(total).to.equal(5)
276 250
277 expect(res.body.total).to.equal(5) 251 for (const video of data) {
278 for (const video of res.body.data) {
279 expect(video.name).to.not.contain('1-3') 252 expect(video.name).to.not.contain('1-3')
280 expect(video.name).to.not.contain('2-3') 253 expect(video.name).to.not.contain('2-3')
281 expect(video.name).to.not.contain('video server 3 added after follow') 254 expect(video.name).to.not.contain('video server 3 added after follow')
@@ -284,17 +257,17 @@ describe('Test users subscriptions', function () {
284 257
285 it('Should still list subscription videos', async function () { 258 it('Should still list subscription videos', async function () {
286 { 259 {
287 const res = await listUserSubscriptionVideos(servers[0].url, servers[0].accessToken) 260 const body = await command.listVideos()
288 expect(res.body.total).to.equal(0) 261 expect(body.total).to.equal(0)
289 expect(res.body.data).to.be.an('array') 262 expect(body.data).to.be.an('array')
290 expect(res.body.data).to.have.lengthOf(0) 263 expect(body.data).to.have.lengthOf(0)
291 } 264 }
292 265
293 { 266 {
294 const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt') 267 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
295 expect(res.body.total).to.equal(4) 268 expect(body.total).to.equal(4)
296 269
297 const videos: Video[] = res.body.data 270 const videos = body.data
298 expect(videos).to.be.an('array') 271 expect(videos).to.be.an('array')
299 expect(videos).to.have.lengthOf(4) 272 expect(videos).to.have.lengthOf(4)
300 273
@@ -308,58 +281,55 @@ describe('Test users subscriptions', function () {
308 it('Should update a video of server 3 and see the updated video on server 1', async function () { 281 it('Should update a video of server 3 and see the updated video on server 1', async function () {
309 this.timeout(30000) 282 this.timeout(30000)
310 283
311 await updateVideo(servers[2].url, users[2].accessToken, video3UUID, { name: 'video server 3 added after follow updated' }) 284 await servers[2].videos.update({ id: video3UUID, attributes: { name: 'video server 3 added after follow updated' } })
312 285
313 await waitJobs(servers) 286 await waitJobs(servers)
314 287
315 const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt') 288 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
316 const videos: Video[] = res.body.data 289 expect(body.data[2].name).to.equal('video server 3 added after follow updated')
317 expect(videos[2].name).to.equal('video server 3 added after follow updated')
318 }) 290 })
319 291
320 it('Should remove user of server 3 subscription', async function () { 292 it('Should remove user of server 3 subscription', async function () {
321 this.timeout(30000) 293 this.timeout(30000)
322 294
323 await removeUserSubscription(servers[0].url, users[0].accessToken, 'user3_channel@localhost:' + servers[2].port) 295 await command.remove({ token: users[0].accessToken, uri: 'user3_channel@localhost:' + servers[2].port })
324 296
325 await waitJobs(servers) 297 await waitJobs(servers)
326 }) 298 })
327 299
328 it('Should not display its videos anymore', async function () { 300 it('Should not display its videos anymore', async function () {
329 { 301 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
330 const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt') 302 expect(body.total).to.equal(1)
331 expect(res.body.total).to.equal(1)
332 303
333 const videos: Video[] = res.body.data 304 const videos = body.data
334 expect(videos).to.be.an('array') 305 expect(videos).to.be.an('array')
335 expect(videos).to.have.lengthOf(1) 306 expect(videos).to.have.lengthOf(1)
336 307
337 expect(videos[0].name).to.equal('video server 1 added after follow') 308 expect(videos[0].name).to.equal('video server 1 added after follow')
338 }
339 }) 309 })
340 310
341 it('Should remove the root subscription and not display the videos anymore', async function () { 311 it('Should remove the root subscription and not display the videos anymore', async function () {
342 this.timeout(30000) 312 this.timeout(30000)
343 313
344 await removeUserSubscription(servers[0].url, users[0].accessToken, 'root_channel@localhost:' + servers[0].port) 314 await command.remove({ token: users[0].accessToken, uri: 'root_channel@localhost:' + servers[0].port })
345 315
346 await waitJobs(servers) 316 await waitJobs(servers)
347 317
348 { 318 {
349 const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt') 319 const body = await command.list({ token: users[0].accessToken, sort: 'createdAt' })
350 expect(res.body.total).to.equal(0) 320 expect(body.total).to.equal(0)
351 321
352 const videos: Video[] = res.body.data 322 const videos = body.data
353 expect(videos).to.be.an('array') 323 expect(videos).to.be.an('array')
354 expect(videos).to.have.lengthOf(0) 324 expect(videos).to.have.lengthOf(0)
355 } 325 }
356 }) 326 })
357 327
358 it('Should correctly display public videos on server 1', async function () { 328 it('Should correctly display public videos on server 1', async function () {
359 const res = await getVideosList(servers[0].url) 329 const { total, data } = await servers[0].videos.list()
330 expect(total).to.equal(5)
360 331
361 expect(res.body.total).to.equal(5) 332 for (const video of data) {
362 for (const video of res.body.data) {
363 expect(video.name).to.not.contain('1-3') 333 expect(video.name).to.not.contain('1-3')
364 expect(video.name).to.not.contain('2-3') 334 expect(video.name).to.not.contain('2-3')
365 expect(video.name).to.not.contain('video server 3 added after follow updated') 335 expect(video.name).to.not.contain('video server 3 added after follow updated')
@@ -369,15 +339,15 @@ describe('Test users subscriptions', function () {
369 it('Should follow user of server 3 again', async function () { 339 it('Should follow user of server 3 again', async function () {
370 this.timeout(60000) 340 this.timeout(60000)
371 341
372 await addUserSubscription(servers[0].url, users[0].accessToken, 'user3_channel@localhost:' + servers[2].port) 342 await command.add({ token: users[0].accessToken, targetUri: 'user3_channel@localhost:' + servers[2].port })
373 343
374 await waitJobs(servers) 344 await waitJobs(servers)
375 345
376 { 346 {
377 const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt') 347 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
378 expect(res.body.total).to.equal(3) 348 expect(body.total).to.equal(3)
379 349
380 const videos: Video[] = res.body.data 350 const videos = body.data
381 expect(videos).to.be.an('array') 351 expect(videos).to.be.an('array')
382 expect(videos).to.have.lengthOf(3) 352 expect(videos).to.have.lengthOf(3)
383 353
@@ -387,10 +357,10 @@ describe('Test users subscriptions', function () {
387 } 357 }
388 358
389 { 359 {
390 const res = await getVideosList(servers[0].url) 360 const { total, data } = await servers[0].videos.list()
361 expect(total).to.equal(5)
391 362
392 expect(res.body.total).to.equal(5) 363 for (const video of data) {
393 for (const video of res.body.data) {
394 expect(video.name).to.not.contain('1-3') 364 expect(video.name).to.not.contain('1-3')
395 expect(video.name).to.not.contain('2-3') 365 expect(video.name).to.not.contain('2-3')
396 expect(video.name).to.not.contain('video server 3 added after follow updated') 366 expect(video.name).to.not.contain('video server 3 added after follow updated')
diff --git a/server/tests/api/users/users-multiple-servers.ts b/server/tests/api/users/users-multiple-servers.ts
index f60c66e4b..225145957 100644
--- a/server/tests/api/users/users-multiple-servers.ts
+++ b/server/tests/api/users/users-multiple-servers.ts
@@ -1,32 +1,25 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2 2
3import * as chai from 'chai'
4import 'mocha' 3import 'mocha'
5import { Account } from '../../../../shared/models/actors' 4import * as chai from 'chai'
6import { 5import {
6 checkActorFilesWereRemoved,
7 checkTmpIsEmpty, 7 checkTmpIsEmpty,
8 checkVideoFilesWereRemoved, 8 checkVideoFilesWereRemoved,
9 cleanupTests, 9 cleanupTests,
10 createUser, 10 createMultipleServers,
11 doubleFollow, 11 doubleFollow,
12 flushAndRunMultipleServers, 12 PeerTubeServer,
13 getAccountVideos, 13 setAccessTokensToServers,
14 getVideoChannelsList, 14 testImage,
15 removeUser, 15 waitJobs
16 updateMyUser, 16} from '@shared/extra-utils'
17 userLogin 17import { User } from '@shared/models'
18} from '../../../../shared/extra-utils'
19import { getMyUserInformation, ServerInfo, testImage, updateMyAvatar, uploadVideo } from '../../../../shared/extra-utils/index'
20import { checkActorFilesWereRemoved, getAccount, getAccountsList } from '../../../../shared/extra-utils/users/accounts'
21import { setAccessTokensToServers } from '../../../../shared/extra-utils/users/login'
22import { User } from '../../../../shared/models/users'
23import { VideoChannel } from '../../../../shared/models/videos'
24import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
25 18
26const expect = chai.expect 19const expect = chai.expect
27 20
28describe('Test users with multiple servers', function () { 21describe('Test users with multiple servers', function () {
29 let servers: ServerInfo[] = [] 22 let servers: PeerTubeServer[] = []
30 let user: User 23 let user: User
31 let userId: number 24 let userId: number
32 let videoUUID: string 25 let videoUUID: string
@@ -36,7 +29,7 @@ describe('Test users with multiple servers', function () {
36 before(async function () { 29 before(async function () {
37 this.timeout(120_000) 30 this.timeout(120_000)
38 31
39 servers = await flushAndRunMultipleServers(3) 32 servers = await createMultipleServers(3)
40 33
41 // Get the access tokens 34 // Get the access tokens
42 await setAccessTokensToServers(servers) 35 await setAccessTokensToServers(servers)
@@ -49,26 +42,21 @@ describe('Test users with multiple servers', function () {
49 await doubleFollow(servers[1], servers[2]) 42 await doubleFollow(servers[1], servers[2])
50 43
51 // The root user of server 1 is propagated to servers 2 and 3 44 // The root user of server 1 is propagated to servers 2 and 3
52 await uploadVideo(servers[0].url, servers[0].accessToken, {}) 45 await servers[0].videos.upload()
53 46
54 { 47 {
55 const user = { 48 const user = {
56 username: 'user1', 49 username: 'user1',
57 password: 'password' 50 password: 'password'
58 } 51 }
59 const res = await createUser({ 52 const created = await servers[0].users.create(user)
60 url: servers[0].url, 53 userId = created.id
61 accessToken: servers[0].accessToken, 54 userAccessToken = await servers[0].login.getAccessToken(user)
62 username: user.username,
63 password: user.password
64 })
65 userId = res.body.user.id
66 userAccessToken = await userLogin(servers[0], user)
67 } 55 }
68 56
69 { 57 {
70 const resVideo = await uploadVideo(servers[0].url, userAccessToken, {}) 58 const { uuid } = await servers[0].videos.upload({ token: userAccessToken })
71 videoUUID = resVideo.body.video.uuid 59 videoUUID = uuid
72 } 60 }
73 61
74 await waitJobs(servers) 62 await waitJobs(servers)
@@ -77,15 +65,9 @@ describe('Test users with multiple servers', function () {
77 it('Should be able to update my display name', async function () { 65 it('Should be able to update my display name', async function () {
78 this.timeout(10000) 66 this.timeout(10000)
79 67
80 await updateMyUser({ 68 await servers[0].users.updateMe({ displayName: 'my super display name' })
81 url: servers[0].url,
82 accessToken: servers[0].accessToken,
83 displayName: 'my super display name'
84 })
85
86 const res = await getMyUserInformation(servers[0].url, servers[0].accessToken)
87 user = res.body
88 69
70 user = await servers[0].users.getMyInfo()
89 expect(user.account.displayName).to.equal('my super display name') 71 expect(user.account.displayName).to.equal('my super display name')
90 72
91 await waitJobs(servers) 73 await waitJobs(servers)
@@ -94,14 +76,9 @@ describe('Test users with multiple servers', function () {
94 it('Should be able to update my description', async function () { 76 it('Should be able to update my description', async function () {
95 this.timeout(10_000) 77 this.timeout(10_000)
96 78
97 await updateMyUser({ 79 await servers[0].users.updateMe({ description: 'my super description updated' })
98 url: servers[0].url,
99 accessToken: servers[0].accessToken,
100 description: 'my super description updated'
101 })
102 80
103 const res = await getMyUserInformation(servers[0].url, servers[0].accessToken) 81 user = await servers[0].users.getMyInfo()
104 user = res.body
105 expect(user.account.displayName).to.equal('my super display name') 82 expect(user.account.displayName).to.equal('my super display name')
106 expect(user.account.description).to.equal('my super description updated') 83 expect(user.account.description).to.equal('my super description updated')
107 84
@@ -113,15 +90,9 @@ describe('Test users with multiple servers', function () {
113 90
114 const fixture = 'avatar2.png' 91 const fixture = 'avatar2.png'
115 92
116 await updateMyAvatar({ 93 await servers[0].users.updateMyAvatar({ fixture })
117 url: servers[0].url,
118 accessToken: servers[0].accessToken,
119 fixture
120 })
121
122 const res = await getMyUserInformation(servers[0].url, servers[0].accessToken)
123 user = res.body
124 94
95 user = await servers[0].users.getMyInfo()
125 userAvatarFilename = user.account.avatar.path 96 userAvatarFilename = user.account.avatar.path
126 97
127 await testImage(servers[0].url, 'avatar2-resized', userAvatarFilename, '.png') 98 await testImage(servers[0].url, 'avatar2-resized', userAvatarFilename, '.png')
@@ -133,13 +104,12 @@ describe('Test users with multiple servers', function () {
133 let createdAt: string | Date 104 let createdAt: string | Date
134 105
135 for (const server of servers) { 106 for (const server of servers) {
136 const resAccounts = await getAccountsList(server.url, '-createdAt') 107 const body = await server.accounts.list({ sort: '-createdAt' })
137 108
138 const resList = resAccounts.body.data.find(a => a.name === 'root' && a.host === 'localhost:' + servers[0].port) as Account 109 const resList = body.data.find(a => a.name === 'root' && a.host === 'localhost:' + servers[0].port)
139 expect(resList).not.to.be.undefined 110 expect(resList).not.to.be.undefined
140 111
141 const resAccount = await getAccount(server.url, resList.name + '@' + resList.host) 112 const account = await server.accounts.get({ accountName: resList.name + '@' + resList.host })
142 const account = resAccount.body as Account
143 113
144 if (!createdAt) createdAt = account.createdAt 114 if (!createdAt) createdAt = account.createdAt
145 115
@@ -161,31 +131,29 @@ describe('Test users with multiple servers', function () {
161 131
162 it('Should list account videos', async function () { 132 it('Should list account videos', async function () {
163 for (const server of servers) { 133 for (const server of servers) {
164 const res = await getAccountVideos(server.url, server.accessToken, 'user1@localhost:' + servers[0].port, 0, 5) 134 const { total, data } = await server.videos.listByAccount({ handle: 'user1@localhost:' + servers[0].port })
165 135
166 expect(res.body.total).to.equal(1) 136 expect(total).to.equal(1)
167 expect(res.body.data).to.be.an('array') 137 expect(data).to.be.an('array')
168 expect(res.body.data).to.have.lengthOf(1) 138 expect(data).to.have.lengthOf(1)
169 expect(res.body.data[0].uuid).to.equal(videoUUID) 139 expect(data[0].uuid).to.equal(videoUUID)
170 } 140 }
171 }) 141 })
172 142
173 it('Should search through account videos', async function () { 143 it('Should search through account videos', async function () {
174 this.timeout(10_000) 144 this.timeout(10_000)
175 145
176 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'Kami no chikara' }) 146 const created = await servers[0].videos.upload({ token: userAccessToken, attributes: { name: 'Kami no chikara' } })
177 147
178 await waitJobs(servers) 148 await waitJobs(servers)
179 149
180 for (const server of servers) { 150 for (const server of servers) {
181 const res = await getAccountVideos(server.url, server.accessToken, 'user1@localhost:' + servers[0].port, 0, 5, undefined, { 151 const { total, data } = await server.videos.listByAccount({ handle: 'user1@localhost:' + servers[0].port, search: 'Kami' })
182 search: 'Kami' 152
183 }) 153 expect(total).to.equal(1)
184 154 expect(data).to.be.an('array')
185 expect(res.body.total).to.equal(1) 155 expect(data).to.have.lengthOf(1)
186 expect(res.body.data).to.be.an('array') 156 expect(data[0].uuid).to.equal(created.uuid)
187 expect(res.body.data).to.have.lengthOf(1)
188 expect(res.body.data[0].uuid).to.equal(resVideo.body.video.uuid)
189 } 157 }
190 }) 158 })
191 159
@@ -193,32 +161,28 @@ describe('Test users with multiple servers', function () {
193 this.timeout(10_000) 161 this.timeout(10_000)
194 162
195 for (const server of servers) { 163 for (const server of servers) {
196 const resAccounts = await getAccountsList(server.url, '-createdAt') 164 const body = await server.accounts.list({ sort: '-createdAt' })
197 165
198 const accountDeleted = resAccounts.body.data.find(a => a.name === 'user1' && a.host === 'localhost:' + servers[0].port) as Account 166 const accountDeleted = body.data.find(a => a.name === 'user1' && a.host === 'localhost:' + servers[0].port)
199 expect(accountDeleted).not.to.be.undefined 167 expect(accountDeleted).not.to.be.undefined
200 168
201 const resVideoChannels = await getVideoChannelsList(server.url, 0, 10) 169 const { data } = await server.channels.list()
202 const videoChannelDeleted = resVideoChannels.body.data.find(a => { 170 const videoChannelDeleted = data.find(a => a.displayName === 'Main user1 channel' && a.host === 'localhost:' + servers[0].port)
203 return a.displayName === 'Main user1 channel' && a.host === 'localhost:' + servers[0].port
204 }) as VideoChannel
205 expect(videoChannelDeleted).not.to.be.undefined 171 expect(videoChannelDeleted).not.to.be.undefined
206 } 172 }
207 173
208 await removeUser(servers[0].url, userId, servers[0].accessToken) 174 await servers[0].users.remove({ userId })
209 175
210 await waitJobs(servers) 176 await waitJobs(servers)
211 177
212 for (const server of servers) { 178 for (const server of servers) {
213 const resAccounts = await getAccountsList(server.url, '-createdAt') 179 const body = await server.accounts.list({ sort: '-createdAt' })
214 180
215 const accountDeleted = resAccounts.body.data.find(a => a.name === 'user1' && a.host === 'localhost:' + servers[0].port) as Account 181 const accountDeleted = body.data.find(a => a.name === 'user1' && a.host === 'localhost:' + servers[0].port)
216 expect(accountDeleted).to.be.undefined 182 expect(accountDeleted).to.be.undefined
217 183
218 const resVideoChannels = await getVideoChannelsList(server.url, 0, 10) 184 const { data } = await server.channels.list()
219 const videoChannelDeleted = resVideoChannels.body.data.find(a => { 185 const videoChannelDeleted = data.find(a => a.name === 'Main user1 channel' && a.host === 'localhost:' + servers[0].port)
220 return a.name === 'Main user1 channel' && a.host === 'localhost:' + servers[0].port
221 }) as VideoChannel
222 expect(videoChannelDeleted).to.be.undefined 186 expect(videoChannelDeleted).to.be.undefined
223 } 187 }
224 }) 188 })
@@ -231,7 +195,7 @@ describe('Test users with multiple servers', function () {
231 195
232 it('Should not have video files', async () => { 196 it('Should not have video files', async () => {
233 for (const server of servers) { 197 for (const server of servers) {
234 await checkVideoFilesWereRemoved(videoUUID, server.internalServerNumber) 198 await checkVideoFilesWereRemoved(videoUUID, server)
235 } 199 }
236 }) 200 })
237 201
diff --git a/server/tests/api/users/users-verification.ts b/server/tests/api/users/users-verification.ts
index e0f2f2112..f54463359 100644
--- a/server/tests/api/users/users-verification.ts
+++ b/server/tests/api/users/users-verification.ts
@@ -1,30 +1,14 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2 2
3import * as chai from 'chai'
4import 'mocha' 3import 'mocha'
5import { 4import * as chai from 'chai'
6 cleanupTests, 5import { cleanupTests, createSingleServer, MockSmtpServer, PeerTubeServer, setAccessTokensToServers, waitJobs } from '@shared/extra-utils'
7 flushAndRunServer, 6import { HttpStatusCode } from '@shared/models'
8 getMyUserInformation,
9 getUserInformation,
10 login,
11 registerUser,
12 ServerInfo,
13 updateCustomSubConfig,
14 updateMyUser,
15 userLogin,
16 verifyEmail
17} from '../../../../shared/extra-utils'
18import { setAccessTokensToServers } from '../../../../shared/extra-utils/users/login'
19import { MockSmtpServer } from '../../../../shared/extra-utils/miscs/email'
20import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
21import { User } from '../../../../shared/models/users'
22import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
23 7
24const expect = chai.expect 8const expect = chai.expect
25 9
26describe('Test users account verification', function () { 10describe('Test users account verification', function () {
27 let server: ServerInfo 11 let server: PeerTubeServer
28 let userId: number 12 let userId: number
29 let userAccessToken: string 13 let userAccessToken: string
30 let verificationString: string 14 let verificationString: string
@@ -50,7 +34,7 @@ describe('Test users account verification', function () {
50 port 34 port
51 } 35 }
52 } 36 }
53 server = await flushAndRunServer(1, overrideConfig) 37 server = await createSingleServer(1, overrideConfig)
54 38
55 await setAccessTokensToServers([ server ]) 39 await setAccessTokensToServers([ server ])
56 }) 40 })
@@ -58,15 +42,17 @@ describe('Test users account verification', function () {
58 it('Should register user and send verification email if verification required', async function () { 42 it('Should register user and send verification email if verification required', async function () {
59 this.timeout(30000) 43 this.timeout(30000)
60 44
61 await updateCustomSubConfig(server.url, server.accessToken, { 45 await server.config.updateCustomSubConfig({
62 signup: { 46 newConfig: {
63 enabled: true, 47 signup: {
64 requiresEmailVerification: true, 48 enabled: true,
65 limit: 10 49 requiresEmailVerification: true,
50 limit: 10
51 }
66 } 52 }
67 }) 53 })
68 54
69 await registerUser(server.url, user1.username, user1.password) 55 await server.users.register(user1)
70 56
71 await waitJobs(server) 57 await waitJobs(server)
72 expectedEmailsLength++ 58 expectedEmailsLength++
@@ -85,23 +71,23 @@ describe('Test users account verification', function () {
85 71
86 userId = parseInt(userIdMatches[1], 10) 72 userId = parseInt(userIdMatches[1], 10)
87 73
88 const resUserInfo = await getUserInformation(server.url, server.accessToken, userId) 74 const body = await server.users.get({ userId })
89 expect(resUserInfo.body.emailVerified).to.be.false 75 expect(body.emailVerified).to.be.false
90 }) 76 })
91 77
92 it('Should not allow login for user with unverified email', async function () { 78 it('Should not allow login for user with unverified email', async function () {
93 const resLogin = await login(server.url, server.client, user1, HttpStatusCode.BAD_REQUEST_400) 79 const { detail } = await server.login.login({ user: user1, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
94 expect(resLogin.body.detail).to.contain('User email is not verified.') 80 expect(detail).to.contain('User email is not verified.')
95 }) 81 })
96 82
97 it('Should verify the user via email and allow login', async function () { 83 it('Should verify the user via email and allow login', async function () {
98 await verifyEmail(server.url, userId, verificationString) 84 await server.users.verifyEmail({ userId, verificationString })
99 85
100 const res = await login(server.url, server.client, user1) 86 const body = await server.login.login({ user: user1 })
101 userAccessToken = res.body.access_token 87 userAccessToken = body.access_token
102 88
103 const resUserVerified = await getUserInformation(server.url, server.accessToken, userId) 89 const user = await server.users.get({ userId })
104 expect(resUserVerified.body.emailVerified).to.be.true 90 expect(user.emailVerified).to.be.true
105 }) 91 })
106 92
107 it('Should be able to change the user email', async function () { 93 it('Should be able to change the user email', async function () {
@@ -110,9 +96,8 @@ describe('Test users account verification', function () {
110 let updateVerificationString: string 96 let updateVerificationString: string
111 97
112 { 98 {
113 await updateMyUser({ 99 await server.users.updateMe({
114 url: server.url, 100 token: userAccessToken,
115 accessToken: userAccessToken,
116 email: 'updated@example.com', 101 email: 'updated@example.com',
117 currentPassword: user1.password 102 currentPassword: user1.password
118 }) 103 })
@@ -128,19 +113,15 @@ describe('Test users account verification', function () {
128 } 113 }
129 114
130 { 115 {
131 const res = await getMyUserInformation(server.url, userAccessToken) 116 const me = await server.users.getMyInfo({ token: userAccessToken })
132 const me: User = res.body
133
134 expect(me.email).to.equal('user_1@example.com') 117 expect(me.email).to.equal('user_1@example.com')
135 expect(me.pendingEmail).to.equal('updated@example.com') 118 expect(me.pendingEmail).to.equal('updated@example.com')
136 } 119 }
137 120
138 { 121 {
139 await verifyEmail(server.url, userId, updateVerificationString, true) 122 await server.users.verifyEmail({ userId, verificationString: updateVerificationString, isPendingEmail: true })
140
141 const res = await getMyUserInformation(server.url, userAccessToken)
142 const me: User = res.body
143 123
124 const me = await server.users.getMyInfo({ token: userAccessToken })
144 expect(me.email).to.equal('updated@example.com') 125 expect(me.email).to.equal('updated@example.com')
145 expect(me.pendingEmail).to.be.null 126 expect(me.pendingEmail).to.be.null
146 } 127 }
@@ -148,35 +129,39 @@ describe('Test users account verification', function () {
148 129
149 it('Should register user not requiring email verification if setting not enabled', async function () { 130 it('Should register user not requiring email verification if setting not enabled', async function () {
150 this.timeout(5000) 131 this.timeout(5000)
151 await updateCustomSubConfig(server.url, server.accessToken, { 132 await server.config.updateCustomSubConfig({
152 signup: { 133 newConfig: {
153 enabled: true, 134 signup: {
154 requiresEmailVerification: false, 135 enabled: true,
155 limit: 10 136 requiresEmailVerification: false,
137 limit: 10
138 }
156 } 139 }
157 }) 140 })
158 141
159 await registerUser(server.url, user2.username, user2.password) 142 await server.users.register(user2)
160 143
161 await waitJobs(server) 144 await waitJobs(server)
162 expect(emails).to.have.lengthOf(expectedEmailsLength) 145 expect(emails).to.have.lengthOf(expectedEmailsLength)
163 146
164 const accessToken = await userLogin(server, user2) 147 const accessToken = await server.login.getAccessToken(user2)
165 148
166 const resMyUserInfo = await getMyUserInformation(server.url, accessToken) 149 const user = await server.users.getMyInfo({ token: accessToken })
167 expect(resMyUserInfo.body.emailVerified).to.be.null 150 expect(user.emailVerified).to.be.null
168 }) 151 })
169 152
170 it('Should allow login for user with unverified email when setting later enabled', async function () { 153 it('Should allow login for user with unverified email when setting later enabled', async function () {
171 await updateCustomSubConfig(server.url, server.accessToken, { 154 await server.config.updateCustomSubConfig({
172 signup: { 155 newConfig: {
173 enabled: true, 156 signup: {
174 requiresEmailVerification: true, 157 enabled: true,
175 limit: 10 158 requiresEmailVerification: true,
159 limit: 10
160 }
176 } 161 }
177 }) 162 })
178 163
179 await userLogin(server, user2) 164 await server.login.getAccessToken(user2)
180 }) 165 })
181 166
182 after(async function () { 167 after(async function () {
diff --git a/server/tests/api/users/users.ts b/server/tests/api/users/users.ts
index 87ba775f6..1419ae820 100644
--- a/server/tests/api/users/users.ts
+++ b/server/tests/api/users/users.ts
@@ -2,63 +2,24 @@
2 2
3import 'mocha' 3import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5import { AbuseState, AbuseUpdate, MyUser, User, UserRole, Video, VideoPlaylistType } from '@shared/models'
6import { CustomConfig, OAuth2ErrorCode } from '@shared/models/server'
7import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
8import { 5import {
9 addVideoCommentThread,
10 blockUser,
11 cleanupTests, 6 cleanupTests,
12 closeAllSequelize, 7 createSingleServer,
13 createUser,
14 deleteMe,
15 flushAndRunServer,
16 getAccountRatings,
17 getAdminAbusesList,
18 getBlacklistedVideosList,
19 getCustomConfig,
20 getMyUserInformation,
21 getMyUserVideoQuotaUsed,
22 getMyUserVideoRating,
23 getUserInformation,
24 getUsersList,
25 getUsersListPaginationAndSort,
26 getVideoChannel,
27 getVideosList,
28 installPlugin,
29 killallServers, 8 killallServers,
30 login,
31 makePutBodyRequest, 9 makePutBodyRequest,
32 rateVideo, 10 PeerTubeServer,
33 registerUserWithChannel, 11 setAccessTokensToServers,
34 removeUser,
35 removeVideo,
36 reportAbuse,
37 reRunServer,
38 ServerInfo,
39 setTokenField,
40 testImage, 12 testImage,
41 unblockUser,
42 updateAbuse,
43 updateCustomSubConfig,
44 updateMyAvatar,
45 updateMyUser,
46 updateUser,
47 uploadVideo,
48 userLogin,
49 waitJobs 13 waitJobs
50} from '../../../../shared/extra-utils' 14} from '@shared/extra-utils'
51import { follow } from '../../../../shared/extra-utils/server/follows' 15import { AbuseState, HttpStatusCode, OAuth2ErrorCode, UserAdminFlag, UserRole, Video, VideoPlaylistType } from '@shared/models'
52import { logout, refreshToken, setAccessTokensToServers } from '../../../../shared/extra-utils/users/login'
53import { getMyVideos } from '../../../../shared/extra-utils/videos/videos'
54import { UserAdminFlag } from '../../../../shared/models/users/user-flag.model'
55 16
56const expect = chai.expect 17const expect = chai.expect
57 18
58describe('Test users', function () { 19describe('Test users', function () {
59 let server: ServerInfo 20 let server: PeerTubeServer
60 let accessToken: string 21 let token: string
61 let accessTokenUser: string 22 let userToken: string
62 let videoId: number 23 let videoId: number
63 let userId: number 24 let userId: number
64 const user = { 25 const user = {
@@ -69,7 +30,7 @@ describe('Test users', function () {
69 before(async function () { 30 before(async function () {
70 this.timeout(30000) 31 this.timeout(30000)
71 32
72 server = await flushAndRunServer(1, { 33 server = await createSingleServer(1, {
73 rates_limit: { 34 rates_limit: {
74 login: { 35 login: {
75 max: 30 36 max: 30
@@ -79,7 +40,7 @@ describe('Test users', function () {
79 40
80 await setAccessTokensToServers([ server ]) 41 await setAccessTokensToServers([ server ])
81 42
82 await installPlugin({ url: server.url, accessToken: server.accessToken, npmName: 'peertube-theme-background-red' }) 43 await server.plugins.install({ npmName: 'peertube-theme-background-red' })
83 }) 44 })
84 45
85 describe('OAuth client', function () { 46 describe('OAuth client', function () {
@@ -90,158 +51,156 @@ describe('Test users', function () {
90 it('Should remove the last client') 51 it('Should remove the last client')
91 52
92 it('Should not login with an invalid client id', async function () { 53 it('Should not login with an invalid client id', async function () {
93 const client = { id: 'client', secret: server.client.secret } 54 const client = { id: 'client', secret: server.store.client.secret }
94 const res = await login(server.url, client, server.user, HttpStatusCode.BAD_REQUEST_400) 55 const body = await server.login.login({ client, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
95 56
96 expect(res.body.code).to.equal(OAuth2ErrorCode.INVALID_CLIENT) 57 expect(body.code).to.equal(OAuth2ErrorCode.INVALID_CLIENT)
97 expect(res.body.error).to.contain('client is invalid') 58 expect(body.error).to.contain('client is invalid')
98 expect(res.body.type.startsWith('https://')).to.be.true 59 expect(body.type.startsWith('https://')).to.be.true
99 expect(res.body.type).to.contain(OAuth2ErrorCode.INVALID_CLIENT) 60 expect(body.type).to.contain(OAuth2ErrorCode.INVALID_CLIENT)
100 }) 61 })
101 62
102 it('Should not login with an invalid client secret', async function () { 63 it('Should not login with an invalid client secret', async function () {
103 const client = { id: server.client.id, secret: 'coucou' } 64 const client = { id: server.store.client.id, secret: 'coucou' }
104 const res = await login(server.url, client, server.user, HttpStatusCode.BAD_REQUEST_400) 65 const body = await server.login.login({ client, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
105 66
106 expect(res.body.code).to.equal(OAuth2ErrorCode.INVALID_CLIENT) 67 expect(body.code).to.equal(OAuth2ErrorCode.INVALID_CLIENT)
107 expect(res.body.error).to.contain('client is invalid') 68 expect(body.error).to.contain('client is invalid')
108 expect(res.body.type.startsWith('https://')).to.be.true 69 expect(body.type.startsWith('https://')).to.be.true
109 expect(res.body.type).to.contain(OAuth2ErrorCode.INVALID_CLIENT) 70 expect(body.type).to.contain(OAuth2ErrorCode.INVALID_CLIENT)
110 }) 71 })
111 }) 72 })
112 73
113 describe('Login', function () { 74 describe('Login', function () {
114 75
115 it('Should not login with an invalid username', async function () { 76 it('Should not login with an invalid username', async function () {
116 const user = { username: 'captain crochet', password: server.user.password } 77 const user = { username: 'captain crochet', password: server.store.user.password }
117 const res = await login(server.url, server.client, user, HttpStatusCode.BAD_REQUEST_400) 78 const body = await server.login.login({ user, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
118 79
119 expect(res.body.code).to.equal(OAuth2ErrorCode.INVALID_GRANT) 80 expect(body.code).to.equal(OAuth2ErrorCode.INVALID_GRANT)
120 expect(res.body.error).to.contain('credentials are invalid') 81 expect(body.error).to.contain('credentials are invalid')
121 expect(res.body.type.startsWith('https://')).to.be.true 82 expect(body.type.startsWith('https://')).to.be.true
122 expect(res.body.type).to.contain(OAuth2ErrorCode.INVALID_GRANT) 83 expect(body.type).to.contain(OAuth2ErrorCode.INVALID_GRANT)
123 }) 84 })
124 85
125 it('Should not login with an invalid password', async function () { 86 it('Should not login with an invalid password', async function () {
126 const user = { username: server.user.username, password: 'mew_three' } 87 const user = { username: server.store.user.username, password: 'mew_three' }
127 const res = await login(server.url, server.client, user, HttpStatusCode.BAD_REQUEST_400) 88 const body = await server.login.login({ user, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
128 89
129 expect(res.body.code).to.equal(OAuth2ErrorCode.INVALID_GRANT) 90 expect(body.code).to.equal(OAuth2ErrorCode.INVALID_GRANT)
130 expect(res.body.error).to.contain('credentials are invalid') 91 expect(body.error).to.contain('credentials are invalid')
131 expect(res.body.type.startsWith('https://')).to.be.true 92 expect(body.type.startsWith('https://')).to.be.true
132 expect(res.body.type).to.contain(OAuth2ErrorCode.INVALID_GRANT) 93 expect(body.type).to.contain(OAuth2ErrorCode.INVALID_GRANT)
133 }) 94 })
134 95
135 it('Should not be able to upload a video', async function () { 96 it('Should not be able to upload a video', async function () {
136 accessToken = 'my_super_token' 97 token = 'my_super_token'
137 98
138 const videoAttributes = {} 99 await server.videos.upload({ token, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
139 await uploadVideo(server.url, accessToken, videoAttributes, HttpStatusCode.UNAUTHORIZED_401)
140 }) 100 })
141 101
142 it('Should not be able to follow', async function () { 102 it('Should not be able to follow', async function () {
143 accessToken = 'my_super_token' 103 token = 'my_super_token'
144 await follow(server.url, [ 'http://example.com' ], accessToken, HttpStatusCode.UNAUTHORIZED_401) 104
105 await server.follows.follow({
106 hosts: [ 'http://example.com' ],
107 token,
108 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
109 })
145 }) 110 })
146 111
147 it('Should not be able to unfollow') 112 it('Should not be able to unfollow')
148 113
149 it('Should be able to login', async function () { 114 it('Should be able to login', async function () {
150 const res = await login(server.url, server.client, server.user, HttpStatusCode.OK_200) 115 const body = await server.login.login({ expectedStatus: HttpStatusCode.OK_200 })
151 116
152 accessToken = res.body.access_token 117 token = body.access_token
153 }) 118 })
154 119
155 it('Should be able to login with an insensitive username', async function () { 120 it('Should be able to login with an insensitive username', async function () {
156 const user = { username: 'RoOt', password: server.user.password } 121 const user = { username: 'RoOt', password: server.store.user.password }
157 await login(server.url, server.client, user, HttpStatusCode.OK_200) 122 await server.login.login({ user, expectedStatus: HttpStatusCode.OK_200 })
158 123
159 const user2 = { username: 'rOoT', password: server.user.password } 124 const user2 = { username: 'rOoT', password: server.store.user.password }
160 await login(server.url, server.client, user2, HttpStatusCode.OK_200) 125 await server.login.login({ user: user2, expectedStatus: HttpStatusCode.OK_200 })
161 126
162 const user3 = { username: 'ROOt', password: server.user.password } 127 const user3 = { username: 'ROOt', password: server.store.user.password }
163 await login(server.url, server.client, user3, HttpStatusCode.OK_200) 128 await server.login.login({ user: user3, expectedStatus: HttpStatusCode.OK_200 })
164 }) 129 })
165 }) 130 })
166 131
167 describe('Upload', function () { 132 describe('Upload', function () {
168 133
169 it('Should upload the video with the correct token', async function () { 134 it('Should upload the video with the correct token', async function () {
170 const videoAttributes = {} 135 await server.videos.upload({ token })
171 await uploadVideo(server.url, accessToken, videoAttributes) 136 const { data } = await server.videos.list()
172 const res = await getVideosList(server.url) 137 const video = data[0]
173 const video = res.body.data[0]
174 138
175 expect(video.account.name).to.equal('root') 139 expect(video.account.name).to.equal('root')
176 videoId = video.id 140 videoId = video.id
177 }) 141 })
178 142
179 it('Should upload the video again with the correct token', async function () { 143 it('Should upload the video again with the correct token', async function () {
180 const videoAttributes = {} 144 await server.videos.upload({ token })
181 await uploadVideo(server.url, accessToken, videoAttributes)
182 }) 145 })
183 }) 146 })
184 147
185 describe('Ratings', function () { 148 describe('Ratings', function () {
186 149
187 it('Should retrieve a video rating', async function () { 150 it('Should retrieve a video rating', async function () {
188 await rateVideo(server.url, accessToken, videoId, 'like') 151 await server.videos.rate({ id: videoId, rating: 'like' })
189 const res = await getMyUserVideoRating(server.url, accessToken, videoId) 152 const rating = await server.users.getMyRating({ token, videoId })
190 const rating = res.body
191 153
192 expect(rating.videoId).to.equal(videoId) 154 expect(rating.videoId).to.equal(videoId)
193 expect(rating.rating).to.equal('like') 155 expect(rating.rating).to.equal('like')
194 }) 156 })
195 157
196 it('Should retrieve ratings list', async function () { 158 it('Should retrieve ratings list', async function () {
197 await rateVideo(server.url, accessToken, videoId, 'like') 159 await server.videos.rate({ id: videoId, rating: 'like' })
198 160
199 const res = await getAccountRatings(server.url, server.user.username, server.accessToken, null, HttpStatusCode.OK_200) 161 const body = await server.accounts.listRatings({ accountName: server.store.user.username })
200 const ratings = res.body
201 162
202 expect(ratings.total).to.equal(1) 163 expect(body.total).to.equal(1)
203 expect(ratings.data[0].video.id).to.equal(videoId) 164 expect(body.data[0].video.id).to.equal(videoId)
204 expect(ratings.data[0].rating).to.equal('like') 165 expect(body.data[0].rating).to.equal('like')
205 }) 166 })
206 167
207 it('Should retrieve ratings list by rating type', async function () { 168 it('Should retrieve ratings list by rating type', async function () {
208 { 169 {
209 const res = await getAccountRatings(server.url, server.user.username, server.accessToken, 'like') 170 const body = await server.accounts.listRatings({ accountName: server.store.user.username, rating: 'like' })
210 const ratings = res.body 171 expect(body.data.length).to.equal(1)
211 expect(ratings.data.length).to.equal(1)
212 } 172 }
213 173
214 { 174 {
215 const res = await getAccountRatings(server.url, server.user.username, server.accessToken, 'dislike') 175 const body = await server.accounts.listRatings({ accountName: server.store.user.username, rating: 'dislike' })
216 const ratings = res.body 176 expect(body.data.length).to.equal(0)
217 expect(ratings.data.length).to.equal(0)
218 } 177 }
219 }) 178 })
220 }) 179 })
221 180
222 describe('Remove video', function () { 181 describe('Remove video', function () {
223 it('Should not be able to remove the video with an incorrect token', async function () { 182 it('Should not be able to remove the video with an incorrect token', async function () {
224 await removeVideo(server.url, 'bad_token', videoId, HttpStatusCode.UNAUTHORIZED_401) 183 await server.videos.remove({ token: 'bad_token', id: videoId, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
225 }) 184 })
226 185
227 it('Should not be able to remove the video with the token of another account') 186 it('Should not be able to remove the video with the token of another account')
228 187
229 it('Should be able to remove the video with the correct token', async function () { 188 it('Should be able to remove the video with the correct token', async function () {
230 await removeVideo(server.url, accessToken, videoId) 189 await server.videos.remove({ token, id: videoId })
231 }) 190 })
232 }) 191 })
233 192
234 describe('Logout', function () { 193 describe('Logout', function () {
235 it('Should logout (revoke token)', async function () { 194 it('Should logout (revoke token)', async function () {
236 await logout(server.url, server.accessToken) 195 await server.login.logout({ token: server.accessToken })
237 }) 196 })
238 197
239 it('Should not be able to get the user information', async function () { 198 it('Should not be able to get the user information', async function () {
240 await getMyUserInformation(server.url, server.accessToken, HttpStatusCode.UNAUTHORIZED_401) 199 await server.users.getMyInfo({ expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
241 }) 200 })
242 201
243 it('Should not be able to upload a video', async function () { 202 it('Should not be able to upload a video', async function () {
244 await uploadVideo(server.url, server.accessToken, { name: 'video' }, HttpStatusCode.UNAUTHORIZED_401) 203 await server.videos.upload({ attributes: { name: 'video' }, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
245 }) 204 })
246 205
247 it('Should not be able to rate a video', async function () { 206 it('Should not be able to rate a video', async function () {
@@ -255,79 +214,70 @@ describe('Test users', function () {
255 path: path + videoId, 214 path: path + videoId,
256 token: 'wrong token', 215 token: 'wrong token',
257 fields: data, 216 fields: data,
258 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 217 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
259 } 218 }
260 await makePutBodyRequest(options) 219 await makePutBodyRequest(options)
261 }) 220 })
262 221
263 it('Should be able to login again', async function () { 222 it('Should be able to login again', async function () {
264 const res = await login(server.url, server.client, server.user) 223 const body = await server.login.login()
265 server.accessToken = res.body.access_token 224 server.accessToken = body.access_token
266 server.refreshToken = res.body.refresh_token 225 server.refreshToken = body.refresh_token
267 }) 226 })
268 227
269 it('Should be able to get my user information again', async function () { 228 it('Should be able to get my user information again', async function () {
270 await getMyUserInformation(server.url, server.accessToken) 229 await server.users.getMyInfo()
271 }) 230 })
272 231
273 it('Should have an expired access token', async function () { 232 it('Should have an expired access token', async function () {
274 this.timeout(15000) 233 this.timeout(15000)
275 234
276 await setTokenField(server.internalServerNumber, server.accessToken, 'accessTokenExpiresAt', new Date().toISOString()) 235 await server.sql.setTokenField(server.accessToken, 'accessTokenExpiresAt', new Date().toISOString())
277 await setTokenField(server.internalServerNumber, server.accessToken, 'refreshTokenExpiresAt', new Date().toISOString()) 236 await server.sql.setTokenField(server.accessToken, 'refreshTokenExpiresAt', new Date().toISOString())
278 237
279 killallServers([ server ]) 238 await killallServers([ server ])
280 await reRunServer(server) 239 await server.run()
281 240
282 await getMyUserInformation(server.url, server.accessToken, 401) 241 await server.users.getMyInfo({ expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
283 }) 242 })
284 243
285 it('Should not be able to refresh an access token with an expired refresh token', async function () { 244 it('Should not be able to refresh an access token with an expired refresh token', async function () {
286 await refreshToken(server, server.refreshToken, 400) 245 await server.login.refreshToken({ refreshToken: server.refreshToken, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
287 }) 246 })
288 247
289 it('Should refresh the token', async function () { 248 it('Should refresh the token', async function () {
290 this.timeout(15000) 249 this.timeout(15000)
291 250
292 const futureDate = new Date(new Date().getTime() + 1000 * 60).toISOString() 251 const futureDate = new Date(new Date().getTime() + 1000 * 60).toISOString()
293 await setTokenField(server.internalServerNumber, server.accessToken, 'refreshTokenExpiresAt', futureDate) 252 await server.sql.setTokenField(server.accessToken, 'refreshTokenExpiresAt', futureDate)
294 253
295 killallServers([ server ]) 254 await killallServers([ server ])
296 await reRunServer(server) 255 await server.run()
297 256
298 const res = await refreshToken(server, server.refreshToken) 257 const res = await server.login.refreshToken({ refreshToken: server.refreshToken })
299 server.accessToken = res.body.access_token 258 server.accessToken = res.body.access_token
300 server.refreshToken = res.body.refresh_token 259 server.refreshToken = res.body.refresh_token
301 }) 260 })
302 261
303 it('Should be able to get my user information again', async function () { 262 it('Should be able to get my user information again', async function () {
304 await getMyUserInformation(server.url, server.accessToken) 263 await server.users.getMyInfo()
305 }) 264 })
306 }) 265 })
307 266
308 describe('Creating a user', function () { 267 describe('Creating a user', function () {
309 268
310 it('Should be able to create a new user', async function () { 269 it('Should be able to create a new user', async function () {
311 await createUser({ 270 await server.users.create({ ...user, videoQuota: 2 * 1024 * 1024, adminFlags: UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST })
312 url: server.url,
313 accessToken: accessToken,
314 username: user.username,
315 password: user.password,
316 videoQuota: 2 * 1024 * 1024,
317 adminFlags: UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST
318 })
319 }) 271 })
320 272
321 it('Should be able to login with this user', async function () { 273 it('Should be able to login with this user', async function () {
322 accessTokenUser = await userLogin(server, user) 274 userToken = await server.login.getAccessToken(user)
323 }) 275 })
324 276
325 it('Should be able to get user information', async function () { 277 it('Should be able to get user information', async function () {
326 const res1 = await getMyUserInformation(server.url, accessTokenUser) 278 const userMe = await server.users.getMyInfo({ token: userToken })
327 const userMe: MyUser = res1.body
328 279
329 const res2 = await getUserInformation(server.url, server.accessToken, userMe.id, true) 280 const userGet = await server.users.get({ userId: userMe.id, withStats: true })
330 const userGet: User = res2.body
331 281
332 for (const user of [ userMe, userGet ]) { 282 for (const user of [ userMe, userGet ]) {
333 expect(user.username).to.equal('user_1') 283 expect(user.username).to.equal('user_1')
@@ -363,34 +313,28 @@ describe('Test users', function () {
363 it('Should be able to upload a video with this user', async function () { 313 it('Should be able to upload a video with this user', async function () {
364 this.timeout(10000) 314 this.timeout(10000)
365 315
366 const videoAttributes = { 316 const attributes = {
367 name: 'super user video', 317 name: 'super user video',
368 fixture: 'video_short.webm' 318 fixture: 'video_short.webm'
369 } 319 }
370 await uploadVideo(server.url, accessTokenUser, videoAttributes) 320 await server.videos.upload({ token: userToken, attributes })
371 }) 321 })
372 322
373 it('Should have video quota updated', async function () { 323 it('Should have video quota updated', async function () {
374 const res = await getMyUserVideoQuotaUsed(server.url, accessTokenUser) 324 const quota = await server.users.getMyQuotaUsed({ token: userToken })
375 const data = res.body 325 expect(quota.videoQuotaUsed).to.equal(218910)
376
377 expect(data.videoQuotaUsed).to.equal(218910)
378
379 const resUsers = await getUsersList(server.url, server.accessToken)
380 326
381 const users: User[] = resUsers.body.data 327 const { data } = await server.users.list()
382 const tmpUser = users.find(u => u.username === user.username) 328 const tmpUser = data.find(u => u.username === user.username)
383 expect(tmpUser.videoQuotaUsed).to.equal(218910) 329 expect(tmpUser.videoQuotaUsed).to.equal(218910)
384 }) 330 })
385 331
386 it('Should be able to list my videos', async function () { 332 it('Should be able to list my videos', async function () {
387 const res = await getMyVideos(server.url, accessTokenUser, 0, 5) 333 const { total, data } = await server.videos.listMyVideos({ token: userToken })
388 expect(res.body.total).to.equal(1) 334 expect(total).to.equal(1)
335 expect(data).to.have.lengthOf(1)
389 336
390 const videos = res.body.data 337 const video: Video = data[0]
391 expect(videos).to.have.lengthOf(1)
392
393 const video: Video = videos[0]
394 expect(video.name).to.equal('super user video') 338 expect(video.name).to.equal('super user video')
395 expect(video.thumbnailPath).to.not.be.null 339 expect(video.thumbnailPath).to.not.be.null
396 expect(video.previewPath).to.not.be.null 340 expect(video.previewPath).to.not.be.null
@@ -398,19 +342,15 @@ describe('Test users', function () {
398 342
399 it('Should be able to search in my videos', async function () { 343 it('Should be able to search in my videos', async function () {
400 { 344 {
401 const res = await getMyVideos(server.url, accessTokenUser, 0, 5, '-createdAt', 'user video') 345 const { total, data } = await server.videos.listMyVideos({ token: userToken, sort: '-createdAt', search: 'user video' })
402 expect(res.body.total).to.equal(1) 346 expect(total).to.equal(1)
403 347 expect(data).to.have.lengthOf(1)
404 const videos = res.body.data
405 expect(videos).to.have.lengthOf(1)
406 } 348 }
407 349
408 { 350 {
409 const res = await getMyVideos(server.url, accessTokenUser, 0, 5, '-createdAt', 'toto') 351 const { total, data } = await server.videos.listMyVideos({ token: userToken, sort: '-createdAt', search: 'toto' })
410 expect(res.body.total).to.equal(0) 352 expect(total).to.equal(0)
411 353 expect(data).to.have.lengthOf(0)
412 const videos = res.body.data
413 expect(videos).to.have.lengthOf(0)
414 } 354 }
415 }) 355 })
416 356
@@ -418,28 +358,25 @@ describe('Test users', function () {
418 this.timeout(60000) 358 this.timeout(60000)
419 359
420 { 360 {
421 const res = await getCustomConfig(server.url, server.accessToken) 361 const config = await server.config.getCustomConfig()
422 const config = res.body as CustomConfig
423 config.transcoding.webtorrent.enabled = false 362 config.transcoding.webtorrent.enabled = false
424 config.transcoding.hls.enabled = true 363 config.transcoding.hls.enabled = true
425 config.transcoding.enabled = true 364 config.transcoding.enabled = true
426 await updateCustomSubConfig(server.url, server.accessToken, config) 365 await server.config.updateCustomSubConfig({ newConfig: config })
427 } 366 }
428 367
429 { 368 {
430 const videoAttributes = { 369 const attributes = {
431 name: 'super user video 2', 370 name: 'super user video 2',
432 fixture: 'video_short.webm' 371 fixture: 'video_short.webm'
433 } 372 }
434 await uploadVideo(server.url, accessTokenUser, videoAttributes) 373 await server.videos.upload({ token: userToken, attributes })
435 374
436 await waitJobs([ server ]) 375 await waitJobs([ server ])
437 } 376 }
438 377
439 { 378 {
440 const res = await getMyUserVideoQuotaUsed(server.url, accessTokenUser) 379 const data = await server.users.getMyQuotaUsed({ token: userToken })
441 const data = res.body
442
443 expect(data.videoQuotaUsed).to.be.greaterThan(220000) 380 expect(data.videoQuotaUsed).to.be.greaterThan(220000)
444 } 381 }
445 }) 382 })
@@ -448,21 +385,18 @@ describe('Test users', function () {
448 describe('Users listing', function () { 385 describe('Users listing', function () {
449 386
450 it('Should list all the users', async function () { 387 it('Should list all the users', async function () {
451 const res = await getUsersList(server.url, server.accessToken) 388 const { data, total } = await server.users.list()
452 const result = res.body
453 const total = result.total
454 const users = result.data
455 389
456 expect(total).to.equal(2) 390 expect(total).to.equal(2)
457 expect(users).to.be.an('array') 391 expect(data).to.be.an('array')
458 expect(users.length).to.equal(2) 392 expect(data.length).to.equal(2)
459 393
460 const user = users[0] 394 const user = data[0]
461 expect(user.username).to.equal('user_1') 395 expect(user.username).to.equal('user_1')
462 expect(user.email).to.equal('user_1@example.com') 396 expect(user.email).to.equal('user_1@example.com')
463 expect(user.nsfwPolicy).to.equal('display') 397 expect(user.nsfwPolicy).to.equal('display')
464 398
465 const rootUser = users[1] 399 const rootUser = data[1]
466 expect(rootUser.username).to.equal('root') 400 expect(rootUser.username).to.equal('root')
467 expect(rootUser.email).to.equal('admin' + server.internalServerNumber + '@example.com') 401 expect(rootUser.email).to.equal('admin' + server.internalServerNumber + '@example.com')
468 expect(user.nsfwPolicy).to.equal('display') 402 expect(user.nsfwPolicy).to.equal('display')
@@ -474,16 +408,12 @@ describe('Test users', function () {
474 }) 408 })
475 409
476 it('Should list only the first user by username asc', async function () { 410 it('Should list only the first user by username asc', async function () {
477 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, 'username') 411 const { total, data } = await server.users.list({ start: 0, count: 1, sort: 'username' })
478
479 const result = res.body
480 const total = result.total
481 const users = result.data
482 412
483 expect(total).to.equal(2) 413 expect(total).to.equal(2)
484 expect(users.length).to.equal(1) 414 expect(data.length).to.equal(1)
485 415
486 const user = users[0] 416 const user = data[0]
487 expect(user.username).to.equal('root') 417 expect(user.username).to.equal('root')
488 expect(user.email).to.equal('admin' + server.internalServerNumber + '@example.com') 418 expect(user.email).to.equal('admin' + server.internalServerNumber + '@example.com')
489 expect(user.roleLabel).to.equal('Administrator') 419 expect(user.roleLabel).to.equal('Administrator')
@@ -491,111 +421,90 @@ describe('Test users', function () {
491 }) 421 })
492 422
493 it('Should list only the first user by username desc', async function () { 423 it('Should list only the first user by username desc', async function () {
494 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, '-username') 424 const { total, data } = await server.users.list({ start: 0, count: 1, sort: '-username' })
495 const result = res.body
496 const total = result.total
497 const users = result.data
498 425
499 expect(total).to.equal(2) 426 expect(total).to.equal(2)
500 expect(users.length).to.equal(1) 427 expect(data.length).to.equal(1)
501 428
502 const user = users[0] 429 const user = data[0]
503 expect(user.username).to.equal('user_1') 430 expect(user.username).to.equal('user_1')
504 expect(user.email).to.equal('user_1@example.com') 431 expect(user.email).to.equal('user_1@example.com')
505 expect(user.nsfwPolicy).to.equal('display') 432 expect(user.nsfwPolicy).to.equal('display')
506 }) 433 })
507 434
508 it('Should list only the second user by createdAt desc', async function () { 435 it('Should list only the second user by createdAt desc', async function () {
509 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, '-createdAt') 436 const { data, total } = await server.users.list({ start: 0, count: 1, sort: '-createdAt' })
510 const result = res.body
511 const total = result.total
512 const users = result.data
513
514 expect(total).to.equal(2) 437 expect(total).to.equal(2)
515 expect(users.length).to.equal(1)
516 438
517 const user = users[0] 439 expect(data.length).to.equal(1)
440
441 const user = data[0]
518 expect(user.username).to.equal('user_1') 442 expect(user.username).to.equal('user_1')
519 expect(user.email).to.equal('user_1@example.com') 443 expect(user.email).to.equal('user_1@example.com')
520 expect(user.nsfwPolicy).to.equal('display') 444 expect(user.nsfwPolicy).to.equal('display')
521 }) 445 })
522 446
523 it('Should list all the users by createdAt asc', async function () { 447 it('Should list all the users by createdAt asc', async function () {
524 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt') 448 const { data, total } = await server.users.list({ start: 0, count: 2, sort: 'createdAt' })
525 const result = res.body
526 const total = result.total
527 const users = result.data
528 449
529 expect(total).to.equal(2) 450 expect(total).to.equal(2)
530 expect(users.length).to.equal(2) 451 expect(data.length).to.equal(2)
531 452
532 expect(users[0].username).to.equal('root') 453 expect(data[0].username).to.equal('root')
533 expect(users[0].email).to.equal('admin' + server.internalServerNumber + '@example.com') 454 expect(data[0].email).to.equal('admin' + server.internalServerNumber + '@example.com')
534 expect(users[0].nsfwPolicy).to.equal('display') 455 expect(data[0].nsfwPolicy).to.equal('display')
535 456
536 expect(users[1].username).to.equal('user_1') 457 expect(data[1].username).to.equal('user_1')
537 expect(users[1].email).to.equal('user_1@example.com') 458 expect(data[1].email).to.equal('user_1@example.com')
538 expect(users[1].nsfwPolicy).to.equal('display') 459 expect(data[1].nsfwPolicy).to.equal('display')
539 }) 460 })
540 461
541 it('Should search user by username', async function () { 462 it('Should search user by username', async function () {
542 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt', 'oot') 463 const { data, total } = await server.users.list({ start: 0, count: 2, sort: 'createdAt', search: 'oot' })
543 const users = res.body.data as User[] 464 expect(total).to.equal(1)
544 465 expect(data.length).to.equal(1)
545 expect(res.body.total).to.equal(1) 466 expect(data[0].username).to.equal('root')
546 expect(users.length).to.equal(1)
547
548 expect(users[0].username).to.equal('root')
549 }) 467 })
550 468
551 it('Should search user by email', async function () { 469 it('Should search user by email', async function () {
552 { 470 {
553 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt', 'r_1@exam') 471 const { total, data } = await server.users.list({ start: 0, count: 2, sort: 'createdAt', search: 'r_1@exam' })
554 const users = res.body.data as User[] 472 expect(total).to.equal(1)
555 473 expect(data.length).to.equal(1)
556 expect(res.body.total).to.equal(1) 474 expect(data[0].username).to.equal('user_1')
557 expect(users.length).to.equal(1) 475 expect(data[0].email).to.equal('user_1@example.com')
558
559 expect(users[0].username).to.equal('user_1')
560 expect(users[0].email).to.equal('user_1@example.com')
561 } 476 }
562 477
563 { 478 {
564 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt', 'example') 479 const { total, data } = await server.users.list({ start: 0, count: 2, sort: 'createdAt', search: 'example' })
565 const users = res.body.data as User[] 480 expect(total).to.equal(2)
566 481 expect(data.length).to.equal(2)
567 expect(res.body.total).to.equal(2) 482 expect(data[0].username).to.equal('root')
568 expect(users.length).to.equal(2) 483 expect(data[1].username).to.equal('user_1')
569
570 expect(users[0].username).to.equal('root')
571 expect(users[1].username).to.equal('user_1')
572 } 484 }
573 }) 485 })
574 }) 486 })
575 487
576 describe('Update my account', function () { 488 describe('Update my account', function () {
489
577 it('Should update my password', async function () { 490 it('Should update my password', async function () {
578 await updateMyUser({ 491 await server.users.updateMe({
579 url: server.url, 492 token: userToken,
580 accessToken: accessTokenUser,
581 currentPassword: 'super password', 493 currentPassword: 'super password',
582 password: 'new password' 494 password: 'new password'
583 }) 495 })
584 user.password = 'new password' 496 user.password = 'new password'
585 497
586 await userLogin(server, user, HttpStatusCode.OK_200) 498 await server.login.login({ user })
587 }) 499 })
588 500
589 it('Should be able to change the NSFW display attribute', async function () { 501 it('Should be able to change the NSFW display attribute', async function () {
590 await updateMyUser({ 502 await server.users.updateMe({
591 url: server.url, 503 token: userToken,
592 accessToken: accessTokenUser,
593 nsfwPolicy: 'do_not_list' 504 nsfwPolicy: 'do_not_list'
594 }) 505 })
595 506
596 const res = await getMyUserInformation(server.url, accessTokenUser) 507 const user = await server.users.getMyInfo({ token: userToken })
597 const user = res.body
598
599 expect(user.username).to.equal('user_1') 508 expect(user.username).to.equal('user_1')
600 expect(user.email).to.equal('user_1@example.com') 509 expect(user.email).to.equal('user_1@example.com')
601 expect(user.nsfwPolicy).to.equal('do_not_list') 510 expect(user.nsfwPolicy).to.equal('do_not_list')
@@ -606,42 +515,33 @@ describe('Test users', function () {
606 }) 515 })
607 516
608 it('Should be able to change the autoPlayVideo attribute', async function () { 517 it('Should be able to change the autoPlayVideo attribute', async function () {
609 await updateMyUser({ 518 await server.users.updateMe({
610 url: server.url, 519 token: userToken,
611 accessToken: accessTokenUser,
612 autoPlayVideo: false 520 autoPlayVideo: false
613 }) 521 })
614 522
615 const res = await getMyUserInformation(server.url, accessTokenUser) 523 const user = await server.users.getMyInfo({ token: userToken })
616 const user = res.body
617
618 expect(user.autoPlayVideo).to.be.false 524 expect(user.autoPlayVideo).to.be.false
619 }) 525 })
620 526
621 it('Should be able to change the autoPlayNextVideo attribute', async function () { 527 it('Should be able to change the autoPlayNextVideo attribute', async function () {
622 await updateMyUser({ 528 await server.users.updateMe({
623 url: server.url, 529 token: userToken,
624 accessToken: accessTokenUser,
625 autoPlayNextVideo: true 530 autoPlayNextVideo: true
626 }) 531 })
627 532
628 const res = await getMyUserInformation(server.url, accessTokenUser) 533 const user = await server.users.getMyInfo({ token: userToken })
629 const user = res.body
630
631 expect(user.autoPlayNextVideo).to.be.true 534 expect(user.autoPlayNextVideo).to.be.true
632 }) 535 })
633 536
634 it('Should be able to change the email attribute', async function () { 537 it('Should be able to change the email attribute', async function () {
635 await updateMyUser({ 538 await server.users.updateMe({
636 url: server.url, 539 token: userToken,
637 accessToken: accessTokenUser,
638 currentPassword: 'new password', 540 currentPassword: 'new password',
639 email: 'updated@example.com' 541 email: 'updated@example.com'
640 }) 542 })
641 543
642 const res = await getMyUserInformation(server.url, accessTokenUser) 544 const user = await server.users.getMyInfo({ token: userToken })
643 const user = res.body
644
645 expect(user.username).to.equal('user_1') 545 expect(user.username).to.equal('user_1')
646 expect(user.email).to.equal('updated@example.com') 546 expect(user.email).to.equal('updated@example.com')
647 expect(user.nsfwPolicy).to.equal('do_not_list') 547 expect(user.nsfwPolicy).to.equal('do_not_list')
@@ -654,15 +554,9 @@ describe('Test users', function () {
654 it('Should be able to update my avatar with a gif', async function () { 554 it('Should be able to update my avatar with a gif', async function () {
655 const fixture = 'avatar.gif' 555 const fixture = 'avatar.gif'
656 556
657 await updateMyAvatar({ 557 await server.users.updateMyAvatar({ token: userToken, fixture })
658 url: server.url,
659 accessToken: accessTokenUser,
660 fixture
661 })
662
663 const res = await getMyUserInformation(server.url, accessTokenUser)
664 const user = res.body
665 558
559 const user = await server.users.getMyInfo({ token: userToken })
666 await testImage(server.url, 'avatar-resized', user.account.avatar.path, '.gif') 560 await testImage(server.url, 'avatar-resized', user.account.avatar.path, '.gif')
667 }) 561 })
668 562
@@ -670,29 +564,17 @@ describe('Test users', function () {
670 for (const extension of [ '.png', '.gif' ]) { 564 for (const extension of [ '.png', '.gif' ]) {
671 const fixture = 'avatar' + extension 565 const fixture = 'avatar' + extension
672 566
673 await updateMyAvatar({ 567 await server.users.updateMyAvatar({ token: userToken, fixture })
674 url: server.url,
675 accessToken: accessTokenUser,
676 fixture
677 })
678
679 const res = await getMyUserInformation(server.url, accessTokenUser)
680 const user = res.body
681 568
569 const user = await server.users.getMyInfo({ token: userToken })
682 await testImage(server.url, 'avatar-resized', user.account.avatar.path, extension) 570 await testImage(server.url, 'avatar-resized', user.account.avatar.path, extension)
683 } 571 }
684 }) 572 })
685 573
686 it('Should be able to update my display name', async function () { 574 it('Should be able to update my display name', async function () {
687 await updateMyUser({ 575 await server.users.updateMe({ token: userToken, displayName: 'new display name' })
688 url: server.url,
689 accessToken: accessTokenUser,
690 displayName: 'new display name'
691 })
692
693 const res = await getMyUserInformation(server.url, accessTokenUser)
694 const user = res.body
695 576
577 const user = await server.users.getMyInfo({ token: userToken })
696 expect(user.username).to.equal('user_1') 578 expect(user.username).to.equal('user_1')
697 expect(user.email).to.equal('updated@example.com') 579 expect(user.email).to.equal('updated@example.com')
698 expect(user.nsfwPolicy).to.equal('do_not_list') 580 expect(user.nsfwPolicy).to.equal('do_not_list')
@@ -703,15 +585,9 @@ describe('Test users', function () {
703 }) 585 })
704 586
705 it('Should be able to update my description', async function () { 587 it('Should be able to update my description', async function () {
706 await updateMyUser({ 588 await server.users.updateMe({ token: userToken, description: 'my super description updated' })
707 url: server.url,
708 accessToken: accessTokenUser,
709 description: 'my super description updated'
710 })
711
712 const res = await getMyUserInformation(server.url, accessTokenUser)
713 const user: User = res.body
714 589
590 const user = await server.users.getMyInfo({ token: userToken })
715 expect(user.username).to.equal('user_1') 591 expect(user.username).to.equal('user_1')
716 expect(user.email).to.equal('updated@example.com') 592 expect(user.email).to.equal('updated@example.com')
717 expect(user.nsfwPolicy).to.equal('do_not_list') 593 expect(user.nsfwPolicy).to.equal('do_not_list')
@@ -725,30 +601,21 @@ describe('Test users', function () {
725 601
726 it('Should be able to update my theme', async function () { 602 it('Should be able to update my theme', async function () {
727 for (const theme of [ 'background-red', 'default', 'instance-default' ]) { 603 for (const theme of [ 'background-red', 'default', 'instance-default' ]) {
728 await updateMyUser({ 604 await server.users.updateMe({ token: userToken, theme })
729 url: server.url,
730 accessToken: accessTokenUser,
731 theme
732 })
733 605
734 const res = await getMyUserInformation(server.url, accessTokenUser) 606 const user = await server.users.getMyInfo({ token: userToken })
735 const body: User = res.body 607 expect(user.theme).to.equal(theme)
736
737 expect(body.theme).to.equal(theme)
738 } 608 }
739 }) 609 })
740 610
741 it('Should be able to update my modal preferences', async function () { 611 it('Should be able to update my modal preferences', async function () {
742 await updateMyUser({ 612 await server.users.updateMe({
743 url: server.url, 613 token: userToken,
744 accessToken: accessTokenUser,
745 noInstanceConfigWarningModal: true, 614 noInstanceConfigWarningModal: true,
746 noWelcomeModal: true 615 noWelcomeModal: true
747 }) 616 })
748 617
749 const res = await getMyUserInformation(server.url, accessTokenUser) 618 const user = await server.users.getMyInfo({ token: userToken })
750 const user: User = res.body
751
752 expect(user.noWelcomeModal).to.be.true 619 expect(user.noWelcomeModal).to.be.true
753 expect(user.noInstanceConfigWarningModal).to.be.true 620 expect(user.noInstanceConfigWarningModal).to.be.true
754 }) 621 })
@@ -756,10 +623,9 @@ describe('Test users', function () {
756 623
757 describe('Updating another user', function () { 624 describe('Updating another user', function () {
758 it('Should be able to update another user', async function () { 625 it('Should be able to update another user', async function () {
759 await updateUser({ 626 await server.users.update({
760 url: server.url,
761 userId, 627 userId,
762 accessToken, 628 token,
763 email: 'updated2@example.com', 629 email: 'updated2@example.com',
764 emailVerified: true, 630 emailVerified: true,
765 videoQuota: 42, 631 videoQuota: 42,
@@ -768,8 +634,7 @@ describe('Test users', function () {
768 pluginAuth: 'toto' 634 pluginAuth: 'toto'
769 }) 635 })
770 636
771 const res = await getUserInformation(server.url, accessToken, userId) 637 const user = await server.users.get({ token, userId })
772 const user = res.body as User
773 638
774 expect(user.username).to.equal('user_1') 639 expect(user.username).to.equal('user_1')
775 expect(user.email).to.equal('updated2@example.com') 640 expect(user.email).to.equal('updated2@example.com')
@@ -783,57 +648,50 @@ describe('Test users', function () {
783 }) 648 })
784 649
785 it('Should reset the auth plugin', async function () { 650 it('Should reset the auth plugin', async function () {
786 await updateUser({ url: server.url, userId, accessToken, pluginAuth: null }) 651 await server.users.update({ userId, token, pluginAuth: null })
787 652
788 const res = await getUserInformation(server.url, accessToken, userId) 653 const user = await server.users.get({ token, userId })
789 const user = res.body as User
790 expect(user.pluginAuth).to.be.null 654 expect(user.pluginAuth).to.be.null
791 }) 655 })
792 656
793 it('Should have removed the user token', async function () { 657 it('Should have removed the user token', async function () {
794 await getMyUserVideoQuotaUsed(server.url, accessTokenUser, HttpStatusCode.UNAUTHORIZED_401) 658 await server.users.getMyQuotaUsed({ token: userToken, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
795 659
796 accessTokenUser = await userLogin(server, user) 660 userToken = await server.login.getAccessToken(user)
797 }) 661 })
798 662
799 it('Should be able to update another user password', async function () { 663 it('Should be able to update another user password', async function () {
800 await updateUser({ 664 await server.users.update({ userId, token, password: 'password updated' })
801 url: server.url,
802 userId,
803 accessToken,
804 password: 'password updated'
805 })
806 665
807 await getMyUserVideoQuotaUsed(server.url, accessTokenUser, HttpStatusCode.UNAUTHORIZED_401) 666 await server.users.getMyQuotaUsed({ token: userToken, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
808 667
809 await userLogin(server, user, HttpStatusCode.BAD_REQUEST_400) 668 await server.login.login({ user, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
810 669
811 user.password = 'password updated' 670 user.password = 'password updated'
812 accessTokenUser = await userLogin(server, user) 671 userToken = await server.login.getAccessToken(user)
813 }) 672 })
814 }) 673 })
815 674
816 describe('Video blacklists', function () { 675 describe('Video blacklists', function () {
817 it('Should be able to list video blacklist by a moderator', async function () { 676 it('Should be able to list video blacklist by a moderator', async function () {
818 await getBlacklistedVideosList({ url: server.url, token: accessTokenUser }) 677 await server.blacklist.list({ token: userToken })
819 }) 678 })
820 }) 679 })
821 680
822 describe('Remove a user', function () { 681 describe('Remove a user', function () {
823 it('Should be able to remove this user', async function () { 682 it('Should be able to remove this user', async function () {
824 await removeUser(server.url, userId, accessToken) 683 await server.users.remove({ userId, token })
825 }) 684 })
826 685
827 it('Should not be able to login with this user', async function () { 686 it('Should not be able to login with this user', async function () {
828 await userLogin(server, user, HttpStatusCode.BAD_REQUEST_400) 687 await server.login.login({ user, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
829 }) 688 })
830 689
831 it('Should not have videos of this user', async function () { 690 it('Should not have videos of this user', async function () {
832 const res = await getVideosList(server.url) 691 const { data, total } = await server.videos.list()
833 692 expect(total).to.equal(1)
834 expect(res.body.total).to.equal(1)
835 693
836 const video = res.body.data[0] 694 const video = data[0]
837 expect(video.account.name).to.equal('root') 695 expect(video.account.name).to.equal('root')
838 }) 696 })
839 }) 697 })
@@ -845,7 +703,7 @@ describe('Test users', function () {
845 const user = { displayName: 'super user 15', username: 'user_15', password: 'my super password' } 703 const user = { displayName: 'super user 15', username: 'user_15', password: 'my super password' }
846 const channel = { name: 'my_user_15_channel', displayName: 'my channel rocks' } 704 const channel = { name: 'my_user_15_channel', displayName: 'my channel rocks' }
847 705
848 await registerUserWithChannel({ url: server.url, user, channel }) 706 await server.users.register({ ...user, channel })
849 }) 707 })
850 708
851 it('Should be able to login with this registered user', async function () { 709 it('Should be able to login with this registered user', async function () {
@@ -854,40 +712,36 @@ describe('Test users', function () {
854 password: 'my super password' 712 password: 'my super password'
855 } 713 }
856 714
857 user15AccessToken = await userLogin(server, user15) 715 user15AccessToken = await server.login.getAccessToken(user15)
858 }) 716 })
859 717
860 it('Should have the correct display name', async function () { 718 it('Should have the correct display name', async function () {
861 const res = await getMyUserInformation(server.url, user15AccessToken) 719 const user = await server.users.getMyInfo({ token: user15AccessToken })
862 const user: User = res.body
863
864 expect(user.account.displayName).to.equal('super user 15') 720 expect(user.account.displayName).to.equal('super user 15')
865 }) 721 })
866 722
867 it('Should have the correct video quota', async function () { 723 it('Should have the correct video quota', async function () {
868 const res = await getMyUserInformation(server.url, user15AccessToken) 724 const user = await server.users.getMyInfo({ token: user15AccessToken })
869 const user = res.body
870
871 expect(user.videoQuota).to.equal(5 * 1024 * 1024) 725 expect(user.videoQuota).to.equal(5 * 1024 * 1024)
872 }) 726 })
873 727
874 it('Should have created the channel', async function () { 728 it('Should have created the channel', async function () {
875 const res = await getVideoChannel(server.url, 'my_user_15_channel') 729 const { displayName } = await server.channels.get({ channelName: 'my_user_15_channel' })
876 730
877 expect(res.body.displayName).to.equal('my channel rocks') 731 expect(displayName).to.equal('my channel rocks')
878 }) 732 })
879 733
880 it('Should remove me', async function () { 734 it('Should remove me', async function () {
881 { 735 {
882 const res = await getUsersList(server.url, server.accessToken) 736 const { data } = await server.users.list()
883 expect(res.body.data.find(u => u.username === 'user_15')).to.not.be.undefined 737 expect(data.find(u => u.username === 'user_15')).to.not.be.undefined
884 } 738 }
885 739
886 await deleteMe(server.url, user15AccessToken) 740 await server.users.deleteMe({ token: user15AccessToken })
887 741
888 { 742 {
889 const res = await getUsersList(server.url, server.accessToken) 743 const { data } = await server.users.list()
890 expect(res.body.data.find(u => u.username === 'user_15')).to.be.undefined 744 expect(data.find(u => u.username === 'user_15')).to.be.undefined
891 } 745 }
892 }) 746 })
893 }) 747 })
@@ -901,49 +755,40 @@ describe('Test users', function () {
901 } 755 }
902 756
903 it('Should block a user', async function () { 757 it('Should block a user', async function () {
904 const resUser = await createUser({ 758 const user = await server.users.create({ ...user16 })
905 url: server.url, 759 user16Id = user.id
906 accessToken: server.accessToken,
907 username: user16.username,
908 password: user16.password
909 })
910 user16Id = resUser.body.user.id
911 760
912 user16AccessToken = await userLogin(server, user16) 761 user16AccessToken = await server.login.getAccessToken(user16)
913 762
914 await getMyUserInformation(server.url, user16AccessToken, HttpStatusCode.OK_200) 763 await server.users.getMyInfo({ token: user16AccessToken, expectedStatus: HttpStatusCode.OK_200 })
915 await blockUser(server.url, user16Id, server.accessToken) 764 await server.users.banUser({ userId: user16Id })
916 765
917 await getMyUserInformation(server.url, user16AccessToken, HttpStatusCode.UNAUTHORIZED_401) 766 await server.users.getMyInfo({ token: user16AccessToken, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
918 await userLogin(server, user16, HttpStatusCode.BAD_REQUEST_400) 767 await server.login.login({ user: user16, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
919 }) 768 })
920 769
921 it('Should search user by banned status', async function () { 770 it('Should search user by banned status', async function () {
922 { 771 {
923 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt', undefined, true) 772 const { data, total } = await server.users.list({ start: 0, count: 2, sort: 'createdAt', blocked: true })
924 const users = res.body.data as User[] 773 expect(total).to.equal(1)
774 expect(data.length).to.equal(1)
925 775
926 expect(res.body.total).to.equal(1) 776 expect(data[0].username).to.equal(user16.username)
927 expect(users.length).to.equal(1)
928
929 expect(users[0].username).to.equal(user16.username)
930 } 777 }
931 778
932 { 779 {
933 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt', undefined, false) 780 const { data, total } = await server.users.list({ start: 0, count: 2, sort: 'createdAt', blocked: false })
934 const users = res.body.data as User[] 781 expect(total).to.equal(1)
935 782 expect(data.length).to.equal(1)
936 expect(res.body.total).to.equal(1)
937 expect(users.length).to.equal(1)
938 783
939 expect(users[0].username).to.not.equal(user16.username) 784 expect(data[0].username).to.not.equal(user16.username)
940 } 785 }
941 }) 786 })
942 787
943 it('Should unblock a user', async function () { 788 it('Should unblock a user', async function () {
944 await unblockUser(server.url, user16Id, server.accessToken) 789 await server.users.unbanUser({ userId: user16Id })
945 user16AccessToken = await userLogin(server, user16) 790 user16AccessToken = await server.login.getAccessToken(user16)
946 await getMyUserInformation(server.url, user16AccessToken, HttpStatusCode.OK_200) 791 await server.users.getMyInfo({ token: user16AccessToken, expectedStatus: HttpStatusCode.OK_200 })
947 }) 792 })
948 }) 793 })
949 794
@@ -956,19 +801,12 @@ describe('Test users', function () {
956 username: 'user_17', 801 username: 'user_17',
957 password: 'my super password' 802 password: 'my super password'
958 } 803 }
959 const resUser = await createUser({ 804 const created = await server.users.create({ ...user17 })
960 url: server.url,
961 accessToken: server.accessToken,
962 username: user17.username,
963 password: user17.password
964 })
965 805
966 user17Id = resUser.body.user.id 806 user17Id = created.id
967 user17AccessToken = await userLogin(server, user17) 807 user17AccessToken = await server.login.getAccessToken(user17)
968
969 const res = await getUserInformation(server.url, server.accessToken, user17Id, true)
970 const user: User = res.body
971 808
809 const user = await server.users.get({ userId: user17Id, withStats: true })
972 expect(user.videosCount).to.equal(0) 810 expect(user.videosCount).to.equal(0)
973 expect(user.videoCommentsCount).to.equal(0) 811 expect(user.videoCommentsCount).to.equal(0)
974 expect(user.abusesCount).to.equal(0) 812 expect(user.abusesCount).to.equal(0)
@@ -977,54 +815,43 @@ describe('Test users', function () {
977 }) 815 })
978 816
979 it('Should report correct videos count', async function () { 817 it('Should report correct videos count', async function () {
980 const videoAttributes = { 818 const attributes = { name: 'video to test user stats' }
981 name: 'video to test user stats' 819 await server.videos.upload({ token: user17AccessToken, attributes })
982 }
983 await uploadVideo(server.url, user17AccessToken, videoAttributes)
984 const res1 = await getVideosList(server.url)
985 videoId = res1.body.data.find(video => video.name === videoAttributes.name).id
986 820
987 const res2 = await getUserInformation(server.url, server.accessToken, user17Id, true) 821 const { data } = await server.videos.list()
988 const user: User = res2.body 822 videoId = data.find(video => video.name === attributes.name).id
989 823
824 const user = await server.users.get({ userId: user17Id, withStats: true })
990 expect(user.videosCount).to.equal(1) 825 expect(user.videosCount).to.equal(1)
991 }) 826 })
992 827
993 it('Should report correct video comments for user', async function () { 828 it('Should report correct video comments for user', async function () {
994 const text = 'super comment' 829 const text = 'super comment'
995 await addVideoCommentThread(server.url, user17AccessToken, videoId, text) 830 await server.comments.createThread({ token: user17AccessToken, videoId, text })
996
997 const res = await getUserInformation(server.url, server.accessToken, user17Id, true)
998 const user: User = res.body
999 831
832 const user = await server.users.get({ userId: user17Id, withStats: true })
1000 expect(user.videoCommentsCount).to.equal(1) 833 expect(user.videoCommentsCount).to.equal(1)
1001 }) 834 })
1002 835
1003 it('Should report correct abuses counts', async function () { 836 it('Should report correct abuses counts', async function () {
1004 const reason = 'my super bad reason' 837 const reason = 'my super bad reason'
1005 await reportAbuse({ url: server.url, token: user17AccessToken, videoId, reason }) 838 await server.abuses.report({ token: user17AccessToken, videoId, reason })
1006
1007 const res1 = await getAdminAbusesList({ url: server.url, token: server.accessToken })
1008 const abuseId = res1.body.data[0].id
1009 839
1010 const res2 = await getUserInformation(server.url, server.accessToken, user17Id, true) 840 const body1 = await server.abuses.getAdminList()
1011 const user2: User = res2.body 841 const abuseId = body1.data[0].id
1012 842
843 const user2 = await server.users.get({ userId: user17Id, withStats: true })
1013 expect(user2.abusesCount).to.equal(1) // number of incriminations 844 expect(user2.abusesCount).to.equal(1) // number of incriminations
1014 expect(user2.abusesCreatedCount).to.equal(1) // number of reports created 845 expect(user2.abusesCreatedCount).to.equal(1) // number of reports created
1015 846
1016 const body: AbuseUpdate = { state: AbuseState.ACCEPTED } 847 await server.abuses.update({ abuseId, body: { state: AbuseState.ACCEPTED } })
1017 await updateAbuse(server.url, server.accessToken, abuseId, body)
1018
1019 const res3 = await getUserInformation(server.url, server.accessToken, user17Id, true)
1020 const user3: User = res3.body
1021 848
849 const user3 = await server.users.get({ userId: user17Id, withStats: true })
1022 expect(user3.abusesAcceptedCount).to.equal(1) // number of reports created accepted 850 expect(user3.abusesAcceptedCount).to.equal(1) // number of reports created accepted
1023 }) 851 })
1024 }) 852 })
1025 853
1026 after(async function () { 854 after(async function () {
1027 await closeAllSequelize([ server ])
1028 await cleanupTests([ server ]) 855 await cleanupTests([ server ])
1029 }) 856 })
1030}) 857})
diff --git a/server/tests/api/videos/audio-only.ts b/server/tests/api/videos/audio-only.ts
index 7ddbd5cd9..15c3ae6d6 100644
--- a/server/tests/api/videos/audio-only.ts
+++ b/server/tests/api/videos/audio-only.ts
@@ -4,23 +4,12 @@ import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5import { join } from 'path' 5import { join } from 'path'
6import { getAudioStream, getVideoStreamSize } from '@server/helpers/ffprobe-utils' 6import { getAudioStream, getVideoStreamSize } from '@server/helpers/ffprobe-utils'
7import { 7import { cleanupTests, createMultipleServers, doubleFollow, PeerTubeServer, setAccessTokensToServers, waitJobs } from '@shared/extra-utils'
8 buildServerDirectory,
9 cleanupTests,
10 doubleFollow,
11 flushAndRunMultipleServers,
12 getVideo,
13 ServerInfo,
14 setAccessTokensToServers,
15 uploadVideo,
16 waitJobs
17} from '../../../../shared/extra-utils'
18import { VideoDetails } from '../../../../shared/models/videos'
19 8
20const expect = chai.expect 9const expect = chai.expect
21 10
22describe('Test audio only video transcoding', function () { 11describe('Test audio only video transcoding', function () {
23 let servers: ServerInfo[] = [] 12 let servers: PeerTubeServer[] = []
24 let videoUUID: string 13 let videoUUID: string
25 14
26 before(async function () { 15 before(async function () {
@@ -47,7 +36,7 @@ describe('Test audio only video transcoding', function () {
47 } 36 }
48 } 37 }
49 } 38 }
50 servers = await flushAndRunMultipleServers(2, configOverride) 39 servers = await createMultipleServers(2, configOverride)
51 40
52 // Get the access tokens 41 // Get the access tokens
53 await setAccessTokensToServers(servers) 42 await setAccessTokensToServers(servers)
@@ -59,15 +48,13 @@ describe('Test audio only video transcoding', function () {
59 it('Should upload a video and transcode it', async function () { 48 it('Should upload a video and transcode it', async function () {
60 this.timeout(120000) 49 this.timeout(120000)
61 50
62 const resUpload = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'audio only' }) 51 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'audio only' } })
63 videoUUID = resUpload.body.video.uuid 52 videoUUID = uuid
64 53
65 await waitJobs(servers) 54 await waitJobs(servers)
66 55
67 for (const server of servers) { 56 for (const server of servers) {
68 const res = await getVideo(server.url, videoUUID) 57 const video = await server.videos.get({ id: videoUUID })
69 const video: VideoDetails = res.body
70
71 expect(video.streamingPlaylists).to.have.lengthOf(1) 58 expect(video.streamingPlaylists).to.have.lengthOf(1)
72 59
73 for (const files of [ video.files, video.streamingPlaylists[0].files ]) { 60 for (const files of [ video.files, video.streamingPlaylists[0].files ]) {
@@ -81,8 +68,8 @@ describe('Test audio only video transcoding', function () {
81 68
82 it('0p transcoded video should not have video', async function () { 69 it('0p transcoded video should not have video', async function () {
83 const paths = [ 70 const paths = [
84 buildServerDirectory(servers[0], join('videos', videoUUID + '-0.mp4')), 71 servers[0].servers.buildDirectory(join('videos', videoUUID + '-0.mp4')),
85 buildServerDirectory(servers[0], join('streaming-playlists', 'hls', videoUUID, videoUUID + '-0-fragmented.mp4')) 72 servers[0].servers.buildDirectory(join('streaming-playlists', 'hls', videoUUID, videoUUID + '-0-fragmented.mp4'))
86 ] 73 ]
87 74
88 for (const path of paths) { 75 for (const path of paths) {
diff --git a/server/tests/api/videos/multiple-servers.ts b/server/tests/api/videos/multiple-servers.ts
index a8c8a889b..d916abb09 100644
--- a/server/tests/api/videos/multiple-servers.ts
+++ b/server/tests/api/videos/multiple-servers.ts
@@ -3,49 +3,28 @@
3import 'mocha' 3import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5import * as request from 'supertest' 5import * as request from 'supertest'
6import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
7import { 6import {
8 addVideoChannel,
9 buildAbsoluteFixturePath, 7 buildAbsoluteFixturePath,
10 checkTmpIsEmpty, 8 checkTmpIsEmpty,
11 checkVideoFilesWereRemoved, 9 checkVideoFilesWereRemoved,
12 cleanupTests, 10 cleanupTests,
13 completeVideoCheck, 11 completeVideoCheck,
14 createUser, 12 createMultipleServers,
15 dateIsValid, 13 dateIsValid,
16 doubleFollow, 14 doubleFollow,
17 flushAndRunMultipleServers, 15 PeerTubeServer,
18 getLocalVideos,
19 getVideo,
20 getVideoChannelsList,
21 getVideosList,
22 rateVideo,
23 removeVideo,
24 ServerInfo,
25 setAccessTokensToServers, 16 setAccessTokensToServers,
26 testImage, 17 testImage,
27 updateVideo,
28 uploadVideo,
29 userLogin,
30 viewVideo,
31 wait, 18 wait,
19 waitJobs,
32 webtorrentAdd 20 webtorrentAdd
33} from '../../../../shared/extra-utils' 21} from '@shared/extra-utils'
34import { waitJobs } from '../../../../shared/extra-utils/server/jobs' 22import { HttpStatusCode, VideoCommentThreadTree, VideoPrivacy } from '@shared/models'
35import {
36 addVideoCommentReply,
37 addVideoCommentThread,
38 deleteVideoComment,
39 findCommentId,
40 getVideoCommentThreads,
41 getVideoThreadComments
42} from '../../../../shared/extra-utils/videos/video-comments'
43import { VideoComment, VideoCommentThreadTree, VideoPrivacy } from '../../../../shared/models/videos'
44 23
45const expect = chai.expect 24const expect = chai.expect
46 25
47describe('Test multiple servers', function () { 26describe('Test multiple servers', function () {
48 let servers: ServerInfo[] = [] 27 let servers: PeerTubeServer[] = []
49 const toRemove = [] 28 const toRemove = []
50 let videoUUID = '' 29 let videoUUID = ''
51 let videoChannelId: number 30 let videoChannelId: number
@@ -53,7 +32,7 @@ describe('Test multiple servers', function () {
53 before(async function () { 32 before(async function () {
54 this.timeout(120000) 33 this.timeout(120000)
55 34
56 servers = await flushAndRunMultipleServers(3) 35 servers = await createMultipleServers(3)
57 36
58 // Get the access tokens 37 // Get the access tokens
59 await setAccessTokensToServers(servers) 38 await setAccessTokensToServers(servers)
@@ -64,9 +43,9 @@ describe('Test multiple servers', function () {
64 displayName: 'my channel', 43 displayName: 'my channel',
65 description: 'super channel' 44 description: 'super channel'
66 } 45 }
67 await addVideoChannel(servers[0].url, servers[0].accessToken, videoChannel) 46 await servers[0].channels.create({ attributes: videoChannel })
68 const channelRes = await getVideoChannelsList(servers[0].url, 0, 1) 47 const { data } = await servers[0].channels.list({ start: 0, count: 1 })
69 videoChannelId = channelRes.body.data[0].id 48 videoChannelId = data[0].id
70 } 49 }
71 50
72 // Server 1 and server 2 follow each other 51 // Server 1 and server 2 follow each other
@@ -79,10 +58,9 @@ describe('Test multiple servers', function () {
79 58
80 it('Should not have videos for all servers', async function () { 59 it('Should not have videos for all servers', async function () {
81 for (const server of servers) { 60 for (const server of servers) {
82 const res = await getVideosList(server.url) 61 const { data } = await server.videos.list()
83 const videos = res.body.data 62 expect(data).to.be.an('array')
84 expect(videos).to.be.an('array') 63 expect(data.length).to.equal(0)
85 expect(videos.length).to.equal(0)
86 } 64 }
87 }) 65 })
88 66
@@ -90,7 +68,7 @@ describe('Test multiple servers', function () {
90 it('Should upload the video on server 1 and propagate on each server', async function () { 68 it('Should upload the video on server 1 and propagate on each server', async function () {
91 this.timeout(25000) 69 this.timeout(25000)
92 70
93 const videoAttributes = { 71 const attributes = {
94 name: 'my super name for server 1', 72 name: 'my super name for server 1',
95 category: 5, 73 category: 5,
96 licence: 4, 74 licence: 4,
@@ -103,7 +81,7 @@ describe('Test multiple servers', function () {
103 channelId: videoChannelId, 81 channelId: videoChannelId,
104 fixture: 'video_short1.webm' 82 fixture: 'video_short1.webm'
105 } 83 }
106 await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes) 84 await servers[0].videos.upload({ attributes })
107 85
108 await waitJobs(servers) 86 await waitJobs(servers)
109 87
@@ -146,14 +124,13 @@ describe('Test multiple servers', function () {
146 ] 124 ]
147 } 125 }
148 126
149 const res = await getVideosList(server.url) 127 const { data } = await server.videos.list()
150 const videos = res.body.data 128 expect(data).to.be.an('array')
151 expect(videos).to.be.an('array') 129 expect(data.length).to.equal(1)
152 expect(videos.length).to.equal(1) 130 const video = data[0]
153 const video = videos[0]
154 131
155 await completeVideoCheck(server.url, video, checkAttributes) 132 await completeVideoCheck(server, video, checkAttributes)
156 publishedAt = video.publishedAt 133 publishedAt = video.publishedAt as string
157 } 134 }
158 }) 135 })
159 136
@@ -164,10 +141,10 @@ describe('Test multiple servers', function () {
164 username: 'user1', 141 username: 'user1',
165 password: 'super_password' 142 password: 'super_password'
166 } 143 }
167 await createUser({ url: servers[1].url, accessToken: servers[1].accessToken, username: user.username, password: user.password }) 144 await servers[1].users.create({ username: user.username, password: user.password })
168 const userAccessToken = await userLogin(servers[1], user) 145 const userAccessToken = await servers[1].login.getAccessToken(user)
169 146
170 const videoAttributes = { 147 const attributes = {
171 name: 'my super name for server 2', 148 name: 'my super name for server 2',
172 category: 4, 149 category: 4,
173 licence: 3, 150 licence: 3,
@@ -180,7 +157,7 @@ describe('Test multiple servers', function () {
180 thumbnailfile: 'thumbnail.jpg', 157 thumbnailfile: 'thumbnail.jpg',
181 previewfile: 'preview.jpg' 158 previewfile: 'preview.jpg'
182 } 159 }
183 await uploadVideo(servers[1].url, userAccessToken, videoAttributes, HttpStatusCode.OK_200, 'resumable') 160 await servers[1].videos.upload({ token: userAccessToken, attributes, mode: 'resumable' })
184 161
185 // Transcoding 162 // Transcoding
186 await waitJobs(servers) 163 await waitJobs(servers)
@@ -235,65 +212,67 @@ describe('Test multiple servers', function () {
235 previewfile: 'preview' 212 previewfile: 'preview'
236 } 213 }
237 214
238 const res = await getVideosList(server.url) 215 const { data } = await server.videos.list()
239 const videos = res.body.data 216 expect(data).to.be.an('array')
240 expect(videos).to.be.an('array') 217 expect(data.length).to.equal(2)
241 expect(videos.length).to.equal(2) 218 const video = data[1]
242 const video = videos[1]
243 219
244 await completeVideoCheck(server.url, video, checkAttributes) 220 await completeVideoCheck(server, video, checkAttributes)
245 } 221 }
246 }) 222 })
247 223
248 it('Should upload two videos on server 3 and propagate on each server', async function () { 224 it('Should upload two videos on server 3 and propagate on each server', async function () {
249 this.timeout(45000) 225 this.timeout(45000)
250 226
251 const videoAttributes1 = { 227 {
252 name: 'my super name for server 3', 228 const attributes = {
253 category: 6, 229 name: 'my super name for server 3',
254 licence: 5, 230 category: 6,
255 language: 'de', 231 licence: 5,
256 nsfw: true, 232 language: 'de',
257 description: 'my super description for server 3', 233 nsfw: true,
258 support: 'my super support text for server 3', 234 description: 'my super description for server 3',
259 tags: [ 'tag1p3' ], 235 support: 'my super support text for server 3',
260 fixture: 'video_short3.webm' 236 tags: [ 'tag1p3' ],
237 fixture: 'video_short3.webm'
238 }
239 await servers[2].videos.upload({ attributes })
261 } 240 }
262 await uploadVideo(servers[2].url, servers[2].accessToken, videoAttributes1) 241
263 242 {
264 const videoAttributes2 = { 243 const attributes = {
265 name: 'my super name for server 3-2', 244 name: 'my super name for server 3-2',
266 category: 7, 245 category: 7,
267 licence: 6, 246 licence: 6,
268 language: 'ko', 247 language: 'ko',
269 nsfw: false, 248 nsfw: false,
270 description: 'my super description for server 3-2', 249 description: 'my super description for server 3-2',
271 support: 'my super support text for server 3-2', 250 support: 'my super support text for server 3-2',
272 tags: [ 'tag2p3', 'tag3p3', 'tag4p3' ], 251 tags: [ 'tag2p3', 'tag3p3', 'tag4p3' ],
273 fixture: 'video_short.webm' 252 fixture: 'video_short.webm'
253 }
254 await servers[2].videos.upload({ attributes })
274 } 255 }
275 await uploadVideo(servers[2].url, servers[2].accessToken, videoAttributes2)
276 256
277 await waitJobs(servers) 257 await waitJobs(servers)
278 258
279 // All servers should have this video 259 // All servers should have this video
280 for (const server of servers) { 260 for (const server of servers) {
281 const isLocal = server.url === 'http://localhost:' + servers[2].port 261 const isLocal = server.url === 'http://localhost:' + servers[2].port
282 const res = await getVideosList(server.url) 262 const { data } = await server.videos.list()
283 263
284 const videos = res.body.data 264 expect(data).to.be.an('array')
285 expect(videos).to.be.an('array') 265 expect(data.length).to.equal(4)
286 expect(videos.length).to.equal(4)
287 266
288 // We not sure about the order of the two last uploads 267 // We not sure about the order of the two last uploads
289 let video1 = null 268 let video1 = null
290 let video2 = null 269 let video2 = null
291 if (videos[2].name === 'my super name for server 3') { 270 if (data[2].name === 'my super name for server 3') {
292 video1 = videos[2] 271 video1 = data[2]
293 video2 = videos[3] 272 video2 = data[3]
294 } else { 273 } else {
295 video1 = videos[3] 274 video1 = data[3]
296 video2 = videos[2] 275 video2 = data[2]
297 } 276 }
298 277
299 const checkAttributesVideo1 = { 278 const checkAttributesVideo1 = {
@@ -328,7 +307,7 @@ describe('Test multiple servers', function () {
328 } 307 }
329 ] 308 ]
330 } 309 }
331 await completeVideoCheck(server.url, video1, checkAttributesVideo1) 310 await completeVideoCheck(server, video1, checkAttributesVideo1)
332 311
333 const checkAttributesVideo2 = { 312 const checkAttributesVideo2 = {
334 name: 'my super name for server 3-2', 313 name: 'my super name for server 3-2',
@@ -362,38 +341,38 @@ describe('Test multiple servers', function () {
362 } 341 }
363 ] 342 ]
364 } 343 }
365 await completeVideoCheck(server.url, video2, checkAttributesVideo2) 344 await completeVideoCheck(server, video2, checkAttributesVideo2)
366 } 345 }
367 }) 346 })
368 }) 347 })
369 348
370 describe('It should list local videos', function () { 349 describe('It should list local videos', function () {
371 it('Should list only local videos on server 1', async function () { 350 it('Should list only local videos on server 1', async function () {
372 const { body } = await getLocalVideos(servers[0].url) 351 const { data, total } = await servers[0].videos.list({ filter: 'local' })
373 352
374 expect(body.total).to.equal(1) 353 expect(total).to.equal(1)
375 expect(body.data).to.be.an('array') 354 expect(data).to.be.an('array')
376 expect(body.data.length).to.equal(1) 355 expect(data.length).to.equal(1)
377 expect(body.data[0].name).to.equal('my super name for server 1') 356 expect(data[0].name).to.equal('my super name for server 1')
378 }) 357 })
379 358
380 it('Should list only local videos on server 2', async function () { 359 it('Should list only local videos on server 2', async function () {
381 const { body } = await getLocalVideos(servers[1].url) 360 const { data, total } = await servers[1].videos.list({ filter: 'local' })
382 361
383 expect(body.total).to.equal(1) 362 expect(total).to.equal(1)
384 expect(body.data).to.be.an('array') 363 expect(data).to.be.an('array')
385 expect(body.data.length).to.equal(1) 364 expect(data.length).to.equal(1)
386 expect(body.data[0].name).to.equal('my super name for server 2') 365 expect(data[0].name).to.equal('my super name for server 2')
387 }) 366 })
388 367
389 it('Should list only local videos on server 3', async function () { 368 it('Should list only local videos on server 3', async function () {
390 const { body } = await getLocalVideos(servers[2].url) 369 const { data, total } = await servers[2].videos.list({ filter: 'local' })
391 370
392 expect(body.total).to.equal(2) 371 expect(total).to.equal(2)
393 expect(body.data).to.be.an('array') 372 expect(data).to.be.an('array')
394 expect(body.data.length).to.equal(2) 373 expect(data.length).to.equal(2)
395 expect(body.data[0].name).to.equal('my super name for server 3') 374 expect(data[0].name).to.equal('my super name for server 3')
396 expect(body.data[1].name).to.equal('my super name for server 3-2') 375 expect(data[1].name).to.equal('my super name for server 3-2')
397 }) 376 })
398 }) 377 })
399 378
@@ -401,15 +380,13 @@ describe('Test multiple servers', function () {
401 it('Should add the file 1 by asking server 3', async function () { 380 it('Should add the file 1 by asking server 3', async function () {
402 this.timeout(10000) 381 this.timeout(10000)
403 382
404 const res = await getVideosList(servers[2].url) 383 const { data } = await servers[2].videos.list()
405
406 const video = res.body.data[0]
407 toRemove.push(res.body.data[2])
408 toRemove.push(res.body.data[3])
409 384
410 const res2 = await getVideo(servers[2].url, video.id) 385 const video = data[0]
411 const videoDetails = res2.body 386 toRemove.push(data[2])
387 toRemove.push(data[3])
412 388
389 const videoDetails = await servers[2].videos.get({ id: video.id })
413 const torrent = await webtorrentAdd(videoDetails.files[0].magnetUri, true) 390 const torrent = await webtorrentAdd(videoDetails.files[0].magnetUri, true)
414 expect(torrent.files).to.be.an('array') 391 expect(torrent.files).to.be.an('array')
415 expect(torrent.files.length).to.equal(1) 392 expect(torrent.files.length).to.equal(1)
@@ -419,11 +396,10 @@ describe('Test multiple servers', function () {
419 it('Should add the file 2 by asking server 1', async function () { 396 it('Should add the file 2 by asking server 1', async function () {
420 this.timeout(10000) 397 this.timeout(10000)
421 398
422 const res = await getVideosList(servers[0].url) 399 const { data } = await servers[0].videos.list()
423 400
424 const video = res.body.data[1] 401 const video = data[1]
425 const res2 = await getVideo(servers[0].url, video.id) 402 const videoDetails = await servers[0].videos.get({ id: video.id })
426 const videoDetails = res2.body
427 403
428 const torrent = await webtorrentAdd(videoDetails.files[0].magnetUri, true) 404 const torrent = await webtorrentAdd(videoDetails.files[0].magnetUri, true)
429 expect(torrent.files).to.be.an('array') 405 expect(torrent.files).to.be.an('array')
@@ -434,11 +410,10 @@ describe('Test multiple servers', function () {
434 it('Should add the file 3 by asking server 2', async function () { 410 it('Should add the file 3 by asking server 2', async function () {
435 this.timeout(10000) 411 this.timeout(10000)
436 412
437 const res = await getVideosList(servers[1].url) 413 const { data } = await servers[1].videos.list()
438 414
439 const video = res.body.data[2] 415 const video = data[2]
440 const res2 = await getVideo(servers[1].url, video.id) 416 const videoDetails = await servers[1].videos.get({ id: video.id })
441 const videoDetails = res2.body
442 417
443 const torrent = await webtorrentAdd(videoDetails.files[0].magnetUri, true) 418 const torrent = await webtorrentAdd(videoDetails.files[0].magnetUri, true)
444 expect(torrent.files).to.be.an('array') 419 expect(torrent.files).to.be.an('array')
@@ -449,11 +424,10 @@ describe('Test multiple servers', function () {
449 it('Should add the file 3-2 by asking server 1', async function () { 424 it('Should add the file 3-2 by asking server 1', async function () {
450 this.timeout(10000) 425 this.timeout(10000)
451 426
452 const res = await getVideosList(servers[0].url) 427 const { data } = await servers[0].videos.list()
453 428
454 const video = res.body.data[3] 429 const video = data[3]
455 const res2 = await getVideo(servers[0].url, video.id) 430 const videoDetails = await servers[0].videos.get({ id: video.id })
456 const videoDetails = res2.body
457 431
458 const torrent = await webtorrentAdd(videoDetails.files[0].magnetUri) 432 const torrent = await webtorrentAdd(videoDetails.files[0].magnetUri)
459 expect(torrent.files).to.be.an('array') 433 expect(torrent.files).to.be.an('array')
@@ -464,11 +438,10 @@ describe('Test multiple servers', function () {
464 it('Should add the file 2 in 360p by asking server 1', async function () { 438 it('Should add the file 2 in 360p by asking server 1', async function () {
465 this.timeout(10000) 439 this.timeout(10000)
466 440
467 const res = await getVideosList(servers[0].url) 441 const { data } = await servers[0].videos.list()
468 442
469 const video = res.body.data.find(v => v.name === 'my super name for server 2') 443 const video = data.find(v => v.name === 'my super name for server 2')
470 const res2 = await getVideo(servers[0].url, video.id) 444 const videoDetails = await servers[0].videos.get({ id: video.id })
471 const videoDetails = res2.body
472 445
473 const file = videoDetails.files.find(f => f.resolution.id === 360) 446 const file = videoDetails.files.find(f => f.resolution.id === 360)
474 expect(file).not.to.be.undefined 447 expect(file).not.to.be.undefined
@@ -487,30 +460,36 @@ describe('Test multiple servers', function () {
487 let remoteVideosServer3 = [] 460 let remoteVideosServer3 = []
488 461
489 before(async function () { 462 before(async function () {
490 const res1 = await getVideosList(servers[0].url) 463 {
491 remoteVideosServer1 = res1.body.data.filter(video => video.isLocal === false).map(video => video.uuid) 464 const { data } = await servers[0].videos.list()
465 remoteVideosServer1 = data.filter(video => video.isLocal === false).map(video => video.uuid)
466 }
492 467
493 const res2 = await getVideosList(servers[1].url) 468 {
494 remoteVideosServer2 = res2.body.data.filter(video => video.isLocal === false).map(video => video.uuid) 469 const { data } = await servers[1].videos.list()
470 remoteVideosServer2 = data.filter(video => video.isLocal === false).map(video => video.uuid)
471 }
495 472
496 const res3 = await getVideosList(servers[2].url) 473 {
497 localVideosServer3 = res3.body.data.filter(video => video.isLocal === true).map(video => video.uuid) 474 const { data } = await servers[2].videos.list()
498 remoteVideosServer3 = res3.body.data.filter(video => video.isLocal === false).map(video => video.uuid) 475 localVideosServer3 = data.filter(video => video.isLocal === true).map(video => video.uuid)
476 remoteVideosServer3 = data.filter(video => video.isLocal === false).map(video => video.uuid)
477 }
499 }) 478 })
500 479
501 it('Should view multiple videos on owned servers', async function () { 480 it('Should view multiple videos on owned servers', async function () {
502 this.timeout(30000) 481 this.timeout(30000)
503 482
504 await viewVideo(servers[2].url, localVideosServer3[0]) 483 await servers[2].videos.view({ id: localVideosServer3[0] })
505 await wait(1000) 484 await wait(1000)
506 485
507 await viewVideo(servers[2].url, localVideosServer3[0]) 486 await servers[2].videos.view({ id: localVideosServer3[0] })
508 await viewVideo(servers[2].url, localVideosServer3[1]) 487 await servers[2].videos.view({ id: localVideosServer3[1] })
509 488
510 await wait(1000) 489 await wait(1000)
511 490
512 await viewVideo(servers[2].url, localVideosServer3[0]) 491 await servers[2].videos.view({ id: localVideosServer3[0] })
513 await viewVideo(servers[2].url, localVideosServer3[0]) 492 await servers[2].videos.view({ id: localVideosServer3[0] })
514 493
515 await waitJobs(servers) 494 await waitJobs(servers)
516 495
@@ -520,11 +499,10 @@ describe('Test multiple servers', function () {
520 await waitJobs(servers) 499 await waitJobs(servers)
521 500
522 for (const server of servers) { 501 for (const server of servers) {
523 const res = await getVideosList(server.url) 502 const { data } = await server.videos.list()
524 503
525 const videos = res.body.data 504 const video0 = data.find(v => v.uuid === localVideosServer3[0])
526 const video0 = videos.find(v => v.uuid === localVideosServer3[0]) 505 const video1 = data.find(v => v.uuid === localVideosServer3[1])
527 const video1 = videos.find(v => v.uuid === localVideosServer3[1])
528 506
529 expect(video0.views).to.equal(3) 507 expect(video0.views).to.equal(3)
530 expect(video1.views).to.equal(1) 508 expect(video1.views).to.equal(1)
@@ -535,16 +513,16 @@ describe('Test multiple servers', function () {
535 this.timeout(45000) 513 this.timeout(45000)
536 514
537 const tasks: Promise<any>[] = [] 515 const tasks: Promise<any>[] = []
538 tasks.push(viewVideo(servers[0].url, remoteVideosServer1[0])) 516 tasks.push(servers[0].videos.view({ id: remoteVideosServer1[0] }))
539 tasks.push(viewVideo(servers[1].url, remoteVideosServer2[0])) 517 tasks.push(servers[1].videos.view({ id: remoteVideosServer2[0] }))
540 tasks.push(viewVideo(servers[1].url, remoteVideosServer2[0])) 518 tasks.push(servers[1].videos.view({ id: remoteVideosServer2[0] }))
541 tasks.push(viewVideo(servers[2].url, remoteVideosServer3[0])) 519 tasks.push(servers[2].videos.view({ id: remoteVideosServer3[0] }))
542 tasks.push(viewVideo(servers[2].url, remoteVideosServer3[1])) 520 tasks.push(servers[2].videos.view({ id: remoteVideosServer3[1] }))
543 tasks.push(viewVideo(servers[2].url, remoteVideosServer3[1])) 521 tasks.push(servers[2].videos.view({ id: remoteVideosServer3[1] }))
544 tasks.push(viewVideo(servers[2].url, remoteVideosServer3[1])) 522 tasks.push(servers[2].videos.view({ id: remoteVideosServer3[1] }))
545 tasks.push(viewVideo(servers[2].url, localVideosServer3[1])) 523 tasks.push(servers[2].videos.view({ id: localVideosServer3[1] }))
546 tasks.push(viewVideo(servers[2].url, localVideosServer3[1])) 524 tasks.push(servers[2].videos.view({ id: localVideosServer3[1] }))
547 tasks.push(viewVideo(servers[2].url, localVideosServer3[1])) 525 tasks.push(servers[2].videos.view({ id: localVideosServer3[1] }))
548 526
549 await Promise.all(tasks) 527 await Promise.all(tasks)
550 528
@@ -558,18 +536,16 @@ describe('Test multiple servers', function () {
558 let baseVideos = null 536 let baseVideos = null
559 537
560 for (const server of servers) { 538 for (const server of servers) {
561 const res = await getVideosList(server.url) 539 const { data } = await server.videos.list()
562
563 const videos = res.body.data
564 540
565 // Initialize base videos for future comparisons 541 // Initialize base videos for future comparisons
566 if (baseVideos === null) { 542 if (baseVideos === null) {
567 baseVideos = videos 543 baseVideos = data
568 continue 544 continue
569 } 545 }
570 546
571 for (const baseVideo of baseVideos) { 547 for (const baseVideo of baseVideos) {
572 const sameVideo = videos.find(video => video.name === baseVideo.name) 548 const sameVideo = data.find(video => video.name === baseVideo.name)
573 expect(baseVideo.views).to.equal(sameVideo.views) 549 expect(baseVideo.views).to.equal(sameVideo.views)
574 } 550 }
575 } 551 }
@@ -578,35 +554,34 @@ describe('Test multiple servers', function () {
578 it('Should like and dislikes videos on different services', async function () { 554 it('Should like and dislikes videos on different services', async function () {
579 this.timeout(50000) 555 this.timeout(50000)
580 556
581 await rateVideo(servers[0].url, servers[0].accessToken, remoteVideosServer1[0], 'like') 557 await servers[0].videos.rate({ id: remoteVideosServer1[0], rating: 'like' })
582 await wait(500) 558 await wait(500)
583 await rateVideo(servers[0].url, servers[0].accessToken, remoteVideosServer1[0], 'dislike') 559 await servers[0].videos.rate({ id: remoteVideosServer1[0], rating: 'dislike' })
584 await wait(500) 560 await wait(500)
585 await rateVideo(servers[0].url, servers[0].accessToken, remoteVideosServer1[0], 'like') 561 await servers[0].videos.rate({ id: remoteVideosServer1[0], rating: 'like' })
586 await rateVideo(servers[2].url, servers[2].accessToken, localVideosServer3[1], 'like') 562 await servers[2].videos.rate({ id: localVideosServer3[1], rating: 'like' })
587 await wait(500) 563 await wait(500)
588 await rateVideo(servers[2].url, servers[2].accessToken, localVideosServer3[1], 'dislike') 564 await servers[2].videos.rate({ id: localVideosServer3[1], rating: 'dislike' })
589 await rateVideo(servers[2].url, servers[2].accessToken, remoteVideosServer3[1], 'dislike') 565 await servers[2].videos.rate({ id: remoteVideosServer3[1], rating: 'dislike' })
590 await wait(500) 566 await wait(500)
591 await rateVideo(servers[2].url, servers[2].accessToken, remoteVideosServer3[0], 'like') 567 await servers[2].videos.rate({ id: remoteVideosServer3[0], rating: 'like' })
592 568
593 await waitJobs(servers) 569 await waitJobs(servers)
594 await wait(5000) 570 await wait(5000)
571 await waitJobs(servers)
595 572
596 let baseVideos = null 573 let baseVideos = null
597 for (const server of servers) { 574 for (const server of servers) {
598 const res = await getVideosList(server.url) 575 const { data } = await server.videos.list()
599
600 const videos = res.body.data
601 576
602 // Initialize base videos for future comparisons 577 // Initialize base videos for future comparisons
603 if (baseVideos === null) { 578 if (baseVideos === null) {
604 baseVideos = videos 579 baseVideos = data
605 continue 580 continue
606 } 581 }
607 582
608 for (const baseVideo of baseVideos) { 583 for (const baseVideo of baseVideos) {
609 const sameVideo = videos.find(video => video.name === baseVideo.name) 584 const sameVideo = data.find(video => video.name === baseVideo.name)
610 expect(baseVideo.likes).to.equal(sameVideo.likes) 585 expect(baseVideo.likes).to.equal(sameVideo.likes)
611 expect(baseVideo.dislikes).to.equal(sameVideo.dislikes) 586 expect(baseVideo.dislikes).to.equal(sameVideo.dislikes)
612 } 587 }
@@ -632,7 +607,7 @@ describe('Test multiple servers', function () {
632 previewfile: 'preview.jpg' 607 previewfile: 'preview.jpg'
633 } 608 }
634 609
635 await updateVideo(servers[2].url, servers[2].accessToken, toRemove[0].id, attributes) 610 await servers[2].videos.update({ id: toRemove[0].id, attributes })
636 611
637 await waitJobs(servers) 612 await waitJobs(servers)
638 }) 613 })
@@ -641,10 +616,9 @@ describe('Test multiple servers', function () {
641 this.timeout(10000) 616 this.timeout(10000)
642 617
643 for (const server of servers) { 618 for (const server of servers) {
644 const res = await getVideosList(server.url) 619 const { data } = await server.videos.list()
645 620
646 const videos = res.body.data 621 const videoUpdated = data.find(video => video.name === 'my super video updated')
647 const videoUpdated = videos.find(video => video.name === 'my super video updated')
648 expect(!!videoUpdated).to.be.true 622 expect(!!videoUpdated).to.be.true
649 623
650 const isLocal = server.url === 'http://localhost:' + servers[2].port 624 const isLocal = server.url === 'http://localhost:' + servers[2].port
@@ -683,49 +657,46 @@ describe('Test multiple servers', function () {
683 thumbnailfile: 'thumbnail', 657 thumbnailfile: 'thumbnail',
684 previewfile: 'preview' 658 previewfile: 'preview'
685 } 659 }
686 await completeVideoCheck(server.url, videoUpdated, checkAttributes) 660 await completeVideoCheck(server, videoUpdated, checkAttributes)
687 } 661 }
688 }) 662 })
689 663
690 it('Should remove the videos 3 and 3-2 by asking server 3', async function () { 664 it('Should remove the videos 3 and 3-2 by asking server 3', async function () {
691 this.timeout(10000) 665 this.timeout(10000)
692 666
693 await removeVideo(servers[2].url, servers[2].accessToken, toRemove[0].id) 667 await servers[2].videos.remove({ id: toRemove[0].id })
694 await removeVideo(servers[2].url, servers[2].accessToken, toRemove[1].id) 668 await servers[2].videos.remove({ id: toRemove[1].id })
695 669
696 await waitJobs(servers) 670 await waitJobs(servers)
697 }) 671 })
698 672
699 it('Should not have files of videos 3 and 3-2 on each server', async function () { 673 it('Should not have files of videos 3 and 3-2 on each server', async function () {
700 for (const server of servers) { 674 for (const server of servers) {
701 await checkVideoFilesWereRemoved(toRemove[0].uuid, server.internalServerNumber) 675 await checkVideoFilesWereRemoved(toRemove[0].uuid, server)
702 await checkVideoFilesWereRemoved(toRemove[1].uuid, server.internalServerNumber) 676 await checkVideoFilesWereRemoved(toRemove[1].uuid, server)
703 } 677 }
704 }) 678 })
705 679
706 it('Should have videos 1 and 3 on each server', async function () { 680 it('Should have videos 1 and 3 on each server', async function () {
707 for (const server of servers) { 681 for (const server of servers) {
708 const res = await getVideosList(server.url) 682 const { data } = await server.videos.list()
709 683
710 const videos = res.body.data 684 expect(data).to.be.an('array')
711 expect(videos).to.be.an('array') 685 expect(data.length).to.equal(2)
712 expect(videos.length).to.equal(2) 686 expect(data[0].name).not.to.equal(data[1].name)
713 expect(videos[0].name).not.to.equal(videos[1].name) 687 expect(data[0].name).not.to.equal(toRemove[0].name)
714 expect(videos[0].name).not.to.equal(toRemove[0].name) 688 expect(data[1].name).not.to.equal(toRemove[0].name)
715 expect(videos[1].name).not.to.equal(toRemove[0].name) 689 expect(data[0].name).not.to.equal(toRemove[1].name)
716 expect(videos[0].name).not.to.equal(toRemove[1].name) 690 expect(data[1].name).not.to.equal(toRemove[1].name)
717 expect(videos[1].name).not.to.equal(toRemove[1].name) 691
718 692 videoUUID = data.find(video => video.name === 'my super name for server 1').uuid
719 videoUUID = videos.find(video => video.name === 'my super name for server 1').uuid
720 } 693 }
721 }) 694 })
722 695
723 it('Should get the same video by UUID on each server', async function () { 696 it('Should get the same video by UUID on each server', async function () {
724 let baseVideo = null 697 let baseVideo = null
725 for (const server of servers) { 698 for (const server of servers) {
726 const res = await getVideo(server.url, videoUUID) 699 const video = await server.videos.get({ id: videoUUID })
727
728 const video = res.body
729 700
730 if (baseVideo === null) { 701 if (baseVideo === null) {
731 baseVideo = video 702 baseVideo = video
@@ -748,8 +719,7 @@ describe('Test multiple servers', function () {
748 719
749 it('Should get the preview from each server', async function () { 720 it('Should get the preview from each server', async function () {
750 for (const server of servers) { 721 for (const server of servers) {
751 const res = await getVideo(server.url, videoUUID) 722 const video = await server.videos.get({ id: videoUUID })
752 const video = res.body
753 723
754 await testImage(server.url, 'video_short1-preview.webm', video.previewPath) 724 await testImage(server.url, 'video_short1-preview.webm', video.previewPath)
755 } 725 }
@@ -764,36 +734,36 @@ describe('Test multiple servers', function () {
764 734
765 { 735 {
766 const text = 'my super first comment' 736 const text = 'my super first comment'
767 await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoUUID, text) 737 await servers[0].comments.createThread({ videoId: videoUUID, text })
768 } 738 }
769 739
770 { 740 {
771 const text = 'my super second comment' 741 const text = 'my super second comment'
772 await addVideoCommentThread(servers[2].url, servers[2].accessToken, videoUUID, text) 742 await servers[2].comments.createThread({ videoId: videoUUID, text })
773 } 743 }
774 744
775 await waitJobs(servers) 745 await waitJobs(servers)
776 746
777 { 747 {
778 const threadId = await findCommentId(servers[1].url, videoUUID, 'my super first comment') 748 const threadId = await servers[1].comments.findCommentId({ videoId: videoUUID, text: 'my super first comment' })
779 749
780 const text = 'my super answer to thread 1' 750 const text = 'my super answer to thread 1'
781 await addVideoCommentReply(servers[1].url, servers[1].accessToken, videoUUID, threadId, text) 751 await servers[1].comments.addReply({ videoId: videoUUID, toCommentId: threadId, text })
782 } 752 }
783 753
784 await waitJobs(servers) 754 await waitJobs(servers)
785 755
786 { 756 {
787 const threadId = await findCommentId(servers[2].url, videoUUID, 'my super first comment') 757 const threadId = await servers[2].comments.findCommentId({ videoId: videoUUID, text: 'my super first comment' })
788 758
789 const res2 = await getVideoThreadComments(servers[2].url, videoUUID, threadId) 759 const body = await servers[2].comments.getThread({ videoId: videoUUID, threadId })
790 const childCommentId = res2.body.children[0].comment.id 760 const childCommentId = body.children[0].comment.id
791 761
792 const text3 = 'my second answer to thread 1' 762 const text3 = 'my second answer to thread 1'
793 await addVideoCommentReply(servers[2].url, servers[2].accessToken, videoUUID, threadId, text3) 763 await servers[2].comments.addReply({ videoId: videoUUID, toCommentId: threadId, text: text3 })
794 764
795 const text2 = 'my super answer to answer of thread 1' 765 const text2 = 'my super answer to answer of thread 1'
796 await addVideoCommentReply(servers[2].url, servers[2].accessToken, videoUUID, childCommentId, text2) 766 await servers[2].comments.addReply({ videoId: videoUUID, toCommentId: childCommentId, text: text2 })
797 } 767 }
798 768
799 await waitJobs(servers) 769 await waitJobs(servers)
@@ -801,14 +771,14 @@ describe('Test multiple servers', function () {
801 771
802 it('Should have these threads', async function () { 772 it('Should have these threads', async function () {
803 for (const server of servers) { 773 for (const server of servers) {
804 const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5) 774 const body = await server.comments.listThreads({ videoId: videoUUID })
805 775
806 expect(res.body.total).to.equal(2) 776 expect(body.total).to.equal(2)
807 expect(res.body.data).to.be.an('array') 777 expect(body.data).to.be.an('array')
808 expect(res.body.data).to.have.lengthOf(2) 778 expect(body.data).to.have.lengthOf(2)
809 779
810 { 780 {
811 const comment: VideoComment = res.body.data.find(c => c.text === 'my super first comment') 781 const comment = body.data.find(c => c.text === 'my super first comment')
812 expect(comment).to.not.be.undefined 782 expect(comment).to.not.be.undefined
813 expect(comment.inReplyToCommentId).to.be.null 783 expect(comment.inReplyToCommentId).to.be.null
814 expect(comment.account.name).to.equal('root') 784 expect(comment.account.name).to.equal('root')
@@ -819,7 +789,7 @@ describe('Test multiple servers', function () {
819 } 789 }
820 790
821 { 791 {
822 const comment: VideoComment = res.body.data.find(c => c.text === 'my super second comment') 792 const comment = body.data.find(c => c.text === 'my super second comment')
823 expect(comment).to.not.be.undefined 793 expect(comment).to.not.be.undefined
824 expect(comment.inReplyToCommentId).to.be.null 794 expect(comment.inReplyToCommentId).to.be.null
825 expect(comment.account.name).to.equal('root') 795 expect(comment.account.name).to.equal('root')
@@ -833,12 +803,11 @@ describe('Test multiple servers', function () {
833 803
834 it('Should have these comments', async function () { 804 it('Should have these comments', async function () {
835 for (const server of servers) { 805 for (const server of servers) {
836 const res1 = await getVideoCommentThreads(server.url, videoUUID, 0, 5) 806 const body = await server.comments.listThreads({ videoId: videoUUID })
837 const threadId = res1.body.data.find(c => c.text === 'my super first comment').id 807 const threadId = body.data.find(c => c.text === 'my super first comment').id
838 808
839 const res2 = await getVideoThreadComments(server.url, videoUUID, threadId) 809 const tree = await server.comments.getThread({ videoId: videoUUID, threadId })
840 810
841 const tree: VideoCommentThreadTree = res2.body
842 expect(tree.comment.text).equal('my super first comment') 811 expect(tree.comment.text).equal('my super first comment')
843 expect(tree.comment.account.name).equal('root') 812 expect(tree.comment.account.name).equal('root')
844 expect(tree.comment.account.host).equal('localhost:' + servers[0].port) 813 expect(tree.comment.account.host).equal('localhost:' + servers[0].port)
@@ -867,19 +836,17 @@ describe('Test multiple servers', function () {
867 it('Should delete a reply', async function () { 836 it('Should delete a reply', async function () {
868 this.timeout(10000) 837 this.timeout(10000)
869 838
870 await deleteVideoComment(servers[2].url, servers[2].accessToken, videoUUID, childOfFirstChild.comment.id) 839 await servers[2].comments.delete({ videoId: videoUUID, commentId: childOfFirstChild.comment.id })
871 840
872 await waitJobs(servers) 841 await waitJobs(servers)
873 }) 842 })
874 843
875 it('Should have this comment marked as deleted', async function () { 844 it('Should have this comment marked as deleted', async function () {
876 for (const server of servers) { 845 for (const server of servers) {
877 const res1 = await getVideoCommentThreads(server.url, videoUUID, 0, 5) 846 const { data } = await server.comments.listThreads({ videoId: videoUUID })
878 const threadId = res1.body.data.find(c => c.text === 'my super first comment').id 847 const threadId = data.find(c => c.text === 'my super first comment').id
879
880 const res2 = await getVideoThreadComments(server.url, videoUUID, threadId)
881 848
882 const tree: VideoCommentThreadTree = res2.body 849 const tree = await server.comments.getThread({ videoId: videoUUID, threadId })
883 expect(tree.comment.text).equal('my super first comment') 850 expect(tree.comment.text).equal('my super first comment')
884 851
885 const firstChild = tree.children[0] 852 const firstChild = tree.children[0]
@@ -900,23 +867,23 @@ describe('Test multiple servers', function () {
900 it('Should delete the thread comments', async function () { 867 it('Should delete the thread comments', async function () {
901 this.timeout(10000) 868 this.timeout(10000)
902 869
903 const res = await getVideoCommentThreads(servers[0].url, videoUUID, 0, 5) 870 const { data } = await servers[0].comments.listThreads({ videoId: videoUUID })
904 const threadId = res.body.data.find(c => c.text === 'my super first comment').id 871 const commentId = data.find(c => c.text === 'my super first comment').id
905 await deleteVideoComment(servers[0].url, servers[0].accessToken, videoUUID, threadId) 872 await servers[0].comments.delete({ videoId: videoUUID, commentId })
906 873
907 await waitJobs(servers) 874 await waitJobs(servers)
908 }) 875 })
909 876
910 it('Should have the threads marked as deleted on other servers too', async function () { 877 it('Should have the threads marked as deleted on other servers too', async function () {
911 for (const server of servers) { 878 for (const server of servers) {
912 const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5) 879 const body = await server.comments.listThreads({ videoId: videoUUID })
913 880
914 expect(res.body.total).to.equal(2) 881 expect(body.total).to.equal(2)
915 expect(res.body.data).to.be.an('array') 882 expect(body.data).to.be.an('array')
916 expect(res.body.data).to.have.lengthOf(2) 883 expect(body.data).to.have.lengthOf(2)
917 884
918 { 885 {
919 const comment: VideoComment = res.body.data[0] 886 const comment = body.data[0]
920 expect(comment).to.not.be.undefined 887 expect(comment).to.not.be.undefined
921 expect(comment.inReplyToCommentId).to.be.null 888 expect(comment.inReplyToCommentId).to.be.null
922 expect(comment.account.name).to.equal('root') 889 expect(comment.account.name).to.equal('root')
@@ -927,7 +894,7 @@ describe('Test multiple servers', function () {
927 } 894 }
928 895
929 { 896 {
930 const deletedComment: VideoComment = res.body.data[1] 897 const deletedComment = body.data[1]
931 expect(deletedComment).to.not.be.undefined 898 expect(deletedComment).to.not.be.undefined
932 expect(deletedComment.isDeleted).to.be.true 899 expect(deletedComment.isDeleted).to.be.true
933 expect(deletedComment.deletedAt).to.not.be.null 900 expect(deletedComment.deletedAt).to.not.be.null
@@ -945,22 +912,22 @@ describe('Test multiple servers', function () {
945 it('Should delete a remote thread by the origin server', async function () { 912 it('Should delete a remote thread by the origin server', async function () {
946 this.timeout(5000) 913 this.timeout(5000)
947 914
948 const res = await getVideoCommentThreads(servers[0].url, videoUUID, 0, 5) 915 const { data } = await servers[0].comments.listThreads({ videoId: videoUUID })
949 const threadId = res.body.data.find(c => c.text === 'my super second comment').id 916 const commentId = data.find(c => c.text === 'my super second comment').id
950 await deleteVideoComment(servers[0].url, servers[0].accessToken, videoUUID, threadId) 917 await servers[0].comments.delete({ videoId: videoUUID, commentId })
951 918
952 await waitJobs(servers) 919 await waitJobs(servers)
953 }) 920 })
954 921
955 it('Should have the threads marked as deleted on other servers too', async function () { 922 it('Should have the threads marked as deleted on other servers too', async function () {
956 for (const server of servers) { 923 for (const server of servers) {
957 const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5) 924 const body = await server.comments.listThreads({ videoId: videoUUID })
958 925
959 expect(res.body.total).to.equal(2) 926 expect(body.total).to.equal(2)
960 expect(res.body.data).to.have.lengthOf(2) 927 expect(body.data).to.have.lengthOf(2)
961 928
962 { 929 {
963 const comment: VideoComment = res.body.data[0] 930 const comment = body.data[0]
964 expect(comment.text).to.equal('') 931 expect(comment.text).to.equal('')
965 expect(comment.isDeleted).to.be.true 932 expect(comment.isDeleted).to.be.true
966 expect(comment.createdAt).to.not.be.null 933 expect(comment.createdAt).to.not.be.null
@@ -970,7 +937,7 @@ describe('Test multiple servers', function () {
970 } 937 }
971 938
972 { 939 {
973 const comment: VideoComment = res.body.data[1] 940 const comment = body.data[1]
974 expect(comment.text).to.equal('') 941 expect(comment.text).to.equal('')
975 expect(comment.isDeleted).to.be.true 942 expect(comment.isDeleted).to.be.true
976 expect(comment.createdAt).to.not.be.null 943 expect(comment.createdAt).to.not.be.null
@@ -989,17 +956,17 @@ describe('Test multiple servers', function () {
989 downloadEnabled: false 956 downloadEnabled: false
990 } 957 }
991 958
992 await updateVideo(servers[0].url, servers[0].accessToken, videoUUID, attributes) 959 await servers[0].videos.update({ id: videoUUID, attributes })
993 960
994 await waitJobs(servers) 961 await waitJobs(servers)
995 962
996 for (const server of servers) { 963 for (const server of servers) {
997 const res = await getVideo(server.url, videoUUID) 964 const video = await server.videos.get({ id: videoUUID })
998 expect(res.body.commentsEnabled).to.be.false 965 expect(video.commentsEnabled).to.be.false
999 expect(res.body.downloadEnabled).to.be.false 966 expect(video.downloadEnabled).to.be.false
1000 967
1001 const text = 'my super forbidden comment' 968 const text = 'my super forbidden comment'
1002 await addVideoCommentThread(server.url, server.accessToken, videoUUID, text, HttpStatusCode.CONFLICT_409) 969 await server.comments.createThread({ videoId: videoUUID, text, expectedStatus: HttpStatusCode.CONFLICT_409 })
1003 } 970 }
1004 }) 971 })
1005 }) 972 })
@@ -1024,8 +991,8 @@ describe('Test multiple servers', function () {
1024 await waitJobs(servers) 991 await waitJobs(servers)
1025 992
1026 for (const server of servers) { 993 for (const server of servers) {
1027 const res = await getVideosList(server.url) 994 const { data } = await server.videos.list()
1028 const video = res.body.data.find(v => v.name === 'minimum parameters') 995 const video = data.find(v => v.name === 'minimum parameters')
1029 996
1030 const isLocal = server.url === 'http://localhost:' + servers[1].port 997 const isLocal = server.url === 'http://localhost:' + servers[1].port
1031 const checkAttributes = { 998 const checkAttributes = {
@@ -1072,7 +1039,7 @@ describe('Test multiple servers', function () {
1072 } 1039 }
1073 ] 1040 ]
1074 } 1041 }
1075 await completeVideoCheck(server.url, video, checkAttributes) 1042 await completeVideoCheck(server, video, checkAttributes)
1076 } 1043 }
1077 }) 1044 })
1078 }) 1045 })
diff --git a/server/tests/api/videos/resumable-upload.ts b/server/tests/api/videos/resumable-upload.ts
index 4fc3317df..13e47c85e 100644
--- a/server/tests/api/videos/resumable-upload.ts
+++ b/server/tests/api/videos/resumable-upload.ts
@@ -4,22 +4,15 @@ import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5import { pathExists, readdir, stat } from 'fs-extra' 5import { pathExists, readdir, stat } from 'fs-extra'
6import { join } from 'path' 6import { join } from 'path'
7import { HttpStatusCode } from '@shared/core-utils'
8import { 7import {
9 buildAbsoluteFixturePath, 8 buildAbsoluteFixturePath,
10 buildServerDirectory,
11 cleanupTests, 9 cleanupTests,
12 flushAndRunServer, 10 createSingleServer,
13 getMyUserInformation, 11 PeerTubeServer,
14 prepareResumableUpload,
15 sendDebugCommand,
16 sendResumableChunks,
17 ServerInfo,
18 setAccessTokensToServers, 12 setAccessTokensToServers,
19 setDefaultVideoChannel, 13 setDefaultVideoChannel
20 updateUser
21} from '@shared/extra-utils' 14} from '@shared/extra-utils'
22import { MyUser, VideoPrivacy } from '@shared/models' 15import { HttpStatusCode, VideoPrivacy } from '@shared/models'
23 16
24const expect = chai.expect 17const expect = chai.expect
25 18
@@ -27,7 +20,7 @@ const expect = chai.expect
27 20
28describe('Test resumable upload', function () { 21describe('Test resumable upload', function () {
29 const defaultFixture = 'video_short.mp4' 22 const defaultFixture = 'video_short.mp4'
30 let server: ServerInfo 23 let server: PeerTubeServer
31 let rootId: number 24 let rootId: number
32 25
33 async function buildSize (fixture: string, size?: number) { 26 async function buildSize (fixture: string, size?: number) {
@@ -42,14 +35,14 @@ describe('Test resumable upload', function () {
42 35
43 const attributes = { 36 const attributes = {
44 name: 'video', 37 name: 'video',
45 channelId: server.videoChannel.id, 38 channelId: server.store.channel.id,
46 privacy: VideoPrivacy.PUBLIC, 39 privacy: VideoPrivacy.PUBLIC,
47 fixture: defaultFixture 40 fixture: defaultFixture
48 } 41 }
49 42
50 const mimetype = 'video/mp4' 43 const mimetype = 'video/mp4'
51 44
52 const res = await prepareResumableUpload({ url: server.url, token: server.accessToken, attributes, size, mimetype }) 45 const res = await server.videos.prepareResumableUpload({ attributes, size, mimetype })
53 46
54 return res.header['location'].split('?')[1] 47 return res.header['location'].split('?')[1]
55 } 48 }
@@ -67,15 +60,13 @@ describe('Test resumable upload', function () {
67 const size = await buildSize(defaultFixture, options.size) 60 const size = await buildSize(defaultFixture, options.size)
68 const absoluteFilePath = buildAbsoluteFixturePath(defaultFixture) 61 const absoluteFilePath = buildAbsoluteFixturePath(defaultFixture)
69 62
70 return sendResumableChunks({ 63 return server.videos.sendResumableChunks({
71 url: server.url,
72 token: server.accessToken,
73 pathUploadId, 64 pathUploadId,
74 videoFilePath: absoluteFilePath, 65 videoFilePath: absoluteFilePath,
75 size, 66 size,
76 contentLength, 67 contentLength,
77 contentRangeBuilder, 68 contentRangeBuilder,
78 specialStatus: expectedStatus 69 expectedStatus
79 }) 70 })
80 } 71 }
81 72
@@ -83,7 +74,7 @@ describe('Test resumable upload', function () {
83 const uploadId = uploadIdArg.replace(/^upload_id=/, '') 74 const uploadId = uploadIdArg.replace(/^upload_id=/, '')
84 75
85 const subPath = join('tmp', 'resumable-uploads', uploadId) 76 const subPath = join('tmp', 'resumable-uploads', uploadId)
86 const filePath = buildServerDirectory(server, subPath) 77 const filePath = server.servers.buildDirectory(subPath)
87 const exists = await pathExists(filePath) 78 const exists = await pathExists(filePath)
88 79
89 if (expectedSize === null) { 80 if (expectedSize === null) {
@@ -98,7 +89,7 @@ describe('Test resumable upload', function () {
98 89
99 async function countResumableUploads () { 90 async function countResumableUploads () {
100 const subPath = join('tmp', 'resumable-uploads') 91 const subPath = join('tmp', 'resumable-uploads')
101 const filePath = buildServerDirectory(server, subPath) 92 const filePath = server.servers.buildDirectory(subPath)
102 93
103 const files = await readdir(filePath) 94 const files = await readdir(filePath)
104 return files.length 95 return files.length
@@ -107,19 +98,14 @@ describe('Test resumable upload', function () {
107 before(async function () { 98 before(async function () {
108 this.timeout(30000) 99 this.timeout(30000)
109 100
110 server = await flushAndRunServer(1) 101 server = await createSingleServer(1)
111 await setAccessTokensToServers([ server ]) 102 await setAccessTokensToServers([ server ])
112 await setDefaultVideoChannel([ server ]) 103 await setDefaultVideoChannel([ server ])
113 104
114 const res = await getMyUserInformation(server.url, server.accessToken) 105 const body = await server.users.getMyInfo()
115 rootId = (res.body as MyUser).id 106 rootId = body.id
116 107
117 await updateUser({ 108 await server.users.update({ userId: rootId, videoQuota: 10_000_000 })
118 url: server.url,
119 userId: rootId,
120 accessToken: server.accessToken,
121 videoQuota: 10_000_000
122 })
123 }) 109 })
124 110
125 describe('Directory cleaning', function () { 111 describe('Directory cleaning', function () {
@@ -138,13 +124,13 @@ describe('Test resumable upload', function () {
138 }) 124 })
139 125
140 it('Should not delete recent uploads', async function () { 126 it('Should not delete recent uploads', async function () {
141 await sendDebugCommand(server.url, server.accessToken, { command: 'remove-dandling-resumable-uploads' }) 127 await server.debug.sendCommand({ body: { command: 'remove-dandling-resumable-uploads' } })
142 128
143 expect(await countResumableUploads()).to.equal(2) 129 expect(await countResumableUploads()).to.equal(2)
144 }) 130 })
145 131
146 it('Should delete old uploads', async function () { 132 it('Should delete old uploads', async function () {
147 await sendDebugCommand(server.url, server.accessToken, { command: 'remove-dandling-resumable-uploads' }) 133 await server.debug.sendCommand({ body: { command: 'remove-dandling-resumable-uploads' } })
148 134
149 expect(await countResumableUploads()).to.equal(0) 135 expect(await countResumableUploads()).to.equal(0)
150 }) 136 })
diff --git a/server/tests/api/videos/single-server.ts b/server/tests/api/videos/single-server.ts
index 1058a1e9c..c0535be09 100644
--- a/server/tests/api/videos/single-server.ts
+++ b/server/tests/api/videos/single-server.ts
@@ -2,43 +2,26 @@
2 2
3import 'mocha' 3import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5import { keyBy } from 'lodash'
6
7import { 5import {
8 checkVideoFilesWereRemoved, 6 checkVideoFilesWereRemoved,
9 cleanupTests, 7 cleanupTests,
10 completeVideoCheck, 8 completeVideoCheck,
11 flushAndRunServer, 9 createSingleServer,
12 getVideo, 10 PeerTubeServer,
13 getVideoCategories,
14 getVideoLanguages,
15 getVideoLicences,
16 getVideoPrivacies,
17 getVideosList,
18 getVideosListPagination,
19 getVideosListSort,
20 getVideosWithFilters,
21 rateVideo,
22 removeVideo,
23 ServerInfo,
24 setAccessTokensToServers, 11 setAccessTokensToServers,
25 testImage, 12 testImage,
26 updateVideo,
27 uploadVideo,
28 viewVideo,
29 wait 13 wait
30} from '../../../../shared/extra-utils' 14} from '@shared/extra-utils'
31import { VideoPrivacy } from '../../../../shared/models/videos' 15import { Video, VideoPrivacy } from '@shared/models'
32import { HttpStatusCode } from '@shared/core-utils'
33 16
34const expect = chai.expect 17const expect = chai.expect
35 18
36describe('Test a single server', function () { 19describe('Test a single server', function () {
37 20
38 function runSuite (mode: 'legacy' | 'resumable') { 21 function runSuite (mode: 'legacy' | 'resumable') {
39 let server: ServerInfo = null 22 let server: PeerTubeServer = null
40 let videoId = -1 23 let videoId: number | string
41 let videoId2 = -1 24 let videoId2: string
42 let videoUUID = '' 25 let videoUUID = ''
43 let videosListBase: any[] = null 26 let videosListBase: any[] = null
44 27
@@ -111,134 +94,122 @@ describe('Test a single server', function () {
111 before(async function () { 94 before(async function () {
112 this.timeout(30000) 95 this.timeout(30000)
113 96
114 server = await flushAndRunServer(1) 97 server = await createSingleServer(1)
115 98
116 await setAccessTokensToServers([ server ]) 99 await setAccessTokensToServers([ server ])
117 }) 100 })
118 101
119 it('Should list video categories', async function () { 102 it('Should list video categories', async function () {
120 const res = await getVideoCategories(server.url) 103 const categories = await server.videos.getCategories()
121
122 const categories = res.body
123 expect(Object.keys(categories)).to.have.length.above(10) 104 expect(Object.keys(categories)).to.have.length.above(10)
124 105
125 expect(categories[11]).to.equal('News & Politics') 106 expect(categories[11]).to.equal('News & Politics')
126 }) 107 })
127 108
128 it('Should list video licences', async function () { 109 it('Should list video licences', async function () {
129 const res = await getVideoLicences(server.url) 110 const licences = await server.videos.getLicences()
130
131 const licences = res.body
132 expect(Object.keys(licences)).to.have.length.above(5) 111 expect(Object.keys(licences)).to.have.length.above(5)
133 112
134 expect(licences[3]).to.equal('Attribution - No Derivatives') 113 expect(licences[3]).to.equal('Attribution - No Derivatives')
135 }) 114 })
136 115
137 it('Should list video languages', async function () { 116 it('Should list video languages', async function () {
138 const res = await getVideoLanguages(server.url) 117 const languages = await server.videos.getLanguages()
139
140 const languages = res.body
141 expect(Object.keys(languages)).to.have.length.above(5) 118 expect(Object.keys(languages)).to.have.length.above(5)
142 119
143 expect(languages['ru']).to.equal('Russian') 120 expect(languages['ru']).to.equal('Russian')
144 }) 121 })
145 122
146 it('Should list video privacies', async function () { 123 it('Should list video privacies', async function () {
147 const res = await getVideoPrivacies(server.url) 124 const privacies = await server.videos.getPrivacies()
148
149 const privacies = res.body
150 expect(Object.keys(privacies)).to.have.length.at.least(3) 125 expect(Object.keys(privacies)).to.have.length.at.least(3)
151 126
152 expect(privacies[3]).to.equal('Private') 127 expect(privacies[3]).to.equal('Private')
153 }) 128 })
154 129
155 it('Should not have videos', async function () { 130 it('Should not have videos', async function () {
156 const res = await getVideosList(server.url) 131 const { data, total } = await server.videos.list()
157 132
158 expect(res.body.total).to.equal(0) 133 expect(total).to.equal(0)
159 expect(res.body.data).to.be.an('array') 134 expect(data).to.be.an('array')
160 expect(res.body.data.length).to.equal(0) 135 expect(data.length).to.equal(0)
161 }) 136 })
162 137
163 it('Should upload the video', async function () { 138 it('Should upload the video', async function () {
164 this.timeout(10000) 139 this.timeout(10000)
165 140
166 const videoAttributes = { 141 const attributes = {
167 name: 'my super name', 142 name: 'my super name',
168 category: 2, 143 category: 2,
169 nsfw: true, 144 nsfw: true,
170 licence: 6, 145 licence: 6,
171 tags: [ 'tag1', 'tag2', 'tag3' ] 146 tags: [ 'tag1', 'tag2', 'tag3' ]
172 } 147 }
173 const res = await uploadVideo(server.url, server.accessToken, videoAttributes, HttpStatusCode.OK_200, mode) 148 const video = await server.videos.upload({ attributes, mode })
174 expect(res.body.video).to.not.be.undefined 149 expect(video).to.not.be.undefined
175 expect(res.body.video.id).to.equal(1) 150 expect(video.id).to.equal(1)
176 expect(res.body.video.uuid).to.have.length.above(5) 151 expect(video.uuid).to.have.length.above(5)
177 152
178 videoId = res.body.video.id 153 videoId = video.id
179 videoUUID = res.body.video.uuid 154 videoUUID = video.uuid
180 }) 155 })
181 156
182 it('Should get and seed the uploaded video', async function () { 157 it('Should get and seed the uploaded video', async function () {
183 this.timeout(5000) 158 this.timeout(5000)
184 159
185 const res = await getVideosList(server.url) 160 const { data, total } = await server.videos.list()
186 161
187 expect(res.body.total).to.equal(1) 162 expect(total).to.equal(1)
188 expect(res.body.data).to.be.an('array') 163 expect(data).to.be.an('array')
189 expect(res.body.data.length).to.equal(1) 164 expect(data.length).to.equal(1)
190 165
191 const video = res.body.data[0] 166 const video = data[0]
192 await completeVideoCheck(server.url, video, getCheckAttributes()) 167 await completeVideoCheck(server, video, getCheckAttributes())
193 }) 168 })
194 169
195 it('Should get the video by UUID', async function () { 170 it('Should get the video by UUID', async function () {
196 this.timeout(5000) 171 this.timeout(5000)
197 172
198 const res = await getVideo(server.url, videoUUID) 173 const video = await server.videos.get({ id: videoUUID })
199 174 await completeVideoCheck(server, video, getCheckAttributes())
200 const video = res.body
201 await completeVideoCheck(server.url, video, getCheckAttributes())
202 }) 175 })
203 176
204 it('Should have the views updated', async function () { 177 it('Should have the views updated', async function () {
205 this.timeout(20000) 178 this.timeout(20000)
206 179
207 await viewVideo(server.url, videoId) 180 await server.videos.view({ id: videoId })
208 await viewVideo(server.url, videoId) 181 await server.videos.view({ id: videoId })
209 await viewVideo(server.url, videoId) 182 await server.videos.view({ id: videoId })
210 183
211 await wait(1500) 184 await wait(1500)
212 185
213 await viewVideo(server.url, videoId) 186 await server.videos.view({ id: videoId })
214 await viewVideo(server.url, videoId) 187 await server.videos.view({ id: videoId })
215 188
216 await wait(1500) 189 await wait(1500)
217 190
218 await viewVideo(server.url, videoId) 191 await server.videos.view({ id: videoId })
219 await viewVideo(server.url, videoId) 192 await server.videos.view({ id: videoId })
220 193
221 // Wait the repeatable job 194 // Wait the repeatable job
222 await wait(8000) 195 await wait(8000)
223 196
224 const res = await getVideo(server.url, videoId) 197 const video = await server.videos.get({ id: videoId })
225
226 const video = res.body
227 expect(video.views).to.equal(3) 198 expect(video.views).to.equal(3)
228 }) 199 })
229 200
230 it('Should remove the video', async function () { 201 it('Should remove the video', async function () {
231 await removeVideo(server.url, server.accessToken, videoId) 202 await server.videos.remove({ id: videoId })
232 203
233 await checkVideoFilesWereRemoved(videoUUID, 1) 204 await checkVideoFilesWereRemoved(videoUUID, server)
234 }) 205 })
235 206
236 it('Should not have videos', async function () { 207 it('Should not have videos', async function () {
237 const res = await getVideosList(server.url) 208 const { total, data } = await server.videos.list()
238 209
239 expect(res.body.total).to.equal(0) 210 expect(total).to.equal(0)
240 expect(res.body.data).to.be.an('array') 211 expect(data).to.be.an('array')
241 expect(res.body.data).to.have.lengthOf(0) 212 expect(data).to.have.lengthOf(0)
242 }) 213 })
243 214
244 it('Should upload 6 videos', async function () { 215 it('Should upload 6 videos', async function () {
@@ -250,7 +221,7 @@ describe('Test a single server', function () {
250 ]) 221 ])
251 222
252 for (const video of videos) { 223 for (const video of videos) {
253 const videoAttributes = { 224 const attributes = {
254 name: video + ' name', 225 name: video + ' name',
255 description: video + ' description', 226 description: video + ' description',
256 category: 2, 227 category: 2,
@@ -261,19 +232,20 @@ describe('Test a single server', function () {
261 fixture: video 232 fixture: video
262 } 233 }
263 234
264 await uploadVideo(server.url, server.accessToken, videoAttributes, HttpStatusCode.OK_200, mode) 235 await server.videos.upload({ attributes, mode })
265 } 236 }
266 }) 237 })
267 238
268 it('Should have the correct durations', async function () { 239 it('Should have the correct durations', async function () {
269 const res = await getVideosList(server.url) 240 const { total, data } = await server.videos.list()
241
242 expect(total).to.equal(6)
243 expect(data).to.be.an('array')
244 expect(data).to.have.lengthOf(6)
270 245
271 expect(res.body.total).to.equal(6) 246 const videosByName: { [ name: string ]: Video } = {}
272 const videos = res.body.data 247 data.forEach(v => { videosByName[v.name] = v })
273 expect(videos).to.be.an('array')
274 expect(videos).to.have.lengthOf(6)
275 248
276 const videosByName = keyBy<{ duration: number }>(videos, 'name')
277 expect(videosByName['video_short.mp4 name'].duration).to.equal(5) 249 expect(videosByName['video_short.mp4 name'].duration).to.equal(5)
278 expect(videosByName['video_short.ogv name'].duration).to.equal(5) 250 expect(videosByName['video_short.ogv name'].duration).to.equal(5)
279 expect(videosByName['video_short.webm name'].duration).to.equal(5) 251 expect(videosByName['video_short.webm name'].duration).to.equal(5)
@@ -283,96 +255,87 @@ describe('Test a single server', function () {
283 }) 255 })
284 256
285 it('Should have the correct thumbnails', async function () { 257 it('Should have the correct thumbnails', async function () {
286 const res = await getVideosList(server.url) 258 const { data } = await server.videos.list()
287 259
288 const videos = res.body.data
289 // For the next test 260 // For the next test
290 videosListBase = videos 261 videosListBase = data
291 262
292 for (const video of videos) { 263 for (const video of data) {
293 const videoName = video.name.replace(' name', '') 264 const videoName = video.name.replace(' name', '')
294 await testImage(server.url, videoName, video.thumbnailPath) 265 await testImage(server.url, videoName, video.thumbnailPath)
295 } 266 }
296 }) 267 })
297 268
298 it('Should list only the two first videos', async function () { 269 it('Should list only the two first videos', async function () {
299 const res = await getVideosListPagination(server.url, 0, 2, 'name') 270 const { total, data } = await server.videos.list({ start: 0, count: 2, sort: 'name' })
300 271
301 const videos = res.body.data 272 expect(total).to.equal(6)
302 expect(res.body.total).to.equal(6) 273 expect(data.length).to.equal(2)
303 expect(videos.length).to.equal(2) 274 expect(data[0].name).to.equal(videosListBase[0].name)
304 expect(videos[0].name).to.equal(videosListBase[0].name) 275 expect(data[1].name).to.equal(videosListBase[1].name)
305 expect(videos[1].name).to.equal(videosListBase[1].name)
306 }) 276 })
307 277
308 it('Should list only the next three videos', async function () { 278 it('Should list only the next three videos', async function () {
309 const res = await getVideosListPagination(server.url, 2, 3, 'name') 279 const { total, data } = await server.videos.list({ start: 2, count: 3, sort: 'name' })
310 280
311 const videos = res.body.data 281 expect(total).to.equal(6)
312 expect(res.body.total).to.equal(6) 282 expect(data.length).to.equal(3)
313 expect(videos.length).to.equal(3) 283 expect(data[0].name).to.equal(videosListBase[2].name)
314 expect(videos[0].name).to.equal(videosListBase[2].name) 284 expect(data[1].name).to.equal(videosListBase[3].name)
315 expect(videos[1].name).to.equal(videosListBase[3].name) 285 expect(data[2].name).to.equal(videosListBase[4].name)
316 expect(videos[2].name).to.equal(videosListBase[4].name)
317 }) 286 })
318 287
319 it('Should list the last video', async function () { 288 it('Should list the last video', async function () {
320 const res = await getVideosListPagination(server.url, 5, 6, 'name') 289 const { total, data } = await server.videos.list({ start: 5, count: 6, sort: 'name' })
321 290
322 const videos = res.body.data 291 expect(total).to.equal(6)
323 expect(res.body.total).to.equal(6) 292 expect(data.length).to.equal(1)
324 expect(videos.length).to.equal(1) 293 expect(data[0].name).to.equal(videosListBase[5].name)
325 expect(videos[0].name).to.equal(videosListBase[5].name)
326 }) 294 })
327 295
328 it('Should not have the total field', async function () { 296 it('Should not have the total field', async function () {
329 const res = await getVideosListPagination(server.url, 5, 6, 'name', true) 297 const { total, data } = await server.videos.list({ start: 5, count: 6, sort: 'name', skipCount: true })
330 298
331 const videos = res.body.data 299 expect(total).to.not.exist
332 expect(res.body.total).to.not.exist 300 expect(data.length).to.equal(1)
333 expect(videos.length).to.equal(1) 301 expect(data[0].name).to.equal(videosListBase[5].name)
334 expect(videos[0].name).to.equal(videosListBase[5].name)
335 }) 302 })
336 303
337 it('Should list and sort by name in descending order', async function () { 304 it('Should list and sort by name in descending order', async function () {
338 const res = await getVideosListSort(server.url, '-name') 305 const { total, data } = await server.videos.list({ sort: '-name' })
339 306
340 const videos = res.body.data 307 expect(total).to.equal(6)
341 expect(res.body.total).to.equal(6) 308 expect(data.length).to.equal(6)
342 expect(videos.length).to.equal(6) 309 expect(data[0].name).to.equal('video_short.webm name')
343 expect(videos[0].name).to.equal('video_short.webm name') 310 expect(data[1].name).to.equal('video_short.ogv name')
344 expect(videos[1].name).to.equal('video_short.ogv name') 311 expect(data[2].name).to.equal('video_short.mp4 name')
345 expect(videos[2].name).to.equal('video_short.mp4 name') 312 expect(data[3].name).to.equal('video_short3.webm name')
346 expect(videos[3].name).to.equal('video_short3.webm name') 313 expect(data[4].name).to.equal('video_short2.webm name')
347 expect(videos[4].name).to.equal('video_short2.webm name') 314 expect(data[5].name).to.equal('video_short1.webm name')
348 expect(videos[5].name).to.equal('video_short1.webm name')
349 315
350 videoId = videos[3].uuid 316 videoId = data[3].uuid
351 videoId2 = videos[5].uuid 317 videoId2 = data[5].uuid
352 }) 318 })
353 319
354 it('Should list and sort by trending in descending order', async function () { 320 it('Should list and sort by trending in descending order', async function () {
355 const res = await getVideosListPagination(server.url, 0, 2, '-trending') 321 const { total, data } = await server.videos.list({ start: 0, count: 2, sort: '-trending' })
356 322
357 const videos = res.body.data 323 expect(total).to.equal(6)
358 expect(res.body.total).to.equal(6) 324 expect(data.length).to.equal(2)
359 expect(videos.length).to.equal(2)
360 }) 325 })
361 326
362 it('Should list and sort by hotness in descending order', async function () { 327 it('Should list and sort by hotness in descending order', async function () {
363 const res = await getVideosListPagination(server.url, 0, 2, '-hot') 328 const { total, data } = await server.videos.list({ start: 0, count: 2, sort: '-hot' })
364 329
365 const videos = res.body.data 330 expect(total).to.equal(6)
366 expect(res.body.total).to.equal(6) 331 expect(data.length).to.equal(2)
367 expect(videos.length).to.equal(2)
368 }) 332 })
369 333
370 it('Should list and sort by best in descending order', async function () { 334 it('Should list and sort by best in descending order', async function () {
371 const res = await getVideosListPagination(server.url, 0, 2, '-best') 335 const { total, data } = await server.videos.list({ start: 0, count: 2, sort: '-best' })
372 336
373 const videos = res.body.data 337 expect(total).to.equal(6)
374 expect(res.body.total).to.equal(6) 338 expect(data.length).to.equal(2)
375 expect(videos.length).to.equal(2)
376 }) 339 })
377 340
378 it('Should update a video', async function () { 341 it('Should update a video', async function () {
@@ -387,67 +350,66 @@ describe('Test a single server', function () {
387 downloadEnabled: false, 350 downloadEnabled: false,
388 tags: [ 'tagup1', 'tagup2' ] 351 tags: [ 'tagup1', 'tagup2' ]
389 } 352 }
390 await updateVideo(server.url, server.accessToken, videoId, attributes) 353 await server.videos.update({ id: videoId, attributes })
391 }) 354 })
392 355
393 it('Should filter by tags and category', async function () { 356 it('Should filter by tags and category', async function () {
394 const res1 = await getVideosWithFilters(server.url, { tagsAllOf: [ 'tagup1', 'tagup2' ], categoryOneOf: [ 4 ] }) 357 {
395 expect(res1.body.total).to.equal(1) 358 const { data, total } = await server.videos.list({ tagsAllOf: [ 'tagup1', 'tagup2' ], categoryOneOf: [ 4 ] })
396 expect(res1.body.data[0].name).to.equal('my super video updated') 359 expect(total).to.equal(1)
360 expect(data[0].name).to.equal('my super video updated')
361 }
397 362
398 const res2 = await getVideosWithFilters(server.url, { tagsAllOf: [ 'tagup1', 'tagup2' ], categoryOneOf: [ 3 ] }) 363 {
399 expect(res2.body.total).to.equal(0) 364 const { total } = await server.videos.list({ tagsAllOf: [ 'tagup1', 'tagup2' ], categoryOneOf: [ 3 ] })
365 expect(total).to.equal(0)
366 }
400 }) 367 })
401 368
402 it('Should have the video updated', async function () { 369 it('Should have the video updated', async function () {
403 this.timeout(60000) 370 this.timeout(60000)
404 371
405 const res = await getVideo(server.url, videoId) 372 const video = await server.videos.get({ id: videoId })
406 const video = res.body
407 373
408 await completeVideoCheck(server.url, video, updateCheckAttributes()) 374 await completeVideoCheck(server, video, updateCheckAttributes())
409 }) 375 })
410 376
411 it('Should update only the tags of a video', async function () { 377 it('Should update only the tags of a video', async function () {
412 const attributes = { 378 const attributes = {
413 tags: [ 'supertag', 'tag1', 'tag2' ] 379 tags: [ 'supertag', 'tag1', 'tag2' ]
414 } 380 }
415 await updateVideo(server.url, server.accessToken, videoId, attributes) 381 await server.videos.update({ id: videoId, attributes })
416 382
417 const res = await getVideo(server.url, videoId) 383 const video = await server.videos.get({ id: videoId })
418 const video = res.body
419 384
420 await completeVideoCheck(server.url, video, Object.assign(updateCheckAttributes(), attributes)) 385 await completeVideoCheck(server, video, Object.assign(updateCheckAttributes(), attributes))
421 }) 386 })
422 387
423 it('Should update only the description of a video', async function () { 388 it('Should update only the description of a video', async function () {
424 const attributes = { 389 const attributes = {
425 description: 'hello everybody' 390 description: 'hello everybody'
426 } 391 }
427 await updateVideo(server.url, server.accessToken, videoId, attributes) 392 await server.videos.update({ id: videoId, attributes })
428 393
429 const res = await getVideo(server.url, videoId) 394 const video = await server.videos.get({ id: videoId })
430 const video = res.body
431 395
432 const expectedAttributes = Object.assign(updateCheckAttributes(), { tags: [ 'supertag', 'tag1', 'tag2' ] }, attributes) 396 const expectedAttributes = Object.assign(updateCheckAttributes(), { tags: [ 'supertag', 'tag1', 'tag2' ] }, attributes)
433 await completeVideoCheck(server.url, video, expectedAttributes) 397 await completeVideoCheck(server, video, expectedAttributes)
434 }) 398 })
435 399
436 it('Should like a video', async function () { 400 it('Should like a video', async function () {
437 await rateVideo(server.url, server.accessToken, videoId, 'like') 401 await server.videos.rate({ id: videoId, rating: 'like' })
438 402
439 const res = await getVideo(server.url, videoId) 403 const video = await server.videos.get({ id: videoId })
440 const video = res.body
441 404
442 expect(video.likes).to.equal(1) 405 expect(video.likes).to.equal(1)
443 expect(video.dislikes).to.equal(0) 406 expect(video.dislikes).to.equal(0)
444 }) 407 })
445 408
446 it('Should dislike the same video', async function () { 409 it('Should dislike the same video', async function () {
447 await rateVideo(server.url, server.accessToken, videoId, 'dislike') 410 await server.videos.rate({ id: videoId, rating: 'dislike' })
448 411
449 const res = await getVideo(server.url, videoId) 412 const video = await server.videos.get({ id: videoId })
450 const video = res.body
451 413
452 expect(video.likes).to.equal(0) 414 expect(video.likes).to.equal(0)
453 expect(video.dislikes).to.equal(1) 415 expect(video.dislikes).to.equal(1)
@@ -457,10 +419,10 @@ describe('Test a single server', function () {
457 { 419 {
458 const now = new Date() 420 const now = new Date()
459 const attributes = { originallyPublishedAt: now.toISOString() } 421 const attributes = { originallyPublishedAt: now.toISOString() }
460 await updateVideo(server.url, server.accessToken, videoId, attributes) 422 await server.videos.update({ id: videoId, attributes })
461 423
462 const res = await getVideosListSort(server.url, '-originallyPublishedAt') 424 const { data } = await server.videos.list({ sort: '-originallyPublishedAt' })
463 const names = res.body.data.map(v => v.name) 425 const names = data.map(v => v.name)
464 426
465 expect(names[0]).to.equal('my super video updated') 427 expect(names[0]).to.equal('my super video updated')
466 expect(names[1]).to.equal('video_short2.webm name') 428 expect(names[1]).to.equal('video_short2.webm name')
@@ -473,10 +435,10 @@ describe('Test a single server', function () {
473 { 435 {
474 const now = new Date() 436 const now = new Date()
475 const attributes = { originallyPublishedAt: now.toISOString() } 437 const attributes = { originallyPublishedAt: now.toISOString() }
476 await updateVideo(server.url, server.accessToken, videoId2, attributes) 438 await server.videos.update({ id: videoId2, attributes })
477 439
478 const res = await getVideosListSort(server.url, '-originallyPublishedAt') 440 const { data } = await server.videos.list({ sort: '-originallyPublishedAt' })
479 const names = res.body.data.map(v => v.name) 441 const names = data.map(v => v.name)
480 442
481 expect(names[0]).to.equal('video_short1.webm name') 443 expect(names[0]).to.equal('video_short1.webm name')
482 expect(names[1]).to.equal('my super video updated') 444 expect(names[1]).to.equal('my super video updated')
diff --git a/server/tests/api/videos/video-captions.ts b/server/tests/api/videos/video-captions.ts
index 14ecedfa6..4c8e28adf 100644
--- a/server/tests/api/videos/video-captions.ts
+++ b/server/tests/api/videos/video-captions.ts
@@ -1,72 +1,61 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2 2
3import * as chai from 'chai'
4import 'mocha' 3import 'mocha'
4import * as chai from 'chai'
5import { 5import {
6 checkVideoFilesWereRemoved, 6 checkVideoFilesWereRemoved,
7 cleanupTests, 7 cleanupTests,
8 createMultipleServers,
8 doubleFollow, 9 doubleFollow,
9 flushAndRunMultipleServers, 10 PeerTubeServer,
10 removeVideo, 11 setAccessTokensToServers,
11 uploadVideo, 12 testCaptionFile,
12 wait 13 wait,
13} from '../../../../shared/extra-utils' 14 waitJobs
14import { ServerInfo, setAccessTokensToServers } from '../../../../shared/extra-utils/index' 15} from '@shared/extra-utils'
15import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
16import {
17 createVideoCaption,
18 deleteVideoCaption,
19 listVideoCaptions,
20 testCaptionFile
21} from '../../../../shared/extra-utils/videos/video-captions'
22import { VideoCaption } from '../../../../shared/models/videos/caption/video-caption.model'
23 16
24const expect = chai.expect 17const expect = chai.expect
25 18
26describe('Test video captions', function () { 19describe('Test video captions', function () {
27 const uuidRegex = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}' 20 const uuidRegex = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'
28 21
29 let servers: ServerInfo[] 22 let servers: PeerTubeServer[]
30 let videoUUID: string 23 let videoUUID: string
31 24
32 before(async function () { 25 before(async function () {
33 this.timeout(60000) 26 this.timeout(60000)
34 27
35 servers = await flushAndRunMultipleServers(2) 28 servers = await createMultipleServers(2)
36 29
37 await setAccessTokensToServers(servers) 30 await setAccessTokensToServers(servers)
38 await doubleFollow(servers[0], servers[1]) 31 await doubleFollow(servers[0], servers[1])
39 32
40 await waitJobs(servers) 33 await waitJobs(servers)
41 34
42 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'my video name' }) 35 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'my video name' } })
43 videoUUID = res.body.video.uuid 36 videoUUID = uuid
44 37
45 await waitJobs(servers) 38 await waitJobs(servers)
46 }) 39 })
47 40
48 it('Should list the captions and return an empty list', async function () { 41 it('Should list the captions and return an empty list', async function () {
49 for (const server of servers) { 42 for (const server of servers) {
50 const res = await listVideoCaptions(server.url, videoUUID) 43 const body = await server.captions.list({ videoId: videoUUID })
51 expect(res.body.total).to.equal(0) 44 expect(body.total).to.equal(0)
52 expect(res.body.data).to.have.lengthOf(0) 45 expect(body.data).to.have.lengthOf(0)
53 } 46 }
54 }) 47 })
55 48
56 it('Should create two new captions', async function () { 49 it('Should create two new captions', async function () {
57 this.timeout(30000) 50 this.timeout(30000)
58 51
59 await createVideoCaption({ 52 await servers[0].captions.add({
60 url: servers[0].url,
61 accessToken: servers[0].accessToken,
62 language: 'ar', 53 language: 'ar',
63 videoId: videoUUID, 54 videoId: videoUUID,
64 fixture: 'subtitle-good1.vtt' 55 fixture: 'subtitle-good1.vtt'
65 }) 56 })
66 57
67 await createVideoCaption({ 58 await servers[0].captions.add({
68 url: servers[0].url,
69 accessToken: servers[0].accessToken,
70 language: 'zh', 59 language: 'zh',
71 videoId: videoUUID, 60 videoId: videoUUID,
72 fixture: 'subtitle-good2.vtt', 61 fixture: 'subtitle-good2.vtt',
@@ -78,17 +67,17 @@ describe('Test video captions', function () {
78 67
79 it('Should list these uploaded captions', async function () { 68 it('Should list these uploaded captions', async function () {
80 for (const server of servers) { 69 for (const server of servers) {
81 const res = await listVideoCaptions(server.url, videoUUID) 70 const body = await server.captions.list({ videoId: videoUUID })
82 expect(res.body.total).to.equal(2) 71 expect(body.total).to.equal(2)
83 expect(res.body.data).to.have.lengthOf(2) 72 expect(body.data).to.have.lengthOf(2)
84 73
85 const caption1: VideoCaption = res.body.data[0] 74 const caption1 = body.data[0]
86 expect(caption1.language.id).to.equal('ar') 75 expect(caption1.language.id).to.equal('ar')
87 expect(caption1.language.label).to.equal('Arabic') 76 expect(caption1.language.label).to.equal('Arabic')
88 expect(caption1.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-ar.vtt$')) 77 expect(caption1.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-ar.vtt$'))
89 await testCaptionFile(server.url, caption1.captionPath, 'Subtitle good 1.') 78 await testCaptionFile(server.url, caption1.captionPath, 'Subtitle good 1.')
90 79
91 const caption2: VideoCaption = res.body.data[1] 80 const caption2 = body.data[1]
92 expect(caption2.language.id).to.equal('zh') 81 expect(caption2.language.id).to.equal('zh')
93 expect(caption2.language.label).to.equal('Chinese') 82 expect(caption2.language.label).to.equal('Chinese')
94 expect(caption2.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-zh.vtt$')) 83 expect(caption2.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-zh.vtt$'))
@@ -99,9 +88,7 @@ describe('Test video captions', function () {
99 it('Should replace an existing caption', async function () { 88 it('Should replace an existing caption', async function () {
100 this.timeout(30000) 89 this.timeout(30000)
101 90
102 await createVideoCaption({ 91 await servers[0].captions.add({
103 url: servers[0].url,
104 accessToken: servers[0].accessToken,
105 language: 'ar', 92 language: 'ar',
106 videoId: videoUUID, 93 videoId: videoUUID,
107 fixture: 'subtitle-good2.vtt' 94 fixture: 'subtitle-good2.vtt'
@@ -112,11 +99,11 @@ describe('Test video captions', function () {
112 99
113 it('Should have this caption updated', async function () { 100 it('Should have this caption updated', async function () {
114 for (const server of servers) { 101 for (const server of servers) {
115 const res = await listVideoCaptions(server.url, videoUUID) 102 const body = await server.captions.list({ videoId: videoUUID })
116 expect(res.body.total).to.equal(2) 103 expect(body.total).to.equal(2)
117 expect(res.body.data).to.have.lengthOf(2) 104 expect(body.data).to.have.lengthOf(2)
118 105
119 const caption1: VideoCaption = res.body.data[0] 106 const caption1 = body.data[0]
120 expect(caption1.language.id).to.equal('ar') 107 expect(caption1.language.id).to.equal('ar')
121 expect(caption1.language.label).to.equal('Arabic') 108 expect(caption1.language.label).to.equal('Arabic')
122 expect(caption1.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-ar.vtt$')) 109 expect(caption1.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-ar.vtt$'))
@@ -127,9 +114,7 @@ describe('Test video captions', function () {
127 it('Should replace an existing caption with a srt file and convert it', async function () { 114 it('Should replace an existing caption with a srt file and convert it', async function () {
128 this.timeout(30000) 115 this.timeout(30000)
129 116
130 await createVideoCaption({ 117 await servers[0].captions.add({
131 url: servers[0].url,
132 accessToken: servers[0].accessToken,
133 language: 'ar', 118 language: 'ar',
134 videoId: videoUUID, 119 videoId: videoUUID,
135 fixture: 'subtitle-good.srt' 120 fixture: 'subtitle-good.srt'
@@ -143,11 +128,11 @@ describe('Test video captions', function () {
143 128
144 it('Should have this caption updated and converted', async function () { 129 it('Should have this caption updated and converted', async function () {
145 for (const server of servers) { 130 for (const server of servers) {
146 const res = await listVideoCaptions(server.url, videoUUID) 131 const body = await server.captions.list({ videoId: videoUUID })
147 expect(res.body.total).to.equal(2) 132 expect(body.total).to.equal(2)
148 expect(res.body.data).to.have.lengthOf(2) 133 expect(body.data).to.have.lengthOf(2)
149 134
150 const caption1: VideoCaption = res.body.data[0] 135 const caption1 = body.data[0]
151 expect(caption1.language.id).to.equal('ar') 136 expect(caption1.language.id).to.equal('ar')
152 expect(caption1.language.label).to.equal('Arabic') 137 expect(caption1.language.label).to.equal('Arabic')
153 expect(caption1.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-ar.vtt$')) 138 expect(caption1.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-ar.vtt$'))
@@ -172,18 +157,18 @@ describe('Test video captions', function () {
172 it('Should remove one caption', async function () { 157 it('Should remove one caption', async function () {
173 this.timeout(30000) 158 this.timeout(30000)
174 159
175 await deleteVideoCaption(servers[0].url, servers[0].accessToken, videoUUID, 'ar') 160 await servers[0].captions.delete({ videoId: videoUUID, language: 'ar' })
176 161
177 await waitJobs(servers) 162 await waitJobs(servers)
178 }) 163 })
179 164
180 it('Should only list the caption that was not deleted', async function () { 165 it('Should only list the caption that was not deleted', async function () {
181 for (const server of servers) { 166 for (const server of servers) {
182 const res = await listVideoCaptions(server.url, videoUUID) 167 const body = await server.captions.list({ videoId: videoUUID })
183 expect(res.body.total).to.equal(1) 168 expect(body.total).to.equal(1)
184 expect(res.body.data).to.have.lengthOf(1) 169 expect(body.data).to.have.lengthOf(1)
185 170
186 const caption: VideoCaption = res.body.data[0] 171 const caption = body.data[0]
187 172
188 expect(caption.language.id).to.equal('zh') 173 expect(caption.language.id).to.equal('zh')
189 expect(caption.language.label).to.equal('Chinese') 174 expect(caption.language.label).to.equal('Chinese')
@@ -193,9 +178,9 @@ describe('Test video captions', function () {
193 }) 178 })
194 179
195 it('Should remove the video, and thus all video captions', async function () { 180 it('Should remove the video, and thus all video captions', async function () {
196 await removeVideo(servers[0].url, servers[0].accessToken, videoUUID) 181 await servers[0].videos.remove({ id: videoUUID })
197 182
198 await checkVideoFilesWereRemoved(videoUUID, 1) 183 await checkVideoFilesWereRemoved(videoUUID, servers[0])
199 }) 184 })
200 185
201 after(async function () { 186 after(async function () {
diff --git a/server/tests/api/videos/video-change-ownership.ts b/server/tests/api/videos/video-change-ownership.ts
index a3384851b..6ae6d3004 100644
--- a/server/tests/api/videos/video-change-ownership.ts
+++ b/server/tests/api/videos/video-change-ownership.ts
@@ -2,234 +2,212 @@
2 2
3import 'mocha' 3import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
6import { 5import {
7 acceptChangeOwnership, 6 ChangeOwnershipCommand,
8 changeVideoOwnership,
9 cleanupTests, 7 cleanupTests,
10 createLive, 8 createMultipleServers,
11 createUser, 9 createSingleServer,
12 doubleFollow, 10 doubleFollow,
13 flushAndRunMultipleServers, 11 PeerTubeServer,
14 flushAndRunServer,
15 getMyUserInformation,
16 getVideo,
17 getVideoChangeOwnershipList,
18 getVideosList,
19 refuseChangeOwnership,
20 ServerInfo,
21 setAccessTokensToServers, 12 setAccessTokensToServers,
22 setDefaultVideoChannel, 13 setDefaultVideoChannel,
23 updateCustomSubConfig, 14 waitJobs
24 uploadVideo, 15} from '@shared/extra-utils'
25 userLogin 16import { HttpStatusCode, VideoPrivacy } from '@shared/models'
26} from '../../../../shared/extra-utils'
27import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
28import { User } from '../../../../shared/models/users'
29import { VideoDetails, VideoPrivacy } from '../../../../shared/models/videos'
30 17
31const expect = chai.expect 18const expect = chai.expect
32 19
33describe('Test video change ownership - nominal', function () { 20describe('Test video change ownership - nominal', function () {
34 let servers: ServerInfo[] = [] 21 let servers: PeerTubeServer[] = []
35 const firstUser = { 22
36 username: 'first', 23 const firstUser = 'first'
37 password: 'My great password' 24 const secondUser = 'second'
38 } 25
39 const secondUser = { 26 let firstUserToken = ''
40 username: 'second',
41 password: 'My other password'
42 }
43
44 let firstUserAccessToken = ''
45 let firstUserChannelId: number 27 let firstUserChannelId: number
46 28
47 let secondUserAccessToken = '' 29 let secondUserToken = ''
48 let secondUserChannelId: number 30 let secondUserChannelId: number
49 31
50 let lastRequestChangeOwnershipId = '' 32 let lastRequestId: number
51 33
52 let liveId: number 34 let liveId: number
53 35
36 let command: ChangeOwnershipCommand
37
54 before(async function () { 38 before(async function () {
55 this.timeout(50000) 39 this.timeout(50000)
56 40
57 servers = await flushAndRunMultipleServers(2) 41 servers = await createMultipleServers(2)
58 await setAccessTokensToServers(servers) 42 await setAccessTokensToServers(servers)
59 await setDefaultVideoChannel(servers) 43 await setDefaultVideoChannel(servers)
60 44
61 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, { 45 await servers[0].config.updateCustomSubConfig({
62 transcoding: { 46 newConfig: {
63 enabled: false 47 transcoding: {
64 }, 48 enabled: false
65 live: { 49 },
66 enabled: true 50 live: {
51 enabled: true
52 }
67 } 53 }
68 }) 54 })
69 55
70 const videoQuota = 42000000 56 firstUserToken = await servers[0].users.generateUserAndToken(firstUser)
71 await createUser({ 57 secondUserToken = await servers[0].users.generateUserAndToken(secondUser)
72 url: servers[0].url,
73 accessToken: servers[0].accessToken,
74 username: firstUser.username,
75 password: firstUser.password,
76 videoQuota: videoQuota
77 })
78 await createUser({
79 url: servers[0].url,
80 accessToken: servers[0].accessToken,
81 username: secondUser.username,
82 password: secondUser.password,
83 videoQuota: videoQuota
84 })
85
86 firstUserAccessToken = await userLogin(servers[0], firstUser)
87 secondUserAccessToken = await userLogin(servers[0], secondUser)
88 58
89 { 59 {
90 const res = await getMyUserInformation(servers[0].url, firstUserAccessToken) 60 const { videoChannels } = await servers[0].users.getMyInfo({ token: firstUserToken })
91 const firstUserInformation: User = res.body 61 firstUserChannelId = videoChannels[0].id
92 firstUserChannelId = firstUserInformation.videoChannels[0].id
93 } 62 }
94 63
95 { 64 {
96 const res = await getMyUserInformation(servers[0].url, secondUserAccessToken) 65 const { videoChannels } = await servers[0].users.getMyInfo({ token: secondUserToken })
97 const secondUserInformation: User = res.body 66 secondUserChannelId = videoChannels[0].id
98 secondUserChannelId = secondUserInformation.videoChannels[0].id
99 } 67 }
100 68
101 { 69 {
102 const videoAttributes = { 70 const attributes = {
103 name: 'my super name', 71 name: 'my super name',
104 description: 'my super description' 72 description: 'my super description'
105 } 73 }
106 const res = await uploadVideo(servers[0].url, firstUserAccessToken, videoAttributes) 74 const { id } = await servers[0].videos.upload({ token: firstUserToken, attributes })
107 75
108 const resVideo = await getVideo(servers[0].url, res.body.video.id) 76 servers[0].store.video = await servers[0].videos.get({ id })
109 servers[0].video = resVideo.body
110 } 77 }
111 78
112 { 79 {
113 const attributes = { name: 'live', channelId: firstUserChannelId, privacy: VideoPrivacy.PUBLIC } 80 const attributes = { name: 'live', channelId: firstUserChannelId, privacy: VideoPrivacy.PUBLIC }
114 const res = await createLive(servers[0].url, firstUserAccessToken, attributes) 81 const video = await servers[0].live.create({ token: firstUserToken, fields: attributes })
115 82
116 liveId = res.body.video.id 83 liveId = video.id
117 } 84 }
118 85
86 command = servers[0].changeOwnership
87
119 await doubleFollow(servers[0], servers[1]) 88 await doubleFollow(servers[0], servers[1])
120 }) 89 })
121 90
122 it('Should not have video change ownership', async function () { 91 it('Should not have video change ownership', async function () {
123 const resFirstUser = await getVideoChangeOwnershipList(servers[0].url, firstUserAccessToken) 92 {
93 const body = await command.list({ token: firstUserToken })
124 94
125 expect(resFirstUser.body.total).to.equal(0) 95 expect(body.total).to.equal(0)
126 expect(resFirstUser.body.data).to.be.an('array') 96 expect(body.data).to.be.an('array')
127 expect(resFirstUser.body.data.length).to.equal(0) 97 expect(body.data.length).to.equal(0)
98 }
128 99
129 const resSecondUser = await getVideoChangeOwnershipList(servers[0].url, secondUserAccessToken) 100 {
101 const body = await command.list({ token: secondUserToken })
130 102
131 expect(resSecondUser.body.total).to.equal(0) 103 expect(body.total).to.equal(0)
132 expect(resSecondUser.body.data).to.be.an('array') 104 expect(body.data).to.be.an('array')
133 expect(resSecondUser.body.data.length).to.equal(0) 105 expect(body.data.length).to.equal(0)
106 }
134 }) 107 })
135 108
136 it('Should send a request to change ownership of a video', async function () { 109 it('Should send a request to change ownership of a video', async function () {
137 this.timeout(15000) 110 this.timeout(15000)
138 111
139 await changeVideoOwnership(servers[0].url, firstUserAccessToken, servers[0].video.id, secondUser.username) 112 await command.create({ token: firstUserToken, videoId: servers[0].store.video.id, username: secondUser })
140 }) 113 })
141 114
142 it('Should only return a request to change ownership for the second user', async function () { 115 it('Should only return a request to change ownership for the second user', async function () {
143 const resFirstUser = await getVideoChangeOwnershipList(servers[0].url, firstUserAccessToken) 116 {
117 const body = await command.list({ token: firstUserToken })
144 118
145 expect(resFirstUser.body.total).to.equal(0) 119 expect(body.total).to.equal(0)
146 expect(resFirstUser.body.data).to.be.an('array') 120 expect(body.data).to.be.an('array')
147 expect(resFirstUser.body.data.length).to.equal(0) 121 expect(body.data.length).to.equal(0)
122 }
148 123
149 const resSecondUser = await getVideoChangeOwnershipList(servers[0].url, secondUserAccessToken) 124 {
125 const body = await command.list({ token: secondUserToken })
150 126
151 expect(resSecondUser.body.total).to.equal(1) 127 expect(body.total).to.equal(1)
152 expect(resSecondUser.body.data).to.be.an('array') 128 expect(body.data).to.be.an('array')
153 expect(resSecondUser.body.data.length).to.equal(1) 129 expect(body.data.length).to.equal(1)
154 130
155 lastRequestChangeOwnershipId = resSecondUser.body.data[0].id 131 lastRequestId = body.data[0].id
132 }
156 }) 133 })
157 134
158 it('Should accept the same change ownership request without crashing', async function () { 135 it('Should accept the same change ownership request without crashing', async function () {
159 this.timeout(10000) 136 this.timeout(10000)
160 137
161 await changeVideoOwnership(servers[0].url, firstUserAccessToken, servers[0].video.id, secondUser.username) 138 await command.create({ token: firstUserToken, videoId: servers[0].store.video.id, username: secondUser })
162 }) 139 })
163 140
164 it('Should not create multiple change ownership requests while one is waiting', async function () { 141 it('Should not create multiple change ownership requests while one is waiting', async function () {
165 this.timeout(10000) 142 this.timeout(10000)
166 143
167 const resSecondUser = await getVideoChangeOwnershipList(servers[0].url, secondUserAccessToken) 144 const body = await command.list({ token: secondUserToken })
168 145
169 expect(resSecondUser.body.total).to.equal(1) 146 expect(body.total).to.equal(1)
170 expect(resSecondUser.body.data).to.be.an('array') 147 expect(body.data).to.be.an('array')
171 expect(resSecondUser.body.data.length).to.equal(1) 148 expect(body.data.length).to.equal(1)
172 }) 149 })
173 150
174 it('Should not be possible to refuse the change of ownership from first user', async function () { 151 it('Should not be possible to refuse the change of ownership from first user', async function () {
175 this.timeout(10000) 152 this.timeout(10000)
176 153
177 await refuseChangeOwnership(servers[0].url, firstUserAccessToken, lastRequestChangeOwnershipId, HttpStatusCode.FORBIDDEN_403) 154 await command.refuse({ token: firstUserToken, ownershipId: lastRequestId, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
178 }) 155 })
179 156
180 it('Should be possible to refuse the change of ownership from second user', async function () { 157 it('Should be possible to refuse the change of ownership from second user', async function () {
181 this.timeout(10000) 158 this.timeout(10000)
182 159
183 await refuseChangeOwnership(servers[0].url, secondUserAccessToken, lastRequestChangeOwnershipId) 160 await command.refuse({ token: secondUserToken, ownershipId: lastRequestId })
184 }) 161 })
185 162
186 it('Should send a new request to change ownership of a video', async function () { 163 it('Should send a new request to change ownership of a video', async function () {
187 this.timeout(15000) 164 this.timeout(15000)
188 165
189 await changeVideoOwnership(servers[0].url, firstUserAccessToken, servers[0].video.id, secondUser.username) 166 await command.create({ token: firstUserToken, videoId: servers[0].store.video.id, username: secondUser })
190 }) 167 })
191 168
192 it('Should return two requests to change ownership for the second user', async function () { 169 it('Should return two requests to change ownership for the second user', async function () {
193 const resFirstUser = await getVideoChangeOwnershipList(servers[0].url, firstUserAccessToken) 170 {
171 const body = await command.list({ token: firstUserToken })
194 172
195 expect(resFirstUser.body.total).to.equal(0) 173 expect(body.total).to.equal(0)
196 expect(resFirstUser.body.data).to.be.an('array') 174 expect(body.data).to.be.an('array')
197 expect(resFirstUser.body.data.length).to.equal(0) 175 expect(body.data.length).to.equal(0)
176 }
198 177
199 const resSecondUser = await getVideoChangeOwnershipList(servers[0].url, secondUserAccessToken) 178 {
179 const body = await command.list({ token: secondUserToken })
200 180
201 expect(resSecondUser.body.total).to.equal(2) 181 expect(body.total).to.equal(2)
202 expect(resSecondUser.body.data).to.be.an('array') 182 expect(body.data).to.be.an('array')
203 expect(resSecondUser.body.data.length).to.equal(2) 183 expect(body.data.length).to.equal(2)
204 184
205 lastRequestChangeOwnershipId = resSecondUser.body.data[0].id 185 lastRequestId = body.data[0].id
186 }
206 }) 187 })
207 188
208 it('Should not be possible to accept the change of ownership from first user', async function () { 189 it('Should not be possible to accept the change of ownership from first user', async function () {
209 this.timeout(10000) 190 this.timeout(10000)
210 191
211 await acceptChangeOwnership( 192 await command.accept({
212 servers[0].url, 193 token: firstUserToken,
213 firstUserAccessToken, 194 ownershipId: lastRequestId,
214 lastRequestChangeOwnershipId, 195 channelId: secondUserChannelId,
215 secondUserChannelId, 196 expectedStatus: HttpStatusCode.FORBIDDEN_403
216 HttpStatusCode.FORBIDDEN_403 197 })
217 )
218 }) 198 })
219 199
220 it('Should be possible to accept the change of ownership from second user', async function () { 200 it('Should be possible to accept the change of ownership from second user', async function () {
221 this.timeout(10000) 201 this.timeout(10000)
222 202
223 await acceptChangeOwnership(servers[0].url, secondUserAccessToken, lastRequestChangeOwnershipId, secondUserChannelId) 203 await command.accept({ token: secondUserToken, ownershipId: lastRequestId, channelId: secondUserChannelId })
224 204
225 await waitJobs(servers) 205 await waitJobs(servers)
226 }) 206 })
227 207
228 it('Should have the channel of the video updated', async function () { 208 it('Should have the channel of the video updated', async function () {
229 for (const server of servers) { 209 for (const server of servers) {
230 const res = await getVideo(server.url, servers[0].video.uuid) 210 const video = await server.videos.get({ id: servers[0].store.video.uuid })
231
232 const video: VideoDetails = res.body
233 211
234 expect(video.name).to.equal('my super name') 212 expect(video.name).to.equal('my super name')
235 expect(video.channel.displayName).to.equal('Main second channel') 213 expect(video.channel.displayName).to.equal('Main second channel')
@@ -240,27 +218,25 @@ describe('Test video change ownership - nominal', function () {
240 it('Should send a request to change ownership of a live', async function () { 218 it('Should send a request to change ownership of a live', async function () {
241 this.timeout(15000) 219 this.timeout(15000)
242 220
243 await changeVideoOwnership(servers[0].url, firstUserAccessToken, liveId, secondUser.username) 221 await command.create({ token: firstUserToken, videoId: liveId, username: secondUser })
244 222
245 const resSecondUser = await getVideoChangeOwnershipList(servers[0].url, secondUserAccessToken) 223 const body = await command.list({ token: secondUserToken })
246 224
247 expect(resSecondUser.body.total).to.equal(3) 225 expect(body.total).to.equal(3)
248 expect(resSecondUser.body.data.length).to.equal(3) 226 expect(body.data.length).to.equal(3)
249 227
250 lastRequestChangeOwnershipId = resSecondUser.body.data[0].id 228 lastRequestId = body.data[0].id
251 }) 229 })
252 230
253 it('Should accept a live ownership change', async function () { 231 it('Should accept a live ownership change', async function () {
254 this.timeout(20000) 232 this.timeout(20000)
255 233
256 await acceptChangeOwnership(servers[0].url, secondUserAccessToken, lastRequestChangeOwnershipId, secondUserChannelId) 234 await command.accept({ token: secondUserToken, ownershipId: lastRequestId, channelId: secondUserChannelId })
257 235
258 await waitJobs(servers) 236 await waitJobs(servers)
259 237
260 for (const server of servers) { 238 for (const server of servers) {
261 const res = await getVideo(server.url, servers[0].video.uuid) 239 const video = await server.videos.get({ id: servers[0].store.video.uuid })
262
263 const video: VideoDetails = res.body
264 240
265 expect(video.name).to.equal('my super name') 241 expect(video.name).to.equal('my super name')
266 expect(video.channel.displayName).to.equal('Main second channel') 242 expect(video.channel.displayName).to.equal('Main second channel')
@@ -274,99 +250,79 @@ describe('Test video change ownership - nominal', function () {
274}) 250})
275 251
276describe('Test video change ownership - quota too small', function () { 252describe('Test video change ownership - quota too small', function () {
277 let server: ServerInfo 253 let server: PeerTubeServer
278 const firstUser = { 254 const firstUser = 'first'
279 username: 'first', 255 const secondUser = 'second'
280 password: 'My great password' 256
281 } 257 let firstUserToken = ''
282 const secondUser = { 258 let secondUserToken = ''
283 username: 'second', 259 let lastRequestId: number
284 password: 'My other password'
285 }
286 let firstUserAccessToken = ''
287 let secondUserAccessToken = ''
288 let lastRequestChangeOwnershipId = ''
289 260
290 before(async function () { 261 before(async function () {
291 this.timeout(50000) 262 this.timeout(50000)
292 263
293 // Run one server 264 // Run one server
294 server = await flushAndRunServer(1) 265 server = await createSingleServer(1)
295 await setAccessTokensToServers([ server ]) 266 await setAccessTokensToServers([ server ])
296 267
297 const videoQuota = 42000000 268 await server.users.create({ username: secondUser, videoQuota: 10 })
298 const limitedVideoQuota = 10
299 await createUser({
300 url: server.url,
301 accessToken: server.accessToken,
302 username: firstUser.username,
303 password: firstUser.password,
304 videoQuota: videoQuota
305 })
306 await createUser({
307 url: server.url,
308 accessToken: server.accessToken,
309 username: secondUser.username,
310 password: secondUser.password,
311 videoQuota: limitedVideoQuota
312 })
313 269
314 firstUserAccessToken = await userLogin(server, firstUser) 270 firstUserToken = await server.users.generateUserAndToken(firstUser)
315 secondUserAccessToken = await userLogin(server, secondUser) 271 secondUserToken = await server.login.getAccessToken(secondUser)
316 272
317 // Upload some videos on the server 273 // Upload some videos on the server
318 const video1Attributes = { 274 const attributes = {
319 name: 'my super name', 275 name: 'my super name',
320 description: 'my super description' 276 description: 'my super description'
321 } 277 }
322 await uploadVideo(server.url, firstUserAccessToken, video1Attributes) 278 await server.videos.upload({ token: firstUserToken, attributes })
323 279
324 await waitJobs(server) 280 await waitJobs(server)
325 281
326 const res = await getVideosList(server.url) 282 const { data } = await server.videos.list()
327 const videos = res.body.data 283 expect(data.length).to.equal(1)
328
329 expect(videos.length).to.equal(1)
330 284
331 server.video = videos.find(video => video.name === 'my super name') 285 server.store.video = data.find(video => video.name === 'my super name')
332 }) 286 })
333 287
334 it('Should send a request to change ownership of a video', async function () { 288 it('Should send a request to change ownership of a video', async function () {
335 this.timeout(15000) 289 this.timeout(15000)
336 290
337 await changeVideoOwnership(server.url, firstUserAccessToken, server.video.id, secondUser.username) 291 await server.changeOwnership.create({ token: firstUserToken, videoId: server.store.video.id, username: secondUser })
338 }) 292 })
339 293
340 it('Should only return a request to change ownership for the second user', async function () { 294 it('Should only return a request to change ownership for the second user', async function () {
341 const resFirstUser = await getVideoChangeOwnershipList(server.url, firstUserAccessToken) 295 {
296 const body = await server.changeOwnership.list({ token: firstUserToken })
342 297
343 expect(resFirstUser.body.total).to.equal(0) 298 expect(body.total).to.equal(0)
344 expect(resFirstUser.body.data).to.be.an('array') 299 expect(body.data).to.be.an('array')
345 expect(resFirstUser.body.data.length).to.equal(0) 300 expect(body.data.length).to.equal(0)
301 }
346 302
347 const resSecondUser = await getVideoChangeOwnershipList(server.url, secondUserAccessToken) 303 {
304 const body = await server.changeOwnership.list({ token: secondUserToken })
348 305
349 expect(resSecondUser.body.total).to.equal(1) 306 expect(body.total).to.equal(1)
350 expect(resSecondUser.body.data).to.be.an('array') 307 expect(body.data).to.be.an('array')
351 expect(resSecondUser.body.data.length).to.equal(1) 308 expect(body.data.length).to.equal(1)
352 309
353 lastRequestChangeOwnershipId = resSecondUser.body.data[0].id 310 lastRequestId = body.data[0].id
311 }
354 }) 312 })
355 313
356 it('Should not be possible to accept the change of ownership from second user because of exceeded quota', async function () { 314 it('Should not be possible to accept the change of ownership from second user because of exceeded quota', async function () {
357 this.timeout(10000) 315 this.timeout(10000)
358 316
359 const secondUserInformationResponse = await getMyUserInformation(server.url, secondUserAccessToken) 317 const { videoChannels } = await server.users.getMyInfo({ token: secondUserToken })
360 const secondUserInformation: User = secondUserInformationResponse.body 318 const channelId = videoChannels[0].id
361 const channelId = secondUserInformation.videoChannels[0].id
362 319
363 await acceptChangeOwnership( 320 await server.changeOwnership.accept({
364 server.url, 321 token: secondUserToken,
365 secondUserAccessToken, 322 ownershipId: lastRequestId,
366 lastRequestChangeOwnershipId,
367 channelId, 323 channelId,
368 HttpStatusCode.PAYLOAD_TOO_LARGE_413 324 expectedStatus: HttpStatusCode.PAYLOAD_TOO_LARGE_413
369 ) 325 })
370 }) 326 })
371 327
372 after(async function () { 328 after(async function () {
diff --git a/server/tests/api/videos/video-channels.ts b/server/tests/api/videos/video-channels.ts
index 865098777..c25754eb6 100644
--- a/server/tests/api/videos/video-channels.ts
+++ b/server/tests/api/videos/video-channels.ts
@@ -6,48 +6,28 @@ import { basename } from 'path'
6import { ACTOR_IMAGES_SIZE } from '@server/initializers/constants' 6import { ACTOR_IMAGES_SIZE } from '@server/initializers/constants'
7import { 7import {
8 cleanupTests, 8 cleanupTests,
9 createUser, 9 createMultipleServers,
10 deleteVideoChannelImage,
11 doubleFollow, 10 doubleFollow,
12 flushAndRunMultipleServers, 11 PeerTubeServer,
13 getActorImage, 12 setAccessTokensToServers,
14 getVideo,
15 getVideoChannel,
16 getVideoChannelVideos,
17 setDefaultVideoChannel, 13 setDefaultVideoChannel,
18 testFileExistsOrNot, 14 testFileExistsOrNot,
19 testImage, 15 testImage,
20 updateVideo, 16 wait,
21 updateVideoChannelImage, 17 waitJobs
22 uploadVideo, 18} from '@shared/extra-utils'
23 userLogin, 19import { User, VideoChannel } from '@shared/models'
24 wait
25} from '../../../../shared/extra-utils'
26import {
27 addVideoChannel,
28 deleteVideoChannel,
29 getAccountVideoChannelsList,
30 getMyUserInformation,
31 getVideoChannelsList,
32 ServerInfo,
33 setAccessTokensToServers,
34 updateVideoChannel,
35 viewVideo
36} from '../../../../shared/extra-utils/index'
37import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
38import { User, Video, VideoChannel, VideoDetails } from '../../../../shared/index'
39 20
40const expect = chai.expect 21const expect = chai.expect
41 22
42async function findChannel (server: ServerInfo, channelId: number) { 23async function findChannel (server: PeerTubeServer, channelId: number) {
43 const res = await getVideoChannelsList(server.url, 0, 5, '-name') 24 const body = await server.channels.list({ sort: '-name' })
44 const videoChannel = res.body.data.find(c => c.id === channelId)
45 25
46 return videoChannel as VideoChannel 26 return body.data.find(c => c.id === channelId)
47} 27}
48 28
49describe('Test video channels', function () { 29describe('Test video channels', function () {
50 let servers: ServerInfo[] 30 let servers: PeerTubeServer[]
51 let userInfo: User 31 let userInfo: User
52 let secondVideoChannelId: number 32 let secondVideoChannelId: number
53 let totoChannel: number 33 let totoChannel: number
@@ -60,7 +40,7 @@ describe('Test video channels', function () {
60 before(async function () { 40 before(async function () {
61 this.timeout(60000) 41 this.timeout(60000)
62 42
63 servers = await flushAndRunMultipleServers(2) 43 servers = await createMultipleServers(2)
64 44
65 await setAccessTokensToServers(servers) 45 await setAccessTokensToServers(servers)
66 await setDefaultVideoChannel(servers) 46 await setDefaultVideoChannel(servers)
@@ -69,11 +49,11 @@ describe('Test video channels', function () {
69 }) 49 })
70 50
71 it('Should have one video channel (created with root)', async () => { 51 it('Should have one video channel (created with root)', async () => {
72 const res = await getVideoChannelsList(servers[0].url, 0, 2) 52 const body = await servers[0].channels.list({ start: 0, count: 2 })
73 53
74 expect(res.body.total).to.equal(1) 54 expect(body.total).to.equal(1)
75 expect(res.body.data).to.be.an('array') 55 expect(body.data).to.be.an('array')
76 expect(res.body.data).to.have.lengthOf(1) 56 expect(body.data).to.have.lengthOf(1)
77 }) 57 })
78 58
79 it('Should create another video channel', async function () { 59 it('Should create another video channel', async function () {
@@ -86,23 +66,22 @@ describe('Test video channels', function () {
86 description: 'super video channel description', 66 description: 'super video channel description',
87 support: 'super video channel support text' 67 support: 'super video channel support text'
88 } 68 }
89 const res = await addVideoChannel(servers[0].url, servers[0].accessToken, videoChannel) 69 const created = await servers[0].channels.create({ attributes: videoChannel })
90 secondVideoChannelId = res.body.videoChannel.id 70 secondVideoChannelId = created.id
91 } 71 }
92 72
93 // The channel is 1 is propagated to servers 2 73 // The channel is 1 is propagated to servers 2
94 { 74 {
95 const videoAttributesArg = { name: 'my video name', channelId: secondVideoChannelId, support: 'video support field' } 75 const attributes = { name: 'my video name', channelId: secondVideoChannelId, support: 'video support field' }
96 const res = await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributesArg) 76 const { uuid } = await servers[0].videos.upload({ attributes })
97 videoUUID = res.body.video.uuid 77 videoUUID = uuid
98 } 78 }
99 79
100 await waitJobs(servers) 80 await waitJobs(servers)
101 }) 81 })
102 82
103 it('Should have two video channels when getting my information', async () => { 83 it('Should have two video channels when getting my information', async () => {
104 const res = await getMyUserInformation(servers[0].url, servers[0].accessToken) 84 userInfo = await servers[0].users.getMyInfo()
105 userInfo = res.body
106 85
107 expect(userInfo.videoChannels).to.be.an('array') 86 expect(userInfo.videoChannels).to.be.an('array')
108 expect(userInfo.videoChannels).to.have.lengthOf(2) 87 expect(userInfo.videoChannels).to.have.lengthOf(2)
@@ -120,16 +99,14 @@ describe('Test video channels', function () {
120 }) 99 })
121 100
122 it('Should have two video channels when getting account channels on server 1', async function () { 101 it('Should have two video channels when getting account channels on server 1', async function () {
123 const res = await getAccountVideoChannelsList({ 102 const body = await servers[0].channels.listByAccount({ accountName })
124 url: servers[0].url, 103 expect(body.total).to.equal(2)
125 accountName 104
126 }) 105 const videoChannels = body.data
127 106
128 expect(res.body.total).to.equal(2) 107 expect(videoChannels).to.be.an('array')
129 expect(res.body.data).to.be.an('array') 108 expect(videoChannels).to.have.lengthOf(2)
130 expect(res.body.data).to.have.lengthOf(2)
131 109
132 const videoChannels = res.body.data
133 expect(videoChannels[0].name).to.equal('root_channel') 110 expect(videoChannels[0].name).to.equal('root_channel')
134 expect(videoChannels[0].displayName).to.equal('Main root channel') 111 expect(videoChannels[0].displayName).to.equal('Main root channel')
135 112
@@ -141,79 +118,69 @@ describe('Test video channels', function () {
141 118
142 it('Should paginate and sort account channels', async function () { 119 it('Should paginate and sort account channels', async function () {
143 { 120 {
144 const res = await getAccountVideoChannelsList({ 121 const body = await servers[0].channels.listByAccount({
145 url: servers[0].url,
146 accountName, 122 accountName,
147 start: 0, 123 start: 0,
148 count: 1, 124 count: 1,
149 sort: 'createdAt' 125 sort: 'createdAt'
150 }) 126 })
151 127
152 expect(res.body.total).to.equal(2) 128 expect(body.total).to.equal(2)
153 expect(res.body.data).to.have.lengthOf(1) 129 expect(body.data).to.have.lengthOf(1)
154 130
155 const videoChannel: VideoChannel = res.body.data[0] 131 const videoChannel: VideoChannel = body.data[0]
156 expect(videoChannel.name).to.equal('root_channel') 132 expect(videoChannel.name).to.equal('root_channel')
157 } 133 }
158 134
159 { 135 {
160 const res = await getAccountVideoChannelsList({ 136 const body = await servers[0].channels.listByAccount({
161 url: servers[0].url,
162 accountName, 137 accountName,
163 start: 0, 138 start: 0,
164 count: 1, 139 count: 1,
165 sort: '-createdAt' 140 sort: '-createdAt'
166 }) 141 })
167 142
168 expect(res.body.total).to.equal(2) 143 expect(body.total).to.equal(2)
169 expect(res.body.data).to.have.lengthOf(1) 144 expect(body.data).to.have.lengthOf(1)
170 145 expect(body.data[0].name).to.equal('second_video_channel')
171 const videoChannel: VideoChannel = res.body.data[0]
172 expect(videoChannel.name).to.equal('second_video_channel')
173 } 146 }
174 147
175 { 148 {
176 const res = await getAccountVideoChannelsList({ 149 const body = await servers[0].channels.listByAccount({
177 url: servers[0].url,
178 accountName, 150 accountName,
179 start: 1, 151 start: 1,
180 count: 1, 152 count: 1,
181 sort: '-createdAt' 153 sort: '-createdAt'
182 }) 154 })
183 155
184 expect(res.body.total).to.equal(2) 156 expect(body.total).to.equal(2)
185 expect(res.body.data).to.have.lengthOf(1) 157 expect(body.data).to.have.lengthOf(1)
186 158 expect(body.data[0].name).to.equal('root_channel')
187 const videoChannel: VideoChannel = res.body.data[0]
188 expect(videoChannel.name).to.equal('root_channel')
189 } 159 }
190 }) 160 })
191 161
192 it('Should have one video channel when getting account channels on server 2', async function () { 162 it('Should have one video channel when getting account channels on server 2', async function () {
193 const res = await getAccountVideoChannelsList({ 163 const body = await servers[1].channels.listByAccount({ accountName })
194 url: servers[1].url,
195 accountName
196 })
197 164
198 expect(res.body.total).to.equal(1) 165 expect(body.total).to.equal(1)
199 expect(res.body.data).to.be.an('array') 166 expect(body.data).to.be.an('array')
200 expect(res.body.data).to.have.lengthOf(1) 167 expect(body.data).to.have.lengthOf(1)
201 168
202 const videoChannels = res.body.data 169 const videoChannel = body.data[0]
203 expect(videoChannels[0].name).to.equal('second_video_channel') 170 expect(videoChannel.name).to.equal('second_video_channel')
204 expect(videoChannels[0].displayName).to.equal('second video channel') 171 expect(videoChannel.displayName).to.equal('second video channel')
205 expect(videoChannels[0].description).to.equal('super video channel description') 172 expect(videoChannel.description).to.equal('super video channel description')
206 expect(videoChannels[0].support).to.equal('super video channel support text') 173 expect(videoChannel.support).to.equal('super video channel support text')
207 }) 174 })
208 175
209 it('Should list video channels', async function () { 176 it('Should list video channels', async function () {
210 const res = await getVideoChannelsList(servers[0].url, 1, 1, '-name') 177 const body = await servers[0].channels.list({ start: 1, count: 1, sort: '-name' })
211 178
212 expect(res.body.total).to.equal(2) 179 expect(body.total).to.equal(2)
213 expect(res.body.data).to.be.an('array') 180 expect(body.data).to.be.an('array')
214 expect(res.body.data).to.have.lengthOf(1) 181 expect(body.data).to.have.lengthOf(1)
215 expect(res.body.data[0].name).to.equal('root_channel') 182 expect(body.data[0].name).to.equal('root_channel')
216 expect(res.body.data[0].displayName).to.equal('Main root channel') 183 expect(body.data[0].displayName).to.equal('Main root channel')
217 }) 184 })
218 185
219 it('Should update video channel', async function () { 186 it('Should update video channel', async function () {
@@ -225,30 +192,29 @@ describe('Test video channels', function () {
225 support: 'support updated' 192 support: 'support updated'
226 } 193 }
227 194
228 await updateVideoChannel(servers[0].url, servers[0].accessToken, 'second_video_channel', videoChannelAttributes) 195 await servers[0].channels.update({ channelName: 'second_video_channel', attributes: videoChannelAttributes })
229 196
230 await waitJobs(servers) 197 await waitJobs(servers)
231 }) 198 })
232 199
233 it('Should have video channel updated', async function () { 200 it('Should have video channel updated', async function () {
234 for (const server of servers) { 201 for (const server of servers) {
235 const res = await getVideoChannelsList(server.url, 0, 1, '-name') 202 const body = await server.channels.list({ start: 0, count: 1, sort: '-name' })
236 203
237 expect(res.body.total).to.equal(2) 204 expect(body.total).to.equal(2)
238 expect(res.body.data).to.be.an('array') 205 expect(body.data).to.be.an('array')
239 expect(res.body.data).to.have.lengthOf(1) 206 expect(body.data).to.have.lengthOf(1)
240 expect(res.body.data[0].name).to.equal('second_video_channel') 207
241 expect(res.body.data[0].displayName).to.equal('video channel updated') 208 expect(body.data[0].name).to.equal('second_video_channel')
242 expect(res.body.data[0].description).to.equal('video channel description updated') 209 expect(body.data[0].displayName).to.equal('video channel updated')
243 expect(res.body.data[0].support).to.equal('support updated') 210 expect(body.data[0].description).to.equal('video channel description updated')
211 expect(body.data[0].support).to.equal('support updated')
244 } 212 }
245 }) 213 })
246 214
247 it('Should not have updated the video support field', async function () { 215 it('Should not have updated the video support field', async function () {
248 for (const server of servers) { 216 for (const server of servers) {
249 const res = await getVideo(server.url, videoUUID) 217 const video = await server.videos.get({ id: videoUUID })
250 const video: VideoDetails = res.body
251
252 expect(video.support).to.equal('video support field') 218 expect(video.support).to.equal('video support field')
253 } 219 }
254 }) 220 })
@@ -261,14 +227,12 @@ describe('Test video channels', function () {
261 bulkVideosSupportUpdate: true 227 bulkVideosSupportUpdate: true
262 } 228 }
263 229
264 await updateVideoChannel(servers[0].url, servers[0].accessToken, 'second_video_channel', videoChannelAttributes) 230 await servers[0].channels.update({ channelName: 'second_video_channel', attributes: videoChannelAttributes })
265 231
266 await waitJobs(servers) 232 await waitJobs(servers)
267 233
268 for (const server of servers) { 234 for (const server of servers) {
269 const res = await getVideo(server.url, videoUUID) 235 const video = await server.videos.get({ id: videoUUID })
270 const video: VideoDetails = res.body
271
272 expect(video.support).to.equal(videoChannelAttributes.support) 236 expect(video.support).to.equal(videoChannelAttributes.support)
273 } 237 }
274 }) 238 })
@@ -278,10 +242,8 @@ describe('Test video channels', function () {
278 242
279 const fixture = 'avatar.png' 243 const fixture = 'avatar.png'
280 244
281 await updateVideoChannelImage({ 245 await servers[0].channels.updateImage({
282 url: servers[0].url, 246 channelName: 'second_video_channel',
283 accessToken: servers[0].accessToken,
284 videoChannelName: 'second_video_channel',
285 fixture, 247 fixture,
286 type: 'avatar' 248 type: 'avatar'
287 }) 249 })
@@ -295,7 +257,7 @@ describe('Test video channels', function () {
295 await testImage(server.url, 'avatar-resized', avatarPaths[server.port], '.png') 257 await testImage(server.url, 'avatar-resized', avatarPaths[server.port], '.png')
296 await testFileExistsOrNot(server, 'avatars', basename(avatarPaths[server.port]), true) 258 await testFileExistsOrNot(server, 'avatars', basename(avatarPaths[server.port]), true)
297 259
298 const row = await getActorImage(server.internalServerNumber, basename(avatarPaths[server.port])) 260 const row = await server.sql.getActorImage(basename(avatarPaths[server.port]))
299 expect(row.height).to.equal(ACTOR_IMAGES_SIZE.AVATARS.height) 261 expect(row.height).to.equal(ACTOR_IMAGES_SIZE.AVATARS.height)
300 expect(row.width).to.equal(ACTOR_IMAGES_SIZE.AVATARS.width) 262 expect(row.width).to.equal(ACTOR_IMAGES_SIZE.AVATARS.width)
301 } 263 }
@@ -306,10 +268,8 @@ describe('Test video channels', function () {
306 268
307 const fixture = 'banner.jpg' 269 const fixture = 'banner.jpg'
308 270
309 await updateVideoChannelImage({ 271 await servers[0].channels.updateImage({
310 url: servers[0].url, 272 channelName: 'second_video_channel',
311 accessToken: servers[0].accessToken,
312 videoChannelName: 'second_video_channel',
313 fixture, 273 fixture,
314 type: 'banner' 274 type: 'banner'
315 }) 275 })
@@ -317,14 +277,13 @@ describe('Test video channels', function () {
317 await waitJobs(servers) 277 await waitJobs(servers)
318 278
319 for (const server of servers) { 279 for (const server of servers) {
320 const res = await getVideoChannel(server.url, 'second_video_channel@' + servers[0].host) 280 const videoChannel = await server.channels.get({ channelName: 'second_video_channel@' + servers[0].host })
321 const videoChannel = res.body
322 281
323 bannerPaths[server.port] = videoChannel.banner.path 282 bannerPaths[server.port] = videoChannel.banner.path
324 await testImage(server.url, 'banner-resized', bannerPaths[server.port]) 283 await testImage(server.url, 'banner-resized', bannerPaths[server.port])
325 await testFileExistsOrNot(server, 'avatars', basename(bannerPaths[server.port]), true) 284 await testFileExistsOrNot(server, 'avatars', basename(bannerPaths[server.port]), true)
326 285
327 const row = await getActorImage(server.internalServerNumber, basename(bannerPaths[server.port])) 286 const row = await server.sql.getActorImage(basename(bannerPaths[server.port]))
328 expect(row.height).to.equal(ACTOR_IMAGES_SIZE.BANNERS.height) 287 expect(row.height).to.equal(ACTOR_IMAGES_SIZE.BANNERS.height)
329 expect(row.width).to.equal(ACTOR_IMAGES_SIZE.BANNERS.width) 288 expect(row.width).to.equal(ACTOR_IMAGES_SIZE.BANNERS.width)
330 } 289 }
@@ -333,12 +292,7 @@ describe('Test video channels', function () {
333 it('Should delete the video channel avatar', async function () { 292 it('Should delete the video channel avatar', async function () {
334 this.timeout(15000) 293 this.timeout(15000)
335 294
336 await deleteVideoChannelImage({ 295 await servers[0].channels.deleteImage({ channelName: 'second_video_channel', type: 'avatar' })
337 url: servers[0].url,
338 accessToken: servers[0].accessToken,
339 videoChannelName: 'second_video_channel',
340 type: 'avatar'
341 })
342 296
343 await waitJobs(servers) 297 await waitJobs(servers)
344 298
@@ -353,12 +307,7 @@ describe('Test video channels', function () {
353 it('Should delete the video channel banner', async function () { 307 it('Should delete the video channel banner', async function () {
354 this.timeout(15000) 308 this.timeout(15000)
355 309
356 await deleteVideoChannelImage({ 310 await servers[0].channels.deleteImage({ channelName: 'second_video_channel', type: 'banner' })
357 url: servers[0].url,
358 accessToken: servers[0].accessToken,
359 videoChannelName: 'second_video_channel',
360 type: 'banner'
361 })
362 311
363 await waitJobs(servers) 312 await waitJobs(servers)
364 313
@@ -375,18 +324,19 @@ describe('Test video channels', function () {
375 324
376 for (const server of servers) { 325 for (const server of servers) {
377 const channelURI = 'second_video_channel@localhost:' + servers[0].port 326 const channelURI = 'second_video_channel@localhost:' + servers[0].port
378 const res1 = await getVideoChannelVideos(server.url, server.accessToken, channelURI, 0, 5) 327 const { total, data } = await server.videos.listByChannel({ handle: channelURI })
379 expect(res1.body.total).to.equal(1) 328
380 expect(res1.body.data).to.be.an('array') 329 expect(total).to.equal(1)
381 expect(res1.body.data).to.have.lengthOf(1) 330 expect(data).to.be.an('array')
382 expect(res1.body.data[0].name).to.equal('my video name') 331 expect(data).to.have.lengthOf(1)
332 expect(data[0].name).to.equal('my video name')
383 } 333 }
384 }) 334 })
385 335
386 it('Should change the video channel of a video', async function () { 336 it('Should change the video channel of a video', async function () {
387 this.timeout(10000) 337 this.timeout(10000)
388 338
389 await updateVideo(servers[0].url, servers[0].accessToken, videoUUID, { channelId: servers[0].videoChannel.id }) 339 await servers[0].videos.update({ id: videoUUID, attributes: { channelId: servers[0].store.channel.id } })
390 340
391 await waitJobs(servers) 341 await waitJobs(servers)
392 }) 342 })
@@ -395,47 +345,50 @@ describe('Test video channels', function () {
395 this.timeout(10000) 345 this.timeout(10000)
396 346
397 for (const server of servers) { 347 for (const server of servers) {
398 const secondChannelURI = 'second_video_channel@localhost:' + servers[0].port 348 {
399 const res1 = await getVideoChannelVideos(server.url, server.accessToken, secondChannelURI, 0, 5) 349 const secondChannelURI = 'second_video_channel@localhost:' + servers[0].port
400 expect(res1.body.total).to.equal(0) 350 const { total } = await server.videos.listByChannel({ handle: secondChannelURI })
401 351 expect(total).to.equal(0)
402 const channelURI = 'root_channel@localhost:' + servers[0].port 352 }
403 const res2 = await getVideoChannelVideos(server.url, server.accessToken, channelURI, 0, 5) 353
404 expect(res2.body.total).to.equal(1) 354 {
405 355 const channelURI = 'root_channel@localhost:' + servers[0].port
406 const videos: Video[] = res2.body.data 356 const { total, data } = await server.videos.listByChannel({ handle: channelURI })
407 expect(videos).to.be.an('array') 357 expect(total).to.equal(1)
408 expect(videos).to.have.lengthOf(1) 358
409 expect(videos[0].name).to.equal('my video name') 359 expect(data).to.be.an('array')
360 expect(data).to.have.lengthOf(1)
361 expect(data[0].name).to.equal('my video name')
362 }
410 } 363 }
411 }) 364 })
412 365
413 it('Should delete video channel', async function () { 366 it('Should delete video channel', async function () {
414 await deleteVideoChannel(servers[0].url, servers[0].accessToken, 'second_video_channel') 367 await servers[0].channels.delete({ channelName: 'second_video_channel' })
415 }) 368 })
416 369
417 it('Should have video channel deleted', async function () { 370 it('Should have video channel deleted', async function () {
418 const res = await getVideoChannelsList(servers[0].url, 0, 10) 371 const body = await servers[0].channels.list({ start: 0, count: 10 })
419 372
420 expect(res.body.total).to.equal(1) 373 expect(body.total).to.equal(1)
421 expect(res.body.data).to.be.an('array') 374 expect(body.data).to.be.an('array')
422 expect(res.body.data).to.have.lengthOf(1) 375 expect(body.data).to.have.lengthOf(1)
423 expect(res.body.data[0].displayName).to.equal('Main root channel') 376 expect(body.data[0].displayName).to.equal('Main root channel')
424 }) 377 })
425 378
426 it('Should create the main channel with an uuid if there is a conflict', async function () { 379 it('Should create the main channel with an uuid if there is a conflict', async function () {
427 { 380 {
428 const videoChannel = { name: 'toto_channel', displayName: 'My toto channel' } 381 const videoChannel = { name: 'toto_channel', displayName: 'My toto channel' }
429 const res = await addVideoChannel(servers[0].url, servers[0].accessToken, videoChannel) 382 const created = await servers[0].channels.create({ attributes: videoChannel })
430 totoChannel = res.body.videoChannel.id 383 totoChannel = created.id
431 } 384 }
432 385
433 { 386 {
434 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: 'toto', password: 'password' }) 387 await servers[0].users.create({ username: 'toto', password: 'password' })
435 const accessToken = await userLogin(servers[0], { username: 'toto', password: 'password' }) 388 const accessToken = await servers[0].login.getAccessToken({ username: 'toto', password: 'password' })
436 389
437 const res = await getMyUserInformation(servers[0].url, accessToken) 390 const { videoChannels } = await servers[0].users.getMyInfo({ token: accessToken })
438 const videoChannel = res.body.videoChannels[0] 391 const videoChannel = videoChannels[0]
439 expect(videoChannel.name).to.match(/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/) 392 expect(videoChannel.name).to.match(/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/)
440 } 393 }
441 }) 394 })
@@ -444,15 +397,9 @@ describe('Test video channels', function () {
444 this.timeout(10000) 397 this.timeout(10000)
445 398
446 { 399 {
447 const res = await getAccountVideoChannelsList({ 400 const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true })
448 url: servers[0].url,
449 accountName,
450 withStats: true
451 })
452
453 const channels: VideoChannel[] = res.body.data
454 401
455 for (const channel of channels) { 402 for (const channel of data) {
456 expect(channel).to.haveOwnProperty('viewsPerDay') 403 expect(channel).to.haveOwnProperty('viewsPerDay')
457 expect(channel.viewsPerDay).to.have.length(30 + 1) // daysPrior + today 404 expect(channel.viewsPerDay).to.have.length(30 + 1) // daysPrior + today
458 405
@@ -464,33 +411,24 @@ describe('Test video channels', function () {
464 } 411 }
465 412
466 { 413 {
467 // video has been posted on channel servers[0].videoChannel.id since last update 414 // video has been posted on channel servers[0].store.videoChannel.id since last update
468 await viewVideo(servers[0].url, videoUUID, 204, '0.0.0.1,127.0.0.1') 415 await servers[0].videos.view({ id: videoUUID, xForwardedFor: '0.0.0.1,127.0.0.1' })
469 await viewVideo(servers[0].url, videoUUID, 204, '0.0.0.2,127.0.0.1') 416 await servers[0].videos.view({ id: videoUUID, xForwardedFor: '0.0.0.2,127.0.0.1' })
470 417
471 // Wait the repeatable job 418 // Wait the repeatable job
472 await wait(8000) 419 await wait(8000)
473 420
474 const res = await getAccountVideoChannelsList({ 421 const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true })
475 url: servers[0].url, 422 const channelWithView = data.find(channel => channel.id === servers[0].store.channel.id)
476 accountName,
477 withStats: true
478 })
479 const channelWithView = res.body.data.find((channel: VideoChannel) => channel.id === servers[0].videoChannel.id)
480 expect(channelWithView.viewsPerDay.slice(-1)[0].views).to.equal(2) 423 expect(channelWithView.viewsPerDay.slice(-1)[0].views).to.equal(2)
481 } 424 }
482 }) 425 })
483 426
484 it('Should report correct videos count', async function () { 427 it('Should report correct videos count', async function () {
485 const res = await getAccountVideoChannelsList({ 428 const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true })
486 url: servers[0].url,
487 accountName,
488 withStats: true
489 })
490 const channels: VideoChannel[] = res.body.data
491 429
492 const totoChannel = channels.find(c => c.name === 'toto_channel') 430 const totoChannel = data.find(c => c.name === 'toto_channel')
493 const rootChannel = channels.find(c => c.name === 'root_channel') 431 const rootChannel = data.find(c => c.name === 'root_channel')
494 432
495 expect(rootChannel.videosCount).to.equal(1) 433 expect(rootChannel.videosCount).to.equal(1)
496 expect(totoChannel.videosCount).to.equal(0) 434 expect(totoChannel.videosCount).to.equal(0)
@@ -498,26 +436,18 @@ describe('Test video channels', function () {
498 436
499 it('Should search among account video channels', async function () { 437 it('Should search among account video channels', async function () {
500 { 438 {
501 const res = await getAccountVideoChannelsList({ 439 const body = await servers[0].channels.listByAccount({ accountName, search: 'root' })
502 url: servers[0].url, 440 expect(body.total).to.equal(1)
503 accountName,
504 search: 'root'
505 })
506 expect(res.body.total).to.equal(1)
507 441
508 const channels = res.body.data 442 const channels = body.data
509 expect(channels).to.have.lengthOf(1) 443 expect(channels).to.have.lengthOf(1)
510 } 444 }
511 445
512 { 446 {
513 const res = await getAccountVideoChannelsList({ 447 const body = await servers[0].channels.listByAccount({ accountName, search: 'does not exist' })
514 url: servers[0].url, 448 expect(body.total).to.equal(0)
515 accountName,
516 search: 'does not exist'
517 })
518 expect(res.body.total).to.equal(0)
519 449
520 const channels = res.body.data 450 const channels = body.data
521 expect(channels).to.have.lengthOf(0) 451 expect(channels).to.have.lengthOf(0)
522 } 452 }
523 }) 453 })
@@ -525,34 +455,24 @@ describe('Test video channels', function () {
525 it('Should list channels by updatedAt desc if a video has been uploaded', async function () { 455 it('Should list channels by updatedAt desc if a video has been uploaded', async function () {
526 this.timeout(30000) 456 this.timeout(30000)
527 457
528 await uploadVideo(servers[0].url, servers[0].accessToken, { channelId: totoChannel }) 458 await servers[0].videos.upload({ attributes: { channelId: totoChannel } })
529 await waitJobs(servers) 459 await waitJobs(servers)
530 460
531 for (const server of servers) { 461 for (const server of servers) {
532 const res = await getAccountVideoChannelsList({ 462 const { data } = await server.channels.listByAccount({ accountName, sort: '-updatedAt' })
533 url: server.url,
534 accountName,
535 sort: '-updatedAt'
536 })
537 463
538 const channels: VideoChannel[] = res.body.data 464 expect(data[0].name).to.equal('toto_channel')
539 expect(channels[0].name).to.equal('toto_channel') 465 expect(data[1].name).to.equal('root_channel')
540 expect(channels[1].name).to.equal('root_channel')
541 } 466 }
542 467
543 await uploadVideo(servers[0].url, servers[0].accessToken, { channelId: servers[0].videoChannel.id }) 468 await servers[0].videos.upload({ attributes: { channelId: servers[0].store.channel.id } })
544 await waitJobs(servers) 469 await waitJobs(servers)
545 470
546 for (const server of servers) { 471 for (const server of servers) {
547 const res = await getAccountVideoChannelsList({ 472 const { data } = await server.channels.listByAccount({ accountName, sort: '-updatedAt' })
548 url: server.url,
549 accountName,
550 sort: '-updatedAt'
551 })
552 473
553 const channels: VideoChannel[] = res.body.data 474 expect(data[0].name).to.equal('root_channel')
554 expect(channels[0].name).to.equal('root_channel') 475 expect(data[1].name).to.equal('toto_channel')
555 expect(channels[1].name).to.equal('toto_channel')
556 } 476 }
557 }) 477 })
558 478
diff --git a/server/tests/api/videos/video-comments.ts b/server/tests/api/videos/video-comments.ts
index b6b002307..61ee54540 100644
--- a/server/tests/api/videos/video-comments.ts
+++ b/server/tests/api/videos/video-comments.ts
@@ -2,80 +2,62 @@
2 2
3import 'mocha' 3import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5import { VideoComment, VideoCommentAdmin, VideoCommentThreadTree } from '@shared/models'
6import { cleanupTests, testImage } from '../../../../shared/extra-utils'
7import { 5import {
8 createUser, 6 cleanupTests,
7 CommentsCommand,
8 createSingleServer,
9 dateIsValid, 9 dateIsValid,
10 flushAndRunServer, 10 PeerTubeServer,
11 getAccessToken,
12 ServerInfo,
13 setAccessTokensToServers, 11 setAccessTokensToServers,
14 updateMyAvatar, 12 testImage
15 uploadVideo 13} from '@shared/extra-utils'
16} from '../../../../shared/extra-utils/index'
17import {
18 addVideoCommentReply,
19 addVideoCommentThread,
20 deleteVideoComment,
21 getAdminVideoComments,
22 getVideoCommentThreads,
23 getVideoThreadComments
24} from '../../../../shared/extra-utils/videos/video-comments'
25 14
26const expect = chai.expect 15const expect = chai.expect
27 16
28describe('Test video comments', function () { 17describe('Test video comments', function () {
29 let server: ServerInfo 18 let server: PeerTubeServer
30 let videoId 19 let videoId: number
31 let videoUUID 20 let videoUUID: string
32 let threadId 21 let threadId: number
33 let replyToDeleteId: number 22 let replyToDeleteId: number
34 23
35 let userAccessTokenServer1: string 24 let userAccessTokenServer1: string
36 25
26 let command: CommentsCommand
27
37 before(async function () { 28 before(async function () {
38 this.timeout(30000) 29 this.timeout(30000)
39 30
40 server = await flushAndRunServer(1) 31 server = await createSingleServer(1)
41 32
42 await setAccessTokensToServers([ server ]) 33 await setAccessTokensToServers([ server ])
43 34
44 const res = await uploadVideo(server.url, server.accessToken, {}) 35 const { id, uuid } = await server.videos.upload()
45 videoUUID = res.body.video.uuid 36 videoUUID = uuid
46 videoId = res.body.video.id 37 videoId = id
47 38
48 await updateMyAvatar({ 39 await server.users.updateMyAvatar({ fixture: 'avatar.png' })
49 url: server.url,
50 accessToken: server.accessToken,
51 fixture: 'avatar.png'
52 })
53 40
54 await createUser({ 41 userAccessTokenServer1 = await server.users.generateUserAndToken('user1')
55 url: server.url, 42
56 accessToken: server.accessToken, 43 command = server.comments
57 username: 'user1',
58 password: 'password'
59 })
60 userAccessTokenServer1 = await getAccessToken(server.url, 'user1', 'password')
61 }) 44 })
62 45
63 describe('User comments', function () { 46 describe('User comments', function () {
64 47
65 it('Should not have threads on this video', async function () { 48 it('Should not have threads on this video', async function () {
66 const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5) 49 const body = await command.listThreads({ videoId: videoUUID })
67 50
68 expect(res.body.total).to.equal(0) 51 expect(body.total).to.equal(0)
69 expect(res.body.totalNotDeletedComments).to.equal(0) 52 expect(body.totalNotDeletedComments).to.equal(0)
70 expect(res.body.data).to.be.an('array') 53 expect(body.data).to.be.an('array')
71 expect(res.body.data).to.have.lengthOf(0) 54 expect(body.data).to.have.lengthOf(0)
72 }) 55 })
73 56
74 it('Should create a thread in this video', async function () { 57 it('Should create a thread in this video', async function () {
75 const text = 'my super first comment' 58 const text = 'my super first comment'
76 59
77 const res = await addVideoCommentThread(server.url, server.accessToken, videoUUID, text) 60 const comment = await command.createThread({ videoId: videoUUID, text })
78 const comment = res.body.comment
79 61
80 expect(comment.inReplyToCommentId).to.be.null 62 expect(comment.inReplyToCommentId).to.be.null
81 expect(comment.text).equal('my super first comment') 63 expect(comment.text).equal('my super first comment')
@@ -91,14 +73,14 @@ describe('Test video comments', function () {
91 }) 73 })
92 74
93 it('Should list threads of this video', async function () { 75 it('Should list threads of this video', async function () {
94 const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5) 76 const body = await command.listThreads({ videoId: videoUUID })
95 77
96 expect(res.body.total).to.equal(1) 78 expect(body.total).to.equal(1)
97 expect(res.body.totalNotDeletedComments).to.equal(1) 79 expect(body.totalNotDeletedComments).to.equal(1)
98 expect(res.body.data).to.be.an('array') 80 expect(body.data).to.be.an('array')
99 expect(res.body.data).to.have.lengthOf(1) 81 expect(body.data).to.have.lengthOf(1)
100 82
101 const comment: VideoComment = res.body.data[0] 83 const comment = body.data[0]
102 expect(comment.inReplyToCommentId).to.be.null 84 expect(comment.inReplyToCommentId).to.be.null
103 expect(comment.text).equal('my super first comment') 85 expect(comment.text).equal('my super first comment')
104 expect(comment.videoId).to.equal(videoId) 86 expect(comment.videoId).to.equal(videoId)
@@ -117,9 +99,9 @@ describe('Test video comments', function () {
117 }) 99 })
118 100
119 it('Should get all the thread created', async function () { 101 it('Should get all the thread created', async function () {
120 const res = await getVideoThreadComments(server.url, videoUUID, threadId) 102 const body = await command.getThread({ videoId: videoUUID, threadId })
121 103
122 const rootComment = res.body.comment 104 const rootComment = body.comment
123 expect(rootComment.inReplyToCommentId).to.be.null 105 expect(rootComment.inReplyToCommentId).to.be.null
124 expect(rootComment.text).equal('my super first comment') 106 expect(rootComment.text).equal('my super first comment')
125 expect(rootComment.videoId).to.equal(videoId) 107 expect(rootComment.videoId).to.equal(videoId)
@@ -129,20 +111,19 @@ describe('Test video comments', function () {
129 111
130 it('Should create multiple replies in this thread', async function () { 112 it('Should create multiple replies in this thread', async function () {
131 const text1 = 'my super answer to thread 1' 113 const text1 = 'my super answer to thread 1'
132 const childCommentRes = await addVideoCommentReply(server.url, server.accessToken, videoId, threadId, text1) 114 const created = await command.addReply({ videoId, toCommentId: threadId, text: text1 })
133 const childCommentId = childCommentRes.body.comment.id 115 const childCommentId = created.id
134 116
135 const text2 = 'my super answer to answer of thread 1' 117 const text2 = 'my super answer to answer of thread 1'
136 await addVideoCommentReply(server.url, server.accessToken, videoId, childCommentId, text2) 118 await command.addReply({ videoId, toCommentId: childCommentId, text: text2 })
137 119
138 const text3 = 'my second answer to thread 1' 120 const text3 = 'my second answer to thread 1'
139 await addVideoCommentReply(server.url, server.accessToken, videoId, threadId, text3) 121 await command.addReply({ videoId, toCommentId: threadId, text: text3 })
140 }) 122 })
141 123
142 it('Should get correctly the replies', async function () { 124 it('Should get correctly the replies', async function () {
143 const res = await getVideoThreadComments(server.url, videoUUID, threadId) 125 const tree = await command.getThread({ videoId: videoUUID, threadId })
144 126
145 const tree: VideoCommentThreadTree = res.body
146 expect(tree.comment.text).equal('my super first comment') 127 expect(tree.comment.text).equal('my super first comment')
147 expect(tree.children).to.have.lengthOf(2) 128 expect(tree.children).to.have.lengthOf(2)
148 129
@@ -163,42 +144,41 @@ describe('Test video comments', function () {
163 144
164 it('Should create other threads', async function () { 145 it('Should create other threads', async function () {
165 const text1 = 'super thread 2' 146 const text1 = 'super thread 2'
166 await addVideoCommentThread(server.url, server.accessToken, videoUUID, text1) 147 await command.createThread({ videoId: videoUUID, text: text1 })
167 148
168 const text2 = 'super thread 3' 149 const text2 = 'super thread 3'
169 await addVideoCommentThread(server.url, server.accessToken, videoUUID, text2) 150 await command.createThread({ videoId: videoUUID, text: text2 })
170 }) 151 })
171 152
172 it('Should list the threads', async function () { 153 it('Should list the threads', async function () {
173 const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5, 'createdAt') 154 const body = await command.listThreads({ videoId: videoUUID, sort: 'createdAt' })
174 155
175 expect(res.body.total).to.equal(3) 156 expect(body.total).to.equal(3)
176 expect(res.body.totalNotDeletedComments).to.equal(6) 157 expect(body.totalNotDeletedComments).to.equal(6)
177 expect(res.body.data).to.be.an('array') 158 expect(body.data).to.be.an('array')
178 expect(res.body.data).to.have.lengthOf(3) 159 expect(body.data).to.have.lengthOf(3)
179 160
180 expect(res.body.data[0].text).to.equal('my super first comment') 161 expect(body.data[0].text).to.equal('my super first comment')
181 expect(res.body.data[0].totalReplies).to.equal(3) 162 expect(body.data[0].totalReplies).to.equal(3)
182 expect(res.body.data[1].text).to.equal('super thread 2') 163 expect(body.data[1].text).to.equal('super thread 2')
183 expect(res.body.data[1].totalReplies).to.equal(0) 164 expect(body.data[1].totalReplies).to.equal(0)
184 expect(res.body.data[2].text).to.equal('super thread 3') 165 expect(body.data[2].text).to.equal('super thread 3')
185 expect(res.body.data[2].totalReplies).to.equal(0) 166 expect(body.data[2].totalReplies).to.equal(0)
186 }) 167 })
187 168
188 it('Should delete a reply', async function () { 169 it('Should delete a reply', async function () {
189 await deleteVideoComment(server.url, server.accessToken, videoId, replyToDeleteId) 170 await command.delete({ videoId, commentId: replyToDeleteId })
190 171
191 { 172 {
192 const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5, 'createdAt') 173 const body = await command.listThreads({ videoId: videoUUID, sort: 'createdAt' })
193 174
194 expect(res.body.total).to.equal(3) 175 expect(body.total).to.equal(3)
195 expect(res.body.totalNotDeletedComments).to.equal(5) 176 expect(body.totalNotDeletedComments).to.equal(5)
196 } 177 }
197 178
198 { 179 {
199 const res = await getVideoThreadComments(server.url, videoUUID, threadId) 180 const tree = await command.getThread({ videoId: videoUUID, threadId })
200 181
201 const tree: VideoCommentThreadTree = res.body
202 expect(tree.comment.text).equal('my super first comment') 182 expect(tree.comment.text).equal('my super first comment')
203 expect(tree.children).to.have.lengthOf(2) 183 expect(tree.children).to.have.lengthOf(2)
204 184
@@ -220,99 +200,88 @@ describe('Test video comments', function () {
220 }) 200 })
221 201
222 it('Should delete a complete thread', async function () { 202 it('Should delete a complete thread', async function () {
223 await deleteVideoComment(server.url, server.accessToken, videoId, threadId) 203 await command.delete({ videoId, commentId: threadId })
224 204
225 const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5, 'createdAt') 205 const body = await command.listThreads({ videoId: videoUUID, sort: 'createdAt' })
226 expect(res.body.total).to.equal(3) 206 expect(body.total).to.equal(3)
227 expect(res.body.data).to.be.an('array') 207 expect(body.data).to.be.an('array')
228 expect(res.body.data).to.have.lengthOf(3) 208 expect(body.data).to.have.lengthOf(3)
229 209
230 expect(res.body.data[0].text).to.equal('') 210 expect(body.data[0].text).to.equal('')
231 expect(res.body.data[0].isDeleted).to.be.true 211 expect(body.data[0].isDeleted).to.be.true
232 expect(res.body.data[0].deletedAt).to.not.be.null 212 expect(body.data[0].deletedAt).to.not.be.null
233 expect(res.body.data[0].account).to.be.null 213 expect(body.data[0].account).to.be.null
234 expect(res.body.data[0].totalReplies).to.equal(2) 214 expect(body.data[0].totalReplies).to.equal(2)
235 expect(res.body.data[1].text).to.equal('super thread 2') 215 expect(body.data[1].text).to.equal('super thread 2')
236 expect(res.body.data[1].totalReplies).to.equal(0) 216 expect(body.data[1].totalReplies).to.equal(0)
237 expect(res.body.data[2].text).to.equal('super thread 3') 217 expect(body.data[2].text).to.equal('super thread 3')
238 expect(res.body.data[2].totalReplies).to.equal(0) 218 expect(body.data[2].totalReplies).to.equal(0)
239 }) 219 })
240 220
241 it('Should count replies from the video author correctly', async function () { 221 it('Should count replies from the video author correctly', async function () {
242 const text = 'my super first comment' 222 await command.createThread({ videoId: videoUUID, text: 'my super first comment' })
243 await addVideoCommentThread(server.url, server.accessToken, videoUUID, text) 223
244 let res = await getVideoCommentThreads(server.url, videoUUID, 0, 5) 224 const { data } = await command.listThreads({ videoId: videoUUID })
245 const comment: VideoComment = res.body.data[0] 225 const threadId2 = data[0].threadId
246 const threadId2 = comment.threadId
247 226
248 const text2 = 'a first answer to thread 4 by a third party' 227 const text2 = 'a first answer to thread 4 by a third party'
249 await addVideoCommentReply(server.url, userAccessTokenServer1, videoId, threadId2, text2) 228 await command.addReply({ token: userAccessTokenServer1, videoId, toCommentId: threadId2, text: text2 })
250 229
251 const text3 = 'my second answer to thread 4' 230 const text3 = 'my second answer to thread 4'
252 await addVideoCommentReply(server.url, server.accessToken, videoId, threadId2, text3) 231 await command.addReply({ videoId, toCommentId: threadId2, text: text3 })
253 232
254 res = await getVideoThreadComments(server.url, videoUUID, threadId2) 233 const tree = await command.getThread({ videoId: videoUUID, threadId: threadId2 })
255 const tree: VideoCommentThreadTree = res.body
256 expect(tree.comment.totalReplies).to.equal(tree.comment.totalRepliesFromVideoAuthor + 1) 234 expect(tree.comment.totalReplies).to.equal(tree.comment.totalRepliesFromVideoAuthor + 1)
257 }) 235 })
258 }) 236 })
259 237
260 describe('All instance comments', function () { 238 describe('All instance comments', function () {
261 async function getComments (options: any = {}) {
262 const res = await getAdminVideoComments(Object.assign({
263 url: server.url,
264 token: server.accessToken,
265 start: 0,
266 count: 10
267 }, options))
268
269 return { comments: res.body.data as VideoCommentAdmin[], total: res.body.total as number }
270 }
271 239
272 it('Should list instance comments as admin', async function () { 240 it('Should list instance comments as admin', async function () {
273 const { comments } = await getComments({ start: 0, count: 1 }) 241 const { data } = await command.listForAdmin({ start: 0, count: 1 })
274 242
275 expect(comments[0].text).to.equal('my second answer to thread 4') 243 expect(data[0].text).to.equal('my second answer to thread 4')
276 }) 244 })
277 245
278 it('Should filter instance comments by isLocal', async function () { 246 it('Should filter instance comments by isLocal', async function () {
279 const { total, comments } = await getComments({ isLocal: false }) 247 const { total, data } = await command.listForAdmin({ isLocal: false })
280 248
281 expect(comments).to.have.lengthOf(0) 249 expect(data).to.have.lengthOf(0)
282 expect(total).to.equal(0) 250 expect(total).to.equal(0)
283 }) 251 })
284 252
285 it('Should search instance comments by account', async function () { 253 it('Should search instance comments by account', async function () {
286 const { total, comments } = await getComments({ searchAccount: 'user' }) 254 const { total, data } = await command.listForAdmin({ searchAccount: 'user' })
287 255
288 expect(comments).to.have.lengthOf(1) 256 expect(data).to.have.lengthOf(1)
289 expect(total).to.equal(1) 257 expect(total).to.equal(1)
290 258
291 expect(comments[0].text).to.equal('a first answer to thread 4 by a third party') 259 expect(data[0].text).to.equal('a first answer to thread 4 by a third party')
292 }) 260 })
293 261
294 it('Should search instance comments by video', async function () { 262 it('Should search instance comments by video', async function () {
295 { 263 {
296 const { total, comments } = await getComments({ searchVideo: 'video' }) 264 const { total, data } = await command.listForAdmin({ searchVideo: 'video' })
297 265
298 expect(comments).to.have.lengthOf(7) 266 expect(data).to.have.lengthOf(7)
299 expect(total).to.equal(7) 267 expect(total).to.equal(7)
300 } 268 }
301 269
302 { 270 {
303 const { total, comments } = await getComments({ searchVideo: 'hello' }) 271 const { total, data } = await command.listForAdmin({ searchVideo: 'hello' })
304 272
305 expect(comments).to.have.lengthOf(0) 273 expect(data).to.have.lengthOf(0)
306 expect(total).to.equal(0) 274 expect(total).to.equal(0)
307 } 275 }
308 }) 276 })
309 277
310 it('Should search instance comments', async function () { 278 it('Should search instance comments', async function () {
311 const { total, comments } = await getComments({ search: 'super thread 3' }) 279 const { total, data } = await command.listForAdmin({ search: 'super thread 3' })
312 280
313 expect(comments).to.have.lengthOf(1)
314 expect(total).to.equal(1) 281 expect(total).to.equal(1)
315 expect(comments[0].text).to.equal('super thread 3') 282
283 expect(data).to.have.lengthOf(1)
284 expect(data[0].text).to.equal('super thread 3')
316 }) 285 })
317 }) 286 })
318 287
diff --git a/server/tests/api/videos/video-description.ts b/server/tests/api/videos/video-description.ts
index b8e98e45f..d22b4ed96 100644
--- a/server/tests/api/videos/video-description.ts
+++ b/server/tests/api/videos/video-description.ts
@@ -1,25 +1,13 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2 2
3import * as chai from 'chai'
4import 'mocha' 3import 'mocha'
5import { 4import * as chai from 'chai'
6 cleanupTests, 5import { cleanupTests, createMultipleServers, doubleFollow, PeerTubeServer, setAccessTokensToServers, waitJobs } from '@shared/extra-utils'
7 flushAndRunMultipleServers,
8 getVideo,
9 getVideoDescription,
10 getVideosList,
11 ServerInfo,
12 setAccessTokensToServers,
13 updateVideo,
14 uploadVideo
15} from '../../../../shared/extra-utils/index'
16import { doubleFollow } from '../../../../shared/extra-utils/server/follows'
17import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
18 6
19const expect = chai.expect 7const expect = chai.expect
20 8
21describe('Test video description', function () { 9describe('Test video description', function () {
22 let servers: ServerInfo[] = [] 10 let servers: PeerTubeServer[] = []
23 let videoUUID = '' 11 let videoUUID = ''
24 let videoId: number 12 let videoId: number
25 const longDescription = 'my super description for server 1'.repeat(50) 13 const longDescription = 'my super description for server 1'.repeat(50)
@@ -28,7 +16,7 @@ describe('Test video description', function () {
28 this.timeout(40000) 16 this.timeout(40000)
29 17
30 // Run servers 18 // Run servers
31 servers = await flushAndRunMultipleServers(2) 19 servers = await createMultipleServers(2)
32 20
33 // Get the access tokens 21 // Get the access tokens
34 await setAccessTokensToServers(servers) 22 await setAccessTokensToServers(servers)
@@ -43,20 +31,19 @@ describe('Test video description', function () {
43 const attributes = { 31 const attributes = {
44 description: longDescription 32 description: longDescription
45 } 33 }
46 await uploadVideo(servers[0].url, servers[0].accessToken, attributes) 34 await servers[0].videos.upload({ attributes })
47 35
48 await waitJobs(servers) 36 await waitJobs(servers)
49 37
50 const res = await getVideosList(servers[0].url) 38 const { data } = await servers[0].videos.list()
51 39
52 videoId = res.body.data[0].id 40 videoId = data[0].id
53 videoUUID = res.body.data[0].uuid 41 videoUUID = data[0].uuid
54 }) 42 })
55 43
56 it('Should have a truncated description on each server', async function () { 44 it('Should have a truncated description on each server', async function () {
57 for (const server of servers) { 45 for (const server of servers) {
58 const res = await getVideo(server.url, videoUUID) 46 const video = await server.videos.get({ id: videoUUID })
59 const video = res.body
60 47
61 // 30 characters * 6 -> 240 characters 48 // 30 characters * 6 -> 240 characters
62 const truncatedDescription = 'my super description for server 1'.repeat(7) + 49 const truncatedDescription = 'my super description for server 1'.repeat(7) +
@@ -68,11 +55,10 @@ describe('Test video description', function () {
68 55
69 it('Should fetch long description on each server', async function () { 56 it('Should fetch long description on each server', async function () {
70 for (const server of servers) { 57 for (const server of servers) {
71 const res = await getVideo(server.url, videoUUID) 58 const video = await server.videos.get({ id: videoUUID })
72 const video = res.body
73 59
74 const res2 = await getVideoDescription(server.url, video.descriptionPath) 60 const { description } = await server.videos.getDescription({ descriptionPath: video.descriptionPath })
75 expect(res2.body.description).to.equal(longDescription) 61 expect(description).to.equal(longDescription)
76 } 62 }
77 }) 63 })
78 64
@@ -82,20 +68,19 @@ describe('Test video description', function () {
82 const attributes = { 68 const attributes = {
83 description: 'short description' 69 description: 'short description'
84 } 70 }
85 await updateVideo(servers[0].url, servers[0].accessToken, videoId, attributes) 71 await servers[0].videos.update({ id: videoId, attributes })
86 72
87 await waitJobs(servers) 73 await waitJobs(servers)
88 }) 74 })
89 75
90 it('Should have a small description on each server', async function () { 76 it('Should have a small description on each server', async function () {
91 for (const server of servers) { 77 for (const server of servers) {
92 const res = await getVideo(server.url, videoUUID) 78 const video = await server.videos.get({ id: videoUUID })
93 const video = res.body
94 79
95 expect(video.description).to.equal('short description') 80 expect(video.description).to.equal('short description')
96 81
97 const res2 = await getVideoDescription(server.url, video.descriptionPath) 82 const { description } = await server.videos.getDescription({ descriptionPath: video.descriptionPath })
98 expect(res2.body.description).to.equal('short description') 83 expect(description).to.equal('short description')
99 } 84 }
100 }) 85 })
101 86
diff --git a/server/tests/api/videos/video-hls.ts b/server/tests/api/videos/video-hls.ts
index 03ac3f321..7845f7334 100644
--- a/server/tests/api/videos/video-hls.ts
+++ b/server/tests/api/videos/video-hls.ts
@@ -9,31 +9,22 @@ import {
9 checkSegmentHash, 9 checkSegmentHash,
10 checkTmpIsEmpty, 10 checkTmpIsEmpty,
11 cleanupTests, 11 cleanupTests,
12 createMultipleServers,
12 doubleFollow, 13 doubleFollow,
13 flushAndRunMultipleServers,
14 getPlaylist,
15 getVideo,
16 makeRawRequest, 14 makeRawRequest,
17 removeVideo, 15 PeerTubeServer,
18 ServerInfo,
19 setAccessTokensToServers, 16 setAccessTokensToServers,
20 updateCustomSubConfig,
21 updateVideo,
22 uploadVideo,
23 waitJobs, 17 waitJobs,
24 webtorrentAdd 18 webtorrentAdd
25} from '../../../../shared/extra-utils' 19} from '@shared/extra-utils'
26import { VideoDetails } from '../../../../shared/models/videos' 20import { HttpStatusCode, VideoStreamingPlaylistType } from '@shared/models'
27import { VideoStreamingPlaylistType } from '../../../../shared/models/videos/video-streaming-playlist.type'
28import { DEFAULT_AUDIO_RESOLUTION } from '../../../initializers/constants' 21import { DEFAULT_AUDIO_RESOLUTION } from '../../../initializers/constants'
29import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
30 22
31const expect = chai.expect 23const expect = chai.expect
32 24
33async function checkHlsPlaylist (servers: ServerInfo[], videoUUID: string, hlsOnly: boolean, resolutions = [ 240, 360, 480, 720 ]) { 25async function checkHlsPlaylist (servers: PeerTubeServer[], videoUUID: string, hlsOnly: boolean, resolutions = [ 240, 360, 480, 720 ]) {
34 for (const server of servers) { 26 for (const server of servers) {
35 const resVideoDetails = await getVideo(server.url, videoUUID) 27 const videoDetails = await server.videos.get({ id: videoUUID })
36 const videoDetails: VideoDetails = resVideoDetails.body
37 const baseUrl = `http://${videoDetails.account.host}` 28 const baseUrl = `http://${videoDetails.account.host}`
38 29
39 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1) 30 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
@@ -68,10 +59,9 @@ async function checkHlsPlaylist (servers: ServerInfo[], videoUUID: string, hlsOn
68 } 59 }
69 60
70 { 61 {
71 await checkResolutionsInMasterPlaylist(hlsPlaylist.playlistUrl, resolutions) 62 await checkResolutionsInMasterPlaylist({ server, playlistUrl: hlsPlaylist.playlistUrl, resolutions })
72 63
73 const res = await getPlaylist(hlsPlaylist.playlistUrl) 64 const masterPlaylist = await server.streamingPlaylists.get({ url: hlsPlaylist.playlistUrl })
74 const masterPlaylist = res.text
75 65
76 for (const resolution of resolutions) { 66 for (const resolution of resolutions) {
77 expect(masterPlaylist).to.contain(`${resolution}.m3u8`) 67 expect(masterPlaylist).to.contain(`${resolution}.m3u8`)
@@ -81,9 +71,10 @@ async function checkHlsPlaylist (servers: ServerInfo[], videoUUID: string, hlsOn
81 71
82 { 72 {
83 for (const resolution of resolutions) { 73 for (const resolution of resolutions) {
84 const res = await getPlaylist(`${baseUrl}/static/streaming-playlists/hls/${videoUUID}/${resolution}.m3u8`) 74 const subPlaylist = await server.streamingPlaylists.get({
75 url: `${baseUrl}/static/streaming-playlists/hls/${videoUUID}/${resolution}.m3u8`
76 })
85 77
86 const subPlaylist = res.text
87 expect(subPlaylist).to.contain(`${videoUUID}-${resolution}-fragmented.mp4`) 78 expect(subPlaylist).to.contain(`${videoUUID}-${resolution}-fragmented.mp4`)
88 } 79 }
89 } 80 }
@@ -92,23 +83,31 @@ async function checkHlsPlaylist (servers: ServerInfo[], videoUUID: string, hlsOn
92 const baseUrlAndPath = baseUrl + '/static/streaming-playlists/hls' 83 const baseUrlAndPath = baseUrl + '/static/streaming-playlists/hls'
93 84
94 for (const resolution of resolutions) { 85 for (const resolution of resolutions) {
95 await checkSegmentHash(baseUrlAndPath, baseUrlAndPath, videoUUID, resolution, hlsPlaylist) 86 await checkSegmentHash({
87 server,
88 baseUrlPlaylist: baseUrlAndPath,
89 baseUrlSegment: baseUrlAndPath,
90 videoUUID,
91 resolution,
92 hlsPlaylist
93 })
96 } 94 }
97 } 95 }
98 } 96 }
99} 97}
100 98
101describe('Test HLS videos', function () { 99describe('Test HLS videos', function () {
102 let servers: ServerInfo[] = [] 100 let servers: PeerTubeServer[] = []
103 let videoUUID = '' 101 let videoUUID = ''
104 let videoAudioUUID = '' 102 let videoAudioUUID = ''
105 103
106 function runTestSuite (hlsOnly: boolean) { 104 function runTestSuite (hlsOnly: boolean) {
105
107 it('Should upload a video and transcode it to HLS', async function () { 106 it('Should upload a video and transcode it to HLS', async function () {
108 this.timeout(120000) 107 this.timeout(120000)
109 108
110 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video 1', fixture: 'video_short.webm' }) 109 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video 1', fixture: 'video_short.webm' } })
111 videoUUID = res.body.video.uuid 110 videoUUID = uuid
112 111
113 await waitJobs(servers) 112 await waitJobs(servers)
114 113
@@ -118,8 +117,8 @@ describe('Test HLS videos', function () {
118 it('Should upload an audio file and transcode it to HLS', async function () { 117 it('Should upload an audio file and transcode it to HLS', async function () {
119 this.timeout(120000) 118 this.timeout(120000)
120 119
121 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video audio', fixture: 'sample.ogg' }) 120 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video audio', fixture: 'sample.ogg' } })
122 videoAudioUUID = res.body.video.uuid 121 videoAudioUUID = uuid
123 122
124 await waitJobs(servers) 123 await waitJobs(servers)
125 124
@@ -129,7 +128,7 @@ describe('Test HLS videos', function () {
129 it('Should update the video', async function () { 128 it('Should update the video', async function () {
130 this.timeout(10000) 129 this.timeout(10000)
131 130
132 await updateVideo(servers[0].url, servers[0].accessToken, videoUUID, { name: 'video 1 updated' }) 131 await servers[0].videos.update({ id: videoUUID, attributes: { name: 'video 1 updated' } })
133 132
134 await waitJobs(servers) 133 await waitJobs(servers)
135 134
@@ -139,14 +138,14 @@ describe('Test HLS videos', function () {
139 it('Should delete videos', async function () { 138 it('Should delete videos', async function () {
140 this.timeout(10000) 139 this.timeout(10000)
141 140
142 await removeVideo(servers[0].url, servers[0].accessToken, videoUUID) 141 await servers[0].videos.remove({ id: videoUUID })
143 await removeVideo(servers[0].url, servers[0].accessToken, videoAudioUUID) 142 await servers[0].videos.remove({ id: videoAudioUUID })
144 143
145 await waitJobs(servers) 144 await waitJobs(servers)
146 145
147 for (const server of servers) { 146 for (const server of servers) {
148 await getVideo(server.url, videoUUID, HttpStatusCode.NOT_FOUND_404) 147 await server.videos.get({ id: videoUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
149 await getVideo(server.url, videoAudioUUID, HttpStatusCode.NOT_FOUND_404) 148 await server.videos.get({ id: videoAudioUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
150 } 149 }
151 }) 150 })
152 151
@@ -176,7 +175,7 @@ describe('Test HLS videos', function () {
176 } 175 }
177 } 176 }
178 } 177 }
179 servers = await flushAndRunMultipleServers(2, configOverride) 178 servers = await createMultipleServers(2, configOverride)
180 179
181 // Get the access tokens 180 // Get the access tokens
182 await setAccessTokensToServers(servers) 181 await setAccessTokensToServers(servers)
@@ -192,24 +191,26 @@ describe('Test HLS videos', function () {
192 describe('With only HLS enabled', function () { 191 describe('With only HLS enabled', function () {
193 192
194 before(async function () { 193 before(async function () {
195 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, { 194 await servers[0].config.updateCustomSubConfig({
196 transcoding: { 195 newConfig: {
197 enabled: true, 196 transcoding: {
198 allowAudioFiles: true, 197 enabled: true,
199 resolutions: { 198 allowAudioFiles: true,
200 '240p': true, 199 resolutions: {
201 '360p': true, 200 '240p': true,
202 '480p': true, 201 '360p': true,
203 '720p': true, 202 '480p': true,
204 '1080p': true, 203 '720p': true,
205 '1440p': true, 204 '1080p': true,
206 '2160p': true 205 '1440p': true,
207 }, 206 '2160p': true
208 hls: { 207 },
209 enabled: true 208 hls: {
210 }, 209 enabled: true
211 webtorrent: { 210 },
212 enabled: false 211 webtorrent: {
212 enabled: false
213 }
213 } 214 }
214 } 215 }
215 }) 216 })
diff --git a/server/tests/api/videos/video-imports.ts b/server/tests/api/videos/video-imports.ts
index 80834ca86..2eac130d2 100644
--- a/server/tests/api/videos/video-imports.ts
+++ b/server/tests/api/videos/video-imports.ts
@@ -3,43 +3,30 @@
3import 'mocha' 3import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5import { 5import {
6 areHttpImportTestsDisabled,
6 cleanupTests, 7 cleanupTests,
8 createMultipleServers,
7 doubleFollow, 9 doubleFollow,
8 flushAndRunMultipleServers, 10 FIXTURE_URLS,
9 getMyUserInformation, 11 PeerTubeServer,
10 getMyVideos,
11 getVideo,
12 getVideosList,
13 immutableAssign,
14 listVideoCaptions,
15 ServerInfo,
16 setAccessTokensToServers, 12 setAccessTokensToServers,
17 testCaptionFile, 13 testCaptionFile,
18 updateCustomSubConfig 14 testImage,
19} from '../../../../shared/extra-utils' 15 waitJobs
20import { areHttpImportTestsDisabled, testImage } from '../../../../shared/extra-utils/miscs/miscs' 16} from '@shared/extra-utils'
21import { waitJobs } from '../../../../shared/extra-utils/server/jobs' 17import { VideoPrivacy, VideoResolution } from '@shared/models'
22import {
23 getMagnetURI,
24 getMyVideoImports,
25 getYoutubeHDRVideoUrl,
26 getYoutubeVideoUrl,
27 importVideo
28} from '../../../../shared/extra-utils/videos/video-imports'
29import { VideoCaption, VideoDetails, VideoImport, VideoPrivacy, VideoResolution } from '../../../../shared/models/videos'
30 18
31const expect = chai.expect 19const expect = chai.expect
32 20
33describe('Test video imports', function () { 21describe('Test video imports', function () {
34 let servers: ServerInfo[] = [] 22 let servers: PeerTubeServer[] = []
35 let channelIdServer1: number 23 let channelIdServer1: number
36 let channelIdServer2: number 24 let channelIdServer2: number
37 25
38 if (areHttpImportTestsDisabled()) return 26 if (areHttpImportTestsDisabled()) return
39 27
40 async function checkVideosServer1 (url: string, idHttp: string, idMagnet: string, idTorrent: string) { 28 async function checkVideosServer1 (server: PeerTubeServer, idHttp: string, idMagnet: string, idTorrent: string) {
41 const resHttp = await getVideo(url, idHttp) 29 const videoHttp = await server.videos.get({ id: idHttp })
42 const videoHttp: VideoDetails = resHttp.body
43 30
44 expect(videoHttp.name).to.equal('small video - youtube') 31 expect(videoHttp.name).to.equal('small video - youtube')
45 // FIXME: youtube-dl seems broken 32 // FIXME: youtube-dl seems broken
@@ -56,10 +43,8 @@ describe('Test video imports', function () {
56 expect(originallyPublishedAt.getMonth()).to.equal(0) 43 expect(originallyPublishedAt.getMonth()).to.equal(0)
57 expect(originallyPublishedAt.getFullYear()).to.equal(2019) 44 expect(originallyPublishedAt.getFullYear()).to.equal(2019)
58 45
59 const resMagnet = await getVideo(url, idMagnet) 46 const videoMagnet = await server.videos.get({ id: idMagnet })
60 const videoMagnet: VideoDetails = resMagnet.body 47 const videoTorrent = await server.videos.get({ id: idTorrent })
61 const resTorrent = await getVideo(url, idTorrent)
62 const videoTorrent: VideoDetails = resTorrent.body
63 48
64 for (const video of [ videoMagnet, videoTorrent ]) { 49 for (const video of [ videoMagnet, videoTorrent ]) {
65 expect(video.category.label).to.equal('Misc') 50 expect(video.category.label).to.equal('Misc')
@@ -74,13 +59,12 @@ describe('Test video imports', function () {
74 expect(videoTorrent.name).to.contain('你好 世界 720p.mp4') 59 expect(videoTorrent.name).to.contain('你好 世界 720p.mp4')
75 expect(videoMagnet.name).to.contain('super peertube2 video') 60 expect(videoMagnet.name).to.contain('super peertube2 video')
76 61
77 const resCaptions = await listVideoCaptions(url, idHttp) 62 const bodyCaptions = await server.captions.list({ videoId: idHttp })
78 expect(resCaptions.body.total).to.equal(2) 63 expect(bodyCaptions.total).to.equal(2)
79 } 64 }
80 65
81 async function checkVideoServer2 (url: string, id: number | string) { 66 async function checkVideoServer2 (server: PeerTubeServer, id: number | string) {
82 const res = await getVideo(url, id) 67 const video = await server.videos.get({ id })
83 const video: VideoDetails = res.body
84 68
85 expect(video.name).to.equal('my super name') 69 expect(video.name).to.equal('my super name')
86 expect(video.category.label).to.equal('Entertainment') 70 expect(video.category.label).to.equal('Entertainment')
@@ -92,26 +76,26 @@ describe('Test video imports', function () {
92 76
93 expect(video.files).to.have.lengthOf(1) 77 expect(video.files).to.have.lengthOf(1)
94 78
95 const resCaptions = await listVideoCaptions(url, id) 79 const bodyCaptions = await server.captions.list({ videoId: id })
96 expect(resCaptions.body.total).to.equal(2) 80 expect(bodyCaptions.total).to.equal(2)
97 } 81 }
98 82
99 before(async function () { 83 before(async function () {
100 this.timeout(30_000) 84 this.timeout(30_000)
101 85
102 // Run servers 86 // Run servers
103 servers = await flushAndRunMultipleServers(2) 87 servers = await createMultipleServers(2)
104 88
105 await setAccessTokensToServers(servers) 89 await setAccessTokensToServers(servers)
106 90
107 { 91 {
108 const res = await getMyUserInformation(servers[0].url, servers[0].accessToken) 92 const { videoChannels } = await servers[0].users.getMyInfo()
109 channelIdServer1 = res.body.videoChannels[0].id 93 channelIdServer1 = videoChannels[0].id
110 } 94 }
111 95
112 { 96 {
113 const res = await getMyUserInformation(servers[1].url, servers[1].accessToken) 97 const { videoChannels } = await servers[1].users.getMyInfo()
114 channelIdServer2 = res.body.videoChannels[0].id 98 channelIdServer2 = videoChannels[0].id
115 } 99 }
116 100
117 await doubleFollow(servers[0], servers[1]) 101 await doubleFollow(servers[0], servers[1])
@@ -126,18 +110,18 @@ describe('Test video imports', function () {
126 } 110 }
127 111
128 { 112 {
129 const attributes = immutableAssign(baseAttributes, { targetUrl: getYoutubeVideoUrl() }) 113 const attributes = { ...baseAttributes, targetUrl: FIXTURE_URLS.youtube }
130 const res = await importVideo(servers[0].url, servers[0].accessToken, attributes) 114 const { video } = await servers[0].imports.importVideo({ attributes })
131 expect(res.body.video.name).to.equal('small video - youtube') 115 expect(video.name).to.equal('small video - youtube')
132 116
133 expect(res.body.video.thumbnailPath).to.match(new RegExp(`^/static/thumbnails/.+.jpg$`)) 117 expect(video.thumbnailPath).to.match(new RegExp(`^/static/thumbnails/.+.jpg$`))
134 expect(res.body.video.previewPath).to.match(new RegExp(`^/lazy-static/previews/.+.jpg$`)) 118 expect(video.previewPath).to.match(new RegExp(`^/lazy-static/previews/.+.jpg$`))
135 119
136 await testImage(servers[0].url, 'video_import_thumbnail', res.body.video.thumbnailPath) 120 await testImage(servers[0].url, 'video_import_thumbnail', video.thumbnailPath)
137 await testImage(servers[0].url, 'video_import_preview', res.body.video.previewPath) 121 await testImage(servers[0].url, 'video_import_preview', video.previewPath)
138 122
139 const resCaptions = await listVideoCaptions(servers[0].url, res.body.video.id) 123 const bodyCaptions = await servers[0].captions.list({ videoId: video.id })
140 const videoCaptions: VideoCaption[] = resCaptions.body.data 124 const videoCaptions = bodyCaptions.data
141 expect(videoCaptions).to.have.lengthOf(2) 125 expect(videoCaptions).to.have.lengthOf(2)
142 126
143 const enCaption = videoCaptions.find(caption => caption.language.id === 'en') 127 const enCaption = videoCaptions.find(caption => caption.language.id === 'en')
@@ -176,52 +160,52 @@ Ajouter un sous-titre est vraiment facile`)
176 } 160 }
177 161
178 { 162 {
179 const attributes = immutableAssign(baseAttributes, { 163 const attributes = {
180 magnetUri: getMagnetURI(), 164 ...baseAttributes,
165 magnetUri: FIXTURE_URLS.magnet,
181 description: 'this is a super torrent description', 166 description: 'this is a super torrent description',
182 tags: [ 'tag_torrent1', 'tag_torrent2' ] 167 tags: [ 'tag_torrent1', 'tag_torrent2' ]
183 }) 168 }
184 const res = await importVideo(servers[0].url, servers[0].accessToken, attributes) 169 const { video } = await servers[0].imports.importVideo({ attributes })
185 expect(res.body.video.name).to.equal('super peertube2 video') 170 expect(video.name).to.equal('super peertube2 video')
186 } 171 }
187 172
188 { 173 {
189 const attributes = immutableAssign(baseAttributes, { 174 const attributes = {
175 ...baseAttributes,
190 torrentfile: 'video-720p.torrent' as any, 176 torrentfile: 'video-720p.torrent' as any,
191 description: 'this is a super torrent description', 177 description: 'this is a super torrent description',
192 tags: [ 'tag_torrent1', 'tag_torrent2' ] 178 tags: [ 'tag_torrent1', 'tag_torrent2' ]
193 }) 179 }
194 const res = await importVideo(servers[0].url, servers[0].accessToken, attributes) 180 const { video } = await servers[0].imports.importVideo({ attributes })
195 expect(res.body.video.name).to.equal('你好 世界 720p.mp4') 181 expect(video.name).to.equal('你好 世界 720p.mp4')
196 } 182 }
197 }) 183 })
198 184
199 it('Should list the videos to import in my videos on server 1', async function () { 185 it('Should list the videos to import in my videos on server 1', async function () {
200 const res = await getMyVideos(servers[0].url, servers[0].accessToken, 0, 5, 'createdAt') 186 const { total, data } = await servers[0].videos.listMyVideos({ sort: 'createdAt' })
201 187
202 expect(res.body.total).to.equal(3) 188 expect(total).to.equal(3)
203 189
204 const videos = res.body.data 190 expect(data).to.have.lengthOf(3)
205 expect(videos).to.have.lengthOf(3) 191 expect(data[0].name).to.equal('small video - youtube')
206 expect(videos[0].name).to.equal('small video - youtube') 192 expect(data[1].name).to.equal('super peertube2 video')
207 expect(videos[1].name).to.equal('super peertube2 video') 193 expect(data[2].name).to.equal('你好 世界 720p.mp4')
208 expect(videos[2].name).to.equal('你好 世界 720p.mp4')
209 }) 194 })
210 195
211 it('Should list the videos to import in my imports on server 1', async function () { 196 it('Should list the videos to import in my imports on server 1', async function () {
212 const res = await getMyVideoImports(servers[0].url, servers[0].accessToken, '-createdAt') 197 const { total, data: videoImports } = await servers[0].imports.getMyVideoImports({ sort: '-createdAt' })
198 expect(total).to.equal(3)
213 199
214 expect(res.body.total).to.equal(3)
215 const videoImports: VideoImport[] = res.body.data
216 expect(videoImports).to.have.lengthOf(3) 200 expect(videoImports).to.have.lengthOf(3)
217 201
218 expect(videoImports[2].targetUrl).to.equal(getYoutubeVideoUrl()) 202 expect(videoImports[2].targetUrl).to.equal(FIXTURE_URLS.youtube)
219 expect(videoImports[2].magnetUri).to.be.null 203 expect(videoImports[2].magnetUri).to.be.null
220 expect(videoImports[2].torrentName).to.be.null 204 expect(videoImports[2].torrentName).to.be.null
221 expect(videoImports[2].video.name).to.equal('small video - youtube') 205 expect(videoImports[2].video.name).to.equal('small video - youtube')
222 206
223 expect(videoImports[1].targetUrl).to.be.null 207 expect(videoImports[1].targetUrl).to.be.null
224 expect(videoImports[1].magnetUri).to.equal(getMagnetURI()) 208 expect(videoImports[1].magnetUri).to.equal(FIXTURE_URLS.magnet)
225 expect(videoImports[1].torrentName).to.be.null 209 expect(videoImports[1].torrentName).to.be.null
226 expect(videoImports[1].video.name).to.equal('super peertube2 video') 210 expect(videoImports[1].video.name).to.equal('super peertube2 video')
227 211
@@ -237,12 +221,12 @@ Ajouter un sous-titre est vraiment facile`)
237 await waitJobs(servers) 221 await waitJobs(servers)
238 222
239 for (const server of servers) { 223 for (const server of servers) {
240 const res = await getVideosList(server.url) 224 const { total, data } = await server.videos.list()
241 expect(res.body.total).to.equal(3) 225 expect(total).to.equal(3)
242 expect(res.body.data).to.have.lengthOf(3) 226 expect(data).to.have.lengthOf(3)
243 227
244 const [ videoHttp, videoMagnet, videoTorrent ] = res.body.data 228 const [ videoHttp, videoMagnet, videoTorrent ] = data
245 await checkVideosServer1(server.url, videoHttp.uuid, videoMagnet.uuid, videoTorrent.uuid) 229 await checkVideosServer1(server, videoHttp.uuid, videoMagnet.uuid, videoTorrent.uuid)
246 } 230 }
247 }) 231 })
248 232
@@ -250,7 +234,7 @@ Ajouter un sous-titre est vraiment facile`)
250 this.timeout(60_000) 234 this.timeout(60_000)
251 235
252 const attributes = { 236 const attributes = {
253 targetUrl: getYoutubeVideoUrl(), 237 targetUrl: FIXTURE_URLS.youtube,
254 channelId: channelIdServer2, 238 channelId: channelIdServer2,
255 privacy: VideoPrivacy.PUBLIC, 239 privacy: VideoPrivacy.PUBLIC,
256 category: 10, 240 category: 10,
@@ -260,8 +244,8 @@ Ajouter un sous-titre est vraiment facile`)
260 description: 'my super description', 244 description: 'my super description',
261 tags: [ 'supertag1', 'supertag2' ] 245 tags: [ 'supertag1', 'supertag2' ]
262 } 246 }
263 const res = await importVideo(servers[1].url, servers[1].accessToken, attributes) 247 const { video } = await servers[1].imports.importVideo({ attributes })
264 expect(res.body.video.name).to.equal('my super name') 248 expect(video.name).to.equal('my super name')
265 }) 249 })
266 250
267 it('Should have the videos listed on the two instances', async function () { 251 it('Should have the videos listed on the two instances', async function () {
@@ -270,14 +254,14 @@ Ajouter un sous-titre est vraiment facile`)
270 await waitJobs(servers) 254 await waitJobs(servers)
271 255
272 for (const server of servers) { 256 for (const server of servers) {
273 const res = await getVideosList(server.url) 257 const { total, data } = await server.videos.list()
274 expect(res.body.total).to.equal(4) 258 expect(total).to.equal(4)
275 expect(res.body.data).to.have.lengthOf(4) 259 expect(data).to.have.lengthOf(4)
276 260
277 await checkVideoServer2(server.url, res.body.data[0].uuid) 261 await checkVideoServer2(server, data[0].uuid)
278 262
279 const [ , videoHttp, videoMagnet, videoTorrent ] = res.body.data 263 const [ , videoHttp, videoMagnet, videoTorrent ] = data
280 await checkVideosServer1(server.url, videoHttp.uuid, videoMagnet.uuid, videoTorrent.uuid) 264 await checkVideosServer1(server, videoHttp.uuid, videoMagnet.uuid, videoTorrent.uuid)
281 } 265 }
282 }) 266 })
283 267
@@ -286,18 +270,17 @@ Ajouter un sous-titre est vraiment facile`)
286 270
287 const attributes = { 271 const attributes = {
288 name: 'transcoded video', 272 name: 'transcoded video',
289 magnetUri: getMagnetURI(), 273 magnetUri: FIXTURE_URLS.magnet,
290 channelId: channelIdServer2, 274 channelId: channelIdServer2,
291 privacy: VideoPrivacy.PUBLIC 275 privacy: VideoPrivacy.PUBLIC
292 } 276 }
293 const res = await importVideo(servers[1].url, servers[1].accessToken, attributes) 277 const { video } = await servers[1].imports.importVideo({ attributes })
294 const videoUUID = res.body.video.uuid 278 const videoUUID = video.uuid
295 279
296 await waitJobs(servers) 280 await waitJobs(servers)
297 281
298 for (const server of servers) { 282 for (const server of servers) {
299 const res = await getVideo(server.url, videoUUID) 283 const video = await server.videos.get({ id: videoUUID })
300 const video: VideoDetails = res.body
301 284
302 expect(video.name).to.equal('transcoded video') 285 expect(video.name).to.equal('transcoded video')
303 expect(video.files).to.have.lengthOf(4) 286 expect(video.files).to.have.lengthOf(4)
@@ -333,22 +316,21 @@ Ajouter un sous-titre est vraiment facile`)
333 } 316 }
334 } 317 }
335 } 318 }
336 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, config) 319 await servers[0].config.updateCustomSubConfig({ newConfig: config })
337 320
338 const attributes = { 321 const attributes = {
339 name: 'hdr video', 322 name: 'hdr video',
340 targetUrl: getYoutubeHDRVideoUrl(), 323 targetUrl: FIXTURE_URLS.youtubeHDR,
341 channelId: channelIdServer1, 324 channelId: channelIdServer1,
342 privacy: VideoPrivacy.PUBLIC 325 privacy: VideoPrivacy.PUBLIC
343 } 326 }
344 const res1 = await importVideo(servers[0].url, servers[0].accessToken, attributes) 327 const { video: videoImported } = await servers[0].imports.importVideo({ attributes })
345 const videoUUID = res1.body.video.uuid 328 const videoUUID = videoImported.uuid
346 329
347 await waitJobs(servers) 330 await waitJobs(servers)
348 331
349 // test resolution 332 // test resolution
350 const res2 = await getVideo(servers[0].url, videoUUID) 333 const video = await servers[0].videos.get({ id: videoUUID })
351 const video: VideoDetails = res2.body
352 expect(video.name).to.equal('hdr video') 334 expect(video.name).to.equal('hdr video')
353 const maxResolution = Math.max.apply(Math, video.files.map(function (o) { return o.resolution.id })) 335 const maxResolution = Math.max.apply(Math, video.files.map(function (o) { return o.resolution.id }))
354 expect(maxResolution, 'expected max resolution not met').to.equals(VideoResolution.H_1080P) 336 expect(maxResolution, 'expected max resolution not met').to.equals(VideoResolution.H_1080P)
diff --git a/server/tests/api/videos/video-nsfw.ts b/server/tests/api/videos/video-nsfw.ts
index b16b484b9..b5d183d62 100644
--- a/server/tests/api/videos/video-nsfw.ts
+++ b/server/tests/api/videos/video-nsfw.ts
@@ -1,117 +1,96 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2 2
3import * as chai from 'chai'
4import 'mocha' 3import 'mocha'
5import { cleanupTests, getVideosList, ServerInfo, setAccessTokensToServers, uploadVideo } from '../../../../shared/extra-utils/index' 4import * as chai from 'chai'
6import { userLogin } from '../../../../shared/extra-utils/users/login' 5import { cleanupTests, createSingleServer, PeerTubeServer, setAccessTokensToServers } from '@shared/extra-utils'
7import { createUser } from '../../../../shared/extra-utils/users/users' 6import { BooleanBothQuery, CustomConfig, ResultList, Video, VideosOverview } from '@shared/models'
8import { getMyVideos } from '../../../../shared/extra-utils/videos/videos'
9import {
10 flushAndRunServer,
11 getAccountVideos,
12 getConfig,
13 getCustomConfig,
14 getMyUserInformation,
15 getVideoChannelVideos,
16 getVideosListWithToken,
17 searchVideo,
18 searchVideoWithToken,
19 updateCustomConfig,
20 updateMyUser
21} from '../../../../shared/extra-utils'
22import { ServerConfig, VideosOverview } from '../../../../shared/models'
23import { CustomConfig } from '../../../../shared/models/server/custom-config.model'
24import { User } from '../../../../shared/models/users'
25import { getVideosOverview, getVideosOverviewWithToken } from '@shared/extra-utils/overviews/overviews'
26 7
27const expect = chai.expect 8const expect = chai.expect
28 9
29function createOverviewRes (res: any) { 10function createOverviewRes (overview: VideosOverview) {
30 const overview = res.body as VideosOverview
31
32 const videos = overview.categories[0].videos 11 const videos = overview.categories[0].videos
33 return { body: { data: videos, total: videos.length } } 12 return { data: videos, total: videos.length }
34} 13}
35 14
36describe('Test video NSFW policy', function () { 15describe('Test video NSFW policy', function () {
37 let server: ServerInfo 16 let server: PeerTubeServer
38 let userAccessToken: string 17 let userAccessToken: string
39 let customConfig: CustomConfig 18 let customConfig: CustomConfig
40 19
41 function getVideosFunctions (token?: string, query = {}) { 20 async function getVideosFunctions (token?: string, query: { nsfw?: BooleanBothQuery } = {}) {
42 return getMyUserInformation(server.url, server.accessToken) 21 const user = await server.users.getMyInfo()
43 .then(res => { 22
44 const user: User = res.body 23 const channelName = user.videoChannels[0].name
45 const videoChannelName = user.videoChannels[0].name 24 const accountName = user.account.name + '@' + user.account.host
46 const accountName = user.account.name + '@' + user.account.host 25
47 const hasQuery = Object.keys(query).length !== 0 26 const hasQuery = Object.keys(query).length !== 0
48 let promises: Promise<any>[] 27 let promises: Promise<ResultList<Video>>[]
49 28
50 if (token) { 29 if (token) {
51 promises = [ 30 promises = [
52 getVideosListWithToken(server.url, token, query), 31 server.search.advancedVideoSearch({ token, search: { search: 'n', sort: '-publishedAt', ...query } }),
53 searchVideoWithToken(server.url, 'n', token, query), 32 server.videos.listWithToken({ token, ...query }),
54 getAccountVideos(server.url, token, accountName, 0, 5, undefined, query), 33 server.videos.listByAccount({ token, handle: accountName, ...query }),
55 getVideoChannelVideos(server.url, token, videoChannelName, 0, 5, undefined, query) 34 server.videos.listByChannel({ token, handle: channelName, ...query })
56 ] 35 ]
57 36
58 // Overviews do not support video filters 37 // Overviews do not support video filters
59 if (!hasQuery) { 38 if (!hasQuery) {
60 promises.push(getVideosOverviewWithToken(server.url, 1, token).then(res => createOverviewRes(res))) 39 const p = server.overviews.getVideos({ page: 1, token })
61 } 40 .then(res => createOverviewRes(res))
62 41 promises.push(p)
63 return Promise.all(promises) 42 }
64 } 43
65 44 return Promise.all(promises)
66 promises = [ 45 }
67 getVideosList(server.url), 46
68 searchVideo(server.url, 'n'), 47 promises = [
69 getAccountVideos(server.url, undefined, accountName, 0, 5), 48 server.search.searchVideos({ search: 'n', sort: '-publishedAt' }),
70 getVideoChannelVideos(server.url, undefined, videoChannelName, 0, 5) 49 server.videos.list(),
71 ] 50 server.videos.listByAccount({ token: null, handle: accountName }),
72 51 server.videos.listByChannel({ token: null, handle: channelName })
73 // Overviews do not support video filters 52 ]
74 if (!hasQuery) { 53
75 promises.push(getVideosOverview(server.url, 1).then(res => createOverviewRes(res))) 54 // Overviews do not support video filters
76 } 55 if (!hasQuery) {
77 56 const p = server.overviews.getVideos({ page: 1 })
78 return Promise.all(promises) 57 .then(res => createOverviewRes(res))
79 }) 58 promises.push(p)
59 }
60
61 return Promise.all(promises)
80 } 62 }
81 63
82 before(async function () { 64 before(async function () {
83 this.timeout(50000) 65 this.timeout(50000)
84 server = await flushAndRunServer(1) 66 server = await createSingleServer(1)
85 67
86 // Get the access tokens 68 // Get the access tokens
87 await setAccessTokensToServers([ server ]) 69 await setAccessTokensToServers([ server ])
88 70
89 { 71 {
90 const attributes = { name: 'nsfw', nsfw: true, category: 1 } 72 const attributes = { name: 'nsfw', nsfw: true, category: 1 }
91 await uploadVideo(server.url, server.accessToken, attributes) 73 await server.videos.upload({ attributes })
92 } 74 }
93 75
94 { 76 {
95 const attributes = { name: 'normal', nsfw: false, category: 1 } 77 const attributes = { name: 'normal', nsfw: false, category: 1 }
96 await uploadVideo(server.url, server.accessToken, attributes) 78 await server.videos.upload({ attributes })
97 } 79 }
98 80
99 { 81 customConfig = await server.config.getCustomConfig()
100 const res = await getCustomConfig(server.url, server.accessToken)
101 customConfig = res.body
102 }
103 }) 82 })
104 83
105 describe('Instance default NSFW policy', function () { 84 describe('Instance default NSFW policy', function () {
85
106 it('Should display NSFW videos with display default NSFW policy', async function () { 86 it('Should display NSFW videos with display default NSFW policy', async function () {
107 const resConfig = await getConfig(server.url) 87 const serverConfig = await server.config.getConfig()
108 const serverConfig: ServerConfig = resConfig.body
109 expect(serverConfig.instance.defaultNSFWPolicy).to.equal('display') 88 expect(serverConfig.instance.defaultNSFWPolicy).to.equal('display')
110 89
111 for (const res of await getVideosFunctions()) { 90 for (const body of await getVideosFunctions()) {
112 expect(res.body.total).to.equal(2) 91 expect(body.total).to.equal(2)
113 92
114 const videos = res.body.data 93 const videos = body.data
115 expect(videos).to.have.lengthOf(2) 94 expect(videos).to.have.lengthOf(2)
116 expect(videos[0].name).to.equal('normal') 95 expect(videos[0].name).to.equal('normal')
117 expect(videos[1].name).to.equal('nsfw') 96 expect(videos[1].name).to.equal('nsfw')
@@ -120,16 +99,15 @@ describe('Test video NSFW policy', function () {
120 99
121 it('Should not display NSFW videos with do_not_list default NSFW policy', async function () { 100 it('Should not display NSFW videos with do_not_list default NSFW policy', async function () {
122 customConfig.instance.defaultNSFWPolicy = 'do_not_list' 101 customConfig.instance.defaultNSFWPolicy = 'do_not_list'
123 await updateCustomConfig(server.url, server.accessToken, customConfig) 102 await server.config.updateCustomConfig({ newCustomConfig: customConfig })
124 103
125 const resConfig = await getConfig(server.url) 104 const serverConfig = await server.config.getConfig()
126 const serverConfig: ServerConfig = resConfig.body
127 expect(serverConfig.instance.defaultNSFWPolicy).to.equal('do_not_list') 105 expect(serverConfig.instance.defaultNSFWPolicy).to.equal('do_not_list')
128 106
129 for (const res of await getVideosFunctions()) { 107 for (const body of await getVideosFunctions()) {
130 expect(res.body.total).to.equal(1) 108 expect(body.total).to.equal(1)
131 109
132 const videos = res.body.data 110 const videos = body.data
133 expect(videos).to.have.lengthOf(1) 111 expect(videos).to.have.lengthOf(1)
134 expect(videos[0].name).to.equal('normal') 112 expect(videos[0].name).to.equal('normal')
135 } 113 }
@@ -137,16 +115,15 @@ describe('Test video NSFW policy', function () {
137 115
138 it('Should display NSFW videos with blur default NSFW policy', async function () { 116 it('Should display NSFW videos with blur default NSFW policy', async function () {
139 customConfig.instance.defaultNSFWPolicy = 'blur' 117 customConfig.instance.defaultNSFWPolicy = 'blur'
140 await updateCustomConfig(server.url, server.accessToken, customConfig) 118 await server.config.updateCustomConfig({ newCustomConfig: customConfig })
141 119
142 const resConfig = await getConfig(server.url) 120 const serverConfig = await server.config.getConfig()
143 const serverConfig: ServerConfig = resConfig.body
144 expect(serverConfig.instance.defaultNSFWPolicy).to.equal('blur') 121 expect(serverConfig.instance.defaultNSFWPolicy).to.equal('blur')
145 122
146 for (const res of await getVideosFunctions()) { 123 for (const body of await getVideosFunctions()) {
147 expect(res.body.total).to.equal(2) 124 expect(body.total).to.equal(2)
148 125
149 const videos = res.body.data 126 const videos = body.data
150 expect(videos).to.have.lengthOf(2) 127 expect(videos).to.have.lengthOf(2)
151 expect(videos[0].name).to.equal('normal') 128 expect(videos[0].name).to.equal('normal')
152 expect(videos[1].name).to.equal('nsfw') 129 expect(videos[1].name).to.equal('nsfw')
@@ -159,24 +136,22 @@ describe('Test video NSFW policy', function () {
159 it('Should create a user having the default nsfw policy', async function () { 136 it('Should create a user having the default nsfw policy', async function () {
160 const username = 'user1' 137 const username = 'user1'
161 const password = 'my super password' 138 const password = 'my super password'
162 await createUser({ url: server.url, accessToken: server.accessToken, username: username, password: password }) 139 await server.users.create({ username: username, password: password })
163
164 userAccessToken = await userLogin(server, { username, password })
165 140
166 const res = await getMyUserInformation(server.url, userAccessToken) 141 userAccessToken = await server.login.getAccessToken({ username, password })
167 const user = res.body
168 142
143 const user = await server.users.getMyInfo({ token: userAccessToken })
169 expect(user.nsfwPolicy).to.equal('blur') 144 expect(user.nsfwPolicy).to.equal('blur')
170 }) 145 })
171 146
172 it('Should display NSFW videos with blur user NSFW policy', async function () { 147 it('Should display NSFW videos with blur user NSFW policy', async function () {
173 customConfig.instance.defaultNSFWPolicy = 'do_not_list' 148 customConfig.instance.defaultNSFWPolicy = 'do_not_list'
174 await updateCustomConfig(server.url, server.accessToken, customConfig) 149 await server.config.updateCustomConfig({ newCustomConfig: customConfig })
175 150
176 for (const res of await getVideosFunctions(userAccessToken)) { 151 for (const body of await getVideosFunctions(userAccessToken)) {
177 expect(res.body.total).to.equal(2) 152 expect(body.total).to.equal(2)
178 153
179 const videos = res.body.data 154 const videos = body.data
180 expect(videos).to.have.lengthOf(2) 155 expect(videos).to.have.lengthOf(2)
181 expect(videos[0].name).to.equal('normal') 156 expect(videos[0].name).to.equal('normal')
182 expect(videos[1].name).to.equal('nsfw') 157 expect(videos[1].name).to.equal('nsfw')
@@ -184,16 +159,12 @@ describe('Test video NSFW policy', function () {
184 }) 159 })
185 160
186 it('Should display NSFW videos with display user NSFW policy', async function () { 161 it('Should display NSFW videos with display user NSFW policy', async function () {
187 await updateMyUser({ 162 await server.users.updateMe({ nsfwPolicy: 'display' })
188 url: server.url,
189 accessToken: server.accessToken,
190 nsfwPolicy: 'display'
191 })
192 163
193 for (const res of await getVideosFunctions(server.accessToken)) { 164 for (const body of await getVideosFunctions(server.accessToken)) {
194 expect(res.body.total).to.equal(2) 165 expect(body.total).to.equal(2)
195 166
196 const videos = res.body.data 167 const videos = body.data
197 expect(videos).to.have.lengthOf(2) 168 expect(videos).to.have.lengthOf(2)
198 expect(videos[0].name).to.equal('normal') 169 expect(videos[0].name).to.equal('normal')
199 expect(videos[1].name).to.equal('nsfw') 170 expect(videos[1].name).to.equal('nsfw')
@@ -201,56 +172,51 @@ describe('Test video NSFW policy', function () {
201 }) 172 })
202 173
203 it('Should not display NSFW videos with do_not_list user NSFW policy', async function () { 174 it('Should not display NSFW videos with do_not_list user NSFW policy', async function () {
204 await updateMyUser({ 175 await server.users.updateMe({ nsfwPolicy: 'do_not_list' })
205 url: server.url,
206 accessToken: server.accessToken,
207 nsfwPolicy: 'do_not_list'
208 })
209 176
210 for (const res of await getVideosFunctions(server.accessToken)) { 177 for (const body of await getVideosFunctions(server.accessToken)) {
211 expect(res.body.total).to.equal(1) 178 expect(body.total).to.equal(1)
212 179
213 const videos = res.body.data 180 const videos = body.data
214 expect(videos).to.have.lengthOf(1) 181 expect(videos).to.have.lengthOf(1)
215 expect(videos[0].name).to.equal('normal') 182 expect(videos[0].name).to.equal('normal')
216 } 183 }
217 }) 184 })
218 185
219 it('Should be able to see my NSFW videos even with do_not_list user NSFW policy', async function () { 186 it('Should be able to see my NSFW videos even with do_not_list user NSFW policy', async function () {
220 const res = await getMyVideos(server.url, server.accessToken, 0, 5) 187 const { total, data } = await server.videos.listMyVideos()
221 expect(res.body.total).to.equal(2) 188 expect(total).to.equal(2)
222 189
223 const videos = res.body.data 190 expect(data).to.have.lengthOf(2)
224 expect(videos).to.have.lengthOf(2) 191 expect(data[0].name).to.equal('normal')
225 expect(videos[0].name).to.equal('normal') 192 expect(data[1].name).to.equal('nsfw')
226 expect(videos[1].name).to.equal('nsfw')
227 }) 193 })
228 194
229 it('Should display NSFW videos when the nsfw param === true', async function () { 195 it('Should display NSFW videos when the nsfw param === true', async function () {
230 for (const res of await getVideosFunctions(server.accessToken, { nsfw: true })) { 196 for (const body of await getVideosFunctions(server.accessToken, { nsfw: 'true' })) {
231 expect(res.body.total).to.equal(1) 197 expect(body.total).to.equal(1)
232 198
233 const videos = res.body.data 199 const videos = body.data
234 expect(videos).to.have.lengthOf(1) 200 expect(videos).to.have.lengthOf(1)
235 expect(videos[0].name).to.equal('nsfw') 201 expect(videos[0].name).to.equal('nsfw')
236 } 202 }
237 }) 203 })
238 204
239 it('Should hide NSFW videos when the nsfw param === true', async function () { 205 it('Should hide NSFW videos when the nsfw param === true', async function () {
240 for (const res of await getVideosFunctions(server.accessToken, { nsfw: false })) { 206 for (const body of await getVideosFunctions(server.accessToken, { nsfw: 'false' })) {
241 expect(res.body.total).to.equal(1) 207 expect(body.total).to.equal(1)
242 208
243 const videos = res.body.data 209 const videos = body.data
244 expect(videos).to.have.lengthOf(1) 210 expect(videos).to.have.lengthOf(1)
245 expect(videos[0].name).to.equal('normal') 211 expect(videos[0].name).to.equal('normal')
246 } 212 }
247 }) 213 })
248 214
249 it('Should display both videos when the nsfw param === both', async function () { 215 it('Should display both videos when the nsfw param === both', async function () {
250 for (const res of await getVideosFunctions(server.accessToken, { nsfw: 'both' })) { 216 for (const body of await getVideosFunctions(server.accessToken, { nsfw: 'both' })) {
251 expect(res.body.total).to.equal(2) 217 expect(body.total).to.equal(2)
252 218
253 const videos = res.body.data 219 const videos = body.data
254 expect(videos).to.have.lengthOf(2) 220 expect(videos).to.have.lengthOf(2)
255 expect(videos[0].name).to.equal('normal') 221 expect(videos[0].name).to.equal('normal')
256 expect(videos[1].name).to.equal('nsfw') 222 expect(videos[1].name).to.equal('nsfw')
diff --git a/server/tests/api/videos/video-playlist-thumbnails.ts b/server/tests/api/videos/video-playlist-thumbnails.ts
index a93a0b7de..f0b2ca169 100644
--- a/server/tests/api/videos/video-playlist-thumbnails.ts
+++ b/server/tests/api/videos/video-playlist-thumbnails.ts
@@ -1,21 +1,15 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2 2
3import * as chai from 'chai'
4import 'mocha' 3import 'mocha'
4import * as chai from 'chai'
5import { 5import {
6 addVideoInPlaylist,
7 cleanupTests, 6 cleanupTests,
8 createVideoPlaylist, 7 createMultipleServers,
9 doubleFollow, 8 doubleFollow,
10 flushAndRunMultipleServers, 9 PeerTubeServer,
11 getVideoPlaylistsList,
12 removeVideoFromPlaylist,
13 reorderVideosPlaylist,
14 ServerInfo,
15 setAccessTokensToServers, 10 setAccessTokensToServers,
16 setDefaultVideoChannel, 11 setDefaultVideoChannel,
17 testImage, 12 testImage,
18 uploadVideoAndGetId,
19 waitJobs 13 waitJobs
20} from '../../../../shared/extra-utils' 14} from '../../../../shared/extra-utils'
21import { VideoPlaylistPrivacy } from '../../../../shared/models/videos/playlist/video-playlist-privacy.model' 15import { VideoPlaylistPrivacy } from '../../../../shared/models/videos/playlist/video-playlist-privacy.model'
@@ -23,10 +17,10 @@ import { VideoPlaylistPrivacy } from '../../../../shared/models/videos/playlist/
23const expect = chai.expect 17const expect = chai.expect
24 18
25describe('Playlist thumbnail', function () { 19describe('Playlist thumbnail', function () {
26 let servers: ServerInfo[] = [] 20 let servers: PeerTubeServer[] = []
27 21
28 let playlistWithoutThumbnail: number 22 let playlistWithoutThumbnailId: number
29 let playlistWithThumbnail: number 23 let playlistWithThumbnailId: number
30 24
31 let withThumbnailE1: number 25 let withThumbnailE1: number
32 let withThumbnailE2: number 26 let withThumbnailE2: number
@@ -36,22 +30,22 @@ describe('Playlist thumbnail', function () {
36 let video1: number 30 let video1: number
37 let video2: number 31 let video2: number
38 32
39 async function getPlaylistWithoutThumbnail (server: ServerInfo) { 33 async function getPlaylistWithoutThumbnail (server: PeerTubeServer) {
40 const res = await getVideoPlaylistsList(server.url, 0, 10) 34 const body = await server.playlists.list({ start: 0, count: 10 })
41 35
42 return res.body.data.find(p => p.displayName === 'playlist without thumbnail') 36 return body.data.find(p => p.displayName === 'playlist without thumbnail')
43 } 37 }
44 38
45 async function getPlaylistWithThumbnail (server: ServerInfo) { 39 async function getPlaylistWithThumbnail (server: PeerTubeServer) {
46 const res = await getVideoPlaylistsList(server.url, 0, 10) 40 const body = await server.playlists.list({ start: 0, count: 10 })
47 41
48 return res.body.data.find(p => p.displayName === 'playlist with thumbnail') 42 return body.data.find(p => p.displayName === 'playlist with thumbnail')
49 } 43 }
50 44
51 before(async function () { 45 before(async function () {
52 this.timeout(120000) 46 this.timeout(120000)
53 47
54 servers = await flushAndRunMultipleServers(2, { transcoding: { enabled: false } }) 48 servers = await createMultipleServers(2, { transcoding: { enabled: false } })
55 49
56 // Get the access tokens 50 // Get the access tokens
57 await setAccessTokensToServers(servers) 51 await setAccessTokensToServers(servers)
@@ -60,8 +54,8 @@ describe('Playlist thumbnail', function () {
60 // Server 1 and server 2 follow each other 54 // Server 1 and server 2 follow each other
61 await doubleFollow(servers[0], servers[1]) 55 await doubleFollow(servers[0], servers[1])
62 56
63 video1 = (await uploadVideoAndGetId({ server: servers[0], videoName: 'video 1' })).id 57 video1 = (await servers[0].videos.quickUpload({ name: 'video 1' })).id
64 video2 = (await uploadVideoAndGetId({ server: servers[0], videoName: 'video 2' })).id 58 video2 = (await servers[0].videos.quickUpload({ name: 'video 2' })).id
65 59
66 await waitJobs(servers) 60 await waitJobs(servers)
67 }) 61 })
@@ -69,24 +63,20 @@ describe('Playlist thumbnail', function () {
69 it('Should automatically update the thumbnail when adding an element', async function () { 63 it('Should automatically update the thumbnail when adding an element', async function () {
70 this.timeout(30000) 64 this.timeout(30000)
71 65
72 const res = await createVideoPlaylist({ 66 const created = await servers[1].playlists.create({
73 url: servers[1].url, 67 attributes: {
74 token: servers[1].accessToken,
75 playlistAttrs: {
76 displayName: 'playlist without thumbnail', 68 displayName: 'playlist without thumbnail',
77 privacy: VideoPlaylistPrivacy.PUBLIC, 69 privacy: VideoPlaylistPrivacy.PUBLIC,
78 videoChannelId: servers[1].videoChannel.id 70 videoChannelId: servers[1].store.channel.id
79 } 71 }
80 }) 72 })
81 playlistWithoutThumbnail = res.body.videoPlaylist.id 73 playlistWithoutThumbnailId = created.id
82 74
83 const res2 = await addVideoInPlaylist({ 75 const added = await servers[1].playlists.addElement({
84 url: servers[1].url, 76 playlistId: playlistWithoutThumbnailId,
85 token: servers[1].accessToken, 77 attributes: { videoId: video1 }
86 playlistId: playlistWithoutThumbnail,
87 elementAttrs: { videoId: video1 }
88 }) 78 })
89 withoutThumbnailE1 = res2.body.videoPlaylistElement.id 79 withoutThumbnailE1 = added.id
90 80
91 await waitJobs(servers) 81 await waitJobs(servers)
92 82
@@ -99,25 +89,21 @@ describe('Playlist thumbnail', function () {
99 it('Should not update the thumbnail if we explicitly uploaded a thumbnail', async function () { 89 it('Should not update the thumbnail if we explicitly uploaded a thumbnail', async function () {
100 this.timeout(30000) 90 this.timeout(30000)
101 91
102 const res = await createVideoPlaylist({ 92 const created = await servers[1].playlists.create({
103 url: servers[1].url, 93 attributes: {
104 token: servers[1].accessToken,
105 playlistAttrs: {
106 displayName: 'playlist with thumbnail', 94 displayName: 'playlist with thumbnail',
107 privacy: VideoPlaylistPrivacy.PUBLIC, 95 privacy: VideoPlaylistPrivacy.PUBLIC,
108 videoChannelId: servers[1].videoChannel.id, 96 videoChannelId: servers[1].store.channel.id,
109 thumbnailfile: 'thumbnail.jpg' 97 thumbnailfile: 'thumbnail.jpg'
110 } 98 }
111 }) 99 })
112 playlistWithThumbnail = res.body.videoPlaylist.id 100 playlistWithThumbnailId = created.id
113 101
114 const res2 = await addVideoInPlaylist({ 102 const added = await servers[1].playlists.addElement({
115 url: servers[1].url, 103 playlistId: playlistWithThumbnailId,
116 token: servers[1].accessToken, 104 attributes: { videoId: video1 }
117 playlistId: playlistWithThumbnail,
118 elementAttrs: { videoId: video1 }
119 }) 105 })
120 withThumbnailE1 = res2.body.videoPlaylistElement.id 106 withThumbnailE1 = added.id
121 107
122 await waitJobs(servers) 108 await waitJobs(servers)
123 109
@@ -130,19 +116,15 @@ describe('Playlist thumbnail', function () {
130 it('Should automatically update the thumbnail when moving the first element', async function () { 116 it('Should automatically update the thumbnail when moving the first element', async function () {
131 this.timeout(30000) 117 this.timeout(30000)
132 118
133 const res = await addVideoInPlaylist({ 119 const added = await servers[1].playlists.addElement({
134 url: servers[1].url, 120 playlistId: playlistWithoutThumbnailId,
135 token: servers[1].accessToken, 121 attributes: { videoId: video2 }
136 playlistId: playlistWithoutThumbnail,
137 elementAttrs: { videoId: video2 }
138 }) 122 })
139 withoutThumbnailE2 = res.body.videoPlaylistElement.id 123 withoutThumbnailE2 = added.id
140 124
141 await reorderVideosPlaylist({ 125 await servers[1].playlists.reorderElements({
142 url: servers[1].url, 126 playlistId: playlistWithoutThumbnailId,
143 token: servers[1].accessToken, 127 attributes: {
144 playlistId: playlistWithoutThumbnail,
145 elementAttrs: {
146 startPosition: 1, 128 startPosition: 1,
147 insertAfterPosition: 2 129 insertAfterPosition: 2
148 } 130 }
@@ -159,19 +141,15 @@ describe('Playlist thumbnail', function () {
159 it('Should not update the thumbnail when moving the first element if we explicitly uploaded a thumbnail', async function () { 141 it('Should not update the thumbnail when moving the first element if we explicitly uploaded a thumbnail', async function () {
160 this.timeout(30000) 142 this.timeout(30000)
161 143
162 const res = await addVideoInPlaylist({ 144 const added = await servers[1].playlists.addElement({
163 url: servers[1].url, 145 playlistId: playlistWithThumbnailId,
164 token: servers[1].accessToken, 146 attributes: { videoId: video2 }
165 playlistId: playlistWithThumbnail,
166 elementAttrs: { videoId: video2 }
167 }) 147 })
168 withThumbnailE2 = res.body.videoPlaylistElement.id 148 withThumbnailE2 = added.id
169 149
170 await reorderVideosPlaylist({ 150 await servers[1].playlists.reorderElements({
171 url: servers[1].url, 151 playlistId: playlistWithThumbnailId,
172 token: servers[1].accessToken, 152 attributes: {
173 playlistId: playlistWithThumbnail,
174 elementAttrs: {
175 startPosition: 1, 153 startPosition: 1,
176 insertAfterPosition: 2 154 insertAfterPosition: 2
177 } 155 }
@@ -188,11 +166,9 @@ describe('Playlist thumbnail', function () {
188 it('Should automatically update the thumbnail when deleting the first element', async function () { 166 it('Should automatically update the thumbnail when deleting the first element', async function () {
189 this.timeout(30000) 167 this.timeout(30000)
190 168
191 await removeVideoFromPlaylist({ 169 await servers[1].playlists.removeElement({
192 url: servers[1].url, 170 playlistId: playlistWithoutThumbnailId,
193 token: servers[1].accessToken, 171 elementId: withoutThumbnailE1
194 playlistId: playlistWithoutThumbnail,
195 playlistElementId: withoutThumbnailE1
196 }) 172 })
197 173
198 await waitJobs(servers) 174 await waitJobs(servers)
@@ -206,11 +182,9 @@ describe('Playlist thumbnail', function () {
206 it('Should not update the thumbnail when deleting the first element if we explicitly uploaded a thumbnail', async function () { 182 it('Should not update the thumbnail when deleting the first element if we explicitly uploaded a thumbnail', async function () {
207 this.timeout(30000) 183 this.timeout(30000)
208 184
209 await removeVideoFromPlaylist({ 185 await servers[1].playlists.removeElement({
210 url: servers[1].url, 186 playlistId: playlistWithThumbnailId,
211 token: servers[1].accessToken, 187 elementId: withThumbnailE1
212 playlistId: playlistWithThumbnail,
213 playlistElementId: withThumbnailE1
214 }) 188 })
215 189
216 await waitJobs(servers) 190 await waitJobs(servers)
@@ -224,11 +198,9 @@ describe('Playlist thumbnail', function () {
224 it('Should the thumbnail when we delete the last element', async function () { 198 it('Should the thumbnail when we delete the last element', async function () {
225 this.timeout(30000) 199 this.timeout(30000)
226 200
227 await removeVideoFromPlaylist({ 201 await servers[1].playlists.removeElement({
228 url: servers[1].url, 202 playlistId: playlistWithoutThumbnailId,
229 token: servers[1].accessToken, 203 elementId: withoutThumbnailE2
230 playlistId: playlistWithoutThumbnail,
231 playlistElementId: withoutThumbnailE2
232 }) 204 })
233 205
234 await waitJobs(servers) 206 await waitJobs(servers)
@@ -242,11 +214,9 @@ describe('Playlist thumbnail', function () {
242 it('Should not update the thumbnail when we delete the last element if we explicitly uploaded a thumbnail', async function () { 214 it('Should not update the thumbnail when we delete the last element if we explicitly uploaded a thumbnail', async function () {
243 this.timeout(30000) 215 this.timeout(30000)
244 216
245 await removeVideoFromPlaylist({ 217 await servers[1].playlists.removeElement({
246 url: servers[1].url, 218 playlistId: playlistWithThumbnailId,
247 token: servers[1].accessToken, 219 elementId: withThumbnailE2
248 playlistId: playlistWithThumbnail,
249 playlistElementId: withThumbnailE2
250 }) 220 })
251 221
252 await waitJobs(servers) 222 await waitJobs(servers)
diff --git a/server/tests/api/videos/video-playlists.ts b/server/tests/api/videos/video-playlists.ts
index da8de054b..f42aee2ff 100644
--- a/server/tests/api/videos/video-playlists.ts
+++ b/server/tests/api/videos/video-playlists.ts
@@ -2,71 +2,33 @@
2 2
3import 'mocha' 3import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
6import { 5import {
7 addVideoChannel,
8 addVideoInPlaylist,
9 addVideoToBlacklist,
10 checkPlaylistFilesWereRemoved, 6 checkPlaylistFilesWereRemoved,
11 cleanupTests, 7 cleanupTests,
12 createUser, 8 createMultipleServers,
13 createVideoPlaylist,
14 deleteVideoChannel,
15 deleteVideoPlaylist,
16 doubleFollow, 9 doubleFollow,
17 doVideosExistInMyPlaylist, 10 PeerTubeServer,
18 flushAndRunMultipleServers, 11 PlaylistsCommand,
19 generateUserAccessToken,
20 getAccessToken,
21 getAccountPlaylistsList,
22 getAccountPlaylistsListWithToken,
23 getMyUserInformation,
24 getPlaylistVideos,
25 getVideoChannelPlaylistsList,
26 getVideoPlaylist,
27 getVideoPlaylistPrivacies,
28 getVideoPlaylistsList,
29 getVideoPlaylistWithToken,
30 removeUser,
31 removeVideoFromBlacklist,
32 removeVideoFromPlaylist,
33 reorderVideosPlaylist,
34 ServerInfo,
35 setAccessTokensToServers, 12 setAccessTokensToServers,
36 setDefaultVideoChannel, 13 setDefaultVideoChannel,
37 testImage, 14 testImage,
38 unfollow,
39 updateVideo,
40 updateVideoPlaylist,
41 updateVideoPlaylistElement,
42 uploadVideo,
43 uploadVideoAndGetId,
44 userLogin,
45 wait, 15 wait,
46 waitJobs 16 waitJobs
47} from '../../../../shared/extra-utils' 17} from '@shared/extra-utils'
48import { 18import {
49 addAccountToAccountBlocklist, 19 HttpStatusCode,
50 addAccountToServerBlocklist, 20 VideoPlaylist,
51 addServerToAccountBlocklist, 21 VideoPlaylistCreateResult,
52 addServerToServerBlocklist, 22 VideoPlaylistElementType,
53 removeAccountFromAccountBlocklist, 23 VideoPlaylistPrivacy,
54 removeAccountFromServerBlocklist, 24 VideoPlaylistType,
55 removeServerFromAccountBlocklist, 25 VideoPrivacy
56 removeServerFromServerBlocklist 26} from '@shared/models'
57} from '../../../../shared/extra-utils/users/blocklist'
58import { User } from '../../../../shared/models/users'
59import { VideoPlaylistCreateResult, VideoPrivacy } from '../../../../shared/models/videos'
60import { VideoExistInPlaylist } from '../../../../shared/models/videos/playlist/video-exist-in-playlist.model'
61import { VideoPlaylistElement, VideoPlaylistElementType } from '../../../../shared/models/videos/playlist/video-playlist-element.model'
62import { VideoPlaylistPrivacy } from '../../../../shared/models/videos/playlist/video-playlist-privacy.model'
63import { VideoPlaylistType } from '../../../../shared/models/videos/playlist/video-playlist-type.model'
64import { VideoPlaylist } from '../../../../shared/models/videos/playlist/video-playlist.model'
65 27
66const expect = chai.expect 28const expect = chai.expect
67 29
68async function checkPlaylistElementType ( 30async function checkPlaylistElementType (
69 servers: ServerInfo[], 31 servers: PeerTubeServer[],
70 playlistId: string, 32 playlistId: string,
71 type: VideoPlaylistElementType, 33 type: VideoPlaylistElementType,
72 position: number, 34 position: number,
@@ -74,10 +36,10 @@ async function checkPlaylistElementType (
74 total: number 36 total: number
75) { 37) {
76 for (const server of servers) { 38 for (const server of servers) {
77 const res = await getPlaylistVideos(server.url, server.accessToken, playlistId, 0, 10) 39 const body = await server.playlists.listVideos({ token: server.accessToken, playlistId, start: 0, count: 10 })
78 expect(res.body.total).to.equal(total) 40 expect(body.total).to.equal(total)
79 41
80 const videoElement: VideoPlaylistElement = res.body.data.find((e: VideoPlaylistElement) => e.position === position) 42 const videoElement = body.data.find(e => e.position === position)
81 expect(videoElement.type).to.equal(type, 'On server ' + server.url) 43 expect(videoElement.type).to.equal(type, 'On server ' + server.url)
82 44
83 if (type === VideoPlaylistElementType.REGULAR) { 45 if (type === VideoPlaylistElementType.REGULAR) {
@@ -90,11 +52,11 @@ async function checkPlaylistElementType (
90} 52}
91 53
92describe('Test video playlists', function () { 54describe('Test video playlists', function () {
93 let servers: ServerInfo[] = [] 55 let servers: PeerTubeServer[] = []
94 56
95 let playlistServer2Id1: number 57 let playlistServer2Id1: number
96 let playlistServer2Id2: number 58 let playlistServer2Id2: number
97 let playlistServer2UUID2: number 59 let playlistServer2UUID2: string
98 60
99 let playlistServer1Id: number 61 let playlistServer1Id: number
100 let playlistServer1UUID: string 62 let playlistServer1UUID: string
@@ -106,12 +68,14 @@ describe('Test video playlists', function () {
106 68
107 let nsfwVideoServer1: number 69 let nsfwVideoServer1: number
108 70
109 let userAccessTokenServer1: string 71 let userTokenServer1: string
72
73 let commands: PlaylistsCommand[]
110 74
111 before(async function () { 75 before(async function () {
112 this.timeout(120000) 76 this.timeout(120000)
113 77
114 servers = await flushAndRunMultipleServers(3, { transcoding: { enabled: false } }) 78 servers = await createMultipleServers(3, { transcoding: { enabled: false } })
115 79
116 // Get the access tokens 80 // Get the access tokens
117 await setAccessTokensToServers(servers) 81 await setAccessTokensToServers(servers)
@@ -122,86 +86,78 @@ describe('Test video playlists', function () {
122 // Server 1 and server 3 follow each other 86 // Server 1 and server 3 follow each other
123 await doubleFollow(servers[0], servers[2]) 87 await doubleFollow(servers[0], servers[2])
124 88
89 commands = servers.map(s => s.playlists)
90
125 { 91 {
126 servers[0].videos = [] 92 servers[0].store.videos = []
127 servers[1].videos = [] 93 servers[1].store.videos = []
128 servers[2].videos = [] 94 servers[2].store.videos = []
129 95
130 for (const server of servers) { 96 for (const server of servers) {
131 for (let i = 0; i < 7; i++) { 97 for (let i = 0; i < 7; i++) {
132 const name = `video ${i} server ${server.serverNumber}` 98 const name = `video ${i} server ${server.serverNumber}`
133 const resVideo = await uploadVideo(server.url, server.accessToken, { name, nsfw: false }) 99 const video = await server.videos.upload({ attributes: { name, nsfw: false } })
134 100
135 server.videos.push(resVideo.body.video) 101 server.store.videos.push(video)
136 } 102 }
137 } 103 }
138 } 104 }
139 105
140 nsfwVideoServer1 = (await uploadVideoAndGetId({ server: servers[0], videoName: 'NSFW video', nsfw: true })).id 106 nsfwVideoServer1 = (await servers[0].videos.quickUpload({ name: 'NSFW video', nsfw: true })).id
141 107
142 { 108 userTokenServer1 = await servers[0].users.generateUserAndToken('user1')
143 await createUser({
144 url: servers[0].url,
145 accessToken: servers[0].accessToken,
146 username: 'user1',
147 password: 'password'
148 })
149 userAccessTokenServer1 = await getAccessToken(servers[0].url, 'user1', 'password')
150 }
151 109
152 await waitJobs(servers) 110 await waitJobs(servers)
153 }) 111 })
154 112
155 describe('Get default playlists', function () { 113 describe('Get default playlists', function () {
114
156 it('Should list video playlist privacies', async function () { 115 it('Should list video playlist privacies', async function () {
157 const res = await getVideoPlaylistPrivacies(servers[0].url) 116 const privacies = await commands[0].getPrivacies()
158 117
159 const privacies = res.body
160 expect(Object.keys(privacies)).to.have.length.at.least(3) 118 expect(Object.keys(privacies)).to.have.length.at.least(3)
161
162 expect(privacies[3]).to.equal('Private') 119 expect(privacies[3]).to.equal('Private')
163 }) 120 })
164 121
165 it('Should list watch later playlist', async function () { 122 it('Should list watch later playlist', async function () {
166 const url = servers[0].url 123 const token = servers[0].accessToken
167 const accessToken = servers[0].accessToken
168 124
169 { 125 {
170 const res = await getAccountPlaylistsListWithToken(url, accessToken, 'root', 0, 5, VideoPlaylistType.WATCH_LATER) 126 const body = await commands[0].listByAccount({ token, handle: 'root', playlistType: VideoPlaylistType.WATCH_LATER })
171 127
172 expect(res.body.total).to.equal(1) 128 expect(body.total).to.equal(1)
173 expect(res.body.data).to.have.lengthOf(1) 129 expect(body.data).to.have.lengthOf(1)
174 130
175 const playlist: VideoPlaylist = res.body.data[0] 131 const playlist = body.data[0]
176 expect(playlist.displayName).to.equal('Watch later') 132 expect(playlist.displayName).to.equal('Watch later')
177 expect(playlist.type.id).to.equal(VideoPlaylistType.WATCH_LATER) 133 expect(playlist.type.id).to.equal(VideoPlaylistType.WATCH_LATER)
178 expect(playlist.type.label).to.equal('Watch later') 134 expect(playlist.type.label).to.equal('Watch later')
179 } 135 }
180 136
181 { 137 {
182 const res = await getAccountPlaylistsListWithToken(url, accessToken, 'root', 0, 5, VideoPlaylistType.REGULAR) 138 const body = await commands[0].listByAccount({ token, handle: 'root', playlistType: VideoPlaylistType.REGULAR })
183 139
184 expect(res.body.total).to.equal(0) 140 expect(body.total).to.equal(0)
185 expect(res.body.data).to.have.lengthOf(0) 141 expect(body.data).to.have.lengthOf(0)
186 } 142 }
187 143
188 { 144 {
189 const res = await getAccountPlaylistsList(url, 'root', 0, 5) 145 const body = await commands[0].listByAccount({ handle: 'root' })
190 expect(res.body.total).to.equal(0) 146 expect(body.total).to.equal(0)
191 expect(res.body.data).to.have.lengthOf(0) 147 expect(body.data).to.have.lengthOf(0)
192 } 148 }
193 }) 149 })
194 150
195 it('Should get private playlist for a classic user', async function () { 151 it('Should get private playlist for a classic user', async function () {
196 const token = await generateUserAccessToken(servers[0], 'toto') 152 const token = await servers[0].users.generateUserAndToken('toto')
197 153
198 const res = await getAccountPlaylistsListWithToken(servers[0].url, token, 'toto', 0, 5) 154 const body = await commands[0].listByAccount({ token, handle: 'toto' })
199 155
200 expect(res.body.total).to.equal(1) 156 expect(body.total).to.equal(1)
201 expect(res.body.data).to.have.lengthOf(1) 157 expect(body.data).to.have.lengthOf(1)
202 158
203 const playlistId = res.body.data[0].id 159 const playlistId = body.data[0].id
204 await getPlaylistVideos(servers[0].url, token, playlistId, 0, 5) 160 await commands[0].listVideos({ token, playlistId })
205 }) 161 })
206 }) 162 })
207 163
@@ -210,15 +166,13 @@ describe('Test video playlists', function () {
210 it('Should create a playlist on server 1 and have the playlist on server 2 and 3', async function () { 166 it('Should create a playlist on server 1 and have the playlist on server 2 and 3', async function () {
211 this.timeout(30000) 167 this.timeout(30000)
212 168
213 await createVideoPlaylist({ 169 await commands[0].create({
214 url: servers[0].url, 170 attributes: {
215 token: servers[0].accessToken,
216 playlistAttrs: {
217 displayName: 'my super playlist', 171 displayName: 'my super playlist',
218 privacy: VideoPlaylistPrivacy.PUBLIC, 172 privacy: VideoPlaylistPrivacy.PUBLIC,
219 description: 'my super description', 173 description: 'my super description',
220 thumbnailfile: 'thumbnail.jpg', 174 thumbnailfile: 'thumbnail.jpg',
221 videoChannelId: servers[0].videoChannel.id 175 videoChannelId: servers[0].store.channel.id
222 } 176 }
223 }) 177 })
224 178
@@ -227,14 +181,13 @@ describe('Test video playlists', function () {
227 await wait(3000) 181 await wait(3000)
228 182
229 for (const server of servers) { 183 for (const server of servers) {
230 const res = await getVideoPlaylistsList(server.url, 0, 5) 184 const body = await server.playlists.list({ start: 0, count: 5 })
231 expect(res.body.total).to.equal(1) 185 expect(body.total).to.equal(1)
232 expect(res.body.data).to.have.lengthOf(1) 186 expect(body.data).to.have.lengthOf(1)
233 187
234 const playlistFromList = res.body.data[0] as VideoPlaylist 188 const playlistFromList = body.data[0]
235 189
236 const res2 = await getVideoPlaylist(server.url, playlistFromList.uuid) 190 const playlistFromGet = await server.playlists.get({ playlistId: playlistFromList.uuid })
237 const playlistFromGet = res2.body as VideoPlaylist
238 191
239 for (const playlist of [ playlistFromGet, playlistFromList ]) { 192 for (const playlist of [ playlistFromGet, playlistFromList ]) {
240 expect(playlist.id).to.be.a('number') 193 expect(playlist.id).to.be.a('number')
@@ -264,46 +217,38 @@ describe('Test video playlists', function () {
264 this.timeout(30000) 217 this.timeout(30000)
265 218
266 { 219 {
267 const res = await createVideoPlaylist({ 220 const playlist = await servers[1].playlists.create({
268 url: servers[1].url, 221 attributes: {
269 token: servers[1].accessToken,
270 playlistAttrs: {
271 displayName: 'playlist 2', 222 displayName: 'playlist 2',
272 privacy: VideoPlaylistPrivacy.PUBLIC, 223 privacy: VideoPlaylistPrivacy.PUBLIC,
273 videoChannelId: servers[1].videoChannel.id 224 videoChannelId: servers[1].store.channel.id
274 } 225 }
275 }) 226 })
276 playlistServer2Id1 = res.body.videoPlaylist.id 227 playlistServer2Id1 = playlist.id
277 } 228 }
278 229
279 { 230 {
280 const res = await createVideoPlaylist({ 231 const playlist = await servers[1].playlists.create({
281 url: servers[1].url, 232 attributes: {
282 token: servers[1].accessToken,
283 playlistAttrs: {
284 displayName: 'playlist 3', 233 displayName: 'playlist 3',
285 privacy: VideoPlaylistPrivacy.PUBLIC, 234 privacy: VideoPlaylistPrivacy.PUBLIC,
286 thumbnailfile: 'thumbnail.jpg', 235 thumbnailfile: 'thumbnail.jpg',
287 videoChannelId: servers[1].videoChannel.id 236 videoChannelId: servers[1].store.channel.id
288 } 237 }
289 }) 238 })
290 239
291 playlistServer2Id2 = res.body.videoPlaylist.id 240 playlistServer2Id2 = playlist.id
292 playlistServer2UUID2 = res.body.videoPlaylist.uuid 241 playlistServer2UUID2 = playlist.uuid
293 } 242 }
294 243
295 for (const id of [ playlistServer2Id1, playlistServer2Id2 ]) { 244 for (const id of [ playlistServer2Id1, playlistServer2Id2 ]) {
296 await addVideoInPlaylist({ 245 await servers[1].playlists.addElement({
297 url: servers[1].url,
298 token: servers[1].accessToken,
299 playlistId: id, 246 playlistId: id,
300 elementAttrs: { videoId: servers[1].videos[0].id, startTimestamp: 1, stopTimestamp: 2 } 247 attributes: { videoId: servers[1].store.videos[0].id, startTimestamp: 1, stopTimestamp: 2 }
301 }) 248 })
302 await addVideoInPlaylist({ 249 await servers[1].playlists.addElement({
303 url: servers[1].url,
304 token: servers[1].accessToken,
305 playlistId: id, 250 playlistId: id,
306 elementAttrs: { videoId: servers[1].videos[1].id } 251 attributes: { videoId: servers[1].store.videos[1].id }
307 }) 252 })
308 } 253 }
309 254
@@ -311,20 +256,20 @@ describe('Test video playlists', function () {
311 await wait(3000) 256 await wait(3000)
312 257
313 for (const server of [ servers[0], servers[1] ]) { 258 for (const server of [ servers[0], servers[1] ]) {
314 const res = await getVideoPlaylistsList(server.url, 0, 5) 259 const body = await server.playlists.list({ start: 0, count: 5 })
315 260
316 const playlist2 = res.body.data.find(p => p.displayName === 'playlist 2') 261 const playlist2 = body.data.find(p => p.displayName === 'playlist 2')
317 expect(playlist2).to.not.be.undefined 262 expect(playlist2).to.not.be.undefined
318 await testImage(server.url, 'thumbnail-playlist', playlist2.thumbnailPath) 263 await testImage(server.url, 'thumbnail-playlist', playlist2.thumbnailPath)
319 264
320 const playlist3 = res.body.data.find(p => p.displayName === 'playlist 3') 265 const playlist3 = body.data.find(p => p.displayName === 'playlist 3')
321 expect(playlist3).to.not.be.undefined 266 expect(playlist3).to.not.be.undefined
322 await testImage(server.url, 'thumbnail', playlist3.thumbnailPath) 267 await testImage(server.url, 'thumbnail', playlist3.thumbnailPath)
323 } 268 }
324 269
325 const res = await getVideoPlaylistsList(servers[2].url, 0, 5) 270 const body = await servers[2].playlists.list({ start: 0, count: 5 })
326 expect(res.body.data.find(p => p.displayName === 'playlist 2')).to.be.undefined 271 expect(body.data.find(p => p.displayName === 'playlist 2')).to.be.undefined
327 expect(res.body.data.find(p => p.displayName === 'playlist 3')).to.be.undefined 272 expect(body.data.find(p => p.displayName === 'playlist 3')).to.be.undefined
328 }) 273 })
329 274
330 it('Should have the playlist on server 3 after a new follow', async function () { 275 it('Should have the playlist on server 3 after a new follow', async function () {
@@ -333,13 +278,13 @@ describe('Test video playlists', function () {
333 // Server 2 and server 3 follow each other 278 // Server 2 and server 3 follow each other
334 await doubleFollow(servers[1], servers[2]) 279 await doubleFollow(servers[1], servers[2])
335 280
336 const res = await getVideoPlaylistsList(servers[2].url, 0, 5) 281 const body = await servers[2].playlists.list({ start: 0, count: 5 })
337 282
338 const playlist2 = res.body.data.find(p => p.displayName === 'playlist 2') 283 const playlist2 = body.data.find(p => p.displayName === 'playlist 2')
339 expect(playlist2).to.not.be.undefined 284 expect(playlist2).to.not.be.undefined
340 await testImage(servers[2].url, 'thumbnail-playlist', playlist2.thumbnailPath) 285 await testImage(servers[2].url, 'thumbnail-playlist', playlist2.thumbnailPath)
341 286
342 expect(res.body.data.find(p => p.displayName === 'playlist 3')).to.not.be.undefined 287 expect(body.data.find(p => p.displayName === 'playlist 3')).to.not.be.undefined
343 }) 288 })
344 }) 289 })
345 290
@@ -349,22 +294,20 @@ describe('Test video playlists', function () {
349 this.timeout(30000) 294 this.timeout(30000)
350 295
351 { 296 {
352 const res = await getVideoPlaylistsList(servers[2].url, 1, 2, 'createdAt') 297 const body = await servers[2].playlists.list({ start: 1, count: 2, sort: 'createdAt' })
353 298 expect(body.total).to.equal(3)
354 expect(res.body.total).to.equal(3)
355 299
356 const data: VideoPlaylist[] = res.body.data 300 const data = body.data
357 expect(data).to.have.lengthOf(2) 301 expect(data).to.have.lengthOf(2)
358 expect(data[0].displayName).to.equal('playlist 2') 302 expect(data[0].displayName).to.equal('playlist 2')
359 expect(data[1].displayName).to.equal('playlist 3') 303 expect(data[1].displayName).to.equal('playlist 3')
360 } 304 }
361 305
362 { 306 {
363 const res = await getVideoPlaylistsList(servers[2].url, 1, 2, '-createdAt') 307 const body = await servers[2].playlists.list({ start: 1, count: 2, sort: '-createdAt' })
308 expect(body.total).to.equal(3)
364 309
365 expect(res.body.total).to.equal(3) 310 const data = body.data
366
367 const data: VideoPlaylist[] = res.body.data
368 expect(data).to.have.lengthOf(2) 311 expect(data).to.have.lengthOf(2)
369 expect(data[0].displayName).to.equal('playlist 2') 312 expect(data[0].displayName).to.equal('playlist 2')
370 expect(data[1].displayName).to.equal('my super playlist') 313 expect(data[1].displayName).to.equal('my super playlist')
@@ -375,11 +318,10 @@ describe('Test video playlists', function () {
375 this.timeout(30000) 318 this.timeout(30000)
376 319
377 { 320 {
378 const res = await getVideoChannelPlaylistsList(servers[0].url, 'root_channel', 0, 2, '-createdAt') 321 const body = await commands[0].listByChannel({ handle: 'root_channel', start: 0, count: 2, sort: '-createdAt' })
379 322 expect(body.total).to.equal(1)
380 expect(res.body.total).to.equal(1)
381 323
382 const data: VideoPlaylist[] = res.body.data 324 const data = body.data
383 expect(data).to.have.lengthOf(1) 325 expect(data).to.have.lengthOf(1)
384 expect(data[0].displayName).to.equal('my super playlist') 326 expect(data[0].displayName).to.equal('my super playlist')
385 } 327 }
@@ -389,41 +331,37 @@ describe('Test video playlists', function () {
389 this.timeout(30000) 331 this.timeout(30000)
390 332
391 { 333 {
392 const res = await getAccountPlaylistsList(servers[1].url, 'root', 1, 2, '-createdAt') 334 const body = await servers[1].playlists.listByAccount({ handle: 'root', start: 1, count: 2, sort: '-createdAt' })
335 expect(body.total).to.equal(2)
393 336
394 expect(res.body.total).to.equal(2) 337 const data = body.data
395
396 const data: VideoPlaylist[] = res.body.data
397 expect(data).to.have.lengthOf(1) 338 expect(data).to.have.lengthOf(1)
398 expect(data[0].displayName).to.equal('playlist 2') 339 expect(data[0].displayName).to.equal('playlist 2')
399 } 340 }
400 341
401 { 342 {
402 const res = await getAccountPlaylistsList(servers[1].url, 'root', 1, 2, 'createdAt') 343 const body = await servers[1].playlists.listByAccount({ handle: 'root', start: 1, count: 2, sort: 'createdAt' })
403 344 expect(body.total).to.equal(2)
404 expect(res.body.total).to.equal(2)
405 345
406 const data: VideoPlaylist[] = res.body.data 346 const data = body.data
407 expect(data).to.have.lengthOf(1) 347 expect(data).to.have.lengthOf(1)
408 expect(data[0].displayName).to.equal('playlist 3') 348 expect(data[0].displayName).to.equal('playlist 3')
409 } 349 }
410 350
411 { 351 {
412 const res = await getAccountPlaylistsList(servers[1].url, 'root', 0, 10, 'createdAt', '3') 352 const body = await servers[1].playlists.listByAccount({ handle: 'root', sort: 'createdAt', search: '3' })
353 expect(body.total).to.equal(1)
413 354
414 expect(res.body.total).to.equal(1) 355 const data = body.data
415
416 const data: VideoPlaylist[] = res.body.data
417 expect(data).to.have.lengthOf(1) 356 expect(data).to.have.lengthOf(1)
418 expect(data[0].displayName).to.equal('playlist 3') 357 expect(data[0].displayName).to.equal('playlist 3')
419 } 358 }
420 359
421 { 360 {
422 const res = await getAccountPlaylistsList(servers[1].url, 'root', 0, 10, 'createdAt', '4') 361 const body = await servers[1].playlists.listByAccount({ handle: 'root', sort: 'createdAt', search: '4' })
423 362 expect(body.total).to.equal(0)
424 expect(res.body.total).to.equal(0)
425 363
426 const data: VideoPlaylist[] = res.body.data 364 const data = body.data
427 expect(data).to.have.lengthOf(0) 365 expect(data).to.have.lengthOf(0)
428 } 366 }
429 }) 367 })
@@ -437,28 +375,22 @@ describe('Test video playlists', function () {
437 this.timeout(30000) 375 this.timeout(30000)
438 376
439 { 377 {
440 const res = await createVideoPlaylist({ 378 unlistedPlaylist = await servers[1].playlists.create({
441 url: servers[1].url, 379 attributes: {
442 token: servers[1].accessToken,
443 playlistAttrs: {
444 displayName: 'playlist unlisted', 380 displayName: 'playlist unlisted',
445 privacy: VideoPlaylistPrivacy.UNLISTED, 381 privacy: VideoPlaylistPrivacy.UNLISTED,
446 videoChannelId: servers[1].videoChannel.id 382 videoChannelId: servers[1].store.channel.id
447 } 383 }
448 }) 384 })
449 unlistedPlaylist = res.body.videoPlaylist
450 } 385 }
451 386
452 { 387 {
453 const res = await createVideoPlaylist({ 388 privatePlaylist = await servers[1].playlists.create({
454 url: servers[1].url, 389 attributes: {
455 token: servers[1].accessToken,
456 playlistAttrs: {
457 displayName: 'playlist private', 390 displayName: 'playlist private',
458 privacy: VideoPlaylistPrivacy.PRIVATE 391 privacy: VideoPlaylistPrivacy.PRIVATE
459 } 392 }
460 }) 393 })
461 privatePlaylist = res.body.videoPlaylist
462 } 394 }
463 395
464 await waitJobs(servers) 396 await waitJobs(servers)
@@ -468,15 +400,15 @@ describe('Test video playlists', function () {
468 it('Should not list unlisted or private playlists', async function () { 400 it('Should not list unlisted or private playlists', async function () {
469 for (const server of servers) { 401 for (const server of servers) {
470 const results = [ 402 const results = [
471 await getAccountPlaylistsList(server.url, 'root@localhost:' + servers[1].port, 0, 5, '-createdAt'), 403 await server.playlists.listByAccount({ handle: 'root@localhost:' + servers[1].port, sort: '-createdAt' }),
472 await getVideoPlaylistsList(server.url, 0, 2, '-createdAt') 404 await server.playlists.list({ start: 0, count: 2, sort: '-createdAt' })
473 ] 405 ]
474 406
475 expect(results[0].body.total).to.equal(2) 407 expect(results[0].total).to.equal(2)
476 expect(results[1].body.total).to.equal(3) 408 expect(results[1].total).to.equal(3)
477 409
478 for (const res of results) { 410 for (const body of results) {
479 const data: VideoPlaylist[] = res.body.data 411 const data = body.data
480 expect(data).to.have.lengthOf(2) 412 expect(data).to.have.lengthOf(2)
481 expect(data[0].displayName).to.equal('playlist 3') 413 expect(data[0].displayName).to.equal('playlist 3')
482 expect(data[1].displayName).to.equal('playlist 2') 414 expect(data[1].displayName).to.equal('playlist 2')
@@ -485,23 +417,23 @@ describe('Test video playlists', function () {
485 }) 417 })
486 418
487 it('Should not get unlisted playlist using only the id', async function () { 419 it('Should not get unlisted playlist using only the id', async function () {
488 await getVideoPlaylist(servers[1].url, unlistedPlaylist.id, 404) 420 await servers[1].playlists.get({ playlistId: unlistedPlaylist.id, expectedStatus: 404 })
489 }) 421 })
490 422
491 it('Should get unlisted plyaylist using uuid or shortUUID', async function () { 423 it('Should get unlisted plyaylist using uuid or shortUUID', async function () {
492 await getVideoPlaylist(servers[1].url, unlistedPlaylist.uuid) 424 await servers[1].playlists.get({ playlistId: unlistedPlaylist.uuid })
493 await getVideoPlaylist(servers[1].url, unlistedPlaylist.shortUUID) 425 await servers[1].playlists.get({ playlistId: unlistedPlaylist.shortUUID })
494 }) 426 })
495 427
496 it('Should not get private playlist without token', async function () { 428 it('Should not get private playlist without token', async function () {
497 for (const id of [ privatePlaylist.id, privatePlaylist.uuid, privatePlaylist.shortUUID ]) { 429 for (const id of [ privatePlaylist.id, privatePlaylist.uuid, privatePlaylist.shortUUID ]) {
498 await getVideoPlaylist(servers[1].url, id, 401) 430 await servers[1].playlists.get({ playlistId: id, expectedStatus: 401 })
499 } 431 }
500 }) 432 })
501 433
502 it('Should get private playlist with a token', async function () { 434 it('Should get private playlist with a token', async function () {
503 for (const id of [ privatePlaylist.id, privatePlaylist.uuid, privatePlaylist.shortUUID ]) { 435 for (const id of [ privatePlaylist.id, privatePlaylist.uuid, privatePlaylist.shortUUID ]) {
504 await getVideoPlaylistWithToken(servers[1].url, servers[1].accessToken, id) 436 await servers[1].playlists.get({ token: servers[1].accessToken, playlistId: id })
505 } 437 }
506 }) 438 })
507 }) 439 })
@@ -511,15 +443,13 @@ describe('Test video playlists', function () {
511 it('Should update a playlist', async function () { 443 it('Should update a playlist', async function () {
512 this.timeout(30000) 444 this.timeout(30000)
513 445
514 await updateVideoPlaylist({ 446 await servers[1].playlists.update({
515 url: servers[1].url, 447 attributes: {
516 token: servers[1].accessToken,
517 playlistAttrs: {
518 displayName: 'playlist 3 updated', 448 displayName: 'playlist 3 updated',
519 description: 'description updated', 449 description: 'description updated',
520 privacy: VideoPlaylistPrivacy.UNLISTED, 450 privacy: VideoPlaylistPrivacy.UNLISTED,
521 thumbnailfile: 'thumbnail.jpg', 451 thumbnailfile: 'thumbnail.jpg',
522 videoChannelId: servers[1].videoChannel.id 452 videoChannelId: servers[1].store.channel.id
523 }, 453 },
524 playlistId: playlistServer2Id2 454 playlistId: playlistServer2Id2
525 }) 455 })
@@ -527,8 +457,7 @@ describe('Test video playlists', function () {
527 await waitJobs(servers) 457 await waitJobs(servers)
528 458
529 for (const server of servers) { 459 for (const server of servers) {
530 const res = await getVideoPlaylist(server.url, playlistServer2UUID2) 460 const playlist = await server.playlists.get({ playlistId: playlistServer2UUID2 })
531 const playlist: VideoPlaylist = res.body
532 461
533 expect(playlist.displayName).to.equal('playlist 3 updated') 462 expect(playlist.displayName).to.equal('playlist 3 updated')
534 expect(playlist.description).to.equal('description updated') 463 expect(playlist.description).to.equal('description updated')
@@ -554,39 +483,37 @@ describe('Test video playlists', function () {
554 it('Should create a playlist containing different startTimestamp/endTimestamp videos', async function () { 483 it('Should create a playlist containing different startTimestamp/endTimestamp videos', async function () {
555 this.timeout(30000) 484 this.timeout(30000)
556 485
557 const addVideo = (elementAttrs: any) => { 486 const addVideo = (attributes: any) => {
558 return addVideoInPlaylist({ url: servers[0].url, token: servers[0].accessToken, playlistId: playlistServer1Id, elementAttrs }) 487 return commands[0].addElement({ playlistId: playlistServer1Id, attributes })
559 } 488 }
560 489
561 const res = await createVideoPlaylist({ 490 const playlist = await commands[0].create({
562 url: servers[0].url, 491 attributes: {
563 token: servers[0].accessToken,
564 playlistAttrs: {
565 displayName: 'playlist 4', 492 displayName: 'playlist 4',
566 privacy: VideoPlaylistPrivacy.PUBLIC, 493 privacy: VideoPlaylistPrivacy.PUBLIC,
567 videoChannelId: servers[0].videoChannel.id 494 videoChannelId: servers[0].store.channel.id
568 } 495 }
569 }) 496 })
570 497
571 playlistServer1Id = res.body.videoPlaylist.id 498 playlistServer1Id = playlist.id
572 playlistServer1UUID = res.body.videoPlaylist.uuid 499 playlistServer1UUID = playlist.uuid
573 500
574 await addVideo({ videoId: servers[0].videos[0].uuid, startTimestamp: 15, stopTimestamp: 28 }) 501 await addVideo({ videoId: servers[0].store.videos[0].uuid, startTimestamp: 15, stopTimestamp: 28 })
575 await addVideo({ videoId: servers[2].videos[1].uuid, startTimestamp: 35 }) 502 await addVideo({ videoId: servers[2].store.videos[1].uuid, startTimestamp: 35 })
576 await addVideo({ videoId: servers[2].videos[2].uuid }) 503 await addVideo({ videoId: servers[2].store.videos[2].uuid })
577 { 504 {
578 const res = await addVideo({ videoId: servers[0].videos[3].uuid, stopTimestamp: 35 }) 505 const element = await addVideo({ videoId: servers[0].store.videos[3].uuid, stopTimestamp: 35 })
579 playlistElementServer1Video4 = res.body.videoPlaylistElement.id 506 playlistElementServer1Video4 = element.id
580 } 507 }
581 508
582 { 509 {
583 const res = await addVideo({ videoId: servers[0].videos[4].uuid, startTimestamp: 45, stopTimestamp: 60 }) 510 const element = await addVideo({ videoId: servers[0].store.videos[4].uuid, startTimestamp: 45, stopTimestamp: 60 })
584 playlistElementServer1Video5 = res.body.videoPlaylistElement.id 511 playlistElementServer1Video5 = element.id
585 } 512 }
586 513
587 { 514 {
588 const res = await addVideo({ videoId: nsfwVideoServer1, startTimestamp: 5 }) 515 const element = await addVideo({ videoId: nsfwVideoServer1, startTimestamp: 5 })
589 playlistElementNSFW = res.body.videoPlaylistElement.id 516 playlistElementNSFW = element.id
590 517
591 await addVideo({ videoId: nsfwVideoServer1, startTimestamp: 4 }) 518 await addVideo({ videoId: nsfwVideoServer1, startTimestamp: 4 })
592 await addVideo({ videoId: nsfwVideoServer1 }) 519 await addVideo({ videoId: nsfwVideoServer1 })
@@ -599,64 +526,68 @@ describe('Test video playlists', function () {
599 this.timeout(30000) 526 this.timeout(30000)
600 527
601 for (const server of servers) { 528 for (const server of servers) {
602 const res = await getPlaylistVideos(server.url, server.accessToken, playlistServer1UUID, 0, 10) 529 {
603 530 const body = await server.playlists.listVideos({ playlistId: playlistServer1UUID, start: 0, count: 10 })
604 expect(res.body.total).to.equal(8) 531
605 532 expect(body.total).to.equal(8)
606 const videoElements: VideoPlaylistElement[] = res.body.data 533
607 expect(videoElements).to.have.lengthOf(8) 534 const videoElements = body.data
608 535 expect(videoElements).to.have.lengthOf(8)
609 expect(videoElements[0].video.name).to.equal('video 0 server 1') 536
610 expect(videoElements[0].position).to.equal(1) 537 expect(videoElements[0].video.name).to.equal('video 0 server 1')
611 expect(videoElements[0].startTimestamp).to.equal(15) 538 expect(videoElements[0].position).to.equal(1)
612 expect(videoElements[0].stopTimestamp).to.equal(28) 539 expect(videoElements[0].startTimestamp).to.equal(15)
613 540 expect(videoElements[0].stopTimestamp).to.equal(28)
614 expect(videoElements[1].video.name).to.equal('video 1 server 3') 541
615 expect(videoElements[1].position).to.equal(2) 542 expect(videoElements[1].video.name).to.equal('video 1 server 3')
616 expect(videoElements[1].startTimestamp).to.equal(35) 543 expect(videoElements[1].position).to.equal(2)
617 expect(videoElements[1].stopTimestamp).to.be.null 544 expect(videoElements[1].startTimestamp).to.equal(35)
618 545 expect(videoElements[1].stopTimestamp).to.be.null
619 expect(videoElements[2].video.name).to.equal('video 2 server 3') 546
620 expect(videoElements[2].position).to.equal(3) 547 expect(videoElements[2].video.name).to.equal('video 2 server 3')
621 expect(videoElements[2].startTimestamp).to.be.null 548 expect(videoElements[2].position).to.equal(3)
622 expect(videoElements[2].stopTimestamp).to.be.null 549 expect(videoElements[2].startTimestamp).to.be.null
623 550 expect(videoElements[2].stopTimestamp).to.be.null
624 expect(videoElements[3].video.name).to.equal('video 3 server 1') 551
625 expect(videoElements[3].position).to.equal(4) 552 expect(videoElements[3].video.name).to.equal('video 3 server 1')
626 expect(videoElements[3].startTimestamp).to.be.null 553 expect(videoElements[3].position).to.equal(4)
627 expect(videoElements[3].stopTimestamp).to.equal(35) 554 expect(videoElements[3].startTimestamp).to.be.null
628 555 expect(videoElements[3].stopTimestamp).to.equal(35)
629 expect(videoElements[4].video.name).to.equal('video 4 server 1') 556
630 expect(videoElements[4].position).to.equal(5) 557 expect(videoElements[4].video.name).to.equal('video 4 server 1')
631 expect(videoElements[4].startTimestamp).to.equal(45) 558 expect(videoElements[4].position).to.equal(5)
632 expect(videoElements[4].stopTimestamp).to.equal(60) 559 expect(videoElements[4].startTimestamp).to.equal(45)
633 560 expect(videoElements[4].stopTimestamp).to.equal(60)
634 expect(videoElements[5].video.name).to.equal('NSFW video') 561
635 expect(videoElements[5].position).to.equal(6) 562 expect(videoElements[5].video.name).to.equal('NSFW video')
636 expect(videoElements[5].startTimestamp).to.equal(5) 563 expect(videoElements[5].position).to.equal(6)
637 expect(videoElements[5].stopTimestamp).to.be.null 564 expect(videoElements[5].startTimestamp).to.equal(5)
638 565 expect(videoElements[5].stopTimestamp).to.be.null
639 expect(videoElements[6].video.name).to.equal('NSFW video') 566
640 expect(videoElements[6].position).to.equal(7) 567 expect(videoElements[6].video.name).to.equal('NSFW video')
641 expect(videoElements[6].startTimestamp).to.equal(4) 568 expect(videoElements[6].position).to.equal(7)
642 expect(videoElements[6].stopTimestamp).to.be.null 569 expect(videoElements[6].startTimestamp).to.equal(4)
643 570 expect(videoElements[6].stopTimestamp).to.be.null
644 expect(videoElements[7].video.name).to.equal('NSFW video') 571
645 expect(videoElements[7].position).to.equal(8) 572 expect(videoElements[7].video.name).to.equal('NSFW video')
646 expect(videoElements[7].startTimestamp).to.be.null 573 expect(videoElements[7].position).to.equal(8)
647 expect(videoElements[7].stopTimestamp).to.be.null 574 expect(videoElements[7].startTimestamp).to.be.null
648 575 expect(videoElements[7].stopTimestamp).to.be.null
649 const res3 = await getPlaylistVideos(server.url, server.accessToken, playlistServer1UUID, 0, 2) 576 }
650 expect(res3.body.data).to.have.lengthOf(2) 577
578 {
579 const body = await server.playlists.listVideos({ playlistId: playlistServer1UUID, start: 0, count: 2 })
580 expect(body.data).to.have.lengthOf(2)
581 }
651 } 582 }
652 }) 583 })
653 }) 584 })
654 585
655 describe('Element type', function () { 586 describe('Element type', function () {
656 let groupUser1: ServerInfo[] 587 let groupUser1: PeerTubeServer[]
657 let groupWithoutToken1: ServerInfo[] 588 let groupWithoutToken1: PeerTubeServer[]
658 let group1: ServerInfo[] 589 let group1: PeerTubeServer[]
659 let group2: ServerInfo[] 590 let group2: PeerTubeServer[]
660 591
661 let video1: string 592 let video1: string
662 let video2: string 593 let video2: string
@@ -665,31 +596,30 @@ describe('Test video playlists', function () {
665 before(async function () { 596 before(async function () {
666 this.timeout(60000) 597 this.timeout(60000)
667 598
668 groupUser1 = [ Object.assign({}, servers[0], { accessToken: userAccessTokenServer1 }) ] 599 groupUser1 = [ Object.assign({}, servers[0], { accessToken: userTokenServer1 }) ]
669 groupWithoutToken1 = [ Object.assign({}, servers[0], { accessToken: undefined }) ] 600 groupWithoutToken1 = [ Object.assign({}, servers[0], { accessToken: undefined }) ]
670 group1 = [ servers[0] ] 601 group1 = [ servers[0] ]
671 group2 = [ servers[1], servers[2] ] 602 group2 = [ servers[1], servers[2] ]
672 603
673 const res = await createVideoPlaylist({ 604 const playlist = await commands[0].create({
674 url: servers[0].url, 605 token: userTokenServer1,
675 token: userAccessTokenServer1, 606 attributes: {
676 playlistAttrs: {
677 displayName: 'playlist 56', 607 displayName: 'playlist 56',
678 privacy: VideoPlaylistPrivacy.PUBLIC, 608 privacy: VideoPlaylistPrivacy.PUBLIC,
679 videoChannelId: servers[0].videoChannel.id 609 videoChannelId: servers[0].store.channel.id
680 } 610 }
681 }) 611 })
682 612
683 const playlistServer1Id2 = res.body.videoPlaylist.id 613 const playlistServer1Id2 = playlist.id
684 playlistServer1UUID2 = res.body.videoPlaylist.uuid 614 playlistServer1UUID2 = playlist.uuid
685 615
686 const addVideo = (elementAttrs: any) => { 616 const addVideo = (attributes: any) => {
687 return addVideoInPlaylist({ url: servers[0].url, token: userAccessTokenServer1, playlistId: playlistServer1Id2, elementAttrs }) 617 return commands[0].addElement({ token: userTokenServer1, playlistId: playlistServer1Id2, attributes })
688 } 618 }
689 619
690 video1 = (await uploadVideoAndGetId({ server: servers[0], videoName: 'video 89', token: userAccessTokenServer1 })).uuid 620 video1 = (await servers[0].videos.quickUpload({ name: 'video 89', token: userTokenServer1 })).uuid
691 video2 = (await uploadVideoAndGetId({ server: servers[1], videoName: 'video 90' })).uuid 621 video2 = (await servers[1].videos.quickUpload({ name: 'video 90' })).uuid
692 video3 = (await uploadVideoAndGetId({ server: servers[0], videoName: 'video 91', nsfw: true })).uuid 622 video3 = (await servers[0].videos.quickUpload({ name: 'video 91', nsfw: true })).uuid
693 623
694 await waitJobs(servers) 624 await waitJobs(servers)
695 625
@@ -707,7 +637,7 @@ describe('Test video playlists', function () {
707 const position = 1 637 const position = 1
708 638
709 { 639 {
710 await updateVideo(servers[0].url, servers[0].accessToken, video1, { privacy: VideoPrivacy.PRIVATE }) 640 await servers[0].videos.update({ id: video1, attributes: { privacy: VideoPrivacy.PRIVATE } })
711 await waitJobs(servers) 641 await waitJobs(servers)
712 642
713 await checkPlaylistElementType(groupUser1, playlistServer1UUID2, VideoPlaylistElementType.REGULAR, position, name, 3) 643 await checkPlaylistElementType(groupUser1, playlistServer1UUID2, VideoPlaylistElementType.REGULAR, position, name, 3)
@@ -717,7 +647,7 @@ describe('Test video playlists', function () {
717 } 647 }
718 648
719 { 649 {
720 await updateVideo(servers[0].url, servers[0].accessToken, video1, { privacy: VideoPrivacy.PUBLIC }) 650 await servers[0].videos.update({ id: video1, attributes: { privacy: VideoPrivacy.PUBLIC } })
721 await waitJobs(servers) 651 await waitJobs(servers)
722 652
723 await checkPlaylistElementType(groupUser1, playlistServer1UUID2, VideoPlaylistElementType.REGULAR, position, name, 3) 653 await checkPlaylistElementType(groupUser1, playlistServer1UUID2, VideoPlaylistElementType.REGULAR, position, name, 3)
@@ -735,7 +665,7 @@ describe('Test video playlists', function () {
735 const position = 1 665 const position = 1
736 666
737 { 667 {
738 await addVideoToBlacklist(servers[0].url, servers[0].accessToken, video1, 'reason', true) 668 await servers[0].blacklist.add({ videoId: video1, reason: 'reason', unfederate: true })
739 await waitJobs(servers) 669 await waitJobs(servers)
740 670
741 await checkPlaylistElementType(groupUser1, playlistServer1UUID2, VideoPlaylistElementType.REGULAR, position, name, 3) 671 await checkPlaylistElementType(groupUser1, playlistServer1UUID2, VideoPlaylistElementType.REGULAR, position, name, 3)
@@ -745,7 +675,7 @@ describe('Test video playlists', function () {
745 } 675 }
746 676
747 { 677 {
748 await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, video1) 678 await servers[0].blacklist.remove({ videoId: video1 })
749 await waitJobs(servers) 679 await waitJobs(servers)
750 680
751 await checkPlaylistElementType(groupUser1, playlistServer1UUID2, VideoPlaylistElementType.REGULAR, position, name, 3) 681 await checkPlaylistElementType(groupUser1, playlistServer1UUID2, VideoPlaylistElementType.REGULAR, position, name, 3)
@@ -759,56 +689,58 @@ describe('Test video playlists', function () {
759 it('Should update the element type if the account or server of the video is blocked', async function () { 689 it('Should update the element type if the account or server of the video is blocked', async function () {
760 this.timeout(90000) 690 this.timeout(90000)
761 691
692 const command = servers[0].blocklist
693
762 const name = 'video 90' 694 const name = 'video 90'
763 const position = 2 695 const position = 2
764 696
765 { 697 {
766 await addAccountToAccountBlocklist(servers[0].url, userAccessTokenServer1, 'root@localhost:' + servers[1].port) 698 await command.addToMyBlocklist({ token: userTokenServer1, account: 'root@localhost:' + servers[1].port })
767 await waitJobs(servers) 699 await waitJobs(servers)
768 700
769 await checkPlaylistElementType(groupUser1, playlistServer1UUID2, VideoPlaylistElementType.UNAVAILABLE, position, name, 3) 701 await checkPlaylistElementType(groupUser1, playlistServer1UUID2, VideoPlaylistElementType.UNAVAILABLE, position, name, 3)
770 await checkPlaylistElementType(group2, playlistServer1UUID2, VideoPlaylistElementType.REGULAR, position, name, 3) 702 await checkPlaylistElementType(group2, playlistServer1UUID2, VideoPlaylistElementType.REGULAR, position, name, 3)
771 703
772 await removeAccountFromAccountBlocklist(servers[0].url, userAccessTokenServer1, 'root@localhost:' + servers[1].port) 704 await command.removeFromMyBlocklist({ token: userTokenServer1, account: 'root@localhost:' + servers[1].port })
773 await waitJobs(servers) 705 await waitJobs(servers)
774 706
775 await checkPlaylistElementType(group2, playlistServer1UUID2, VideoPlaylistElementType.REGULAR, position, name, 3) 707 await checkPlaylistElementType(group2, playlistServer1UUID2, VideoPlaylistElementType.REGULAR, position, name, 3)
776 } 708 }
777 709
778 { 710 {
779 await addServerToAccountBlocklist(servers[0].url, userAccessTokenServer1, 'localhost:' + servers[1].port) 711 await command.addToMyBlocklist({ token: userTokenServer1, server: 'localhost:' + servers[1].port })
780 await waitJobs(servers) 712 await waitJobs(servers)
781 713
782 await checkPlaylistElementType(groupUser1, playlistServer1UUID2, VideoPlaylistElementType.UNAVAILABLE, position, name, 3) 714 await checkPlaylistElementType(groupUser1, playlistServer1UUID2, VideoPlaylistElementType.UNAVAILABLE, position, name, 3)
783 await checkPlaylistElementType(group2, playlistServer1UUID2, VideoPlaylistElementType.REGULAR, position, name, 3) 715 await checkPlaylistElementType(group2, playlistServer1UUID2, VideoPlaylistElementType.REGULAR, position, name, 3)
784 716
785 await removeServerFromAccountBlocklist(servers[0].url, userAccessTokenServer1, 'localhost:' + servers[1].port) 717 await command.removeFromMyBlocklist({ token: userTokenServer1, server: 'localhost:' + servers[1].port })
786 await waitJobs(servers) 718 await waitJobs(servers)
787 719
788 await checkPlaylistElementType(group2, playlistServer1UUID2, VideoPlaylistElementType.REGULAR, position, name, 3) 720 await checkPlaylistElementType(group2, playlistServer1UUID2, VideoPlaylistElementType.REGULAR, position, name, 3)
789 } 721 }
790 722
791 { 723 {
792 await addAccountToServerBlocklist(servers[0].url, servers[0].accessToken, 'root@localhost:' + servers[1].port) 724 await command.addToServerBlocklist({ account: 'root@localhost:' + servers[1].port })
793 await waitJobs(servers) 725 await waitJobs(servers)
794 726
795 await checkPlaylistElementType(groupUser1, playlistServer1UUID2, VideoPlaylistElementType.UNAVAILABLE, position, name, 3) 727 await checkPlaylistElementType(groupUser1, playlistServer1UUID2, VideoPlaylistElementType.UNAVAILABLE, position, name, 3)
796 await checkPlaylistElementType(group2, playlistServer1UUID2, VideoPlaylistElementType.REGULAR, position, name, 3) 728 await checkPlaylistElementType(group2, playlistServer1UUID2, VideoPlaylistElementType.REGULAR, position, name, 3)
797 729
798 await removeAccountFromServerBlocklist(servers[0].url, servers[0].accessToken, 'root@localhost:' + servers[1].port) 730 await command.removeFromServerBlocklist({ account: 'root@localhost:' + servers[1].port })
799 await waitJobs(servers) 731 await waitJobs(servers)
800 732
801 await checkPlaylistElementType(group2, playlistServer1UUID2, VideoPlaylistElementType.REGULAR, position, name, 3) 733 await checkPlaylistElementType(group2, playlistServer1UUID2, VideoPlaylistElementType.REGULAR, position, name, 3)
802 } 734 }
803 735
804 { 736 {
805 await addServerToServerBlocklist(servers[0].url, servers[0].accessToken, 'localhost:' + servers[1].port) 737 await command.addToServerBlocklist({ server: 'localhost:' + servers[1].port })
806 await waitJobs(servers) 738 await waitJobs(servers)
807 739
808 await checkPlaylistElementType(groupUser1, playlistServer1UUID2, VideoPlaylistElementType.UNAVAILABLE, position, name, 3) 740 await checkPlaylistElementType(groupUser1, playlistServer1UUID2, VideoPlaylistElementType.UNAVAILABLE, position, name, 3)
809 await checkPlaylistElementType(group2, playlistServer1UUID2, VideoPlaylistElementType.REGULAR, position, name, 3) 741 await checkPlaylistElementType(group2, playlistServer1UUID2, VideoPlaylistElementType.REGULAR, position, name, 3)
810 742
811 await removeServerFromServerBlocklist(servers[0].url, servers[0].accessToken, 'localhost:' + servers[1].port) 743 await command.removeFromServerBlocklist({ server: 'localhost:' + servers[1].port })
812 await waitJobs(servers) 744 await waitJobs(servers)
813 745
814 await checkPlaylistElementType(group2, playlistServer1UUID2, VideoPlaylistElementType.REGULAR, position, name, 3) 746 await checkPlaylistElementType(group2, playlistServer1UUID2, VideoPlaylistElementType.REGULAR, position, name, 3)
@@ -816,10 +748,10 @@ describe('Test video playlists', function () {
816 }) 748 })
817 749
818 it('Should hide the video if it is NSFW', async function () { 750 it('Should hide the video if it is NSFW', async function () {
819 const res = await getPlaylistVideos(servers[0].url, userAccessTokenServer1, playlistServer1UUID2, 0, 10, { nsfw: false }) 751 const body = await commands[0].listVideos({ token: userTokenServer1, playlistId: playlistServer1UUID2, query: { nsfw: 'false' } })
820 expect(res.body.total).to.equal(3) 752 expect(body.total).to.equal(3)
821 753
822 const elements: VideoPlaylistElement[] = res.body.data 754 const elements = body.data
823 const element = elements.find(e => e.position === 3) 755 const element = elements.find(e => e.position === 3)
824 756
825 expect(element).to.exist 757 expect(element).to.exist
@@ -835,11 +767,9 @@ describe('Test video playlists', function () {
835 this.timeout(30000) 767 this.timeout(30000)
836 768
837 { 769 {
838 await reorderVideosPlaylist({ 770 await commands[0].reorderElements({
839 url: servers[0].url,
840 token: servers[0].accessToken,
841 playlistId: playlistServer1Id, 771 playlistId: playlistServer1Id,
842 elementAttrs: { 772 attributes: {
843 startPosition: 2, 773 startPosition: 2,
844 insertAfterPosition: 3 774 insertAfterPosition: 3
845 } 775 }
@@ -848,8 +778,8 @@ describe('Test video playlists', function () {
848 await waitJobs(servers) 778 await waitJobs(servers)
849 779
850 for (const server of servers) { 780 for (const server of servers) {
851 const res = await getPlaylistVideos(server.url, server.accessToken, playlistServer1UUID, 0, 10) 781 const body = await server.playlists.listVideos({ playlistId: playlistServer1UUID, start: 0, count: 10 })
852 const names = (res.body.data as VideoPlaylistElement[]).map(v => v.video.name) 782 const names = body.data.map(v => v.video.name)
853 783
854 expect(names).to.deep.equal([ 784 expect(names).to.deep.equal([
855 'video 0 server 1', 785 'video 0 server 1',
@@ -865,11 +795,9 @@ describe('Test video playlists', function () {
865 } 795 }
866 796
867 { 797 {
868 await reorderVideosPlaylist({ 798 await commands[0].reorderElements({
869 url: servers[0].url,
870 token: servers[0].accessToken,
871 playlistId: playlistServer1Id, 799 playlistId: playlistServer1Id,
872 elementAttrs: { 800 attributes: {
873 startPosition: 1, 801 startPosition: 1,
874 reorderLength: 3, 802 reorderLength: 3,
875 insertAfterPosition: 4 803 insertAfterPosition: 4
@@ -879,8 +807,8 @@ describe('Test video playlists', function () {
879 await waitJobs(servers) 807 await waitJobs(servers)
880 808
881 for (const server of servers) { 809 for (const server of servers) {
882 const res = await getPlaylistVideos(server.url, server.accessToken, playlistServer1UUID, 0, 10) 810 const body = await server.playlists.listVideos({ playlistId: playlistServer1UUID, start: 0, count: 10 })
883 const names = (res.body.data as VideoPlaylistElement[]).map(v => v.video.name) 811 const names = body.data.map(v => v.video.name)
884 812
885 expect(names).to.deep.equal([ 813 expect(names).to.deep.equal([
886 'video 3 server 1', 814 'video 3 server 1',
@@ -896,11 +824,9 @@ describe('Test video playlists', function () {
896 } 824 }
897 825
898 { 826 {
899 await reorderVideosPlaylist({ 827 await commands[0].reorderElements({
900 url: servers[0].url,
901 token: servers[0].accessToken,
902 playlistId: playlistServer1Id, 828 playlistId: playlistServer1Id,
903 elementAttrs: { 829 attributes: {
904 startPosition: 6, 830 startPosition: 6,
905 insertAfterPosition: 3 831 insertAfterPosition: 3
906 } 832 }
@@ -909,8 +835,7 @@ describe('Test video playlists', function () {
909 await waitJobs(servers) 835 await waitJobs(servers)
910 836
911 for (const server of servers) { 837 for (const server of servers) {
912 const res = await getPlaylistVideos(server.url, server.accessToken, playlistServer1UUID, 0, 10) 838 const { data: elements } = await server.playlists.listVideos({ playlistId: playlistServer1UUID, start: 0, count: 10 })
913 const elements: VideoPlaylistElement[] = res.body.data
914 const names = elements.map(v => v.video.name) 839 const names = elements.map(v => v.video.name)
915 840
916 expect(names).to.deep.equal([ 841 expect(names).to.deep.equal([
@@ -934,22 +859,18 @@ describe('Test video playlists', function () {
934 it('Should update startTimestamp/endTimestamp of some elements', async function () { 859 it('Should update startTimestamp/endTimestamp of some elements', async function () {
935 this.timeout(30000) 860 this.timeout(30000)
936 861
937 await updateVideoPlaylistElement({ 862 await commands[0].updateElement({
938 url: servers[0].url,
939 token: servers[0].accessToken,
940 playlistId: playlistServer1Id, 863 playlistId: playlistServer1Id,
941 playlistElementId: playlistElementServer1Video4, 864 elementId: playlistElementServer1Video4,
942 elementAttrs: { 865 attributes: {
943 startTimestamp: 1 866 startTimestamp: 1
944 } 867 }
945 }) 868 })
946 869
947 await updateVideoPlaylistElement({ 870 await commands[0].updateElement({
948 url: servers[0].url,
949 token: servers[0].accessToken,
950 playlistId: playlistServer1Id, 871 playlistId: playlistServer1Id,
951 playlistElementId: playlistElementServer1Video5, 872 elementId: playlistElementServer1Video5,
952 elementAttrs: { 873 attributes: {
953 stopTimestamp: null 874 stopTimestamp: null
954 } 875 }
955 }) 876 })
@@ -957,8 +878,7 @@ describe('Test video playlists', function () {
957 await waitJobs(servers) 878 await waitJobs(servers)
958 879
959 for (const server of servers) { 880 for (const server of servers) {
960 const res = await getPlaylistVideos(server.url, server.accessToken, playlistServer1UUID, 0, 10) 881 const { data: elements } = await server.playlists.listVideos({ playlistId: playlistServer1UUID, start: 0, count: 10 })
961 const elements: VideoPlaylistElement[] = res.body.data
962 882
963 expect(elements[0].video.name).to.equal('video 3 server 1') 883 expect(elements[0].video.name).to.equal('video 3 server 1')
964 expect(elements[0].position).to.equal(1) 884 expect(elements[0].position).to.equal(1)
@@ -974,17 +894,16 @@ describe('Test video playlists', function () {
974 894
975 it('Should check videos existence in my playlist', async function () { 895 it('Should check videos existence in my playlist', async function () {
976 const videoIds = [ 896 const videoIds = [
977 servers[0].videos[0].id, 897 servers[0].store.videos[0].id,
978 42000, 898 42000,
979 servers[0].videos[3].id, 899 servers[0].store.videos[3].id,
980 43000, 900 43000,
981 servers[0].videos[4].id 901 servers[0].store.videos[4].id
982 ] 902 ]
983 const res = await doVideosExistInMyPlaylist(servers[0].url, servers[0].accessToken, videoIds) 903 const obj = await commands[0].videosExist({ videoIds })
984 const obj = res.body as VideoExistInPlaylist
985 904
986 { 905 {
987 const elem = obj[servers[0].videos[0].id] 906 const elem = obj[servers[0].store.videos[0].id]
988 expect(elem).to.have.lengthOf(1) 907 expect(elem).to.have.lengthOf(1)
989 expect(elem[0].playlistElementId).to.exist 908 expect(elem[0].playlistElementId).to.exist
990 expect(elem[0].playlistId).to.equal(playlistServer1Id) 909 expect(elem[0].playlistId).to.equal(playlistServer1Id)
@@ -993,7 +912,7 @@ describe('Test video playlists', function () {
993 } 912 }
994 913
995 { 914 {
996 const elem = obj[servers[0].videos[3].id] 915 const elem = obj[servers[0].store.videos[3].id]
997 expect(elem).to.have.lengthOf(1) 916 expect(elem).to.have.lengthOf(1)
998 expect(elem[0].playlistElementId).to.equal(playlistElementServer1Video4) 917 expect(elem[0].playlistElementId).to.equal(playlistElementServer1Video4)
999 expect(elem[0].playlistId).to.equal(playlistServer1Id) 918 expect(elem[0].playlistId).to.equal(playlistServer1Id)
@@ -1002,7 +921,7 @@ describe('Test video playlists', function () {
1002 } 921 }
1003 922
1004 { 923 {
1005 const elem = obj[servers[0].videos[4].id] 924 const elem = obj[servers[0].store.videos[4].id]
1006 expect(elem).to.have.lengthOf(1) 925 expect(elem).to.have.lengthOf(1)
1007 expect(elem[0].playlistId).to.equal(playlistServer1Id) 926 expect(elem[0].playlistId).to.equal(playlistServer1Id)
1008 expect(elem[0].startTimestamp).to.equal(45) 927 expect(elem[0].startTimestamp).to.equal(45)
@@ -1015,42 +934,29 @@ describe('Test video playlists', function () {
1015 934
1016 it('Should automatically update updatedAt field of playlists', async function () { 935 it('Should automatically update updatedAt field of playlists', async function () {
1017 const server = servers[1] 936 const server = servers[1]
1018 const videoId = servers[1].videos[5].id 937 const videoId = servers[1].store.videos[5].id
1019 938
1020 async function getPlaylistNames () { 939 async function getPlaylistNames () {
1021 const res = await getAccountPlaylistsListWithToken(server.url, server.accessToken, 'root', 0, 5, undefined, '-updatedAt') 940 const { data } = await server.playlists.listByAccount({ token: server.accessToken, handle: 'root', sort: '-updatedAt' })
1022 941
1023 return (res.body.data as VideoPlaylist[]).map(p => p.displayName) 942 return data.map(p => p.displayName)
1024 } 943 }
1025 944
1026 const elementAttrs = { videoId } 945 const attributes = { videoId }
1027 const res1 = await addVideoInPlaylist({ url: server.url, token: server.accessToken, playlistId: playlistServer2Id1, elementAttrs }) 946 const element1 = await server.playlists.addElement({ playlistId: playlistServer2Id1, attributes })
1028 const res2 = await addVideoInPlaylist({ url: server.url, token: server.accessToken, playlistId: playlistServer2Id2, elementAttrs }) 947 const element2 = await server.playlists.addElement({ playlistId: playlistServer2Id2, attributes })
1029
1030 const element1 = res1.body.videoPlaylistElement.id
1031 const element2 = res2.body.videoPlaylistElement.id
1032 948
1033 const names1 = await getPlaylistNames() 949 const names1 = await getPlaylistNames()
1034 expect(names1[0]).to.equal('playlist 3 updated') 950 expect(names1[0]).to.equal('playlist 3 updated')
1035 expect(names1[1]).to.equal('playlist 2') 951 expect(names1[1]).to.equal('playlist 2')
1036 952
1037 await removeVideoFromPlaylist({ 953 await server.playlists.removeElement({ playlistId: playlistServer2Id1, elementId: element1.id })
1038 url: server.url,
1039 token: server.accessToken,
1040 playlistId: playlistServer2Id1,
1041 playlistElementId: element1
1042 })
1043 954
1044 const names2 = await getPlaylistNames() 955 const names2 = await getPlaylistNames()
1045 expect(names2[0]).to.equal('playlist 2') 956 expect(names2[0]).to.equal('playlist 2')
1046 expect(names2[1]).to.equal('playlist 3 updated') 957 expect(names2[1]).to.equal('playlist 3 updated')
1047 958
1048 await removeVideoFromPlaylist({ 959 await server.playlists.removeElement({ playlistId: playlistServer2Id2, elementId: element2.id })
1049 url: server.url,
1050 token: server.accessToken,
1051 playlistId: playlistServer2Id2,
1052 playlistElementId: element2
1053 })
1054 960
1055 const names3 = await getPlaylistNames() 961 const names3 = await getPlaylistNames()
1056 expect(names3[0]).to.equal('playlist 3 updated') 962 expect(names3[0]).to.equal('playlist 3 updated')
@@ -1060,28 +966,16 @@ describe('Test video playlists', function () {
1060 it('Should delete some elements', async function () { 966 it('Should delete some elements', async function () {
1061 this.timeout(30000) 967 this.timeout(30000)
1062 968
1063 await removeVideoFromPlaylist({ 969 await commands[0].removeElement({ playlistId: playlistServer1Id, elementId: playlistElementServer1Video4 })
1064 url: servers[0].url, 970 await commands[0].removeElement({ playlistId: playlistServer1Id, elementId: playlistElementNSFW })
1065 token: servers[0].accessToken,
1066 playlistId: playlistServer1Id,
1067 playlistElementId: playlistElementServer1Video4
1068 })
1069
1070 await removeVideoFromPlaylist({
1071 url: servers[0].url,
1072 token: servers[0].accessToken,
1073 playlistId: playlistServer1Id,
1074 playlistElementId: playlistElementNSFW
1075 })
1076 971
1077 await waitJobs(servers) 972 await waitJobs(servers)
1078 973
1079 for (const server of servers) { 974 for (const server of servers) {
1080 const res = await getPlaylistVideos(server.url, server.accessToken, playlistServer1UUID, 0, 10) 975 const body = await server.playlists.listVideos({ playlistId: playlistServer1UUID, start: 0, count: 10 })
976 expect(body.total).to.equal(6)
1081 977
1082 expect(res.body.total).to.equal(6) 978 const elements = body.data
1083
1084 const elements: VideoPlaylistElement[] = res.body.data
1085 expect(elements).to.have.lengthOf(6) 979 expect(elements).to.have.lengthOf(6)
1086 980
1087 expect(elements[0].video.name).to.equal('video 0 server 1') 981 expect(elements[0].video.name).to.equal('video 0 server 1')
@@ -1107,34 +1001,31 @@ describe('Test video playlists', function () {
1107 it('Should be able to create a public playlist, and set it to private', async function () { 1001 it('Should be able to create a public playlist, and set it to private', async function () {
1108 this.timeout(30000) 1002 this.timeout(30000)
1109 1003
1110 const res = await createVideoPlaylist({ 1004 const videoPlaylistIds = await commands[0].create({
1111 url: servers[0].url, 1005 attributes: {
1112 token: servers[0].accessToken,
1113 playlistAttrs: {
1114 displayName: 'my super public playlist', 1006 displayName: 'my super public playlist',
1115 privacy: VideoPlaylistPrivacy.PUBLIC, 1007 privacy: VideoPlaylistPrivacy.PUBLIC,
1116 videoChannelId: servers[0].videoChannel.id 1008 videoChannelId: servers[0].store.channel.id
1117 } 1009 }
1118 }) 1010 })
1119 const videoPlaylistIds = res.body.videoPlaylist
1120 1011
1121 await waitJobs(servers) 1012 await waitJobs(servers)
1122 1013
1123 for (const server of servers) { 1014 for (const server of servers) {
1124 await getVideoPlaylist(server.url, videoPlaylistIds.uuid, HttpStatusCode.OK_200) 1015 await server.playlists.get({ playlistId: videoPlaylistIds.uuid, expectedStatus: HttpStatusCode.OK_200 })
1125 } 1016 }
1126 1017
1127 const playlistAttrs = { privacy: VideoPlaylistPrivacy.PRIVATE } 1018 const attributes = { privacy: VideoPlaylistPrivacy.PRIVATE }
1128 await updateVideoPlaylist({ url: servers[0].url, token: servers[0].accessToken, playlistId: videoPlaylistIds.id, playlistAttrs }) 1019 await commands[0].update({ playlistId: videoPlaylistIds.id, attributes })
1129 1020
1130 await waitJobs(servers) 1021 await waitJobs(servers)
1131 1022
1132 for (const server of [ servers[1], servers[2] ]) { 1023 for (const server of [ servers[1], servers[2] ]) {
1133 await getVideoPlaylist(server.url, videoPlaylistIds.uuid, HttpStatusCode.NOT_FOUND_404) 1024 await server.playlists.get({ playlistId: videoPlaylistIds.uuid, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
1134 } 1025 }
1135 await getVideoPlaylist(servers[0].url, videoPlaylistIds.uuid, HttpStatusCode.UNAUTHORIZED_401)
1136 1026
1137 await getVideoPlaylistWithToken(servers[0].url, servers[0].accessToken, videoPlaylistIds.uuid, HttpStatusCode.OK_200) 1027 await commands[0].get({ playlistId: videoPlaylistIds.uuid, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
1028 await commands[0].get({ token: servers[0].accessToken, playlistId: videoPlaylistIds.uuid, expectedStatus: HttpStatusCode.OK_200 })
1138 }) 1029 })
1139 }) 1030 })
1140 1031
@@ -1143,12 +1034,12 @@ describe('Test video playlists', function () {
1143 it('Should delete the playlist on server 1 and delete on server 2 and 3', async function () { 1034 it('Should delete the playlist on server 1 and delete on server 2 and 3', async function () {
1144 this.timeout(30000) 1035 this.timeout(30000)
1145 1036
1146 await deleteVideoPlaylist(servers[0].url, servers[0].accessToken, playlistServer1Id) 1037 await commands[0].delete({ playlistId: playlistServer1Id })
1147 1038
1148 await waitJobs(servers) 1039 await waitJobs(servers)
1149 1040
1150 for (const server of servers) { 1041 for (const server of servers) {
1151 await getVideoPlaylist(server.url, playlistServer1UUID, HttpStatusCode.NOT_FOUND_404) 1042 await server.playlists.get({ playlistId: playlistServer1UUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
1152 } 1043 }
1153 }) 1044 })
1154 1045
@@ -1163,75 +1054,61 @@ describe('Test video playlists', function () {
1163 it('Should unfollow servers 1 and 2 and hide their playlists', async function () { 1054 it('Should unfollow servers 1 and 2 and hide their playlists', async function () {
1164 this.timeout(30000) 1055 this.timeout(30000)
1165 1056
1166 const finder = data => data.find(p => p.displayName === 'my super playlist') 1057 const finder = (data: VideoPlaylist[]) => data.find(p => p.displayName === 'my super playlist')
1167 1058
1168 { 1059 {
1169 const res = await getVideoPlaylistsList(servers[2].url, 0, 5) 1060 const body = await servers[2].playlists.list({ start: 0, count: 5 })
1170 expect(res.body.total).to.equal(3) 1061 expect(body.total).to.equal(3)
1171 expect(finder(res.body.data)).to.not.be.undefined 1062
1063 expect(finder(body.data)).to.not.be.undefined
1172 } 1064 }
1173 1065
1174 await unfollow(servers[2].url, servers[2].accessToken, servers[0]) 1066 await servers[2].follows.unfollow({ target: servers[0] })
1175 1067
1176 { 1068 {
1177 const res = await getVideoPlaylistsList(servers[2].url, 0, 5) 1069 const body = await servers[2].playlists.list({ start: 0, count: 5 })
1178 expect(res.body.total).to.equal(1) 1070 expect(body.total).to.equal(1)
1179 1071
1180 expect(finder(res.body.data)).to.be.undefined 1072 expect(finder(body.data)).to.be.undefined
1181 } 1073 }
1182 }) 1074 })
1183 1075
1184 it('Should delete a channel and put the associated playlist in private mode', async function () { 1076 it('Should delete a channel and put the associated playlist in private mode', async function () {
1185 this.timeout(30000) 1077 this.timeout(30000)
1186 1078
1187 const res = await addVideoChannel(servers[0].url, servers[0].accessToken, { name: 'super_channel', displayName: 'super channel' }) 1079 const channel = await servers[0].channels.create({ attributes: { name: 'super_channel', displayName: 'super channel' } })
1188 const videoChannelId = res.body.videoChannel.id
1189 1080
1190 const res2 = await createVideoPlaylist({ 1081 const playlistCreated = await commands[0].create({
1191 url: servers[0].url, 1082 attributes: {
1192 token: servers[0].accessToken,
1193 playlistAttrs: {
1194 displayName: 'channel playlist', 1083 displayName: 'channel playlist',
1195 privacy: VideoPlaylistPrivacy.PUBLIC, 1084 privacy: VideoPlaylistPrivacy.PUBLIC,
1196 videoChannelId 1085 videoChannelId: channel.id
1197 } 1086 }
1198 }) 1087 })
1199 const videoPlaylistUUID = res2.body.videoPlaylist.uuid
1200 1088
1201 await waitJobs(servers) 1089 await waitJobs(servers)
1202 1090
1203 await deleteVideoChannel(servers[0].url, servers[0].accessToken, 'super_channel') 1091 await servers[0].channels.delete({ channelName: 'super_channel' })
1204 1092
1205 await waitJobs(servers) 1093 await waitJobs(servers)
1206 1094
1207 const res3 = await getVideoPlaylistWithToken(servers[0].url, servers[0].accessToken, videoPlaylistUUID) 1095 const body = await commands[0].get({ token: servers[0].accessToken, playlistId: playlistCreated.uuid })
1208 expect(res3.body.displayName).to.equal('channel playlist') 1096 expect(body.displayName).to.equal('channel playlist')
1209 expect(res3.body.privacy.id).to.equal(VideoPlaylistPrivacy.PRIVATE) 1097 expect(body.privacy.id).to.equal(VideoPlaylistPrivacy.PRIVATE)
1210 1098
1211 await getVideoPlaylist(servers[1].url, videoPlaylistUUID, HttpStatusCode.NOT_FOUND_404) 1099 await servers[1].playlists.get({ playlistId: playlistCreated.uuid, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
1212 }) 1100 })
1213 1101
1214 it('Should delete an account and delete its playlists', async function () { 1102 it('Should delete an account and delete its playlists', async function () {
1215 this.timeout(30000) 1103 this.timeout(30000)
1216 1104
1217 const user = { username: 'user_1', password: 'password' } 1105 const { userId, token } = await servers[0].users.generate('user_1')
1218 const res = await createUser({
1219 url: servers[0].url,
1220 accessToken: servers[0].accessToken,
1221 username: user.username,
1222 password: user.password
1223 })
1224
1225 const userId = res.body.user.id
1226 const userAccessToken = await userLogin(servers[0], user)
1227 1106
1228 const resChannel = await getMyUserInformation(servers[0].url, userAccessToken) 1107 const { videoChannels } = await servers[0].users.getMyInfo({ token })
1229 const userChannel = (resChannel.body as User).videoChannels[0] 1108 const userChannel = videoChannels[0]
1230 1109
1231 await createVideoPlaylist({ 1110 await commands[0].create({
1232 url: servers[0].url, 1111 attributes: {
1233 token: userAccessToken,
1234 playlistAttrs: {
1235 displayName: 'playlist to be deleted', 1112 displayName: 'playlist to be deleted',
1236 privacy: VideoPlaylistPrivacy.PUBLIC, 1113 privacy: VideoPlaylistPrivacy.PUBLIC,
1237 videoChannelId: userChannel.id 1114 videoChannelId: userChannel.id
@@ -1240,22 +1117,24 @@ describe('Test video playlists', function () {
1240 1117
1241 await waitJobs(servers) 1118 await waitJobs(servers)
1242 1119
1243 const finder = data => data.find(p => p.displayName === 'playlist to be deleted') 1120 const finder = (data: VideoPlaylist[]) => data.find(p => p.displayName === 'playlist to be deleted')
1244 1121
1245 { 1122 {
1246 for (const server of [ servers[0], servers[1] ]) { 1123 for (const server of [ servers[0], servers[1] ]) {
1247 const res = await getVideoPlaylistsList(server.url, 0, 15) 1124 const body = await server.playlists.list({ start: 0, count: 15 })
1248 expect(finder(res.body.data)).to.not.be.undefined 1125
1126 expect(finder(body.data)).to.not.be.undefined
1249 } 1127 }
1250 } 1128 }
1251 1129
1252 await removeUser(servers[0].url, userId, servers[0].accessToken) 1130 await servers[0].users.remove({ userId })
1253 await waitJobs(servers) 1131 await waitJobs(servers)
1254 1132
1255 { 1133 {
1256 for (const server of [ servers[0], servers[1] ]) { 1134 for (const server of [ servers[0], servers[1] ]) {
1257 const res = await getVideoPlaylistsList(server.url, 0, 15) 1135 const body = await server.playlists.list({ start: 0, count: 15 })
1258 expect(finder(res.body.data)).to.be.undefined 1136
1137 expect(finder(body.data)).to.be.undefined
1259 } 1138 }
1260 } 1139 }
1261 }) 1140 })
diff --git a/server/tests/api/videos/video-privacy.ts b/server/tests/api/videos/video-privacy.ts
index 950aeb7cf..b51b3bcdd 100644
--- a/server/tests/api/videos/video-privacy.ts
+++ b/server/tests/api/videos/video-privacy.ts
@@ -2,28 +2,13 @@
2 2
3import 'mocha' 3import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes' 5import { cleanupTests, createSingleServer, doubleFollow, PeerTubeServer, setAccessTokensToServers, waitJobs } from '@shared/extra-utils'
6import { Video, VideoCreateResult } from '@shared/models' 6import { HttpStatusCode, VideoCreateResult, VideoPrivacy } from '@shared/models'
7import {
8 cleanupTests,
9 flushAndRunServer,
10 getVideosList,
11 getVideosListWithToken,
12 ServerInfo,
13 setAccessTokensToServers,
14 uploadVideo
15} from '../../../../shared/extra-utils/index'
16import { doubleFollow } from '../../../../shared/extra-utils/server/follows'
17import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
18import { userLogin } from '../../../../shared/extra-utils/users/login'
19import { createUser } from '../../../../shared/extra-utils/users/users'
20import { getMyVideos, getVideo, getVideoWithToken, updateVideo } from '../../../../shared/extra-utils/videos/videos'
21import { VideoPrivacy } from '../../../../shared/models/videos/video-privacy.enum'
22 7
23const expect = chai.expect 8const expect = chai.expect
24 9
25describe('Test video privacy', function () { 10describe('Test video privacy', function () {
26 const servers: ServerInfo[] = [] 11 const servers: PeerTubeServer[] = []
27 let anotherUserToken: string 12 let anotherUserToken: string
28 13
29 let privateVideoId: number 14 let privateVideoId: number
@@ -49,8 +34,8 @@ describe('Test video privacy', function () {
49 this.timeout(50000) 34 this.timeout(50000)
50 35
51 // Run servers 36 // Run servers
52 servers.push(await flushAndRunServer(1, dontFederateUnlistedConfig)) 37 servers.push(await createSingleServer(1, dontFederateUnlistedConfig))
53 servers.push(await flushAndRunServer(2)) 38 servers.push(await createSingleServer(2))
54 39
55 // Get the access tokens 40 // Get the access tokens
56 await setAccessTokensToServers(servers) 41 await setAccessTokensToServers(servers)
@@ -66,55 +51,53 @@ describe('Test video privacy', function () {
66 51
67 for (const privacy of [ VideoPrivacy.PRIVATE, VideoPrivacy.INTERNAL ]) { 52 for (const privacy of [ VideoPrivacy.PRIVATE, VideoPrivacy.INTERNAL ]) {
68 const attributes = { privacy } 53 const attributes = { privacy }
69 await uploadVideo(servers[0].url, servers[0].accessToken, attributes) 54 await servers[0].videos.upload({ attributes })
70 } 55 }
71 56
72 await waitJobs(servers) 57 await waitJobs(servers)
73 }) 58 })
74 59
75 it('Should not have these private and internal videos on server 2', async function () { 60 it('Should not have these private and internal videos on server 2', async function () {
76 const res = await getVideosList(servers[1].url) 61 const { total, data } = await servers[1].videos.list()
77 62
78 expect(res.body.total).to.equal(0) 63 expect(total).to.equal(0)
79 expect(res.body.data).to.have.lengthOf(0) 64 expect(data).to.have.lengthOf(0)
80 }) 65 })
81 66
82 it('Should not list the private and internal videos for an unauthenticated user on server 1', async function () { 67 it('Should not list the private and internal videos for an unauthenticated user on server 1', async function () {
83 const res = await getVideosList(servers[0].url) 68 const { total, data } = await servers[0].videos.list()
84 69
85 expect(res.body.total).to.equal(0) 70 expect(total).to.equal(0)
86 expect(res.body.data).to.have.lengthOf(0) 71 expect(data).to.have.lengthOf(0)
87 }) 72 })
88 73
89 it('Should not list the private video and list the internal video for an authenticated user on server 1', async function () { 74 it('Should not list the private video and list the internal video for an authenticated user on server 1', async function () {
90 const res = await getVideosListWithToken(servers[0].url, servers[0].accessToken) 75 const { total, data } = await servers[0].videos.listWithToken()
91 76
92 expect(res.body.total).to.equal(1) 77 expect(total).to.equal(1)
93 expect(res.body.data).to.have.lengthOf(1) 78 expect(data).to.have.lengthOf(1)
94 79
95 expect(res.body.data[0].privacy.id).to.equal(VideoPrivacy.INTERNAL) 80 expect(data[0].privacy.id).to.equal(VideoPrivacy.INTERNAL)
96 }) 81 })
97 82
98 it('Should list my (private and internal) videos', async function () { 83 it('Should list my (private and internal) videos', async function () {
99 const res = await getMyVideos(servers[0].url, servers[0].accessToken, 0, 10) 84 const { total, data } = await servers[0].videos.listMyVideos()
100 85
101 expect(res.body.total).to.equal(2) 86 expect(total).to.equal(2)
102 expect(res.body.data).to.have.lengthOf(2) 87 expect(data).to.have.lengthOf(2)
103 88
104 const videos: Video[] = res.body.data 89 const privateVideo = data.find(v => v.privacy.id === VideoPrivacy.PRIVATE)
105
106 const privateVideo = videos.find(v => v.privacy.id === VideoPrivacy.PRIVATE)
107 privateVideoId = privateVideo.id 90 privateVideoId = privateVideo.id
108 privateVideoUUID = privateVideo.uuid 91 privateVideoUUID = privateVideo.uuid
109 92
110 const internalVideo = videos.find(v => v.privacy.id === VideoPrivacy.INTERNAL) 93 const internalVideo = data.find(v => v.privacy.id === VideoPrivacy.INTERNAL)
111 internalVideoId = internalVideo.id 94 internalVideoId = internalVideo.id
112 internalVideoUUID = internalVideo.uuid 95 internalVideoUUID = internalVideo.uuid
113 }) 96 })
114 97
115 it('Should not be able to watch the private/internal video with non authenticated user', async function () { 98 it('Should not be able to watch the private/internal video with non authenticated user', async function () {
116 await getVideo(servers[0].url, privateVideoUUID, HttpStatusCode.UNAUTHORIZED_401) 99 await servers[0].videos.get({ id: privateVideoUUID, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
117 await getVideo(servers[0].url, internalVideoUUID, HttpStatusCode.UNAUTHORIZED_401) 100 await servers[0].videos.get({ id: internalVideoUUID, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
118 }) 101 })
119 102
120 it('Should not be able to watch the private video with another user', async function () { 103 it('Should not be able to watch the private video with another user', async function () {
@@ -124,18 +107,23 @@ describe('Test video privacy', function () {
124 username: 'hello', 107 username: 'hello',
125 password: 'super password' 108 password: 'super password'
126 } 109 }
127 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: user.username, password: user.password }) 110 await servers[0].users.create({ username: user.username, password: user.password })
111
112 anotherUserToken = await servers[0].login.getAccessToken(user)
128 113
129 anotherUserToken = await userLogin(servers[0], user) 114 await servers[0].videos.getWithToken({
130 await getVideoWithToken(servers[0].url, anotherUserToken, privateVideoUUID, HttpStatusCode.FORBIDDEN_403) 115 token: anotherUserToken,
116 id: privateVideoUUID,
117 expectedStatus: HttpStatusCode.FORBIDDEN_403
118 })
131 }) 119 })
132 120
133 it('Should be able to watch the internal video with another user', async function () { 121 it('Should be able to watch the internal video with another user', async function () {
134 await getVideoWithToken(servers[0].url, anotherUserToken, internalVideoUUID, HttpStatusCode.OK_200) 122 await servers[0].videos.getWithToken({ token: anotherUserToken, id: internalVideoUUID })
135 }) 123 })
136 124
137 it('Should be able to watch the private video with the correct user', async function () { 125 it('Should be able to watch the private video with the correct user', async function () {
138 await getVideoWithToken(servers[0].url, servers[0].accessToken, privateVideoUUID, HttpStatusCode.OK_200) 126 await servers[0].videos.getWithToken({ id: privateVideoUUID })
139 }) 127 })
140 }) 128 })
141 129
@@ -148,7 +136,7 @@ describe('Test video privacy', function () {
148 name: 'unlisted video', 136 name: 'unlisted video',
149 privacy: VideoPrivacy.UNLISTED 137 privacy: VideoPrivacy.UNLISTED
150 } 138 }
151 await uploadVideo(servers[1].url, servers[1].accessToken, attributes) 139 await servers[1].videos.upload({ attributes })
152 140
153 // Server 2 has transcoding enabled 141 // Server 2 has transcoding enabled
154 await waitJobs(servers) 142 await waitJobs(servers)
@@ -156,32 +144,32 @@ describe('Test video privacy', function () {
156 144
157 it('Should not have this unlisted video listed on server 1 and 2', async function () { 145 it('Should not have this unlisted video listed on server 1 and 2', async function () {
158 for (const server of servers) { 146 for (const server of servers) {
159 const res = await getVideosList(server.url) 147 const { total, data } = await server.videos.list()
160 148
161 expect(res.body.total).to.equal(0) 149 expect(total).to.equal(0)
162 expect(res.body.data).to.have.lengthOf(0) 150 expect(data).to.have.lengthOf(0)
163 } 151 }
164 }) 152 })
165 153
166 it('Should list my (unlisted) videos', async function () { 154 it('Should list my (unlisted) videos', async function () {
167 const res = await getMyVideos(servers[1].url, servers[1].accessToken, 0, 1) 155 const { total, data } = await servers[1].videos.listMyVideos()
168 156
169 expect(res.body.total).to.equal(1) 157 expect(total).to.equal(1)
170 expect(res.body.data).to.have.lengthOf(1) 158 expect(data).to.have.lengthOf(1)
171 159
172 unlistedVideo = res.body.data[0] 160 unlistedVideo = data[0]
173 }) 161 })
174 162
175 it('Should not be able to get this unlisted video using its id', async function () { 163 it('Should not be able to get this unlisted video using its id', async function () {
176 await getVideo(servers[1].url, unlistedVideo.id, 404) 164 await servers[1].videos.get({ id: unlistedVideo.id, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
177 }) 165 })
178 166
179 it('Should be able to get this unlisted video using its uuid/shortUUID', async function () { 167 it('Should be able to get this unlisted video using its uuid/shortUUID', async function () {
180 for (const server of servers) { 168 for (const server of servers) {
181 for (const id of [ unlistedVideo.uuid, unlistedVideo.shortUUID ]) { 169 for (const id of [ unlistedVideo.uuid, unlistedVideo.shortUUID ]) {
182 const res = await getVideo(server.url, id) 170 const video = await server.videos.get({ id })
183 171
184 expect(res.body.name).to.equal('unlisted video') 172 expect(video.name).to.equal('unlisted video')
185 } 173 }
186 } 174 }
187 }) 175 })
@@ -193,28 +181,28 @@ describe('Test video privacy', function () {
193 name: 'unlisted video', 181 name: 'unlisted video',
194 privacy: VideoPrivacy.UNLISTED 182 privacy: VideoPrivacy.UNLISTED
195 } 183 }
196 await uploadVideo(servers[0].url, servers[0].accessToken, attributes) 184 await servers[0].videos.upload({ attributes })
197 185
198 await waitJobs(servers) 186 await waitJobs(servers)
199 }) 187 })
200 188
201 it('Should list my new unlisted video', async function () { 189 it('Should list my new unlisted video', async function () {
202 const res = await getMyVideos(servers[0].url, servers[0].accessToken, 0, 3) 190 const { total, data } = await servers[0].videos.listMyVideos()
203 191
204 expect(res.body.total).to.equal(3) 192 expect(total).to.equal(3)
205 expect(res.body.data).to.have.lengthOf(3) 193 expect(data).to.have.lengthOf(3)
206 194
207 nonFederatedUnlistedVideoUUID = res.body.data[0].uuid 195 nonFederatedUnlistedVideoUUID = data[0].uuid
208 }) 196 })
209 197
210 it('Should be able to get non-federated unlisted video from origin', async function () { 198 it('Should be able to get non-federated unlisted video from origin', async function () {
211 const res = await getVideo(servers[0].url, nonFederatedUnlistedVideoUUID) 199 const video = await servers[0].videos.get({ id: nonFederatedUnlistedVideoUUID })
212 200
213 expect(res.body.name).to.equal('unlisted video') 201 expect(video.name).to.equal('unlisted video')
214 }) 202 })
215 203
216 it('Should not be able to get non-federated unlisted video from federated server', async function () { 204 it('Should not be able to get non-federated unlisted video from federated server', async function () {
217 await getVideo(servers[1].url, nonFederatedUnlistedVideoUUID, HttpStatusCode.NOT_FOUND_404) 205 await servers[1].videos.get({ id: nonFederatedUnlistedVideoUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
218 }) 206 })
219 }) 207 })
220 208
@@ -226,20 +214,20 @@ describe('Test video privacy', function () {
226 now = Date.now() 214 now = Date.now()
227 215
228 { 216 {
229 const attribute = { 217 const attributes = {
230 name: 'private video becomes public', 218 name: 'private video becomes public',
231 privacy: VideoPrivacy.PUBLIC 219 privacy: VideoPrivacy.PUBLIC
232 } 220 }
233 221
234 await updateVideo(servers[0].url, servers[0].accessToken, privateVideoId, attribute) 222 await servers[0].videos.update({ id: privateVideoId, attributes })
235 } 223 }
236 224
237 { 225 {
238 const attribute = { 226 const attributes = {
239 name: 'internal video becomes public', 227 name: 'internal video becomes public',
240 privacy: VideoPrivacy.PUBLIC 228 privacy: VideoPrivacy.PUBLIC
241 } 229 }
242 await updateVideo(servers[0].url, servers[0].accessToken, internalVideoId, attribute) 230 await servers[0].videos.update({ id: internalVideoId, attributes })
243 } 231 }
244 232
245 await waitJobs(servers) 233 await waitJobs(servers)
@@ -247,13 +235,12 @@ describe('Test video privacy', function () {
247 235
248 it('Should have this new public video listed on server 1 and 2', async function () { 236 it('Should have this new public video listed on server 1 and 2', async function () {
249 for (const server of servers) { 237 for (const server of servers) {
250 const res = await getVideosList(server.url) 238 const { total, data } = await server.videos.list()
251 expect(res.body.total).to.equal(2) 239 expect(total).to.equal(2)
252 expect(res.body.data).to.have.lengthOf(2) 240 expect(data).to.have.lengthOf(2)
253 241
254 const videos: Video[] = res.body.data 242 const privateVideo = data.find(v => v.name === 'private video becomes public')
255 const privateVideo = videos.find(v => v.name === 'private video becomes public') 243 const internalVideo = data.find(v => v.name === 'internal video becomes public')
256 const internalVideo = videos.find(v => v.name === 'internal video becomes public')
257 244
258 expect(privateVideo).to.not.be.undefined 245 expect(privateVideo).to.not.be.undefined
259 expect(internalVideo).to.not.be.undefined 246 expect(internalVideo).to.not.be.undefined
@@ -270,27 +257,25 @@ describe('Test video privacy', function () {
270 it('Should set these videos as private and internal', async function () { 257 it('Should set these videos as private and internal', async function () {
271 this.timeout(10000) 258 this.timeout(10000)
272 259
273 await updateVideo(servers[0].url, servers[0].accessToken, internalVideoId, { privacy: VideoPrivacy.PRIVATE }) 260 await servers[0].videos.update({ id: internalVideoId, attributes: { privacy: VideoPrivacy.PRIVATE } })
274 await updateVideo(servers[0].url, servers[0].accessToken, privateVideoId, { privacy: VideoPrivacy.INTERNAL }) 261 await servers[0].videos.update({ id: privateVideoId, attributes: { privacy: VideoPrivacy.INTERNAL } })
275 262
276 await waitJobs(servers) 263 await waitJobs(servers)
277 264
278 for (const server of servers) { 265 for (const server of servers) {
279 const res = await getVideosList(server.url) 266 const { total, data } = await server.videos.list()
280 267
281 expect(res.body.total).to.equal(0) 268 expect(total).to.equal(0)
282 expect(res.body.data).to.have.lengthOf(0) 269 expect(data).to.have.lengthOf(0)
283 } 270 }
284 271
285 { 272 {
286 const res = await getMyVideos(servers[0].url, servers[0].accessToken, 0, 5) 273 const { total, data } = await servers[0].videos.listMyVideos()
287 const videos = res.body.data 274 expect(total).to.equal(3)
288 275 expect(data).to.have.lengthOf(3)
289 expect(res.body.total).to.equal(3)
290 expect(videos).to.have.lengthOf(3)
291 276
292 const privateVideo = videos.find(v => v.name === 'private video becomes public') 277 const privateVideo = data.find(v => v.name === 'private video becomes public')
293 const internalVideo = videos.find(v => v.name === 'internal video becomes public') 278 const internalVideo = data.find(v => v.name === 'internal video becomes public')
294 279
295 expect(privateVideo).to.not.be.undefined 280 expect(privateVideo).to.not.be.undefined
296 expect(internalVideo).to.not.be.undefined 281 expect(internalVideo).to.not.be.undefined
diff --git a/server/tests/api/videos/video-schedule-update.ts b/server/tests/api/videos/video-schedule-update.ts
index 204f43611..3f7738784 100644
--- a/server/tests/api/videos/video-schedule-update.ts
+++ b/server/tests/api/videos/video-schedule-update.ts
@@ -1,22 +1,17 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2 2
3import * as chai from 'chai'
4import 'mocha' 3import 'mocha'
5import { VideoPrivacy } from '../../../../shared/models/videos' 4import * as chai from 'chai'
6import { 5import {
7 cleanupTests, 6 cleanupTests,
7 createMultipleServers,
8 doubleFollow, 8 doubleFollow,
9 flushAndRunMultipleServers, 9 PeerTubeServer,
10 getMyVideos,
11 getVideosList,
12 getVideoWithToken,
13 ServerInfo,
14 setAccessTokensToServers, 10 setAccessTokensToServers,
15 updateVideo, 11 wait,
16 uploadVideo, 12 waitJobs
17 wait 13} from '@shared/extra-utils'
18} from '../../../../shared/extra-utils' 14import { VideoPrivacy } from '@shared/models'
19import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
20 15
21const expect = chai.expect 16const expect = chai.expect
22 17
@@ -28,14 +23,14 @@ function in10Seconds () {
28} 23}
29 24
30describe('Test video update scheduler', function () { 25describe('Test video update scheduler', function () {
31 let servers: ServerInfo[] = [] 26 let servers: PeerTubeServer[] = []
32 let video2UUID: string 27 let video2UUID: string
33 28
34 before(async function () { 29 before(async function () {
35 this.timeout(30000) 30 this.timeout(30000)
36 31
37 // Run servers 32 // Run servers
38 servers = await flushAndRunMultipleServers(2) 33 servers = await createMultipleServers(2)
39 34
40 await setAccessTokensToServers(servers) 35 await setAccessTokensToServers(servers)
41 36
@@ -45,35 +40,34 @@ describe('Test video update scheduler', function () {
45 it('Should upload a video and schedule an update in 10 seconds', async function () { 40 it('Should upload a video and schedule an update in 10 seconds', async function () {
46 this.timeout(10000) 41 this.timeout(10000)
47 42
48 const videoAttributes = { 43 const attributes = {
49 name: 'video 1', 44 name: 'video 1',
50 privacy: VideoPrivacy.PRIVATE, 45 privacy: VideoPrivacy.PRIVATE,
51 scheduleUpdate: { 46 scheduleUpdate: {
52 updateAt: in10Seconds().toISOString(), 47 updateAt: in10Seconds().toISOString(),
53 privacy: VideoPrivacy.PUBLIC 48 privacy: VideoPrivacy.PUBLIC as VideoPrivacy.PUBLIC
54 } 49 }
55 } 50 }
56 51
57 await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes) 52 await servers[0].videos.upload({ attributes })
58 53
59 await waitJobs(servers) 54 await waitJobs(servers)
60 }) 55 })
61 56
62 it('Should not list the video (in privacy mode)', async function () { 57 it('Should not list the video (in privacy mode)', async function () {
63 for (const server of servers) { 58 for (const server of servers) {
64 const res = await getVideosList(server.url) 59 const { total } = await server.videos.list()
65 60
66 expect(res.body.total).to.equal(0) 61 expect(total).to.equal(0)
67 } 62 }
68 }) 63 })
69 64
70 it('Should have my scheduled video in my account videos', async function () { 65 it('Should have my scheduled video in my account videos', async function () {
71 const res = await getMyVideos(servers[0].url, servers[0].accessToken, 0, 5) 66 const { total, data } = await servers[0].videos.listMyVideos()
72 expect(res.body.total).to.equal(1) 67 expect(total).to.equal(1)
73 68
74 const videoFromList = res.body.data[0] 69 const videoFromList = data[0]
75 const res2 = await getVideoWithToken(servers[0].url, servers[0].accessToken, videoFromList.uuid) 70 const videoFromGet = await servers[0].videos.getWithToken({ id: videoFromList.uuid })
76 const videoFromGet = res2.body
77 71
78 for (const video of [ videoFromList, videoFromGet ]) { 72 for (const video of [ videoFromList, videoFromGet ]) {
79 expect(video.name).to.equal('video 1') 73 expect(video.name).to.equal('video 1')
@@ -90,23 +84,23 @@ describe('Test video update scheduler', function () {
90 await waitJobs(servers) 84 await waitJobs(servers)
91 85
92 for (const server of servers) { 86 for (const server of servers) {
93 const res = await getVideosList(server.url) 87 const { total, data } = await server.videos.list()
94 88
95 expect(res.body.total).to.equal(1) 89 expect(total).to.equal(1)
96 expect(res.body.data[0].name).to.equal('video 1') 90 expect(data[0].name).to.equal('video 1')
97 } 91 }
98 }) 92 })
99 93
100 it('Should upload a video without scheduling an update', async function () { 94 it('Should upload a video without scheduling an update', async function () {
101 this.timeout(10000) 95 this.timeout(10000)
102 96
103 const videoAttributes = { 97 const attributes = {
104 name: 'video 2', 98 name: 'video 2',
105 privacy: VideoPrivacy.PRIVATE 99 privacy: VideoPrivacy.PRIVATE
106 } 100 }
107 101
108 const res = await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes) 102 const { uuid } = await servers[0].videos.upload({ attributes })
109 video2UUID = res.body.video.uuid 103 video2UUID = uuid
110 104
111 await waitJobs(servers) 105 await waitJobs(servers)
112 }) 106 })
@@ -114,31 +108,31 @@ describe('Test video update scheduler', function () {
114 it('Should update a video by scheduling an update', async function () { 108 it('Should update a video by scheduling an update', async function () {
115 this.timeout(10000) 109 this.timeout(10000)
116 110
117 const videoAttributes = { 111 const attributes = {
118 name: 'video 2 updated', 112 name: 'video 2 updated',
119 scheduleUpdate: { 113 scheduleUpdate: {
120 updateAt: in10Seconds().toISOString(), 114 updateAt: in10Seconds().toISOString(),
121 privacy: VideoPrivacy.PUBLIC 115 privacy: VideoPrivacy.PUBLIC as VideoPrivacy.PUBLIC
122 } 116 }
123 } 117 }
124 118
125 await updateVideo(servers[0].url, servers[0].accessToken, video2UUID, videoAttributes) 119 await servers[0].videos.update({ id: video2UUID, attributes })
126 await waitJobs(servers) 120 await waitJobs(servers)
127 }) 121 })
128 122
129 it('Should not display the updated video', async function () { 123 it('Should not display the updated video', async function () {
130 for (const server of servers) { 124 for (const server of servers) {
131 const res = await getVideosList(server.url) 125 const { total } = await server.videos.list()
132 126
133 expect(res.body.total).to.equal(1) 127 expect(total).to.equal(1)
134 } 128 }
135 }) 129 })
136 130
137 it('Should have my scheduled updated video in my account videos', async function () { 131 it('Should have my scheduled updated video in my account videos', async function () {
138 const res = await getMyVideos(servers[0].url, servers[0].accessToken, 0, 5) 132 const { total, data } = await servers[0].videos.listMyVideos()
139 expect(res.body.total).to.equal(2) 133 expect(total).to.equal(2)
140 134
141 const video = res.body.data.find(v => v.uuid === video2UUID) 135 const video = data.find(v => v.uuid === video2UUID)
142 expect(video).not.to.be.undefined 136 expect(video).not.to.be.undefined
143 137
144 expect(video.name).to.equal('video 2 updated') 138 expect(video.name).to.equal('video 2 updated')
@@ -155,11 +149,10 @@ describe('Test video update scheduler', function () {
155 await waitJobs(servers) 149 await waitJobs(servers)
156 150
157 for (const server of servers) { 151 for (const server of servers) {
158 const res = await getVideosList(server.url) 152 const { total, data } = await server.videos.list()
159 153 expect(total).to.equal(2)
160 expect(res.body.total).to.equal(2)
161 154
162 const video = res.body.data.find(v => v.uuid === video2UUID) 155 const video = data.find(v => v.uuid === video2UUID)
163 expect(video).not.to.be.undefined 156 expect(video).not.to.be.undefined
164 expect(video.name).to.equal('video 2 updated') 157 expect(video.name).to.equal('video 2 updated')
165 } 158 }
diff --git a/server/tests/api/videos/video-transcoder.ts b/server/tests/api/videos/video-transcoder.ts
index ea5ffd239..e4892bb24 100644
--- a/server/tests/api/videos/video-transcoder.ts
+++ b/server/tests/api/videos/video-transcoder.ts
@@ -2,36 +2,23 @@
2 2
3import 'mocha' 3import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5import { FfprobeData } from 'fluent-ffmpeg'
6import { omit } from 'lodash' 5import { omit } from 'lodash'
7import { join } from 'path' 6import { join } from 'path'
8import { Job } from '@shared/models'
9import { VIDEO_TRANSCODING_FPS } from '../../../../server/initializers/constants'
10import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
11import { 7import {
12 buildAbsoluteFixturePath, 8 buildAbsoluteFixturePath,
13 buildServerDirectory,
14 cleanupTests, 9 cleanupTests,
10 createMultipleServers,
15 doubleFollow, 11 doubleFollow,
16 flushAndRunMultipleServers,
17 generateHighBitrateVideo, 12 generateHighBitrateVideo,
18 generateVideoWithFramerate, 13 generateVideoWithFramerate,
19 getJobsListPaginationAndSort,
20 getMyVideos,
21 getServerFileSize,
22 getVideo,
23 getVideoFileMetadataUrl,
24 getVideosList,
25 makeGetRequest, 14 makeGetRequest,
26 ServerInfo, 15 PeerTubeServer,
27 setAccessTokensToServers, 16 setAccessTokensToServers,
28 updateCustomSubConfig,
29 uploadVideo,
30 uploadVideoAndGetId,
31 waitJobs, 17 waitJobs,
32 webtorrentAdd 18 webtorrentAdd
33} from '../../../../shared/extra-utils' 19} from '@shared/extra-utils'
34import { getMaxBitrate, VideoDetails, VideoResolution, VideoState } from '../../../../shared/models/videos' 20import { getMaxBitrate, HttpStatusCode, VideoResolution, VideoState } from '@shared/models'
21import { VIDEO_TRANSCODING_FPS } from '../../../../server/initializers/constants'
35import { 22import {
36 canDoQuickTranscode, 23 canDoQuickTranscode,
37 getAudioStream, 24 getAudioStream,
@@ -43,37 +30,39 @@ import {
43 30
44const expect = chai.expect 31const expect = chai.expect
45 32
46function updateConfigForTranscoding (server: ServerInfo) { 33function updateConfigForTranscoding (server: PeerTubeServer) {
47 return updateCustomSubConfig(server.url, server.accessToken, { 34 return server.config.updateCustomSubConfig({
48 transcoding: { 35 newConfig: {
49 enabled: true, 36 transcoding: {
50 allowAdditionalExtensions: true, 37 enabled: true,
51 allowAudioFiles: true, 38 allowAdditionalExtensions: true,
52 hls: { enabled: true }, 39 allowAudioFiles: true,
53 webtorrent: { enabled: true }, 40 hls: { enabled: true },
54 resolutions: { 41 webtorrent: { enabled: true },
55 '0p': false, 42 resolutions: {
56 '240p': true, 43 '0p': false,
57 '360p': true, 44 '240p': true,
58 '480p': true, 45 '360p': true,
59 '720p': true, 46 '480p': true,
60 '1080p': true, 47 '720p': true,
61 '1440p': true, 48 '1080p': true,
62 '2160p': true 49 '1440p': true,
50 '2160p': true
51 }
63 } 52 }
64 } 53 }
65 }) 54 })
66} 55}
67 56
68describe('Test video transcoding', function () { 57describe('Test video transcoding', function () {
69 let servers: ServerInfo[] = [] 58 let servers: PeerTubeServer[] = []
70 let video4k: string 59 let video4k: string
71 60
72 before(async function () { 61 before(async function () {
73 this.timeout(30_000) 62 this.timeout(30_000)
74 63
75 // Run servers 64 // Run servers
76 servers = await flushAndRunMultipleServers(2) 65 servers = await createMultipleServers(2)
77 66
78 await setAccessTokensToServers(servers) 67 await setAccessTokensToServers(servers)
79 68
@@ -87,21 +76,20 @@ describe('Test video transcoding', function () {
87 it('Should not transcode video on server 1', async function () { 76 it('Should not transcode video on server 1', async function () {
88 this.timeout(60_000) 77 this.timeout(60_000)
89 78
90 const videoAttributes = { 79 const attributes = {
91 name: 'my super name for server 1', 80 name: 'my super name for server 1',
92 description: 'my super description for server 1', 81 description: 'my super description for server 1',
93 fixture: 'video_short.webm' 82 fixture: 'video_short.webm'
94 } 83 }
95 await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes) 84 await servers[0].videos.upload({ attributes })
96 85
97 await waitJobs(servers) 86 await waitJobs(servers)
98 87
99 for (const server of servers) { 88 for (const server of servers) {
100 const res = await getVideosList(server.url) 89 const { data } = await server.videos.list()
101 const video = res.body.data[0] 90 const video = data[0]
102 91
103 const res2 = await getVideo(server.url, video.id) 92 const videoDetails = await server.videos.get({ id: video.id })
104 const videoDetails = res2.body
105 expect(videoDetails.files).to.have.lengthOf(1) 93 expect(videoDetails.files).to.have.lengthOf(1)
106 94
107 const magnetUri = videoDetails.files[0].magnetUri 95 const magnetUri = videoDetails.files[0].magnetUri
@@ -117,21 +105,20 @@ describe('Test video transcoding', function () {
117 it('Should transcode video on server 2', async function () { 105 it('Should transcode video on server 2', async function () {
118 this.timeout(120_000) 106 this.timeout(120_000)
119 107
120 const videoAttributes = { 108 const attributes = {
121 name: 'my super name for server 2', 109 name: 'my super name for server 2',
122 description: 'my super description for server 2', 110 description: 'my super description for server 2',
123 fixture: 'video_short.webm' 111 fixture: 'video_short.webm'
124 } 112 }
125 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes) 113 await servers[1].videos.upload({ attributes })
126 114
127 await waitJobs(servers) 115 await waitJobs(servers)
128 116
129 for (const server of servers) { 117 for (const server of servers) {
130 const res = await getVideosList(server.url) 118 const { data } = await server.videos.list()
131 119
132 const video = res.body.data.find(v => v.name === videoAttributes.name) 120 const video = data.find(v => v.name === attributes.name)
133 const res2 = await getVideo(server.url, video.id) 121 const videoDetails = await server.videos.get({ id: video.id })
134 const videoDetails = res2.body
135 122
136 expect(videoDetails.files).to.have.lengthOf(4) 123 expect(videoDetails.files).to.have.lengthOf(4)
137 124
@@ -150,47 +137,50 @@ describe('Test video transcoding', function () {
150 137
151 { 138 {
152 // Upload the video, but wait transcoding 139 // Upload the video, but wait transcoding
153 const videoAttributes = { 140 const attributes = {
154 name: 'waiting video', 141 name: 'waiting video',
155 fixture: 'video_short1.webm', 142 fixture: 'video_short1.webm',
156 waitTranscoding: true 143 waitTranscoding: true
157 } 144 }
158 const resVideo = await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes) 145 const { uuid } = await servers[1].videos.upload({ attributes })
159 const videoId = resVideo.body.video.uuid 146 const videoId = uuid
160 147
161 // Should be in transcode state 148 // Should be in transcode state
162 const { body } = await getVideo(servers[1].url, videoId) 149 const body = await servers[1].videos.get({ id: videoId })
163 expect(body.name).to.equal('waiting video') 150 expect(body.name).to.equal('waiting video')
164 expect(body.state.id).to.equal(VideoState.TO_TRANSCODE) 151 expect(body.state.id).to.equal(VideoState.TO_TRANSCODE)
165 expect(body.state.label).to.equal('To transcode') 152 expect(body.state.label).to.equal('To transcode')
166 expect(body.waitTranscoding).to.be.true 153 expect(body.waitTranscoding).to.be.true
167 154
168 // Should have my video 155 {
169 const resMyVideos = await getMyVideos(servers[1].url, servers[1].accessToken, 0, 10) 156 // Should have my video
170 const videoToFindInMine = resMyVideos.body.data.find(v => v.name === videoAttributes.name) 157 const { data } = await servers[1].videos.listMyVideos()
171 expect(videoToFindInMine).not.to.be.undefined 158 const videoToFindInMine = data.find(v => v.name === attributes.name)
172 expect(videoToFindInMine.state.id).to.equal(VideoState.TO_TRANSCODE) 159 expect(videoToFindInMine).not.to.be.undefined
173 expect(videoToFindInMine.state.label).to.equal('To transcode') 160 expect(videoToFindInMine.state.id).to.equal(VideoState.TO_TRANSCODE)
174 expect(videoToFindInMine.waitTranscoding).to.be.true 161 expect(videoToFindInMine.state.label).to.equal('To transcode')
162 expect(videoToFindInMine.waitTranscoding).to.be.true
163 }
175 164
176 // Should not list this video 165 {
177 const resVideos = await getVideosList(servers[1].url) 166 // Should not list this video
178 const videoToFindInList = resVideos.body.data.find(v => v.name === videoAttributes.name) 167 const { data } = await servers[1].videos.list()
179 expect(videoToFindInList).to.be.undefined 168 const videoToFindInList = data.find(v => v.name === attributes.name)
169 expect(videoToFindInList).to.be.undefined
170 }
180 171
181 // Server 1 should not have the video yet 172 // Server 1 should not have the video yet
182 await getVideo(servers[0].url, videoId, HttpStatusCode.NOT_FOUND_404) 173 await servers[0].videos.get({ id: videoId, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
183 } 174 }
184 175
185 await waitJobs(servers) 176 await waitJobs(servers)
186 177
187 for (const server of servers) { 178 for (const server of servers) {
188 const res = await getVideosList(server.url) 179 const { data } = await server.videos.list()
189 const videoToFind = res.body.data.find(v => v.name === 'waiting video') 180 const videoToFind = data.find(v => v.name === 'waiting video')
190 expect(videoToFind).not.to.be.undefined 181 expect(videoToFind).not.to.be.undefined
191 182
192 const res2 = await getVideo(server.url, videoToFind.id) 183 const videoDetails = await server.videos.get({ id: videoToFind.id })
193 const videoDetails: VideoDetails = res2.body
194 184
195 expect(videoDetails.state.id).to.equal(VideoState.PUBLISHED) 185 expect(videoDetails.state.id).to.equal(VideoState.PUBLISHED)
196 expect(videoDetails.state.label).to.equal('Published') 186 expect(videoDetails.state.label).to.equal('Published')
@@ -211,22 +201,20 @@ describe('Test video transcoding', function () {
211 } 201 }
212 202
213 for (const fixture of [ 'video_short.mkv', 'video_short.avi' ]) { 203 for (const fixture of [ 'video_short.mkv', 'video_short.avi' ]) {
214 const videoAttributes = { 204 const attributes = {
215 name: fixture, 205 name: fixture,
216 fixture 206 fixture
217 } 207 }
218 208
219 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes) 209 await servers[1].videos.upload({ attributes })
220 210
221 await waitJobs(servers) 211 await waitJobs(servers)
222 212
223 for (const server of servers) { 213 for (const server of servers) {
224 const res = await getVideosList(server.url) 214 const { data } = await server.videos.list()
225
226 const video = res.body.data.find(v => v.name === videoAttributes.name)
227 const res2 = await getVideo(server.url, video.id)
228 const videoDetails = res2.body
229 215
216 const video = data.find(v => v.name === attributes.name)
217 const videoDetails = await server.videos.get({ id: video.id })
230 expect(videoDetails.files).to.have.lengthOf(4) 218 expect(videoDetails.files).to.have.lengthOf(4)
231 219
232 const magnetUri = videoDetails.files[0].magnetUri 220 const magnetUri = videoDetails.files[0].magnetUri
@@ -238,22 +226,20 @@ describe('Test video transcoding', function () {
238 it('Should transcode a 4k video', async function () { 226 it('Should transcode a 4k video', async function () {
239 this.timeout(200_000) 227 this.timeout(200_000)
240 228
241 const videoAttributes = { 229 const attributes = {
242 name: '4k video', 230 name: '4k video',
243 fixture: 'video_short_4k.mp4' 231 fixture: 'video_short_4k.mp4'
244 } 232 }
245 233
246 const resUpload = await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes) 234 const { uuid } = await servers[1].videos.upload({ attributes })
247 video4k = resUpload.body.video.uuid 235 video4k = uuid
248 236
249 await waitJobs(servers) 237 await waitJobs(servers)
250 238
251 const resolutions = [ 240, 360, 480, 720, 1080, 1440, 2160 ] 239 const resolutions = [ 240, 360, 480, 720, 1080, 1440, 2160 ]
252 240
253 for (const server of servers) { 241 for (const server of servers) {
254 const res = await getVideo(server.url, video4k) 242 const videoDetails = await server.videos.get({ id: video4k })
255 const videoDetails: VideoDetails = res.body
256
257 expect(videoDetails.files).to.have.lengthOf(resolutions.length) 243 expect(videoDetails.files).to.have.lengthOf(resolutions.length)
258 244
259 for (const r of resolutions) { 245 for (const r of resolutions) {
@@ -269,24 +255,23 @@ describe('Test video transcoding', function () {
269 it('Should transcode high bit rate mp3 to proper bit rate', async function () { 255 it('Should transcode high bit rate mp3 to proper bit rate', async function () {
270 this.timeout(60_000) 256 this.timeout(60_000)
271 257
272 const videoAttributes = { 258 const attributes = {
273 name: 'mp3_256k', 259 name: 'mp3_256k',
274 fixture: 'video_short_mp3_256k.mp4' 260 fixture: 'video_short_mp3_256k.mp4'
275 } 261 }
276 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes) 262 await servers[1].videos.upload({ attributes })
277 263
278 await waitJobs(servers) 264 await waitJobs(servers)
279 265
280 for (const server of servers) { 266 for (const server of servers) {
281 const res = await getVideosList(server.url) 267 const { data } = await server.videos.list()
282 268
283 const video = res.body.data.find(v => v.name === videoAttributes.name) 269 const video = data.find(v => v.name === attributes.name)
284 const res2 = await getVideo(server.url, video.id) 270 const videoDetails = await server.videos.get({ id: video.id })
285 const videoDetails: VideoDetails = res2.body
286 271
287 expect(videoDetails.files).to.have.lengthOf(4) 272 expect(videoDetails.files).to.have.lengthOf(4)
288 273
289 const path = buildServerDirectory(servers[1], join('videos', video.uuid + '-240.mp4')) 274 const path = servers[1].servers.buildDirectory(join('videos', video.uuid + '-240.mp4'))
290 const probe = await getAudioStream(path) 275 const probe = await getAudioStream(path)
291 276
292 if (probe.audioStream) { 277 if (probe.audioStream) {
@@ -301,23 +286,22 @@ describe('Test video transcoding', function () {
301 it('Should transcode video with no audio and have no audio itself', async function () { 286 it('Should transcode video with no audio and have no audio itself', async function () {
302 this.timeout(60_000) 287 this.timeout(60_000)
303 288
304 const videoAttributes = { 289 const attributes = {
305 name: 'no_audio', 290 name: 'no_audio',
306 fixture: 'video_short_no_audio.mp4' 291 fixture: 'video_short_no_audio.mp4'
307 } 292 }
308 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes) 293 await servers[1].videos.upload({ attributes })
309 294
310 await waitJobs(servers) 295 await waitJobs(servers)
311 296
312 for (const server of servers) { 297 for (const server of servers) {
313 const res = await getVideosList(server.url) 298 const { data } = await server.videos.list()
314 299
315 const video = res.body.data.find(v => v.name === videoAttributes.name) 300 const video = data.find(v => v.name === attributes.name)
316 const res2 = await getVideo(server.url, video.id) 301 const videoDetails = await server.videos.get({ id: video.id })
317 const videoDetails: VideoDetails = res2.body
318 302
319 expect(videoDetails.files).to.have.lengthOf(4) 303 expect(videoDetails.files).to.have.lengthOf(4)
320 const path = buildServerDirectory(servers[1], join('videos', video.uuid + '-240.mp4')) 304 const path = servers[1].servers.buildDirectory(join('videos', video.uuid + '-240.mp4'))
321 const probe = await getAudioStream(path) 305 const probe = await getAudioStream(path)
322 expect(probe).to.not.have.property('audioStream') 306 expect(probe).to.not.have.property('audioStream')
323 } 307 }
@@ -326,26 +310,25 @@ describe('Test video transcoding', function () {
326 it('Should leave the audio untouched, but properly transcode the video', async function () { 310 it('Should leave the audio untouched, but properly transcode the video', async function () {
327 this.timeout(60_000) 311 this.timeout(60_000)
328 312
329 const videoAttributes = { 313 const attributes = {
330 name: 'untouched_audio', 314 name: 'untouched_audio',
331 fixture: 'video_short.mp4' 315 fixture: 'video_short.mp4'
332 } 316 }
333 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes) 317 await servers[1].videos.upload({ attributes })
334 318
335 await waitJobs(servers) 319 await waitJobs(servers)
336 320
337 for (const server of servers) { 321 for (const server of servers) {
338 const res = await getVideosList(server.url) 322 const { data } = await server.videos.list()
339 323
340 const video = res.body.data.find(v => v.name === videoAttributes.name) 324 const video = data.find(v => v.name === attributes.name)
341 const res2 = await getVideo(server.url, video.id) 325 const videoDetails = await server.videos.get({ id: video.id })
342 const videoDetails: VideoDetails = res2.body
343 326
344 expect(videoDetails.files).to.have.lengthOf(4) 327 expect(videoDetails.files).to.have.lengthOf(4)
345 328
346 const fixturePath = buildAbsoluteFixturePath(videoAttributes.fixture) 329 const fixturePath = buildAbsoluteFixturePath(attributes.fixture)
347 const fixtureVideoProbe = await getAudioStream(fixturePath) 330 const fixtureVideoProbe = await getAudioStream(fixturePath)
348 const path = buildServerDirectory(servers[1], join('videos', video.uuid + '-240.mp4')) 331 const path = servers[1].servers.buildDirectory(join('videos', video.uuid + '-240.mp4'))
349 332
350 const videoProbe = await getAudioStream(path) 333 const videoProbe = await getAudioStream(path)
351 334
@@ -364,19 +347,21 @@ describe('Test video transcoding', function () {
364 function runSuite (mode: 'legacy' | 'resumable') { 347 function runSuite (mode: 'legacy' | 'resumable') {
365 348
366 before(async function () { 349 before(async function () {
367 await updateCustomSubConfig(servers[1].url, servers[1].accessToken, { 350 await servers[1].config.updateCustomSubConfig({
368 transcoding: { 351 newConfig: {
369 hls: { enabled: true }, 352 transcoding: {
370 webtorrent: { enabled: true }, 353 hls: { enabled: true },
371 resolutions: { 354 webtorrent: { enabled: true },
372 '0p': false, 355 resolutions: {
373 '240p': false, 356 '0p': false,
374 '360p': false, 357 '240p': false,
375 '480p': false, 358 '360p': false,
376 '720p': false, 359 '480p': false,
377 '1080p': false, 360 '720p': false,
378 '1440p': false, 361 '1080p': false,
379 '2160p': false 362 '1440p': false,
363 '2160p': false
364 }
380 } 365 }
381 } 366 }
382 }) 367 })
@@ -385,22 +370,21 @@ describe('Test video transcoding', function () {
385 it('Should merge an audio file with the preview file', async function () { 370 it('Should merge an audio file with the preview file', async function () {
386 this.timeout(60_000) 371 this.timeout(60_000)
387 372
388 const videoAttributesArg = { name: 'audio_with_preview', previewfile: 'preview.jpg', fixture: 'sample.ogg' } 373 const attributes = { name: 'audio_with_preview', previewfile: 'preview.jpg', fixture: 'sample.ogg' }
389 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributesArg, HttpStatusCode.OK_200, mode) 374 await servers[1].videos.upload({ attributes, mode })
390 375
391 await waitJobs(servers) 376 await waitJobs(servers)
392 377
393 for (const server of servers) { 378 for (const server of servers) {
394 const res = await getVideosList(server.url) 379 const { data } = await server.videos.list()
395 380
396 const video = res.body.data.find(v => v.name === 'audio_with_preview') 381 const video = data.find(v => v.name === 'audio_with_preview')
397 const res2 = await getVideo(server.url, video.id) 382 const videoDetails = await server.videos.get({ id: video.id })
398 const videoDetails: VideoDetails = res2.body
399 383
400 expect(videoDetails.files).to.have.lengthOf(1) 384 expect(videoDetails.files).to.have.lengthOf(1)
401 385
402 await makeGetRequest({ url: server.url, path: videoDetails.thumbnailPath, statusCodeExpected: HttpStatusCode.OK_200 }) 386 await makeGetRequest({ url: server.url, path: videoDetails.thumbnailPath, expectedStatus: HttpStatusCode.OK_200 })
403 await makeGetRequest({ url: server.url, path: videoDetails.previewPath, statusCodeExpected: HttpStatusCode.OK_200 }) 387 await makeGetRequest({ url: server.url, path: videoDetails.previewPath, expectedStatus: HttpStatusCode.OK_200 })
404 388
405 const magnetUri = videoDetails.files[0].magnetUri 389 const magnetUri = videoDetails.files[0].magnetUri
406 expect(magnetUri).to.contain('.mp4') 390 expect(magnetUri).to.contain('.mp4')
@@ -410,22 +394,21 @@ describe('Test video transcoding', function () {
410 it('Should upload an audio file and choose a default background image', async function () { 394 it('Should upload an audio file and choose a default background image', async function () {
411 this.timeout(60_000) 395 this.timeout(60_000)
412 396
413 const videoAttributesArg = { name: 'audio_without_preview', fixture: 'sample.ogg' } 397 const attributes = { name: 'audio_without_preview', fixture: 'sample.ogg' }
414 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributesArg, HttpStatusCode.OK_200, mode) 398 await servers[1].videos.upload({ attributes, mode })
415 399
416 await waitJobs(servers) 400 await waitJobs(servers)
417 401
418 for (const server of servers) { 402 for (const server of servers) {
419 const res = await getVideosList(server.url) 403 const { data } = await server.videos.list()
420 404
421 const video = res.body.data.find(v => v.name === 'audio_without_preview') 405 const video = data.find(v => v.name === 'audio_without_preview')
422 const res2 = await getVideo(server.url, video.id) 406 const videoDetails = await server.videos.get({ id: video.id })
423 const videoDetails = res2.body
424 407
425 expect(videoDetails.files).to.have.lengthOf(1) 408 expect(videoDetails.files).to.have.lengthOf(1)
426 409
427 await makeGetRequest({ url: server.url, path: videoDetails.thumbnailPath, statusCodeExpected: HttpStatusCode.OK_200 }) 410 await makeGetRequest({ url: server.url, path: videoDetails.thumbnailPath, expectedStatus: HttpStatusCode.OK_200 })
428 await makeGetRequest({ url: server.url, path: videoDetails.previewPath, statusCodeExpected: HttpStatusCode.OK_200 }) 411 await makeGetRequest({ url: server.url, path: videoDetails.previewPath, expectedStatus: HttpStatusCode.OK_200 })
429 412
430 const magnetUri = videoDetails.files[0].magnetUri 413 const magnetUri = videoDetails.files[0].magnetUri
431 expect(magnetUri).to.contain('.mp4') 414 expect(magnetUri).to.contain('.mp4')
@@ -435,26 +418,27 @@ describe('Test video transcoding', function () {
435 it('Should upload an audio file and create an audio version only', async function () { 418 it('Should upload an audio file and create an audio version only', async function () {
436 this.timeout(60_000) 419 this.timeout(60_000)
437 420
438 await updateCustomSubConfig(servers[1].url, servers[1].accessToken, { 421 await servers[1].config.updateCustomSubConfig({
439 transcoding: { 422 newConfig: {
440 hls: { enabled: true }, 423 transcoding: {
441 webtorrent: { enabled: true }, 424 hls: { enabled: true },
442 resolutions: { 425 webtorrent: { enabled: true },
443 '0p': true, 426 resolutions: {
444 '240p': false, 427 '0p': true,
445 '360p': false 428 '240p': false,
429 '360p': false
430 }
446 } 431 }
447 } 432 }
448 }) 433 })
449 434
450 const videoAttributesArg = { name: 'audio_with_preview', previewfile: 'preview.jpg', fixture: 'sample.ogg' } 435 const attributes = { name: 'audio_with_preview', previewfile: 'preview.jpg', fixture: 'sample.ogg' }
451 const resVideo = await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributesArg, HttpStatusCode.OK_200, mode) 436 const { id } = await servers[1].videos.upload({ attributes, mode })
452 437
453 await waitJobs(servers) 438 await waitJobs(servers)
454 439
455 for (const server of servers) { 440 for (const server of servers) {
456 const res2 = await getVideo(server.url, resVideo.body.video.id) 441 const videoDetails = await server.videos.get({ id })
457 const videoDetails: VideoDetails = res2.body
458 442
459 for (const files of [ videoDetails.files, videoDetails.streamingPlaylists[0].files ]) { 443 for (const files of [ videoDetails.files, videoDetails.streamingPlaylists[0].files ]) {
460 expect(files).to.have.lengthOf(2) 444 expect(files).to.have.lengthOf(2)
@@ -480,21 +464,20 @@ describe('Test video transcoding', function () {
480 it('Should transcode a 60 FPS video', async function () { 464 it('Should transcode a 60 FPS video', async function () {
481 this.timeout(60_000) 465 this.timeout(60_000)
482 466
483 const videoAttributes = { 467 const attributes = {
484 name: 'my super 30fps name for server 2', 468 name: 'my super 30fps name for server 2',
485 description: 'my super 30fps description for server 2', 469 description: 'my super 30fps description for server 2',
486 fixture: '60fps_720p_small.mp4' 470 fixture: '60fps_720p_small.mp4'
487 } 471 }
488 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes) 472 await servers[1].videos.upload({ attributes })
489 473
490 await waitJobs(servers) 474 await waitJobs(servers)
491 475
492 for (const server of servers) { 476 for (const server of servers) {
493 const res = await getVideosList(server.url) 477 const { data } = await server.videos.list()
494 478
495 const video = res.body.data.find(v => v.name === videoAttributes.name) 479 const video = data.find(v => v.name === attributes.name)
496 const res2 = await getVideo(server.url, video.id) 480 const videoDetails = await server.videos.get({ id: video.id })
497 const videoDetails: VideoDetails = res2.body
498 481
499 expect(videoDetails.files).to.have.lengthOf(4) 482 expect(videoDetails.files).to.have.lengthOf(4)
500 expect(videoDetails.files[0].fps).to.be.above(58).and.below(62) 483 expect(videoDetails.files[0].fps).to.be.above(58).and.below(62)
@@ -503,13 +486,13 @@ describe('Test video transcoding', function () {
503 expect(videoDetails.files[3].fps).to.be.below(31) 486 expect(videoDetails.files[3].fps).to.be.below(31)
504 487
505 for (const resolution of [ '240', '360', '480' ]) { 488 for (const resolution of [ '240', '360', '480' ]) {
506 const path = buildServerDirectory(servers[1], join('videos', video.uuid + '-' + resolution + '.mp4')) 489 const path = servers[1].servers.buildDirectory(join('videos', video.uuid + '-' + resolution + '.mp4'))
507 const fps = await getVideoFileFPS(path) 490 const fps = await getVideoFileFPS(path)
508 491
509 expect(fps).to.be.below(31) 492 expect(fps).to.be.below(31)
510 } 493 }
511 494
512 const path = buildServerDirectory(servers[1], join('videos', video.uuid + '-720.mp4')) 495 const path = servers[1].servers.buildDirectory(join('videos', video.uuid + '-720.mp4'))
513 const fps = await getVideoFileFPS(path) 496 const fps = await getVideoFileFPS(path)
514 497
515 expect(fps).to.be.above(58).and.below(62) 498 expect(fps).to.be.above(58).and.below(62)
@@ -528,29 +511,29 @@ describe('Test video transcoding', function () {
528 expect(fps).to.be.equal(59) 511 expect(fps).to.be.equal(59)
529 } 512 }
530 513
531 const videoAttributes = { 514 const attributes = {
532 name: '59fps video', 515 name: '59fps video',
533 description: '59fps video', 516 description: '59fps video',
534 fixture: tempFixturePath 517 fixture: tempFixturePath
535 } 518 }
536 519
537 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes) 520 await servers[1].videos.upload({ attributes })
538 521
539 await waitJobs(servers) 522 await waitJobs(servers)
540 523
541 for (const server of servers) { 524 for (const server of servers) {
542 const res = await getVideosList(server.url) 525 const { data } = await server.videos.list()
543 526
544 const video = res.body.data.find(v => v.name === videoAttributes.name) 527 const video = data.find(v => v.name === attributes.name)
545 528
546 { 529 {
547 const path = buildServerDirectory(servers[1], join('videos', video.uuid + '-240.mp4')) 530 const path = servers[1].servers.buildDirectory(join('videos', video.uuid + '-240.mp4'))
548 const fps = await getVideoFileFPS(path) 531 const fps = await getVideoFileFPS(path)
549 expect(fps).to.be.equal(25) 532 expect(fps).to.be.equal(25)
550 } 533 }
551 534
552 { 535 {
553 const path = buildServerDirectory(servers[1], join('videos', video.uuid + '-720.mp4')) 536 const path = servers[1].servers.buildDirectory(join('videos', video.uuid + '-720.mp4'))
554 const fps = await getVideoFileFPS(path) 537 const fps = await getVideoFileFPS(path)
555 expect(fps).to.be.equal(59) 538 expect(fps).to.be.equal(59)
556 } 539 }
@@ -571,23 +554,23 @@ describe('Test video transcoding', function () {
571 expect(bitrate).to.be.above(getMaxBitrate(VideoResolution.H_1080P, 25, VIDEO_TRANSCODING_FPS)) 554 expect(bitrate).to.be.above(getMaxBitrate(VideoResolution.H_1080P, 25, VIDEO_TRANSCODING_FPS))
572 } 555 }
573 556
574 const videoAttributes = { 557 const attributes = {
575 name: 'high bitrate video', 558 name: 'high bitrate video',
576 description: 'high bitrate video', 559 description: 'high bitrate video',
577 fixture: tempFixturePath 560 fixture: tempFixturePath
578 } 561 }
579 562
580 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes) 563 await servers[1].videos.upload({ attributes })
581 564
582 await waitJobs(servers) 565 await waitJobs(servers)
583 566
584 for (const server of servers) { 567 for (const server of servers) {
585 const res = await getVideosList(server.url) 568 const { data } = await server.videos.list()
586 569
587 const video = res.body.data.find(v => v.name === videoAttributes.name) 570 const video = data.find(v => v.name === attributes.name)
588 571
589 for (const resolution of [ '240', '360', '480', '720', '1080' ]) { 572 for (const resolution of [ '240', '360', '480', '720', '1080' ]) {
590 const path = buildServerDirectory(servers[1], join('videos', video.uuid + '-' + resolution + '.mp4')) 573 const path = servers[1].servers.buildDirectory(join('videos', video.uuid + '-' + resolution + '.mp4'))
591 574
592 const bitrate = await getVideoFileBitrate(path) 575 const bitrate = await getVideoFileBitrate(path)
593 const fps = await getVideoFileFPS(path) 576 const fps = await getVideoFileFPS(path)
@@ -602,7 +585,7 @@ describe('Test video transcoding', function () {
602 it('Should not transcode to an higher bitrate than the original file', async function () { 585 it('Should not transcode to an higher bitrate than the original file', async function () {
603 this.timeout(160_000) 586 this.timeout(160_000)
604 587
605 const config = { 588 const newConfig = {
606 transcoding: { 589 transcoding: {
607 enabled: true, 590 enabled: true,
608 resolutions: { 591 resolutions: {
@@ -618,22 +601,21 @@ describe('Test video transcoding', function () {
618 hls: { enabled: true } 601 hls: { enabled: true }
619 } 602 }
620 } 603 }
621 await updateCustomSubConfig(servers[1].url, servers[1].accessToken, config) 604 await servers[1].config.updateCustomSubConfig({ newConfig })
622 605
623 const videoAttributes = { 606 const attributes = {
624 name: 'low bitrate', 607 name: 'low bitrate',
625 fixture: 'low-bitrate.mp4' 608 fixture: 'low-bitrate.mp4'
626 } 609 }
627 610
628 const resUpload = await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes) 611 const { uuid } = await servers[1].videos.upload({ attributes })
629 const videoUUID = resUpload.body.video.uuid
630 612
631 await waitJobs(servers) 613 await waitJobs(servers)
632 614
633 const resolutions = [ 240, 360, 480, 720, 1080 ] 615 const resolutions = [ 240, 360, 480, 720, 1080 ]
634 for (const r of resolutions) { 616 for (const r of resolutions) {
635 const path = `videos/${videoUUID}-${r}.mp4` 617 const path = `videos/${uuid}-${r}.mp4`
636 const size = await getServerFileSize(servers[1], path) 618 const size = await servers[1].servers.getServerFileSize(path)
637 expect(size, `${path} not below ${60_000}`).to.be.below(60_000) 619 expect(size, `${path} not below ${60_000}`).to.be.below(60_000)
638 } 620 }
639 }) 621 })
@@ -644,11 +626,11 @@ describe('Test video transcoding', function () {
644 it('Should provide valid ffprobe data', async function () { 626 it('Should provide valid ffprobe data', async function () {
645 this.timeout(160_000) 627 this.timeout(160_000)
646 628
647 const videoUUID = (await uploadVideoAndGetId({ server: servers[1], videoName: 'ffprobe data' })).uuid 629 const videoUUID = (await servers[1].videos.quickUpload({ name: 'ffprobe data' })).uuid
648 await waitJobs(servers) 630 await waitJobs(servers)
649 631
650 { 632 {
651 const path = buildServerDirectory(servers[1], join('videos', videoUUID + '-240.mp4')) 633 const path = servers[1].servers.buildDirectory(join('videos', videoUUID + '-240.mp4'))
652 const metadata = await getMetadataFromFile(path) 634 const metadata = await getMetadataFromFile(path)
653 635
654 // expected format properties 636 // expected format properties
@@ -678,8 +660,7 @@ describe('Test video transcoding', function () {
678 } 660 }
679 661
680 for (const server of servers) { 662 for (const server of servers) {
681 const res2 = await getVideo(server.url, videoUUID) 663 const videoDetails = await server.videos.get({ id: videoUUID })
682 const videoDetails: VideoDetails = res2.body
683 664
684 const videoFiles = videoDetails.files 665 const videoFiles = videoDetails.files
685 .concat(videoDetails.streamingPlaylists[0].files) 666 .concat(videoDetails.streamingPlaylists[0].files)
@@ -691,8 +672,7 @@ describe('Test video transcoding', function () {
691 expect(file.metadataUrl).to.contain(servers[1].url) 672 expect(file.metadataUrl).to.contain(servers[1].url)
692 expect(file.metadataUrl).to.contain(videoUUID) 673 expect(file.metadataUrl).to.contain(videoUUID)
693 674
694 const res3 = await getVideoFileMetadataUrl(file.metadataUrl) 675 const metadata = await server.videos.getFileMetadata({ url: file.metadataUrl })
695 const metadata: FfprobeData = res3.body
696 expect(metadata).to.have.nested.property('format.size') 676 expect(metadata).to.have.nested.property('format.size')
697 } 677 }
698 } 678 }
@@ -709,17 +689,14 @@ describe('Test video transcoding', function () {
709 describe('Transcoding job queue', function () { 689 describe('Transcoding job queue', function () {
710 690
711 it('Should have the appropriate priorities for transcoding jobs', async function () { 691 it('Should have the appropriate priorities for transcoding jobs', async function () {
712 const res = await getJobsListPaginationAndSort({ 692 const body = await servers[1].jobs.getJobsList({
713 url: servers[1].url,
714 accessToken: servers[1].accessToken,
715 start: 0, 693 start: 0,
716 count: 100, 694 count: 100,
717 sort: '-createdAt', 695 sort: '-createdAt',
718 jobType: 'video-transcoding' 696 jobType: 'video-transcoding'
719 }) 697 })
720 698
721 const jobs = res.body.data as Job[] 699 const jobs = body.data
722
723 const transcodingJobs = jobs.filter(j => j.data.videoUUID === video4k) 700 const transcodingJobs = jobs.filter(j => j.data.videoUUID === video4k)
724 701
725 expect(transcodingJobs).to.have.lengthOf(14) 702 expect(transcodingJobs).to.have.lengthOf(14)
diff --git a/server/tests/api/videos/videos-filter.ts b/server/tests/api/videos/videos-filter.ts
index 7428b82c5..2306807bf 100644
--- a/server/tests/api/videos/videos-filter.ts
+++ b/server/tests/api/videos/videos-filter.ts
@@ -1,25 +1,18 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2 2
3import * as chai from 'chai'
4import 'mocha' 3import 'mocha'
4import { expect } from 'chai'
5import { 5import {
6 cleanupTests, 6 cleanupTests,
7 createUser, 7 createMultipleServers,
8 doubleFollow, 8 doubleFollow,
9 flushAndRunMultipleServers,
10 makeGetRequest, 9 makeGetRequest,
11 ServerInfo, 10 PeerTubeServer,
12 setAccessTokensToServers, 11 setAccessTokensToServers
13 uploadVideo, 12} from '@shared/extra-utils'
14 userLogin 13import { HttpStatusCode, UserRole, Video, VideoPrivacy } from '@shared/models'
15} from '../../../../shared/extra-utils' 14
16import { Video, VideoPrivacy } from '../../../../shared/models/videos' 15async function getVideosNames (server: PeerTubeServer, token: string, filter: string, expectedStatus = HttpStatusCode.OK_200) {
17import { UserRole } from '../../../../shared/models/users'
18import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
19
20const expect = chai.expect
21
22async function getVideosNames (server: ServerInfo, token: string, filter: string, statusCodeExpected = HttpStatusCode.OK_200) {
23 const paths = [ 16 const paths = [
24 '/api/v1/video-channels/root_channel/videos', 17 '/api/v1/video-channels/root_channel/videos',
25 '/api/v1/accounts/root/videos', 18 '/api/v1/accounts/root/videos',
@@ -38,7 +31,7 @@ async function getVideosNames (server: ServerInfo, token: string, filter: string
38 sort: 'createdAt', 31 sort: 'createdAt',
39 filter 32 filter
40 }, 33 },
41 statusCodeExpected 34 expectedStatus
42 }) 35 })
43 36
44 videosResults.push(res.body.data.map(v => v.name)) 37 videosResults.push(res.body.data.map(v => v.name))
@@ -48,42 +41,32 @@ async function getVideosNames (server: ServerInfo, token: string, filter: string
48} 41}
49 42
50describe('Test videos filter', function () { 43describe('Test videos filter', function () {
51 let servers: ServerInfo[] 44 let servers: PeerTubeServer[]
52 45
53 // --------------------------------------------------------------- 46 // ---------------------------------------------------------------
54 47
55 before(async function () { 48 before(async function () {
56 this.timeout(160000) 49 this.timeout(160000)
57 50
58 servers = await flushAndRunMultipleServers(2) 51 servers = await createMultipleServers(2)
59 52
60 await setAccessTokensToServers(servers) 53 await setAccessTokensToServers(servers)
61 54
62 for (const server of servers) { 55 for (const server of servers) {
63 const moderator = { username: 'moderator', password: 'my super password' } 56 const moderator = { username: 'moderator', password: 'my super password' }
64 await createUser( 57 await server.users.create({ username: moderator.username, password: moderator.password, role: UserRole.MODERATOR })
65 { 58 server['moderatorAccessToken'] = await server.login.getAccessToken(moderator)
66 url: server.url,
67 accessToken: server.accessToken,
68 username: moderator.username,
69 password: moderator.password,
70 videoQuota: undefined,
71 videoQuotaDaily: undefined,
72 role: UserRole.MODERATOR
73 }
74 )
75 server['moderatorAccessToken'] = await userLogin(server, moderator)
76 59
77 await uploadVideo(server.url, server.accessToken, { name: 'public ' + server.serverNumber }) 60 await server.videos.upload({ attributes: { name: 'public ' + server.serverNumber } })
78 61
79 { 62 {
80 const attributes = { name: 'unlisted ' + server.serverNumber, privacy: VideoPrivacy.UNLISTED } 63 const attributes = { name: 'unlisted ' + server.serverNumber, privacy: VideoPrivacy.UNLISTED }
81 await uploadVideo(server.url, server.accessToken, attributes) 64 await server.videos.upload({ attributes })
82 } 65 }
83 66
84 { 67 {
85 const attributes = { name: 'private ' + server.serverNumber, privacy: VideoPrivacy.PRIVATE } 68 const attributes = { name: 'private ' + server.serverNumber, privacy: VideoPrivacy.PRIVATE }
86 await uploadVideo(server.url, server.accessToken, attributes) 69 await server.videos.upload({ attributes })
87 } 70 }
88 } 71 }
89 72
diff --git a/server/tests/api/videos/videos-history.ts b/server/tests/api/videos/videos-history.ts
index b25cff879..e4bc0bb3a 100644
--- a/server/tests/api/videos/videos-history.ts
+++ b/server/tests/api/videos/videos-history.ts
@@ -1,74 +1,66 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2 2
3import * as chai from 'chai'
4import 'mocha' 3import 'mocha'
4import * as chai from 'chai'
5import { 5import {
6 cleanupTests, 6 cleanupTests,
7 createUser, 7 createSingleServer,
8 flushAndRunServer, 8 HistoryCommand,
9 getVideosListWithToken,
10 getVideoWithToken,
11 killallServers, 9 killallServers,
12 reRunServer, 10 PeerTubeServer,
13 searchVideoWithToken,
14 ServerInfo,
15 setAccessTokensToServers, 11 setAccessTokensToServers,
16 updateMyUser,
17 uploadVideo,
18 userLogin,
19 wait 12 wait
20} from '../../../../shared/extra-utils' 13} from '@shared/extra-utils'
21import { Video, VideoDetails } from '../../../../shared/models/videos' 14import { HttpStatusCode, Video } from '@shared/models'
22import { listMyVideosHistory, removeMyVideosHistory, userWatchVideo } from '../../../../shared/extra-utils/videos/video-history'
23import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
24 15
25const expect = chai.expect 16const expect = chai.expect
26 17
27describe('Test videos history', function () { 18describe('Test videos history', function () {
28 let server: ServerInfo = null 19 let server: PeerTubeServer = null
29 let video1UUID: string 20 let video1UUID: string
30 let video2UUID: string 21 let video2UUID: string
31 let video3UUID: string 22 let video3UUID: string
32 let video3WatchedDate: Date 23 let video3WatchedDate: Date
33 let userAccessToken: string 24 let userAccessToken: string
25 let command: HistoryCommand
34 26
35 before(async function () { 27 before(async function () {
36 this.timeout(30000) 28 this.timeout(30000)
37 29
38 server = await flushAndRunServer(1) 30 server = await createSingleServer(1)
39 31
40 await setAccessTokensToServers([ server ]) 32 await setAccessTokensToServers([ server ])
41 33
34 command = server.history
35
42 { 36 {
43 const res = await uploadVideo(server.url, server.accessToken, { name: 'video 1' }) 37 const { uuid } = await server.videos.upload({ attributes: { name: 'video 1' } })
44 video1UUID = res.body.video.uuid 38 video1UUID = uuid
45 } 39 }
46 40
47 { 41 {
48 const res = await uploadVideo(server.url, server.accessToken, { name: 'video 2' }) 42 const { uuid } = await server.videos.upload({ attributes: { name: 'video 2' } })
49 video2UUID = res.body.video.uuid 43 video2UUID = uuid
50 } 44 }
51 45
52 { 46 {
53 const res = await uploadVideo(server.url, server.accessToken, { name: 'video 3' }) 47 const { uuid } = await server.videos.upload({ attributes: { name: 'video 3' } })
54 video3UUID = res.body.video.uuid 48 video3UUID = uuid
55 } 49 }
56 50
57 const user = { 51 const user = {
58 username: 'user_1', 52 username: 'user_1',
59 password: 'super password' 53 password: 'super password'
60 } 54 }
61 await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password }) 55 await server.users.create({ username: user.username, password: user.password })
62 userAccessToken = await userLogin(server, user) 56 userAccessToken = await server.login.getAccessToken(user)
63 }) 57 })
64 58
65 it('Should get videos, without watching history', async function () { 59 it('Should get videos, without watching history', async function () {
66 const res = await getVideosListWithToken(server.url, server.accessToken) 60 const { data } = await server.videos.listWithToken()
67 const videos: Video[] = res.body.data
68 61
69 for (const video of videos) { 62 for (const video of data) {
70 const resDetail = await getVideoWithToken(server.url, server.accessToken, video.id) 63 const videoDetails = await server.videos.getWithToken({ id: video.id })
71 const videoDetails: VideoDetails = resDetail.body
72 64
73 expect(video.userHistory).to.be.undefined 65 expect(video.userHistory).to.be.undefined
74 expect(videoDetails.userHistory).to.be.undefined 66 expect(videoDetails.userHistory).to.be.undefined
@@ -76,21 +68,21 @@ describe('Test videos history', function () {
76 }) 68 })
77 69
78 it('Should watch the first and second video', async function () { 70 it('Should watch the first and second video', async function () {
79 await userWatchVideo(server.url, server.accessToken, video2UUID, 8) 71 await command.wathVideo({ videoId: video2UUID, currentTime: 8 })
80 await userWatchVideo(server.url, server.accessToken, video1UUID, 3) 72 await command.wathVideo({ videoId: video1UUID, currentTime: 3 })
81 }) 73 })
82 74
83 it('Should return the correct history when listing, searching and getting videos', async function () { 75 it('Should return the correct history when listing, searching and getting videos', async function () {
84 const videosOfVideos: Video[][] = [] 76 const videosOfVideos: Video[][] = []
85 77
86 { 78 {
87 const res = await getVideosListWithToken(server.url, server.accessToken) 79 const { data } = await server.videos.listWithToken()
88 videosOfVideos.push(res.body.data) 80 videosOfVideos.push(data)
89 } 81 }
90 82
91 { 83 {
92 const res = await searchVideoWithToken(server.url, 'video', server.accessToken) 84 const body = await server.search.searchVideos({ token: server.accessToken, search: 'video' })
93 videosOfVideos.push(res.body.data) 85 videosOfVideos.push(body.data)
94 } 86 }
95 87
96 for (const videos of videosOfVideos) { 88 for (const videos of videosOfVideos) {
@@ -108,24 +100,21 @@ describe('Test videos history', function () {
108 } 100 }
109 101
110 { 102 {
111 const resDetail = await getVideoWithToken(server.url, server.accessToken, video1UUID) 103 const videoDetails = await server.videos.getWithToken({ id: video1UUID })
112 const videoDetails: VideoDetails = resDetail.body
113 104
114 expect(videoDetails.userHistory).to.not.be.undefined 105 expect(videoDetails.userHistory).to.not.be.undefined
115 expect(videoDetails.userHistory.currentTime).to.equal(3) 106 expect(videoDetails.userHistory.currentTime).to.equal(3)
116 } 107 }
117 108
118 { 109 {
119 const resDetail = await getVideoWithToken(server.url, server.accessToken, video2UUID) 110 const videoDetails = await server.videos.getWithToken({ id: video2UUID })
120 const videoDetails: VideoDetails = resDetail.body
121 111
122 expect(videoDetails.userHistory).to.not.be.undefined 112 expect(videoDetails.userHistory).to.not.be.undefined
123 expect(videoDetails.userHistory.currentTime).to.equal(8) 113 expect(videoDetails.userHistory.currentTime).to.equal(8)
124 } 114 }
125 115
126 { 116 {
127 const resDetail = await getVideoWithToken(server.url, server.accessToken, video3UUID) 117 const videoDetails = await server.videos.getWithToken({ id: video3UUID })
128 const videoDetails: VideoDetails = resDetail.body
129 118
130 expect(videoDetails.userHistory).to.be.undefined 119 expect(videoDetails.userHistory).to.be.undefined
131 } 120 }
@@ -133,71 +122,64 @@ describe('Test videos history', function () {
133 122
134 it('Should have these videos when listing my history', async function () { 123 it('Should have these videos when listing my history', async function () {
135 video3WatchedDate = new Date() 124 video3WatchedDate = new Date()
136 await userWatchVideo(server.url, server.accessToken, video3UUID, 2) 125 await command.wathVideo({ videoId: video3UUID, currentTime: 2 })
137 126
138 const res = await listMyVideosHistory(server.url, server.accessToken) 127 const body = await command.list()
139 128
140 expect(res.body.total).to.equal(3) 129 expect(body.total).to.equal(3)
141 130
142 const videos: Video[] = res.body.data 131 const videos = body.data
143 expect(videos[0].name).to.equal('video 3') 132 expect(videos[0].name).to.equal('video 3')
144 expect(videos[1].name).to.equal('video 1') 133 expect(videos[1].name).to.equal('video 1')
145 expect(videos[2].name).to.equal('video 2') 134 expect(videos[2].name).to.equal('video 2')
146 }) 135 })
147 136
148 it('Should not have videos history on another user', async function () { 137 it('Should not have videos history on another user', async function () {
149 const res = await listMyVideosHistory(server.url, userAccessToken) 138 const body = await command.list({ token: userAccessToken })
150 139
151 expect(res.body.total).to.equal(0) 140 expect(body.total).to.equal(0)
152 expect(res.body.data).to.have.lengthOf(0) 141 expect(body.data).to.have.lengthOf(0)
153 }) 142 })
154 143
155 it('Should be able to search through videos in my history', async function () { 144 it('Should be able to search through videos in my history', async function () {
156 const res = await listMyVideosHistory(server.url, server.accessToken, '2') 145 const body = await command.list({ search: '2' })
157 146 expect(body.total).to.equal(1)
158 expect(res.body.total).to.equal(1)
159 147
160 const videos: Video[] = res.body.data 148 const videos = body.data
161 expect(videos[0].name).to.equal('video 2') 149 expect(videos[0].name).to.equal('video 2')
162 }) 150 })
163 151
164 it('Should clear my history', async function () { 152 it('Should clear my history', async function () {
165 await removeMyVideosHistory(server.url, server.accessToken, video3WatchedDate.toISOString()) 153 await command.remove({ beforeDate: video3WatchedDate.toISOString() })
166 }) 154 })
167 155
168 it('Should have my history cleared', async function () { 156 it('Should have my history cleared', async function () {
169 const res = await listMyVideosHistory(server.url, server.accessToken) 157 const body = await command.list()
158 expect(body.total).to.equal(1)
170 159
171 expect(res.body.total).to.equal(1) 160 const videos = body.data
172
173 const videos: Video[] = res.body.data
174 expect(videos[0].name).to.equal('video 3') 161 expect(videos[0].name).to.equal('video 3')
175 }) 162 })
176 163
177 it('Should disable videos history', async function () { 164 it('Should disable videos history', async function () {
178 await updateMyUser({ 165 await server.users.updateMe({
179 url: server.url,
180 accessToken: server.accessToken,
181 videosHistoryEnabled: false 166 videosHistoryEnabled: false
182 }) 167 })
183 168
184 await userWatchVideo(server.url, server.accessToken, video2UUID, 8, HttpStatusCode.CONFLICT_409) 169 await command.wathVideo({ videoId: video2UUID, currentTime: 8, expectedStatus: HttpStatusCode.CONFLICT_409 })
185 }) 170 })
186 171
187 it('Should re-enable videos history', async function () { 172 it('Should re-enable videos history', async function () {
188 await updateMyUser({ 173 await server.users.updateMe({
189 url: server.url,
190 accessToken: server.accessToken,
191 videosHistoryEnabled: true 174 videosHistoryEnabled: true
192 }) 175 })
193 176
194 await userWatchVideo(server.url, server.accessToken, video1UUID, 8) 177 await command.wathVideo({ videoId: video1UUID, currentTime: 8 })
195
196 const res = await listMyVideosHistory(server.url, server.accessToken)
197 178
198 expect(res.body.total).to.equal(2) 179 const body = await command.list()
180 expect(body.total).to.equal(2)
199 181
200 const videos: Video[] = res.body.data 182 const videos = body.data
201 expect(videos[0].name).to.equal('video 1') 183 expect(videos[0].name).to.equal('video 1')
202 expect(videos[1].name).to.equal('video 3') 184 expect(videos[1].name).to.equal('video 3')
203 }) 185 })
@@ -205,30 +187,29 @@ describe('Test videos history', function () {
205 it('Should not clean old history', async function () { 187 it('Should not clean old history', async function () {
206 this.timeout(50000) 188 this.timeout(50000)
207 189
208 killallServers([ server ]) 190 await killallServers([ server ])
209 191
210 await reRunServer(server, { history: { videos: { max_age: '10 days' } } }) 192 await server.run({ history: { videos: { max_age: '10 days' } } })
211 193
212 await wait(6000) 194 await wait(6000)
213 195
214 // Should still have history 196 // Should still have history
215 197
216 const res = await listMyVideosHistory(server.url, server.accessToken) 198 const body = await command.list()
217 199 expect(body.total).to.equal(2)
218 expect(res.body.total).to.equal(2)
219 }) 200 })
220 201
221 it('Should clean old history', async function () { 202 it('Should clean old history', async function () {
222 this.timeout(50000) 203 this.timeout(50000)
223 204
224 killallServers([ server ]) 205 await killallServers([ server ])
225 206
226 await reRunServer(server, { history: { videos: { max_age: '5 seconds' } } }) 207 await server.run({ history: { videos: { max_age: '5 seconds' } } })
227 208
228 await wait(6000) 209 await wait(6000)
229 210
230 const res = await listMyVideosHistory(server.url, server.accessToken) 211 const body = await command.list()
231 expect(res.body.total).to.equal(0) 212 expect(body.total).to.equal(0)
232 }) 213 })
233 214
234 after(async function () { 215 after(async function () {
diff --git a/server/tests/api/videos/videos-overview.ts b/server/tests/api/videos/videos-overview.ts
index c266a1dc5..70aa66549 100644
--- a/server/tests/api/videos/videos-overview.ts
+++ b/server/tests/api/videos/videos-overview.ts
@@ -2,29 +2,15 @@
2 2
3import 'mocha' 3import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5 5import { cleanupTests, createSingleServer, PeerTubeServer, setAccessTokensToServers, wait } from '@shared/extra-utils'
6import { 6import { VideosOverview } from '@shared/models'
7 cleanupTests,
8 flushAndRunServer,
9 generateUserAccessToken,
10 ServerInfo,
11 setAccessTokensToServers,
12 uploadVideo,
13 wait
14} from '../../../../shared/extra-utils'
15import { getVideosOverview, getVideosOverviewWithToken } from '../../../../shared/extra-utils/overviews/overviews'
16import { VideosOverview } from '../../../../shared/models/overviews'
17import { addAccountToAccountBlocklist } from '@shared/extra-utils/users/blocklist'
18import { Response } from 'superagent'
19 7
20const expect = chai.expect 8const expect = chai.expect
21 9
22describe('Test a videos overview', function () { 10describe('Test a videos overview', function () {
23 let server: ServerInfo = null 11 let server: PeerTubeServer = null
24
25 function testOverviewCount (res: Response, expected: number) {
26 const overview: VideosOverview = res.body
27 12
13 function testOverviewCount (overview: VideosOverview, expected: number) {
28 expect(overview.tags).to.have.lengthOf(expected) 14 expect(overview.tags).to.have.lengthOf(expected)
29 expect(overview.categories).to.have.lengthOf(expected) 15 expect(overview.categories).to.have.lengthOf(expected)
30 expect(overview.channels).to.have.lengthOf(expected) 16 expect(overview.channels).to.have.lengthOf(expected)
@@ -33,15 +19,15 @@ describe('Test a videos overview', function () {
33 before(async function () { 19 before(async function () {
34 this.timeout(30000) 20 this.timeout(30000)
35 21
36 server = await flushAndRunServer(1) 22 server = await createSingleServer(1)
37 23
38 await setAccessTokensToServers([ server ]) 24 await setAccessTokensToServers([ server ])
39 }) 25 })
40 26
41 it('Should send empty overview', async function () { 27 it('Should send empty overview', async function () {
42 const res = await getVideosOverview(server.url, 1) 28 const body = await server.overviews.getVideos({ page: 1 })
43 29
44 testOverviewCount(res, 0) 30 testOverviewCount(body, 0)
45 }) 31 })
46 32
47 it('Should upload 5 videos in a specific category, tag and channel but not include them in overview', async function () { 33 it('Should upload 5 videos in a specific category, tag and channel but not include them in overview', async function () {
@@ -49,40 +35,45 @@ describe('Test a videos overview', function () {
49 35
50 await wait(3000) 36 await wait(3000)
51 37
52 await uploadVideo(server.url, server.accessToken, { 38 await server.videos.upload({
53 name: 'video 0', 39 attributes: {
54 category: 3, 40 name: 'video 0',
55 tags: [ 'coucou1', 'coucou2' ] 41 category: 3,
42 tags: [ 'coucou1', 'coucou2' ]
43 }
56 }) 44 })
57 45
58 const res = await getVideosOverview(server.url, 1) 46 const body = await server.overviews.getVideos({ page: 1 })
59 47
60 testOverviewCount(res, 0) 48 testOverviewCount(body, 0)
61 }) 49 })
62 50
63 it('Should upload another video and include all videos in the overview', async function () { 51 it('Should upload another video and include all videos in the overview', async function () {
64 this.timeout(30000) 52 this.timeout(30000)
65 53
66 for (let i = 1; i < 6; i++) { 54 {
67 await uploadVideo(server.url, server.accessToken, { 55 for (let i = 1; i < 6; i++) {
68 name: 'video ' + i, 56 await server.videos.upload({
69 category: 3, 57 attributes: {
70 tags: [ 'coucou1', 'coucou2' ] 58 name: 'video ' + i,
71 }) 59 category: 3,
60 tags: [ 'coucou1', 'coucou2' ]
61 }
62 })
63 }
64
65 await wait(3000)
72 } 66 }
73 67
74 await wait(3000)
75
76 { 68 {
77 const res = await getVideosOverview(server.url, 1) 69 const body = await server.overviews.getVideos({ page: 1 })
78 70
79 testOverviewCount(res, 1) 71 testOverviewCount(body, 1)
80 } 72 }
81 73
82 { 74 {
83 const res = await getVideosOverview(server.url, 2) 75 const overview = await server.overviews.getVideos({ page: 2 })
84 76
85 const overview: VideosOverview = res.body
86 expect(overview.tags).to.have.lengthOf(1) 77 expect(overview.tags).to.have.lengthOf(1)
87 expect(overview.categories).to.have.lengthOf(0) 78 expect(overview.categories).to.have.lengthOf(0)
88 expect(overview.channels).to.have.lengthOf(0) 79 expect(overview.channels).to.have.lengthOf(0)
@@ -90,20 +81,10 @@ describe('Test a videos overview', function () {
90 }) 81 })
91 82
92 it('Should have the correct overview', async function () { 83 it('Should have the correct overview', async function () {
93 const res1 = await getVideosOverview(server.url, 1) 84 const overview1 = await server.overviews.getVideos({ page: 1 })
94 const res2 = await getVideosOverview(server.url, 2) 85 const overview2 = await server.overviews.getVideos({ page: 2 })
95
96 const overview1: VideosOverview = res1.body
97 const overview2: VideosOverview = res2.body
98
99 const tmp = [
100 overview1.tags,
101 overview1.categories,
102 overview1.channels,
103 overview2.tags
104 ]
105 86
106 for (const arr of tmp) { 87 for (const arr of [ overview1.tags, overview1.categories, overview1.channels, overview2.tags ]) {
107 expect(arr).to.have.lengthOf(1) 88 expect(arr).to.have.lengthOf(1)
108 89
109 const obj = arr[0] 90 const obj = arr[0]
@@ -127,20 +108,20 @@ describe('Test a videos overview', function () {
127 }) 108 })
128 109
129 it('Should hide muted accounts', async function () { 110 it('Should hide muted accounts', async function () {
130 const token = await generateUserAccessToken(server, 'choco') 111 const token = await server.users.generateUserAndToken('choco')
131 112
132 await addAccountToAccountBlocklist(server.url, token, 'root@' + server.host) 113 await server.blocklist.addToMyBlocklist({ token, account: 'root@' + server.host })
133 114
134 { 115 {
135 const res = await getVideosOverview(server.url, 1) 116 const body = await server.overviews.getVideos({ page: 1 })
136 117
137 testOverviewCount(res, 1) 118 testOverviewCount(body, 1)
138 } 119 }
139 120
140 { 121 {
141 const res = await getVideosOverviewWithToken(server.url, 1, token) 122 const body = await server.overviews.getVideos({ page: 1, token })
142 123
143 testOverviewCount(res, 0) 124 testOverviewCount(body, 0)
144 } 125 }
145 }) 126 })
146 127
diff --git a/server/tests/api/videos/videos-views-cleaner.ts b/server/tests/api/videos/videos-views-cleaner.ts
index b89f33217..82268b1be 100644
--- a/server/tests/api/videos/videos-views-cleaner.ts
+++ b/server/tests/api/videos/videos-views-cleaner.ts
@@ -1,19 +1,14 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2 2
3import * as chai from 'chai'
4import 'mocha' 3import 'mocha'
4import * as chai from 'chai'
5import { 5import {
6 cleanupTests, 6 cleanupTests,
7 closeAllSequelize, 7 createMultipleServers,
8 countVideoViewsOf,
9 doubleFollow, 8 doubleFollow,
10 flushAndRunMultipleServers,
11 killallServers, 9 killallServers,
12 reRunServer, 10 PeerTubeServer,
13 ServerInfo,
14 setAccessTokensToServers, 11 setAccessTokensToServers,
15 uploadVideoAndGetId,
16 viewVideo,
17 wait, 12 wait,
18 waitJobs 13 waitJobs
19} from '../../../../shared/extra-utils' 14} from '../../../../shared/extra-utils'
@@ -21,7 +16,7 @@ import {
21const expect = chai.expect 16const expect = chai.expect
22 17
23describe('Test video views cleaner', function () { 18describe('Test video views cleaner', function () {
24 let servers: ServerInfo[] 19 let servers: PeerTubeServer[]
25 20
26 let videoIdServer1: string 21 let videoIdServer1: string
27 let videoIdServer2: string 22 let videoIdServer2: string
@@ -29,20 +24,20 @@ describe('Test video views cleaner', function () {
29 before(async function () { 24 before(async function () {
30 this.timeout(120000) 25 this.timeout(120000)
31 26
32 servers = await flushAndRunMultipleServers(2) 27 servers = await createMultipleServers(2)
33 await setAccessTokensToServers(servers) 28 await setAccessTokensToServers(servers)
34 29
35 await doubleFollow(servers[0], servers[1]) 30 await doubleFollow(servers[0], servers[1])
36 31
37 videoIdServer1 = (await uploadVideoAndGetId({ server: servers[0], videoName: 'video server 1' })).uuid 32 videoIdServer1 = (await servers[0].videos.quickUpload({ name: 'video server 1' })).uuid
38 videoIdServer2 = (await uploadVideoAndGetId({ server: servers[1], videoName: 'video server 2' })).uuid 33 videoIdServer2 = (await servers[1].videos.quickUpload({ name: 'video server 2' })).uuid
39 34
40 await waitJobs(servers) 35 await waitJobs(servers)
41 36
42 await viewVideo(servers[0].url, videoIdServer1) 37 await servers[0].videos.view({ id: videoIdServer1 })
43 await viewVideo(servers[1].url, videoIdServer1) 38 await servers[1].videos.view({ id: videoIdServer1 })
44 await viewVideo(servers[0].url, videoIdServer2) 39 await servers[0].videos.view({ id: videoIdServer2 })
45 await viewVideo(servers[1].url, videoIdServer2) 40 await servers[1].videos.view({ id: videoIdServer2 })
46 41
47 await waitJobs(servers) 42 await waitJobs(servers)
48 }) 43 })
@@ -50,9 +45,9 @@ describe('Test video views cleaner', function () {
50 it('Should not clean old video views', async function () { 45 it('Should not clean old video views', async function () {
51 this.timeout(50000) 46 this.timeout(50000)
52 47
53 killallServers([ servers[0] ]) 48 await killallServers([ servers[0] ])
54 49
55 await reRunServer(servers[0], { views: { videos: { remote: { max_age: '10 days' } } } }) 50 await servers[0].run({ views: { videos: { remote: { max_age: '10 days' } } } })
56 51
57 await wait(6000) 52 await wait(6000)
58 53
@@ -60,14 +55,14 @@ describe('Test video views cleaner', function () {
60 55
61 { 56 {
62 for (const server of servers) { 57 for (const server of servers) {
63 const total = await countVideoViewsOf(server.internalServerNumber, videoIdServer1) 58 const total = await server.sql.countVideoViewsOf(videoIdServer1)
64 expect(total).to.equal(2, 'Server ' + server.serverNumber + ' does not have the correct amount of views') 59 expect(total).to.equal(2, 'Server ' + server.serverNumber + ' does not have the correct amount of views')
65 } 60 }
66 } 61 }
67 62
68 { 63 {
69 for (const server of servers) { 64 for (const server of servers) {
70 const total = await countVideoViewsOf(server.internalServerNumber, videoIdServer2) 65 const total = await server.sql.countVideoViewsOf(videoIdServer2)
71 expect(total).to.equal(2, 'Server ' + server.serverNumber + ' does not have the correct amount of views') 66 expect(total).to.equal(2, 'Server ' + server.serverNumber + ' does not have the correct amount of views')
72 } 67 }
73 } 68 }
@@ -76,9 +71,9 @@ describe('Test video views cleaner', function () {
76 it('Should clean old video views', async function () { 71 it('Should clean old video views', async function () {
77 this.timeout(50000) 72 this.timeout(50000)
78 73
79 killallServers([ servers[0] ]) 74 await killallServers([ servers[0] ])
80 75
81 await reRunServer(servers[0], { views: { videos: { remote: { max_age: '5 seconds' } } } }) 76 await servers[0].run({ views: { videos: { remote: { max_age: '5 seconds' } } } })
82 77
83 await wait(6000) 78 await wait(6000)
84 79
@@ -86,23 +81,21 @@ describe('Test video views cleaner', function () {
86 81
87 { 82 {
88 for (const server of servers) { 83 for (const server of servers) {
89 const total = await countVideoViewsOf(server.internalServerNumber, videoIdServer1) 84 const total = await server.sql.countVideoViewsOf(videoIdServer1)
90 expect(total).to.equal(2) 85 expect(total).to.equal(2)
91 } 86 }
92 } 87 }
93 88
94 { 89 {
95 const totalServer1 = await countVideoViewsOf(servers[0].internalServerNumber, videoIdServer2) 90 const totalServer1 = await servers[0].sql.countVideoViewsOf(videoIdServer2)
96 expect(totalServer1).to.equal(0) 91 expect(totalServer1).to.equal(0)
97 92
98 const totalServer2 = await countVideoViewsOf(servers[1].internalServerNumber, videoIdServer2) 93 const totalServer2 = await servers[1].sql.countVideoViewsOf(videoIdServer2)
99 expect(totalServer2).to.equal(2) 94 expect(totalServer2).to.equal(2)
100 } 95 }
101 }) 96 })
102 97
103 after(async function () { 98 after(async function () {
104 await closeAllSequelize(servers)
105
106 await cleanupTests(servers) 99 await cleanupTests(servers)
107 }) 100 })
108}) 101})