]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/server/email.ts
Add ability to delete and update abuse on client
[github/Chocobozzz/PeerTube.git] / server / tests / api / server / email.ts
CommitLineData
f076daa7
C
1/* tslint:disable:no-unused-expression */
2
3import * as chai from 'chai'
4import 'mocha'
eacb25c4
C
5import {
6 askResetPassword,
7 blockUser,
8 createUser,
9 reportVideoAbuse,
10 resetPassword,
11 runServer,
12 unblockUser,
13 uploadVideo,
14 userLogin
15} from '../../utils'
f076daa7
C
16import { flushTests, killallServers, ServerInfo, setAccessTokensToServers } from '../../utils/index'
17import { mockSmtpServer } from '../../utils/miscs/email'
3cd0734f 18import { waitJobs } from '../../utils/server/jobs'
f076daa7
C
19
20const expect = chai.expect
21
22describe('Test emails', function () {
23 let server: ServerInfo
24 let userId: number
ba75d268 25 let videoUUID: string
f076daa7
C
26 let verificationString: string
27 const emails: object[] = []
28 const user = {
29 username: 'user_1',
30 password: 'super_password'
31 }
32
33 before(async function () {
34 this.timeout(30000)
35
36 await mockSmtpServer(emails)
37
38 await flushTests()
39
40 const overrideConfig = {
41 smtp: {
42 hostname: 'localhost'
43 }
44 }
45 server = await runServer(1, overrideConfig)
f076daa7
C
46 await setAccessTokensToServers([ server ])
47
ba75d268
C
48 {
49 const res = await createUser(server.url, server.accessToken, user.username, user.password)
50 userId = res.body.user.id
51 }
52
53 {
54 const attributes = {
55 name: 'my super name'
56 }
57 const res = await uploadVideo(server.url, server.accessToken, attributes)
58 videoUUID = res.body.video.uuid
59 }
f076daa7
C
60 })
61
62 describe('When resetting user password', function () {
63
64 it('Should ask to reset the password', async function () {
65 this.timeout(10000)
66
67 await askResetPassword(server.url, 'user_1@example.com')
68
3cd0734f 69 await waitJobs(server)
f076daa7
C
70 expect(emails).to.have.lengthOf(1)
71
72 const email = emails[0]
73
74 expect(email['from'][0]['address']).equal('test-admin@localhost')
75 expect(email['to'][0]['address']).equal('user_1@example.com')
76 expect(email['subject']).contains('password')
77
78 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
79 expect(verificationStringMatches).not.to.be.null
80
81 verificationString = verificationStringMatches[1]
82 expect(verificationString).to.have.length.above(2)
83
84 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
85 expect(userIdMatches).not.to.be.null
86
87 userId = parseInt(userIdMatches[1], 10)
88 expect(verificationString).to.not.be.undefined
89 })
90
91 it('Should not reset the password with an invalid verification string', async function () {
92 await resetPassword(server.url, userId, verificationString + 'b', 'super_password2', 403)
93 })
94
95 it('Should reset the password', async function () {
96 await resetPassword(server.url, userId, verificationString, 'super_password2')
97 })
98
99 it('Should login with this new password', async function () {
100 user.password = 'super_password2'
101
102 await userLogin(server, user)
103 })
104 })
105
ba75d268
C
106 describe('When creating a video abuse', function () {
107 it('Should send the notification email', async function () {
108 this.timeout(10000)
109
110 const reason = 'my super bad reason'
111 await reportVideoAbuse(server.url, server.accessToken, videoUUID, reason)
112
3cd0734f 113 await waitJobs(server)
ba75d268
C
114 expect(emails).to.have.lengthOf(2)
115
116 const email = emails[1]
117
118 expect(email['from'][0]['address']).equal('test-admin@localhost')
119 expect(email['to'][0]['address']).equal('admin1@example.com')
120 expect(email['subject']).contains('abuse')
121 expect(email['text']).contains(videoUUID)
122 })
123 })
124
eacb25c4
C
125 describe('When blocking/unblocking user', async function () {
126 it('Should send the notification email when blocking a user', async function () {
127 this.timeout(10000)
128
129 const reason = 'my super bad reason'
130 await blockUser(server.url, userId, server.accessToken, 204, reason)
131
132 await waitJobs(server)
133 expect(emails).to.have.lengthOf(3)
134
135 const email = emails[2]
136
137 expect(email['from'][0]['address']).equal('test-admin@localhost')
138 expect(email['to'][0]['address']).equal('user_1@example.com')
139 expect(email['subject']).contains(' blocked')
140 expect(email['text']).contains(' blocked')
141 expect(email['text']).contains(reason)
142 })
143
144 it('Should send the notification email when unblocking a user', async function () {
145 this.timeout(10000)
146
147 await unblockUser(server.url, userId, server.accessToken, 204)
148
149 await waitJobs(server)
150 expect(emails).to.have.lengthOf(4)
151
152 const email = emails[3]
153
154 expect(email['from'][0]['address']).equal('test-admin@localhost')
155 expect(email['to'][0]['address']).equal('user_1@example.com')
156 expect(email['subject']).contains(' unblocked')
157 expect(email['text']).contains(' unblocked')
158 })
159 })
160
f076daa7
C
161 after(async function () {
162 killallServers([ server ])
f076daa7
C
163 })
164})