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