UNPKG

@mytmpvpn/mytmpvpn-client

Version:

MyTmpVpn Client Library

119 lines (118 loc) 4.99 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.deleteCleanUpAllAndDeleteAuthUser = exports.createAuthUser = exports.cleanUp = exports.cleanUpAll = exports.createUserProfile = exports.getMaxPeanutsConfig = exports.getGoodPeanutsConfig = exports.getMinPeanutsConfig = exports.getTooManyPeanutsConfig = exports.getTooFewPeanutsConfig = exports.VPN_REGION = void 0; const loglevel_1 = require("loglevel"); const util = require("util"); const utils = require("@mytmpvpn/mytmpvpn-common/utils"); const vpnlib = require("@mytmpvpn/mytmpvpn-common/models/vpn"); const lib = require("../src/"); // This region should always be an option exports.VPN_REGION = 'us-west-2'; function getTooFewPeanutsConfig(vpnConfigLimits) { return { type: vpnlib.VpnType.WireGuard, // We divide by 2 because this make sure it remains positive maxPeanuts: vpnConfigLimits.maxPeanutsFieldMinValue / 2 }; } exports.getTooFewPeanutsConfig = getTooFewPeanutsConfig; function getTooManyPeanutsConfig(vpnConfigLimits) { return { type: vpnlib.VpnType.WireGuard, maxPeanuts: vpnConfigLimits.maxPeanutsFieldMaxValue * 2 }; } exports.getTooManyPeanutsConfig = getTooManyPeanutsConfig; function getMinPeanutsConfig(vpnConfigLimits) { return { type: vpnlib.VpnType.WireGuard, maxPeanuts: vpnConfigLimits.maxPeanutsFieldMinValue }; } exports.getMinPeanutsConfig = getMinPeanutsConfig; function getGoodPeanutsConfig(vpnConfigLimits) { // We don't need more than 1 peanut for testing purpose // Let's make sure it's in the range let maxPeanuts = Math.min(vpnConfigLimits.maxPeanutsFieldMaxValue, Math.max(1, vpnConfigLimits.maxPeanutsFieldMinValue)); return { type: vpnlib.VpnType.WireGuard, maxPeanuts }; } exports.getGoodPeanutsConfig = getGoodPeanutsConfig; function getMaxPeanutsConfig(vpnConfigLimits) { // Though the given parameter is useless, we keep it for API consistency return { type: vpnlib.VpnType.WireGuard, maxPeanuts: -1 // By convention, this means use all available peanuts }; } exports.getMaxPeanutsConfig = getMaxPeanutsConfig; const PASSWORD_ALPHABET = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']; const PASSWORD_DIGITS = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']; const PASSWORD_SYMBOLS = ['!', '@', '#', '$', '%', '^', '&']; function createPassword() { const password = []; for (let i = 0; i < 16; i++) { password.push(utils.choose(PASSWORD_ALPHABET)); } // Make sure we have at least one digit, one high cap, and one symbol password.push(utils.choose(PASSWORD_DIGITS)); password.push(utils.choose(PASSWORD_SYMBOLS)); password.push(utils.choose(PASSWORD_ALPHABET).toUpperCase()); return password.join(''); } function createUserProfile() { const TEST_USERNAME_TEMPLATE = utils.getFromEnvOrThrow('TEST_USERNAME_TEMPLATE'); // Generate a unique id from a random number in hex format const id = Math.floor(Math.random() * 1000000).toString(16); const username = TEST_USERNAME_TEMPLATE.replace('${id}', id); // Generate a password using random number generation const password = createPassword(); const userProfile = { username, password }; // log.debug(`User profile generated: ${JSON.stringify(userProfile)}`) return userProfile; } exports.createUserProfile = createUserProfile; async function cleanUpAll(client) { const vpns = await client.listVpns(); const deleted = await client.waitAndDeleteAll(vpns.map(vpn => vpn.vpnId)); } exports.cleanUpAll = cleanUpAll; async function cleanUp(client, vpn) { await client.waitAndDeleteVpn(vpn.vpnId); } exports.cleanUp = cleanUp; async function createAuthUser(appConfig, userProfile) { const user = await lib.registerUser(appConfig, userProfile); loglevel_1.default.debug(`User ${JSON.stringify(user)} registered`); return await lib.getLoggedInClient(appConfig, userProfile); } exports.createAuthUser = createAuthUser; async function deleteCleanUpAllAndDeleteAuthUser(client, appConfig, userProfile) { const user = client.getUser(); if (user == null) { client = await lib.getLoggedInClient(appConfig, userProfile); } try { // We try our best to clean up in each individual test // but here we do it for sure await cleanUpAll(client); } catch (err) { console.error(`Error while cleaning up: ${err}`); throw err; } finally { const user = client.getUser(); if (user != null) { const deleteUser = util.promisify(user.deleteUser).bind(user); const result = await deleteUser(); loglevel_1.default.debug(`User deleted: ${JSON.stringify(result)}`); } } } exports.deleteCleanUpAllAndDeleteAuthUser = deleteCleanUpAllAndDeleteAuthUser;