Skip to content

Commit

Permalink
[FIX] mail_tracking_mailgun: discard non Odoo events
Browse files Browse the repository at this point in the history
When we use the same Mailgun domain for other services, the email events
for those services we'll be pushed to the Odoo controller as well. We
want to discard them as they're useless to us.

Aside from that, in the case a wrong db is called to the controller, we
better logging the failed request is going to be more useful than
raising an error.
  • Loading branch information
chienandalu committed Jul 11, 2024
1 parent 165ab96 commit b58238d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
12 changes: 11 additions & 1 deletion mail_tracking_mailgun/models/mail_tracking_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,18 @@ def _mailgun_event_process(self, event_data, metadata):
In https://documentation.mailgun.com/en/latest/api-events.html#event-structure
you can read the event payload format as obtained from webhooks or calls to API.
"""
# Just ignore these events, as they will be from another system using the same
# smtp domain
if "odoo_db" not in event_data["user-variables"]:
_logger.debug(f"Mailgun: dropping not Odoo event: {event_data}")
return
# Don't fail too hard, just drop and log the issue
if event_data["user-variables"]["odoo_db"] != self.env.cr.dbname:
raise ValidationError(_("Wrong database for event!"))
_logger.error(
f"Mailgun: event for DB {event_data['user-variables']['odoo_db']} "
f"received in DB {self.env.cr.dbname}: {event_data}"
)
return
# Do nothing if event was already processed
mailgun_id = event_data["id"]
db_event = self.env["mail.tracking.event"].search(
Expand Down
14 changes: 12 additions & 2 deletions mail_tracking_mailgun/tests/test_mailgun.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,21 @@ def test_tracking_not_found(self):
with self._request_mock(), self.assertRaises(MissingError):
self.MailTrackingController.mail_tracking_mailgun_webhook()

@mute_logger("odoo.addons.mail_tracking_mailgun.models.mail_tracking_email")
def test_tracking_wrong_db(self):
self.event["user-variables"]["odoo_db"] = "%s_nope" % self.env.cr.dbname
with self._request_mock(), self.assertRaises(ValidationError):
with self._request_mock(), self.assertLogs(level="ERROR") as log_catcher:
self.MailTrackingController.mail_tracking_mailgun_webhook()
self.assertIn(
f"Mailgun: event for DB {self.env.cr.dbname}_nope "
f"received in DB {self.env.cr.dbname}",
log_catcher.output[0],
)

def test_tracking_not_odoo_event(self):
self.event["user-variables"].pop("odoo_db")
with self._request_mock(), self.assertLogs(level="DEBUG") as log_catcher:
self.MailTrackingController.mail_tracking_mailgun_webhook()
self.assertIn("Mailgun: dropping not Odoo event", log_catcher.output[-1:][0])

# https://documentation.mailgun.com/en/latest/user_manual.html#tracking-deliveries
def test_event_delivered(self):
Expand Down

0 comments on commit b58238d

Please sign in to comment.