Skip to content

Commit

Permalink
Merge branch 'main' into 805-correct-cananda-urls
Browse files Browse the repository at this point in the history
  • Loading branch information
MackHalliday authored Oct 9, 2024
2 parents 5d747ce + f0b0c00 commit 10fce57
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .talismanrc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fileignoreconfig:
- filename: scripts/trigger_task.py
checksum: 0e9d244dbe285de23fc84bb643407963dacf7d25a3358373f01f6272fb217778
- filename: tests/app/celery/test_process_ga4_measurement_task.py
checksum: d33a6911258922f4bd3d149c90c2ee16c021a8e59e462594e4b1cd972902d689
checksum: 6ffb8742a19c5b834c608826fd459cc1b6ea35ebfffd2d929a3a0f269c74183d
- filename: tests/app/conftest.py
checksum: a80aa727586db82ed1b50bdb81ddfe1379e649a9dfc1ece2c36047486b41b83d
- filename: tests/app/notifications/test_process_notifications_for_profile_v3.py
Expand Down
22 changes: 15 additions & 7 deletions app/celery/process_ga4_measurement_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from app import db, notify_celery
from app.celery.exceptions import AutoRetryException
from app.models import Notification
from app.models import Notification, NotificationHistory, TemplateHistory


def get_ga4_config() -> tuple:
Expand Down Expand Up @@ -54,14 +54,22 @@ def post_to_ga4(notification_id: str, event_name, event_source, event_medium) ->
current_app.logger.error('GA4_MEASUREMENT_ID is not set')
return False

# Retrieve the notification from the database
# Retrieve the notification from the database. It might have moved to history.
notification = db.session.get(Notification, notification_id)
if not notification:
current_app.logger.error('GA4: Notification %s not found', notification_id)
return False
if notification is None:
notification = db.session.get(NotificationHistory, notification_id)
if notification is None:
current_app.logger.warning('GA4: Notification %s not found', notification_id)
return False
else:
# The notification is a NotificationHistory instance.
template_id = notification.template_id
template_name = db.session.get(TemplateHistory, (template_id, notification.template_version)).name
else:
# The notification is a Notification instance.
template_id = notification.template.id
template_name = notification.template.name

template_name = notification.template.name
template_id = notification.template.id
service_id = notification.service_id
service_name = notification.service.name

Expand Down
50 changes: 41 additions & 9 deletions tests/app/celery/test_process_ga4_measurement_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@

from app.celery.exceptions import AutoRetryException
from app.celery.process_ga4_measurement_tasks import post_to_ga4
from app.models import Notification, NotificationHistory, TemplateHistory


def test_it_post_to_ga4_with_valid_data(rmock, sample_notification, ga4_sample_payload):
def post_to_ga4_with_valid_data_helper(
notification: Notification | NotificationHistory,
rmock,
ga4_sample_payload,
expected_template_id,
expected_template_name,
):
# Patch the requests.post method to return a 204 status code.
notification = sample_notification()
rmock.register_uri(
'POST', 'http://foo.bar/ga4?measurement_id=ga4_measurement_id&api_secret=ga4_api_secret', status_code=204
)
Expand All @@ -22,21 +28,47 @@ def test_it_post_to_ga4_with_valid_data(rmock, sample_notification, ga4_sample_p
assert response
assert rmock.called
assert (
rmock.request_history[0].url == 'http://foo.bar/ga4?measurement_id=ga4_measurement_id&api_secret=ga4_api_secret'
rmock.request_history[0].url == 'http://foo.bar/ga4?measurement_id=ga4_measurement_id'
'&api_secret=ga4_api_secret'
)

actual_ga4_payload = rmock.request_history[0].json()
assert actual_ga4_payload['client_id'] == ga4_sample_payload['source']
assert actual_ga4_payload['events'][0]['name'] == ga4_sample_payload['name']
assert actual_ga4_payload['events'][0]['params']['campaign_id'] == str(notification.template.id)
assert actual_ga4_payload['events'][0]['params']['campaign'] == notification.template.name
assert actual_ga4_payload['events'][0]['params']['campaign_id'] == str(expected_template_id)
assert actual_ga4_payload['events'][0]['params']['campaign'] == expected_template_name
assert actual_ga4_payload['events'][0]['params']['source'] == ga4_sample_payload['source']
assert actual_ga4_payload['events'][0]['params']['medium'] == ga4_sample_payload['medium']
assert actual_ga4_payload['events'][0]['params']['service_id'] == str(notification.service_id)
assert actual_ga4_payload['events'][0]['params']['service_name'] == notification.service.name
assert actual_ga4_payload['events'][0]['params']['notification_id'] == str(notification.id)


def test_it_post_to_ga4_returns_4xx(rmock, ga4_sample_payload):
def test_post_to_ga4_with_valid_data_in_notifications(
notify_db_session, rmock, sample_notification, ga4_sample_payload
):
notification = sample_notification()
expected_template_id = notification.template.id
expected_template_name = notification.template.name
post_to_ga4_with_valid_data_helper(
notification, rmock, ga4_sample_payload, expected_template_id, expected_template_name
)


def test_post_to_ga4_with_valid_data_in_notifications_history(
notify_db_session, rmock, sample_notification_history, ga4_sample_payload
):
notification = sample_notification_history()
expected_template_id = notification.template_id
expected_template_name = notify_db_session.session.get(
TemplateHistory, (expected_template_id, notification.template_version)
).name
post_to_ga4_with_valid_data_helper(
notification, rmock, ga4_sample_payload, expected_template_id, expected_template_name
)


def test_post_to_ga4_returns_4xx(rmock, ga4_sample_payload):
rmock.register_uri('POST', 'http://foo.bar/ga4', status_code=400)
response = post_to_ga4(
ga4_sample_payload['notification_id'],
Expand All @@ -57,7 +89,7 @@ def test_it_post_to_ga4_returns_4xx(rmock, ga4_sample_payload):
('', ''),
],
)
def test_it_post_to_ga4_missing_config(rmock, ga4_sample_payload, ga4_config):
def test_post_to_ga4_missing_config(rmock, ga4_sample_payload, ga4_config):
with patch('app.celery.process_ga4_measurement_tasks.get_ga4_config') as mock_get_ga4_config:
mock_get_ga4_config.return_value = ga4_config

Expand All @@ -80,7 +112,7 @@ def test_it_post_to_ga4_missing_config(rmock, ga4_sample_payload, ga4_config):
requests.HTTPError,
],
)
def test_it_post_to_ga4_exception(rmock, sample_notification, ga4_sample_payload, mock_exception):
def test_post_to_ga4_exception(rmock, sample_notification, ga4_sample_payload, mock_exception):
notification = sample_notification()
rmock.register_uri('POST', 'http://foo.bar/ga4', exc=mock_exception)
with pytest.raises(AutoRetryException):
Expand All @@ -92,7 +124,7 @@ def test_it_post_to_ga4_exception(rmock, sample_notification, ga4_sample_payload
)


def test_it_post_to_ga4_does_not_retry_unhandled_exception(rmock, ga4_sample_payload):
def test_post_to_ga4_does_not_retry_unhandled_exception(rmock, ga4_sample_payload):
rmock.register_uri('POST', 'http://foo.bar/ga4', exc=Exception)
response = post_to_ga4(
ga4_sample_payload['notification_id'],
Expand Down

0 comments on commit 10fce57

Please sign in to comment.