714f481e9a2dba71659e96c44a7f1162640885f0
[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       blacklistOnMyVideo: UserNotificationSettingValue.WEB,
172       myVideoImportFinished: UserNotificationSettingValue.WEB,
173       myVideoPublished: UserNotificationSettingValue.WEB,
174       commentMention: UserNotificationSettingValue.WEB,
175       newFollow: UserNotificationSettingValue.WEB,
176       newUserRegistration: 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:9001/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:9001/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:9001/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     killallServers([ server ])
291
292     // Keep the logs if the test failed
293     if (this['ok']) {
294       await flushTests()
295     }
296   })
297 })