X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;ds=sidebyside;f=server%2Ftests%2Fapi%2Fusers%2Fusers-verification.ts;h=9b23ba7972a76ed400a9e82bf502aeb87d751d75;hb=bf54587a3e2ad9c2c186828f2a5682b91ee2cc00;hp=295caae978dd13f71397ef69dcc16b05a2308723;hpb=210feb6cc484a6c5c63c98f770de34e223f944cb;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/tests/api/users/users-verification.ts b/server/tests/api/users/users-verification.ts index 295caae97..9b23ba797 100644 --- a/server/tests/api/users/users-verification.ts +++ b/server/tests/api/users/users-verification.ts @@ -1,20 +1,16 @@ -/* tslint:disable:no-unused-expression */ +/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ -import * as chai from 'chai' import 'mocha' -import { - registerUser, flushTests, getUserInformation, getMyUserInformation, killallServers, - userLogin, login, flushAndRunServer, ServerInfo, verifyEmail, updateCustomSubConfig, wait -} from '../../../../shared/extra-utils' -import { setAccessTokensToServers } from '../../../../shared/extra-utils/users/login' -import { MockSmtpServer } from '../../../../shared/extra-utils/miscs/email' -import { waitJobs } from '../../../../shared/extra-utils/server/jobs' +import * as chai from 'chai' +import { cleanupTests, createSingleServer, MockSmtpServer, PeerTubeServer, setAccessTokensToServers, waitJobs } from '@shared/server-commands' +import { HttpStatusCode } from '@shared/models' const expect = chai.expect describe('Test users account verification', function () { - let server: ServerInfo + let server: PeerTubeServer let userId: number + let userAccessToken: string let verificationString: string let expectedEmailsLength = 0 const user1 = { @@ -30,29 +26,33 @@ describe('Test users account verification', function () { before(async function () { this.timeout(30000) - await MockSmtpServer.Instance.collectEmails(emails) + const port = await MockSmtpServer.Instance.collectEmails(emails) const overrideConfig = { smtp: { - hostname: 'localhost' + hostname: 'localhost', + port } } - server = await flushAndRunServer(1, overrideConfig) + server = await createSingleServer(1, overrideConfig) await setAccessTokensToServers([ server ]) }) it('Should register user and send verification email if verification required', async function () { - this.timeout(5000) - await updateCustomSubConfig(server.url, server.accessToken, { - signup: { - enabled: true, - requiresEmailVerification: true, - limit: 10 + this.timeout(30000) + + await server.config.updateCustomSubConfig({ + newConfig: { + signup: { + enabled: true, + requiresEmailVerification: true, + limit: 10 + } } }) - await registerUser(server.url, user1.username, user1.password) + await server.users.register(user1) await waitJobs(server) expectedEmailsLength++ @@ -71,57 +71,102 @@ describe('Test users account verification', function () { userId = parseInt(userIdMatches[1], 10) - const resUserInfo = await getUserInformation(server.url, server.accessToken, userId) - expect(resUserInfo.body.emailVerified).to.be.false + const body = await server.users.get({ userId }) + expect(body.emailVerified).to.be.false }) it('Should not allow login for user with unverified email', async function () { - const resLogin = await login(server.url, server.client, user1, 400) - expect(resLogin.body.error).to.contain('User email is not verified.') + const { detail } = await server.login.login({ user: user1, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) + expect(detail).to.contain('User email is not verified.') }) it('Should verify the user via email and allow login', async function () { - await verifyEmail(server.url, userId, verificationString) - await login(server.url, server.client, user1) - const resUserVerified = await getUserInformation(server.url, server.accessToken, userId) - expect(resUserVerified.body.emailVerified).to.be.true + await server.users.verifyEmail({ userId, verificationString }) + + const body = await server.login.login({ user: user1 }) + userAccessToken = body.access_token + + const user = await server.users.get({ userId }) + expect(user.emailVerified).to.be.true + }) + + it('Should be able to change the user email', async function () { + this.timeout(10000) + + let updateVerificationString: string + + { + await server.users.updateMe({ + token: userAccessToken, + email: 'updated@example.com', + currentPassword: user1.password + }) + + 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] + } + + { + const me = await server.users.getMyInfo({ token: userAccessToken }) + expect(me.email).to.equal('user_1@example.com') + expect(me.pendingEmail).to.equal('updated@example.com') + } + + { + await server.users.verifyEmail({ userId, verificationString: updateVerificationString, isPendingEmail: true }) + + const me = await server.users.getMyInfo({ token: userAccessToken }) + 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) - await updateCustomSubConfig(server.url, server.accessToken, { - signup: { - enabled: true, - requiresEmailVerification: false, - limit: 10 + await server.config.updateCustomSubConfig({ + newConfig: { + signup: { + enabled: true, + requiresEmailVerification: false, + limit: 10 + } } }) - await registerUser(server.url, user2.username, user2.password) + await server.users.register(user2) await waitJobs(server) expect(emails).to.have.lengthOf(expectedEmailsLength) - const accessToken = await userLogin(server, user2) + const accessToken = await server.login.getAccessToken(user2) - const resMyUserInfo = await getMyUserInformation(server.url, accessToken) - expect(resMyUserInfo.body.emailVerified).to.be.null + const user = await server.users.getMyInfo({ token: accessToken }) + expect(user.emailVerified).to.be.null }) it('Should allow login for user with unverified email when setting later enabled', async function () { - await updateCustomSubConfig(server.url, server.accessToken, { - signup: { - enabled: true, - requiresEmailVerification: true, - limit: 10 + await server.config.updateCustomSubConfig({ + newConfig: { + signup: { + enabled: true, + requiresEmailVerification: true, + limit: 10 + } } }) - await userLogin(server, user2) + await server.login.getAccessToken(user2) }) - after(function () { + after(async function () { MockSmtpServer.Instance.kill() - killallServers([ server ]) + + await cleanupTests([ server ]) }) })