Files
odoo_source/addons/website/tests/test_get_current_website.py
T
Romain Derie bb65f518c3 [FIX] website: empty website domain correctly in tests
Emptying the domain should be setting it to False, not empty string,
which is saved in DB as such.

This was not problematic at all before but since [1] the domain must be
unique. As we removed the "multi website by domain" feature, we then
were able to add that constraint.
But since then, having two website with domain == '' would raise the
constraint error, while null values (False in python) would not.

[1]: https://github.com/odoo/odoo/commit/507db4e179514d171ec82e8ea0cbaf2323a6c30d

runbot-5041

closes odoo/odoo#100430

Signed-off-by: Quentin Smetz (qsm) <qsm@odoo.com>
2022-09-17 00:52:04 +02:00

88 lines
3.8 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo.tests import tagged
from odoo.tests.common import TransactionCase
@tagged('post_install', '-at_install')
class TestGetCurrentWebsite(TransactionCase):
def setUp(self):
# Unlink unused website(s) to avoid messing with the expected results
self.website = self.env.ref('website.default_website')
for w in self.env['website'].search([('id', '!=', self.website.id)]):
try:
# Website are impossible to delete most often than not, as if
# there is critical business data linked to it, it will prevent
# the unlink. Could easily happen with a bridge module adding
# some custom data.
w.unlink()
except Exception:
pass
def test_01_get_current_website_id(self):
"""Make sure `_get_current_website_id works`."""
Website = self.env['website']
# clean initial state
website1 = self.website
website1.domain = False
website2 = Website.create({'name': 'My Website 2'})
# CASE: no domain: get first
self.assertEqual(Website._get_current_website_id(''), website1.id)
# setup domain
website1.domain = 'my-site-1.fr'
website2.domain = 'https://my2ndsite.com:80'
# CASE: domain set: get matching domain
self.assertEqual(Website._get_current_website_id('my-site-1.fr'), website1.id)
# CASE: domain set: get matching domain (scheme and port supported)
self.assertEqual(Website._get_current_website_id('my-site-1.fr:8069'), website1.id)
self.assertEqual(Website._get_current_website_id('my2ndsite.com:80'), website2.id)
self.assertEqual(Website._get_current_website_id('my2ndsite.com:8069'), website2.id)
self.assertEqual(Website._get_current_website_id('my2ndsite.com'), website2.id)
# CASE: domain set, wrong domain: get first
self.assertEqual(Website._get_current_website_id('test.com'), website1.id)
# CASE: subdomain: not supported
self.assertEqual(Website._get_current_website_id('www.my2ndsite.com'), website1.id)
# CASE: domain set: get by domain in priority
self.assertEqual(Website._get_current_website_id('my2ndsite.com'), website2.id)
self.assertEqual(Website._get_current_website_id('my-site-1.fr'), website1.id)
# CASE: overlapping domain: get exact match
website1.domain = 'site-1.com'
website2.domain = 'even-better-site-1.com'
self.assertEqual(Website._get_current_website_id('site-1.com'), website1.id)
self.assertEqual(Website._get_current_website_id('even-better-site-1.com'), website2.id)
# CASE: case insensitive
website1.domain = 'Site-1.com'
website2.domain = 'Even-Better-site-1.com'
self.assertEqual(Website._get_current_website_id('sitE-1.com'), website1.id)
self.assertEqual(Website._get_current_website_id('even-beTTer-site-1.com'), website2.id)
# CASE: same domain, different port
website1.domain = 'site-1.com:80'
website2.domain = 'site-1.com:81'
self.assertEqual(Website._get_current_website_id('site-1.com:80'), website1.id)
self.assertEqual(Website._get_current_website_id('site-1.com:81'), website2.id)
self.assertEqual(Website._get_current_website_id('site-1.com:82'), website1.id)
self.assertEqual(Website._get_current_website_id('site-1.com'), website1.id)
def test_02_signup_user_website_id(self):
website = self.website
website.specific_user_account = True
user = self.env['res.users'].create({'website_id': website.id, 'login': 'sad@mail.com', 'name': 'Hope Fully'})
self.assertTrue(user.website_id == user.partner_id.website_id == website)