]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-12148 Use cookies to display error message in EmailAlreadyExists and Unauthoriz...
authorGrégoire Aubert <gregoire.aubert@sonarsource.com>
Mon, 24 Jun 2019 13:47:48 +0000 (15:47 +0200)
committersonartech <sonartech@sonarsource.com>
Fri, 28 Jun 2019 06:45:54 +0000 (08:45 +0200)
server/sonar-web/src/main/js/apps/sessions/components/EmailAlreadyExists.tsx
server/sonar-web/src/main/js/apps/sessions/components/Unauthorized.tsx
server/sonar-web/src/main/js/apps/sessions/components/__tests__/EmailAlreadyExists-test.tsx
server/sonar-web/src/main/js/apps/sessions/components/__tests__/Unauthorized-test.tsx

index 21a52dc287123d7ee04a63a2db9b3b0ca0f4b8b5..871586f1e2d04732fbbbbd815871174b4af1cb09 100644 (file)
  */
 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() + '/'}>
index 2ad6161d198e9573423a024f60008e5c0d773433..8f7f4fd710d0a83df41f8860e6f959072fec5468 100644 (file)
 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">
index e794a7d27df74930b09c8d9e3c3032228aff9428..9b43ee6d4f58ba4d60dcb285b13d8974efd65013 100644 (file)
@@ -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);
index dc40e933d5ac31cbce3ccca355e31597b86e2123..e8df4c544e18fe8d15ce1836154a5cec92d5d14f 100644 (file)
@@ -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();
 });