feat(js-sandbox): add pw.env.unset method (#3677)
Co-authored-by: jamesgeorge007 <jamesgeorge998001@gmail.com>
This commit is contained in:
@@ -79,6 +79,13 @@
|
|||||||
status="updations"
|
status="updations"
|
||||||
global
|
global
|
||||||
/>
|
/>
|
||||||
|
<HttpTestResultEnv
|
||||||
|
v-for="(env, index) in testResults.envDiff.global.deletions"
|
||||||
|
:key="`env-${env.key}-${index}`"
|
||||||
|
:env="env"
|
||||||
|
status="deletions"
|
||||||
|
global
|
||||||
|
/>
|
||||||
<HttpTestResultEnv
|
<HttpTestResultEnv
|
||||||
v-for="(env, index) in testResults.envDiff.selected.additions"
|
v-for="(env, index) in testResults.envDiff.selected.additions"
|
||||||
:key="`env-${env.key}-${index}`"
|
:key="`env-${env.key}-${index}`"
|
||||||
|
|||||||
@@ -8,7 +8,8 @@
|
|||||||
"value": "?",
|
"value": "?",
|
||||||
"writable": "bool",
|
"writable": "bool",
|
||||||
"get": "fn() -> ?",
|
"get": "fn() -> ?",
|
||||||
"set": "fn(value: ?)"
|
"set": "fn(value: ?)",
|
||||||
|
"unset": "fn(value: ?)"
|
||||||
},
|
},
|
||||||
"Promise.prototype": {
|
"Promise.prototype": {
|
||||||
"catch": {
|
"catch": {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
"pw": {
|
"pw": {
|
||||||
"env": {
|
"env": {
|
||||||
"set": "fn(key: string, value: string)",
|
"set": "fn(key: string, value: string)",
|
||||||
|
"unset": "fn(key: string)",
|
||||||
"get": "fn(key: string) -> string",
|
"get": "fn(key: string) -> string",
|
||||||
"getResolve": "fn(key: string) -> string",
|
"getResolve": "fn(key: string) -> string",
|
||||||
"resolve": "fn(value: string) -> string"
|
"resolve": "fn(value: string) -> string"
|
||||||
|
|||||||
@@ -22,6 +22,7 @@
|
|||||||
},
|
},
|
||||||
"env": {
|
"env": {
|
||||||
"set": "fn(key: string, value: string)",
|
"set": "fn(key: string, value: string)",
|
||||||
|
"unset": "fn(key: string)",
|
||||||
"get": "fn(key: string) -> string",
|
"get": "fn(key: string) -> string",
|
||||||
"getResolve": "fn(key: string) -> string",
|
"getResolve": "fn(key: string) -> string",
|
||||||
"resolve": "fn(value: string) -> string"
|
"resolve": "fn(value: string) -> string"
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
import { runPreRequestScript } from "~/pre-request/node-vm"
|
|
||||||
import "@relmify/jest-fp-ts"
|
import "@relmify/jest-fp-ts"
|
||||||
|
|
||||||
describe("execPreRequestScript", () => {
|
import { runPreRequestScript } from "~/pre-request/node-vm"
|
||||||
test("returns the updated envirionment properly", () => {
|
|
||||||
|
describe("runPreRequestScript", () => {
|
||||||
|
test("returns the updated environment properly", () => {
|
||||||
return expect(
|
return expect(
|
||||||
runPreRequestScript(
|
runPreRequestScript(
|
||||||
`
|
`
|
||||||
|
|||||||
@@ -0,0 +1,243 @@
|
|||||||
|
import * as TE from "fp-ts/TaskEither"
|
||||||
|
import { pipe } from "fp-ts/function"
|
||||||
|
|
||||||
|
import { runTestScript } from "~/test-runner/node-vm"
|
||||||
|
import { TestResponse, TestResult } from "~/types"
|
||||||
|
|
||||||
|
const fakeResponse: TestResponse = {
|
||||||
|
status: 200,
|
||||||
|
body: "hoi",
|
||||||
|
headers: [],
|
||||||
|
}
|
||||||
|
|
||||||
|
const func = (script: string, envs: TestResult["envs"]) =>
|
||||||
|
pipe(
|
||||||
|
runTestScript(script, envs, fakeResponse),
|
||||||
|
TE.map((x) => x.envs)
|
||||||
|
)
|
||||||
|
|
||||||
|
const funcTest = (script: string, envs: TestResult["envs"]) =>
|
||||||
|
pipe(
|
||||||
|
runTestScript(script, envs, fakeResponse),
|
||||||
|
TE.map((x) => x.tests)
|
||||||
|
)
|
||||||
|
|
||||||
|
describe("pw.env.unset", () => {
|
||||||
|
test("removes the variable set in selected environment correctly", () => {
|
||||||
|
return expect(
|
||||||
|
func(
|
||||||
|
`
|
||||||
|
pw.env.unset("baseUrl")
|
||||||
|
`,
|
||||||
|
{
|
||||||
|
global: [],
|
||||||
|
selected: [
|
||||||
|
{
|
||||||
|
key: "baseUrl",
|
||||||
|
value: "https://echo.hoppscotch.io",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
)()
|
||||||
|
).resolves.toEqualRight(
|
||||||
|
expect.objectContaining({
|
||||||
|
selected: [],
|
||||||
|
})
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
test("removes the variable set in global environment correctly", () => {
|
||||||
|
return expect(
|
||||||
|
func(
|
||||||
|
`
|
||||||
|
pw.env.unset("baseUrl")
|
||||||
|
`,
|
||||||
|
{
|
||||||
|
global: [
|
||||||
|
{
|
||||||
|
key: "baseUrl",
|
||||||
|
value: "https://echo.hoppscotch.io",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
selected: [],
|
||||||
|
}
|
||||||
|
)()
|
||||||
|
).resolves.toEqualRight(
|
||||||
|
expect.objectContaining({
|
||||||
|
global: [],
|
||||||
|
})
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
test("removes the variable from selected environment if the entry is present in both selected and global environments", () => {
|
||||||
|
return expect(
|
||||||
|
func(
|
||||||
|
`
|
||||||
|
pw.env.unset("baseUrl")
|
||||||
|
`,
|
||||||
|
{
|
||||||
|
global: [
|
||||||
|
{
|
||||||
|
key: "baseUrl",
|
||||||
|
value: "https://httpbin.org",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
selected: [
|
||||||
|
{
|
||||||
|
key: "baseUrl",
|
||||||
|
value: "https://echo.hoppscotch.io",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
)()
|
||||||
|
).resolves.toEqualRight(
|
||||||
|
expect.objectContaining({
|
||||||
|
global: [
|
||||||
|
{
|
||||||
|
key: "baseUrl",
|
||||||
|
value: "https://httpbin.org",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
selected: [],
|
||||||
|
})
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
test("removes the initial occurrence of an entry if duplicate entries exist in the selected environment", () => {
|
||||||
|
return expect(
|
||||||
|
func(
|
||||||
|
`
|
||||||
|
pw.env.unset("baseUrl")
|
||||||
|
`,
|
||||||
|
{
|
||||||
|
global: [
|
||||||
|
{
|
||||||
|
key: "baseUrl",
|
||||||
|
value: "https://echo.hoppscotch.io",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
selected: [
|
||||||
|
{
|
||||||
|
key: "baseUrl",
|
||||||
|
value: "https://httpbin.org",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: "baseUrl",
|
||||||
|
value: "https://echo.hoppscotch.io",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
)()
|
||||||
|
).resolves.toEqualRight(
|
||||||
|
expect.objectContaining({
|
||||||
|
global: [
|
||||||
|
{
|
||||||
|
key: "baseUrl",
|
||||||
|
value: "https://echo.hoppscotch.io",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
selected: [
|
||||||
|
{
|
||||||
|
key: "baseUrl",
|
||||||
|
value: "https://echo.hoppscotch.io",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
})
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
test("removes the initial occurrence of an entry if duplicate entries exist in the global environment", () => {
|
||||||
|
return expect(
|
||||||
|
func(
|
||||||
|
`
|
||||||
|
pw.env.unset("baseUrl")
|
||||||
|
`,
|
||||||
|
{
|
||||||
|
global: [
|
||||||
|
{
|
||||||
|
key: "baseUrl",
|
||||||
|
value: "https://httpbin.org/",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: "baseUrl",
|
||||||
|
value: "https://echo.hoppscotch.io",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
selected: [],
|
||||||
|
}
|
||||||
|
)()
|
||||||
|
).resolves.toEqualRight(
|
||||||
|
expect.objectContaining({
|
||||||
|
global: [
|
||||||
|
{
|
||||||
|
key: "baseUrl",
|
||||||
|
value: "https://echo.hoppscotch.io",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
selected: [],
|
||||||
|
})
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
test("no change if attempting to delete non-existent keys", () => {
|
||||||
|
return expect(
|
||||||
|
func(
|
||||||
|
`
|
||||||
|
pw.env.unset("baseUrl")
|
||||||
|
`,
|
||||||
|
{
|
||||||
|
global: [],
|
||||||
|
selected: [],
|
||||||
|
}
|
||||||
|
)()
|
||||||
|
).resolves.toEqualRight(
|
||||||
|
expect.objectContaining({
|
||||||
|
global: [],
|
||||||
|
selected: [],
|
||||||
|
})
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
test("keys should be a string", () => {
|
||||||
|
return expect(
|
||||||
|
func(
|
||||||
|
`
|
||||||
|
pw.env.unset(5)
|
||||||
|
`,
|
||||||
|
{
|
||||||
|
global: [],
|
||||||
|
selected: [],
|
||||||
|
}
|
||||||
|
)()
|
||||||
|
).resolves.toBeLeft()
|
||||||
|
})
|
||||||
|
|
||||||
|
test("set environment values are reflected in the script execution", () => {
|
||||||
|
return expect(
|
||||||
|
funcTest(
|
||||||
|
`
|
||||||
|
pw.env.unset("baseUrl")
|
||||||
|
pw.expect(pw.env.get("baseUrl")).toBe(undefined)
|
||||||
|
`,
|
||||||
|
{
|
||||||
|
global: [],
|
||||||
|
selected: [
|
||||||
|
{
|
||||||
|
key: "baseUrl",
|
||||||
|
value: "https://echo.hoppscotch.io",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
)()
|
||||||
|
).resolves.toEqualRight([
|
||||||
|
expect.objectContaining({
|
||||||
|
expectResults: [
|
||||||
|
{
|
||||||
|
status: "pass",
|
||||||
|
message: "Expected 'undefined' to be 'undefined'",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
])
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -16,8 +16,8 @@ const func = (script: string, res: TestResponse) =>
|
|||||||
TE.map((x) => x.tests)
|
TE.map((x) => x.tests)
|
||||||
)
|
)
|
||||||
|
|
||||||
describe("execTestScript function behavior", () => {
|
describe("runTestScript", () => {
|
||||||
test("returns a resolved promise for a valid test scripts with all green", () => {
|
test("returns a resolved promise for a valid test script with all green", () => {
|
||||||
return expect(
|
return expect(
|
||||||
func(
|
func(
|
||||||
`
|
`
|
||||||
|
|||||||
@@ -18,6 +18,63 @@ const getEnv = (envName: string, envs: TestResult["envs"]) => {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const findEnvIndex = (
|
||||||
|
envName: string,
|
||||||
|
envList: SelectedEnvItem[] | GlobalEnvItem[]
|
||||||
|
): number => {
|
||||||
|
return envList.findIndex(
|
||||||
|
(envItem: SelectedEnvItem) => envItem.key === envName
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
const setEnv = (
|
||||||
|
envName: string,
|
||||||
|
envValue: string,
|
||||||
|
envs: TestResult["envs"]
|
||||||
|
): TestResult["envs"] => {
|
||||||
|
const { global, selected } = envs
|
||||||
|
|
||||||
|
const indexInSelected = findEnvIndex(envName, selected)
|
||||||
|
const indexInGlobal = findEnvIndex(envName, global)
|
||||||
|
|
||||||
|
if (indexInSelected >= 0) {
|
||||||
|
selected[indexInSelected].value = envValue
|
||||||
|
} else if (indexInGlobal >= 0) {
|
||||||
|
global[indexInGlobal].value = envValue
|
||||||
|
} else {
|
||||||
|
selected.push({
|
||||||
|
key: envName,
|
||||||
|
value: envValue,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
global,
|
||||||
|
selected,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const unsetEnv = (
|
||||||
|
envName: string,
|
||||||
|
envs: TestResult["envs"]
|
||||||
|
): TestResult["envs"] => {
|
||||||
|
const { global, selected } = envs
|
||||||
|
|
||||||
|
const indexInSelected = findEnvIndex(envName, selected)
|
||||||
|
const indexInGlobal = findEnvIndex(envName, global)
|
||||||
|
|
||||||
|
if (indexInSelected >= 0) {
|
||||||
|
selected.splice(indexInSelected, 1)
|
||||||
|
} else if (indexInGlobal >= 0) {
|
||||||
|
global.splice(indexInGlobal, 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
global,
|
||||||
|
selected,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Compiles shared scripting API methods for use in both pre and post request scripts
|
// Compiles shared scripting API methods for use in both pre and post request scripts
|
||||||
const getSharedMethods = (envs: TestResult["envs"]) => {
|
const getSharedMethods = (envs: TestResult["envs"]) => {
|
||||||
let updatedEnvs = envs
|
let updatedEnvs = envs
|
||||||
@@ -79,6 +136,16 @@ const getSharedMethods = (envs: TestResult["envs"]) => {
|
|||||||
return undefined
|
return undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const envUnsetFn = (key: any) => {
|
||||||
|
if (typeof key !== "string") {
|
||||||
|
throw new Error("Expected key to be a string")
|
||||||
|
}
|
||||||
|
|
||||||
|
updatedEnvs = unsetEnv(key, updatedEnvs)
|
||||||
|
|
||||||
|
return undefined
|
||||||
|
}
|
||||||
|
|
||||||
const envResolveFn = (value: any) => {
|
const envResolveFn = (value: any) => {
|
||||||
if (typeof value !== "string") {
|
if (typeof value !== "string") {
|
||||||
throw new Error("Expected value to be a string")
|
throw new Error("Expected value to be a string")
|
||||||
@@ -101,6 +168,7 @@ const getSharedMethods = (envs: TestResult["envs"]) => {
|
|||||||
get: envGetFn,
|
get: envGetFn,
|
||||||
getResolve: envGetResolveFn,
|
getResolve: envGetResolveFn,
|
||||||
set: envSetFn,
|
set: envSetFn,
|
||||||
|
unset: envUnsetFn,
|
||||||
resolve: envResolveFn,
|
resolve: envResolveFn,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -108,51 +176,6 @@ const getSharedMethods = (envs: TestResult["envs"]) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const setEnv = (
|
|
||||||
envName: string,
|
|
||||||
envValue: string,
|
|
||||||
envs: TestResult["envs"]
|
|
||||||
): TestResult["envs"] => {
|
|
||||||
const { global, selected } = envs
|
|
||||||
|
|
||||||
const indexInSelected = selected.findIndex(
|
|
||||||
(x: SelectedEnvItem) => x.key === envName
|
|
||||||
)
|
|
||||||
|
|
||||||
// Found the match in selected
|
|
||||||
if (indexInSelected >= 0) {
|
|
||||||
selected[indexInSelected].value = envValue
|
|
||||||
|
|
||||||
return {
|
|
||||||
global,
|
|
||||||
selected,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const indexInGlobal = global.findIndex((x: GlobalEnvItem) => x.key == envName)
|
|
||||||
|
|
||||||
// Found a match in globals
|
|
||||||
if (indexInGlobal >= 0) {
|
|
||||||
global[indexInGlobal].value = envValue
|
|
||||||
|
|
||||||
return {
|
|
||||||
global,
|
|
||||||
selected,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Didn't find in both places, create a new variable in selected
|
|
||||||
selected.push({
|
|
||||||
key: envName,
|
|
||||||
value: envValue,
|
|
||||||
})
|
|
||||||
|
|
||||||
return {
|
|
||||||
global,
|
|
||||||
selected,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function preventCyclicObjects(
|
export function preventCyclicObjects(
|
||||||
obj: Record<string, any>
|
obj: Record<string, any>
|
||||||
): E.Left<string> | E.Right<Record<string, any>> {
|
): E.Left<string> | E.Right<Record<string, any>> {
|
||||||
|
|||||||
Reference in New Issue
Block a user