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