Skip to content

ActivityPub-federated video streaming platform using P2P directly in your web browser

Fix external auths tests

Chocobozzza2a60b164a0f
diff --git a/packages/tests/src/plugins/external-auth.ts b/packages/tests/src/plugins/external-auth.ts
index 8bb624ebbe..a229328d0b 100644
--- a/packages/tests/src/plugins/external-auth.ts
+++ b/packages/tests/src/plugins/external-auth.ts
@@ -1,6 +1,5 @@
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
-import { expect } from 'chai'
import { wait } from '@peertube/peertube-core-utils'
import { HttpStatusCode, HttpStatusCodeType, UserAdminFlag, UserRole } from '@peertube/peertube-models'
import {
@@ -11,6 +10,7 @@ import {
PluginsCommand,
setAccessTokensToServers
} from '@peertube/peertube-server-commands'
+import { expect } from 'chai'
async function loginExternal (options: {
server: PeerTubeServer
@@ -20,6 +20,25 @@ async function loginExternal (options: {
query?: any
expectedStatus?: HttpStatusCodeType
expectedStatusStep2?: HttpStatusCodeType
+}) {
+ const externalAuthToken = await fetchExternalToken(options)
+ if (!externalAuthToken) return
+
+ const resLogin = await options.server.login.loginUsingExternalToken({
+ username: options.username,
+ externalAuthToken,
+ expectedStatus: options.expectedStatusStep2
+ })
+
+ return resLogin.body
+}
+
+async function fetchExternalToken (options: {
+ server: PeerTubeServer
+ npmName: string
+ authName: string
+ query?: any
+ expectedStatus?: HttpStatusCodeType
}) {
const res = await options.server.plugins.getExternalAuth({
npmName: options.npmName,
@@ -29,18 +48,12 @@ async function loginExternal (options: {
expectedStatus: options.expectedStatus || HttpStatusCode.FOUND_302
})
- if (res.status !== HttpStatusCode.FOUND_302) return
+ if (res.status !== HttpStatusCode.FOUND_302) return undefined
const location = res.header.location
const { externalAuthToken } = decodeQueryString(location)
- const resLogin = await options.server.login.loginUsingExternalToken({
- username: options.username,
- externalAuthToken: externalAuthToken as string,
- expectedStatus: options.expectedStatusStep2
- })
-
- return resLogin.body
+ return externalAuthToken as string
}
describe('Test external auth plugins', function () {
@@ -53,7 +66,17 @@ describe('Test external auth plugins', function () {
let kefkaRefreshToken: string
let kefkaId: number
- let externalAuthToken: string
+ async function fetchCyanExternalToken () {
+ return fetchExternalToken({
+ server,
+ npmName: 'test-external-auth-one',
+ authName: 'external-auth-1',
+ query: {
+ username: 'cyan'
+ },
+ expectedStatus: HttpStatusCode.FOUND_302
+ })
+ }
before(async function () {
this.timeout(30000)
@@ -79,7 +102,7 @@ describe('Test external auth plugins', function () {
const auths = config.plugin.registeredExternalAuths
expect(auths).to.have.lengthOf(10)
- const auth2 = auths.find((a) => a.authName === 'external-auth-2')
+ const auth2 = auths.find(a => a.authName === 'external-auth-2')
expect(auth2).to.exist
expect(auth2.authDisplayName).to.equal('External Auth 2')
expect(auth2.npmName).to.equal('peertube-plugin-test-external-auth-one')
@@ -103,8 +126,6 @@ describe('Test external auth plugins', function () {
expect(searchParams.externalAuthToken).to.exist
expect(searchParams.username).to.equal('cyan')
-
- externalAuthToken = searchParams.externalAuthToken as string
})
it('Should reject auto external login with a missing or invalid token', async function () {
@@ -117,13 +138,22 @@ describe('Test external auth plugins', function () {
it('Should reject auto external login with a missing or invalid username', async function () {
const command = server.login
- await command.loginUsingExternalToken({ username: '', externalAuthToken, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
- await command.loginUsingExternalToken({ username: '', externalAuthToken, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
+ {
+ const externalAuthToken = await fetchCyanExternalToken()
+ await command.loginUsingExternalToken({ username: '', externalAuthToken, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
+ }
+
+ {
+ const externalAuthToken = await fetchCyanExternalToken()
+ await command.loginUsingExternalToken({ username: '', externalAuthToken, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
+ }
})
it('Should reject auto external login with an expired token', async function () {
this.timeout(30000)
+ const externalAuthToken = await fetchCyanExternalToken()
+
await wait(5000)
await server.login.loginUsingExternalToken({
@@ -402,7 +432,7 @@ describe('Test external auth plugins', function () {
const auths = config.plugin.registeredExternalAuths
expect(auths).to.have.lengthOf(8)
- const auth2 = auths.find((a) => a.authName === 'external-auth-2')
+ const auth2 = auths.find(a => a.authName === 'external-auth-2')
expect(auth2).to.not.exist
})
@@ -435,7 +465,6 @@ describe('Test external auth plugins', function () {
})
it('Should redirect to an external site after login if externalRedirectUri is set', async function () {
-
const res = await server.plugins.getExternalAuth({
npmName: 'test-external-auth-three',
npmVersion: '0.0.1',
@@ -448,13 +477,13 @@ describe('Test external auth plugins', function () {
const location = res.header.location
expect(location.startsWith('https://external.com/some/redirect/path?')).to.be.true
-
+
const searchParams = decodeQueryString(location)
-
+
expect(searchParams.externalAuthToken).to.exist
expect(searchParams.username).to.equal('cid')
- externalAuthToken = searchParams.externalAuthToken as string
+ const externalAuthToken = searchParams.externalAuthToken as string
await server.login.loginUsingExternalToken({ username: 'cid', externalAuthToken, expectedStatus: HttpStatusCode.OK_200 })
})