UNPKG

@mytmpvpn/mytmpvpn-client

Version:

MyTmpVpn Client Library

138 lines (137 loc) 7.65 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const loglevel_1 = __importDefault(require("loglevel")); const vpnlib = __importStar(require("@mytmpvpn/mytmpvpn-common/models/vpn")); const appconfig = __importStar(require("../src/appconfig")); const common_1 = require("./common"); const auth_1 = require("../src/auth"); describe('Testing deleteVpn APIs', () => { let client; const appConfig = appconfig.loadDefaultAppConfig(); const userProfile = (0, auth_1.createTestUserProfile)(); beforeAll(async () => { client = await (0, common_1.createAuthUser)(appConfig, userProfile); }, 600000); afterAll(async () => { await (0, common_1.deleteCleanUpAllAndDeleteAuthUser)(client, appConfig, userProfile); }, 600000); it("should error when deleting unknown vpn", async () => { const badVpnToDelete = "9999000000000000@us-west-2"; try { const response = await client.deleteVpn(badVpnToDelete); loglevel_1.default.debug(`response: ${JSON.stringify(response)}`); fail("Should not reach here"); } catch (err) { expect(err.response).toBeDefined(); expect(err.response.status).toBeDefined(); expect(err.response.data).toBeDefined(); expect(err.response.status).toBe(404); expect(err.response.data.error).toContain(badVpnToDelete); } }); it("should accept deleting already deleted vpn", async () => { const vpnConfig = (0, common_1.getGoodPeanutsConfig)(await client.getVpnConfigLimits()); const vpn = await client.createVpnSync(common_1.VPN_GEONAMES_ID, vpnConfig); loglevel_1.default.debug(`vpn created: ${JSON.stringify(vpn)}`); expect(vpn).toBeDefined(); expect(vpn.vpnId).toBeDefined(); expect(vpn.state).toEqual(vpnlib.VpnState.Running); let response = await client.deleteVpn(vpn.vpnId); loglevel_1.default.debug(`response: ${JSON.stringify(response)}`); expect(response.vpn.vpnId).toBeDefined(); expect(response.vpn.vpnId).toEqual(vpn.vpnId); let current = await client.waitUntilVpnStateIs(response.vpn.vpnId, vpnlib.VpnState.Deprovisioning); loglevel_1.default.debug(`current: ${JSON.stringify(current)}`); expect(current.vpnId).toBeDefined(); expect(current.vpnId).toEqual(vpn.vpnId); expect(vpnlib.toRank(current.state)).toBeGreaterThanOrEqual(vpnlib.toRank(vpnlib.VpnState.Deprovisioning)); response = await client.deleteVpn(vpn.vpnId); loglevel_1.default.debug(`response: ${JSON.stringify(response)}`); expect(response.vpn.vpnId).toBeDefined(); expect(response.vpn.vpnId).toEqual(vpn.vpnId); current = await client.waitUntilVpnStateIs(response.vpn.vpnId, vpnlib.VpnState.Deleted); loglevel_1.default.debug(`current: ${JSON.stringify(current)}`); expect(current.vpnId).toBeDefined(); expect(current.vpnId).toEqual(vpn.vpnId); expect(current.state).toEqual(vpnlib.VpnState.Deleted); response = await client.deleteVpn(vpn.vpnId); loglevel_1.default.debug(`response: ${JSON.stringify(response)}`); expect(response.vpn.vpnId).toBeDefined(); expect(response.vpn.vpnId).toEqual(vpn.vpnId); }); it("should delete after given deleteAfter", async () => { const vpnConfigLimits = await client.getVpnConfigLimits(); const vpn = await client.createVpnSync(common_1.VPN_GEONAMES_ID, { maxPeanuts: -1, type: vpnlib.VpnType.WireGuard, deleteAfter: vpnConfigLimits.deleteAfterFieldMinValue }); loglevel_1.default.log(`vpn: ${JSON.stringify(vpn)}`); expect(vpn).toBeDefined(); let current = await client.waitUntilVpnStateIs(vpn.vpnId, vpnlib.VpnState.Running); loglevel_1.default.log(`current: ${JSON.stringify(current)}`); expect(current.vpnId).toBeDefined(); expect(current.vpnId).toEqual(vpn.vpnId); // The waiting time is tricky to get right as it's best effort. // 1. We are waiting for the monitoring to catch up once the VPN is in Running state but it // could take up to 5 minutes to catch up. In testing we expect monitoring to be less than // this (1 minute ideally). // 2. As of 2025-05, the monitoring computes the time from the "createAt" field. // That is *not* when the instance has been provisioned and running. // 3. As of 2025-05, the waitUntilVpnStatIs() function is based on polling. So when the Vpn is // in Running state, it has probably been for a while already. // Given this, we will wait for the monitoring to catch up. // This should ideally be 1 min in testing environments (and 5 minutes in prod). const timeoutInSeconds = 60 * 2; // Let's double as this cannot be exact // It can take couple of minutes to deprovision, so we don't expect the state to be Delete // under the given timeout. Anything that is beyond Deprovisioning is fine. current = await client.waitUntilVpnStateIs(vpn.vpnId, vpnlib.VpnState.Deprovisioning, timeoutInSeconds); loglevel_1.default.log(`current: ${JSON.stringify(current)}`); expect(current.vpnId).toBeDefined(); expect(current.vpnId).toEqual(vpn.vpnId); }); it("should delete after given maxPeanuts", async () => { const vpnConfig = (0, common_1.getMinPeanutsConfig)(await client.getVpnConfigLimits()); const vpn = await client.createVpnSync(common_1.VPN_GEONAMES_ID, vpnConfig); loglevel_1.default.log(`vpn: ${JSON.stringify(vpn)}`); expect(vpn).toBeDefined(); let current = await client.waitUntilVpnStateIs(vpn.vpnId, vpnlib.VpnState.Running); loglevel_1.default.log(`current: ${JSON.stringify(current)}`); expect(current.vpnId).toBeDefined(); expect(current.vpnId).toEqual(vpn.vpnId); // Let's wait enough time for this to happen as we gave only minimum number of peanuts // this is best-effort. current = await client.waitUntilVpnStateIs(vpn.vpnId, vpnlib.VpnState.Deleted, 10 * 60); loglevel_1.default.log(`current: ${JSON.stringify(current)}`); expect(current.vpnId).toBeDefined(); expect(current.vpnId).toEqual(vpn.vpnId); expect(current.state).toEqual(vpnlib.VpnState.Deleted); }); });