Skip to content

Commit

Permalink
[CI] Auto-commit changed files from 'node scripts/eslint --no-cache -…
Browse files Browse the repository at this point in the history
…-fix'
  • Loading branch information
kibanamachine committed Dec 7, 2023
1 parent 405dde8 commit c4ead20
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 94 deletions.
193 changes: 103 additions & 90 deletions packages/kbn-test/src/auth/sesson_manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,133 +16,146 @@ import { Role, User } from './types';

const log = new ToolingLog();

const cookieInstance = Cookie.parse('sid=kbn_cookie_value; Path=/; Expires=Wed, 01 Oct 2023 07:00:00 GMT')!;
const cookieInstance = Cookie.parse(
'sid=kbn_cookie_value; Path=/; Expires=Wed, 01 Oct 2023 07:00:00 GMT'
)!;
const email = '[email protected]';
const fullname = 'Test User';

const cloudCookieInstance = Cookie.parse('sid=cloud_cookie_value; Path=/; Expires=Wed, 01 Oct 2023 07:00:00 GMT')!;
const cloudCookieInstance = Cookie.parse(
'sid=cloud_cookie_value; Path=/; Expires=Wed, 01 Oct 2023 07:00:00 GMT'
)!;
const cloudEmail = '[email protected]';
const cloudFullname = 'Test Viewer';

const cloudUsers = new Array<[Role, User]>();
cloudUsers.push(['viewer', {email: "[email protected]", password: "p1234"}])
cloudUsers.push(['admin', {email: "[email protected]", password: "p1234"}])
cloudUsers.push(['viewer', { email: '[email protected]', password: 'p1234' }]);
cloudUsers.push(['admin', { email: '[email protected]', password: 'p1234' }]);

const createLocalSAMLSessionMock = jest.spyOn(samlAuth, 'createLocalSAMLSession');
const createCloudSAMLSessionMock = jest.spyOn(samlAuth, 'createCloudSAMLSession');
const readCloudUsersFromFileMock = jest.spyOn(helper, 'readCloudUsersFromFile');

jest.mock('../kbn_client/kbn_client', () => {
return {
KbnClient : jest.fn(),
};
})
return {
KbnClient: jest.fn(),
};
});
const get = jest.fn();

beforeEach(() => {
jest.resetAllMocks();
jest.requireMock('../kbn_client/kbn_client').KbnClient.mockImplementation(() => ({ version: { get } }));
jest
.requireMock('../kbn_client/kbn_client')
.KbnClient.mockImplementation(() => ({ version: { get } }));
get.mockImplementationOnce(() => Promise.resolve('8.12.0'));

createLocalSAMLSessionMock.mockResolvedValue(new Session(cookieInstance, email, fullname));
createCloudSAMLSessionMock.mockResolvedValue(new Session(cloudCookieInstance, cloudEmail, cloudFullname));
createCloudSAMLSessionMock.mockResolvedValue(
new Session(cloudCookieInstance, cloudEmail, cloudFullname)
);
readCloudUsersFromFileMock.mockReturnValue(cloudUsers);

});

describe('SamlSessionManager', () => {
describe('for local session', () => {
const hostOptions = {
protocol: 'http' as "http" | "https",
hostname: 'localhost',
port: 5620,
username: 'elastic',
password: 'changeme',
}
protocol: 'http' as 'http' | 'https',
hostname: 'localhost',
port: 5620,
username: 'elastic',
password: 'changeme',
};
const isCloud = false;
test('should create an instance of SamlSessionManager', () => {
const samlSessionManager = new SamlSessionManager({hostOptions, log, isCloud});
expect(samlSessionManager).toBeInstanceOf(SamlSessionManager);
const samlSessionManager = new SamlSessionManager({ hostOptions, log, isCloud });
expect(samlSessionManager).toBeInstanceOf(SamlSessionManager);
});

test(`'getSessionCookieForRole' should return the actual cookie value`, async () => {
const samlSessionManager = new SamlSessionManager({hostOptions, log, isCloud});
const cookie = await samlSessionManager.getSessionCookieForRole('tester');
expect(cookie).toBe(cookieInstance.value);
});

test(`'getApiCredentialsForRole' should return {Cookie: <cookieString>}`, async () => {
const samlSessionManager = new SamlSessionManager({hostOptions, log, isCloud});
const credentials = await samlSessionManager.getApiCredentialsForRole('tester')
expect(credentials).toEqual({Cookie: `${cookieInstance.cookieString()}`});
});

test(`'getSessionCookieForRole' should call 'createLocalSAMLSession' only once for the same role`, async () => {
const samlSessionManager = new SamlSessionManager({hostOptions, log, isCloud});
await samlSessionManager.getSessionCookieForRole('tester');
await samlSessionManager.getSessionCookieForRole('admin');
await samlSessionManager.getSessionCookieForRole('tester');
expect(createLocalSAMLSessionMock.mock.calls).toHaveLength(2);
expect(createCloudSAMLSessionMock.mock.calls).toHaveLength(0);

});

test(`'getUserData' should return the correct email & fullname`, async () => {
const samlSessionManager = new SamlSessionManager({hostOptions, log, isCloud});
const data = await samlSessionManager.getUserData('tester');
expect(data).toEqual({email, fullname})
});
})
const samlSessionManager = new SamlSessionManager({ hostOptions, log, isCloud });
const cookie = await samlSessionManager.getSessionCookieForRole('tester');
expect(cookie).toBe(cookieInstance.value);
});

