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'
|
2019-07-11 17:23:24 +02:00
|
|
|
import { getClient } from '../server/clients'
|
2020-12-08 21:16:10 +01:00
|
|
|
import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
|
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 }
|
|
|
|
|
2020-12-08 21:16:10 +01:00
|
|
|
function login (url: string, client: Client, user: User, expectedStatus = HttpStatusCode.OK_200) {
|
2017-09-04 21:21:47 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-12-08 21:16:10 +01:00
|
|
|
function logout (url: string, token: string, expectedStatus = HttpStatusCode.OK_200) {
|
2020-04-22 16:07:04 +02:00
|
|
|
const path = '/api/v1/users/revoke-token'
|
|
|
|
|
|
|
|
return request(url)
|
|
|
|
.post(path)
|
|
|
|
.set('Authorization', 'Bearer ' + token)
|
|
|
|
.type('form')
|
|
|
|
.expect(expectedStatus)
|
|
|
|
}
|
|
|
|
|
2017-12-28 14:29:57 +01:00
|
|
|
async function serverLogin (server: Server) {
|
2020-12-08 21:16:10 +01:00
|
|
|
const res = await login(server.url, server.client, server.user, HttpStatusCode.OK_200)
|
2017-09-04 21:21:47 +02:00
|
|
|
|
|
|
|
return res.body.access_token as string
|
|
|
|
}
|
|
|
|
|
2020-12-08 21:16:10 +01:00
|
|
|
function refreshToken (server: ServerInfo, refreshToken: string, expectedStatus = HttpStatusCode.OK_200) {
|
2020-04-24 11:33:01 +02:00
|
|
|
const path = '/api/v1/users/token'
|
|
|
|
|
|
|
|
const body = {
|
|
|
|
client_id: server.client.id,
|
|
|
|
client_secret: server.client.secret,
|
|
|
|
refresh_token: refreshToken,
|
|
|
|
response_type: 'code',
|
|
|
|
grant_type: 'refresh_token'
|
|
|
|
}
|
|
|
|
|
|
|
|
return request(server.url)
|
|
|
|
.post(path)
|
|
|
|
.type('form')
|
|
|
|
.send(body)
|
|
|
|
.expect(expectedStatus)
|
|
|
|
}
|
|
|
|
|
2020-12-08 21:16:10 +01:00
|
|
|
async function userLogin (server: Server, user: User, expectedStatus = HttpStatusCode.OK_200) {
|
2018-01-23 09:15:36 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-07-11 17:23:24 +02:00
|
|
|
async function getAccessToken (url: string, username: string, password: string) {
|
|
|
|
const resClient = await getClient(url)
|
|
|
|
const client = {
|
|
|
|
id: resClient.body.client_id,
|
|
|
|
secret: resClient.body.client_secret
|
|
|
|
}
|
|
|
|
|
|
|
|
const user = { username, password }
|
|
|
|
|
|
|
|
try {
|
|
|
|
const res = await login(url, client, user)
|
|
|
|
return res.body.access_token
|
|
|
|
} catch (err) {
|
|
|
|
throw new Error('Cannot authenticate. Please check your username/password.')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-04 21:21:47 +02:00
|
|
|
function setAccessTokensToServers (servers: ServerInfo[]) {
|
|
|
|
const tasks: Promise<any>[] = []
|
|
|
|
|
|
|
|
for (const server of servers) {
|
2020-01-31 16:56:52 +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)
|
|
|
|
}
|
|
|
|
|
2020-12-08 21:16:10 +01:00
|
|
|
function loginUsingExternalToken (server: Server, username: string, externalAuthToken: string, expectedStatus = HttpStatusCode.OK_200) {
|
2020-04-29 09:04:42 +02:00
|
|
|
const path = '/api/v1/users/token'
|
|
|
|
|
|
|
|
const body = {
|
|
|
|
client_id: server.client.id,
|
|
|
|
client_secret: server.client.secret,
|
|
|
|
username: username,
|
|
|
|
response_type: 'code',
|
|
|
|
grant_type: 'password',
|
|
|
|
scope: 'upload',
|
|
|
|
externalAuthToken
|
|
|
|
}
|
|
|
|
|
|
|
|
return request(server.url)
|
|
|
|
.post(path)
|
|
|
|
.type('form')
|
|
|
|
.send(body)
|
|
|
|
.expect(expectedStatus)
|
|
|
|
}
|
|
|
|
|
2017-09-04 21:21:47 +02:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
login,
|
2020-04-22 16:07:04 +02:00
|
|
|
logout,
|
2017-12-28 14:29:57 +01:00
|
|
|
serverLogin,
|
2020-04-24 11:33:01 +02:00
|
|
|
refreshToken,
|
2017-12-28 14:29:57 +01:00
|
|
|
userLogin,
|
2019-07-11 17:23:24 +02:00
|
|
|
getAccessToken,
|
2018-07-25 22:01:25 +02:00
|
|
|
setAccessTokensToServers,
|
|
|
|
Server,
|
|
|
|
Client,
|
2020-04-29 09:04:42 +02:00
|
|
|
User,
|
|
|
|
loginUsingExternalToken
|
2017-09-04 21:21:47 +02:00
|
|
|
}
|