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