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