Fix images size limit
[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 { askResetPassword, createUser, reportVideoAbuse, resetPassword, runServer, uploadVideo, userLogin, wait } from '../../utils'
6 import { flushTests, killallServers, ServerInfo, setAccessTokensToServers } from '../../utils/index'
7 import { mockSmtpServer } from '../../utils/miscs/email'
8 import { waitJobs } from '../../utils/server/jobs'
9
10 const expect = chai.expect
11
12 describe('Test emails', function () {
13   let server: ServerInfo
14   let userId: number
15   let videoUUID: string
16   let verificationString: string
17   const emails: object[] = []
18   const user = {
19     username: 'user_1',
20     password: 'super_password'
21   }
22
23   before(async function () {
24     this.timeout(30000)
25
26     await mockSmtpServer(emails)
27
28     await flushTests()
29
30     const overrideConfig = {
31       smtp: {
32         hostname: 'localhost'
33       }
34     }
35     server = await runServer(1, overrideConfig)
36     await setAccessTokensToServers([ server ])
37
38     {
39       const res = await createUser(server.url, server.accessToken, user.username, user.password)
40       userId = res.body.user.id
41     }
42
43     {
44       const attributes = {
45         name: 'my super name'
46       }
47       const res = await uploadVideo(server.url, server.accessToken, attributes)
48       videoUUID = res.body.video.uuid
49     }
50   })
51
52   describe('When resetting user password', function () {
53
54     it('Should ask to reset the password', async function () {
55       this.timeout(10000)
56
57       await askResetPassword(server.url, 'user_1@example.com')
58
59       await waitJobs(server)
60       expect(emails).to.have.lengthOf(1)
61
62       const email = emails[0]
63
64       expect(email['from'][0]['address']).equal('test-admin@localhost')
65       expect(email['to'][0]['address']).equal('user_1@example.com')
66       expect(email['subject']).contains('password')
67
68       const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
69       expect(verificationStringMatches).not.to.be.null
70
71       verificationString = verificationStringMatches[1]
72       expect(verificationString).to.have.length.above(2)
73
74       const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
75       expect(userIdMatches).not.to.be.null
76
77       userId = parseInt(userIdMatches[1], 10)
78       expect(verificationString).to.not.be.undefined
79     })
80
81     it('Should not reset the password with an invalid verification string', async function () {
82       await resetPassword(server.url, userId, verificationString + 'b', 'super_password2', 403)
83     })
84
85     it('Should reset the password', async function () {
86       await resetPassword(server.url, userId, verificationString, 'super_password2')
87     })
88
89     it('Should login with this new password', async function () {
90       user.password = 'super_password2'
91
92       await userLogin(server, user)
93     })
94   })
95
96   describe('When creating a video abuse', function () {
97     it('Should send the notification email', async function () {
98       this.timeout(10000)
99
100       const reason = 'my super bad reason'
101       await reportVideoAbuse(server.url, server.accessToken, videoUUID, reason)
102
103       await waitJobs(server)
104       expect(emails).to.have.lengthOf(2)
105
106       const email = emails[1]
107
108       expect(email['from'][0]['address']).equal('test-admin@localhost')
109       expect(email['to'][0]['address']).equal('admin1@example.com')
110       expect(email['subject']).contains('abuse')
111       expect(email['text']).contains(videoUUID)
112     })
113   })
114
115   after(async function () {
116     killallServers([ server ])
117   })
118 })