635f5c9a3650ca62e779f0679e31a775e42c8fa1
[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: 5
104         },
105         token: server.accessToken,
106         statusCodeExpected: 400
107       })
108     })
109
110     it('Should fail with a non authenticated user', async function () {
111       await makePostBodyRequest({
112         url: server.url,
113         path,
114         fields: {
115           ids: [ 5 ]
116         },
117         statusCodeExpected: 401
118       })
119     })
120
121     it('Should succeed with the correct parameters', async function () {
122       await makePostBodyRequest({
123         url: server.url,
124         path,
125         fields: {
126           ids: [ 5 ]
127         },
128         token: server.accessToken,
129         statusCodeExpected: 204
130       })
131     })
132   })
133
134   describe('When updating my notification settings', function () {
135     const path = '/api/v1/users/me/notification-settings'
136     const correctFields: UserNotificationSetting = {
137       newVideoFromSubscription: UserNotificationSettingValue.WEB_NOTIFICATION,
138       newCommentOnMyVideo: UserNotificationSettingValue.WEB_NOTIFICATION,
139       videoAbuseAsModerator: UserNotificationSettingValue.WEB_NOTIFICATION,
140       blacklistOnMyVideo: UserNotificationSettingValue.WEB_NOTIFICATION,
141       myVideoImportFinished: UserNotificationSettingValue.WEB_NOTIFICATION,
142       myVideoPublished: UserNotificationSettingValue.WEB_NOTIFICATION,
143       commentMention: UserNotificationSettingValue.WEB_NOTIFICATION,
144       newFollow: UserNotificationSettingValue.WEB_NOTIFICATION,
145       newUserRegistration: UserNotificationSettingValue.WEB_NOTIFICATION
146     }
147
148     it('Should fail with missing fields', async function () {
149       await makePutBodyRequest({
150         url: server.url,
151         path,
152         token: server.accessToken,
153         fields: { newVideoFromSubscription: UserNotificationSettingValue.WEB_NOTIFICATION },
154         statusCodeExpected: 400
155       })
156     })
157
158     it('Should fail with incorrect field values', async function () {
159       {
160         const fields = immutableAssign(correctFields, { newCommentOnMyVideo: 15 })
161
162         await makePutBodyRequest({
163           url: server.url,
164           path,
165           token: server.accessToken,
166           fields,
167           statusCodeExpected: 400
168         })
169       }
170
171       {
172         const fields = immutableAssign(correctFields, { newCommentOnMyVideo: 'toto' })
173
174         await makePutBodyRequest({
175           url: server.url,
176           path,
177           fields,
178           token: server.accessToken,
179           statusCodeExpected: 400
180         })
181       }
182     })
183
184     it('Should fail with a non authenticated user', async function () {
185       await makePutBodyRequest({
186         url: server.url,
187         path,
188         fields: correctFields,
189         statusCodeExpected: 401
190       })
191     })
192
193     it('Should succeed with the correct parameters', async function () {
194       await makePutBodyRequest({
195         url: server.url,
196         path,
197         token: server.accessToken,
198         fields: correctFields,
199         statusCodeExpected: 204
200       })
201     })
202   })
203
204   describe('When connecting to my notification socket', function () {
205     it('Should fail with no token', function (next) {
206       const socket = io('http://localhost:9001/user-notifications', { reconnection: false })
207
208       socket.on('error', () => {
209         socket.removeListener('error', this)
210         socket.disconnect()
211         next()
212       })
213
214       socket.on('connect', () => {
215         socket.disconnect()
216         next(new Error('Connected with a missing token.'))
217       })
218     })
219
220     it('Should fail with an invalid token', function (next) {
221       const socket = io('http://localhost:9001/user-notifications', {
222         query: { accessToken: 'bad_access_token' },
223         reconnection: false
224       })
225
226       socket.on('error', () => {
227         socket.removeListener('error', this)
228         socket.disconnect()
229         next()
230       })
231
232       socket.on('connect', () => {
233         socket.disconnect()
234         next(new Error('Connected with an invalid token.'))
235       })
236     })
237
238     it('Should success with the correct token', function (next) {
239       const socket = io('http://localhost:9001/user-notifications', {
240         query: { accessToken: server.accessToken },
241         reconnection: false
242       })
243
244       const errorListener = socket.on('error', err => {
245         next(new Error('Error in connection: ' + err))
246       })
247
248       socket.on('connect', async () => {
249         socket.removeListener('error', errorListener)
250         socket.disconnect()
251
252         await wait(500)
253         next()
254       })
255     })
256   })
257
258   after(async function () {
259     killallServers([ server ])
260
261     // Keep the logs if the test failed
262     if (this['ok']) {
263       await flushTests()
264     }
265   })
266 })