@budibase/backend-core 2.9.40-alpha.6 → 2.10.1

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.
Files changed (252) hide show
  1. package/dist/index.js +5 -4
  2. package/dist/index.js.map +2 -2
  3. package/dist/index.js.meta.json +1 -1
  4. package/dist/package.json +6 -6
  5. package/dist/src/cache/appMetadata.js +1 -1
  6. package/dist/src/cache/appMetadata.js.map +1 -1
  7. package/dist/src/constants/misc.d.ts +0 -2
  8. package/dist/src/constants/misc.js +0 -2
  9. package/dist/src/constants/misc.js.map +1 -1
  10. package/dist/src/environment.js +5 -4
  11. package/dist/src/environment.js.map +1 -1
  12. package/dist/src/logging/system.d.ts +1 -1
  13. package/dist/src/timers/timers.d.ts +1 -1
  14. package/package.json +6 -6
  15. package/src/accounts/accounts.ts +82 -0
  16. package/src/accounts/api.ts +59 -0
  17. package/src/accounts/index.ts +1 -0
  18. package/src/auth/auth.ts +208 -0
  19. package/src/auth/index.ts +1 -0
  20. package/src/auth/tests/auth.spec.ts +14 -0
  21. package/src/blacklist/blacklist.ts +54 -0
  22. package/src/blacklist/index.ts +1 -0
  23. package/src/blacklist/tests/blacklist.spec.ts +46 -0
  24. package/src/cache/appMetadata.ts +88 -0
  25. package/src/cache/base/index.ts +92 -0
  26. package/src/cache/generic.ts +30 -0
  27. package/src/cache/index.ts +5 -0
  28. package/src/cache/tests/writethrough.spec.ts +138 -0
  29. package/src/cache/user.ts +83 -0
  30. package/src/cache/writethrough.ts +133 -0
  31. package/src/configs/configs.ts +257 -0
  32. package/src/configs/index.ts +1 -0
  33. package/src/configs/tests/configs.spec.ts +184 -0
  34. package/src/constants/db.ts +63 -0
  35. package/src/constants/index.ts +2 -0
  36. package/src/constants/misc.ts +50 -0
  37. package/src/context/Context.ts +14 -0
  38. package/src/context/identity.ts +58 -0
  39. package/src/context/index.ts +3 -0
  40. package/src/context/mainContext.ts +310 -0
  41. package/src/context/tests/index.spec.ts +147 -0
  42. package/src/context/types.ts +11 -0
  43. package/src/db/Replication.ts +84 -0
  44. package/src/db/constants.ts +10 -0
  45. package/src/db/couch/DatabaseImpl.ts +238 -0
  46. package/src/db/couch/connections.ts +77 -0
  47. package/src/db/couch/index.ts +5 -0
  48. package/src/db/couch/pouchDB.ts +97 -0
  49. package/src/db/couch/pouchDump.ts +0 -0
  50. package/src/db/couch/utils.ts +50 -0
  51. package/src/db/db.ts +43 -0
  52. package/src/db/errors.ts +14 -0
  53. package/src/db/index.ts +12 -0
  54. package/src/db/lucene.ts +750 -0
  55. package/src/db/searchIndexes/index.ts +1 -0
  56. package/src/db/searchIndexes/searchIndexes.ts +62 -0
  57. package/src/db/tests/index.spec.js +25 -0
  58. package/src/db/tests/lucene.spec.ts +368 -0
  59. package/src/db/tests/pouch.spec.js +62 -0
  60. package/src/db/tests/utils.spec.ts +63 -0
  61. package/src/db/utils.ts +207 -0
  62. package/src/db/views.ts +241 -0
  63. package/src/docIds/conversions.ts +59 -0
  64. package/src/docIds/ids.ts +113 -0
  65. package/src/docIds/index.ts +2 -0
  66. package/src/docIds/newid.ts +5 -0
  67. package/src/docIds/params.ts +174 -0
  68. package/src/docUpdates/index.ts +29 -0
  69. package/src/environment.ts +201 -0
  70. package/src/errors/errors.ts +119 -0
  71. package/src/errors/index.ts +1 -0
  72. package/src/events/analytics.ts +6 -0
  73. package/src/events/asyncEvents/index.ts +2 -0
  74. package/src/events/asyncEvents/publisher.ts +12 -0
  75. package/src/events/asyncEvents/queue.ts +22 -0
  76. package/src/events/backfill.ts +183 -0
  77. package/src/events/documentId.ts +56 -0
  78. package/src/events/events.ts +40 -0
  79. package/src/events/identification.ts +310 -0
  80. package/src/events/index.ts +14 -0
  81. package/src/events/processors/AnalyticsProcessor.ts +64 -0
  82. package/src/events/processors/AuditLogsProcessor.ts +93 -0
  83. package/src/events/processors/LoggingProcessor.ts +37 -0
  84. package/src/events/processors/Processors.ts +52 -0
  85. package/src/events/processors/async/DocumentUpdateProcessor.ts +43 -0
  86. package/src/events/processors/index.ts +19 -0
  87. package/src/events/processors/posthog/PosthogProcessor.ts +118 -0
  88. package/src/events/processors/posthog/index.ts +2 -0
  89. package/src/events/processors/posthog/rateLimiting.ts +106 -0
  90. package/src/events/processors/posthog/tests/PosthogProcessor.spec.ts +168 -0
  91. package/src/events/processors/types.ts +1 -0
  92. package/src/events/publishers/account.ts +35 -0
  93. package/src/events/publishers/app.ts +155 -0
  94. package/src/events/publishers/auditLog.ts +26 -0
  95. package/src/events/publishers/auth.ts +73 -0
  96. package/src/events/publishers/automation.ts +110 -0
  97. package/src/events/publishers/backfill.ts +74 -0
  98. package/src/events/publishers/backup.ts +42 -0
  99. package/src/events/publishers/datasource.ts +48 -0
  100. package/src/events/publishers/email.ts +17 -0
  101. package/src/events/publishers/environmentVariable.ts +38 -0
  102. package/src/events/publishers/group.ts +99 -0
  103. package/src/events/publishers/index.ts +24 -0
  104. package/src/events/publishers/installation.ts +38 -0
  105. package/src/events/publishers/layout.ts +26 -0
  106. package/src/events/publishers/license.ts +84 -0
  107. package/src/events/publishers/org.ts +37 -0
  108. package/src/events/publishers/plugin.ts +47 -0
  109. package/src/events/publishers/query.ts +88 -0
  110. package/src/events/publishers/role.ts +62 -0
  111. package/src/events/publishers/rows.ts +29 -0
  112. package/src/events/publishers/screen.ts +36 -0
  113. package/src/events/publishers/serve.ts +43 -0
  114. package/src/events/publishers/table.ts +70 -0
  115. package/src/events/publishers/user.ts +202 -0
  116. package/src/events/publishers/view.ts +107 -0
  117. package/src/features/index.ts +78 -0
  118. package/src/features/installation.ts +17 -0
  119. package/src/features/tests/featureFlags.spec.ts +85 -0
  120. package/src/helpers.ts +9 -0
  121. package/src/index.ts +54 -0
  122. package/src/installation.ts +107 -0
  123. package/src/logging/alerts.ts +26 -0
  124. package/src/logging/correlation/correlation.ts +13 -0
  125. package/src/logging/correlation/index.ts +1 -0
  126. package/src/logging/correlation/middleware.ts +17 -0
  127. package/src/logging/index.ts +4 -0
  128. package/src/logging/pino/logger.ts +232 -0
  129. package/src/logging/pino/middleware.ts +45 -0
  130. package/src/logging/system.ts +81 -0
  131. package/src/logging/tests/system.spec.ts +61 -0
  132. package/src/middleware/adminOnly.ts +9 -0
  133. package/src/middleware/auditLog.ts +6 -0
  134. package/src/middleware/authenticated.ts +193 -0
  135. package/src/middleware/builderOnly.ts +21 -0
  136. package/src/middleware/builderOrAdmin.ts +21 -0
  137. package/src/middleware/csrf.ts +81 -0
  138. package/src/middleware/errorHandling.ts +29 -0
  139. package/src/middleware/index.ts +21 -0
  140. package/src/middleware/internalApi.ts +23 -0
  141. package/src/middleware/joi-validator.ts +45 -0
  142. package/src/middleware/matchers.ts +47 -0
  143. package/src/middleware/passport/datasource/google.ts +95 -0
  144. package/src/middleware/passport/local.ts +54 -0
  145. package/src/middleware/passport/sso/google.ts +77 -0
  146. package/src/middleware/passport/sso/oidc.ts +154 -0
  147. package/src/middleware/passport/sso/sso.ts +165 -0
  148. package/src/middleware/passport/sso/tests/google.spec.ts +67 -0
  149. package/src/middleware/passport/sso/tests/oidc.spec.ts +152 -0
  150. package/src/middleware/passport/sso/tests/sso.spec.ts +197 -0
  151. package/src/middleware/passport/utils.ts +38 -0
  152. package/src/middleware/querystringToBody.ts +28 -0
  153. package/src/middleware/tenancy.ts +36 -0
  154. package/src/middleware/tests/builder.spec.ts +180 -0
  155. package/src/middleware/tests/matchers.spec.ts +134 -0
  156. package/src/migrations/definitions.ts +40 -0
  157. package/src/migrations/index.ts +2 -0
  158. package/src/migrations/migrations.ts +191 -0
  159. package/src/migrations/tests/__snapshots__/migrations.spec.ts.snap +11 -0
  160. package/src/migrations/tests/migrations.spec.ts +64 -0
  161. package/src/objectStore/buckets/app.ts +40 -0
  162. package/src/objectStore/buckets/global.ts +29 -0
  163. package/src/objectStore/buckets/index.ts +3 -0
  164. package/src/objectStore/buckets/plugins.ts +71 -0
  165. package/src/objectStore/buckets/tests/app.spec.ts +171 -0
  166. package/src/objectStore/buckets/tests/global.spec.ts +74 -0
  167. package/src/objectStore/buckets/tests/plugins.spec.ts +111 -0
  168. package/src/objectStore/cloudfront.ts +41 -0
  169. package/src/objectStore/index.ts +3 -0
  170. package/src/objectStore/objectStore.ts +440 -0
  171. package/src/objectStore/utils.ts +27 -0
  172. package/src/platform/index.ts +3 -0
  173. package/src/platform/platformDb.ts +6 -0
  174. package/src/platform/tenants.ts +101 -0
  175. package/src/platform/tests/tenants.spec.ts +26 -0
  176. package/src/platform/users.ts +90 -0
  177. package/src/plugin/index.ts +1 -0
  178. package/src/plugin/tests/validation.spec.ts +83 -0
  179. package/src/plugin/utils.ts +156 -0
  180. package/src/queue/constants.ts +6 -0
  181. package/src/queue/inMemoryQueue.ts +141 -0
  182. package/src/queue/index.ts +2 -0
  183. package/src/queue/listeners.ts +195 -0
  184. package/src/queue/queue.ts +54 -0
  185. package/src/redis/index.ts +6 -0
  186. package/src/redis/init.ts +86 -0
  187. package/src/redis/redis.ts +308 -0
  188. package/src/redis/redlockImpl.ts +139 -0
  189. package/src/redis/utils.ts +117 -0
  190. package/src/security/encryption.ts +179 -0
  191. package/src/security/permissions.ts +158 -0
  192. package/src/security/roles.ts +389 -0
  193. package/src/security/sessions.ts +120 -0
  194. package/src/security/tests/encryption.spec.ts +31 -0
  195. package/src/security/tests/permissions.spec.ts +145 -0
  196. package/src/security/tests/sessions.spec.ts +12 -0
  197. package/src/tenancy/db.ts +6 -0
  198. package/src/tenancy/index.ts +2 -0
  199. package/src/tenancy/tenancy.ts +140 -0
  200. package/src/tenancy/tests/tenancy.spec.ts +184 -0
  201. package/src/timers/index.ts +1 -0
  202. package/src/timers/timers.ts +22 -0
  203. package/src/users/db.ts +484 -0
  204. package/src/users/events.ts +176 -0
  205. package/src/users/index.ts +4 -0
  206. package/src/users/lookup.ts +102 -0
  207. package/src/users/users.ts +276 -0
  208. package/src/users/utils.ts +55 -0
  209. package/src/utils/hashing.ts +14 -0
  210. package/src/utils/index.ts +3 -0
  211. package/src/utils/stringUtils.ts +8 -0
  212. package/src/utils/tests/utils.spec.ts +191 -0
  213. package/src/utils/utils.ts +239 -0
  214. package/tests/core/logging.ts +34 -0
  215. package/tests/core/utilities/index.ts +6 -0
  216. package/tests/core/utilities/jestUtils.ts +30 -0
  217. package/tests/core/utilities/mocks/alerts.ts +3 -0
  218. package/tests/core/utilities/mocks/date.ts +2 -0
  219. package/tests/core/utilities/mocks/events.ts +131 -0
  220. package/tests/core/utilities/mocks/fetch.ts +17 -0
  221. package/tests/core/utilities/mocks/index.ts +10 -0
  222. package/tests/core/utilities/mocks/licenses.ts +115 -0
  223. package/tests/core/utilities/mocks/posthog.ts +7 -0
  224. package/tests/core/utilities/structures/Chance.ts +20 -0
  225. package/tests/core/utilities/structures/accounts.ts +115 -0
  226. package/tests/core/utilities/structures/apps.ts +21 -0
  227. package/tests/core/utilities/structures/common.ts +7 -0
  228. package/tests/core/utilities/structures/db.ts +12 -0
  229. package/tests/core/utilities/structures/documents/index.ts +1 -0
  230. package/tests/core/utilities/structures/documents/platform/index.ts +1 -0
  231. package/tests/core/utilities/structures/documents/platform/installation.ts +12 -0
  232. package/tests/core/utilities/structures/generator.ts +2 -0
  233. package/tests/core/utilities/structures/index.ts +15 -0
  234. package/tests/core/utilities/structures/koa.ts +16 -0
  235. package/tests/core/utilities/structures/licenses.ts +167 -0
  236. package/tests/core/utilities/structures/plugins.ts +19 -0
  237. package/tests/core/utilities/structures/quotas.ts +67 -0
  238. package/tests/core/utilities/structures/scim.ts +80 -0
  239. package/tests/core/utilities/structures/shared.ts +19 -0
  240. package/tests/core/utilities/structures/sso.ts +119 -0
  241. package/tests/core/utilities/structures/tenants.ts +5 -0
  242. package/tests/core/utilities/structures/userGroups.ts +10 -0
  243. package/tests/core/utilities/structures/users.ts +73 -0
  244. package/tests/core/utilities/testContainerUtils.ts +85 -0
  245. package/tests/core/utilities/utils/index.ts +1 -0
  246. package/tests/core/utilities/utils/time.ts +3 -0
  247. package/tests/extra/DBTestConfiguration.ts +36 -0
  248. package/tests/extra/index.ts +2 -0
  249. package/tests/extra/testEnv.ts +95 -0
  250. package/tests/index.ts +1 -0
  251. package/tests/jestEnv.ts +6 -0
  252. package/tests/jestSetup.ts +28 -0
