1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { expect } from 'chai'
4 import { MockSmtpServer } from '@server/tests/shared'
5 import { HttpStatusCode } from '@shared/models'
6 import { cleanupTests, createSingleServer, PeerTubeServer, setAccessTokensToServers, waitJobs } from '@shared/server-commands'
8 describe('Test emails', function () {
9 let server: PeerTubeServer
12 let userAccessToken: string
14 let videoShortUUID: string
17 let videoUserUUID: string
19 let verificationString: string
20 let verificationString2: string
22 const emails: object[] = []
25 password: 'super_password'
29 before(async function () {
32 emailPort = await MockSmtpServer.Instance.collectEmails(emails)
34 const overrideConfig = {
36 hostname: '127.0.0.1',
40 server = await createSingleServer(1, overrideConfig)
41 await setAccessTokensToServers([ server ])
44 const created = await server.users.create({ username: user.username, password: user.password })
47 userAccessToken = await server.login.getAccessToken(user)
51 const attributes = { name: 'my super user video' }
52 const { uuid } = await server.videos.upload({ token: userAccessToken, attributes })
60 const { shortUUID, id } = await server.videos.upload({ attributes })
61 videoShortUUID = shortUUID
66 describe('When resetting user password', function () {
68 it('Should ask to reset the password', async function () {
69 await server.users.askResetPassword({ email: 'user_1@example.com' })
71 await waitJobs(server)
72 expect(emails).to.have.lengthOf(1)
74 const email = emails[0]
76 expect(email['from'][0]['name']).equal('PeerTube')
77 expect(email['from'][0]['address']).equal('test-admin@127.0.0.1')
78 expect(email['to'][0]['address']).equal('user_1@example.com')
79 expect(email['subject']).contains('password')
81 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
82 expect(verificationStringMatches).not.to.be.null
84 verificationString = verificationStringMatches[1]
85 expect(verificationString).to.have.length.above(2)
87 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
88 expect(userIdMatches).not.to.be.null
90 userId = parseInt(userIdMatches[1], 10)
91 expect(verificationString).to.not.be.undefined
94 it('Should not reset the password with an invalid verification string', async function () {
95 await server.users.resetPassword({
97 verificationString: verificationString + 'b',
98 password: 'super_password2',
99 expectedStatus: HttpStatusCode.FORBIDDEN_403
103 it('Should reset the password', async function () {
104 await server.users.resetPassword({ userId, verificationString, password: 'super_password2' })
107 it('Should not reset the password with the same verification string', async function () {
108 await server.users.resetPassword({
111 password: 'super_password3',
112 expectedStatus: HttpStatusCode.FORBIDDEN_403
116 it('Should login with this new password', async function () {
117 user.password = 'super_password2'
119 await server.login.getAccessToken(user)
123 describe('When creating a user without password', function () {
125 it('Should send a create password email', async function () {
128 await server.users.create({ username: 'create_password', password: '' })
130 await waitJobs(server)
131 expect(emails).to.have.lengthOf(2)
133 const email = emails[1]
135 expect(email['from'][0]['name']).equal('PeerTube')
136 expect(email['from'][0]['address']).equal('test-admin@127.0.0.1')
137 expect(email['to'][0]['address']).equal('create_password@example.com')
138 expect(email['subject']).contains('account')
139 expect(email['subject']).contains('password')
141 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
142 expect(verificationStringMatches).not.to.be.null
144 verificationString2 = verificationStringMatches[1]
145 expect(verificationString2).to.have.length.above(2)
147 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
148 expect(userIdMatches).not.to.be.null
150 userId2 = parseInt(userIdMatches[1], 10)
153 it('Should not reset the password with an invalid verification string', async function () {
154 await server.users.resetPassword({
156 verificationString: verificationString2 + 'c',
157 password: 'newly_created_password',
158 expectedStatus: HttpStatusCode.FORBIDDEN_403
162 it('Should reset the password', async function () {
163 await server.users.resetPassword({
165 verificationString: verificationString2,
166 password: 'newly_created_password'
170 it('Should login with this new password', async function () {
171 await server.login.getAccessToken({
172 username: 'create_password',
173 password: 'newly_created_password'
178 describe('When creating an abuse', function () {
180 it('Should send the notification email', async function () {
183 const reason = 'my super bad reason'
184 await server.abuses.report({ token: userAccessToken, videoId, reason })
186 await waitJobs(server)
187 expect(emails).to.have.lengthOf(3)
189 const email = emails[2]
191 expect(email['from'][0]['name']).equal('PeerTube')
192 expect(email['from'][0]['address']).equal('test-admin@127.0.0.1')
193 expect(email['to'][0]['address']).equal('admin' + server.internalServerNumber + '@example.com')
194 expect(email['subject']).contains('abuse')
195 expect(email['text']).contains(videoShortUUID)
199 describe('When blocking/unblocking user', function () {
201 it('Should send the notification email when blocking a user', async function () {
204 const reason = 'my super bad reason'
205 await server.users.banUser({ userId, reason })
207 await waitJobs(server)
208 expect(emails).to.have.lengthOf(4)
210 const email = emails[3]
212 expect(email['from'][0]['name']).equal('PeerTube')
213 expect(email['from'][0]['address']).equal('test-admin@127.0.0.1')
214 expect(email['to'][0]['address']).equal('user_1@example.com')
215 expect(email['subject']).contains(' blocked')
216 expect(email['text']).contains(' blocked')
217 expect(email['text']).contains('bad reason')
220 it('Should send the notification email when unblocking a user', async function () {
223 await server.users.unbanUser({ userId })
225 await waitJobs(server)
226 expect(emails).to.have.lengthOf(5)
228 const email = emails[4]
230 expect(email['from'][0]['name']).equal('PeerTube')
231 expect(email['from'][0]['address']).equal('test-admin@127.0.0.1')
232 expect(email['to'][0]['address']).equal('user_1@example.com')
233 expect(email['subject']).contains(' unblocked')
234 expect(email['text']).contains(' unblocked')
238 describe('When blacklisting a video', function () {
239 it('Should send the notification email', async function () {
242 const reason = 'my super reason'
243 await server.blacklist.add({ videoId: videoUserUUID, reason })
245 await waitJobs(server)
246 expect(emails).to.have.lengthOf(6)
248 const email = emails[5]
250 expect(email['from'][0]['name']).equal('PeerTube')
251 expect(email['from'][0]['address']).equal('test-admin@127.0.0.1')
252 expect(email['to'][0]['address']).equal('user_1@example.com')
253 expect(email['subject']).contains(' blacklisted')
254 expect(email['text']).contains('my super user video')
255 expect(email['text']).contains('my super reason')
258 it('Should send the notification email', async function () {
261 await server.blacklist.remove({ videoId: videoUserUUID })
263 await waitJobs(server)
264 expect(emails).to.have.lengthOf(7)
266 const email = emails[6]
268 expect(email['from'][0]['name']).equal('PeerTube')
269 expect(email['from'][0]['address']).equal('test-admin@127.0.0.1')
270 expect(email['to'][0]['address']).equal('user_1@example.com')
271 expect(email['subject']).contains(' unblacklisted')
272 expect(email['text']).contains('my super user video')
275 it('Should have the manage preferences link in the email', async function () {
276 const email = emails[6]
277 expect(email['text']).to.contain('Manage your notification preferences')
281 describe('When verifying a user email', function () {
283 it('Should ask to send the verification email', async function () {
286 await server.users.askSendVerifyEmail({ email: 'user_1@example.com' })
288 await waitJobs(server)
289 expect(emails).to.have.lengthOf(8)
291 const email = emails[7]
293 expect(email['from'][0]['name']).equal('PeerTube')
294 expect(email['from'][0]['address']).equal('test-admin@127.0.0.1')
295 expect(email['to'][0]['address']).equal('user_1@example.com')
296 expect(email['subject']).contains('Verify')
298 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
299 expect(verificationStringMatches).not.to.be.null
301 verificationString = verificationStringMatches[1]
302 expect(verificationString).to.not.be.undefined
303 expect(verificationString).to.have.length.above(2)
305 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
306 expect(userIdMatches).not.to.be.null
308 userId = parseInt(userIdMatches[1], 10)
311 it('Should not verify the email with an invalid verification string', async function () {
312 await server.users.verifyEmail({
314 verificationString: verificationString + 'b',
315 isPendingEmail: false,
316 expectedStatus: HttpStatusCode.FORBIDDEN_403
320 it('Should verify the email', async function () {
321 await server.users.verifyEmail({ userId, verificationString })
325 after(async function () {
326 MockSmtpServer.Instance.kill()
328 await cleanupTests([ server ])