-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Authenticate and generate token for AgavePy
- Loading branch information
Showing
8 changed files
with
86 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,4 +15,6 @@ | |
``` | ||
""" | ||
from . import auth | ||
from . import db | ||
from . import jobs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .auth import init |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |