Skip to content

Commit 351639a

Browse files
authored
Merge pull request #1816 from CruGlobal/hs-1639514
HS-1639514 - Fix Google Import, MailChimp, Organization, and PrayerLetters redirects
2 parents 5a00e95 + a6b05bc commit 351639a

2 files changed

Lines changed: 88 additions & 5 deletions

File tree

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import React, { PropsWithChildren } from 'react';
2+
import { renderHook } from '@testing-library/react';
3+
import { Session } from 'next-auth';
4+
import { useSession } from 'next-auth/react';
5+
import { renderToString } from 'react-dom/server';
6+
import TestRouter from '__tests__/util/TestRouter';
7+
import { IntegrationAccordion } from 'src/components/Shared/Forms/Accordions/AccordionEnum';
8+
import { useOauthUrl } from './useOauthUrl';
9+
10+
jest.mock('next-auth/react');
11+
12+
const accountListId = 'account-list-1';
13+
const apiToken = 'apiToken';
14+
15+
const Wrapper = ({ children }: PropsWithChildren) => (
16+
<TestRouter router={{ query: { accountListId } }}>{children}</TestRouter>
17+
);
18+
19+
const renderUseOauthUrl = () =>
20+
renderHook(() => useOauthUrl(), { wrapper: Wrapper });
21+
22+
beforeEach(() => {
23+
process.env.OAUTH_URL = 'https://auth.mpdx.org';
24+
(useSession as jest.Mock).mockReturnValue({
25+
data: { user: { apiToken } } as Session,
26+
});
27+
});
28+
29+
describe('useOauthUrl', () => {
30+
it('builds the Google OAuth url with the redirect back to integrations', () => {
31+
const { result } = renderUseOauthUrl();
32+
33+
expect(result.current.getGoogleOauthUrl()).toBe(
34+
`https://auth.mpdx.org/auth/user/google?account_list_id=${accountListId}&redirect_to=${encodeURIComponent(
35+
`http://localhost/accountLists/${accountListId}/settings/integrations?selectedTab=${IntegrationAccordion.Google}`,
36+
)}&access_token=${apiToken}`,
37+
);
38+
});
39+
40+
it('includes the organization id in the DonorHub OAuth url', () => {
41+
const { result } = renderUseOauthUrl();
42+
43+
expect(result.current.getOrganizationOauthUrl('org-1')).toBe(
44+
`https://auth.mpdx.org/auth/user/donorhub?account_list_id=${accountListId}&redirect_to=${encodeURIComponent(
45+
`http://localhost/accountLists/${accountListId}/settings/integrations?selectedTab=${IntegrationAccordion.Organization}`,
46+
)}&access_token=${apiToken}&organization_id=org-1`,
47+
);
48+
});
49+
50+
it('renders on the server when window is undefined', () => {
51+
const Probe = () => {
52+
const { getGoogleOauthUrl } = useOauthUrl();
53+
return <a href={getGoogleOauthUrl()}>{'link'}</a>;
54+
};
55+
56+
const originalWindow = global.window;
57+
// @ts-expect-error simulating a server environment with no window
58+
delete global.window;
59+
60+
try {
61+
let html = '';
62+
expect(() => {
63+
html = renderToString(
64+
<Wrapper>
65+
<Probe />
66+
</Wrapper>,
67+
);
68+
}).not.toThrow();
69+
70+
expect(html).toContain(
71+
`redirect_to=${encodeURIComponent(
72+
`/accountLists/${accountListId}/settings/integrations?selectedTab=${IntegrationAccordion.Google}`,
73+
)}`,
74+
);
75+
} finally {
76+
global.window = originalWindow;
77+
}
78+
});
79+
});

src/components/Settings/integrations/useOauthUrl.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { useEffect, useState } from 'react';
12
import { IntegrationAccordion } from 'src/components/Shared/Forms/Accordions/AccordionEnum';
23
import { useAccountListId } from 'src/hooks/useAccountListId';
34
import { useRequiredSession } from 'src/hooks/useRequiredSession';
@@ -6,12 +7,15 @@ export const useOauthUrl = () => {
67
const { apiToken } = useRequiredSession();
78
const accountListId = useAccountListId();
89

9-
const getRedirectUrl = (accordion: IntegrationAccordion) => {
10-
const domain = window.location.origin;
11-
return encodeURIComponent(
12-
`${domain}/accountLists/${accountListId}/settings/integrations?selectedTab=${accordion}`,
10+
const [origin, setOrigin] = useState('');
11+
useEffect(() => {
12+
setOrigin(window.location.origin);
13+
}, []);
14+
15+
const getRedirectUrl = (accordion: IntegrationAccordion) =>
16+
encodeURIComponent(
17+
`${origin}/accountLists/${accountListId}/settings/integrations?selectedTab=${accordion}`,
1318
);
14-
};
1519

1620
return {
1721
getGoogleOauthUrl: () =>

0 commit comments

Comments
 (0)