Repository navigation
fix(ENG-902): send a cleared optional field as null on account edit #250
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
55b3100
c42c453
df8b48a
d6ec9ca
9745845
9407133
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,173 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import type { ConnectorConfigField } from '../types'; | ||
| import { buildCredentialsPayload, withoutClearedFields } from './credentialsPayload'; | ||
|
|
||
| const field = (overrides: Partial<ConnectorConfigField>): ConnectorConfigField => ({ | ||
| label: overrides.key ?? 'field', | ||
| key: 'field', | ||
| required: false, | ||
| readOnly: false, | ||
| secret: false, | ||
| placeholder: '', | ||
| ...overrides, | ||
| }); | ||
|
|
||
| const placeholder = '__secretvalue:**redacted**1234'; | ||
|
|
||
| const shownFields = [ | ||
| field({ key: 'apiUrl', required: true, value: 'https://api.example.com' }), | ||
| field({ key: 'region', value: 'eu' }), | ||
| field({ key: 'retries', value: '3' }), | ||
| field({ key: 'subdomain' }), | ||
| field({ key: 'apiKey', secret: true, required: true, value: placeholder }), | ||
| field({ key: 'apiToken', type: 'password', value: placeholder }), | ||
| ]; | ||
|
|
||
| describe('buildCredentialsPayload', () => { | ||
| describe('when editing an account', () => { | ||
| it('sends a field the form showed with a value and the user emptied as null', () => { | ||
| const actual = buildCredentialsPayload({ | ||
| formData: { apiUrl: 'https://api.example.com', region: '', retries: '' }, | ||
| fields: shownFields, | ||
| editingSecrets: new Set(), | ||
| editing: true, | ||
| }); | ||
|
|
||
| expect(actual).toEqual({ | ||
| apiUrl: 'https://api.example.com', | ||
| region: null, | ||
| retries: null, | ||
| }); | ||
| }); | ||
|
|
||
| it('sends a field showing a connector default the user emptied as null', () => { | ||
| const actual = buildCredentialsPayload({ | ||
| formData: { apiUrl: 'https://api.example.com', timeout: '' }, | ||
| fields: [...shownFields, field({ key: 'timeout', type: 'number', value: 0 })], | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The hook never produces this state. The prefill in Either use a truthy default like
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That state is reachable for a connector default, as opposed to a stored value. When nothing is stored, What you're describing is the stored-value case: a stored |
||
| editingSecrets: new Set(), | ||
| editing: true, | ||
| }); | ||
|
|
||
| expect(actual).toEqual({ apiUrl: 'https://api.example.com', timeout: null }); | ||
| }); | ||
|
|
||
| it('never clears a secret field, even one showing a real value', () => { | ||
| const actual = buildCredentialsPayload({ | ||
| formData: { apiUrl: 'https://api.example.com', apiToken: '' }, | ||
| fields: [ | ||
| ...shownFields.filter((f) => f.key !== 'apiToken'), | ||
| field({ key: 'apiToken', type: 'password', value: 'legacy-token' }), | ||
| ], | ||
| editingSecrets: new Set(['apiToken']), | ||
| editing: true, | ||
| }); | ||
|
|
||
| expect(actual).toEqual({ apiUrl: 'https://api.example.com' }); | ||
| }); | ||
|
|
||
| it('treats a field without a secret flag as a secret, as the API stores it', () => { | ||
| const actual = buildCredentialsPayload({ | ||
| formData: { apiUrl: 'https://api.example.com', webhookKey: '' }, | ||
| fields: [ | ||
| ...shownFields, | ||
| field({ key: 'webhookKey', secret: undefined, value: placeholder }), | ||
| ], | ||
| editingSecrets: new Set(['webhookKey']), | ||
| editing: true, | ||
| }); | ||
|
|
||
| expect(actual).toEqual({ apiUrl: 'https://api.example.com' }); | ||
| }); | ||
|
|
||
| it('never clears a required field, leaving the stored value to the API', () => { | ||
| const actual = buildCredentialsPayload({ | ||
| formData: { apiUrl: '', region: 'eu' }, | ||
| fields: shownFields, | ||
| editingSecrets: new Set(), | ||
| editing: true, | ||
| }); | ||
|
|
||
| expect(actual).toEqual({ region: 'eu' }); | ||
| }); | ||
|
|
||
| it('leaves out an empty field the form showed without a value', () => { | ||
| const actual = buildCredentialsPayload({ | ||
| formData: { apiUrl: 'https://api.example.com', subdomain: '' }, | ||
| fields: shownFields, | ||
| editingSecrets: new Set(), | ||
| editing: true, | ||
| }); | ||
|
|
||
| expect(actual).toEqual({ apiUrl: 'https://api.example.com' }); | ||
| }); | ||
|
|
||
| it('leaves out a secret the user did not open, so the stored one is kept', () => { | ||
| const actual = buildCredentialsPayload({ | ||
| formData: { apiUrl: 'https://api.example.com', apiKey: placeholder }, | ||
| fields: shownFields, | ||
| editingSecrets: new Set(), | ||
| editing: true, | ||
| }); | ||
|
|
||
| expect(actual).toEqual({ apiUrl: 'https://api.example.com' }); | ||
| }); | ||
|
|
||
| it('leaves out a secret the user opened and left empty, so the stored one is kept', () => { | ||
| const actual = buildCredentialsPayload({ | ||
| formData: { apiUrl: 'https://api.example.com', apiToken: '' }, | ||
| fields: shownFields, | ||
| editingSecrets: new Set(['apiToken']), | ||
| editing: true, | ||
| }); | ||
|
|
||
| expect(actual).toEqual({ apiUrl: 'https://api.example.com' }); | ||
| }); | ||
|
|
||
| it('sends a placeholder the user opened as it stands', () => { | ||
| const actual = buildCredentialsPayload({ | ||
| formData: { apiUrl: 'https://api.example.com', apiKey: placeholder }, | ||
| fields: shownFields, | ||
| editingSecrets: new Set(['apiKey']), | ||
| editing: true, | ||
| }); | ||
|
|
||
| expect(actual).toEqual({ apiUrl: 'https://api.example.com', apiKey: placeholder }); | ||
| }); | ||
|
|
||
| it('sends a value the user entered', () => { | ||
| const actual = buildCredentialsPayload({ | ||
| formData: { apiUrl: 'https://api.example.com', apiKey: 'new-key', region: 'us' }, | ||
| fields: shownFields, | ||
| editingSecrets: new Set(['apiKey']), | ||
| editing: true, | ||
| }); | ||
|
|
||
| expect(actual).toEqual({ | ||
| apiUrl: 'https://api.example.com', | ||
| apiKey: 'new-key', | ||
| region: 'us', | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('when nothing stored is shown, as on a new account or another profile', () => { | ||
| it('leaves out an empty field', () => { | ||
| const actual = buildCredentialsPayload({ | ||
| formData: { apiUrl: 'https://api.example.com', region: '', apiToken: '' }, | ||
| fields: shownFields, | ||
| editingSecrets: new Set(), | ||
| editing: false, | ||
| }); | ||
|
|
||
| expect(actual).toEqual({ apiUrl: 'https://api.example.com' }); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('withoutClearedFields', () => { | ||
| it('drops the cleared fields and keeps the rest', () => { | ||
| const actual = withoutClearedFields({ apiUrl: 'https://api.example.com', region: null }); | ||
|
|
||
| expect(actual).toEqual({ apiUrl: 'https://api.example.com' }); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import type { ConnectorConfigField } from '../types'; | ||
| import { isSecretPlaceholder } from './secretPlaceholder'; | ||
|
|
||
| export type CredentialsPayload = Record<string, string | null>; | ||
|
|
||
| export const shownFieldValue = (field: ConnectorConfigField): string => | ||
| field.value === undefined ? '' : field.value.toString(); | ||
|
|
||
| const isSecretField = (field: ConnectorConfigField) => | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optional secrets still can't be cleared. Should an optional secret in
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not clearing secrets is intentional. The Edit button on a secret runs A |
||
| field.secret !== false || field.type === 'password'; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A field with no
Switching to |
||
|
|
||
| const untouchedSecretKeys = ( | ||
| formData: Record<string, string>, | ||
| fields: ConnectorConfigField[], | ||
| editingSecrets: Set<string>, | ||
| ) => | ||
| new Set( | ||
| fields | ||
| .filter( | ||
| (field) => | ||
| isSecretField(field) && | ||
| isSecretPlaceholder(formData[field.key]) && | ||
| !editingSecrets.has(field.key), | ||
| ) | ||
| .map((field) => field.key), | ||
| ); | ||
|
|
||
| const emptiedKeys = (formData: Record<string, string>, fields: ConnectorConfigField[]) => | ||
| new Set( | ||
| fields | ||
| .filter( | ||
| (field) => | ||
| !isSecretField(field) && | ||
| field.required !== true && | ||
| shownFieldValue(field) !== '' && | ||
| formData[field.key] === '', | ||
| ) | ||
| .map((field) => field.key), | ||
| ); | ||
|
|
||
| /** | ||
| * On an edit, a field the form showed with a value and the user emptied is sent as `null`, so | ||
| * the API removes it. Secrets are never cleared this way: opening one empties it before any typing. | ||
| */ | ||
| export const buildCredentialsPayload = ({ | ||
| formData, | ||
| fields, | ||
| editingSecrets, | ||
| editing, | ||
| }: { | ||
| formData: Record<string, string>; | ||
| fields: ConnectorConfigField[]; | ||
| editingSecrets: Set<string>; | ||
| editing: boolean; | ||
| }): CredentialsPayload => { | ||
| const keptSecrets = untouchedSecretKeys(formData, fields, editingSecrets); | ||
| const cleared = editing ? emptiedKeys(formData, fields) : new Set<string>(); | ||
|
|
||
| return Object.fromEntries( | ||
| Object.entries(formData) | ||
| .filter(([key, value]) => !keptSecrets.has(key) && (value !== '' || cleared.has(key))) | ||
| .map(([key, value]) => [key, cleared.has(key) ? null : value]), | ||
| ); | ||
| }; | ||
|
|
||
| export const withoutClearedFields = (credentials: CredentialsPayload): Record<string, string> => | ||
| Object.fromEntries( | ||
| Object.entries(credentials).filter((entry): entry is [string, string] => entry[1] !== null), | ||
| ); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On OAuth2 accounts a cleared optional field is still kept.
filledCredentialshas thenullkeys stripped, so the popup URL carries nothing for a field the user emptied, and the account keeps the old value. For example, withregion=eustored, emptyingregionand reconnecting leavesregion=eu.Could we pass cleared keys through the URL too (an empty value the API reads as removal), or call out in the PR that OAuth edits are out of scope for ENG-902?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Out of scope for ENG-902, as the PR body's "Sibling surfaces" table says (deferred). An empty query param alone wouldn't fix it: the OAuth callback merges the URL params onto the stored credentials, the same way the PATCH did. So removal needs an API change on the
/connect/oauth2callback too, ideally carrying the cleared keys on the connection attempt rather than in the URL. That's a separate change across hub and uc-api, so it'll get its own follow-up ticket, linked here once it's filed.