]>
Commit | Line | Data |
---|---|---|
a1587156 | 1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ |
cef534ed C |
2 | |
3 | import 'mocha' | |
fce7fe04 | 4 | import { io } from 'socket.io-client' |
cef534ed C |
5 | |
6 | import { | |
7c3b7976 C |
7 | cleanupTests, |
8 | flushAndRunServer, | |
cef534ed | 9 | immutableAssign, |
cef534ed C |
10 | makeGetRequest, |
11 | makePostBodyRequest, | |
12 | makePutBodyRequest, | |
cef534ed C |
13 | ServerInfo, |
14 | setAccessTokensToServers, | |
15 | wait | |
94565d52 | 16 | } from '../../../../shared/extra-utils' |
cef534ed C |
17 | import { |
18 | checkBadCountPagination, | |
19 | checkBadSortPagination, | |
20 | checkBadStartPagination | |
94565d52 | 21 | } from '../../../../shared/extra-utils/requests/check-api-params' |
cef534ed C |
22 | import { UserNotificationSetting, UserNotificationSettingValue } from '../../../../shared/models/users' |
23 | ||
24 | describe('Test user notifications API validators', function () { | |
25 | let server: ServerInfo | |
26 | ||
27 | // --------------------------------------------------------------- | |
28 | ||
29 | before(async function () { | |
30 | this.timeout(30000) | |
31 | ||
210feb6c | 32 | server = await flushAndRunServer(1) |
cef534ed C |
33 | |
34 | await setAccessTokensToServers([ server ]) | |
35 | }) | |
36 | ||
37 | describe('When listing my notifications', function () { | |
38 | const path = '/api/v1/users/me/notifications' | |
39 | ||
40 | it('Should fail with a bad start pagination', async function () { | |
41 | await checkBadStartPagination(server.url, path, server.accessToken) | |
42 | }) | |
43 | ||
44 | it('Should fail with a bad count pagination', async function () { | |
45 | await checkBadCountPagination(server.url, path, server.accessToken) | |
46 | }) | |
47 | ||
48 | it('Should fail with an incorrect sort', async function () { | |
49 | await checkBadSortPagination(server.url, path, server.accessToken) | |
50 | }) | |
51 | ||
dc133480 C |
52 | it('Should fail with an incorrect unread parameter', async function () { |
53 | await makeGetRequest({ | |
54 | url: server.url, | |
55 | path, | |
56 | query: { | |
57 | unread: 'toto' | |
58 | }, | |
59 | token: server.accessToken, | |
60 | statusCodeExpected: 200 | |
61 | }) | |
62 | }) | |
63 | ||
cef534ed C |
64 | it('Should fail with a non authenticated user', async function () { |
65 | await makeGetRequest({ | |
66 | url: server.url, | |
67 | path, | |
68 | statusCodeExpected: 401 | |
69 | }) | |
70 | }) | |
71 | ||
72 | it('Should succeed with the correct parameters', async function () { | |
73 | await makeGetRequest({ | |
74 | url: server.url, | |
75 | path, | |
76 | token: server.accessToken, | |
77 | statusCodeExpected: 200 | |
78 | }) | |
79 | }) | |
80 | }) | |
81 | ||
82 | describe('When marking as read my notifications', function () { | |
83 | const path = '/api/v1/users/me/notifications/read' | |
84 | ||
85 | it('Should fail with wrong ids parameters', async function () { | |
86 | await makePostBodyRequest({ | |
87 | url: server.url, | |
88 | path, | |
89 | fields: { | |
90 | ids: [ 'hello' ] | |
91 | }, | |
92 | token: server.accessToken, | |
93 | statusCodeExpected: 400 | |
94 | }) | |
95 | ||
2f1548fd C |
96 | await makePostBodyRequest({ |
97 | url: server.url, | |
98 | path, | |
99 | fields: { | |
100 | ids: [ ] | |
101 | }, | |
102 | token: server.accessToken, | |
103 | statusCodeExpected: 400 | |
104 | }) | |
105 | ||
cef534ed C |
106 | await makePostBodyRequest({ |
107 | url: server.url, | |
108 | path, | |
109 | fields: { | |
110 | ids: 5 | |
111 | }, | |
112 | token: server.accessToken, | |
113 | statusCodeExpected: 400 | |
114 | }) | |
115 | }) | |
116 | ||
117 | it('Should fail with a non authenticated user', async function () { | |
118 | await makePostBodyRequest({ | |
119 | url: server.url, | |
120 | path, | |
121 | fields: { | |
122 | ids: [ 5 ] | |
123 | }, | |
124 | statusCodeExpected: 401 | |
125 | }) | |
126 | }) | |
127 | ||
128 | it('Should succeed with the correct parameters', async function () { | |
129 | await makePostBodyRequest({ | |
130 | url: server.url, | |
131 | path, | |
132 | fields: { | |
133 | ids: [ 5 ] | |
134 | }, | |
135 | token: server.accessToken, | |
136 | statusCodeExpected: 204 | |
137 | }) | |
138 | }) | |
139 | }) | |
140 | ||
2f1548fd C |
141 | describe('When marking as read my notifications', function () { |
142 | const path = '/api/v1/users/me/notifications/read-all' | |
143 | ||
144 | it('Should fail with a non authenticated user', async function () { | |
145 | await makePostBodyRequest({ | |
146 | url: server.url, | |
147 | path, | |
148 | statusCodeExpected: 401 | |
149 | }) | |
150 | }) | |
151 | ||
152 | it('Should succeed with the correct parameters', async function () { | |
153 | await makePostBodyRequest({ | |
154 | url: server.url, | |
155 | path, | |
156 | token: server.accessToken, | |
157 | statusCodeExpected: 204 | |
158 | }) | |
159 | }) | |
160 | }) | |
161 | ||
cef534ed C |
162 | describe('When updating my notification settings', function () { |
163 | const path = '/api/v1/users/me/notification-settings' | |
164 | const correctFields: UserNotificationSetting = { | |
2f1548fd C |
165 | newVideoFromSubscription: UserNotificationSettingValue.WEB, |
166 | newCommentOnMyVideo: UserNotificationSettingValue.WEB, | |
4f32032f | 167 | abuseAsModerator: UserNotificationSettingValue.WEB, |
7ccddd7b | 168 | videoAutoBlacklistAsModerator: UserNotificationSettingValue.WEB, |
2f1548fd C |
169 | blacklistOnMyVideo: UserNotificationSettingValue.WEB, |
170 | myVideoImportFinished: UserNotificationSettingValue.WEB, | |
171 | myVideoPublished: UserNotificationSettingValue.WEB, | |
172 | commentMention: UserNotificationSettingValue.WEB, | |
173 | newFollow: UserNotificationSettingValue.WEB, | |
883993c8 | 174 | newUserRegistration: UserNotificationSettingValue.WEB, |
8424c402 | 175 | newInstanceFollower: UserNotificationSettingValue.WEB, |
594d3e48 C |
176 | autoInstanceFollowing: UserNotificationSettingValue.WEB, |
177 | abuseNewMessage: UserNotificationSettingValue.WEB, | |
178 | abuseStateChange: UserNotificationSettingValue.WEB | |
cef534ed C |
179 | } |
180 | ||
181 | it('Should fail with missing fields', async function () { | |
182 | await makePutBodyRequest({ | |
183 | url: server.url, | |
184 | path, | |
185 | token: server.accessToken, | |
2f1548fd | 186 | fields: { newVideoFromSubscription: UserNotificationSettingValue.WEB }, |
cef534ed C |
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 () { | |
fce7fe04 | 238 | |
cef534ed | 239 | it('Should fail with no token', function (next) { |
7c3b7976 | 240 | const socket = io(`http://localhost:${server.port}/user-notifications`, { reconnection: false }) |
cef534ed | 241 | |
fce7fe04 | 242 | socket.once('connect_error', function () { |
cef534ed C |
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) { | |
7c3b7976 | 254 | const socket = io(`http://localhost:${server.port}/user-notifications`, { |
cef534ed C |
255 | query: { accessToken: 'bad_access_token' }, |
256 | reconnection: false | |
257 | }) | |
258 | ||
fce7fe04 | 259 | socket.once('connect_error', function () { |
cef534ed C |
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) { | |
7c3b7976 | 271 | const socket = io(`http://localhost:${server.port}/user-notifications`, { |
cef534ed C |
272 | query: { accessToken: server.accessToken }, |
273 | reconnection: false | |
274 | }) | |
275 | ||
99fa5c5b | 276 | function errorListener (err) { |
cef534ed | 277 | next(new Error('Error in connection: ' + err)) |
99fa5c5b C |
278 | } |
279 | ||
fce7fe04 | 280 | socket.on('connect_error', errorListener) |
cef534ed | 281 | |
fce7fe04 | 282 | socket.once('connect', async () => { |
cef534ed C |
283 | socket.disconnect() |
284 | ||
285 | await wait(500) | |
286 | next() | |
287 | }) | |
288 | }) | |
289 | }) | |
290 | ||
7c3b7976 C |
291 | after(async function () { |
292 | await cleanupTests([ server ]) | |
cef534ed C |
293 | }) |
294 | }) |