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