Skip to content

Commit 9a36015

Browse files
committed
fix(tom-server): updates null detection and mock ordering
Signed-off-by: Pierre 'McFly' Marty <[email protected]>
1 parent c1411ad commit 9a36015

File tree

2 files changed

+59
-29
lines changed

2 files changed

+59
-29
lines changed

packages/tom-server/src/user-info-api/services/index.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,7 @@ class UserInfoService implements IUserInfoService {
114114
})()
115115

116116
const directoryPromise = (async () => {
117-
if (
118-
!this.enableAdditionalFeatures &&
119-
process.env.ADDITIONAL_FEATURES !== 'true'
120-
) {
117+
if (!this.enableAdditionalFeatures) {
121118
return null
122119
}
123120
const rows = (await this.userDb.db.get(
@@ -129,10 +126,7 @@ class UserInfoService implements IUserInfoService {
129126
})()
130127

131128
const settingsPromise = (async () => {
132-
if (
133-
!this.enableCommonSettings &&
134-
process.env.FEATURE_COMMON_SETTINGS_ENABLED !== 'true'
135-
) {
129+
if (!this.enableCommonSettings) {
136130
return null
137131
}
138132
const rows = (await this.db.get('usersettings', ['*'], {
@@ -177,16 +171,20 @@ class UserInfoService implements IUserInfoService {
177171
}
178172

179173
if (settingsRow) {
180-
Object.assign(result, {
181-
language: settingsRow.settings.language ?? '',
182-
timezone: settingsRow.settings.timezone ?? ''
183-
})
174+
if (settingsRow.settings.language)
175+
result.language = settingsRow.settings.language
176+
if (settingsRow.settings.timezone)
177+
result.timezone = settingsRow.settings.timezone
184178
}
185179

186-
if (Object.keys(result).length === 1 && result.uid != null) {
180+
const finalResult = Object.fromEntries(
181+
Object.entries(result).filter(([_, v]) => v != null)
182+
)
183+
184+
if (Object.keys(finalResult).length === 1 && finalResult.uid != null) {
187185
return null
188186
}
189-
return result as UserInformation
187+
return finalResult as unknown as UserInformation
190188
} catch (error) {
191189
throw new Error(
192190
`Error getting user info ${JSON.stringify({ cause: error })}`,

packages/tom-server/src/user-info-api/tests/service.test.ts

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,38 @@ describe('user info service', () => {
288288
expect(user).toHaveProperty('timezone', 'Europe/Paris')
289289
})
290290

291+
it('returns user info but without language/timezone if common settings is enabled but common settings service returns no values', async () => {
292+
const configWithCommon = {
293+
...config,
294+
features: { common_settings: { enabled: true } }
295+
} as unknown as Config
296+
297+
const mockTwakeDBNoSettings = {
298+
...twakeDBMock,
299+
get: jest.fn().mockImplementation(async (table, _fields, query) => {
300+
if (table === 'usersettings' && query.matrix_id != null) {
301+
return []
302+
}
303+
return []
304+
})
305+
}
306+
307+
const svc = new UserInfoService(
308+
userDb,
309+
mockTwakeDBNoSettings as unknown as TwakeDB,
310+
matrixDBMock as unknown as MatrixDB,
311+
configWithCommon,
312+
logger
313+
)
314+
315+
const user = await svc.get('@dwho:docker.localhost')
316+
317+
expect(user).not.toBeNull()
318+
expect(user).toHaveProperty('display_name', 'Dr Who')
319+
expect(user).not.toHaveProperty('language')
320+
expect(user).not.toHaveProperty('timezone')
321+
})
322+
291323
it('propagates avatar_url as avatar when present', async () => {
292324
;(matrixDBMock.get as jest.Mock).mockResolvedValueOnce([
293325
{ displayname: 'Dr Who', avatar_url: 'http://example.com/avatar.png' }
@@ -323,8 +355,8 @@ describe('user info service', () => {
323355
getSpy.mockRestore()
324356
})
325357

326-
it('passes the exact matrix id to the usersettings query', async () => {
327-
// Create a service instance with the commonsettings feature turned ON
358+
it('passes the exact matrix id to the usersettings query (when common settings enabled)', async () => {
359+
// Create a service instance with the common-settings feature turned ON
328360
const cfg = {
329361
...config,
330362
features: { common_settings: { enabled: true } }
@@ -338,16 +370,16 @@ describe('user info service', () => {
338370
logger
339371
)
340372

341-
// spy on the DB mock (twakeDBMock.get is already a jest.fn)
342-
const spy = jest.spyOn(twakeDBMock, 'get')
373+
twakeDBMock.get.mockClear()
374+
343375
await svc.get('@dwho:docker.localhost')
344-
expect(spy).toHaveBeenCalledWith('usersettings', ['*'], {
376+
377+
expect(twakeDBMock.get).toHaveBeenCalledWith('usersettings', ['*'], {
345378
matrix_id: '@dwho:docker.localhost'
346379
})
347-
spy.mockRestore()
348380
})
349381

350-
it('rethrows a wrapped error when an internal query fails', async () => {
382+
it('re-throws a wrapped error when an internal query fails', async () => {
351383
const brokenMatrix = {
352384
get: jest.fn().mockRejectedValue(new Error('boom'))
353385
} as unknown as MatrixDB
@@ -365,21 +397,29 @@ describe('user info service', () => {
365397
})
366398

367399
it('honors the env var FEATURE_COMMON_SETTINGS_ENABLED', async () => {
400+
// Set the environment variable BEFORE creating the service instance
368401
process.env.FEATURE_COMMON_SETTINGS_ENABLED = 'true'
402+
369403
const cfg = {
370404
...config,
371405
features: { common_settings: { enabled: false } } as any
372406
}
407+
408+
// New service instance created AFTER env var is set
373409
const svc = new UserInfoService(
374410
userDb,
375411
twakeDBMock as unknown as TwakeDB,
376412
matrixDBMock as unknown as MatrixDB,
377413
cfg as unknown as Config,
378414
logger
379415
)
416+
380417
const user = await svc.get('@dwho:docker.localhost')
418+
expect(user).not.toBeNull()
381419
expect(user).toHaveProperty('language', 'fr')
382420
expect(user).toHaveProperty('timezone', 'Europe/Paris')
421+
422+
// Clean up the environment variable
383423
delete process.env.FEATURE_COMMON_SETTINGS_ENABLED
384424
})
385425

@@ -419,12 +459,4 @@ describe('user info service', () => {
419459
expect(user?.sn).toBe('One')
420460
expect(user?.givenName).toBe('Alpha')
421461
})
422-
423-
it('passes the exact matrix id to the usersettings query', async () => {
424-
const spy = jest.spyOn(twakeDBMock, 'get')
425-
await service.get('@dwho:docker.localhost')
426-
expect(spy).toHaveBeenCalledWith('usersettings', ['*'], {
427-
matrix_id: '@dwho:docker.localhost'
428-
})
429-
})
430462
})

0 commit comments

Comments
 (0)