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
18 before(async function () {
21 server = await createSingleServer(1)
22 await setAccessTokensToServers([ server ])
24 for (const suffix of [ 'one', 'two', 'three' ]) {
25 await server.plugins.install({ path: PluginsCommand.getPluginTestPath('-id-pass-auth-' + suffix) })
29 it('Should display the correct configuration', async function () {
30 const config = await server.config.getConfig()
32 const auths = config.plugin.registeredIdAndPassAuths
33 expect(auths).to.have.lengthOf(8)
35 const crashAuth = auths.find(a => a.authName === 'crash-auth')
36 expect(crashAuth).to.exist
37 expect(crashAuth.npmName).to.equal('peertube-plugin-test-id-pass-auth-one')
38 expect(crashAuth.weight).to.equal(50)
41 it('Should not login', async function () {
42 await server.login.login({ user: { username: 'toto', password: 'password' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
45 it('Should login Spyro, create the user and use the token', async function () {
46 const accessToken = await server.login.getAccessToken({ username: 'spyro', password: 'spyro password' })
48 const body = await server.users.getMyInfo({ token: accessToken })
50 expect(body.username).to.equal('spyro')
51 expect(body.account.displayName).to.equal('Spyro the Dragon')
52 expect(body.role.id).to.equal(UserRole.USER)
55 it('Should login Crash, create the user and use the token', async function () {
57 const body = await server.login.login({ user: { username: 'crash', password: 'crash password' } })
58 crashAccessToken = body.access_token
59 crashRefreshToken = body.refresh_token
63 const body = await server.users.getMyInfo({ token: crashAccessToken })
65 expect(body.username).to.equal('crash')
66 expect(body.account.displayName).to.equal('Crash Bandicoot')
67 expect(body.role.id).to.equal(UserRole.MODERATOR)
71 it('Should login the first Laguna, create the user and use the token', async function () {
73 const body = await server.login.login({ user: { username: 'laguna', password: 'laguna password' } })
74 lagunaAccessToken = body.access_token
75 lagunaRefreshToken = body.refresh_token
79 const body = await server.users.getMyInfo({ token: lagunaAccessToken })
81 expect(body.username).to.equal('laguna')
82 expect(body.account.displayName).to.equal('Laguna Loire')
83 expect(body.role.id).to.equal(UserRole.USER)
89 it('Should refresh crash token, but not laguna token', async function () {
91 const resRefresh = await server.login.refreshToken({ refreshToken: crashRefreshToken })
92 crashAccessToken = resRefresh.body.access_token
93 crashRefreshToken = resRefresh.body.refresh_token
95 const body = await server.users.getMyInfo({ token: crashAccessToken })
96 expect(body.username).to.equal('crash')
100 await server.login.refreshToken({ refreshToken: lagunaRefreshToken, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
104 it('Should update Crash profile', async function () {
105 await server.users.updateMe({
106 token: crashAccessToken,
107 displayName: 'Beautiful Crash',
108 description: 'Mutant eastern barred bandicoot'
111 const body = await server.users.getMyInfo({ token: crashAccessToken })
113 expect(body.account.displayName).to.equal('Beautiful Crash')
114 expect(body.account.description).to.equal('Mutant eastern barred bandicoot')
117 it('Should logout Crash', async function () {
118 await server.login.logout({ token: crashAccessToken })
121 it('Should have logged out Crash', async function () {
122 await server.servers.waitUntilLog('On logout for auth 1 - 2')
124 await server.users.getMyInfo({ token: crashAccessToken, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
127 it('Should login Crash and keep the old existing profile', async function () {
128 crashAccessToken = await server.login.getAccessToken({ username: 'crash', password: 'crash password' })
130 const body = await server.users.getMyInfo({ token: crashAccessToken })
132 expect(body.username).to.equal('crash')
133 expect(body.account.displayName).to.equal('Beautiful Crash')
134 expect(body.account.description).to.equal('Mutant eastern barred bandicoot')
135 expect(body.role.id).to.equal(UserRole.MODERATOR)
138 it('Should login Laguna and update the profile', async function () {
140 await server.users.update({ userId: lagunaId, videoQuota: 43000, videoQuotaDaily: 43100 })
141 await server.users.updateMe({ token: lagunaAccessToken, displayName: 'laguna updated' })
143 const body = await server.users.getMyInfo({ token: lagunaAccessToken })
144 expect(body.username).to.equal('laguna')
145 expect(body.account.displayName).to.equal('laguna updated')
146 expect(body.videoQuota).to.equal(43000)
147 expect(body.videoQuotaDaily).to.equal(43100)
151 const body = await server.login.login({ user: { username: 'laguna', password: 'laguna password' } })
152 lagunaAccessToken = body.access_token
153 lagunaRefreshToken = body.refresh_token
157 const body = await server.users.getMyInfo({ token: lagunaAccessToken })
158 expect(body.username).to.equal('laguna')
159 expect(body.account.displayName).to.equal('Laguna Loire')
160 expect(body.videoQuota).to.equal(42000)
161 expect(body.videoQuotaDaily).to.equal(43100)
165 it('Should reject token of laguna by the plugin hook', async function () {
170 await server.users.getMyInfo({ token: lagunaAccessToken, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
173 it('Should reject an invalid username, email, role or display name', async function () {
174 const command = server.login
176 await command.login({ user: { username: 'ward', password: 'ward password' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
177 await server.servers.waitUntilLog('valid username')
179 await command.login({ user: { username: 'kiros', password: 'kiros password' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
180 await server.servers.waitUntilLog('valid displayName')
182 await command.login({ user: { username: 'raine', password: 'raine password' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
183 await server.servers.waitUntilLog('valid role')
185 await command.login({ user: { username: 'ellone', password: 'elonne password' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
186 await server.servers.waitUntilLog('valid email')
189 it('Should unregister spyro-auth and do not login existing Spyro', async function () {
190 await server.plugins.updateSettings({
191 npmName: 'peertube-plugin-test-id-pass-auth-one',
192 settings: { disableSpyro: true }
195 const command = server.login
196 await command.login({ user: { username: 'spyro', password: 'spyro password' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
197 await command.login({ user: { username: 'spyro', password: 'fake' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
200 it('Should have disabled this auth', async function () {
201 const config = await server.config.getConfig()
203 const auths = config.plugin.registeredIdAndPassAuths
204 expect(auths).to.have.lengthOf(7)
206 const spyroAuth = auths.find(a => a.authName === 'spyro-auth')
207 expect(spyroAuth).to.not.exist
210 it('Should uninstall the plugin one and do not login existing Crash', async function () {
211 await server.plugins.uninstall({ npmName: 'peertube-plugin-test-id-pass-auth-one' })
213 await server.login.login({
214 user: { username: 'crash', password: 'crash password' },
215 expectedStatus: HttpStatusCode.BAD_REQUEST_400
219 it('Should display the correct configuration', async function () {
220 const config = await server.config.getConfig()
222 const auths = config.plugin.registeredIdAndPassAuths
223 expect(auths).to.have.lengthOf(6)
225 const crashAuth = auths.find(a => a.authName === 'crash-auth')
226 expect(crashAuth).to.not.exist
229 it('Should display plugin auth information in users list', async function () {
230 const { data } = await server.users.list()
232 const root = data.find(u => u.username === 'root')
233 const crash = data.find(u => u.username === 'crash')
234 const laguna = data.find(u => u.username === 'laguna')
236 expect(root.pluginAuth).to.be.null
237 expect(crash.pluginAuth).to.equal('peertube-plugin-test-id-pass-auth-one')
238 expect(laguna.pluginAuth).to.equal('peertube-plugin-test-id-pass-auth-two')
241 after(async function () {
242 await cleanupTests([ server ])