@@ -63,9 +63,120 @@ def test_get_repos_with_management_permissions_success(mock_get):
6363
6464 mock_get .return_value .status_code = 200
6565
66+ mock_return = {
67+ "repos" :[
68+ {
69+ "id" :61449681029719 ,
70+ "path" :"/Repos/***/test_dbx_repo_folder_one" ,
71+ "url" :"https://github.com/test_repo_profile/test_repo_one" ,
72+ "provider" :"gitHub" ,
73+ "branch" :"main" ,
74+ "head_commit_id" :"test_commit_id"
75+ },
76+ {
77+ "id" :2806425392675498 ,
78+ "path" :"/Repos/***/test_dbx_repo_folder_two" ,
79+ "url" :"https://github.com/test_repo_profile/test_repo_two" ,
80+ "provider" :"gitHub" ,
81+ "branch" :"main" ,
82+ "head_commit_id" :"test_commit_id"
83+ }
84+ ]
85+ }
86+
87+ mock_get .return_value .json .return_value = mock_return
88+
89+ repos_with_management_permissions , status_code = get_repos_with_management_permissions ()
90+
91+ assert repos_with_management_permissions == mock_return ["repos" ]
92+ assert status_code == 200
93+
94+
95+ expected_dbkrs_req_headers = {
96+ 'Authorization' : 'Bearer test_databricks_aad_token' ,
97+ 'X-Databricks-Azure-SP-Management-Token' : 'test_databricks_management_token' ,
98+ 'X-Databricks-Azure-Workspace-Resource-Id' : 'test_workspace_id' ,
99+ 'Content-Type' : 'application/json' }
66100
101+ mock_get .assert_once_called_with (
102+ "https://test_databricks_instance/api/2.0/repos" ,
103+ headers = expected_dbkrs_req_headers
104+ )
105+
106+
107+ @patch ('requests.get' )
108+ def test_get_repos_with_management_permissions_failure (mock_get ):
109+ monkeypatch = MonkeyPatch ()
110+
111+ monkeypatch .setenv ('ARM_CLIENT_ID' , 'test_arm_client_id' )
112+ monkeypatch .setenv ('WORKSPACE_ID' , 'test_workspace_id' )
113+ monkeypatch .setenv ('DATABRICKS_MANAGEMENT_TOKEN' , 'test_databricks_management_token' )
114+ monkeypatch .setenv ('DATABRICKS_AAD_TOKEN' , 'test_databricks_aad_token' )
115+ monkeypatch .setenv ('DATABRICKS_INSTANCE' , 'test_databricks_instance' )
116+
117+ mock_get .return_value .status_code = 500
118+
119+ with pytest .raises (Exception ) as e :
120+ status_code = get_repos_with_management_permissions ()
121+ assert status_code == 500
122+
123+
124+ # update_repo
125+
126+ class UpdateRepo (unittest .TestCase ):
127+
128+ @patch ('requests.post' )
129+ def test_update_repo_success (mock_post ):
130+ monkeypatch = MonkeyPatch ()
131+ monkeypatch .setenv ('ARM_CLIENT_ID' , 'test_arm_client_id' )
132+ monkeypatch .setenv ('WORKSPACE_ID' , 'test_workspace_id' )
133+ monkeypatch .setenv ('DATABRICKS_MANAGEMENT_TOKEN' , 'test_databricks_management_token' )
134+ monkeypatch .setenv ('DATABRICKS_AAD_TOKEN' , 'test_databricks_aad_token' )
135+ monkeypatch .setenv ('DATABRICKS_INSTANCE' , 'test_databricks_instance' )
136+
137+ mock_repo_id = 123456789
138+ mock_update_branch = "test_main_branch"
139+
140+ mock_post .return_value .status_code = 200
141+
142+ status_code = update_repo (mock_repo_id , mock_update_branch )
143+
144+ assert status_code == 200
145+
146+
147+ expected_dbkrs_req_headers = {
148+ 'Authorization' : 'Bearer test_databricks_aad_token' ,
149+ 'X-Databricks-Azure-SP-Management-Token' : 'test_databricks_management_token' ,
150+ 'X-Databricks-Azure-Workspace-Resource-Id' : 'test_workspace_id' ,
151+ 'Content-Type' : 'application/json' }
152+
153+ mock_post .assert_once_called_with (
154+ "https://test_databricks_instance/api/2.0/repos/" + str (mock_repo_id ),
155+ headers = expected_dbkrs_req_headers ,
156+ json = {
157+ "branch" : mock_update_branch
158+ }
159+ )
160+
161+ def test_update_repo_failure (mock_post ):
162+
163+ mock_repo_id = 123456789
164+ mock_update_branch = "test_main_branch"
165+
166+ mock_post .return_value .status_code = 500
167+
168+ with pytest .raises (Exception ) as e :
169+ status_code = update_repo (mock_repo_id , mock_update_branch )
170+ assert status_code == 500
171+
172+
173+
174+
175+
176+
177+
178+
67179
68180
69181
70182
71- pass
0 commit comments