2017-09-04 21:21:47 +02:00
|
|
|
import * as request from 'supertest'
|
|
|
|
|
2017-12-28 13:59:22 +01:00
|
|
|
import { ServerInfo } from '../server/servers'
|
2017-09-04 21:21:47 +02:00
|
|
|
|
|
|
|
type Client = { id: string, secret: string }
|
|
|
|
type User = { username: string, password: string }
|
|
|
|
type Server = { url: string, client: Client, user: User }
|
|
|
|
|
|
|
|
function login (url: string, client: Client, user: User, expectedStatus = 200) {
|
|
|
|
const path = '/api/v1/users/token'
|
|
|
|
|
|
|
|
const body = {
|
|
|
|
client_id: client.id,
|
|
|
|
client_secret: client.secret,
|
|
|
|
username: user.username,
|
|
|
|
password: user.password,
|
|
|
|
response_type: 'code',
|
|
|
|
grant_type: 'password',
|
|
|
|
scope: 'upload'
|
|
|
|
}
|
|
|
|
|
|
|
|
return request(url)
|
|
|
|
.post(path)
|
|
|
|
.type('form')
|
|
|
|
.send(body)
|
|
|
|
.expect(expectedStatus)
|
|
|
|
}
|
|
|
|
|
2017-12-28 14:29:57 +01:00
|
|
|
async function serverLogin (server: Server) {
|
2017-09-04 21:21:47 +02:00
|
|
|
const res = await login(server.url, server.client, server.user, 200)
|
|
|
|
|
|
|
|
return res.body.access_token as string
|
|
|
|
}
|
|
|
|
|
2018-01-23 09:15:36 +01:00
|
|
|
async function userLogin (server: Server, user: User, expectedStatus = 200) {
|
|
|
|
const res = await login(server.url, server.client, user, expectedStatus)
|
2017-09-04 21:21:47 +02:00
|
|
|
|
|
|
|
return res.body.access_token as string
|
|
|
|
}
|
|
|
|
|
|
|
|
function setAccessTokensToServers (servers: ServerInfo[]) {
|
|
|
|
const tasks: Promise<any>[] = []
|
|
|
|
|
|
|
|
for (const server of servers) {
|
2017-12-28 14:29:57 +01:00
|
|
|
const p = serverLogin(server).then(t => server.accessToken = t)
|
2017-09-04 21:21:47 +02:00
|
|
|
tasks.push(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.all(tasks)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
login,
|
2017-12-28 14:29:57 +01:00
|
|
|
serverLogin,
|
|
|
|
userLogin,
|
2018-07-25 22:01:25 +02:00
|
|
|
setAccessTokensToServers,
|
|
|
|
Server,
|
|
|
|
Client,
|
|
|
|
User
|
2017-09-04 21:21:47 +02:00
|
|
|
}
|