|
| 1 | +const axios = require('axios'); |
| 2 | +const path = require('path'); |
| 3 | +const environment = require('dotenv'); |
| 4 | +const varium = require('varium'); |
| 5 | +const manifestPath = path.join(__dirname, '../env.manifest'); |
| 6 | + |
| 7 | +environment.config({ path: './test/.env' }); |
| 8 | +varium({ manifestPath }); |
| 9 | + |
| 10 | +const instance = axios.create({ |
| 11 | + baseURL: process.env.SERVER |
| 12 | +}); |
| 13 | + |
| 14 | +let adminToken = false; |
| 15 | + |
| 16 | +const login = () => |
| 17 | + instance |
| 18 | + .post( |
| 19 | + '/fmi/admin/api/v2/user/auth', |
| 20 | + {}, |
| 21 | + { |
| 22 | + auth: { |
| 23 | + username: process.env.ADMIN_USER, |
| 24 | + password: process.env.ADMIN_PASSWORD |
| 25 | + } |
| 26 | + } |
| 27 | + ) |
| 28 | + .then(response => { |
| 29 | + adminToken = response.data.response.token; |
| 30 | + return response.data.response.token; |
| 31 | + }); |
| 32 | + |
| 33 | +const logout = (token = adminToken) => instance |
| 34 | + .delete(`/fmi/admin/api/v2/user/auth/${token}`, {}) |
| 35 | + |
| 36 | +const remove = ({ id }, token = adminToken) => |
| 37 | + instance |
| 38 | + .delete(`/fmi/admin/api/v2/clients/${id}`, { |
| 39 | + params: { |
| 40 | + graceTime: 0 |
| 41 | + }, |
| 42 | + headers: { Authorization: `Bearer ${token}` } |
| 43 | + }) |
| 44 | + .then(response => response.data) |
| 45 | + .catch(error => console.log(error.response.data)); |
| 46 | + |
| 47 | +const drop = account => |
| 48 | + find(account).then(sessions => { |
| 49 | + const removals = []; |
| 50 | + sessions.forEach(session => removals.push(remove(session))); |
| 51 | + return Promise.all(removals); |
| 52 | + }); |
| 53 | + |
| 54 | +const find = ({ userName }, token = adminToken) => |
| 55 | + instance |
| 56 | + .get('/fmi/admin/api/v2/clients', { |
| 57 | + headers: { Authorization: `Bearer ${token}` } |
| 58 | + }) |
| 59 | + .then(response => |
| 60 | + response.data.response.clients.filter( |
| 61 | + client => client.userName === userName |
| 62 | + ) |
| 63 | + ); |
| 64 | + |
| 65 | +module.exports = { admin: { login, logout, sessions: { find, drop } } }; |
0 commit comments