changeset 7c88d5832e1a in modules/notification_email:default
details:
https://hg.tryton.org/modules/notification_email?cmd=changeset&node=7c88d5832e1a
description:
Replace test setuptools command by unittest discover
issue9215
review389851002
diffstat:
setup.py | 6 +-
tests/__init__.py | 8 -
tests/test_module.py | 406 ++++++++++++++++++++++++++++++++++++++
tests/test_notification_email.py | 412 ---------------------------------------
tox.ini | 3 +-
5 files changed, 411 insertions(+), 424 deletions(-)
diffs (874 lines):
diff -r b8937caac8f7 -r 7c88d5832e1a setup.py
--- a/setup.py Mon Apr 11 23:24:21 2022 +0200
+++ b/setup.py Sat Apr 16 18:30:17 2022 +0200
@@ -141,13 +141,13 @@
license='GPL-3',
python_requires='>=3.7',
install_requires=requires,
+ extras_require={
+ 'test': tests_require,
+ },
dependency_links=dependency_links,
zip_safe=False,
entry_points="""
[trytond.modules]
notification_email = trytond.modules.notification_email
""",
- test_suite='tests',
- test_loader='trytond.test_loader:Loader',
- tests_require=tests_require,
)
diff -r b8937caac8f7 -r 7c88d5832e1a tests/__init__.py
--- a/tests/__init__.py Mon Apr 11 23:24:21 2022 +0200
+++ b/tests/__init__.py Sat Apr 16 18:30:17 2022 +0200
@@ -1,10 +1,2 @@
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
-
-try:
- from trytond.modules.notification_email.tests.test_notification_email
import (
- suite)
-except ImportError:
- from .test_notification_email import suite
-
-__all__ = ['suite']
diff -r b8937caac8f7 -r 7c88d5832e1a tests/test_module.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test_module.py Sat Apr 16 18:30:17 2022 +0200
@@ -0,0 +1,406 @@
+# This file is part of Tryton. The COPYRIGHT file at the top level of
+# this repository contains the full copyright notices and license terms.
+import datetime as dt
+import sys
+import unittest
+from unittest.mock import ANY, patch
+
+from trytond.config import config
+
+try:
+ from trytond.modules.company.tests import CompanyTestMixin
+except ImportError:
+ class CompanyTestMixin:
+ pass
+from trytond.modules.notification_email import \
+ notification as notification_module
+from trytond.pool import Pool
+from trytond.tests.test_tryton import ModuleTestCase, with_transaction
+from trytond.transaction import Transaction
+
+FROM = '[email protected]'
+
+
+class NotificationEmailTestCase(CompanyTestMixin, ModuleTestCase):
+ "Test Notification Email module"
+ module = 'notification_email'
+ extras = ['company', 'commission', 'party', 'web_user']
+
+ def setUp(self):
+ super(NotificationEmailTestCase, self).setUp()
+ reset_from = config.get('email', 'from', default='')
+ config.set('email', 'from', FROM)
+ self.addCleanup(lambda: config.set('email', 'from', reset_from))
+
+ def _setup_notification(self, recipient_field='create_uid'):
+ pool = Pool()
+ Model = pool.get('ir.model')
+ ModelField = pool.get('ir.model.field')
+ Action = pool.get('ir.action')
+ Report = pool.get('ir.action.report')
+ User = pool.get('res.user')
+ NotificationEmail = pool.get('notification.email')
+
+ model, = Model.search([
+ ('model', '=', User.__name__),
+ ])
+
+ action = Action(name="Notification Email", type='ir.action.report')
+ action.save()
+ report = Report()
+ report.report_name = 'notification_notification.test.report'
+ report.action = action
+ report.template_extension = 'txt'
+ report.report_content = b'Hello ${records[0].name}'
+ report.model = model.model
+ report.save()
+
+ user = User(Transaction().user)
+ user.email = '[email protected]'
+ user.save()
+
+ notification_email = NotificationEmail()
+ notification_email.recipients, = ModelField.search([
+ ('model.model', '=', model.model),
+ ('name', '=', recipient_field),
+ ])
+ notification_email.content = report
+ notification_email.save()
+
+ def run_tasks(self, count=None):
+ pool = Pool()
+ Queue = pool.get('ir.queue')
+ transaction = Transaction()
+ self.assertTrue(transaction.tasks)
+ i = 0
+ while transaction.tasks:
+ task = Queue(transaction.tasks.pop())
+ task.run()
+ i += 1
+ if count is not None:
+ if i >= count:
+ break
+
+ @unittest.skipIf(
+ (3, 5, 0) <= sys.version_info < (3, 5, 2), "python bug #25195")
+ @with_transaction()
+ def test_notification_email(self):
+ "Test email notificiation is sent on trigger"
+ pool = Pool()
+ User = pool.get('res.user')
+ Trigger = pool.get('ir.trigger')
+ Model = pool.get('ir.model')
+ NotificationEmail = pool.get('notification.email')
+ Log = pool.get('notification.email.log')
+
+ self._setup_notification()
+ notification_email, = NotificationEmail.search([])
+
+ model, = Model.search([
+ ('model', '=', User.__name__),
+ ])
+ Trigger.create([{
+ 'name': 'Test creation',
+ 'model': model.id,
+ 'on_create': True,
+ 'condition': 'true',
+ 'notification_email': notification_email.id,
+ 'action': 'notification.email|trigger',
+ }])
+
+ with patch.object(
+ notification_module, 'sendmail_transactional') as sendmail, \
+ patch.object(notification_module, 'SMTPDataManager'):
+ User.create([{'name': "Michael Scott", 'login': "msc"}])
+ self.run_tasks()
+ sendmail.assert_called_once_with(
+ FROM, ['[email protected]'], ANY,
+ datamanager=ANY)
+ _, _, msg = sendmail.call_args[0]
+ self.assertEqual(msg['From'], FROM)
+ self.assertEqual(msg['Subject'], 'Notification Email')
+ self.assertEqual(msg['To'], 'Administrator <[email protected]>')
+ self.assertEqual(msg['Auto-Submitted'], 'auto-generated')
+ self.assertEqual(msg.get_content_type(), 'multipart/alternative')
+ self.assertEqual(
+ msg.get_payload(0).get_payload(), 'Hello Michael Scott')
+
+ log, = Log.search([])
+ self.assertEqual(log.trigger.notification_email, notification_email)
+ self.assertEqual(log.recipients, 'Administrator <[email protected]>')
+ self.assertEqual(log.recipients_secondary, '')
+ self.assertEqual(log.recipients_hidden, '')
+
+ @unittest.skipIf(
+ (3, 5, 0) <= sys.version_info < (3, 5, 2), "python bug #25195")
+ @with_transaction()
+ def test_notification_email_id_recipient(self):
+ "Test email notificiation is sent when using id as recipient"
+ pool = Pool()
+ User = pool.get('res.user')
+ Trigger = pool.get('ir.trigger')
+ Model = pool.get('ir.model')
+ NotificationEmail = pool.get('notification.email')
+ Log = pool.get('notification.email.log')
+
+ self._setup_notification(recipient_field='id')
+ notification_email, = NotificationEmail.search([])
+
+ model, = Model.search([
+ ('model', '=', User.__name__),
+ ])
+ Trigger.create([{
+ 'name': 'Test creation',
+ 'model': model.id,
+ 'on_create': True,
+ 'condition': 'true',
+ 'notification_email': notification_email.id,
+ 'action': 'notification.email|trigger',
+ }])
+
+ with patch.object(
+ notification_module, 'sendmail_transactional') as sendmail, \
+ patch.object(notification_module, 'SMTPDataManager'):
+ User.create([{
+ 'name': "Michael Scott",
+ 'login': "msc",
+ 'email': '[email protected]'}])
+ self.run_tasks()
+ sendmail.assert_called_once_with(
+ FROM, ['[email protected]'], ANY,
+ datamanager=ANY)
+
+ log, = Log.search([])
+ self.assertEqual(log.trigger.notification_email, notification_email)
+ self.assertEqual(log.recipients, 'Michael Scott <[email protected]>')
+
+ @with_transaction()
+ def test_notification_email_delay(self):
+ "Test email notification is sent with delay"
+ pool = Pool()
+ User = pool.get('res.user')
+ Trigger = pool.get('ir.trigger')
+ Model = pool.get('ir.model')
+ NotificationEmail = pool.get('notification.email')
+
+ self._setup_notification()
+ notification_email, = NotificationEmail.search([])
+ notification_email.send_after = dt.timedelta(minutes=5)
+ notification_email.save()
+
+ model, = Model.search([
+ ('model', '=', User.__name__),
+ ])
+ Trigger.create([{
+ 'name': 'Test creation',
+ 'model': model.id,
+ 'on_create': True,
+ 'condition': 'true',
+ 'notification_email': notification_email.id,
+ 'action': 'notification.email|trigger',
+ }])
+
+ with patch.object(
+ notification_module, 'sendmail_transactional') as sendmail, \
+ patch.object(notification_module, 'SMTPDataManager'):
+ User.create([{'name': "Michael Scott", 'login': "msc"}])
+ self.run_tasks(1)
+ sendmail.assert_not_called()
+ self.run_tasks()
+ sendmail.assert_called_once_with(
+ FROM, ['[email protected]'], ANY, datamanager=ANY)
+
+ @with_transaction()
+ def test_notification_email_attachment(self):
+ "Test email notificiation with attachment"
+ pool = Pool()
+ Model = pool.get('ir.model')
+ Report = pool.get('ir.action.report')
+ User = pool.get('res.user')
+ NotificationEmail = pool.get('notification.email')
+ Language = pool.get('ir.lang')
+
+ self._setup_notification()
+ model, = Model.search([
+ ('model', '=', User.__name__),
+ ])
+ en, = Language.search([('code', '=', 'en')])
+
+ attachment = Report()
+ attachment.name = "Attachment"
+ attachment.report_name = 'notification_notification.test.report'
+ attachment.template_extension = 'txt'
+ attachment.report_content = b'attachment for ${records[0].name}'
+ attachment.model = model.model
+ attachment.save()
+
+ notification_email, = NotificationEmail.search([])
+ notification_email.attachments = [attachment]
+ notification_email.save()
+
+ user, = User.create([{'name': "Michael Scott", 'login': "msc"}])
+
+ msg = notification_email.get_email(
+ user, FROM, ['Administrator <[email protected]>'], [], [], [en])
+
+ self.assertEqual(msg['From'], FROM)
+ self.assertEqual(msg['Subject'], 'Notification Email')
+ self.assertEqual(msg['To'], 'Administrator <[email protected]>')
+ self.assertEqual(msg.get_content_type(), 'multipart/mixed')
+ self.assertEqual(
+ msg.get_payload(0).get_content_type(), 'multipart/alternative')
+
+ attachment = msg.get_payload(1)
+ self.assertEqual(
+ attachment.get_payload(None, True),
+ b'attachment for Michael Scott')
+ self.assertEqual(
+ attachment.get_content_type(), 'text/plain')
+ self.assertEqual(
+ attachment.get_filename(), "Attachment-Michael Scott.txt")
+
+ @with_transaction()
+ def test_notification_email_subject(self):
+ "Test email notificiation with subject"
+ pool = Pool()
+ Model = pool.get('ir.model')
+ User = pool.get('res.user')
+ NotificationEmail = pool.get('notification.email')
+ Language = pool.get('ir.lang')
+
+ self._setup_notification()
+ model, = Model.search([
+ ('model', '=', User.__name__),
+ ])
+ en, = Language.search([('code', '=', 'en')])
+
+ notification_email, = NotificationEmail.search([])
+ notification_email.subject = 'Notification for ${record.name}'
+ notification_email.save()
+
+ user, = User.create([{'name': "Michael Scott", 'login': "msc"}])
+
+ msg = notification_email.get_email(
+ user, FROM, ['Administrator <[email protected]>'], [], [], [en])
+
+ self.assertEqual(msg['Subject'], 'Notification for Michael Scott')
+
+ @with_transaction()
+ def test_notification_email_translated_subject(self):
+ "Test email notificiation with translated subject"
+ pool = Pool()
+ Model = pool.get('ir.model')
+ User = pool.get('res.user')
+ NotificationEmail = pool.get('notification.email')
+ Language = pool.get('ir.lang')
+
+ self._setup_notification()
+ model, = Model.search([
+ ('model', '=', User.__name__),
+ ])
+ es, = Language.search([('code', '=', 'es')])
+ Language.load_translations([es])
+
+ notification_email, = NotificationEmail.search([])
+ notification_email.subject = 'Notification for ${record.name}'
+ notification_email.save()
+
+ with Transaction().set_context(lang='es'):
+ notification_email, = NotificationEmail.search([])
+ notification_email.subject = 'Notificación para ${record.name}'
+ notification_email.save()
+
+ user, = User.create([{
+ 'name': "Michael Scott",
+ 'login': "msc",
+ 'language': es.id,
+ }])
+
+ msg = notification_email.get_email(
+ user, FROM, ['Administrator <[email protected]>'], [], [], [es])
+
+ self.assertEqual(msg['Subject'], 'Notificación para Michael Scott')
+
+ @unittest.skipIf(
+ (3, 5, 0) <= sys.version_info < (3, 5, 2), "python bug #25195")
+ @with_transaction()
+ def test_notification_email_fallback(self):
+ "Test email notification fallback"
+ pool = Pool()
+ User = pool.get('res.user')
+ Trigger = pool.get('ir.trigger')
+ Model = pool.get('ir.model')
+ NotificationEmail = pool.get('notification.email')
+ User = pool.get('res.user')
+
+ fallback_user = User()
+ fallback_user.name = 'Fallback'
+ fallback_user.email = '[email protected]'
+ fallback_user.login = 'fallback'
+ fallback_user.save()
+
+ self._setup_notification()
+ notification_email, = NotificationEmail.search([])
+ notification_email.recipients = None
+ notification_email.fallback_recipients = fallback_user
+ notification_email.save()
+
+ model, = Model.search([
+ ('model', '=', User.__name__),
+ ])
+ Trigger.create([{
+ 'name': 'Test creation',
+ 'model': model.id,
+ 'on_create': True,
+ 'condition': 'true',
+ 'notification_email': notification_email.id,
+ 'action': 'notification.email|trigger',
+ }])
+
+ with patch.object(
+ notification_module, 'sendmail_transactional') as sendmail, \
+ patch.object(notification_module, 'SMTPDataManager'):
+ User.create([{'name': "Michael Scott", 'login': "msc"}])
+ self.run_tasks()
+ sendmail.assert_called_once_with(
+ FROM, ['[email protected]'], ANY,
+ datamanager=ANY)
+ _, _, msg = sendmail.call_args[0]
+ self.assertEqual(msg['To'], 'Fallback <[email protected]>')
+
+ @with_transaction()
+ def test_notification_email_no_recipient(self):
+ "Test email notification no recipient"
+ pool = Pool()
+ User = pool.get('res.user')
+ Trigger = pool.get('ir.trigger')
+ Model = pool.get('ir.model')
+ NotificationEmail = pool.get('notification.email')
+ User = pool.get('res.user')
+
+ self._setup_notification()
+ notification_email, = NotificationEmail.search([])
+ notification_email.recipients = None
+ notification_email.save()
+
+ model, = Model.search([
+ ('model', '=', User.__name__),
+ ])
+ Trigger.create([{
+ 'name': 'Test creation',
+ 'model': model.id,
+ 'on_create': True,
+ 'condition': 'true',
+ 'notification_email': notification_email.id,
+ 'action': 'notification.email|trigger',
+ }])
+
+ with patch.object(
+ notification_module, 'get_email') as get_email, \
+ patch.object(notification_module, 'SMTPDataManager'):
+ User.create([{'name': "Michael Scott", 'login': "msc"}])
+ self.run_tasks()
+ get_email.assert_not_called()
+
+
+del ModuleTestCase
diff -r b8937caac8f7 -r 7c88d5832e1a tests/test_notification_email.py
--- a/tests/test_notification_email.py Mon Apr 11 23:24:21 2022 +0200
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,412 +0,0 @@
-# This file is part of Tryton. The COPYRIGHT file at the top level of
-# this repository contains the full copyright notices and license terms.
-import datetime as dt
-import sys
-import unittest
-from unittest.mock import ANY, patch
-
-from trytond.config import config
-
-try:
- from trytond.modules.company.tests import CompanyTestMixin
-except ImportError:
- class CompanyTestMixin:
- pass
-from trytond.modules.notification_email import \
- notification as notification_module
-from trytond.pool import Pool
-from trytond.tests.test_tryton import ModuleTestCase
-from trytond.tests.test_tryton import suite as test_suite
-from trytond.tests.test_tryton import with_transaction
-from trytond.transaction import Transaction
-
-FROM = '[email protected]'
-
-
-class NotificationEmailTestCase(CompanyTestMixin, ModuleTestCase):
- "Test Notification Email module"
- module = 'notification_email'
- extras = ['company', 'commission', 'party', 'web_user']
-
- def setUp(self):
- super(NotificationEmailTestCase, self).setUp()
- reset_from = config.get('email', 'from', default='')
- config.set('email', 'from', FROM)
- self.addCleanup(lambda: config.set('email', 'from', reset_from))
-
- def _setup_notification(self, recipient_field='create_uid'):
- pool = Pool()
- Model = pool.get('ir.model')
- ModelField = pool.get('ir.model.field')
- Action = pool.get('ir.action')
- Report = pool.get('ir.action.report')
- User = pool.get('res.user')
- NotificationEmail = pool.get('notification.email')
-
- model, = Model.search([
- ('model', '=', User.__name__),
- ])
-
- action = Action(name="Notification Email", type='ir.action.report')
- action.save()
- report = Report()
- report.report_name = 'notification_notification.test.report'
- report.action = action
- report.template_extension = 'txt'
- report.report_content = b'Hello ${records[0].name}'
- report.model = model.model
- report.save()
-
- user = User(Transaction().user)
- user.email = '[email protected]'
- user.save()
-
- notification_email = NotificationEmail()
- notification_email.recipients, = ModelField.search([
- ('model.model', '=', model.model),
- ('name', '=', recipient_field),
- ])
- notification_email.content = report
- notification_email.save()
-
- def run_tasks(self, count=None):
- pool = Pool()
- Queue = pool.get('ir.queue')
- transaction = Transaction()
- self.assertTrue(transaction.tasks)
- i = 0
- while transaction.tasks:
- task = Queue(transaction.tasks.pop())
- task.run()
- i += 1
- if count is not None:
- if i >= count:
- break
-
- @unittest.skipIf(
- (3, 5, 0) <= sys.version_info < (3, 5, 2), "python bug #25195")
- @with_transaction()
- def test_notification_email(self):
- "Test email notificiation is sent on trigger"
- pool = Pool()
- User = pool.get('res.user')
- Trigger = pool.get('ir.trigger')
- Model = pool.get('ir.model')
- NotificationEmail = pool.get('notification.email')
- Log = pool.get('notification.email.log')
-
- self._setup_notification()
- notification_email, = NotificationEmail.search([])
-
- model, = Model.search([
- ('model', '=', User.__name__),
- ])
- Trigger.create([{
- 'name': 'Test creation',
- 'model': model.id,
- 'on_create': True,
- 'condition': 'true',
- 'notification_email': notification_email.id,
- 'action': 'notification.email|trigger',
- }])
-
- with patch.object(
- notification_module, 'sendmail_transactional') as sendmail, \
- patch.object(notification_module, 'SMTPDataManager'):
- User.create([{'name': "Michael Scott", 'login': "msc"}])
- self.run_tasks()
- sendmail.assert_called_once_with(
- FROM, ['[email protected]'], ANY,
- datamanager=ANY)
- _, _, msg = sendmail.call_args[0]
- self.assertEqual(msg['From'], FROM)
- self.assertEqual(msg['Subject'], 'Notification Email')
- self.assertEqual(msg['To'], 'Administrator <[email protected]>')
- self.assertEqual(msg['Auto-Submitted'], 'auto-generated')
- self.assertEqual(msg.get_content_type(), 'multipart/alternative')
- self.assertEqual(
- msg.get_payload(0).get_payload(), 'Hello Michael Scott')
-
- log, = Log.search([])
- self.assertEqual(log.trigger.notification_email, notification_email)
- self.assertEqual(log.recipients, 'Administrator <[email protected]>')
- self.assertEqual(log.recipients_secondary, '')
- self.assertEqual(log.recipients_hidden, '')
-
- @unittest.skipIf(
- (3, 5, 0) <= sys.version_info < (3, 5, 2), "python bug #25195")
- @with_transaction()
- def test_notification_email_id_recipient(self):
- "Test email notificiation is sent when using id as recipient"
- pool = Pool()
- User = pool.get('res.user')
- Trigger = pool.get('ir.trigger')
- Model = pool.get('ir.model')
- NotificationEmail = pool.get('notification.email')
- Log = pool.get('notification.email.log')
-
- self._setup_notification(recipient_field='id')
- notification_email, = NotificationEmail.search([])
-
- model, = Model.search([
- ('model', '=', User.__name__),
- ])
- Trigger.create([{
- 'name': 'Test creation',
- 'model': model.id,
- 'on_create': True,
- 'condition': 'true',
- 'notification_email': notification_email.id,
- 'action': 'notification.email|trigger',
- }])
-
- with patch.object(
- notification_module, 'sendmail_transactional') as sendmail, \
- patch.object(notification_module, 'SMTPDataManager'):
- User.create([{
- 'name': "Michael Scott",
- 'login': "msc",
- 'email': '[email protected]'}])
- self.run_tasks()
- sendmail.assert_called_once_with(
- FROM, ['[email protected]'], ANY,
- datamanager=ANY)
-
- log, = Log.search([])
- self.assertEqual(log.trigger.notification_email, notification_email)
- self.assertEqual(log.recipients, 'Michael Scott <[email protected]>')
-
- @with_transaction()
- def test_notification_email_delay(self):
- "Test email notification is sent with delay"
- pool = Pool()
- User = pool.get('res.user')
- Trigger = pool.get('ir.trigger')
- Model = pool.get('ir.model')
- NotificationEmail = pool.get('notification.email')
-
- self._setup_notification()
- notification_email, = NotificationEmail.search([])
- notification_email.send_after = dt.timedelta(minutes=5)
- notification_email.save()
-
- model, = Model.search([
- ('model', '=', User.__name__),
- ])
- Trigger.create([{
- 'name': 'Test creation',
- 'model': model.id,
- 'on_create': True,
- 'condition': 'true',
- 'notification_email': notification_email.id,
- 'action': 'notification.email|trigger',
- }])
-
- with patch.object(
- notification_module, 'sendmail_transactional') as sendmail, \
- patch.object(notification_module, 'SMTPDataManager'):
- User.create([{'name': "Michael Scott", 'login': "msc"}])
- self.run_tasks(1)
- sendmail.assert_not_called()
- self.run_tasks()
- sendmail.assert_called_once_with(
- FROM, ['[email protected]'], ANY, datamanager=ANY)
-
- @with_transaction()
- def test_notification_email_attachment(self):
- "Test email notificiation with attachment"
- pool = Pool()
- Model = pool.get('ir.model')
- Report = pool.get('ir.action.report')
- User = pool.get('res.user')
- NotificationEmail = pool.get('notification.email')
- Language = pool.get('ir.lang')
-
- self._setup_notification()
- model, = Model.search([
- ('model', '=', User.__name__),
- ])
- en, = Language.search([('code', '=', 'en')])
-
- attachment = Report()
- attachment.name = "Attachment"
- attachment.report_name = 'notification_notification.test.report'
- attachment.template_extension = 'txt'
- attachment.report_content = b'attachment for ${records[0].name}'
- attachment.model = model.model
- attachment.save()
-
- notification_email, = NotificationEmail.search([])
- notification_email.attachments = [attachment]
- notification_email.save()
-
- user, = User.create([{'name': "Michael Scott", 'login': "msc"}])
-
- msg = notification_email.get_email(
- user, FROM, ['Administrator <[email protected]>'], [], [], [en])
-
- self.assertEqual(msg['From'], FROM)
- self.assertEqual(msg['Subject'], 'Notification Email')
- self.assertEqual(msg['To'], 'Administrator <[email protected]>')
- self.assertEqual(msg.get_content_type(), 'multipart/mixed')
- self.assertEqual(
- msg.get_payload(0).get_content_type(), 'multipart/alternative')
-
- attachment = msg.get_payload(1)
- self.assertEqual(
- attachment.get_payload(None, True),
- b'attachment for Michael Scott')
- self.assertEqual(
- attachment.get_content_type(), 'text/plain')
- self.assertEqual(
- attachment.get_filename(), "Attachment-Michael Scott.txt")
-
- @with_transaction()
- def test_notification_email_subject(self):
- "Test email notificiation with subject"
- pool = Pool()
- Model = pool.get('ir.model')
- User = pool.get('res.user')
- NotificationEmail = pool.get('notification.email')
- Language = pool.get('ir.lang')
-
- self._setup_notification()
- model, = Model.search([
- ('model', '=', User.__name__),
- ])
- en, = Language.search([('code', '=', 'en')])
-
- notification_email, = NotificationEmail.search([])
- notification_email.subject = 'Notification for ${record.name}'
- notification_email.save()
-
- user, = User.create([{'name': "Michael Scott", 'login': "msc"}])
-
- msg = notification_email.get_email(
- user, FROM, ['Administrator <[email protected]>'], [], [], [en])
-
- self.assertEqual(msg['Subject'], 'Notification for Michael Scott')
-
- @with_transaction()
- def test_notification_email_translated_subject(self):
- "Test email notificiation with translated subject"
- pool = Pool()
- Model = pool.get('ir.model')
- User = pool.get('res.user')
- NotificationEmail = pool.get('notification.email')
- Language = pool.get('ir.lang')
-
- self._setup_notification()
- model, = Model.search([
- ('model', '=', User.__name__),
- ])
- es, = Language.search([('code', '=', 'es')])
- Language.load_translations([es])
-
- notification_email, = NotificationEmail.search([])
- notification_email.subject = 'Notification for ${record.name}'
- notification_email.save()
-
- with Transaction().set_context(lang='es'):
- notification_email, = NotificationEmail.search([])
- notification_email.subject = 'Notificación para ${record.name}'
- notification_email.save()
-
- user, = User.create([{
- 'name': "Michael Scott",
- 'login': "msc",
- 'language': es.id,
- }])
-
- msg = notification_email.get_email(
- user, FROM, ['Administrator <[email protected]>'], [], [], [es])
-
- self.assertEqual(msg['Subject'], 'Notificación para Michael Scott')
-
- @unittest.skipIf(
- (3, 5, 0) <= sys.version_info < (3, 5, 2), "python bug #25195")
- @with_transaction()
- def test_notification_email_fallback(self):
- "Test email notification fallback"
- pool = Pool()
- User = pool.get('res.user')
- Trigger = pool.get('ir.trigger')
- Model = pool.get('ir.model')
- NotificationEmail = pool.get('notification.email')
- User = pool.get('res.user')
-
- fallback_user = User()
- fallback_user.name = 'Fallback'
- fallback_user.email = '[email protected]'
- fallback_user.login = 'fallback'
- fallback_user.save()
-
- self._setup_notification()
- notification_email, = NotificationEmail.search([])
- notification_email.recipients = None
- notification_email.fallback_recipients = fallback_user
- notification_email.save()
-
- model, = Model.search([
- ('model', '=', User.__name__),
- ])
- Trigger.create([{
- 'name': 'Test creation',
- 'model': model.id,
- 'on_create': True,
- 'condition': 'true',
- 'notification_email': notification_email.id,
- 'action': 'notification.email|trigger',
- }])
-
- with patch.object(
- notification_module, 'sendmail_transactional') as sendmail, \
- patch.object(notification_module, 'SMTPDataManager'):
- User.create([{'name': "Michael Scott", 'login': "msc"}])
- self.run_tasks()
- sendmail.assert_called_once_with(
- FROM, ['[email protected]'], ANY,
- datamanager=ANY)
- _, _, msg = sendmail.call_args[0]
- self.assertEqual(msg['To'], 'Fallback <[email protected]>')
-
- @with_transaction()
- def test_notification_email_no_recipient(self):
- "Test email notification no recipient"
- pool = Pool()
- User = pool.get('res.user')
- Trigger = pool.get('ir.trigger')
- Model = pool.get('ir.model')
- NotificationEmail = pool.get('notification.email')
- User = pool.get('res.user')
-
- self._setup_notification()
- notification_email, = NotificationEmail.search([])
- notification_email.recipients = None
- notification_email.save()
-
- model, = Model.search([
- ('model', '=', User.__name__),
- ])
- Trigger.create([{
- 'name': 'Test creation',
- 'model': model.id,
- 'on_create': True,
- 'condition': 'true',
- 'notification_email': notification_email.id,
- 'action': 'notification.email|trigger',
- }])
-
- with patch.object(
- notification_module, 'get_email') as get_email, \
- patch.object(notification_module, 'SMTPDataManager'):
- User.create([{'name': "Michael Scott", 'login': "msc"}])
- self.run_tasks()
- get_email.assert_not_called()
-
-
-def suite():
- suite = test_suite()
- suite.addTests(unittest.TestLoader().loadTestsFromTestCase(
- NotificationEmailTestCase))
- return suite
diff -r b8937caac8f7 -r 7c88d5832e1a tox.ini
--- a/tox.ini Mon Apr 11 23:24:21 2022 +0200
+++ b/tox.ini Sat Apr 16 18:30:17 2022 +0200
@@ -2,8 +2,9 @@
envlist = {py37,py38,py39,py310}-{sqlite,postgresql}
[testenv]
+extras = test
commands =
- coverage run --include=.*/notification_email/* setup.py test
+ coverage run --include=.*/notification_email/* -m unittest discover -s
tests
coverage report --include=.*/notification_email/* --omit=*/tests/*
deps =
coverage