Add external login tests

This commit is contained in:
Chocobozzz
2020-04-29 09:04:42 +02:00
committed by Chocobozzz
parent 4a8d113b9b
commit 9107d791e2
19 changed files with 582 additions and 27 deletions

View File

@@ -64,6 +64,7 @@ describe('Test server plugins API validators', function () {
describe('With static plugin routes', function () {
it('Should fail with an unknown plugin name/plugin version', async function () {
const paths = [
'/plugins/' + pluginName + '/0.0.1/auth/fake-auth',
'/plugins/' + pluginName + '/0.0.1/static/images/chocobo.png',
'/plugins/' + pluginName + '/0.0.1/client-scripts/client/common-client-plugin.js',
'/themes/' + themeName + '/0.0.1/static/images/chocobo.png',
@@ -86,6 +87,7 @@ describe('Test server plugins API validators', function () {
it('Should fail with invalid versions', async function () {
const paths = [
'/plugins/' + pluginName + '/0.0.1.1/auth/fake-auth',
'/plugins/' + pluginName + '/0.0.1.1/static/images/chocobo.png',
'/plugins/' + pluginName + '/0.1/client-scripts/client/common-client-plugin.js',
'/themes/' + themeName + '/1/static/images/chocobo.png',
@@ -112,6 +114,12 @@ describe('Test server plugins API validators', function () {
}
})
it('Should fail with an unknown auth name', async function () {
const path = '/plugins/' + pluginName + '/' + npmVersion + '/auth/bad-auth'
await makeGetRequest({ url: server.url, path, statusCodeExpected: 404 })
})
it('Should fail with an unknown static file', async function () {
const paths = [
'/plugins/' + pluginName + '/' + npmVersion + '/static/fake/chocobo.png',
@@ -145,6 +153,9 @@ describe('Test server plugins API validators', function () {
for (const p of paths) {
await makeGetRequest({ url: server.url, path: p, statusCodeExpected: 200 })
}
const authPath = '/plugins/' + pluginName + '/' + npmVersion + '/auth/fake-auth'
await makeGetRequest({ url: server.url, path: authPath, statusCodeExpected: 302 })
})
})
@@ -462,6 +473,8 @@ describe('Test server plugins API validators', function () {
})
it('Should succeed with the correct parameters', async function () {
this.timeout(10000)
const it = [
{ suffix: 'install', status: 200 },
{ suffix: 'update', status: 200 },

View File

@@ -1,10 +1,18 @@
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
import 'mocha'
import { getMyUserInformation, installPlugin, setAccessTokensToServers, updatePluginSettings, userLogin, uploadVideo, uninstallPlugin } from '../../../shared/extra-utils'
import { cleanupTests, flushAndRunServer, ServerInfo } from '../../../shared/extra-utils/server/servers'
import { User } from '@shared/models/users/user.model'
import { expect } from 'chai'
import { User } from '@shared/models/users/user.model'
import {
getMyUserInformation,
installPlugin,
setAccessTokensToServers,
uninstallPlugin,
updatePluginSettings,
uploadVideo,
userLogin
} from '../../../shared/extra-utils'
import { cleanupTests, flushAndRunServer, ServerInfo } from '../../../shared/extra-utils/server/servers'
describe('Official plugin auth-ldap', function () {
let server: ServerInfo

View File

@@ -0,0 +1,67 @@
async function register ({
registerExternalAuth,
peertubeHelpers
}) {
{
const result = registerExternalAuth({
authName: 'external-auth-1',
authDisplayName: 'External Auth 1',
onLogout: user => peertubeHelpers.logger.info('On logout %s', user.username),
onAuthRequest: (req, res) => {
const username = req.query.username
result.userAuthenticated({
req,
res,
username,
email: username + '@example.com'
})
}
})
}
{
const result = registerExternalAuth({
authName: 'external-auth-2',
authDisplayName: 'External Auth 2',
onAuthRequest: (req, res) => {
result.userAuthenticated({
req,
res,
username: 'kefka',
email: 'kefka@example.com',
role: 0,
displayName: 'Kefka Palazzo'
})
},
hookTokenValidity: (options) => {
if (options.type === 'refresh') {
return { valid: false }
}
if (options.type === 'access') {
const token = options.token
const now = new Date()
now.setTime(now.getTime() - 5000)
const createdAt = new Date(token.createdAt)
return { valid: createdAt.getTime() >= now.getTime() }
}
return { valid: true }
}
})
}
}
async function unregister () {
return
}
module.exports = {
register,
unregister
}
// ###########################################################################

View File

@@ -0,0 +1,20 @@
{
"name": "peertube-plugin-test-external-auth-one",
"version": "0.0.1",
"description": "External auth one",
"engine": {
"peertube": ">=1.3.0"
},
"keywords": [
"peertube",
"plugin"
],
"homepage": "https://github.com/Chocobozzz/PeerTube",
"author": "Chocobozzz",
"bugs": "https://github.com/Chocobozzz/PeerTube/issues",
"library": "./main.js",
"staticDirs": {},
"css": [],
"clientScripts": [],
"translations": {}
}

View File

@@ -0,0 +1,31 @@
async function register ({
registerExternalAuth,
peertubeHelpers
}) {
{
const result = registerExternalAuth({
authName: 'external-auth-3',
authDisplayName: 'External Auth 3',
onAuthRequest: (req, res) => {
result.userAuthenticated({
req,
res,
username: 'cid',
email: 'cid@example.com',
displayName: 'Cid Marquez'
})
}
})
}
}
async function unregister () {
return
}
module.exports = {
register,
unregister
}
// ###########################################################################

View File

@@ -0,0 +1,20 @@
{
"name": "peertube-plugin-test-external-auth-two",
"version": "0.0.1",
"description": "External auth two",
"engine": {
"peertube": ">=1.3.0"
},
"keywords": [
"peertube",
"plugin"
],
"homepage": "https://github.com/Chocobozzz/PeerTube",
"author": "Chocobozzz",
"bugs": "https://github.com/Chocobozzz/PeerTube/issues",
"library": "./main.js",
"staticDirs": {},
"css": [],
"clientScripts": [],
"translations": {}
}

View File

@@ -0,0 +1,295 @@
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
import 'mocha'
import { expect } from 'chai'
import { ServerConfig, User, UserRole } from '@shared/models'
import {
decodeQueryString,
getConfig,
getExternalAuth,
getMyUserInformation,
getPluginTestPath,
installPlugin,
loginUsingExternalToken,
logout,
refreshToken,
setAccessTokensToServers,
uninstallPlugin,
updateMyUser,
wait
} from '../../../shared/extra-utils'
import { cleanupTests, flushAndRunServer, ServerInfo, waitUntilLog } from '../../../shared/extra-utils/server/servers'
async function loginExternal (options: {
server: ServerInfo
npmName: string
authName: string
username: string
query?: any
statusCodeExpected?: number
}) {
const res = await getExternalAuth({
url: options.server.url,
npmName: options.npmName,
npmVersion: '0.0.1',
authName: options.authName,
query: options.query,
statusCodeExpected: options.statusCodeExpected || 302
})
if (res.status !== 302) return
const location = res.header.location
const { externalAuthToken } = decodeQueryString(location)
const resLogin = await loginUsingExternalToken(
options.server,
options.username,
externalAuthToken as string
)
return resLogin.body
}
describe('Test external auth plugins', function () {
let server: ServerInfo
let cyanAccessToken: string
let cyanRefreshToken: string
let kefkaAccessToken: string
let kefkaRefreshToken: string
let externalAuthToken: string
before(async function () {
this.timeout(30000)
server = await flushAndRunServer(1)
await setAccessTokensToServers([ server ])
for (const suffix of [ 'one', 'two' ]) {
await installPlugin({
url: server.url,
accessToken: server.accessToken,
path: getPluginTestPath('-external-auth-' + suffix)
})
}
})
it('Should display the correct configuration', async function () {
const res = await getConfig(server.url)
const config: ServerConfig = res.body
const auths = config.plugin.registeredExternalAuths
expect(auths).to.have.lengthOf(3)
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')
})
it('Should redirect for a Cyan login', async function () {
const res = await getExternalAuth({
url: server.url,
npmName: 'test-external-auth-one',
npmVersion: '0.0.1',
authName: 'external-auth-1',
query: {
username: 'cyan'
},
statusCodeExpected: 302
})
const location = res.header.location
expect(location.startsWith('/login?')).to.be.true
const searchParams = decodeQueryString(location)
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 () {
await loginUsingExternalToken(server, 'cyan', '', 400)
await loginUsingExternalToken(server, 'cyan', 'blabla', 400)
})
it('Should reject auto external login with a missing or invalid username', async function () {
await loginUsingExternalToken(server, '', externalAuthToken, 400)
await loginUsingExternalToken(server, '', externalAuthToken, 400)
})
it('Should reject auto external login with an expired token', async function () {
this.timeout(15000)
await wait(5000)
await loginUsingExternalToken(server, 'cyan', externalAuthToken, 400)
await waitUntilLog(server, 'expired external auth token')
})
it('Should auto login Cyan, create the user and use the token', async function () {
{
const res = await loginExternal({
server,
npmName: 'test-external-auth-one',
authName: 'external-auth-1',
query: {
username: 'cyan'
},
username: 'cyan'
})
cyanAccessToken = res.access_token
cyanRefreshToken = res.refresh_token
}
{
const res = await getMyUserInformation(server.url, cyanAccessToken)
const body: User = res.body
expect(body.username).to.equal('cyan')
expect(body.account.displayName).to.equal('cyan')
expect(body.email).to.equal('cyan@example.com')
expect(body.role).to.equal(UserRole.USER)
}
})
it('Should auto login Kefka, create the user and use the token', async function () {
{
const res = await loginExternal({
server,
npmName: 'test-external-auth-one',
authName: 'external-auth-2',
username: 'kefka'
})
kefkaAccessToken = res.access_token
kefkaRefreshToken = res.refresh_token
}
{
const res = await getMyUserInformation(server.url, kefkaAccessToken)
const body: User = res.body
expect(body.username).to.equal('kefka')
expect(body.account.displayName).to.equal('Kefka Palazzo')
expect(body.email).to.equal('kefka@example.com')
expect(body.role).to.equal(UserRole.ADMINISTRATOR)
}
})
it('Should refresh Cyan token, but not Kefka token', async function () {
{
const resRefresh = await refreshToken(server, cyanRefreshToken)
cyanAccessToken = resRefresh.body.access_token
cyanRefreshToken = resRefresh.body.refresh_token
const res = await getMyUserInformation(server.url, cyanAccessToken)
const user: User = res.body
expect(user.username).to.equal('cyan')
}
{
await refreshToken(server, kefkaRefreshToken, 400)
}
})
it('Should update Cyan profile', async function () {
await updateMyUser({
url: server.url,
accessToken: cyanAccessToken,
displayName: 'Cyan Garamonde',
description: 'Retainer to the king of Doma'
})
const res = await getMyUserInformation(server.url, cyanAccessToken)
const body: User = res.body
expect(body.account.displayName).to.equal('Cyan Garamonde')
expect(body.account.description).to.equal('Retainer to the king of Doma')
})
it('Should logout Cyan', async function () {
await logout(server.url, cyanAccessToken)
})
it('Should have logged out Cyan', async function () {
await waitUntilLog(server, 'On logout cyan')
await getMyUserInformation(server.url, cyanAccessToken, 401)
})
it('Should login Cyan and keep the old existing profile', async function () {
{
const res = await loginExternal({
server,
npmName: 'test-external-auth-one',
authName: 'external-auth-1',
query: {
username: 'cyan'
},
username: 'cyan'
})
cyanAccessToken = res.access_token
}
const res = await getMyUserInformation(server.url, cyanAccessToken)
const body: User = res.body
expect(body.username).to.equal('cyan')
expect(body.account.displayName).to.equal('Cyan Garamonde')
expect(body.account.description).to.equal('Retainer to the king of Doma')
expect(body.role).to.equal(UserRole.USER)
})
it('Should reject token of Kefka by the plugin hook', async function () {
this.timeout(10000)
await wait(5000)
await getMyUserInformation(server.url, kefkaAccessToken, 401)
})
it('Should uninstall the plugin one and do not login Cyan', async function () {
await uninstallPlugin({
url: server.url,
accessToken: server.accessToken,
npmName: 'peertube-plugin-test-external-auth-one'
})
await loginExternal({
server,
npmName: 'test-external-auth-one',
authName: 'external-auth-1',
query: {
username: 'cyan'
},
username: 'cyan',
statusCodeExpected: 404
})
})
it('Should display the correct configuration', async function () {
const res = await getConfig(server.url)
const config: ServerConfig = res.body
const auths = config.plugin.registeredExternalAuths
expect(auths).to.have.lengthOf(1)
const auth2 = auths.find((a) => a.authName === 'external-auth-2')
expect(auth2).to.not.exist
})
after(async function () {
await cleanupTests([ server ])
})
})

View File

@@ -12,9 +12,9 @@ import {
updateMyUser,
userLogin,
wait,
login, refreshToken
login, refreshToken, getConfig
} from '../../../shared/extra-utils'
import { User, UserRole } from '@shared/models'
import { User, UserRole, ServerConfig } from '@shared/models'
import { expect } from 'chai'
describe('Test id and pass auth plugins', function () {
@@ -41,6 +41,20 @@ describe('Test id and pass auth plugins', function () {
}
})
it('Should display the correct configuration', async function () {
const res = await getConfig(server.url)
const config: ServerConfig = res.body
const auths = config.plugin.registeredIdAndPassAuths
expect(auths).to.have.lengthOf(8)
const crashAuth = auths.find(a => a.authName === 'crash-auth')
expect(crashAuth).to.exist
expect(crashAuth.npmName).to.equal('peertube-plugin-test-id-pass-auth-one')
expect(crashAuth.weight).to.equal(50)
})
it('Should not login', async function () {
await userLogin(server, { username: 'toto', password: 'password' }, 400)
})
@@ -175,6 +189,18 @@ describe('Test id and pass auth plugins', function () {
await userLogin(server, { username: 'crash', password: 'crash password' }, 400)
})
it('Should display the correct configuration', async function () {
const res = await getConfig(server.url)
const config: ServerConfig = res.body
const auths = config.plugin.registeredIdAndPassAuths
expect(auths).to.have.lengthOf(6)
const crashAuth = auths.find(a => a.authName === 'crash-auth')
expect(crashAuth).to.not.exist
})
after(async function () {
await cleanupTests([ server ])
})

View File

@@ -1,5 +1,6 @@
import './action-hooks'
import './id-and-pass-auth'
import './external-auth'
import './filter-hooks'
import './translations'
import './video-constants'