Add refresh video on search
[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   blockUser,
9   createUser, removeVideoFromBlacklist,
10   reportVideoAbuse,
11   resetPassword,
12   runServer,
13   unblockUser,
14   uploadVideo,
15   userLogin
16 } from '../../utils'
17 import { flushTests, killallServers, ServerInfo, setAccessTokensToServers } from '../../utils/index'
18 import { mockSmtpServer } from '../../utils/miscs/email'
19 import { waitJobs } from '../../utils/server/jobs'
20
21 const expect = chai.expect
22
23 describe('Test emails', function () {
24   let server: ServerInfo
25   let userId: number
26   let userAccessToken: string
27   let videoUUID: string
28   let videoUserUUID: string
29   let verificationString: string
30   const emails: object[] = []
31   const user = {
32     username: 'user_1',
33     password: 'super_password'
34   }
35
36   before(async function () {
37     this.timeout(30000)
38
39     await mockSmtpServer(emails)
40
41     await flushTests()
42
43     const overrideConfig = {
44       smtp: {
45         hostname: 'localhost'
46       }
47     }
48     server = await runServer(1, overrideConfig)
49     await setAccessTokensToServers([ server ])
50
51     {
52       const res = await createUser(server.url, server.accessToken, user.username, user.password)
53       userId = res.body.user.id
54
55       userAccessToken = await userLogin(server, user)
56     }
57
58     {
59       const attributes = {
60         name: 'my super user video'
61       }
62       const res = await uploadVideo(server.url, userAccessToken, attributes)
63       videoUserUUID = res.body.video.uuid
64     }
65
66     {
67       const attributes = {
68         name: 'my super name'
69       }
70       const res = await uploadVideo(server.url, server.accessToken, attributes)
71       videoUUID = res.body.video.uuid
72     }
73   })
74
75   describe('When resetting user password', function () {
76
77     it('Should ask to reset the password', async function () {
78       this.timeout(10000)
79
80       await askResetPassword(server.url, 'user_1@example.com')
81
82       await waitJobs(server)
83       expect(emails).to.have.lengthOf(1)
84
85       const email = emails[0]
86
87       expect(email['from'][0]['address']).equal('test-admin@localhost')
88       expect(email['to'][0]['address']).equal('user_1@example.com')
89       expect(email['subject']).contains('password')
90
91       const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
92       expect(verificationStringMatches).not.to.be.null
93
94       verificationString = verificationStringMatches[1]
95       expect(verificationString).to.have.length.above(2)
96
97       const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
98       expect(userIdMatches).not.to.be.null
99
100       userId = parseInt(userIdMatches[1], 10)
101       expect(verificationString).to.not.be.undefined
102     })
103
104     it('Should not reset the password with an invalid verification string', async function () {
105       await resetPassword(server.url, userId, verificationString + 'b', 'super_password2', 403)
106     })
107
108     it('Should reset the password', async function () {
109       await resetPassword(server.url, userId, verificationString, 'super_password2')
110     })
111
112     it('Should login with this new password', async function () {
113       user.password = 'super_password2'
114
115       await userLogin(server, user)
116     })
117   })
118
119   describe('When creating a video abuse', function () {
120     it('Should send the notification email', async function () {
121       this.timeout(10000)
122
123       const reason = 'my super bad reason'
124       await reportVideoAbuse(server.url, server.accessToken, videoUUID, reason)
125
126       await waitJobs(server)
127       expect(emails).to.have.lengthOf(2)
128
129       const email = emails[1]
130
131       expect(email['from'][0]['address']).equal('test-admin@localhost')
132       expect(email['to'][0]['address']).equal('admin1@example.com')
133       expect(email['subject']).contains('abuse')
134       expect(email['text']).contains(videoUUID)
135     })
136   })
137
138   describe('When blocking/unblocking user', async function () {
139     it('Should send the notification email when blocking a user', async function () {
140       this.timeout(10000)
141
142       const reason = 'my super bad reason'
143       await blockUser(server.url, userId, server.accessToken, 204, reason)
144
145       await waitJobs(server)
146       expect(emails).to.have.lengthOf(3)
147
148       const email = emails[2]
149
150       expect(email['from'][0]['address']).equal('test-admin@localhost')
151       expect(email['to'][0]['address']).equal('user_1@example.com')
152       expect(email['subject']).contains(' blocked')
153       expect(email['text']).contains(' blocked')
154       expect(email['text']).contains(reason)
155     })
156
157     it('Should send the notification email when unblocking a user', async function () {
158       this.timeout(10000)
159
160       await unblockUser(server.url, userId, server.accessToken, 204)
161
162       await waitJobs(server)
163       expect(emails).to.have.lengthOf(4)
164
165       const email = emails[3]
166
167       expect(email['from'][0]['address']).equal('test-admin@localhost')
168       expect(email['to'][0]['address']).equal('user_1@example.com')
169       expect(email['subject']).contains(' unblocked')
170       expect(email['text']).contains(' unblocked')
171     })
172   })
173
174   describe('When blacklisting a video', function () {
175     it('Should send the notification email', async function () {
176       this.timeout(10000)
177
178       const reason = 'my super reason'
179       await addVideoToBlacklist(server.url, server.accessToken, videoUserUUID, reason)
180
181       await waitJobs(server)
182       expect(emails).to.have.lengthOf(5)
183
184       const email = emails[4]
185
186       expect(email['from'][0]['address']).equal('test-admin@localhost')
187       expect(email['to'][0]['address']).equal('user_1@example.com')
188       expect(email['subject']).contains(' blacklisted')
189       expect(email['text']).contains('my super user video')
190       expect(email['text']).contains('my super reason')
191     })
192
193     it('Should send the notification email', async function () {
194       this.timeout(10000)
195
196       await removeVideoFromBlacklist(server.url, server.accessToken, videoUserUUID)
197
198       await waitJobs(server)
199       expect(emails).to.have.lengthOf(6)
200
201       const email = emails[5]
202
203       expect(email['from'][0]['address']).equal('test-admin@localhost')
204       expect(email['to'][0]['address']).equal('user_1@example.com')
205       expect(email['subject']).contains(' unblacklisted')
206       expect(email['text']).contains('my super user video')
207     })
208   })
209
210   after(async function () {
211     killallServers([ server ])
212   })
213 })