Skip to content

Commit

Permalink
Refactor into a validateOutgoingDomains helper function and create te…
Browse files Browse the repository at this point in the history
…sts to parse valid and invalid domains

Signed-off-by: Arun <arun@arun.blog>
  • Loading branch information
arunsathiya committed Nov 21, 2023
1 parent 3ccea37 commit 2cff6ca
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 8 deletions.
66 changes: 65 additions & 1 deletion src/manifest/manifest_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
ISlackManifestRunOnSlack,
SlackManifestType,
} from "./types.ts";
import { Manifest, SlackManifest } from "./mod.ts";
import { Manifest, SlackManifest, validateOutgoingDomains } from "./mod.ts";
import {
DefineDatastore,
DefineEvent,
Expand All @@ -26,6 +26,7 @@ import {
import { DefineConnector } from "../functions/mod.ts";
import { InternalSlackTypes } from "../schema/slack/types/custom/mod.ts";
import { DuplicateCallbackIdError, DuplicateNameError } from "./errors.ts";
import { assertThrows } from "https://deno.land/std@0.152.0/testing/asserts.ts";

Deno.test("SlackManifestType correctly resolves to a Hosted App when runOnSlack = true", () => {
const definition: SlackManifestType = {
Expand Down Expand Up @@ -1172,3 +1173,66 @@ Deno.test("Manifest throws error when CustomEvents with duplicate name are added
assertStringIncludes(error.message, "CustomEvent");
}
});

Deno.test("Manifest correctly parses valid domains", () => {
const def = {
outgoingDomains: [
"https://slack.com",
"http://salesforce.com",
"https://api.slack.com",
],
};
const expected = ["slack.com", "salesforce.com", "api.slack.com"];

const definition: SlackManifestType = {
runOnSlack: false,
name: "test",
description: "description",
backgroundColor: "#FFF",
longDescription:
"The book is a roman à clef, rooted in autobiographical incidents. The story follows its protagonist, Raoul Duke, and his attorney, Dr. Gonzo, as they descend on Las Vegas to chase the American Dream...",
displayName: "displayName",
icon: "icon.png",
botScopes: ["channels:history", "chat:write", "commands"],
};
const manifest = Manifest(definition);

manifest.outgoing_domains = validateOutgoingDomains(
def.outgoingDomains || [],
);

assertEquals(manifest.outgoing_domains, expected);
});

Deno.test("Manifest throws error for invalid domains", () => {
const def = {
outgoingDomains: [
"slack",
"htt*ps://api.slack.com",
"https://api slack.com",
],
};

assertThrows(
() => {
const definition: SlackManifestType = {
runOnSlack: false,
name: "test",
description: "description",
backgroundColor: "#FFF",
longDescription:
"The book is a roman à clef, rooted in autobiographical incidents. The story follows its protagonist, Raoul Duke, and his attorney, Dr. Gonzo, as they descend on Las Vegas to chase the American Dream...",
displayName: "displayName",
icon: "icon.png",
botScopes: ["channels:history", "chat:write", "commands"],
};
const manifest = Manifest(definition);

manifest.outgoing_domains = validateOutgoingDomains(
def.outgoingDomains || [],
);
},
Error,
"Invalid outgoing domain",
);
});
22 changes: 15 additions & 7 deletions src/manifest/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ export const Manifest = (
return manifest.export();
};

export const validateOutgoingDomains = (
domains: string[],
) => {
return domains.map((domain) => {
try {
return new URL(domain).hostname;
} catch (e) {
throw new Error(`Invalid outgoing domain: ${domain}, error ${e}`);
}
});
};

export class SlackManifest {
constructor(private definition: SlackManifestType) {
this.registerFeatures();
Expand Down Expand Up @@ -132,13 +144,9 @@ export class SlackManifest {
);
}

manifest.outgoing_domains = (def.outgoingDomains || []).map((domain) => {
try {
return new URL(domain).hostname;
} catch (e) {
throw new Error(`Invalid outgoing domain: ${domain}, error ${e}`);
}
});
manifest.outgoing_domains = validateOutgoingDomains(
def.outgoingDomains || [],
);

// Assign remote hosted app properties
if (manifest.settings.function_runtime === "slack") {
Expand Down

0 comments on commit 2cff6ca

Please sign in to comment.