Add hls support on server
[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]['address']).equal('test-admin@localhost')
93       expect(email['to'][0]['address']).equal('user_1@example.com')
94       expect(email['subject']).contains('password')
95
96       const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
97       expect(verificationStringMatches).not.to.be.null
98
99       verificationString = verificationStringMatches[1]
100       expect(verificationString).to.have.length.above(2)
101
102       const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
103       expect(userIdMatches).not.to.be.null
104
105       userId = parseInt(userIdMatches[1], 10)
106       expect(verificationString).to.not.be.undefined
107     })
108
109     it('Should not reset the password with an invalid verification string', async function () {
110       await resetPassword(server.url, userId, verificationString + 'b', 'super_password2', 403)
111     })
112
113     it('Should reset the password', async function () {
114       await resetPassword(server.url, userId, verificationString, 'super_password2')
115     })
116
117     it('Should login with this new password', async function () {
118       user.password = 'super_password2'
119
120       await userLogin(server, user)
121     })
122   })
123
124   describe('When creating a video abuse', function () {
125     it('Should send the notification email', async function () {
126       this.timeout(10000)
127
128       const reason = 'my super bad reason'
129       await reportVideoAbuse(server.url, server.accessToken, videoUUID, reason)
130
131       await waitJobs(server)
132       expect(emails).to.have.lengthOf(2)
133
134       const email = emails[1]
135
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', async function () {
144     it('Should send the notification email when blocking a user', async function () {
145       this.timeout(10000)
146
147       const reason = 'my super bad reason'
148       await blockUser(server.url, userId, server.accessToken, 204, reason)
149
150       await waitJobs(server)
151       expect(emails).to.have.lengthOf(3)
152
153       const email = emails[2]
154
155       expect(email['from'][0]['address']).equal('test-admin@localhost')
156       expect(email['to'][0]['address']).equal('user_1@example.com')
157       expect(email['subject']).contains(' blocked')
158       expect(email['text']).contains(' blocked')
159       expect(email['text']).contains(reason)
160     })
161
162     it('Should send the notification email when unblocking a user', async function () {
163       this.timeout(10000)
164
165       await unblockUser(server.url, userId, server.accessToken, 204)
166
167       await waitJobs(server)
168       expect(emails).to.have.lengthOf(4)
169
170       const email = emails[3]
171
172       expect(email['from'][0]['address']).equal('test-admin@localhost')
173       expect(email['to'][0]['address']).equal('user_1@example.com')
174       expect(email['subject']).contains(' unblocked')
175       expect(email['text']).contains(' unblocked')
176     })
177   })
178
179   describe('When blacklisting a video', function () {
180     it('Should send the notification email', async function () {
181       this.timeout(10000)
182
183       const reason = 'my super reason'
184       await addVideoToBlacklist(server.url, server.accessToken, videoUserUUID, reason)
185
186       await waitJobs(server)
187       expect(emails).to.have.lengthOf(5)
188
189       const email = emails[4]
190
191       expect(email['from'][0]['address']).equal('test-admin@localhost')
192       expect(email['to'][0]['address']).equal('user_1@example.com')
193       expect(email['subject']).contains(' blacklisted')
194       expect(email['text']).contains('my super user video')
195       expect(email['text']).contains('my super reason')
196     })
197
198     it('Should send the notification email', async function () {
199       this.timeout(10000)
200
201       await removeVideoFromBlacklist(server.url, server.accessToken, videoUserUUID)
202
203       await waitJobs(server)
204       expect(emails).to.have.lengthOf(6)
205
206       const email = emails[5]
207
208       expect(email['from'][0]['address']).equal('test-admin@localhost')
209       expect(email['to'][0]['address']).equal('user_1@example.com')
210       expect(email['subject']).contains(' unblacklisted')
211       expect(email['text']).contains('my super user video')
212     })
213   })
214
215   describe('When verifying a user email', function () {
216
217     it('Should ask to send the verification email', async function () {
218       this.timeout(10000)
219
220       await askSendVerifyEmail(server.url, 'user_1@example.com')
221
222       await waitJobs(server)
223       expect(emails).to.have.lengthOf(7)
224
225       const email = emails[6]
226
227       expect(email['from'][0]['address']).equal('test-admin@localhost')
228       expect(email['to'][0]['address']).equal('user_1@example.com')
229       expect(email['subject']).contains('Verify')
230
231       const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
232       expect(verificationStringMatches).not.to.be.null
233
234       verificationString = verificationStringMatches[1]
235       expect(verificationString).to.not.be.undefined
236       expect(verificationString).to.have.length.above(2)
237
238       const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
239       expect(userIdMatches).not.to.be.null
240
241       userId = parseInt(userIdMatches[1], 10)
242     })
243
244     it('Should not verify the email with an invalid verification string', async function () {
245       await verifyEmail(server.url, userId, verificationString + 'b', 403)
246     })
247
248     it('Should verify the email', async function () {
249       await verifyEmail(server.url, userId, verificationString)
250     })
251   })
252
253   after(async function () {
254     MockSmtpServer.Instance.kill()
255     killallServers([ server ])
256   })
257 })