]>
Commit | Line | Data |
---|---|---|
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ | |
2 | ||
3 | import { expect } from 'chai' | |
4 | import { MockSmtpServer } from '@server/tests/shared' | |
5 | import { UserRegistrationState, UserRole } from '@shared/models' | |
6 | import { | |
7 | cleanupTests, | |
8 | ConfigCommand, | |
9 | createSingleServer, | |
10 | PeerTubeServer, | |
11 | setAccessTokensToServers, | |
12 | waitJobs | |
13 | } from '@shared/server-commands' | |
14 | ||
15 | describe('Test registrations', function () { | |
16 | let server: PeerTubeServer | |
17 | ||
18 | const emails: object[] = [] | |
19 | let emailPort: number | |
20 | ||
21 | before(async function () { | |
22 | this.timeout(30000) | |
23 | ||
24 | emailPort = await MockSmtpServer.Instance.collectEmails(emails) | |
25 | ||
26 | server = await createSingleServer(1, ConfigCommand.getEmailOverrideConfig(emailPort)) | |
27 | ||
28 | await setAccessTokensToServers([ server ]) | |
29 | await server.config.enableSignup(false) | |
30 | }) | |
31 | ||
32 | describe('Direct registrations of a new user', function () { | |
33 | let user1Token: string | |
34 | ||
35 | it('Should register a new user', async function () { | |
36 | const user = { displayName: 'super user 1', username: 'user_1', password: 'my super password' } | |
37 | const channel = { name: 'my_user_1_channel', displayName: 'my channel rocks' } | |
38 | ||
39 | await server.registrations.register({ ...user, channel }) | |
40 | }) | |
41 | ||
42 | it('Should be able to login with this registered user', async function () { | |
43 | const user1 = { username: 'user_1', password: 'my super password' } | |
44 | ||
45 | user1Token = await server.login.getAccessToken(user1) | |
46 | }) | |
47 | ||
48 | it('Should have the correct display name', async function () { | |
49 | const user = await server.users.getMyInfo({ token: user1Token }) | |
50 | expect(user.account.displayName).to.equal('super user 1') | |
51 | }) | |
52 | ||
53 | it('Should have the correct video quota', async function () { | |
54 | const user = await server.users.getMyInfo({ token: user1Token }) | |
55 | expect(user.videoQuota).to.equal(5 * 1024 * 1024) | |
56 | }) | |
57 | ||
58 | it('Should have created the channel', async function () { | |
59 | const { displayName } = await server.channels.get({ channelName: 'my_user_1_channel' }) | |
60 | ||
61 | expect(displayName).to.equal('my channel rocks') | |
62 | }) | |
63 | ||
64 | it('Should remove me', async function () { | |
65 | { | |
66 | const { data } = await server.users.list() | |
67 | expect(data.find(u => u.username === 'user_1')).to.not.be.undefined | |
68 | } | |
69 | ||
70 | await server.users.deleteMe({ token: user1Token }) | |
71 | ||
72 | { | |
73 | const { data } = await server.users.list() | |
74 | expect(data.find(u => u.username === 'user_1')).to.be.undefined | |
75 | } | |
76 | }) | |
77 | }) | |
78 | ||
79 | describe('Registration requests', function () { | |
80 | let id2: number | |
81 | let id3: number | |
82 | let id4: number | |
83 | ||
84 | let user2Token: string | |
85 | let user3Token: string | |
86 | ||
87 | before(async function () { | |
88 | this.timeout(60000) | |
89 | ||
90 | await server.config.enableSignup(true) | |
91 | ||
92 | { | |
93 | const { id } = await server.registrations.requestRegistration({ | |
94 | username: 'user4', | |
95 | registrationReason: 'registration reason 4' | |
96 | }) | |
97 | ||
98 | id4 = id | |
99 | } | |
100 | }) | |
101 | ||
102 | it('Should request a registration without a channel', async function () { | |
103 | { | |
104 | const { id } = await server.registrations.requestRegistration({ | |
105 | username: 'user2', | |
106 | displayName: 'my super user 2', | |
107 | email: 'user2@example.com', | |
108 | password: 'user2password', | |
109 | registrationReason: 'registration reason 2' | |
110 | }) | |
111 | ||
112 | id2 = id | |
113 | } | |
114 | }) | |
115 | ||
116 | it('Should request a registration with a channel', async function () { | |
117 | const { id } = await server.registrations.requestRegistration({ | |
118 | username: 'user3', | |
119 | displayName: 'my super user 3', | |
120 | channel: { | |
121 | displayName: 'my user 3 channel', | |
122 | name: 'super_user3_channel' | |
123 | }, | |
124 | email: 'user3@example.com', | |
125 | password: 'user3password', | |
126 | registrationReason: 'registration reason 3' | |
127 | }) | |
128 | ||
129 | id3 = id | |
130 | }) | |
131 | ||
132 | it('Should list these registration requests', async function () { | |
133 | { | |
134 | const { total, data } = await server.registrations.list({ sort: '-createdAt' }) | |
135 | expect(total).to.equal(3) | |
136 | expect(data).to.have.lengthOf(3) | |
137 | ||
138 | { | |
139 | expect(data[0].id).to.equal(id3) | |
140 | expect(data[0].username).to.equal('user3') | |
141 | expect(data[0].accountDisplayName).to.equal('my super user 3') | |
142 | ||
143 | expect(data[0].channelDisplayName).to.equal('my user 3 channel') | |
144 | expect(data[0].channelHandle).to.equal('super_user3_channel') | |
145 | ||
146 | expect(data[0].createdAt).to.exist | |
147 | expect(data[0].updatedAt).to.exist | |
148 | ||
149 | expect(data[0].email).to.equal('user3@example.com') | |
150 | expect(data[0].emailVerified).to.be.null | |
151 | ||
152 | expect(data[0].moderationResponse).to.be.null | |
153 | expect(data[0].registrationReason).to.equal('registration reason 3') | |
154 | expect(data[0].state.id).to.equal(UserRegistrationState.PENDING) | |
155 | expect(data[0].state.label).to.equal('Pending') | |
156 | expect(data[0].user).to.be.null | |
157 | } | |
158 | ||
159 | { | |
160 | expect(data[1].id).to.equal(id2) | |
161 | expect(data[1].username).to.equal('user2') | |
162 | expect(data[1].accountDisplayName).to.equal('my super user 2') | |
163 | ||
164 | expect(data[1].channelDisplayName).to.be.null | |
165 | expect(data[1].channelHandle).to.be.null | |
166 | ||
167 | expect(data[1].createdAt).to.exist | |
168 | expect(data[1].updatedAt).to.exist | |
169 | ||
170 | expect(data[1].email).to.equal('user2@example.com') | |
171 | expect(data[1].emailVerified).to.be.null | |
172 | ||
173 | expect(data[1].moderationResponse).to.be.null | |
174 | expect(data[1].registrationReason).to.equal('registration reason 2') | |
175 | expect(data[1].state.id).to.equal(UserRegistrationState.PENDING) | |
176 | expect(data[1].state.label).to.equal('Pending') | |
177 | expect(data[1].user).to.be.null | |
178 | } | |
179 | ||
180 | { | |
181 | expect(data[2].username).to.equal('user4') | |
182 | } | |
183 | } | |
184 | ||
185 | { | |
186 | const { total, data } = await server.registrations.list({ count: 1, start: 1, sort: 'createdAt' }) | |
187 | ||
188 | expect(total).to.equal(3) | |
189 | expect(data).to.have.lengthOf(1) | |
190 | expect(data[0].id).to.equal(id2) | |
191 | } | |
192 | ||
193 | { | |
194 | const { total, data } = await server.registrations.list({ search: 'user3' }) | |
195 | expect(total).to.equal(1) | |
196 | expect(data).to.have.lengthOf(1) | |
197 | expect(data[0].id).to.equal(id3) | |
198 | } | |
199 | }) | |
200 | ||
201 | it('Should reject a registration request', async function () { | |
202 | await server.registrations.reject({ id: id4, moderationResponse: 'I do not want id 4 on this instance' }) | |
203 | }) | |
204 | ||
205 | it('Should have sent an email to the user explanining the registration has been rejected', async function () { | |
206 | this.timeout(50000) | |
207 | ||
208 | await waitJobs([ server ]) | |
209 | ||
210 | const email = emails.find(e => e['to'][0]['address'] === 'user4@example.com') | |
211 | expect(email).to.exist | |
212 | ||
213 | expect(email['subject']).to.contain('been rejected') | |
214 | expect(email['text']).to.contain('been rejected') | |
215 | expect(email['text']).to.contain('I do not want id 4 on this instance') | |
216 | }) | |
217 | ||
218 | it('Should accept registration requests', async function () { | |
219 | await server.registrations.accept({ id: id2, moderationResponse: 'Welcome id 2' }) | |
220 | await server.registrations.accept({ id: id3, moderationResponse: 'Welcome id 3' }) | |
221 | }) | |
222 | ||
223 | it('Should have sent an email to the user explanining the registration has been accepted', async function () { | |
224 | this.timeout(50000) | |
225 | ||
226 | await waitJobs([ server ]) | |
227 | ||
228 | { | |
229 | const email = emails.find(e => e['to'][0]['address'] === 'user2@example.com') | |
230 | expect(email).to.exist | |
231 | ||
232 | expect(email['subject']).to.contain('been accepted') | |
233 | expect(email['text']).to.contain('been accepted') | |
234 | expect(email['text']).to.contain('Welcome id 2') | |
235 | } | |
236 | ||
237 | { | |
238 | const email = emails.find(e => e['to'][0]['address'] === 'user3@example.com') | |
239 | expect(email).to.exist | |
240 | ||
241 | expect(email['subject']).to.contain('been accepted') | |
242 | expect(email['text']).to.contain('been accepted') | |
243 | expect(email['text']).to.contain('Welcome id 3') | |
244 | } | |
245 | }) | |
246 | ||
247 | it('Should login with these users', async function () { | |
248 | user2Token = await server.login.getAccessToken({ username: 'user2', password: 'user2password' }) | |
249 | user3Token = await server.login.getAccessToken({ username: 'user3', password: 'user3password' }) | |
250 | }) | |
251 | ||
252 | it('Should have created the appropriate attributes for user 2', async function () { | |
253 | const me = await server.users.getMyInfo({ token: user2Token }) | |
254 | ||
255 | expect(me.username).to.equal('user2') | |
256 | expect(me.account.displayName).to.equal('my super user 2') | |
257 | expect(me.videoQuota).to.equal(5 * 1024 * 1024) | |
258 | expect(me.videoChannels[0].name).to.equal('user2_channel') | |
259 | expect(me.videoChannels[0].displayName).to.equal('Main user2 channel') | |
260 | expect(me.role.id).to.equal(UserRole.USER) | |
261 | expect(me.email).to.equal('user2@example.com') | |
262 | }) | |
263 | ||
264 | it('Should have created the appropriate attributes for user 3', async function () { | |
265 | const me = await server.users.getMyInfo({ token: user3Token }) | |
266 | ||
267 | expect(me.username).to.equal('user3') | |
268 | expect(me.account.displayName).to.equal('my super user 3') | |
269 | expect(me.videoQuota).to.equal(5 * 1024 * 1024) | |
270 | expect(me.videoChannels[0].name).to.equal('super_user3_channel') | |
271 | expect(me.videoChannels[0].displayName).to.equal('my user 3 channel') | |
272 | expect(me.role.id).to.equal(UserRole.USER) | |
273 | expect(me.email).to.equal('user3@example.com') | |
274 | }) | |
275 | ||
276 | it('Should list these accepted/rejected registration requests', async function () { | |
277 | const { data } = await server.registrations.list({ sort: 'createdAt' }) | |
278 | const { data: users } = await server.users.list() | |
279 | ||
280 | { | |
281 | expect(data[0].id).to.equal(id4) | |
282 | expect(data[0].state.id).to.equal(UserRegistrationState.REJECTED) | |
283 | expect(data[0].state.label).to.equal('Rejected') | |
284 | ||
285 | expect(data[0].moderationResponse).to.equal('I do not want id 4 on this instance') | |
286 | expect(data[0].user).to.be.null | |
287 | ||
288 | expect(users.find(u => u.username === 'user4')).to.not.exist | |
289 | } | |
290 | ||
291 | { | |
292 | expect(data[1].id).to.equal(id2) | |
293 | expect(data[1].state.id).to.equal(UserRegistrationState.ACCEPTED) | |
294 | expect(data[1].state.label).to.equal('Accepted') | |
295 | ||
296 | expect(data[1].moderationResponse).to.equal('Welcome id 2') | |
297 | expect(data[1].user).to.exist | |
298 | ||
299 | const user2 = users.find(u => u.username === 'user2') | |
300 | expect(data[1].user.id).to.equal(user2.id) | |
301 | } | |
302 | ||
303 | { | |
304 | expect(data[2].id).to.equal(id3) | |
305 | expect(data[2].state.id).to.equal(UserRegistrationState.ACCEPTED) | |
306 | expect(data[2].state.label).to.equal('Accepted') | |
307 | ||
308 | expect(data[2].moderationResponse).to.equal('Welcome id 3') | |
309 | expect(data[2].user).to.exist | |
310 | ||
311 | const user3 = users.find(u => u.username === 'user3') | |
312 | expect(data[2].user.id).to.equal(user3.id) | |
313 | } | |
314 | }) | |
315 | ||
316 | it('Shoulde delete a registration', async function () { | |
317 | await server.registrations.delete({ id: id2 }) | |
318 | await server.registrations.delete({ id: id3 }) | |
319 | ||
320 | const { total, data } = await server.registrations.list() | |
321 | expect(total).to.equal(1) | |
322 | expect(data).to.have.lengthOf(1) | |
323 | expect(data[0].id).to.equal(id4) | |
324 | ||
325 | const { data: users } = await server.users.list() | |
326 | ||
327 | for (const username of [ 'user2', 'user3' ]) { | |
328 | expect(users.find(u => u.username === username)).to.exist | |
329 | } | |
330 | }) | |
331 | ||
332 | it('Should be able to prevent email delivery on accept/reject', async function () { | |
333 | this.timeout(50000) | |
334 | ||
335 | let id1: number | |
336 | let id2: number | |
337 | ||
338 | { | |
339 | const { id } = await server.registrations.requestRegistration({ | |
340 | username: 'user7', | |
341 | email: 'user7@example.com', | |
342 | registrationReason: 'tt' | |
343 | }) | |
344 | id1 = id | |
345 | } | |
346 | { | |
347 | const { id } = await server.registrations.requestRegistration({ | |
348 | username: 'user8', | |
349 | email: 'user8@example.com', | |
350 | registrationReason: 'tt' | |
351 | }) | |
352 | id2 = id | |
353 | } | |
354 | ||
355 | await server.registrations.accept({ id: id1, moderationResponse: 'tt', preventEmailDelivery: true }) | |
356 | await server.registrations.reject({ id: id2, moderationResponse: 'tt', preventEmailDelivery: true }) | |
357 | ||
358 | await waitJobs([ server ]) | |
359 | ||
360 | const filtered = emails.filter(e => { | |
361 | const address = e['to'][0]['address'] | |
362 | return address === 'user7@example.com' || address === 'user8@example.com' | |
363 | }) | |
364 | ||
365 | expect(filtered).to.have.lengthOf(0) | |
366 | }) | |
367 | ||
368 | it('Should request a registration without a channel, that will conflict with an already existing channel', async function () { | |
369 | let id1: number | |
370 | let id2: number | |
371 | ||
372 | { | |
373 | const { id } = await server.registrations.requestRegistration({ | |
374 | registrationReason: 'tt', | |
375 | username: 'user5', | |
376 | password: 'user5password', | |
377 | channel: { | |
378 | displayName: 'channel 6', | |
379 | name: 'user6_channel' | |
380 | } | |
381 | }) | |
382 | ||
383 | id1 = id | |
384 | } | |
385 | ||
386 | { | |
387 | const { id } = await server.registrations.requestRegistration({ | |
388 | registrationReason: 'tt', | |
389 | username: 'user6', | |
390 | password: 'user6password' | |
391 | }) | |
392 | ||
393 | id2 = id | |
394 | } | |
395 | ||
396 | await server.registrations.accept({ id: id1, moderationResponse: 'tt' }) | |
397 | await server.registrations.accept({ id: id2, moderationResponse: 'tt' }) | |
398 | ||
399 | const user5Token = await server.login.getAccessToken('user5', 'user5password') | |
400 | const user6Token = await server.login.getAccessToken('user6', 'user6password') | |
401 | ||
402 | const user5 = await server.users.getMyInfo({ token: user5Token }) | |
403 | const user6 = await server.users.getMyInfo({ token: user6Token }) | |
404 | ||
405 | expect(user5.videoChannels[0].name).to.equal('user6_channel') | |
406 | expect(user6.videoChannels[0].name).to.equal('user6_channel-1') | |
407 | }) | |
408 | }) | |
409 | ||
410 | after(async function () { | |
411 | MockSmtpServer.Instance.kill() | |
412 | ||
413 | await cleanupTests([ server ]) | |
414 | }) | |
415 | }) |