From 41d1d075011174e73dccb74006181a92a618d7b4 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 13 Jul 2021 11:05:15 +0200 Subject: Introduce login command --- server/tests/plugins/action-hooks.ts | 3 +- server/tests/plugins/external-auth.ts | 57 ++++++++++++++++---------------- server/tests/plugins/id-and-pass-auth.ts | 49 ++++++++++++++------------- 3 files changed, 56 insertions(+), 53 deletions(-) (limited to 'server/tests/plugins') diff --git a/server/tests/plugins/action-hooks.ts b/server/tests/plugins/action-hooks.ts index cf81e44b7..84f4e8501 100644 --- a/server/tests/plugins/action-hooks.ts +++ b/server/tests/plugins/action-hooks.ts @@ -14,7 +14,6 @@ import { updateUser, updateVideo, uploadVideo, - userLogin, viewVideo } from '../../../shared/extra-utils' import { @@ -138,7 +137,7 @@ describe('Test plugin action hooks', function () { }) it('Should run action:api.user.oauth2-got-token', async function () { - await userLogin(servers[0], { username: 'created_user', password: 'super_password' }) + await servers[0].loginCommand.getAccessToken('created_user', 'super_password') await checkHook('action:api.user.oauth2-got-token') }) diff --git a/server/tests/plugins/external-auth.ts b/server/tests/plugins/external-auth.ts index f7cee588a..e421fd224 100644 --- a/server/tests/plugins/external-auth.ts +++ b/server/tests/plugins/external-auth.ts @@ -9,14 +9,10 @@ import { decodeQueryString, flushAndRunServer, getMyUserInformation, - loginUsingExternalToken, - logout, PluginsCommand, - refreshToken, ServerInfo, setAccessTokensToServers, updateMyUser, - userLogin, wait } from '@shared/extra-utils' import { User, UserRole } from '@shared/models' @@ -43,12 +39,11 @@ async function loginExternal (options: { const location = res.header.location const { externalAuthToken } = decodeQueryString(location) - const resLogin = await loginUsingExternalToken( - options.server, - options.username, - externalAuthToken as string, - options.statusCodeExpectedStep2 - ) + const resLogin = await options.server.loginCommand.loginUsingExternalToken({ + username: options.username, + externalAuthToken: externalAuthToken as string, + expectedStatus: options.statusCodeExpectedStep2 + }) return resLogin.body } @@ -110,13 +105,17 @@ describe('Test external auth plugins', function () { }) it('Should reject auto external login with a missing or invalid token', async function () { - await loginUsingExternalToken(server, 'cyan', '', HttpStatusCode.BAD_REQUEST_400) - await loginUsingExternalToken(server, 'cyan', 'blabla', HttpStatusCode.BAD_REQUEST_400) + const command = server.loginCommand + + await command.loginUsingExternalToken({ username: 'cyan', externalAuthToken: '', expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) + await command.loginUsingExternalToken({ username: 'cyan', externalAuthToken: 'blabla', expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) }) it('Should reject auto external login with a missing or invalid username', async function () { - await loginUsingExternalToken(server, '', externalAuthToken, HttpStatusCode.BAD_REQUEST_400) - await loginUsingExternalToken(server, '', externalAuthToken, HttpStatusCode.BAD_REQUEST_400) + const command = server.loginCommand + + await command.loginUsingExternalToken({ username: '', externalAuthToken, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) + await command.loginUsingExternalToken({ username: '', externalAuthToken, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) }) it('Should reject auto external login with an expired token', async function () { @@ -124,7 +123,11 @@ describe('Test external auth plugins', function () { await wait(5000) - await loginUsingExternalToken(server, 'cyan', externalAuthToken, HttpStatusCode.BAD_REQUEST_400) + await server.loginCommand.loginUsingExternalToken({ + username: 'cyan', + externalAuthToken, + expectedStatus: HttpStatusCode.BAD_REQUEST_400 + }) await server.serversCommand.waitUntilLog('expired external auth token', 2) }) @@ -182,7 +185,7 @@ describe('Test external auth plugins', function () { it('Should refresh Cyan token, but not Kefka token', async function () { { - const resRefresh = await refreshToken(server, cyanRefreshToken) + const resRefresh = await server.loginCommand.refreshToken({ refreshToken: cyanRefreshToken }) cyanAccessToken = resRefresh.body.access_token cyanRefreshToken = resRefresh.body.refresh_token @@ -192,7 +195,7 @@ describe('Test external auth plugins', function () { } { - await refreshToken(server, kefkaRefreshToken, HttpStatusCode.BAD_REQUEST_400) + await server.loginCommand.refreshToken({ refreshToken: kefkaRefreshToken, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) } }) @@ -212,7 +215,7 @@ describe('Test external auth plugins', function () { }) it('Should logout Cyan', async function () { - await logout(server.url, cyanAccessToken) + await server.loginCommand.logout({ token: cyanAccessToken }) }) it('Should have logged out Cyan', async function () { @@ -269,7 +272,7 @@ describe('Test external auth plugins', function () { settings: { disableKefka: true } }) - await userLogin(server, { username: 'kefka', password: 'fake' }, HttpStatusCode.BAD_REQUEST_400) + await server.loginCommand.login({ user: { username: 'kefka', password: 'fake' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) await loginExternal({ server, @@ -307,9 +310,9 @@ describe('Test external auth plugins', function () { statusCodeExpected: HttpStatusCode.NOT_FOUND_404 }) - await userLogin(server, { username: 'cyan', password: null }, HttpStatusCode.BAD_REQUEST_400) - await userLogin(server, { username: 'cyan', password: '' }, HttpStatusCode.BAD_REQUEST_400) - await userLogin(server, { username: 'cyan', password: 'fake' }, HttpStatusCode.BAD_REQUEST_400) + await server.loginCommand.login({ user: { username: 'cyan', password: null }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) + await server.loginCommand.login({ user: { username: 'cyan', password: '' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) + await server.loginCommand.login({ user: { username: 'cyan', password: 'fake' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) }) it('Should not login kefka with another plugin', async function () { @@ -369,9 +372,8 @@ describe('Test external auth plugins', function () { username: 'cid' }) - const resLogout = await logout(server.url, resLogin.access_token) - - expect(resLogout.body.redirectUrl).to.equal('https://example.com/redirectUrl') + const { redirectUrl } = await server.loginCommand.logout({ token: resLogin.access_token }) + expect(redirectUrl).to.equal('https://example.com/redirectUrl') }) it('Should call the plugin\'s onLogout method with the request', async function () { @@ -382,8 +384,7 @@ describe('Test external auth plugins', function () { username: 'cid' }) - const resLogout = await logout(server.url, resLogin.access_token) - - expect(resLogout.body.redirectUrl).to.equal('https://example.com/redirectUrl?access_token=' + resLogin.access_token) + const { redirectUrl } = await server.loginCommand.logout({ token: resLogin.access_token }) + expect(redirectUrl).to.equal('https://example.com/redirectUrl?access_token=' + resLogin.access_token) }) }) diff --git a/server/tests/plugins/id-and-pass-auth.ts b/server/tests/plugins/id-and-pass-auth.ts index a0b31bc1f..e3da64110 100644 --- a/server/tests/plugins/id-and-pass-auth.ts +++ b/server/tests/plugins/id-and-pass-auth.ts @@ -2,19 +2,16 @@ import 'mocha' import { expect } from 'chai' +import { HttpStatusCode } from '@shared/core-utils' import { cleanupTests, flushAndRunServer, getMyUserInformation, getUsersList, - login, - logout, PluginsCommand, - refreshToken, ServerInfo, setAccessTokensToServers, updateMyUser, - userLogin, wait } from '@shared/extra-utils' import { User, UserRole } from '@shared/models' @@ -52,11 +49,11 @@ describe('Test id and pass auth plugins', function () { }) it('Should not login', async function () { - await userLogin(server, { username: 'toto', password: 'password' }, 400) + await server.loginCommand.login({ user: { username: 'toto', password: 'password' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) }) it('Should login Spyro, create the user and use the token', async function () { - const accessToken = await userLogin(server, { username: 'spyro', password: 'spyro password' }) + const accessToken = await server.loginCommand.getAccessToken({ username: 'spyro', password: 'spyro password' }) const res = await getMyUserInformation(server.url, accessToken) @@ -68,9 +65,9 @@ describe('Test id and pass auth plugins', function () { it('Should login Crash, create the user and use the token', async function () { { - const res = await login(server.url, server.client, { username: 'crash', password: 'crash password' }) - crashAccessToken = res.body.access_token - crashRefreshToken = res.body.refresh_token + const body = await server.loginCommand.login({ user: { username: 'crash', password: 'crash password' } }) + crashAccessToken = body.access_token + crashRefreshToken = body.refresh_token } { @@ -85,9 +82,9 @@ describe('Test id and pass auth plugins', function () { it('Should login the first Laguna, create the user and use the token', async function () { { - const res = await login(server.url, server.client, { username: 'laguna', password: 'laguna password' }) - lagunaAccessToken = res.body.access_token - lagunaRefreshToken = res.body.refresh_token + const body = await server.loginCommand.login({ user: { username: 'laguna', password: 'laguna password' } }) + lagunaAccessToken = body.access_token + lagunaRefreshToken = body.refresh_token } { @@ -102,7 +99,7 @@ describe('Test id and pass auth plugins', function () { it('Should refresh crash token, but not laguna token', async function () { { - const resRefresh = await refreshToken(server, crashRefreshToken) + const resRefresh = await server.loginCommand.refreshToken({ refreshToken: crashRefreshToken }) crashAccessToken = resRefresh.body.access_token crashRefreshToken = resRefresh.body.refresh_token @@ -112,7 +109,7 @@ describe('Test id and pass auth plugins', function () { } { - await refreshToken(server, lagunaRefreshToken, 400) + await server.loginCommand.refreshToken({ refreshToken: lagunaRefreshToken, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) } }) @@ -132,7 +129,7 @@ describe('Test id and pass auth plugins', function () { }) it('Should logout Crash', async function () { - await logout(server.url, crashAccessToken) + await server.loginCommand.logout({ token: crashAccessToken }) }) it('Should have logged out Crash', async function () { @@ -142,7 +139,7 @@ describe('Test id and pass auth plugins', function () { }) it('Should login Crash and keep the old existing profile', async function () { - crashAccessToken = await userLogin(server, { username: 'crash', password: 'crash password' }) + crashAccessToken = await server.loginCommand.getAccessToken({ username: 'crash', password: 'crash password' }) const res = await getMyUserInformation(server.url, crashAccessToken) @@ -162,16 +159,18 @@ describe('Test id and pass auth plugins', function () { }) it('Should reject an invalid username, email, role or display name', async function () { - await userLogin(server, { username: 'ward', password: 'ward password' }, 400) + const command = server.loginCommand + + await command.login({ user: { username: 'ward', password: 'ward password' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) await server.serversCommand.waitUntilLog('valid username') - await userLogin(server, { username: 'kiros', password: 'kiros password' }, 400) + await command.login({ user: { username: 'kiros', password: 'kiros password' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) await server.serversCommand.waitUntilLog('valid display name') - await userLogin(server, { username: 'raine', password: 'raine password' }, 400) + await command.login({ user: { username: 'raine', password: 'raine password' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) await server.serversCommand.waitUntilLog('valid role') - await userLogin(server, { username: 'ellone', password: 'elonne password' }, 400) + await command.login({ user: { username: 'ellone', password: 'elonne password' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) await server.serversCommand.waitUntilLog('valid email') }) @@ -181,8 +180,9 @@ describe('Test id and pass auth plugins', function () { settings: { disableSpyro: true } }) - await userLogin(server, { username: 'spyro', password: 'spyro password' }, 400) - await userLogin(server, { username: 'spyro', password: 'fake' }, 400) + const command = server.loginCommand + await command.login({ user: { username: 'spyro', password: 'spyro password' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) + await command.login({ user: { username: 'spyro', password: 'fake' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) }) it('Should have disabled this auth', async function () { @@ -198,7 +198,10 @@ describe('Test id and pass auth plugins', function () { it('Should uninstall the plugin one and do not login existing Crash', async function () { await server.pluginsCommand.uninstall({ npmName: 'peertube-plugin-test-id-pass-auth-one' }) - await userLogin(server, { username: 'crash', password: 'crash password' }, 400) + await server.loginCommand.login({ + user: { username: 'crash', password: 'crash password' }, + expectedStatus: HttpStatusCode.BAD_REQUEST_400 + }) }) it('Should display the correct configuration', async function () { -- cgit v1.2.3