Files
odoo_source/addons/test_mass_mailing/tests/test_link_tracker.py
T
std-odoo b7e5eb639a [FIX] link_tracker: clean code and fix add a constraint
Purpose
=======
Add a unicity constraint (on the url, UTM campaign, medium and source).
As we must care about null values, we can not implement it in SQL
(possible in pure SQL, but we will need to create an index on the
columns, which is not possible in Odoo if we want to customize the
error message).

Clean the code, before the "search or create" behavior was implemented
in the create, causing strange behavior (creating a link tracker with
the same values as an existing one didn't raise an error, but open
the form view of the existing link tracker).

Task-2090344

closes odoo/odoo#48845

Related: odoo/enterprise#9608
Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com>
2021-05-17 13:28:43 +00:00

66 lines
2.2 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo.tests.common import users
from odoo.addons.test_mass_mailing.tests import common
class TestLinkTracker(common.TestMassMailCommon):
def setUp(self):
super(TestLinkTracker, self).setUp()
self.link = self.env['link.tracker'].search_or_create({
'url': 'https://www.example.com'
})
self.click = self.env['link.tracker.click'].create({
'link_id': self.link.id,
'ip': '100.00.00.00',
'country_id': self.env.ref('base.fr').id,
})
def test_add_link(self):
code = self.link.code
self.assertEqual(self.link.count, 1)
# click from a new IP should create a new entry
click = self.env['link.tracker.click'].sudo().add_click(
code,
ip='100.00.00.01',
country_code='BEL'
)
self.assertEqual(click.ip, '100.00.00.01')
self.assertEqual(click.country_id, self.env.ref('base.be'))
self.assertEqual(self.link.count, 2)
# click from same IP (even another country) does not create a new entry
click = self.env['link.tracker.click'].sudo().add_click(
code,
ip='100.00.00.01',
country_code='FRA'
)
self.assertEqual(click, None)
self.assertEqual(self.link.count, 2)
@users('user_marketing')
def test_add_link_mail_stat(self):
mailing = self.env['mailing.mailing'].create({'name': 'Test Mailing', "subject": "Hi!"})
code = self.link.code
self.assertEqual(self.link.count, 1)
stat = self.env['mailing.trace'].create({'mass_mailing_id': mailing.id})
self.assertFalse(stat.opened)
self.assertFalse(stat.clicked)
# click from a new IP should create a new entry and update stat when provided
click = self.env['link.tracker.click'].sudo().add_click(
code,
ip='100.00.00.01',
country_code='BEL',
mailing_trace_id=stat.id
)
self.assertEqual(self.link.count, 2)
self.assertEqual(click.mass_mailing_id, mailing)
self.assertTrue(stat.opened)
self.assertTrue(stat.clicked)