PeerTube/server/tests/api/users/users-email-verification.ts

166 lines
4.7 KiB
TypeScript
Raw Normal View History

2020-01-31 09:56:52 -06:00
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2022-08-17 08:44:32 -05:00
import { expect } from 'chai'
import { MockSmtpServer } from '@server/tests/shared'
2021-07-16 07:27:30 -05:00
import { HttpStatusCode } from '@shared/models'
2023-01-19 02:28:29 -06:00
import {
cleanupTests,
ConfigCommand,
createSingleServer,
PeerTubeServer,
setAccessTokensToServers,
waitJobs
} from '@shared/server-commands'
describe('Test users email verification', function () {
2021-07-16 02:47:51 -05:00
let server: PeerTubeServer
let userId: number
2019-06-11 04:54:33 -05:00
let userAccessToken: string
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)
const port = await MockSmtpServer.Instance.collectEmails(emails)
2023-01-19 02:28:29 -06:00
server = await createSingleServer(1, ConfigCommand.getEmailOverrideConfig(port))
await setAccessTokensToServers([ server ])
})
it('Should register user and send verification email if verification required', async function () {
2020-12-11 03:36:05 -06:00
this.timeout(30000)
2023-01-19 02:28:29 -06:00
await server.config.updateExistingSubConfig({
2021-07-07 04:51:09 -05:00
newConfig: {
signup: {
enabled: true,
2023-01-19 02:28:29 -06:00
requiresApproval: false,
2021-07-07 04:51:09 -05:00
requiresEmailVerification: true,
limit: 10
}
}
})
2023-01-19 02:28:29 -06:00
await server.registrations.register(user1)
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 02:04:35 -05:00
const body = await server.users.get({ userId })
2021-07-13 07:23:01 -05:00
expect(body.emailVerified).to.be.false
})
it('Should not allow login for user with unverified email', async function () {
2021-07-16 02:04:35 -05:00
const { detail } = await server.login.login({ user: user1, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
2021-07-13 04:05:15 -05:00
expect(detail).to.contain('User email is not verified.')
})
it('Should verify the user via email and allow login', async function () {
2021-07-16 02:04:35 -05:00
await server.users.verifyEmail({ userId, verificationString })
2019-06-11 04:54:33 -05:00
2021-07-16 02:04:35 -05:00
const body = await server.login.login({ user: user1 })
2021-07-13 04:05:15 -05:00
userAccessToken = body.access_token
2019-06-11 04:54:33 -05:00
2021-07-16 02:04:35 -05:00
const user = await server.users.get({ userId })
2021-07-13 07:23:01 -05:00
expect(user.emailVerified).to.be.true
})
2019-06-11 04:54:33 -05:00
it('Should be able to change the user email', async function () {
let updateVerificationString: string
{
2021-07-16 02:04:35 -05:00
await server.users.updateMe({
2021-07-13 07:23:01 -05:00
token: userAccessToken,
2019-06-11 08:59:10 -05:00
email: 'updated@example.com',
currentPassword: user1.password
2019-06-11 04:54:33 -05: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 02:04:35 -05:00
const me = await server.users.getMyInfo({ token: userAccessToken })
2019-06-11 04:54:33 -05:00
expect(me.email).to.equal('user_1@example.com')
expect(me.pendingEmail).to.equal('updated@example.com')
}
{
2021-07-16 02:04:35 -05:00
await server.users.verifyEmail({ userId, verificationString: updateVerificationString, isPendingEmail: true })
2019-06-11 04:54:33 -05:00
2021-07-16 02:04:35 -05:00
const me = await server.users.getMyInfo({ token: userAccessToken })
2019-06-11 04:54:33 -05:00
expect(me.email).to.equal('updated@example.com')
expect(me.pendingEmail).to.be.null
}
})
it('Should register user not requiring email verification if setting not enabled', async function () {
this.timeout(5000)
2023-01-19 02:28:29 -06:00
await server.config.updateExistingSubConfig({
2021-07-07 04:51:09 -05:00
newConfig: {
signup: {
2023-01-19 02:28:29 -06:00
requiresEmailVerification: false
2021-07-07 04:51:09 -05:00
}
}
})
2023-01-19 02:28:29 -06:00
await server.registrations.register(user2)
await waitJobs(server)
expect(emails).to.have.lengthOf(expectedEmailsLength)
2021-07-16 02:04:35 -05:00
const accessToken = await server.login.getAccessToken(user2)
2021-07-16 02:04:35 -05:00
const user = await server.users.getMyInfo({ token: accessToken })
2021-07-13 07:23:01 -05:00
expect(user.emailVerified).to.be.null
})
it('Should allow login for user with unverified email when setting later enabled', async function () {
2021-07-16 02:04:35 -05:00
await server.config.updateCustomSubConfig({
2021-07-07 04:51:09 -05:00
newConfig: {
signup: {
2023-01-19 02:28:29 -06:00
requiresEmailVerification: true
2021-07-07 04:51:09 -05:00
}
}
})
2021-07-16 02:04:35 -05:00
await server.login.getAccessToken(user2)
})
2019-04-24 08:10:37 -05:00
after(async function () {
2019-01-08 08:51:52 -06:00
MockSmtpServer.Instance.kill()
2019-04-24 08:10:37 -05:00
await cleanupTests([ server ])
})
})