diff options
author | Grégoire Aubert <gregoire.aubert@sonarsource.com> | 2019-06-24 15:47:48 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2019-06-28 08:45:54 +0200 |
commit | 369b4771c0354647c8c54f3e42be742f33e26c7f (patch) | |
tree | 2326a058aab0b2f848f145ca9d3d492e40d6e736 | |
parent | 9669c6d690b5ced80d0b9b80feeb8b9813284ebe (diff) | |
download | sonarqube-369b4771c0354647c8c54f3e42be742f33e26c7f.tar.gz sonarqube-369b4771c0354647c8c54f3e42be742f33e26c7f.zip |
SONAR-12148 Use cookies to display error message in EmailAlreadyExists and Unauthorized sessions
4 files changed, 45 insertions, 52 deletions
diff --git a/server/sonar-web/src/main/js/apps/sessions/components/EmailAlreadyExists.tsx b/server/sonar-web/src/main/js/apps/sessions/components/EmailAlreadyExists.tsx index 21a52dc2871..871586f1e2d 100644 --- a/server/sonar-web/src/main/js/apps/sessions/components/EmailAlreadyExists.tsx +++ b/server/sonar-web/src/main/js/apps/sessions/components/EmailAlreadyExists.tsx @@ -19,33 +19,21 @@ */ import * as React from 'react'; import { FormattedMessage } from 'react-intl'; -import { getIdentityProviders } from '../../../api/users'; import * as theme from '../../../app/theme'; +import { Alert } from '../../../components/ui/Alert'; +import { getIdentityProviders } from '../../../api/users'; +import { getCookie } from '../../../helpers/cookies'; +import { getBaseUrl } from '../../../helpers/urls'; import { getTextColor } from '../../../helpers/colors'; import { translate } from '../../../helpers/l10n'; -import { getBaseUrl } from '../../../helpers/urls'; -import { Alert } from '../../../components/ui/Alert'; - -interface Props { - location: { - query: { - email: string; - login: string; - provider: string; - existingLogin: string; - existingProvider: string; - }; - }; -} interface State { identityProviders: T.IdentityProvider[]; - loading: boolean; } -export default class EmailAlreadyExists extends React.PureComponent<Props, State> { +export default class EmailAlreadyExists extends React.PureComponent<{}, State> { mounted = false; - state: State = { identityProviders: [], loading: true }; + state: State = { identityProviders: [] }; componentDidMount() { this.mounted = true; @@ -57,22 +45,31 @@ export default class EmailAlreadyExists extends React.PureComponent<Props, State } fetchIdentityProviders = () => { - this.setState({ loading: true }); getIdentityProviders().then( ({ identityProviders }) => { if (this.mounted) { - this.setState({ identityProviders, loading: false }); + this.setState({ identityProviders }); } }, - () => { - if (this.mounted) { - this.setState({ loading: false }); - } - } + () => {} ); }; - renderIdentityProvier = (provider: string, login: string) => { + getAuthError = (): { + email?: string; + login?: string; + provider?: string; + existingLogin?: string; + existingProvider?: string; + } => { + const cookie = getCookie('AUTHENTICATION-ERROR'); + if (cookie) { + return JSON.parse(decodeURIComponent(cookie)); + } + return {}; + }; + + renderIdentityProvier = (provider?: string, login?: string) => { const identityProvider = this.state.identityProviders.find(p => p.key === provider); return identityProvider ? ( @@ -99,8 +96,7 @@ export default class EmailAlreadyExists extends React.PureComponent<Props, State }; render() { - const { query } = this.props.location; - + const authError = this.getAuthError(); return ( <div className="page-wrapper-simple" id="bd"> <div className="page-simple" id="nonav"> @@ -109,15 +105,15 @@ export default class EmailAlreadyExists extends React.PureComponent<Props, State <FormattedMessage defaultMessage={translate('sessions.email_already_exists.1')} id="sessions.email_already_exists.1" - values={{ email: <strong>{query.email}</strong> }} + values={{ email: <strong>{authError.email}</strong> }} /> </p> - {this.renderIdentityProvier(query.existingProvider, query.existingLogin)} + {this.renderIdentityProvier(authError.existingProvider, authError.existingLogin)} </div> <div className="big-spacer-bottom js-new-account"> <p className="little-spacer-bottom">{translate('sessions.email_already_exists.2')}</p> - {this.renderIdentityProvier(query.provider, query.login)} + {this.renderIdentityProvier(authError.provider, authError.login)} </div> <Alert variant="warning"> @@ -132,7 +128,7 @@ export default class EmailAlreadyExists extends React.PureComponent<Props, State <div className="big-spacer-top text-right"> <a className="button js-continue" - href={`${getBaseUrl()}/sessions/init/${query.provider}?allowEmailShift=true`}> + href={`${getBaseUrl()}/sessions/init/${authError.provider}?allowEmailShift=true`}> {translate('continue')} </a> <a className="big-spacer-left js-cancel" href={getBaseUrl() + '/'}> diff --git a/server/sonar-web/src/main/js/apps/sessions/components/Unauthorized.tsx b/server/sonar-web/src/main/js/apps/sessions/components/Unauthorized.tsx index 2ad6161d198..8f7f4fd710d 100644 --- a/server/sonar-web/src/main/js/apps/sessions/components/Unauthorized.tsx +++ b/server/sonar-web/src/main/js/apps/sessions/components/Unauthorized.tsx @@ -20,18 +20,10 @@ import * as React from 'react'; import { translate } from '../../../helpers/l10n'; import { getBaseUrl } from '../../../helpers/urls'; +import { getCookie } from '../../../helpers/cookies'; -interface Props { - location: { - query: { - message: string; - }; - }; -} - -export default function Unauthorized(props: Props) { - const { message } = props.location.query; - +export default function Unauthorized() { + const message = decodeURIComponent(getCookie('AUTHENTICATION-ERROR') || ''); return ( <div className="page-wrapper-simple" id="bd"> <div className="page-simple" id="nonav"> diff --git a/server/sonar-web/src/main/js/apps/sessions/components/__tests__/EmailAlreadyExists-test.tsx b/server/sonar-web/src/main/js/apps/sessions/components/__tests__/EmailAlreadyExists-test.tsx index e794a7d27df..9b43ee6d4f5 100644 --- a/server/sonar-web/src/main/js/apps/sessions/components/__tests__/EmailAlreadyExists-test.tsx +++ b/server/sonar-web/src/main/js/apps/sessions/components/__tests__/EmailAlreadyExists-test.tsx @@ -42,15 +42,16 @@ jest.mock('../../../../api/users', () => ({ }) })); +jest.mock('../../../../helpers/cookies', () => ({ + getCookie: jest + .fn() + .mockReturnValue( + '%7B%22email%22%3A%22mail%40example.com%22%2C%22login%22%3A%22foo%22%2C%22provider%22%3A%22github%22%2C%22existingLogin%22%3A%22bar%22%2C%22existingProvider%22%3A%22bitbucket%22%7D' + ) +})); + it('render', async () => { - const query = { - email: 'mail@example.com', - login: 'foo', - provider: 'github', - existingLogin: 'bar', - existingProvider: 'bitbucket' - }; - const wrapper = shallow(<EmailAlreadyExists location={{ query }} />); + const wrapper = shallow(<EmailAlreadyExists />); (wrapper.instance() as EmailAlreadyExists).mounted = true; (wrapper.instance() as EmailAlreadyExists).fetchIdentityProviders(); await waitAndUpdate(wrapper); diff --git a/server/sonar-web/src/main/js/apps/sessions/components/__tests__/Unauthorized-test.tsx b/server/sonar-web/src/main/js/apps/sessions/components/__tests__/Unauthorized-test.tsx index dc40e933d5a..e8df4c544e1 100644 --- a/server/sonar-web/src/main/js/apps/sessions/components/__tests__/Unauthorized-test.tsx +++ b/server/sonar-web/src/main/js/apps/sessions/components/__tests__/Unauthorized-test.tsx @@ -21,6 +21,10 @@ import * as React from 'react'; import { shallow } from 'enzyme'; import Unauthorized from '../Unauthorized'; +jest.mock('../../../../helpers/cookies', () => ({ + getCookie: jest.fn().mockReturnValue('Foo') +})); + it('render', () => { - expect(shallow(<Unauthorized location={{ query: { message: 'Foo' } }} />)).toMatchSnapshot(); + expect(shallow(<Unauthorized />)).toMatchSnapshot(); }); |