1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { expect } from 'chai'
4 import { wait } from '@shared/core-utils'
5 import { HttpStatusCode, UserRole } from '@shared/models'
6 import { cleanupTests, createSingleServer, PeerTubeServer, PluginsCommand, setAccessTokensToServers } from '@shared/server-commands'
8 describe('Test id and pass auth plugins', function () {
9 let server: PeerTubeServer
11 let crashAccessToken: string
12 let crashRefreshToken: string
14 let lagunaAccessToken: string
15 let lagunaRefreshToken: string
17 before(async function () {
20 server = await createSingleServer(1)
21 await setAccessTokensToServers([ server ])
23 for (const suffix of [ 'one', 'two', 'three' ]) {
24 await server.plugins.install({ path: PluginsCommand.getPluginTestPath('-id-pass-auth-' + suffix) })
28 it('Should display the correct configuration', async function () {
29 const config = await server.config.getConfig()
31 const auths = config.plugin.registeredIdAndPassAuths
32 expect(auths).to.have.lengthOf(8)
34 const crashAuth = auths.find(a => a.authName === 'crash-auth')
35 expect(crashAuth).to.exist
36 expect(crashAuth.npmName).to.equal('peertube-plugin-test-id-pass-auth-one')
37 expect(crashAuth.weight).to.equal(50)
40 it('Should not login', async function () {
41 await server.login.login({ user: { username: 'toto', password: 'password' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
44 it('Should login Spyro, create the user and use the token', async function () {
45 const accessToken = await server.login.getAccessToken({ username: 'spyro', password: 'spyro password' })
47 const body = await server.users.getMyInfo({ token: accessToken })
49 expect(body.username).to.equal('spyro')
50 expect(body.account.displayName).to.equal('Spyro the Dragon')
51 expect(body.role.id).to.equal(UserRole.USER)
54 it('Should login Crash, create the user and use the token', async function () {
56 const body = await server.login.login({ user: { username: 'crash', password: 'crash password' } })
57 crashAccessToken = body.access_token
58 crashRefreshToken = body.refresh_token
62 const body = await server.users.getMyInfo({ token: crashAccessToken })
64 expect(body.username).to.equal('crash')
65 expect(body.account.displayName).to.equal('Crash Bandicoot')
66 expect(body.role.id).to.equal(UserRole.MODERATOR)
70 it('Should login the first Laguna, create the user and use the token', async function () {
72 const body = await server.login.login({ user: { username: 'laguna', password: 'laguna password' } })
73 lagunaAccessToken = body.access_token
74 lagunaRefreshToken = body.refresh_token
78 const body = await server.users.getMyInfo({ token: lagunaAccessToken })
80 expect(body.username).to.equal('laguna')
81 expect(body.account.displayName).to.equal('laguna')
82 expect(body.role.id).to.equal(UserRole.USER)
86 it('Should refresh crash token, but not laguna token', async function () {
88 const resRefresh = await server.login.refreshToken({ refreshToken: crashRefreshToken })
89 crashAccessToken = resRefresh.body.access_token
90 crashRefreshToken = resRefresh.body.refresh_token
92 const body = await server.users.getMyInfo({ token: crashAccessToken })
93 expect(body.username).to.equal('crash')
97 await server.login.refreshToken({ refreshToken: lagunaRefreshToken, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
101 it('Should update Crash profile', async function () {
102 await server.users.updateMe({
103 token: crashAccessToken,
104 displayName: 'Beautiful Crash',
105 description: 'Mutant eastern barred bandicoot'
108 const body = await server.users.getMyInfo({ token: crashAccessToken })
110 expect(body.account.displayName).to.equal('Beautiful Crash')
111 expect(body.account.description).to.equal('Mutant eastern barred bandicoot')
114 it('Should logout Crash', async function () {
115 await server.login.logout({ token: crashAccessToken })
118 it('Should have logged out Crash', async function () {
119 await server.servers.waitUntilLog('On logout for auth 1 - 2')
121 await server.users.getMyInfo({ token: crashAccessToken, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
124 it('Should login Crash and keep the old existing profile', async function () {
125 crashAccessToken = await server.login.getAccessToken({ username: 'crash', password: 'crash password' })
127 const body = await server.users.getMyInfo({ token: crashAccessToken })
129 expect(body.username).to.equal('crash')
130 expect(body.account.displayName).to.equal('Beautiful Crash')
131 expect(body.account.description).to.equal('Mutant eastern barred bandicoot')
132 expect(body.role.id).to.equal(UserRole.MODERATOR)
135 it('Should reject token of laguna by the plugin hook', async function () {
140 await server.users.getMyInfo({ token: lagunaAccessToken, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
143 it('Should reject an invalid username, email, role or display name', async function () {
144 const command = server.login
146 await command.login({ user: { username: 'ward', password: 'ward password' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
147 await server.servers.waitUntilLog('valid username')
149 await command.login({ user: { username: 'kiros', password: 'kiros password' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
150 await server.servers.waitUntilLog('valid display name')
152 await command.login({ user: { username: 'raine', password: 'raine password' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
153 await server.servers.waitUntilLog('valid role')
155 await command.login({ user: { username: 'ellone', password: 'elonne password' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
156 await server.servers.waitUntilLog('valid email')
159 it('Should unregister spyro-auth and do not login existing Spyro', async function () {
160 await server.plugins.updateSettings({
161 npmName: 'peertube-plugin-test-id-pass-auth-one',
162 settings: { disableSpyro: true }
165 const command = server.login
166 await command.login({ user: { username: 'spyro', password: 'spyro password' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
167 await command.login({ user: { username: 'spyro', password: 'fake' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
170 it('Should have disabled this auth', async function () {
171 const config = await server.config.getConfig()
173 const auths = config.plugin.registeredIdAndPassAuths
174 expect(auths).to.have.lengthOf(7)
176 const spyroAuth = auths.find(a => a.authName === 'spyro-auth')
177 expect(spyroAuth).to.not.exist
180 it('Should uninstall the plugin one and do not login existing Crash', async function () {
181 await server.plugins.uninstall({ npmName: 'peertube-plugin-test-id-pass-auth-one' })
183 await server.login.login({
184 user: { username: 'crash', password: 'crash password' },
185 expectedStatus: HttpStatusCode.BAD_REQUEST_400
189 it('Should display the correct configuration', async function () {
190 const config = await server.config.getConfig()
192 const auths = config.plugin.registeredIdAndPassAuths
193 expect(auths).to.have.lengthOf(6)
195 const crashAuth = auths.find(a => a.authName === 'crash-auth')
196 expect(crashAuth).to.not.exist
199 it('Should display plugin auth information in users list', async function () {
200 const { data } = await server.users.list()
202 const root = data.find(u => u.username === 'root')
203 const crash = data.find(u => u.username === 'crash')
204 const laguna = data.find(u => u.username === 'laguna')
206 expect(root.pluginAuth).to.be.null
207 expect(crash.pluginAuth).to.equal('peertube-plugin-test-id-pass-auth-one')
208 expect(laguna.pluginAuth).to.equal('peertube-plugin-test-id-pass-auth-two')
211 after(async function () {
212 await cleanupTests([ server ])