test(`'getApiCredentialsForRole' should return {Cookie: <cookieString>}`, async () => {
const samlSessionManager = new SamlSessionManager({ hostOptions, log, isCloud });
const credentials = await samlSessionManager.getApiCredentialsForRole('tester');
expect(credentials).toEqual({ Cookie: `${cookieInstance.cookieString()}` });
});

test(`'getSessionCookieForRole' should call 'createLocalSAMLSession' only once for the same role`, async () => {
const samlSessionManager = new SamlSessionManager({ hostOptions, log, isCloud });
await samlSessionManager.getSessionCookieForRole('tester');
await samlSessionManager.getSessionCookieForRole('admin');
await samlSessionManager.getSessionCookieForRole('tester');
expect(createLocalSAMLSessionMock.mock.calls).toHaveLength(2);
expect(createCloudSAMLSessionMock.mock.calls).toHaveLength(0);
});

test(`'getUserData' should return the correct email & fullname`, async () => {
const samlSessionManager = new SamlSessionManager({ hostOptions, log, isCloud });
const data = await samlSessionManager.getUserData('tester');
expect(data).toEqual({ email, fullname });
});
});

describe('for cloud session', () => {
const hostOptions = {
protocol: 'https' as "http" | "https",
hostname: 'cloud',
username: 'elastic',
password: 'changeme',
}
protocol: 'https' as 'http' | 'https',
hostname: 'cloud',
username: 'elastic',
password: 'changeme',
};
const isCloud = true;
test('should create an instance of SamlSessionManager', () => {
const samlSessionManager = new SamlSessionManager({hostOptions, log, isCloud});
expect(samlSessionManager).toBeInstanceOf(SamlSessionManager);
const samlSessionManager = new SamlSessionManager({ hostOptions, log, isCloud });
expect(samlSessionManager).toBeInstanceOf(SamlSessionManager);
});

test(`'getSessionCookieForRole' should return the actual cookie value`, async () => {
const samlSessionManager = new SamlSessionManager({hostOptions, log, isCloud});
createCloudSAMLSessionMock.mockResolvedValue(new Session(cloudCookieInstance, cloudEmail, cloudFullname));
const cookie = await samlSessionManager.getSessionCookieForRole('viewer');
expect(cookie).toBe(cloudCookieInstance.value);
});

test(`'getApiCredentialsForRole' should return {Cookie: <cookieString>}`, async () => {
const samlSessionManager = new SamlSessionManager({hostOptions, log, isCloud});
const credentials = await samlSessionManager.getApiCredentialsForRole('viewer')
expect(credentials).toEqual({Cookie: `${cloudCookieInstance.cookieString()}`});
});

test(`'getSessionCookieForRole' should call 'createCloudSAMLSession' only once for the same role`, async () => {
const samlSessionManager = new SamlSessionManager({hostOptions, log, isCloud});
await samlSessionManager.getSessionCookieForRole('viewer');
await samlSessionManager.getSessionCookieForRole('admin');
await samlSessionManager.getSessionCookieForRole('viewer');
expect(createLocalSAMLSessionMock.mock.calls).toHaveLength(0);
expect(createCloudSAMLSessionMock.mock.calls).toHaveLength(2);
});

test(`'getUserData' should return the correct email & fullname`, async () => {
const samlSessionManager = new SamlSessionManager({hostOptions, log, isCloud});
const data = await samlSessionManager.getUserData('viewer');
expect(data).toEqual({email: cloudEmail, fullname: cloudFullname})
});

test(`throws error when roles does not exist`, async () => {
const nonExistingRole = 'tester';
const samlSessionManager = new SamlSessionManager({hostOptions, log, isCloud});
await expect(samlSessionManager.getSessionCookieForRole(nonExistingRole)).rejects.toThrow(`User with '${nonExistingRole}' role is not defined`);
await expect(samlSessionManager.getApiCredentialsForRole(nonExistingRole)).rejects.toThrow(`User with '${nonExistingRole}' role is not defined`);
await expect(samlSessionManager.getUserData(nonExistingRole)).rejects.toThrow(`User with '${nonExistingRole}' role is not defined`);
expect(createCloudSAMLSessionMock.mock.calls).toHaveLength(0);
});
})
});
const samlSessionManager = new SamlSessionManager({ hostOptions, log, isCloud });
createCloudSAMLSessionMock.mockResolvedValue(
new Session(cloudCookieInstance, cloudEmail, cloudFullname)
);
const cookie = await samlSessionManager.getSessionCookieForRole('viewer');
expect(cookie).toBe(cloudCookieInstance.value);
});

