11import mock
22from click .testing import CliRunner
33
4+ import paperspace .client
45from paperspace import cli
56from paperspace .commands import deployments as deployments_commands
67from tests import example_responses , MockResponse
1011
1112class TestDeploymentsCreate (object ):
1213 URL = "https://api.paperspace.io/deployments/createDeployment/"
14+ EXPECTED_HEADERS_WITH_CHANGED_API_KEY = paperspace .client .default_headers .copy ()
15+ EXPECTED_HEADERS_WITH_CHANGED_API_KEY ["X-API-Key" ] = "some_key"
1316 BASIC_OPTIONS_COMMAND = [
1417 "deployments" , "create" ,
1518 "--deploymentType" , "tfserving" ,
@@ -19,6 +22,16 @@ class TestDeploymentsCreate(object):
1922 "--imageUrl" , "https://www.latlmes.com/breaking/paperspace-now-has-a-100-bilion-valuation" ,
2023 "--instanceCount" , "666" ,
2124 ]
25+ BASIC_OPTIONS_COMMAND_WITH_API_KEY = [
26+ "deployments" , "create" ,
27+ "--deploymentType" , "tfserving" ,
28+ "--modelId" , "some_model_id" ,
29+ "--name" , "some_name" ,
30+ "--machineType" , "HAL9000" ,
31+ "--imageUrl" , "https://www.latlmes.com/breaking/paperspace-now-has-a-100-bilion-valuation" ,
32+ "--instanceCount" , "666" ,
33+ "--apiKey" , "some_key" ,
34+ ]
2235 BASIC_OPTIONS_REQUEST = {
2336 "machineType" : u"HAL9000" ,
2437 "name" : u"some_name" ,
@@ -48,6 +61,20 @@ def test_should_send_proper_data_and_print_message_when_create_deployment_with_b
4861 assert result .output == self .EXPECTED_STDOUT
4962 assert result .exit_code == 0
5063
64+ @mock .patch ("paperspace.cli.deployments_commands.client.requests.post" )
65+ def test_should_send_different_api_key_when_api_key_parameter_was_used (self , post_patched ):
66+ post_patched .return_value = MockResponse (self .RESPONSE_JSON_200 , 200 , "fake content" )
67+
68+ runner = CliRunner ()
69+ result = runner .invoke (cli .cli , self .BASIC_OPTIONS_COMMAND_WITH_API_KEY )
70+
71+ post_patched .assert_called_once_with (self .URL ,
72+ headers = self .EXPECTED_HEADERS_WITH_CHANGED_API_KEY ,
73+ json = self .BASIC_OPTIONS_REQUEST ,
74+ params = None )
75+ assert result .output == self .EXPECTED_STDOUT
76+ assert result .exit_code == 0
77+
5178 @mock .patch ("paperspace.cli.deployments_commands.client.requests.post" )
5279 def test_should_send_proper_data_and_print_message_when_create_wrong_model_id_was_given (self , post_patched ):
5380 post_patched .return_value = MockResponse (self .RESPONSE_JSON_404_MODEL_NOT_FOUND , 404 ,
@@ -65,8 +92,15 @@ def test_should_send_proper_data_and_print_message_when_create_wrong_model_id_wa
6592
6693
6794class TestDeploymentsList (object ):
95+ URL = "https://api.paperspace.io/deployments/getDeploymentList/"
96+
6897 COMMAND = ["deployments" , "list" ]
6998 LIST_JSON = example_responses .LIST_DEPLOYMENTS
99+
100+ COMMAND_WITH_API_KEY = ["deployments" , "list" , "--apiKey" , "some_key" ]
101+ EXPECTED_HEADERS_WITH_CHANGED_API_KEY = paperspace .client .default_headers .copy ()
102+ EXPECTED_HEADERS_WITH_CHANGED_API_KEY ["X-API-Key" ] = "some_key"
103+
70104 COMMAND_WITH_FILTER_WITH_STATE = ["deployments" , "list" , "--state" , "Stopped" ]
71105 LIST_WITH_FILTER_REQUEST_JSON = {"filter" : {"where" : {"and" : [{"state" : "Stopped" }]}}}
72106 LIST_WITH_FILTER_RESPONSE_JSON_WHEN_NO_DEPLOYMENTS_FOUND = {"deploymentList" : [], "total" : 17 , "displayTotal" : 0 ,
@@ -89,6 +123,23 @@ def test_should_send_get_request_and_print_list_of_deployments(self, get_patched
89123 runner = CliRunner ()
90124 result = runner .invoke (cli .cli , self .COMMAND )
91125
126+ get_patched .assert_called_once_with (self .URL ,
127+ headers = EXPECTED_HEADERS ,
128+ json = None ,
129+ params = None )
130+ assert result .output == self .DETAILS_STDOUT
131+
132+ @mock .patch ("paperspace.cli.deployments_commands.client.requests.get" )
133+ def test_should_send_get_request_with_custom_api_key_when_api_key_parameter_was_provided (self , get_patched ):
134+ get_patched .return_value = MockResponse (self .LIST_JSON , 200 , "fake content" )
135+
136+ runner = CliRunner ()
137+ result = runner .invoke (cli .cli , self .COMMAND_WITH_API_KEY )
138+
139+ get_patched .assert_called_once_with (self .URL ,
140+ headers = self .EXPECTED_HEADERS_WITH_CHANGED_API_KEY ,
141+ json = None ,
142+ params = None )
92143 assert result .output == self .DETAILS_STDOUT
93144
94145 @mock .patch ("paperspace.cli.deployments_commands.pydoc" )
@@ -101,6 +152,10 @@ def test_should_send_get_request_and_paginate_list_when_output_table_len_is_gt_l
101152 runner = CliRunner ()
102153 result = runner .invoke (cli .cli , self .COMMAND )
103154
155+ get_patched .assert_called_once_with (self .URL ,
156+ headers = EXPECTED_HEADERS ,
157+ json = None ,
158+ params = None )
104159 pydoc_patched .pager .assert_called_once ()
105160 assert result .exit_code == 0
106161
@@ -126,6 +181,10 @@ def test_should_send_get_request_and_print_list_of_deployments_filtered_with_sta
126181 runner = CliRunner ()
127182 result = runner .invoke (cli .cli , self .COMMAND_WITH_FILTER_WITH_STATE )
128183
184+ get_patched .assert_called_once_with (self .URL ,
185+ headers = EXPECTED_HEADERS ,
186+ json = self .LIST_WITH_FILTER_REQUEST_JSON ,
187+ params = None )
129188 assert result .output == "No deployments found\n "
130189
131190
@@ -138,6 +197,15 @@ class TestDeploymentsUpdate(object):
138197 ]
139198 BASIC_OPTIONS_REQUEST_JSON = {"upd" : {"name" : u"new_name" }, "id" : u"some_id" }
140199
200+ COMMAND_WITH_API_KEY = [
201+ "deployments" , "update" ,
202+ "--id" , "some_id" ,
203+ "--name" , "new_name" ,
204+ "--apiKey" , "some_key"
205+ ]
206+ EXPECTED_HEADERS_WITH_CHANGED_API_KEY = paperspace .client .default_headers .copy ()
207+ EXPECTED_HEADERS_WITH_CHANGED_API_KEY ["X-API-Key" ] = "some_key"
208+
141209 RESPONSE_JSON_200 = {"upd" : {"name" : u"asd" }, "id" : u"dennaw0wzbvvg3" }
142210 EXPECTED_STDOUT = "Deployment model updated.\n "
143211
@@ -158,6 +226,20 @@ def test_should_send_proper_data_and_print_message_when_update_deployment(self,
158226 assert result .output == self .EXPECTED_STDOUT
159227 assert result .exit_code == 0
160228
229+ @mock .patch ("paperspace.cli.deployments_commands.client.requests.post" )
230+ def test_should_send_proper_data_with_custom_api_key_when_api_key_parameter_was_provided (self , post_patched ):
231+ post_patched .return_value = MockResponse (self .RESPONSE_JSON_200 , 200 , "fake content" )
232+
233+ runner = CliRunner ()
234+ result = runner .invoke (cli .cli , self .COMMAND_WITH_API_KEY )
235+
236+ post_patched .assert_called_once_with (self .URL ,
237+ headers = self .EXPECTED_HEADERS_WITH_CHANGED_API_KEY ,
238+ json = self .BASIC_OPTIONS_REQUEST_JSON ,
239+ params = None )
240+ assert result .output == self .EXPECTED_STDOUT
241+ assert result .exit_code == 0
242+
161243 @mock .patch ("paperspace.cli.deployments_commands.client.requests.post" )
162244 def test_should_send_proper_data_and_print_message_when_update_deployment_used_with_wrong_id (self , post_patched ):
163245 post_patched .return_value = MockResponse (self .RESPONSE_JSON_400 , 400 , "fake content" )
@@ -199,10 +281,17 @@ class TestDeleteDeployment(object):
199281 URL = "https://api.paperspace.io/deployments/updateDeployment/"
200282 COMMAND = ["deployments" , "delete" ,
201283 "--id" , "some_id" ]
202-
203284 REQUEST_JSON = {"upd" : {"isDeleted" : True }, "id" : u"some_id" }
204285 EXPECTED_STDOUT = "Deployment deleted.\n "
205286
287+ COMMAND_WITH_API_KEY = [
288+ "deployments" , "delete" ,
289+ "--id" , "some_id" ,
290+ "--apiKey" , "some_key" ,
291+ ]
292+ EXPECTED_HEADERS_WITH_CHANGED_API_KEY = paperspace .client .default_headers .copy ()
293+ EXPECTED_HEADERS_WITH_CHANGED_API_KEY ["X-API-Key" ] = "some_key"
294+
206295 RESPONSE_JSON_400 = {"error" : {"name" : "Error" , "status" : 400 , "message" : "Unable to access deployment" }}
207296 EXPECTED_STDOUT_WITH_WRONG_ID = "Unable to access deployment\n "
208297
@@ -220,6 +309,20 @@ def test_should_send_proper_data_and_print_message_when_deployments_delete_was_u
220309 assert result .output == self .EXPECTED_STDOUT
221310 assert result .exit_code == 0
222311
312+ @mock .patch ("paperspace.cli.deployments_commands.client.requests.post" )
313+ def test_should_send_proper_data_with_custom_api_key_when_api_key_parameter_was_provided (self , post_patched ):
314+ post_patched .return_value = MockResponse (None , 204 , "fake content" )
315+
316+ runner = CliRunner ()
317+ result = runner .invoke (cli .cli , self .COMMAND_WITH_API_KEY )
318+
319+ post_patched .assert_called_once_with (self .URL ,
320+ headers = self .EXPECTED_HEADERS_WITH_CHANGED_API_KEY ,
321+ json = self .REQUEST_JSON ,
322+ params = None )
323+ assert result .output == self .EXPECTED_STDOUT
324+ assert result .exit_code == 0
325+
223326 @mock .patch ("paperspace.cli.deployments_commands.client.requests.post" )
224327 def test_should_send_proper_data_and_print_message_when_deployments_delete_used_with_wrong_id (self , post_patched ):
225328 post_patched .return_value = MockResponse (self .RESPONSE_JSON_400 , 400 , "fake content" )
0 commit comments