Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
MarconLP committed Oct 17, 2024
1 parent 4c0ba1b commit a9ecdbb
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
18 changes: 8 additions & 10 deletions posthog/cdp/templates/discord/template_discord.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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,
},
Expand All @@ -46,19 +48,15 @@
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(
id="survey_response",
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}**"},
),
],
)
33 changes: 33 additions & 0 deletions posthog/cdp/templates/discord/test_template_discord.py
Original file line number Diff line number Diff line change
@@ -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": "**[email protected]** 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": "**[email protected]** triggered event: '$pageview'",
},
},
)
)

0 comments on commit a9ecdbb

Please sign in to comment.