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