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