Skip to content

Commit

Permalink
fix(authentication): issues/260 apply optin code (RedHatInsights#277)
Browse files Browse the repository at this point in the history

* authentication, optin error code check
* i18n test
  • Loading branch information
cdcabrera committed May 8, 2020
1 parent 47173e1 commit f9da761
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 133 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Authorization Component should render a connected component: connected 1`] = `
exports[`Authentication Component should render a connected component: connected 1`] = `
<ContextConsumer>
<Component />
</ContextConsumer>
`;

exports[`Authorization Component should render a non-connected component authorized: non-connected authorized 1`] = `
exports[`Authentication Component should render a non-connected component authorized: non-connected authorized 1`] = `
<Authentication
authorizeUser={[Function]}
history={
Expand Down Expand Up @@ -36,7 +36,7 @@ exports[`Authorization Component should render a non-connected component authori
</Authentication>
`;

exports[`Authorization Component should render a non-connected component error: non-connected error 1`] = `
exports[`Authentication Component should render a non-connected component error: non-connected error 1`] = `
<Authentication
authorizeUser={[Function]}
history={
Expand Down Expand Up @@ -167,7 +167,7 @@ exports[`Authorization Component should render a non-connected component error:
</Authentication>
`;

exports[`Authorization Component should render a non-connected component pending: non-connected pending 1`] = `
exports[`Authentication Component should render a non-connected component pending: non-connected pending 1`] = `
<MessageView
icon={[Function]}
message="t(curiosity-auth.pending, ...)"
Expand All @@ -176,108 +176,24 @@ exports[`Authorization Component should render a non-connected component pending
/>
`;

exports[`Authorization Component should return a message on 401 error: 401 error 1`] = `
<PageLayout>
<PageHeader
key=".0"
>
<PageHeader>
<section
className="pf-l-page-header pf-c-page-header pf-l-page__main-section pf-c-page__main-section pf-m-light"
widget-type="InsightsPageHeader"
>
<PageHeaderTitle
title="Subscription Watch"
>
<Title
className=""
size="2xl"
widget-type="InsightsPageHeaderTitle"
>
<h1
className="pf-c-title pf-m-2xl"
widget-type="InsightsPageHeaderTitle"
>
Subscription Watch
</h1>
</Title>
</PageHeaderTitle>
</section>
</PageHeader>
</PageHeader>
<PageSection
className="curiosity"
>
<section
className="pf-c-page__main-section curiosity"
>
<EmptyState
className="fadein"
key=".1"
variant="full"
>
<div
className="pf-c-empty-state fadein"
>
<Title
headingLevel="h2"
size="lg"
>
<h2
className="pf-c-title pf-m-lg"
>
t(curiosity-auth.authorizedTitle, [object Object])
</h2>
</Title>
<EmptyStateIcon
icon={[Function]}
>
<LockIcon
aria-hidden="true"
className="pf-c-empty-state__icon"
color="currentColor"
noVerticalAlign={false}
size="sm"
title={null}
>
<svg
aria-hidden="true"
aria-labelledby={null}
className="pf-c-empty-state__icon"
fill="currentColor"
height="1em"
role="img"
style={
Object {
"verticalAlign": "-0.125em",
}
}
viewBox="0 0 448 512"
width="1em"
>
<path
d="M400 224h-24v-72C376 68.2 307.8 0 224 0S72 68.2 72 152v72H48c-26.5 0-48 21.5-48 48v192c0 26.5 21.5 48 48 48h352c26.5 0 48-21.5 48-48V272c0-26.5-21.5-48-48-48zm-104 0H152v-72c0-39.7 32.3-72 72-72s72 32.3 72 72v72z"
transform=""
/>
</svg>
</LockIcon>
</EmptyStateIcon>
<EmptyStateBody>
<div
className="pf-c-empty-state__body"
>
t(curiosity-auth.authorizedCopy, ...)
</div>
</EmptyStateBody>
</div>
</EmptyState>
</section>
</PageSection>
</PageLayout>
exports[`Authentication Component should return a message on 401 error: 401 error 1`] = `
<MessageView
icon={[Function]}
message="t(curiosity-auth.authorizedCopy, ...)"
pageTitle={null}
title="t(curiosity-auth.authorizedTitle, [object Object])"
/>
`;

exports[`Authorization Component should return a redirect on 403 error: 403 error 1`] = `"403 redirect"`;
exports[`Authentication Component should return a redirect on 418 error: 418 error 1`] = `"418 redirect"`;

exports[`Authentication Component should return a redirect on a specific 403 error and error code: 403 error 1`] = `
<MessageView
icon={[Function]}
message="t(curiosity-auth.authorizedCopy, ...)"
pageTitle={null}
title="t(curiosity-auth.authorizedTitle, [object Object])"
/>
`;

exports[`Authorization Component should return a redirect on 418 error: 418 error 1`] = `"418 redirect"`;
exports[`Authentication Component should return a redirect on a specific 403 error and error code: 403 redirect error 1`] = `"403 redirect"`;
50 changes: 27 additions & 23 deletions src/components/authentication/__tests__/authentication.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React from 'react';
import configureMockStore from 'redux-mock-store';
import { mount, shallow } from 'enzyme';
import { BrowserRouter } from 'react-router-dom';
import { helpers } from '../../../common/helpers';
import { ConnectedAuthentication, Authentication } from '../authentication';
import { rhsmApiTypes } from '../../../types';

describe('Authorization Component', () => {
describe('Authentication Component', () => {
const generateEmptyStore = (obj = {}) => configureMockStore()(obj);

it('should render a connected component', () => {
Expand Down Expand Up @@ -59,18 +59,16 @@ describe('Authorization Component', () => {
pending: false
}
};
const component = mount(
<BrowserRouter>
<Authentication {...props}>
<span className="test">lorem</span>
</Authentication>
</BrowserRouter>
const component = shallow(
<Authentication {...props}>
<span className="test">lorem</span>
</Authentication>
);

expect(component.html()).toMatchSnapshot('418 error');
});

it('should return a redirect on 403 error', () => {
it('should return a redirect on a specific 403 error and error code', () => {
const props = {
history: {
listen: helpers.noop,
Expand All @@ -80,19 +78,27 @@ describe('Authorization Component', () => {
authorized: false,
error: true,
status: 403,
errorCodes: [rhsmApiTypes.RHSM_API_RESPONSE_ERROR_DATA_CODE_TYPES.OPTIN],
errorMessage: `Forbidden`,
pending: false
}
};
const component = mount(
<BrowserRouter>
<Authentication {...props}>
<span className="test">lorem</span>
</Authentication>
</BrowserRouter>
const component = shallow(
<Authentication {...props}>
<span className="test">lorem</span>
</Authentication>
);

expect(component.html()).toMatchSnapshot('403 error');
expect(component.html()).toMatchSnapshot('403 redirect error');

component.setProps({
session: {
...props.session,
errorCodes: []
}
});

expect(component).toMatchSnapshot('403 error');
});

it('should return a message on 401 error', () => {
Expand All @@ -106,15 +112,13 @@ describe('Authorization Component', () => {
status: 401
}
};
const component = mount(
<BrowserRouter>
<Authentication {...props}>
<span className="test">lorem</span>
</Authentication>
</BrowserRouter>
const component = shallow(
<Authentication {...props}>
<span className="test">lorem</span>
</Authentication>
);

expect(component.find('PageLayout')).toMatchSnapshot('401 error');
expect(component).toMatchSnapshot('401 error');
});

it('should render a non-connected component pending', () => {
Expand Down
8 changes: 7 additions & 1 deletion src/components/authentication/authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { BinocularsIcon, LockIcon } from '@patternfly/react-icons';
import { connectRouterTranslate, reduxActions } from '../../redux';
import { rhsmApiTypes } from '../../types';
import { helpers } from '../../common';
import { Redirect, routerHelpers, routerTypes } from '../router/router';
import MessageView from '../messageView/messageView';
Expand Down Expand Up @@ -58,7 +59,10 @@ class Authentication extends Component {
return <MessageView pageTitle="&nbsp;" message={t('curiosity-auth.pending', '...')} icon={BinocularsIcon} />;
}

if (session.status === 403 || session.status === 418) {
if (
(session.errorCodes && session.errorCodes.includes(rhsmApiTypes.RHSM_API_RESPONSE_ERROR_DATA_CODE_TYPES.OPTIN)) ||
session.status === 418
) {
if (helpers.TEST_MODE) {
return <React.Fragment>{session.status} redirect</React.Fragment>;
}
Expand Down Expand Up @@ -95,6 +99,7 @@ Authentication.propTypes = {
session: PropTypes.shape({
authorized: PropTypes.bool,
error: PropTypes.bool,
errorCodes: PropTypes.arrayOf(PropTypes.string),
errorMessage: PropTypes.string,
pending: PropTypes.bool,
status: PropTypes.number
Expand All @@ -117,6 +122,7 @@ Authentication.defaultProps = {
session: {
authorized: false,
error: false,
errorCodes: [],
errorMessage: '',
pending: false,
status: null
Expand Down
6 changes: 3 additions & 3 deletions src/components/i18n/__tests__/__snapshots__/i18n.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ exports[`I18n Component should generate a predictable pot output snapshot: pot o
msgstr \\"\\"
\\"Content-Type: text/plain; charset=UTF-8\\\\n\\"
#: src/components/authentication/authentication.js:70
#: src/components/authentication/authentication.js:74
msgid \\"curiosity-auth.authorizedTitle\\"
msgstr \\"\\"
Expand Down Expand Up @@ -209,12 +209,12 @@ msgstr \\"\\"
msgid \\"curiosity-view.rhel\\"
msgstr \\"\\"
#: src/components/authentication/authentication.js:71
#: src/components/authentication/authentication.js:75
msgctxt \\"...\\"
msgid \\"curiosity-auth.authorizedCopy\\"
msgstr \\"\\"
#: src/components/authentication/authentication.js:58
#: src/components/authentication/authentication.js:59
msgctxt \\"...\\"
msgid \\"curiosity-auth.pending\\"
msgstr \\"\\"
Expand Down

0 comments on commit f9da761

Please sign in to comment.