Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions __tests__/customization/preference-navigation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import { describe, expect, it } from 'vitest'

import {
PROFILE_SECTIONS,
PROFILE_SECTION_ALIASES,
SETTINGS_SECTIONS,
SETTINGS_SECTION_ALIASES,
readPreferenceSection,
resolvePreferenceUrl,
} from '@/lib/preferences/navigation'

describe('preference link normalization', () => {
it.each([
['appearance', 'workspace-appearance'],
['security', 'password'],
])(
'keeps a saved profile %s link on its original card',
(section, anchor) => {
const input = new URL(
`https://flare.example/dashboard/profile?section=${section}&source=bookmark`
)
const result = resolvePreferenceUrl(
input,
PROFILE_SECTIONS,
'account',
PROFILE_SECTION_ALIASES
)
expect(result.section).toBe('account')
expect(result.url.pathname + result.url.search + result.url.hash).toBe(
`/dashboard/profile?section=account&source=bookmark#${anchor}`
)
expect(input.searchParams.get('section')).toBe(section)
}
)

it.each([
['advanced', 'appearance', 'advanced-styles'],
['about', 'general', 'instance-information'],
])('preserves recovery when resolving %s', (legacy, section, anchor) => {
const { url, section: selected } = resolvePreferenceUrl(
new URL(
`https://flare.example/dashboard/settings?section=${legacy}&recovery=1&source=help`
),
SETTINGS_SECTIONS,
'general',
SETTINGS_SECTION_ALIASES
)
expect(selected).toBe(section)
expect(url.searchParams.get('recovery')).toBe('1')
expect(url.searchParams.get('source')).toBe('help')
expect(url.searchParams.get('section')).toBe(section)
expect(url.hash).toBe(`#${anchor}`)
expect(
resolvePreferenceUrl(
url,
SETTINGS_SECTIONS,
'general',
SETTINGS_SECTION_ALIASES
).url.href
).toBe(url.href)
})

it('retains an explicitly requested card anchor', () => {
const { url } = resolvePreferenceUrl(
new URL(
'https://flare.example/dashboard/settings?section=advanced&recovery=1#custom-css'
),
SETTINGS_SECTIONS,
'general',
SETTINGS_SECTION_ALIASES
)
expect(url.hash).toBe('#custom-css')
expect(url.searchParams.get('section')).toBe('appearance')
})

it.each([
undefined,
null,
'',
'constructor',
'__proto__',
'https://untrusted.example',
'advanced&recovery=1',
['advanced', 'appearance'],
])('rejects invalid section values: %j', (value) => {
expect(
readPreferenceSection(
value,
SETTINGS_SECTIONS,
'general',
SETTINGS_SECTION_ALIASES
)
).toBe('general')
})

it('matches server fallback for repeated section parameters', () => {
const { section, url } = resolvePreferenceUrl(
new URL(
'https://flare.example/dashboard/settings?section=advanced&section=email&recovery=1'
),
SETTINGS_SECTIONS,
'general',
SETTINGS_SECTION_ALIASES
)
expect(section).toBe('general')
expect(url.hash).toBe('')
expect(url.searchParams.getAll('section')).toEqual(['advanced', 'email'])
})

it('keeps canonical links and fragments unchanged', () => {
const input = new URL(
'https://flare.example/dashboard/profile?section=account#workspace-appearance'
)
expect(
resolvePreferenceUrl(
input,
PROFILE_SECTIONS,
'account',
PROFILE_SECTION_ALIASES
)
).toEqual({ section: 'account', url: input })
})
})
53 changes: 52 additions & 1 deletion __tests__/customization/settings-navigation.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import CustomizePage from '@/app/(main)/dashboard/customize/page'
import IntegrationsPage from '@/app/(main)/dashboard/integrations/page'
import ProfilePage from '@/app/(main)/dashboard/profile/page'
import SettingsPage from '@/app/(main)/dashboard/settings/page'
import UploadProfilesPage from '@/app/(main)/dashboard/upload-profiles/page'
import { beforeEach, describe, expect, it, vi } from 'vitest'
Expand Down Expand Up @@ -30,6 +31,7 @@ vi.mock('@/lib/email/config', () => ({
vi.mock('@/components/settings/instance-settings', () => ({
InstanceSettings: () => null,
}))
vi.mock('@/components/profile', () => ({ ProfileClient: () => null }))

beforeEach(() => {
vi.clearAllMocks()
Expand Down Expand Up @@ -66,14 +68,20 @@ describe('settings permission and legacy navigation', () => {
mocks.user.mockResolvedValue({ role: 'USER' })
await expect(
CustomizePage({ searchParams: Promise.resolve({ recovery: '1' }) })
).rejects.toThrow('redirect:/dashboard/profile?section=appearance')
).rejects.toThrow(
'redirect:/dashboard/profile?section=account#workspace-appearance'
)
expect(mocks.config).not.toHaveBeenCalled()
})

it.each([
['appearance', 'appearance'],
['advanced', 'appearance'],
['about', 'general'],
['access', 'access'],
['https://untrusted.example', 'general'],
['appearance&recovery=1', 'general'],
[['appearance', 'advanced'], 'general'],
])(
'loads an authorized section without exposing email credentials: %s',
async (section, expected) => {
Expand Down Expand Up @@ -123,3 +131,46 @@ describe('settings permission and legacy navigation', () => {
await expect(IntegrationsPage()).rejects.toThrow('redirect:/auth/login')
})
})

describe('profile section compatibility', () => {
it.each([
['account', 'account'],
['appearance', 'account'],
['security', 'account'],
['uploads', 'uploads'],
['integrations', 'integrations'],
['data', 'data'],
['advanced', 'account'],
[['appearance', 'uploads'], 'account'],
])('loads the canonical section for %s', async (section, expected) => {
mocks.user.mockResolvedValue({
id: 'operator',
role: 'ADMIN',
storageUsed: 0,
preferences: {},
_count: { files: 0, shortenedUrls: 0 },
})
mocks.config.mockResolvedValue({
settings: {
general: {
storage: {
quotas: { enabled: false, default: { value: 1, unit: 'GB' } },
},
},
},
})
const page = await ProfilePage({
searchParams: Promise.resolve({ section }),
})
expect(page.props.initialSection).toBe(expected)
})

it('still requires sign-in before loading an aliased section', async () => {
mocks.session.mockResolvedValue(null)
await expect(
ProfilePage({ searchParams: Promise.resolve({ section: 'security' }) })
).rejects.toThrow('redirect:/auth/login')
expect(mocks.user).not.toHaveBeenCalled()
expect(mocks.config).not.toHaveBeenCalled()
})
})
Loading
Loading