]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/check-params/user-notifications.ts
Add import finished and video published notifs
[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: 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 }
144
145 it('Should fail with missing fields', async function () {
146 await makePutBodyRequest({
147 url: server.url,
148 path,
149 token: server.accessToken,
150 fields: { newVideoFromSubscription: UserNotificationSettingValue.WEB_NOTIFICATION },
151 statusCodeExpected: 400
152 })
153 })
154
155 it('Should fail with incorrect field values', async function () {
156 {
157 const fields = immutableAssign(correctFields, { newCommentOnMyVideo: 15 })
158
159 await makePutBodyRequest({
160 url: server.url,
161 path,
162 token: server.accessToken,
163 fields,
164 statusCodeExpected: 400
165 })
166 }
167
168 {
169 const fields = immutableAssign(correctFields, { newCommentOnMyVideo: 'toto' })
170
171 await makePutBodyRequest({
172 url: server.url,
173 path,
174 fields,
175 token: server.accessToken,
176 statusCodeExpected: 400
177 })
178 }
179 })
180
181 it('Should fail with a non authenticated user', async function () {
182 await makePutBodyRequest({
183 url: server.url,
184 path,
185 fields: correctFields,
186 statusCodeExpected: 401
187 })
188 })
189
190 it('Should succeed with the correct parameters', async function () {
191 await makePutBodyRequest({
192 url: server.url,
193 path,
194 token: server.accessToken,
195 fields: correctFields,
196 statusCodeExpected: 204
197 })
198 })
199 })
200
201 describe('When connecting to my notification socket', function () {
202 it('Should fail with no token', function (next) {
203 const socket = io('http://localhost:9001/user-notifications', { reconnection: false })
204
205 socket.on('error', () => {
206 socket.removeListener('error', this)
207 socket.disconnect()
208 next()
209 })
210
211 socket.on('connect', () => {
212 socket.disconnect()
213 next(new Error('Connected with a missing token.'))
214 })
215 })
216
217 it('Should fail with an invalid token', function (next) {
218 const socket = io('http://localhost:9001/user-notifications', {
219 query: { accessToken: 'bad_access_token' },
220 reconnection: false
221 })
222
223 socket.on('error', () => {
224 socket.removeListener('error', this)
225 socket.disconnect()
226 next()
227 })
228
229 socket.on('connect', () => {
230 socket.disconnect()
231 next(new Error('Connected with an invalid token.'))
232 })
233 })
234
235 it('Should success with the correct token', function (next) {
236 const socket = io('http://localhost:9001/user-notifications', {
237 query: { accessToken: server.accessToken },
238 reconnection: false
239 })
240
241 const errorListener = socket.on('error', err => {
242 next(new Error('Error in connection: ' + err))
243 })
244
245 socket.on('connect', async () => {
246 socket.removeListener('error', errorListener)
247 socket.disconnect()
248
249 await wait(500)
250 next()
251 })
252 })
253 })
254
255 after(async function () {
256 killallServers([ server ])
257
258 // Keep the logs if the test failed
259 if (this['ok']) {
260 await flushTests()
261 }
262 })
263 })