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