Skip to content
This repository was archived by the owner on Mar 15, 2018. It is now read-only.

Commit db00443

Browse files
author
Rob Hudson
committed
Mock ES to stop series security failures (bug 734217)
This also fixed a few cases where Mock was backwards incompatible.
1 parent 0816388 commit db00443

File tree

6 files changed

+43
-39
lines changed

6 files changed

+43
-39
lines changed

apps/amo/tests/__init__.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -171,21 +171,29 @@ def __getattr__(self, name):
171171
raise AttributeError
172172

173173

174+
ES_patcher = mock.patch('elasticutils.get_es', spec=True)
175+
176+
174177
class TestCase(RedisTest, test_utils.TestCase):
175178
"""Base class for all amo tests."""
176179
client_class = TestClient
180+
mock_es = True
181+
182+
@classmethod
183+
def setUpClass(cls):
184+
super(TestCase, cls).setUpClass()
185+
if cls.mock_es:
186+
ES_patcher.start()
187+
188+
@classmethod
189+
def tearDownClass(cls):
190+
super(TestCase, cls).tearDownClass()
191+
if cls.mock_es:
192+
ES_patcher.stop()
177193

178194
def _pre_setup(self):
179195
super(TestCase, self)._pre_setup()
180196
self.reset_featured_addons()
181-
# Mock out ES indexing for non-ES tests.
182-
if not getattr(self, 'es', False):
183-
for p in ['addons.tasks.index_addons',
184-
'addons.tasks.unindex_addons',
185-
'amo.models.SearchMixin']:
186-
patcher = mock.patch(p)
187-
patcher.start()
188-
self.addCleanup(patcher.stop)
189197

190198
def reset_featured_addons(self):
191199
from addons.cron import reset_featured_addons
@@ -383,10 +391,12 @@ class ESTestCase(TestCase):
383391
# outside Django transactions so be careful to clean up afterwards.
384392
es = True
385393
use_es = None
394+
mock_es = False
386395
exempt_from_fixture_bundling = True # ES doesn't support bundling (yet?)
387396

388397
@classmethod
389398
def setUpClass(cls):
399+
super(ESTestCase, cls).setUpClass()
390400
cls.es = elasticutils.get_es()
391401

392402
if ESTestCase.use_es is None:
@@ -410,7 +420,6 @@ def setUpClass(cls):
410420
except:
411421
raise
412422

413-
super(ESTestCase, cls).setUpClass()
414423
addons.search.setup_mapping()
415424
stats.search.setup_indexes()
416425

@@ -426,6 +435,7 @@ def tearDownClass(cls):
426435
Translation, Addon, Collection, AppVersion, Application)
427436
for model in models:
428437
model.objects.all().delete()
438+
429439
super(ESTestCase, cls).tearDownClass()
430440

431441
@classmethod

apps/amo/tests/test_search.py

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,48 @@
11
from django.core import paginator
22

3+
import elasticutils
34
import mock
45
from nose.tools import eq_
56

7+
import amo
68
import amo.tests
7-
import amo.utils
89
from addons.models import Addon
9-
import addons.tasks
1010

1111

1212
class TestESIndexing(amo.tests.ESTestCase):
13+
mock_es = False
1314
es = True
1415

15-
@classmethod
16-
def setUpClass(cls):
17-
super(TestESIndexing, cls).setUpClass()
18-
cls.setUpIndex()
19-
2016
# This needs to be in its own class for data isolation.
2117
def test_indexed_count(self):
2218
# Did all the right addons get indexed?
2319
eq_(Addon.search().filter(type=1, is_disabled=False).count(),
2420
Addon.objects.filter(disabled_by_user=False,
2521
status__in=amo.VALID_STATUSES).count())
2622

27-
def test_real_indexing(self):
28-
def check(s):
29-
assert callable(s) and not isinstance(s, mock.Mock)
30-
check(addons.tasks.index_addons)
31-
check(addons.tasks.unindex_addons)
32-
check(amo.models.SearchMixin)
23+
def test_get_es_not_mocked(self):
24+
es = elasticutils.get_es()
25+
assert not issubclass(es.__class__, mock.Mock)
3326

