1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { HttpStatusCode } from '@shared/models'
4 import { cleanupTests, createSingleServer, PeerTubeServer, setAccessTokensToServers, TwoFactorCommand } from '@shared/server-commands'
6 describe('Test two factor API validators', function () {
7 let server: PeerTubeServer
10 let rootPassword: string
11 let rootRequestToken: string
12 let rootOTPToken: string
16 let userPassword: string
17 let userRequestToken: string
18 let userOTPToken: string
20 // ---------------------------------------------------------------
22 before(async function () {
26 server = await createSingleServer(1)
27 await setAccessTokensToServers([ server ])
31 const result = await server.users.generate('user1')
32 userToken = result.token
33 userId = result.userId
34 userPassword = result.password
38 const { id } = await server.users.getMyInfo()
40 rootPassword = server.store.user.password
44 describe('When requesting two factor', function () {
46 it('Should fail with an unknown user id', async function () {
47 await server.twoFactor.request({ userId: 42, currentPassword: rootPassword, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
50 it('Should fail with an invalid user id', async function () {
51 await server.twoFactor.request({
52 userId: 'invalid' as any,
53 currentPassword: rootPassword,
54 expectedStatus: HttpStatusCode.BAD_REQUEST_400
58 it('Should fail to request another user two factor without the appropriate rights', async function () {
59 await server.twoFactor.request({
62 currentPassword: userPassword,
63 expectedStatus: HttpStatusCode.FORBIDDEN_403
67 it('Should succeed to request another user two factor with the appropriate rights', async function () {
68 await server.twoFactor.request({ userId, currentPassword: rootPassword })
71 it('Should fail to request two factor without a password', async function () {
72 await server.twoFactor.request({
75 currentPassword: undefined,
76 expectedStatus: HttpStatusCode.BAD_REQUEST_400
80 it('Should fail to request two factor with an incorrect password', async function () {
81 await server.twoFactor.request({
84 currentPassword: rootPassword,
85 expectedStatus: HttpStatusCode.FORBIDDEN_403
89 it('Should succeed to request two factor without a password when targeting a remote user with an admin account', async function () {
90 await server.twoFactor.request({ userId })
93 it('Should fail to request two factor without a password when targeting myself with an admin account', async function () {
94 await server.twoFactor.request({ userId: rootId, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
95 await server.twoFactor.request({ userId: rootId, currentPassword: 'bad', expectedStatus: HttpStatusCode.FORBIDDEN_403 })
98 it('Should succeed to request my two factor auth', async function () {
100 const { otpRequest } = await server.twoFactor.request({ userId, token: userToken, currentPassword: userPassword })
101 userRequestToken = otpRequest.requestToken
102 userOTPToken = TwoFactorCommand.buildOTP({ secret: otpRequest.secret }).generate()
106 const { otpRequest } = await server.twoFactor.request({ userId: rootId, currentPassword: rootPassword })
107 rootRequestToken = otpRequest.requestToken
108 rootOTPToken = TwoFactorCommand.buildOTP({ secret: otpRequest.secret }).generate()
113 describe('When confirming two factor request', function () {
115 it('Should fail with an unknown user id', async function () {
116 await server.twoFactor.confirmRequest({
118 requestToken: rootRequestToken,
119 otpToken: rootOTPToken,
120 expectedStatus: HttpStatusCode.NOT_FOUND_404
124 it('Should fail with an invalid user id', async function () {
125 await server.twoFactor.confirmRequest({
126 userId: 'invalid' as any,
127 requestToken: rootRequestToken,
128 otpToken: rootOTPToken,
129 expectedStatus: HttpStatusCode.BAD_REQUEST_400
133 it('Should fail to confirm another user two factor request without the appropriate rights', async function () {
134 await server.twoFactor.confirmRequest({
137 requestToken: rootRequestToken,
138 otpToken: rootOTPToken,
139 expectedStatus: HttpStatusCode.FORBIDDEN_403
143 it('Should fail without request token', async function () {
144 await server.twoFactor.confirmRequest({
146 requestToken: undefined,
147 otpToken: userOTPToken,
148 expectedStatus: HttpStatusCode.BAD_REQUEST_400
152 it('Should fail with an invalid request token', async function () {
153 await server.twoFactor.confirmRequest({
155 requestToken: 'toto',
156 otpToken: userOTPToken,
157 expectedStatus: HttpStatusCode.FORBIDDEN_403
161 it('Should fail with request token of another user', async function () {
162 await server.twoFactor.confirmRequest({
164 requestToken: rootRequestToken,
165 otpToken: userOTPToken,
166 expectedStatus: HttpStatusCode.FORBIDDEN_403
170 it('Should fail without an otp token', async function () {
171 await server.twoFactor.confirmRequest({
173 requestToken: userRequestToken,
175 expectedStatus: HttpStatusCode.BAD_REQUEST_400
179 it('Should fail with a bad otp token', async function () {
180 await server.twoFactor.confirmRequest({
182 requestToken: userRequestToken,
184 expectedStatus: HttpStatusCode.FORBIDDEN_403
188 it('Should succeed to confirm another user two factor request with the appropriate rights', async function () {
189 await server.twoFactor.confirmRequest({
191 requestToken: userRequestToken,
192 otpToken: userOTPToken
196 await server.twoFactor.disable({ userId, currentPassword: rootPassword })
199 it('Should succeed to confirm my two factor request', async function () {
200 await server.twoFactor.confirmRequest({
203 requestToken: userRequestToken,
204 otpToken: userOTPToken
208 it('Should fail to confirm again two factor request', async function () {
209 await server.twoFactor.confirmRequest({
212 requestToken: userRequestToken,
213 otpToken: userOTPToken,
214 expectedStatus: HttpStatusCode.BAD_REQUEST_400
219 describe('When disabling two factor', function () {
221 it('Should fail with an unknown user id', async function () {
222 await server.twoFactor.disable({
224 currentPassword: rootPassword,
225 expectedStatus: HttpStatusCode.NOT_FOUND_404
229 it('Should fail with an invalid user id', async function () {
230 await server.twoFactor.disable({
231 userId: 'invalid' as any,
232 currentPassword: rootPassword,
233 expectedStatus: HttpStatusCode.BAD_REQUEST_400
237 it('Should fail to disable another user two factor without the appropriate rights', async function () {
238 await server.twoFactor.disable({
241 currentPassword: userPassword,
242 expectedStatus: HttpStatusCode.FORBIDDEN_403
246 it('Should fail to disable two factor with an incorrect password', async function () {
247 await server.twoFactor.disable({
250 currentPassword: rootPassword,
251 expectedStatus: HttpStatusCode.FORBIDDEN_403
255 it('Should succeed to disable two factor without a password when targeting a remote user with an admin account', async function () {
256 await server.twoFactor.disable({ userId })
257 await server.twoFactor.requestAndConfirm({ userId })
260 it('Should fail to disable two factor without a password when targeting myself with an admin account', async function () {
261 await server.twoFactor.disable({ userId: rootId, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
262 await server.twoFactor.disable({ userId: rootId, currentPassword: 'bad', expectedStatus: HttpStatusCode.FORBIDDEN_403 })
265 it('Should succeed to disable another user two factor with the appropriate rights', async function () {
266 await server.twoFactor.disable({ userId, currentPassword: rootPassword })
268 await server.twoFactor.requestAndConfirm({ userId })
271 it('Should succeed to update my two factor auth', async function () {
272 await server.twoFactor.disable({ userId, token: userToken, currentPassword: userPassword })
275 it('Should fail to disable again two factor', async function () {
276 await server.twoFactor.disable({
279 currentPassword: userPassword,
280 expectedStatus: HttpStatusCode.BAD_REQUEST_400
285 after(async function () {
286 await cleanupTests([ server ])