|
|
|
@ -14,8 +14,10 @@ jest.mock("aws-sdk", () => ({ |
|
|
|
})) |
|
|
|
|
|
|
|
const setup = require("./utilities") |
|
|
|
const { events } = require("@budibase/backend-core") |
|
|
|
const version = require("../../../../package.json").version |
|
|
|
|
|
|
|
describe("/attachments", () => { |
|
|
|
describe("/static", () => { |
|
|
|
let request = setup.getRequest() |
|
|
|
let config = setup.getConfig() |
|
|
|
let app |
|
|
|
@ -26,73 +28,133 @@ describe("/attachments", () => { |
|
|
|
app = await config.init() |
|
|
|
}) |
|
|
|
|
|
|
|
describe("generateSignedUrls", () => { |
|
|
|
let datasource |
|
|
|
|
|
|
|
beforeEach(async () => { |
|
|
|
datasource = await config.createDatasource({ |
|
|
|
datasource: { |
|
|
|
type: "datasource", |
|
|
|
name: "Test", |
|
|
|
source: "S3", |
|
|
|
config: {}, |
|
|
|
}, |
|
|
|
}) |
|
|
|
describe("/builder", () => { |
|
|
|
it("should serve the builder", async () => { |
|
|
|
const res = await request |
|
|
|
.get("/builder/portal") |
|
|
|
.set(config.defaultHeaders()) |
|
|
|
.expect("Content-Type", /text\/html/) |
|
|
|
.expect(200) |
|
|
|
|
|
|
|
expect(res.text).toContain("<title>Budibase</title>") |
|
|
|
expect(events.serve.servedBuilder).toBeCalledTimes(1) |
|
|
|
expect(events.serve.servedBuilder).toBeCalledWith(version) |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
describe("/app", () => { |
|
|
|
|
|
|
|
it("should be able to generate a signed upload URL", async () => { |
|
|
|
const bucket = "foo" |
|
|
|
const key = "bar" |
|
|
|
beforeEach(() => { |
|
|
|
jest.clearAllMocks() |
|
|
|
}) |
|
|
|
|
|
|
|
it("should serve the app by id", async () => { |
|
|
|
const res = await request |
|
|
|
.post(`/api/attachments/${datasource._id}/url`) |
|
|
|
.send({ bucket, key }) |
|
|
|
.get(`/${config.prodAppId}`) |
|
|
|
.set(config.defaultHeaders()) |
|
|
|
.expect("Content-Type", /json/) |
|
|
|
.expect(200) |
|
|
|
expect(res.body.signedUrl).toEqual("my-url") |
|
|
|
expect(res.body.publicUrl).toEqual( |
|
|
|
`https://${bucket}.s3.eu-west-1.amazonaws.com/${key}` |
|
|
|
) |
|
|
|
|
|
|
|
expect(res.body.appId).toBe(config.prodAppId) |
|
|
|
expect(events.serve.servedApp).toBeCalledTimes(1) |
|
|
|
expect(events.serve.servedApp).toBeCalledWith(res.body) |
|
|
|
expect(events.serve.servedAppPreview).not.toBeCalled() |
|
|
|
}) |
|
|
|
|
|
|
|
it("should handle an invalid datasource ID", async () => { |
|
|
|
it("should serve the app by url", async () => { |
|
|
|
const res = await request |
|
|
|
.post(`/api/attachments/foo/url`) |
|
|
|
.send({ |
|
|
|
bucket: "foo", |
|
|
|
key: "bar", |
|
|
|
}) |
|
|
|
.get(`${config.prodApp.url}`) |
|
|
|
.set(config.defaultHeaders()) |
|
|
|
.expect("Content-Type", /json/) |
|
|
|
.expect(400) |
|
|
|
expect(res.body.message).toEqual( |
|
|
|
"The specified datasource could not be found" |
|
|
|
) |
|
|
|
.expect(200) |
|
|
|
|
|
|
|
expect(res.body.appId).toBe(config.prodAppId) |
|
|
|
expect(events.serve.servedApp).toBeCalledTimes(1) |
|
|
|
expect(events.serve.servedApp).toBeCalledWith(res.body) |
|
|
|
expect(events.serve.servedAppPreview).not.toBeCalled() |
|
|
|
}) |
|
|
|
|
|
|
|
it("should require a bucket parameter", async () => { |
|
|
|
it("should serve the app preview by id", async () => { |
|
|
|
const res = await request |
|
|
|
.post(`/api/attachments/${datasource._id}/url`) |
|
|
|
.send({ |
|
|
|
bucket: undefined, |
|
|
|
key: "bar", |
|
|
|
}) |
|
|
|
.get(`/${config.appId}`) |
|
|
|
.set(config.defaultHeaders()) |
|
|
|
.expect("Content-Type", /json/) |
|
|
|
.expect(400) |
|
|
|
expect(res.body.message).toEqual("bucket and key values are required") |
|
|
|
.expect(200) |
|
|
|
|
|
|
|
expect(res.body.appId).toBe(config.appId) |
|
|
|
expect(events.serve.servedAppPreview).toBeCalledTimes(1) |
|
|
|
expect(events.serve.servedAppPreview).toBeCalledWith(res.body) |
|
|
|
expect(events.serve.servedApp).not.toBeCalled() |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
it("should require a key parameter", async () => { |
|
|
|
const res = await request |
|
|
|
.post(`/api/attachments/${datasource._id}/url`) |
|
|
|
.send({ |
|
|
|
bucket: "foo", |
|
|
|
describe("/attachments", () => { |
|
|
|
describe("generateSignedUrls", () => { |
|
|
|
let datasource |
|
|
|
|
|
|
|
beforeEach(async () => { |
|
|
|
datasource = await config.createDatasource({ |
|
|
|
datasource: { |
|
|
|
type: "datasource", |
|
|
|
name: "Test", |
|
|
|
source: "S3", |
|
|
|
config: {}, |
|
|
|
}, |
|
|
|
}) |
|
|
|
.set(config.defaultHeaders()) |
|
|
|
.expect("Content-Type", /json/) |
|
|
|
.expect(400) |
|
|
|
expect(res.body.message).toEqual("bucket and key values are required") |
|
|
|
}) |
|
|
|
|
|
|
|
it("should be able to generate a signed upload URL", async () => { |
|
|
|
const bucket = "foo" |
|
|
|
const key = "bar" |
|
|
|
const res = await request |
|
|
|
.post(`/api/attachments/${datasource._id}/url`) |
|
|
|
.send({ bucket, key }) |
|
|
|
.set(config.defaultHeaders()) |
|
|
|
.expect("Content-Type", /json/) |
|
|
|
.expect(200) |
|
|
|
expect(res.body.signedUrl).toEqual("my-url") |
|
|
|
expect(res.body.publicUrl).toEqual( |
|
|
|
`https://${bucket}.s3.eu-west-1.amazonaws.com/${key}` |
|
|
|
) |
|
|
|
}) |
|
|
|
|
|
|
|
it("should handle an invalid datasource ID", async () => { |
|
|
|
const res = await request |
|
|
|
.post(`/api/attachments/foo/url`) |
|
|
|
.send({ |
|
|
|
bucket: "foo", |
|
|
|
key: "bar", |
|
|
|
}) |
|
|
|
.set(config.defaultHeaders()) |
|
|
|
.expect("Content-Type", /json/) |
|
|
|
.expect(400) |
|
|
|
expect(res.body.message).toEqual( |
|
|
|
"The specified datasource could not be found" |
|
|
|
) |
|
|
|
}) |
|
|
|
|
|
|
|
it("should require a bucket parameter", async () => { |
|
|
|
const res = await request |
|
|
|
.post(`/api/attachments/${datasource._id}/url`) |
|
|
|
.send({ |
|
|
|
bucket: undefined, |
|
|
|
key: "bar", |
|
|
|
}) |
|
|
|
.set(config.defaultHeaders()) |
|
|
|
.expect("Content-Type", /json/) |
|
|
|
.expect(400) |
|
|
|
expect(res.body.message).toEqual("bucket and key values are required") |
|
|
|
}) |
|
|
|
|
|
|
|
it("should require a key parameter", async () => { |
|
|
|
const res = await request |
|
|
|
.post(`/api/attachments/${datasource._id}/url`) |
|
|
|
.send({ |
|
|
|
bucket: "foo", |
|
|
|
}) |
|
|
|
.set(config.defaultHeaders()) |
|
|
|
.expect("Content-Type", /json/) |
|
|
|
.expect(400) |
|
|
|
expect(res.body.message).toEqual("bucket and key values are required") |
|
|
|
}) |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
}) |
|
|
|
|