Skip to content

Commit

Permalink
Fix authentification unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rle-earthdaily committed Jul 4, 2024
1 parent ba17ab6 commit 01e2cc0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 33 deletions.
5 changes: 2 additions & 3 deletions earthdaily/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ def EarthDataStore(
"""
Open earth data store connection to allow for datacube requests.
Try to read Earth Data Store credentials from multiple sources, in the following order:
- from input credentials stored in a JOSN file
- from input credentials stored in a given JSON file
- from input credentials stored in a given TOML file
- from environement variables
- from a given credentials TOML file and a given profile
- from a given credentials TOML file and the "default" profile
- from the $HOME/.earthdaily/credentials TOML file and a given profile
- from the $HOME/.earthdaily/credentials TOML file and the "default" profile
Expand Down
19 changes: 11 additions & 8 deletions earthdaily/earthdatastore/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,10 +413,9 @@ def from_credentials(
"""
Secondary Constructor.
Try to read Earth Data Store credentials from multiple sources, in the following order:
- from input credentials stored in a JOSN file
- from input credentials stored in a given JSON file
- from input credentials stored in a given TOML file
- from environement variables
- from a given credentials TOML file and a given profile
- from a given credentials TOML file and the "default" profile
- from the $HOME/.earthdaily/credentials TOML file and a given profile
- from the $HOME/.earthdaily/credentials TOML file and the "default" profile
Expand Down Expand Up @@ -455,10 +454,9 @@ def read_credentials(
) -> "Auth":
"""
Try to read Earth Data Store credentials from multiple sources, in the following order:
- from input credentials stored in a JOSN file
- from input credentials stored in a given JSON file
- from input credentials stored in a given TOML file
- from environement variables
- from a given credentials TOML file and a given profile
- from a given credentials TOML file and the "default" profile
- from the $HOME/.earthdaily/credentials TOML file and a given profile
- from the $HOME/.earthdaily/credentials TOML file and the "default" profile
Expand All @@ -479,6 +477,11 @@ def read_credentials(
if json_path is not None:
config = cls.read_credentials_from_json(json_path=json_path)

elif toml_path is not None:
config = cls.read_credentials_from_toml(
toml_path=toml_path, profile=profile
)

elif (
os.getenv("EDS_AUTH_URL")
and os.getenv("EDS_SECRET")
Expand All @@ -487,8 +490,8 @@ def read_credentials(
config = cls.read_credentials_from_environment()

else:
if toml_path is None:
toml_path = Path.home() / ".earthdaily/credentials"

toml_path = Path.home() / ".earthdaily/credentials"

if profile is None:
profile = "default"
Expand Down
25 changes: 3 additions & 22 deletions tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,34 +42,15 @@ def tearDown(self) -> None:
def test_from_json(self) -> None:
eds = EarthDataStore(json_path = self.json_path)

def test_from_input_profile(self) -> None:
eds = EarthDataStore(toml_path = self.toml_path, profile = "test_profile")

def test_from_environment(self) -> None:
# Ensure environment variables are set
os.environ["EDS_AUTH_URL"] = self.credentials["EDS_AUTH_URL"]
os.environ["EDS_SECRET"] = self.credentials["EDS_SECRET"]
os.environ["EDS_CLIENT_ID"] = self.credentials["EDS_CLIENT_ID"]

eds = EarthDataStore()

def test_from_default_profile(self) -> None:
# Ensure environment variables are unset
if "EDS_AUTH_URL" in os.environ:
del os.environ["EDS_AUTH_URL"]
if "EDS_SECRET" in os.environ:
del os.environ["EDS_SECRET"]
if "EDS_CLIENT_ID" in os.environ:
del os.environ["EDS_CLIENT_ID"]

eds = EarthDataStore()

def test_from_input_profile(self) -> None:
# Ensure environment variables are unset
if "EDS_AUTH_URL" in os.environ:
del os.environ["EDS_AUTH_URL"]
if "EDS_SECRET" in os.environ:
del os.environ["EDS_SECRET"]
if "EDS_CLIENT_ID" in os.environ:
del os.environ["EDS_CLIENT_ID"]
eds = EarthDataStore(toml_path = self.toml_path, profile = "test_profile")


if __name__ == "__main__":
Expand Down

0 comments on commit 01e2cc0

Please sign in to comment.