neo4j-client-sso
Version:
Single sign-on client (frontend) library for Neo4j products
162 lines (161 loc) • 9.41 kB
JavaScript
;
/*
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
const helpers_1 = require("./helpers");
describe('addSearchParamsInBrowserHistory', () => {
const originalWindowLocationHref = 'http://localhost/app';
beforeEach(() => {
window.history.replaceState({}, '', originalWindowLocationHref);
});
test('no args passed in', () => {
expect(window.location.href).toEqual(originalWindowLocationHref);
(0, helpers_1.addSearchParamsInBrowserHistory)(null);
expect(window.location.href).toEqual(originalWindowLocationHref);
});
test('empty passed in args', () => {
expect(window.location.href).toEqual(originalWindowLocationHref);
(0, helpers_1.addSearchParamsInBrowserHistory)({});
expect(window.location.href).toEqual(originalWindowLocationHref + '?');
});
test('simple search parameter', () => {
expect(window.location.href).toEqual(originalWindowLocationHref);
(0, helpers_1.addSearchParamsInBrowserHistory)({ rest: 'test' });
expect(window.location.href).toEqual(originalWindowLocationHref + '?rest=test');
});
test('search parameter with special characters', () => {
expect(window.location.href).toEqual(originalWindowLocationHref);
(0, helpers_1.addSearchParamsInBrowserHistory)({ test_entry: '5%73bsod?892()€%&#"€' });
expect(window.location.href).toEqual(originalWindowLocationHref +
'?test_entry=5%2573bsod%3F892%28%29%E2%82%AC%25%26%23%22%E2%82%AC');
});
test('multiple search parameters', () => {
expect(window.location.href).toEqual(originalWindowLocationHref);
(0, helpers_1.addSearchParamsInBrowserHistory)({
test_entry: '5%73bsod?892()€%&#"€',
entryUrl: 'http://localhost:8043/test/?entry=boat'
});
expect(window.location.href).toEqual(originalWindowLocationHref +
'?test_entry=5%2573bsod%3F892%28%29%E2%82%AC%25%26%23%22%E2%82%AC&entryUrl=http%3A%2F%2Flocalhost%3A8043%2Ftest%2F%3Fentry%3Dboat');
});
test('keeps URL hash parameters', () => {
const hashUrlParams = '#code=df56&code_verifier=rt43';
window.history.replaceState({}, '', originalWindowLocationHref + hashUrlParams);
expect(window.location.href).toEqual(originalWindowLocationHref + hashUrlParams);
(0, helpers_1.addSearchParamsInBrowserHistory)({ rest: 'test' });
expect(window.location.href).toEqual(originalWindowLocationHref + '?rest=test' + hashUrlParams);
});
test('test a url without a pathname present', () => {
const tmpOriginalWindowLocationHref = 'http://localhost/';
window.history.replaceState({}, '', tmpOriginalWindowLocationHref);
expect(window.location.href).toEqual(tmpOriginalWindowLocationHref);
(0, helpers_1.addSearchParamsInBrowserHistory)({ rest: 'test' });
expect(window.location.href).toEqual(tmpOriginalWindowLocationHref + '?rest=test');
});
});
describe('removeSearchParamsInBrowserHistory', () => {
const originalWindowLocationHref = 'http://localhost/app?test_param=house&code=843cvg';
beforeEach(() => {
window.history.replaceState({}, '', originalWindowLocationHref);
});
test('no args passed in', () => {
expect(window.location.href).toEqual(originalWindowLocationHref);
(0, helpers_1.removeSearchParamsInBrowserHistory)(null);
expect(window.location.href).toEqual(originalWindowLocationHref);
});
test('empty passed args', () => {
expect(window.location.href).toEqual(originalWindowLocationHref);
(0, helpers_1.removeSearchParamsInBrowserHistory)([]);
expect(window.location.href).toEqual(originalWindowLocationHref);
});
test('non-existing parameter in browser history', () => {
expect(window.location.href).toEqual(originalWindowLocationHref);
(0, helpers_1.removeSearchParamsInBrowserHistory)(['kode']);
expect(window.location.href).toEqual(originalWindowLocationHref);
});
test('remove existing parameter from browser history', () => {
expect(window.location.href).toEqual(originalWindowLocationHref);
(0, helpers_1.removeSearchParamsInBrowserHistory)(['test_param']);
expect(window.location.href).toEqual('http://localhost/app?code=843cvg');
});
test('remove all existing parameters from browser history', () => {
expect(window.location.href).toEqual(originalWindowLocationHref);
(0, helpers_1.removeSearchParamsInBrowserHistory)(['test_param', 'code']);
expect(window.location.href).toEqual('http://localhost/app?');
});
test('does not keep URL hash parameters', () => {
const hashUrlParams = '#box=/&%#/=Q4535&state_test=ljhf873';
window.history.replaceState({}, '', originalWindowLocationHref + hashUrlParams);
expect(window.location.href).toEqual(originalWindowLocationHref + hashUrlParams);
(0, helpers_1.removeSearchParamsInBrowserHistory)(['test_param']);
expect(window.location.href).toEqual('http://localhost/app?code=843cvg');
});
test('test a url without a pathname present', () => {
const tmpOriginalWindowLocationHref = 'http://localhost/?test_param=house&code=843cvg';
window.history.replaceState({}, '', tmpOriginalWindowLocationHref);
expect(window.location.href).toEqual(tmpOriginalWindowLocationHref);
(0, helpers_1.removeSearchParamsInBrowserHistory)(['test_param', 'code']);
expect(window.location.href).toEqual('http://localhost/?');
});
});
describe('assembleRedirectUri', () => {
const IDP_ID = 'keycloak-oidc';
test('test a url, no path and no search params', () => {
const tmpOriginalWindowLocationHref = 'http://localhost';
window.history.replaceState({}, '', tmpOriginalWindowLocationHref);
const expected = 'http://localhost?idp_id=keycloak-oidc&auth_flow_step=redirect_uri';
expect((0, helpers_1.assembleRedirectUri)(IDP_ID)).toEqual(expected);
});
test('test that trailing forward slash in origin gets removed', () => {
const tmpOriginalWindowLocationHref = 'http://localhost/';
window.history.replaceState({}, '', tmpOriginalWindowLocationHref);
const expected = 'http://localhost?idp_id=keycloak-oidc&auth_flow_step=redirect_uri'; // the forward slash here between 'localhost' and '?' gets removed. only on root(!) though
expect((0, helpers_1.assembleRedirectUri)(IDP_ID)).toEqual(expected);
});
test('test a url with path and no search params', () => {
const tmpOriginalWindowLocationHref = 'http://localhost/app/test';
window.history.replaceState({}, '', tmpOriginalWindowLocationHref);
const expected = 'http://localhost/app/test?idp_id=keycloak-oidc&auth_flow_step=redirect_uri';
expect((0, helpers_1.assembleRedirectUri)(IDP_ID)).toEqual(expected);
});
test('test a url with path and trailing slash', () => {
const tmpOriginalWindowLocationHref = 'http://localhost/app/test/';
window.history.replaceState({}, '', tmpOriginalWindowLocationHref);
const expected = 'http://localhost/app/test/?idp_id=keycloak-oidc&auth_flow_step=redirect_uri'; // the forward slash here between 'test' and '?' is kept.
expect((0, helpers_1.assembleRedirectUri)(IDP_ID)).toEqual(expected);
});
test('test a url with path and two search params, ignores existing search params', () => {
const tmpOriginalWindowLocationHref = 'http://localhost/app?test_param=house&code=843cvg';
window.history.replaceState({}, '', tmpOriginalWindowLocationHref);
const expected = 'http://localhost/app?idp_id=keycloak-oidc&auth_flow_step=redirect_uri';
expect((0, helpers_1.assembleRedirectUri)(IDP_ID)).toEqual(expected);
});
test('test a url without path and two search params, ignores existing search params', () => {
const tmpOriginalWindowLocationHref = 'http://localhost?test_param=house&code=843cvg';
window.history.replaceState({}, '', tmpOriginalWindowLocationHref);
const expected = 'http://localhost?idp_id=keycloak-oidc&auth_flow_step=redirect_uri';
expect((0, helpers_1.assembleRedirectUri)(IDP_ID)).toEqual(expected);
});
test('test that hash params get ignored', () => {
const tmpOriginalWindowLocationHref = 'http://localhost?test_param=house&code=843cvg#yest=yest';
window.history.replaceState({}, '', tmpOriginalWindowLocationHref);
const expected = 'http://localhost?idp_id=keycloak-oidc&auth_flow_step=redirect_uri';
expect((0, helpers_1.assembleRedirectUri)(IDP_ID)).toEqual(expected);
});
});