Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect cookies on login page #120944

Merged
merged 16 commits into from Dec 16, 2021
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Expand Up @@ -18,7 +18,7 @@ describe('loginApp', () => {

loginApp.create({
...coreSetupMock,
config: { loginAssistanceMessage: '' },
config: { loginAssistanceMessage: '', sameSiteCookies: undefined },
});

expect(coreSetupMock.http.anonymousPaths.register).toHaveBeenCalledTimes(1);
Expand All @@ -43,7 +43,7 @@ describe('loginApp', () => {

loginApp.create({
...coreSetupMock,
config: { loginAssistanceMessage: 'some-message' },
config: { loginAssistanceMessage: 'some-message', sameSiteCookies: undefined },
});

const [[{ mount }]] = coreSetupMock.application.register.mock.calls;
Expand Down
Expand Up @@ -19,7 +19,7 @@ interface CreateDeps {
application: ApplicationSetup;
http: HttpSetup;
getStartServices: StartServicesAccessor;
config: Pick<ConfigType, 'loginAssistanceMessage'>;
config: Pick<ConfigType, 'loginAssistanceMessage' | 'sameSiteCookies'>;
}

export const loginApp = Object.freeze({
Expand All @@ -44,6 +44,7 @@ export const loginApp = Object.freeze({
notifications: coreStart.notifications,
fatalErrors: coreStart.fatalErrors,
loginAssistanceMessage: config.loginAssistanceMessage,
sameSiteCookies: config.sameSiteCookies,
}
);
},
Expand Down
Expand Up @@ -5,6 +5,7 @@
* 2.0.
*/

import { EuiFlexItem } from '@elastic/eui';
import { act } from '@testing-library/react';
import { shallow } from 'enzyme';
import React from 'react';
Expand Down Expand Up @@ -75,6 +76,20 @@ describe('LoginPage', () => {
});

describe('disabled form states', () => {
const originalNavigator = window.navigator;
const originalTop = window.top;

afterEach(function () {
Object.defineProperty(window, 'navigator', {
value: originalNavigator,
writable: true,
});
Object.defineProperty(window, 'top', {
value: originalTop,
writable: true,
});
});

it('renders as expected when secure connection is required but not present', async () => {
const coreStartMock = coreMock.createStart();
httpMock.get.mockResolvedValue(createLoginState({ requiresSecureConnection: true }));
Expand Down Expand Up @@ -183,6 +198,94 @@ describe('LoginPage', () => {

expect(wrapper.find(DisabledLoginForm)).toMatchSnapshot();
});

it('renders CTA and cross-origin cookie warning when cookies are disabled, document is embedded inside iframe, and cross-origin cookies are blocked', async () => {
const coreStartMock = coreMock.createStart();
httpMock.get.mockResolvedValue(createLoginState());

Object.defineProperty(window, 'navigator', {
value: { cookieEnabled: false },
writable: true,
});
Object.defineProperty(window, 'top', {
value: {},
writable: true,
});

const wrapper = shallow(
<LoginPage
http={httpMock}
notifications={coreStartMock.notifications}
fatalErrors={coreStartMock.fatalErrors}
loginAssistanceMessage=""
sameSiteCookies="Lax"
/>
);

await act(async () => {
await nextTick();
wrapper.update();
});

expect(wrapper.find(EuiFlexItem).children()).toMatchSnapshot();
});

it('renders CTA and browser settings warning when cookies are disabled, document is embedded inside iframe, and cross-origin cookies are allowed', async () => {
const coreStartMock = coreMock.createStart();
httpMock.get.mockResolvedValue(createLoginState());

Object.defineProperty(window, 'navigator', {
value: { cookieEnabled: false },
writable: true,
});
Object.defineProperty(window, 'top', {
value: {},
writable: true,
});

const wrapper = shallow(
<LoginPage
http={httpMock}
notifications={coreStartMock.notifications}
fatalErrors={coreStartMock.fatalErrors}
loginAssistanceMessage=""
sameSiteCookies="None"
/>
);

await act(async () => {
await nextTick();
wrapper.update();
});

expect(wrapper.find(EuiFlexItem).children()).toMatchSnapshot();
});

it('renders warning when cookies are disabled and document is not embedded inside iframe', async () => {
const coreStartMock = coreMock.createStart();
httpMock.get.mockResolvedValue(createLoginState());

Object.defineProperty(window, 'navigator', {
value: { cookieEnabled: false },
writable: true,
});

const wrapper = shallow(
<LoginPage
http={httpMock}
notifications={coreStartMock.notifications}
fatalErrors={coreStartMock.fatalErrors}
loginAssistanceMessage=""
/>
);

await act(async () => {
await nextTick();
wrapper.update();
});

expect(wrapper.find(DisabledLoginForm)).toMatchSnapshot();
});
});

describe('enabled form state', () => {
Expand Down