SignIn button - use correct url (#26239)

* SignIn button - use correct url

* Fix SignIn test (#26266)

* Don't use absolute URL

* post review

* Fix snap

Co-authored-by: Sofia Papagiannaki <papagian@users.noreply.github.com>
This commit is contained in:
Dominik Prokop 2020-07-14 12:56:55 +02:00 committed by GitHub
parent 54ad5b869e
commit 187612ca8d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 39 additions and 15 deletions

View File

@ -2,13 +2,9 @@ import React from 'react';
import { shallow } from 'enzyme';
import { SignIn } from './SignIn';
jest.mock('../../config', () => ({
appUrl: 'http://localhost:3000/',
}));
describe('Render', () => {
it('should render component', () => {
const wrapper = shallow(<SignIn url="/" />);
const wrapper = shallow(<SignIn url="/whatever" />);
expect(wrapper).toMatchSnapshot();
});

View File

@ -1,18 +1,13 @@
import React, { FC } from 'react';
import config from 'app/core/config';
import { connectWithStore } from 'app/core/utils/connectWithReduxStore';
import { StoreState } from 'app/types';
import { Icon } from '@grafana/ui';
const getForcedLoginUrl = (url: string) => {
const urlObj = new URL(url, config.appUrl);
let params = urlObj.searchParams;
params.set('forceLogin', 'true');
return urlObj.toString();
};
import { getForcedLoginUrl } from './utils';
export const SignIn: FC<any> = ({ url }) => {
const forcedLoginUrl = getForcedLoginUrl(url);
return (
<div className="sidemenu-item">
<a href={forcedLoginUrl} className="sidemenu-link" target="_self">

View File

@ -6,7 +6,7 @@ exports[`Render should render component 1`] = `
>
<a
className="sidemenu-link"
href="http://localhost:3000/?forceLogin=true"
href="/whatever?forceLogin=true"
target="_self"
>
<span
@ -19,7 +19,7 @@ exports[`Render should render component 1`] = `
</span>
</a>
<a
href="http://localhost:3000/?forceLogin=true"
href="/whatever?forceLogin=true"
target="_self"
>
<ul

View File

@ -0,0 +1,25 @@
import { updateConfig } from '../../config';
import { getForcedLoginUrl } from './utils';
describe('getForcedLoginUrl', () => {
it.each`
appSubUrl | url | expected
${''} | ${'/whatever?a=1&b=2'} | ${'/whatever?a=1&b=2&forceLogin=true'}
${'/grafana'} | ${'/whatever?a=1&b=2'} | ${'/grafana/whatever?a=1&b=2&forceLogin=true'}
${'/grafana/test'} | ${'/whatever?a=1&b=2'} | ${'/grafana/test/whatever?a=1&b=2&forceLogin=true'}
${'/grafana'} | ${''} | ${'/grafana?forceLogin=true'}
${'/grafana'} | ${'/whatever'} | ${'/grafana/whatever?forceLogin=true'}
${'/grafana'} | ${'/whatever/'} | ${'/grafana/whatever/?forceLogin=true'}
`(
"when appUrl set to '$appUrl' and appSubUrl set to '$appSubUrl' then result should be '$expected'",
({ appSubUrl, url, expected }) => {
updateConfig({
appSubUrl,
});
const result = getForcedLoginUrl(url);
expect(result).toBe(expected);
}
);
});

View File

@ -0,0 +1,8 @@
import { getConfig } from 'app/core/config';
export const getForcedLoginUrl = (url: string) => {
const queryParams = new URLSearchParams(url.split('?')[1]);
queryParams.append('forceLogin', 'true');
return `${getConfig().appSubUrl}${url.split('?')[0]}?${queryParams.toString()}`;
};