]>
Commit | Line | Data |
---|---|---|
7fed6375 C |
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ |
2 | ||
3 | import 'mocha' | |
ae2abfd3 | 4 | import { expect } from 'chai' |
4c7e60bc C |
5 | import { cleanupTests, createSingleServer, PeerTubeServer, PluginsCommand, setAccessTokensToServers, wait } from '@shared/extra-utils' |
6 | import { HttpStatusCode, UserRole } from '@shared/models' | |
7fed6375 C |
7 | |
8 | describe('Test id and pass auth plugins', function () { | |
254d3579 | 9 | let server: PeerTubeServer |
e307e4fc C |
10 | |
11 | let crashAccessToken: string | |
12 | let crashRefreshToken: string | |
13 | ||
14 | let lagunaAccessToken: string | |
15 | let lagunaRefreshToken: string | |
7fed6375 C |
16 | |
17 | before(async function () { | |
18 | this.timeout(30000) | |
19 | ||
254d3579 | 20 | server = await createSingleServer(1) |
7fed6375 C |
21 | await setAccessTokensToServers([ server ]) |
22 | ||
e1c55031 | 23 | for (const suffix of [ 'one', 'two', 'three' ]) { |
89d241a7 | 24 | await server.plugins.install({ path: PluginsCommand.getPluginTestPath('-id-pass-auth-' + suffix) }) |
e1c55031 | 25 | } |
7fed6375 C |
26 | }) |
27 | ||
9107d791 | 28 | it('Should display the correct configuration', async function () { |
89d241a7 | 29 | const config = await server.config.getConfig() |
9107d791 C |
30 | |
31 | const auths = config.plugin.registeredIdAndPassAuths | |
32 | expect(auths).to.have.lengthOf(8) | |
33 | ||
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) | |
38 | }) | |
39 | ||
e1c55031 | 40 | it('Should not login', async function () { |
89d241a7 | 41 | await server.login.login({ user: { username: 'toto', password: 'password' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) |
7fed6375 C |
42 | }) |
43 | ||
e1c55031 | 44 | it('Should login Spyro, create the user and use the token', async function () { |
89d241a7 | 45 | const accessToken = await server.login.getAccessToken({ username: 'spyro', password: 'spyro password' }) |
7fed6375 | 46 | |
89d241a7 | 47 | const body = await server.users.getMyInfo({ token: accessToken }) |
e1c55031 | 48 | |
e1c55031 C |
49 | expect(body.username).to.equal('spyro') |
50 | expect(body.account.displayName).to.equal('Spyro the Dragon') | |
51 | expect(body.role).to.equal(UserRole.USER) | |
7fed6375 C |
52 | }) |
53 | ||
e1c55031 | 54 | it('Should login Crash, create the user and use the token', async function () { |
e307e4fc | 55 | { |
89d241a7 | 56 | const body = await server.login.login({ user: { username: 'crash', password: 'crash password' } }) |
41d1d075 C |
57 | crashAccessToken = body.access_token |
58 | crashRefreshToken = body.refresh_token | |
e307e4fc | 59 | } |
e1c55031 | 60 | |
e307e4fc | 61 | { |
89d241a7 | 62 | const body = await server.users.getMyInfo({ token: crashAccessToken }) |
7fed6375 | 63 | |
e307e4fc C |
64 | expect(body.username).to.equal('crash') |
65 | expect(body.account.displayName).to.equal('Crash Bandicoot') | |
66 | expect(body.role).to.equal(UserRole.MODERATOR) | |
67 | } | |
7fed6375 C |
68 | }) |
69 | ||
e1c55031 | 70 | it('Should login the first Laguna, create the user and use the token', async function () { |
e307e4fc | 71 | { |
89d241a7 | 72 | const body = await server.login.login({ user: { username: 'laguna', password: 'laguna password' } }) |
41d1d075 C |
73 | lagunaAccessToken = body.access_token |
74 | lagunaRefreshToken = body.refresh_token | |
e307e4fc | 75 | } |
7fed6375 | 76 | |
e307e4fc | 77 | { |
89d241a7 | 78 | const body = await server.users.getMyInfo({ token: lagunaAccessToken }) |
e1c55031 | 79 | |
e307e4fc C |
80 | expect(body.username).to.equal('laguna') |
81 | expect(body.account.displayName).to.equal('laguna') | |
82 | expect(body.role).to.equal(UserRole.USER) | |
83 | } | |
84 | }) | |
85 | ||
86 | it('Should refresh crash token, but not laguna token', async function () { | |
87 | { | |
89d241a7 | 88 | const resRefresh = await server.login.refreshToken({ refreshToken: crashRefreshToken }) |
e307e4fc C |
89 | crashAccessToken = resRefresh.body.access_token |
90 | crashRefreshToken = resRefresh.body.refresh_token | |
91 | ||
89d241a7 | 92 | const body = await server.users.getMyInfo({ token: crashAccessToken }) |
7926c5f9 | 93 | expect(body.username).to.equal('crash') |
e307e4fc C |
94 | } |
95 | ||
96 | { | |
89d241a7 | 97 | await server.login.refreshToken({ refreshToken: lagunaRefreshToken, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) |
e307e4fc | 98 | } |
7fed6375 C |
99 | }) |
100 | ||
101 | it('Should update Crash profile', async function () { | |
89d241a7 | 102 | await server.users.updateMe({ |
7926c5f9 | 103 | token: crashAccessToken, |
e1c55031 C |
104 | displayName: 'Beautiful Crash', |
105 | description: 'Mutant eastern barred bandicoot' | |
106 | }) | |
7fed6375 | 107 | |
89d241a7 | 108 | const body = await server.users.getMyInfo({ token: crashAccessToken }) |
e1c55031 | 109 | |
e1c55031 C |
110 | expect(body.account.displayName).to.equal('Beautiful Crash') |
111 | expect(body.account.description).to.equal('Mutant eastern barred bandicoot') | |
7fed6375 C |
112 | }) |
113 | ||
114 | it('Should logout Crash', async function () { | |
89d241a7 | 115 | await server.login.logout({ token: crashAccessToken }) |
7fed6375 C |
116 | }) |
117 | ||
e1c55031 | 118 | it('Should have logged out Crash', async function () { |
89d241a7 | 119 | await server.servers.waitUntilLog('On logout for auth 1 - 2') |
e307e4fc | 120 | |
89d241a7 | 121 | await server.users.getMyInfo({ token: crashAccessToken, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 }) |
7fed6375 C |
122 | }) |
123 | ||
124 | it('Should login Crash and keep the old existing profile', async function () { | |
89d241a7 | 125 | crashAccessToken = await server.login.getAccessToken({ username: 'crash', password: 'crash password' }) |
7fed6375 | 126 | |
89d241a7 | 127 | const body = await server.users.getMyInfo({ token: crashAccessToken }) |
e1c55031 | 128 | |
e1c55031 C |
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).to.equal(UserRole.MODERATOR) | |
7fed6375 C |
133 | }) |
134 | ||
055cfb11 | 135 | it('Should reject token of laguna by the plugin hook', async function () { |
e307e4fc C |
136 | this.timeout(10000) |
137 | ||
138 | await wait(5000) | |
139 | ||
89d241a7 | 140 | await server.users.getMyInfo({ token: lagunaAccessToken, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 }) |
e307e4fc C |
141 | }) |
142 | ||
98813e69 | 143 | it('Should reject an invalid username, email, role or display name', async function () { |
89d241a7 | 144 | const command = server.login |
41d1d075 C |
145 | |
146 | await command.login({ user: { username: 'ward', password: 'ward password' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) | |
89d241a7 | 147 | await server.servers.waitUntilLog('valid username') |
98813e69 | 148 | |
41d1d075 | 149 | await command.login({ user: { username: 'kiros', password: 'kiros password' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) |
89d241a7 | 150 | await server.servers.waitUntilLog('valid display name') |
98813e69 | 151 | |
41d1d075 | 152 | await command.login({ user: { username: 'raine', password: 'raine password' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) |
89d241a7 | 153 | await server.servers.waitUntilLog('valid role') |
98813e69 | 154 | |
41d1d075 | 155 | await command.login({ user: { username: 'ellone', password: 'elonne password' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) |
89d241a7 | 156 | await server.servers.waitUntilLog('valid email') |
98813e69 C |
157 | }) |
158 | ||
a4995eb7 | 159 | it('Should unregister spyro-auth and do not login existing Spyro', async function () { |
89d241a7 | 160 | await server.plugins.updateSettings({ |
a4995eb7 C |
161 | npmName: 'peertube-plugin-test-id-pass-auth-one', |
162 | settings: { disableSpyro: true } | |
163 | }) | |
164 | ||
89d241a7 | 165 | const command = server.login |
41d1d075 C |
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 }) | |
a4995eb7 C |
168 | }) |
169 | ||
170 | it('Should have disabled this auth', async function () { | |
89d241a7 | 171 | const config = await server.config.getConfig() |
a4995eb7 C |
172 | |
173 | const auths = config.plugin.registeredIdAndPassAuths | |
174 | expect(auths).to.have.lengthOf(7) | |
175 | ||
176 | const spyroAuth = auths.find(a => a.authName === 'spyro-auth') | |
177 | expect(spyroAuth).to.not.exist | |
178 | }) | |
179 | ||
7fed6375 | 180 | it('Should uninstall the plugin one and do not login existing Crash', async function () { |
89d241a7 | 181 | await server.plugins.uninstall({ npmName: 'peertube-plugin-test-id-pass-auth-one' }) |
7fed6375 | 182 | |
89d241a7 | 183 | await server.login.login({ |
41d1d075 C |
184 | user: { username: 'crash', password: 'crash password' }, |
185 | expectedStatus: HttpStatusCode.BAD_REQUEST_400 | |
186 | }) | |
7fed6375 C |
187 | }) |
188 | ||
9107d791 | 189 | it('Should display the correct configuration', async function () { |
89d241a7 | 190 | const config = await server.config.getConfig() |
9107d791 C |
191 | |
192 | const auths = config.plugin.registeredIdAndPassAuths | |
193 | expect(auths).to.have.lengthOf(6) | |
194 | ||
195 | const crashAuth = auths.find(a => a.authName === 'crash-auth') | |
196 | expect(crashAuth).to.not.exist | |
197 | }) | |
198 | ||
8bb71f2e | 199 | it('Should display plugin auth information in users list', async function () { |
89d241a7 | 200 | const { data } = await server.users.list() |
8bb71f2e | 201 | |
7926c5f9 C |
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') | |
8bb71f2e C |
205 | |
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') | |
209 | }) | |
210 | ||
7fed6375 C |
211 | after(async function () { |
212 | await cleanupTests([ server ]) | |
213 | }) | |
214 | }) |