You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
6.7 KiB

  1. # Copyright 2019 Komit <https://komit-consulting.com>
  2. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
  3. from unittest.mock import patch
  4. from odoo.exceptions import ValidationError
  5. from odoo.tests.common import TransactionCase
  6. from odoo.tools.misc import mute_logger
  7. class TestPartnerEmailCheck(TransactionCase):
  8. def setUp(self):
  9. super(TestPartnerEmailCheck, self).setUp()
  10. # Checks are disabled during tests unless this key is set
  11. self.res_partner = self.env['res.partner'].with_context(
  12. test_partner_email_check=True
  13. )
  14. self.test_partner = self.res_partner.create({
  15. 'name': 'test',
  16. })
  17. self.wizard = self.env['res.config.settings'].create({})
  18. self.wizard.partner_email_check_filter_duplicates = False
  19. self.wizard.partner_email_check_check_deliverability = False
  20. self.wizard.set_values()
  21. def test_bad_email(self):
  22. """Test rejection of bad emails."""
  23. with self.assertRaises(ValidationError):
  24. self.test_partner.email = 'bad@email@domain..com'
  25. def test_good_email(self):
  26. """Test acceptance of good"""
  27. self.test_partner.email = 'goodemail@domain.com'
  28. self.assertTrue(self.test_partner.email)
  29. def test_bad_emails(self):
  30. """Test rejection of bad emails."""
  31. with self.assertRaises(ValidationError):
  32. self.test_partner.email = 'good@domain.com,bad@email@domain..com'
  33. def test_good_emails(self):
  34. """Test acceptance of good"""
  35. self.test_partner.email = 'goodemail@domain.com,goodemail2@domain.com'
  36. self.assertTrue(self.test_partner.email)
  37. def test_email_domain_normalization(self):
  38. """Test normalization of email domain names, including punycode."""
  39. self.test_partner.write({'email': 'goodemail@xn--xamPle-9ua.com'})
  40. self.assertEqual(self.test_partner.email, u'goodemail@éxample.com')
  41. def test_multi_email_domain_normalization(self):
  42. """Test normalization of email domain names of multiple addresses."""
  43. self.test_partner.write({
  44. 'email': 'goodemail@doMAIN.com,othergood@xn--xample-9ua.com'
  45. })
  46. self.assertEqual(
  47. self.test_partner.email,
  48. u'goodemail@domain.com,othergood@éxample.com'
  49. )
  50. def test_email_local_normalization(self):
  51. """Test normalization of the local part of email addresses."""
  52. self.test_partner.write({'email': 'Me@mail.org'})
  53. # .lower() is locale-dependent, so don't hardcode the result
  54. self.assertEqual(self.test_partner.email, 'Me'.lower() + '@mail.org')
  55. def test_multi_email_local_normalization(self):
  56. """Test normalization of the local part of multiple addresses."""
  57. self.test_partner.write({'email': 'You@mAiL.net,mE@mail.com'})
  58. self.assertEqual(
  59. self.test_partner.email,
  60. 'You'.lower() + '@mail.net,' + 'mE'.lower() + '@mail.com'
  61. )
  62. def disallow_duplicates(self):
  63. self.wizard.partner_email_check_filter_duplicates = True
  64. self.wizard.set_values()
  65. def test_duplicate_addresses_disallowed(self):
  66. self.disallow_duplicates()
  67. self.test_partner.write({'email': 'email@domain.tld'})
  68. with self.assertRaises(ValidationError):
  69. self.res_partner.create({
  70. 'name': 'alsotest',
  71. 'email': 'email@domain.tld'
  72. })
  73. def test_duplicate_after_normalization_addresses_disallowed(self):
  74. self.disallow_duplicates()
  75. self.res_partner.create({
  76. 'name': 'alsotest',
  77. 'email': 'email@doMAIN.tld'
  78. })
  79. with self.assertRaises(ValidationError):
  80. self.test_partner.email = 'email@domain.tld'
  81. def test_multiple_addresses_disallowed_when_duplicates_filtered(self):
  82. self.disallow_duplicates()
  83. with self.assertRaises(ValidationError):
  84. self.test_partner.email = 'foo@bar.org,email@domain.tld'
  85. def test_duplicate_addresses_allowed_by_default(self):
  86. self.res_partner.create({
  87. 'name': 'alsotest',
  88. 'email': 'email@domain.tld',
  89. })
  90. self.test_partner.email = 'email@domain.tld'
  91. def check_deliverability(self):
  92. self.wizard.partner_email_check_check_deliverability = True
  93. self.wizard.set_values()
  94. def test_deliverable_addresses_allowed(self):
  95. self.check_deliverability()
  96. # We only need a resolving domain, not a real user
  97. self.test_partner.email = 'gooddomain-icraglusrk@gmail.com'
  98. self.assertTrue(self.test_partner.email)
  99. def test_nondeliverable_addresses_not_allowed(self):
  100. self.check_deliverability()
  101. with self.assertRaises(ValidationError):
  102. # This domain may resolve by mistake on certain network setups
  103. # At least until a new version of email-validator is released
  104. # See https://github.com/JoshData/python-email-validator/pull/30
  105. self.test_partner.email = 'cezrik@acoa.nrdkt'
  106. def test_config_getters(self):
  107. other_wizard = self.env['res.config.settings'].create({})
  108. self.assertFalse(other_wizard.partner_email_check_check_deliverability)
  109. self.assertFalse(other_wizard.partner_email_check_filter_duplicates)
  110. self.disallow_duplicates()
  111. self.check_deliverability()
  112. other_wizard = self.env['res.config.settings'].create({})
  113. self.assertTrue(other_wizard.partner_email_check_check_deliverability)
  114. self.assertTrue(other_wizard.partner_email_check_filter_duplicates)
  115. @mute_logger('odoo.addons.partner_email_check.models.res_partner')
  116. def test_lacking_dependency_does_not_halt_execution(self):
  117. with patch('odoo.addons.partner_email_check.models.res_partner.'
  118. 'validate_email', None):
  119. self.test_partner.email = 'notatallvalid@@domain'
  120. @mute_logger('odoo.addons.partner_email_check.models.res_partner')
  121. def test_lacking_dependency_keeps_uniqueness_constraint_working(self):
  122. self.disallow_duplicates()
  123. with patch('odoo.addons.partner_email_check.models.res_partner.'
  124. 'validate_email', None):
  125. self.res_partner.create({
  126. 'name': 'alsotest',
  127. 'email': 'email@domain.tld'
  128. })
  129. with self.assertRaises(ValidationError):
  130. self.test_partner.email = 'email@domain.tld'
  131. def test_invalid_email_addresses_allowed_during_tests(self):
  132. # Note: testing without test_partner_email_check in the context
  133. new_partner = self.env['res.partner'].create({
  134. 'name': 'invalidly emailed',
  135. 'email': 'invalid'
  136. })
  137. self.assertEqual('invalid', new_partner.email)