Skip to content

Commit 1e0115d

Browse files
committed
[IMP] ..aware_sender: add test for server selection
1 parent b54d05c commit 1e0115d

File tree

5 files changed

+72
-28
lines changed

5 files changed

+72
-28
lines changed

mail_company_aware_sender/README.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
.. image:: https://odoo-community.org/readme-banner-image
2+
:target: https://odoo-community.org/get-involved?utm_source=readme
3+
:alt: Odoo Community Association
4+
15
=========================
26
Mail Sender Company Aware
37
=========================
@@ -13,7 +17,7 @@ Mail Sender Company Aware
1317
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
1418
:target: https://odoo-community.org/page/development-status
1519
:alt: Beta
16-
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
20+
.. |badge2| image:: https://img.shields.io/badge/license-AGPL--3-blue.png
1721
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
1822
:alt: License: AGPL-3
1923
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fsocial-lightgray.png?logo=github

mail_company_aware_sender/static/description/index.html

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<head>
44
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
55
<meta name="generator" content="Docutils: https://docutils.sourceforge.io/" />
6-
<title>Mail Sender Company Aware</title>
6+
<title>README.rst</title>
77
<style type="text/css">
88

99
/*
@@ -360,16 +360,21 @@
360360
</style>
361361
</head>
362362
<body>
363-
<div class="document" id="mail-sender-company-aware">
364-
<h1 class="title">Mail Sender Company Aware</h1>
363+
<div class="document">
365364

365+
366+
<a class="reference external image-reference" href="https://odoo-community.org/get-involved?utm_source=readme">
367+
<img alt="Odoo Community Association" src="https://odoo-community.org/readme-banner-image" />
368+
</a>
369+
<div class="section" id="mail-sender-company-aware">
370+
<h1>Mail Sender Company Aware</h1>
366371
<!-- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
367372
!! This file is generated by oca-gen-addon-readme !!
368373
!! changes will be overwritten. !!
369374
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
370375
!! source digest: sha256:7ee2897c3172b5b4d4927dbf7ec1e94e1c71bc5602f7bdf85b3ac0cae33481d0
371376
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
372-
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/social/tree/14.0/mail_company_aware_sender"><img alt="OCA/social" src="https://img.shields.io/badge/github-OCA%2Fsocial-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/social-14-0/social-14-0-mail_company_aware_sender"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/social&amp;target_branch=14.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
377+
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/license-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/social/tree/14.0/mail_company_aware_sender"><img alt="OCA/social" src="https://img.shields.io/badge/github-OCA%2Fsocial-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/social-14-0/social-14-0-mail_company_aware_sender"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/social&amp;target_branch=14.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
373378
<p>This module allows to send out mails using a company aware email address.
374379
Suppose we have a user Charles the Magnificent with email address
375380
<a class="reference external" href="mailto:charlemagne&#64;kingdom.fr">charlemagne&#64;kingdom.fr</a>. However this user works for two companies, one
@@ -401,7 +406,7 @@ <h1 class="title">Mail Sender Company Aware</h1>
401406
</ul>
402407
</div>
403408
<div class="section" id="configuration">
404-
<h1><a class="toc-backref" href="#toc-entry-1">Configuration</a></h1>
409+
<h2><a class="toc-backref" href="#toc-entry-1">Configuration</a></h2>
405410
<dl class="docutils">
406411
<dt>To send out mails with company aware from addresses, three things are needed:</dt>
407412
<dd><ul class="first last simple">
@@ -419,23 +424,23 @@ <h1><a class="toc-backref" href="#toc-entry-1">Configuration</a></h1>
419424
emails. This can be configured right under the email field in the partner form.</p>
420425
</div>
421426
<div class="section" id="bug-tracker">
422-
<h1><a class="toc-backref" href="#toc-entry-2">Bug Tracker</a></h1>
427+
<h2><a class="toc-backref" href="#toc-entry-2">Bug Tracker</a></h2>
423428
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/social/issues">GitHub Issues</a>.
424429
In case of trouble, please check there if your issue has already been reported.
425430
If you spotted it first, help us to smash it by providing a detailed and welcomed
426431
<a class="reference external" href="https://github.com/OCA/social/issues/new?body=module:%20mail_company_aware_sender%0Aversion:%2014.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
427432
<p>Do not contact contributors directly about support or help with technical issues.</p>
428433
</div>
429434
<div class="section" id="credits">
430-
<h1><a class="toc-backref" href="#toc-entry-3">Credits</a></h1>
435+
<h2><a class="toc-backref" href="#toc-entry-3">Credits</a></h2>
431436
<div class="section" id="authors">
432-
<h2><a class="toc-backref" href="#toc-entry-4">Authors</a></h2>
437+
<h3><a class="toc-backref" href="#toc-entry-4">Authors</a></h3>
433438
<ul class="simple">
434439
<li>Therp BV</li>
435440
</ul>
436441
</div>
437442
<div class="section" id="contributors">
438-
<h2><a class="toc-backref" href="#toc-entry-5">Contributors</a></h2>
443+
<h3><a class="toc-backref" href="#toc-entry-5">Contributors</a></h3>
439444
<ul class="simple">
440445
<li><a class="reference external" href="https://therp.nl">Therp BV</a>:<ul>
441446
<li>Ronald Portier (NL66278)</li>
@@ -444,7 +449,7 @@ <h2><a class="toc-backref" href="#toc-entry-5">Contributors</a></h2>
444449
</ul>
445450
</div>
446451
<div class="section" id="maintainers">
447-
<h2><a class="toc-backref" href="#toc-entry-6">Maintainers</a></h2>
452+
<h3><a class="toc-backref" href="#toc-entry-6">Maintainers</a></h3>
448453
<p>This module is maintained by the OCA.</p>
449454
<a class="reference external image-reference" href="https://odoo-community.org">
450455
<img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" />
@@ -457,5 +462,6 @@ <h2><a class="toc-backref" href="#toc-entry-6">Maintainers</a></h2>
457462
</div>
458463
</div>
459464
</div>
465+
</div>
460466
</body>
461467
</html>

mail_company_aware_sender/tests/common.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Copyright 2025 Therp BV <https://therp.nl>.
22
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
3+
from email.utils import parseaddr
4+
35
from odoo.tests.common import SavepointCase
46

57

@@ -17,6 +19,7 @@ def setUpClass(cls):
1719
"name": "localhost",
1820
"smtp_host": "localhost",
1921
"domain_whitelist": "therp.nl",
22+
"sequence": 99,
2023
}
2124
)
2225
cls.company_kingdom = cls.Company.create(
@@ -58,3 +61,10 @@ def setUpClass(cls):
5861
"company_id": cls.company_imperium.id,
5962
}
6063
)
64+
65+
def _assert_email(self, email_from, expected_email, expected_name=None):
66+
"""Assert email_from matches expected parts, tolerant to quoting differences."""
67+
name, email = parseaddr(email_from or "")
68+
self.assertEqual(email, expected_email)
69+
if expected_name is not None:
70+
self.assertEqual(name, expected_name)

mail_company_aware_sender/tests/test_company_aware_sender.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
11
# Copyright 2025 Therp BV <https://therp.nl>.
22
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
3-
from email.utils import parseaddr
43

54
from .common import CompanyAwareSenderCase
65

76

87
class TestCompanyAwareSender(CompanyAwareSenderCase):
9-
def _assert_email(self, email_from, expected_email, expected_name=None):
10-
"""Assert email_from matches expected parts, tolerant to quoting differences."""
11-
name, email = parseaddr(email_from or "")
12-
self.assertEqual(email, expected_email)
13-
if expected_name is not None:
14-
self.assertEqual(name, expected_name)
15-
168
def test_nothing_changed(self):
179
# Check with default user and author (current user).
1810
mail_thread = (

mail_company_aware_sender/tests/test_mail_server.py

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
1-
# Copyright 2025 Therp BV <https://therp.nl>.
1+
# Copyright 2025-2026 Therp BV <https://therp.nl>.
22
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
3-
from email.utils import parseaddr
43

54
from odoo.exceptions import ValidationError
65

76
from .common import CompanyAwareSenderCase
87

98

109
class TestMailServer(CompanyAwareSenderCase):
11-
def _assert_email(self, email_from, expected_email, expected_name=None):
12-
"""Assert email_from matches expected parts, tolerant to quoting differences."""
13-
name, email = parseaddr(email_from or "")
14-
self.assertEqual(email, expected_email)
15-
if expected_name is not None:
16-
self.assertEqual(name, expected_name)
17-
1810
def test_test_email_adresses(self):
1911
# Whitelist domain, and enable from address.
2012
self.mail_server.write(
@@ -54,3 +46,43 @@ def test_disable_encapsulation(self):
5446
self.company_imperium
5547
)._get_default_from_address()
5648
self.assertEqual(email_from, None)
49+
50+
def test_email_server_selection(self):
51+
# When one server has domain whitelisted, that server should send message.
52+
self.IrMailServer.create(
53+
{
54+
"name": "default mail server",
55+
"smtp_host": "localhost",
56+
"domain_whitelist": "therp.nl",
57+
"sequence": 5,
58+
}
59+
)
60+
# First test with smtp_from.
61+
self.mail_server.write(
62+
{
63+
"smtp_from": "info@imperiumromanum.org",
64+
"domain_whitelist": "imperiumromanum.org",
65+
}
66+
)
67+
# Partner charles in imperium should use company aware from.
68+
email_from, email_to = (
69+
self.mail_server.with_user(self.user_charles)
70+
.with_company(self.company_imperium)
71+
._get_test_email_addresses()
72+
)
73+
self._assert_email(email_from, "info@imperiumromanum.org")
74+
# Call the (misspelled) _get_mail_sever method, defined in the
75+
# mail_outbound_static module, to select the right mail server.
76+
mail_server_id = self.IrMailServer._get_mail_sever("imperiumromanum.org")
77+
# The server with the domain should be used, despite higher sequence.
78+
self.assertEqual(mail_server_id, self.mail_server.id)
79+
# Now test with no smtp_from.
80+
self.mail_server.write({"smtp_from": False})
81+
email_from, email_to = (
82+
self.mail_server.with_user(self.user_charles)
83+
.with_company(self.company_imperium)
84+
._get_test_email_addresses()
85+
)
86+
self._assert_email(email_from, "charlemagne@imperiumromanum.org")
87+
mail_server_id = self.IrMailServer._get_mail_sever("imperiumromanum.org")
88+
self.assertEqual(mail_server_id, self.mail_server.id)

0 commit comments

Comments
 (0)