-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Added separate base 64 referrer for oauth state
- Loading branch information
Showing
5 changed files
with
107 additions
and
17 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 |
---|---|---|
|
@@ -16,7 +16,7 @@ | |
*/ | ||
import { render, screen } from 'tests/utils'; | ||
import React from 'react'; | ||
import { useSearchParams } from 'react-router-dom'; | ||
import { useNavigate, useSearchParams } from 'react-router-dom'; | ||
import * as accountService from 'terraso-client-shared/account/accountService'; | ||
|
||
import AccountLogin from 'account/components/AccountLogin'; | ||
|
@@ -26,10 +26,12 @@ jest.mock('terraso-client-shared/account/accountService'); | |
jest.mock('react-router-dom', () => ({ | ||
...jest.requireActual('react-router-dom'), | ||
useSearchParams: jest.fn(), | ||
useNavigate: jest.fn(), | ||
})); | ||
|
||
beforeEach(() => { | ||
useSearchParams.mockReturnValue([new URLSearchParams(), () => {}]); | ||
useNavigate.mockReturnValue(jest.fn()); | ||
}); | ||
|
||
test('AccountLogin: Display error', async () => { | ||
|
@@ -61,7 +63,8 @@ test('AccountLogin: Display buttons', async () => { | |
|
||
test('AccountLogin: Add referrer', async () => { | ||
const searchParams = new URLSearchParams(); | ||
searchParams.set('referrer', 'groups?sort=-name'); | ||
const referrer = encodeURIComponent('groups?sort=-name&other=1'); | ||
searchParams.set('referrer', referrer); | ||
useSearchParams.mockReturnValue([searchParams]); | ||
accountService.getAuthURLs.mockReturnValue( | ||
Promise.resolve({ | ||
|
@@ -71,17 +74,76 @@ test('AccountLogin: Add referrer', async () => { | |
); | ||
await render(<AccountLogin />); | ||
expect(screen.getByText('Continue with Google')).toBeInTheDocument(); | ||
const state = `account?referrerBase64=${btoa(referrer)}`; | ||
expect(screen.getByText('Continue with Google')).toHaveAttribute( | ||
'href', | ||
'google.url&state=groups?sort=-name' | ||
`google.url&state=${state}` | ||
); | ||
expect(screen.getByText('Continue with Apple')).toBeInTheDocument(); | ||
expect(screen.getByText('Continue with Apple')).toHaveAttribute( | ||
'href', | ||
'apple.url&state=groups?sort=-name' | ||
`apple.url&state=${state}` | ||
); | ||
}); | ||
|
||
test('AccountLogin: Navigate to referrer if logged in', async () => { | ||
accountService.getAuthURLs.mockReturnValue( | ||
Promise.resolve({ | ||
google: 'google.url', | ||
apple: 'apple.url', | ||
}) | ||
); | ||
const navigate = jest.fn(); | ||
useNavigate.mockReturnValue(navigate); | ||
const searchParams = new URLSearchParams(); | ||
const referrer = encodeURIComponent('groups?sort=-name&other=1'); | ||
searchParams.set('referrer', referrer); | ||
useSearchParams.mockReturnValue([searchParams]); | ||
await render(<AccountLogin />, { | ||
account: { | ||
hasToken: true, | ||
login: {}, | ||
currentUser: { | ||
data: { | ||
email: '[email protected]', | ||
}, | ||
}, | ||
}, | ||
}); | ||
expect(navigate).toHaveBeenCalledWith('groups?sort=-name&other=1', { | ||
replace: true, | ||
}); | ||
}); | ||
|
||
test('AccountLogin: Navigate to referrer base 64 if logged in', async () => { | ||
accountService.getAuthURLs.mockReturnValue( | ||
Promise.resolve({ | ||
google: 'google.url', | ||
apple: 'apple.url', | ||
}) | ||
); | ||
const navigate = jest.fn(); | ||
useNavigate.mockReturnValue(navigate); | ||
const searchParams = new URLSearchParams(); | ||
const referrer = encodeURIComponent('groups?sort=-name&other=1'); | ||
searchParams.set('referrerBase64', btoa(referrer)); | ||
useSearchParams.mockReturnValue([searchParams]); | ||
await render(<AccountLogin />, { | ||
account: { | ||
hasToken: true, | ||
login: {}, | ||
currentUser: { | ||
data: { | ||
email: '[email protected]', | ||
}, | ||
}, | ||
}, | ||
}); | ||
expect(navigate).toHaveBeenCalledWith('groups?sort=-name&other=1', { | ||
replace: true, | ||
}); | ||
}); | ||
|
||
test('AccountLogin: Display locale picker', async () => { | ||
accountService.getAuthURLs.mockReturnValue( | ||
Promise.resolve({ | ||
|
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
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