Skip to content

Commit

Permalink
Authenticate and generate token for AgavePy
Browse files Browse the repository at this point in the history
  • Loading branch information
kks32 committed Jan 21, 2024
1 parent 45cfcf1 commit 660c47d
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.9", "3.10", "3.11"]
python-version: ["3.9"]
poetry-version: ["1.6.1"]
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ To run the unit test
poetry run pytest -v
```

## Known Issues

The project only works on `Python 3.9` due to AgavePy Issue [#125](https://github.com/TACC/agavepy/issues/125).


## License

Expand Down
2 changes: 2 additions & 0 deletions dapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@
```
"""
from . import auth
from . import db
from . import jobs
1 change: 1 addition & 0 deletions dapi/auth/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .auth import init
30 changes: 30 additions & 0 deletions dapi/auth/auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from agavepy.agave import Agave
from collections.abc import Mapping


def init(username, password):
"""
Initialize an Agave object with a new client and an active token.
Args:
username (str): The username.
password (str): The password.
Returns:
object: The Agave object.
"""
# Authenticate with Agave
ag = Agave(
base_url="https://agave.designsafe-ci.org", username=username, password=password
)
# Create a new client
new_client = ag.clients_create()
# create a new ag object with the new client, at this point ag will have a new token
ag = Agave(
base_url="https://agave.designsafe-ci.org",
username=username,
password=password,
api_key=new_client["api_key"],
api_secret=new_client["api_secret"],
)
return ag
2 changes: 1 addition & 1 deletion dapi/jobs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
`dapi` is a library that simplifies the process of submitting, running, and monitoring [TAPIS v2 / AgavePy](https://agavepy.readthedocs.io/en/latest/index.html) jobs on [DesignSafe](https://designsafe-ci.org) via [Jupyter Notebooks](https://jupyter.designsafe-ci.org).
`dapi` job submodule simplifies the process of submitting, running, and monitoring [TAPIS v2 / AgavePy](https://agavepy.readthedocs.io/en/latest/index.html) jobs on [DesignSafe](https://designsafe-ci.org) via [Jupyter Notebooks](https://jupyter.designsafe-ci.org).
## Features
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ readme = "README.md"
packages = [{include = "dapi"}]

[tool.poetry.dependencies]
python = "^3.9"
python = "3.9.*"
tqdm = "^4.66.1"
agavepy = ">0.9.5"
exceptiongroup = "^1.1.3"
Expand Down
46 changes: 46 additions & 0 deletions tests/auth/test_auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import unittest
from unittest.mock import patch, MagicMock
from dapi.auth.auth import init


class TestAuthInit(unittest.TestCase):
@patch("dapi.auth.auth.Agave")
def test_init_success(self, mock_agave):
# Setup
username = "test_user"
password = "test_password"
mock_agave_obj = MagicMock()
mock_agave.return_value = mock_agave_obj
mock_agave_obj.clients_create.return_value = {
"api_key": "test_api_key",
"api_secret": "test_api_secret",
}

# Execute
result = init(username, password)

# Verify
mock_agave.assert_called_with(
base_url="https://agave.designsafe-ci.org",
username=username,
password=password,
api_key="test_api_key",
api_secret="test_api_secret",
)
self.assertIsInstance(result, MagicMock)

@patch("dapi.auth.auth.Agave")
def test_init_invalid_credentials(self, mock_agave):
# Setup
username = "invalid_user"
password = "invalid_password"
mock_agave.side_effect = Exception("Invalid credentials")

# Execute & Verify
with self.assertRaises(Exception):
init(username, password)


# This allows running the test from the command line
if __name__ == "__main__":
unittest.main()

0 comments on commit 660c47d

Please sign in to comment.