Skip to content

Commit

Permalink
issue #254 move clear cache and improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ElienVandermaesenVITO authored and soxofaan committed Jan 17, 2025
1 parent b09416e commit 20b039d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 12 deletions.
4 changes: 2 additions & 2 deletions openeo/rest/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,14 +404,14 @@ def authenticate_basic(self, username: Optional[str] = None, password: Optional[
if username is None:
raise OpenEoClientException("No username/password given or found.")

self._capabilities_cache.clear()
resp = self.get(
'/credentials/basic',
# /credentials/basic is the only endpoint that expects a Basic HTTP auth
auth=HTTPBasicAuth(username, password)
).json()
# Switch to bearer based authentication in further requests.
self.auth = BasicBearerAuth(access_token=resp["access_token"])
self._capabilities_cache.clear()
return self

def _get_oidc_provider(
Expand Down Expand Up @@ -473,7 +473,6 @@ def _get_oidc_provider(
f"No OIDC provider given. Using first provider {provider_id!r} as advertised by backend."
)

self._capabilities_cache.clear()
provider_info = OidcProviderInfo.from_dict(provider) if parse_info else None

return provider_id, provider_info
Expand Down Expand Up @@ -547,6 +546,7 @@ def _authenticate_oidc(
_log.warning("No OIDC refresh token to store.")
token = tokens.access_token
self.auth = OidcBearerAuth(provider_id=provider_id, access_token=token)
self._capabilities_cache.clear()
self._oidc_auth_renewer = oidc_auth_renewer
return self

Expand Down
67 changes: 57 additions & 10 deletions tests/rest/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,21 +553,51 @@ def test_capabilities_caching(requests_mock):

def test_capabilities_caching_after_authenticate_basic(requests_mock):
user, pwd = "john262", "J0hndo3"
requests_mock.get(API_URL, json={"api_version": "1.0.0", "endpoints": BASIC_ENDPOINTS})

def get_capabilities(request, context):
endpoints = BASIC_ENDPOINTS
if "Authorization" in request.headers:
endpoints.append({"path": "/account/status", "methods": ["GET"]})
return {"api_version": "1.0.0", "endpoints": endpoints}

get_capabilities_mock = requests_mock.get(API_URL, json=get_capabilities)
requests_mock.get(API_URL + 'credentials/basic', text=_credentials_basic_handler(user, pwd))

with mock.patch('openeo.rest.connection.AuthConfig') as AuthConfig:
conn = Connection(API_URL)
conn._capabilities_cache._cache={"test":"test1"}
assert conn._capabilities_cache._cache != {}
AuthConfig.return_value.get_basic_auth.return_value = (user, pwd)
conn.authenticate_basic(user, pwd)
assert conn._capabilities_cache._cache == {}
con = Connection(API_URL)
assert con.capabilities().capabilities == {
"api_version": "1.0.0",
"endpoints": [
{"methods": ["GET"], "path": "/credentials/basic"},
],
}
assert get_capabilities_mock.call_count == 1
con.capabilities()
assert get_capabilities_mock.call_count == 1

con.authenticate_basic(user, pwd)
assert get_capabilities_mock.call_count == 1
assert con.capabilities().capabilities == {
"api_version": "1.0.0",
"endpoints": [
{"methods": ["GET"], "path": "/credentials/basic"},
{"methods": ["GET"], "path": "/account/status"},
],
}
assert get_capabilities_mock.call_count == 2



def test_capabilities_caching_after_authenticate_oidc(requests_mock):
requests_mock.get(API_URL, json={"api_version": "1.0.0"})
client_id = "myclient"

def get_capabilities(request, context):
endpoints = BASIC_ENDPOINTS
if "Authorization" in request.headers:
endpoints.append({"path": "/account/status", "methods": ["GET"]})
return {"api_version": "1.0.0", "endpoints": endpoints}

get_capabilities_mock = requests_mock.get(API_URL, json=get_capabilities)
requests_mock.get(API_URL + 'credentials/oidc', json={
"providers": [{"id": "fauth", "issuer": "https://fauth.test", "title": "Foo Auth", "scopes": ["openid", "im"]}]
})
Expand All @@ -580,9 +610,26 @@ def test_capabilities_caching_after_authenticate_oidc(requests_mock):
scopes_supported=["openid", "im"],
)
conn = Connection(API_URL)
conn._capabilities_cache._cache = {"test": "test1"}
assert conn.capabilities().capabilities == {
"api_version": "1.0.0",
"endpoints": [
{"methods": ["GET"], "path": "/credentials/basic"},
],
}
assert get_capabilities_mock.call_count == 1
conn.capabilities()
assert get_capabilities_mock.call_count == 1

conn.authenticate_oidc_authorization_code(client_id=client_id, webbrowser_open=oidc_mock.webbrowser_open)
assert conn._capabilities_cache._cache == {}
assert get_capabilities_mock.call_count == 1
assert conn.capabilities().capabilities == {
"api_version": "1.0.0",
"endpoints": [
{"methods": ["GET"], "path": "/credentials/basic"},
{"methods": ["GET"], "path": "/account/status"},
],
}
assert get_capabilities_mock.call_count == 2


def test_file_formats(requests_mock):
Expand Down

0 comments on commit 20b039d

Please sign in to comment.