Cleanup tmp directory at startup
[oweals/peertube.git] / server / tests / api / users / users-verification.ts
1 /* tslint:disable:no-unused-expression */
2
3 import * as chai from 'chai'
4 import 'mocha'
5 import {
6   registerUser, flushTests, getUserInformation, getMyUserInformation, killallServers,
7   userLogin, login, runServer, ServerInfo, verifyEmail, updateCustomSubConfig, wait
8 } from '../../../../shared/utils'
9 import { setAccessTokensToServers } from '../../../../shared/utils/users/login'
10 import { MockSmtpServer } from '../../../../shared/utils/miscs/email'
11 import { waitJobs } from '../../../../shared/utils/server/jobs'
12
13 const expect = chai.expect
14
15 describe('Test users account verification', function () {
16   let server: ServerInfo
17   let userId: number
18   let verificationString: string
19   let expectedEmailsLength = 0
20   const user1 = {
21     username: 'user_1',
22     password: 'super password'
23   }
24   const user2 = {
25     username: 'user_2',
26     password: 'super password'
27   }
28   const emails: object[] = []
29
30   before(async function () {
31     this.timeout(30000)
32
33     await MockSmtpServer.Instance.collectEmails(emails)
34
35     await flushTests()
36
37     const overrideConfig = {
38       smtp: {
39         hostname: 'localhost'
40       }
41     }
42     server = await runServer(1, overrideConfig)
43
44     await setAccessTokensToServers([ server ])
45   })
46
47   it('Should register user and send verification email if verification required', async function () {
48     this.timeout(5000)
49     await updateCustomSubConfig(server.url, server.accessToken, {
50       signup: {
51         enabled: true,
52         requiresEmailVerification: true,
53         limit: 10
54       }
55     })
56
57     await registerUser(server.url, user1.username, user1.password)
58
59     await waitJobs(server)
60     expectedEmailsLength++
61     expect(emails).to.have.lengthOf(expectedEmailsLength)
62
63     const email = emails[expectedEmailsLength - 1]
64
65     const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
66     expect(verificationStringMatches).not.to.be.null
67
68     verificationString = verificationStringMatches[1]
69     expect(verificationString).to.have.length.above(2)
70
71     const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
72     expect(userIdMatches).not.to.be.null
73
74     userId = parseInt(userIdMatches[1], 10)
75
76     const resUserInfo = await getUserInformation(server.url, server.accessToken, userId)
77     expect(resUserInfo.body.emailVerified).to.be.false
78   })
79
80   it('Should not allow login for user with unverified email', async function () {
81     const resLogin = await login(server.url, server.client, user1, 400)
82     expect(resLogin.body.error).to.contain('User email is not verified.')
83   })
84
85   it('Should verify the user via email and allow login', async function () {
86     await verifyEmail(server.url, userId, verificationString)
87     await login(server.url, server.client, user1)
88     const resUserVerified = await getUserInformation(server.url, server.accessToken, userId)
89     expect(resUserVerified.body.emailVerified).to.be.true
90   })
91
92   it('Should register user not requiring email verification if setting not enabled', async function () {
93     this.timeout(5000)
94     await updateCustomSubConfig(server.url, server.accessToken, {
95       signup: {
96         enabled: true,
97         requiresEmailVerification: false,
98         limit: 10
99       }
100     })
101
102     await registerUser(server.url, user2.username, user2.password)
103
104     await waitJobs(server)
105     expect(emails).to.have.lengthOf(expectedEmailsLength)
106
107     const accessToken = await userLogin(server, user2)
108
109     const resMyUserInfo = await getMyUserInformation(server.url, accessToken)
110     expect(resMyUserInfo.body.emailVerified).to.be.null
111   })
112
113   it('Should allow login for user with unverified email when setting later enabled', async function () {
114     await updateCustomSubConfig(server.url, server.accessToken, {
115       signup: {
116         enabled: true,
117         requiresEmailVerification: true,
118         limit: 10
119       }
120     })
121
122     await userLogin(server, user2)
123   })
124
125   after(async function () {
126     MockSmtpServer.Instance.kill()
127     killallServers([ server ])
128
129     // Keep the logs if the test failed
130     if (this[ 'ok' ]) {
131       await flushTests()
132     }
133   })
134 })