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