Files
twenty/packages/twenty-server/test/integration/metadata/suites/object-metadata/failing-create-one-object-metadata.integration-spec.ts
Paul Rastoin 41f3a63962 [BUGFIX] ObjectMetadata item server validation (#10699)
# Introduction
This PR contains several SNAPSHOT files explaining big +

While refactoring the Object Model settings page in
https://github.com/twentyhq/twenty/pull/10653, encountered a critical
issue when submitting either one or both names with `""` empty string
hard corrupting a workspace.

This motivate this PR reviewing server side validation

I feel like we could share zod schema between front and back

## Refactored server validation
What to expect from Names:
- Plural and singular have to be different ( case insensitive and
trimmed check )
- Contains only a-z A-Z and 0-9
- Follows camelCase
- Is not empty => Is not too short ( 1 )
- Is not too long ( 63 )
- Is case insensitive( fooBar and fOoBar now rejected )

What to expect from Labels:
- Plural and singular have to be different ( case insensitive and
trimmed check )
- Is not empty => Is not too short ( 1 )
- Is not too long ( 63 )
- Is case insensitive ( fooBar and fOoBar now rejected )

close https://github.com/twentyhq/twenty/issues/10694

## Creation integrations tests
Created new integrations tests, following
[EachTesting](https://jestjs.io/docs/api#testeachtablename-fn-timeout)
pattern and uses snapshot to assert errors message. These tests cover
several failing use cases and started to implement ones for the happy
path but object metadata item deletion is currently broken unless I'm
mistaken @Weiko is on it

## Notes
- [ ] As we've added new validation rules towards names and labels we
should scan db in order to standardize existing values using either a
migration command or manual check
- [ ] Will review in an other PR the update path, adding integrations
tests and so on
2025-03-11 12:14:37 +01:00

138 lines
3.9 KiB
TypeScript

import { getMockCreateObjectInput } from 'test/integration/utils/object-metadata/generate-mock-create-object-metadata-input';
import { performFailingObjectMetadataCreation } from 'test/integration/utils/object-metadata/perform-failing-object-metadata-creation';
import { EachTestingContext } from 'twenty-shared';
import { CreateObjectInput } from 'src/engine/metadata-modules/object-metadata/dtos/create-object.input';
import { ErrorCode } from 'src/engine/core-modules/graphql/utils/graphql-errors.util';
type CreateObjectInputPayload = Omit<
CreateObjectInput,
'workspaceId' | 'dataSourceId'
>;
type CreateOneObjectMetadataItemTestingContext = EachTestingContext<
Partial<CreateObjectInputPayload>
>[];
const failingNamesCreationTestsUseCase: CreateOneObjectMetadataItemTestingContext =
[
{
title: 'when nameSingular has invalid characters',
context: { nameSingular: 'μ' },
},
{
title: 'when namePlural has invalid characters',
context: { namePlural: 'μ' },
},
{
title: 'when nameSingular is a reserved keyword',
context: { nameSingular: 'user' },
},
{
title: 'when namePlural is a reserved keyword',
context: { namePlural: 'users' },
},
{
title: 'when nameSingular is not camelCased',
context: { nameSingular: 'Not_Camel_Case' },
},
{
title: 'when namePlural is not camelCased',
context: { namePlural: 'Not_Camel_Case' },
},
{
title: 'when namePlural is an empty string',
context: { namePlural: '' },
},
{
title: 'when nameSingular is an empty string',
context: { nameSingular: '' },
},
{
title: 'when nameSingular contains only whitespaces',
context: { nameSingular: ' ' },
},
{
title: 'when nameSingular contains only one char and whitespaces',
context: { nameSingular: ' a a ' },
},
{
title: 'when name exceeds maximum length',
context: { nameSingular: 'a'.repeat(64) },
},
{
title: 'when names are identical',
context: {
nameSingular: 'fooBar',
namePlural: 'fooBar',
},
},
{
title: 'when names with whitespaces result to be identical',
context: {
nameSingular: ' fooBar ',
namePlural: 'fooBar',
},
},
];
const failingLabelsCreationTestsUseCase: CreateOneObjectMetadataItemTestingContext =
[
{
title: 'when labelSingular is empty',
context: { labelSingular: '' },
},
{
title: 'when labelPlural is empty',
context: { labelPlural: '' },
},
{
title: 'when labelSingular exceeds maximum length',
context: { labelSingular: 'A'.repeat(64) },
},
{
title: 'when labelPlural exceeds maximum length',
context: { labelPlural: 'A'.repeat(64) },
},
{
title: 'when labelSingular contains only whitespace',
context: { labelSingular: ' ' },
},
{
title: 'when labelPlural contains only whitespace',
context: { labelPlural: ' ' },
},
{
title: 'when labels are identical',
context: {
labelPlural: 'fooBar',
labelSingular: 'fooBar',
},
},
{
title: 'when labels with whitespaces result to be identical',
context: {
labelPlural: ' fooBar ',
labelSingular: 'fooBar',
},
},
];
const allTestsUseCases = [
...failingNamesCreationTestsUseCase,
...failingLabelsCreationTestsUseCase,
];
describe('Object metadata creation should fail', () => {
it.each(allTestsUseCases)('$title', async ({ context }) => {
const errors = await performFailingObjectMetadataCreation(
getMockCreateObjectInput(context),
);
expect(errors.length).toBe(1);
const firstError = errors[0];
expect(firstError.extensions.code).toBe(ErrorCode.BAD_USER_INPUT);
expect(firstError.message).toMatchSnapshot();
});
});