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