Skip to content

Commit

Permalink
Separate testing keycache and cleanup after test
Browse files Browse the repository at this point in the history
  • Loading branch information
phantominh authored and djw8605 committed May 31, 2024
1 parent 9bfbbba commit a9027a8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
22 changes: 19 additions & 3 deletions tests/test_create_scitoken.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class TestCreation(unittest.TestCase):
Test the creation of a simple SciToken
"""

def setUp(self):
def setUp(self):
self._private_key = generate_private_key(
public_exponent=65537,
key_size=2048,
Expand All @@ -41,11 +41,27 @@ def setUp(self):
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo
)
keycache = scitokens.utils.keycache.KeyCache.getinstance()
keycache.addkeyinfo("local", "sample_key", self._private_key.public_key())

# Force the keycache to create a cache in a new directory
self.tmp_dir = tempfile.mkdtemp()
self.old_xdg = os.environ.get('XDG_CACHE_HOME', None)
os.environ['XDG_CACHE_HOME'] = self.tmp_dir
# Clear the cache
self.keycache = scitokens.utils.keycache.KeyCache()
# Make sure it made the directory where I wanted it
self.assertTrue(self.keycache.cache_location.startswith(self.tmp_dir))
self.assertTrue(os.path.exists(self.keycache.cache_location))

self.keycache.addkeyinfo("local", "sample_key", self._private_key.public_key())
self._token = scitokens.SciToken(key = self._private_key, key_id="sample_key")
self._no_kid_token = scitokens.SciToken(key = self._private_key)

def tearDown(self):
shutil.rmtree(self.tmp_dir)
if self.old_xdg:
os.environ['XDG_CACHE_HOME'] = self.old_xdg
# Clean up, delete everything

def test_create(self):
"""
Test the creation of a simple SciToken.
Expand Down
12 changes: 11 additions & 1 deletion tests/test_with_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import os
import sys
import unittest
import tempfile
import shutil
from pathlib import Path

# Allow unittests to be run from within the project base.
Expand All @@ -29,6 +31,11 @@ class TestDeserialization(unittest.TestCase):
"""

def setUp(self):
# Force the keycache to create a cache in a new directory
self.tmp_dir = tempfile.mkdtemp()
self.old_xdg = os.environ.get('XDG_CACHE_HOME', None)
os.environ['XDG_CACHE_HOME'] = self.tmp_dir

with open(TESTS_DIR / 'simple_private_key.pem', 'rb') as key_file:
self.private_key = serialization.load_pem_private_key(
key_file.read(),
Expand All @@ -49,7 +56,10 @@ def setUp(self):
self.ec_public_numbers = self.ec_private_key.public_key().public_numbers()

def tearDown(self):
pass
shutil.rmtree(self.tmp_dir)
if self.old_xdg:
os.environ['XDG_CACHE_HOME'] = self.old_xdg
# Clean up, delete everything

def test_deserialization(self):
"""
Expand Down

0 comments on commit a9027a8

Please sign in to comment.