From a9ecdbb3d1d2cca17901874213f712564e54a13e Mon Sep 17 00:00:00 2001 From: MarconLP <13001502+MarconLP@users.noreply.github.com> Date: Thu, 17 Oct 2024 14:16:18 +0200 Subject: [PATCH] add test --- .../cdp/templates/discord/template_discord.py | 18 +++++----- .../discord/test_template_discord.py | 33 +++++++++++++++++++ 2 files changed, 41 insertions(+), 10 deletions(-) create mode 100644 posthog/cdp/templates/discord/test_template_discord.py diff --git a/posthog/cdp/templates/discord/template_discord.py b/posthog/cdp/templates/discord/template_discord.py index 576324ef21e4a..2fdebbd28d01b 100644 --- a/posthog/cdp/templates/discord/template_discord.py +++ b/posthog/cdp/templates/discord/template_discord.py @@ -9,14 +9,16 @@ category=["Customer Success"], hog=""" let res := fetch(inputs.webhookUrl, { - 'body': inputs.content, + 'body': { + 'content': inputs.content + }, 'method': 'POST', 'headers': { 'Content-Type': 'application/json' } }); -if (res.status != 200 or not res.body.ok) { +if (res.status >= 400) { throw Error(f'Failed to post message to Discord: {res.status}: {res.body}'); } """.strip(), @@ -31,10 +33,10 @@ }, { "key": "content", - "type": "json", + "type": "string", "label": "Content", "description": "(see https://support.discord.com/hc/en-us/articles/210298617-Markdown-Text-101-Chat-Formatting-Bold-Italic-Underline)", - "default": {"content": "**{person.name}** triggered event: '{event.event}'"}, + "default": "**{person.name}** triggered event: '{event.event}'", "secret": False, "required": True, }, @@ -46,9 +48,7 @@ description="Posts a message to Discord when a user enrolls or un-enrolls in an early access feature", filters=SUB_TEMPLATE_COMMON["early_access_feature_enrollment"].filters, inputs={ - "content": { - "content": "**{person.name}** {event.properties.$feature_enrollment ? 'enrolled in' : 'un-enrolled from'} the early access feature for '{event.properties.$feature_flag}'" - }, + "content": "**{person.name}** {event.properties.$feature_enrollment ? 'enrolled in' : 'un-enrolled from'} the early access feature for '{event.properties.$feature_flag}'" }, ), HogFunctionSubTemplate( @@ -56,9 +56,7 @@ name="Post to Discord on survey response", description="Posts a message to Discord when a user responds to a survey", filters=SUB_TEMPLATE_COMMON["survey_response"].filters, - inputs={ - "content": {"content": "**{person.name}** responded to survey **{event.properties.$survey_name}**"}, - }, + inputs={"content": "**{person.name}** responded to survey **{event.properties.$survey_name}**"}, ), ], ) diff --git a/posthog/cdp/templates/discord/test_template_discord.py b/posthog/cdp/templates/discord/test_template_discord.py new file mode 100644 index 0000000000000..ddaa622cdef9b --- /dev/null +++ b/posthog/cdp/templates/discord/test_template_discord.py @@ -0,0 +1,33 @@ +from inline_snapshot import snapshot +from posthog.cdp.templates.helpers import BaseHogFunctionTemplateTest +from posthog.cdp.templates.discord.template_discord import template as template_discord + + +class TestTemplateDiscord(BaseHogFunctionTemplateTest): + template = template_discord + + def _inputs(self, **kwargs): + inputs = { + "webhookUrl": "https://discord.com/api/webhooks/00000000000000000/xxxxxxxxxxxxxx", + "content": "**max@posthog.com** triggered event: '$pageview'", + } + inputs.update(kwargs) + return inputs + + def test_function_works(self): + self.run_function(inputs=self._inputs()) + + assert self.get_mock_fetch_calls()[0] == snapshot( + ( + "https://discord.com/api/webhooks/00000000000000000/xxxxxxxxxxxxxx", + { + "method": "POST", + "headers": { + "Content-Type": "application/json", + }, + "body": { + "content": "**max@posthog.com** triggered event: '$pageview'", + }, + }, + ) + )