2020-04-22 09:07:04 -05:00
|
|
|
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
|
|
|
|
|
|
|
|
import 'mocha'
|
2020-04-23 04:36:50 -05:00
|
|
|
import { cleanupTests, flushAndRunServer, ServerInfo, waitUntilLog } from '../../../shared/extra-utils/server/servers'
|
|
|
|
import {
|
|
|
|
getMyUserInformation,
|
|
|
|
getPluginTestPath,
|
|
|
|
installPlugin,
|
|
|
|
logout,
|
|
|
|
setAccessTokensToServers,
|
|
|
|
uninstallPlugin,
|
|
|
|
updateMyUser,
|
2020-04-24 04:33:01 -05:00
|
|
|
userLogin,
|
|
|
|
wait,
|
2020-04-29 02:04:42 -05:00
|
|
|
login, refreshToken, getConfig
|
2020-04-23 04:36:50 -05:00
|
|
|
} from '../../../shared/extra-utils'
|
2020-04-29 02:04:42 -05:00
|
|
|
import { User, UserRole, ServerConfig } from '@shared/models'
|
2020-04-23 04:36:50 -05:00
|
|
|
import { expect } from 'chai'
|
2020-04-22 09:07:04 -05:00
|
|
|
|
|
|
|
describe('Test id and pass auth plugins', function () {
|
|
|
|
let server: ServerInfo
|
2020-04-24 04:33:01 -05:00
|
|
|
|
|
|
|
let crashAccessToken: string
|
|
|
|
let crashRefreshToken: string
|
|
|
|
|
|
|
|
let lagunaAccessToken: string
|
|
|
|
let lagunaRefreshToken: string
|
2020-04-22 09:07:04 -05:00
|
|
|
|
|
|
|
before(async function () {
|
|
|
|
this.timeout(30000)
|
|
|
|
|
|
|
|
server = await flushAndRunServer(1)
|
|
|
|
await setAccessTokensToServers([ server ])
|
|
|
|
|
2020-04-23 04:36:50 -05:00
|
|
|
for (const suffix of [ 'one', 'two', 'three' ]) {
|
|
|
|
await installPlugin({
|
|
|
|
url: server.url,
|
|
|
|
accessToken: server.accessToken,
|
|
|
|
path: getPluginTestPath('-id-pass-auth-' + suffix)
|
|
|
|
})
|
|
|
|
}
|
2020-04-22 09:07:04 -05:00
|
|
|
})
|
|
|
|
|
2020-04-29 02:04:42 -05:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
|
2020-04-23 04:36:50 -05:00
|
|
|
it('Should not login', async function () {
|
|
|
|
await userLogin(server, { username: 'toto', password: 'password' }, 400)
|
2020-04-22 09:07:04 -05:00
|
|
|
})
|
|
|
|
|
2020-04-23 04:36:50 -05:00
|
|
|
it('Should login Spyro, create the user and use the token', async function () {
|
|
|
|
const accessToken = await userLogin(server, { username: 'spyro', password: 'spyro password' })
|
2020-04-22 09:07:04 -05:00
|
|
|
|
2020-04-23 04:36:50 -05:00
|
|
|
const res = await getMyUserInformation(server.url, accessToken)
|
|
|
|
|
|
|
|
const body: User = res.body
|
|
|
|
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 09:07:04 -05:00
|
|
|
})
|
|
|
|
|
2020-04-23 04:36:50 -05:00
|
|
|
it('Should login Crash, create the user and use the token', async function () {
|
2020-04-24 04:33:01 -05:00
|
|
|
{
|
|
|
|
const res = await login(server.url, server.client, { username: 'crash', password: 'crash password' })
|
|
|
|
crashAccessToken = res.body.access_token
|
|
|
|
crashRefreshToken = res.body.refresh_token
|
|
|
|
}
|
2020-04-23 04:36:50 -05:00
|
|
|
|
2020-04-24 04:33:01 -05:00
|
|
|
{
|
|
|
|
const res = await getMyUserInformation(server.url, crashAccessToken)
|
2020-04-22 09:07:04 -05:00
|
|
|
|
2020-04-24 04:33:01 -05:00
|
|
|
const body: User = res.body
|
|
|
|
expect(body.username).to.equal('crash')
|
|
|
|
expect(body.account.displayName).to.equal('Crash Bandicoot')
|
|
|
|
expect(body.role).to.equal(UserRole.MODERATOR)
|
|
|
|
}
|
2020-04-22 09:07:04 -05:00
|
|
|
})
|
|
|
|
|
2020-04-23 04:36:50 -05:00
|
|
|
it('Should login the first Laguna, create the user and use the token', async function () {
|
2020-04-24 04:33:01 -05:00
|
|
|
{
|
|
|
|
const res = await login(server.url, server.client, { username: 'laguna', password: 'laguna password' })
|
|
|
|
lagunaAccessToken = res.body.access_token
|
|
|
|
lagunaRefreshToken = res.body.refresh_token
|
|
|
|
}
|
2020-04-22 09:07:04 -05:00
|
|
|
|
2020-04-24 04:33:01 -05:00
|
|
|
{
|
|
|
|
const res = await getMyUserInformation(server.url, lagunaAccessToken)
|
2020-04-23 04:36:50 -05:00
|
|
|
|
2020-04-24 04:33:01 -05:00
|
|
|
const body: User = res.body
|
|
|
|
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 () {
|
|
|
|
{
|
|
|
|
const resRefresh = await refreshToken(server, crashRefreshToken)
|
|
|
|
crashAccessToken = resRefresh.body.access_token
|
|
|
|
crashRefreshToken = resRefresh.body.refresh_token
|
|
|
|
|
|
|
|
const res = await getMyUserInformation(server.url, crashAccessToken)
|
|
|
|
const user: User = res.body
|
|
|
|
expect(user.username).to.equal('crash')
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
await refreshToken(server, lagunaRefreshToken, 400)
|
|
|
|
}
|
2020-04-22 09:07:04 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
it('Should update Crash profile', async function () {
|
2020-04-23 04:36:50 -05:00
|
|
|
await updateMyUser({
|
|
|
|
url: server.url,
|
2020-04-24 04:33:01 -05:00
|
|
|
accessToken: crashAccessToken,
|
2020-04-23 04:36:50 -05:00
|
|
|
displayName: 'Beautiful Crash',
|
|
|
|
description: 'Mutant eastern barred bandicoot'
|
|
|
|
})
|
2020-04-22 09:07:04 -05:00
|
|
|
|
2020-04-24 04:33:01 -05:00
|
|
|
const res = await getMyUserInformation(server.url, crashAccessToken)
|
2020-04-23 04:36:50 -05:00
|
|
|
|
|
|
|
const body: User = res.body
|
|
|
|
expect(body.account.displayName).to.equal('Beautiful Crash')
|
|
|
|
expect(body.account.description).to.equal('Mutant eastern barred bandicoot')
|
2020-04-22 09:07:04 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
it('Should logout Crash', async function () {
|
2020-04-24 04:33:01 -05:00
|
|
|
await logout(server.url, crashAccessToken)
|
2020-04-22 09:07:04 -05:00
|
|
|
})
|
|
|
|
|
2020-04-23 04:36:50 -05:00
|
|
|
it('Should have logged out Crash', async function () {
|
|
|
|
await waitUntilLog(server, 'On logout for auth 1 - 2')
|
2020-04-24 04:33:01 -05:00
|
|
|
|
|
|
|
await getMyUserInformation(server.url, crashAccessToken, 401)
|
2020-04-22 09:07:04 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
it('Should login Crash and keep the old existing profile', async function () {
|
2020-04-24 04:33:01 -05:00
|
|
|
crashAccessToken = await userLogin(server, { username: 'crash', password: 'crash password' })
|
2020-04-22 09:07:04 -05:00
|
|
|
|
2020-04-24 04:33:01 -05:00
|
|
|
const res = await getMyUserInformation(server.url, crashAccessToken)
|
2020-04-23 04:36:50 -05:00
|
|
|
|
|
|
|
const body: User = res.body
|
|
|
|
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 09:07:04 -05:00
|
|
|
})
|
|
|
|
|
2020-04-27 03:19:14 -05:00
|
|
|
it('Should reject token of laguna by the plugin hook', async function () {
|
2020-04-24 04:33:01 -05:00
|
|
|
this.timeout(10000)
|
|
|
|
|
|
|
|
await wait(5000)
|
|
|
|
|
|
|
|
await getMyUserInformation(server.url, lagunaAccessToken, 401)
|
|
|
|
})
|
|
|
|
|
2020-04-27 04:42:01 -05:00
|
|
|
it('Should reject an invalid username, email, role or display name', async function () {
|
|
|
|
await userLogin(server, { username: 'ward', password: 'ward password' }, 400)
|
|
|
|
await waitUntilLog(server, 'valid username')
|
|
|
|
|
|
|
|
await userLogin(server, { username: 'kiros', password: 'kiros password' }, 400)
|
|
|
|
await waitUntilLog(server, 'valid display name')
|
|
|
|
|
|
|
|
await userLogin(server, { username: 'raine', password: 'raine password' }, 400)
|
|
|
|
await waitUntilLog(server, 'valid role')
|
|
|
|
|
|
|
|
await userLogin(server, { username: 'ellone', password: 'elonne password' }, 400)
|
|
|
|
await waitUntilLog(server, 'valid email')
|
|
|
|
})
|
|
|
|
|
2020-04-22 09:07:04 -05:00
|
|
|
it('Should uninstall the plugin one and do not login existing Crash', async function () {
|
2020-04-23 04:36:50 -05:00
|
|
|
await uninstallPlugin({
|
|
|
|
url: server.url,
|
|
|
|
accessToken: server.accessToken,
|
|
|
|
npmName: 'peertube-plugin-test-id-pass-auth-one'
|
|
|
|
})
|
2020-04-22 09:07:04 -05:00
|
|
|
|
2020-04-23 04:36:50 -05:00
|
|
|
await userLogin(server, { username: 'crash', password: 'crash password' }, 400)
|
2020-04-22 09:07:04 -05:00
|
|
|
})
|
|
|
|
|
2020-04-29 02:04:42 -05:00
|
|
|
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
|
|
|
|
})
|
|
|
|
|
2020-04-22 09:07:04 -05:00
|
|
|
after(async function () {
|
|
|
|
await cleanupTests([ server ])
|
|
|
|
})
|
|
|
|
})
|