Skip to content

Commit 865b9b3

Browse files
committed
ref(controller): collapse duplicate mock definitions
1 parent 324aa65 commit 865b9b3

7 files changed

Lines changed: 35 additions & 67 deletions

File tree

controller/api/tests/__init__.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from django.conf import settings
77
from django.test.client import RequestFactory, Client
88
from django.test.simple import DjangoTestSuiteRunner
9+
import requests
910

1011

1112
# add patch support to built-in django test client
@@ -46,18 +47,25 @@ def run_tests(self, test_labels, extra_tests=None, **kwargs):
4647
test_labels, extra_tests, **kwargs)
4748

4849

50+
def mock_status_ok(*args, **kwargs):
51+
resp = requests.Response()
52+
resp.status_code = 200
53+
resp._content_consumed = True
54+
return resp
55+
56+
4957
from .test_api_middleware import * # noqa
5058
from .test_app import * # noqa
5159
from .test_auth import * # noqa
5260
from .test_build import * # noqa
53-
from .test_config import * # noqa
54-
from .test_domain import * # noqa
5561
from .test_certificate import * # noqa
62+
from .test_config import * # noqa
5663
from .test_container import * # noqa
64+
from .test_domain import * # noqa
5765
from .test_hooks import * # noqa
5866
from .test_key import * # noqa
67+
from .test_limits import * # noqa
5968
from .test_perm import * # noqa
6069
from .test_release import * # noqa
6170
from .test_scheduler import * # noqa
6271
from .test_users import * # noqa
63-
from .test_limits import * # noqa

controller/api/tests/test_app.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,14 @@
1010
import logging
1111
import mock
1212
import os.path
13-
import requests
1413

1514
from django.conf import settings
1615
from django.contrib.auth.models import User
1716
from django.test import TestCase
1817
from rest_framework.authtoken.models import Token
1918

2019
from api.models import App
21-
22-
23-
def mock_import_repository_task(*args, **kwargs):
24-
resp = requests.Response()
25-
resp.status_code = 200
26-
resp._content_consumed = True
27-
return resp
20+
from . import mock_status_ok
2821

2922

3023
class AppTest(TestCase):
@@ -198,7 +191,7 @@ def test_app_structure_is_valid_json(self):
198191
self.assertIn('structure', response.data)
199192
self.assertEqual(response.data['structure'], {"web": 1})
200193

201-
@mock.patch('requests.post', mock_import_repository_task)
194+
@mock.patch('requests.post', mock_status_ok)
202195
@mock.patch('api.models.logger')
203196
def test_admin_can_manage_other_apps(self, mock_logger):
204197
"""Administrators of Deis should be able to manage all applications.

controller/api/tests/test_build.py

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,14 @@
77
from __future__ import unicode_literals
88

99
import json
10-
import requests
1110

1211
from django.contrib.auth.models import User
1312
from django.test import TransactionTestCase
1413
import mock
1514
from rest_framework.authtoken.models import Token
1615

1716
from api.models import Build
18-
19-
20-
def mock_import_repository_task(*args, **kwargs):
21-
resp = requests.Response()
22-
resp.status_code = 200
23-
resp._content_consumed = True
24-
return resp
17+
from . import mock_status_ok
2518

2619

2720
@mock.patch('api.models.publish_release', lambda *args: None)
@@ -35,7 +28,7 @@ def setUp(self):
3528
self.user = User.objects.get(username='autotest')
3629
self.token = Token.objects.get(user=self.user).key
3730

38-
@mock.patch('requests.post', mock_import_repository_task)
31+
@mock.patch('requests.post', mock_status_ok)
3932
def test_build(self):
4033
"""
4134
Test that a null build is created and that users can post new builds
@@ -83,7 +76,7 @@ def test_build(self):
8376
response = self.client.delete(url, HTTP_AUTHORIZATION='token {}'.format(self.token))
8477
self.assertEqual(response.status_code, 405)
8578

86-
@mock.patch('requests.post', mock_import_repository_task)
79+
@mock.patch('requests.post', mock_status_ok)
8780
def test_response_data(self):
8881
"""Test that the serialized response contains only relevant data."""
8982
body = {'id': 'test'}
@@ -109,7 +102,7 @@ def test_response_data(self):
109102
}
110103
self.assertDictContainsSubset(expected, response.data)
111104

