]>
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' |
c0e8b12e | 5 | import { HttpStatusCode } from '@shared/models' |
cef534ed | 6 | import { |
6c5065a0 C |
7 | checkBadCountPagination, |
8 | checkBadSortPagination, | |
9 | checkBadStartPagination, | |
7c3b7976 | 10 | cleanupTests, |
254d3579 | 11 | createSingleServer, |
cef534ed C |
12 | makeGetRequest, |
13 | makePostBodyRequest, | |
14 | makePutBodyRequest, | |
254d3579 | 15 | PeerTubeServer, |
cef534ed C |
16 | setAccessTokensToServers, |
17 | wait | |
6c5065a0 C |
18 | } from '@shared/extra-utils' |
19 | import { UserNotificationSetting, UserNotificationSettingValue } from '@shared/models' | |
cef534ed C |
20 | |
21 | describe('Test user notifications API validators', function () { | |
254d3579 | 22 | let server: PeerTubeServer |
cef534ed C |
23 | |
24 | // --------------------------------------------------------------- | |
25 | ||
26 | before(async function () { | |
27 | this.timeout(30000) | |
28 | ||
254d3579 | 29 | server = await createSingleServer(1) |
cef534ed C |
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 | ||
dc133480 C |
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, | |
c0e8b12e | 57 | expectedStatus: HttpStatusCode.OK_200 |
dc133480 C |
58 | }) |
59 | }) | |
60 | ||
cef534ed C |
61 | it('Should fail with a non authenticated user', async function () { |
62 | await makeGetRequest({ | |
63 | url: server.url, | |
64 | path, | |
c0e8b12e | 65 | expectedStatus: HttpStatusCode.UNAUTHORIZED_401 |
cef534ed C |
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, | |
c0e8b12e | 74 | expectedStatus: HttpStatusCode.OK_200 |
cef534ed C |
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, | |
c0e8b12e | 90 | expectedStatus: HttpStatusCode.BAD_REQUEST_400 |
cef534ed C |
91 | }) |
92 | ||
2f1548fd C |
93 | await makePostBodyRequest({ |
94 | url: server.url, | |
95 | path, | |
96 | fields: { | |
97 | ids: [ ] | |
98 | }, | |
99 | token: server.accessToken, | |
c0e8b12e | 100 | expectedStatus: HttpStatusCode.BAD_REQUEST_400 |
2f1548fd C |
101 | }) |
102 | ||
cef534ed C |
103 | await makePostBodyRequest({ |
104 | url: server.url, | |
105 | path, | |
106 | fields: { | |
107 | ids: 5 | |
108 | }, | |
109 | token: server.accessToken, | |
c0e8b12e | 110 | expectedStatus: HttpStatusCode.BAD_REQUEST_400 |
cef534ed C |
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 | }, | |
c0e8b12e | 121 | expectedStatus: HttpStatusCode.UNAUTHORIZED_401 |
cef534ed C |
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, | |
c0e8b12e | 133 | expectedStatus: HttpStatusCode.NO_CONTENT_204 |
cef534ed C |
134 | }) |
135 | }) | |
136 | }) | |
137 | ||
2f1548fd C |
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, | |
c0e8b12e | 145 | expectedStatus: HttpStatusCode.UNAUTHORIZED_401 |
2f1548fd C |
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, | |
c0e8b12e | 154 | expectedStatus: HttpStatusCode.NO_CONTENT_204 |
2f1548fd C |
155 | }) |
156 | }) | |
157 | }) | |
158 | ||
cef534ed C |
159 | describe('When updating my notification settings', function () { |
160 | const path = '/api/v1/users/me/notification-settings' | |
161 | const correctFields: UserNotificationSetting = { | |
2f1548fd C |
162 | newVideoFromSubscription: UserNotificationSettingValue.WEB, |
163 | newCommentOnMyVideo: UserNotificationSettingValue.WEB, | |
4f32032f | 164 | abuseAsModerator: UserNotificationSettingValue.WEB, |
7ccddd7b | 165 | videoAutoBlacklistAsModerator: UserNotificationSettingValue.WEB, |
2f1548fd C |
166 | blacklistOnMyVideo: UserNotificationSettingValue.WEB, |
167 | myVideoImportFinished: UserNotificationSettingValue.WEB, | |
168 | myVideoPublished: UserNotificationSettingValue.WEB, | |
169 | commentMention: UserNotificationSettingValue.WEB, | |
170 | newFollow: UserNotificationSettingValue.WEB, | |
883993c8 | 171 | newUserRegistration: UserNotificationSettingValue.WEB, |
8424c402 | 172 | newInstanceFollower: UserNotificationSettingValue.WEB, |
594d3e48 C |
173 | autoInstanceFollowing: UserNotificationSettingValue.WEB, |
174 | abuseNewMessage: UserNotificationSettingValue.WEB, | |
32a18cbf C |
175 | abuseStateChange: UserNotificationSettingValue.WEB, |
176 | newPeerTubeVersion: UserNotificationSettingValue.WEB, | |
177 | newPluginVersion: UserNotificationSettingValue.WEB | |
cef534ed C |
178 | } |
179 | ||
180 | it('Should fail with missing fields', async function () { | |
181 | await makePutBodyRequest({ | |
182 | url: server.url, | |
183 | path, | |
184 | token: server.accessToken, | |
2f1548fd | 185 | fields: { newVideoFromSubscription: UserNotificationSettingValue.WEB }, |
c0e8b12e | 186 | expectedStatus: HttpStatusCode.BAD_REQUEST_400 |
cef534ed C |
187 | }) |
188 | }) | |
189 | ||
190 | it('Should fail with incorrect field values', async function () { | |
191 | { | |
6c5065a0 | 192 | const fields = { ...correctFields, newCommentOnMyVideo: 15 } |
cef534ed C |
193 | |
194 | await makePutBodyRequest({ | |
195 | url: server.url, | |
196 | path, | |
197 | token: server.accessToken, | |
198 | fields, | |
c0e8b12e | 199 | expectedStatus: HttpStatusCode.BAD_REQUEST_400 |
cef534ed C |
200 | }) |
201 | } | |
202 | ||
203 | { | |
6c5065a0 | 204 | const fields = { ...correctFields, newCommentOnMyVideo: 'toto' } |
cef534ed C |
205 | |
206 | await makePutBodyRequest({ | |
207 | url: server.url, | |
208 | path, | |
209 | fields, | |
210 | token: server.accessToken, | |
c0e8b12e | 211 | expectedStatus: HttpStatusCode.BAD_REQUEST_400 |
cef534ed C |
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, | |
c0e8b12e | 221 | expectedStatus: HttpStatusCode.UNAUTHORIZED_401 |
cef534ed C |
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, | |
c0e8b12e | 231 | expectedStatus: HttpStatusCode.NO_CONTENT_204 |
cef534ed C |
232 | }) |
233 | }) | |
234 | }) | |
235 | ||
236 | describe('When connecting to my notification socket', function () { | |
fce7fe04 | 237 | |
cef534ed | 238 | it('Should fail with no token', function (next) { |
7c3b7976 | 239 | const socket = io(`http://localhost:${server.port}/user-notifications`, { reconnection: false }) |
cef534ed | 240 | |
fce7fe04 | 241 | socket.once('connect_error', function () { |
cef534ed C |
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) { | |
7c3b7976 | 253 | const socket = io(`http://localhost:${server.port}/user-notifications`, { |
cef534ed C |
254 | query: { accessToken: 'bad_access_token' }, |
255 | reconnection: false | |
256 | }) | |
257 | ||
fce7fe04 | 258 | socket.once('connect_error', function () { |
cef534ed C |
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) { | |
7c3b7976 | 270 | const socket = io(`http://localhost:${server.port}/user-notifications`, { |
cef534ed C |
271 | query: { accessToken: server.accessToken }, |
272 | reconnection: false | |
273 | }) | |
274 | ||
99fa5c5b | 275 | function errorListener (err) { |
cef534ed | 276 | next(new Error('Error in connection: ' + err)) |
99fa5c5b C |
277 | } |
278 | ||
fce7fe04 | 279 | socket.on('connect_error', errorListener) |
cef534ed | 280 | |
fce7fe04 | 281 | socket.once('connect', async () => { |
cef534ed C |
282 | socket.disconnect() |
283 | ||
284 | await wait(500) | |
285 | next() | |
286 | }) | |
287 | }) | |
288 | }) | |
289 | ||
7c3b7976 C |
290 | after(async function () { |
291 | await cleanupTests([ server ]) | |
cef534ed C |
292 | }) |
293 | }) |