Skip to content

Commit 5f2ee41

Browse files
committed
Merge pull request #547 from helgi/reduce_mock_status_ok
chore(tests): remove various redundant python-request mocks
2 parents 807ee50 + d073ed4 commit 5f2ee41

7 files changed

Lines changed: 4 additions & 62 deletions

File tree

rootfs/api/tests/__init__.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import logging
22

33
from django.test.runner import DiscoverRunner
4-
import requests
54

65

76
class SilentDjangoTestSuiteRunner(DiscoverRunner):
@@ -13,14 +12,3 @@ def run_tests(self, test_labels, extra_tests=None, **kwargs):
1312
logging.disable(logging.ERROR)
1413
return super(SilentDjangoTestSuiteRunner, self).run_tests(
1514
test_labels, extra_tests, **kwargs)
16-
17-
18-
def mock_status_ok(*args, **kwargs):
19-
resp = requests.Response()
20-
resp.status_code = 200
21-
resp._content_consumed = True
22-
return resp
23-
24-
25-
def mock_none(*args, **kwargs):
26-
return None

rootfs/api/tests/test_app.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
from rest_framework.authtoken.models import Token
1717

1818
from api.models import App
19-
from . import mock_status_ok, mock_none
19+
20+
21+
def mock_none(*args, **kwargs):
22+
return None
2023

2124

2225
class AppTest(TestCase):
@@ -200,7 +203,6 @@ def test_app_structure_is_valid_json(self):
200203
self.assertIn('structure', response.data)
201204
self.assertEqual(response.data['structure'], {"web": 1})
202205

203-
@mock.patch('requests.post', mock_status_ok)
204206
@mock.patch('api.models.logger')
205207
def test_admin_can_manage_other_apps(self, mock_logger):
206208
"""Administrators of Deis should be able to manage all applications.

rootfs/api/tests/test_build.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from rest_framework.authtoken.models import Token
1515

1616
from api.models import Build
17-
from . import mock_status_ok
1817

1918

2019
@mock.patch('api.models.release.publish_release', lambda *args: None)
@@ -32,7 +31,6 @@ def tearDown(self):
3231
# make sure every test has a clean slate for k8s mocking
3332
cache.clear()
3433

35-
@mock.patch('requests.post', mock_status_ok)
3634
def test_build(self):
3735
"""
3836
Test that a null build is created and that users can post new builds
@@ -80,7 +78,6 @@ def test_build(self):
8078
response = self.client.delete(url, HTTP_AUTHORIZATION='token {}'.format(self.token))
8179
self.assertEqual(response.status_code, 405)
8280

83-
@mock.patch('requests.post', mock_status_ok)
8481
def test_response_data(self):
8582
"""Test that the serialized response contains only relevant data."""
8683
body = {'id': 'test'}
@@ -107,7 +104,6 @@ def test_response_data(self):
107104
}
108105
self.assertDictContainsSubset(expected, response.data)
109106

110-
@mock.patch('requests.post', mock_status_ok)
111107
def test_build_default_containers(self):
112108
url = '/v2/apps'
113109
response = self.client.post(url, HTTP_AUTHORIZATION='token {}'.format(self.token))
@@ -208,7 +204,6 @@ def test_build_default_containers(self):
208204
# pod name is auto generated so use regex
209205
self.assertRegex(container['name'], app_id + '-v2-web-[a-z0-9]{5}')
210206

211-
@mock.patch('requests.post', mock_status_ok)
212207
def test_build_str(self):
213208
"""Test the text representation of a build."""
214209
url = '/v2/apps'
@@ -225,7 +220,6 @@ def test_build_str(self):
225220
self.assertEqual(str(build), "{}-{}".format(
226221
response.data['app'], str(response.data['uuid'])[:7]))
227222

228-
@mock.patch('requests.post', mock_status_ok)
229223
def test_admin_can_create_builds_on_other_apps(self):
230224
"""If a user creates an application, an administrator should be able
231225
to push builds.
@@ -249,7 +243,6 @@ def test_admin_can_create_builds_on_other_apps(self):
249243
self.assertEqual(str(build), "{}-{}".format(
250244
response.data['app'], str(response.data['uuid'])[:7]))
251245

252-
@mock.patch('requests.post', mock_status_ok)
253246
def test_unauthorized_user_cannot_modify_build(self):
254247
"""
255248
An unauthorized user should not be able to modify other builds.
@@ -270,7 +263,6 @@ def test_unauthorized_user_cannot_modify_build(self):
270263
HTTP_AUTHORIZATION='token {}'.format(unauthorized_token))
271264
self.assertEqual(response.status_code, 403)
272265