test(`'getApiCredentialsForRole' should return {Cookie: <cookieString>}`, async () => {
const samlSessionManager = new SamlSessionManager({ hostOptions, log, isCloud });
const credentials = await samlSessionManager.getApiCredentialsForRole('viewer');
expect(credentials).toEqual({ Cookie: `${cloudCookieInstance.cookieString()}` });
});

test(`'getSessionCookieForRole' should call 'createCloudSAMLSession' only once for the same role`, async () => {
const samlSessionManager = new SamlSessionManager({ hostOptions, log, isCloud });
await samlSessionManager.getSessionCookieForRole('viewer');
await samlSessionManager.getSessionCookieForRole('admin');
await samlSessionManager.getSessionCookieForRole('viewer');
expect(createLocalSAMLSessionMock.mock.calls).toHaveLength(0);
expect(createCloudSAMLSessionMock.mock.calls).toHaveLength(2);
});

test(`'getUserData' should return the correct email & fullname`, async () => {
const samlSessionManager = new SamlSessionManager({ hostOptions, log, isCloud });
const data = await samlSessionManager.getUserData('viewer');
expect(data).toEqual({ email: cloudEmail, fullname: cloudFullname });
});

test(`throws error when roles does not exist`, async () => {
const nonExistingRole = 'tester';
const samlSessionManager = new SamlSessionManager({ hostOptions, log, isCloud });
await expect(samlSessionManager.getSessionCookieForRole(nonExistingRole)).rejects.toThrow(
`User with '${nonExistingRole}' role is not defined`
);
await expect(samlSessionManager.getApiCredentialsForRole(nonExistingRole)).rejects.toThrow(
`User with '${nonExistingRole}' role is not defined`
);
await expect(samlSessionManager.getUserData(nonExistingRole)).rejects.toThrow(
`User with '${nonExistingRole}' role is not defined`
);
expect(createCloudSAMLSessionMock.mock.calls).toHaveLength(0);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ export default function ({ getService }: FtrProviderContext) {

it('returns valid user data for authenticated request', async () => {
const { body, status } = await supertestWithoutAuth
.get('/internal/security/me')
.set(svlCommonApi.getInternalRequestHeader())
.set(credentials);
.get('/internal/security/me')
.set(svlCommonApi.getInternalRequestHeader())
.set(credentials);

const userData = await svlUserManager.getUserData('viewer');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export function SvlCommonPageProvider({ getService, getPageObjects }: FtrProvide
const { body } = await supertestWithoutAuth
.get('/internal/security/me')
.set(svlCommonApi.getInternalRequestHeader())
.set({Cookie: `sid=${browserCookies[0].value}`});
.set({ Cookie: `sid=${browserCookies[0].value}` });

const userData = await svlUserManager.getUserData(role);
// email returned from API call must match the email for the specified role
Expand Down

0 comments on commit c4ead20

Please sign in to comment.