|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { describe, expect, it } from 'vitest' |
| 5 | +import { |
| 6 | + BOX_SERVICE_ACCOUNT_PROVIDER_ID, |
| 7 | + getClientCredentialAccountDescriptor, |
| 8 | + partitionClientCredentialFields, |
| 9 | + resolveClientCredentialAuthMethod, |
| 10 | + resolveSalesforceAuthMethod, |
| 11 | + SALESFORCE_DEFAULT_AUTH_METHOD, |
| 12 | + SALESFORCE_SERVICE_ACCOUNT_PROVIDER_ID, |
| 13 | + ZOHO_DESK_SERVICE_ACCOUNT_PROVIDER_ID, |
| 14 | +} from '@/lib/credentials/client-credential-accounts/descriptors' |
| 15 | + |
| 16 | +const salesforce = getClientCredentialAccountDescriptor(SALESFORCE_SERVICE_ACCOUNT_PROVIDER_ID)! |
| 17 | +const box = getClientCredentialAccountDescriptor(BOX_SERVICE_ACCOUNT_PROVIDER_ID)! |
| 18 | +const zohoDesk = getClientCredentialAccountDescriptor(ZOHO_DESK_SERVICE_ACCOUNT_PROVIDER_ID)! |
| 19 | + |
| 20 | +const ids = (fields: { id: string }[]) => fields.map((field) => field.id) |
| 21 | + |
| 22 | +describe('partitionClientCredentialFields', () => { |
| 23 | + describe('single-grant providers are unaffected by the auth-method machinery', () => { |
| 24 | + it('keeps every Box field visible and required', () => { |
| 25 | + const { visible, required } = partitionClientCredentialFields(box, undefined) |
| 26 | + expect(ids(visible)).toEqual(['clientId', 'clientSecret', 'orgId']) |
| 27 | + expect(ids(required)).toEqual(['clientId', 'clientSecret', 'orgId']) |
| 28 | + }) |
| 29 | + |
| 30 | + it("keeps Zoho Desk's optional data center visible but not required", () => { |
| 31 | + const { visible, required } = partitionClientCredentialFields(zohoDesk, undefined) |
| 32 | + expect(ids(visible)).toContain('dataCenter') |
| 33 | + expect(ids(required)).not.toContain('dataCenter') |
| 34 | + expect(ids(required)).toEqual(['clientId', 'clientSecret', 'orgId']) |
| 35 | + }) |
| 36 | + |
| 37 | + it('ignores an auth method a single-grant provider does not declare', () => { |
| 38 | + const { required } = partitionClientCredentialFields(box, 'jwt_bearer') |
| 39 | + expect(ids(required)).toEqual(['clientId', 'clientSecret', 'orgId']) |
| 40 | + }) |
| 41 | + }) |
| 42 | + |
| 43 | + describe('Salesforce, which offers two grants', () => { |
| 44 | + it('requires the consumer secret and hides key material on the client-credentials branch', () => { |
| 45 | + const { visible, required } = partitionClientCredentialFields( |
| 46 | + salesforce, |
| 47 | + 'client_credentials' |
| 48 | + ) |
| 49 | + expect(ids(required)).toEqual(['clientId', 'clientSecret', 'orgId']) |
| 50 | + expect(ids(visible)).not.toContain('privateKey') |
| 51 | + expect(ids(visible)).not.toContain('username') |
| 52 | + }) |
| 53 | + |
| 54 | + it('requires the key and username on the JWT branch, and hides the consumer secret', () => { |
| 55 | + const { visible, required } = partitionClientCredentialFields(salesforce, 'jwt_bearer') |
| 56 | + expect(ids(required)).toEqual(['clientId', 'privateKey', 'username', 'orgId']) |
| 57 | + expect(ids(visible)).not.toContain('clientSecret') |
| 58 | + }) |
| 59 | + |
| 60 | + it.each([ |
| 61 | + ['absent', undefined], |
| 62 | + ['empty', ''], |
| 63 | + ['unrecognized', 'totally-made-up'], |
| 64 | + ])('falls back to the default grant when the method is %s', (_label, authMethod) => { |
| 65 | + // Credentials created before the JWT branch existed carry no `authMethod`, |
| 66 | + // so the fallback is what keeps them minting as they always did. |
| 67 | + const { required } = partitionClientCredentialFields(salesforce, authMethod) |
| 68 | + expect(ids(required)).toContain('clientSecret') |
| 69 | + expect(ids(required)).not.toContain('privateKey') |
| 70 | + }) |
| 71 | + |
| 72 | + it('never marks the method selector itself required', () => { |
| 73 | + const { visible, required } = partitionClientCredentialFields(salesforce, 'jwt_bearer') |
| 74 | + expect(ids(visible)).toContain('authMethod') |
| 75 | + expect(ids(required)).not.toContain('authMethod') |
| 76 | + }) |
| 77 | + }) |
| 78 | +}) |
| 79 | + |
| 80 | +describe('resolveClientCredentialAuthMethod', () => { |
| 81 | + it('returns undefined for a provider that declares no method selector', () => { |
| 82 | + expect(resolveClientCredentialAuthMethod(box, 'jwt_bearer')).toBeUndefined() |
| 83 | + }) |
| 84 | + |
| 85 | + it('accepts a declared method and rejects anything else', () => { |
| 86 | + expect(resolveClientCredentialAuthMethod(salesforce, 'jwt_bearer')).toBe('jwt_bearer') |
| 87 | + expect(resolveClientCredentialAuthMethod(salesforce, ' jwt_bearer ')).toBe('jwt_bearer') |
| 88 | + expect(resolveClientCredentialAuthMethod(salesforce, 'nope')).toBe( |
| 89 | + SALESFORCE_DEFAULT_AUTH_METHOD |
| 90 | + ) |
| 91 | + }) |
| 92 | +}) |
| 93 | + |
| 94 | +describe('resolveSalesforceAuthMethod', () => { |
| 95 | + it('agrees with the descriptor-driven resolver the create path uses', () => { |
| 96 | + // The minter holds only the raw stored value; if these two ever disagreed a |
| 97 | + // credential would validate under one grant and mint under the other. |
| 98 | + for (const value of [undefined, '', 'client_credentials', 'jwt_bearer', 'garbage']) { |
| 99 | + expect(resolveSalesforceAuthMethod(value)).toBe( |
| 100 | + resolveClientCredentialAuthMethod(salesforce, value) |
| 101 | + ) |
| 102 | + } |
| 103 | + }) |
| 104 | +}) |
0 commit comments