1111from pathlib import Path
1212
1313import pytest
14- import requests . auth
14+ import requests
1515import requests_mock
1616import shapely .geometry
1717
3030from openeo .rest ._testing import DummyBackend , build_capabilities
3131from openeo .rest .auth .auth import BearerAuth , NullAuth
3232from openeo .rest .auth .oidc import OidcException
33- from openeo .rest .auth .testing import ABSENT , OidcMock
33+ from openeo .rest .auth .testing import ABSENT , OidcMock , SimpleBasicAuthMocker
3434from openeo .rest .connection import (
3535 DEFAULT_TIMEOUT ,
3636 DEFAULT_TIMEOUT_SYNCHRONOUS_EXECUTE ,
@@ -564,10 +564,8 @@ def _get_capabilities_auth_dependent(request, context):
564564 return capabilities
565565
566566
567- def test_capabilities_caching_after_authenticate_basic (requests_mock ):
568- user , pwd = "john262" , "J0hndo3"
567+ def test_capabilities_caching_after_authenticate_basic (requests_mock , basic_auth ):
569568 get_capabilities_mock = requests_mock .get (API_URL , json = _get_capabilities_auth_dependent )
570- requests_mock .get (API_URL + 'credentials/basic' , text = _credentials_basic_handler (user , pwd ))
571569
572570 con = Connection (API_URL )
573571 assert con .capabilities ().capabilities ["endpoints" ] == [
@@ -578,7 +576,7 @@ def test_capabilities_caching_after_authenticate_basic(requests_mock):
578576 con .capabilities ()
579577 assert get_capabilities_mock .call_count == 1
580578
581- con .authenticate_basic (username = user , password = pwd )
579+ con .authenticate_basic (username = basic_auth . username , password = basic_auth . password )
582580 assert get_capabilities_mock .call_count == 1
583581 assert con .capabilities ().capabilities ["endpoints" ] == [
584582 {"methods" : ["GET" ], "path" : "/credentials/basic" },
@@ -715,30 +713,17 @@ def test_api_error_non_json(requests_mock):
715713 assert exc .message == "olapola"
716714
717715
718- def _credentials_basic_handler (username , password , access_token = "w3lc0m3" ):
719- # TODO: better reuse of this helper
720- expected_auth = requests .auth ._basic_auth_str (username = username , password = password )
721-
722- def handler (request , context ):
723- assert request .headers ["Authorization" ] == expected_auth
724- return json .dumps ({"access_token" : access_token })
725-
726- return handler
727-
728-
729- def test_create_connection_lazy_auth_config (requests_mock , api_version ):
730- user , pwd = "john262" , "J0hndo3"
716+ def test_create_connection_lazy_auth_config (requests_mock , api_version , basic_auth ):
731717 requests_mock .get (API_URL , json = {"api_version" : api_version , "endpoints" : BASIC_ENDPOINTS })
732- requests_mock .get (API_URL + 'credentials/basic' , text = _credentials_basic_handler (user , pwd ))
733718
734719 with mock .patch ('openeo.rest.connection.AuthConfig' ) as AuthConfig :
735720 # Don't create default AuthConfig when not necessary
736721 conn = Connection (API_URL )
737722 assert AuthConfig .call_count == 0
738- conn .authenticate_basic (user , pwd )
723+ conn .authenticate_basic (basic_auth . username , basic_auth . password )
739724 assert AuthConfig .call_count == 0
740725 # call `authenticate_basic` so that fallback AuthConfig is created/used lazily
741- AuthConfig .return_value .get_basic_auth .return_value = (user , pwd )
726+ AuthConfig .return_value .get_basic_auth .return_value = (basic_auth . username , basic_auth . password )
742727 conn .authenticate_basic ()
743728 assert AuthConfig .call_count == 1
744729 conn .authenticate_basic ()
@@ -785,29 +770,25 @@ def test_authenticate_basic_no_support(requests_mock, api_version):
785770 assert isinstance (conn .auth , NullAuth )
786771
787772
788- def test_authenticate_basic (requests_mock , api_version ):
789- user , pwd = "john262" , "J0hndo3"
773+ def test_authenticate_basic (requests_mock , api_version , basic_auth ):
790774 requests_mock .get (API_URL , json = {"api_version" : api_version , "endpoints" : BASIC_ENDPOINTS })
791- requests_mock .get (API_URL + 'credentials/basic' , text = _credentials_basic_handler (user , pwd ))
792775
793776 conn = Connection (API_URL )
794777 assert isinstance (conn .auth , NullAuth )
795- conn .authenticate_basic (username = user , password = pwd )
778+ conn .authenticate_basic (username = basic_auth . username , password = basic_auth . password )
796779 assert isinstance (conn .auth , BearerAuth )
797- assert conn .auth .bearer == "basic//w3lc0m3 "
780+ assert conn .auth .bearer == "basic//6cc3570k3n "
798781
799782
800- def test_authenticate_basic_from_config (requests_mock , api_version , auth_config ):
801- user , pwd = "john281" , "J0hndo3"
783+ def test_authenticate_basic_from_config (requests_mock , api_version , auth_config , basic_auth ):
802784 requests_mock .get (API_URL , json = {"api_version" : api_version , "endpoints" : BASIC_ENDPOINTS })
803- requests_mock .get (API_URL + 'credentials/basic' , text = _credentials_basic_handler (user , pwd ))
804- auth_config .set_basic_auth (backend = API_URL , username = user , password = pwd )
785+ auth_config .set_basic_auth (backend = API_URL , username = basic_auth .username , password = basic_auth .password )
805786
806787 conn = Connection (API_URL )
807788 assert isinstance (conn .auth , NullAuth )
808789 conn .authenticate_basic ()
809790 assert isinstance (conn .auth , BearerAuth )
810- assert conn .auth .bearer == "basic//w3lc0m3 "
791+ assert conn .auth .bearer == "basic//6cc3570k3n "
811792
812793
813794@pytest .mark .slow
@@ -3966,9 +3947,10 @@ def test_connect_auto_auth_from_config_basic(
39663947 """ ))
39673948 user , pwd = "john" , "j0hn"
39683949 for u , a in [(default , "Hell0!" ), (other , "Wazz6!" )]:
3969- auth_config .set_basic_auth (backend = u , username = user , password = pwd )
3950+ basic_auth_mocker = SimpleBasicAuthMocker (username = user , password = pwd , access_token = a )
3951+ auth_config .set_basic_auth (backend = u , username = basic_auth_mocker .username , password = basic_auth_mocker .password )
39703952 requests_mock .get (u , json = {"api_version" : "1.0.0" , "endpoints" : BASIC_ENDPOINTS })
3971- requests_mock . get ( f" { u } /credentials/basic" , text = _credentials_basic_handler ( user , pwd , access_token = a ) )
3953+ basic_auth_mocker . setup_credentials_basic_handler ( api_root = u , requests_mock = requests_mock )
39723954
39733955 if use_default :
39743956 # Without arguments: use default
0 commit comments