Change tests to use internal apis
This commit is contained in:
parent
f8a3c12608
commit
bcfb0f3727
|
@ -2,7 +2,7 @@ import * as setup from "../../tests/utilities"
|
||||||
import { checkSlashesInUrl } from "../../../../utilities"
|
import { checkSlashesInUrl } from "../../../../utilities"
|
||||||
import supertest from "supertest"
|
import supertest from "supertest"
|
||||||
|
|
||||||
export type HttpMethod = "post" | "get" | "put" | "delete"
|
export type HttpMethod = "post" | "get" | "put" | "delete" | "patch"
|
||||||
|
|
||||||
export type MakeRequestResponse = (
|
export type MakeRequestResponse = (
|
||||||
method: HttpMethod,
|
method: HttpMethod,
|
||||||
|
@ -11,7 +11,10 @@ export type MakeRequestResponse = (
|
||||||
intAppId?: string
|
intAppId?: string
|
||||||
) => Promise<supertest.Response>
|
) => Promise<supertest.Response>
|
||||||
|
|
||||||
export function generateMakeRequest(apiKey: string): MakeRequestResponse {
|
export function generateMakeRequest(
|
||||||
|
apiKey: string,
|
||||||
|
isInternal = false
|
||||||
|
): MakeRequestResponse {
|
||||||
const request = setup.getRequest()!
|
const request = setup.getRequest()!
|
||||||
const config = setup.getConfig()
|
const config = setup.getConfig()
|
||||||
return async (
|
return async (
|
||||||
|
@ -26,9 +29,14 @@ export function generateMakeRequest(apiKey: string): MakeRequestResponse {
|
||||||
if (intAppId) {
|
if (intAppId) {
|
||||||
extraHeaders["x-budibase-app-id"] = intAppId
|
extraHeaders["x-budibase-app-id"] = intAppId
|
||||||
}
|
}
|
||||||
const req = request[method](
|
|
||||||
checkSlashesInUrl(`/api/public/v1/${endpoint}`)
|
const url = isInternal
|
||||||
).set(config.defaultHeaders(extraHeaders))
|
? endpoint
|
||||||
|
: checkSlashesInUrl(`/api/public/v1/${endpoint}`)
|
||||||
|
|
||||||
|
const req = request[method](url).set(
|
||||||
|
config.defaultHeaders(extraHeaders, isInternal)
|
||||||
|
)
|
||||||
if (body) {
|
if (body) {
|
||||||
req.send(body)
|
req.send(body)
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@ import {
|
||||||
} from "../api/routes/public/tests/utils"
|
} from "../api/routes/public/tests/utils"
|
||||||
|
|
||||||
import * as setup from "../api/routes/tests/utilities"
|
import * as setup from "../api/routes/tests/utilities"
|
||||||
import { Datasource, FieldType, SourceName, Table } from "@budibase/types"
|
import { Datasource, FieldType, Row, SourceName, Table } from "@budibase/types"
|
||||||
import _ from "lodash"
|
import _ from "lodash"
|
||||||
|
|
||||||
const config = setup.getConfig()
|
const config = setup.getConfig()
|
||||||
|
@ -133,14 +133,13 @@ describe("row api - postgres", () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("create a row", () => {
|
describe("create a row", () => {
|
||||||
|
const createRow = (tableId: string | undefined, body: object) =>
|
||||||
|
makeRequest("post", `/api/${tableId}/rows`, body)
|
||||||
|
|
||||||
test("Given than no row exists, adding a new row persists it", async () => {
|
test("Given than no row exists, adding a new row persists it", async () => {
|
||||||
const newRow = createRandomRow()
|
const newRow = createRandomRow()
|
||||||
|
|
||||||
const res = await makeRequest(
|
const res = await createRow(postgresTable._id, newRow)
|
||||||
"post",
|
|
||||||
`/tables/${postgresTable._id}/rows`,
|
|
||||||
newRow
|
|
||||||
)
|
|
||||||
|
|
||||||
expect(res.status).toBe(200)
|
expect(res.status).toBe(200)
|
||||||
|
|
||||||
|
@ -148,7 +147,7 @@ describe("row api - postgres", () => {
|
||||||
expect(persistedRows).toHaveLength(1)
|
expect(persistedRows).toHaveLength(1)
|
||||||
expect(persistedRows).toEqual([
|
expect(persistedRows).toEqual([
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
...res.body.data,
|
...res.body,
|
||||||
...newRow,
|
...newRow,
|
||||||
}),
|
}),
|
||||||
])
|
])
|
||||||
|
@ -159,11 +158,7 @@ describe("row api - postgres", () => {
|
||||||
const newRows = Array(numberOfRows).fill(createRandomRow())
|
const newRows = Array(numberOfRows).fill(createRandomRow())
|
||||||
|
|
||||||
for (const newRow of newRows) {
|
for (const newRow of newRows) {
|
||||||
const res = await makeRequest(
|
const res = await createRow(postgresTable._id, newRow)
|
||||||
"post",
|
|
||||||
`/tables/${postgresTable._id}/rows`,
|
|
||||||
newRow
|
|
||||||
)
|
|
||||||
expect(res.status).toBe(200)
|
expect(res.status).toBe(200)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -176,21 +171,21 @@ describe("row api - postgres", () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("update a row", () => {
|
describe("update a row", () => {
|
||||||
|
const updateRow = (tableId: string | undefined, body: Row) =>
|
||||||
|
makeRequest("patch", `/api/${tableId}/rows`, body)
|
||||||
|
|
||||||
test("Given than a row exists, updating it persists it", async () => {
|
test("Given than a row exists, updating it persists it", async () => {
|
||||||
let { rowData, row } = _.sample(await populateRows(10))!
|
let { row } = _.sample(await populateRows(10))!
|
||||||
|
|
||||||
const newName = faker.random.words(3)
|
const newName = faker.random.words(3)
|
||||||
const newValue = +faker.random.numeric()
|
const newValue = +faker.random.numeric()
|
||||||
const updateRow = {
|
const updatedRow = {
|
||||||
|
...row,
|
||||||
name: newName,
|
name: newName,
|
||||||
value: newValue,
|
value: newValue,
|
||||||
}
|
}
|
||||||
|
|
||||||
const res = await makeRequest(
|
const res = await updateRow(postgresTable._id, updatedRow)
|
||||||
"put",
|
|
||||||
`/tables/${postgresTable._id}/rows/${row._id}`,
|
|
||||||
updateRow
|
|
||||||
)
|
|
||||||
|
|
||||||
expect(res.status).toBe(200)
|
expect(res.status).toBe(200)
|
||||||
|
|
||||||
|
@ -198,23 +193,25 @@ describe("row api - postgres", () => {
|
||||||
|
|
||||||
expect(persistedRow).toEqual(
|
expect(persistedRow).toEqual(
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
...res.body.data,
|
_id: row._id,
|
||||||
...rowData,
|
name: newName,
|
||||||
...updateRow,
|
value: newValue,
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("delete a row", () => {
|
describe("delete a row", () => {
|
||||||
|
const deleteRow = (
|
||||||
|
tableId: string | undefined,
|
||||||
|
body: Row | { rows: Row[] }
|
||||||
|
) => makeRequest("delete", `/api/${tableId}/rows`, body)
|
||||||
|
|
||||||
test("Given than a row exists, delete request removes it", async () => {
|
test("Given than a row exists, delete request removes it", async () => {
|
||||||
const numberOfInitialRows = 5
|
const numberOfInitialRows = 5
|
||||||
let { row } = _.sample(await populateRows(numberOfInitialRows))!
|
let { row } = _.sample(await populateRows(numberOfInitialRows))!
|
||||||
|
|
||||||
const res = await makeRequest(
|
const res = await deleteRow(postgresTable._id, row)
|
||||||
"delete",
|
|
||||||
`/tables/${postgresTable._id}/rows/${row._id}`
|
|
||||||
)
|
|
||||||
|
|
||||||
expect(res.status).toBe(200)
|
expect(res.status).toBe(200)
|
||||||
|
|
||||||
|
@ -226,40 +223,39 @@ describe("row api - postgres", () => {
|
||||||
expect.objectContaining({ _id: row._id })
|
expect.objectContaining({ _id: row._id })
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// TODO: delete multiple rows
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("retrieve a row", () => {
|
describe("retrieve a row", () => {
|
||||||
|
const getRow = (tableId: string | undefined, rowId?: string | undefined) =>
|
||||||
|
makeRequest("get", `/api/${tableId}/rows/${rowId}`)
|
||||||
|
|
||||||
test("Given than a table have a single row, the row can be retrieved successfully", async () => {
|
test("Given than a table have a single row, the row can be retrieved successfully", async () => {
|
||||||
const [{ rowData, row }] = await populateRows(1)
|
const [{ rowData, row }] = await populateRows(1)
|
||||||
|
|
||||||
const res = await makeRequest(
|
const res = await getRow(postgresTable._id, row._id)
|
||||||
"get",
|
|
||||||
`/tables/${postgresTable._id}/rows/${row._id}`
|
|
||||||
)
|
|
||||||
|
|
||||||
expect(res.status).toBe(200)
|
expect(res.status).toBe(200)
|
||||||
|
|
||||||
expect(res.body.data).toEqual(expect.objectContaining(rowData))
|
expect(res.body).toEqual(expect.objectContaining(rowData))
|
||||||
})
|
})
|
||||||
|
|
||||||
test("Given than a table have a multiple rows, a single row can be retrieved successfully", async () => {
|
test("Given than a table have a multiple rows, a single row can be retrieved successfully", async () => {
|
||||||
const rows = await populateRows(10)
|
const rows = await populateRows(10)
|
||||||
const { rowData, row } = _.sample(rows)!
|
const { rowData, row } = _.sample(rows)!
|
||||||
|
|
||||||
const res = await makeRequest(
|
const res = await getRow(postgresTable._id, row._id)
|
||||||
"get",
|
|
||||||
`/tables/${postgresTable._id}/rows/${row._id}`
|
|
||||||
)
|
|
||||||
|
|
||||||
expect(res.status).toBe(200)
|
expect(res.status).toBe(200)
|
||||||
|
|
||||||
expect(res.body.data).toEqual(expect.objectContaining(rowData))
|
expect(res.body).toEqual(expect.objectContaining(rowData))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("search for rows", () => {
|
describe("search for rows", () => {
|
||||||
const search = (tableId: string | undefined, body?: object) =>
|
const search = (tableId: string | undefined, body?: object) =>
|
||||||
makeRequest("post", `/tables/${tableId}/rows/search`, body)
|
makeRequest("post", `/api/${tableId}/search`, body)
|
||||||
|
|
||||||
describe("empty search", () => {
|
describe("empty search", () => {
|
||||||
test("Given than a table has no rows, search without query returns empty", async () => {
|
test("Given than a table has no rows, search without query returns empty", async () => {
|
||||||
|
@ -267,7 +263,7 @@ describe("row api - postgres", () => {
|
||||||
|
|
||||||
expect(res.status).toBe(200)
|
expect(res.status).toBe(200)
|
||||||
|
|
||||||
expect(res.body.data).toHaveLength(0)
|
expect(res.body).toEqual({ rows: [] })
|
||||||
})
|
})
|
||||||
|
|
||||||
test("Given than a table has multiple rows, search without query returns all of them", async () => {
|
test("Given than a table has multiple rows, search without query returns all of them", async () => {
|
||||||
|
@ -278,12 +274,12 @@ describe("row api - postgres", () => {
|
||||||
|
|
||||||
expect(res.status).toBe(200)
|
expect(res.status).toBe(200)
|
||||||
|
|
||||||
expect(res.body.data).toHaveLength(rowsCount)
|
expect(res.body).toEqual({
|
||||||
expect(res.body.data).toEqual(
|
rows: expect.arrayContaining(
|
||||||
expect.arrayContaining(
|
|
||||||
rows.map(r => expect.objectContaining(r.rowData))
|
rows.map(r => expect.objectContaining(r.rowData))
|
||||||
)
|
),
|
||||||
)
|
})
|
||||||
|
expect(res.body.rows).toHaveLength(rowsCount)
|
||||||
})
|
})
|
||||||
|
|
||||||
test("Given than multiple tables have multiple rows, search only return the requested ones", async () => {
|
test("Given than multiple tables have multiple rows, search only return the requested ones", async () => {
|
||||||
|
@ -296,7 +292,7 @@ describe("row api - postgres", () => {
|
||||||
|
|
||||||
expect(res.status).toBe(200)
|
expect(res.status).toBe(200)
|
||||||
|
|
||||||
expect(res.body.data).toHaveLength(rowsCount)
|
expect(res.body.rows).toHaveLength(rowsCount)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -331,10 +327,10 @@ describe("row api - postgres", () => {
|
||||||
|
|
||||||
expect(res.status).toBe(200)
|
expect(res.status).toBe(200)
|
||||||
|
|
||||||
expect(res.body.data).toHaveLength(4)
|
expect(res.body).toEqual({
|
||||||
expect(res.body.data).toEqual(
|
rows: expect.arrayContaining(rowsToFilter.map(expect.objectContaining)),
|
||||||
expect.arrayContaining(rowsToFilter.map(expect.objectContaining))
|
})
|
||||||
)
|
expect(res.body.rows).toHaveLength(4)
|
||||||
})
|
})
|
||||||
|
|
||||||
test("Querying respects the limit fields", async () => {
|
test("Querying respects the limit fields", async () => {
|
||||||
|
@ -346,7 +342,7 @@ describe("row api - postgres", () => {
|
||||||
|
|
||||||
expect(res.status).toBe(200)
|
expect(res.status).toBe(200)
|
||||||
|
|
||||||
expect(res.body.data).toHaveLength(2)
|
expect(res.body.rows).toHaveLength(2)
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("sort", () => {
|
describe("sort", () => {
|
||||||
|
@ -377,15 +373,13 @@ describe("row api - postgres", () => {
|
||||||
|
|
||||||
test("Querying respects the sort order when sorting ascending by a string value", async () => {
|
test("Querying respects the sort order when sorting ascending by a string value", async () => {
|
||||||
const res = await search(postgresTable._id, {
|
const res = await search(postgresTable._id, {
|
||||||
sort: {
|
sort: "name",
|
||||||
order: "ascending",
|
sortOrder: "ascending",
|
||||||
column: "name",
|
sortType: "string",
|
||||||
type: "string",
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
|
|
||||||
expect(res.status).toBe(200)
|
expect(res.status).toBe(200)
|
||||||
expect(res.body.data).toEqual([
|
expect(res.body.rows).toEqual([
|
||||||
expect.objectContaining({ name: "aaa" }),
|
expect.objectContaining({ name: "aaa" }),
|
||||||
expect.objectContaining({ name: "bb" }),
|
expect.objectContaining({ name: "bb" }),
|
||||||
expect.objectContaining({ name: "ccccc" }),
|
expect.objectContaining({ name: "ccccc" }),
|
||||||
|
@ -395,15 +389,13 @@ describe("row api - postgres", () => {
|
||||||
|
|
||||||
test("Querying respects the sort order when sorting descending by a string value", async () => {
|
test("Querying respects the sort order when sorting descending by a string value", async () => {
|
||||||
const res = await search(postgresTable._id, {
|
const res = await search(postgresTable._id, {
|
||||||
sort: {
|
sort: "name",
|
||||||
order: "descending",
|
sortOrder: "descending",
|
||||||
column: "name",
|
sortType: "string",
|
||||||
type: "string",
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
|
|
||||||
expect(res.status).toBe(200)
|
expect(res.status).toBe(200)
|
||||||
expect(res.body.data).toEqual([
|
expect(res.body.rows).toEqual([
|
||||||
expect.objectContaining({ name: "d" }),
|
expect.objectContaining({ name: "d" }),
|
||||||
expect.objectContaining({ name: "ccccc" }),
|
expect.objectContaining({ name: "ccccc" }),
|
||||||
expect.objectContaining({ name: "bb" }),
|
expect.objectContaining({ name: "bb" }),
|
||||||
|
@ -413,15 +405,13 @@ describe("row api - postgres", () => {
|
||||||
|
|
||||||
test("Querying respects the sort order when sorting ascending by a numeric value", async () => {
|
test("Querying respects the sort order when sorting ascending by a numeric value", async () => {
|
||||||
const res = await search(postgresTable._id, {
|
const res = await search(postgresTable._id, {
|
||||||
sort: {
|
sort: "value",
|
||||||
order: "ascending",
|
sortOrder: "ascending",
|
||||||
column: "value",
|
sortType: "number",
|
||||||
type: "number",
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
|
|
||||||
expect(res.status).toBe(200)
|
expect(res.status).toBe(200)
|
||||||
expect(res.body.data).toEqual([
|
expect(res.body.rows).toEqual([
|
||||||
expect.objectContaining({ value: -5 }),
|
expect.objectContaining({ value: -5 }),
|
||||||
expect.objectContaining({ value: 0 }),
|
expect.objectContaining({ value: 0 }),
|
||||||
expect.objectContaining({ value: 3 }),
|
expect.objectContaining({ value: 3 }),
|
||||||
|
@ -431,15 +421,13 @@ describe("row api - postgres", () => {
|
||||||
|
|
||||||
test("Querying respects the sort order when sorting descending by a numeric value", async () => {
|
test("Querying respects the sort order when sorting descending by a numeric value", async () => {
|
||||||
const res = await search(postgresTable._id, {
|
const res = await search(postgresTable._id, {
|
||||||
sort: {
|
sort: "value",
|
||||||
order: "descending",
|
sortOrder: "descending",
|
||||||
column: "value",
|
sortType: "number",
|
||||||
type: "number",
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
|
|
||||||
expect(res.status).toBe(200)
|
expect(res.status).toBe(200)
|
||||||
expect(res.body.data).toEqual([
|
expect(res.body.rows).toEqual([
|
||||||
expect.objectContaining({ value: 40 }),
|
expect.objectContaining({ value: 40 }),
|
||||||
expect.objectContaining({ value: 3 }),
|
expect.objectContaining({ value: 3 }),
|
||||||
expect.objectContaining({ value: 0 }),
|
expect.objectContaining({ value: 0 }),
|
||||||
|
|
|
@ -300,7 +300,7 @@ class TestConfiguration {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
defaultHeaders(extras = {}) {
|
defaultHeaders(extras = {}, isInternal: boolean) {
|
||||||
const authObj = {
|
const authObj = {
|
||||||
userId: GLOBAL_USER_ID,
|
userId: GLOBAL_USER_ID,
|
||||||
sessionId: "sessionid",
|
sessionId: "sessionid",
|
||||||
|
@ -314,13 +314,17 @@ class TestConfiguration {
|
||||||
const appToken = auth.jwt.sign(app, env.JWT_SECRET)
|
const appToken = auth.jwt.sign(app, env.JWT_SECRET)
|
||||||
const headers: any = {
|
const headers: any = {
|
||||||
Accept: "application/json",
|
Accept: "application/json",
|
||||||
Cookie: [
|
|
||||||
`${constants.Cookie.Auth}=${authToken}`,
|
|
||||||
`${constants.Cookie.CurrentApp}=${appToken}`,
|
|
||||||
],
|
|
||||||
[constants.Header.CSRF_TOKEN]: CSRF_TOKEN,
|
[constants.Header.CSRF_TOKEN]: CSRF_TOKEN,
|
||||||
...extras,
|
...extras,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!isInternal) {
|
||||||
|
headers.Cookie = [
|
||||||
|
`${constants.Cookie.Auth}=${authToken}`,
|
||||||
|
`${constants.Cookie.CurrentApp}=${appToken}`,
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
if (this.appId) {
|
if (this.appId) {
|
||||||
headers[constants.Header.APP_ID] = this.appId
|
headers[constants.Header.APP_ID] = this.appId
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue