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