]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/notifications/user-notifications.ts
Fix multiple servers tests
[github/Chocobozzz/PeerTube.git] / server / tests / api / notifications / user-notifications.ts
CommitLineData
a1587156 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
cef534ed 2
cef534ed 3import 'mocha'
8eb07b01 4import * as chai from 'chai'
cef534ed
C
5import {
6 CheckerBaseParams,
f7cc67b4
C
7 checkMyVideoImportIsFinished,
8 checkNewActorFollow,
cef534ed 9 checkNewVideoFromSubscription,
f7cc67b4 10 checkVideoIsPublished,
59bbcced 11 FIXTURE_URLS,
2c27e704 12 MockSmtpServer,
59bbcced 13 prepareNotificationsTest,
c55e3d72
C
14 uploadRandomVideoOnServers
15} from '@server/tests/shared'
0628157f
C
16import { wait } from '@shared/core-utils'
17import { buildUUID } from '@shared/extra-utils'
2c27e704 18import { UserNotification, UserNotificationType, VideoPrivacy } from '@shared/models'
c55e3d72 19import { cleanupTests, PeerTubeServer, waitJobs } from '@shared/server-commands'
cef534ed
C
20
21const expect = chai.expect
22
8eb07b01 23describe('Test user notifications', function () {
254d3579 24 let servers: PeerTubeServer[] = []
cef534ed 25 let userAccessToken: string
8eb07b01
C
26 let userNotifications: UserNotification[] = []
27 let adminNotifications: UserNotification[] = []
28 let adminNotificationsServer2: UserNotification[] = []
29 let emails: object[] = []
dc133480
C
30 let channelId: number
31
cef534ed
C
32 before(async function () {
33 this.timeout(120000)
34
8eb07b01
C
35 const res = await prepareNotificationsTest(3)
36 emails = res.emails
37 userAccessToken = res.userAccessToken
38 servers = res.servers
39 userNotifications = res.userNotifications
40 adminNotifications = res.adminNotifications
41 adminNotificationsServer2 = res.adminNotificationsServer2
42 channelId = res.channelId
cef534ed
C
43 })
44
45 describe('New video from my subscription notification', function () {
46 let baseParams: CheckerBaseParams
47
48 before(() => {
49 baseParams = {
50 server: servers[0],
51 emails,
52 socketNotifications: userNotifications,
53 token: userAccessToken
54 }
55 })
56
57 it('Should not send notifications if the user does not follow the video publisher', async function () {
59fd824c 58 this.timeout(50000)
f7effe8d 59
8eb07b01 60 await uploadRandomVideoOnServers(servers, 1)
cef534ed 61
5d3c5f27 62 const notification = await servers[0].notifications.getLatest({ token: userAccessToken })
cef534ed
C
63 expect(notification).to.be.undefined
64
65 expect(emails).to.have.lengthOf(0)
66 expect(userNotifications).to.have.lengthOf(0)
67 })
68
69 it('Should send a new video notification if the user follows the local video publisher', async function () {
89ada4e2 70 this.timeout(15000)
2f1548fd 71
89d241a7 72 await servers[0].subscriptions.add({ token: userAccessToken, targetUri: 'root_channel@localhost:' + servers[0].port })
2f1548fd 73 await waitJobs(servers)
cef534ed 74
29837f88
C
75 const { name, shortUUID } = await uploadRandomVideoOnServers(servers, 1)
76 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
cef534ed
C
77 })
78
79 it('Should send a new video notification from a remote account', async function () {
83befebe 80 this.timeout(150000) // Server 2 has transcoding enabled
cef534ed 81
89d241a7 82 await servers[0].subscriptions.add({ token: userAccessToken, targetUri: 'root_channel@localhost:' + servers[1].port })
2f1548fd 83 await waitJobs(servers)
cef534ed 84
29837f88
C
85 const { name, shortUUID } = await uploadRandomVideoOnServers(servers, 2)
86 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
cef534ed
C
87 })
88
89 it('Should send a new video notification on a scheduled publication', async function () {
59fd824c 90 this.timeout(50000)
cef534ed 91
cef534ed 92 // In 2 seconds
a1587156 93 const updateAt = new Date(new Date().getTime() + 2000)
cef534ed
C
94
95 const data = {
96 privacy: VideoPrivacy.PRIVATE,
97 scheduleUpdate: {
98 updateAt: updateAt.toISOString(),
d23dd9fb 99 privacy: VideoPrivacy.PUBLIC as VideoPrivacy.PUBLIC
cef534ed
C
100 }
101 }
29837f88 102 const { name, shortUUID } = await uploadRandomVideoOnServers(servers, 1, data)
cef534ed
C
103
104 await wait(6000)
29837f88 105 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
cef534ed
C
106 })
107
108 it('Should send a new video notification on a remote scheduled publication', async function () {
8ace6805 109 this.timeout(100000)
cef534ed 110
cef534ed 111 // In 2 seconds
a1587156 112 const updateAt = new Date(new Date().getTime() + 2000)
cef534ed
C
113
114 const data = {
115 privacy: VideoPrivacy.PRIVATE,
116 scheduleUpdate: {
117 updateAt: updateAt.toISOString(),
d23dd9fb 118 privacy: VideoPrivacy.PUBLIC as VideoPrivacy.PUBLIC
cef534ed
C
119 }
120 }
29837f88 121 const { name, shortUUID } = await uploadRandomVideoOnServers(servers, 2, data)
e8d246d5 122 await waitJobs(servers)
cef534ed
C
123
124 await wait(6000)
29837f88 125 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
cef534ed
C
126 })
127
128 it('Should not send a notification before the video is published', async function () {
21d68e68 129 this.timeout(150000)
cef534ed 130
a1587156 131 const updateAt = new Date(new Date().getTime() + 1000000)
cef534ed
C
132
133 const data = {
134 privacy: VideoPrivacy.PRIVATE,
135 scheduleUpdate: {
136 updateAt: updateAt.toISOString(),
d23dd9fb 137 privacy: VideoPrivacy.PUBLIC as VideoPrivacy.PUBLIC
cef534ed
C
138 }
139 }
29837f88 140 const { name, shortUUID } = await uploadRandomVideoOnServers(servers, 1, data)
cef534ed
C
141
142 await wait(6000)
29837f88 143 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'absence' })
cef534ed
C
144 })
145
146 it('Should send a new video notification when a video becomes public', async function () {
59fd824c 147 this.timeout(50000)
cef534ed 148
cef534ed 149 const data = { privacy: VideoPrivacy.PRIVATE }
29837f88 150 const { name, uuid, shortUUID } = await uploadRandomVideoOnServers(servers, 1, data)
cef534ed 151
29837f88 152 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'absence' })
cef534ed 153
89d241a7 154 await servers[0].videos.update({ id: uuid, attributes: { privacy: VideoPrivacy.PUBLIC } })
cef534ed 155
9f39c546 156 await waitJobs(servers)
29837f88 157 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
cef534ed
C
158 })
159
160 it('Should send a new video notification when a remote video becomes public', async function () {
59fd824c 161 this.timeout(50000)
cef534ed 162
cef534ed 163 const data = { privacy: VideoPrivacy.PRIVATE }
29837f88 164 const { name, uuid, shortUUID } = await uploadRandomVideoOnServers(servers, 2, data)
cef534ed 165
29837f88 166 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'absence' })
cef534ed 167
89d241a7 168 await servers[1].videos.update({ id: uuid, attributes: { privacy: VideoPrivacy.PUBLIC } })
cef534ed
C
169
170 await waitJobs(servers)
29837f88 171 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
cef534ed
C
172 })
173
174 it('Should not send a new video notification when a video becomes unlisted', async function () {
59fd824c 175 this.timeout(50000)
cef534ed 176
cef534ed 177 const data = { privacy: VideoPrivacy.PRIVATE }
29837f88 178 const { name, uuid, shortUUID } = await uploadRandomVideoOnServers(servers, 1, data)
cef534ed 179
89d241a7 180 await servers[0].videos.update({ id: uuid, attributes: { privacy: VideoPrivacy.UNLISTED } })
cef534ed 181
29837f88 182 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'absence' })
cef534ed
C
183 })
184
185 it('Should not send a new video notification when a remote video becomes unlisted', async function () {
59fd824c 186 this.timeout(50000)
cef534ed 187
cef534ed 188 const data = { privacy: VideoPrivacy.PRIVATE }
29837f88 189 const { name, uuid, shortUUID } = await uploadRandomVideoOnServers(servers, 2, data)
cef534ed 190
89d241a7 191 await servers[1].videos.update({ id: uuid, attributes: { privacy: VideoPrivacy.UNLISTED } })
cef534ed
C
192
193 await waitJobs(servers)
29837f88 194 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'absence' })
cef534ed
C
195 })
196
197 it('Should send a new video notification after a video import', async function () {
7ccddd7b 198 this.timeout(100000)
cef534ed 199
d4a8e7a6 200 const name = 'video import ' + buildUUID()
cef534ed
C
201
202 const attributes = {
dc133480 203 name,
cef534ed
C
204 channelId,
205 privacy: VideoPrivacy.PUBLIC,
59bbcced 206 targetUrl: FIXTURE_URLS.goodVideo
cef534ed 207 }
89d241a7 208 const { video } = await servers[0].imports.importVideo({ attributes })
cef534ed
C
209
210 await waitJobs(servers)
211
29837f88 212 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID: video.shortUUID, checkType: 'presence' })
cef534ed
C
213 })
214 })
215
dc133480
C
216 describe('My video is published', function () {
217 let baseParams: CheckerBaseParams
218
219 before(() => {
220 baseParams = {
221 server: servers[1],
222 emails,
223 socketNotifications: adminNotificationsServer2,
224 token: servers[1].accessToken
225 }
226 })
227
228 it('Should not send a notification if transcoding is not enabled', async function () {
59fd824c 229 this.timeout(50000)
f7effe8d 230
29837f88 231 const { name, shortUUID } = await uploadRandomVideoOnServers(servers, 1)
dc133480
C
232 await waitJobs(servers)
233
29837f88 234 await checkVideoIsPublished({ ...baseParams, videoName: name, shortUUID, checkType: 'absence' })
dc133480
C
235 })
236
237 it('Should not send a notification if the wait transcoding is false', async function () {
5d3c5f27 238 this.timeout(100_000)
dc133480 239
8eb07b01 240 await uploadRandomVideoOnServers(servers, 2, { waitTranscoding: false })
dc133480
C
241 await waitJobs(servers)
242
5d3c5f27 243 const notification = await servers[0].notifications.getLatest({ token: userAccessToken })
dc133480
C
244 if (notification) {
245 expect(notification.type).to.not.equal(UserNotificationType.MY_VIDEO_PUBLISHED)
246 }
247 })
248
249 it('Should send a notification even if the video is not transcoded in other resolutions', async function () {
250 this.timeout(50000)
251
29837f88 252 const { name, shortUUID } = await uploadRandomVideoOnServers(servers, 2, { waitTranscoding: true, fixture: 'video_short_240p.mp4' })
dc133480
C
253 await waitJobs(servers)
254
29837f88 255 await checkVideoIsPublished({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
dc133480
C
256 })
257
258 it('Should send a notification with a transcoded video', async function () {
259 this.timeout(50000)
260
29837f88 261 const { name, shortUUID } = await uploadRandomVideoOnServers(servers, 2, { waitTranscoding: true })
dc133480
C
262 await waitJobs(servers)
263
29837f88 264 await checkVideoIsPublished({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
dc133480
C
265 })
266
267 it('Should send a notification when an imported video is transcoded', async function () {
33675a47 268 this.timeout(120000)
dc133480 269
d4a8e7a6 270 const name = 'video import ' + buildUUID()
dc133480
C
271
272 const attributes = {
273 name,
274 channelId,
275 privacy: VideoPrivacy.PUBLIC,
59bbcced 276 targetUrl: FIXTURE_URLS.goodVideo,
dc133480
C
277 waitTranscoding: true
278 }
89d241a7 279 const { video } = await servers[1].imports.importVideo({ attributes })
dc133480
C
280
281 await waitJobs(servers)
29837f88 282 await checkVideoIsPublished({ ...baseParams, videoName: name, shortUUID: video.shortUUID, checkType: 'presence' })
dc133480
C
283 })
284
285 it('Should send a notification when the scheduled update has been proceeded', async function () {
286 this.timeout(70000)
287
288 // In 2 seconds
a1587156 289 const updateAt = new Date(new Date().getTime() + 2000)
dc133480
C
290
291 const data = {
292 privacy: VideoPrivacy.PRIVATE,
293 scheduleUpdate: {
294 updateAt: updateAt.toISOString(),
d23dd9fb 295 privacy: VideoPrivacy.PUBLIC as VideoPrivacy.PUBLIC
dc133480
C
296 }
297 }
29837f88 298 const { name, shortUUID } = await uploadRandomVideoOnServers(servers, 2, data)
dc133480
C
299
300 await wait(6000)
29837f88 301 await checkVideoIsPublished({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
dc133480 302 })
f7effe8d
JM
303
304 it('Should not send a notification before the video is published', async function () {
94d721ef 305 this.timeout(50000)
f7effe8d 306
a1587156 307 const updateAt = new Date(new Date().getTime() + 1000000)
f7effe8d
JM
308
309 const data = {
310 privacy: VideoPrivacy.PRIVATE,
311 scheduleUpdate: {
312 updateAt: updateAt.toISOString(),
d23dd9fb 313 privacy: VideoPrivacy.PUBLIC as VideoPrivacy.PUBLIC
f7effe8d
JM
314 }
315 }
29837f88 316 const { name, shortUUID } = await uploadRandomVideoOnServers(servers, 2, data)
f7effe8d
JM
317
318 await wait(6000)
29837f88 319 await checkVideoIsPublished({ ...baseParams, videoName: name, shortUUID, checkType: 'absence' })
f7effe8d 320 })
dc133480
C
321 })
322
323 describe('My video is imported', function () {
324 let baseParams: CheckerBaseParams
325
326 before(() => {
327 baseParams = {
328 server: servers[0],
329 emails,
330 socketNotifications: adminNotifications,
331 token: servers[0].accessToken
332 }
333 })
334
335 it('Should send a notification when the video import failed', async function () {
336 this.timeout(70000)
337
d4a8e7a6 338 const name = 'video import ' + buildUUID()
dc133480
C
339
340 const attributes = {
341 name,
342 channelId,
343 privacy: VideoPrivacy.PRIVATE,
59bbcced 344 targetUrl: FIXTURE_URLS.badVideo
dc133480 345 }
29837f88 346 const { video: { shortUUID } } = await servers[0].imports.importVideo({ attributes })
dc133480
C
347
348 await waitJobs(servers)
29837f88
C
349
350 const url = FIXTURE_URLS.badVideo
351 await checkMyVideoImportIsFinished({ ...baseParams, videoName: name, shortUUID, url, success: false, checkType: 'presence' })
dc133480
C
352 })
353
354 it('Should send a notification when the video import succeeded', async function () {
355 this.timeout(70000)
356
d4a8e7a6 357 const name = 'video import ' + buildUUID()
dc133480
C
358
359 const attributes = {
360 name,
361 channelId,
362 privacy: VideoPrivacy.PRIVATE,
59bbcced 363 targetUrl: FIXTURE_URLS.goodVideo
dc133480 364 }
29837f88 365 const { video: { shortUUID } } = await servers[0].imports.importVideo({ attributes })
dc133480
C
366
367 await waitJobs(servers)
29837f88
C
368
369 const url = FIXTURE_URLS.goodVideo
370 await checkMyVideoImportIsFinished({ ...baseParams, videoName: name, shortUUID, url, success: true, checkType: 'presence' })
cef534ed
C
371 })
372 })
373
f7cc67b4
C
374 describe('New actor follow', function () {
375 let baseParams: CheckerBaseParams
a1587156
C
376 const myChannelName = 'super channel name'
377 const myUserName = 'super user name'
f7cc67b4
C
378
379 before(async () => {
380 baseParams = {
381 server: servers[0],
382 emails,
383 socketNotifications: userNotifications,
384 token: userAccessToken
385 }
386
89d241a7 387 await servers[0].users.updateMe({ displayName: 'super root name' })
f7cc67b4 388
89d241a7 389 await servers[0].users.updateMe({
7926c5f9 390 token: userAccessToken,
f7cc67b4
C
391 displayName: myUserName
392 })
393
89d241a7 394 await servers[1].users.updateMe({ displayName: 'super root 2 name' })
f7cc67b4 395
89d241a7 396 await servers[0].channels.update({
a5461888
C
397 token: userAccessToken,
398 channelName: 'user_1_channel',
399 attributes: { displayName: myChannelName }
400 })
f7cc67b4
C
401 })
402
403 it('Should notify when a local channel is following one of our channel', async function () {
59fd824c 404 this.timeout(50000)
f7cc67b4 405
89d241a7 406 await servers[0].subscriptions.add({ targetUri: 'user_1_channel@localhost:' + servers[0].port })
f7cc67b4
C
407 await waitJobs(servers)
408
29837f88
C
409 await checkNewActorFollow({
410 ...baseParams,
411 followType: 'channel',
412 followerName: 'root',
413 followerDisplayName: 'super root name',
414 followingDisplayName: myChannelName,
415 checkType: 'presence'
416 })
f7cc67b4 417
89d241a7 418 await servers[0].subscriptions.remove({ uri: 'user_1_channel@localhost:' + servers[0].port })
f7cc67b4
C
419 })
420
421 it('Should notify when a remote channel is following one of our channel', async function () {
59fd824c 422 this.timeout(50000)
f7cc67b4 423
89d241a7 424 await servers[1].subscriptions.add({ targetUri: 'user_1_channel@localhost:' + servers[0].port })
f7cc67b4
C
425 await waitJobs(servers)
426
29837f88
C
427 await checkNewActorFollow({
428 ...baseParams,
429 followType: 'channel',
430 followerName: 'root',
431 followerDisplayName: 'super root 2 name',
432 followingDisplayName: myChannelName,
433 checkType: 'presence'
434 })
f7cc67b4 435
89d241a7 436 await servers[1].subscriptions.remove({ uri: 'user_1_channel@localhost:' + servers[0].port })
f7cc67b4
C
437 })
438
0dd57e4d
C
439 // PeerTube does not support accout -> account follows
440 // it('Should notify when a local account is following one of our channel', async function () {
59fd824c 441 // this.timeout(50000)
0dd57e4d
C
442 //
443 // await addUserSubscription(servers[0].url, servers[0].accessToken, 'user_1@localhost:' + servers[0].port)
444 //
445 // await waitJobs(servers)
446 //
447 // await checkNewActorFollow(baseParams, 'account', 'root', 'super root name', myUserName, 'presence')
448 // })
f7cc67b4 449
6ed2e4ea 450 // it('Should notify when a remote account is following one of our channel', async function () {
59fd824c 451 // this.timeout(50000)
6ed2e4ea
C
452 //
453 // await addUserSubscription(servers[1].url, servers[1].accessToken, 'user_1@localhost:' + servers[0].port)
454 //
455 // await waitJobs(servers)
456 //
457 // await checkNewActorFollow(baseParams, 'account', 'root', 'super root 2 name', myUserName, 'presence')
458 // })
f7cc67b4
C
459 })
460
7c3b7976 461 after(async function () {
89ada4e2
C
462 MockSmtpServer.Instance.kill()
463
7c3b7976 464 await cleanupTests(servers)
cef534ed
C
465 })
466})