273-
@mock.patch('requests.post', mock_status_ok)
274266
def test_new_build_does_not_scale_up_automatically(self):
275267
"""
276268
After the first initial deploy, if the containers are scaled down to zero,

rootfs/api/tests/test_config.py

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88

99
import json
10-
import requests
1110

1211
from django.contrib.auth.models import User
1312
from django.core.cache import cache
@@ -16,18 +15,6 @@
1615
from rest_framework.authtoken.models import Token
1716

1817
from api.models import App, Config
19-
from . import mock_status_ok
20-
21-
22-
def mock_status_not_found(*args, **kwargs):
23-
resp = requests.Response()
24-
resp.status_code = 404
25-
resp._content_consumed = True
26-
return resp
27-
28-
29-
def mock_request_connection_error(*args, **kwargs):
30-
raise requests.exceptions.ConnectionError("connection error")
3118

3219

3320
@mock.patch('api.models.release.publish_release', lambda *args: None)
@@ -49,7 +36,6 @@ def tearDown(self):
4936
# make sure every test has a clean slate for k8s mocking
5037
cache.clear()
5138

52-
@mock.patch('requests.post', mock_status_ok)
5339
def test_config(self):
5440
"""
5541
Test that config is auto-created for a new app and that
@@ -121,7 +107,6 @@ def test_config(self):
121107
self.assertEqual(response.status_code, 405)
122108
return config5
123109

124-
@mock.patch('requests.post', mock_status_ok)
125110
def test_response_data(self):
126111
"""Test that the serialized response contains only relevant data."""
127112
body = {'id': 'test'}
@@ -146,7 +131,6 @@ def test_response_data(self):
146131
}
147132
self.assertDictContainsSubset(expected, response.data)
148133

149-
@mock.patch('requests.post', mock_status_ok)
150134
def test_response_data_types_converted(self):
151135
"""Test that config data is converted into the correct type."""
152136
body = {'id': 'test'}
@@ -178,7 +162,6 @@ def test_response_data_types_converted(self):
178162
self.assertEqual(response.status_code, 400)
179163
self.assertIn('CPU shares must be a numeric value', response.data['cpu'])
180164

181-
@mock.patch('requests.post', mock_status_ok)
182165
def test_config_set_same_key(self):
183166
"""
184167
Test that config sets on the same key function properly
@@ -202,7 +185,6 @@ def test_config_set_same_key(self):
202185
self.assertIn('PORT', response.data['values'])
203186
self.assertEqual(response.data['values']['PORT'], '5001')
204187

205-
@mock.patch('requests.post', mock_status_ok)
206188
def test_config_set_unicode(self):
207189
"""
208190
Test that config sets with unicode values are accepted.
@@ -233,14 +215,12 @@ def test_config_set_unicode(self):
233215
self.assertIn('INTEGER', response.data['values'])
234216
self.assertEqual(response.data['values']['INTEGER'], '1')
235217

236-
@mock.patch('requests.post', mock_status_ok)
237218
def test_config_str(self):
238219
"""Test the text representation of a node."""
239220
config5 = self.test_config()
240221
config = Config.objects.get(uuid=config5['uuid'])
241222
self.assertEqual(str(config), "{}-{}".format(config5['app'], str(config5['uuid'])[:7]))
242223

243-
@mock.patch('requests.post', mock_status_ok)
244224
def test_valid_config_keys(self):
245225
"""Test that valid config keys are accepted.
246226
"""
@@ -258,7 +238,6 @@ def test_valid_config_keys(self):
258238
self.assertEqual(resp.status_code, 201)
259239
self.assertIn(k, resp.data['values'])
260240

261-
@mock.patch('requests.post', mock_status_ok)
262241
def test_invalid_config_keys(self):
263242
"""Test that invalid config keys are rejected.
264243
"""
@@ -275,7 +254,6 @@ def test_invalid_config_keys(self):
275254
HTTP_AUTHORIZATION='token {}'.format(self.token))
276255
self.assertEqual(resp.status_code, 400)
277256

278-
@mock.patch('requests.post', mock_status_ok)
279257
def test_admin_can_create_config_on_other_apps(self):
280258
"""If a non-admin creates an app, an administrator should be able to set config
281259
values for that app.
@@ -295,7 +273,6 @@ def test_admin_can_create_config_on_other_apps(self):
295273
self.assertIn('PORT', response.data['values'])
296274
return response
297275

298-
@mock.patch('requests.post', mock_status_ok)
299276
def test_limit_memory(self):
300277
"""
301278
Test that limit is auto-created for a new app and that
@@ -383,7 +360,6 @@ def test_limit_memory(self):
383360
self.assertEqual(response.status_code, 405)
384361
return limit4
385362

386-
@mock.patch('requests.post', mock_status_ok)
387363
def test_limit_cpu(self):
388364
"""
389365
Test that CPU limits can be set
@@ -454,7 +430,6 @@ def test_limit_cpu(self):
454430
self.assertEqual(response.status_code, 405)
455431
return limit4
456432

457-
@mock.patch('requests.post', mock_status_ok)
458433
def test_tags(self):
459434
"""
460435
Test that tags can be set on an application

rootfs/api/tests/test_hooks.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
from unittest import mock
1515
from rest_framework.authtoken.models import Token
1616

