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