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