Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(workflows): copy customer.io email images into the media library on import (#123753)

Daniel Marchukc013edd9eecb
diff --git a/products/workflows/backend/facade/contracts.py b/products/workflows/backend/facade/contracts.py
index 461a659f0281..592ae8378fe8 100644
--- a/products/workflows/backend/facade/contracts.py
+++ b/products/workflows/backend/facade/contracts.py
@@ -1,4 +1,5 @@
import json
+import time
from collections.abc import Mapping
from dataclasses import dataclass, field
from datetime import datetime
@@ -1126,6 +1127,25 @@ class SnippetRoom:
remaining: int = 5_000_000
+@frozen
+class RehostedWorkflow:
+ """An imported workflow whose email images point at copies in the team's media library."""
+
+ workflow: dict[str, Any]
+ notes: tuple[ImportNote, ...]
+
+
+@dataclass(frozen=False)
+class ImageBudget:
+ """How many more email images one import request may copy, and until when, shared by every automation in it."""
+
+ remaining: int = 100
+ # A `time.monotonic()` value. Downloads still running then are dropped, so a slow host can't hold the request.
+ deadline: float = field(default_factory=lambda: time.monotonic() + 60)
+ # False when the caller's token can't write to the media library, so nothing is copied.
+ can_upload: bool = True
+
+
@frozen
class PreviousImport:
"""The earlier import of a source object, when there is one."""
diff --git a/products/workflows/backend/facade/workflow_imports.py b/products/workflows/backend/facade/workflow_imports.py
index 35f7b48d8005..23de522c1420 100644
--- a/products/workflows/backend/facade/workflow_imports.py
+++ b/products/workflows/backend/facade/workflow_imports.py
@@ -1,6 +1,7 @@
"""Workflows imported from other messaging tools, starting with Customer.io automations."""
from products.workflows.backend.services.customerio_import.content import import_content
+from products.workflows.backend.services.customerio_import.images import rehost_images
from products.workflows.backend.services.customerio_import.service import (
import_for_workflow,
previous_import,
@@ -13,5 +14,6 @@ __all__ = [
"import_for_workflow",
"previous_import",
"record_import",
+ "rehost_images",
"translate_campaign",
]
diff --git a/products/workflows/backend/presentation/views/workflow_import_drafts.py b/products/workflows/backend/presentation/views/workflow_import_drafts.py
index 052ae0b6c68b..9af8ee6d4f4f 100644
--- a/products/workflows/backend/presentation/views/workflow_import_drafts.py
+++ b/products/workflows/backend/presentation/views/workflow_import_drafts.py
@@ -14,12 +14,15 @@ from rest_framework.request import Request
from posthog.dataclasses import frozen
from posthog.event_usage import EventSource
from posthog.models import Team, User
+from posthog.permissions import get_authenticator_scopes
+from posthog.scopes import scopes_not_covered
from products.access_control.backend.facade.contracts import ObjectAccessRef
from products.access_control.backend.facade.user_access_control import UserAccessControl
from products.workflows.backend.facade.activity import log_workflow_activity
from products.workflows.backend.facade.contracts import (
CampaignSource,
+ ImageBudget,
ImportNote,
ImportOptions,
PreviousImport,
@@ -32,6 +35,7 @@ from products.workflows.backend.facade.workflow_imports import (
import_content,
previous_import,
record_import,
+ rehost_images,
translate_campaign,
)
from products.workflows.backend.facade.writes import create_workflow, defer_hog_flow_reloads, update_workflow
@@ -68,6 +72,7 @@ def import_campaigns(
content = import_content(
team.id, sources, options, sender_identities, segments, deadline, _left_alone(team, context)
)
+ budget = ImageBudget(deadline=deadline, can_upload=_can_upload_media(request, context.access))
with defer_hog_flow_reloads():
results = [
import_campaign(
@@ -78,6 +83,7 @@ def import_campaigns(
dry_run,
content.fetch_notes.get(_source_id(source), ()),
context,
+ budget,
)
for source in content.sources
]
@@ -132,6 +138,7 @@ def import_campaign(
dry_run: bool,
extra_notes: tuple[ImportNote, ...],
context: _RequestContext,
+ image_budget: ImageBudget,
) -> dict[str, Any]:
"""Imports one Customer.io automation, or previews it, and returns its result in the response shape."""
try:
@@ -149,15 +156,15 @@ def import_campaign(
],
"status": "failed",
}
- notes = (*extra_notes, *translated.notes)
result: dict[str, Any] = {
"source_id": translated.source_id,
"name": translated.name,
"workflow_id": None,
- "notes": [asdict(note) for note in notes],
+ "notes": [asdict(note) for note in (*extra_notes, *translated.notes)],
"validation_errors": [],
}
previous = context.previous.get(translated.source_id) or previous_import(team, translated.source_id)
+ # Checked before images are copied, so a workflow the import leaves alone costs no downloads.
if previous.hog_flow_id is not None and not _can_edit(context.access, team, previous):
return _skipped(
result,
@@ -166,12 +173,20 @@ def import_campaign(
)
if previous.hog_flow_id is not None and not previous.hog_flow_is_draft:
return _skipped(result, previous.hog_flow_id, _NO_LONGER_DRAFT)
+ # Checked before images are copied too, so a draft that can't save costs no downloads.
validated, errors = _validated_draft(team, translated.workflow, context.request)
if validated is None:
return {**result, "status": "failed", "validation_errors": errors}
+ rehosted = rehost_images(team, user, translated.workflow, dry_run, image_budget)
+ notes = (*extra_notes, *translated.notes, *rehosted.notes)
+ result["notes"] = [asdict(note) for note in notes]
if dry_run:
replaces = str(previous.hog_flow_id) if previous.hog_flow_id is not None else None
return {**result, "status": "preview", "workflow_id": replaces}
+ if rehosted.workflow is not translated.workflow:
+ validated, errors = _validated_draft(team, rehosted.workflow, context.request)
+ if validated is None:
+ return {**result, "status": "failed", "validation_errors": errors}
created_name: str | None = None
try:
with transaction.atomic():
@@ -234,6 +249,14 @@ def _skipped(result: dict[str, Any], workflow_id: UUID | None, message: str) ->
return {**result, "status": "skipped", "workflow_id": str(workflow_id), "notes": [asdict(note)]}
+def _can_upload_media(request: Request, access: UserAccessControl) -> bool:
+ # Copying images writes to the media library, so the caller needs the same access as an upload there.
+ scopes = get_authenticator_scopes(getattr(request, "successful_authenticator", None))
+ if scopes is not None and "*" not in scopes and scopes_not_covered(scopes, ["uploaded_media:write"]):
+ return False
+ return access.check_access_level_for_resource("uploaded_media", "editor")
+
+
def _can_edit(access: UserAccessControl, team: Team, previous: PreviousImport) -> bool:
ref = ObjectAccessRef(
resource="hog_flow",
diff --git a/products/workflows/backend/services/customerio_import/images.py b/products/workflows/backend/services/customerio_import/images.py
new file mode 100644
index 000000000000..2547c3766c5e
--- /dev/null
+++ b/products/workflows/backend/services/customerio_import/images.py
@@ -0,0 +1,296 @@
+"""Copies the images an imported Customer.io email shows into the team's email media library.
+
+Customer.io serves email images from its own asset hosts, so they stop loading once the team closes its account.
+Only those hosts are read, and redirects are not followed.
+"""
+
+import re
+import html
+import time
+import hashlib
+from concurrent.futures import ThreadPoolExecutor, wait
+from http.client import HTTPException
+from typing import Any
+from urllib.parse import urlsplit
+
+import requests
+import structlog
+from urllib3.exceptions import HTTPError as Urllib3HTTPError
+
+from posthog.models import Team, User
+from posthog.models.uploaded_media import MEDIA_PURPOSE_EMAIL, ObjectStorageUnavailable, UploadedMedia, sniff_image
+
+from products.workflows.backend.facade.contracts import ImageBudget, ImportNote, RehostedWorkflow
+from products.workflows.backend.facade.enums import WorkflowImportNoteLevel
+
+logger = structlog.get_logger(__name__)
+
+# ASCII only, so a lookalike Unicode host can't match. `_download` checks the host again after unescaping.
+_ASSET_URL = re.compile(r"https://[a-z0-9-]+\.customeriomail\.com/[^\s\"'<>]*", re.IGNORECASE | re.ASCII)
+_ASSET_HOST_SUFFIX = ".customeriomail.com"
+_LINK_ATTRIBUTE = re.compile(r"href\s*=\s*[\"']?$", re.IGNORECASE)
+_MAX_BYTES = 4 * 1024 * 1024
+_TIMEOUT_SECONDS = 10
+_DOWNLOAD_THREADS = 4
+
+
+def rehost_images(
+ team: Team, user: User, workflow: dict[str, Any], dry_run: bool, budget: ImageBudget
+) -> RehostedWorkflow:
+ """The workflow with every Customer.io-hosted email image pointing at a copy in the media library."""
+ urls_by_step = {
+ action["id"]: list(dict.fromkeys(_asset_urls(body)))
+ for action in workflow["actions"]
+ if (body := _html(action)) is not None
+ }
+ all_urls = list(dict.fromkeys(url for urls in urls_by_step.values() for url in urls))
+ if not all_urls:
+ return RehostedWorkflow(workflow=workflow, notes=())
+ count = len({html.unescape(url) for url in all_urls})
+ if not budget.can_upload:
+ return RehostedWorkflow(
+ workflow=workflow,
+ notes=(
+ ImportNote(
+ level=WorkflowImportNoteLevel.NEEDS_INPUT,
+ message=f"{count} email {_images(count)} still {_load(count)} from Customer.io's servers, "
+ "because this import can't write to the media library: the user needs editor access to it, "
+ "and an API key the uploaded_media:write scope. Import as someone who can, or upload "
+ f"{_them(count)} before you close your Customer.io account.",
+ ),
+ ),
+ )
+ if dry_run:
+ return RehostedWorkflow(
+ workflow=workflow,
+ notes=(
+ ImportNote(
+ level=WorkflowImportNoteLevel.APPROXIMATED,
+ message=f"{count} email {_images(count)} on Customer.io's servers will be "
+ "copied into your email media library when you import.",
+ ),
+ ),
+ )
+
+ copied = _copy_images(team, user, all_urls, budget)
+ notes: list[ImportNote] = []
+ actions = []
+ for action in workflow["actions"]:
+ body = _html(action)
+ if body is None:
+ actions.append(action)
+ continue
+ # One pass over whole matches, so a URL that starts with another copied URL keeps its own copy.
+ body = _ASSET_URL.sub(lambda match: _replace(match, copied), body)
+ actions.append(_with_html(action, body))
+ missed = [url for url in urls_by_step.get(action["id"], []) if url not in copied]
+ if missed:
+ notes.append(
+ ImportNote(
+ level=WorkflowImportNoteLevel.NEEDS_INPUT,
+ message=f"{len(missed)} {_images(len(missed))} could not be copied from Customer.io and still "
+ f"{_load(len(missed))} from its servers: {', '.join(missed[:3])}{'…' if len(missed) > 3 else ''}. "
+ f"Upload {_them(len(missed))} to the media library before you close your Customer.io account.",
+ step_id=action["id"],
+ step_name=action.get("name"),
+ )
+ )
+ return RehostedWorkflow(workflow={**workflow, "actions": actions}, notes=tuple(notes))
+
+
+def _copy_images(team: Team, user: User, urls: list[str], budget: ImageBudget) -> dict[str, str]:
+ """Source URL, as the HTML writes it, to media library URL, for every image that is in the library now."""
+ copied: dict[str, str] = {}
+ to_download: list[str] = []
+ names = {url: _file_name(url) for url in urls}
+ existing: dict[str, UploadedMedia] = {}
+ # Anyone with media access can upload under these names, so only copies this user saved count as the import's own.
+ for media in (
+ UploadedMedia.objects.filter(
+ team=team,
+ created_by=user,
+ purpose=MEDIA_PURPOSE_EMAIL,
+ file_name__in=set(names.values()),
+ pending=False,
+ archived_at__isnull=True,
+ )
+ .exclude(media_location__isnull=True)
+ .exclude(media_location="")
+ .order_by("created_at")
+ ):
+ existing.setdefault(media.file_name, media)
+ # Spellings of one image, like `&amp;` and `&`, share a name, so it is downloaded once.
+ spellings: dict[str, list[str]] = {}
+ for url, name in names.items():
+ if name in existing:
+ copied[url] = existing[name].get_absolute_url()
+ else:
+ if name not in spellings:
+ to_download.append(url)
+ spellings.setdefault(name, []).append(url)
+ to_download = to_download[: max(budget.remaining, 0)]
+ budget.remaining -= len(to_download)
+ pool = ThreadPoolExecutor(max_workers=_DOWNLOAD_THREADS)
+ try:
+ # A few at a time, so at most that many images sit in memory before they are saved.
+ for start in range(0, len(to_download), _DOWNLOAD_THREADS):
+ if time.monotonic() >= budget.deadline:
+ break
+ futures = {
+ pool.submit(_download, url, budget.deadline): url
+ for url in to_download[start : start + _DOWNLOAD_THREADS]
+ }
+ done, _ = wait(futures, timeout=max(budget.deadline - time.monotonic(), 0))
+ for future, url in futures.items():
+ content = future.result() if future in done else None
+ # Storage can be slow too, so nothing new is saved once the deadline has passed.
+ if content is None or time.monotonic() > budget.deadline:
+ continue
+ media = _save(team, user, url, content)
+ if media is not None:
+ for spelling in spellings[names[url]]:
+ copied[spelling] = media.get_absolute_url()
+ finally:
+ # A download still running stops at its next read after the deadline, so the request does not wait for it.
+ pool.shutdown(wait=False, cancel_futures=True)
+ return copied
+
+
+def _download(url: str, deadline: float) -> bytes | None:
+ # Browsers read `&amp;` in an attribute as `&`, so the request has to as well.
+ source = html.unescape(url)
+ parts = urlsplit(source)
+ if (
+ parts.scheme != "https"
+ or not (parts.hostname or "").endswith(_ASSET_HOST_SUFFIX)
+ or parts.username is not None
+ or parts.port is not None
+ ):
+ return None
+ try:
+ with requests.get(
+ source,
+ timeout=_TIMEOUT_SECONDS,
+ allow_redirects=False,
+ stream=True,
+ headers={"Accept-Encoding": "identity"},
+ ) as response:
+ if response.status_code != 200:
+ logger.info("workflows.import.image_download_failed", status=response.status_code)
+ return None
+ # `read1` returns what one socket read gives, so a host that sends a byte at a time can't hold the
+ # thread past the deadline. A buffered read waits until it has the whole chunk.
+ read = getattr(getattr(response.raw, "_fp", None), "read1", None) or response.raw.read
+ content = b""
+ while chunk := read(16 * 1024):
+ content += chunk
+ if len(content) > _MAX_BYTES or time.monotonic() > deadline:
+ logger.info("workflows.import.image_download_failed", reason="too_large_or_slow")
+ return None
+ return content
+ # The raw read raises socket and HTTP errors itself, outside what requests wraps.
+ except (requests.RequestException, OSError, HTTPException, Urllib3HTTPError) as error:
+ logger.info("workflows.import.image_download_failed", error=type(error).__name__)
+ return None
+
+
+def _asset_urls(body: str) -> list[str]:
+ return [
+ url
+ for match in _ASSET_URL.finditer(body)
+ if not _is_link(match) and (url := _trimmed(match.group(0))) is not None
+ ]
+
+
+def _is_link(match: re.Match[str]) -> bool:
+ # A link to a Customer.io asset, like a PDF, is not an image the email shows.
+ return _LINK_ATTRIBUTE.search(match.string, max(0, match.start() - 20), match.start()) is not None
+
+
+def _trimmed(found: str) -> str | None:
+ """The image URL in a match, or None for a URL built with Liquid, whose real address only exists at send time."""
+ # A parenthesis is part of the URL unless it closes a CSS `url(...)` around it.
+ end, extra_closing = len(found), found.count(")") - found.count("(")
+ while extra_closing > 0 and end > 0 and found[end - 1] == ")":
+ end -= 1
+ extra_closing -= 1
+ url = found[:end]
+ return None if "{" in url else url
+
+
+def _replace(match: re.Match[str], copied: dict[str, str]) -> str:
+ found = match.group(0)
+ url = None if _is_link(match) else _trimmed(found)
+ if url is None or url not in copied:
+ return found
+ return copied[url] + found[len(url) :]
+
+
+def _save(team: Team, user: User, url: str, content: bytes) -> UploadedMedia | None:
+ sniffed = sniff_image(content)
+ if sniffed is None:
+ return None
+ try:
+ media = UploadedMedia.save_content(
+ team=team,
+ created_by=user,
+ file_name=_file_name(url),
+ content_type=sniffed.content_type,
+ content=content,
+ purpose=MEDIA_PURPOSE_EMAIL,
+ )
+ except ObjectStorageUnavailable:
+ # `save_content` keeps its row before it raises, and that row has no file behind it.
+ UploadedMedia.objects.filter(
+ team=team, purpose=MEDIA_PURPOSE_EMAIL, file_name=_file_name(url), media_location__isnull=True
+ ).delete()
+ return None
+ if media is None:
+ return None
+ media.size_bytes = len(content)
+ media.width = sniffed.width
+ media.height = sniffed.height
+ media.save(update_fields=["size_bytes", "width", "height"])
+ return media
+
+
+def _file_name(url: str) -> str:
+ # The digest makes the name unique per source URL, so importing again reuses the copy.
+ source = html.unescape(url)
+ digest = hashlib.sha256(source.encode()).hexdigest()[:12]
+ base = urlsplit(source).path.rsplit("/", 1)[-1] or "image"
+ return f"customerio-{digest}-{base}"[:200]
+
+
+def _html(action: dict[str, Any]) -> str | None:
+ if action.get("type") != "function_email":
+ return None
+ value = (((action.get("config") or {}).get("inputs") or {}).get("email") or {}).get("value") or {}
+ body = value.get("html")
+ return body if isinstance(body, str) else None
+
+
+def _with_html(action: dict[str, Any], body: str) -> dict[str, Any]:
+ config = action["config"]
+ email = config["inputs"]["email"]
+ # The design is built from the HTML when the draft is checked, so it is dropped and built again from the copies.
+ value = {key: item for key, item in email["value"].items() if key != "design"}
+ return {
+ **action,
+ "config": {
+ **config,
+ "inputs": {**config["inputs"], "email": {**email, "value": {**value, "html": body}}},
+ },
+ }
+
+
+def _images(count: int) -> str:
+ return "image" if count == 1 else "images"
+
+
+def _load(count: int) -> str:
+ return "loads" if count == 1 else "load"
+
+
+def _them(count: int) -> str:
+ return "it" if count == 1 else "them"
diff --git a/products/workflows/backend/tests/test_customerio_import.py b/products/workflows/backend/tests/test_customerio_import.py
index 6461b7564236..940d4f0d8374 100644
--- a/products/workflows/backend/tests/test_customerio_import.py
+++ b/products/workflows/backend/tests/test_customerio_import.py
@@ -1,3 +1,4 @@
+import io
import json
import base64
from datetime import UTC, datetime
@@ -8,11 +9,17 @@ from posthog.test.base import APIBaseTest, BaseTest
from unittest.mock import MagicMock, patch
from django.test import SimpleTestCase
+from django.utils import timezone
from parameterized import parameterized
+from PIL import Image
from posthog.event_usage import EventSource
+from posthog.models import User
from posthog.models.integration import Integration
+from posthog.models.personal_api_key import PersonalAPIKey, hash_key_value
+from posthog.models.uploaded_media import UploadedMedia
+from posthog.models.utils import generate_random_token_personal
from products.cdp.backend.api.test.test_hog_function_templates import MOCK_NODE_TEMPLATES
from products.cdp.backend.templates.hog_function_template import sync_template_to_db
@@ -30,6 +37,7 @@ from products.workflows.backend.facade.enums import WorkflowImportNoteLevel as N
from products.workflows.backend.models.hog_flow.hog_flow import HogFlow
from products.workflows.backend.presentation.views.data_suggestion_drafts import validated_draft
from products.workflows.backend.services.customerio_import import conditions, liquid
+from products.workflows.backend.services.customerio_import.images import _file_name
from products.workflows.backend.services.customerio_import.translator import translate_campaign
@@ -658,6 +666,154 @@ class TestWorkflowImportAPI(APIBaseTest):
assert result["status"] == "created", result["validation_errors"]
assert any("could not read this automation's messages" in note["message"] for note in result["notes"])
+ @patch("products.workflows.backend.services.customerio_import.images.requests.get")
+ def test_copies_customerio_email_images_into_the_media_library(self, mock_get: MagicMock) -> None:
+ png = io.BytesIO()
+ Image.new("RGB", (3, 2)).save(png, "PNG")
+ hero = "https://userimg-assets.customeriomail.com/images/client-env-1/hero.png"
+ gone = "https://userimg-assets.customeriomail.com/images/client-env-1/gone.png"
+ # A longer URL that starts with `hero`, and one whose query the HTML escapes.
+ hero_webp, sized = f"{hero}.webp", f"{hero}?w=1&amp;h=2"
+ logo = "https://userimg-assets.customeriomail.com/images/client-env-1/logo(1).png"
+ # Neither may be requested: one only has an address at send time, one unescapes to another host.
+ templated = "https://userimg-assets.customeriomail.com/images/client-env-1/v.png?v={{ person.properties.v }}"
+ smuggled = "https://userimg-assets.customeriomail.com&#64;evil.example.com/a.png"
+ lookalike = "https://userimg-assets.customeriomaİl.com/a.png"
+ guide = "https://userimg-assets.customeriomail.com/files/client-env-1/guide.pdf"
+ stalled = "https://userimg-assets.customeriomail.com/images/client-env-1/stalled.png"
+ served = {hero, hero_webp, f"{hero}?w=1&h=2", logo}
+
+ def respond(url: str, **kwargs: Any) -> MagicMock:
+ response = MagicMock(status_code=200 if url in served or url == stalled else 404)
+ response.__enter__.return_value = response
+ response.raw = io.BytesIO(png.getvalue())
+ if url == stalled:
+ response.raw = MagicMock(spec=["read"], read=MagicMock(side_effect=TimeoutError("read timed out")))
+ return response
+
+ mock_get.side_effect = respond
+ source = _onboarding_campaign()
+ templates = {
+ **source.templates,
+ "11": {
+ **source.templates["11"],
+ "body": f'<img src="{hero}"><img src="{gone}"><img src="https://cdn.example.com/logo.png">'
+ f'<img src="{hero_webp}"><img src="{sized}"><div style="background: url({logo})"></div>'
+ f'<img src="{templated}"><img src="{smuggled}"><img src="{lookalike}">'
+ f'<a href="{guide}">Guide</a><img src="{hero}?w=1&h=2"><img src="{stalled}">',
+ },
+ }
+ campaigns = [{"campaign": source.campaign, "actions": source.actions, "templates": list(templates.values())}]
+
+ preview = self._import(campaigns=campaigns, dry_run=True)
+ assert any("6 email images on Customer.io's servers will be copied" in n["message"] for n in preview["notes"])
+ assert not mock_get.called
+
+ planted = UploadedMedia.objects.create(
+ team=self.team,
+ created_by=User.objects.create_and_join(self.organization, "[email protected]", None),
+ purpose="email",
+ file_name=_file_name(hero),
+ media_location="planted/hero.png",
+ )
+ first = self._import(campaigns=campaigns)
+ html = next(a for a in HogFlow.objects.get(pk=first["workflow_id"]).actions if a["id"] == "cio_1")["config"][
+ "inputs"
+ ]["email"]["value"]["html"]
+ assert planted.get_absolute_url() not in html
+ copies = list(
+ UploadedMedia.objects.filter(team=self.team, purpose="email", created_by=self.user).order_by("created_at")
+ )
+ assert [(m.content_type, m.width, m.height) for m in copies] == [("image/png", 3, 2)] * 4
+ urls = [media.get_absolute_url() for media in copies]
+ assert html == (
+ '<div style="font-family: Arial, Helvetica, sans-serif;">'
+ f'<img src="{urls[0]}"><img src="{gone}"><img src="https://cdn.example.com/logo.png">'
+ f'<img src="{urls[1]}"><img src="{urls[2]}"><div style="background: url({urls[3]})"></div>'
+ f'<img src="{templated}"><img src="{smuggled}"><img src="{lookalike}">'
+ f'<a href="{guide}">Guide</a><img src="{urls[2]}"><img src="{stalled}"></div>'
+ )
+ requested = [call.args[0] for call in mock_get.call_args_list]
+ assert "evil.example.com" not in " ".join(requested)
+ assert guide not in requested
+ assert requested.count(f"{hero}?w=1&h=2") == 1
+ assert any(n["step_id"] == "cio_1" and gone in n["message"] for n in first["notes"])
+ design = json.dumps(
+ next(a for a in HogFlow.objects.get(pk=first["workflow_id"]).actions if a["id"] == "cio_1")["config"][
+ "inputs"
+ ]["email"]["value"]["design"]
+ )
+ assert hero_webp not in design and urls[1] in design
+
+ self._import(campaigns=campaigns)
+ assert UploadedMedia.objects.filter(team=self.team, purpose="email", created_by=self.user).count() == 4
+ assert [call.args[0] for call in mock_get.call_args_list].count(hero) == 1
+
+ UploadedMedia.objects.filter(pk=copies[0].pk).update(archived_at=timezone.now())
+ self._import(campaigns=campaigns)
+ assert [call.args[0] for call in mock_get.call_args_list].count(hero) == 2
+ assert (
+ UploadedMedia.objects.filter(
+ team=self.team, purpose="email", created_by=self.user, archived_at__isnull=True
+ ).count()
+ == 4
+ )
+ assert all(call.kwargs["allow_redirects"] is False for call in mock_get.call_args_list)
+
+ @patch("products.workflows.backend.presentation.views.workflow_import_drafts._REQUEST_SECONDS", 0)
+ @patch("products.workflows.backend.services.customerio_import.images.requests.get")
+ def test_no_image_downloads_start_after_the_deadline(self, mock_get: MagicMock) -> None:
+ source = _onboarding_campaign()
+ image = "https://userimg-assets.customeriomail.com/images/client-env-1/late.png"
+ templates = {**source.templates, "11": {**source.templates["11"], "body": f'<img src="{image}">'}}
+ result = self._import(
+ campaigns=[{"campaign": source.campaign, "actions": source.actions, "templates": list(templates.values())}]
+ )
+
+ assert result["status"] == "created"
+ assert any(image in note["message"] for note in result["notes"])
+ assert not mock_get.called
+
+ @parameterized.expand(
+ [
+ ("an API key without the media scope", ["hog_flow:write"], True),
+ ("a user without media access", ["*"], False),
+ ]
+ )
+ @patch("products.workflows.backend.services.customerio_import.images.requests.get")
+ def test_no_images_are_copied_without_media_access(
+ self, _name: str, scopes: list[str], media_access: bool, mock_get: MagicMock
+ ) -> None:
+ key = generate_random_token_personal()
+ PersonalAPIKey.objects.create(label="key", user=self.user, secure_value=hash_key_value(key), scopes=scopes)
+ self.client.logout()
+ source = _onboarding_campaign()
+ image = "https://userimg-assets.customeriomail.com/images/client-env-1/hero.png"
+ templates = {**source.templates, "11": {**source.templates["11"], "body": f'<img src="{image}">'}}
+ body = {
+ "source": "customerio",
+ "campaigns": [
+ {"campaign": source.campaign, "actions": source.actions, "templates": list(templates.values())}
+ ],
+ }
+ with patch(
+ "products.workflows.backend.presentation.views.workflow_import_drafts.UserAccessControl.check_access_level_for_resource",
+ side_effect=lambda resource, *_args, **_kwargs: media_access or resource != "uploaded_media",
+ ):
+ response = self.client.post(
+ f"/api/projects/{self.team.id}/workflow_imports/",
+ body,
+ format="json",
+ headers={"authorization": f"Bearer {key}"},
+ )
+
+ assert response.status_code == 200, response.json()
+ result = response.json()["results"][0]
+ assert result["status"] == "created"
+ assert any("media library" in note["message"] for note in result["notes"])
+ assert not mock_get.called
+ assert not UploadedMedia.objects.filter(team=self.team).exists()
+
def test_notes_are_readable_by_workflow(self) -> None:
result = self._import()