]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/plugins/id-and-pass-auth.ts
Introduce stats command
[github/Chocobozzz/PeerTube.git] / server / tests / plugins / id-and-pass-auth.ts
CommitLineData
7fed6375
C
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3import 'mocha'
ae2abfd3 4import { expect } from 'chai'
e1c55031 5import {
ae2abfd3
C
6 cleanupTests,
7 flushAndRunServer,
8 getConfig,
e1c55031 9 getMyUserInformation,
ae2abfd3
C
10 getUsersList,
11 login,
e1c55031 12 logout,
ae2abfd3
C
13 PluginsCommand,
14 refreshToken,
15 ServerInfo,
e1c55031 16 setAccessTokensToServers,
e1c55031 17 updateMyUser,
e307e4fc
C
18 userLogin,
19 wait,
ae2abfd3
C
20 waitUntilLog
21} from '@shared/extra-utils'
22import { ServerConfig, User, UserRole } from '@shared/models'
7fed6375
C
23
24describe('Test id and pass auth plugins', function () {
25 let server: ServerInfo
e307e4fc
C
26
27 let crashAccessToken: string
28 let crashRefreshToken: string
29
30 let lagunaAccessToken: string
31 let lagunaRefreshToken: string
7fed6375
C
32
33 before(async function () {
34 this.timeout(30000)
35
36 server = await flushAndRunServer(1)
37 await setAccessTokensToServers([ server ])
38
e1c55031 39 for (const suffix of [ 'one', 'two', 'three' ]) {
ae2abfd3 40 await server.pluginsCommand.install({ path: PluginsCommand.getPluginTestPath('-id-pass-auth-' + suffix) })
e1c55031 41 }
7fed6375
C
42 })
43
9107d791
C
44 it('Should display the correct configuration', async function () {
45 const res = await getConfig(server.url)
46
47 const config: ServerConfig = res.body
48
49 const auths = config.plugin.registeredIdAndPassAuths
50 expect(auths).to.have.lengthOf(8)
51
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)
56 })
57
e1c55031
C
58 it('Should not login', async function () {
59 await userLogin(server, { username: 'toto', password: 'password' }, 400)
7fed6375
C
60 })
61
e1c55031
C
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' })
7fed6375 64
e1c55031
C
65 const res = await getMyUserInformation(server.url, accessToken)
66
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)
7fed6375
C
71 })
72
e1c55031 73 it('Should login Crash, create the user and use the token', async function () {
e307e4fc
C
74 {
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
78 }
e1c55031 79
e307e4fc
C
80 {
81 const res = await getMyUserInformation(server.url, crashAccessToken)
7fed6375 82
e307e4fc
C
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)
87 }
7fed6375
C
88 })
89
e1c55031 90 it('Should login the first Laguna, create the user and use the token', async function () {
e307e4fc
C
91 {
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
95 }
7fed6375 96
e307e4fc
C
97 {
98 const res = await getMyUserInformation(server.url, lagunaAccessToken)
e1c55031 99
e307e4fc
C
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)
104 }
105 })
106
107 it('Should refresh crash token, but not laguna token', async function () {
108 {
109 const resRefresh = await refreshToken(server, crashRefreshToken)
110 crashAccessToken = resRefresh.body.access_token
111 crashRefreshToken = resRefresh.body.refresh_token
112
113 const res = await getMyUserInformation(server.url, crashAccessToken)
114 const user: User = res.body
115 expect(user.username).to.equal('crash')
116 }
117
118 {
119 await refreshToken(server, lagunaRefreshToken, 400)
120 }
7fed6375
C
121 })
122
123 it('Should update Crash profile', async function () {
e1c55031
C
124 await updateMyUser({
125 url: server.url,
e307e4fc 126 accessToken: crashAccessToken,
e1c55031
C
127 displayName: 'Beautiful Crash',
128 description: 'Mutant eastern barred bandicoot'
129 })
7fed6375 130
e307e4fc 131 const res = await getMyUserInformation(server.url, crashAccessToken)
e1c55031
C
132
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')
7fed6375
C
136 })
137
138 it('Should logout Crash', async function () {
e307e4fc 139 await logout(server.url, crashAccessToken)
7fed6375
C
140 })
141
e1c55031 142 it('Should have logged out Crash', async function () {
e1c55031 143 await waitUntilLog(server, 'On logout for auth 1 - 2')
e307e4fc
C
144
145 await getMyUserInformation(server.url, crashAccessToken, 401)
7fed6375
C
146 })
147
148 it('Should login Crash and keep the old existing profile', async function () {
e307e4fc 149 crashAccessToken = await userLogin(server, { username: 'crash', password: 'crash password' })
7fed6375 150
e307e4fc 151 const res = await getMyUserInformation(server.url, crashAccessToken)
e1c55031
C
152
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)
7fed6375
C
158 })
159
055cfb11 160 it('Should reject token of laguna by the plugin hook', async function () {
e307e4fc
C
161 this.timeout(10000)
162
163 await wait(5000)
164
165 await getMyUserInformation(server.url, lagunaAccessToken, 401)
166 })
167
98813e69
C
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')
171
172 await userLogin(server, { username: 'kiros', password: 'kiros password' }, 400)
173 await waitUntilLog(server, 'valid display name')
174
175 await userLogin(server, { username: 'raine', password: 'raine password' }, 400)
176 await waitUntilLog(server, 'valid role')
177
178 await userLogin(server, { username: 'ellone', password: 'elonne password' }, 400)
179 await waitUntilLog(server, 'valid email')
180 })
181
a4995eb7 182 it('Should unregister spyro-auth and do not login existing Spyro', async function () {
ae2abfd3 183 await server.pluginsCommand.updateSettings({
a4995eb7
C
184 npmName: 'peertube-plugin-test-id-pass-auth-one',
185 settings: { disableSpyro: true }
186 })
187
188 await userLogin(server, { username: 'spyro', password: 'spyro password' }, 400)
189 await userLogin(server, { username: 'spyro', password: 'fake' }, 400)
190 })
191
192 it('Should have disabled this auth', async function () {
193 const res = await getConfig(server.url)
194
195 const config: ServerConfig = res.body
196
197 const auths = config.plugin.registeredIdAndPassAuths
198 expect(auths).to.have.lengthOf(7)
199
200 const spyroAuth = auths.find(a => a.authName === 'spyro-auth')
201 expect(spyroAuth).to.not.exist
202 })
203
7fed6375 204 it('Should uninstall the plugin one and do not login existing Crash', async function () {
ae2abfd3 205 await server.pluginsCommand.uninstall({ npmName: 'peertube-plugin-test-id-pass-auth-one' })
7fed6375 206
e1c55031 207 await userLogin(server, { username: 'crash', password: 'crash password' }, 400)
7fed6375
C
208 })
209
9107d791
C
210 it('Should display the correct configuration', async function () {
211 const res = await getConfig(server.url)
212
213 const config: ServerConfig = res.body
214
215 const auths = config.plugin.registeredIdAndPassAuths
216 expect(auths).to.have.lengthOf(6)
217
218 const crashAuth = auths.find(a => a.authName === 'crash-auth')
219 expect(crashAuth).to.not.exist
220 })
221
8bb71f2e
C
222 it('Should display plugin auth information in users list', async function () {
223 const res = await getUsersList(server.url, server.accessToken)
224
225 const users: User[] = res.body.data
226
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')
230
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')
234 })
235
7fed6375
C
236 after(async function () {
237 await cleanupTests([ server ])
238 })
239})