Skip to content

Commit 7f30b6b

Browse files
authored
DEV-1936 Remove default name from WorkPackageConfig (#22)
Signed-off-by: vince <[email protected]>
1 parent 397e863 commit 7f30b6b

File tree

4 files changed

+57
-43
lines changed

4 files changed

+57
-43
lines changed

changelog.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
# EAS Python client
22
## [0.16.0] - UNRELEASED
33
### Breaking Changes
4-
* None.
4+
* Updated `WorkPackageConfig` constructor to reorder the parameters and no longer provide a default value for `name`. A `name` must now be provided by the user.
55

66
### New Features
77
* Support specifying a `seed` in `ModelConfig` to allow reproducible scenarios
88
* Support for using access tokens for authentication that takes advantage of the new EAS personal access token authentication system.
99

1010
### Enhancements
1111
* Update requests restrictions to support all version 2 minor versions.
12+
* Removed check that prevented passing a `client_secret` for "password" grant_type.
1213

1314
### Fixes
1415
* None.

src/zepben/eas/client/eas_client.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def __init__(
6565
for. (Optional)
6666
:param username: The username used for account authentication. (Optional)
6767
:param password: The password used for account authentication. (Optional)
68-
:param access_token: The access token used for M2M authentication generate from Evolve App Server or Web Client. (Optional)
68+
:param access_token: The access token used for authentication, generated by Evolve App Server. (Optional)
6969
:param client_secret: The Auth0 client secret used for M2M authentication. (Optional)
7070
:param token_fetcher: A ZepbenTokenFetcher used to fetch auth tokens for access to the Evolve App Server.
7171
(Optional)
@@ -85,7 +85,7 @@ def __init__(
8585
self._verify_certificate = verify_certificate
8686
self._ca_filename = ca_filename
8787
self._access_token = access_token
88-
if protocol != "https" and (token_fetcher or client_id or access_token or username or password or client_secret):
88+
if protocol != "https" and (token_fetcher or client_id or access_token):
8989
raise ValueError(
9090
"Incompatible arguments passed to connect to secured Evolve App Server. "
9191
"Authentication tokens must be sent via https. "
@@ -94,24 +94,17 @@ def __init__(
9494
if access_token and (client_id or client_secret or username or password or token_fetcher):
9595
raise ValueError(
9696
"Incompatible arguments passed to connect to secured Evolve App Server. "
97-
"You cannot provide both an access_token and client_id, client_id + client_secret, username + password, or token_fetcher. "
98-
"Please provide either access_token or client_id + client_secret, client_id + username + password, or token_fetcher."
97+
"You cannot provide multiple types of authentication. "
98+
"When using an access_token, do not provide client_id, client_secret, username, password, or token_fetcher."
9999
)
100100

101-
if token_fetcher and (client_id or client_secret or username or password, access_token):
101+
if token_fetcher and (client_id or client_secret or username or password or access_token):
102102
raise ValueError(
103103
"Incompatible arguments passed to connect to secured Evolve App Server. "
104104
"You cannot provide both a token_fetcher and credentials, "
105105
"please provide either client_id, client_id + client_secret, username + password, access_token or token_fetcher."
106106
)
107107

108-
if client_secret and (username or password):
109-
raise ValueError(
110-
"Incompatible arguments passed to connect to secured Evolve App Server. "
111-
"You cannot provide both a client_secret and username/password, "
112-
"please provide either client_id + client_secret or client_id + username + password."
113-
)
114-
115108
if client_id:
116109
self._token_fetcher = create_token_fetcher(
117110
conf_address=f"{self._protocol}://{self._host}:{self._port}/api/config/auth",
@@ -177,9 +170,8 @@ def _get_request_headers(self, content_type: str = "application/json") -> dict:
177170
headers = {"content-type": content_type}
178171
if self._access_token:
179172
headers["authorization"] = f"Bearer {self._access_token}"
180-
elif self._token_fetcher:
173+
elif self._token_fetcher:
181174
headers["authorization"] = self._token_fetcher.fetch_token()
182-
183175
return headers
184176

185177
def run_hosting_capacity_work_package(self, work_package: WorkPackageConfig):

src/zepben/eas/client/work_package.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ class ResultProcessorConfig:
225225
@dataclass
226226
class WorkPackageConfig:
227227
""" A data class representing the configuration for a hosting capacity work package """
228+
name: str
228229
feeders: List[str]
229230
years: List[int]
230231
scenarios: List[str]
@@ -233,7 +234,6 @@ class WorkPackageConfig:
233234
generator_config: Optional[GeneratorConfig] = None
234235
executor_config: Optional[object] = None
235236
result_processor_config: Optional[ResultProcessorConfig] = None
236-
name: str = ""
237237

238238

239239
@dataclass

test/test_eas_client.py

Lines changed: 48 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
# License, v. 2.0. If a copy of the MPL was not distributed with this
55
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
66
import random
7-
import re
87
import ssl
98
import string
109
from datetime import datetime
@@ -87,6 +86,20 @@ def test_get_request_headers_adds_access_token_in_auth_header():
8786
assert headers["authorization"] == f"Bearer {mock_access_token}"
8887

8988

89+
@mock.patch("zepben.auth.client.zepben_token_fetcher.ZepbenTokenFetcher.fetch_token", return_value="test_token3")
90+
def test_get_request_headers_adds_token_from_token_fetcher_in_auth_header(_):
91+
eas_client = EasClient(
92+
mock_host,
93+
mock_port,
94+
token_fetcher=ZepbenTokenFetcher(audience="fake", token_endpoint="unused")
95+
)
96+
97+
assert eas_client is not None
98+
assert eas_client._token_fetcher is not None
99+
headers = eas_client._get_request_headers()
100+
assert headers["authorization"] == "test_token3"
101+
102+
90103
@mock.patch("zepben.auth.client.zepben_token_fetcher.requests.get", side_effect=lambda *args, **kwargs: MockResponse(
91104
{"authType": "AUTH0", "audience": mock_audience, "issuer": "test_issuer"}, 200))
92105
def test_create_eas_client_with_password_success(_):
@@ -162,6 +175,7 @@ def test_run_hosting_capacity_work_package_no_verify_success(httpserver: HTTPSer
162175
httpserver.expect_oneshot_request("/api/graphql").respond_with_json({"data": {"runWorkPackage": "workPackageId"}})
163176
res = eas_client.run_hosting_capacity_work_package(
164177
WorkPackageConfig(
178+
"wp_name",
165179
["feeder"],
166180
[1],
167181
["scenario"],
@@ -188,6 +202,7 @@ def test_run_hosting_capacity_work_package_invalid_certificate_failure(ca: trust
188202
with pytest.raises(ssl.SSLError):
189203
eas_client.run_hosting_capacity_work_package(
190204
WorkPackageConfig(
205+
"wp_name",
191206
["feeder"],
192207
[1],
193208
["scenario"],
@@ -211,6 +226,7 @@ def test_run_hosting_capacity_work_package_valid_certificate_success(ca: trustme
211226
{"data": {"runWorkPackage": "workPackageId"}})
212227
res = eas_client.run_hosting_capacity_work_package(
213228
WorkPackageConfig(
229+
"wp_name",
214230
["feeder"],
215231
[1],
216232
["scenario"],
@@ -392,27 +408,27 @@ def test_raises_error_if_token_fetcher_and_creds_configured(httpserver: HTTPServ
392408
)
393409

394410

395-
def test_raises_error_if_secret_and_creds_configured(httpserver: HTTPServer):
396-
with pytest.raises(ValueError, match="You cannot provide both a client_secret and username/password"):
397-
EasClient(
398-
LOCALHOST,
399-
httpserver.port,
400-
protocol="https",
401-
client_id=mock_client_id,
402-
client_secret=mock_client_secret,
403-
username=mock_username,
404-
)
405-
406-
with pytest.raises(ValueError,
407-
match="You cannot provide both a client_secret and username/password"):
408-
EasClient(
409-
LOCALHOST,
410-
httpserver.port,
411-
protocol="https",
412-
client_id=mock_client_id,
413-
client_secret=mock_client_secret,
414-
password=mock_password,
415-
)
411+
@mock.patch("zepben.auth.client.zepben_token_fetcher.requests.get", side_effect=lambda *args, **kwargs: MockResponse(
412+
{"authType": "AUTH0", "audience": mock_audience, "issuer": "test_issuer"}, 200))
413+
def test_allows_secret_and_creds_configured(httpserver: HTTPServer):
414+
eas_client = EasClient(
415+
mock_host,
416+
mock_port,
417+
protocol="https",
418+
client_id=mock_client_id,
419+
client_secret=mock_client_secret,
420+
username=mock_username,
421+
password=mock_password
422+
)
423+
assert eas_client is not None
424+
assert eas_client._token_fetcher is not None
425+
assert eas_client._token_fetcher.token_request_data["grant_type"] == "password"
426+
assert eas_client._token_fetcher.token_request_data["client_id"] == mock_client_id
427+
assert eas_client._token_fetcher.token_request_data["username"] == mock_username
428+
assert eas_client._token_fetcher.token_request_data["password"] == mock_password
429+
assert eas_client._token_fetcher.token_request_data["client_secret"] == mock_client_secret
430+
assert eas_client._host == mock_host
431+
assert eas_client._port == mock_port
416432

417433

418434
def test_raises_error_if_access_token_and_creds_configured(httpserver: HTTPServer):
@@ -424,7 +440,8 @@ def test_raises_error_if_access_token_and_creds_configured(httpserver: HTTPServe
424440
access_token=mock_access_token,
425441
username=mock_username,
426442
)
427-
assert "You cannot provide both an access_token and client_id, client_id + client_secret, username + password, or token_fetcher." in str(error_message_for_username.value)
443+
assert "Incompatible arguments passed to connect to secured Evolve App Server. You cannot provide multiple types of authentication. When using an access_token, do not provide client_id, client_secret, username, password, or token_fetcher." in str(
444+
error_message_for_username.value)
428445

429446
with pytest.raises(ValueError) as error_message_for_password:
430447
EasClient(
@@ -434,7 +451,8 @@ def test_raises_error_if_access_token_and_creds_configured(httpserver: HTTPServe
434451
access_token=mock_access_token,
435452
password=mock_password,
436453
)
437-
assert "You cannot provide both an access_token and client_id, client_id + client_secret, username + password, or token_fetcher." in str(error_message_for_password.value)
454+
assert "Incompatible arguments passed to connect to secured Evolve App Server. You cannot provide multiple types of authentication. When using an access_token, do not provide client_id, client_secret, username, password, or token_fetcher." in str(
455+
error_message_for_password.value)
438456

439457

440458
def test_raises_error_if_access_token_and_token_fetcher_configured(httpserver: HTTPServer):
@@ -446,7 +464,8 @@ def test_raises_error_if_access_token_and_token_fetcher_configured(httpserver: H
446464
access_token=mock_access_token,
447465
token_fetcher=ZepbenTokenFetcher(audience="test", auth_method="test", token_endpoint="test")
448466
)
449-
assert "You cannot provide both an access_token and client_id, client_id + client_secret, username + password, or token_fetcher." in str(error_message_for_username.value)
467+
assert "Incompatible arguments passed to connect to secured Evolve App Server. You cannot provide multiple types of authentication. When using an access_token, do not provide client_id, client_secret, username, password, or token_fetcher." in str(
468+
error_message_for_username.value)
450469

451470

452471
def test_raises_error_if_access_token_and_client_id_configured(httpserver: HTTPServer):
@@ -458,7 +477,8 @@ def test_raises_error_if_access_token_and_client_id_configured(httpserver: HTTPS
458477
access_token=mock_access_token,
459478
client_id=mock_client_id
460479
)
461-
assert "You cannot provide both an access_token and client_id, client_id + client_secret, username + password, or token_fetcher." in str(error_message_for_username.value)
480+
assert "Incompatible arguments passed to connect to secured Evolve App Server. You cannot provide multiple types of authentication. When using an access_token, do not provide client_id, client_secret, username, password, or token_fetcher." in str(
481+
error_message_for_username.value)
462482

463483

464484
def test_raises_error_if_access_token_and_client_secret_configured(httpserver: HTTPServer):
@@ -470,4 +490,5 @@ def test_raises_error_if_access_token_and_client_secret_configured(httpserver: H
470490
access_token=mock_access_token,
471491
client_secret=mock_client_secret
472492
)
473-
assert "You cannot provide both an access_token and client_id, client_id + client_secret, username + password, or token_fetcher." in str(error_message_for_username.value)
493+
assert "Incompatible arguments passed to connect to secured Evolve App Server. You cannot provide multiple types of authentication. When using an access_token, do not provide client_id, client_secret, username, password, or token_fetcher." in str(
494+
error_message_for_username.value)

0 commit comments

Comments
 (0)