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