2023-01-11 18:57:51 +01:00
|
|
|
// @ts-ignore
|
2022-12-12 23:02:32 +01:00
|
|
|
import fs from "fs"
|
2023-11-20 21:52:29 +01:00
|
|
|
|
2021-06-25 14:46:02 +02:00
|
|
|
module FetchMock {
|
2023-01-11 18:57:51 +01:00
|
|
|
// @ts-ignore
|
2021-06-25 14:46:02 +02:00
|
|
|
const fetch = jest.requireActual("node-fetch")
|
2022-01-05 16:01:28 +01:00
|
|
|
let failCount = 0
|
2023-03-31 11:32:13 +02:00
|
|
|
let mockSearch = false
|
2021-06-25 14:46:02 +02:00
|
|
|
|
2022-11-14 21:25:05 +01:00
|
|
|
const func = async (url: any, opts: any) => {
|
2024-03-06 17:57:29 +01:00
|
|
|
const { host, pathname } = new URL(url)
|
2021-06-25 14:46:02 +02:00
|
|
|
function json(body: any, status = 200) {
|
|
|
|
return {
|
|
|
|
status,
|
|
|
|
headers: {
|
2021-12-09 15:12:01 +01:00
|
|
|
raw: () => {
|
|
|
|
return { "content-type": ["application/json"] }
|
|
|
|
},
|
2021-06-25 14:46:02 +02:00
|
|
|
get: () => {
|
|
|
|
return ["application/json"]
|
|
|
|
},
|
|
|
|
},
|
|
|
|
json: async () => {
|
2022-06-17 13:00:42 +02:00
|
|
|
//x-www-form-encoded body is a URLSearchParams
|
|
|
|
//The call to stringify it leaves it blank
|
|
|
|
if (body?.opts?.body instanceof URLSearchParams) {
|
|
|
|
const paramArray = Array.from(body.opts.body.entries())
|
|
|
|
body.opts.body = paramArray.reduce((acc: any, pair: any) => {
|
|
|
|
acc[pair[0]] = pair[1]
|
|
|
|
return acc
|
|
|
|
}, {})
|
|
|
|
}
|
2021-06-25 14:46:02 +02:00
|
|
|
return body
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-06 17:57:29 +01:00
|
|
|
if (pathname.includes("/api/global")) {
|
2022-10-27 16:15:08 +02:00
|
|
|
const user = {
|
2024-02-08 17:32:14 +01:00
|
|
|
email: "test@example.com",
|
|
|
|
_id: "us_test@example.com",
|
2021-06-25 14:46:02 +02:00
|
|
|
status: "active",
|
2022-10-27 16:15:08 +02:00
|
|
|
roles: {},
|
|
|
|
builder: {
|
|
|
|
global: false,
|
|
|
|
},
|
|
|
|
admin: {
|
|
|
|
global: false,
|
|
|
|
},
|
|
|
|
}
|
2024-03-06 17:57:29 +01:00
|
|
|
return pathname.endsWith("/users") && opts.method === "GET"
|
2022-10-27 16:15:08 +02:00
|
|
|
? json([user])
|
|
|
|
: json(user)
|
2021-06-25 14:46:02 +02:00
|
|
|
}
|
|
|
|
// mocked data based on url
|
2024-03-06 17:57:29 +01:00
|
|
|
else if (pathname.includes("api/apps")) {
|
2021-06-25 14:46:02 +02:00
|
|
|
return json({
|
|
|
|
app1: {
|
|
|
|
url: "/app1",
|
|
|
|
},
|
|
|
|
})
|
2024-03-06 17:57:29 +01:00
|
|
|
} else if (host.includes("example.com")) {
|
2021-06-25 14:46:02 +02:00
|
|
|
return json({
|
|
|
|
body: opts.body,
|
|
|
|
url,
|
|
|
|
method: opts.method,
|
|
|
|
})
|
2024-03-06 17:57:29 +01:00
|
|
|
} else if (host.includes("invalid.com")) {
|
2021-06-25 14:46:02 +02:00
|
|
|
return json(
|
|
|
|
{
|
|
|
|
invalid: true,
|
|
|
|
},
|
|
|
|
404
|
|
|
|
)
|
2024-03-06 17:57:29 +01:00
|
|
|
} else if (mockSearch && pathname.includes("_search")) {
|
2022-08-08 20:16:25 +02:00
|
|
|
const body = opts.body
|
|
|
|
const parts = body.split("tableId:")
|
|
|
|
let tableId
|
|
|
|
if (parts && parts[1]) {
|
|
|
|
tableId = parts[1].split('"')[0]
|
|
|
|
}
|
2021-06-25 14:46:02 +02:00
|
|
|
return json({
|
|
|
|
rows: [
|
|
|
|
{
|
|
|
|
doc: {
|
|
|
|
_id: "test",
|
2022-08-08 20:16:25 +02:00
|
|
|
tableId: tableId,
|
|
|
|
query: opts.body,
|
2021-06-25 14:46:02 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
bookmark: "test",
|
|
|
|
})
|
2024-03-06 17:57:29 +01:00
|
|
|
} else if (host.includes("google.com")) {
|
2022-01-04 19:23:45 +01:00
|
|
|
return json({
|
|
|
|
url,
|
|
|
|
opts,
|
2022-01-20 09:17:08 +01:00
|
|
|
value:
|
|
|
|
'<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="en-GB"></html>',
|
2022-01-04 19:23:45 +01:00
|
|
|
})
|
2022-12-12 23:02:32 +01:00
|
|
|
} else if (
|
|
|
|
url === "https://api.github.com/repos/my-repo/budibase-comment-box"
|
|
|
|
) {
|
|
|
|
return Promise.resolve({
|
|
|
|
json: () => {
|
|
|
|
return {
|
|
|
|
name: "budibase-comment-box",
|
|
|
|
releases_url:
|
|
|
|
"https://api.github.com/repos/my-repo/budibase-comment-box{/id}",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
|
|
|
} else if (
|
|
|
|
url === "https://api.github.com/repos/my-repo/budibase-comment-box/latest"
|
|
|
|
) {
|
|
|
|
return Promise.resolve({
|
|
|
|
json: () => {
|
|
|
|
return {
|
|
|
|
assets: [
|
|
|
|
{
|
|
|
|
content_type: "application/gzip",
|
|
|
|
browser_download_url:
|
|
|
|
"https://github.com/my-repo/budibase-comment-box/releases/download/v1.0.2/comment-box-1.0.2.tar.gz",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
|
|
|
} else if (
|
|
|
|
url ===
|
|
|
|
"https://github.com/my-repo/budibase-comment-box/releases/download/v1.0.2/comment-box-1.0.2.tar.gz"
|
|
|
|
) {
|
|
|
|
return Promise.resolve({
|
|
|
|
body: fs.createReadStream(
|
|
|
|
"src/api/routes/tests/data/comment-box-1.0.2.tar.gz"
|
|
|
|
),
|
|
|
|
ok: true,
|
|
|
|
})
|
|
|
|
} else if (url === "https://www.npmjs.com/package/budibase-component") {
|
|
|
|
return Promise.resolve({
|
|
|
|
status: 200,
|
|
|
|
json: () => {
|
|
|
|
return {
|
|
|
|
name: "budibase-component",
|
|
|
|
"dist-tags": {
|
|
|
|
latest: "1.0.0",
|
|
|
|
},
|
|
|
|
versions: {
|
|
|
|
"1.0.0": {
|
|
|
|
dist: {
|
|
|
|
tarball:
|
|
|
|
"https://registry.npmjs.org/budibase-component/-/budibase-component-1.0.2.tgz",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
|
|
|
} else if (
|
|
|
|
url ===
|
|
|
|
"https://registry.npmjs.org/budibase-component/-/budibase-component-1.0.2.tgz"
|
|
|
|
) {
|
|
|
|
return Promise.resolve({
|
|
|
|
body: fs.createReadStream(
|
|
|
|
"src/api/routes/tests/data/budibase-component-1.0.2.tgz"
|
|
|
|
),
|
|
|
|
ok: true,
|
|
|
|
})
|
|
|
|
} else if (
|
|
|
|
url === "https://www.someurl.com/comment-box/comment-box-1.0.2.tar.gz"
|
|
|
|
) {
|
|
|
|
return Promise.resolve({
|
|
|
|
body: fs.createReadStream(
|
|
|
|
"src/api/routes/tests/data/comment-box-1.0.2.tar.gz"
|
|
|
|
),
|
|
|
|
ok: true,
|
|
|
|
})
|
2023-02-27 17:25:48 +01:00
|
|
|
} else if (url === "https://www.googleapis.com/oauth2/v4/token") {
|
|
|
|
// any valid response
|
|
|
|
return json({})
|
2024-03-06 17:57:29 +01:00
|
|
|
} else if (host.includes("failonce.com")) {
|
2022-01-05 16:01:28 +01:00
|
|
|
failCount++
|
|
|
|
if (failCount === 1) {
|
|
|
|
return json({ message: "error" }, 500)
|
|
|
|
} else {
|
|
|
|
return json({
|
|
|
|
fails: failCount - 1,
|
|
|
|
url,
|
|
|
|
opts,
|
|
|
|
})
|
|
|
|
}
|
2021-06-25 14:46:02 +02:00
|
|
|
}
|
|
|
|
return fetch(url, opts)
|
|
|
|
}
|
2022-11-14 21:25:05 +01:00
|
|
|
|
|
|
|
func.Headers = fetch.Headers
|
|
|
|
|
2023-03-31 11:32:13 +02:00
|
|
|
func.mockSearch = () => {
|
|
|
|
mockSearch = true
|
|
|
|
}
|
|
|
|
|
2022-11-14 21:25:05 +01:00
|
|
|
module.exports = func
|
2021-06-25 14:46:02 +02:00
|
|
|
}
|