]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/check-params/user-notifications.ts
05a78b0adc320d49940fe44788e7455ac42ce5fb
[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 }
181
182 it('Should fail with missing fields', async function () {
183 await makePutBodyRequest({
184 url: server.url,
185 path,
186 token: server.accessToken,
187 fields: { newVideoFromSubscription: UserNotificationSettingValue.WEB },
188 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
189 })
190 })
191
192 it('Should fail with incorrect field values', async function () {
193 {
194 const fields = immutableAssign(correctFields, { newCommentOnMyVideo: 15 })
195
196 await makePutBodyRequest({
197 url: server.url,
198 path,
199 token: server.accessToken,
200 fields,
201 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
202 })
203 }
204
205 {
206 const fields = immutableAssign(correctFields, { newCommentOnMyVideo: 'toto' })
207
208 await makePutBodyRequest({
209 url: server.url,
210 path,
211 fields,
212 token: server.accessToken,
213 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
214 })
215 }
216 })
217
218 it('Should fail with a non authenticated user', async function () {
219 await makePutBodyRequest({
220 url: server.url,
221 path,
222 fields: correctFields,
223 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
224 })
225 })
226
227 it('Should succeed with the correct parameters', async function () {
228 await makePutBodyRequest({
229 url: server.url,
230 path,
231 token: server.accessToken,
232 fields: correctFields,
233 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
234 })
235 })
236 })
237
238 describe('When connecting to my notification socket', function () {
239
240 it('Should fail with no token', function (next) {
241 const socket = io(`http://localhost:${server.port}/user-notifications`, { reconnection: false })
242
243 socket.once('connect_error', function () {
244 socket.disconnect()
245 next()
246 })
247
248 socket.on('connect', () => {
249 socket.disconnect()
250 next(new Error('Connected with a missing token.'))
251 })
252 })
253
254 it('Should fail with an invalid token', function (next) {
255 const socket = io(`http://localhost:${server.port}/user-notifications`, {
256 query: { accessToken: 'bad_access_token' },
257 reconnection: false
258 })
259
260 socket.once('connect_error', function () {
261 socket.disconnect()
262 next()
263 })
264
265 socket.on('connect', () => {
266 socket.disconnect()
267 next(new Error('Connected with an invalid token.'))
268 })
269 })
270
271 it('Should success with the correct token', function (next) {
272 const socket = io(`http://localhost:${server.port}/user-notifications`, {
273 query: { accessToken: server.accessToken },
274 reconnection: false
275 })
276
277 function errorListener (err) {
278 next(new Error('Error in connection: ' + err))
279 }
280
281 socket.on('connect_error', errorListener)
282
283 socket.once('connect', async () => {
284 socket.disconnect()
285
286 await wait(500)
287 next()
288 })
289 })
290 })
291
292 after(async function () {
293 await cleanupTests([ server ])
294 })
295 })