def test_cached_copy_is_missing(self): cache_path = os.path.join(config.VAR_DIR, dmarc.LOCAL_FILE_NAME) self.assertFalse(os.path.exists(cache_path)) new_path = dmarc.ensure_current_suffix_list() self.assertEqual(cache_path, new_path) with open(cache_path, 'r', encoding='utf-8') as fp: contents = fp.read() self.assertEqual(contents, 'abc') self.assertEqual( os.stat(new_path).st_mtime, (now() + as_timedelta(config.dmarc.cache_lifetime)).timestamp())
def test_cached_copy_is_good(self): cache_path = os.path.join(config.VAR_DIR, dmarc.LOCAL_FILE_NAME) with open(cache_path, 'w', encoding='utf-8') as fp: print('xyz', end='', file=fp) # The cache expires a day from now. expires = (now() + timedelta(days=1)).timestamp() os.utime(cache_path, (expires, expires)) new_path = dmarc.ensure_current_suffix_list() self.assertEqual(cache_path, new_path) with open(cache_path, 'r', encoding='utf-8') as fp: contents = fp.read() self.assertEqual(contents, 'xyz') self.assertEqual(os.stat(new_path).st_mtime, expires)
def test_cached_copy_is_missing_download_404s(self): # There's no cached file and we'll get a 404 with the .err file so # we'll have to fall back to our internal copy. cache_path = os.path.join(config.VAR_DIR, dmarc.LOCAL_FILE_NAME) self.assertFalse(os.path.exists(cache_path)) new_path = dmarc.ensure_current_suffix_list() self.assertEqual(cache_path, new_path) with open(cache_path, 'r', encoding='utf-8') as fp: contents = fp.read() # The contents is *not* equal to our dummy test data, but don't tie it # too closely to the in-tree file contents since that might change # when and if we update that. self.assertNotEqual(contents, 'abc') self.assertEqual( os.stat(new_path).st_mtime, (now() + as_timedelta(config.dmarc.cache_lifetime)).timestamp())
def test_cached_copy_is_expired(self): cache_path = os.path.join(config.VAR_DIR, dmarc.LOCAL_FILE_NAME) with open(cache_path, 'w', encoding='utf-8') as fp: print('xyz', end='', file=fp) # Expire the cache file. That way the current cached file will be # invalid and a new one will be downloaded. expires = (now() - timedelta(days=1)).timestamp() os.utime(cache_path, (expires, expires)) new_path = dmarc.ensure_current_suffix_list() self.assertEqual(cache_path, new_path) with open(cache_path, 'r', encoding='utf-8') as fp: contents = fp.read() self.assertEqual(contents, 'abc') self.assertEqual( os.stat(new_path).st_mtime, (now() + as_timedelta(config.dmarc.cache_lifetime)).timestamp())
def test_cached_copy_is_expired_download_404s(self): # Because the cached copy is out of date, we try to download the new # version. But that 404s so we end up continuing to use the cached # copy. cache_path = os.path.join(config.VAR_DIR, dmarc.LOCAL_FILE_NAME) with open(cache_path, 'w', encoding='utf-8') as fp: print('xyz', end='', file=fp) # Expire the cache file. That way the current cached file will be # invalid and a new one will be downloaded. expires = (now() - timedelta(days=1)).timestamp() os.utime(cache_path, (expires, expires)) new_path = dmarc.ensure_current_suffix_list() self.assertEqual(cache_path, new_path) with open(cache_path, 'r', encoding='utf-8') as fp: contents = fp.read() # The contents are from the cached file. self.assertEqual(contents, 'xyz') # The cached file timestamp doesn't change. self.assertEqual(os.stat(new_path).st_mtime, expires)