Current User binding and tests.
This commit is contained in:
parent
ee0c4187c8
commit
18acaccfcb
|
@ -622,6 +622,48 @@ describe.each([
|
|||
})
|
||||
})
|
||||
|
||||
describe("date column", () => {
|
||||
it("creates a row with a default value successfully", async () => {
|
||||
const table = await config.api.table.save(
|
||||
saveTableRequest({
|
||||
schema: {
|
||||
date: {
|
||||
name: "date",
|
||||
type: FieldType.DATETIME,
|
||||
default: "2023-01-26T11:48:57.000Z",
|
||||
},
|
||||
},
|
||||
})
|
||||
)
|
||||
const row = await config.api.row.save(table._id!, {})
|
||||
expect(row.date).toEqual("2023-01-26T11:48:57.000Z")
|
||||
})
|
||||
|
||||
it("gives an error if the default value is invalid", async () => {
|
||||
const table = await config.api.table.save(
|
||||
saveTableRequest({
|
||||
schema: {
|
||||
date: {
|
||||
name: "date",
|
||||
type: FieldType.DATETIME,
|
||||
default: "invalid",
|
||||
},
|
||||
},
|
||||
})
|
||||
)
|
||||
await config.api.row.save(
|
||||
table._id!,
|
||||
{},
|
||||
{
|
||||
status: 400,
|
||||
body: {
|
||||
message: `Invalid default value for field 'date' - Invalid date value: "invalid"`,
|
||||
},
|
||||
}
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("bindings", () => {
|
||||
describe("string column", () => {
|
||||
beforeAll(async () => {
|
||||
|
@ -651,6 +693,40 @@ describe.each([
|
|||
})
|
||||
expect(row.description).toEqual("specified description")
|
||||
})
|
||||
|
||||
it("can bind the current user", async () => {
|
||||
const table = await config.api.table.save(
|
||||
saveTableRequest({
|
||||
schema: {
|
||||
user: {
|
||||
name: "user",
|
||||
type: FieldType.STRING,
|
||||
default: `{{ [Current User]._id }}`,
|
||||
},
|
||||
},
|
||||
})
|
||||
)
|
||||
const row = await config.api.row.save(table._id!, {})
|
||||
expect(row.user).toEqual(config.getUser()._id)
|
||||
})
|
||||
|
||||
it("cannot access current user password", async () => {
|
||||
const table = await config.api.table.save(
|
||||
saveTableRequest({
|
||||
schema: {
|
||||
user: {
|
||||
name: "user",
|
||||
type: FieldType.STRING,
|
||||
default: `{{ user.password }}`,
|
||||
},
|
||||
},
|
||||
})
|
||||
)
|
||||
const row = await config.api.row.save(table._id!, {})
|
||||
// For some reason it's null for internal tables, and undefined for
|
||||
// external.
|
||||
expect(row.user == null).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe("number column", () => {
|
||||
|
|
|
@ -1,14 +1,22 @@
|
|||
import * as linkRows from "../../db/linkedRows"
|
||||
import { fixAutoColumnSubType, processFormulas } from "./utils"
|
||||
import { HTTPError, objectStore, utils } from "@budibase/backend-core"
|
||||
import {
|
||||
cache,
|
||||
context,
|
||||
HTTPError,
|
||||
objectStore,
|
||||
utils,
|
||||
} from "@budibase/backend-core"
|
||||
import { InternalTables } from "../../db/utils"
|
||||
import { TYPE_TRANSFORM_MAP } from "./map"
|
||||
import {
|
||||
AutoFieldSubType,
|
||||
FieldType,
|
||||
IdentityType,
|
||||
Row,
|
||||
RowAttachment,
|
||||
Table,
|
||||
User,
|
||||
} from "@budibase/types"
|
||||
import { cloneDeep } from "lodash/fp"
|
||||
import {
|
||||
|
@ -92,9 +100,22 @@ export async function processAutoColumn(
|
|||
}
|
||||
|
||||
async function processDeafultValues(table: Table, row: Row) {
|
||||
const ctx: { ["Current User"]?: User; user?: User } = {}
|
||||
|
||||
const identity = context.getIdentity()
|
||||
if (identity) {
|
||||
if (identity._id && identity.type === IdentityType.USER) {
|
||||
const user = await cache.user.getUser(identity._id)
|
||||
delete user.password
|
||||
|
||||
ctx["Current User"] = user
|
||||
ctx.user = user
|
||||
}
|
||||
}
|
||||
|
||||
for (let [key, schema] of Object.entries(table.schema)) {
|
||||
if ("default" in schema && schema.default != null && row[key] == null) {
|
||||
const processed = await processString(schema.default, {})
|
||||
const processed = await processString(schema.default, ctx)
|
||||
|
||||
try {
|
||||
row[key] = coerce(processed, schema.type)
|
||||
|
|
|
@ -115,8 +115,13 @@ export const TYPE_TRANSFORM_MAP: any = {
|
|||
parse: (date: any) => {
|
||||
if (date instanceof Date) {
|
||||
return date.toISOString()
|
||||
} else {
|
||||
const parsed = new Date(date)
|
||||
if (isNaN(parsed.getTime())) {
|
||||
throw new Error(`Invalid date value: "${date}"`)
|
||||
}
|
||||
return date
|
||||
}
|
||||
return date
|
||||
},
|
||||
},
|
||||
[FieldType.ATTACHMENTS]: {
|
||||
|
|
Loading…
Reference in New Issue