2121# See the License for the specific language governing permissions and
2222# limitations under the License.
2323import json
24+ import os
2425
2526import pytest
2627import requests
@@ -44,6 +45,12 @@ def m():
4445 with requests_mock .Mocker () as m :
4546 yield m
4647
48+ @pytest .fixture (autouse = False )
49+ def netrc_file (tmp_path , monkeypatch ):
50+ netrc_file_path = os .path .join (str (tmp_path ), ".netrc" )
51+ monkeypatch .setenv ("NETRC" , netrc_file_path )
52+ return netrc_file_path
53+
4754def test_simple_request (m ):
4855 data = {'cucumber' : 'dade' }
4956 m .get ('https://databricks.com/api/2.0/endpoint' , text = json .dumps (data ))
@@ -130,3 +137,38 @@ def test_api_client_url_parsing():
130137 # databricks_cli.configure.cli
131138 client = ApiClient (host = 'http://databricks.com' )
132139 assert client .get_url ('' ) == 'http://databricks.com/api/2.0'
140+
141+ def test_api_client_auth_netrc_and_user_password (m , netrc_file ):
142+ with open (netrc_file , "w+" ) as netrc :
143+ #generates header Authorization: 'Basic bmV0cmM6cGFzc3dvcmQ='
144+ netrc .write ("machine databricks.com login netrc password password" )
145+
146+ m .get ('https://databricks.com/api/2.0/endpoint' , text = json .dumps ({}))
147+ client = ApiClient (user = "apple" , password = "banana" , host = "https://databricks.com" )
148+ client .perform_query ("GET" , "/endpoint" )
149+ assert m .request_history [0 ].headers ['Authorization' ] == "Basic YXBwbGU6YmFuYW5h"
150+
151+ def test_api_client_auth_only_valid_netrc (m , netrc_file ):
152+ with open (netrc_file , "w+" ) as netrc :
153+ #generates header Authorization: 'Basic bmV0cmM6cGFzc3dvcmQ='
154+ netrc .write ("machine databricks.com login netrc password password" )
155+
156+ m .get ('https://databricks.com/api/2.0/endpoint' , text = json .dumps ({}))
157+ client = ApiClient (host = "https://databricks.com" )
158+ client .perform_query ("GET" , "/endpoint" )
159+ assert m .request_history [0 ].headers ['Authorization' ] == "Basic bmV0cmM6cGFzc3dvcmQ="
160+
161+ def test_api_client_auth_invalid_netrc (m , netrc_file ):
162+ with open (netrc_file , "w+" ) as netrc :
163+ netrc .write ("garbage" )
164+
165+ m .get ('https://databricks.com/api/2.0/endpoint' , text = json .dumps ({}))
166+ client = ApiClient (host = "https://databricks.com" )
167+ client .perform_query ("GET" , "/endpoint" )
168+ assert "Authorization" not in m .request_history [0 ].headers
169+
170+ def test_api_client_auth_no_netrc (m ):
171+ m .get ('https://databricks.com/api/2.0/endpoint' , text = json .dumps ({}))
172+ client = ApiClient (host = "https://databricks.com" )
173+ client .perform_query ("GET" , "/endpoint" )
174+ assert "Authorization" not in m .request_history [0 ].headers
0 commit comments