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