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