Skip to content

Commit 660c47d

Browse files
committed
Authenticate and generate token for AgavePy
1 parent 45cfcf1 commit 660c47d

File tree

8 files changed

+86
-3
lines changed

8 files changed

+86
-3
lines changed

.github/workflows/build-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jobs:
66
strategy:
77
fail-fast: false
88
matrix:
9-
python-version: ["3.9", "3.10", "3.11"]
9+
python-version: ["3.9"]
1010
poetry-version: ["1.6.1"]
1111
os: [ubuntu-latest]
1212
runs-on: ${{ matrix.os }}

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ To run the unit test
122122
poetry run pytest -v
123123
```
124124

125+
## Known Issues
126+
127+
The project only works on `Python 3.9` due to AgavePy Issue [#125](https://github.com/TACC/agavepy/issues/125).
128+
125129

126130
## License
127131

dapi/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@
1515
```
1616
1717
"""
18+
from . import auth
19+
from . import db
1820
from . import jobs

dapi/auth/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .auth import init

dapi/auth/auth.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from agavepy.agave import Agave
2+
from collections.abc import Mapping
3+
4+
5+
def init(username, password):
6+
"""
7+
Initialize an Agave object with a new client and an active token.
8+
9+
Args:
10+
username (str): The username.
11+
password (str): The password.
12+
13+
Returns:
14+
object: The Agave object.
15+
"""
16+
# Authenticate with Agave
17+
ag = Agave(
18+
base_url="https://agave.designsafe-ci.org", username=username, password=password
19+
)
20+
# Create a new client
21+
new_client = ag.clients_create()
22+
# create a new ag object with the new client, at this point ag will have a new token
23+
ag = Agave(
24+
base_url="https://agave.designsafe-ci.org",
25+
username=username,
26+
password=password,
27+
api_key=new_client["api_key"],
28+
api_secret=new_client["api_secret"],
29+
)
30+
return ag

dapi/jobs/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
`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).
2+
`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).
33
44
55
## Features

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ readme = "README.md"
1111
packages = [{include = "dapi"}]
1212

1313
[tool.poetry.dependencies]
14-
python = "^3.9"
14+
python = "3.9.*"
1515
tqdm = "^4.66.1"
1616
agavepy = ">0.9.5"
1717
exceptiongroup = "^1.1.3"

tests/auth/test_auth.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import unittest
2+
from unittest.mock import patch, MagicMock
3+
from dapi.auth.auth import init
4+
5+
6+
class TestAuthInit(unittest.TestCase):
7+
@patch("dapi.auth.auth.Agave")
8+
def test_init_success(self, mock_agave):
9+
# Setup
10+
username = "test_user"
11+
password = "test_password"
12+
mock_agave_obj = MagicMock()
13+
mock_agave.return_value = mock_agave_obj
14+
mock_agave_obj.clients_create.return_value = {
15+
"api_key": "test_api_key",
16+
"api_secret": "test_api_secret",
17+
}
18+
19+
# Execute
20+
result = init(username, password)
21+
22+
# Verify
23+
mock_agave.assert_called_with(
24+
base_url="https://agave.designsafe-ci.org",
25+
username=username,
26+
password=password,
27+
api_key="test_api_key",
28+
api_secret="test_api_secret",
29+
)
30+
self.assertIsInstance(result, MagicMock)
31+
32+
@patch("dapi.auth.auth.Agave")
33+
def test_init_invalid_credentials(self, mock_agave):
34+
# Setup
35+
username = "invalid_user"
36+
password = "invalid_password"
37+
mock_agave.side_effect = Exception("Invalid credentials")
38+
39+
# Execute & Verify
40+
with self.assertRaises(Exception):
41+
init(username, password)
42+
43+
44+
# This allows running the test from the command line
45+
if __name__ == "__main__":
46+
unittest.main()

0 commit comments

Comments
 (0)