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