|
@ -5,12 +5,12 @@ |
|
|
import mock |
|
|
import mock |
|
|
import base64 |
|
|
import base64 |
|
|
import time |
|
|
import time |
|
|
from openerp.tests.common import TransactionCase |
|
|
|
|
|
|
|
|
from odoo import http |
|
|
|
|
|
from odoo.tests.common import TransactionCase |
|
|
from ..controllers.main import MailTrackingController, BLANK |
|
|
from ..controllers.main import MailTrackingController, BLANK |
|
|
|
|
|
|
|
|
mock_request = 'openerp.http.request' |
|
|
|
|
|
mock_send_email = ('openerp.addons.base.ir.ir_mail_server.' |
|
|
|
|
|
'ir_mail_server.send_email') |
|
|
|
|
|
|
|
|
mock_send_email = ('odoo.addons.base.ir.ir_mail_server.' |
|
|
|
|
|
'IrMailServer.send_email') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class FakeUserAgent(object): |
|
|
class FakeUserAgent(object): |
|
@ -22,11 +22,9 @@ class FakeUserAgent(object): |
|
|
return 'Test suite' |
|
|
return 'Test suite' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# One test case per method |
|
|
|
|
|
class TestMailTracking(TransactionCase): |
|
|
class TestMailTracking(TransactionCase): |
|
|
# Use case : Prepare some data for current test case |
|
|
|
|
|
def setUp(self): |
|
|
|
|
|
super(TestMailTracking, self).setUp() |
|
|
|
|
|
|
|
|
def setUp(self, *args, **kwargs): |
|
|
|
|
|
super(TestMailTracking, self).setUp(*args, **kwargs) |
|
|
self.sender = self.env['res.partner'].create({ |
|
|
self.sender = self.env['res.partner'].create({ |
|
|
'name': 'Test sender', |
|
|
'name': 'Test sender', |
|
|
'email': 'sender@example.com', |
|
|
'email': 'sender@example.com', |
|
@ -37,12 +35,22 @@ class TestMailTracking(TransactionCase): |
|
|
'email': 'recipient@example.com', |
|
|
'email': 'recipient@example.com', |
|
|
'notify_email': 'always', |
|
|
'notify_email': 'always', |
|
|
}) |
|
|
}) |
|
|
self.request = { |
|
|
|
|
|
|
|
|
self.last_request = http.request |
|
|
|
|
|
http.request = type('obj', (object,), { |
|
|
|
|
|
'db': self.env.cr.dbname, |
|
|
|
|
|
'env': self.env, |
|
|
|
|
|
'endpoint': type('obj', (object,), { |
|
|
|
|
|
'routing': [], |
|
|
|
|
|
}), |
|
|
'httprequest': type('obj', (object,), { |
|
|
'httprequest': type('obj', (object,), { |
|
|
'remote_addr': '123.123.123.123', |
|
|
'remote_addr': '123.123.123.123', |
|
|
'user_agent': FakeUserAgent(), |
|
|
'user_agent': FakeUserAgent(), |
|
|
}), |
|
|
}), |
|
|
} |
|
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
def tearDown(self, *args, **kwargs): |
|
|
|
|
|
http.request = self.last_request |
|
|
|
|
|
return super(TestMailTracking, self).tearDown(*args, **kwargs) |
|
|
|
|
|
|
|
|
def test_message_post(self): |
|
|
def test_message_post(self): |
|
|
# This message will generate a notification for recipient |
|
|
# This message will generate a notification for recipient |
|
@ -50,7 +58,7 @@ class TestMailTracking(TransactionCase): |
|
|
'subject': 'Message test', |
|
|
'subject': 'Message test', |
|
|
'author_id': self.sender.id, |
|
|
'author_id': self.sender.id, |
|
|
'email_from': self.sender.email, |
|
|
'email_from': self.sender.email, |
|
|
'type': 'comment', |
|
|
|
|
|
|
|
|
'message_type': 'comment', |
|
|
'model': 'res.partner', |
|
|
'model': 'res.partner', |
|
|
'res_id': self.recipient.id, |
|
|
'res_id': self.recipient.id, |
|
|
'partner_ids': [(4, self.recipient.id)], |
|
|
'partner_ids': [(4, self.recipient.id)], |
|
@ -65,9 +73,7 @@ class TestMailTracking(TransactionCase): |
|
|
self.assertTrue(tracking_email) |
|
|
self.assertTrue(tracking_email) |
|
|
self.assertEqual(tracking_email.state, 'sent') |
|
|
self.assertEqual(tracking_email.state, 'sent') |
|
|
# message_dict read by web interface |
|
|
# message_dict read by web interface |
|
|
message_dict = message.message_read() |
|
|
|
|
|
# First item in threads is message content |
|
|
|
|
|
message_dict = message_dict['threads'][0][0] |
|
|
|
|
|
|
|
|
message_dict = message.message_format()[0] |
|
|
self.assertTrue(len(message_dict['partner_ids']) > 0) |
|
|
self.assertTrue(len(message_dict['partner_ids']) > 0) |
|
|
# First partner is recipient |
|
|
# First partner is recipient |
|
|
partner_id = message_dict['partner_ids'][0][0] |
|
|
partner_id = message_dict['partner_ids'][0][0] |
|
@ -110,10 +116,15 @@ class TestMailTracking(TransactionCase): |
|
|
mail, tracking = self.mail_send(self.recipient.email) |
|
|
mail, tracking = self.mail_send(self.recipient.email) |
|
|
self.assertEqual(mail.email_to, tracking.recipient) |
|
|
self.assertEqual(mail.email_to, tracking.recipient) |
|
|
self.assertEqual(mail.email_from, tracking.sender) |
|
|
self.assertEqual(mail.email_from, tracking.sender) |
|
|
with mock.patch(mock_request) as mock_func: |
|
|
|
|
|
mock_func.return_value = type('obj', (object,), self.request) |
|
|
|
|
|
res = controller.mail_tracking_open(db, tracking.id) |
|
|
res = controller.mail_tracking_open(db, tracking.id) |
|
|
self.assertEqual(image, res.response[0]) |
|
|
self.assertEqual(image, res.response[0]) |
|
|
|
|
|
# Two events: sent and open |
|
|
|
|
|
self.assertEqual(2, len(tracking.tracking_event_ids)) |
|
|
|
|
|
# Fake event: tracking_email_id = False |
|
|
|
|
|
res = controller.mail_tracking_open(db, False) |
|
|
|
|
|
self.assertEqual(image, res.response[0]) |
|
|
|
|
|
# Two events again because no tracking_email_id found for False |
|
|
|
|
|
self.assertEqual(2, len(tracking.tracking_event_ids)) |
|
|
|
|
|
|
|
|
def test_concurrent_open(self): |
|
|
def test_concurrent_open(self): |
|
|
mail, tracking = self.mail_send(self.recipient.email) |
|
|
mail, tracking = self.mail_send(self.recipient.email) |
|
@ -193,31 +204,38 @@ class TestMailTracking(TransactionCase): |
|
|
self.assertEqual('error', tracking.state) |
|
|
self.assertEqual('error', tracking.state) |
|
|
self.assertEqual('Warning', tracking.error_type) |
|
|
self.assertEqual('Warning', tracking.error_type) |
|
|
self.assertEqual('Test error', tracking.error_description) |
|
|
self.assertEqual('Test error', tracking.error_description) |
|
|
|
|
|
self.assertTrue(self.recipient.email_bounced) |
|
|
|
|
|
|
|
|
def test_partner_email_change(self): |
|
|
def test_partner_email_change(self): |
|
|
mail, tracking = self.mail_send(self.recipient.email) |
|
|
mail, tracking = self.mail_send(self.recipient.email) |
|
|
tracking.event_create('open', {}) |
|
|
tracking.event_create('open', {}) |
|
|
orig_score = self.recipient.email_score |
|
|
orig_score = self.recipient.email_score |
|
|
|
|
|
orig_count = self.recipient.tracking_emails_count |
|
|
orig_email = self.recipient.email |
|
|
orig_email = self.recipient.email |
|
|
self.recipient.email = orig_email + '2' |
|
|
self.recipient.email = orig_email + '2' |
|
|
self.assertEqual(50.0, self.recipient.email_score) |
|
|
self.assertEqual(50.0, self.recipient.email_score) |
|
|
|
|
|
self.assertEqual(0, self.recipient.tracking_emails_count) |
|
|
self.recipient.email = orig_email |
|
|
self.recipient.email = orig_email |
|
|
self.assertEqual(orig_score, self.recipient.email_score) |
|
|
self.assertEqual(orig_score, self.recipient.email_score) |
|
|
|
|
|
self.assertEqual(orig_count, self.recipient.tracking_emails_count) |
|
|
|
|
|
|
|
|
def test_process_hard_bounce(self): |
|
|
def test_process_hard_bounce(self): |
|
|
mail, tracking = self.mail_send(self.recipient.email) |
|
|
mail, tracking = self.mail_send(self.recipient.email) |
|
|
tracking.event_create('hard_bounce', {}) |
|
|
tracking.event_create('hard_bounce', {}) |
|
|
self.assertEqual('bounced', tracking.state) |
|
|
self.assertEqual('bounced', tracking.state) |
|
|
|
|
|
self.assertTrue(self.recipient.email_score < 50.0) |
|
|
|
|
|
|
|
|
def test_process_soft_bounce(self): |
|
|
def test_process_soft_bounce(self): |
|
|
mail, tracking = self.mail_send(self.recipient.email) |
|
|
mail, tracking = self.mail_send(self.recipient.email) |
|
|
tracking.event_create('soft_bounce', {}) |
|
|
tracking.event_create('soft_bounce', {}) |
|
|
self.assertEqual('soft-bounced', tracking.state) |
|
|
self.assertEqual('soft-bounced', tracking.state) |
|
|
|
|
|
self.assertTrue(self.recipient.email_score < 50.0) |
|
|
|
|
|
|
|
|
def test_process_delivered(self): |
|
|
def test_process_delivered(self): |
|
|
mail, tracking = self.mail_send(self.recipient.email) |
|
|
mail, tracking = self.mail_send(self.recipient.email) |
|
|
tracking.event_create('delivered', {}) |
|
|
tracking.event_create('delivered', {}) |
|
|
self.assertEqual('delivered', tracking.state) |
|
|
self.assertEqual('delivered', tracking.state) |
|
|
|
|
|
self.assertTrue(self.recipient.email_score > 50.0) |
|
|
|
|
|
|
|
|
def test_process_deferral(self): |
|
|
def test_process_deferral(self): |
|
|
mail, tracking = self.mail_send(self.recipient.email) |
|
|
mail, tracking = self.mail_send(self.recipient.email) |
|
@ -228,32 +246,42 @@ class TestMailTracking(TransactionCase): |
|
|
mail, tracking = self.mail_send(self.recipient.email) |
|
|
mail, tracking = self.mail_send(self.recipient.email) |
|
|
tracking.event_create('spam', {}) |
|
|
tracking.event_create('spam', {}) |
|
|
self.assertEqual('spam', tracking.state) |
|
|
self.assertEqual('spam', tracking.state) |
|
|
|
|
|
self.assertTrue(self.recipient.email_score < 50.0) |
|
|
|
|
|
|
|
|
def test_process_unsub(self): |
|
|
def test_process_unsub(self): |
|
|
mail, tracking = self.mail_send(self.recipient.email) |
|
|
mail, tracking = self.mail_send(self.recipient.email) |
|
|
tracking.event_create('unsub', {}) |
|
|
tracking.event_create('unsub', {}) |
|
|
self.assertEqual('unsub', tracking.state) |
|
|
self.assertEqual('unsub', tracking.state) |
|
|
|
|
|
self.assertTrue(self.recipient.email_score < 50.0) |
|
|
|
|
|
|
|
|
def test_process_reject(self): |
|
|
def test_process_reject(self): |
|
|
mail, tracking = self.mail_send(self.recipient.email) |
|
|
mail, tracking = self.mail_send(self.recipient.email) |
|
|
tracking.event_create('reject', {}) |
|
|
tracking.event_create('reject', {}) |
|
|
self.assertEqual('rejected', tracking.state) |
|
|
self.assertEqual('rejected', tracking.state) |
|
|
|
|
|
self.assertTrue(self.recipient.email_score < 50.0) |
|
|
|
|
|
|
|
|
def test_process_open(self): |
|
|
def test_process_open(self): |
|
|
mail, tracking = self.mail_send(self.recipient.email) |
|
|
mail, tracking = self.mail_send(self.recipient.email) |
|
|
tracking.event_create('open', {}) |
|
|
tracking.event_create('open', {}) |
|
|
self.assertEqual('opened', tracking.state) |
|
|
self.assertEqual('opened', tracking.state) |
|
|
|
|
|
self.assertTrue(self.recipient.email_score > 50.0) |
|
|
|
|
|
|
|
|
def test_process_click(self): |
|
|
def test_process_click(self): |
|
|
mail, tracking = self.mail_send(self.recipient.email) |
|
|
mail, tracking = self.mail_send(self.recipient.email) |
|
|
tracking.event_create('click', {}) |
|
|
tracking.event_create('click', {}) |
|
|
self.assertEqual('opened', tracking.state) |
|
|
self.assertEqual('opened', tracking.state) |
|
|
|
|
|
self.assertTrue(self.recipient.email_score > 50.0) |
|
|
|
|
|
|
|
|
|
|
|
def test_process_several_bounce(self): |
|
|
|
|
|
for i in range(1, 10): |
|
|
|
|
|
mail, tracking = self.mail_send(self.recipient.email) |
|
|
|
|
|
tracking.event_create('hard_bounce', {}) |
|
|
|
|
|
self.assertEqual('bounced', tracking.state) |
|
|
|
|
|
self.assertEqual(0.0, self.recipient.email_score) |
|
|
|
|
|
|
|
|
def test_db(self): |
|
|
def test_db(self): |
|
|
db = self.env.cr.dbname |
|
|
db = self.env.cr.dbname |
|
|
controller = MailTrackingController() |
|
|
controller = MailTrackingController() |
|
|
with mock.patch(mock_request) as mock_func: |
|
|
|
|
|
mock_func.return_value = type('obj', (object,), self.request) |
|
|
|
|
|
not_found = controller.mail_tracking_all('not_found_db') |
|
|
not_found = controller.mail_tracking_all('not_found_db') |
|
|
self.assertEqual('NOT FOUND', not_found.response[0]) |
|
|
self.assertEqual('NOT FOUND', not_found.response[0]) |
|
|
none = controller.mail_tracking_all(db) |
|
|
none = controller.mail_tracking_all(db) |
|
|