* Tests updated

* Correct handling of forced refresh
This commit is contained in:
Stephane Bortzmeyer 2022-11-09 15:17:03 +00:00
parent a117da20b1
commit 46538fa620
2 changed files with 15 additions and 8 deletions

View File

@ -49,13 +49,14 @@ def parse_expires(h):
class IanaRDAPDatabase(): class IanaRDAPDatabase():
def __init__(self, category="domains", maxage=MAXAGE, cachedir=CACHE, def __init__(self, category="domains", maxage=None, cachedir=CACHE,
pickleformat=False): pickleformat=False):
"""Retrieves the IANA database, if not already cached. maxage is in """Retrieves the IANA database, if not already cached. maxage is in
hours. The cachedir is a directory (it will be created if not already hours. The cachedir is a directory (it will be created if not already
existant). pickleformat is not the default because it is not really existant). pickleformat is not the default because it is not really
faster *and* it introduces security risks if someone can write in the faster *and* it introduces security risks if someone can write in the
file (see the documentation of the module). file (see the documentation of the module).
""" """
cache_valid = False cache_valid = False
@ -69,6 +70,12 @@ file (see the documentation of the module).
self.cachefile = cachefile + ".json" self.cachefile = cachefile + ".json"
self.lockname = self.cachefile + ".lock" self.lockname = self.cachefile + ".lock"
self.expirationfile = self.cachefile + ".expires" self.expirationfile = self.cachefile + ".expires"
if maxage is not None:
with open(self.expirationfile, 'w'):
self.expirationtime = time.mktime((datetime.datetime.now() + \
datetime.timedelta(hours=maxage)).timetuple())
os.utime(self.expirationfile,
times = (self.expirationtime, self.expirationtime))
loaded = False loaded = False
tests = 0 tests = 0
errmsg = "No error" errmsg = "No error"

View File

@ -17,13 +17,13 @@ def test_basic():
len(database.services) > 1000 len(database.services) > 1000
def test_alternative_cache(): def test_alternative_cache():
tmpfile = tempfile.NamedTemporaryFile(suffix=".testianacache", delete=False) tmpdir = tempfile.TemporaryDirectory(suffix=".testianacaches")
database = ianardap.IanaRDAPDatabase(cachefile=tmpfile.name, maxage=0) database = ianardap.IanaRDAPDatabase(cachedir=tmpdir.name, maxage=0)
assert os.path.exists(tmpfile.name) and \ assert os.path.exists(database.cachefile) and \
datetime.datetime.fromtimestamp(os.path.getmtime(tmpfile.name)) > \ datetime.datetime.fromtimestamp(os.path.getmtime(database.cachefile)) > \
(datetime.datetime.now() - datetime.timedelta(minutes=1)) (datetime.datetime.now() - datetime.timedelta(minutes=1))
os.remove(tmpfile.name) os.remove(database.cachefile)
os.remove(tmpfile.name + ".json.lock") os.remove(database.cachefile + ".lock")
def test_refresh(): def test_refresh():
# Force a resfresh # Force a resfresh
@ -35,7 +35,7 @@ def test_refresh():
def test_find_exists(): def test_find_exists():
database = ianardap.IanaRDAPDatabase() database = ianardap.IanaRDAPDatabase()
server = database.find("www.foobar.ar") server = database.find("www.foobar.ar")
assert server == "https://rdap.nic.ar/" assert server == ["https://rdap.nic.ar/"]
def test_find_not_exists(): def test_find_not_exists():
database = ianardap.IanaRDAPDatabase() database = ianardap.IanaRDAPDatabase()