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