Skip to content

Commit

Permalink
[FIX] mail_send_copy: work with existing BCC+adding tests2
Browse files Browse the repository at this point in the history
  • Loading branch information
liklee-it committed Dec 21, 2024
1 parent 2b41d52 commit 69e2a69
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 26 deletions.
2 changes: 1 addition & 1 deletion mail_send_copy/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from . import test_mail_send_copy
from . import test_mail_send_copy
62 changes: 37 additions & 25 deletions mail_send_copy/tests/test_mail_send_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,35 @@
from odoo.addons.mail.tests.test_mail_composer import TestMailComposer


class TestMailSendCopy(TestMailComposer):
class TestMailSendCopy(TestMailComposer):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.env = cls.env(context={"testing": True})

# Create test partner
cls.partner = cls.env["res.partner"].create({
"name": "Test Partner",
"email": "[email protected]",
})
cls.partner = cls.env["res.partner"].create(
{
"name": "Test Partner",
"email": "[email protected]",
}
)

def test_send_email_with_copy(self):
"""Test that sender is added to BCC when sending email"""
composer = self.env["mail.compose.message"].create({
"partner_ids": [(6, 0, [self.partner.id])],
"subject": "Test Subject",
"body": "<p>Test Body</p>",
"email_from": "[email protected]",
})
composer = self.env["mail.compose.message"].create(
{
"partner_ids": [(6, 0, [self.partner.id])],
"subject": "Test Subject",
"body": "<p>Test Body</p>",
"email_from": "[email protected]",
}
)

# Mock the send_email method
with patch('odoo.addons.base.models.ir_mail_server.IrMailServer.send_email') as mock_send_email:
with patch(
"odoo.addons.base.models.ir_mail_server.IrMailServer.send_email"
) as mock_send_email:
composer._action_send_mail()
# Verify that send_email was called
self.assertTrue(mock_send_email.called)
Expand All @@ -39,21 +45,27 @@ def test_send_email_with_copy(self):
# The message is the first argument
message = call_args[0]
# Check BCC in the email message
self.assertIn('[email protected]', message['Bcc'])
self.assertIn("[email protected]", message["Bcc"])

def test_send_email_without_copy(self):
"""Test that sender is not added to BCC when do_not_send_copy is True"""
composer = self.env["mail.compose.message"].with_context(
do_not_send_copy=True
).create({
"partner_ids": [(6, 0, [self.partner.id])],
"subject": "Test Subject No Copy",
"body": "<p>Test Body</p>",
"email_from": "[email protected]",
})

with patch('odoo.addons.base.models.ir_mail_server.IrMailServer.send_email') as mock_send_email:
composer = (
self.env["mail.compose.message"]
.with_context(do_not_send_copy=True)
.create(
{
"partner_ids": [(6, 0, [self.partner.id])],
"subject": "Test Subject No Copy",
"body": "<p>Test Body</p>",
"email_from": "[email protected]",
}
)
)

with patch(
"odoo.addons.base.models.ir_mail_server.IrMailServer.send_email"
) as mock_send_email:
composer._action_send_mail()
self.assertTrue(mock_send_email.called)
message = mock_send_email.call_args[0][0]
self.assertNotIn('Bcc', message)
self.assertNotIn("Bcc", message)

0 comments on commit 69e2a69

Please sign in to comment.