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