3427

3528
class TestNoESIndexing(amo.tests.TestCase):
3629

30+
mock_es = True
31+
3732
def test_no_es(self):
3833
assert not getattr(self, 'es', False), (
3934
'TestCase should not have "es" attribute')
4035

41-
def test_mocked_indexing(self):
42-
def check(s):
43-
assert callable(s) and isinstance(s, mock.Mock)
44-
check(addons.tasks.index_addons)
45-
check(addons.tasks.unindex_addons)
46-
check(amo.models.SearchMixin)
47-
4836
def test_not_indexed(self):
4937
addon = Addon.objects.create(type=amo.ADDON_EXTENSION,
5038
status=amo.STATUS_PUBLIC)
51-
eq_(Addon.search().filter(id__in=addon.id).count(), 0)
39+
assert issubclass(
40+
Addon.search().filter(id__in=addon.id).count().__class__,
41+
mock.Mock)
42+
43+
def test_get_es_mocked(self):
44+
es = elasticutils.get_es()
45+
assert issubclass(es.__class__, mock.Mock)
5246

5347

5448
class TestES(amo.tests.ESTestCase):

apps/editors/tests/test_views.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
from django.utils.datastructures import SortedDict
1010

1111
import jingo
12-
from mock import patch
12+
from mock import Mock, patch
1313
from nose.tools import eq_
1414
from pyquery import PyQuery as pq
15+
import waffle
1516

1617
import amo
1718
import amo.tests
@@ -245,10 +246,9 @@ def test_search_addon_doesnt_exist(self):
245246
def test_breadcrumbs(self):
246247
self._test_breadcrumbs([('Add-on Review Log', None)])
247248

248-
@patch('devhub.models.ActivityLog.arguments')
249-
def test_addon_missing(self, arguments):
249+
@patch('devhub.models.ActivityLog.arguments', new=Mock)
250+
def test_addon_missing(self):
250251
self.make_approvals()
251-
arguments.return_value = []
252252
r = self.client.get(self.url)
253253
eq_(pq(r.content)('#log-listing tr td').eq(1).text(),
254254
'Add-on has been deleted.')
@@ -1889,9 +1889,8 @@ def test_compare_link(self):
18891889
]
18901890
check_links(expected, links, verify=False)
18911891

1892-
# TODO: Remove 'marketplace' switch.
1893-
@patch.dict(jingo.env.globals['waffle'], {'switch': lambda x: True})
18941892
def test_show_premium(self):
1893+
waffle.models.Switch.objects.create(name='marketplace', active=True)
18951894
for type_ in amo.ADDON_PREMIUMS:
18961895
self.addon.update(premium_type=type_)
18971896
res = self.client.get(self.url)

apps/stats/tests/test_views.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ def index(self):
8686

8787
class TestSeriesSecurity(ESStatsTest):
8888
"""Tests to make sure all restricted data remains restricted."""
89+
mock_es = True # We're checking only headers, not content.
8990

9091
def _check_it(self, views, status):
9192
for view, kwargs in views:

mkt/site/tests/test_views.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77

88
class TestRobots(amo.tests.TestCase):
99

10-
@mock.patch_object(settings, 'ENGAGE_ROBOTS', True)
10+
@mock.patch.object(settings, 'ENGAGE_ROBOTS', True)
1111
def test_engage_robots(self):
1212
rs = self.client.get('/robots.txt')
1313
self.assertContains(rs, 'Allow: /')
1414

15-
@mock.patch_object(settings, 'ENGAGE_ROBOTS', False)
15+
@mock.patch.object(settings, 'ENGAGE_ROBOTS', False)
1616
def test_do_not_engage_robots(self):
1717
rs = self.client.get('/robots.txt')
1818
self.assertContains(rs, 'Disallow: /')

0 commit comments

Comments
 (0)