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