-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cdp): sendgrid template updates (#24814)
- Loading branch information
1 parent
1107dd7
commit b3d24c3
Showing
4 changed files
with
242 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
from inline_snapshot import snapshot | ||
from posthog.cdp.templates.helpers import BaseHogFunctionTemplateTest | ||
from posthog.cdp.templates.sendgrid.template_sendgrid import template as template_sendgrid | ||
from posthog.cdp.templates.sendgrid.template_sendgrid import template as template_sendgrid, TemplateSendGridMigrator | ||
from posthog.models import PluginConfig | ||
from posthog.test.base import BaseTest | ||
|
||
|
||
class TestTemplateSendgrid(BaseHogFunctionTemplateTest): | ||
|
@@ -47,3 +49,138 @@ def test_function_doesnt_include_empty_properties(self): | |
assert self.get_mock_fetch_calls()[0][1]["body"]["contacts"] == snapshot( | ||
[{"email": "[email protected]", "last_name": "included"}] | ||
) | ||
|
||
def test_function_adds_custom_fields(self): | ||
self.mock_fetch_response = lambda *args: { # type: ignore | ||
"status": 200, | ||
"body": {"custom_fields": [{"id": "id7", "name": "custom_field"}]}, | ||
} | ||
|
||
res = self.run_function( | ||
inputs=self._inputs( | ||
custom_fields={"custom_field": "custom_value"}, | ||
) | ||
) | ||
assert res.result is None | ||
|
||
assert self.get_mock_fetch_calls()[0] == snapshot( | ||
( | ||
"https://api.sendgrid.com/v3/marketing/field_definitions", | ||
{ | ||
"method": "GET", | ||
"headers": {"Authorization": "Bearer API_KEY", "Content-Type": "application/json"}, | ||
}, | ||
) | ||
) | ||
|
||
assert self.get_mock_fetch_calls()[1] == snapshot( | ||
( | ||
"https://api.sendgrid.com/v3/marketing/contacts", | ||
{ | ||
"method": "PUT", | ||
"headers": {"Authorization": "Bearer API_KEY", "Content-Type": "application/json"}, | ||
"body": { | ||
"contacts": [ | ||
{ | ||
"email": "[email protected]", | ||
"last_name": "example", | ||
"custom_fields": {"id7": "custom_value"}, | ||
} | ||
] | ||
}, | ||
}, | ||
) | ||
) | ||
|
||
|
||
class TestTemplateMigration(BaseTest): | ||
def get_plugin_config(self, config: dict): | ||
_config = { | ||
"sendgridApiKey": "SENDGRID_API_KEY", | ||
"customFields": "", | ||
} | ||
_config.update(config) | ||
return PluginConfig(enabled=True, order=0, config=_config) | ||
|
||
def test_empty_fields(self): | ||
obj = self.get_plugin_config({}) | ||
|
||
template = TemplateSendGridMigrator.migrate(obj) | ||
assert template["inputs"] == snapshot( | ||
{ | ||
"api_key": {"value": "SENDGRID_API_KEY"}, | ||
"email": {"value": "{person.properties.email}"}, | ||
"custom_fields": {"value": {}}, | ||
"properties": {"value": {}}, | ||
} | ||
) | ||
assert template["filters"] == snapshot( | ||
{"events": [{"id": "$identify", "name": "$identify", "type": "events", "order": 0}]} | ||
) | ||
|
||
def test_default_properties(self): | ||
obj = self.get_plugin_config({"customFields": "last_name,first_name"}) | ||
|
||
template = TemplateSendGridMigrator.migrate(obj) | ||
assert template["inputs"] == snapshot( | ||
{ | ||
"api_key": {"value": "SENDGRID_API_KEY"}, | ||
"email": {"value": "{person.properties.email}"}, | ||
"custom_fields": {"value": {}}, | ||
"properties": { | ||
"value": { | ||
"last_name": "{person.properties.last_name}", | ||
"first_name": "{person.properties.first_name}", | ||
} | ||
}, | ||
} | ||
) | ||
assert template["filters"] == snapshot( | ||
{"events": [{"id": "$identify", "name": "$identify", "type": "events", "order": 0}]} | ||
) | ||
|
||
def test_custom_fields(self): | ||
obj = self.get_plugin_config({"customFields": "last_name,first_name,misc_name,banana"}) | ||
|
||
template = TemplateSendGridMigrator.migrate(obj) | ||
assert template["inputs"] == snapshot( | ||
{ | ||
"api_key": {"value": "SENDGRID_API_KEY"}, | ||
"email": {"value": "{person.properties.email}"}, | ||
"custom_fields": { | ||
"value": {"misc_name": "{person.properties.misc_name}", "banana": "{person.properties.banana}"} | ||
}, | ||
"properties": { | ||
"value": { | ||
"last_name": "{person.properties.last_name}", | ||
"first_name": "{person.properties.first_name}", | ||
} | ||
}, | ||
} | ||
) | ||
assert template["filters"] == snapshot( | ||
{"events": [{"id": "$identify", "name": "$identify", "type": "events", "order": 0}]} | ||
) | ||
|
||
def test_property_rename(self): | ||
obj = self.get_plugin_config({"customFields": "$lastName=last_name,first_name,misc_name,$pineapple=banana"}) | ||
|
||
template = TemplateSendGridMigrator.migrate(obj) | ||
assert template["inputs"] == snapshot( | ||
{ | ||
"api_key": {"value": "SENDGRID_API_KEY"}, | ||
"email": {"value": "{person.properties.email}"}, | ||
"custom_fields": { | ||
"value": {"misc_name": "{person.properties.misc_name}", "banana": "{person.properties.$pineapple}"} | ||
}, | ||
"properties": { | ||
"value": { | ||
"last_name": "{person.properties.$lastName}", | ||
"first_name": "{person.properties.first_name}", | ||
} | ||
}, | ||
} | ||
) | ||
assert template["filters"] == snapshot( | ||
{"events": [{"id": "$identify", "name": "$identify", "type": "events", "order": 0}]} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters