Herstel: uitnodigingslink bevat het admin-basispad
De frontend bouwde origin + inviteLink; op een host waar de root een aparte website is leidde de link naar die website. Nu origin + BASE_URL + pad via buildAppUrl. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { buildAppUrl } from '@/lib/links';
|
||||
|
||||
const ORIGIN = 'https://test.andreaslop.nl';
|
||||
|
||||
describe('buildAppUrl', () => {
|
||||
it('puts the admin base path between origin and path (production build)', () => {
|
||||
expect(buildAppUrl('/invite/complete?token=abc', '/admin/', ORIGIN)).toBe(
|
||||
'https://test.andreaslop.nl/admin/invite/complete?token=abc',
|
||||
);
|
||||
});
|
||||
|
||||
it('works without a trailing slash on the base', () => {
|
||||
expect(buildAppUrl('/invite/complete?token=abc', '/admin', ORIGIN)).toBe(
|
||||
'https://test.andreaslop.nl/admin/invite/complete?token=abc',
|
||||
);
|
||||
});
|
||||
|
||||
it('adds nothing for the root base (dev server)', () => {
|
||||
expect(buildAppUrl('/invite/complete?token=abc', '/', 'http://localhost:5173')).toBe(
|
||||
'http://localhost:5173/invite/complete?token=abc',
|
||||
);
|
||||
});
|
||||
|
||||
it('tolerates a path without a leading slash', () => {
|
||||
expect(buildAppUrl('invite/complete', '/admin/', ORIGIN)).toBe('https://test.andreaslop.nl/admin/invite/complete');
|
||||
});
|
||||
|
||||
it('keeps an encoded token intact', () => {
|
||||
expect(buildAppUrl('/invite/complete?token=a%2Fb%3D', '/admin/', ORIGIN)).toBe(
|
||||
'https://test.andreaslop.nl/admin/invite/complete?token=a%2Fb%3D',
|
||||
);
|
||||
});
|
||||
|
||||
it.each([
|
||||
['/admin/', '/invite/complete?token=a'],
|
||||
['/admin//', '/invite/complete?token=a'],
|
||||
['/admin', 'invite/complete?token=a'],
|
||||
['/', '/invite/complete?token=a'],
|
||||
['', 'invite/complete?token=a'],
|
||||
])('never produces a double or missing slash (base %j, path %j)', (base, path) => {
|
||||
const url = buildAppUrl(path, base, ORIGIN);
|
||||
|
||||
expect(url.replace('https://', '')).not.toContain('//');
|
||||
expect(url).toMatch(/^https:\/\/test\.andreaslop\.nl(\/admin)?\/invite\/complete\?token=a$/);
|
||||
});
|
||||
|
||||
it('defaults to the Vite base and the current origin', () => {
|
||||
expect(buildAppUrl('/x')).toBe(`${window.location.origin}${import.meta.env.BASE_URL.replace(/\/+$/, '')}/x`);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,18 @@
|
||||
/**
|
||||
* Turns an app-relative path from the API (for example an invitation link,
|
||||
* `/invite/complete?token=...`) into an absolute URL a person can open.
|
||||
*
|
||||
* The admin is mounted under a base path ('/admin/' in builds, '/' in dev), which is the same value the
|
||||
* router uses as its basepath. Prefixing only the origin would point at the host root, which on a host
|
||||
* that serves a separate website there is that website's page, not this app.
|
||||
*/
|
||||
export function buildAppUrl(
|
||||
appPath: string,
|
||||
base: string = import.meta.env.BASE_URL,
|
||||
origin: string = window.location.origin,
|
||||
): string {
|
||||
const trimmedBase = base.replace(/\/+$/, '');
|
||||
const path = appPath.startsWith('/') ? appPath : `/${appPath}`;
|
||||
|
||||
return `${origin}${trimmedBase}${path}`;
|
||||
}
|
||||
Reference in New Issue
Block a user