17-
from . import mock_status_ok
18-
1917
RSA_PUBKEY = (
2018
"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCfQkkUUoxpvcNMkvv7jqnfodgs37M2eBO"
2119
"APgLK+KNBMaZaaKB4GF1QhTCMfFhoiTW3rqa0J75bHJcdkoobtTHlK8XUrFqsquWyg3XhsT"
@@ -196,7 +194,6 @@ def test_push_abuse(self):
196194
HTTP_X_DEIS_BUILDER_AUTH=settings.BUILDER_KEY)
197195
self.assertEqual(response.status_code, 403)
198196

199-
@mock.patch('requests.post', mock_status_ok)
200197
def test_build_hook(self):
201198
"""Test creating a Build via an API Hook"""
202199
url = '/v2/apps'
@@ -218,7 +215,6 @@ def test_build_hook(self):
218215
self.assertIn('release', response.data)
219216
self.assertIn('version', response.data['release'])
220217

221-
@mock.patch('requests.post', mock_status_ok)
222218
def test_build_hook_slug_url(self):
223219
"""Test creating a slug_url build via an API Hook"""
224220
url = '/v2/apps'

rootfs/api/tests/test_pods.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from rest_framework.authtoken.models import Token
1515

1616
from api.models import App, Build, Release
17-
from . import mock_status_ok
1817

1918

2019
@mock.patch('api.models.release.publish_release', lambda *args: None)
@@ -132,7 +131,6 @@ def test_container_api_heroku(self):
132131
response = self.client.get(url, HTTP_AUTHORIZATION='token {}'.format(self.token))
133132
self.assertEqual(response.status_code, 200)
134133

135-
@mock.patch('requests.post', mock_status_ok)
136134
def test_container_api_docker(self):
137135
url = '/v2/apps'
138136
response = self.client.post(url, HTTP_AUTHORIZATION='token {}'.format(self.token))
@@ -209,7 +207,6 @@ def test_container_api_docker(self):
209207
response = self.client.get(url, HTTP_AUTHORIZATION='token {}'.format(self.token))
210208
self.assertEqual(response.status_code, 200)
211209

212-
@mock.patch('requests.post', mock_status_ok)
213210
def test_release(self):
214211
url = '/v2/apps'
215212
response = self.client.post(url, HTTP_AUTHORIZATION='token {}'.format(self.token))

rootfs/api/tests/test_release.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
from rest_framework.authtoken.models import Token
1616

1717
from api.models import Release
18-
from . import mock_status_ok
1918

2019

2120
@mock.patch('api.models.release.publish_release', lambda *args: None)
@@ -33,7 +32,6 @@ def tearDown(self):
3332
# make sure every test has a clean slate for k8s mocking
3433
cache.clear()
3534

36-
@mock.patch('requests.post', mock_status_ok)
3735
def test_release(self):
3836
"""
3937
Test that a release is created when an app is created, and
@@ -110,7 +108,6 @@ def test_release(self):
110108
self.assertEqual(response.status_code, 405)
111109
return release3
112110

113-
@mock.patch('requests.post', mock_status_ok)
114111
def test_response_data(self):
115112
body = {'id': 'test'}
116113
response = self.client.post('/v2/apps', json.dumps(body),
@@ -135,7 +132,6 @@ def test_response_data(self):
135132
}
136133
self.assertDictContainsSubset(expected, response.data)
137134

138-
@mock.patch('requests.post', mock_status_ok)
139135
def test_release_rollback(self):
140136
url = '/v2/apps'
141137
response = self.client.post(url, HTTP_AUTHORIZATION='token {}'.format(self.token))
@@ -233,22 +229,19 @@ def test_release_rollback(self):
233229
self.assertIn('NEW_URL1', values)
234230
self.assertEqual('http://localhost:8080/', values['NEW_URL1'])
235231

236-
@mock.patch('requests.post', mock_status_ok)
237232
def test_release_str(self):
238233
"""Test the text representation of a release."""
239234
release3 = self.test_release()
240235
release = Release.objects.get(uuid=release3['uuid'])
241236
self.assertEqual(str(release), "{}-v3".format(release3['app']))
242237

243-
@mock.patch('requests.post', mock_status_ok)
244238
def test_release_summary(self):
245239
"""Test the text summary of a release."""
246240
release3 = self.test_release()
247241
release = Release.objects.get(uuid=release3['uuid'])
248242
# check that the release has push and env change messages
249243
self.assertIn('autotest deployed ', release.summary)
250244

251-
@mock.patch('requests.post', mock_status_ok)
252245
def test_admin_can_create_release(self):
253246
"""If a non-user creates an app, an admin should be able to create releases."""
254247
user = User.objects.get(username='autotest2')
@@ -272,7 +265,6 @@ def test_admin_can_create_release(self):
272265
# account for the config release as well
273266
self.assertEqual(response.data['count'], 2)
274267

275-
@mock.patch('requests.post', mock_status_ok)
276268
def test_unauthorized_user_cannot_modify_release(self):
277269
"""
278270
An unauthorized user should not be able to modify other releases.

0 commit comments

Comments
 (0)