Merge pull request #13094 from Budibase/fix-flaky-user-test

Fix flaky user test
This commit is contained in:
Adria Navarro 2024-02-21 11:14:31 +01:00 committed by GitHub
commit 6129b047b8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 46 additions and 48 deletions

View File

@ -84,7 +84,8 @@ describe("syncGlobalUsers", () => {
await syncGlobalUsers() await syncGlobalUsers()
const metadata = await rawUserMetadata() 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(metadata).toContainEqual(
expect.objectContaining({ expect.objectContaining({
_id: db.generateUserMetadataID(user1._id!), _id: db.generateUserMetadataID(user1._id!),

View File

@ -76,14 +76,6 @@ mocks.licenses.useUnlimited()
dbInit() dbInit()
type DefaultUserValues = {
globalUserId: string
email: string
firstName: string
lastName: string
csrfToken: string
}
export interface TableToBuild extends Omit<Table, "sourceId" | "sourceType"> { export interface TableToBuild extends Omit<Table, "sourceId" | "sourceType"> {
sourceId?: string sourceId?: string
sourceType?: TableSourceType sourceType?: TableSourceType
@ -99,14 +91,17 @@ export default class TestConfiguration {
prodApp: any prodApp: any
prodAppId: any prodAppId: any
user: any user: any
globalUserId: any
userMetadataId: any userMetadataId: any
table?: Table table?: Table
automation: any automation: any
datasource?: Datasource datasource?: Datasource
tenantId?: string tenantId?: string
defaultUserValues: DefaultUserValues
api: API api: API
csrfToken?: string
private get globalUserId() {
return this.user._id
}
constructor(openServer = true) { constructor(openServer = true) {
if (openServer) { if (openServer) {
@ -121,21 +116,10 @@ export default class TestConfiguration {
} }
this.appId = null this.appId = null
this.allApps = [] this.allApps = []
this.defaultUserValues = this.populateDefaultUserValues()
this.api = new API(this) this.api = new API(this)
} }
populateDefaultUserValues(): DefaultUserValues {
return {
globalUserId: `us_${newid()}`,
email: generator.email(),
firstName: generator.first(),
lastName: generator.last(),
csrfToken: generator.hash(),
}
}
getRequest() { getRequest() {
return this.request return this.request
} }
@ -162,10 +146,10 @@ export default class TestConfiguration {
getUserDetails() { getUserDetails() {
return { return {
globalId: this.defaultUserValues.globalUserId, globalId: this.globalUserId,
email: this.defaultUserValues.email, email: this.user.email,
firstName: this.defaultUserValues.firstName, firstName: this.user.firstName,
lastName: this.defaultUserValues.lastName, lastName: this.user.lastName,
} }
} }
@ -300,15 +284,27 @@ export default class TestConfiguration {
} }
// USER / AUTH // USER / AUTH
async globalUser({ async globalUser(
id = this.defaultUserValues.globalUserId, config: {
firstName = this.defaultUserValues.firstName, id?: string
lastName = this.defaultUserValues.lastName, firstName?: string
lastName?: string
builder?: boolean
admin?: boolean
email?: string
roles?: any
} = {}
): Promise<User> {
const {
id = `us_${newid()}`,
firstName = generator.first(),
lastName = generator.last(),
builder = true, builder = true,
admin = false, admin = false,
email = this.defaultUserValues.email, email = generator.email(),
roles, roles,
}: any = {}): Promise<User> { } = config
const db = tenancy.getTenantDB(this.getTenantId()) const db = tenancy.getTenantDB(this.getTenantId())
let existing let existing
try { try {
@ -327,7 +323,7 @@ export default class TestConfiguration {
await sessions.createASession(id, { await sessions.createASession(id, {
sessionId: "sessionid", sessionId: "sessionid",
tenantId: this.getTenantId(), tenantId: this.getTenantId(),
csrfToken: this.defaultUserValues.csrfToken, csrfToken: this.csrfToken,
}) })
if (builder) { if (builder) {
user.builder = { global: true } user.builder = { global: true }
@ -358,9 +354,9 @@ export default class TestConfiguration {
} = {} } = {}
): Promise<User> { ): Promise<User> {
let { id, firstName, lastName, email, builder, admin, roles } = user let { id, firstName, lastName, email, builder, admin, roles } = user
firstName = firstName || this.defaultUserValues.firstName ;(firstName = firstName || generator.first()),
lastName = lastName || this.defaultUserValues.lastName (lastName = lastName || generator.last()),
email = email || this.defaultUserValues.email (email = email || generator.email())
roles = roles || {} roles = roles || {}
if (builder == null) { if (builder == null) {
builder = true builder = true
@ -448,7 +444,7 @@ export default class TestConfiguration {
defaultHeaders(extras = {}, prodApp = false) { defaultHeaders(extras = {}, prodApp = false) {
const tenantId = this.getTenantId() const tenantId = this.getTenantId()
const authObj: AuthToken = { const authObj: AuthToken = {
userId: this.defaultUserValues.globalUserId, userId: this.globalUserId,
sessionId: "sessionid", sessionId: "sessionid",
tenantId, tenantId,
} }
@ -457,7 +453,7 @@ export default class TestConfiguration {
const headers: any = { const headers: any = {
Accept: "application/json", Accept: "application/json",
Cookie: [`${constants.Cookie.Auth}=${authToken}`], Cookie: [`${constants.Cookie.Auth}=${authToken}`],
[constants.Header.CSRF_TOKEN]: this.defaultUserValues.csrfToken, [constants.Header.CSRF_TOKEN]: this.csrfToken,
Host: this.tenantHost(), Host: this.tenantHost(),
...extras, ...extras,
} }
@ -487,7 +483,7 @@ export default class TestConfiguration {
async basicRoleHeaders() { async basicRoleHeaders() {
return await this.roleHeaders({ return await this.roleHeaders({
email: this.defaultUserValues.email, email: generator.email(),
builder: false, builder: false,
prodApp: true, prodApp: true,
roleId: roles.BUILTIN_ROLE_IDS.BASIC, roleId: roles.BUILTIN_ROLE_IDS.BASIC,
@ -495,7 +491,7 @@ export default class TestConfiguration {
} }
async roleHeaders({ async roleHeaders({
email = this.defaultUserValues.email, email = generator.email(),
roleId = roles.BUILTIN_ROLE_IDS.ADMIN, roleId = roles.BUILTIN_ROLE_IDS.ADMIN,
builder = false, builder = false,
prodApp = true, prodApp = true,
@ -519,11 +515,12 @@ export default class TestConfiguration {
} }
async newTenant(appName = newid()): Promise<App> { async newTenant(appName = newid()): Promise<App> {
this.defaultUserValues = this.populateDefaultUserValues() this.csrfToken = generator.hash()
this.tenantId = structures.tenant.id() this.tenantId = structures.tenant.id()
this.user = await this.globalUser() this.user = await this.globalUser()
this.globalUserId = this.user._id this.userMetadataId = generateUserMetadataID(this.user._id)
this.userMetadataId = generateUserMetadataID(this.globalUserId)
return this.createApp(appName) return this.createApp(appName)
} }
@ -533,7 +530,7 @@ export default class TestConfiguration {
// API // API
async generateApiKey(userId = this.defaultUserValues.globalUserId) { async generateApiKey(userId = this.user._id) {
const db = tenancy.getTenantDB(this.getTenantId()) const db = tenancy.getTenantDB(this.getTenantId())
const id = dbCore.generateDevInfoID(userId) const id = dbCore.generateDevInfoID(userId)
let devInfo: any let devInfo: any