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