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