]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/check-params/user-notifications.ts
Merge branch 'release/3.1.0' into develop
[github/Chocobozzz/PeerTube.git] / server / tests / api / check-params / user-notifications.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import 'mocha'
4 import { io } from 'socket.io-client'
5
6 import {
7 cleanupTests,
8 flushAndRunServer,
9 immutableAssign,
10 makeGetRequest,
11 makePostBodyRequest,
12 makePutBodyRequest,
13 ServerInfo,
14 setAccessTokensToServers,
15 wait
16 } from '../../../../shared/extra-utils'
17 import {
18 checkBadCountPagination,
19 checkBadSortPagination,
20 checkBadStartPagination
21 } from '../../../../shared/extra-utils/requests/check-api-params'
22 import { UserNotificationSetting, UserNotificationSettingValue } from '../../../../shared/models/users'
23 import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
24
25 describe('Test user notifications API validators', function () {
26 let server: ServerInfo
27
28 // ---------------------------------------------------------------
29
30 before(async function () {
31 this.timeout(30000)
32
33 server = await flushAndRunServer(1)
34
35 await setAccessTokensToServers([ server ])
36 })
37
38 describe('When listing my notifications', function () {
39 const path = '/api/v1/users/me/notifications'
40
41 it('Should fail with a bad start pagination', async function () {
42 await checkBadStartPagination(server.url, path, server.accessToken)
43 })
44
45 it('Should fail with a bad count pagination', async function () {
46 await checkBadCountPagination(server.url, path, server.accessToken)
47 })
48
49 it('Should fail with an incorrect sort', async function () {
50 await checkBadSortPagination(server.url, path, server.accessToken)
51 })
52
53 it('Should fail with an incorrect unread parameter', async function () {
54 await makeGetRequest({
55 url: server.url,
56 path,
57 query: {
58 unread: 'toto'
59 },
60 token: server.accessToken,
61 statusCodeExpected: HttpStatusCode.OK_200
62 })
63 })
64
65 it('Should fail with a non authenticated user', async function () {
66 await makeGetRequest({
67 url: server.url,
68 path,
69 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
70 })
71 })
72
73 it('Should succeed with the correct parameters', async function () {
74 await makeGetRequest({
75 url: server.url,
76 path,
77 token: server.accessToken,
78 statusCodeExpected: HttpStatusCode.OK_200
79 })
80 })
81 })
82
83 describe('When marking as read my notifications', function () {
84 const path = '/api/v1/users/me/notifications/read'
85
86 it('Should fail with wrong ids parameters', async function () {
87 await makePostBodyRequest({
88 url: server.url,
89 path,
90 fields: {
91 ids: [ 'hello' ]
92 },
93 token: server.accessToken,
94 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
95 })
96
97 await makePostBodyRequest({
98 url: server.url,
99 path,
100 fields: {
101 ids: [ ]
102 },
103 token: server.accessToken,
104 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
105 })
106
107 await makePostBodyRequest({
108 url: server.url,
109 path,
110 fields: {
111 ids: 5
112 },
113 token: server.accessToken,
114 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
115 })
116 })
117
118 it('Should fail with a non authenticated user', async function () {
119 await makePostBodyRequest({
120 url: server.url,
121 path,
122 fields: {
123 ids: [ 5 ]
124 },
125 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
126 })
127 })
128
129 it('Should succeed with the correct parameters', async function () {
130 await makePostBodyRequest({
131 url: server.url,
132 path,
133 fields: {
134 ids: [ 5 ]
135 },
136 token: server.accessToken,
137 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
138 })
139 })
140 })
141
142 describe('When marking as read my notifications', function () {
143 const path = '/api/v1/users/me/notifications/read-all'
144
145 it('Should fail with a non authenticated user', async function () {
146 await makePostBodyRequest({
147 url: server.url,
148 path,
149 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
150 })
151 })
152
153 it('Should succeed with the correct parameters', async function () {
154 await makePostBodyRequest({
155 url: server.url,
156 path,
157 token: server.accessToken,
158 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
159 })
160 })
161 })
162
163 describe('When updating my notification settings', function () {
164 const path = '/api/v1/users/me/notification-settings'
165 const correctFields: UserNotificationSetting = {
166 newVideoFromSubscription: UserNotificationSettingValue.WEB,
167 newCommentOnMyVideo: UserNotificationSettingValue.WEB,
168 abuseAsModerator: UserNotificationSettingValue.WEB,
169 videoAutoBlacklistAsModerator: UserNotificationSettingValue.WEB,
170 blacklistOnMyVideo: UserNotificationSettingValue.WEB,
171 myVideoImportFinished: UserNotificationSettingValue.WEB,
172 myVideoPublished: UserNotificationSettingValue.WEB,
173 commentMention: UserNotificationSettingValue.WEB,
174 newFollow: UserNotificationSettingValue.WEB,
175 newUserRegistration: UserNotificationSettingValue.WEB,
176 newInstanceFollower: UserNotificationSettingValue.WEB,
177 autoInstanceFollowing: UserNotificationSettingValue.WEB,
178 abuseNewMessage: UserNotificationSettingValue.WEB,
179 abuseStateChange: UserNotificationSettingValue.WEB,
180 newPeerTubeVersion: UserNotificationSettingValue.WEB,
181 newPluginVersion: UserNotificationSettingValue.WEB
182 }
183
184 it('Should fail with missing fields', async function () {
185 await makePutBodyRequest({
186 url: server.url,
187 path,
188 token: server.accessToken,
189 fields: { newVideoFromSubscription: UserNotificationSettingValue.WEB },
190 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
191 })
192 })
193
194 it('Should fail with incorrect field values', async function () {
195 {
196 const fields = immutableAssign(correctFields, { newCommentOnMyVideo: 15 })
197
198 await makePutBodyRequest({
199 url: server.url,
200 path,
201 token: server.accessToken,
202 fields,
203 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
204 })
205 }
206
207 {
208 const fields = immutableAssign(correctFields, { newCommentOnMyVideo: 'toto' })
209
210 await makePutBodyRequest({
211 url: server.url,
212 path,
213 fields,
214 token: server.accessToken,
215 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
216 })
217 }
218 })
219
220 it('Should fail with a non authenticated user', async function () {
221 await makePutBodyRequest({
222 url: server.url,
223 path,
224 fields: correctFields,
225 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
226 })
227 })
228
229 it('Should succeed with the correct parameters', async function () {
230 await makePutBodyRequest({
231 url: server.url,
232 path,
233 token: server.accessToken,
234 fields: correctFields,
235 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
236 })
237 })
238 })
239
240 describe('When connecting to my notification socket', function () {
241
242 it('Should fail with no token', function (next) {
243 const socket = io(`http://localhost:${server.port}/user-notifications`, { reconnection: false })
244
245 socket.once('connect_error', function () {
246 socket.disconnect()
247 next()
248 })
249
250 socket.on('connect', () => {
251 socket.disconnect()
252 next(new Error('Connected with a missing token.'))
253 })
254 })
255
256 it('Should fail with an invalid token', function (next) {
257 const socket = io(`http://localhost:${server.port}/user-notifications`, {
258 query: { accessToken: 'bad_access_token' },
259 reconnection: false
260 })
261
262 socket.once('connect_error', function () {
263 socket.disconnect()
264 next()
265 })
266
267 socket.on('connect', () => {
268 socket.disconnect()
269 next(new Error('Connected with an invalid token.'))
270 })
271 })
272
273 it('Should success with the correct token', function (next) {
274 const socket = io(`http://localhost:${server.port}/user-notifications`, {
275 query: { accessToken: server.accessToken },
276 reconnection: false
277 })
278
279 function errorListener (err) {
280 next(new Error('Error in connection: ' + err))
281 }
282
283 socket.on('connect_error', errorListener)
284
285 socket.once('connect', async () => {
286 socket.disconnect()
287
288 await wait(500)
289 next()
290 })
291 })
292 })
293
294 after(async function () {
295 await cleanupTests([ server ])
296 })
297 })