]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/check-params/user-notifications.ts
Add playback metric endpoint sent to OTEL
[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'
c55e3d72
C
5import { checkBadCountPagination, checkBadSortPagination, checkBadStartPagination } from '@server/tests/shared'
6import { wait } from '@shared/core-utils'
7import { HttpStatusCode, UserNotificationSetting, UserNotificationSettingValue } from '@shared/models'
cef534ed 8import {
7c3b7976 9 cleanupTests,
254d3579 10 createSingleServer,
cef534ed
C
11 makeGetRequest,
12 makePostBodyRequest,
13 makePutBodyRequest,
254d3579 14 PeerTubeServer,
c55e3d72 15 setAccessTokensToServers
bf54587a 16} from '@shared/server-commands'
cef534ed
C
17
18describe('Test user notifications API validators', function () {
254d3579 19 let server: PeerTubeServer
cef534ed
C
20
21 // ---------------------------------------------------------------
22
23 before(async function () {
24 this.timeout(30000)
25
254d3579 26 server = await createSingleServer(1)
cef534ed
C
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
dc133480
C
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,
c0e8b12e 54 expectedStatus: HttpStatusCode.OK_200
dc133480
C
55 })
56 })
57
cef534ed
C
58 it('Should fail with a non authenticated user', async function () {
59 await makeGetRequest({
60 url: server.url,
61 path,
c0e8b12e 62 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
cef534ed
C
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,
c0e8b12e 71 expectedStatus: HttpStatusCode.OK_200
cef534ed
C
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,
c0e8b12e 87 expectedStatus: HttpStatusCode.BAD_REQUEST_400
cef534ed
C
88 })
89
2f1548fd
C
90 await makePostBodyRequest({
91 url: server.url,
92 path,
93 fields: {
94 ids: [ ]
95 },
96 token: server.accessToken,
c0e8b12e 97 expectedStatus: HttpStatusCode.BAD_REQUEST_400
2f1548fd
C
98 })
99
cef534ed
C
100 await makePostBodyRequest({
101 url: server.url,
102 path,
103 fields: {
104 ids: 5
105 },
106 token: server.accessToken,
c0e8b12e 107 expectedStatus: HttpStatusCode.BAD_REQUEST_400
cef534ed
C
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 },
c0e8b12e 118 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
cef534ed
C
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,
c0e8b12e 130 expectedStatus: HttpStatusCode.NO_CONTENT_204
cef534ed
C
131 })
132 })
133 })
134
2f1548fd
C
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,
c0e8b12e 142 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
2f1548fd
C
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,
c0e8b12e 151 expectedStatus: HttpStatusCode.NO_CONTENT_204
2f1548fd
C
152 })
153 })
154 })
155
cef534ed
C
156 describe('When updating my notification settings', function () {
157 const path = '/api/v1/users/me/notification-settings'
158 const correctFields: UserNotificationSetting = {
2f1548fd
C
159 newVideoFromSubscription: UserNotificationSettingValue.WEB,
160 newCommentOnMyVideo: UserNotificationSettingValue.WEB,
4f32032f 161 abuseAsModerator: UserNotificationSettingValue.WEB,
7ccddd7b 162 videoAutoBlacklistAsModerator: UserNotificationSettingValue.WEB,
2f1548fd
C
163 blacklistOnMyVideo: UserNotificationSettingValue.WEB,
164 myVideoImportFinished: UserNotificationSettingValue.WEB,
165 myVideoPublished: UserNotificationSettingValue.WEB,
166 commentMention: UserNotificationSettingValue.WEB,
167 newFollow: UserNotificationSettingValue.WEB,
883993c8 168 newUserRegistration: UserNotificationSettingValue.WEB,
8424c402 169 newInstanceFollower: UserNotificationSettingValue.WEB,
594d3e48
C
170 autoInstanceFollowing: UserNotificationSettingValue.WEB,
171 abuseNewMessage: UserNotificationSettingValue.WEB,
32a18cbf
C
172 abuseStateChange: UserNotificationSettingValue.WEB,
173 newPeerTubeVersion: UserNotificationSettingValue.WEB,
92e66e04 174 myVideoStudioEditionFinished: UserNotificationSettingValue.WEB,
32a18cbf 175 newPluginVersion: UserNotificationSettingValue.WEB
cef534ed
C
176 }
177
178 it('Should fail with missing fields', async function () {
179 await makePutBodyRequest({
180 url: server.url,
181 path,
182 token: server.accessToken,
2f1548fd 183 fields: { newVideoFromSubscription: UserNotificationSettingValue.WEB },
c0e8b12e 184 expectedStatus: HttpStatusCode.BAD_REQUEST_400
cef534ed
C
185 })
186 })
187
188 it('Should fail with incorrect field values', async function () {
189 {
6c5065a0 190 const fields = { ...correctFields, newCommentOnMyVideo: 15 }
cef534ed
C
191
192 await makePutBodyRequest({
193 url: server.url,
194 path,
195 token: server.accessToken,
196 fields,
c0e8b12e 197 expectedStatus: HttpStatusCode.BAD_REQUEST_400
cef534ed
C
198 })
199 }
200
201 {
6c5065a0 202 const fields = { ...correctFields, newCommentOnMyVideo: 'toto' }
cef534ed
C
203
204 await makePutBodyRequest({
205 url: server.url,
206 path,
207 fields,
208 token: server.accessToken,
c0e8b12e 209 expectedStatus: HttpStatusCode.BAD_REQUEST_400
cef534ed
C
210 })
211 }
212 })
213
214 it('Should fail with a non authenticated user', async function () {
215 await makePutBodyRequest({
216 url: server.url,
217 path,
218 fields: correctFields,
c0e8b12e 219 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
cef534ed
C
220 })
221 })
222
223 it('Should succeed with the correct parameters', async function () {
224 await makePutBodyRequest({
225 url: server.url,
226 path,
227 token: server.accessToken,
228 fields: correctFields,
c0e8b12e 229 expectedStatus: HttpStatusCode.NO_CONTENT_204
cef534ed
C
230 })
231 })
232 })
233
234 describe('When connecting to my notification socket', function () {
fce7fe04 235
cef534ed 236 it('Should fail with no token', function (next) {
7c3b7976 237 const socket = io(`http://localhost:${server.port}/user-notifications`, { reconnection: false })
cef534ed 238
fce7fe04 239 socket.once('connect_error', function () {
cef534ed
C
240 socket.disconnect()
241 next()
242 })
243
244 socket.on('connect', () => {
245 socket.disconnect()
246 next(new Error('Connected with a missing token.'))
247 })
248 })
249
250 it('Should fail with an invalid token', function (next) {
7c3b7976 251 const socket = io(`http://localhost:${server.port}/user-notifications`, {
cef534ed
C
252 query: { accessToken: 'bad_access_token' },
253 reconnection: false
254 })
255
fce7fe04 256 socket.once('connect_error', function () {
cef534ed
C
257 socket.disconnect()
258 next()
259 })
260
261 socket.on('connect', () => {
262 socket.disconnect()
263 next(new Error('Connected with an invalid token.'))
264 })
265 })
266
267 it('Should success with the correct token', function (next) {
7c3b7976 268 const socket = io(`http://localhost:${server.port}/user-notifications`, {
cef534ed
C
269 query: { accessToken: server.accessToken },
270 reconnection: false
271 })
272
99fa5c5b 273 function errorListener (err) {
cef534ed 274 next(new Error('Error in connection: ' + err))
99fa5c5b
C
275 }
276
fce7fe04 277 socket.on('connect_error', errorListener)
cef534ed 278
fce7fe04 279 socket.once('connect', async () => {
cef534ed
C
280 socket.disconnect()
281
282 await wait(500)
283 next()
284 })
285 })
286 })
287
7c3b7976
C
288 after(async function () {
289 await cleanupTests([ server ])
cef534ed
C
290 })
291})