1212import pytest
1313
1414from test .auth .test_auth_manager import \
15- _assert_client_credentials_auth_context , _assert_pkce_auth_context , _assert_sp_credentials_auth_context # NOQA
15+ _assert_client_credentials_auth_context , _assert_pkce_auth_context , _assert_sp_credentials_auth_context # NOQA
1616from test .fixtures import get_auth_manager as pkce_auth_manager # NOQA
1717from test .fixtures import get_client_auth_manager as client_auth_manager # NOQA
18+ from test .fixtures import get_client_auth_manager_scoped as client_auth_manager_scoped # NOQA
1819from test .fixtures import get_service_principal_auth_manager as service_principal_auth_manager # NOQA
1920from splunk_sdk .common .context import Context
2021from splunk_sdk .base_client import BaseClient
2122from splunk_sdk .identity import Identity as IdentityAndAccessControl
2223
23-
2424@pytest .mark .usefixtures ("pkce_auth_manager" ) # NOQA
2525def test_base_client_instance_with_pkce_auth (pkce_auth_manager ):
2626 """Get a base client with a context and a pkce auth manager."""
@@ -65,7 +65,6 @@ def test_base_client_empty_hooks_with_pkce_auth(pkce_auth_manager):
6565 IdentityAndAccessControl (base_client ).validate_token ()
6666 assert True
6767
68-
6968@pytest .mark .usefixtures ("pkce_auth_manager" ) # NOQA
7069def test_base_client_no_list_hooks_with_pkce_auth (pkce_auth_manager ):
7170 def test_hook (* args , ** kwargs ):
@@ -85,6 +84,105 @@ def test_base_client_instance_with_client_auth(client_auth_manager):
8584 assert (base_client is not None )
8685 _assert_client_credentials_auth_context (base_client .auth_manager .context )
8786
87+ @pytest .mark .usefixtures ("client_auth_manager_scoped" ) # NOQA
88+ def test_base_client_instance_with_client_auth_tenant_scoped (client_auth_manager_scoped ):
89+ """Get a base client with a context and a client auth manager."""
90+ context = Context (tenant_scoped = True ,
91+ tenant = os .environ .get ('SPLUNK_TENANT_2' ),
92+ host = os .environ .get ('SPLUNK_HOST_2' ),
93+ api_host = os .environ .get ('SPLUNK_HOST_2' ),
94+ region = os .environ .get ('SPLUNK_REGION' ),
95+ debug = os .environ .get (
96+ 'SPLUNK_DEBUG' , 'false' ).lower ().strip () == 'true' )
97+
98+ base_client = BaseClient (context = context ,
99+ auth_manager = client_auth_manager_scoped )
100+ assert (base_client is not None )
101+ assert (base_client .context .tenant_scoped is True )
102+ assert (base_client .context .tenant == os .environ .get ('SPLUNK_TENANT_2' ))
103+ assert (base_client .context .region == os .environ .get ('SPLUNK_REGION' ))
104+
105+ tenant_path = "/exampleservice/path"
106+ system_path = "/system/exampleservice/path"
107+
108+ url_tenant = base_client .build_url (tenant_path )
109+ expected_tenant_url_path_template = 'https://%s.%s/%s%s'
110+ expected_system_url_path_template = 'https://region-%s.%s%s'
111+
112+ expected_tenant_url_path = expected_tenant_url_path_template % (os .environ .get ('SPLUNK_TENANT_2' ), os .environ .get ('SPLUNK_HOST_2' ), os .environ .get ('SPLUNK_TENANT_2' ), tenant_path )
113+ assert (url_tenant == expected_tenant_url_path )
114+
115+ expected_system_url_path = expected_system_url_path_template % (os .environ .get ('SPLUNK_REGION' ), os .environ .get ('SPLUNK_HOST_2' ), system_path )
116+ url_system = base_client .build_url (system_path )
117+ assert (url_system == expected_system_url_path )
118+
119+ _assert_client_credentials_auth_context (base_client .auth_manager .context )
120+
121+ @pytest .mark .usefixtures ("client_auth_manager_scoped" ) # NOQA
122+ def test_base_client_instance_with_client_auth_scoped_off (client_auth_manager ):
123+ """Get a base client with a context and a client auth manager."""
124+ context = Context (tenant_scoped = False ,
125+ tenant = os .environ .get ('SPLUNK_TENANT' ),
126+ host = os .environ .get ('SPLUNK_HOST' ),
127+ api_host = os .environ .get ('SPLUNK_HOST' ),
128+ region = os .environ .get ('SPLUNK_REGION' ),
129+ debug = os .environ .get (
130+ 'SPLUNK_DEBUG' , 'false' ).lower ().strip () == 'true' )
131+
132+ base_client = BaseClient (context = context ,
133+ auth_manager = client_auth_manager )
134+ assert (base_client is not None )
135+ assert (base_client .context .tenant_scoped is False )
136+ assert (base_client .context .tenant == os .environ .get ('SPLUNK_TENANT' ))
137+ assert (base_client .context .region == os .environ .get ('SPLUNK_REGION' ))
138+
139+ tenant_path = "/exampleservice/path"
140+ system_path = "/system/exampleservice/path"
141+
142+ url_tenant = base_client .build_url (tenant_path )
143+ expected_tenant_url_path_template = 'https://%s/%s%s'
144+ expected_system_url_path_template = 'https://%s%s'
145+
146+ expected_tenant_url_path = expected_tenant_url_path_template % (os .environ .get ('SPLUNK_HOST' ), os .environ .get ('SPLUNK_TENANT' ), tenant_path )
147+ assert (url_tenant == expected_tenant_url_path )
148+
149+ expected_system_url_path = expected_system_url_path_template % (os .environ .get ('SPLUNK_HOST' ), system_path )
150+ url_system = base_client .build_url (system_path )
151+ assert (url_system == expected_system_url_path )
152+
153+ _assert_client_credentials_auth_context (base_client .auth_manager .context )
154+
155+ @pytest .mark .usefixtures ("client_auth_manager_scoped" ) # NOQA
156+ def test_base_client_instance_with_client_auth_default (client_auth_manager ):
157+ """Get a base client with a context and a client auth manager."""
158+ context = Context (tenant = os .environ .get ('SPLUNK_TENANT' ),
159+ host = os .environ .get ('SPLUNK_HOST' ),
160+ api_host = os .environ .get ('SPLUNK_HOST' ),
161+ debug = os .environ .get (
162+ 'SPLUNK_DEBUG' , 'false' ).lower ().strip () == 'true' )
163+
164+ base_client = BaseClient (context = context ,
165+ auth_manager = client_auth_manager )
166+ assert (base_client is not None )
167+ assert (base_client .context .tenant_scoped is False )
168+ assert (base_client .context .tenant == os .environ .get ('SPLUNK_TENANT' ))
169+ assert (base_client .context .region is None )
170+
171+ tenant_path = "/exampleservice/path"
172+ system_path = "/system/exampleservice/path"
173+
174+ url_tenant = base_client .build_url (tenant_path )
175+ expected_tenant_url_path_template = 'https://%s/%s%s'
176+ expected_system_url_path_template = 'https://%s%s'
177+
178+ expected_tenant_url_path = expected_tenant_url_path_template % (os .environ .get ('SPLUNK_HOST' ), os .environ .get ('SPLUNK_TENANT' ), tenant_path )
179+ assert (url_tenant == expected_tenant_url_path )
180+
181+ expected_system_url_path = expected_system_url_path_template % (os .environ .get ('SPLUNK_HOST' ), system_path )
182+ url_system = base_client .build_url (system_path )
183+ assert (url_system == expected_system_url_path )
184+
185+ _assert_client_credentials_auth_context (base_client .auth_manager .context )
88186
89187@pytest .mark .usefixtures ("service_principal_auth_manager" ) # NOQA
90188def test_base_client_instance_with_sp_auth (service_principal_auth_manager ):
0 commit comments