Files
upstream-twenty/packages/twenty-server/test/integration/metadata/suites/application/failing-application-file-upload-registration-ownership.integration-spec.ts
upstream-mirror e7da97194a
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 3s
snapshot of https://github.com/twentyhq/twenty.git (main, 2026-09-17; blobs > 131072B omitted)
2026-09-17 23:28:13 +05:00

331 lines
10 KiB
TypeScript

import { readFileSync } from 'fs';
import { join } from 'path';
import { expectOneNotInternalServerErrorSnapshot } from 'test/integration/graphql/utils/expect-one-not-internal-server-error-snapshot.util';
import { cleanupApplicationAndAppRegistration } from 'test/integration/metadata/suites/application/utils/cleanup-application-and-app-registration.util';
import { completeApplicationFileUploads } from 'test/integration/metadata/suites/application/utils/complete-application-file-uploads.util';
import { createApplicationFileUploads } from 'test/integration/metadata/suites/application/utils/create-application-file-uploads.util';
import { putApplicationFileUploadTarget } from 'test/integration/metadata/suites/application/utils/put-application-file-upload-target.util';
import { setupApplicationForSync } from 'test/integration/metadata/suites/application/utils/setup-application-for-sync.util';
import { uploadApplicationFile } from 'test/integration/metadata/suites/application/utils/upload-application-file.util';
import {
type EachTestingContext,
eachTestingContextFilter,
} from 'twenty-shared/testing';
import { v4 as uuidv4 } from 'uuid';
import {
SEED_APPLE_WORKSPACE_ID,
SEED_YCOMBINATOR_WORKSPACE_ID,
} from 'src/engine/workspace-manager/dev-seeder/core/constants/seeder-workspaces.constant';
import { TWENTY_STANDARD_APPLICATION } from 'src/engine/workspace-manager/twenty-standard-application/constants/twenty-standard-applications';
const TEST_APP_UID = uuidv4();
const STORAGE_LOCAL_PATH = join(process.cwd(), '.local-storage');
const HANDLER_PATH = 'handler.mjs';
const OWNER_HANDLER = 'export const main = () => "owner";';
const REPLACEMENT_HANDLER = 'export const main = () => process.env;';
type RegistrationOwnership = 'foreign' | 'unclaimed' | 'none';
type TestContext = {
registrationOwnership: RegistrationOwnership;
unlinkApplicationRegistration?: boolean;
};
const FAILING_TEST_CASES: EachTestingContext<TestContext>[] = [
{
title: 'when another workspace owns the application registration',
context: { registrationOwnership: 'foreign' },
},
{
title: 'when no workspace has claimed the application registration',
context: { registrationOwnership: 'unclaimed' },
},
{
title: 'when the application has no registration',
context: { registrationOwnership: 'none' },
},
{
title:
'when the application has no registration link and another workspace owns the registration',
context: {
registrationOwnership: 'foreign',
unlinkApplicationRegistration: true,
},
},
];
const normalizeMessage = (message: string) =>
message.replace(
new RegExp(TEST_APP_UID, 'g'),
'<applicationUniversalIdentifier>',
);
const getTargetApplicationUniversalIdentifier = (
registrationOwnership: RegistrationOwnership,
) =>
registrationOwnership === 'none'
? TWENTY_STANDARD_APPLICATION.universalIdentifier
: TEST_APP_UID;
const setTestRegistrationOwnerWorkspaceId = async (
ownerWorkspaceId: string | null,
) => {
await globalThis.testDataSource.query(
`UPDATE core."applicationRegistration" SET "workspaceId" = $1
WHERE "universalIdentifier" = $2`,
[ownerWorkspaceId, TEST_APP_UID],
);
};
const unlinkTestApplicationRegistration = async () => {
await globalThis.testDataSource.query(
`UPDATE core."application" SET "applicationRegistrationId" = NULL
WHERE "universalIdentifier" = $1 AND "workspaceId" = $2`,
[TEST_APP_UID, SEED_APPLE_WORKSPACE_ID],
);
};
const relinkTestApplicationRegistration = async () => {
await globalThis.testDataSource.query(
`UPDATE core."application" SET "applicationRegistrationId" = (
SELECT id FROM core."applicationRegistration" WHERE "universalIdentifier" = $1
)
WHERE "universalIdentifier" = $1 AND "workspaceId" = $2`,
[TEST_APP_UID, SEED_APPLE_WORKSPACE_ID],
);
};
const applyRegistrationLink = async ({
unlinkApplicationRegistration,
}: TestContext) => {
if (unlinkApplicationRegistration === true) {
await unlinkTestApplicationRegistration();
}
};
const applyRegistrationOwnership = async (
registrationOwnership: RegistrationOwnership,
) => {
if (registrationOwnership === 'foreign') {
await setTestRegistrationOwnerWorkspaceId(SEED_YCOMBINATOR_WORKSPACE_ID);
}
if (registrationOwnership === 'unclaimed') {
await setTestRegistrationOwnerWorkspaceId(null);
}
};
const readStoredHandler = (applicationUniversalIdentifier: string) => {
try {
return readFileSync(
join(
STORAGE_LOCAL_PATH,
SEED_APPLE_WORKSPACE_ID,
applicationUniversalIdentifier,
'built-logic-function',
HANDLER_PATH,
),
'utf-8',
);
} catch {
return null;
}
};
const countApplicationFiles = async (
applicationUniversalIdentifier: string,
) => {
const [{ count }] = await globalThis.testDataSource.query(
`SELECT count(*)::int AS count FROM core."file" WHERE "applicationId" IN (
SELECT id FROM core."application"
WHERE "universalIdentifier" = $1 AND "workspaceId" = $2
)`,
[applicationUniversalIdentifier, SEED_APPLE_WORKSPACE_ID],
);
return count as number;
};
const getFileStatus = async (fileId: string) => {
const [file] = await globalThis.testDataSource.query(
`SELECT status FROM core."file" WHERE id = $1`,
[fileId],
);
return file?.status as string | undefined;
};
const reservePendingHandlerUpload = async () => {
const body = Buffer.from(REPLACEMENT_HANDLER);
const { data } = await createApplicationFileUploads({
applicationUniversalIdentifier: TEST_APP_UID,
files: [
{
fileFolder: 'BuiltLogicFunction',
filePath: HANDLER_PATH,
size: body.length,
},
],
});
const [uploadTarget] = data.createApplicationFileUploads.targets;
const response = await putApplicationFileUploadTarget({
uploadTarget,
body,
});
expect(response.status).toBe(204);
return uploadTarget.fileId;
};
describe('Application file endpoints should fail', () => {
beforeAll(async () => {
await setupApplicationForSync({
applicationUniversalIdentifier: TEST_APP_UID,
name: 'Test Application File Upload Ownership App',
description: 'App for verifying registration ownership on file uploads',
sourcePath: 'test-application-file-upload-ownership',
});
jest.useRealTimers();
await uploadApplicationFile({
applicationUniversalIdentifier: TEST_APP_UID,
fileFolder: 'BuiltLogicFunction',
filePath: HANDLER_PATH,
fileBuffer: Buffer.from(OWNER_HANDLER),
filename: HANDLER_PATH,
contentType: 'application/javascript',
});
jest.useFakeTimers();
}, 60000);
beforeEach(() => {
jest.useRealTimers();
});
afterEach(async () => {
await setTestRegistrationOwnerWorkspaceId(SEED_APPLE_WORKSPACE_ID);
await relinkTestApplicationRegistration();
jest.useFakeTimers();
});
afterAll(async () => {
await cleanupApplicationAndAppRegistration({
applicationUniversalIdentifier: TEST_APP_UID,
});
});
describe('uploadApplicationFile', () => {
it.each(eachTestingContextFilter(FAILING_TEST_CASES))(
'$title',
async ({ context }) => {
const applicationUniversalIdentifier =
getTargetApplicationUniversalIdentifier(
context.registrationOwnership,
);
const storedHandlerBefore = readStoredHandler(
applicationUniversalIdentifier,
);
const filesBefore = await countApplicationFiles(
applicationUniversalIdentifier,
);
await applyRegistrationLink(context);
await applyRegistrationOwnership(context.registrationOwnership);
const { errors } = await uploadApplicationFile({
applicationUniversalIdentifier,
fileFolder: 'BuiltLogicFunction',
filePath: HANDLER_PATH,
fileBuffer: Buffer.from(REPLACEMENT_HANDLER),
filename: HANDLER_PATH,
contentType: 'application/javascript',
expectToFail: true,
});
expectOneNotInternalServerErrorSnapshot({ errors, normalizeMessage });
expect(readStoredHandler(applicationUniversalIdentifier)).toBe(
storedHandlerBefore,
);
expect(
await countApplicationFiles(applicationUniversalIdentifier),
).toBe(filesBefore);
},
30000,
);
});
describe('createApplicationFileUploads', () => {
it.each(eachTestingContextFilter(FAILING_TEST_CASES))(
'$title',
async ({ context }) => {
const applicationUniversalIdentifier =
getTargetApplicationUniversalIdentifier(
context.registrationOwnership,
);
const filesBefore = await countApplicationFiles(
applicationUniversalIdentifier,
);
await applyRegistrationLink(context);
await applyRegistrationOwnership(context.registrationOwnership);
const { errors } = await createApplicationFileUploads({
applicationUniversalIdentifier,
files: [
{
fileFolder: 'BuiltLogicFunction',
filePath: HANDLER_PATH,
size: 10,
},
{ fileFolder: 'Dependencies', filePath: 'package.json', size: 10 },
],
expectToFail: true,
});
expectOneNotInternalServerErrorSnapshot({ errors, normalizeMessage });
expect(
await countApplicationFiles(applicationUniversalIdentifier),
).toBe(filesBefore);
},
30000,
);
});
describe('completeApplicationFileUploads', () => {
it.each(eachTestingContextFilter(FAILING_TEST_CASES))(
'$title',
async ({ context }) => {
await applyRegistrationLink(context);
// Reserving while the row is unlinked also proves the owner is still
// allowed to upload when the registration only matches by identifier.
const pendingFileId = await reservePendingHandlerUpload();
await applyRegistrationOwnership(context.registrationOwnership);
const { errors } = await completeApplicationFileUploads({
applicationUniversalIdentifier:
getTargetApplicationUniversalIdentifier(
context.registrationOwnership,
),
fileIds: [pendingFileId],
expectToFail: true,
});
expectOneNotInternalServerErrorSnapshot({ errors, normalizeMessage });
expect(await getFileStatus(pendingFileId)).toBe('PENDING');
},
30000,
);
});
});