1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import { expect } from 'chai'
16 setAccessTokensToServers,
21 } from '@shared/extra-utils'
22 import { ServerConfig, User, UserRole } from '@shared/models'
24 describe('Test id and pass auth plugins', function () {
25 let server: ServerInfo
27 let crashAccessToken: string
28 let crashRefreshToken: string
30 let lagunaAccessToken: string
31 let lagunaRefreshToken: string
33 before(async function () {
36 server = await flushAndRunServer(1)
37 await setAccessTokensToServers([ server ])
39 for (const suffix of [ 'one', 'two', 'three' ]) {
40 await server.pluginsCommand.install({ path: PluginsCommand.getPluginTestPath('-id-pass-auth-' + suffix) })
44 it('Should display the correct configuration', async function () {
45 const res = await getConfig(server.url)
47 const config: ServerConfig = res.body
49 const auths = config.plugin.registeredIdAndPassAuths
50 expect(auths).to.have.lengthOf(8)
52 const crashAuth = auths.find(a => a.authName === 'crash-auth')
53 expect(crashAuth).to.exist
54 expect(crashAuth.npmName).to.equal('peertube-plugin-test-id-pass-auth-one')
55 expect(crashAuth.weight).to.equal(50)
58 it('Should not login', async function () {
59 await userLogin(server, { username: 'toto', password: 'password' }, 400)
62 it('Should login Spyro, create the user and use the token', async function () {
63 const accessToken = await userLogin(server, { username: 'spyro', password: 'spyro password' })
65 const res = await getMyUserInformation(server.url, accessToken)
67 const body: User = res.body
68 expect(body.username).to.equal('spyro')
69 expect(body.account.displayName).to.equal('Spyro the Dragon')
70 expect(body.role).to.equal(UserRole.USER)
73 it('Should login Crash, create the user and use the token', async function () {
75 const res = await login(server.url, server.client, { username: 'crash', password: 'crash password' })
76 crashAccessToken = res.body.access_token
77 crashRefreshToken = res.body.refresh_token
81 const res = await getMyUserInformation(server.url, crashAccessToken)
83 const body: User = res.body
84 expect(body.username).to.equal('crash')
85 expect(body.account.displayName).to.equal('Crash Bandicoot')
86 expect(body.role).to.equal(UserRole.MODERATOR)
90 it('Should login the first Laguna, create the user and use the token', async function () {
92 const res = await login(server.url, server.client, { username: 'laguna', password: 'laguna password' })
93 lagunaAccessToken = res.body.access_token
94 lagunaRefreshToken = res.body.refresh_token
98 const res = await getMyUserInformation(server.url, lagunaAccessToken)
100 const body: User = res.body
101 expect(body.username).to.equal('laguna')
102 expect(body.account.displayName).to.equal('laguna')
103 expect(body.role).to.equal(UserRole.USER)
107 it('Should refresh crash token, but not laguna token', async function () {
109 const resRefresh = await refreshToken(server, crashRefreshToken)
110 crashAccessToken = resRefresh.body.access_token
111 crashRefreshToken = resRefresh.body.refresh_token
113 const res = await getMyUserInformation(server.url, crashAccessToken)
114 const user: User = res.body
115 expect(user.username).to.equal('crash')
119 await refreshToken(server, lagunaRefreshToken, 400)
123 it('Should update Crash profile', async function () {
126 accessToken: crashAccessToken,
127 displayName: 'Beautiful Crash',
128 description: 'Mutant eastern barred bandicoot'
131 const res = await getMyUserInformation(server.url, crashAccessToken)
133 const body: User = res.body
134 expect(body.account.displayName).to.equal('Beautiful Crash')
135 expect(body.account.description).to.equal('Mutant eastern barred bandicoot')
138 it('Should logout Crash', async function () {
139 await logout(server.url, crashAccessToken)
142 it('Should have logged out Crash', async function () {
143 await waitUntilLog(server, 'On logout for auth 1 - 2')
145 await getMyUserInformation(server.url, crashAccessToken, 401)
148 it('Should login Crash and keep the old existing profile', async function () {
149 crashAccessToken = await userLogin(server, { username: 'crash', password: 'crash password' })
151 const res = await getMyUserInformation(server.url, crashAccessToken)
153 const body: User = res.body
154 expect(body.username).to.equal('crash')
155 expect(body.account.displayName).to.equal('Beautiful Crash')
156 expect(body.account.description).to.equal('Mutant eastern barred bandicoot')
157 expect(body.role).to.equal(UserRole.MODERATOR)
160 it('Should reject token of laguna by the plugin hook', async function () {
165 await getMyUserInformation(server.url, lagunaAccessToken, 401)
168 it('Should reject an invalid username, email, role or display name', async function () {
169 await userLogin(server, { username: 'ward', password: 'ward password' }, 400)
170 await waitUntilLog(server, 'valid username')
172 await userLogin(server, { username: 'kiros', password: 'kiros password' }, 400)
173 await waitUntilLog(server, 'valid display name')
175 await userLogin(server, { username: 'raine', password: 'raine password' }, 400)
176 await waitUntilLog(server, 'valid role')
178 await userLogin(server, { username: 'ellone', password: 'elonne password' }, 400)
179 await waitUntilLog(server, 'valid email')
182 it('Should unregister spyro-auth and do not login existing Spyro', async function () {
183 await server.pluginsCommand.updateSettings({
184 npmName: 'peertube-plugin-test-id-pass-auth-one',
185 settings: { disableSpyro: true }
188 await userLogin(server, { username: 'spyro', password: 'spyro password' }, 400)
189 await userLogin(server, { username: 'spyro', password: 'fake' }, 400)
192 it('Should have disabled this auth', async function () {
193 const res = await getConfig(server.url)
195 const config: ServerConfig = res.body
197 const auths = config.plugin.registeredIdAndPassAuths
198 expect(auths).to.have.lengthOf(7)
200 const spyroAuth = auths.find(a => a.authName === 'spyro-auth')
201 expect(spyroAuth).to.not.exist
204 it('Should uninstall the plugin one and do not login existing Crash', async function () {
205 await server.pluginsCommand.uninstall({ npmName: 'peertube-plugin-test-id-pass-auth-one' })
207 await userLogin(server, { username: 'crash', password: 'crash password' }, 400)
210 it('Should display the correct configuration', async function () {
211 const res = await getConfig(server.url)
213 const config: ServerConfig = res.body
215 const auths = config.plugin.registeredIdAndPassAuths
216 expect(auths).to.have.lengthOf(6)
218 const crashAuth = auths.find(a => a.authName === 'crash-auth')
219 expect(crashAuth).to.not.exist
222 it('Should display plugin auth information in users list', async function () {
223 const res = await getUsersList(server.url, server.accessToken)
225 const users: User[] = res.body.data
227 const root = users.find(u => u.username === 'root')
228 const crash = users.find(u => u.username === 'crash')
229 const laguna = users.find(u => u.username === 'laguna')
231 expect(root.pluginAuth).to.be.null
232 expect(crash.pluginAuth).to.equal('peertube-plugin-test-id-pass-auth-one')
233 expect(laguna.pluginAuth).to.equal('peertube-plugin-test-id-pass-auth-two')
236 after(async function () {
237 await cleanupTests([ server ])