Support logout and add id and pass tests
[oweals/peertube.git] / server / tests / plugins / id-and-pass-auth.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import 'mocha'
4 import { cleanupTests, flushAndRunServer, ServerInfo, waitUntilLog } from '../../../shared/extra-utils/server/servers'
5 import {
6   getMyUserInformation,
7   getPluginTestPath,
8   installPlugin,
9   logout,
10   setAccessTokensToServers,
11   uninstallPlugin,
12   updateMyUser,
13   userLogin
14 } from '../../../shared/extra-utils'
15 import { User, UserRole } from '@shared/models'
16 import { expect } from 'chai'
17
18 describe('Test id and pass auth plugins', function () {
19   let server: ServerInfo
20   let crashToken: string
21
22   before(async function () {
23     this.timeout(30000)
24
25     server = await flushAndRunServer(1)
26     await setAccessTokensToServers([ server ])
27
28     for (const suffix of [ 'one', 'two', 'three' ]) {
29       await installPlugin({
30         url: server.url,
31         accessToken: server.accessToken,
32         path: getPluginTestPath('-id-pass-auth-' + suffix)
33       })
34     }
35   })
36
37   it('Should not login', async function () {
38     await userLogin(server, { username: 'toto', password: 'password' }, 400)
39   })
40
41   it('Should login Spyro, create the user and use the token', async function () {
42     const accessToken = await userLogin(server, { username: 'spyro', password: 'spyro password' })
43
44     const res = await getMyUserInformation(server.url, accessToken)
45
46     const body: User = res.body
47     expect(body.username).to.equal('spyro')
48     expect(body.account.displayName).to.equal('Spyro the Dragon')
49     expect(body.role).to.equal(UserRole.USER)
50   })
51
52   it('Should login Crash, create the user and use the token', async function () {
53     crashToken = await userLogin(server, { username: 'crash', password: 'crash password' })
54
55     const res = await getMyUserInformation(server.url, crashToken)
56
57     const body: User = res.body
58     expect(body.username).to.equal('crash')
59     expect(body.account.displayName).to.equal('Crash Bandicoot')
60     expect(body.role).to.equal(UserRole.MODERATOR)
61   })
62
63   it('Should login the first Laguna, create the user and use the token', async function () {
64     const accessToken = await userLogin(server, { username: 'laguna', password: 'laguna password' })
65
66     const res = await getMyUserInformation(server.url, accessToken)
67
68     const body: User = res.body
69     expect(body.username).to.equal('laguna')
70     expect(body.account.displayName).to.equal('laguna')
71     expect(body.role).to.equal(UserRole.USER)
72   })
73
74   it('Should update Crash profile', async function () {
75     await updateMyUser({
76       url: server.url,
77       accessToken: crashToken,
78       displayName: 'Beautiful Crash',
79       description: 'Mutant eastern barred bandicoot'
80     })
81
82     const res = await getMyUserInformation(server.url, crashToken)
83
84     const body: User = res.body
85     expect(body.account.displayName).to.equal('Beautiful Crash')
86     expect(body.account.description).to.equal('Mutant eastern barred bandicoot')
87   })
88
89   it('Should logout Crash', async function () {
90     await logout(server.url, crashToken)
91   })
92
93   it('Should have logged out Crash', async function () {
94     await getMyUserInformation(server.url, crashToken, 401)
95
96     await waitUntilLog(server, 'On logout for auth 1 - 2')
97   })
98
99   it('Should login Crash and keep the old existing profile', async function () {
100     crashToken = await userLogin(server, { username: 'crash', password: 'crash password' })
101
102     const res = await getMyUserInformation(server.url, crashToken)
103
104     const body: User = res.body
105     expect(body.username).to.equal('crash')
106     expect(body.account.displayName).to.equal('Beautiful Crash')
107     expect(body.account.description).to.equal('Mutant eastern barred bandicoot')
108     expect(body.role).to.equal(UserRole.MODERATOR)
109   })
110
111   it('Should uninstall the plugin one and do not login existing Crash', async function () {
112     await uninstallPlugin({
113       url: server.url,
114       accessToken: server.accessToken,
115       npmName: 'peertube-plugin-test-id-pass-auth-one'
116     })
117
118     await userLogin(server, { username: 'crash', password: 'crash password' }, 400)
119   })
120
121   after(async function () {
122     await cleanupTests([ server ])
123   })
124 })