Skip to content

Commit

Permalink
Removing invalid characters from auto-generated integration keys (#5501)
Browse files Browse the repository at this point in the history
  • Loading branch information
galvana authored Nov 18, 2024
1 parent b8add27 commit 2c64212
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,7 @@ import {
ConnectorParametersForm,
TestConnectionResponse,
} from "./ConnectorParametersForm";

const generateIntegrationKey = (
systemFidesKey: string,
connectionOption: ConnectionSystemTypeMap,
): string => {
let integrationKey = systemFidesKey;

if (!systemFidesKey.includes(connectionOption.identifier)) {
integrationKey += `_${connectionOption.identifier}`;
}

if (connectionOption.type === SystemType.SAAS) {
integrationKey += "_api";
}

return integrationKey;
};
import { generateIntegrationKey } from "./helpers";

/**
* Only handles creating saas connectors. The BE handler automatically
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { SystemType } from "~/types/api/models/SystemType";

import { generateIntegrationKey } from "./helpers";

describe("generateIntegrationKey", () => {
it("removes special characters except allowed ones", () => {
const result = generateIntegrationKey("test.system!@#$%^&*()", {
identifier: "test",
type: SystemType.DATABASE,
});
expect(result).toBe("testsystem");
});

it("keeps allowed characters (alphanumeric, hyphen, underscore)", () => {
const result = generateIntegrationKey("test-system_123", {
identifier: "test",
type: SystemType.DATABASE,
});
expect(result).toBe("test-system_123");
});

it("adds identifier if not present", () => {
const result = generateIntegrationKey("system", {
identifier: "postgres",
type: SystemType.DATABASE,
});
expect(result).toBe("system_postgres");
});

it("does not add identifier if already present", () => {
const result = generateIntegrationKey("system_postgres", {
identifier: "postgres",
type: SystemType.DATABASE,
});
expect(result).toBe("system_postgres");
});

it("adds API suffix for SaaS type", () => {
const result = generateIntegrationKey("system", {
identifier: "salesforce",
type: SystemType.SAAS,
});
expect(result).toBe("system_salesforce_api");
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { ConnectionTypeSecretSchemaResponse } from "~/features/connection-type/types";
import { ConnectionSystemTypeMap } from "~/types/api/models/ConnectionSystemTypeMap";
import { SystemType } from "~/types/api/models/SystemType";

/**
* Fill in default values based off of a schema
Expand Down Expand Up @@ -29,3 +31,25 @@ export const fillInDefaults = (
}
return filledInValues;
};

/**
*
* Auto-generate an integration key based on the system name
*
*/
export const generateIntegrationKey = (
systemFidesKey: string,
connectionOption: Pick<ConnectionSystemTypeMap, "identifier" | "type">,
): string => {
let integrationKey = systemFidesKey.replace(/[^A-Za-z0-9\-_]/g, "");

if (!integrationKey.includes(connectionOption.identifier)) {
integrationKey += `_${connectionOption.identifier}`;
}

if (connectionOption.type === SystemType.SAAS) {
integrationKey += "_api";
}

return integrationKey;
};

0 comments on commit 2c64212

Please sign in to comment.