@@ -0,0 +1,115 @@
1
+ import { generator, uuid, quotas } from "."
2
+ import { generateGlobalUserID } from "../../../../src/docIds"
3
+ import {
4
+ Account,
5
+ AccountSSOProvider,
6
+ AccountSSOProviderType,
7
+ AuthType,
8
+ CloudAccount,
9
+ Hosting,
10
+ SSOAccount,
11
+ CreateAccount,
12
+ CreatePassswordAccount,
13
+ } from "@budibase/types"
14
+ import sample from "lodash/sample"
15
+
16
+ export const account = (partial: Partial<Account> = {}): Account => {
17
+ return {
18
+ accountId: uuid(),
19
+ tenantId: generator.word(),
20
+ email: generator.email(),
21
+ tenantName: generator.word(),
22
+ hosting: Hosting.SELF,
23
+ createdAt: Date.now(),
24
+ verified: true,
25
+ verificationSent: true,
26
+ tier: "FREE", // DEPRECATED
27
+ authType: AuthType.PASSWORD,
28
+ name: generator.name(),
29
+ size: "10+",
30
+ profession: "Software Engineer",
31
+ quotaUsage: quotas.usage(),
32
+ ...partial,
33
+ }
34
+ }
35
+
36
+ export function selfHostAccount() {
37
+ return account()
38
+ }
39
+
40
+ export const cloudAccount = (): CloudAccount => {
41
+ return {
42
+ ...account(),
43
+ hosting: Hosting.CLOUD,
44
+ budibaseUserId: generateGlobalUserID(),
45
+ }
46
+ }
47
+
48
+ function providerType(): AccountSSOProviderType {
49
+ return sample(Object.values(AccountSSOProviderType)) as AccountSSOProviderType
50
+ }
51
+
52
+ function provider(): AccountSSOProvider {
53
+ return sample(Object.values(AccountSSOProvider)) as AccountSSOProvider
54
+ }
55
+
56
+ export function ssoAccount(account: Account = cloudAccount()): SSOAccount {
57
+ return {
58
+ ...account,
59
+ authType: AuthType.SSO,
60
+ oauth2: {
61
+ accessToken: generator.string(),
62
+ refreshToken: generator.string(),
63
+ },
64
+ pictureUrl: generator.url(),
65
+ provider: provider(),
66
+ providerType: providerType(),
67
+ thirdPartyProfile: {},
68
+ }
69
+ }
70
+
71
+ export const cloudCreateAccount: CreatePassswordAccount = {
72
+ email: "cloud@budibase.com",
73
+ tenantId: "cloud",
74
+ hosting: Hosting.CLOUD,
75
+ authType: AuthType.PASSWORD,
76
+ password: "Password123!",
77
+ tenantName: "cloud",
78
+ name: "Budi Armstrong",
79
+ size: "10+",
80
+ profession: "Software Engineer",
81
+ }
82
+
83
+ export const cloudSSOCreateAccount: CreateAccount = {
84
+ email: "cloud-sso@budibase.com",
85
+ tenantId: "cloud-sso",
86
+ hosting: Hosting.CLOUD,
87
+ authType: AuthType.SSO,
88
+ tenantName: "cloudsso",
89
+ name: "Budi Armstrong",
90
+ size: "10+",
91
+ profession: "Software Engineer",
92
+ }
93
+
94
+ export const selfCreateAccount: CreatePassswordAccount = {
95
+ email: "self@budibase.com",
96
+ tenantId: "self",
97
+ hosting: Hosting.SELF,
98
+ authType: AuthType.PASSWORD,
99
+ password: "Password123!",
100
+ tenantName: "self",
101
+ name: "Budi Armstrong",
102
+ size: "10+",
103
+ profession: "Software Engineer",
104
+ }
105
+
106
+ export const selfSSOCreateAccount: CreateAccount = {
107
+ email: "self-sso@budibase.com",
108
+ tenantId: "self-sso",
109
+ hosting: Hosting.SELF,
110
+ authType: AuthType.SSO,
111
+ tenantName: "selfsso",
112
+ name: "Budi Armstrong",
113
+ size: "10+",
114
+ profession: "Software Engineer",
115
+ }
@@ -0,0 +1,21 @@
1
+ import { generator } from "."
2
+ import { App } from "@budibase/types"
3
+ import { DEFAULT_TENANT_ID, DocumentType } from "../../../../src/constants"
4
+
5
+ export function app(id: string): App {
6
+ return {
7
+ _id: DocumentType.APP_METADATA,
8
+ appId: id,
9
+ type: "",
10
+ version: "0.0.1",
11
+ componentLibraries: [],
12
+ name: generator.name(),
13
+ url: `/custom-url`,
14
+ instance: {
15
+ _id: id,
16
+ },
17
+ tenantId: DEFAULT_TENANT_ID,
18
+ status: "",
19
+ template: undefined,
20
+ }
21
+ }
@@ -0,0 +1,7 @@
1
+ import { v4 as uuid } from "uuid"
2
+
3
+ export { v4 as uuid } from "uuid"
4
+
5
+ export const email = () => {
6
+ return `${uuid()}@test.com`
7
+ }
@@ -0,0 +1,12 @@
1
+ import { generator } from "./generator"
2
+ import { newid } from "../../../../src/docIds/newid"
3
+
4
+ export function id() {
5
+ return `db_${newid()}`
6
+ }
7
+
8
+ export function rev() {
9
+ return `${generator.character({
10
+ numeric: true,
11
+ })}-${generator.guid().replace(/-/, "")}`
12
+ }
@@ -0,0 +1 @@
1
+ export * from "./platform"
@@ -0,0 +1 @@
1
+ export * as installation from "./installation"
@@ -0,0 +1,12 @@
1
+ import { generator } from "../../generator"
2
+ import { Installation } from "@budibase/types"
3
+ import * as db from "../../db"
4
+
5
+ export function install(): Installation {
6
+ return {
7
+ _id: "install",
8
+ _rev: db.rev(),
9
+ installId: generator.guid(),
10
+ version: generator.string(),
11
+ }
12
+ }
@@ -0,0 +1,2 @@
1
+ import Chance from "./Chance"
2
+ export const generator = new Chance()
@@ -0,0 +1,15 @@
1
+ export * from "./common"
2
+ export * as accounts from "./accounts"
3
+ export * as apps from "./apps"
4
+ export * as db from "./db"
5
+ export * as docs from "./documents"
6
+ export * as koa from "./koa"
7
+ export * as licenses from "./licenses"
8
+ export * as plugins from "./plugins"
9
+ export * as sso from "./sso"
10
+ export * as tenant from "./tenants"
11
+ export * as users from "./users"
12
+ export * as userGroups from "./userGroups"
13
+ export { generator } from "./generator"
14
+ export * as scim from "./scim"
15
+ export * as quotas from "./quotas"
@@ -0,0 +1,16 @@
1
+ import { createMockContext, createMockCookies } from "@shopify/jest-koa-mocks"
2
+ import { BBContext } from "@budibase/types"
3
+
4
+ export const newContext = (): BBContext => {
5
+ const ctx = createMockContext()
6
+ return {
7
+ ...ctx,
8
+ path: "/",
9
+ cookies: createMockCookies(),
10
+ request: {
11
+ ...ctx.request,
12
+ headers: {},
13
+ body: {},
14
+ },
15
+ }
16
+ }
@@ -0,0 +1,167 @@
1
+ import {
2
+ Billing,
3
+ Customer,
4
+ Feature,
5
+ License,
6
+ OfflineIdentifier,
7
+ OfflineLicense,
8
+ PlanModel,
9
+ PlanType,
10
+ PriceDuration,
11
+ PurchasedPlan,
12
+ PurchasedPrice,
13
+ Quotas,
14
+ Subscription,
15
+ } from "@budibase/types"
16
+ import { generator } from "./generator"
17
+
18
+ export function price(): PurchasedPrice {
19
+ return {
20
+ amount: 10000,
21
+ amountMonthly: 10000,
22
+ currency: "usd",
23
+ duration: PriceDuration.MONTHLY,
24
+ priceId: "price_123",
25
+ dayPasses: undefined,
26
+ isPerUser: true,
27
+ }
28
+ }
29
+
30
+ export const plan = (type: PlanType = PlanType.FREE): PurchasedPlan => {
31
+ return {
32
+ type,
33
+ usesInvoicing: false,
34
+ model: PlanModel.PER_USER,
35
+ price: type !== PlanType.FREE ? price() : undefined,
36
+ }
37
+ }
38
+
39
+ export function quotas(): Quotas {
40
+ return {
41
+ usage: {
42
+ monthly: {
43
+ queries: {
44
+ name: "Queries",
45
+ value: 1,
46
+ triggers: [],
47
+ },
48
+ automations: {
49
+ name: "Queries",
50
+ value: 1,
51
+ triggers: [],
52
+ },
53
+ dayPasses: {
54
+ name: "Queries",
55
+ value: 1,
56
+ triggers: [],
57
+ },
58
+ },
59
+ static: {
60
+ rows: {
61
+ name: "Rows",
62
+ value: 1,
63
+ triggers: [],
64
+ },
65
+ apps: {
66
+ name: "Apps",
67
+ value: 1,
68
+ triggers: [],
69
+ },
70
+ users: {
71
+ name: "Users",
72
+ value: 1,
73
+ triggers: [],
74
+ },
75
+ userGroups: {
76
+ name: "User Groups",
77
+ value: 1,
78
+ triggers: [],
79
+ },
80
+ plugins: {
81
+ name: "Plugins",
82
+ value: 1,
83
+ triggers: [],
84
+ },
85
+ },
86
+ },
87
+ constant: {
88
+ automationLogRetentionDays: {
89
+ name: "Automation Logs",
90
+ value: 1,
91
+ triggers: [],
92
+ },
93
+ appBackupRetentionDays: {
94
+ name: "Backups",
95
+ value: 1,
96
+ triggers: [],
97
+ },
98
+ },
99
+ }
100
+ }
101
+
102
+ export function billing(
103
+ opts: { customer?: Customer; subscription?: Subscription } = {}
104
+ ): Billing {
105
+ return {
106
+ customer: opts.customer || customer(),
107
+ subscription: opts.subscription || subscription(),
108
+ }
109
+ }
110
+
111
+ export function customer(): Customer {
112
+ return {
113
+ balance: 0,
114
+ currency: "usd",
115
+ }
116
+ }
117
+
118
+ export function subscription(): Subscription {
119
+ return {
120
+ amount: 10000,
121
+ cancelAt: undefined,
122
+ currency: "usd",
123
+ currentPeriodEnd: 0,
124
+ currentPeriodStart: 0,
125
+ downgradeAt: 0,
126
+ duration: PriceDuration.MONTHLY,
127
+ pastDueAt: undefined,
128
+ quantity: 0,
129
+ status: "active",
130
+ }
131
+ }
132
+
133
+ interface GenerateLicenseOpts {
134
+ quotas?: Quotas
135
+ plan?: PurchasedPlan
136
+ planType?: PlanType
137
+ features?: Feature[]
138
+ billing?: Billing
139
+ }
140
+
141
+ export const license = (opts: GenerateLicenseOpts = {}): License => {
142
+ return {
143
+ features: opts.features || [],
144
+ quotas: opts.quotas || quotas(),
145
+ plan: opts.plan || plan(opts.planType),
146
+ billing: opts.billing || billing(),
147
+ }
148
+ }
149
+
150
+ export function offlineLicense(opts: GenerateLicenseOpts = {}): OfflineLicense {
151
+ const base = license(opts)
152
+ return {
153
+ ...base,
154
+ expireAt: new Date().toISOString(),
155
+ identifier: offlineIdentifier(),
156
+ }
157
+ }
158
+
159
+ export function offlineIdentifier(
160
+ installId: string = generator.guid(),
161
+ tenantId: string = generator.guid()
162
+ ): OfflineIdentifier {
163
+ return {
164
+ installId,
165
+ tenantId,
166
+ }
167
+ }
@@ -0,0 +1,19 @@
1
+ import { generator } from "."
2
+ import { Plugin, PluginSource, PluginType } from "@budibase/types"
3
+
4
+ export function plugin(): Plugin {
5
+ return {
6
+ description: generator.word(),
7
+ name: generator.word(),
8
+ version: "1.0.0",
9
+ source: PluginSource.FILE,
10
+ package: {
11
+ name: generator.word,
12
+ },
13
+ hash: generator.hash(),
14
+ schema: {
15
+ type: PluginType.DATASOURCE,
16
+ },
17
+ iconFileName: "icon.svg",
18
+ }
19
+ }
@@ -0,0 +1,67 @@
1
+ import { MonthlyQuotaName, QuotaUsage } from "@budibase/types"
2
+
3
+ export const usage = (): QuotaUsage => {
4
+ return {
5
+ _id: "usage_quota",
6
+ quotaReset: new Date().toISOString(),
7
+ apps: {
8
+ app_1: {
9
+ // @ts-ignore - the apps definition doesn't match up to actual usage
10
+ usageQuota: {
11
+ rows: 0,
12
+ },
13
+ },
14
+ },
15
+ monthly: {
16
+ "01-2023": {
17
+ automations: 0,
18
+ dayPasses: 0,
19
+ queries: 0,
20
+ triggers: {},
21
+ breakdown: {
22
+ rowQueries: {
23
+ parent: MonthlyQuotaName.QUERIES,
24
+ values: {
25
+ row_1: 0,
26
+ row_2: 0,
27
+ },
28
+ },
29
+ datasourceQueries: {
30
+ parent: MonthlyQuotaName.QUERIES,
31
+ values: {
32
+ ds_1: 0,
33
+ ds_2: 0,
34
+ },
35
+ },
36
+ automations: {
37
+ parent: MonthlyQuotaName.AUTOMATIONS,
38
+ values: {
39
+ auto_1: 0,
40
+ auto_2: 0,
41
+ },
42
+ },
43
+ },
44
+ },
45
+ "02-2023": {
46
+ automations: 0,
47
+ dayPasses: 0,
48
+ queries: 0,
49
+ triggers: {},
50
+ },
51
+ current: {
52
+ automations: 0,
53
+ dayPasses: 0,
54
+ queries: 0,
55
+ triggers: {},
56
+ },
57
+ },
58
+ usageQuota: {
59
+ apps: 0,
60
+ plugins: 0,
61
+ users: 0,
62
+ userGroups: 0,
63
+ rows: 0,
64
+ triggers: {},
65
+ },
66
+ }
67
+ }
@@ -0,0 +1,80 @@
1
+ import { ScimCreateGroupRequest, ScimCreateUserRequest } from "@budibase/types"
2
+ import { uuid } from "./common"
3
+ import { generator } from "./generator"
4
+
5
+ interface CreateUserRequestFields {
6
+ externalId: string
7
+ email: string
8
+ firstName: string
9
+ lastName: string
10
+ username: string
11
+ }
12
+
13
+ export function createUserRequest(userData?: Partial<CreateUserRequestFields>) {
14
+ const defaultValues = {
15
+ externalId: uuid(),
16
+ email: generator.email(),
17
+ firstName: generator.first(),
18
+ lastName: generator.last(),
19
+ username: generator.name(),
20
+ }
21
+
22
+ const { externalId, email, firstName, lastName, username } = {
23
+ ...defaultValues,
24
+ ...userData,
25
+ }
26
+
27
+ let user: ScimCreateUserRequest = {
28
+ schemas: [
29
+ "urn:ietf:params:scim:schemas:core:2.0:User",
30
+ "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User",
31
+ ],
32
+ externalId,
33
+ userName: username,
34
+ active: true,
35
+ emails: [
36
+ {
37
+ primary: true,
38
+ type: "work",
39
+ value: email,
40
+ },
41
+ ],
42
+ meta: {
43
+ resourceType: "User",
44
+ },
45
+ roles: [],
46
+ }
47
+
48
+ if (firstName || lastName) {
49
+ user.name = {
50
+ formatted: [firstName, lastName].filter(s => s).join(" "),
51
+ familyName: lastName,
52
+ givenName: firstName,
53
+ }
54
+ }
55
+
56
+ return user
57
+ }
58
+
59
+ export function createGroupRequest(groupData?: {
60
+ externalId?: string
61
+ displayName?: string
62
+ }) {
63
+ const { externalId = uuid(), displayName = generator.word() } =
64
+ groupData || {}
65
+
66
+ const group: ScimCreateGroupRequest = {
67
+ schemas: [
68
+ "urn:ietf:params:scim:schemas:core:2.0:Group",
69
+ "http://schemas.microsoft.com/2006/11/ResourceManagement/ADSCIM/2.0/Group",
70
+ ],
71
+ externalId: externalId,
72
+ displayName: displayName,
73
+ meta: {
74
+ resourceType: "Group",
75
+ created: new Date(),
76
+ lastModified: new Date(),
77
+ },
78
+ }
79
+ return group
80
+ }
@@ -0,0 +1,19 @@
1
+ import { User } from "@budibase/types"
2
+ import { generator } from "./generator"
3
+ import { uuid } from "./common"
4
+
5
+ export const newEmail = () => {
6
+ return `${uuid()}@test.com`
7
+ }
8
+
9
+ export const user = (userProps?: any): User => {
10
+ return {
11
+ email: newEmail(),
12
+ password: "test",
13
+ roles: { app_test: "admin" },
14
+ firstName: generator.first(),
15
+ lastName: generator.last(),
16
+ pictureUrl: "http://test.com",
17
+ ...userProps,
18
+ }
19
+ }
@@ -0,0 +1,119 @@
1
+ import {
2
+ ConfigType,
3
+ GoogleConfig,
4
+ GoogleInnerConfig,
5
+ JwtClaims,
6
+ OAuth2,
7
+ OIDCInnerConfig,
8
+ OIDCWellKnownConfig,
9
+ SSOAuthDetails,
10
+ SSOProfile,
11
+ SSOProviderType,
12
+ User,
13
+ } from "@budibase/types"
14
+ import { generator } from "./generator"
15
+ import { email, uuid } from "./common"
16
+ import * as shared from "./shared"
17
+ import { user } from "./shared"
18
+ import sample from "lodash/sample"
19
+
20
+ export function OAuth(): OAuth2 {
21
+ return {
22
+ refreshToken: generator.string(),
23
+ accessToken: generator.string(),
24
+ }
25
+ }
26
+
27
+ export function authDetails(userDoc?: User): SSOAuthDetails {
28
+ if (!userDoc) {
29
+ userDoc = user()
30
+ }
31
+
32
+ const userId = userDoc._id || uuid()
33
+ const provider = generator.string()
34
+
35
+ const profile = ssoProfile(userDoc)
36
+ profile.provider = provider
37
+ profile.id = userId
38
+
39
+ return {
40
+ email: userDoc.email,
41
+ oauth2: OAuth(),
42
+ profile,
43
+ provider,
44
+ providerType: providerType(),
45
+ userId,
46
+ }
47
+ }
48
+
49
+ export function providerType(): SSOProviderType {
50
+ return sample(Object.values(SSOProviderType)) as SSOProviderType
51
+ }
52
+
53
+ export function ssoProfile(user?: User): SSOProfile {
54
+ if (!user) {
55
+ user = shared.user()
56
+ }
57
+ return {
58
+ id: user._id!,
59
+ name: {
60
+ givenName: user.firstName,
61
+ familyName: user.lastName,
62
+ },
63
+ _json: {
64
+ email: user.email,
65
+ picture: "http://test.com",
66
+ },
67
+ provider: generator.string(),
68
+ }
69
+ }
70
+
71
+ // OIDC
72
+
73
+ export function oidcConfig(): OIDCInnerConfig {
74
+ return {
75
+ uuid: uuid(),
76
+ activated: true,
77
+ logo: "",
78
+ name: generator.string(),
79
+ configUrl: "http://someconfigurl",
80
+ clientID: generator.string(),
81
+ clientSecret: generator.string(),
82
+ scopes: [],
83
+ }
84
+ }
85
+
86
+ // response from .well-known/openid-configuration
87
+ export function oidcWellKnownConfig(): OIDCWellKnownConfig {
88
+ return {
89
+ issuer: generator.string(),
90
+ authorization_endpoint: generator.url(),
91
+ token_endpoint: generator.url(),
92
+ userinfo_endpoint: generator.url(),
93
+ }
94
+ }
95
+
96
+ export function jwtClaims(): JwtClaims {
97
+ return {
98
+ email: email(),
99
+ preferred_username: email(),
100
+ }
101
+ }
102
+
103
+ // GOOGLE
104
+
105
+ export function googleConfig(): GoogleInnerConfig {
106
+ return {
107
+ activated: true,
108
+ clientID: generator.string(),
109
+ clientSecret: generator.string(),
110
+ }
111
+ }
112
+
113
+ export function googleConfigDoc(): GoogleConfig {
114
+ return {
115
+ _id: "config_google",
116
+ type: ConfigType.GOOGLE,
117
+ config: googleConfig(),
118
+ }
119
+ }
@@ -0,0 +1,5 @@
1
+ import { newid } from "../../../../src/docIds/newid"
2
+
3
+ export function id() {
4
+ return `tenant-${newid()}`
5
+ }