]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/check-params/user-notifications.ts
Add next to stats github ci
[github/Chocobozzz/PeerTube.git] / server / tests / api / check-params / user-notifications.ts
CommitLineData
a1587156 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
cef534ed
C
2
3import 'mocha'
fce7fe04 4import { io } from 'socket.io-client'
cef534ed
C
5
6import {
7c3b7976
C
7 cleanupTests,
8 flushAndRunServer,
cef534ed 9 immutableAssign,
cef534ed
C
10 makeGetRequest,
11 makePostBodyRequest,
12 makePutBodyRequest,
cef534ed
C
13 ServerInfo,
14 setAccessTokensToServers,
15 wait
94565d52 16} from '../../../../shared/extra-utils'
cef534ed
C
17import {
18 checkBadCountPagination,
19 checkBadSortPagination,
20 checkBadStartPagination
94565d52 21} from '../../../../shared/extra-utils/requests/check-api-params'
cef534ed 22import { UserNotificationSetting, UserNotificationSettingValue } from '../../../../shared/models/users'
2d53be02 23import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
cef534ed
C
24
25describe('Test user notifications API validators', function () {
26 let server: ServerInfo
27
28 // ---------------------------------------------------------------
29
30 before(async function () {
31 this.timeout(30000)
32
210feb6c 33 server = await flushAndRunServer(1)
cef534ed
C
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
dc133480
C
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,
2d53be02 61 statusCodeExpected: HttpStatusCode.OK_200
dc133480
C
62 })
63 })
64
cef534ed
C
65 it('Should fail with a non authenticated user', async function () {
66 await makeGetRequest({
67 url: server.url,
68 path,
2d53be02 69 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
cef534ed
C
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,
2d53be02 78 statusCodeExpected: HttpStatusCode.OK_200
cef534ed
C
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,
2d53be02 94 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
cef534ed
C
95 })
96
2f1548fd
C
97 await makePostBodyRequest({
98 url: server.url,
99 path,
100 fields: {
101 ids: [ ]
102 },
103 token: server.accessToken,
2d53be02 104 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
2f1548fd
C
105 })
106
cef534ed
C
107 await makePostBodyRequest({
108 url: server.url,
109 path,
110 fields: {
111 ids: 5
112 },
113 token: server.accessToken,
2d53be02 114 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
cef534ed
C
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 },
2d53be02 125 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
cef534ed
C
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,
2d53be02 137 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
cef534ed
C
138 })
139 })
140 })
141
2f1548fd
C
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,
2d53be02 149 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
2f1548fd
C
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,
2d53be02 158 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
2f1548fd
C
159 })
160 })
161 })
162
cef534ed
C
163 describe('When updating my notification settings', function () {
164 const path = '/api/v1/users/me/notification-settings'
165 const correctFields: UserNotificationSetting = {
2f1548fd
C
166 newVideoFromSubscription: UserNotificationSettingValue.WEB,
167 newCommentOnMyVideo: UserNotificationSettingValue.WEB,
4f32032f 168 abuseAsModerator: UserNotificationSettingValue.WEB,
7ccddd7b 169 videoAutoBlacklistAsModerator: UserNotificationSettingValue.WEB,
2f1548fd
C
170 blacklistOnMyVideo: UserNotificationSettingValue.WEB,
171 myVideoImportFinished: UserNotificationSettingValue.WEB,
172 myVideoPublished: UserNotificationSettingValue.WEB,
173 commentMention: UserNotificationSettingValue.WEB,
174 newFollow: UserNotificationSettingValue.WEB,
883993c8 175 newUserRegistration: UserNotificationSettingValue.WEB,
8424c402 176 newInstanceFollower: UserNotificationSettingValue.WEB,
594d3e48
C
177 autoInstanceFollowing: UserNotificationSettingValue.WEB,
178 abuseNewMessage: UserNotificationSettingValue.WEB,
179 abuseStateChange: UserNotificationSettingValue.WEB
cef534ed
C
180 }
181
182 it('Should fail with missing fields', async function () {
183 await makePutBodyRequest({
184 url: server.url,
185 path,
186 token: server.accessToken,
2f1548fd 187 fields: { newVideoFromSubscription: UserNotificationSettingValue.WEB },
2d53be02 188 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
cef534ed
C
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,
2d53be02 201 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
cef534ed
C
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,
2d53be02 213 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
cef534ed
C
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,
2d53be02 223 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
cef534ed
C
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,
2d53be02 233 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
cef534ed
C
234 })
235 })
236 })
237
238 describe('When connecting to my notification socket', function () {
fce7fe04 239
cef534ed 240 it('Should fail with no token', function (next) {
7c3b7976 241 const socket = io(`http://localhost:${server.port}/user-notifications`, { reconnection: false })
cef534ed 242
fce7fe04 243 socket.once('connect_error', function () {
cef534ed
C
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) {
7c3b7976 255 const socket = io(`http://localhost:${server.port}/user-notifications`, {
cef534ed
C
256 query: { accessToken: 'bad_access_token' },
257 reconnection: false
258 })
259
fce7fe04 260 socket.once('connect_error', function () {
cef534ed
C
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) {
7c3b7976 272 const socket = io(`http://localhost:${server.port}/user-notifications`, {
cef534ed
C
273 query: { accessToken: server.accessToken },
274 reconnection: false
275 })
276
99fa5c5b 277 function errorListener (err) {
cef534ed 278 next(new Error('Error in connection: ' + err))
99fa5c5b
C
279 }
280
fce7fe04 281 socket.on('connect_error', errorListener)
cef534ed 282
fce7fe04 283 socket.once('connect', async () => {
cef534ed
C
284 socket.disconnect()
285
286 await wait(500)
287 next()
288 })
289 })
290 })
291
7c3b7976
C
292 after(async function () {
293 await cleanupTests([ server ])
cef534ed
C
294 })
295})