budibase/packages/server/src/integrations/tests/rest.spec.js

163 lines
3.6 KiB
JavaScript
Raw Normal View History

2021-10-05 13:38:03 +02:00
jest.mock("node-fetch", () =>
2021-11-03 16:45:19 +01:00
jest.fn(() => ({
headers: {
get: () => ["application/json"]
},
json: jest.fn(),
text: jest.fn()
}))
2021-10-05 13:38:03 +02:00
)
2021-03-16 15:51:14 +01:00
const fetch = require("node-fetch")
const RestIntegration = require("../rest")
const { AuthType } = require("../rest")
2021-03-16 15:51:14 +01:00
class TestConfiguration {
constructor(config = {}) {
2021-10-05 13:38:03 +02:00
this.integration = new RestIntegration.integration(config)
2021-03-16 15:51:14 +01:00
}
}
describe("REST Integration", () => {
const BASE_URL = "https://myapi.com"
2021-10-05 13:38:03 +02:00
let config
2021-03-16 15:51:14 +01:00
beforeEach(() => {
config = new TestConfiguration({
2021-10-05 13:38:03 +02:00
url: BASE_URL,
2021-03-16 15:51:14 +01:00
})
})
// afterEach(() => {
// jest.clearAllMocks()
// })
2021-03-16 15:51:14 +01:00
it("calls the create method with the correct params", async () => {
const query = {
2021-10-05 13:38:03 +02:00
path: "api",
queryString: "test=1",
2021-03-16 15:51:14 +01:00
headers: {
2021-10-05 13:38:03 +02:00
Accept: "application/json",
2021-03-16 15:51:14 +01:00
},
json: {
2021-10-05 13:38:03 +02:00
name: "test",
},
2021-03-16 15:51:14 +01:00
}
const response = await config.integration.create(query)
expect(fetch).toHaveBeenCalledWith(`${BASE_URL}/api?test=1`, {
method: "POST",
2021-10-05 13:38:03 +02:00
body: '{"name":"test"}',
2021-03-16 15:51:14 +01:00
headers: {
2021-10-05 13:38:03 +02:00
Accept: "application/json",
},
2021-03-16 15:51:14 +01:00
})
})
it("calls the read method with the correct params", async () => {
const query = {
2021-10-05 13:38:03 +02:00
path: "api",
queryString: "test=1",
2021-03-16 15:51:14 +01:00
headers: {
2021-10-05 13:38:03 +02:00
Accept: "text/html",
},
2021-03-16 15:51:14 +01:00
}
const response = await config.integration.read(query)
expect(fetch).toHaveBeenCalledWith(`${BASE_URL}/api?test=1`, {
headers: {
2021-10-05 13:38:03 +02:00
Accept: "text/html",
},
2021-03-16 15:51:14 +01:00
})
})
it("calls the update method with the correct params", async () => {
const query = {
2021-10-05 13:38:03 +02:00
path: "api",
queryString: "test=1",
2021-03-16 15:51:14 +01:00
headers: {
2021-10-05 13:38:03 +02:00
Accept: "application/json",
2021-03-16 15:51:14 +01:00
},
json: {
2021-10-05 13:38:03 +02:00
name: "test",
},
2021-03-16 15:51:14 +01:00
}
const response = await config.integration.update(query)
expect(fetch).toHaveBeenCalledWith(`${BASE_URL}/api?test=1`, {
method: "POST",
2021-10-05 13:38:03 +02:00
body: '{"name":"test"}',
2021-03-16 15:51:14 +01:00
headers: {
2021-10-05 13:38:03 +02:00
Accept: "application/json",
},
2021-03-16 15:51:14 +01:00
})
})
it("calls the delete method with the correct params", async () => {
const query = {
2021-10-05 13:38:03 +02:00
path: "api",
queryString: "test=1",
2021-03-16 15:51:14 +01:00
headers: {
2021-10-05 13:38:03 +02:00
Accept: "application/json",
2021-03-16 15:51:14 +01:00
},
json: {
2021-10-05 13:38:03 +02:00
name: "test",
},
2021-03-16 15:51:14 +01:00
}
const response = await config.integration.delete(query)
expect(fetch).toHaveBeenCalledWith(`${BASE_URL}/api?test=1`, {
method: "DELETE",
headers: {
2021-10-05 13:38:03 +02:00
Accept: "application/json",
},
2021-03-16 15:51:14 +01:00
})
})
describe("authentication", () => {
const basicAuth = {
id: "basic-1",
type : AuthType.BASIC,
config : {
username: "user",
password: "password"
}
}
const bearerAuth = {
id: "bearer-1",
type : AuthType.BEARER,
config : {
"token": "mytoken"
}
}
beforeEach(() => {
config = new TestConfiguration({
url: BASE_URL,
authConfigs : [basicAuth, bearerAuth]
})
})
it("adds basic auth", async () => {
const query = {
authConfigId: "basic-1"
}
await config.integration.read(query)
expect(fetch).toHaveBeenCalledWith(`${BASE_URL}/?`, {
method: "GET",
headers: {
Authorization: "Basic dXNlcjpwYXNzd29yZA=="
},
})
})
it("adds bearer auth", async () => {
const query = {
authConfigId: "bearer-1"
}
await config.integration.read(query)
expect(fetch).toHaveBeenCalledWith(`${BASE_URL}/?`, {
method: "GET",
headers: {
Authorization: "Bearer mytoken"
},
})
})
})
2021-10-05 13:38:03 +02:00
})