112-
@mock.patch('requests.post', mock_import_repository_task)
105+
@mock.patch('requests.post', mock_status_ok)
113106
def test_build_default_containers(self):
114107
url = '/v1/apps'
115108
response = self.client.post(url, HTTP_AUTHORIZATION='token {}'.format(self.token))
@@ -195,7 +188,7 @@ def test_build_default_containers(self):
195188
self.assertEqual(container['type'], 'web')
196189
self.assertEqual(container['num'], 1)
197190

198-
@mock.patch('requests.post', mock_import_repository_task)
191+
@mock.patch('requests.post', mock_status_ok)
199192
def test_build_str(self):
200193
"""Test the text representation of a build."""
201194
url = '/v1/apps'
@@ -212,7 +205,7 @@ def test_build_str(self):
212205
self.assertEqual(str(build), "{}-{}".format(
213206
response.data['app'], response.data['uuid'][:7]))
214207

215-
@mock.patch('requests.post', mock_import_repository_task)
208+
@mock.patch('requests.post', mock_status_ok)
216209
def test_admin_can_create_builds_on_other_apps(self):
217210
"""If a user creates an application, an administrator should be able
218211
to push builds.
@@ -234,7 +227,7 @@ def test_admin_can_create_builds_on_other_apps(self):
234227
self.assertEqual(str(build), "{}-{}".format(
235228
response.data['app'], response.data['uuid'][:7]))
236229

237-
@mock.patch('requests.post', mock_import_repository_task)
230+
@mock.patch('requests.post', mock_status_ok)
238231
def test_unauthorized_user_cannot_modify_build(self):
239232
"""
240233
An unauthorized user should not be able to modify other builds.
@@ -255,7 +248,7 @@ def test_unauthorized_user_cannot_modify_build(self):
255248
HTTP_AUTHORIZATION='token {}'.format(unauthorized_token))
256249
self.assertEqual(response.status_code, 403)
257250

258-
@mock.patch('requests.post', mock_import_repository_task)
251+
@mock.patch('requests.post', mock_status_ok)
259252
def test_new_build_does_not_scale_up_automatically(self):
260253
"""
261254
After the first initial deploy, if the containers are scaled down to zero,

controller/api/tests/test_config.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,7 @@
1919

2020
import api.exceptions
2121
from api.models import App, Config
22-
23-
24-
def mock_status_ok(*args, **kwargs):
25-
resp = requests.Response()
26-
resp.status_code = 200
27-
resp._content_consumed = True
28-
return resp
22+
from . import mock_status_ok
2923

3024

3125
def mock_status_not_found(*args, **kwargs):

controller/api/tests/test_container.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from __future__ import unicode_literals
88

99
import json
10-
import requests
1110

1211
from django.contrib.auth.models import User
1312
from django.test import TransactionTestCase
@@ -16,13 +15,7 @@
1615

1716
from api.models import App, Build, Container, Release
1817
from scheduler.states import TransitionError
19-
20-
21-
def mock_import_repository_task(*args, **kwargs):
22-
resp = requests.Response()
23-
resp.status_code = 200
24-
resp._content_consumed = True
25-
return resp
18+
from . import mock_status_ok
2619

2720

2821
@mock.patch('api.models.publish_release', lambda *args: None)
@@ -171,7 +164,7 @@ def test_container_api_heroku(self):
171164
response = self.client.get(url, HTTP_AUTHORIZATION='token {}'.format(self.token))
172165
self.assertEqual(response.status_code, 200)
173166

174-
@mock.patch('requests.post', mock_import_repository_task)
167+
@mock.patch('requests.post', mock_status_ok)
175168
def test_container_api_docker(self):
176169
url = '/v1/apps'
177170
response = self.client.post(url, HTTP_AUTHORIZATION='token {}'.format(self.token))
@@ -234,7 +227,7 @@ def test_container_api_docker(self):
234227
response = self.client.get(url, HTTP_AUTHORIZATION='token {}'.format(self.token))
235228
self.assertEqual(response.status_code, 200)
236229

237-
@mock.patch('requests.post', mock_import_repository_task)
230+
@mock.patch('requests.post', mock_status_ok)
238231
def test_container_release(self):
239232
url = '/v1/apps'
240233
response = self.client.post(url, HTTP_AUTHORIZATION='token {}'.format(self.token))

controller/api/tests/test_hooks.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,14 @@
77
from __future__ import unicode_literals
88

99
import json
10-
import requests
1110

1211
from django.conf import settings
1312
from django.contrib.auth.models import User
1413
from django.test import TransactionTestCase
1514
import mock
1615
from rest_framework.authtoken.models import Token
1716

18-
19-
def mock_import_repository_task(*args, **kwargs):
20-
resp = requests.Response()
21-
resp.status_code = 200
22-
resp._content_consumed = True
23-
return resp
17+
from . import mock_status_ok
2418

2519

2620
@mock.patch('api.models.publish_release', lambda *args: None)
@@ -98,7 +92,7 @@ def test_push_abuse(self):
9892
HTTP_X_DEIS_BUILDER_AUTH=settings.BUILDER_KEY)
9993
self.assertEqual(response.status_code, 403)
10094

101-
@mock.patch('requests.post', mock_import_repository_task)
95+
@mock.patch('requests.post', mock_status_ok)
10296
def test_build_hook(self):
10397
"""Test creating a Build via an API Hook"""
10498
url = '/v1/apps'

controller/api/tests/test_release.py

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,14 @@
77
from __future__ import unicode_literals
88

99
import json
10-
import requests
1110

1211
from django.contrib.auth.models import User
1312
from django.test import TransactionTestCase
1413
import mock
1514
from rest_framework.authtoken.models import Token
1615

1716
from api.models import Release
18-
19-
20-
def mock_import_repository_task(*args, **kwargs):
21-
resp = requests.Response()
22-
resp.status_code = 200
23-
resp._content_consumed = True
24-
return resp
17+
from . import mock_status_ok
2518

2619

2720
@mock.patch('api.models.publish_release', lambda *args: None)
@@ -35,7 +28,7 @@ def setUp(self):
3528
self.user = User.objects.get(username='autotest')
3629
self.token = Token.objects.get(user=self.user).key
3730

38-
@mock.patch('requests.post', mock_import_repository_task)
31+
@mock.patch('requests.post', mock_status_ok)
3932
def test_release(self):
4033
"""
4134
Test that a release is created when an app is created, and
@@ -112,7 +105,7 @@ def test_release(self):
112105
self.assertEqual(response.status_code, 405)
113106
return release3
114107

