]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/notifications/notifications-api.ts
Move test functions outside extra-utils
[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('Mark as read', function () {
42
43 it('Should mark as read some notifications', async function () {
44 const { data } = await server.notifications.list({ token: userToken, start: 2, count: 3 })
45 const ids = data.map(n => n.id)
46
47 await server.notifications.markAsRead({ token: userToken, ids })
48 })
49
50 it('Should have the notifications marked as read', async function () {
51 const { data } = await server.notifications.list({ token: userToken, start: 0, count: 10 })
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
59 })
60
61 it('Should only list read notifications', async function () {
62 const { data } = await server.notifications.list({ token: userToken, start: 0, count: 10, unread: false })
63
64 for (const notification of data) {
65 expect(notification.read).to.be.true
66 }
67 })
68
69 it('Should only list unread notifications', async function () {
70 const { data } = await server.notifications.list({ token: userToken, start: 0, count: 10, unread: true })
71
72 for (const notification of data) {
73 expect(notification.read).to.be.false
74 }
75 })
76
77 it('Should mark as read all notifications', async function () {
78 await server.notifications.markAsReadAll({ token: userToken })
79
80 const body = await server.notifications.list({ token: userToken, start: 0, count: 10, unread: true })
81
82 expect(body.total).to.equal(0)
83 expect(body.data).to.have.lengthOf(0)
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,
95 token: userToken
96 }
97 })
98
99 it('Should not have notifications', async function () {
100 this.timeout(20000)
101
102 await server.notifications.updateMySettings({
103 token: userToken,
104 settings: { ...getAllNotificationsSettings(), newVideoFromSubscription: UserNotificationSettingValue.NONE }
105 })
106
107 {
108 const info = await server.users.getMyInfo({ token: userToken })
109 expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.NONE)
110 }
111
112 const { name, shortUUID } = await server.videos.randomUpload()
113
114 const check = { web: true, mail: true }
115 await checkNewVideoFromSubscription({ ...baseParams, check, videoName: name, shortUUID, checkType: 'absence' })
116 })
117
118 it('Should only have web notifications', async function () {
119 this.timeout(20000)
120
121 await server.notifications.updateMySettings({
122 token: userToken,
123 settings: { ...getAllNotificationsSettings(), newVideoFromSubscription: UserNotificationSettingValue.WEB }
124 })
125
126 {
127 const info = await server.users.getMyInfo({ token: userToken })
128 expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.WEB)
129 }
130
131 const { name, shortUUID } = await server.videos.randomUpload()
132
133 {
134 const check = { mail: true, web: false }
135 await checkNewVideoFromSubscription({ ...baseParams, check, videoName: name, shortUUID, checkType: 'absence' })
136 }
137
138 {
139 const check = { mail: false, web: true }
140 await checkNewVideoFromSubscription({ ...baseParams, check, videoName: name, shortUUID, checkType: 'presence' })
141 }
142 })
143
144 it('Should only have mail notifications', async function () {
145 this.timeout(20000)
146
147 await server.notifications.updateMySettings({
148 token: userToken,
149 settings: { ...getAllNotificationsSettings(), newVideoFromSubscription: UserNotificationSettingValue.EMAIL }
150 })
151
152 {
153 const info = await server.users.getMyInfo({ token: userToken })
154 expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.EMAIL)
155 }
156
157 const { name, shortUUID } = await server.videos.randomUpload()
158
159 {
160 const check = { mail: false, web: true }
161 await checkNewVideoFromSubscription({ ...baseParams, check, videoName: name, shortUUID, checkType: 'absence' })
162 }
163
164 {
165 const check = { mail: true, web: false }
166 await checkNewVideoFromSubscription({ ...baseParams, check, videoName: name, shortUUID, checkType: 'presence' })
167 }
168 })
169
170 it('Should have email and web notifications', async function () {
171 this.timeout(20000)
172
173 await server.notifications.updateMySettings({
174 token: userToken,
175 settings: {
176 ...getAllNotificationsSettings(),
177 newVideoFromSubscription: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL
178 }
179 })
180
181 {
182 const info = await server.users.getMyInfo({ token: userToken })
183 expect(info.notificationSettings.newVideoFromSubscription).to.equal(
184 UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL
185 )
186 }
187
188 const { name, shortUUID } = await server.videos.randomUpload()
189
190 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
191 })
192 })
193
194 after(async function () {
195 MockSmtpServer.Instance.kill()
196
197 await cleanupTests([ server ])
198 })
199 })