Fix from header in contact form
[oweals/peertube.git] / server / tests / api / server / email.ts
1 /* tslint:disable:no-unused-expression */
2
3 import * as chai from 'chai'
4 import 'mocha'
5 import {
6   addVideoToBlacklist,
7   askResetPassword,
8   askSendVerifyEmail,
9   blockUser,
10   createUser, removeVideoFromBlacklist,
11   reportVideoAbuse,
12   resetPassword,
13   runServer,
14   unblockUser,
15   uploadVideo,
16   userLogin,
17   verifyEmail,
18   flushTests,
19   killallServers,
20   ServerInfo,
21   setAccessTokensToServers
22 } from '../../../../shared/utils'
23 import { MockSmtpServer } from '../../../../shared/utils/miscs/email'
24 import { waitJobs } from '../../../../shared/utils/server/jobs'
25
26 const expect = chai.expect
27
28 describe('Test emails', function () {
29   let server: ServerInfo
30   let userId: number
31   let userAccessToken: string
32   let videoUUID: string
33   let videoUserUUID: string
34   let verificationString: string
35   const emails: object[] = []
36   const user = {
37     username: 'user_1',
38     password: 'super_password'
39   }
40
41   before(async function () {
42     this.timeout(30000)
43
44     await MockSmtpServer.Instance.collectEmails(emails)
45
46     await flushTests()
47
48     const overrideConfig = {
49       smtp: {
50         hostname: 'localhost'
51       }
52     }
53     server = await runServer(1, overrideConfig)
54     await setAccessTokensToServers([ server ])
55
56     {
57       const res = await createUser(server.url, server.accessToken, user.username, user.password)
58       userId = res.body.user.id
59
60       userAccessToken = await userLogin(server, user)
61     }
62
63     {
64       const attributes = {
65         name: 'my super user video'
66       }
67       const res = await uploadVideo(server.url, userAccessToken, attributes)
68       videoUserUUID = res.body.video.uuid
69     }
70
71     {
72       const attributes = {
73         name: 'my super name'
74       }
75       const res = await uploadVideo(server.url, server.accessToken, attributes)
76       videoUUID = res.body.video.uuid
77     }
78   })
79
80   describe('When resetting user password', function () {
81
82     it('Should ask to reset the password', async function () {
83       this.timeout(10000)
84
85       await askResetPassword(server.url, 'user_1@example.com')
86
87       await waitJobs(server)
88       expect(emails).to.have.lengthOf(1)
89
90       const email = emails[0]
91
92       expect(email['from'][0]['name']).equal('localhost:9001')
93       expect(email['from'][0]['address']).equal('test-admin@localhost')
94       expect(email['to'][0]['address']).equal('user_1@example.com')
95       expect(email['subject']).contains('password')
96
97       const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
98       expect(verificationStringMatches).not.to.be.null
99
100       verificationString = verificationStringMatches[1]
101       expect(verificationString).to.have.length.above(2)
102
103       const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
104       expect(userIdMatches).not.to.be.null
105
106       userId = parseInt(userIdMatches[1], 10)
107       expect(verificationString).to.not.be.undefined
108     })
109
110     it('Should not reset the password with an invalid verification string', async function () {
111       await resetPassword(server.url, userId, verificationString + 'b', 'super_password2', 403)
112     })
113
114     it('Should reset the password', async function () {
115       await resetPassword(server.url, userId, verificationString, 'super_password2')
116     })
117
118     it('Should login with this new password', async function () {
119       user.password = 'super_password2'
120
121       await userLogin(server, user)
122     })
123   })
124
125   describe('When creating a video abuse', function () {
126     it('Should send the notification email', async function () {
127       this.timeout(10000)
128
129       const reason = 'my super bad reason'
130       await reportVideoAbuse(server.url, server.accessToken, videoUUID, reason)
131
132       await waitJobs(server)
133       expect(emails).to.have.lengthOf(2)
134
135       const email = emails[1]
136
137       expect(email['from'][0]['name']).equal('localhost:9001')
138       expect(email['from'][0]['address']).equal('test-admin@localhost')
139       expect(email['to'][0]['address']).equal('admin1@example.com')
140       expect(email['subject']).contains('abuse')
141       expect(email['text']).contains(videoUUID)
142     })
143   })
144
145   describe('When blocking/unblocking user', async function () {
146     it('Should send the notification email when blocking a user', async function () {
147       this.timeout(10000)
148
149       const reason = 'my super bad reason'
150       await blockUser(server.url, userId, server.accessToken, 204, reason)
151
152       await waitJobs(server)
153       expect(emails).to.have.lengthOf(3)
154
155       const email = emails[2]
156
157       expect(email['from'][0]['name']).equal('localhost:9001')
158       expect(email['from'][0]['address']).equal('test-admin@localhost')
159       expect(email['to'][0]['address']).equal('user_1@example.com')
160       expect(email['subject']).contains(' blocked')
161       expect(email['text']).contains(' blocked')
162       expect(email['text']).contains(reason)
163     })
164
165     it('Should send the notification email when unblocking a user', async function () {
166       this.timeout(10000)
167
168       await unblockUser(server.url, userId, server.accessToken, 204)
169
170       await waitJobs(server)
171       expect(emails).to.have.lengthOf(4)
172
173       const email = emails[3]
174
175       expect(email['from'][0]['name']).equal('localhost:9001')
176       expect(email['from'][0]['address']).equal('test-admin@localhost')
177       expect(email['to'][0]['address']).equal('user_1@example.com')
178       expect(email['subject']).contains(' unblocked')
179       expect(email['text']).contains(' unblocked')
180     })
181   })
182
183   describe('When blacklisting a video', function () {
184     it('Should send the notification email', async function () {
185       this.timeout(10000)
186
187       const reason = 'my super reason'
188       await addVideoToBlacklist(server.url, server.accessToken, videoUserUUID, reason)
189
190       await waitJobs(server)
191       expect(emails).to.have.lengthOf(5)
192
193       const email = emails[4]
194
195       expect(email['from'][0]['name']).equal('localhost:9001')
196       expect(email['from'][0]['address']).equal('test-admin@localhost')
197       expect(email['to'][0]['address']).equal('user_1@example.com')
198       expect(email['subject']).contains(' blacklisted')
199       expect(email['text']).contains('my super user video')
200       expect(email['text']).contains('my super reason')
201     })
202
203     it('Should send the notification email', async function () {
204       this.timeout(10000)
205
206       await removeVideoFromBlacklist(server.url, server.accessToken, videoUserUUID)
207
208       await waitJobs(server)
209       expect(emails).to.have.lengthOf(6)
210
211       const email = emails[5]
212
213       expect(email['from'][0]['name']).equal('localhost:9001')
214       expect(email['from'][0]['address']).equal('test-admin@localhost')
215       expect(email['to'][0]['address']).equal('user_1@example.com')
216       expect(email['subject']).contains(' unblacklisted')
217       expect(email['text']).contains('my super user video')
218     })
219   })
220
221   describe('When verifying a user email', function () {
222
223     it('Should ask to send the verification email', async function () {
224       this.timeout(10000)
225
226       await askSendVerifyEmail(server.url, 'user_1@example.com')
227
228       await waitJobs(server)
229       expect(emails).to.have.lengthOf(7)
230
231       const email = emails[6]
232
233       expect(email['from'][0]['name']).equal('localhost:9001')
234       expect(email['from'][0]['address']).equal('test-admin@localhost')
235       expect(email['to'][0]['address']).equal('user_1@example.com')
236       expect(email['subject']).contains('Verify')
237
238       const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
239       expect(verificationStringMatches).not.to.be.null
240
241       verificationString = verificationStringMatches[1]
242       expect(verificationString).to.not.be.undefined
243       expect(verificationString).to.have.length.above(2)
244
245       const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
246       expect(userIdMatches).not.to.be.null
247
248       userId = parseInt(userIdMatches[1], 10)
249     })
250
251     it('Should not verify the email with an invalid verification string', async function () {
252       await verifyEmail(server.url, userId, verificationString + 'b', 403)
253     })
254
255     it('Should verify the email', async function () {
256       await verifyEmail(server.url, userId, verificationString)
257     })
258   })
259
260   after(async function () {
261     MockSmtpServer.Instance.kill()
262     killallServers([ server ])
263   })
264 })