fix(admin): keep the place search provider reachable on a managed install

The API keys card was hidden wholesale on a managed install, which took
the provider choice with it although the server leaves places_provider
to the admin there. The card stays; the keys, their Save button and the
Google switches fold away, on the phone shell now too. The mobile
warning uses the pending token instead of an undefined one.

The desktop tests find a key input through its labelled toggle rather
than by position, which is where the accessible name sits.
This commit is contained in:
Maurice
2026-09-12 13:56:49 +02:00
parent f120266f9e
commit 2fd5b6b00a
6 changed files with 77 additions and 31 deletions
@@ -40,6 +40,7 @@ export default function MAdminSettingsSection({ admin, t }: MAdminSettingsSectio
allowedFileTypes, setAllowedFileTypes, savingFileTypes, setSavingFileTypes,
mapsKey, setMapsKey, unsplashKey, setUnsplashKey, amapKey, setAmapKey, savingKeys, validating, validation,
placesProvider, savingPlacesProvider, handleSavePlacesProvider,
managed,
setShowRotateJwtModal,
handleToggleAuthSetting, handleToggleRequireMfa,
handleSaveApiKeys, handleValidateKey,
@@ -216,6 +217,9 @@ export default function MAdminSettingsSection({ admin, t }: MAdminSettingsSectio
<MAdminCard>
<MAdminCardHead title={t('admin.apiKeys')} hint={t('admin.apiKeysHint')} />
<div className="space-y-3">
{/* The keys belong to the operator on a managed install, same as on the
desktop tab; the provider choice below stays the admin's. */}
{!managed && (<>
<MAdminField
label={
<span className="flex items-center gap-2">
@@ -269,6 +273,16 @@ export default function MAdminSettingsSection({ admin, t }: MAdminSettingsSectio
/>
</MAdminField>
<MAdminField label={t('admin.unsplashKey')} hint={t('admin.unsplashKeyHint')}>
<MAdminSecretInput
aria-label={t('admin.unsplashKey')}
value={unsplashKey}
onChange={(e) => setUnsplashKey(e.target.value)}
placeholder={t('settings.keyPlaceholder')}
/>
</MAdminField>
</>)}
<MAdminField label={t('admin.placesProvider.title')} hint={t('admin.placesProvider.subtitle')}>
<select
value={placesProvider}
@@ -282,22 +296,16 @@ export default function MAdminSettingsSection({ admin, t }: MAdminSettingsSectio
<option value="openstreetmap">{t('admin.placesProvider.openstreetmap')}</option>
</select>
{((placesProvider === 'google' && !mapsKey) || (placesProvider === 'amap' && !amapKey)) && (
<p className="mt-1 font-geist text-[0.625rem] font-bold text-[color:var(--m-st-warn,#b45309)]">
<p className="mt-1 font-geist text-[0.625rem] font-bold text-[color:var(--m-st-pending)]">
{t('admin.placesProvider.missingKey')}
</p>
)}
</MAdminField>
<MAdminField label={t('admin.unsplashKey')} hint={t('admin.unsplashKeyHint')}>
<MAdminSecretInput
aria-label={t('admin.unsplashKey')}
value={unsplashKey}
onChange={(e) => setUnsplashKey(e.target.value)}
placeholder={t('settings.keyPlaceholder')}
/>
</MAdminField>
<div>
{/* The four Google switches trade away quota that is the operator's on
a managed install; the search log below is the instance's own. */}
{!managed && (<>
<MAdminRow
title={t('admin.placesPhotos.title')}
hint={t('admin.placesPhotos.subtitle')}
@@ -378,6 +386,7 @@ export default function MAdminSettingsSection({ admin, t }: MAdminSettingsSectio
/>
}
/>
</>)}
<MAdminRow
title={t('admin.placeShadow.title')}
hint={t('admin.placeShadow.subtitle')}
@@ -433,9 +442,11 @@ export default function MAdminSettingsSection({ admin, t }: MAdminSettingsSectio
</div>
</div>
<MAdminButton busy={savingKeys} onClick={handleSaveApiKeys}>
{t('common.save')}
</MAdminButton>
{!managed && (
<MAdminButton busy={savingKeys} onClick={handleSaveApiKeys}>
{t('common.save')}
</MAdminButton>
)}
</div>
</MAdminCard>
+7 -4
View File
@@ -1170,8 +1170,10 @@ describe('AdminPage', () => {
// By accessible name, not by position: the card gained an Amap field
// between Maps and Unsplash, and an index would have kept passing while
// asserting about the wrong input.
fireEvent.change(within(apiKeysCard!).getByLabelText('Unsplash API Key'), {
// asserting about the wrong input. The name sits on the show/hide toggle;
// the input is its sibling.
const unsplashToggle = within(apiKeysCard!).getByLabelText('Unsplash API Key');
fireEvent.change(unsplashToggle.parentElement!.querySelector('input')!, {
target: { value: 'test-unsplash-key' },
});
@@ -1198,9 +1200,10 @@ describe('AdminPage', () => {
fireEvent.click(screen.getByRole('button', { name: /settings/i }));
const apiKeysHeading = await screen.findByRole('heading', { name: /^api keys$/i });
const apiKeysCard = apiKeysHeading.closest<HTMLElement>('.bg-white');
const apiKeysCard = apiKeysHeading.closest<HTMLElement>('.rounded-xl');
fireEvent.change(within(apiKeysCard!).getByLabelText(/Amap/), {
const amapToggle = within(apiKeysCard!).getByLabelText(/Amap/);
fireEvent.change(amapToggle.parentElement!.querySelector('input')!, {
target: { value: 'test-amap-key' },
});
fireEvent.click(within(apiKeysCard!).getByRole('button', { name: /^save$/i }));
@@ -29,6 +29,15 @@ function card(heading: string | RegExp): HTMLElement {
return screen.getByRole('heading', { name: heading }).closest<HTMLElement>('.rounded-xl')!;
}
/**
* The key input in the block whose show/hide toggle carries the given name. The
* toggle is the labelled element and the input its sibling, so this still finds
* the right field when the card gains a block, where an index would not.
*/
function keyInput(name: string | RegExp): HTMLInputElement {
return within(card('API Keys')).getByLabelText(name).parentElement!.querySelector('input')!;
}
/** The toggle button in the row belonging to a label paragraph. */
function toggleFor(label: string): HTMLElement {
const row = screen.getAllByText(label).map(el => el.closest<HTMLElement>('.flex.items-center.justify-between'));
@@ -259,16 +268,15 @@ describe('AdminSettingsTab', () => {
it('FE-ADMSET-021: showKeys switches every key input to plain text', () => {
renderTab({ showKeys: { maps: true, unsplash: true, amap: true } });
const keys = within(card('API Keys'));
expect(keys.getByLabelText('Google Maps API Key')).toHaveAttribute('type', 'text');
expect(keys.getByLabelText('Unsplash API Key')).toHaveAttribute('type', 'text');
expect(keys.getByLabelText(/Amap/)).toHaveAttribute('type', 'text');
expect(keyInput('Google Maps API Key')).toHaveAttribute('type', 'text');
expect(keyInput('Unsplash API Key')).toHaveAttribute('type', 'text');
expect(keyInput(/Amap/)).toHaveAttribute('type', 'text');
});
it('FE-ADMSET-022: unsplash key input and its visibility toggle', () => {
const admin = renderTab({ unsplashKey: '' });
const unsplashInput = within(card('API Keys')).getByLabelText('Unsplash API Key');
const unsplashInput = keyInput('Unsplash API Key');
fireEvent.change(unsplashInput, { target: { value: 'unsplash-key' } });
expect(admin.setUnsplashKey).toHaveBeenCalledWith('unsplash-key');
@@ -279,7 +287,7 @@ describe('AdminSettingsTab', () => {
it('FE-ADMSET-022b: amap key input and its visibility toggle', () => {
const admin = renderTab({ amapKey: '' });
const amapInput = within(card('API Keys')).getByLabelText(/Amap/);
const amapInput = keyInput(/Amap/);
fireEvent.change(amapInput, { target: { value: 'amap-key' } });
expect(admin.setAmapKey).toHaveBeenCalledWith('amap-key');
@@ -298,12 +306,23 @@ describe('AdminSettingsTab', () => {
it('FE-ADMSET-022d: saving the provider goes through updateAppSettings, not the keys', () => {
const admin = renderTab({ placesProvider: 'auto' });
fireEvent.change(within(card('API Keys')).getByLabelText('Place search provider'), {
target: { value: 'amap' },
});
// A CustomSelect: the trigger shows the current choice, the menu is portaled.
fireEvent.click(within(card('API Keys')).getByRole('button', { name: 'Automatic' }));
fireEvent.click(screen.getByRole('button', { name: 'Amap (高德地图)' }));
expect(admin.handleSavePlacesProvider).toHaveBeenCalledWith('amap');
});
it('FE-ADMSET-022e: a managed install hides the keys but keeps the provider choice', () => {
// The operator owns the credentials; which of them answers place search is
// still the admin's call (server managed.ts lists places_provider as theirs).
renderTab({ managed: true, placesProvider: 'auto' });
const keys = within(card('API Keys'));
expect(keys.queryByLabelText('Google Maps API Key')).not.toBeInTheDocument();
expect(keys.queryByLabelText(/Amap/)).not.toBeInTheDocument();
expect(keys.getByRole('button', { name: 'Automatic' })).toBeInTheDocument();
});
it('FE-ADMSET-023: the maps Test button is disabled without a key', () => {
renderTab({ mapsKey: '' });
+6 -5
View File
@@ -363,10 +363,10 @@ export default function AdminSettingsTab({ admin, t }: AdminSettingsTabProps): R
</div>
</div>
{/* Google and Unsplash come with the instance, and so does what a lookup costs;
the per-place toggles only ever traded away quota that is not the customer’s
to spend. Weather needs no key at all and has nothing to configure. */}
{!managed && (<>
{/* The card itself stays on a managed install: the TREK index needs no key,
and which keyed provider answers place search is the admin's call there
too (server managed.ts). What folds away below is the operator's:
the keys, what a lookup costs, and the per-place Google switches. */}
{/* API Keys.
The key fields stay visible; only the four Google switches fold away.
They are set once and then never touched, and putting them flat next to
@@ -687,6 +687,7 @@ export default function AdminSettingsTab({ admin, t }: AdminSettingsTabProps): R
/>
</div>
{!managed && (
<button type="button"
onClick={handleSaveApiKeys}
disabled={savingKeys}
@@ -695,9 +696,9 @@ export default function AdminSettingsTab({ admin, t }: AdminSettingsTabProps): R
{savingKeys ? <div className="w-4 h-4 border-2 border-current/30 border-t-current rounded-full animate-spin" /> : <Save className="w-4 h-4" />}
{t('common.save')}
</button>
)}
</div>
</div>
</>)}
</div>
</div>
+1
View File
@@ -37,6 +37,7 @@ export function buildAdminHook(overrides: Record<string, unknown> = {}): AdminHo
hour12: false,
mcpEnabled: false,
devMode: false,
managed: false,
currentUser: buildAdminUser(),
updateApiKeys: vi.fn(async () => undefined),
setAppRequireMfa: vi.fn(),
@@ -266,6 +266,17 @@ describe('MAdminSettingsSection', () => {
expect(admin.handleSaveApiKeys).toHaveBeenCalled();
});
it('FE-MOB-ASET-016b: a managed install hides the keys but keeps the provider choice', () => {
// The operator owns the credentials; which of them answers place search is
// still the admin's call, the same split the desktop tab makes.
renderSettings({ managed: true });
expect(screen.queryByLabelText('Google Maps API Key')).not.toBeInTheDocument();
expect(screen.queryByLabelText(/Amap/)).not.toBeInTheDocument();
expect(screen.queryByLabelText('Unsplash API Key')).not.toBeInTheDocument();
expect(screen.getByDisplayValue('Automatic')).toBeInTheDocument();
});
it('FE-MOB-ASET-017: the Google Places toggles persist optimistically', async () => {
const user = userEvent.setup();
const seen: string[] = [];