PeerTube/server/tests/plugins/id-and-pass-auth.ts

215 lines
7.9 KiB
TypeScript
Raw Normal View History

2020-04-22 16:07:04 +02:00
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2021-07-07 10:33:49 +02:00
import { expect } from 'chai'
import { wait } from '@shared/core-utils'
2021-07-16 14:27:30 +02:00
import { HttpStatusCode, UserRole } from '@shared/models'
import { cleanupTests, createSingleServer, PeerTubeServer, PluginsCommand, setAccessTokensToServers } from '@shared/server-commands'
2020-04-22 16:07:04 +02:00
describe('Test id and pass auth plugins', function () {
2021-07-16 09:47:51 +02:00
let server: PeerTubeServer
let crashAccessToken: string
let crashRefreshToken: string
let lagunaAccessToken: string
let lagunaRefreshToken: string
2020-04-22 16:07:04 +02:00
before(async function () {
this.timeout(30000)
2021-07-16 09:47:51 +02:00
server = await createSingleServer(1)
2020-04-22 16:07:04 +02:00
await setAccessTokensToServers([ server ])
for (const suffix of [ 'one', 'two', 'three' ]) {
2021-07-16 09:04:35 +02:00
await server.plugins.install({ path: PluginsCommand.getPluginTestPath('-id-pass-auth-' + suffix) })
}
2020-04-22 16:07:04 +02:00
})
2020-04-29 09:04:42 +02:00
it('Should display the correct configuration', async function () {
2021-07-16 09:04:35 +02:00
const config = await server.config.getConfig()
2020-04-29 09:04:42 +02:00
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 () {
2021-07-16 09:04:35 +02:00
await server.login.login({ user: { username: 'toto', password: 'password' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
2020-04-22 16:07:04 +02:00
})
it('Should login Spyro, create the user and use the token', async function () {
2021-07-16 09:04:35 +02:00
const accessToken = await server.login.getAccessToken({ username: 'spyro', password: 'spyro password' })
2020-04-22 16:07:04 +02:00
2021-07-16 09:04:35 +02:00
const body = await server.users.getMyInfo({ token: accessToken })
expect(body.username).to.equal('spyro')
expect(body.account.displayName).to.equal('Spyro the Dragon')
expect(body.role).to.equal(UserRole.USER)
2020-04-22 16:07:04 +02:00
})
it('Should login Crash, create the user and use the token', async function () {
{
2021-07-16 09:04:35 +02:00
const body = await server.login.login({ user: { username: 'crash', password: 'crash password' } })
2021-07-13 11:05:15 +02:00
crashAccessToken = body.access_token
crashRefreshToken = body.refresh_token
}
{
2021-07-16 09:04:35 +02:00
const body = await server.users.getMyInfo({ token: crashAccessToken })
2020-04-22 16:07:04 +02:00
expect(body.username).to.equal('crash')
expect(body.account.displayName).to.equal('Crash Bandicoot')
expect(body.role).to.equal(UserRole.MODERATOR)
}
2020-04-22 16:07:04 +02:00
})
it('Should login the first Laguna, create the user and use the token', async function () {
{
2021-07-16 09:04:35 +02:00
const body = await server.login.login({ user: { username: 'laguna', password: 'laguna password' } })
2021-07-13 11:05:15 +02:00
lagunaAccessToken = body.access_token
lagunaRefreshToken = body.refresh_token
}
2020-04-22 16:07:04 +02:00
{
2021-07-16 09:04:35 +02:00
const body = await server.users.getMyInfo({ token: lagunaAccessToken })
expect(body.username).to.equal('laguna')
expect(body.account.displayName).to.equal('laguna')
expect(body.role).to.equal(UserRole.USER)
}
})
it('Should refresh crash token, but not laguna token', async function () {
{
2021-07-16 09:04:35 +02:00
const resRefresh = await server.login.refreshToken({ refreshToken: crashRefreshToken })
crashAccessToken = resRefresh.body.access_token
crashRefreshToken = resRefresh.body.refresh_token
2021-07-16 09:04:35 +02:00
const body = await server.users.getMyInfo({ token: crashAccessToken })
2021-07-13 14:23:01 +02:00
expect(body.username).to.equal('crash')
}
{
2021-07-16 09:04:35 +02:00
await server.login.refreshToken({ refreshToken: lagunaRefreshToken, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
}
2020-04-22 16:07:04 +02:00
})
it('Should update Crash profile', async function () {
2021-07-16 09:04:35 +02:00
await server.users.updateMe({
2021-07-13 14:23:01 +02:00
token: crashAccessToken,
displayName: 'Beautiful Crash',
description: 'Mutant eastern barred bandicoot'
})
2020-04-22 16:07:04 +02:00
2021-07-16 09:04:35 +02:00
const body = await server.users.getMyInfo({ token: crashAccessToken })
expect(body.account.displayName).to.equal('Beautiful Crash')
expect(body.account.description).to.equal('Mutant eastern barred bandicoot')
2020-04-22 16:07:04 +02:00
})
it('Should logout Crash', async function () {
2021-07-16 09:04:35 +02:00
await server.login.logout({ token: crashAccessToken })
2020-04-22 16:07:04 +02:00
})
it('Should have logged out Crash', async function () {
2021-07-16 09:04:35 +02:00
await server.servers.waitUntilLog('On logout for auth 1 - 2')
2021-07-16 09:04:35 +02:00
await server.users.getMyInfo({ token: crashAccessToken, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
2020-04-22 16:07:04 +02:00
})
it('Should login Crash and keep the old existing profile', async function () {
2021-07-16 09:04:35 +02:00
crashAccessToken = await server.login.getAccessToken({ username: 'crash', password: 'crash password' })
2020-04-22 16:07:04 +02:00
2021-07-16 09:04:35 +02:00
const body = await server.users.getMyInfo({ token: crashAccessToken })
expect(body.username).to.equal('crash')
expect(body.account.displayName).to.equal('Beautiful Crash')
expect(body.account.description).to.equal('Mutant eastern barred bandicoot')
expect(body.role).to.equal(UserRole.MODERATOR)
2020-04-22 16:07:04 +02:00
})
2020-04-27 10:19:14 +02:00
it('Should reject token of laguna by the plugin hook', async function () {
this.timeout(10000)
await wait(5000)
2021-07-16 09:04:35 +02:00
await server.users.getMyInfo({ token: lagunaAccessToken, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
})
2020-04-27 11:42:01 +02:00
it('Should reject an invalid username, email, role or display name', async function () {
2021-07-16 09:04:35 +02:00
const command = server.login
2021-07-13 11:05:15 +02:00
await command.login({ user: { username: 'ward', password: 'ward password' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
2021-07-16 09:04:35 +02:00
await server.servers.waitUntilLog('valid username')
2020-04-27 11:42:01 +02:00
2021-07-13 11:05:15 +02:00
await command.login({ user: { username: 'kiros', password: 'kiros password' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
2021-07-16 09:04:35 +02:00
await server.servers.waitUntilLog('valid display name')
2020-04-27 11:42:01 +02:00
2021-07-13 11:05:15 +02:00
await command.login({ user: { username: 'raine', password: 'raine password' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
2021-07-16 09:04:35 +02:00
await server.servers.waitUntilLog('valid role')
2020-04-27 11:42:01 +02:00
2021-07-13 11:05:15 +02:00
await command.login({ user: { username: 'ellone', password: 'elonne password' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
2021-07-16 09:04:35 +02:00
await server.servers.waitUntilLog('valid email')
2020-04-27 11:42:01 +02:00
})
2020-04-30 10:03:09 +02:00
it('Should unregister spyro-auth and do not login existing Spyro', async function () {
2021-07-16 09:04:35 +02:00
await server.plugins.updateSettings({
2020-04-30 10:03:09 +02:00
npmName: 'peertube-plugin-test-id-pass-auth-one',
settings: { disableSpyro: true }
})
2021-07-16 09:04:35 +02:00
const command = server.login
2021-07-13 11:05:15 +02:00
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 })
2020-04-30 10:03:09 +02:00
})
it('Should have disabled this auth', async function () {
2021-07-16 09:04:35 +02:00
const config = await server.config.getConfig()
2020-04-30 10:03:09 +02:00
const auths = config.plugin.registeredIdAndPassAuths
expect(auths).to.have.lengthOf(7)
const spyroAuth = auths.find(a => a.authName === 'spyro-auth')
expect(spyroAuth).to.not.exist
})
2020-04-22 16:07:04 +02:00
it('Should uninstall the plugin one and do not login existing Crash', async function () {
2021-07-16 09:04:35 +02:00
await server.plugins.uninstall({ npmName: 'peertube-plugin-test-id-pass-auth-one' })
2020-04-22 16:07:04 +02:00
2021-07-16 09:04:35 +02:00
await server.login.login({
2021-07-13 11:05:15 +02:00
user: { username: 'crash', password: 'crash password' },
expectedStatus: HttpStatusCode.BAD_REQUEST_400
})
2020-04-22 16:07:04 +02:00
})
2020-04-29 09:04:42 +02:00
it('Should display the correct configuration', async function () {
2021-07-16 09:04:35 +02:00
const config = await server.config.getConfig()
2020-04-29 09:04:42 +02:00
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
})
2020-05-05 09:44:53 +02:00
it('Should display plugin auth information in users list', async function () {
2021-07-16 09:04:35 +02:00
const { data } = await server.users.list()
2020-05-05 09:44:53 +02:00
2021-07-13 14:23:01 +02:00
const root = data.find(u => u.username === 'root')
const crash = data.find(u => u.username === 'crash')
const laguna = data.find(u => u.username === 'laguna')
2020-05-05 09:44:53 +02:00
expect(root.pluginAuth).to.be.null
expect(crash.pluginAuth).to.equal('peertube-plugin-test-id-pass-auth-one')
expect(laguna.pluginAuth).to.equal('peertube-plugin-test-id-pass-auth-two')
})
2020-04-22 16:07:04 +02:00
after(async function () {
await cleanupTests([ server ])
})
})