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