-
-
Notifications
You must be signed in to change notification settings - Fork 624
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[FIX] mail_send_copy: work with existing BCC+adding tests #1527
base: 15.0
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from . import test_mail_send_copy |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
# Copyright from 2024: Alwinen GmbH (https://www.alwinen.de) | ||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). | ||
|
||
# TODO: find a way to test sending email with existing BCC | ||
|
||
Comment on lines
+4
to
+5
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi, could you consider this test please?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice idea, thanks. I have added them in the commit. It seems working, but here I cannot find the results of codecov anymore :( Have I done something wrong? |
||
from unittest.mock import patch | ||
|
||
from odoo.tests import tagged | ||
|
||
from odoo.addons.mail.tests.test_mail_composer import 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": "test@example.com", | ||
} | ||
) | ||
|
||
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": "sender@example.com", | ||
} | ||
) | ||
|
||
# Mock the send_email method | ||
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) | ||
# Get the arguments passed to send_email | ||
call_args = mock_send_email.call_args[0] | ||
# The message is the first argument | ||
message = call_args[0] | ||
# Check BCC in the email message | ||
self.assertIn("sender@example.com", 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": "sender@example.com", | ||
} | ||
) | ||
) | ||
|
||
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) | ||
|
||
|
||
# Code contributed by @trisdoan | ||
@tagged("post_install", "-at_install") | ||
class TestMailSendWithBcc(TestMailSendCopy): | ||
def test_send_email_with_existing_bcc(self): | ||
if not self.env["ir.module.module"].search( | ||
[("name", "=", "mail_composer_cc_bcc"), ("state", "=", "installed")] | ||
): | ||
self.skipTest("mail_composer_cc_bcc module is required for this test") | ||
|
||
partner_bcc = self.env.ref("base.res_partner_main2") | ||
composer = self.env["mail.compose.message"].create( | ||
{ | ||
"partner_ids": [(6, 0, [self.partner.id])], | ||
"subject": "Test Subject", | ||
"body": "<p>Test Body</p>", | ||
"email_from": "sender@example.com", | ||
} | ||
) | ||
composer.partner_bcc_ids = partner_bcc | ||
|
||
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) | ||
call_args = mock_send_email.call_args[0] | ||
message = call_args[0] | ||
# Check existing BCC in the email message | ||
self.assertIn("dwayne.newman28@example.com", message["Bcc"]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so this line should be removed