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'
|
2021-12-17 11:58:15 +01:00
|
|
|
import { wait } from '@shared/core-utils'
|
2021-07-16 14:27:30 +02:00
|
|
|
import { HttpStatusCode, UserRole } from '@shared/models'
|
2021-12-17 11:58:15 +01:00
|
|
|
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
|
2020-04-24 11:33:01 +02:00
|
|
|
|
|
|
|
let crashAccessToken: string
|
|
|
|
let crashRefreshToken: string
|
|
|
|
|
|
|
|
let lagunaAccessToken: string
|
|
|
|
let lagunaRefreshToken: string
|
2022-12-30 10:12:20 +01:00
|
|
|
let lagunaId: number
|
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 ])
|
|
|
|
|
2020-04-23 11:36:50 +02:00
|
|
|
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-23 11:36:50 +02:00
|
|
|
}
|
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)
|
|
|
|
})
|
|
|
|
|
2020-04-23 11:36:50 +02:00
|
|
|
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
|
|
|
})
|
|
|
|
|
2020-04-23 11:36:50 +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 })
|
2020-04-23 11:36:50 +02:00
|
|
|
|
|
|
|
expect(body.username).to.equal('spyro')
|
|
|
|
expect(body.account.displayName).to.equal('Spyro the Dragon')
|
2022-10-28 16:15:04 +02:00
|
|
|
expect(body.role.id).to.equal(UserRole.USER)
|
2020-04-22 16:07:04 +02:00
|
|
|
})
|
|
|
|
|
2020-04-23 11:36:50 +02:00
|
|
|
it('Should login Crash, create the user and use the token', async function () {
|
2020-04-24 11:33:01 +02:00
|
|
|
{
|
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
|
2020-04-24 11:33:01 +02:00
|
|
|
}
|
2020-04-23 11:36:50 +02:00
|
|
|
|
2020-04-24 11:33:01 +02:00
|
|
|
{
|
2021-07-16 09:04:35 +02:00
|
|
|
const body = await server.users.getMyInfo({ token: crashAccessToken })
|
2020-04-22 16:07:04 +02:00
|
|
|
|
2020-04-24 11:33:01 +02:00
|
|
|
expect(body.username).to.equal('crash')
|
|
|
|
expect(body.account.displayName).to.equal('Crash Bandicoot')
|
2022-10-28 16:15:04 +02:00
|
|
|
expect(body.role.id).to.equal(UserRole.MODERATOR)
|
2020-04-24 11:33:01 +02:00
|
|
|
}
|
2020-04-22 16:07:04 +02:00
|
|
|
})
|
|
|
|
|
2020-04-23 11:36:50 +02:00
|
|
|
it('Should login the first Laguna, create the user and use the token', async function () {
|
2020-04-24 11:33:01 +02:00
|
|
|
{
|
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-24 11:33:01 +02:00
|
|
|
}
|
2020-04-22 16:07:04 +02:00
|
|
|
|
2020-04-24 11:33:01 +02:00
|
|
|
{
|
2021-07-16 09:04:35 +02:00
|
|
|
const body = await server.users.getMyInfo({ token: lagunaAccessToken })
|
2020-04-23 11:36:50 +02:00
|
|
|
|
2020-04-24 11:33:01 +02:00
|
|
|
expect(body.username).to.equal('laguna')
|
2022-12-30 10:12:20 +01:00
|
|
|
expect(body.account.displayName).to.equal('Laguna Loire')
|
2022-10-28 16:15:04 +02:00
|
|
|
expect(body.role.id).to.equal(UserRole.USER)
|
2022-12-30 10:12:20 +01:00
|
|
|
|
|
|
|
lagunaId = body.id
|
2020-04-24 11:33:01 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
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 })
|
2020-04-24 11:33:01 +02:00
|
|
|
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')
|
2020-04-24 11:33:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-07-16 09:04:35 +02:00
|
|
|
await server.login.refreshToken({ refreshToken: lagunaRefreshToken, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
|
2020-04-24 11:33:01 +02:00
|
|
|
}
|
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,
|
2020-04-23 11:36:50 +02:00
|
|
|
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 })
|
2020-04-23 11:36:50 +02:00
|
|
|
|
|
|
|
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
|
|
|
})
|
|
|
|
|
2020-04-23 11:36:50 +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')
|
2020-04-24 11:33:01 +02:00
|
|
|
|
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 })
|
2020-04-23 11:36:50 +02:00
|
|
|
|
|
|
|
expect(body.username).to.equal('crash')
|
|
|
|
expect(body.account.displayName).to.equal('Beautiful Crash')
|
|
|
|
expect(body.account.description).to.equal('Mutant eastern barred bandicoot')
|
2022-10-28 16:15:04 +02:00
|
|
|
expect(body.role.id).to.equal(UserRole.MODERATOR)
|
2020-04-22 16:07:04 +02:00
|
|
|
})
|
|
|
|
|
2022-12-30 10:12:20 +01:00
|
|
|
it('Should login Laguna and update the profile', async function () {
|
|
|
|
{
|
|
|
|
await server.users.update({ userId: lagunaId, videoQuota: 43000, videoQuotaDaily: 43100 })
|
|
|
|
await server.users.updateMe({ token: lagunaAccessToken, displayName: 'laguna updated' })
|
|
|
|
|
|
|
|
const body = await server.users.getMyInfo({ token: lagunaAccessToken })
|
|
|
|
expect(body.username).to.equal('laguna')
|
|
|
|
expect(body.account.displayName).to.equal('laguna updated')
|
|
|
|
expect(body.videoQuota).to.equal(43000)
|
|
|
|
expect(body.videoQuotaDaily).to.equal(43100)
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
const body = await server.login.login({ user: { username: 'laguna', password: 'laguna password' } })
|
|
|
|
lagunaAccessToken = body.access_token
|
|
|
|
lagunaRefreshToken = body.refresh_token
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
const body = await server.users.getMyInfo({ token: lagunaAccessToken })
|
|
|
|
expect(body.username).to.equal('laguna')
|
|
|
|
expect(body.account.displayName).to.equal('Laguna Loire')
|
|
|
|
expect(body.videoQuota).to.equal(42000)
|
|
|
|
expect(body.videoQuotaDaily).to.equal(43100)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-04-27 10:19:14 +02:00
|
|
|
it('Should reject token of laguna by the plugin hook', async function () {
|
2020-04-24 11:33:01 +02:00
|
|
|
await wait(5000)
|
|
|
|
|
2021-07-16 09:04:35 +02:00
|
|
|
await server.users.getMyInfo({ token: lagunaAccessToken, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
|
2020-04-24 11:33:01 +02:00
|
|
|
})
|
|
|
|
|
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 })
|
2022-12-30 10:12:20 +01:00
|
|
|
await server.servers.waitUntilLog('valid displayName')
|
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 ])
|
|
|
|
})
|
|
|
|
})
|