]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/notifications/notifications-api.ts
Fix multiple servers tests
[github/Chocobozzz/PeerTube.git] / server / tests / api / notifications / notifications-api.ts
CommitLineData
8eb07b01
C
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3import 'mocha'
4import * as chai from 'chai'
8eb07b01
C
5import {
6 CheckerBaseParams,
7 checkNewVideoFromSubscription,
8 getAllNotificationsSettings,
2c27e704 9 MockSmtpServer,
c55e3d72
C
10 prepareNotificationsTest
11} from '@server/tests/shared'
7926c5f9 12import { UserNotification, UserNotificationSettingValue } from '@shared/models'
c55e3d72 13import { cleanupTests, PeerTubeServer, waitJobs } from '@shared/server-commands'
8eb07b01
C
14
15const expect = chai.expect
16
17describe('Test notifications API', function () {
254d3579 18 let server: PeerTubeServer
8eb07b01 19 let userNotifications: UserNotification[] = []
dd0ebb71 20 let userToken: string
8eb07b01
C
21 let emails: object[] = []
22
23 before(async function () {
24 this.timeout(120000)
25
26 const res = await prepareNotificationsTest(1)
27 emails = res.emails
dd0ebb71 28 userToken = res.userAccessToken
8eb07b01
C
29 userNotifications = res.userNotifications
30 server = res.servers[0]
31
89d241a7 32 await server.subscriptions.add({ token: userToken, targetUri: 'root_channel@localhost:' + server.port })
8eb07b01
C
33
34 for (let i = 0; i < 10; i++) {
89d241a7 35 await server.videos.randomUpload({ wait: false })
8eb07b01
C
36 }
37
38 await waitJobs([ server ])
39 })
40
41 describe('Mark as read', function () {
42
43 it('Should mark as read some notifications', async function () {
89d241a7 44 const { data } = await server.notifications.list({ token: userToken, start: 2, count: 3 })
dd0ebb71 45 const ids = data.map(n => n.id)
8eb07b01 46
89d241a7 47 await server.notifications.markAsRead({ token: userToken, ids })
8eb07b01
C
48 })
49
50 it('Should have the notifications marked as read', async function () {
89d241a7 51 const { data } = await server.notifications.list({ token: userToken, start: 0, count: 10 })
dd0ebb71
C
52
53 expect(data[0].read).to.be.false
54 expect(data[1].read).to.be.false
55 expect(data[2].read).to.be.true
56 expect(data[3].read).to.be.true
57 expect(data[4].read).to.be.true
58 expect(data[5].read).to.be.false
8eb07b01
C
59 })
60
61 it('Should only list read notifications', async function () {
89d241a7 62 const { data } = await server.notifications.list({ token: userToken, start: 0, count: 10, unread: false })
8eb07b01 63
dd0ebb71 64 for (const notification of data) {
8eb07b01
C
65 expect(notification.read).to.be.true
66 }
67 })
68
69 it('Should only list unread notifications', async function () {
89d241a7 70 const { data } = await server.notifications.list({ token: userToken, start: 0, count: 10, unread: true })
8eb07b01 71
dd0ebb71 72 for (const notification of data) {
8eb07b01
C
73 expect(notification.read).to.be.false
74 }
75 })
76
77 it('Should mark as read all notifications', async function () {
89d241a7 78 await server.notifications.markAsReadAll({ token: userToken })
8eb07b01 79
89d241a7 80 const body = await server.notifications.list({ token: userToken, start: 0, count: 10, unread: true })
8eb07b01 81
dd0ebb71
C
82 expect(body.total).to.equal(0)
83 expect(body.data).to.have.lengthOf(0)
8eb07b01
C
84 })
85 })
86
87 describe('Notification settings', function () {
88 let baseParams: CheckerBaseParams
89
90 before(() => {
91 baseParams = {
92 server: server,
93 emails,
94 socketNotifications: userNotifications,
dd0ebb71 95 token: userToken
8eb07b01
C
96 }
97 })
98
99 it('Should not have notifications', async function () {
100 this.timeout(20000)
101
89d241a7 102 await server.notifications.updateMySettings({
dd0ebb71
C
103 token: userToken,
104 settings: { ...getAllNotificationsSettings(), newVideoFromSubscription: UserNotificationSettingValue.NONE }
105 })
8eb07b01
C
106
107 {
89d241a7 108 const info = await server.users.getMyInfo({ token: userToken })
8eb07b01
C
109 expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.NONE)
110 }
111
29837f88 112 const { name, shortUUID } = await server.videos.randomUpload()
8eb07b01
C
113
114 const check = { web: true, mail: true }
29837f88 115 await checkNewVideoFromSubscription({ ...baseParams, check, videoName: name, shortUUID, checkType: 'absence' })
8eb07b01
C
116 })
117
118 it('Should only have web notifications', async function () {
119 this.timeout(20000)
120
89d241a7 121 await server.notifications.updateMySettings({
dd0ebb71
C
122 token: userToken,
123 settings: { ...getAllNotificationsSettings(), newVideoFromSubscription: UserNotificationSettingValue.WEB }
124 })
8eb07b01
C
125
126 {
89d241a7 127 const info = await server.users.getMyInfo({ token: userToken })
8eb07b01
C
128 expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.WEB)
129 }
130
29837f88 131 const { name, shortUUID } = await server.videos.randomUpload()
8eb07b01
C
132
133 {
134 const check = { mail: true, web: false }
29837f88 135 await checkNewVideoFromSubscription({ ...baseParams, check, videoName: name, shortUUID, checkType: 'absence' })
8eb07b01
C
136 }
137
138 {
139 const check = { mail: false, web: true }
29837f88 140 await checkNewVideoFromSubscription({ ...baseParams, check, videoName: name, shortUUID, checkType: 'presence' })
8eb07b01
C
141 }
142 })
143
144 it('Should only have mail notifications', async function () {
145 this.timeout(20000)
146
89d241a7 147 await server.notifications.updateMySettings({
dd0ebb71
C
148 token: userToken,
149 settings: { ...getAllNotificationsSettings(), newVideoFromSubscription: UserNotificationSettingValue.EMAIL }
150 })
8eb07b01
C
151
152 {
89d241a7 153 const info = await server.users.getMyInfo({ token: userToken })
8eb07b01
C
154 expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.EMAIL)
155 }
156
29837f88 157 const { name, shortUUID } = await server.videos.randomUpload()
8eb07b01
C
158
159 {
160 const check = { mail: false, web: true }
29837f88 161 await checkNewVideoFromSubscription({ ...baseParams, check, videoName: name, shortUUID, checkType: 'absence' })
8eb07b01
C
162 }
163
164 {
165 const check = { mail: true, web: false }
29837f88 166 await checkNewVideoFromSubscription({ ...baseParams, check, videoName: name, shortUUID, checkType: 'presence' })
8eb07b01
C
167 }
168 })
169
170 it('Should have email and web notifications', async function () {
171 this.timeout(20000)
172
89d241a7 173 await server.notifications.updateMySettings({
dd0ebb71
C
174 token: userToken,
175 settings: {
176 ...getAllNotificationsSettings(),
177 newVideoFromSubscription: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL
178 }
179 })
8eb07b01
C
180
181 {
89d241a7 182 const info = await server.users.getMyInfo({ token: userToken })
8eb07b01
C
183 expect(info.notificationSettings.newVideoFromSubscription).to.equal(
184 UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL
185 )
186 }
187
29837f88 188 const { name, shortUUID } = await server.videos.randomUpload()
8eb07b01 189
29837f88 190 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
8eb07b01
C
191 })
192 })
193
194 after(async function () {
195 MockSmtpServer.Instance.kill()
196
197 await cleanupTests([ server ])
198 })
199})