11
11
from pathlib import Path
12
12
13
13
import pytest
14
- import requests . auth
14
+ import requests
15
15
import requests_mock
16
16
import shapely .geometry
17
17
30
30
from openeo .rest ._testing import DummyBackend , build_capabilities
31
31
from openeo .rest .auth .auth import BearerAuth , NullAuth
32
32
from 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
34
34
from openeo .rest .connection import (
35
35
DEFAULT_TIMEOUT ,
36
36
DEFAULT_TIMEOUT_SYNCHRONOUS_EXECUTE ,
@@ -564,10 +564,8 @@ def _get_capabilities_auth_dependent(request, context):
564
564
return capabilities
565
565
566
566
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 ):
569
568
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 ))
571
569
572
570
con = Connection (API_URL )
573
571
assert con .capabilities ().capabilities ["endpoints" ] == [
@@ -578,7 +576,7 @@ def test_capabilities_caching_after_authenticate_basic(requests_mock):
578
576
con .capabilities ()
579
577
assert get_capabilities_mock .call_count == 1
580
578
581
- con .authenticate_basic (username = user , password = pwd )
579
+ con .authenticate_basic (username = basic_auth . username , password = basic_auth . password )
582
580
assert get_capabilities_mock .call_count == 1
583
581
assert con .capabilities ().capabilities ["endpoints" ] == [
584
582
{"methods" : ["GET" ], "path" : "/credentials/basic" },
@@ -715,30 +713,17 @@ def test_api_error_non_json(requests_mock):
715
713
assert exc .message == "olapola"
716
714
717
715
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 ):
731
717
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 ))
733
718
734
719
with mock .patch ('openeo.rest.connection.AuthConfig' ) as AuthConfig :
735
720
# Don't create default AuthConfig when not necessary
736
721
conn = Connection (API_URL )
737
722
assert AuthConfig .call_count == 0
738
- conn .authenticate_basic (user , pwd )
723
+ conn .authenticate_basic (basic_auth . username , basic_auth . password )
739
724
assert AuthConfig .call_count == 0
740
725
# 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 )
742
727
conn .authenticate_basic ()
743
728
assert AuthConfig .call_count == 1
744
729
conn .authenticate_basic ()
@@ -785,29 +770,25 @@ def test_authenticate_basic_no_support(requests_mock, api_version):
785
770
assert isinstance (conn .auth , NullAuth )
786
771
787
772
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 ):
790
774
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 ))
792
775
793
776
conn = Connection (API_URL )
794
777
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 )
796
779
assert isinstance (conn .auth , BearerAuth )
797
- assert conn .auth .bearer == "basic//w3lc0m3 "
780
+ assert conn .auth .bearer == "basic//6cc3570k3n "
798
781
799
782
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 ):
802
784
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 )
805
786
806
787
conn = Connection (API_URL )
807
788
assert isinstance (conn .auth , NullAuth )
808
789
conn .authenticate_basic ()
809
790
assert isinstance (conn .auth , BearerAuth )
810
- assert conn .auth .bearer == "basic//w3lc0m3 "
791
+ assert conn .auth .bearer == "basic//6cc3570k3n "
811
792
812
793
813
794
@pytest .mark .slow
@@ -3966,9 +3947,10 @@ def test_connect_auto_auth_from_config_basic(
3966
3947
""" ))
3967
3948
user , pwd = "john" , "j0hn"
3968
3949
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 )
3970
3952
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 )
3972
3954
3973
3955
if use_default :
3974
3956
# Without arguments: use default
0 commit comments