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