Skip to content

The open alternative to Salesforce, designed for AI.

test(apps): cover owner-workspace scope on application registration variables (#26208)

Paul Rastoincc4de38fb862
diff --git a/packages/twenty-server/test/integration/graphql/suites/admin-panel/__snapshots__/failing-admin-panel-application-registration-variables.integration-spec.ts.snap b/packages/twenty-server/test/integration/graphql/suites/admin-panel/__snapshots__/failing-admin-panel-application-registration-variables.integration-spec.ts.snap
new file mode 100644
index 00000000000..e19e854f2b0
--- /dev/null
+++ b/packages/twenty-server/test/integration/graphql/suites/admin-panel/__snapshots__/failing-admin-panel-application-registration-variables.integration-spec.ts.snap
@@ -0,0 +1,25 @@
+// Jest Snapshot v1, https://goo.gl/fbAQLP
+
+exports[`Admin panel application registration variables should fail without the SECURITY permission flag should refuse to read them 1`] = `
+{
+ "extensions": {
+ "code": "FORBIDDEN",
+ "subCode": "PERMISSION_DENIED",
+ "userFriendlyMessage": "User does not have permission.",
+ },
+ "message": "Entity performing the request does not have permission",
+ "name": "ForbiddenError",
+}
+`;
+
+exports[`Admin panel application registration variables should fail without the SECURITY permission flag should refuse to write them and leave the stored value untouched 1`] = `
+{
+ "extensions": {
+ "code": "FORBIDDEN",
+ "subCode": "PERMISSION_DENIED",
+ "userFriendlyMessage": "User does not have permission.",
+ },
+ "message": "Entity performing the request does not have permission",
+ "name": "ForbiddenError",
+}
+`;
diff --git a/packages/twenty-server/test/integration/graphql/suites/admin-panel/failing-admin-panel-application-registration-variables.integration-spec.ts b/packages/twenty-server/test/integration/graphql/suites/admin-panel/failing-admin-panel-application-registration-variables.integration-spec.ts
new file mode 100644
index 00000000000..b8c3e62f555
--- /dev/null
+++ b/packages/twenty-server/test/integration/graphql/suites/admin-panel/failing-admin-panel-application-registration-variables.integration-spec.ts
@@ -0,0 +1,73 @@
+import {
+ findAdminApplicationRegistrationVariables,
+ updateAdminApplicationRegistrationVariable,
+} from 'test/integration/graphql/suites/admin-panel/utils/admin-application-registration-variable-api.util';
+import { expectOneNotInternalServerErrorSnapshot } from 'test/integration/graphql/utils/expect-one-not-internal-server-error-snapshot.util';
+import { isDefined } from 'twenty-shared/utils';
+
+import {
+ deleteSeededRegistration,
+ readEncryptedValue,
+ seedRegistrationOwnedByAnotherWorkspace,
+ type SeededRegistration,
+} from 'test/integration/graphql/suites/admin-panel/utils/seed-registration-with-variables.util';
+
+/* global APPLE_PHIL_GUEST_ACCESS_TOKEN */
+
+describe('Admin panel application registration variables should fail without the SECURITY permission flag', () => {
+ let registration: SeededRegistration;
+
+ const normalizeMessage = (message: string) =>
+ message
+ .replace(
+ registration.applicationRegistrationId,
+ '<applicationRegistrationId>',
+ )
+ .replace(
+ registration.secretVariableId,
+ '<applicationRegistrationVariableId>',
+ );
+
+ beforeAll(async () => {
+ registration = await seedRegistrationOwnedByAnotherWorkspace(
+ 'failing-admin-panel-variables-registration',
+ );
+ });
+
+ afterAll(async () => {
+ // Guarded so a setup failure surfaces as itself rather than as a
+ // TypeError raised while tearing down a fixture that never existed.
+ if (isDefined(registration)) {
+ await deleteSeededRegistration(registration.applicationRegistrationId);
+ }
+ });
+
+ it('should refuse to read them', async () => {
+ const { data, errors } = await findAdminApplicationRegistrationVariables({
+ applicationRegistrationId: registration.applicationRegistrationId,
+ token: APPLE_PHIL_GUEST_ACCESS_TOKEN,
+ });
+
+ expectOneNotInternalServerErrorSnapshot({ errors, normalizeMessage });
+ expect(data?.findAdminApplicationRegistrationVariables).toBeFalsy();
+ });
+
+ it('should refuse to write them and leave the stored value untouched', async () => {
+ const encryptedValueBeforeAttempt = await readEncryptedValue(
+ registration.secretVariableId,
+ );
+
+ const { data, errors } = await updateAdminApplicationRegistrationVariable({
+ id: registration.secretVariableId,
+ value: 'value-from-a-caller-without-the-flag',
+ token: APPLE_PHIL_GUEST_ACCESS_TOKEN,
+ });
+
+ expectOneNotInternalServerErrorSnapshot({ errors, normalizeMessage });
+ expect(data?.updateAdminApplicationRegistrationVariable).toBeFalsy();
+
+ expect(await readEncryptedValue(registration.secretVariableId)).toBe(
+ encryptedValueBeforeAttempt,
+ );
+ });
+});
diff --git a/packages/twenty-server/test/integration/graphql/suites/admin-panel/successful-admin-panel-application-registration-variables.integration-spec.ts b/packages/twenty-server/test/integration/graphql/suites/admin-panel/successful-admin-panel-application-registration-variables.integration-spec.ts
new file mode 100644
index 00000000000..9b525637808
--- /dev/null
+++ b/packages/twenty-server/test/integration/graphql/suites/admin-panel/successful-admin-panel-application-registration-variables.integration-spec.ts
@@ -0,0 +1,99 @@
+import {
+ findAdminApplicationRegistrationVariables,
+ updateAdminApplicationRegistrationVariable,
+} from 'test/integration/graphql/suites/admin-panel/utils/admin-application-registration-variable-api.util';
+import { isDefined } from 'twenty-shared/utils';
+
+import {
+ deleteSeededRegistration,
+ NON_SECRET_PLAINTEXT_VALUE,
+ OBFUSCATED_VALUE,
+ readEncryptedValue,
+ SECRET_PLAINTEXT_VALUE,
+ seedRegistrationOwnedByAnotherWorkspace,
+ type SeededRegistration,
+} from 'test/integration/graphql/suites/admin-panel/utils/seed-registration-with-variables.util';
+
+describe('Admin panel application registration variables should succeed', () => {
+ let registration: SeededRegistration;
+
+ beforeAll(async () => {
+ registration = await seedRegistrationOwnedByAnotherWorkspace(
+ 'successful-admin-panel-variables-registration',
+ );
+ });
+
+ afterAll(async () => {
+ // Guarded so a setup failure surfaces as itself rather than as a
+ // TypeError raised while tearing down a fixture that never existed.
+ if (isDefined(registration)) {
+ await deleteSeededRegistration(registration.applicationRegistrationId);
+ }
+ });
+
+ it('should read the variables of a registration owned by another workspace', async () => {
+ const { data, errors } = await findAdminApplicationRegistrationVariables({
+ applicationRegistrationId: registration.applicationRegistrationId,
+ });
+
+ expect(errors).toBeUndefined();
+
+ const variables = data.findAdminApplicationRegistrationVariables;
+
+ expect(variables).toHaveLength(2);
+ expect(variables.map((variable) => variable.key)).toEqual([
+ 'ADMIN_SCOPE_API_KEY',
+ 'ADMIN_SCOPE_PUBLIC_URL',
+ ]);
+ });
+
+ it('should obfuscate a secret value and never send its plaintext', async () => {
+ const { data, rawBody } = await findAdminApplicationRegistrationVariables({
+ applicationRegistrationId: registration.applicationRegistrationId,
+ });
+
+ const secretVariable = data.findAdminApplicationRegistrationVariables.find(
+ (variable) => variable.id === registration.secretVariableId,
+ );
+
+ expect(secretVariable?.isFilled).toBe(true);
+ expect(secretVariable?.value).toBe(OBFUSCATED_VALUE);
+ expect(JSON.stringify(rawBody)).not.toContain(SECRET_PLAINTEXT_VALUE);
+ });
+
+ it('should return a non-secret value in plaintext', async () => {
+ const { data } = await findAdminApplicationRegistrationVariables({
+ applicationRegistrationId: registration.applicationRegistrationId,
+ });
+
+ const nonSecretVariable =
+ data.findAdminApplicationRegistrationVariables.find(
+ (variable) => variable.id === registration.nonSecretVariableId,
+ );
+
+ expect(nonSecretVariable?.value).toBe(NON_SECRET_PLAINTEXT_VALUE);
+ });
+
+ it('should write a variable of a registration owned by another workspace', async () => {
+ const encryptedValueBeforeUpdate = await readEncryptedValue(
+ registration.secretVariableId,
+ );
+
+ const { data, errors } = await updateAdminApplicationRegistrationVariable({
+ id: registration.secretVariableId,
+ value: 'admin-panel-rotated-value',
+ });
+
+ expect(errors).toBeUndefined();
+
+ const variable = data.updateAdminApplicationRegistrationVariable;
+
+ expect(variable.id).toBe(registration.secretVariableId);
+ expect(variable.isFilled).toBe(true);
+ expect(variable.value).toBe(OBFUSCATED_VALUE);
+
+ expect(await readEncryptedValue(registration.secretVariableId)).not.toBe(
+ encryptedValueBeforeUpdate,
+ );
+ });
+});
diff --git a/packages/twenty-server/test/integration/graphql/suites/admin-panel/utils/admin-application-registration-variable-api.util.ts b/packages/twenty-server/test/integration/graphql/suites/admin-panel/utils/admin-application-registration-variable-api.util.ts
new file mode 100644
index 00000000000..8a3025d6b6f
--- /dev/null
+++ b/packages/twenty-server/test/integration/graphql/suites/admin-panel/utils/admin-application-registration-variable-api.util.ts
@@ -0,0 +1,67 @@
+import {
+ findAdminApplicationRegistrationVariablesQueryFactory,
+ updateAdminApplicationRegistrationVariableMutationFactory,
+} from 'test/integration/graphql/suites/admin-panel/utils/admin-application-registration-variable-query-factories.util';
+import { makeAdminPanelAPIRequest } from 'test/integration/twenty-config/utils/make-admin-panel-api-request.util';
+
+import { type BaseGraphQLError } from 'src/engine/core-modules/graphql/utils/graphql-errors.util';
+
+type AdminVariable = {
+ id: string;
+ key: string;
+ value: string | null;
+ isSecret: boolean;
+ isFilled: boolean;
+};
+
+type AdminResponse<TData> = Promise<{
+ data: TData;
+ errors: BaseGraphQLError[];
+ rawBody: unknown;
+}>;
+
+export const findAdminApplicationRegistrationVariables = async ({
+ applicationRegistrationId,
+ token,
+}: {
+ applicationRegistrationId: string;
+ token?: string;
+}): AdminResponse<{
+ findAdminApplicationRegistrationVariables: AdminVariable[];
+}> => {
+ const graphqlOperation =
+ findAdminApplicationRegistrationVariablesQueryFactory({
+ applicationRegistrationId,
+ });
+
+ const response = await makeAdminPanelAPIRequest(graphqlOperation, token);
+
+ return {
+ data: response.body.data,
+ errors: response.body.errors,
+ rawBody: response.body,
+ };
+};
+
+export const updateAdminApplicationRegistrationVariable = async ({
+ id,
+ value,
+ token,
+}: {
+ id: string;
+ value: string;
+ token?: string;
+}): AdminResponse<{
+ updateAdminApplicationRegistrationVariable: AdminVariable;
+}> => {
+ const graphqlOperation =
+ updateAdminApplicationRegistrationVariableMutationFactory({ id, value });
+
+ const response = await makeAdminPanelAPIRequest(graphqlOperation, token);
+
+ return {
+ data: response.body.data,
+ errors: response.body.errors,
+ rawBody: response.body,
+ };
+};
diff --git a/packages/twenty-server/test/integration/graphql/suites/admin-panel/utils/admin-application-registration-variable-query-factories.util.ts b/packages/twenty-server/test/integration/graphql/suites/admin-panel/utils/admin-application-registration-variable-query-factories.util.ts
new file mode 100644
index 00000000000..69fe432ec2b
--- /dev/null
+++ b/packages/twenty-server/test/integration/graphql/suites/admin-panel/utils/admin-application-registration-variable-query-factories.util.ts
@@ -0,0 +1,47 @@
+import gql from 'graphql-tag';
+
+const ADMIN_VARIABLE_GQL_FIELDS = `
+ id
+ key
+ value
+ isSecret
+ isFilled
+`;
+
+export const findAdminApplicationRegistrationVariablesQueryFactory = ({
+ applicationRegistrationId,
+}: {
+ applicationRegistrationId: string;
+}) => ({
+ query: gql`
+ query FindAdminApplicationRegistrationVariables(
+ $applicationRegistrationId: String!
+ ) {
+ findAdminApplicationRegistrationVariables(
+ applicationRegistrationId: $applicationRegistrationId
+ ) {
+ ${ADMIN_VARIABLE_GQL_FIELDS}
+ }
+ }
+ `,
+ variables: { applicationRegistrationId },
+});
+
+export const updateAdminApplicationRegistrationVariableMutationFactory = ({
+ id,
+ value,
+}: {
+ id: string;
+ value: string;
+}) => ({
+ query: gql`
+ mutation UpdateAdminApplicationRegistrationVariable(
+ $input: UpdateApplicationRegistrationVariableInput!
+ ) {
+ updateAdminApplicationRegistrationVariable(input: $input) {
+ ${ADMIN_VARIABLE_GQL_FIELDS}
+ }
+ }
+ `,
+ variables: { input: { id, update: { value } } },
+});
diff --git a/packages/twenty-server/test/integration/graphql/suites/admin-panel/utils/seed-registration-with-variables.util.ts b/packages/twenty-server/test/integration/graphql/suites/admin-panel/utils/seed-registration-with-variables.util.ts
new file mode 100644
index 00000000000..a3ee4ca8983
--- /dev/null
+++ b/packages/twenty-server/test/integration/graphql/suites/admin-panel/utils/seed-registration-with-variables.util.ts
@@ -0,0 +1,102 @@
+import { randomUUID } from 'crypto';
+
+import { createApplicationRegistrationVariable } from 'test/integration/metadata/suites/application-registration-variable/utils/application-registration-variable-api.util';
+
+import {
+ SEED_APPLE_WORKSPACE_ID,
+ SEED_YCOMBINATOR_WORKSPACE_ID,
+} from 'src/engine/workspace-manager/dev-seeder/core/constants/seeder-workspaces.constant';
+
+export type SeededRegistration = {
+ applicationRegistrationId: string;
+ secretVariableId: string;
+ nonSecretVariableId: string;
+};
+
+export const SECRET_PLAINTEXT_VALUE = 'admin-panel-scope-secret-value';
+export const NON_SECRET_PLAINTEXT_VALUE =
+ 'https://admin-panel-scope.example.com';
+export const OBFUSCATED_VALUE = '•••••••••••••';
+
+export const deleteSeededRegistration = async (
+ applicationRegistrationId: string,
+): Promise<void> => {
+ await globalThis.testDataSource.query(
+ `DELETE FROM core."applicationRegistration" WHERE id = $1`,
+ [applicationRegistrationId],
+ );
+};
+
+// Seeded as Apple-owned so the variables can be created through the
+// workspace-scoped API with properly encrypted values, then handed to
+// YCombinator: the admin panel is expected to reach any workspace.
+export const seedRegistrationOwnedByAnotherWorkspace = async (
+ name: string,
+): Promise<SeededRegistration> => {
+ const applicationRegistrationId = randomUUID();
+
+ await globalThis.testDataSource.query(
+ `INSERT INTO core."applicationRegistration"
+ (id, "universalIdentifier", name, "oAuthClientId",
+ "oAuthRedirectUris", "oAuthScopes", "workspaceId")
+ VALUES ($1, $2, $3, $4, $5, $6, $7)`,
+ [
+ applicationRegistrationId,
+ randomUUID(),
+ name,
+ randomUUID(),
+ [],
+ [],
+ SEED_APPLE_WORKSPACE_ID,
+ ],
+ );
+
+ // The row exists from here on, so anything that throws below has to take it
+ // back out: this database is shared by every suite in the run.
+ try {
+ const { data: secretData } = await createApplicationRegistrationVariable({
+ applicationRegistrationId,
+ key: 'ADMIN_SCOPE_API_KEY',
+ value: SECRET_PLAINTEXT_VALUE,
+ isSecret: true,
+ expectToFail: false,
+ });
+
+ const { data: nonSecretData } = await createApplicationRegistrationVariable(
+ {
+ applicationRegistrationId,
+ key: 'ADMIN_SCOPE_PUBLIC_URL',
+ value: NON_SECRET_PLAINTEXT_VALUE,
+ isSecret: false,
+ expectToFail: false,
+ },
+ );
+
+ await globalThis.testDataSource.query(
+ `UPDATE core."applicationRegistration" SET "workspaceId" = $2 WHERE id = $1`,
+ [applicationRegistrationId, SEED_YCOMBINATOR_WORKSPACE_ID],
+ );
+
+ return {
+ applicationRegistrationId,
+ secretVariableId: secretData.createApplicationRegistrationVariable.id,
+ nonSecretVariableId:
+ nonSecretData.createApplicationRegistrationVariable.id,
+ };
+ } catch (error) {
+ await deleteSeededRegistration(applicationRegistrationId);
+
+ throw error;
+ }
+};
+
+export const readEncryptedValue = async (
+ variableId: string,
+): Promise<string | null> => {
+ const [row] = await globalThis.testDataSource.query(
+ `SELECT "encryptedValue" FROM core."applicationRegistrationVariable" WHERE id = $1`,
+ [variableId],
+ );
+
+ return row?.encryptedValue ?? null;
+};
diff --git a/packages/twenty-server/test/integration/metadata/suites/application-registration-variable/__snapshots__/failing-application-registration-variable-owner-workspace-scope.integration-spec.ts.snap b/packages/twenty-server/test/integration/metadata/suites/application-registration-variable/__snapshots__/failing-application-registration-variable-owner-workspace-scope.integration-spec.ts.snap
new file mode 100644
index 00000000000..68b23e53a55
--- /dev/null
+++ b/packages/twenty-server/test/integration/metadata/suites/application-registration-variable/__snapshots__/failing-application-registration-variable-owner-workspace-scope.integration-spec.ts.snap
@@ -0,0 +1,121 @@
+// Jest Snapshot v1, https://goo.gl/fbAQLP
+
+exports[`Application registration variable access outside the owner workspace should fail a registration owned by another workspace should fail to create a variable on it 1`] = `
+{
+ "extensions": {
+ "code": "NOT_FOUND",
+ "subCode": "APPLICATION_REGISTRATION_NOT_FOUND",
+ "userFriendlyMessage": "Application registration not found.",
+ },
+ "message": "Application registration with id <applicationRegistrationId> not found",
+ "name": "NotFoundError",
+}
+`;
+
+exports[`Application registration variable access outside the owner workspace should fail a registration owned by another workspace should fail to delete one of its variables 1`] = `
+{
+ "extensions": {
+ "code": "NOT_FOUND",
+ "subCode": "APPLICATION_REGISTRATION_NOT_FOUND",
+ "userFriendlyMessage": "Application registration not found.",
+ },
+ "message": "Application registration with id <applicationRegistrationId> not found",
+ "name": "NotFoundError",
+}
+`;
+
+exports[`Application registration variable access outside the owner workspace should fail a registration owned by another workspace should fail to read its variables 1`] = `
+{
+ "extensions": {
+ "code": "NOT_FOUND",
+ "subCode": "APPLICATION_REGISTRATION_NOT_FOUND",
+ "userFriendlyMessage": "Application registration not found.",
+ },
+ "message": "Application registration with id <applicationRegistrationId> not found",
+ "name": "NotFoundError",
+}
+`;
+
+exports[`Application registration variable access outside the owner workspace should fail a registration owned by another workspace should fail to reset one of its variables 1`] = `
+{
+ "extensions": {
+ "code": "NOT_FOUND",
+ "subCode": "APPLICATION_REGISTRATION_NOT_FOUND",
+ "userFriendlyMessage": "Application registration not found.",
+ },
+ "message": "Application registration with id <applicationRegistrationId> not found",
+ "name": "NotFoundError",
+}
+`;
+
+exports[`Application registration variable access outside the owner workspace should fail a registration owned by another workspace should fail to update one of its variables 1`] = `
+{
+ "extensions": {
+ "code": "NOT_FOUND",
+ "subCode": "APPLICATION_REGISTRATION_NOT_FOUND",
+ "userFriendlyMessage": "Application registration not found.",
+ },
+ "message": "Application registration with id <applicationRegistrationId> not found",
+ "name": "NotFoundError",
+}
+`;
+
+exports[`Application registration variable access outside the owner workspace should fail a registration owned by no workspace should fail to create a variable on it 1`] = `
+{
+ "extensions": {
+ "code": "NOT_FOUND",
+ "subCode": "APPLICATION_REGISTRATION_NOT_FOUND",
+ "userFriendlyMessage": "Application registration not found.",
+ },
+ "message": "Application registration with id <applicationRegistrationId> not found",
+ "name": "NotFoundError",
+}
+`;
+
+exports[`Application registration variable access outside the owner workspace should fail a registration owned by no workspace should fail to delete one of its variables 1`] = `
+{
+ "extensions": {
+ "code": "NOT_FOUND",
+ "subCode": "APPLICATION_REGISTRATION_NOT_FOUND",
+ "userFriendlyMessage": "Application registration not found.",
+ },
+ "message": "Application registration with id <applicationRegistrationId> not found",
+ "name": "NotFoundError",
+}
+`;
+
+exports[`Application registration variable access outside the owner workspace should fail a registration owned by no workspace should fail to read its variables 1`] = `
+{
+ "extensions": {
+ "code": "NOT_FOUND",
+ "subCode": "APPLICATION_REGISTRATION_NOT_FOUND",
+ "userFriendlyMessage": "Application registration not found.",
+ },
+ "message": "Application registration with id <applicationRegistrationId> not found",
+ "name": "NotFoundError",
+}
+`;
+
+exports[`Application registration variable access outside the owner workspace should fail a registration owned by no workspace should fail to reset one of its variables 1`] = `
+{
+ "extensions": {
+ "code": "NOT_FOUND",
+ "subCode": "APPLICATION_REGISTRATION_NOT_FOUND",
+ "userFriendlyMessage": "Application registration not found.",
+ },
+ "message": "Application registration with id <applicationRegistrationId> not found",
+ "name": "NotFoundError",
+}
+`;
+
+exports[`Application registration variable access outside the owner workspace should fail a registration owned by no workspace should fail to update one of its variables 1`] = `
+{
+ "extensions": {
+ "code": "NOT_FOUND",
+ "subCode": "APPLICATION_REGISTRATION_NOT_FOUND",
+ "userFriendlyMessage": "Application registration not found.",
+ },
+ "message": "Application registration with id <applicationRegistrationId> not found",
+ "name": "NotFoundError",
+}
+`;
diff --git a/packages/twenty-server/test/integration/metadata/suites/application-registration-variable/failing-application-registration-variable-owner-workspace-scope.integration-spec.ts b/packages/twenty-server/test/integration/metadata/suites/application-registration-variable/failing-application-registration-variable-owner-workspace-scope.integration-spec.ts
new file mode 100644
index 00000000000..c196771ced4
--- /dev/null
+++ b/packages/twenty-server/test/integration/metadata/suites/application-registration-variable/failing-application-registration-variable-owner-workspace-scope.integration-spec.ts
@@ -0,0 +1,200 @@
+import { randomUUID } from 'crypto';
+
+import gql from 'graphql-tag';
+import {
+ createApplicationRegistrationVariable,
+ deleteApplicationRegistrationVariable,
+ findApplicationRegistrationVariables,
+ updateApplicationRegistrationVariable,
+} from 'test/integration/metadata/suites/application-registration-variable/utils/application-registration-variable-api.util';
+import { expectOneNotInternalServerErrorSnapshot } from 'test/integration/graphql/utils/expect-one-not-internal-server-error-snapshot.util';
+import { makeMetadataAPIRequest } from 'test/integration/metadata/suites/utils/make-metadata-api-request.util';
+import {
+ type EachTestingContext,
+ eachTestingContextFilter,
+} from 'twenty-shared/testing';
+
+import {
+ SEED_APPLE_WORKSPACE_ID,
+ SEED_YCOMBINATOR_WORKSPACE_ID,
+} from 'src/engine/workspace-manager/dev-seeder/core/constants/seeder-workspaces.constant';
+
+type TestContext = {
+ ownerWorkspaceId: string | null;
+};
+
+// The metadata API requests below authenticate as Jane, in the Apple workspace.
+// Each case hands the registration to someone else once its variable exists.
+const ownerWorkspaceTestCases: EachTestingContext<TestContext>[] = [
+ {
+ title: 'a registration owned by another workspace',
+ context: { ownerWorkspaceId: SEED_YCOMBINATOR_WORKSPACE_ID },
+ },
+ {
+ title: 'a registration owned by no workspace',
+ context: { ownerWorkspaceId: null },
+ },
+];
+
+const findVariableIdByKey = async (
+ applicationRegistrationId: string,
+ key: string,
+): Promise<string | null> => {
+ const [row] = await globalThis.testDataSource.query(
+ `SELECT id FROM core."applicationRegistrationVariable"
+ WHERE "applicationRegistrationId" = $1 AND key = $2`,
+ [applicationRegistrationId, key],
+ );
+
+ return row?.id ?? null;
+};
+
+const readEncryptedValue = async (
+ variableId: string,
+): Promise<string | null> => {
+ const [row] = await globalThis.testDataSource.query(
+ `SELECT "encryptedValue" FROM core."applicationRegistrationVariable" WHERE id = $1`,
+ [variableId],
+ );
+
+ return row?.encryptedValue ?? null;
+};
+
+describe('Application registration variable access outside the owner workspace should fail', () => {
+ describe.each(eachTestingContextFilter(ownerWorkspaceTestCases))(
+ '$title',
+ ({ context }) => {
+ let foreignRegistrationId: string;
+ let foreignVariableId: string;
+ let encryptedValueBeforeAttempt: string | null;
+
+ const normalizeMessage = (message: string) =>
+ message
+ .replace(foreignRegistrationId, '<applicationRegistrationId>')
+ .replace(foreignVariableId, '<applicationRegistrationVariableId>');
+
+ beforeAll(async () => {
+ foreignRegistrationId = randomUUID();
+
+ await globalThis.testDataSource.query(
+ `INSERT INTO core."applicationRegistration"
+ (id, "universalIdentifier", name, "oAuthClientId",
+ "oAuthRedirectUris", "oAuthScopes", "workspaceId")
+ VALUES ($1, $2, $3, $4, $5, $6, $7)`,
+ [
+ foreignRegistrationId,
+ randomUUID(),
+ `owner-workspace-scope-${foreignRegistrationId}`,
+ randomUUID(),
+ ['http://localhost:3000/callback'],
+ ['read'],
+ SEED_APPLE_WORKSPACE_ID,
+ ],
+ );
+
+ const { data } = await createApplicationRegistrationVariable({
+ applicationRegistrationId: foreignRegistrationId,
+ key: 'FOREIGN_API_KEY',
+ value: 'foreign-secret-value',
+ isSecret: true,
+ expectToFail: false,
+ });
+
+ foreignVariableId = data.createApplicationRegistrationVariable.id;
+
+ await globalThis.testDataSource.query(
+ `UPDATE core."applicationRegistration" SET "workspaceId" = $2 WHERE id = $1`,
+ [foreignRegistrationId, context.ownerWorkspaceId],
+ );
+
+ encryptedValueBeforeAttempt =
+ await readEncryptedValue(foreignVariableId);
+ });
+
+ afterAll(async () => {
+ await globalThis.testDataSource.query(
+ `DELETE FROM core."applicationRegistration" WHERE id = $1`,
+ [foreignRegistrationId],
+ );
+ });
+
+ it('should fail to read its variables', async () => {
+ const { errors } = await findApplicationRegistrationVariables({
+ applicationRegistrationId: foreignRegistrationId,
+ expectToFail: true,
+ });
+
+ expectOneNotInternalServerErrorSnapshot({ errors, normalizeMessage });
+ });
+
+ it('should fail to update one of its variables', async () => {
+ const { errors } = await updateApplicationRegistrationVariable({
+ id: foreignVariableId,
+ value: 'value-from-a-non-owner-workspace',
+ expectToFail: true,
+ });
+
+ expectOneNotInternalServerErrorSnapshot({ errors, normalizeMessage });
+
+ expect(await readEncryptedValue(foreignVariableId)).toBe(
+ encryptedValueBeforeAttempt,
+ );
+ });
+
+ it('should fail to reset one of its variables', async () => {
+ const response = await makeMetadataAPIRequest({
+ query: gql`
+ mutation UpdateApplicationRegistrationVariable(
+ $input: UpdateApplicationRegistrationVariableInput!
+ ) {
+ updateApplicationRegistrationVariable(input: $input) {
+ id
+ isFilled
+ }
+ }
+ `,
+ variables: {
+ input: { id: foreignVariableId, update: { resetValue: true } },
+ },
+ });
+
+ expectOneNotInternalServerErrorSnapshot({
+ errors: response.body.errors,
+ normalizeMessage,
+ });
+
+ expect(await readEncryptedValue(foreignVariableId)).toBe(
+ encryptedValueBeforeAttempt,
+ );
+ });
+
+ it('should fail to create a variable on it', async () => {
+ const { errors } = await createApplicationRegistrationVariable({
+ applicationRegistrationId: foreignRegistrationId,
+ key: 'INJECTED_KEY',
+ value: 'injected-value',
+ expectToFail: true,
+ });
+
+ expectOneNotInternalServerErrorSnapshot({ errors, normalizeMessage });
+
+ expect(
+ await findVariableIdByKey(foreignRegistrationId, 'INJECTED_KEY'),
+ ).toBeNull();
+ });
+
+ it('should fail to delete one of its variables', async () => {
+ const { errors } = await deleteApplicationRegistrationVariable({
+ id: foreignVariableId,
+ expectToFail: true,
+ });
+
+ expectOneNotInternalServerErrorSnapshot({ errors, normalizeMessage });
+
+ expect(await readEncryptedValue(foreignVariableId)).toBe(
+ encryptedValueBeforeAttempt,
+ );
+ });
+ },
+ );
+});
diff --git a/packages/twenty-server/test/integration/twenty-config/utils/make-admin-panel-api-request.util.ts b/packages/twenty-server/test/integration/twenty-config/utils/make-admin-panel-api-request.util.ts
index 6eac8c5008a..e8b03e59d15 100644
--- a/packages/twenty-server/test/integration/twenty-config/utils/make-admin-panel-api-request.util.ts
+++ b/packages/twenty-server/test/integration/twenty-config/utils/make-admin-panel-api-request.util.ts
@@ -10,12 +10,13 @@ type GraphqlOperation = {
export const makeAdminPanelAPIRequest = (
graphqlOperation: GraphqlOperation,
+ token: string = APPLE_JANE_ADMIN_ACCESS_TOKEN,
) => {
const client = request(`http://localhost:${APP_PORT}`);
return client
.post('/admin-panel')
- .set('Authorization', `Bearer ${APPLE_JANE_ADMIN_ACCESS_TOKEN}`)
+ .set('Authorization', `Bearer ${token}`)
.send({
query: print(graphqlOperation.query),
variables: graphqlOperation.variables || {},