2020-01-31 10:56:52 -05:00
|
|
|
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
|
2018-08-31 03:18:19 -04:00
|
|
|
|
|
|
|
import 'mocha'
|
2021-07-06 04:36:54 -04:00
|
|
|
import * as chai from 'chai'
|
2021-12-17 05:58:15 -05:00
|
|
|
import { MockSmtpServer } from '@server/tests/shared'
|
2021-07-16 08:27:30 -04:00
|
|
|
import { HttpStatusCode } from '@shared/models'
|
2021-12-17 05:58:15 -05:00
|
|
|
import { cleanupTests, createSingleServer, PeerTubeServer, setAccessTokensToServers, waitJobs } from '@shared/server-commands'
|
2018-08-31 03:18:19 -04:00
|
|
|
|
|
|
|
const expect = chai.expect
|
|
|
|
|
|
|
|
describe('Test users account verification', function () {
|
2021-07-16 03:47:51 -04:00
|
|
|
let server: PeerTubeServer
|
2018-08-31 03:18:19 -04:00
|
|
|
let userId: number
|
2019-06-11 05:54:33 -04:00
|
|
|
let userAccessToken: string
|
2018-08-31 03:18:19 -04:00
|
|
|
let verificationString: string
|
|
|
|
let expectedEmailsLength = 0
|
|
|
|
const user1 = {
|
|
|
|
username: 'user_1',
|
|
|
|
password: 'super password'
|
|
|
|
}
|
|
|
|
const user2 = {
|
|
|
|
username: 'user_2',
|
|
|
|
password: 'super password'
|
|
|
|
}
|
|
|
|
const emails: object[] = []
|
|
|
|
|
|
|
|
before(async function () {
|
|
|
|
this.timeout(30000)
|
|
|
|
|
2019-04-25 11:14:49 -04:00
|
|
|
const port = await MockSmtpServer.Instance.collectEmails(emails)
|
2018-08-31 03:18:19 -04:00
|
|
|
|
|
|
|
const overrideConfig = {
|
|
|
|
smtp: {
|
2019-04-25 11:14:49 -04:00
|
|
|
hostname: 'localhost',
|
|
|
|
port
|
2018-08-31 03:18:19 -04:00
|
|
|
}
|
|
|
|
}
|
2021-07-16 03:47:51 -04:00
|
|
|
server = await createSingleServer(1, overrideConfig)
|
2018-08-31 03:18:19 -04:00
|
|
|
|
|
|
|
await setAccessTokensToServers([ server ])
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should register user and send verification email if verification required', async function () {
|
2020-12-11 04:36:05 -05:00
|
|
|
this.timeout(30000)
|
|
|
|
|
2021-07-16 03:04:35 -04:00
|
|
|
await server.config.updateCustomSubConfig({
|
2021-07-07 05:51:09 -04:00
|
|
|
newConfig: {
|
|
|
|
signup: {
|
|
|
|
enabled: true,
|
|
|
|
requiresEmailVerification: true,
|
|
|
|
limit: 10
|
|
|
|
}
|
2018-08-31 03:18:19 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-07-16 03:04:35 -04:00
|
|
|
await server.users.register(user1)
|
2018-08-31 03:18:19 -04:00
|
|
|
|
|
|
|
await waitJobs(server)
|
|
|
|
expectedEmailsLength++
|
|
|
|
expect(emails).to.have.lengthOf(expectedEmailsLength)
|
|
|
|
|
|
|
|
const email = emails[expectedEmailsLength - 1]
|
|
|
|
|
|
|
|
const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
|
|
|
|
expect(verificationStringMatches).not.to.be.null
|
|
|
|
|
|
|
|
verificationString = verificationStringMatches[1]
|
|
|
|
expect(verificationString).to.have.length.above(2)
|
|
|
|
|
|
|
|
const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
|
|
|
|
expect(userIdMatches).not.to.be.null
|
|
|
|
|
|
|
|
userId = parseInt(userIdMatches[1], 10)
|
|
|
|
|
2021-07-16 03:04:35 -04:00
|
|
|
const body = await server.users.get({ userId })
|
2021-07-13 08:23:01 -04:00
|
|
|
expect(body.emailVerified).to.be.false
|
2018-08-31 03:18:19 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('Should not allow login for user with unverified email', async function () {
|
2021-07-16 03:04:35 -04:00
|
|
|
const { detail } = await server.login.login({ user: user1, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
|
2021-07-13 05:05:15 -04:00
|
|
|
expect(detail).to.contain('User email is not verified.')
|
2018-08-31 03:18:19 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('Should verify the user via email and allow login', async function () {
|
2021-07-16 03:04:35 -04:00
|
|
|
await server.users.verifyEmail({ userId, verificationString })
|
2019-06-11 05:54:33 -04:00
|
|
|
|
2021-07-16 03:04:35 -04:00
|
|
|
const body = await server.login.login({ user: user1 })
|
2021-07-13 05:05:15 -04:00
|
|
|
userAccessToken = body.access_token
|
2019-06-11 05:54:33 -04:00
|
|
|
|
2021-07-16 03:04:35 -04:00
|
|
|
const user = await server.users.get({ userId })
|
2021-07-13 08:23:01 -04:00
|
|
|
expect(user.emailVerified).to.be.true
|
2018-08-31 03:18:19 -04:00
|
|
|
})
|
|
|
|
|
2019-06-11 05:54:33 -04:00
|
|
|
it('Should be able to change the user email', async function () {
|
2021-01-04 03:54:48 -05:00
|
|
|
this.timeout(10000)
|
|
|
|
|
2019-06-11 05:54:33 -04:00
|
|
|
let updateVerificationString: string
|
|
|
|
|
|
|
|
{
|
2021-07-16 03:04:35 -04:00
|
|
|
await server.users.updateMe({
|
2021-07-13 08:23:01 -04:00
|
|
|
token: userAccessToken,
|
2019-06-11 09:59:10 -04:00
|
|
|
email: 'updated@example.com',
|
|
|
|
currentPassword: user1.password
|
2019-06-11 05:54:33 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
await waitJobs(server)
|
|
|
|
expectedEmailsLength++
|
|
|
|
expect(emails).to.have.lengthOf(expectedEmailsLength)
|
|
|
|
|
|
|
|
const email = emails[expectedEmailsLength - 1]
|
|
|
|
|
|
|
|
const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
|
|
|
|
updateVerificationString = verificationStringMatches[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-07-16 03:04:35 -04:00
|
|
|
const me = await server.users.getMyInfo({ token: userAccessToken })
|
2019-06-11 05:54:33 -04:00
|
|
|
expect(me.email).to.equal('user_1@example.com')
|
|
|
|
expect(me.pendingEmail).to.equal('updated@example.com')
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-07-16 03:04:35 -04:00
|
|
|
await server.users.verifyEmail({ userId, verificationString: updateVerificationString, isPendingEmail: true })
|
2019-06-11 05:54:33 -04:00
|
|
|
|
2021-07-16 03:04:35 -04:00
|
|
|
const me = await server.users.getMyInfo({ token: userAccessToken })
|
2019-06-11 05:54:33 -04:00
|
|
|
expect(me.email).to.equal('updated@example.com')
|
|
|
|
expect(me.pendingEmail).to.be.null
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-08-31 03:18:19 -04:00
|
|
|
it('Should register user not requiring email verification if setting not enabled', async function () {
|
|
|
|
this.timeout(5000)
|
2021-07-16 03:04:35 -04:00
|
|
|
await server.config.updateCustomSubConfig({
|
2021-07-07 05:51:09 -04:00
|
|
|
newConfig: {
|
|
|
|
signup: {
|
|
|
|
enabled: true,
|
|
|
|
requiresEmailVerification: false,
|
|
|
|
limit: 10
|
|
|
|
}
|
2018-08-31 03:18:19 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-07-16 03:04:35 -04:00
|
|
|
await server.users.register(user2)
|
2018-08-31 03:18:19 -04:00
|
|
|
|
|
|
|
await waitJobs(server)
|
|
|
|
expect(emails).to.have.lengthOf(expectedEmailsLength)
|
|
|
|
|
2021-07-16 03:04:35 -04:00
|
|
|
const accessToken = await server.login.getAccessToken(user2)
|
2018-08-31 03:18:19 -04:00
|
|
|
|
2021-07-16 03:04:35 -04:00
|
|
|
const user = await server.users.getMyInfo({ token: accessToken })
|
2021-07-13 08:23:01 -04:00
|
|
|
expect(user.emailVerified).to.be.null
|
2018-08-31 03:18:19 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('Should allow login for user with unverified email when setting later enabled', async function () {
|
2021-07-16 03:04:35 -04:00
|
|
|
await server.config.updateCustomSubConfig({
|
2021-07-07 05:51:09 -04:00
|
|
|
newConfig: {
|
|
|
|
signup: {
|
|
|
|
enabled: true,
|
|
|
|
requiresEmailVerification: true,
|
|
|
|
limit: 10
|
|
|
|
}
|
2018-08-31 03:18:19 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-07-16 03:04:35 -04:00
|
|
|
await server.login.getAccessToken(user2)
|
2018-08-31 03:18:19 -04:00
|
|
|
})
|
|
|
|
|
2019-04-24 09:10:37 -04:00
|
|
|
after(async function () {
|
2019-01-08 09:51:52 -05:00
|
|
|
MockSmtpServer.Instance.kill()
|
2019-04-24 09:10:37 -04:00
|
|
|
|
|
|
|
await cleanupTests([ server ])
|
2018-08-31 03:18:19 -04:00
|
|
|
})
|
|
|
|
})
|