From 46538fa620d772461200fbf9cad4cc5337874f86 Mon Sep 17 00:00:00 2001 From: Stephane Bortzmeyer Date: Wed, 9 Nov 2022 15:17:03 +0000 Subject: [PATCH] * Tests updated * Correct handling of forced refresh --- ianardap.py | 9 ++++++++- test_ianardap.py | 14 +++++++------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/ianardap.py b/ianardap.py index 2bb401f..b7836d2 100755 --- a/ianardap.py +++ b/ianardap.py @@ -49,13 +49,14 @@ def parse_expires(h): class IanaRDAPDatabase(): - def __init__(self, category="domains", maxage=MAXAGE, cachedir=CACHE, + def __init__(self, category="domains", maxage=None, cachedir=CACHE, pickleformat=False): """Retrieves the IANA database, if not already cached. maxage is in hours. The cachedir is a directory (it will be created if not already existant). pickleformat is not the default because it is not really faster *and* it introduces security risks if someone can write in the file (see the documentation of the module). + """ cache_valid = False @@ -69,6 +70,12 @@ file (see the documentation of the module). self.cachefile = cachefile + ".json" self.lockname = self.cachefile + ".lock" 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 tests = 0 errmsg = "No error" diff --git a/test_ianardap.py b/test_ianardap.py index 26d1fb2..0dfdb88 100644 --- a/test_ianardap.py +++ b/test_ianardap.py @@ -17,13 +17,13 @@ def test_basic(): len(database.services) > 1000 def test_alternative_cache(): - tmpfile = tempfile.NamedTemporaryFile(suffix=".testianacache", delete=False) - database = ianardap.IanaRDAPDatabase(cachefile=tmpfile.name, maxage=0) - assert os.path.exists(tmpfile.name) and \ - datetime.datetime.fromtimestamp(os.path.getmtime(tmpfile.name)) > \ + tmpdir = tempfile.TemporaryDirectory(suffix=".testianacaches") + database = ianardap.IanaRDAPDatabase(cachedir=tmpdir.name, maxage=0) + assert os.path.exists(database.cachefile) and \ + datetime.datetime.fromtimestamp(os.path.getmtime(database.cachefile)) > \ (datetime.datetime.now() - datetime.timedelta(minutes=1)) - os.remove(tmpfile.name) - os.remove(tmpfile.name + ".json.lock") + os.remove(database.cachefile) + os.remove(database.cachefile + ".lock") def test_refresh(): # Force a resfresh @@ -35,7 +35,7 @@ def test_refresh(): def test_find_exists(): database = ianardap.IanaRDAPDatabase() server = database.find("www.foobar.ar") - assert server == "https://rdap.nic.ar/" + assert server == ["https://rdap.nic.ar/"] def test_find_not_exists(): database = ianardap.IanaRDAPDatabase()