1 import * as request from 'supertest'
3 import { ServerInfo } from '../server/servers'
4 import { getClient } from '../server/clients'
6 type Client = { id: string, secret: string }
7 type User = { username: string, password: string }
8 type Server = { url: string, client: Client, user: User }
10 function login (url: string, client: Client, user: User, expectedStatus = 200) {
11 const path = '/api/v1/users/token'
15 client_secret: client.secret,
16 username: user.username,
17 password: user.password,
18 response_type: 'code',
19 grant_type: 'password',
27 .expect(expectedStatus)
30 async function serverLogin (server: Server) {
31 const res = await login(server.url, server.client, server.user, 200)
33 return res.body.access_token as string
36 async function userLogin (server: Server, user: User, expectedStatus = 200) {
37 const res = await login(server.url, server.client, user, expectedStatus)
39 return res.body.access_token as string
42 async function getAccessToken (url: string, username: string, password: string) {
43 const resClient = await getClient(url)
45 id: resClient.body.client_id,
46 secret: resClient.body.client_secret
49 const user = { username, password }
52 const res = await login(url, client, user)
53 return res.body.access_token
55 throw new Error('Cannot authenticate. Please check your username/password.')
59 function setAccessTokensToServers (servers: ServerInfo[]) {
60 const tasks: Promise<any>[] = []
62 for (const server of servers) {
63 const p = serverLogin(server).then(t => { server.accessToken = t })
67 return Promise.all(tasks)
70 // ---------------------------------------------------------------------------
77 setAccessTokensToServers,