Skip to content

Commit

Permalink
Add test for redirect uri
Browse files Browse the repository at this point in the history
  • Loading branch information
wizardlyhel committed Jan 15, 2025
1 parent dda1d5f commit 7e79643
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
60 changes: 60 additions & 0 deletions packages/hydrogen/src/customer/customer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1392,6 +1392,66 @@ describe('customer', () => {
expect(customAuthStatusHandler).toHaveBeenCalledOnce();
}
});

it('handles Remix `https://localhost/account/orders.data` url extensions when passing current path as param if logged out', async () => {
const customer = createCustomerAccountClient({
session,
customerAccountId: 'customerAccountId',
customerAccountUrl: 'https://customer-api',
request: new Request('https://localhost/account/orders.data'),
waitUntil: vi.fn(),
});
(session.get as any).mockReturnValueOnce(undefined);

try {
await customer.handleAuthStatus();
} catch (error) {
expect((error as Response).status).toBe(302);
expect((error as Response).headers.get('location')).toBe(
'/account/login?return_to=%2Faccount%2Forders',
);
}
});

it('handles Remix `https://localhost/account/_root.data` url extensions when passing current path as param if logged out', async () => {
const customer = createCustomerAccountClient({
session,
customerAccountId: 'customerAccountId',
customerAccountUrl: 'https://customer-api',
request: new Request('https://localhost/account/_root.data'),
waitUntil: vi.fn(),
});
(session.get as any).mockReturnValueOnce(undefined);

try {
await customer.handleAuthStatus();
} catch (error) {
expect((error as Response).status).toBe(302);
expect((error as Response).headers.get('location')).toBe(
'/account/login?return_to=%2Faccount',
);
}
});

it('handles Remix `https://localhost/_root.data` url extensions when passing current path as param if logged out', async () => {
const customer = createCustomerAccountClient({
session,
customerAccountId: 'customerAccountId',
customerAccountUrl: 'https://customer-api',
request: new Request('https://localhost/_root.data'),
waitUntil: vi.fn(),
});
(session.get as any).mockReturnValueOnce(undefined);

try {
await customer.handleAuthStatus();
} catch (error) {
expect((error as Response).status).toBe(302);
expect((error as Response).headers.get('location')).toBe(
'/account/login?return_to=%2F',
);
}
});
});

describe('query', () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/hydrogen/src/customer/customer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ function defaultAuthStatusHandler(
*/
const cleanedPathname = pathname
.replace(/\.data$/, '')
.replace(/^\/_root$/, '/');
.replace(/\/_root$/, '/')
.replace(/(.+)\/$/, '$1');

const redirectTo =
defaultLoginUrl +
Expand Down

0 comments on commit 7e79643

Please sign in to comment.