From 04588711e211238dc1e9a27aa4736e81b3606dbd Mon Sep 17 00:00:00 2001 From: Dean Date: Tue, 7 May 2024 09:13:43 +0100 Subject: [PATCH] Refactored out config changes and now excluding on the affected SQS/Multiuser tests --- .../api/routes/public/tests/compare.spec.ts | 2 +- .../src/api/routes/tests/search.spec.ts | 361 +++++++++--------- .../functions/tests/appUrls.spec.js | 2 +- .../functions/tests/tableSettings.spec.ts | 2 +- .../usageQuotas/tests/syncApps.spec.ts | 2 +- .../usageQuotas/tests/syncRows.spec.ts | 2 +- .../src/tests/utilities/TestConfiguration.ts | 28 +- 7 files changed, 193 insertions(+), 206 deletions(-) diff --git a/packages/server/src/api/routes/public/tests/compare.spec.ts b/packages/server/src/api/routes/public/tests/compare.spec.ts index 29d9767b85..ccf248f360 100644 --- a/packages/server/src/api/routes/public/tests/compare.spec.ts +++ b/packages/server/src/api/routes/public/tests/compare.spec.ts @@ -68,7 +68,7 @@ describe("check the applications endpoints", () => { describe("check the tables endpoints", () => { it("should allow retrieving tables through search", async () => { - await config.createApp({ appName: "new app 1" }) + await config.createApp("new app 1") table = await config.upsertTable() const res = await makeRequest("post", "/tables/search") expect(res).toSatisfyApiSpec() diff --git a/packages/server/src/api/routes/tests/search.spec.ts b/packages/server/src/api/routes/tests/search.spec.ts index 45bc4aa6d4..27c07d85be 100644 --- a/packages/server/src/api/routes/tests/search.spec.ts +++ b/packages/server/src/api/routes/tests/search.spec.ts @@ -179,173 +179,178 @@ describe.each([ } // Ensure all bindings resolve and perform as expected - !isSqs && - describe("bindings", () => { - let globalUsers: any = [] + describe("bindings", () => { + let globalUsers: any = [] - const future = new Date(serverTime.getTime()) - future.setDate(future.getDate() + 30) + const future = new Date(serverTime.getTime()) + future.setDate(future.getDate() + 30) - const rows = (currentUser: User) => { - return [ - { name: "foo", appointment: "1982-01-05T00:00:00.000Z" }, - { name: "bar", appointment: "1995-05-06T00:00:00.000Z" }, - { name: currentUser.firstName, appointment: future.toISOString() }, - { name: "serverDate", appointment: serverTime.toISOString() }, - { - name: "single user, session user", - single_user: JSON.stringify([currentUser]), - }, - { - name: "single user", - single_user: JSON.stringify([globalUsers[0]]), - }, - { - name: "multi user", - multi_user: JSON.stringify(globalUsers), - }, - { - name: "multi user with session user", - multi_user: JSON.stringify([...globalUsers, currentUser]), - }, - ] - } + const rows = (currentUser: User) => { + return [ + { name: "foo", appointment: "1982-01-05T00:00:00.000Z" }, + { name: "bar", appointment: "1995-05-06T00:00:00.000Z" }, + { name: currentUser.firstName, appointment: future.toISOString() }, + { name: "serverDate", appointment: serverTime.toISOString() }, + { + name: "single user, session user", + single_user: JSON.stringify([currentUser]), + }, + { + name: "single user", + single_user: JSON.stringify([globalUsers[0]]), + }, + { + name: "multi user", + multi_user: JSON.stringify(globalUsers), + }, + { + name: "multi user with session user", + multi_user: JSON.stringify([...globalUsers, currentUser]), + }, + ] + } - beforeAll(async () => { - // Set up some global users - globalUsers = await Promise.all( - Array(2) - .fill(0) - .map(async () => { - const globalUser = await config.globalUser() - const userMedataId = globalUser._id - ? dbCore.generateUserMetadataID(globalUser._id) - : null - return { - _id: globalUser._id, - _meta: userMedataId, - } - }) - ) + beforeAll(async () => { + // Set up some global users + globalUsers = await Promise.all( + Array(2) + .fill(0) + .map(async () => { + const globalUser = await config.globalUser() + const userMedataId = globalUser._id + ? dbCore.generateUserMetadataID(globalUser._id) + : null + return { + _id: globalUser._id, + _meta: userMedataId, + } + }) + ) - await createTable({ - name: { name: "name", type: FieldType.STRING }, - appointment: { name: "appointment", type: FieldType.DATETIME }, - single_user: { - name: "single_user", - type: FieldType.BB_REFERENCE, - subtype: BBReferenceFieldSubType.USER, - }, - multi_user: { - name: "multi_user", - type: FieldType.BB_REFERENCE, - subtype: BBReferenceFieldSubType.USERS, - }, - }) - await createRows([...rows(config.getUser())]) + await createTable({ + name: { name: "name", type: FieldType.STRING }, + appointment: { name: "appointment", type: FieldType.DATETIME }, + single_user: { + name: "single_user", + type: FieldType.BB_REFERENCE, + subtype: BBReferenceFieldSubType.USER, + }, + multi_user: { + name: "multi_user", + type: FieldType.BB_REFERENCE, + subtype: BBReferenceFieldSubType.USERS, + }, }) + await createRows([...rows(config.getUser())]) + }) - // !! Current User is auto generated per run - it("should return all rows matching the session user firstname", async () => { - await expectQuery({ - equal: { name: "{{ [user].firstName }}" }, - }).toContainExactly([ - { - name: config.getUser().firstName, - appointment: future.toISOString(), + // !! Current User is auto generated per run + it("should return all rows matching the session user firstname", async () => { + await expectQuery({ + equal: { name: "{{ [user].firstName }}" }, + }).toContainExactly([ + { + name: config.getUser().firstName, + appointment: future.toISOString(), + }, + ]) + }) + + it("should parse the date binding and return all rows after the resolved value", async () => { + await expectQuery({ + range: { + appointment: { + low: "{{ [now] }}", + high: "9999-00-00T00:00:00.000Z", }, - ]) - }) + }, + }).toContainExactly([ + { + name: config.getUser().firstName, + appointment: future.toISOString(), + }, + { name: "serverDate", appointment: serverTime.toISOString() }, + ]) + }) - it("should parse the date binding and return all rows after the resolved value", async () => { - await expectQuery({ - range: { - appointment: { - low: "{{ [now] }}", - high: "9999-00-00T00:00:00.000Z", - }, + it("should parse the date binding and return all rows before the resolved value", async () => { + await expectQuery({ + range: { + appointment: { + low: "0000-00-00T00:00:00.000Z", + high: "{{ [now] }}", }, - }).toContainExactly([ - { - name: config.getUser().firstName, - appointment: future.toISOString(), + }, + }).toContainExactly([ + { name: "foo", appointment: "1982-01-05T00:00:00.000Z" }, + { name: "bar", appointment: "1995-05-06T00:00:00.000Z" }, + { name: "serverDate", appointment: serverTime.toISOString() }, + ]) + }) + + it("should parse the encoded js snippet. Return rows with appointments up to 1 week in the past", async () => { + const jsBinding = "return snippets.WeeksAgo();" + const encodedBinding = encodeJSBinding(jsBinding) + + await expectQuery({ + range: { + appointment: { + low: "0000-00-00T00:00:00.000Z", + high: encodedBinding, }, - { name: "serverDate", appointment: serverTime.toISOString() }, - ]) - }) + }, + }).toContainExactly([ + { name: "foo", appointment: "1982-01-05T00:00:00.000Z" }, + { name: "bar", appointment: "1995-05-06T00:00:00.000Z" }, + ]) + }) - it("should parse the date binding and return all rows before the resolved value", async () => { - await expectQuery({ - range: { - appointment: { - low: "0000-00-00T00:00:00.000Z", - high: "{{ [now] }}", - }, + it("should parse the encoded js binding. Return rows with appointments 2 weeks in the past", async () => { + const jsBinding = + "const currentTime = new Date()\ncurrentTime.setDate(currentTime.getDate()-14);\nreturn currentTime.toISOString();" + const encodedBinding = encodeJSBinding(jsBinding) + + await expectQuery({ + range: { + appointment: { + low: "0000-00-00T00:00:00.000Z", + high: encodedBinding, }, - }).toContainExactly([ - { name: "foo", appointment: "1982-01-05T00:00:00.000Z" }, - { name: "bar", appointment: "1995-05-06T00:00:00.000Z" }, - { name: "serverDate", appointment: serverTime.toISOString() }, - ]) - }) + }, + }).toContainExactly([ + { name: "foo", appointment: "1982-01-05T00:00:00.000Z" }, + { name: "bar", appointment: "1995-05-06T00:00:00.000Z" }, + ]) + }) - it("should parse the encoded js snippet. Return rows with appointments up to 1 week in the past", async () => { - const jsBinding = "return snippets.WeeksAgo();" - const encodedBinding = encodeJSBinding(jsBinding) - - await expectQuery({ - range: { - appointment: { - low: "0000-00-00T00:00:00.000Z", - high: encodedBinding, - }, - }, - }).toContainExactly([ - { name: "foo", appointment: "1982-01-05T00:00:00.000Z" }, - { name: "bar", appointment: "1995-05-06T00:00:00.000Z" }, - ]) - }) - - it("should parse the encoded js binding. Return rows with appointments 2 weeks in the past", async () => { - const jsBinding = - "const currentTime = new Date()\ncurrentTime.setDate(currentTime.getDate()-14);\nreturn currentTime.toISOString();" - const encodedBinding = encodeJSBinding(jsBinding) - - await expectQuery({ - range: { - appointment: { - low: "0000-00-00T00:00:00.000Z", - high: encodedBinding, - }, - }, - }).toContainExactly([ - { name: "foo", appointment: "1982-01-05T00:00:00.000Z" }, - { name: "bar", appointment: "1995-05-06T00:00:00.000Z" }, - ]) - }) - - it("should match a single user row by the session user id", async () => { - await expectQuery({ - equal: { single_user: "{{ [user]._id }}" }, - }).toContainExactly([ - { - name: "single user, session user", - single_user: [{ _id: config.getUser()._id }], - }, - ]) - }) + it("should match a single user row by the session user id", async () => { + await expectQuery({ + equal: { single_user: "{{ [user]._id }}" }, + }).toContainExactly([ + { + name: "single user, session user", + single_user: [{ _id: config.getUser()._id }], + }, + ]) + }) + !isSqs && it("should match the session user id in a multi user field", async () => { + const allUsers = [...globalUsers, config.getUser()].map((user: any) => { + return { _id: user._id } + }) + await expectQuery({ contains: { multi_user: ["{{ [user]._id }}"] }, }).toContainExactly([ { name: "multi user with session user", - multi_user: [{ _id: config.getUser()._id }], + multi_user: allUsers, }, ]) }) + !isSqs && it("should not match the session user id in a multi user field", async () => { await expectQuery({ notContains: { multi_user: ["{{ [user]._id }}"] }, @@ -360,43 +365,43 @@ describe.each([ ]) }) - it("should match the session user id and a user table row id using helpers, user binding and a static user id.", async () => { - await expectQuery({ - oneOf: { - single_user: [ - "{{ default [user]._id '_empty_' }}", - globalUsers[0]._id, - ], - }, - }).toContainExactly([ - { - name: "single user, session user", - single_user: [{ _id: config.getUser()._id }], - }, - { - name: "single user", - single_user: [{ _id: globalUsers[0]._id }], - }, - ]) - }) - - it("should resolve 'default' helper to '_empty_' when binding resolves to nothing", async () => { - await expectQuery({ - oneOf: { - single_user: [ - "{{ default [user]._idx '_empty_' }}", - globalUsers[0]._id, - ], - }, - }).toContainExactly([ - { - name: "single user", - single_user: [{ _id: globalUsers[0]._id }], - }, - ]) - }) + it("should match the session user id and a user table row id using helpers, user binding and a static user id.", async () => { + await expectQuery({ + oneOf: { + single_user: [ + "{{ default [user]._id '_empty_' }}", + globalUsers[0]._id, + ], + }, + }).toContainExactly([ + { + name: "single user, session user", + single_user: [{ _id: config.getUser()._id }], + }, + { + name: "single user", + single_user: [{ _id: globalUsers[0]._id }], + }, + ]) }) + it("should resolve 'default' helper to '_empty_' when binding resolves to nothing", async () => { + await expectQuery({ + oneOf: { + single_user: [ + "{{ default [user]._idx '_empty_' }}", + globalUsers[0]._id, + ], + }, + }).toContainExactly([ + { + name: "single user", + single_user: [{ _id: globalUsers[0]._id }], + }, + ]) + }) + }) + describe.each([FieldType.STRING, FieldType.LONGFORM])("%s", () => { beforeAll(async () => { await createTable({ diff --git a/packages/server/src/migrations/functions/tests/appUrls.spec.js b/packages/server/src/migrations/functions/tests/appUrls.spec.js index 3d48515068..7a2f324552 100644 --- a/packages/server/src/migrations/functions/tests/appUrls.spec.js +++ b/packages/server/src/migrations/functions/tests/appUrls.spec.js @@ -13,7 +13,7 @@ describe("run", () => { afterAll(config.end) it("runs successfully", async () => { - const app = await config.createApp({ appName: "testApp" }) + const app = await config.createApp("testApp") const metadata = await dbCore.doWithDB(app.appId, async db => { const metadataDoc = await db.get(dbCore.DocumentType.APP_METADATA) delete metadataDoc.url diff --git a/packages/server/src/migrations/functions/tests/tableSettings.spec.ts b/packages/server/src/migrations/functions/tests/tableSettings.spec.ts index 84e26c80f3..8d28a43322 100644 --- a/packages/server/src/migrations/functions/tests/tableSettings.spec.ts +++ b/packages/server/src/migrations/functions/tests/tableSettings.spec.ts @@ -11,7 +11,7 @@ describe("run", () => { beforeAll(async () => { await config.init() - app = await config.createApp({ appName: "testApp" }) + app = await config.createApp("testApp") screen = await config.createScreen() }) diff --git a/packages/server/src/migrations/functions/usageQuotas/tests/syncApps.spec.ts b/packages/server/src/migrations/functions/usageQuotas/tests/syncApps.spec.ts index 3b6d7b66b2..1d4d4d0f71 100644 --- a/packages/server/src/migrations/functions/usageQuotas/tests/syncApps.spec.ts +++ b/packages/server/src/migrations/functions/usageQuotas/tests/syncApps.spec.ts @@ -22,7 +22,7 @@ describe("syncApps", () => { expect(usageDoc.usageQuota.apps).toEqual(3) // create an extra app to test the migration - await config.createApp({ appName: "quota-test" }) + await config.createApp("quota-test") // migrate await syncApps.run() diff --git a/packages/server/src/migrations/functions/usageQuotas/tests/syncRows.spec.ts b/packages/server/src/migrations/functions/usageQuotas/tests/syncRows.spec.ts index cc3daa465f..730278683c 100644 --- a/packages/server/src/migrations/functions/usageQuotas/tests/syncRows.spec.ts +++ b/packages/server/src/migrations/functions/usageQuotas/tests/syncRows.spec.ts @@ -29,7 +29,7 @@ describe("syncRows", () => { await config.createRow() }) // app 2 - const app2 = await config.createApp({ appName: "second-app" }) + const app2 = await config.createApp("second-app") await context.doInAppContext(app2.appId, async () => { await config.createTable() await config.createRow() diff --git a/packages/server/src/tests/utilities/TestConfiguration.ts b/packages/server/src/tests/utilities/TestConfiguration.ts index 20fdcdfed7..58dbecd197 100644 --- a/packages/server/src/tests/utilities/TestConfiguration.ts +++ b/packages/server/src/tests/utilities/TestConfiguration.ts @@ -224,14 +224,11 @@ export default class TestConfiguration { // SETUP / TEARDOWN // use a new id as the name to avoid name collisions - async init(opts = {}) { + async init(appName = newid()) { if (!this.started) { await startup() } - return this.newTenant({ - appName: newid(), - ...opts, - }) + return this.newTenant(appName) } end() { @@ -551,14 +548,14 @@ export default class TestConfiguration { return this.tenantId } - async newTenant(opts: {}): Promise { + async newTenant(appName = newid()): Promise { this.csrfToken = generator.hash() this.tenantId = structures.tenant.id() this.user = await this.globalUser() this.userMetadataId = generateUserMetadataID(this.user._id!) - return this.createApp({ appName: newid(), ...opts }) + return this.createApp(appName) } doInTenant(task: () => T) { @@ -588,9 +585,7 @@ export default class TestConfiguration { } // APP - async createApp(opts: CreateAppRequest): Promise { - const { appName, url, snippets } = opts - + async createApp(appName: string, url?: string): Promise { this.appId = undefined this.app = await context.doInTenant( this.tenantId!, @@ -602,19 +597,6 @@ export default class TestConfiguration { ) this.appId = this.app.appId - if (snippets) { - this.app = await this._req( - appController.update, - { - ...this.app, - snippets, - }, - { - appId: this.appId, - } - ) - } - return await context.doInAppContext(this.app.appId!, async () => { // create production app this.prodApp = await this.publish()