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