| 1 |
import base64 |
| 2 |
from contextlib import contextmanager |
| 3 |
from freezegun import freeze_time |
| 4 |
from unittest.mock import patch |
| 5 |
|
| 6 |
from odoo.tests import BaseCase, TransactionCase |
| 7 |
from odoo.addons.base.models.ir_actions_report import IrActionsReport |
| 8 |
from odoo.addons.mail.tests.common import mail_new_test_user |
| 9 |
|
| 10 |
|
| 11 |
VALID_JPEG = base64.b64decode('/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgKCgkICQkKDA8MCgsOCwkJDRENDg8QEBEQCgwSExIQEw8QEBD/yQALCAABAAEBAREA/8wABgAQEAX/2gAIAQEAAD8A0s8g/9k=') |
| 12 |
|
| 13 |
|
| 14 |
def mock_image_render(func): |
| 15 |
def patched(self, *args, **kwargs): |
| 16 |
with self.mock_image_renderer(collect_params=False): |
| 17 |
return func(self, *args, **kwargs) |
| 18 |
return patched |
| 19 |
|
| 20 |
|
| 21 |
class MockImageRender(BaseCase): |
| 22 |
@contextmanager |
| 23 |
def mock_image_renderer(self, collect_params=True): |
| 24 |
self._wkhtmltoimage_bodies = [] |
| 25 |
|
| 26 |
def _ir_actions_report_build_run_wkhtmltoimage(model, bodies, width, height, image_format="jpg"): |
| 27 |
if collect_params: |
| 28 |
self._wkhtmltoimage_bodies.extend(bodies) |
| 29 |
return [VALID_JPEG] * len(bodies) |
| 30 |
|
| 31 |
with patch.object(IrActionsReport, '_run_wkhtmltoimage', _ir_actions_report_build_run_wkhtmltoimage): |
| 32 |
yield |
| 33 |
|
| 34 |
|
| 35 |
class MarketingCardCommon(TransactionCase, MockImageRender): |
| 36 |
|
| 37 |
@classmethod |
| 38 |
def setUpClass(cls): |
| 39 |
super().setUpClass() |
| 40 |
|
| 41 |
cls.company = cls.env['res.company'].create({ |
| 42 |
'country_id': cls.env.ref("base.be").id, |
| 43 |
'email': 'your.company@example.', |
| 44 |
'name': 'YourTestCompany', |
| 45 |
}) |
| 46 |
cls.marketing_card_manager = mail_new_test_user( |
| 47 |
cls.env, |
| 48 |
company_id=cls.company.id, |
| 49 |
email='[email protected]', |
| 50 |
login='marketing_card_manager', |
| 51 |
groups='marketing_card.marketing_card_group_manager', |
| 52 |
name='Marketing Card Manager', |
| 53 |
) |
| 54 |
cls.marketing_card_user = mail_new_test_user( |
| 55 |
cls.env, |
| 56 |
company_id=cls.company.id, |
| 57 |
email='[email protected]', |
| 58 |
login='marketing_card_user', |
| 59 |
groups='marketing_card.marketing_card_group_user', |
| 60 |
name='Marketing Card User', |
| 61 |
) |
| 62 |
cls.marketing_card_user_2 = cls.marketing_card_user.copy({ |
| 63 |
'email': '[email protected]', |
| 64 |
'login': 'marketing_card_user_2', |
| 65 |
'name': 'Marketing Card User 2', |
| 66 |
}) |
| 67 |
cls.system_admin = mail_new_test_user( |
| 68 |
cls.env, |
| 69 |
company_id=cls.company.id, |
| 70 |
email='[email protected]', |
| 71 |
login='marketing_card_system_admin', |
| 72 |
groups='base.group_system,marketing_card.marketing_card_group_manager', |
| 73 |
name='System Admin', |
| 74 |
) |
| 75 |
|
| 76 |
cls.partners = cls.env['res.partner'].create([ |
| 77 |
{'name': 'John', 'email': '[email protected]'}, |
| 78 |
{'name': 'Bob', 'email': '[email protected]', |
| 79 |
'phone': '+32 123 446 789', 'image_1920': base64.b64encode(VALID_JPEG), |
| 80 |
}, |
| 81 |
]) |
| 82 |
|
| 83 |
cls.card_template = cls.env['card.template'].create({ |
| 84 |
'name': 'Test Template', |
| 85 |
'body': """ |
| 86 |
<html> |
| 87 |
<t t-set="values" t-value="card_campaign._get_card_element_values(object)"/> |
| 88 |
<head> |
| 89 |
<style> |
| 90 |
p { margin: 1px }; |
| 91 |
body { width: 100%; height: 100%; }; |
| 92 |
</style> |
| 93 |
</head> |
| 94 |
<body> |
| 95 |
<div id="body" t-attf-style="background-image: url('data:image/png;base64,{{card_campaign.content_background or card_campaign.card_template_id.default_background}}');"> |
| 96 |
<span id="header" t-out="values['header']" t-att-style="'color: %s;' % card_campaign.content_header_color"/> |
| 97 |
<span id="subheader" t-out="values['sub_header']" t-att-style="'color: %s;' % card_campaign.content_sub_header_color"/> |
| 98 |
<span id="button" t-out="card_campaign.content_button">Button</span> |
| 99 |
<span id="section" t-out="values['section']"/> |
| 100 |
<span id="sub_section1" t-out="values['sub_section1']"/> |
| 101 |
<span id="sub_section2" t-out="values['sub_section2']"/> |
| 102 |
<img id="image1" t-if="values['image1']" t-attf-src="data:image/png;base64,{{values['image1']}}"/> |
| 103 |
<img id="image2" t-if="values['image2']" t-attf-src="data:image/png;base64,{{values['image2']}}"/> |
| 104 |
</div> |
| 105 |
</body> |
| 106 |
</html> |
| 107 |
""", |
| 108 |
}) |
| 109 |
|
| 110 |
cls.campaign = cls.env['card.campaign'].with_user(cls.marketing_card_user).create({ |
| 111 |
'name': 'Test Campaign', |
| 112 |
'card_template_id': cls.card_template.id, |
| 113 |
'post_suggestion': 'Come see my show!', |
| 114 |
'preview_record_ref': f'{cls.partners._name},{cls.partners[0].id}', |
| 115 |
'reward_message': """<p>Thanks for sharing!</p>""", |
| 116 |
'reward_target_url': f"{cls.env['card.campaign'].get_base_url()}/share-rewards/2039-sharer-badge/", |
| 117 |
'target_url': cls.env['card.campaign'].get_base_url(), |
| 118 |
'content_section': 'Contact', |
| 119 |
'content_sub_header_dyn': True, |
| 120 |
'content_sub_header_path': 'name', |
| 121 |
'content_sub_section1_dyn': True, |
| 122 |
'content_sub_section1_path': 'email', |
| 123 |
'content_sub_section2_dyn': True, |
| 124 |
'content_sub_section2_path': 'phone', |
| 125 |
'content_image1_path': 'user_ids.image_256', |
| 126 |
'content_image2_path': 'image_256', |
| 127 |
}) |
| 128 |
cls.static_campaign = cls.env['card.campaign'].with_user(cls.marketing_card_user).create({ |
| 129 |
'name': 'Simple Campaign', |
| 130 |
'card_template_id': cls.card_template.id, |
| 131 |
'post_suggestion': 'Come see my show!', |
| 132 |
'preview_record_ref': f'{cls.partners._name},{cls.partners[0].id}', |
| 133 |
'reward_message': """<p>Thanks for sharing!</p>""", |
| 134 |
'reward_target_url': f"{cls.env['card.campaign'].get_base_url()}/share-rewards/2039-sharer-badge/", |
| 135 |
'target_url': cls.env['card.campaign'].get_base_url(), |
| 136 |
}) |
| 137 |
|
| 138 |
@contextmanager |
| 139 |
def mock_datetime_and_now(self, mock_dt): |
| 140 |
with freeze_time(mock_dt), \ |
| 141 |
patch.object(self.env.cr, 'now', lambda: mock_dt): |
| 142 |
yield |