diff options
author | Chocobozzz <me@florianbigard.com> | 2020-04-29 09:04:42 +0200 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2020-05-04 16:21:39 +0200 |
commit | 9107d791e2eef9a1b24b0499dac8b9dbba8a792f (patch) | |
tree | f09b20f06008b094f3266b39063bc9785dfebf5f /shared | |
parent | 4a8d113b9b57d97ff13ad1608798eabca99643e4 (diff) | |
download | PeerTube-9107d791e2eef9a1b24b0499dac8b9dbba8a792f.tar.gz PeerTube-9107d791e2eef9a1b24b0499dac8b9dbba8a792f.tar.zst PeerTube-9107d791e2eef9a1b24b0499dac8b9dbba8a792f.zip |
Add external login tests
Diffstat (limited to 'shared')
-rw-r--r-- | shared/extra-utils/requests/requests.ts | 8 | ||||
-rw-r--r-- | shared/extra-utils/server/plugins.ts | 24 | ||||
-rw-r--r-- | shared/extra-utils/users/login.ts | 23 | ||||
-rw-r--r-- | shared/models/plugins/register-server-auth.model.ts | 4 |
4 files changed, 55 insertions, 4 deletions
diff --git a/shared/extra-utils/requests/requests.ts b/shared/extra-utils/requests/requests.ts index 61167f212..0e9d67f0b 100644 --- a/shared/extra-utils/requests/requests.ts +++ b/shared/extra-utils/requests/requests.ts | |||
@@ -4,6 +4,7 @@ import * as request from 'supertest' | |||
4 | import { buildAbsoluteFixturePath, root } from '../miscs/miscs' | 4 | import { buildAbsoluteFixturePath, root } from '../miscs/miscs' |
5 | import { isAbsolute, join } from 'path' | 5 | import { isAbsolute, join } from 'path' |
6 | import { URL } from 'url' | 6 | import { URL } from 'url' |
7 | import { decode } from 'querystring' | ||
7 | 8 | ||
8 | function get4KFileUrl () { | 9 | function get4KFileUrl () { |
9 | return 'https://download.cpy.re/peertube/4k_file.txt' | 10 | return 'https://download.cpy.re/peertube/4k_file.txt' |
@@ -23,6 +24,7 @@ function makeGetRequest (options: { | |||
23 | statusCodeExpected?: number | 24 | statusCodeExpected?: number |
24 | contentType?: string | 25 | contentType?: string |
25 | range?: string | 26 | range?: string |
27 | redirects?: number | ||
26 | }) { | 28 | }) { |
27 | if (!options.statusCodeExpected) options.statusCodeExpected = 400 | 29 | if (!options.statusCodeExpected) options.statusCodeExpected = 400 |
28 | if (options.contentType === undefined) options.contentType = 'application/json' | 30 | if (options.contentType === undefined) options.contentType = 'application/json' |
@@ -33,6 +35,7 @@ function makeGetRequest (options: { | |||
33 | if (options.token) req.set('Authorization', 'Bearer ' + options.token) | 35 | if (options.token) req.set('Authorization', 'Bearer ' + options.token) |
34 | if (options.query) req.query(options.query) | 36 | if (options.query) req.query(options.query) |
35 | if (options.range) req.set('Range', options.range) | 37 | if (options.range) req.set('Range', options.range) |
38 | if (options.redirects) req.redirects(options.redirects) | ||
36 | 39 | ||
37 | return req.expect(options.statusCodeExpected) | 40 | return req.expect(options.statusCodeExpected) |
38 | } | 41 | } |
@@ -171,12 +174,17 @@ function updateAvatarRequest (options: { | |||
171 | }) | 174 | }) |
172 | } | 175 | } |
173 | 176 | ||
177 | function decodeQueryString (path: string) { | ||
178 | return decode(path.split('?')[1]) | ||
179 | } | ||
180 | |||
174 | // --------------------------------------------------------------------------- | 181 | // --------------------------------------------------------------------------- |
175 | 182 | ||
176 | export { | 183 | export { |
177 | get4KFileUrl, | 184 | get4KFileUrl, |
178 | makeHTMLRequest, | 185 | makeHTMLRequest, |
179 | makeGetRequest, | 186 | makeGetRequest, |
187 | decodeQueryString, | ||
180 | makeUploadRequest, | 188 | makeUploadRequest, |
181 | makePostBodyRequest, | 189 | makePostBodyRequest, |
182 | makePutBodyRequest, | 190 | makePutBodyRequest, |
diff --git a/shared/extra-utils/server/plugins.ts b/shared/extra-utils/server/plugins.ts index 2d02d823d..b6b5e3958 100644 --- a/shared/extra-utils/server/plugins.ts +++ b/shared/extra-utils/server/plugins.ts | |||
@@ -235,6 +235,27 @@ function getPluginTestPath (suffix = '') { | |||
235 | return join(root(), 'server', 'tests', 'fixtures', 'peertube-plugin-test' + suffix) | 235 | return join(root(), 'server', 'tests', 'fixtures', 'peertube-plugin-test' + suffix) |
236 | } | 236 | } |
237 | 237 | ||
238 | function getExternalAuth (options: { | ||
239 | url: string | ||
240 | npmName: string | ||
241 | npmVersion: string | ||
242 | authName: string | ||
243 | query?: any | ||
244 | statusCodeExpected?: number | ||
245 | }) { | ||
246 | const { url, npmName, npmVersion, authName, statusCodeExpected, query } = options | ||
247 | |||
248 | const path = '/plugins/' + npmName + '/' + npmVersion + '/auth/' + authName | ||
249 | |||
250 | return makeGetRequest({ | ||
251 | url, | ||
252 | path, | ||
253 | query, | ||
254 | statusCodeExpected: statusCodeExpected || 200, | ||
255 | redirects: 0 | ||
256 | }) | ||
257 | } | ||
258 | |||
238 | export { | 259 | export { |
239 | listPlugins, | 260 | listPlugins, |
240 | listAvailablePlugins, | 261 | listAvailablePlugins, |
@@ -250,5 +271,6 @@ export { | |||
250 | updatePluginPackageJSON, | 271 | updatePluginPackageJSON, |
251 | getPluginPackageJSON, | 272 | getPluginPackageJSON, |
252 | getPluginTestPath, | 273 | getPluginTestPath, |
253 | getPublicSettings | 274 | getPublicSettings, |
275 | getExternalAuth | ||
254 | } | 276 | } |
diff --git a/shared/extra-utils/users/login.ts b/shared/extra-utils/users/login.ts index b12b51b8c..275bb0826 100644 --- a/shared/extra-utils/users/login.ts +++ b/shared/extra-utils/users/login.ts | |||
@@ -95,6 +95,26 @@ function setAccessTokensToServers (servers: ServerInfo[]) { | |||
95 | return Promise.all(tasks) | 95 | return Promise.all(tasks) |
96 | } | 96 | } |
97 | 97 | ||
98 | function loginUsingExternalToken (server: Server, username: string, externalAuthToken: string, expectedStatus = 200) { | ||
99 | const path = '/api/v1/users/token' | ||
100 | |||
101 | const body = { | ||
102 | client_id: server.client.id, | ||
103 | client_secret: server.client.secret, | ||
104 | username: username, | ||
105 | response_type: 'code', | ||
106 | grant_type: 'password', | ||
107 | scope: 'upload', | ||
108 | externalAuthToken | ||
109 | } | ||
110 | |||
111 | return request(server.url) | ||
112 | .post(path) | ||
113 | .type('form') | ||
114 | .send(body) | ||
115 | .expect(expectedStatus) | ||
116 | } | ||
117 | |||
98 | // --------------------------------------------------------------------------- | 118 | // --------------------------------------------------------------------------- |
99 | 119 | ||
100 | export { | 120 | export { |
@@ -107,5 +127,6 @@ export { | |||
107 | setAccessTokensToServers, | 127 | setAccessTokensToServers, |
108 | Server, | 128 | Server, |
109 | Client, | 129 | Client, |
110 | User | 130 | User, |
131 | loginUsingExternalToken | ||
111 | } | 132 | } |
diff --git a/shared/models/plugins/register-server-auth.model.ts b/shared/models/plugins/register-server-auth.model.ts index 08053f017..6539dc888 100644 --- a/shared/models/plugins/register-server-auth.model.ts +++ b/shared/models/plugins/register-server-auth.model.ts | |||
@@ -1,5 +1,5 @@ | |||
1 | import { UserRole } from '@shared/models' | 1 | import { UserRole } from '@shared/models' |
2 | import { MOAuthToken } from '@server/typings/models' | 2 | import { MOAuthToken, MUser } from '@server/typings/models' |
3 | import * as express from 'express' | 3 | import * as express from 'express' |
4 | 4 | ||
5 | export type RegisterServerAuthOptions = RegisterServerAuthPassOptions | RegisterServerAuthExternalOptions | 5 | export type RegisterServerAuthOptions = RegisterServerAuthPassOptions | RegisterServerAuthExternalOptions |
@@ -21,7 +21,7 @@ interface RegisterServerAuthBase { | |||
21 | authName: string | 21 | authName: string |
22 | 22 | ||
23 | // Called by PeerTube when a user from your plugin logged out | 23 | // Called by PeerTube when a user from your plugin logged out |
24 | onLogout?(): void | 24 | onLogout?(user: MUser): void |
25 | 25 | ||
26 | // Your plugin can hook PeerTube access/refresh token validity | 26 | // Your plugin can hook PeerTube access/refresh token validity |
27 | // So you can control for your plugin the user session lifetime | 27 | // So you can control for your plugin the user session lifetime |