Browse Source

[FIX] mail_tracking_mailgun: json.load() won't swallow bytes (#269)

- json.load() in python under 3.6 doesn't support binary input.
- https://docs.python.org/3/whatsnew/3.6.html#json
- This way, we let requests to decode the response itself.
pull/270/head
David Vidal 7 years ago
committed by Pedro M. Baeza
parent
commit
9d5d1bcbbc
  1. 2
      mail_tracking_mailgun/__manifest__.py
  2. 5
      mail_tracking_mailgun/models/mail_tracking_email.py
  3. 3
      mail_tracking_mailgun/models/res_partner.py
  4. 25
      mail_tracking_mailgun/tests/test_mailgun.py

2
mail_tracking_mailgun/__manifest__.py

@ -6,7 +6,7 @@
{
"name": "Mail tracking for Mailgun",
"summary": "Mail tracking and Mailgun webhooks integration",
"version": "11.0.1.0.0",
"version": "11.0.1.0.1",
"category": "Social Network",
"website": "https://github.com/OCA/social",
"author": "Tecnativa, "

5
mail_tracking_mailgun/models/mail_tracking_email.py

@ -5,7 +5,6 @@
import hashlib
import hmac
import json
import requests
from datetime import datetime
from odoo import _, api, fields, models
@ -115,7 +114,7 @@ class MailTrackingEmail(models.Model):
ts = event.get('timestamp', False)
try:
ts = float(ts)
except:
except Exception:
ts = False
if ts:
dt = datetime.utcfromtimestamp(ts)
@ -238,7 +237,7 @@ class MailTrackingEmail(models.Model):
if not res or res.status_code != 200:
raise ValidationError(_(
"Couldn't retrieve Mailgun information"))
content = json.loads(res.content)
content = res.json()
if "items" not in content:
raise ValidationError(_("Event information not longer stored"))
for item in content["items"]:

3
mail_tracking_mailgun/models/res_partner.py

@ -6,7 +6,6 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
import requests
import json
from odoo import _, api, models
from odoo.exceptions import UserError
@ -57,7 +56,7 @@ class ResPartner(models.Model):
raise UserError(_(
'Error %s trying to '
'check mail' % res.status_code or 'of connection'))
content = json.loads(res.content)
content = res.json()
if 'mailbox_verification' not in content:
if not self.env.context.get('mailgun_auto_check'):
raise UserError(

25
mail_tracking_mailgun/tests/test_mailgun.py

@ -7,7 +7,6 @@ from odoo.tools import mute_logger
from odoo.tests.common import TransactionCase
from odoo.exceptions import UserError, ValidationError
import mock
import json
_packagepath = 'odoo.addons.mail_tracking_mailgun'
@ -336,10 +335,10 @@ class TestMailgun(TransactionCase):
self.partner.email_bounced = False
mock_request.get.return_value.apparent_encoding = 'ascii'
mock_request.get.return_value.status_code = 200
mock_request.get.return_value.content = json.dumps({
mock_request.get.return_value.json.return_value = {
'is_valid': True,
'mailbox_verification': 'true',
}, ensure_ascii=True)
}
# Trigger email auto validation in partner
self.env['ir.config_parameter'].set_param(
'mailgun.auto_check_partner_email', 'True')
@ -347,24 +346,24 @@ class TestMailgun(TransactionCase):
self.assertFalse(self.partner.email_bounced)
self.partner.email = 'xoxoxoxo@tecnativa.com'
# Not a valid mailbox
mock_request.get.return_value.content = json.dumps({
mock_request.get.return_value.json.return_value = {
'is_valid': True,
'mailbox_verification': 'false',
}, ensure_ascii=True)
}
with self.assertRaises(UserError):
self.partner.check_email_validity()
# Not a valid mail address
mock_request.get.return_value.content = json.dumps({
mock_request.get.return_value.json.return_value = {
'is_valid': False,
'mailbox_verification': 'false',
}, ensure_ascii=True)
}
with self.assertRaises(UserError):
self.partner.check_email_validity()
# Unable to fully validate
mock_request.get.return_value.content = json.dumps({
mock_request.get.return_value.json.return_value = {
'is_valid': True,
'mailbox_verification': 'unknown',
}, ensure_ascii=True)
}
with self.assertRaises(UserError):
self.partner.check_email_validity()
self.assertTrue(self.partner.email_bounced)
@ -402,9 +401,7 @@ class TestMailgun(TransactionCase):
@mock.patch(_packagepath + '.models.mail_tracking_email.requests')
def test_manual_check(self, mock_request):
mock_request.get.return_value.content = json.dumps(self.response,
ensure_ascii=True)
mock_request.get.return_value.apparent_encoding = 'ascii'
mock_request.get.return_value.json.return_value = self.response
mock_request.get.return_value.status_code = 200
self.tracking_email.action_manual_check_mailgun()
event = self.env['mail.tracking.event'].search(
@ -417,8 +414,6 @@ class TestMailgun(TransactionCase):
with self.assertRaises(ValidationError):
self.tracking_email.action_manual_check_mailgun()
mock_request.get.return_value.status_code = 200
mock_request.get.return_value.content = json.dumps('{}',
ensure_ascii=True)
mock_request.get.return_value.apparent_encoding = 'ascii'
mock_request.get.return_value.json.return_value = {}
with self.assertRaises(ValidationError):
self.tracking_email.action_manual_check_mailgun()
Loading…
Cancel
Save