@budibase/server 2.4.42-alpha.6 → 2.4.42-alpha.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@budibase/server",
3
3
  "email": "hi@budibase.com",
4
- "version": "2.4.42-alpha.6",
4
+ "version": "2.4.42-alpha.8",
5
5
  "description": "Budibase Web Server",
6
6
  "main": "src/index.ts",
7
7
  "repository": {
@@ -44,12 +44,12 @@
44
44
  "license": "GPL-3.0",
45
45
  "dependencies": {
46
46
  "@apidevtools/swagger-parser": "10.0.3",
47
- "@budibase/backend-core": "2.4.42-alpha.6",
48
- "@budibase/client": "2.4.42-alpha.6",
49
- "@budibase/pro": "2.4.42-alpha.5",
50
- "@budibase/shared-core": "2.4.42-alpha.6",
51
- "@budibase/string-templates": "2.4.42-alpha.6",
52
- "@budibase/types": "2.4.42-alpha.6",
47
+ "@budibase/backend-core": "2.4.42-alpha.8",
48
+ "@budibase/client": "2.4.42-alpha.8",
49
+ "@budibase/pro": "2.4.42-alpha.7",
50
+ "@budibase/shared-core": "2.4.42-alpha.8",
51
+ "@budibase/string-templates": "2.4.42-alpha.8",
52
+ "@budibase/types": "2.4.42-alpha.8",
53
53
  "@bull-board/api": "3.7.0",
54
54
  "@bull-board/koa": "3.9.4",
55
55
  "@elastic/elasticsearch": "7.10.0",
@@ -176,5 +176,5 @@
176
176
  "optionalDependencies": {
177
177
  "oracledb": "5.3.0"
178
178
  },
179
- "gitHead": "bb8100783e278b68332ae7e3f927947e4c5ebc14"
179
+ "gitHead": "41669d5513e4c0c199aae0d1d85bdca3ecceaaf6"
180
180
  }
@@ -0,0 +1,97 @@
1
+ import { FieldType } from "@budibase/types"
2
+ import { AutoFieldSubTypes } from "../../../../constants"
3
+ import TestConfiguration from "../../../../tests/utilities/TestConfiguration"
4
+ import { importToRows } from "../utils"
5
+
6
+ describe("utils", () => {
7
+ const config = new TestConfiguration()
8
+
9
+ beforeEach(async () => {
10
+ await config.init()
11
+ })
12
+
13
+ afterAll(config.end)
14
+
15
+ describe("importToRows", () => {
16
+ it("consecutive row have consecutive auto ids", async () => {
17
+ await config.doInContext(config.appId, async () => {
18
+ const table = await config.createTable({
19
+ name: "table",
20
+ type: "table",
21
+ schema: {
22
+ autoId: {
23
+ name: "autoId",
24
+ type: FieldType.NUMBER,
25
+ subtype: AutoFieldSubTypes.AUTO_ID,
26
+ autocolumn: true,
27
+ constraints: {
28
+ type: FieldType.NUMBER,
29
+ presence: true,
30
+ },
31
+ },
32
+ name: {
33
+ name: "name",
34
+ type: FieldType.STRING,
35
+ constraints: {
36
+ type: FieldType.STRING,
37
+ presence: true,
38
+ },
39
+ },
40
+ },
41
+ })
42
+
43
+ const data = [{ name: "Alice" }, { name: "Bob" }, { name: "Claire" }]
44
+
45
+ const result = importToRows(data, table, config.user)
46
+ expect(result).toEqual([
47
+ expect.objectContaining({
48
+ autoId: 1,
49
+ name: "Alice",
50
+ }),
51
+ expect.objectContaining({
52
+ autoId: 2,
53
+ name: "Bob",
54
+ }),
55
+ expect.objectContaining({
56
+ autoId: 3,
57
+ name: "Claire",
58
+ }),
59
+ ])
60
+ })
61
+ })
62
+
63
+ it("can import data without a specific user performing the action", async () => {
64
+ await config.doInContext(config.appId, async () => {
65
+ const table = await config.createTable({
66
+ name: "table",
67
+ type: "table",
68
+ schema: {
69
+ autoId: {
70
+ name: "autoId",
71
+ type: FieldType.NUMBER,
72
+ subtype: AutoFieldSubTypes.AUTO_ID,
73
+ autocolumn: true,
74
+ constraints: {
75
+ type: FieldType.NUMBER,
76
+ presence: true,
77
+ },
78
+ },
79
+ name: {
80
+ name: "name",
81
+ type: FieldType.STRING,
82
+ constraints: {
83
+ type: FieldType.STRING,
84
+ presence: true,
85
+ },
86
+ },
87
+ },
88
+ })
89
+
90
+ const data = [{ name: "Alice" }, { name: "Bob" }, { name: "Claire" }]
91
+
92
+ const result = importToRows(data, table)
93
+ expect(result).toHaveLength(3)
94
+ })
95
+ })
96
+ })
97
+ })
@@ -20,7 +20,13 @@ import viewTemplate from "../view/viewBuilder"
20
20
  import { cloneDeep } from "lodash/fp"
21
21
  import { quotas } from "@budibase/pro"
22
22
  import { events, context } from "@budibase/backend-core"
23
- import { Database, Datasource, SourceName, Table } from "@budibase/types"
23
+ import {
24
+ ContextUser,
25
+ Database,
26
+ Datasource,
27
+ SourceName,
28
+ Table,
29
+ } from "@budibase/types"
24
30
 
25
31
  export async function clearColumns(table: any, columnNames: any) {
26
32
  const db: Database = context.getAppDB()
@@ -99,32 +105,35 @@ export function makeSureTableUpToDate(table: any, tableToSave: any) {
99
105
  return tableToSave
100
106
  }
101
107
 
102
- export function importToRows(data: any, table: any, user: any = {}) {
108
+ export function importToRows(
109
+ data: any[],
110
+ table: Table,
111
+ user: ContextUser | null = null
112
+ ) {
103
113
  let finalData: any = []
104
114
  for (let i = 0; i < data.length; i++) {
105
115
  let row = data[i]
106
- row._id = generateRowID(table._id)
116
+ row._id = generateRowID(table._id!)
107
117
  row.tableId = table._id
108
- const processed: any = inputProcessing(user, table, row, {
118
+ const processed = inputProcessing(user, table, row, {
109
119
  noAutoRelationships: true,
110
120
  })
111
121
  row = processed.row
122
+ table = processed.table
112
123
 
113
- let fieldName: any
114
- let schema: any
115
- for ([fieldName, schema] of Object.entries(table.schema)) {
124
+ for (const [fieldName, schema] of Object.entries(table.schema)) {
116
125
  // check whether the options need to be updated for inclusion as part of the data import
117
126
  if (
118
127
  schema.type === FieldTypes.OPTIONS &&
119
128
  row[fieldName] &&
120
- (!schema.constraints.inclusion ||
121
- schema.constraints.inclusion.indexOf(row[fieldName]) === -1)
129
+ (!schema.constraints!.inclusion ||
130
+ schema.constraints!.inclusion.indexOf(row[fieldName]) === -1)
122
131
  ) {
123
- schema.constraints.inclusion = [
124
- ...schema.constraints.inclusion,
132
+ schema.constraints!.inclusion = [
133
+ ...schema.constraints!.inclusion!,
125
134
  row[fieldName],
126
135
  ]
127
- schema.constraints.inclusion.sort()
136
+ schema.constraints!.inclusion.sort()
128
137
  }
129
138
  }
130
139
 
@@ -34,7 +34,7 @@ function syncLastIds(table: Table, rowCount: number) {
34
34
  })
35
35
  }
36
36
 
37
- function tableImport(table: Table, data: Row) {
37
+ function tableImport(table: Table, data: Row[]) {
38
38
  const cloneTable = cloneDeep(table)
39
39
  const rowDocs = importToRows(data, cloneTable)
40
40
  syncLastIds(cloneTable, rowDocs.length)
@@ -245,6 +245,10 @@ class GoogleSheetsIntegration implements DatasourcePlus {
245
245
  }
246
246
 
247
247
  async buildSchema(datasourceId: string, entities: Record<string, Table>) {
248
+ // not fully configured yet
249
+ if (!this.config.auth) {
250
+ return
251
+ }
248
252
  await this.connect()
249
253
  const sheets = this.client.sheetsByIndex
250
254
  const tables: Record<string, Table> = {}
@@ -0,0 +1,159 @@
1
+ import { db, roles } from "@budibase/backend-core"
2
+ import { structures } from "@budibase/backend-core/tests"
3
+ import { sdk as proSdk } from "@budibase/pro"
4
+
5
+ import TestConfiguration from "../../../tests/utilities/TestConfiguration"
6
+ import { rawUserMetadata, syncGlobalUsers } from "../utils"
7
+
8
+ describe("syncGlobalUsers", () => {
9
+ const config = new TestConfiguration()
10
+
11
+ beforeEach(async () => {
12
+ await config.init()
13
+ })
14
+
15
+ afterAll(config.end)
16
+
17
+ it("the default user is synced", async () => {
18
+ await config.doInContext(config.appId, async () => {
19
+ await syncGlobalUsers()
20
+
21
+ const metadata = await rawUserMetadata()
22
+ expect(metadata).toHaveLength(1)
23
+ expect(metadata).toEqual([
24
+ expect.objectContaining({
25
+ _id: db.generateUserMetadataID(config.user._id),
26
+ }),
27
+ ])
28
+ })
29
+ })
30
+
31
+ it("admin and builders users are synced", async () => {
32
+ const user1 = await config.createUser({ admin: true })
33
+ const user2 = await config.createUser({ admin: false, builder: true })
34
+ await config.doInContext(config.appId, async () => {
35
+ expect(await rawUserMetadata()).toHaveLength(1)
36
+ await syncGlobalUsers()
37
+
38
+ const metadata = await rawUserMetadata()
39
+ expect(metadata).toHaveLength(3)
40
+ expect(metadata).toContainEqual(
41
+ expect.objectContaining({
42
+ _id: db.generateUserMetadataID(user1._id),
43
+ })
44
+ )
45
+ expect(metadata).toContainEqual(
46
+ expect.objectContaining({
47
+ _id: db.generateUserMetadataID(user2._id),
48
+ })
49
+ )
50
+ })
51
+ })
52
+
53
+ it("app users are not synced if not specified", async () => {
54
+ const user = await config.createUser({ admin: false, builder: false })
55
+ await config.doInContext(config.appId, async () => {
56
+ await syncGlobalUsers()
57
+
58
+ const metadata = await rawUserMetadata()
59
+ expect(metadata).toHaveLength(1)
60
+ expect(metadata).not.toContainEqual(
61
+ expect.objectContaining({
62
+ _id: db.generateUserMetadataID(user._id),
63
+ })
64
+ )
65
+ })
66
+ })
67
+
68
+ it("app users are added when group is assigned to app", async () => {
69
+ await config.doInTenant(async () => {
70
+ const group = await proSdk.groups.save(structures.userGroups.userGroup())
71
+ const user1 = await config.createUser({ admin: false, builder: false })
72
+ const user2 = await config.createUser({ admin: false, builder: false })
73
+ await proSdk.groups.addUsers(group.id, [user1._id, user2._id])
74
+
75
+ await config.doInContext(config.appId, async () => {
76
+ await syncGlobalUsers()
77
+ expect(await rawUserMetadata()).toHaveLength(1)
78
+
79
+ await proSdk.groups.updateGroupApps(group.id, {
80
+ appsToAdd: [
81
+ { appId: config.prodAppId!, roleId: roles.BUILTIN_ROLE_IDS.BASIC },
82
+ ],
83
+ })
84
+ await syncGlobalUsers()
85
+
86
+ const metadata = await rawUserMetadata()
87
+ expect(metadata).toHaveLength(3)
88
+ expect(metadata).toContainEqual(
89
+ expect.objectContaining({
90
+ _id: db.generateUserMetadataID(user1._id),
91
+ })
92
+ )
93
+ expect(metadata).toContainEqual(
94
+ expect.objectContaining({
95
+ _id: db.generateUserMetadataID(user2._id),
96
+ })
97
+ )
98
+ })
99
+ })
100
+ })
101
+
102
+ it("app users are removed when app is removed from user group", async () => {
103
+ await config.doInTenant(async () => {
104
+ const group = await proSdk.groups.save(structures.userGroups.userGroup())
105
+ const user1 = await config.createUser({ admin: false, builder: false })
106
+ const user2 = await config.createUser({ admin: false, builder: false })
107
+ await proSdk.groups.updateGroupApps(group.id, {
108
+ appsToAdd: [
109
+ { appId: config.prodAppId!, roleId: roles.BUILTIN_ROLE_IDS.BASIC },
110
+ ],
111
+ })
112
+ await proSdk.groups.addUsers(group.id, [user1._id, user2._id])
113
+
114
+ await config.doInContext(config.appId, async () => {
115
+ await syncGlobalUsers()
116
+ expect(await rawUserMetadata()).toHaveLength(3)
117
+
118
+ await proSdk.groups.updateGroupApps(group.id, {
119
+ appsToRemove: [{ appId: config.prodAppId! }],
120
+ })
121
+ await syncGlobalUsers()
122
+
123
+ const metadata = await rawUserMetadata()
124
+ expect(metadata).toHaveLength(1)
125
+ })
126
+ })
127
+ })
128
+
129
+ it("app users are removed when app is removed from user group", async () => {
130
+ await config.doInTenant(async () => {
131
+ const group = await proSdk.groups.save(structures.userGroups.userGroup())
132
+ const user1 = await config.createUser({ admin: false, builder: false })
133
+ const user2 = await config.createUser({ admin: false, builder: false })
134
+ await proSdk.groups.updateGroupApps(group.id, {
135
+ appsToAdd: [
136
+ { appId: config.prodAppId!, roleId: roles.BUILTIN_ROLE_IDS.BASIC },
137
+ ],
138
+ })
139
+ await proSdk.groups.addUsers(group.id, [user1._id, user2._id])
140
+
141
+ await config.doInContext(config.appId, async () => {
142
+ await syncGlobalUsers()
143
+ expect(await rawUserMetadata()).toHaveLength(3)
144
+
145
+ await proSdk.groups.removeUsers(group.id, [user1._id])
146
+ await syncGlobalUsers()
147
+
148
+ const metadata = await rawUserMetadata()
149
+ expect(metadata).toHaveLength(2)
150
+
151
+ expect(metadata).not.toContainEqual(
152
+ expect.objectContaining({
153
+ _id: db.generateUserMetadataID(user1._id),
154
+ })
155
+ )
156
+ })
157
+ })
158
+ })
159
+ })
@@ -6,25 +6,33 @@ import {
6
6
  InternalTables,
7
7
  } from "../../db/utils"
8
8
  import { isEqual } from "lodash"
9
+ import { ContextUser, UserMetadata } from "@budibase/types"
9
10
 
10
- export function combineMetadataAndUser(user: any, metadata: any) {
11
+ export function combineMetadataAndUser(
12
+ user: ContextUser,
13
+ metadata: UserMetadata | UserMetadata[]
14
+ ) {
15
+ const metadataId = generateUserMetadataID(user._id!)
16
+ const found = Array.isArray(metadata)
17
+ ? metadata.find(doc => doc._id === metadataId)
18
+ : metadata
11
19
  // skip users with no access
12
20
  if (
13
21
  user.roleId == null ||
14
22
  user.roleId === rolesCore.BUILTIN_ROLE_IDS.PUBLIC
15
23
  ) {
24
+ // If it exists and it should not, we must remove it
25
+ if (found?._id) {
26
+ return { ...found, _deleted: true }
27
+ }
16
28
  return null
17
29
  }
18
30
  delete user._rev
19
- const metadataId = generateUserMetadataID(user._id)
20
31
  const newDoc = {
21
32
  ...user,
22
33
  _id: metadataId,
23
34
  tableId: InternalTables.USER_METADATA,
24
35
  }
25
- const found = Array.isArray(metadata)
26
- ? metadata.find(doc => doc._id === metadataId)
27
- : metadata
28
36
  // copy rev over for the purposes of equality check
29
37
  if (found) {
30
38
  newDoc._rev = found._rev
@@ -58,7 +66,7 @@ export async function syncGlobalUsers() {
58
66
  ])
59
67
  const toWrite = []
60
68
  for (let user of users) {
61
- const combined = await combineMetadataAndUser(user, metadata)
69
+ const combined = combineMetadataAndUser(user, metadata)
62
70
  if (combined) {
63
71
  toWrite.push(combined)
64
72
  }
@@ -47,6 +47,7 @@ import {
47
47
  SourceName,
48
48
  Table,
49
49
  SearchFilters,
50
+ UserRoles,
50
51
  } from "@budibase/types"
51
52
 
52
53
  type DefaultUserValues = {
@@ -277,7 +278,7 @@ class TestConfiguration {
277
278
  email?: string
278
279
  builder?: boolean
279
280
  admin?: boolean
280
- roles?: any
281
+ roles?: UserRoles
281
282
  } = {}
282
283
  ) {
283
284
  let { id, firstName, lastName, email, builder, admin, roles } = user
@@ -131,7 +131,7 @@ export function coerce(row: any, type: string) {
131
131
  * @returns {object} the row which has been prepared to be written to the DB.
132
132
  */
133
133
  export function inputProcessing(
134
- user: ContextUser,
134
+ user: ContextUser | null,
135
135
  table: Table,
136
136
  row: Row,
137
137
  opts?: AutoColumnProcessingOpts