Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 36 additions & 9 deletions courses/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
from courses.models import (
CourseRunCertificate,
Program,
ProgramCertificate,
)
from hubspot_sync.api import upsert_custom_properties
from hubspot_sync.task_helpers import sync_hubspot_user
from hubspot_sync import tasks as hubspot_tasks


@receiver(
Expand Down Expand Up @@ -44,10 +44,37 @@ def handle_create_course_run_certificate(
lambda: generate_multiple_programs_certificate(user, programs)
)

try:
upsert_custom_properties()
sync_hubspot_user(instance.user)
except Exception: # pylint: disable=broad-except
logger = logging.getLogger(__name__)
logger.exception("Error syncing Hubspot user")
# avoid blocking certificate creation
try:
transaction.on_commit(
lambda: hubspot_tasks.sync_course_run_certificate_with_hubspot.delay(
instance.id
)
)
except Exception: # pylint: disable=broad-except
logger = logging.getLogger(__name__)
logger.exception("Error syncing HubSpot course run certificate")
# avoid blocking certificate save flow


@receiver(
post_save,
sender=ProgramCertificate,
dispatch_uid="programcertificate_post_save",
)
def handle_create_program_certificate(
sender, # pylint: disable=unused-argument # noqa: ARG001
instance,
created,
**kwargs, # pylint: disable=unused-argument # noqa: ARG001
):
"""When a ProgramCertificate model is created."""
try:
transaction.on_commit(
lambda: hubspot_tasks.sync_program_certificate_with_hubspot.delay(
instance.id
)
)
except Exception: # pylint: disable=broad-except
logger = logging.getLogger(__name__)
logger.exception("Error syncing HubSpot program certificate")
# avoid blocking certificate save flow
48 changes: 48 additions & 0 deletions courses/signals_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,27 @@
CourseFactory,
CourseRunCertificateFactory,
CourseRunFactory,
ProgramCertificateFactory,
ProgramFactory,
UserFactory,
)

pytestmark = pytest.mark.django_db


@pytest.fixture(autouse=True)
def mock_certificate_hubspot_sync_tasks(mocker):
"""Mock certificate HubSpot sync tasks to avoid external API calls in signal tests."""
return {
"course_run": mocker.patch(
"courses.signals.hubspot_tasks.sync_course_run_certificate_with_hubspot.delay"
),
"program": mocker.patch(
"courses.signals.hubspot_tasks.sync_program_certificate_with_hubspot.delay"
),
}


# pylint: disable=unused-argument
@patch("courses.signals.transaction.on_commit", side_effect=lambda callback: callback())
@patch("courses.signals.generate_multiple_programs_certificate", autospec=True)
Expand Down Expand Up @@ -69,3 +83,37 @@ def test_generate_program_certificate_not_called(
cert = CourseRunCertificateFactory.create(user=user, course_run=course_run)
cert.save()
generate_program_cert_mock.assert_not_called()


@patch("courses.signals.transaction.on_commit", side_effect=lambda callback: callback())
def test_sync_course_certificate_with_hubspot_on_save(
mock_on_commit, mock_certificate_hubspot_sync_tasks
):
"""Test that course certificate HubSpot sync is triggered on create and update."""
sync_course_cert_mock = mock_certificate_hubspot_sync_tasks["course_run"]
cert = CourseRunCertificateFactory.create()

sync_course_cert_mock.assert_called_once_with(cert.id)

cert.issue_date = cert.issue_date
cert.save()

assert sync_course_cert_mock.call_count == 2
sync_course_cert_mock.assert_called_with(cert.id)


@patch("courses.signals.transaction.on_commit", side_effect=lambda callback: callback())
def test_sync_program_certificate_with_hubspot_on_save(
mock_on_commit, mock_certificate_hubspot_sync_tasks
):
"""Test that program certificate HubSpot sync is triggered on create and update."""
sync_program_cert_mock = mock_certificate_hubspot_sync_tasks["program"]
cert = ProgramCertificateFactory.create()

sync_program_cert_mock.assert_called_once_with(cert.id)

cert.issue_date = cert.issue_date
cert.save()

assert sync_program_cert_mock.call_count == 2
sync_program_cert_mock.assert_called_with(cert.id)
Loading
Loading