1414
1515from api .models import App
1616
17+ from . import adapter
18+ import requests_mock
19+
1720
1821def mock_none (* args , ** kwargs ):
1922 return None
2023
2124
25+ @requests_mock .Mocker (real_http = True , adapter = adapter )
2226class AppTest (APITestCase ):
2327 """Tests creation of applications"""
2428
@@ -33,7 +37,7 @@ def tearDown(self):
3337 # make sure every test has a clean slate for k8s mocking
3438 cache .clear ()
3539
36- def test_app (self ):
40+ def test_app (self , mock_requests ):
3741 """
3842 Test that a user can create, read, update and delete an application
3943 """
@@ -54,7 +58,7 @@ def test_app(self):
5458 response = self .client .delete (url )
5559 self .assertEqual (response .status_code , 204 )
5660
57- def test_response_data (self ):
61+ def test_response_data (self , mock_requests ):
5862 """Test that the serialized response contains only relevant data."""
5963 body = {'id' : 'test' }
6064 response = self .client .post ('/v2/apps' , body )
@@ -67,7 +71,7 @@ def test_response_data(self):
6771 }
6872 self .assertDictContainsSubset (expected , response .data )
6973
70- def test_app_override_id (self ):
74+ def test_app_override_id (self , mock_requests ):
7175 body = {'id' : 'myid' }
7276 response = self .client .post ('/v2/apps' , body )
7377 self .assertEqual (response .status_code , 201 )
@@ -77,7 +81,7 @@ def test_app_override_id(self):
7781 return response
7882
7983 @mock .patch ('requests.get' )
80- def test_app_actions (self , mock_get ):
84+ def test_app_actions (self , mock_requests , mock_get ):
8185 url = '/v2/apps'
8286 body = {'id' : 'autotest' }
8387 response = self .client .post (url , body )
@@ -121,7 +125,7 @@ def test_app_actions(self, mock_get):
121125 # TODO: test run needs an initial build
122126
123127 @mock .patch ('api.models.logger' )
124- def test_app_release_notes_in_logs (self , mock_logger ):
128+ def test_app_release_notes_in_logs (self , mock_requests , mock_logger ):
125129 """Verifies that an app's release summary is dumped into the logs."""
126130 url = '/v2/apps'
127131 body = {'id' : 'autotest' }
@@ -132,7 +136,7 @@ def test_app_release_notes_in_logs(self, mock_logger):
132136 exp_log_call = mock .call (logging .INFO , exp_msg )
133137 mock_logger .log .has_calls (exp_log_call )
134138
135- def test_app_errors (self ):
139+ def test_app_errors (self , mock_requests ):
136140 app_id = 'autotest-errors'
137141 url = '/v2/apps'
138142 body = {'id' : 'camelCase' }
@@ -155,7 +159,7 @@ def test_app_errors(self):
155159 response = self .client .get (url )
156160 self .assertEqual (response .status_code , 404 )
157161
158- def test_app_reserved_names (self ):
162+ def test_app_reserved_names (self , mock_requests ):
159163 """Nobody should be able to create applications with names which are reserved."""
160164 url = '/v2/apps'
161165 reserved_names = ['foo' , 'bar' ]
@@ -168,7 +172,7 @@ def test_app_reserved_names(self):
168172 '{} is a reserved name.' .format (name ),
169173 status_code = 400 )
170174
171- def test_app_structure_is_valid_json (self ):
175+ def test_app_structure_is_valid_json (self , mock_requests ):
172176 """Application structures should be valid JSON objects."""
173177 url = '/v2/apps'
174178 response = self .client .post (url )
@@ -185,7 +189,7 @@ def test_app_structure_is_valid_json(self):
185189 self .assertEqual (response .data ['structure' ], {"web" : 1 })
186190
187191 @mock .patch ('api.models.logger' )
188- def test_admin_can_manage_other_apps (self , mock_logger ):
192+ def test_admin_can_manage_other_apps (self , mock_requests , mock_logger ):
189193 """Administrators of Deis should be able to manage all applications.
190194 """
191195 # log in as non-admin user and create an app
@@ -213,7 +217,7 @@ def test_admin_can_manage_other_apps(self, mock_logger):
213217 response = self .client .delete (url )
214218 self .assertEqual (response .status_code , 204 )
215219
216- def test_admin_can_see_other_apps (self ):
220+ def test_admin_can_see_other_apps (self , mock_requests ):
217221 """If a user creates an application, the administrator should be able
218222 to see it.
219223 """
@@ -231,7 +235,7 @@ def test_admin_can_see_other_apps(self):
231235 response = self .client .get (url )
232236 self .assertEqual (response .data ['count' ], 1 )
233237
234- def test_run_without_release_should_error (self ):
238+ def test_run_without_release_should_error (self , mock_requests ):
235239 """
236240 A user should not be able to run a one-off command unless a release
237241 is present.
@@ -253,7 +257,7 @@ def _mock_run(*args, **kwargs):
253257 @mock .patch ('api.models.App.run' , _mock_run )
254258 @mock .patch ('api.models.App.deploy' , mock_none )
255259 @mock .patch ('api.models.Release.publish' , mock_none )
256- def test_run (self ):
260+ def test_run (self , mock_requests ):
257261 """
258262 A user should be able to run a one off command
259263 """
@@ -276,7 +280,7 @@ def test_run(self):
276280 self .assertEqual (response .data ['rc' ], 0 )
277281 self .assertEqual (response .data ['output' ], 'mock' )
278282
279- def test_unauthorized_user_cannot_see_app (self ):
283+ def test_unauthorized_user_cannot_see_app (self , mock_requests ):
280284 """
281285 An unauthorized user should not be able to access an app's resources.
282286
@@ -307,7 +311,7 @@ def test_unauthorized_user_cannot_see_app(self):
307311 response = self .client .delete (url )
308312 self .assertEqual (response .status_code , 403 )
309313
310- def test_app_info_not_showing_wrong_app (self ):
314+ def test_app_info_not_showing_wrong_app (self , mock_requests ):
311315 app_id = 'autotest'
312316 base_url = '/v2/apps'
313317 body = {'id' : app_id }
@@ -316,7 +320,7 @@ def test_app_info_not_showing_wrong_app(self):
316320 response = self .client .get (url )
317321 self .assertEqual (response .status_code , 404 )
318322
319- def test_app_transfer (self ):
323+ def test_app_transfer (self , mock_requests ):
320324 owner = User .objects .get (username = 'autotest2' )
321325 owner_token = Token .objects .get (user = owner ).key
322326 self .client .credentials (HTTP_AUTHORIZATION = 'Token ' + owner_token )
@@ -364,7 +368,7 @@ def test_app_transfer(self):
364368 self .assertEqual (response .status_code , 200 )
365369 self .assertEqual (response .data ['owner' ], self .user .username )
366370
367- def test_app_exists_in_kubernetes (self ):
371+ def test_app_exists_in_kubernetes (self , mock_requests ):
368372 """
369373 Create an app that has the same namespace as an existing kubernetes namespace
370374 """
0 commit comments