]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - shared/utils/users/login.ts
Move utils to /shared
[github/Chocobozzz/PeerTube.git] / shared / utils / users / login.ts
CommitLineData
0e1dc3e7
C
1import * as request from 'supertest'
2
c5d31dba 3import { ServerInfo } from '../server/servers'
0e1dc3e7
C
4
5type Client = { id: string, secret: string }
6type User = { username: string, password: string }
7type Server = { url: string, client: Client, user: User }
8
9function login (url: string, client: Client, user: User, expectedStatus = 200) {
10 const path = '/api/v1/users/token'
11
12 const body = {
13 client_id: client.id,
14 client_secret: client.secret,
15 username: user.username,
16 password: user.password,
17 response_type: 'code',
18 grant_type: 'password',
19 scope: 'upload'
20 }
21
22 return request(url)
23 .post(path)
24 .type('form')
25 .send(body)
26 .expect(expectedStatus)
27}
28
eec63bbc 29async function serverLogin (server: Server) {
0e1dc3e7
C
30 const res = await login(server.url, server.client, server.user, 200)
31
32 return res.body.access_token as string
33}
34
f8b8c36b
C
35async function userLogin (server: Server, user: User, expectedStatus = 200) {
36 const res = await login(server.url, server.client, user, expectedStatus)
0e1dc3e7
C
37
38 return res.body.access_token as string
39}
40
41function setAccessTokensToServers (servers: ServerInfo[]) {
42 const tasks: Promise<any>[] = []
43
44 for (const server of servers) {
eec63bbc 45 const p = serverLogin(server).then(t => server.accessToken = t)
0e1dc3e7
C
46 tasks.push(p)
47 }
48
49 return Promise.all(tasks)
50}
51
52// ---------------------------------------------------------------------------
53
54export {
55 login,
eec63bbc
C
56 serverLogin,
57 userLogin,
c1e791ba
RK
58 setAccessTokensToServers,
59 Server,
60 Client,
61 User
0e1dc3e7 62}