Merge pull request #1271 from Budibase/develop

Develop
This commit is contained in:
Martin McKeaveney 2021-03-11 13:11:47 +00:00 committed by GitHub
commit a12caebf86
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
37 changed files with 1094 additions and 1096 deletions

View File

@ -1,8 +1,8 @@
{
"baseUrl": "http://localhost:4005/_builder/",
"baseUrl": "http://localhost:4001/_builder/",
"video": true,
"projectId": "bmbemn",
"env": {
"PORT": "4005"
"PORT": "4001"
}
}

View File

@ -1,18 +1,7 @@
context('Create an Application', () => {
beforeEach(() => {
cy.server()
cy.visit(`localhost:${Cypress.env("PORT")}/_builder`)
})
// https://on.cypress.io/interacting-with-elements
it('should create a new application', () => {
// https://on.cypress.io/type
cy.createApp('My Cool App', 'This is a description')
cy.visit(`localhost:${Cypress.env("PORT")}/_builder`)
cy.contains('My Cool App').should('exist')
})
context("Create an Application", () => {
it("should create a new application", () => {
cy.createTestApp()
cy.visit(`localhost:${Cypress.env("PORT")}/_builder`)
cy.contains("Cypress Tests").should("exist")
})
})

View File

@ -1,12 +1,6 @@
context("Create a automation", () => {
before(() => {
cy.server()
cy.visit(`localhost:${Cypress.env("PORT")}/_builder`)
cy.createApp(
"Automation Test App",
"This app is used to test that automations do in fact work!"
)
cy.createTestApp()
})
// https://on.cypress.io/interacting-with-elements

View File

@ -1,18 +1,59 @@
xcontext('Create a Binding', () => {
before(() => {
cy.visit(`localhost:${Cypress.env("PORT")}/_builder`)
cy.createApp('Binding App', 'Binding App Description')
cy.navigateToFrontend()
})
context("Create Bindings", () => {
before(() => {
cy.createTestApp()
cy.navigateToFrontend()
})
it('add an input binding', () => {
cy.get(".nav-items-container").contains('Home').click()
cy.contains("Add").click()
cy.get("[data-cy=Input]").click()
cy.get("[data-cy=Textfield]").click()
cy.contains("Heading").click()
cy.get("[data-cy=text-binding-button]").click()
cy.get("[data-cy=binding-dropdown-modal]").contains('Input 1').click()
cy.get("[data-cy=binding-dropdown-modal] textarea").should('have.value', 'Home{{ Input 1 }}')
it("should add a current user binding", () => {
cy.addComponent("Elements", "Paragraph").then(componentId => {
addSettingBinding("text", "Current User._id")
cy.getComponent(componentId).should(
"have.text",
`ro_ta_users_us_test@test.com`
)
})
})
it("should handle an invalid binding", () => {
cy.addComponent("Elements", "Paragraph").then(componentId => {
// Cypress needs to escape curly brackets
cy.get("[data-cy=setting-text] input")
.type("{{}{{}{{} Current User._id {}}{}}")
.blur()
cy.getComponent(componentId).should("have.text", "{{{ user._id }}")
})
})
it("should add a URL param binding", () => {
const paramName = "foo"
cy.createScreen("Test Param", `/test/:${paramName}`)
cy.addComponent("Elements", "Paragraph").then(componentId => {
addSettingBinding("text", `URL.${paramName}`)
// The builder preview pages don't have a real URL, so all we can do
// is check that we were able to bind to the property, and that the
// component exists on the page
cy.getComponent(componentId).should("have.text", "")
})
})
it("should add a binding with a handlebars helper", () => {
cy.addComponent("Elements", "Paragraph").then(componentId => {
// Cypress needs to escape curly brackets
addSettingBinding("text", "{{}{{} add 1 2 {}}{}}", false)
cy.getComponent(componentId).should("have.text", "3")
})
})
})
const addSettingBinding = (setting, bindingText, clickOption = true) => {
cy.get(`[data-cy="setting-${setting}"] [data-cy=text-binding-button]`).click()
cy.get(".drawer").within(() => {
if (clickOption) {
cy.contains(bindingText).click()
cy.get("textarea").should("have.value", `{{ ${bindingText} }}`)
} else {
cy.get("textarea").type(bindingText)
}
cy.get("button").click()
})
}

View File

@ -1,60 +1,90 @@
xcontext("Create Components", () => {
context("Create Components", () => {
let headlineId
before(() => {
cy.server()
cy.visit(`localhost:${Cypress.env("PORT")}/_builder`)
// https://on.cypress.io/type
cy.createApp("Table App", "Table App Description")
cy.createTable("dog", "name", "age")
cy.addRow("bob", "15")
})
// https://on.cypress.io/interacting-with-elements
it("should add a container", () => {
cy.createTestApp()
cy.createTable("dog")
cy.addColumn("dog", "name", "string")
cy.addColumn("dog", "age", "number")
cy.addColumn("dog", "type", "options")
cy.navigateToFrontend()
cy.get(".switcher > :nth-child(2)").click()
cy.contains("Container").click()
})
it("should add a headline", () => {
cy.addHeadlineComponent("An Amazing headline!")
getIframeBody().contains("An Amazing headline!")
it("should add a container", () => {
cy.addComponent(null, "Container").then(componentId => {
cy.getComponent(componentId).should("exist")
})
})
it("change the font size of the headline", () => {
it("should add a headline", () => {
cy.addComponent("Elements", "Headline").then(componentId => {
headlineId = componentId
cy.getComponent(headlineId).should("exist")
})
})
it("should change the text of the headline", () => {
const text = "Lorem ipsum dolor sit amet."
cy.get("[data-cy=Settings]").click()
cy.get("[data-cy=setting-text] input")
.type(text)
.blur()
cy.getComponent(headlineId).should("have.text", text)
})
it("should change the size of the headline", () => {
cy.get("[data-cy=Design]").click()
cy.contains("Typography").click()
cy.get("[data-cy=font-size-prop-control]").click()
cy.contains("60px").click()
cy.contains("Design").click()
cy.getComponent(headlineId).should("have.css", "font-size", "60px")
})
getIframeBody()
.contains("An Amazing headline!")
.should("have.css", "font-size", "60px")
it("should create a form and reset to match schema", () => {
cy.addComponent("Form", "Form").then(() => {
cy.get("[data-cy=Settings]").click()
cy.get("[data-cy=setting-datasource]")
.contains("Choose option")
.click()
cy.get(".dropdown")
.contains("dog")
.click()
cy.addComponent("Form", "Field Group").then(fieldGroupId => {
cy.get("[data-cy=Settings]").click()
cy.contains("Update Form Fields").click()
cy.get(".modal")
.get("button.primary")
.click()
cy.getComponent(fieldGroupId).within(() => {
cy.contains("name").should("exist")
cy.contains("age").should("exist")
cy.contains("type").should("exist")
})
cy.getComponent(fieldGroupId)
.find("input")
.should("have.length", 2)
cy.getComponent(fieldGroupId)
.find(".spectrum-Picker")
.should("have.length", 1)
})
})
})
it("deletes a component", () => {
cy.addComponent("Elements", "Paragraph").then(componentId => {
cy.get("[data-cy=setting-_instanceName] input")
.type(componentId)
.blur()
cy.get(".ui-nav ul .nav-item.selected .ri-more-line").click({
force: true,
})
cy.get(".dropdown-container")
.contains("Delete")
.click()
cy.get(".modal")
.contains("Delete Component")
.click()
cy.getComponent(componentId).should("not.exist")
})
})
})
const getIframeDocument = () => {
return (
cy
.get("iframe")
// Cypress yields jQuery element, which has the real
// DOM element under property "0".
// From the real DOM iframe element we can get
// the "document" element, it is stored in "contentDocument" property
// Cypress "its" command can access deep properties using dot notation
// https://on.cypress.io/its
.its("0.contentDocument")
.should("exist")
)
}
const getIframeBody = () => {
// get the document
return (
getIframeDocument()
// automatically retries until body is loaded
.its("body")
.should("not.be.undefined")
// wraps "body" DOM element to allow
// chaining more Cypress commands, like ".find(...)"
.then(cy.wrap)
)
}

View File

@ -0,0 +1,10 @@
context("Screen Tests", () => {
before(() => {
cy.createTestApp()
cy.navigateToFrontend()
})
it("Should successfully create a screen", () => {
cy.createScreen("Test Screen", "/test")
})
})

View File

@ -1,7 +1,6 @@
context("Create a Table", () => {
before(() => {
cy.visit(`localhost:${Cypress.env("PORT")}/_builder`)
cy.createApp("Table App", "Table App Description")
cy.createTestApp()
})
it("should create a new Table", () => {
@ -26,7 +25,9 @@ context("Create a Table", () => {
.trigger("mouseover")
.find(".ri-pencil-line")
.click({ force: true })
cy.get(".actions input").first().type("updated")
cy.get(".actions input")
.first()
.type("updated")
// Unset table display column
cy.contains("display column").click()
cy.contains("Save Column").click()
@ -44,7 +45,9 @@ context("Create a Table", () => {
it("deletes a row", () => {
cy.get(".ag-checkbox-input").check({ force: true })
cy.contains("Delete 1 row(s)").click()
cy.get(".modal").contains("Delete").click()
cy.get(".modal")
.contains("Delete")
.click()
cy.contains("RoverUpdated").should("not.exist")
})
@ -55,12 +58,16 @@ context("Create a Table", () => {
.click({ force: true })
cy.contains("Delete").click()
cy.wait(50)
cy.get(".buttons").contains("Delete").click()
cy.get(".buttons")
.contains("Delete")
.click()
cy.contains("nameupdated").should("not.exist")
})
it("deletes a table", () => {
cy.get(".ri-more-line").first().click({ force: true })
cy.get(".ri-more-line")
.first()
.click({ force: true })
cy.get("[data-cy=delete-table]").click()
cy.contains("Delete Table").click()
cy.contains("dog").should("not.exist")

View File

@ -1,17 +1,10 @@
context('Create a User', () => {
context("Create a User", () => {
before(() => {
cy.createTestApp()
})
before(() => {
cy.server()
cy.visit(`localhost:${Cypress.env("PORT")}/_builder`)
// https://on.cypress.io/type
cy.createApp('User App', 'This app is used to test user creation')
})
// https://on.cypress.io/interacting-with-elements
it('should create a user', () => {
cy.createUser("bbuser@test.com", "test", "ADMIN")
// // Check to make sure user was created!
cy.contains("bbuser").should('be.visible')
})
it("should create a user", () => {
cy.createUser("bbuser@test.com", "test", "ADMIN")
cy.contains("bbuser").should("be.visible")
})
})

View File

@ -1,15 +1,6 @@
function removeSpacing(headers) {
let newHeaders = []
for (let header of headers) {
newHeaders.push(header.replace(/\s\s+/g, " "))
}
return newHeaders
}
context("Create a View", () => {
before(() => {
cy.visit(`localhost:${Cypress.env("PORT")}/_builder`)
cy.createApp("View App", "View App Description")
cy.createTestApp()
cy.createTable("data")
cy.addColumn("data", "group", "Text")
cy.addColumn("data", "age", "Number")
@ -36,16 +27,28 @@ context("Create a View", () => {
const headers = Array.from($headers).map(header =>
header.textContent.trim()
)
expect(removeSpacing(headers)).to.deep.eq([ "rating Number", "age Number", "group Text" ])
expect(removeSpacing(headers)).to.deep.eq([
"rating Number",
"age Number",
"group Text",
])
})
})
it("filters the view by age over 10", () => {
cy.contains("Filter").click()
cy.contains("Add Filter").click()
cy.get(".menu-container").find("select").first().select("age")
cy.get(".menu-container").find("select").eq(1).select("More Than")
cy.get(".menu-container").find("input").type(18)
cy.get(".menu-container")
.find("select")
.first()
.select("age")
cy.get(".menu-container")
.find("select")
.eq(1)
.select("More Than")
cy.get(".menu-container")
.find("input")
.type(18)
cy.contains("Save").click()
cy.get("[role=rowgroup] .ag-row").get($values => {
expect($values).to.have.length(5)
@ -57,9 +60,15 @@ context("Create a View", () => {
cy.viewport("macbook-15")
cy.contains("Calculate").click()
cy.get(".menu-container").find("select").eq(0).select("Statistics")
cy.get(".menu-container")
.find("select")
.eq(0)
.select("Statistics")
cy.wait(50)
cy.get(".menu-container").find("select").eq(1).select("age")
cy.get(".menu-container")
.find("select")
.eq(1)
.select("age")
cy.contains("Save").click()
cy.wait(100)
cy.get(".ag-center-cols-viewport").scrollTo("100%")
@ -68,19 +77,19 @@ context("Create a View", () => {
const headers = Array.from($headers).map(header =>
header.textContent.trim()
)
expect(removeSpacing(headers)).to.deep.eq([ "avg Number",
expect(removeSpacing(headers)).to.deep.eq([
"avg Number",
"sumsqr Number",
"count Number",
"max Number",
"min Number",
"sum Number",
"field Text" ])
"field Text",
])
})
cy.get(".ag-cell").then($values => {
let values = Array.from($values).map(header =>
header.textContent.trim()
)
expect(values).to.deep.eq([ "31", "5347", "5", "49", "20", "155", "age" ])
let values = Array.from($values).map(header => header.textContent.trim())
expect(values).to.deep.eq(["31", "5347", "5", "49", "20", "155", "age"])
})
})
@ -99,7 +108,15 @@ context("Create a View", () => {
.find(".ag-cell")
.then($values => {
const values = Array.from($values).map(value => value.textContent)
expect(values).to.deep.eq([ "Students", "23.333333333333332", "1650", "3", "25", "20", "70" ])
expect(values).to.deep.eq([
"Students",
"23.333333333333332",
"1650",
"3",
"25",
"20",
"70",
])
})
})
@ -124,3 +141,11 @@ context("Create a View", () => {
cy.contains("TestView Updated").should("not.be.visible")
})
})
function removeSpacing(headers) {
let newHeaders = []
for (let header of headers) {
newHeaders.push(header.replace(/\s\s+/g, " "))
}
return newHeaders
}

View File

@ -1,13 +0,0 @@
context('Screen Tests', () => {
before(() => {
cy.server()
cy.visit(`localhost:${Cypress.env("PORT")}/_builder`)
cy.createApp('Conor Cy App', 'Table App Description')
cy.navigateToFrontend()
})
it('Should successfully create a screen', () => {
cy.createScreen("test Screen", "/test")
})
})

View File

@ -3,15 +3,12 @@
// 2. Initialises using `.budibase`
// 3. Runs the server using said folder
const rimraf = require("rimraf")
const { join, resolve } = require("path")
const initialiseBudibase = require("../../server/src/utilities/initialiseBudibase")
const cypressConfig = require("../cypress.json")
const homedir = join(require("os").homedir(), ".budibase")
rimraf.sync(homedir)
process.env.BUDIBASE_API_KEY = "6BE826CB-6B30-4AEC-8777-2E90464633DE"
process.env.NODE_ENV = "cypress"
process.env.ENABLE_ANALYTICS = "false"

View File

@ -25,8 +25,8 @@
// Cypress.Commands.overwrite("visit", (originalFn, url, options) => { ... })
Cypress.Commands.add("createApp", name => {
cy.visit(`localhost:${Cypress.env("PORT")}/_builder`)
cy.contains("Create New Web App").click()
cy.get("body")
.then($body => {
if ($body.find("input[name=apiKey]").length) {
@ -41,9 +41,7 @@ Cypress.Commands.add("createApp", name => {
cy.get("input[name=applicationName]")
.type(name)
.should("have.value", name)
cy.contains("Next").click()
cy.get("input[name=email]")
.click()
.type("test@test.com")
@ -57,6 +55,28 @@ Cypress.Commands.add("createApp", name => {
})
})
Cypress.Commands.add("deleteApp", name => {
cy.visit(`localhost:${Cypress.env("PORT")}/_builder`)
cy.get("body").then($body => {
cy.wait(1000)
if ($body.find(`[data-cy="app-${name}"]`).length) {
cy.get(`[data-cy="app-${name}"] a`).click()
cy.get("[data-cy=settings-icon]").click()
cy.get(".modal-content").within(() => {
cy.contains("Danger Zone").click()
cy.get("input").type("DELETE")
cy.contains("Delete Entire App").click()
})
}
})
})
Cypress.Commands.add("createTestApp", () => {
const appName = "Cypress Tests"
cy.deleteApp(appName)
cy.createApp(appName, "This app is used for Cypress testing.")
})
Cypress.Commands.add("createTestTableWithData", () => {
cy.createTable("dog")
cy.addColumn("dog", "name", "Text")
@ -87,6 +107,7 @@ Cypress.Commands.add("addColumn", (tableName, columnName, type) => {
cy.get("input")
.first()
.type(columnName)
// Unset table display column
cy.contains("display column").click()
cy.get("select").select(type)
@ -96,15 +117,12 @@ Cypress.Commands.add("addColumn", (tableName, columnName, type) => {
Cypress.Commands.add("addRow", values => {
cy.contains("Create New Row").click()
cy.get(".modal").within(() => {
for (let i = 0; i < values.length; i++) {
cy.get("input")
.eq(i)
.type(values[i])
}
// Save
cy.get(".buttons")
.contains("Create")
.click()
@ -114,9 +132,7 @@ Cypress.Commands.add("addRow", values => {
Cypress.Commands.add("createUser", (email, password, role) => {
// Create User
cy.contains("Users").click()
cy.contains("Create New User").click()
cy.get(".modal").within(() => {
cy.get("input")
.first()
@ -135,20 +151,29 @@ Cypress.Commands.add("createUser", (email, password, role) => {
})
})
Cypress.Commands.add("addHeadlineComponent", text => {
cy.get(".switcher > :nth-child(2)").click()
cy.get("[data-cy=Text]").click()
cy.get("[data-cy=Headline]").click()
cy.get(".tabs > :nth-child(2)").click()
cy.contains("Settings").click()
cy.get('input[name="text"]').type(text)
cy.contains("Design").click()
Cypress.Commands.add("addComponent", (category, component) => {
if (category) {
cy.get(`[data-cy="category-${category}"]`).click()
}
cy.get(`[data-cy="component-${component}"]`).click()
cy.wait(500)
cy.location().then(loc => {
const params = loc.pathname.split("/")
const componentId = params[params.length - 1]
cy.getComponent(componentId).should("exist")
return cy.wrap(componentId)
})
})
Cypress.Commands.add("addButtonComponent", () => {
cy.get(".switcher > :nth-child(2)").click()
cy.get("[data-cy=Button]").click()
Cypress.Commands.add("getComponent", componentId => {
return cy
.get("iframe")
.its("0.contentDocument")
.should("exist")
.its("body")
.should("not.be.null")
.then(cy.wrap)
.find(`[data-component-id=${componentId}]`)
})
Cypress.Commands.add("navigateToFrontend", () => {

View File

@ -14,8 +14,8 @@
"cy:run": "cypress run",
"cy:open": "cypress open",
"cy:run:ci": "cypress run --browser electron --record --key f308590b-6070-41af-b970-794a3823d451",
"cy:test": "start-server-and-test cy:setup http://localhost:4005/_builder cy:run",
"cy:ci": "start-server-and-test cy:setup http://localhost:4005/_builder cy:run:ci"
"cy:test": "start-server-and-test cy:setup http://localhost:4001/_builder cy:run",
"cy:ci": "start-server-and-test cy:setup http://localhost:4001/_builder cy:run:ci"
},
"jest": {
"globals": {

View File

@ -57,6 +57,7 @@
<div
bind:this={anchors[idx]}
class="category"
data-cy={item.isCategory ? `category-${item.name}` : `component-${item.name}`}
on:click={() => onItemChosen(item, idx)}
class:active={idx === selectedIndex}>
{#if item.icon}<i class={item.icon} />{/if}
@ -74,6 +75,7 @@
{#each enrichedStructure[selectedIndex].children as item}
{#if !item.showOnAsset || item.showOnAsset.includes($currentAssetName)}
<DropdownItem
data-cy={`component-${item.name}`}
icon={item.icon}
title={item.name}
on:click={() => onItemChosen(item)} />

View File

@ -69,7 +69,7 @@
}
</script>
<div class="property-control" bind:this={anchor}>
<div class="property-control" bind:this={anchor} data-cy={`setting-${key}`}>
<div class="label">{label}</div>
<div data-cy={`${key}-prop-control`} class="control">
<svelte:component

View File

@ -5,7 +5,10 @@
let modal
</script>
<div class="topnavitemright settings" on:click={modal.show}>
<div
class="topnavitemright settings"
data-cy="settings-icon"
on:click={modal.show}>
<i class="ri-settings-3-line" />
</div>
<Modal bind:this={modal} width="600px">

View File

@ -30,7 +30,7 @@
<div class="apps-card">
<Heading small black>{name}</Heading>
<Spacer medium />
<div class="card-footer">
<div class="card-footer" data-cy={`app-${name}`}>
<TextButton text medium blue href="/_builder/{_id}">
Open
{name}

View File

@ -52,7 +52,8 @@
"collectCoverageFrom": [
"src/**/*.js",
"!**/node_modules/**",
"!src/db/views/*.js"
"!src/db/views/*.js",
"!src/api/routes/tests"
],
"coverageReporters": [
"lcov",

View File

@ -8,14 +8,13 @@ const {
exports.fetch = async function(ctx) {
const database = new CouchDB(ctx.user.appId)
const datasources = (
ctx.body = (
await database.allDocs(
getDatasourceParams(null, {
include_docs: true,
})
)
).rows.map(row => row.doc)
ctx.body = datasources
}
exports.save = async function(ctx) {

View File

@ -1,25 +1,15 @@
const {
createApplication,
builderEndpointShouldBlockNormalUsers,
supertest,
clearApplications,
defaultHeaders,
} = require("./couchTestUtils")
const { clearAllApps, checkBuilderEndpoint } = require("./utilities/TestFunctions")
const setup = require("./utilities")
describe("/applications", () => {
let request
let server
let request = setup.getRequest()
let config = setup.getConfig()
beforeAll(async () => {
({ request, server } = await supertest())
});
afterAll(setup.afterAll)
beforeEach(async () => {
await clearApplications(request)
})
afterAll(() => {
server.close()
await clearAllApps()
await config.init()
})
describe("create", () => {
@ -27,7 +17,7 @@ describe("/applications", () => {
const res = await request
.post("/api/applications")
.send({ name: "My App" })
.set(defaultHeaders())
.set(config.defaultHeaders())
.expect('Content-Type', /json/)
.expect(200)
expect(res.res.statusMessage).toEqual("Application My App created successfully")
@ -35,41 +25,35 @@ describe("/applications", () => {
})
it("should apply authorization to endpoint", async () => {
const otherApplication = await createApplication(request)
const appId = otherApplication.instance._id
await builderEndpointShouldBlockNormalUsers({
request,
await checkBuilderEndpoint({
config,
method: "POST",
url: `/api/applications`,
appId: appId,
body: { name: "My App" }
})
})
})
describe("fetch", () => {
it("lists all applications", async () => {
await createApplication(request, "app1")
await createApplication(request, "app2")
await config.createApp(request, "app1")
await config.createApp(request, "app2")
const res = await request
.get("/api/applications")
.set(defaultHeaders())
.set(config.defaultHeaders())
.expect('Content-Type', /json/)
.expect(200)
expect(res.body.length).toBe(2)
// two created apps + the inited app
expect(res.body.length).toBe(3)
})
it("should apply authorization to endpoint", async () => {
const otherApplication = await createApplication(request)
const appId = otherApplication.instance._id
await builderEndpointShouldBlockNormalUsers({
request,
await checkBuilderEndpoint({
config,
method: "GET",
url: `/api/applications`,
appId: appId,
})
})
})

View File

@ -1,74 +1,33 @@
const {
createApplication,
createTable,
getAllFromTable,
defaultHeaders,
supertest,
insertDocument,
destroyDocument,
builderEndpointShouldBlockNormalUsers
} = require("./couchTestUtils")
let { generateAutomationID } = require("../../../db/utils")
const { delay } = require("./testUtils")
checkBuilderEndpoint,
getAllTableRows,
clearAllAutomations,
} = require("./utilities/TestFunctions")
const { basicAutomation } = require("./utilities/structures")
const setup = require("./utilities")
const MAX_RETRIES = 4
const AUTOMATION_ID = generateAutomationID()
const TEST_AUTOMATION = {
_id: AUTOMATION_ID,
name: "My Automation",
screenId: "kasdkfldsafkl",
live: true,
uiTree: {
},
definition: {
trigger: {},
steps: [
],
},
type: "automation",
}
let ACTION_DEFINITIONS = {}
let TRIGGER_DEFINITIONS = {}
let LOGIC_DEFINITIONS = {}
describe("/automations", () => {
let request
let server
let app
let appId
let request = setup.getRequest()
let config = setup.getConfig()
let automation
let automationId
beforeAll(async () => {
({ request, server } = await supertest())
})
afterAll(setup.afterAll)
beforeEach(async () => {
app = await createApplication(request)
appId = app.instance._id
if (automation) await destroyDocument(automation.id)
await config.init()
})
afterAll(() => {
server.close()
})
const createAutomation = async () => {
automation = await insertDocument(appId, {
type: "automation",
...TEST_AUTOMATION
})
automation = { ...automation, ...TEST_AUTOMATION }
}
const triggerWorkflow = async (automationId) => {
const triggerWorkflow = async automation => {
return await request
.post(`/api/automations/${automationId}/trigger`)
.post(`/api/automations/${automation._id}/trigger`)
.send({ name: "Test", description: "TEST" })
.set(defaultHeaders(appId))
.set(config.defaultHeaders())
.expect('Content-Type', /json/)
.expect(200)
}
@ -77,7 +36,7 @@ describe("/automations", () => {
it("returns a list of definitions for actions", async () => {
const res = await request
.get(`/api/automations/action/list`)
.set(defaultHeaders(appId))
.set(config.defaultHeaders())
.expect('Content-Type', /json/)
.expect(200)
@ -88,7 +47,7 @@ describe("/automations", () => {
it("returns a list of definitions for triggers", async () => {
const res = await request
.get(`/api/automations/trigger/list`)
.set(defaultHeaders(appId))
.set(config.defaultHeaders())
.expect('Content-Type', /json/)
.expect(200)
@ -99,7 +58,7 @@ describe("/automations", () => {
it("returns a list of definitions for actions", async () => {
const res = await request
.get(`/api/automations/logic/list`)
.set(defaultHeaders(appId))
.set(config.defaultHeaders())
.expect('Content-Type', /json/)
.expect(200)
@ -110,7 +69,7 @@ describe("/automations", () => {
it("returns all of the definitions in one", async () => {
const res = await request
.get(`/api/automations/definitions/list`)
.set(defaultHeaders(appId))
.set(config.defaultHeaders())
.expect('Content-Type', /json/)
.expect(200)
@ -121,6 +80,7 @@ describe("/automations", () => {
})
describe("create", () => {
const autoConfig = basicAutomation()
it("should setup the automation fully", () => {
let trigger = TRIGGER_DEFINITIONS["ROW_SAVED"]
trigger.id = "wadiawdo34"
@ -131,52 +91,51 @@ describe("/automations", () => {
}
createAction.id = "awde444wk"
TEST_AUTOMATION.definition.steps.push(createAction)
TEST_AUTOMATION.definition.trigger = trigger
autoConfig.definition.steps.push(createAction)
autoConfig.definition.trigger = trigger
})
it("returns a success message when the automation is successfully created", async () => {
const res = await request
.post(`/api/automations`)
.set(defaultHeaders(appId))
.send(TEST_AUTOMATION)
.set(config.defaultHeaders())
.send(autoConfig)
.expect('Content-Type', /json/)
.expect(200)
expect(res.body.message).toEqual("Automation created successfully")
expect(res.body.automation.name).toEqual("My Automation")
expect(res.body.automation._id).not.toEqual(null)
automationId = res.body.automation._id
automation = res.body.automation
})
it("should apply authorization to endpoint", async () => {
await builderEndpointShouldBlockNormalUsers({
request,
await checkBuilderEndpoint({
config,
method: "POST",
url: `/api/automations`,
appId: appId,
body: TEST_AUTOMATION
body: autoConfig
})
})
})
describe("trigger", () => {
it("trigger the automation successfully", async () => {
let table = await createTable(request, appId)
TEST_AUTOMATION.definition.trigger.inputs.tableId = table._id
TEST_AUTOMATION.definition.steps[0].inputs.row.tableId = table._id
await createAutomation()
await delay(500)
const res = await triggerWorkflow(automation._id)
let table = await config.createTable()
automation.definition.trigger.inputs.tableId = table._id
automation.definition.steps[0].inputs.row.tableId = table._id
automation = await config.createAutomation(automation)
await setup.delay(500)
const res = await triggerWorkflow(automation)
// this looks a bit mad but we don't actually have a way to wait for a response from the automation to
// know that it has finished all of its actions - this is currently the best way
// also when this runs in CI it is very temper-mental so for now trying to make run stable by repeating until it works
// TODO: update when workflow logs are a thing
for (let tries = 0; tries < MAX_RETRIES; tries++) {
expect(res.body.message).toEqual(`Automation ${automation._id} has been triggered.`)
expect(res.body.automation.name).toEqual(TEST_AUTOMATION.name)
await delay(500)
let elements = await getAllFromTable(request, appId, table._id)
expect(res.body.automation.name).toEqual(automation.name)
await setup.delay(500)
let elements = await getAllTableRows(config)
// don't test it unless there are values to test
if (elements.length > 1) {
expect(elements.length).toEqual(5)
@ -191,65 +150,63 @@ describe("/automations", () => {
describe("update", () => {
it("updates a automations data", async () => {
await createAutomation()
automation._id = automation.id
automation._rev = automation.rev
automation = await config.createAutomation(automation)
automation.name = "Updated Name"
automation.type = "automation"
const res = await request
.put(`/api/automations`)
.set(defaultHeaders(appId))
.set(config.defaultHeaders())
.send(automation)
.expect('Content-Type', /json/)
.expect(200)
expect(res.body.message).toEqual(`Automation ${AUTOMATION_ID} updated successfully.`)
expect(res.body.message).toEqual(`Automation ${automation._id} updated successfully.`)
expect(res.body.automation.name).toEqual("Updated Name")
})
})
describe("fetch", () => {
it("return all the automations for an instance", async () => {
await createAutomation()
await clearAllAutomations(config)
const autoConfig = basicAutomation()
automation = await config.createAutomation(autoConfig)
const res = await request
.get(`/api/automations`)
.set(defaultHeaders(appId))
.set(config.defaultHeaders())
.expect('Content-Type', /json/)
.expect(200)
expect(res.body[0]).toEqual(expect.objectContaining(TEST_AUTOMATION))
expect(res.body[0]).toEqual(expect.objectContaining(autoConfig))
})
it("should apply authorization to endpoint", async () => {
await builderEndpointShouldBlockNormalUsers({
request,
await checkBuilderEndpoint({
config,
method: "GET",
url: `/api/automations`,
appId: appId,
})
})
})
describe("destroy", () => {
it("deletes a automation by its ID", async () => {
await createAutomation()
const automation = await config.createAutomation()
const res = await request
.delete(`/api/automations/${automation.id}/${automation.rev}`)
.set(defaultHeaders(appId))
.set(config.defaultHeaders())
.expect('Content-Type', /json/)
.expect(200)
expect(res.body.id).toEqual(TEST_AUTOMATION._id)
expect(res.body.id).toEqual(automation._id)
})
it("should apply authorization to endpoint", async () => {
await createAutomation()
await builderEndpointShouldBlockNormalUsers({
request,
const automation = await config.createAutomation()
await checkBuilderEndpoint({
config,
method: "DELETE",
url: `/api/automations/${automation.id}/${automation._rev}`,
appId: appId,
})
})
})

View File

@ -1,326 +0,0 @@
const CouchDB = require("../../../db")
const supertest = require("supertest")
const { BUILTIN_ROLE_IDS } = require("../../../utilities/security/roles")
const packageJson = require("../../../../package")
const jwt = require("jsonwebtoken")
const env = require("../../../environment")
const {
BUILTIN_PERMISSION_IDS,
} = require("../../../utilities/security/permissions")
const TEST_CLIENT_ID = "test-client-id"
exports.TEST_CLIENT_ID = TEST_CLIENT_ID
exports.supertest = async () => {
let request
let server
env.PORT = 4002
server = require("../../../app")
request = supertest(server)
return { request, server }
}
exports.defaultHeaders = appId => {
const builderUser = {
userId: "BUILDER",
roleId: BUILTIN_ROLE_IDS.BUILDER,
}
const builderToken = jwt.sign(builderUser, env.JWT_SECRET)
const headers = {
Accept: "application/json",
Cookie: [`budibase:builder:local=${builderToken}`],
}
if (appId) {
headers["x-budibase-app-id"] = appId
}
return headers
}
exports.publicHeaders = appId => {
const headers = {
Accept: "application/json",
}
if (appId) {
headers["x-budibase-app-id"] = appId
}
return headers
}
exports.BASE_TABLE = {
name: "TestTable",
type: "table",
key: "name",
schema: {
name: {
type: "string",
constraints: {
type: "string",
},
},
description: {
type: "string",
constraints: {
type: "string",
},
},
},
}
exports.createTable = async (request, appId, table, removeId = true) => {
if (removeId && table != null && table._id) {
delete table._id
}
table = table || exports.BASE_TABLE
const res = await request
.post(`/api/tables`)
.set(exports.defaultHeaders(appId))
.send(table)
return res.body
}
exports.makeBasicRow = tableId => {
return {
name: "Test Contact",
description: "original description",
status: "new",
tableId: tableId,
}
}
exports.createRow = async (request, appId, tableId, row = null) => {
row = row || exports.makeBasicRow(tableId)
const res = await request
.post(`/api/${tableId}/rows`)
.send(row)
.set(exports.defaultHeaders(appId))
.expect("Content-Type", /json/)
.expect(200)
return res.body
}
exports.createRole = async (request, appId) => {
const roleBody = {
name: "NewRole",
inherits: BUILTIN_ROLE_IDS.BASIC,
permissionId: BUILTIN_PERMISSION_IDS.READ_ONLY,
}
const res = await request
.post(`/api/roles`)
.send(roleBody)
.set(exports.defaultHeaders(appId))
.expect("Content-Type", /json/)
.expect(200)
return res.body
}
exports.addPermission = async (
request,
appId,
role,
resource,
level = "read"
) => {
const res = await request
.post(`/api/permission/${role}/${resource}/${level}`)
.set(exports.defaultHeaders(appId))
.expect("Content-Type", /json/)
.expect(200)
return res.body
}
exports.createLinkedTable = async (request, appId) => {
// get the ID to link to
const table = await exports.createTable(request, appId)
table.primaryDisplay = "name"
table.schema.link = {
type: "link",
fieldName: "link",
tableId: table._id,
}
return exports.createTable(request, appId, table, false)
}
exports.createAttachmentTable = async (request, appId) => {
const table = await exports.createTable(request, appId)
table.schema.attachment = {
type: "attachment",
}
return exports.createTable(request, appId, table, false)
}
exports.getAllFromTable = async (request, appId, tableId) => {
const res = await request
.get(`/api/${tableId}/rows`)
.set(exports.defaultHeaders(appId))
return res.body
}
exports.createView = async (request, appId, tableId, view) => {
view = view || {
map: "function(doc) { emit(doc[doc.key], doc._id); } ",
tableId: tableId,
}
const res = await request
.post(`/api/views`)
.set(exports.defaultHeaders(appId))
.send(view)
return res.body
}
exports.createApplication = async (request, name = "test_application") => {
const res = await request
.post("/api/applications")
.send({
name,
})
.set(exports.defaultHeaders())
return res.body
}
exports.clearApplications = async request => {
const res = await request
.get("/api/applications")
.set(exports.defaultHeaders())
for (let app of res.body) {
const appId = app._id
await request
.delete(`/api/applications/${appId}`)
.set(exports.defaultHeaders(appId))
}
}
exports.createUser = async (
request,
appId,
email = "babs@babs.com",
password = "babs_password"
) => {
const res = await request
.post(`/api/users`)
.set(exports.defaultHeaders(appId))
.send({
name: "Bill",
email,
password,
roleId: BUILTIN_ROLE_IDS.POWER,
})
return res.body
}
const createUserWithRole = async (request, appId, roleId, email) => {
const password = `password_${email}`
await request
.post(`/api/users`)
.set(exports.defaultHeaders(appId))
.send({
email,
password,
roleId,
})
const anonUser = {
userId: "ANON",
roleId: BUILTIN_ROLE_IDS.PUBLIC,
appId: appId,
version: packageJson.version,
}
const anonToken = jwt.sign(anonUser, env.JWT_SECRET)
const loginResult = await request
.post(`/api/authenticate`)
.set({
Cookie: `budibase:${appId}:local=${anonToken}`,
"x-budibase-app-id": appId,
})
.send({ email, password })
// returning necessary request headers
return {
Accept: "application/json",
Cookie: loginResult.headers["set-cookie"],
}
}
exports.testPermissionsForEndpoint = async ({
request,
method,
url,
body,
appId,
passRole,
failRole,
}) => {
const passHeader = await createUserWithRole(
request,
appId,
passRole,
"passUser@budibase.com"
)
await createRequest(request, method, url, body)
.set(passHeader)
.expect(200)
const failHeader = await createUserWithRole(
request,
appId,
failRole,
"failUser@budibase.com"
)
await createRequest(request, method, url, body)
.set(failHeader)
.expect(403)
}
exports.builderEndpointShouldBlockNormalUsers = async ({
request,
method,
url,
body,
appId,
}) => {
const headers = await createUserWithRole(
request,
appId,
BUILTIN_ROLE_IDS.BASIC,
"basicUser@budibase.com"
)
await createRequest(request, method, url, body)
.set(headers)
.expect(403)
}
const createRequest = (request, method, url, body) => {
let req
if (method === "POST") req = request.post(url).send(body)
else if (method === "GET") req = request.get(url)
else if (method === "DELETE") req = request.delete(url)
else if (method === "PATCH") req = request.patch(url).send(body)
else if (method === "PUT") req = request.put(url).send(body)
return req
}
exports.insertDocument = async (databaseId, document) => {
const { id, ...documentFields } = document
return await new CouchDB(databaseId).put({ _id: id, ...documentFields })
}
exports.destroyDocument = async (databaseId, documentId) => {
return await new CouchDB(databaseId).destroy(documentId)
}
exports.getDocument = async (databaseId, documentId) => {
return await new CouchDB(databaseId).get(documentId)
}

View File

@ -1,67 +1,23 @@
const {
supertest,
createApplication,
defaultHeaders,
builderEndpointShouldBlockNormalUsers,
getDocument,
insertDocument
} = require("./couchTestUtils")
let { generateDatasourceID, generateQueryID } = require("../../../db/utils")
const DATASOURCE_ID = generateDatasourceID()
const TEST_DATASOURCE = {
_id: DATASOURCE_ID,
type: "datasource",
name: "Test",
source: "POSTGRES",
config: {},
type: "datasource",
}
const TEST_QUERY = {
_id: generateQueryID(DATASOURCE_ID),
datasourceId: DATASOURCE_ID,
name:"New Query",
parameters:[],
fields:{},
schema:{},
queryVerb:"read",
}
let { basicDatasource } = require("./utilities/structures")
let { checkBuilderEndpoint } = require("./utilities/TestFunctions")
let setup = require("./utilities")
describe("/datasources", () => {
let request
let server
let app
let appId
let datasource
let request = setup.getRequest()
let config = setup.getConfig()
beforeAll(async () => {
({ request, server } = await supertest())
});
afterAll(() => {
server.close()
})
afterAll(setup.afterAll)
beforeEach(async () => {
app = await createApplication(request)
appId = app.instance._id
});
async function createDatasource() {
return await insertDocument(appId, TEST_DATASOURCE)
}
async function createQuery() {
return await insertDocument(appId, TEST_QUERY)
}
await config.init()
})
describe("create", () => {
it("should create a new datasource", async () => {
const res = await request
.post(`/api/datasources`)
.send(TEST_DATASOURCE)
.set(defaultHeaders(appId))
.send(basicDatasource())
.set(config.defaultHeaders())
.expect('Content-Type', /json/)
.expect(200)
@ -74,7 +30,7 @@ describe("/datasources", () => {
let datasource
beforeEach(async () => {
datasource = await createDatasource()
datasource = await config.createDatasource()
});
afterEach(() => {
@ -84,34 +40,34 @@ describe("/datasources", () => {
it("returns all the datasources from the server", async () => {
const res = await request
.get(`/api/datasources`)
.set(defaultHeaders(appId))
.set(config.defaultHeaders())
.expect('Content-Type', /json/)
.expect(200)
const datasources = res.body;
const datasources = res.body
expect(datasources).toEqual([
{
"_id": datasources[0]._id,
"_rev": datasources[0]._rev,
...TEST_DATASOURCE
...basicDatasource()
}
]);
})
it("should apply authorization to endpoint", async () => {
await builderEndpointShouldBlockNormalUsers({
request,
await checkBuilderEndpoint({
config,
method: "GET",
url: `/api/datasources`,
appId: appId,
})
})
});
describe("destroy", () => {
let datasource;
let datasource
beforeEach(async () => {
datasource = await createDatasource()
datasource = await config.createDatasource()
});
afterEach(() => {
@ -119,16 +75,16 @@ describe("/datasources", () => {
});
it("deletes queries for the datasource after deletion and returns a success message", async () => {
await createQuery(datasource.id)
await config.createQuery()
await request
.delete(`/api/datasources/${datasource.id}/${datasource.rev}`)
.set(defaultHeaders(appId))
.delete(`/api/datasources/${datasource._id}/${datasource._rev}`)
.set(config.defaultHeaders())
.expect(200)
const res = await request
.get(`/api/datasources`)
.set(defaultHeaders(appId))
.set(config.defaultHeaders())
.expect('Content-Type', /json/)
.expect(200)
@ -136,11 +92,10 @@ describe("/datasources", () => {
})
it("should apply authorization to endpoint", async () => {
await builderEndpointShouldBlockNormalUsers({
request,
await checkBuilderEndpoint({
config,
method: "DELETE",
url: `/api/datasources/${datasource._id}/${datasource._rev}`,
appId: appId,
})
})

View File

@ -1,46 +1,30 @@
const {
createApplication,
createTable,
createRow,
supertest,
defaultHeaders,
addPermission,
publicHeaders,
makeBasicRow,
} = require("./couchTestUtils")
const { BUILTIN_ROLE_IDS } = require("../../../utilities/security/roles")
const setup = require("./utilities")
const { basicRow } = require("./utilities/structures")
const HIGHER_ROLE_ID = BUILTIN_ROLE_IDS.BASIC
const STD_ROLE_ID = BUILTIN_ROLE_IDS.PUBLIC
describe("/permission", () => {
let server
let request
let appId
let request = setup.getRequest()
let config = setup.getConfig()
let table
let perms
let row
beforeAll(async () => {
;({ request, server } = await supertest())
})
afterAll(() => {
server.close()
})
afterAll(setup.afterAll)
beforeEach(async () => {
let app = await createApplication(request)
appId = app.instance._id
table = await createTable(request, appId)
perms = await addPermission(request, appId, STD_ROLE_ID, table._id)
row = await createRow(request, appId, table._id)
await config.init()
table = await config.createTable()
row = await config.createRow()
perms = await config.addPermission(STD_ROLE_ID, table._id)
})
async function getTablePermissions() {
return request
.get(`/api/permission/${table._id}`)
.set(defaultHeaders(appId))
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
}
@ -49,7 +33,7 @@ describe("/permission", () => {
it("should be able to get levels", async () => {
const res = await request
.get(`/api/permission/levels`)
.set(defaultHeaders(appId))
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
expect(res.body).toBeDefined()
@ -68,7 +52,7 @@ describe("/permission", () => {
it("should get the resource permissions", async () => {
const res = await request
.get(`/api/permission/${table._id}`)
.set(defaultHeaders(appId))
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
expect(res.body["read"]).toEqual(STD_ROLE_ID)
@ -76,13 +60,13 @@ describe("/permission", () => {
})
it("should get resource permissions with multiple roles", async () => {
perms = await addPermission(request, appId, HIGHER_ROLE_ID, table._id, "write")
perms = await config.addPermission(HIGHER_ROLE_ID, table._id, "write")
const res = await getTablePermissions()
expect(res.body["read"]).toEqual(STD_ROLE_ID)
expect(res.body["write"]).toEqual(HIGHER_ROLE_ID)
const allRes = await request
.get(`/api/permission`)
.set(defaultHeaders(appId))
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
expect(allRes.body[table._id]["write"]).toEqual(HIGHER_ROLE_ID)
@ -94,7 +78,7 @@ describe("/permission", () => {
it("should be able to remove the permission", async () => {
const res = await request
.delete(`/api/permission/${STD_ROLE_ID}/${table._id}/read`)
.set(defaultHeaders(appId))
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
expect(res.body[0]._id).toEqual(STD_ROLE_ID)
@ -107,7 +91,7 @@ describe("/permission", () => {
it("should be able to read the row", async () => {
const res = await request
.get(`/api/${table._id}/rows`)
.set(publicHeaders(appId))
.set(config.publicHeaders())
.expect("Content-Type", /json/)
.expect(200)
expect(res.body[0]._id).toEqual(row._id)
@ -116,8 +100,8 @@ describe("/permission", () => {
it("shouldn't allow writing from a public user", async () => {
const res = await request
.post(`/api/${table._id}/rows`)
.send(makeBasicRow(table._id))
.set(publicHeaders(appId))
.send(basicRow(table._id))
.set(config.publicHeaders())
.expect("Content-Type", /json/)
.expect(403)
expect(res.status).toEqual(403)

View File

@ -1,77 +1,35 @@
const {
supertest,
createApplication,
defaultHeaders,
builderEndpointShouldBlockNormalUsers,
getDocument,
insertDocument,
} = require("./couchTestUtils")
let { generateDatasourceID, generateQueryID } = require("../../../db/utils")
const DATASOURCE_ID = generateDatasourceID()
const TEST_DATASOURCE = {
_id: DATASOURCE_ID,
type: "datasource",
name: "Test",
source: "POSTGRES",
config: {},
type: "datasource",
}
const TEST_QUERY = {
_id: generateQueryID(DATASOURCE_ID),
datasourceId: DATASOURCE_ID,
name: "New Query",
parameters: [],
fields: {},
schema: {},
queryVerb: "read",
}
const { checkBuilderEndpoint } = require("./utilities/TestFunctions")
const { basicQuery } = require("./utilities/structures")
const setup = require("./utilities")
describe("/queries", () => {
let request
let server
let app
let appId
let datasource
let query
let request = setup.getRequest()
let config = setup.getConfig()
beforeAll(async () => {
;({ request, server } = await supertest())
})
afterAll(() => {
server.close()
})
afterAll(setup.afterAll)
beforeEach(async () => {
app = await createApplication(request)
appId = app.instance._id
await config.init()
})
async function createDatasource() {
return await insertDocument(appId, TEST_DATASOURCE)
}
async function createQuery() {
return await insertDocument(appId, TEST_QUERY)
}
describe("create", () => {
it("should create a new query", async () => {
const { _id } = await config.createDatasource()
const query = basicQuery(_id)
const res = await request
.post(`/api/queries`)
.send(TEST_QUERY)
.set(defaultHeaders(appId))
.send(query)
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
expect(res.res.statusMessage).toEqual(
`Query ${TEST_QUERY.name} saved successfully.`
`Query ${query.name} saved successfully.`
)
expect(res.body).toEqual({
_rev: res.body._rev,
...TEST_QUERY,
_id: res.body._id,
...query,
})
})
})
@ -80,7 +38,7 @@ describe("/queries", () => {
let datasource
beforeEach(async () => {
datasource = await createDatasource()
datasource = await config.createDatasource()
})
afterEach(() => {
@ -88,29 +46,29 @@ describe("/queries", () => {
})
it("returns all the queries from the server", async () => {
const query = await createQuery()
const query = await config.createQuery()
const res = await request
.get(`/api/queries`)
.set(defaultHeaders(appId))
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
const queries = res.body
expect(queries).toEqual([
{
_rev: query.rev,
...TEST_QUERY,
_rev: query._rev,
_id: query._id,
...basicQuery(datasource._id),
readable: true,
},
])
})
it("should apply authorization to endpoint", async () => {
await builderEndpointShouldBlockNormalUsers({
request,
await checkBuilderEndpoint({
config,
method: "GET",
url: `/api/datasources`,
appId: appId,
})
})
})
@ -119,7 +77,7 @@ describe("/queries", () => {
let datasource
beforeEach(async () => {
datasource = await createDatasource()
datasource = await config.createDatasource()
})
afterEach(() => {
@ -127,16 +85,16 @@ describe("/queries", () => {
})
it("deletes a query and returns a success message", async () => {
const query = await createQuery()
const query = await config.createQuery()
await request
.delete(`/api/queries/${query.id}/${query.rev}`)
.set(defaultHeaders(appId))
.delete(`/api/queries/${query._id}/${query._rev}`)
.set(config.defaultHeaders())
.expect(200)
const res = await request
.get(`/api/queries`)
.set(defaultHeaders(appId))
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
@ -144,11 +102,10 @@ describe("/queries", () => {
})
it("should apply authorization to endpoint", async () => {
await builderEndpointShouldBlockNormalUsers({
request,
await checkBuilderEndpoint({
config,
method: "DELETE",
url: `/api/datasources/${datasource._id}/${datasource._rev}`,
appId: appId,
})
})
})

View File

@ -1,43 +1,26 @@
const {
createApplication,
supertest,
defaultHeaders,
} = require("./couchTestUtils")
const { BUILTIN_ROLE_IDS } = require("../../../utilities/security/roles")
const {
BUILTIN_PERMISSION_IDS,
} = require("../../../utilities/security/permissions")
const roleBody = {
name: "NewRole",
inherits: BUILTIN_ROLE_IDS.BASIC,
permissionId: BUILTIN_PERMISSION_IDS.READ_ONLY,
}
const { basicRole } = require("./utilities/structures")
const setup = require("./utilities")
describe("/roles", () => {
let server
let request
let appId
let request = setup.getRequest()
let config = setup.getConfig()
beforeAll(async () => {
;({ request, server } = await supertest())
})
afterAll(() => {
server.close()
})
afterAll(setup.afterAll)
beforeEach(async () => {
let app = await createApplication(request)
appId = app.instance._id
await config.init()
})
describe("create", () => {
it("returns a success message when role is successfully created", async () => {
const res = await request
.post(`/api/roles`)
.send(roleBody)
.set(defaultHeaders(appId))
.send(basicRole())
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
@ -53,8 +36,8 @@ describe("/roles", () => {
it("should list custom roles, plus 2 default roles", async () => {
const createRes = await request
.post(`/api/roles`)
.send(roleBody)
.set(defaultHeaders(appId))
.send(basicRole())
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
@ -62,7 +45,7 @@ describe("/roles", () => {
const res = await request
.get(`/api/roles`)
.set(defaultHeaders(appId))
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
@ -92,7 +75,7 @@ describe("/roles", () => {
const createRes = await request
.post(`/api/roles`)
.send({ name: "user", permissionId: BUILTIN_PERMISSION_IDS.READ_ONLY })
.set(defaultHeaders(appId))
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
@ -100,12 +83,12 @@ describe("/roles", () => {
await request
.delete(`/api/roles/${customRole._id}/${customRole._rev}`)
.set(defaultHeaders(appId))
.set(config.defaultHeaders())
.expect(200)
await request
.get(`/api/roles/${customRole._id}`)
.set(defaultHeaders(appId))
.set(config.defaultHeaders())
.expect(404)
})
})

View File

@ -1,66 +1,45 @@
const {
createApplication,
createTable,
supertest,
defaultHeaders,
createLinkedTable,
createAttachmentTable,
makeBasicRow,
} = require("./couchTestUtils");
const { outputProcessing } = require("../../../utilities/rowProcessor")
const env = require("../../../environment")
const { basicRow } = require("./utilities/structures")
const setup = require("./utilities")
describe("/rows", () => {
let request
let server
let appId
let request = setup.getRequest()
let config = setup.getConfig()
let table
let row
let app
beforeAll(async () => {
({ request, server } = await supertest())
});
afterAll(() => {
server.close()
})
afterAll(setup.afterAll)
beforeEach(async () => {
app = await createApplication(request)
appId = app.instance._id
table = await createTable(request, appId)
row = makeBasicRow(table._id)
await config.init()
table = await config.createTable()
row = basicRow(table._id)
})
const createRow = async r =>
await request
.post(`/api/${r ? r.tableId : row.tableId}/rows`)
.send(r || row)
.set(defaultHeaders(appId))
.expect('Content-Type', /json/)
.expect(200)
const loadRow = async id =>
await request
.get(`/api/${table._id}/rows/${id}`)
.set(defaultHeaders(appId))
.set(config.defaultHeaders())
.expect('Content-Type', /json/)
.expect(200)
describe("save, load, update, delete", () => {
it("returns a success message when the row is created", async () => {
const res = await createRow()
const res = await request
.post(`/api/${row.tableId}/rows`)
.send(row)
.set(config.defaultHeaders())
.expect('Content-Type', /json/)
.expect(200)
expect(res.res.statusMessage).toEqual(`${table.name} saved successfully`)
expect(res.body.name).toEqual("Test Contact")
expect(res.body._rev).toBeDefined()
})
it("updates a row successfully", async () => {
const rec = await createRow()
const existing = rec.body
const existing = await config.createRow()
const res = await request
.post(`/api/${table._id}/rows`)
@ -70,7 +49,7 @@ describe("/rows", () => {
tableId: table._id,
name: "Updated Name",
})
.set(defaultHeaders(appId))
.set(config.defaultHeaders())
.expect('Content-Type', /json/)
.expect(200)
@ -79,12 +58,11 @@ describe("/rows", () => {
})
it("should load a row", async () => {
const rec = await createRow()
const existing = rec.body
const existing = await config.createRow()
const res = await request
.get(`/api/${table._id}/rows/${existing._id}`)
.set(defaultHeaders(appId))
.set(config.defaultHeaders())
.expect('Content-Type', /json/)
.expect(200)
@ -102,12 +80,12 @@ describe("/rows", () => {
name: "Second Contact",
status: "new"
}
await createRow()
await createRow(newRow)
await config.createRow()
await config.createRow(newRow)
const res = await request
.get(`/api/${table._id}/rows`)
.set(defaultHeaders(appId))
.set(config.defaultHeaders())
.expect('Content-Type', /json/)
.expect(200)
@ -117,10 +95,10 @@ describe("/rows", () => {
})
it("load should return 404 when row does not exist", async () => {
await createRow()
await config.createRow()
await request
.get(`/api/${table._id}/rows/not-a-valid-id`)
.set(defaultHeaders(appId))
.set(config.defaultHeaders())
.expect('Content-Type', /json/)
.expect(404)
})
@ -132,7 +110,7 @@ describe("/rows", () => {
const number = {type:"number", constraints: { type: "number", presence: false }}
const datetime = {type:"datetime", constraints: { type: "string", presence: false, datetime: {earliest:"", latest: ""} }}
table = await createTable(request, appId, {
table = await config.createTable({
name: "TestTable2",
type: "table",
key: "name",
@ -187,7 +165,7 @@ describe("/rows", () => {
attachmentEmpty : "",
}
const id = (await createRow(row)).body._id
const id = (await config.createRow(row))._id
const saved = (await loadRow(id)).body
@ -217,8 +195,7 @@ describe("/rows", () => {
describe("patch", () => {
it("should update only the fields that are supplied", async () => {
const rec = await createRow()
const existing = rec.body
const existing = await config.createRow()
const res = await request
.patch(`/api/${table._id}/rows/${existing._id}`)
@ -228,7 +205,7 @@ describe("/rows", () => {
tableId: table._id,
name: "Updated Name",
})
.set(defaultHeaders(appId))
.set(config.defaultHeaders())
.expect('Content-Type', /json/)
.expect(200)
@ -249,7 +226,7 @@ describe("/rows", () => {
const result = await request
.post(`/api/${table._id}/rows/validate`)
.send({ name: "ivan" })
.set(defaultHeaders(appId))
.set(config.defaultHeaders())
.expect('Content-Type', /json/)
.expect(200)
@ -261,7 +238,7 @@ describe("/rows", () => {
const result = await request
.post(`/api/${table._id}/rows/validate`)
.send({ name: 1 })
.set(defaultHeaders(appId))
.set(config.defaultHeaders())
.expect('Content-Type', /json/)
.expect(200)
@ -273,19 +250,19 @@ describe("/rows", () => {
describe("enrich row unit test", () => {
it("should allow enriching some linked rows", async () => {
const table = await createLinkedTable(request, appId)
const firstRow = (await createRow({
const table = await config.createLinkedTable()
const firstRow = await config.createRow({
name: "Test Contact",
description: "original description",
tableId: table._id
})).body
const secondRow = (await createRow({
})
const secondRow = await config.createRow({
name: "Test 2",
description: "og desc",
link: [{_id: firstRow._id}],
tableId: table._id,
})).body
const enriched = await outputProcessing(appId, table, [secondRow])
})
const enriched = await outputProcessing(config.getAppId(), table, [secondRow])
expect(enriched[0].link.length).toBe(1)
expect(enriched[0].link[0]._id).toBe(firstRow._id)
expect(enriched[0].link[0].primaryDisplay).toBe("Test Contact")
@ -293,20 +270,20 @@ describe("/rows", () => {
})
it("should allow enriching attachment rows", async () => {
const table = await createAttachmentTable(request, appId)
const row = (await createRow({
const table = await config.createAttachmentTable()
const row = await config.createRow({
name: "test",
description: "test",
attachment: [{
url: "/test/thing",
}],
tableId: table._id,
})).body
})
// the environment needs configured for this
env.CLOUD = 1
env.SELF_HOSTED = 1
const enriched = await outputProcessing(appId, table, [row])
expect(enriched[0].attachment[0].url).toBe(`/app-assets/assets/${appId}/test/thing`)
const enriched = await outputProcessing(config.getAppId(), table, [row])
expect(enriched[0].attachment[0].url).toBe(`/app-assets/assets/${config.getAppId()}/test/thing`)
// remove env config
env.CLOUD = undefined
env.SELF_HOSTED = undefined

View File

@ -1,30 +1,15 @@
const {
createTable,
supertest,
createApplication,
defaultHeaders,
builderEndpointShouldBlockNormalUsers,
getDocument
} = require("./couchTestUtils")
const { checkBuilderEndpoint } = require("./utilities/TestFunctions")
const setup = require("./utilities")
describe("/tables", () => {
let request
let server
let app
let appId
let request = setup.getRequest()
let config = setup.getConfig()
beforeAll(async () => {
({ request, server } = await supertest())
});
afterAll(() => {
server.close()
})
afterAll(setup.afterAll)
beforeEach(async () => {
app = await createApplication(request)
appId = app.instance._id
});
await config.init()
})
describe("create", () => {
it("returns a success message when the table is successfully created", done => {
@ -37,25 +22,25 @@ describe("/tables", () => {
name: { type: "string" }
}
})
.set(defaultHeaders(appId))
.set(config.defaultHeaders())
.expect('Content-Type', /json/)
.expect(200)
.end(async (err, res) => {
expect(res.res.statusMessage).toEqual("Table TestTable saved successfully.");
expect(res.body.name).toEqual("TestTable");
done();
});
expect(res.res.statusMessage).toEqual("Table TestTable saved successfully.")
expect(res.body.name).toEqual("TestTable")
done()
})
})
it("renames all the row fields for a table when a schema key is renamed", async () => {
const testTable = await createTable(request, appId);
const testTable = await config.createTable()
const testRow = await request
.post(`/api/${testTable._id}/rows`)
.send({
name: "test"
})
.set(defaultHeaders(appId))
.set(config.defaultHeaders())
.expect('Content-Type', /json/)
.expect(200)
@ -74,29 +59,28 @@ describe("/tables", () => {
updatedName: { type: "string" }
}
})
.set(defaultHeaders(appId))
.set(config.defaultHeaders())
.expect('Content-Type', /json/)
.expect(200)
expect(updatedTable.res.statusMessage).toEqual("Table TestTable saved successfully.");
expect(updatedTable.body.name).toEqual("TestTable");
expect(updatedTable.res.statusMessage).toEqual("Table TestTable saved successfully.")
expect(updatedTable.body.name).toEqual("TestTable")
const res = await request
.get(`/api/${testTable._id}/rows/${testRow.body._id}`)
.set(defaultHeaders(appId))
.set(config.defaultHeaders())
.expect('Content-Type', /json/)
.expect(200)
expect(res.body.updatedName).toEqual("test");
expect(res.body.name).toBeUndefined();
});
expect(res.body.updatedName).toEqual("test")
expect(res.body.name).toBeUndefined()
})
it("should apply authorization to endpoint", async () => {
await builderEndpointShouldBlockNormalUsers({
request,
await checkBuilderEndpoint({
config,
method: "POST",
url: `/api/tables`,
appId: appId,
body: {
name: "TestTable",
key: "name",
@ -106,68 +90,67 @@ describe("/tables", () => {
}
})
})
});
})
describe("fetch", () => {
let testTable
beforeEach(async () => {
testTable = await createTable(request, appId, testTable)
});
testTable = await config.createTable(testTable)
})
afterEach(() => {
delete testTable._rev
});
})
it("returns all the tables for that instance in the response body", done => {
request
.get(`/api/tables`)
.set(defaultHeaders(appId))
.set(config.defaultHeaders())
.expect('Content-Type', /json/)
.expect(200)
.end(async (_, res) => {
const fetchedTable = res.body[0];
expect(fetchedTable.name).toEqual(testTable.name);
expect(fetchedTable.type).toEqual("table");
done();
});
const fetchedTable = res.body[0]
expect(fetchedTable.name).toEqual(testTable.name)
expect(fetchedTable.type).toEqual("table")
done()
})
})
it("should apply authorization to endpoint", async () => {
await builderEndpointShouldBlockNormalUsers({
request,
await checkBuilderEndpoint({
config,
method: "GET",
url: `/api/tables`,
appId: appId,
})
})
});
})
describe("destroy", () => {
let testTable;
let testTable
beforeEach(async () => {
testTable = await createTable(request, appId, testTable)
});
testTable = await config.createTable(testTable)
})
afterEach(() => {
delete testTable._rev
});
})
it("returns a success response when a table is deleted.", async done => {
request
.delete(`/api/tables/${testTable._id}/${testTable._rev}`)
.set(defaultHeaders(appId))
.set(config.defaultHeaders())
.expect('Content-Type', /json/)
.expect(200)
.end(async (_, res) => {
expect(res.res.statusMessage).toEqual(`Table ${testTable._id} deleted.`);
done();
});
expect(res.res.statusMessage).toEqual(`Table ${testTable._id} deleted.`)
done()
})
})
it("deletes linked references to the table after deletion", async done => {
const linkedTable = await createTable(request, appId, {
const linkedTable = await config.createTable({
name: "LinkedTable",
type: "table",
key: "name",
@ -190,25 +173,24 @@ describe("/tables", () => {
request
.delete(`/api/tables/${testTable._id}/${testTable._rev}`)
.set(defaultHeaders(appId))
.set(config.defaultHeaders())
.expect('Content-Type', /json/)
.expect(200)
.end(async (_, res) => {
expect(res.res.statusMessage).toEqual(`Table ${testTable._id} deleted.`);
const dependentTable = await getDocument(appId, linkedTable._id)
expect(dependentTable.schema.TestTable).not.toBeDefined();
done();
});
expect(res.res.statusMessage).toEqual(`Table ${testTable._id} deleted.`)
const dependentTable = await config.getTable(linkedTable._id)
expect(dependentTable.schema.TestTable).not.toBeDefined()
done()
})
})
it("should apply authorization to endpoint", async () => {
await builderEndpointShouldBlockNormalUsers({
request,
await checkBuilderEndpoint({
config,
method: "DELETE",
url: `/api/tables/${testTable._id}/${testTable._rev}`,
appId: appId,
})
})
});
});
})
})

View File

@ -1 +0,0 @@
module.exports.delay = ms => new Promise(resolve => setTimeout(resolve, ms))

View File

@ -1,46 +1,25 @@
const {
createApplication,
supertest,
defaultHeaders,
createUser,
testPermissionsForEndpoint,
} = require("./couchTestUtils")
const { BUILTIN_ROLE_IDS } = require("../../../utilities/security/roles")
const { cloneDeep } = require("lodash/fp")
const baseBody = {
email: "bill@bill.com",
password: "yeeooo",
roleId: BUILTIN_ROLE_IDS.POWER,
}
const { checkPermissionsEndpoint } = require("./utilities/TestFunctions")
const { basicUser } = require("./utilities/structures")
const setup = require("./utilities")
describe("/users", () => {
let request
let server
let app
let appId
let request = setup.getRequest()
let config = setup.getConfig()
beforeAll(async () => {
;({ request, server } = await supertest(server))
})
afterAll(setup.afterAll)
beforeEach(async () => {
app = await createApplication(request)
appId = app.instance._id
})
afterAll(() => {
server.close()
server.destroy()
await config.init()
})
describe("fetch", () => {
it("returns a list of users from an instance db", async () => {
await createUser(request, appId, "brenda@brenda.com", "brendas_password")
await createUser(request, appId, "pam@pam.com", "pam_password")
await config.createUser("brenda@brenda.com", "brendas_password")
await config.createUser("pam@pam.com", "pam_password")
const res = await request
.get(`/api/users`)
.set(defaultHeaders(appId))
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
@ -50,12 +29,12 @@ describe("/users", () => {
})
it("should apply authorization to endpoint", async () => {
await createUser(request, appId, "brenda@brenda.com", "brendas_password")
await testPermissionsForEndpoint({
await config.createUser("brenda@brenda.com", "brendas_password")
await checkPermissionsEndpoint({
config,
request,
method: "GET",
url: `/api/users`,
appId: appId,
passRole: BUILTIN_ROLE_IDS.ADMIN,
failRole: BUILTIN_ROLE_IDS.PUBLIC,
})
@ -64,11 +43,11 @@ describe("/users", () => {
describe("create", () => {
it("returns a success message when a user is successfully created", async () => {
const body = cloneDeep(baseBody)
const body = basicUser(BUILTIN_ROLE_IDS.POWER)
body.email = "bill@budibase.com"
const res = await request
.post(`/api/users`)
.set(defaultHeaders(appId))
.set(config.defaultHeaders())
.send(body)
.expect(200)
.expect("Content-Type", /json/)
@ -78,14 +57,13 @@ describe("/users", () => {
})
it("should apply authorization to endpoint", async () => {
const body = cloneDeep(baseBody)
const body = basicUser(BUILTIN_ROLE_IDS.POWER)
body.email = "brandNewUser@user.com"
await testPermissionsForEndpoint({
request,
await checkPermissionsEndpoint({
config,
method: "POST",
body,
url: `/api/users`,
appId: appId,
passRole: BUILTIN_ROLE_IDS.ADMIN,
failRole: BUILTIN_ROLE_IDS.PUBLIC,
})

View File

@ -0,0 +1,248 @@
const { BUILTIN_ROLE_IDS } = require("../../../../utilities/security/roles")
const jwt = require("jsonwebtoken")
const env = require("../../../../environment")
const {
basicTable,
basicRow,
basicRole,
basicAutomation,
basicDatasource,
basicQuery,
} = require("./structures")
const controllers = require("./controllers")
const supertest = require("supertest")
const EMAIL = "babs@babs.com"
const PASSWORD = "babs_password"
class TestConfiguration {
constructor() {
env.PORT = 4002
this.server = require("../../../../app")
// we need the request for logging in, involves cookies, hard to fake
this.request = supertest(this.server)
this.appId = null
}
getRequest() {
return this.request
}
getAppId() {
return this.appId
}
async _req(config, params, controlFunc) {
const request = {}
// fake cookies, we don't need them
request.cookies = { set: () => {}, get: () => {} }
request.config = { jwtSecret: env.JWT_SECRET }
request.appId = this.appId
request.user = { appId: this.appId }
request.request = {
body: config,
}
if (params) {
request.params = params
}
await controlFunc(request)
return request.body
}
async init(appName = "test_application") {
return this.createApp(appName)
}
end() {
this.server.close()
}
defaultHeaders() {
const builderUser = {
userId: "BUILDER",
roleId: BUILTIN_ROLE_IDS.BUILDER,
}
const builderToken = jwt.sign(builderUser, env.JWT_SECRET)
const headers = {
Accept: "application/json",
Cookie: [`budibase:builder:local=${builderToken}`],
}
if (this.appId) {
headers["x-budibase-app-id"] = this.appId
}
return headers
}
publicHeaders() {
const headers = {
Accept: "application/json",
}
if (this.appId) {
headers["x-budibase-app-id"] = this.appId
}
return headers
}
async createApp(appName) {
this.app = await this._req({ name: appName }, null, controllers.app.create)
this.appId = this.app._id
return this.app
}
async updateTable(config = null) {
config = config || basicTable()
this.table = await this._req(config, null, controllers.table.save)
return this.table
}
async createTable(config = null) {
if (config != null && config._id) {
delete config._id
}
return this.updateTable(config)
}
async getTable(tableId = null) {
tableId = tableId || this.table._id
return this._req(null, { id: tableId }, controllers.table.find)
}
async createLinkedTable() {
if (!this.table) {
throw "Must have created a table first."
}
const tableConfig = basicTable()
tableConfig.primaryDisplay = "name"
tableConfig.schema.link = {
type: "link",
fieldName: "link",
tableId: this.table._id,
}
const linkedTable = await this.createTable(tableConfig)
this.linkedTable = linkedTable
return linkedTable
}
async createAttachmentTable() {
const table = basicTable()
table.schema.attachment = {
type: "attachment",
}
return this.createTable(table)
}
async createRow(config = null) {
if (!this.table) {
throw "Test requires table to be configured."
}
config = config || basicRow(this.table._id)
return this._req(config, { tableId: this.table._id }, controllers.row.save)
}
async createRole(config = null) {
config = config || basicRole()
return this._req(config, null, controllers.role.save)
}
async addPermission(roleId, resourceId, level = "read") {
return this._req(
null,
{
roleId,
resourceId,
level,
},
controllers.perms.addPermission
)
}
async createView(config) {
if (!this.table) {
throw "Test requires table to be configured."
}
const view = config || {
map: "function(doc) { emit(doc[doc.key], doc._id); } ",
tableId: this.table._id,
}
return this._req(view, null, controllers.view.save)
}
async createAutomation(config) {
config = config || basicAutomation()
if (config._rev) {
delete config._rev
}
this.automation = (
await this._req(config, null, controllers.automation.create)
).automation
return this.automation
}
async getAllAutomations() {
return this._req(null, null, controllers.automation.fetch)
}
async deleteAutomation(automation = null) {
automation = automation || this.automation
if (!automation) {
return
}
return this._req(
null,
{ id: automation._id, rev: automation._rev },
controllers.automation.destroy
)
}
async createDatasource(config = null) {
config = config || basicDatasource()
this.datasource = await this._req(config, null, controllers.datasource.save)
return this.datasource
}
async createQuery(config = null) {
if (!this.datasource && !config) {
throw "No data source created for query."
}
config = config || basicQuery(this.datasource._id)
return this._req(config, null, controllers.query.save)
}
async createUser(
email = EMAIL,
password = PASSWORD,
roleId = BUILTIN_ROLE_IDS.POWER
) {
return this._req(
{
email,
password,
roleId,
},
null,
controllers.user.create
)
}
async login(email, password) {
if (!email || !password) {
await this.createUser()
email = EMAIL
password = PASSWORD
}
const result = await this.request
.post(`/api/authenticate`)
.set({
"x-budibase-app-id": this.appId,
})
.send({ email, password })
// returning necessary request headers
return {
Accept: "application/json",
Cookie: result.headers["set-cookie"],
}
}
}
module.exports = TestConfiguration

View File

@ -0,0 +1,79 @@
const rowController = require("../../../controllers/row")
const appController = require("../../../controllers/application")
function Request(appId, params) {
this.user = { appId }
this.params = params
}
exports.getAllTableRows = async config => {
const req = new Request(config.appId, { tableId: config.table._id })
await rowController.fetchTableRows(req)
return req.body
}
exports.clearAllApps = async () => {
const req = {}
await appController.fetch(req)
const apps = req.body
if (!apps || apps.length <= 0) {
return
}
for (let app of apps) {
const appId = app._id
await appController.delete(new Request(null, { appId }))
}
}
exports.clearAllAutomations = async config => {
const automations = await config.getAllAutomations()
for (let auto of automations) {
await config.deleteAutomation(auto)
}
}
exports.createRequest = (request, method, url, body) => {
let req
if (method === "POST") req = request.post(url).send(body)
else if (method === "GET") req = request.get(url)
else if (method === "DELETE") req = request.delete(url)
else if (method === "PATCH") req = request.patch(url).send(body)
else if (method === "PUT") req = request.put(url).send(body)
return req
}
exports.checkBuilderEndpoint = async ({ config, method, url, body }) => {
const headers = await config.login()
await exports
.createRequest(config.request, method, url, body)
.set(headers)
.expect(403)
}
exports.checkPermissionsEndpoint = async ({
config,
method,
url,
body,
passRole,
failRole,
}) => {
const password = "PASSWORD"
await config.createUser("passUser@budibase.com", password, passRole)
const passHeader = await config.login("passUser@budibase.com", password)
await exports
.createRequest(config.request, method, url, body)
.set(passHeader)
.expect(200)
await config.createUser("failUser@budibase.com", password, failRole)
const failHeader = await config.login("failUser@budibase.com", password)
await exports
.createRequest(config.request, method, url, body)
.set(failHeader)
.expect(403)
}

View File

@ -0,0 +1,12 @@
module.exports = {
table: require("../../../controllers/table"),
row: require("../../../controllers/row"),
role: require("../../../controllers/role"),
perms: require("../../../controllers/permission"),
view: require("../../../controllers/view"),
app: require("../../../controllers/application"),
user: require("../../../controllers/user"),
automation: require("../../../controllers/automation"),
datasource: require("../../../controllers/datasource"),
query: require("../../../controllers/query"),
}

View File

@ -0,0 +1,32 @@
const TestConfig = require("./TestConfiguration")
exports.delay = ms => new Promise(resolve => setTimeout(resolve, ms))
let request, config
exports.beforeAll = () => {
config = new TestConfig()
request = config.getRequest()
}
exports.afterAll = () => {
if (config) {
config.end()
}
request = null
config = null
}
exports.getRequest = () => {
if (!request) {
exports.beforeAll()
}
return request
}
exports.getConfig = () => {
if (!config) {
exports.beforeAll()
}
return config
}

View File

@ -0,0 +1,87 @@
const { BUILTIN_ROLE_IDS } = require("../../../../utilities/security/roles")
const {
BUILTIN_PERMISSION_IDS,
} = require("../../../../utilities/security/permissions")
exports.basicTable = () => {
return {
name: "TestTable",
type: "table",
key: "name",
schema: {
name: {
type: "string",
constraints: {
type: "string",
},
},
description: {
type: "string",
constraints: {
type: "string",
},
},
},
}
}
exports.basicAutomation = () => {
return {
name: "My Automation",
screenId: "kasdkfldsafkl",
live: true,
uiTree: {},
definition: {
trigger: {
inputs: {},
},
steps: [],
},
type: "automation",
}
}
exports.basicRow = tableId => {
return {
name: "Test Contact",
description: "original description",
status: "new",
tableId: tableId,
}
}
exports.basicRole = () => {
return {
name: "NewRole",
inherits: BUILTIN_ROLE_IDS.BASIC,
permissionId: BUILTIN_PERMISSION_IDS.READ_ONLY,
}
}
exports.basicDatasource = () => {
return {
type: "datasource",
name: "Test",
source: "POSTGRES",
config: {},
}
}
exports.basicQuery = datasourceId => {
return {
datasourceId: datasourceId,
name: "New Query",
parameters: [],
fields: {},
schema: {},
queryVerb: "read",
}
}
exports.basicUser = role => {
return {
email: "bill@bill.com",
password: "yeeooo",
roleId: role,
}
}

View File

@ -1,65 +1,56 @@
const {
createApplication,
createTable,
supertest,
defaultHeaders,
getDocument
} = require("./couchTestUtils")
const setup = require("./utilities")
describe("/views", () => {
let request
let server
let app
let appId
let request = setup.getRequest()
let config = setup.getConfig()
let table
const createView = async (config = {
name: "TestView",
field: "Price",
calculation: "stats",
tableId: table._id
}) =>
await request
.post(`/api/views`)
.send(config)
.set(defaultHeaders(appId))
.expect('Content-Type', /json/)
.expect(200)
const createRow = async row => request
.post(`/api/${table._id}/rows`)
.send(row)
.set(defaultHeaders(appId))
.expect('Content-Type', /json/)
.expect(200)
beforeAll(async () => {
({ request, server } = await supertest())
})
afterAll(setup.afterAll)
beforeEach(async () => {
app = await createApplication(request)
appId = app.instance._id
})
afterAll(() => {
server.close()
await config.init()
})
describe("create", () => {
beforeEach(async () => {
table = await createTable(request, appId);
table = await config.createTable()
})
it("returns a success message when the view is successfully created", async () => {
const res = await createView()
expect(res.res.statusMessage).toEqual("View TestView saved successfully.");
const res = await request
.post(`/api/views`)
.send({
name: "TestView",
field: "Price",
calculation: "stats",
tableId: table._id,
})
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
expect(res.res.statusMessage).toEqual(
"View TestView saved successfully."
)
})
it("updates the table row with the new view metadata", async () => {
const res = await createView()
expect(res.res.statusMessage).toEqual("View TestView saved successfully.");
const updatedTable = await getDocument(appId, table._id)
const res = await request
.post(`/api/views`)
.send({
name: "TestView",
field: "Price",
calculation: "stats",
tableId: table._id,
})
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
expect(res.res.statusMessage).toEqual(
"View TestView saved successfully."
)
const updatedTable = await config.getTable(table._id)
expect(updatedTable.views).toEqual({
TestView: {
field: "Price",
@ -88,88 +79,98 @@ describe("/views", () => {
field: {
type: "string",
},
}
}
});
},
},
})
})
});
})
describe("fetch", () => {
beforeEach(async () => {
table = await createTable(request, appId);
});
table = await config.createTable()
})
it("returns only custom views", async () => {
await createView()
await config.createView({
name: "TestView",
field: "Price",
calculation: "stats",
tableId: table._id,
})
const res = await request
.get(`/api/views`)
.set(defaultHeaders(appId))
.expect('Content-Type', /json/)
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
expect(res.body.length).toBe(1)
expect(res.body.find(({ name }) => name === "TestView")).toBeDefined()
})
});
})
describe("query", () => {
beforeEach(async () => {
table = await createTable(request, appId);
});
table = await config.createTable()
})
it("returns data for the created view", async () => {
await createView()
await createRow({
await config.createView({
name: "TestView",
field: "Price",
calculation: "stats",
tableId: table._id,
Price: 1000
})
await createRow({
await config.createRow({
tableId: table._id,
Price: 2000
Price: 1000,
})
await createRow({
await config.createRow({
tableId: table._id,
Price: 4000
Price: 2000,
})
await config.createRow({
tableId: table._id,
Price: 4000,
})
const res = await request
.get(`/api/views/TestView?calculation=stats`)
.set(defaultHeaders(appId))
.expect('Content-Type', /json/)
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
expect(res.body.length).toBe(1)
expect(res.body).toMatchSnapshot()
})
it("returns data for the created view using a group by", async () => {
await createView({
await config.createView({
calculation: "stats",
name: "TestView",
field: "Price",
groupBy: "Category",
tableId: table._id
tableId: table._id,
})
await createRow({
await config.createRow({
tableId: table._id,
Price: 1000,
Category: "One"
Category: "One",
})
await createRow({
await config.createRow({
tableId: table._id,
Price: 2000,
Category: "One"
Category: "One",
})
await createRow({
await config.createRow({
tableId: table._id,
Price: 4000,
Category: "Two"
Category: "Two",
})
const res = await request
.get(`/api/views/TestView?calculation=stats&group=Category`)
.set(defaultHeaders(appId))
.expect('Content-Type', /json/)
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
expect(res.body.length).toBe(2)
expect(res.body).toMatchSnapshot()
})
});
});
})
})

View File

@ -49,6 +49,12 @@ const TYPE_TRANSFORM_MAP = {
"": null,
[undefined]: undefined,
[null]: null,
parse: date => {
if (date instanceof Date) {
return date.toISOString()
}
return date
},
},
[FieldTypes.ATTACHMENT]: {
"": [],