Field metadata relation edge cases exceptions coverage (#12866)

# Introduction
Following https://github.com/twentyhq/twenty/pull/12852

Discovered that:
- `relationCreationPayload` does not seem to be validated through the
input decorators
```ts
  // TODO @prastoin implement validation for this with validate nested and dedicated class instance
  @IsOptional()
  @Field(() => GraphQLJSON, { nullable: true })
  relationCreationPayload?: {
    targetObjectMetadataId: string;
    targetFieldLabel: string;
    targetFieldIcon: string;
    type: RelationType;
  };
```
- Sending an unknown `targetObjectMetadataId` generates an
`internal_server_error` `500` @guillim on the go
## Coverage
```ts
 PASS  test/integration/metadata/suites/object-metadata/failing-field-metadata-relation-creation.integration-spec.ts
  Field metadata relation creation should fail
    ✓ relation when targetFieldLabel is empty (109 ms)
    ✓ relation when targetFieldLabel exceeds maximum length (100 ms)
    ✓ relation when targetObjectMetadataId is unknown (97 ms)
    ✓ relation when targetFieldLabel contains only whitespace (103 ms)
    ✓ relation when targetFieldLabel conflicts with an existing field on target object metadata id (108 ms)

Test Suites: 1 passed, 1 total
Tests:       5 passed, 5 total
Snapshots:   5 passed, 5 total
Time:        2.629 s, estimated 3 s
```
This commit is contained in:
Paul Rastoin
2025-06-25 15:03:14 +02:00
committed by GitHub
parent 8d3084ef16
commit 0f106ab8e0
4 changed files with 319 additions and 0 deletions

View File

@ -22,6 +22,7 @@ export class CreateFieldInput extends OmitType(
@IsOptional()
isRemoteCreation?: boolean;
// TODO @prastoin implement validation for this with validate nested and dedicated class instance
@IsOptional()
@Field(() => GraphQLJSON, { nullable: true })
relationCreationPayload?: {

View File

@ -0,0 +1,112 @@
import { EachTestingContext } from 'twenty-shared/testing';
import { computeMetadataNameFromLabel } from 'src/engine/metadata-modules/utils/compute-metadata-name-from-label.util';
import {
InvalidMetadataException,
InvalidMetadataExceptionCode,
} from 'src/engine/metadata-modules/utils/exceptions/invalid-metadata.exception';
type ComputeMetadataNameFromLabelTestCase = EachTestingContext<{
input: string;
expected?: string;
expectToThrow?: {
error: InvalidMetadataException;
};
}>;
describe('computeMetadataNameFromLabel', () => {
const successfulTestCases: ComputeMetadataNameFromLabelTestCase[] = [
{
title: 'should convert a simple label to camelCase',
context: {
input: 'Simple Label',
expected: 'simpleLabel',
},
},
{
title: 'should handle special characters and convert to camelCase',
context: {
input: 'Special & Characters!',
expected: 'specialCharacters',
},
},
{
title: 'should prefix numeric labels with n',
context: {
input: '123 Test',
expected: 'n123Test',
},
},
{
title: 'should handle multiple spaces and convert to camelCase',
context: {
input: 'Multiple Spaces Here',
expected: 'multipleSpacesHere',
},
},
{
title: 'should handle accented characters',
context: {
input: 'Café Crème',
expected: 'cafeCreme',
},
},
{
title: 'should handle empty label',
context: {
input: '',
expected: '',
},
},
{
title: 'should handle mixed case input',
context: {
input: 'MiXeD cAsE',
expected: 'mixedCase',
},
},
];
const failingTestCases: ComputeMetadataNameFromLabelTestCase[] = [
{
title: 'should throw when label is undefined',
context: {
input: undefined as unknown as string,
expectToThrow: {
error: new InvalidMetadataException(
'Label is required',
InvalidMetadataExceptionCode.LABEL_REQUIRED,
),
},
},
},
{
title: 'should throw when label contains only special characters',
context: {
input: '!@#$%^&*()',
expectToThrow: {
error: new InvalidMetadataException(
'Invalid label: "!@#$%^&*()"',
InvalidMetadataExceptionCode.INVALID_LABEL,
),
},
},
},
];
describe('successful cases', () => {
it.each(successfulTestCases)('$title', ({ context }) => {
const result = computeMetadataNameFromLabel(context.input);
expect(result).toBe(context.expected);
});
});
describe('failing cases', () => {
it.each(failingTestCases)('$title', ({ context }) => {
expect(() => computeMetadataNameFromLabel(context.input)).toThrow(
context.expectToThrow?.error,
);
});
});
});