summaryrefslogtreecommitdiff
path: root/addons/test_mail/tests/common.py
diff options
context:
space:
mode:
authorThibault Delavallée <tde@odoo.com>2019-05-21 10:40:09 +0000
committerThibault Delavallée <tde@odoo.com>2019-07-02 10:44:56 +0000
commit20c10fcd19b67cc70b64be0ee81f0582e67a1eba (patch)
tree8f842902df543a10b5191dbda255a6efe9df3f60 /addons/test_mail/tests/common.py
parentdb177636540e632010bf5eebcd314b4c2f6960d3 (diff)
[ADD] sms, test_mail(_full): add basic SMS tests and improve test tools
Purpose of this commit is to improve and add tools, methods and data to test mail and SMS features. We also add tests for current implementation of SMS feature, allowing to better understand future changes. [ADD] test_mail_full * have a module depending on mail sub-applications like sms or snailmail. Its purpose is to check that standard mail features work effectively with all overrides and extra behaviors activated; * add a new model specific for SMS gateway, with default recipient computation; * add SMS tests for SMS module adding SMS capabilities linked to mail feature. This commit tests SMS feature before the upcoming refactoring and improvement of SMS module in community: posting with SMS and sms composer usage; In test_mail * add necessary mobile information on test partners; * improve assertBusNotification that was not correctly asserting all items in message of bus notifications; In sms * add mock for SMS sending. Purpose is to mock the connection to IAP services by mocking the call to IAP server. It allows to perform SMS tests without having to contact (and pay) for this service; * allow some customization when calling the SMS gateway mock to simulate errors and test corner cases; Related to task 1922163 Linked to PR #34516
Diffstat (limited to 'addons/test_mail/tests/common.py')
-rw-r--r--addons/test_mail/tests/common.py88
1 files changed, 58 insertions, 30 deletions
diff --git a/addons/test_mail/tests/common.py b/addons/test_mail/tests/common.py
index fd6ec9ce627..7df3aa0c96a 100644
--- a/addons/test_mail/tests/common.py
+++ b/addons/test_mail/tests/common.py
@@ -37,6 +37,17 @@ class BaseFunctionalTest(common.SavepointCase):
cls.test_record = cls.env['mail.test.simple'].with_context(cls._test_context).create({'name': 'Test', 'email_from': 'ignasse@example.com'})
+ @classmethod
+ def _reset_mail_context(cls, record):
+ return record.with_context(
+ mail_create_nolog=False,
+ mail_create_nosubscribe=False,
+ mail_notrack=False
+ )
+
+ def _clear_bus(self):
+ self.env['bus.bus'].search([]).unlink()
+
@contextmanager
def assertNotifications(self, **counters):
""" Counters: 'partner_attribute': 'inbox' or 'email' """
@@ -83,41 +94,57 @@ class BaseFunctionalTest(common.SavepointCase):
if hasattr(self, 'assertEmails') and len(new_messages) == 1:
self.assertEmails(new_messages.author_id, new_notifications.filtered(lambda n: n.is_email).mapped('res_partner_id'))
- def assertBusNotification(self, channels, message_dicts=None, init=True):
+ def assertBusNotification(self, channels, message_items=None, init=True):
""" Check for bus notifications. Basic check is about used channels.
Verifying content is optional.
:param channels: list of channel
- :param messages: if given, list of message making a valid pair (channel,
+ :param message_items: if given, list of message making a valid pair (channel,
message) to be found in bus.bus
"""
+ def check_content(returned_value, expected_value):
+ if isinstance(expected_value, list):
+ done = []
+ for expected_item in expected_value:
+ for returned_item in returned_value:
+ if check_content(returned_item, expected_item):
+ done.append(expected_item)
+ break
+ else:
+ return False
+ return len(done) == len(expected_value)
+ elif isinstance(expected_value, dict):
+ return all(k in returned_value for k in expected_value.keys()) and all(
+ check_content(returned_value[key], val)
+ for key, val in expected_value.items()
+ )
+ else:
+ return returned_value == expected_value
+
if init:
self.assertEqual(len(self.env['bus.bus'].search([])), len(channels))
notifications = self.env['bus.bus'].search([('channel', 'in', [json_dump(channel) for channel in channels])])
+ notif_messages = [json.loads(n.message) for n in notifications]
self.assertEqual(len(notifications), len(channels))
- if message_dicts:
- notif_messages = [json.loads(n.message) for n in notifications]
- for expected in message_dicts:
- found = False
- for returned in notif_messages:
- for key, val in expected.items():
- if key not in returned:
- continue
- if isinstance(returned[key], list):
- if set(returned[key]) != set(val):
- continue
- else:
- if returned[key] != val:
- continue
- found = True
- break
- if found:
- break
- if not found:
- raise AssertionError("Bus notification content %s not found" % (repr(expected)))
+
+ for expected in message_items or []:
+ for notification in notif_messages:
+ found_keys, not_found_keys = [], []
+ if not all(k in notification for k in expected.keys()):
+ continue
+ for expected_key, expected_value in expected.items():
+ done = check_content(notification[expected_key], expected_value)
+ if done:
+ found_keys.append(expected_key)
+ else:
+ not_found_keys.append(expected_key)
+ if set(found_keys) == set(expected.keys()):
+ break
+ else:
+ raise AssertionError('Keys %s not found (expected: %s - returned: %s)' % (not_found_keys, repr(expected), repr(notif_messages)))
@contextmanager
- def sudoAs(self, login):
+ def sudo(self, login):
old_uid = self.uid
try:
user = self.env['res.users'].sudo().search([('login', '=', login)])
@@ -125,13 +152,11 @@ class BaseFunctionalTest(common.SavepointCase):
# switch user
self.uid = user.id
self.env = self.env(user=self.uid)
- self.test_record = self.test_record.sudo(self.uid)
yield
finally:
# back
self.uid = old_uid
self.env = self.env(user=self.uid)
- self.test_record = self.test_record_old
class TestRecipients(common.SavepointCase):
@@ -147,10 +172,16 @@ class TestRecipients(common.SavepointCase):
})
cls.partner_1 = Partner.create({
'name': 'Valid Lelitre',
- 'email': 'valid.lelitre@agrolait.com'})
+ 'email': 'valid.lelitre@agrolait.com',
+ 'country_id': cls.env.ref('base.be').id,
+ 'mobile': '0475001122',
+ })
cls.partner_2 = Partner.create({
'name': 'Valid Poilvache',
- 'email': 'valid.other@gmail.com'})
+ 'email': 'valid.other@gmail.com',
+ 'country_id': cls.env.ref('base.be').id,
+ 'mobile': '+32 475 22 11 00',
+ })
class MockEmails(common.SingleTransactionCase):
@@ -308,6 +339,3 @@ class Moderation(MockEmails, BaseFunctionalTest):
'subtype_id': self.env['mail.message.subtype'].search([('name', '=', 'Discussions')]).id
})
return message
-
- def _clear_bus(self):
- self.env['bus.bus'].search([]).unlink()