Start odoo-bin shell with `--geoip_country_db='/i/dont/exist'` and type
the following code:
from odoo.http import GeoIP
GeoIP('8.8.8.8').country_code
TypeError: The country method cannot be used with the GeoLite2-City
database
There are two "things" here: the raw country and city entries in the
databases and the rich python country and city objects returned when an
entry was found and parsed.
Then there are two "readers", a reader capable of reading and parsing
entries from the city database and **another** reader for the country.
The two readers are **only** compatible with their specific database:
the city reader cannot read or parse information from the country
database and vis-versa. That's the bug reported by the TypeError that
this commit fixes.
But, the two python instances created from parsing the entries of
respecting databases with respecting parsers **do** share a same API:
the city record class inherits from the country record class.
# Valid
city_db = geoip2.database.Reader(config['geoip_city_db'])
city_record = city_db.city('8.8.8.8')
city_record.country.name # works, a city record also
# holds country attributes
# Invalid
city_db = geoip2.database.Reader(config['geoip_city_db'])
country_record = city_db.country('8.8.8.8') # TypeError
closes odoo/odoo#109095
Signed-off-by: Julien Castiaux <juc@odoo.com>
50 lines
2.2 KiB
Python
50 lines
2.2 KiB
Python
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
|
|
|
from unittest.mock import patch
|
|
|
|
import odoo
|
|
from odoo.http import Session
|
|
from odoo.tests.common import HttpCase
|
|
from odoo.tools.func import lazy_property
|
|
from odoo.addons.test_http.utils import MemoryGeoipResolver, MemorySessionStore
|
|
|
|
|
|
class TestHttpBase(HttpCase):
|
|
@classmethod
|
|
def setUpClass(cls):
|
|
super().setUpClass()
|
|
geoip_resolver = MemoryGeoipResolver()
|
|
session_store = MemorySessionStore(session_class=Session)
|
|
|
|
lazy_property.reset_all(odoo.http.root)
|
|
cls.addClassCleanup(lazy_property.reset_all, odoo.http.root)
|
|
cls.classPatch(odoo.conf, 'server_wide_modules', ['base', 'web', 'test_http'])
|
|
cls.classPatch(odoo.http.Application, 'session_store', session_store)
|
|
cls.classPatch(odoo.http.Application, 'geoip_city_db', geoip_resolver)
|
|
cls.classPatch(odoo.http.Application, 'geoip_country_db', geoip_resolver)
|
|
|
|
def setUp(self):
|
|
super().setUp()
|
|
odoo.http.root.session_store.store.clear()
|
|
|
|
def db_url_open(self, url, *args, allow_redirects=False, **kwargs):
|
|
return self.url_open(url, *args, allow_redirects=allow_redirects, **kwargs)
|
|
|
|
def nodb_url_open(self, url, *args, allow_redirects=False, **kwargs):
|
|
with patch('odoo.http.db_list') as db_list, \
|
|
patch('odoo.http.db_filter') as db_filter:
|
|
db_list.return_value = []
|
|
db_filter.return_value = []
|
|
return self.url_open(url, *args, allow_redirects=allow_redirects, **kwargs)
|
|
|
|
def multidb_url_open(self, url, *args, allow_redirects=False, dblist=(), **kwargs):
|
|
dblist = dblist or self.db_list
|
|
assert len(dblist) >= 2, "There should be at least 2 databases"
|
|
with patch('odoo.http.db_list') as db_list, \
|
|
patch('odoo.http.db_filter') as db_filter, \
|
|
patch('odoo.http.Registry') as Registry:
|
|
db_list.return_value = dblist
|
|
db_filter.side_effect = lambda dbs, host=None: [db for db in dbs if db in dblist]
|
|
Registry.return_value = self.registry
|
|
return self.url_open(url, *args, allow_redirects=allow_redirects, **kwargs)
|