Some checks failed
App Prod-Parity E2E Dispatch / dispatch (push) Failing after 10s
CD Storybook UI / build (push) Failing after 3m25s
CD Storybook UI / dispatch (push) Has been skipped
CD deploy main / website-changed-files (push) Successful in 18m8s
CI Agent Skills / changed-files-check (push) Successful in 18m9s
CI Create App E2E minimal / changed-files-check (push) Successful in 24s
CI Create App / changed-files-check (push) Successful in 26s
CI Docs / changed-files-check (push) Successful in 26s
CI Emails / changed-files-check (push) Successful in 28s
CI Example App Hello World / changed-files-check (push) Successful in 23s
CI Front / changed-files-check (push) Has been skipped
CI Example App Postcard / changed-files-check (push) Successful in 27s
CI UI / changed-files-check (push) Has been skipped
CI Utils Test / changed-files-check (push) Successful in 45s
CI Twenty Apps / discover (push) Successful in 1m3s
CD deploy main / deploy-main (push) Failing after 3s
CI Agent Skills / agent-skills-validate (push) Has been skipped
CI Create App / create-app-test (lint) (push) Has been skipped
CI Emails / emails-test (push) Has been skipped
CI Front / front-task (lingui:compile) (push) Has been skipped
CI Front / front-task (lint) (push) Has been skipped
CI Example App Postcard / example-app-postcard (push) Has been skipped
CI UI / ui-task (lint) (push) Has been skipped
CI UI / ui-task (pack-size) (push) Has been skipped
CI UI / ui-task (size) (push) Has been skipped
CI UI / ui-task (test) (push) Has been skipped
CI UI / ui-task (typecheck) (push) Has been skipped
CI Utils Test / utils-test (push) Has been skipped
CI Twenty Apps / ${{ matrix.app.name }} (push) Has been skipped
CI Emails / ci-emails-status-check (push) Successful in 2s
CI Example App Hello World / ci-example-app-hello-world-status-check (push) Successful in 2s
CI Example App Postcard / ci-example-app-postcard-status-check (push) Successful in 3s
CI UI / ui-sb-test (push) Has been skipped
CI Utils Test / ci-utils-test-status-check (push) Successful in 2s
CI Twenty Apps / ci-twenty-apps-status-check (push) Successful in 2s
CI UI / ci-ui-status-check (push) Successful in 2s
CI Desktop / desktop (push) Failing after 1m8s
Push docs to Crowdin / Push documentation to Crowdin (push) Failing after 55s
Push translations to Crowdin / Extract and upload translations (push) Failing after 5s
Push website translations to Crowdin / Extract and upload website translations (push) Failing after 5s
CI Create App / create-app-test (test) (push) Has been skipped
CI Create App / create-app-test (typecheck) (push) Has been skipped
CI Docs / docs-lint (push) Has been skipped
CI Example App Hello World / example-app-hello-world (push) Has been skipped
CI Front / front-task (lingui:extract) (push) Has been skipped
CI Front / front-task (test) (push) Has been skipped
CI Front / front-task (typecheck) (push) Has been skipped
CI UI / ui-sb-build (push) Failing after 1m32s
CI Twenty Apps / ${{ matrix.app.name }} (${{ matrix.server-source }}) (push) Has been skipped
CI Agent Skills / agent-skills-publish (push) Has been skipped
CI Create App / ci-create-app-status-check (push) Successful in 2s
CI Agent Skills / agent-skills-remote-source (push) Has been skipped
CI Create App E2E minimal / create-app-e2e-minimal (push) Has been cancelled
CI E2E Main / e2e-test (push) Has been cancelled
CI Front / front-sb-build (push) Has been cancelled
CI Front / front-build (push) Has been cancelled
CI Create App E2E minimal / ci-create-app-e2e-minimal-status-check (push) Has been cancelled
CI E2E Main / ci-e2e-main-status-check (push) Has been cancelled
CI E2E Main / notify-main-ci-failure (push) Has been cancelled
CI E2E Main / qa-scout-comment (push) Has been cancelled
CI Front / front-sb-test (push) Has been cancelled
CI Front / front-sb-screenshots (push) Has been cancelled
CI Front / ci-front-status-check (push) Has been cancelled
DPA Sub-Processor Sync / sync-subprocessors (push) Failing after 3s
AI Catalog Sync / sync-catalog (push) Failing after 2s
476 lines
14 KiB
TypeScript
476 lines
14 KiB
TypeScript
import { updateFeatureFlag } from 'test/integration/metadata/suites/utils/update-feature-flag.util';
|
|
import { makeRestAPIRequest } from 'test/integration/rest/utils/make-rest-api-request.util';
|
|
import {
|
|
cleanupTestField,
|
|
cleanupTestObject,
|
|
createTestFieldViaGraphql,
|
|
createTestObjectViaGraphql,
|
|
extractMetadataItemPayload,
|
|
extractMetadataListPayload,
|
|
NON_EXISTENT_UUID,
|
|
uniqueSuffix,
|
|
} from 'test/integration/rest/utils/metadata-rest-api.util';
|
|
import {
|
|
assertRestApiErrorNotFoundResponse,
|
|
assertRestApiErrorResponse,
|
|
assertRestApiSuccessfulResponse,
|
|
} from 'test/integration/rest/utils/rest-test-assertions.util';
|
|
import { FeatureFlagKey, FieldMetadataType } from 'twenty-shared/types';
|
|
|
|
type FieldShape = {
|
|
id: string;
|
|
name?: string;
|
|
label?: string;
|
|
objectMetadataId?: string;
|
|
};
|
|
|
|
describe.each([
|
|
['new format', true],
|
|
['legacy format', false],
|
|
] as const)('Field Metadata REST API (%s)', (_shapeLabel, isNewFormat) => {
|
|
let parentObjectId: string;
|
|
|
|
beforeAll(async () => {
|
|
await updateFeatureFlag({
|
|
featureFlag: FeatureFlagKey.IS_REST_METADATA_API_NEW_FORMAT_DIRECT,
|
|
value: isNewFormat,
|
|
expectToFail: false,
|
|
});
|
|
|
|
const { id } = await createTestObjectViaGraphql();
|
|
|
|
parentObjectId = id;
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await cleanupTestObject(parentObjectId);
|
|
await updateFeatureFlag({
|
|
featureFlag: FeatureFlagKey.IS_REST_METADATA_API_NEW_FORMAT_DIRECT,
|
|
value: false,
|
|
expectToFail: false,
|
|
});
|
|
});
|
|
|
|
describe('GET /metadata/fields', () => {
|
|
const seededIds: string[] = [];
|
|
|
|
beforeAll(async () => {
|
|
for (let i = 0; i < 3; i++) {
|
|
const { id } = await createTestFieldViaGraphql(parentObjectId);
|
|
|
|
seededIds.push(id);
|
|
}
|
|
});
|
|
|
|
afterAll(async () => {
|
|
// Field deletion runs schema migrations that acquire exclusive locks.
|
|
// Keep teardown serial so the suite cannot deadlock against itself.
|
|
for (const id of seededIds) {
|
|
await cleanupTestField(id);
|
|
}
|
|
seededIds.length = 0;
|
|
});
|
|
|
|
it('returns the expected envelope shape', async () => {
|
|
const response = await makeRestAPIRequest({
|
|
method: 'get',
|
|
path: '/metadata/fields',
|
|
bearer: APPLE_JANE_ADMIN_ACCESS_TOKEN,
|
|
});
|
|
|
|
assertRestApiSuccessfulResponse(response);
|
|
|
|
if (isNewFormat) {
|
|
expect(response.body).not.toHaveProperty('data.fields');
|
|
expect(Array.isArray(response.body.data)).toBe(true);
|
|
} else {
|
|
expect(Array.isArray(response.body.data.fields)).toBe(true);
|
|
}
|
|
|
|
expect(response.body).toHaveProperty('pageInfo.hasNextPage');
|
|
expect(response.body).toHaveProperty('pageInfo.hasPreviousPage');
|
|
expect(response.body).toHaveProperty('pageInfo.startCursor');
|
|
expect(response.body).toHaveProperty('pageInfo.endCursor');
|
|
expect(typeof response.body.totalCount).toBe('number');
|
|
expect(response.body.totalCount).toBeGreaterThanOrEqual(seededIds.length);
|
|
});
|
|
|
|
it('respects limit and surfaces hasNextPage', async () => {
|
|
const response = await makeRestAPIRequest({
|
|
method: 'get',
|
|
path: '/metadata/fields?limit=1',
|
|
bearer: APPLE_JANE_ADMIN_ACCESS_TOKEN,
|
|
});
|
|
|
|
assertRestApiSuccessfulResponse(response);
|
|
const { items, pageInfo } = extractMetadataListPayload<FieldShape>(
|
|
response.body,
|
|
'fields',
|
|
);
|
|
|
|
expect(items.length).toBe(1);
|
|
expect(pageInfo.hasNextPage).toBe(true);
|
|
expect(pageInfo.hasPreviousPage).toBe(false);
|
|
});
|
|
|
|
it('paginates forward with starting_after without overlap', async () => {
|
|
const firstPage = await makeRestAPIRequest({
|
|
method: 'get',
|
|
path: '/metadata/fields?limit=2',
|
|
bearer: APPLE_JANE_ADMIN_ACCESS_TOKEN,
|
|
});
|
|
|
|
const firstPayload = extractMetadataListPayload<FieldShape>(
|
|
firstPage.body,
|
|
'fields',
|
|
);
|
|
|
|
const secondPage = await makeRestAPIRequest({
|
|
method: 'get',
|
|
path: `/metadata/fields?limit=2&starting_after=${firstPayload.pageInfo.endCursor}`,
|
|
bearer: APPLE_JANE_ADMIN_ACCESS_TOKEN,
|
|
});
|
|
|
|
assertRestApiSuccessfulResponse(secondPage);
|
|
const secondPayload = extractMetadataListPayload<FieldShape>(
|
|
secondPage.body,
|
|
'fields',
|
|
);
|
|
|
|
const firstIds = firstPayload.items.map((f) => f.id);
|
|
const secondIds = secondPayload.items.map((f) => f.id);
|
|
|
|
expect(firstIds.some((id) => secondIds.includes(id))).toBe(false);
|
|
});
|
|
|
|
it('paginates backward with ending_before', async () => {
|
|
const firstPage = await makeRestAPIRequest({
|
|
method: 'get',
|
|
path: '/metadata/fields?limit=2',
|
|
bearer: APPLE_JANE_ADMIN_ACCESS_TOKEN,
|
|
});
|
|
|
|
const firstPayload = extractMetadataListPayload<FieldShape>(
|
|
firstPage.body,
|
|
'fields',
|
|
);
|
|
|
|
const secondPage = await makeRestAPIRequest({
|
|
method: 'get',
|
|
path: `/metadata/fields?limit=2&starting_after=${firstPayload.pageInfo.endCursor}`,
|
|
bearer: APPLE_JANE_ADMIN_ACCESS_TOKEN,
|
|
});
|
|
|
|
const secondPayload = extractMetadataListPayload<FieldShape>(
|
|
secondPage.body,
|
|
'fields',
|
|
);
|
|
|
|
const backPage = await makeRestAPIRequest({
|
|
method: 'get',
|
|
path: `/metadata/fields?limit=2&ending_before=${secondPayload.pageInfo.startCursor}`,
|
|
bearer: APPLE_JANE_ADMIN_ACCESS_TOKEN,
|
|
});
|
|
|
|
assertRestApiSuccessfulResponse(backPage);
|
|
const backPayload = extractMetadataListPayload<FieldShape>(
|
|
backPage.body,
|
|
'fields',
|
|
);
|
|
|
|
const firstIds = firstPayload.items.map((f) => f.id);
|
|
const backIds = backPayload.items.map((f) => f.id);
|
|
|
|
expect(backIds.every((id) => firstIds.includes(id))).toBe(true);
|
|
expect(backPayload.pageInfo.hasNextPage).toBe(true);
|
|
});
|
|
|
|
it('rejects combining starting_after and ending_before with 400', async () => {
|
|
const response = await makeRestAPIRequest({
|
|
method: 'get',
|
|
path: `/metadata/fields?starting_after=${NON_EXISTENT_UUID}&ending_before=${NON_EXISTENT_UUID}`,
|
|
bearer: APPLE_JANE_ADMIN_ACCESS_TOKEN,
|
|
});
|
|
|
|
assertRestApiErrorResponse(response, 400);
|
|
});
|
|
|
|
it('rejects malformed cursor IDs with 400', async () => {
|
|
const response = await makeRestAPIRequest({
|
|
method: 'get',
|
|
path: '/metadata/fields?starting_after=not-a-uuid',
|
|
bearer: APPLE_JANE_ADMIN_ACCESS_TOKEN,
|
|
});
|
|
|
|
assertRestApiErrorResponse(response, 400);
|
|
});
|
|
|
|
it('keeps totalCount stable across pages', async () => {
|
|
const firstPage = await makeRestAPIRequest({
|
|
method: 'get',
|
|
path: '/metadata/fields?limit=2',
|
|
bearer: APPLE_JANE_ADMIN_ACCESS_TOKEN,
|
|
});
|
|
|
|
const firstPayload = extractMetadataListPayload<FieldShape>(
|
|
firstPage.body,
|
|
'fields',
|
|
);
|
|
|
|
const secondPage = await makeRestAPIRequest({
|
|
method: 'get',
|
|
path: `/metadata/fields?limit=2&starting_after=${firstPayload.pageInfo.endCursor}`,
|
|
bearer: APPLE_JANE_ADMIN_ACCESS_TOKEN,
|
|
});
|
|
|
|
expect(firstPage.body.totalCount).toBe(secondPage.body.totalCount);
|
|
});
|
|
});
|
|
|
|
describe('GET /metadata/fields/:id', () => {
|
|
let testFieldId: string;
|
|
|
|
beforeAll(async () => {
|
|
const { id } = await createTestFieldViaGraphql(parentObjectId);
|
|
|
|
testFieldId = id;
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await cleanupTestField(testFieldId);
|
|
});
|
|
|
|
it('returns the field with expected envelope', async () => {
|
|
const response = await makeRestAPIRequest({
|
|
method: 'get',
|
|
path: `/metadata/fields/${testFieldId}`,
|
|
bearer: APPLE_JANE_ADMIN_ACCESS_TOKEN,
|
|
});
|
|
|
|
assertRestApiSuccessfulResponse(response);
|
|
const field = extractMetadataItemPayload<FieldShape>(
|
|
response.body,
|
|
'field',
|
|
);
|
|
|
|
expect(field.id).toBe(testFieldId);
|
|
expect(field.objectMetadataId).toBe(parentObjectId);
|
|
if (isNewFormat) {
|
|
expect(response.body).not.toHaveProperty('data.field');
|
|
} else {
|
|
expect(response.body).toHaveProperty('data.field');
|
|
}
|
|
});
|
|
|
|
it('returns 400 on a malformed UUID', async () => {
|
|
const response = await makeRestAPIRequest({
|
|
method: 'get',
|
|
path: `/metadata/fields/not-a-uuid`,
|
|
bearer: APPLE_JANE_ADMIN_ACCESS_TOKEN,
|
|
});
|
|
|
|
assertRestApiErrorResponse(response, 400);
|
|
});
|
|
|
|
it('returns 404 for an unknown id', async () => {
|
|
const response = await makeRestAPIRequest({
|
|
method: 'get',
|
|
path: `/metadata/fields/${NON_EXISTENT_UUID}`,
|
|
bearer: APPLE_JANE_ADMIN_ACCESS_TOKEN,
|
|
});
|
|
|
|
assertRestApiErrorNotFoundResponse(response);
|
|
});
|
|
});
|
|
|
|
describe('POST /metadata/fields', () => {
|
|
it('creates a field and returns 201', async () => {
|
|
const suffix = uniqueSuffix();
|
|
const input = {
|
|
objectMetadataId: parentObjectId,
|
|
type: FieldMetadataType.TEXT,
|
|
name: `postField${suffix}`,
|
|
label: `Post Field ${suffix}`,
|
|
isLabelSyncedWithName: false,
|
|
};
|
|
|
|
const response = await makeRestAPIRequest({
|
|
method: 'post',
|
|
path: '/metadata/fields',
|
|
body: input,
|
|
bearer: APPLE_JANE_ADMIN_ACCESS_TOKEN,
|
|
});
|
|
|
|
const field = extractMetadataItemPayload<FieldShape>(
|
|
response.body,
|
|
'createOneField',
|
|
);
|
|
|
|
try {
|
|
assertRestApiSuccessfulResponse(response, 201);
|
|
expect(field.id).toBeDefined();
|
|
expect(field.name).toBe(input.name);
|
|
expect(field.objectMetadataId).toBe(parentObjectId);
|
|
if (isNewFormat) {
|
|
expect(response.body).not.toHaveProperty('data.createOneField');
|
|
} else {
|
|
expect(response.body).toHaveProperty('data.createOneField');
|
|
}
|
|
} finally {
|
|
if (field.id) {
|
|
await cleanupTestField(field.id);
|
|
}
|
|
}
|
|
});
|
|
|
|
it('returns 400 on duplicate name within an object', async () => {
|
|
const { id, input } = await createTestFieldViaGraphql(parentObjectId);
|
|
|
|
try {
|
|
const response = await makeRestAPIRequest({
|
|
method: 'post',
|
|
path: '/metadata/fields',
|
|
body: input,
|
|
bearer: APPLE_JANE_ADMIN_ACCESS_TOKEN,
|
|
});
|
|
|
|
assertRestApiErrorResponse(response, 400);
|
|
} finally {
|
|
await cleanupTestField(id);
|
|
}
|
|
});
|
|
|
|
it('returns 400 on invalid input', async () => {
|
|
const response = await makeRestAPIRequest({
|
|
method: 'post',
|
|
path: '/metadata/fields',
|
|
body: { name: '' },
|
|
bearer: APPLE_JANE_ADMIN_ACCESS_TOKEN,
|
|
});
|
|
|
|
assertRestApiErrorResponse(response, 400);
|
|
});
|
|
});
|
|
|
|
describe('PATCH /metadata/fields/:id', () => {
|
|
let testFieldId: string;
|
|
|
|
beforeEach(async () => {
|
|
const { id } = await createTestFieldViaGraphql(parentObjectId);
|
|
|
|
testFieldId = id;
|
|
});
|
|
|
|
afterEach(async () => {
|
|
await cleanupTestField(testFieldId);
|
|
});
|
|
|
|
it('updates and returns the field', async () => {
|
|
const newLabel = `Updated Field ${uniqueSuffix()}`;
|
|
|
|
const response = await makeRestAPIRequest({
|
|
method: 'patch',
|
|
path: `/metadata/fields/${testFieldId}`,
|
|
body: { label: newLabel },
|
|
bearer: APPLE_JANE_ADMIN_ACCESS_TOKEN,
|
|
});
|
|
|
|
assertRestApiSuccessfulResponse(response);
|
|
const field = extractMetadataItemPayload<FieldShape>(
|
|
response.body,
|
|
'updateOneField',
|
|
);
|
|
|
|
expect(field.id).toBe(testFieldId);
|
|
expect(field.label).toBe(newLabel);
|
|
if (isNewFormat) {
|
|
expect(response.body).not.toHaveProperty('data.updateOneField');
|
|
} else {
|
|
expect(response.body).toHaveProperty('data.updateOneField');
|
|
}
|
|
});
|
|
|
|
it('returns 400 for an unknown id', async () => {
|
|
const response = await makeRestAPIRequest({
|
|
method: 'patch',
|
|
path: `/metadata/fields/${NON_EXISTENT_UUID}`,
|
|
body: { label: 'Whatever' },
|
|
bearer: APPLE_JANE_ADMIN_ACCESS_TOKEN,
|
|
});
|
|
|
|
assertRestApiErrorResponse(response, 400);
|
|
});
|
|
});
|
|
|
|
describe('PUT /metadata/fields/:id', () => {
|
|
it('behaves equivalently to PATCH', async () => {
|
|
const { id } = await createTestFieldViaGraphql(parentObjectId);
|
|
|
|
try {
|
|
const newLabel = `PutField ${uniqueSuffix()}`;
|
|
|
|
const response = await makeRestAPIRequest({
|
|
method: 'put',
|
|
path: `/metadata/fields/${id}`,
|
|
body: { label: newLabel },
|
|
bearer: APPLE_JANE_ADMIN_ACCESS_TOKEN,
|
|
});
|
|
|
|
assertRestApiSuccessfulResponse(response);
|
|
const field = extractMetadataItemPayload<FieldShape>(
|
|
response.body,
|
|
'updateOneField',
|
|
);
|
|
|
|
expect(field.id).toBe(id);
|
|
expect(field.label).toBe(newLabel);
|
|
} finally {
|
|
await cleanupTestField(id);
|
|
}
|
|
});
|
|
});
|
|
|
|
describe('DELETE /metadata/fields/:id', () => {
|
|
it('deletes the field and returns the deleted resource', async () => {
|
|
const { id } = await createTestFieldViaGraphql(parentObjectId);
|
|
|
|
const response = await makeRestAPIRequest({
|
|
method: 'delete',
|
|
path: `/metadata/fields/${id}`,
|
|
bearer: APPLE_JANE_ADMIN_ACCESS_TOKEN,
|
|
});
|
|
|
|
assertRestApiSuccessfulResponse(response);
|
|
const deleted = extractMetadataItemPayload<FieldShape>(
|
|
response.body,
|
|
'deleteOneField',
|
|
);
|
|
|
|
expect(deleted.id).toBe(id);
|
|
if (isNewFormat) {
|
|
expect(response.body).not.toHaveProperty('data.deleteOneField');
|
|
} else {
|
|
expect(response.body).toHaveProperty('data.deleteOneField');
|
|
}
|
|
|
|
const getResponse = await makeRestAPIRequest({
|
|
method: 'get',
|
|
path: `/metadata/fields/${id}`,
|
|
bearer: APPLE_JANE_ADMIN_ACCESS_TOKEN,
|
|
});
|
|
|
|
assertRestApiErrorNotFoundResponse(getResponse);
|
|
});
|
|
|
|
it('returns 404 for an unknown id', async () => {
|
|
const response = await makeRestAPIRequest({
|
|
method: 'delete',
|
|
path: `/metadata/fields/${NON_EXISTENT_UUID}`,
|
|
bearer: APPLE_JANE_ADMIN_ACCESS_TOKEN,
|
|
});
|
|
|
|
assertRestApiErrorNotFoundResponse(response);
|
|
});
|
|
});
|
|
});
|