1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { expect } from 'chai'
4 import { expectStartWith } from '@server/tests/shared'
5 import { HttpStatusCode } from '@shared/models'
6 import { cleanupTests, createSingleServer, PeerTubeServer, setAccessTokensToServers, TwoFactorCommand } from '@shared/server-commands'
8 async function login (options: {
13 expectedStatus?: HttpStatusCode
15 const { server, username, password, otpToken, expectedStatus } = options
17 const user = { username, password }
18 const { res, body: { access_token: token } } = await server.login.loginAndGetResponse({ user, otpToken, expectedStatus })
23 describe('Test users', function () {
24 let server: PeerTubeServer
26 let requestToken: string
28 const userUsername = 'user1'
30 let userPassword: string
33 before(async function () {
36 server = await createSingleServer(1)
38 await setAccessTokensToServers([ server ])
39 const res = await server.users.generate(userUsername)
41 userPassword = res.password
45 it('Should not add the header on login if two factor is not enabled', async function () {
46 const { res, token } = await login({ server, username: userUsername, password: userPassword })
48 expect(res.header['x-peertube-otp']).to.not.exist
50 await server.users.getMyInfo({ token })
53 it('Should request two factor and get the secret and uri', async function () {
54 const { otpRequest } = await server.twoFactor.request({ userId, token: userToken, currentPassword: userPassword })
56 expect(otpRequest.requestToken).to.exist
58 expect(otpRequest.secret).to.exist
59 expect(otpRequest.secret).to.have.lengthOf(32)
61 expect(otpRequest.uri).to.exist
62 expectStartWith(otpRequest.uri, 'otpauth://')
63 expect(otpRequest.uri).to.include(otpRequest.secret)
65 requestToken = otpRequest.requestToken
66 otpSecret = otpRequest.secret
69 it('Should not have two factor confirmed yet', async function () {
70 const { twoFactorEnabled } = await server.users.getMyInfo({ token: userToken })
71 expect(twoFactorEnabled).to.be.false
74 it('Should confirm two factor', async function () {
75 await server.twoFactor.confirmRequest({
78 otpToken: TwoFactorCommand.buildOTP({ secret: otpSecret }).generate(),
83 it('Should not add the header on login if two factor is enabled and password is incorrect', async function () {
84 const { res, token } = await login({ server, username: userUsername, password: 'fake', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
86 expect(res.header['x-peertube-otp']).to.not.exist
87 expect(token).to.not.exist
90 it('Should add the header on login if two factor is enabled and password is correct', async function () {
91 const { res, token } = await login({
93 username: userUsername,
94 password: userPassword,
95 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
98 expect(res.header['x-peertube-otp']).to.exist
99 expect(token).to.not.exist
101 await server.users.getMyInfo({ token })
104 it('Should not login with correct password and incorrect otp secret', async function () {
105 const otp = TwoFactorCommand.buildOTP({ secret: 'a'.repeat(32) })
107 const { res, token } = await login({
109 username: userUsername,
110 password: userPassword,
111 otpToken: otp.generate(),
112 expectedStatus: HttpStatusCode.BAD_REQUEST_400
115 expect(res.header['x-peertube-otp']).to.not.exist
116 expect(token).to.not.exist
119 it('Should not login with correct password and incorrect otp code', async function () {
120 const { res, token } = await login({
122 username: userUsername,
123 password: userPassword,
125 expectedStatus: HttpStatusCode.BAD_REQUEST_400
128 expect(res.header['x-peertube-otp']).to.not.exist
129 expect(token).to.not.exist
132 it('Should not login with incorrect password and correct otp code', async function () {
133 const otpToken = TwoFactorCommand.buildOTP({ secret: otpSecret }).generate()
135 const { res, token } = await login({
137 username: userUsername,
140 expectedStatus: HttpStatusCode.BAD_REQUEST_400
143 expect(res.header['x-peertube-otp']).to.not.exist
144 expect(token).to.not.exist
147 it('Should correctly login with correct password and otp code', async function () {
148 const otpToken = TwoFactorCommand.buildOTP({ secret: otpSecret }).generate()
150 const { res, token } = await login({ server, username: userUsername, password: userPassword, otpToken })
152 expect(res.header['x-peertube-otp']).to.not.exist
153 expect(token).to.exist
155 await server.users.getMyInfo({ token })
158 it('Should have two factor enabled when getting my info', async function () {
159 const { twoFactorEnabled } = await server.users.getMyInfo({ token: userToken })
160 expect(twoFactorEnabled).to.be.true
163 it('Should disable two factor and be able to login without otp token', async function () {
164 await server.twoFactor.disable({ userId, token: userToken, currentPassword: userPassword })
166 const { res, token } = await login({ server, username: userUsername, password: userPassword })
167 expect(res.header['x-peertube-otp']).to.not.exist
169 await server.users.getMyInfo({ token })
172 it('Should have two factor disabled when getting my info', async function () {
173 const { twoFactorEnabled } = await server.users.getMyInfo({ token: userToken })
174 expect(twoFactorEnabled).to.be.false
177 it('Should enable two factor auth without password from an admin', async function () {
178 const { otpRequest } = await server.twoFactor.request({ userId })
180 await server.twoFactor.confirmRequest({
182 otpToken: TwoFactorCommand.buildOTP({ secret: otpRequest.secret }).generate(),
183 requestToken: otpRequest.requestToken
186 const { twoFactorEnabled } = await server.users.getMyInfo({ token: userToken })
187 expect(twoFactorEnabled).to.be.true
190 it('Should disable two factor auth without password from an admin', async function () {
191 await server.twoFactor.disable({ userId })
193 const { twoFactorEnabled } = await server.users.getMyInfo({ token: userToken })
194 expect(twoFactorEnabled).to.be.false
197 after(async function () {
198 await cleanupTests([ server ])