From e988890a7ee6ee9458218d913788101e2cf2b523 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 21 Feb 2024 10:12:06 +0100 Subject: [PATCH 1/5] Remove defaultUserValues from test config --- .../src/tests/utilities/TestConfiguration.ts | 81 ++++++++----------- 1 file changed, 33 insertions(+), 48 deletions(-) diff --git a/packages/server/src/tests/utilities/TestConfiguration.ts b/packages/server/src/tests/utilities/TestConfiguration.ts index ea3204536a..00550c2c24 100644 --- a/packages/server/src/tests/utilities/TestConfiguration.ts +++ b/packages/server/src/tests/utilities/TestConfiguration.ts @@ -76,14 +76,6 @@ mocks.licenses.useUnlimited() dbInit() -type DefaultUserValues = { - globalUserId: string - email: string - firstName: string - lastName: string - csrfToken: string -} - export interface TableToBuild extends Omit { sourceId?: string sourceType?: TableSourceType @@ -105,8 +97,8 @@ export default class TestConfiguration { automation: any datasource?: Datasource tenantId?: string - defaultUserValues: DefaultUserValues api: API + csrfToken?: string constructor(openServer = true) { if (openServer) { @@ -121,21 +113,10 @@ export default class TestConfiguration { } this.appId = null this.allApps = [] - this.defaultUserValues = this.populateDefaultUserValues() this.api = new API(this) } - populateDefaultUserValues(): DefaultUserValues { - return { - globalUserId: `us_${newid()}`, - email: generator.email(), - firstName: generator.first(), - lastName: generator.last(), - csrfToken: generator.hash(), - } - } - getRequest() { return this.request } @@ -160,15 +141,6 @@ export default class TestConfiguration { return this.prodAppId } - getUserDetails() { - return { - globalId: this.defaultUserValues.globalUserId, - email: this.defaultUserValues.email, - firstName: this.defaultUserValues.firstName, - lastName: this.defaultUserValues.lastName, - } - } - async doInContext( appId: string | null, task: () => Promise @@ -300,15 +272,27 @@ export default class TestConfiguration { } // USER / AUTH - async globalUser({ - id = this.defaultUserValues.globalUserId, - firstName = this.defaultUserValues.firstName, - lastName = this.defaultUserValues.lastName, - builder = true, - admin = false, - email = this.defaultUserValues.email, - roles, - }: any = {}): Promise { + async globalUser( + config: { + id?: string + firstName?: string + lastName?: string + builder?: boolean + admin?: boolean + email?: string + roles?: any + } = {} + ): Promise { + const { + id = `us_${newid()}`, + firstName = generator.first(), + lastName = generator.last(), + builder = true, + admin = false, + email, + roles, + } = config + const db = tenancy.getTenantDB(this.getTenantId()) let existing try { @@ -327,7 +311,7 @@ export default class TestConfiguration { await sessions.createASession(id, { sessionId: "sessionid", tenantId: this.getTenantId(), - csrfToken: this.defaultUserValues.csrfToken, + csrfToken: this.csrfToken, }) if (builder) { user.builder = { global: true } @@ -358,9 +342,9 @@ export default class TestConfiguration { } = {} ): Promise { let { id, firstName, lastName, email, builder, admin, roles } = user - firstName = firstName || this.defaultUserValues.firstName - lastName = lastName || this.defaultUserValues.lastName - email = email || this.defaultUserValues.email + ;(firstName = firstName || generator.first()), + (lastName = lastName || generator.last()), + (email = email || generator.email()) roles = roles || {} if (builder == null) { builder = true @@ -448,7 +432,7 @@ export default class TestConfiguration { defaultHeaders(extras = {}, prodApp = false) { const tenantId = this.getTenantId() const authObj: AuthToken = { - userId: this.defaultUserValues.globalUserId, + userId: this.user.globalUserId, sessionId: "sessionid", tenantId, } @@ -457,7 +441,7 @@ export default class TestConfiguration { const headers: any = { Accept: "application/json", Cookie: [`${constants.Cookie.Auth}=${authToken}`], - [constants.Header.CSRF_TOKEN]: this.defaultUserValues.csrfToken, + [constants.Header.CSRF_TOKEN]: this.csrfToken, Host: this.tenantHost(), ...extras, } @@ -487,7 +471,7 @@ export default class TestConfiguration { async basicRoleHeaders() { return await this.roleHeaders({ - email: this.defaultUserValues.email, + email: generator.email(), builder: false, prodApp: true, roleId: roles.BUILTIN_ROLE_IDS.BASIC, @@ -495,7 +479,7 @@ export default class TestConfiguration { } async roleHeaders({ - email = this.defaultUserValues.email, + email = generator.email(), roleId = roles.BUILTIN_ROLE_IDS.ADMIN, builder = false, prodApp = true, @@ -519,11 +503,12 @@ export default class TestConfiguration { } async newTenant(appName = newid()): Promise { - this.defaultUserValues = this.populateDefaultUserValues() this.tenantId = structures.tenant.id() this.user = await this.globalUser() this.globalUserId = this.user._id this.userMetadataId = generateUserMetadataID(this.globalUserId) + + this.csrfToken = generator.hash() return this.createApp(appName) } @@ -533,7 +518,7 @@ export default class TestConfiguration { // API - async generateApiKey(userId = this.defaultUserValues.globalUserId) { + async generateApiKey(userId = this.user.globalUserId) { const db = tenancy.getTenantDB(this.getTenantId()) const id = dbCore.generateDevInfoID(userId) let devInfo: any From f6e968efe848b568b8edeb59a17bf764e8a506a1 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 21 Feb 2024 10:18:46 +0100 Subject: [PATCH 2/5] Fix test --- packages/server/src/sdk/users/tests/utils.spec.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/server/src/sdk/users/tests/utils.spec.ts b/packages/server/src/sdk/users/tests/utils.spec.ts index 86dc411caf..efe790d49b 100644 --- a/packages/server/src/sdk/users/tests/utils.spec.ts +++ b/packages/server/src/sdk/users/tests/utils.spec.ts @@ -84,7 +84,8 @@ describe("syncGlobalUsers", () => { await syncGlobalUsers() const metadata = await rawUserMetadata() - expect(metadata).toHaveLength(2) + + expect(metadata).toHaveLength(2 + 1) // ADMIN user created in test bootstrap still in the application expect(metadata).toContainEqual( expect.objectContaining({ _id: db.generateUserMetadataID(user1._id!), @@ -121,7 +122,7 @@ describe("syncGlobalUsers", () => { await syncGlobalUsers() const metadata = await rawUserMetadata() - expect(metadata).toHaveLength(1) //ADMIN user created in test bootstrap still in the application + expect(metadata).toHaveLength(1) // ADMIN user created in test bootstrap still in the application }) }) }) From 0b5226413b7d76353685c1e70eb9eb18c1a7f922 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 21 Feb 2024 10:36:17 +0100 Subject: [PATCH 3/5] Fix ids --- .../src/tests/utilities/TestConfiguration.ts | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/packages/server/src/tests/utilities/TestConfiguration.ts b/packages/server/src/tests/utilities/TestConfiguration.ts index 00550c2c24..0204eeb178 100644 --- a/packages/server/src/tests/utilities/TestConfiguration.ts +++ b/packages/server/src/tests/utilities/TestConfiguration.ts @@ -91,7 +91,6 @@ export default class TestConfiguration { prodApp: any prodAppId: any user: any - globalUserId: any userMetadataId: any table?: Table automation: any @@ -100,6 +99,10 @@ export default class TestConfiguration { api: API csrfToken?: string + private get globalUserId() { + return this.user._id + } + constructor(openServer = true) { if (openServer) { // use a random port because it doesn't matter @@ -141,6 +144,15 @@ export default class TestConfiguration { return this.prodAppId } + getUserDetails() { + return { + globalId: this.globalUserId, + email: this.user.email, + firstName: this.user.firstName, + lastName: this.user.lastName, + } + } + async doInContext( appId: string | null, task: () => Promise @@ -432,7 +444,7 @@ export default class TestConfiguration { defaultHeaders(extras = {}, prodApp = false) { const tenantId = this.getTenantId() const authObj: AuthToken = { - userId: this.user.globalUserId, + userId: this.globalUserId, sessionId: "sessionid", tenantId, } @@ -505,8 +517,7 @@ export default class TestConfiguration { async newTenant(appName = newid()): Promise { this.tenantId = structures.tenant.id() this.user = await this.globalUser() - this.globalUserId = this.user._id - this.userMetadataId = generateUserMetadataID(this.globalUserId) + this.userMetadataId = generateUserMetadataID(this.user._id) this.csrfToken = generator.hash() return this.createApp(appName) @@ -518,7 +529,7 @@ export default class TestConfiguration { // API - async generateApiKey(userId = this.user.globalUserId) { + async generateApiKey(userId = this.user._id) { const db = tenancy.getTenantDB(this.getTenantId()) const id = dbCore.generateDevInfoID(userId) let devInfo: any From f7d5ccee3c95a68f406d3e3ced363dfd53cd50fb Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 21 Feb 2024 10:42:36 +0100 Subject: [PATCH 4/5] Fix setting up email --- packages/server/src/tests/utilities/TestConfiguration.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/server/src/tests/utilities/TestConfiguration.ts b/packages/server/src/tests/utilities/TestConfiguration.ts index 0204eeb178..5b20f83d77 100644 --- a/packages/server/src/tests/utilities/TestConfiguration.ts +++ b/packages/server/src/tests/utilities/TestConfiguration.ts @@ -301,7 +301,7 @@ export default class TestConfiguration { lastName = generator.last(), builder = true, admin = false, - email, + email = generator.email(), roles, } = config From f68e7359c6379096a55db163bb07bb4b6492853f Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 21 Feb 2024 10:57:49 +0100 Subject: [PATCH 5/5] Fix csrf usage --- packages/server/src/tests/utilities/TestConfiguration.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/server/src/tests/utilities/TestConfiguration.ts b/packages/server/src/tests/utilities/TestConfiguration.ts index 5b20f83d77..bbe3260d26 100644 --- a/packages/server/src/tests/utilities/TestConfiguration.ts +++ b/packages/server/src/tests/utilities/TestConfiguration.ts @@ -515,11 +515,12 @@ export default class TestConfiguration { } 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) - this.csrfToken = generator.hash() return this.createApp(appName) }