-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2987 from Hyperkid123/ansible-trial
fix ansible bundle trial redirects
- Loading branch information
Showing
4 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import React, { PropsWithChildren } from 'react'; | ||
import { MemoryRouter, MemoryRouterProps, useLocation } from 'react-router-dom'; | ||
import ChromeAuthContext, { ChromeAuthContextValue } from '../auth/ChromeAuthContext'; | ||
import { ChromeUser } from '@redhat-cloud-services/types'; | ||
import { renderHook, screen } from '@testing-library/react'; | ||
import useTrialRedirect from './useTrialRedirect'; | ||
|
||
const PathnameSpy = () => { | ||
const { pathname } = useLocation(); | ||
return <h1 aria-label="pathname-spy">{pathname}</h1>; | ||
}; | ||
|
||
type WrapperProps = PropsWithChildren<{ chromeAuthContext?: ChromeAuthContextValue; initialEntries?: MemoryRouterProps['initialEntries'] }>; | ||
|
||
const RouterWrapper = ({ children, initialEntries = ['/'] }: WrapperProps) => { | ||
return ( | ||
<MemoryRouter initialEntries={initialEntries}> | ||
{children} | ||
<PathnameSpy /> | ||
</MemoryRouter> | ||
); | ||
}; | ||
|
||
const defaultChromeUser: ChromeUser = { | ||
entitlements: {}, | ||
identity: {}, | ||
} as ChromeUser; | ||
|
||
const defaultChromeAuthContextValue: ChromeAuthContextValue = { | ||
user: defaultChromeUser, | ||
} as ChromeAuthContextValue; | ||
|
||
const ChromeAuthWrapper = ({ children, chromeAuthContext = defaultChromeAuthContextValue, ...rest }: WrapperProps) => { | ||
return ( | ||
<ChromeAuthContext.Provider value={chromeAuthContext}> | ||
<RouterWrapper {...rest}>{children}</RouterWrapper> | ||
</ChromeAuthContext.Provider> | ||
); | ||
}; | ||
|
||
const cases: { entitlements: ChromeUser['entitlements']; expected: string; initialEntries: string[] }[] = [ | ||
{ | ||
entitlements: { | ||
ansible: { | ||
is_entitled: false, | ||
is_trial: false, | ||
}, | ||
}, | ||
expected: '/ansible/ansible-dashboard/trial', | ||
initialEntries: ['/ansible/ansible-dashboard/foo/bar'], | ||
}, | ||
{ | ||
entitlements: { | ||
ansible: { | ||
is_entitled: true, | ||
is_trial: false, | ||
}, | ||
}, | ||
expected: '/ansible/ansible-dashboard/foo/bar', | ||
initialEntries: ['/ansible/ansible-dashboard/foo/bar'], | ||
}, | ||
{ | ||
entitlements: { | ||
ansible: { | ||
is_entitled: false, | ||
is_trial: true, | ||
}, | ||
}, | ||
expected: '/ansible/ansible-dashboard/foo/bar', | ||
initialEntries: ['/ansible/ansible-dashboard/foo/bar'], | ||
}, | ||
{ | ||
entitlements: { | ||
ansible: { | ||
is_entitled: false, | ||
is_trial: false, | ||
}, | ||
}, | ||
expected: '/ansible/ansible-dashboard/trial/success', | ||
initialEntries: ['/ansible/ansible-dashboard/trial/success'], | ||
}, | ||
]; | ||
|
||
describe('useTrialRedirect', () => { | ||
cases.forEach(({ entitlements, expected, initialEntries }) => { | ||
test(`should be on ${expected} route based on entitlements`, () => { | ||
renderHook(() => useTrialRedirect(), { | ||
wrapper: (props) => ( | ||
<ChromeAuthWrapper | ||
initialEntries={initialEntries} | ||
chromeAuthContext={ | ||
{ | ||
user: { | ||
...defaultChromeUser, | ||
entitlements, | ||
}, | ||
} as ChromeAuthContextValue | ||
} | ||
{...props} | ||
/> | ||
), | ||
}); | ||
|
||
expect(screen.getByLabelText('pathname-spy')).toHaveTextContent(expected); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { useContext, useEffect } from 'react'; | ||
import ChromeAuthContext from '../auth/ChromeAuthContext'; | ||
import { matchPath, useLocation, useNavigate } from 'react-router-dom'; | ||
import { ChromeUser } from '@redhat-cloud-services/types'; | ||
import { isAnsibleTrialFlagActive } from '../utils/isAnsibleTrialFlagActive'; | ||
|
||
const ignoredPaths = ['/ansible/trial/*', '/ansible/ansible-dashboard/trial/*']; | ||
|
||
const redirects: { | ||
[pattern: string]: { | ||
redirect: string; | ||
entitlementsKey: string; | ||
getTempTrialFlag: () => boolean; | ||
}; | ||
} = { | ||
'ansible/*': { redirect: '/ansible/ansible-dashboard/trial', entitlementsKey: 'ansible', getTempTrialFlag: isAnsibleTrialFlagActive }, | ||
}; | ||
|
||
function useTrialRedirect() { | ||
const { user } = useContext(ChromeAuthContext); | ||
const navigate = useNavigate(); | ||
const { pathname } = useLocation(); | ||
|
||
function handleTrialRedirect(pathname: string, entitlements: ChromeUser['entitlements']) { | ||
const isIgnored = ignoredPaths.some((path) => matchPath(path, pathname)); | ||
if (isIgnored) { | ||
return; | ||
} | ||
const match = Object.keys(redirects).find((path) => matchPath(path, pathname)); | ||
if (match && entitlements[redirects[match].entitlementsKey]) { | ||
const entitlement = entitlements[redirects[match].entitlementsKey]; | ||
if (!(entitlement.is_entitled || entitlement.is_trial) && !redirects[match].getTempTrialFlag()) { | ||
navigate(redirects[match].redirect); | ||
} | ||
} | ||
} | ||
useEffect(() => { | ||
handleTrialRedirect(pathname, user.entitlements); | ||
}, [pathname, user.entitlements]); | ||
} | ||
|
||
export default useTrialRedirect; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters