|
| 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