1
0
Fork 0
peertube/server/tests/plugins/id-and-pass-auth.ts

215 lines
7.9 KiB
TypeScript
Raw Normal View History

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