Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
Copy link
Copy Markdown
Member Author

@csr csr Mar 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test file was introduced in #249173 and doesn't exist on 9.3 (the PR wasn't backported). Is it OK if we introduce it in this backport PR? It adds coverage related to the change.

* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { httpServerMock, loggingSystemMock } from '@kbn/core/server/mocks';
import type { SecurityPluginStart } from '@kbn/security-plugin/server';
import { getUserInfo } from './get_user_info';

describe('getUserInfo', () => {
const logger = loggingSystemMock.createLogger();
const request = httpServerMock.createKibanaRequest();

it('returns undefined when security is unavailable', async () => {
const result = await getUserInfo({ request, logger });

expect(result).toBeUndefined();
});

it('returns user profile info when available', async () => {
const security = {
userProfiles: {
getCurrent: jest.fn().mockResolvedValue({
uid: 'profile-1',
user: {
username: 'user-name',
full_name: 'Full Name',
email: 'user@example.com',
},
}),
},
authc: {
getCurrentUser: jest.fn(),
},
} as unknown as SecurityPluginStart;

const result = await getUserInfo({ request, security, logger });

expect(result).toEqual({
username: 'Full Name',
full_name: 'Full Name',
email: 'user@example.com',
profile_uid: 'profile-1',
});
});

it('falls back to authc when user profile lookup fails', async () => {
const security = {
userProfiles: {
getCurrent: jest.fn().mockRejectedValue(new Error('failed')),
},
authc: {
getCurrentUser: jest.fn().mockReturnValue({
username: 'fallback-user',
full_name: null,
email: 'fallback@example.com',
}),
},
} as unknown as SecurityPluginStart;

const result = await getUserInfo({ request, security, logger });

expect(result).toEqual({
username: 'fallback@example.com',
full_name: null,
email: 'fallback@example.com',
profile_uid: null,
});
});

it('uses profile_uid from authc fallback when available', async () => {
const security = {
userProfiles: {
getCurrent: jest.fn().mockResolvedValue(null),
},
authc: {
getCurrentUser: jest.fn().mockReturnValue({
username: 'cloud-user',
full_name: null,
email: 'cloud-user@example.com',
profile_uid: 'u_cloud_profile_123',
}),
},
} as unknown as SecurityPluginStart;

const result = await getUserInfo({ request, security, logger });

expect(result).toEqual({
username: 'cloud-user@example.com',
full_name: null,
email: 'cloud-user@example.com',
profile_uid: 'u_cloud_profile_123',
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export const getUserInfo = async ({
username: displayName,
full_name: user.full_name ?? null,
email: user.email ?? null,
profile_uid: null,
profile_uid: user.profile_uid ?? null,
};
}
} catch (error) {
Expand Down
Loading