]>
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 | 5 | import { |
6c5065a0 C |
6 | checkBadCountPagination, |
7 | checkBadSortPagination, | |
8 | checkBadStartPagination, | |
7c3b7976 | 9 | cleanupTests, |
254d3579 | 10 | createSingleServer, |
cef534ed C |
11 | makeGetRequest, |
12 | makePostBodyRequest, | |
13 | makePutBodyRequest, | |
254d3579 | 14 | PeerTubeServer, |
cef534ed C |
15 | setAccessTokensToServers, |
16 | wait | |
6c5065a0 | 17 | } from '@shared/extra-utils' |
4c7e60bc | 18 | import { HttpStatusCode, UserNotificationSetting, UserNotificationSettingValue } from '@shared/models' |
cef534ed C |
19 | |
20 | describe('Test user notifications API validators', function () { | |
254d3579 | 21 | let server: PeerTubeServer |
cef534ed C |
22 | |
23 | // --------------------------------------------------------------- | |
24 | ||
25 | before(async function () { | |
26 | this.timeout(30000) | |
27 | ||
254d3579 | 28 | server = await createSingleServer(1) |
cef534ed C |
29 | |
30 | await setAccessTokensToServers([ server ]) | |
31 | }) | |
32 | ||
33 | describe('When listing my notifications', function () { | |
34 | const path = '/api/v1/users/me/notifications' | |
35 | ||
36 | it('Should fail with a bad start pagination', async function () { | |
37 | await checkBadStartPagination(server.url, path, server.accessToken) | |
38 | }) | |
39 | ||
40 | it('Should fail with a bad count pagination', async function () { | |
41 | await checkBadCountPagination(server.url, path, server.accessToken) | |
42 | }) | |
43 | ||
44 | it('Should fail with an incorrect sort', async function () { | |
45 | await checkBadSortPagination(server.url, path, server.accessToken) | |
46 | }) | |
47 | ||
dc133480 C |
48 | it('Should fail with an incorrect unread parameter', async function () { |
49 | await makeGetRequest({ | |
50 | url: server.url, | |
51 | path, | |
52 | query: { | |
53 | unread: 'toto' | |
54 | }, | |
55 | token: server.accessToken, | |
c0e8b12e | 56 | expectedStatus: HttpStatusCode.OK_200 |
dc133480 C |
57 | }) |
58 | }) | |
59 | ||
cef534ed C |
60 | it('Should fail with a non authenticated user', async function () { |
61 | await makeGetRequest({ | |
62 | url: server.url, | |
63 | path, | |
c0e8b12e | 64 | expectedStatus: HttpStatusCode.UNAUTHORIZED_401 |
cef534ed C |
65 | }) |
66 | }) | |
67 | ||
68 | it('Should succeed with the correct parameters', async function () { | |
69 | await makeGetRequest({ | |
70 | url: server.url, | |
71 | path, | |
72 | token: server.accessToken, | |
c0e8b12e | 73 | expectedStatus: HttpStatusCode.OK_200 |
cef534ed C |
74 | }) |
75 | }) | |
76 | }) | |
77 | ||
78 | describe('When marking as read my notifications', function () { | |
79 | const path = '/api/v1/users/me/notifications/read' | |
80 | ||
81 | it('Should fail with wrong ids parameters', async function () { | |
82 | await makePostBodyRequest({ | |
83 | url: server.url, | |
84 | path, | |
85 | fields: { | |
86 | ids: [ 'hello' ] | |
87 | }, | |
88 | token: server.accessToken, | |
c0e8b12e | 89 | expectedStatus: HttpStatusCode.BAD_REQUEST_400 |
cef534ed C |
90 | }) |
91 | ||
2f1548fd C |
92 | await makePostBodyRequest({ |
93 | url: server.url, | |
94 | path, | |
95 | fields: { | |
96 | ids: [ ] | |
97 | }, | |
98 | token: server.accessToken, | |
c0e8b12e | 99 | expectedStatus: HttpStatusCode.BAD_REQUEST_400 |
2f1548fd C |
100 | }) |
101 | ||
cef534ed C |
102 | await makePostBodyRequest({ |
103 | url: server.url, | |
104 | path, | |
105 | fields: { | |
106 | ids: 5 | |
107 | }, | |
108 | token: server.accessToken, | |
c0e8b12e | 109 | expectedStatus: HttpStatusCode.BAD_REQUEST_400 |
cef534ed C |
110 | }) |
111 | }) | |
112 | ||
113 | it('Should fail with a non authenticated user', async function () { | |
114 | await makePostBodyRequest({ | |
115 | url: server.url, | |
116 | path, | |
117 | fields: { | |
118 | ids: [ 5 ] | |
119 | }, | |
c0e8b12e | 120 | expectedStatus: HttpStatusCode.UNAUTHORIZED_401 |
cef534ed C |
121 | }) |
122 | }) | |
123 | ||
124 | it('Should succeed with the correct parameters', async function () { | |
125 | await makePostBodyRequest({ | |
126 | url: server.url, | |
127 | path, | |
128 | fields: { | |
129 | ids: [ 5 ] | |
130 | }, | |
131 | token: server.accessToken, | |
c0e8b12e | 132 | expectedStatus: HttpStatusCode.NO_CONTENT_204 |
cef534ed C |
133 | }) |
134 | }) | |
135 | }) | |
136 | ||
2f1548fd C |
137 | describe('When marking as read my notifications', function () { |
138 | const path = '/api/v1/users/me/notifications/read-all' | |
139 | ||
140 | it('Should fail with a non authenticated user', async function () { | |
141 | await makePostBodyRequest({ | |
142 | url: server.url, | |
143 | path, | |
c0e8b12e | 144 | expectedStatus: HttpStatusCode.UNAUTHORIZED_401 |
2f1548fd C |
145 | }) |
146 | }) | |
147 | ||
148 | it('Should succeed with the correct parameters', async function () { | |
149 | await makePostBodyRequest({ | |
150 | url: server.url, | |
151 | path, | |
152 | token: server.accessToken, | |
c0e8b12e | 153 | expectedStatus: HttpStatusCode.NO_CONTENT_204 |
2f1548fd C |
154 | }) |
155 | }) | |
156 | }) | |
157 | ||
cef534ed C |
158 | describe('When updating my notification settings', function () { |
159 | const path = '/api/v1/users/me/notification-settings' | |
160 | const correctFields: UserNotificationSetting = { | |
2f1548fd C |
161 | newVideoFromSubscription: UserNotificationSettingValue.WEB, |
162 | newCommentOnMyVideo: UserNotificationSettingValue.WEB, | |
4f32032f | 163 | abuseAsModerator: UserNotificationSettingValue.WEB, |
7ccddd7b | 164 | videoAutoBlacklistAsModerator: UserNotificationSettingValue.WEB, |
2f1548fd C |
165 | blacklistOnMyVideo: UserNotificationSettingValue.WEB, |
166 | myVideoImportFinished: UserNotificationSettingValue.WEB, | |
167 | myVideoPublished: UserNotificationSettingValue.WEB, | |
168 | commentMention: UserNotificationSettingValue.WEB, | |
169 | newFollow: UserNotificationSettingValue.WEB, | |
883993c8 | 170 | newUserRegistration: UserNotificationSettingValue.WEB, |
8424c402 | 171 | newInstanceFollower: UserNotificationSettingValue.WEB, |
594d3e48 C |
172 | autoInstanceFollowing: UserNotificationSettingValue.WEB, |
173 | abuseNewMessage: UserNotificationSettingValue.WEB, | |
32a18cbf C |
174 | abuseStateChange: UserNotificationSettingValue.WEB, |
175 | newPeerTubeVersion: UserNotificationSettingValue.WEB, | |
176 | newPluginVersion: UserNotificationSettingValue.WEB | |
cef534ed C |
177 | } |
178 | ||
179 | it('Should fail with missing fields', async function () { | |
180 | await makePutBodyRequest({ | |
181 | url: server.url, | |
182 | path, | |
183 | token: server.accessToken, | |
2f1548fd | 184 | fields: { newVideoFromSubscription: UserNotificationSettingValue.WEB }, |
c0e8b12e | 185 | expectedStatus: HttpStatusCode.BAD_REQUEST_400 |
cef534ed C |
186 | }) |
187 | }) | |
188 | ||
189 | it('Should fail with incorrect field values', async function () { | |
190 | { | |
6c5065a0 | 191 | const fields = { ...correctFields, newCommentOnMyVideo: 15 } |
cef534ed C |
192 | |
193 | await makePutBodyRequest({ | |
194 | url: server.url, | |
195 | path, | |
196 | token: server.accessToken, | |
197 | fields, | |
c0e8b12e | 198 | expectedStatus: HttpStatusCode.BAD_REQUEST_400 |
cef534ed C |
199 | }) |
200 | } | |
201 | ||
202 | { | |
6c5065a0 | 203 | const fields = { ...correctFields, newCommentOnMyVideo: 'toto' } |
cef534ed C |
204 | |
205 | await makePutBodyRequest({ | |
206 | url: server.url, | |
207 | path, | |
208 | fields, | |
209 | token: server.accessToken, | |
c0e8b12e | 210 | expectedStatus: HttpStatusCode.BAD_REQUEST_400 |
cef534ed C |
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, | |
c0e8b12e | 220 | expectedStatus: HttpStatusCode.UNAUTHORIZED_401 |
cef534ed C |
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, | |
c0e8b12e | 230 | expectedStatus: HttpStatusCode.NO_CONTENT_204 |
cef534ed C |
231 | }) |
232 | }) | |
233 | }) | |
234 | ||
235 | describe('When connecting to my notification socket', function () { | |
fce7fe04 | 236 | |
cef534ed | 237 | it('Should fail with no token', function (next) { |
7c3b7976 | 238 | const socket = io(`http://localhost:${server.port}/user-notifications`, { reconnection: false }) |
cef534ed | 239 | |
fce7fe04 | 240 | socket.once('connect_error', function () { |
cef534ed C |
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) { | |
7c3b7976 | 252 | const socket = io(`http://localhost:${server.port}/user-notifications`, { |
cef534ed C |
253 | query: { accessToken: 'bad_access_token' }, |
254 | reconnection: false | |
255 | }) | |
256 | ||
fce7fe04 | 257 | socket.once('connect_error', function () { |
cef534ed C |
258 | socket.disconnect() |
259 | next() | |
260 | }) | |
261 | ||
262 | socket.on('connect', () => { | |
263 | socket.disconnect() | |
264 | next(new Error('Connected with an invalid token.')) | |
265 | }) | |
266 | }) | |
267 | ||
268 | it('Should success with the correct token', function (next) { | |
7c3b7976 | 269 | const socket = io(`http://localhost:${server.port}/user-notifications`, { |
cef534ed C |
270 | query: { accessToken: server.accessToken }, |
271 | reconnection: false | |
272 | }) | |
273 | ||
99fa5c5b | 274 | function errorListener (err) { |
cef534ed | 275 | next(new Error('Error in connection: ' + err)) |
99fa5c5b C |
276 | } |
277 | ||
fce7fe04 | 278 | socket.on('connect_error', errorListener) |
cef534ed | 279 | |
fce7fe04 | 280 | socket.once('connect', async () => { |
cef534ed C |
281 | socket.disconnect() |
282 | ||
283 | await wait(500) | |
284 | next() | |
285 | }) | |
286 | }) | |
287 | }) | |
288 | ||
7c3b7976 C |
289 | after(async function () { |
290 | await cleanupTests([ server ]) | |
cef534ed C |
291 | }) |
292 | }) |