115-
@mock.patch('requests.post', mock_import_repository_task)
108+
@mock.patch('requests.post', mock_status_ok)
116109
def test_response_data(self):
117110
body = {'id': 'test'}
118111
response = self.client.post('/v1/apps', json.dumps(body),
@@ -137,7 +130,7 @@ def test_response_data(self):
137130
}
138131
self.assertDictContainsSubset(expected, response.data)
139132

140-
@mock.patch('requests.post', mock_import_repository_task)
133+
@mock.patch('requests.post', mock_status_ok)
141134
def test_release_rollback(self):
142135
url = '/v1/apps'
143136
response = self.client.post(url, HTTP_AUTHORIZATION='token {}'.format(self.token))
@@ -234,22 +227,22 @@ def test_release_rollback(self):
234227
self.assertIn('NEW_URL1', values)
235228
self.assertEqual('http://localhost:8080/', values['NEW_URL1'])
236229

237-
@mock.patch('requests.post', mock_import_repository_task)
230+
@mock.patch('requests.post', mock_status_ok)
238231
def test_release_str(self):
239232
"""Test the text representation of a release."""
240233
release3 = self.test_release()
241234
release = Release.objects.get(uuid=release3['uuid'])
242235
self.assertEqual(str(release), "{}-v3".format(release3['app']))
243236

244-
@mock.patch('requests.post', mock_import_repository_task)
237+
@mock.patch('requests.post', mock_status_ok)
245238
def test_release_summary(self):
246239
"""Test the text summary of a release."""
247240
release3 = self.test_release()
248241
release = Release.objects.get(uuid=release3['uuid'])
249242
# check that the release has push and env change messages
250243
self.assertIn('autotest deployed ', release.summary)
251244

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

276-
@mock.patch('requests.post', mock_import_repository_task)
269+
@mock.patch('requests.post', mock_status_ok)
277270
def test_unauthorized_user_cannot_modify_release(self):
278271
"""
279272
An unauthorized user should not be able to modify other releases.

0 commit comments

Comments
 (0)