From ceef110aa73ac4134b5b75d58a603ee7cb6b584e Mon Sep 17 00:00:00 2001 From: Will Kahn-Greene Date: Fri, 13 Oct 2017 20:47:53 -0400 Subject: [PATCH] bug 1405675 - update crashstats/api/tests/* to pytest --- .../crashstats/api/tests/test_cleaner.py | 27 +- .../api/tests/test_jinja_helpers.py | 10 +- .../crashstats/api/tests/test_views.py | 250 +++++++++--------- 3 files changed, 142 insertions(+), 145 deletions(-) diff --git a/webapp-django/crashstats/api/tests/test_cleaner.py b/webapp-django/crashstats/api/tests/test_cleaner.py index a410ef2352..bb90f3be92 100644 --- a/webapp-django/crashstats/api/tests/test_cleaner.py +++ b/webapp-django/crashstats/api/tests/test_cleaner.py @@ -1,5 +1,4 @@ import mock -from nose.tools import eq_, ok_ from crashstats.base.tests.testbase import TestCase from crashstats.api.cleaner import Cleaner, SmartWhitelistMatcher @@ -30,7 +29,7 @@ def test_simplest_case(self): 'bar': 5}, ] } - eq_(data, expect) + assert data == expect @mock.patch('warnings.warn') def test_simplest_case_with_warning(self, p_warn): @@ -75,7 +74,7 @@ def test_all_dict_data(self): 'bar': 8, }, } - eq_(data, expect) + assert data == expect def test_simple_list(self): whitelist = ('foo', 'bar') @@ -103,7 +102,7 @@ def test_simple_list(self): 'bar': 8, }, ] - eq_(data, expect) + assert data == expect def test_plain_dict(self): whitelist = ('foo', 'bar') @@ -118,7 +117,7 @@ def test_plain_dict(self): 'foo': 1, 'bar': 2, } - eq_(data, expect) + assert data == expect def test_dict_data_with_lists(self): whitelist = { @@ -152,7 +151,7 @@ def test_dict_data_with_lists(self): ] } } - eq_(data, expect) + assert data == expect def test_all_dict_data_deeper(self): whitelist = {Cleaner.ANY: {Cleaner.ANY: ('foo', 'bar')}} @@ -206,7 +205,7 @@ def test_all_dict_data_deeper(self): } }, } - eq_(data, expect) + assert data == expect def test_with_scrubber_cleaning(self): whitelist = {'hits': ('foo', 'bar', 'baz')} @@ -240,7 +239,7 @@ def test_with_scrubber_cleaning(self): 'baz': "talk to bill@gates.com"}, ] } - eq_(data, expect) + assert data == expect class TestSmartWhitelistMatcher(TestCase): @@ -248,9 +247,9 @@ class TestSmartWhitelistMatcher(TestCase): def test_basic_in(self): whitelist = ['some', 'thing*'] matcher = SmartWhitelistMatcher(whitelist) - ok_('some' in matcher) - ok_('something' not in matcher) - ok_('awesome' not in matcher) - ok_('thing' in matcher) - ok_('things' in matcher) - ok_('nothing' not in matcher) + assert 'some' in matcher + assert 'something' not in matcher + assert 'awesome' not in matcher + assert 'thing' in matcher + assert 'things' in matcher + assert 'nothing' not in matcher diff --git a/webapp-django/crashstats/api/tests/test_jinja_helpers.py b/webapp-django/crashstats/api/tests/test_jinja_helpers.py index 8faa84f333..066be3c648 100644 --- a/webapp-django/crashstats/api/tests/test_jinja_helpers.py +++ b/webapp-django/crashstats/api/tests/test_jinja_helpers.py @@ -1,5 +1,3 @@ -from nose.tools import eq_ - from crashstats.base.tests.testbase import TestCase from crashstats.api.templatetags.jinja_helpers import pluralize @@ -7,9 +5,9 @@ class TestPluralize(TestCase): def test_basics(self): - eq_(pluralize(0), 's') - eq_(pluralize(1), '') - eq_(pluralize(59), 's') + assert pluralize(0) == 's' + assert pluralize(1) == '' + assert pluralize(59) == 's' def test_overide_s(self): - eq_(pluralize(59, 'ies'), 'ies') + assert pluralize(59, 'ies') == 'ies' diff --git a/webapp-django/crashstats/api/tests/test_views.py b/webapp-django/crashstats/api/tests/test_views.py index 1e0b909319..a73a2849cd 100644 --- a/webapp-django/crashstats/api/tests/test_views.py +++ b/webapp-django/crashstats/api/tests/test_views.py @@ -7,7 +7,6 @@ import mock import pyquery -from nose.tools import eq_, ok_ from socorro.lib import BadArgumentError, MissingArgumentError from crashstats.base.tests.testbase import TestCase @@ -35,16 +34,16 @@ class TestDedentLeft(TestCase): def test_dedent_left(self): from crashstats.api.views import dedent_left - eq_(dedent_left('Hello', 2), 'Hello') - eq_(dedent_left(' Hello', 2), ' Hello') - eq_(dedent_left(' Hello ', 2), ' Hello ') + assert dedent_left('Hello', 2) == 'Hello' + assert dedent_left(' Hello', 2) == ' Hello' + assert dedent_left(' Hello ', 2) == ' Hello ' text = """Line 1 Line 2 Line 3 """.rstrip() # because this code right above is indented with 2 * 4 spaces - eq_(dedent_left(text, 8), 'Line 1\nLine 2\nLine 3') + assert dedent_left(text, 8) == 'Line 1\nLine 2\nLine 3' class TestDocumentationViews(BaseTestViews): @@ -61,13 +60,13 @@ def mocked_supersearchfields_get_fields(**params): url = reverse('api:documentation') response = self.client.get(url) - eq_(response.status_code, 200) + assert response.status_code == 200 doc = pyquery.PyQuery(response.content) from crashstats.api import views for elt in doc('#mainbody .panel .title h2 a'): - ok_(elt.text not in views.BLACKLIST) + assert elt.text not in views.BLACKLIST class TestViews(BaseTestViews): @@ -86,16 +85,16 @@ def tearDown(self): def test_invalid_url(self): url = reverse('api:model_wrapper', args=('BlaBLabla',)) response = self.client.get(url) - eq_(response.status_code, 404) + assert response.status_code == 404 def test_base_classes_raise_not_found(self): url = reverse('api:model_wrapper', args=('SocorroMiddleware',)) response = self.client.get(url) - eq_(response.status_code, 404) + assert response.status_code == 404 url = reverse('api:model_wrapper', args=('ESSocorroMiddleware',)) response = self.client.get(url) - eq_(response.status_code, 404) + assert response.status_code == 404 def test_CORS(self): """any use of model_wrapper should return a CORS header""" @@ -110,8 +109,8 @@ def mocked_get(**options): url = reverse('api:model_wrapper', args=('Status',)) response = self.client.get(url) - eq_(response.status_code, 200) - eq_(response['Access-Control-Allow-Origin'], '*') + assert response.status_code == 200 + assert response['Access-Control-Allow-Origin'] == '*' def test_cache_control(self): """successful queries against models with caching should @@ -133,11 +132,11 @@ def mocked_get(**options): url = reverse('api:model_wrapper', args=('ProductBuildTypes',)) response = self.client.get(url, {'product': settings.DEFAULT_PRODUCT}) - eq_(response.status_code, 200) + assert response.status_code == 200 assert response['Cache-Control'] - ok_('private' in response['Cache-Control']) + assert 'private' in response['Cache-Control'] cache_seconds = ProductBuildTypes.cache_seconds - ok_('max-age={}'.format(cache_seconds) in response['Cache-Control']) + assert 'max-age={}'.format(cache_seconds) in response['Cache-Control'] def test_ProductVersions(self): @@ -157,30 +156,31 @@ def mocked_get(*args, **k): url = reverse('api:model_wrapper', args=('ProductVersions',)) response = self.client.get(url) - eq_(response.status_code, 200) + assert response.status_code == 200 dump = json.loads(response.content) - eq_(dump['total'], 1) - eq_(dump['hits'][0], { + assert dump['total'] == 1 + expected = { 'product': 'Firefox', 'version': '1.0', - }) + } + assert dump['hits'][0] == expected def test_Platforms(self): # note: this gets mocked out in the setUp url = reverse('api:model_wrapper', args=('Platforms',)) response = self.client.get(url) - eq_(response.status_code, 200) + assert response.status_code == 200 dump = json.loads(response.content) # see the setUp for this fixture - eq_(dump[0], {'code': 'win', 'name': 'Windows'}) + assert dump[0] == {'code': 'win', 'name': 'Windows'} def test_ProcessedCrash(self): url = reverse('api:model_wrapper', args=('ProcessedCrash',)) response = self.client.get(url) - eq_(response.status_code, 400) - eq_(response['Content-Type'], 'application/json; charset=UTF-8') + assert response.status_code == 400 + assert response['Content-Type'] == 'application/json; charset=UTF-8' dump = json.loads(response.content) - ok_(dump['errors']['crash_id']) + assert dump['errors']['crash_id'] def mocked_get(**params): if 'datatype' in params and params['datatype'] == 'processed': @@ -224,17 +224,17 @@ def mocked_get(**params): response = self.client.get(url, { 'crash_id': '123', }) - eq_(response.status_code, 200, response.content) + assert response.status_code == 200 dump = json.loads(response.content) - eq_(dump['uuid'], u'11cb72f5-eb28-41e1-a8e4-849982120611') - ok_('upload_file_minidump_flash2' in dump) - ok_('url' not in dump) + assert dump['uuid'] == u'11cb72f5-eb28-41e1-a8e4-849982120611' + assert 'upload_file_minidump_flash2' in dump + assert 'url' not in dump def test_UnredactedCrash(self): url = reverse('api:model_wrapper', args=('UnredactedCrash',)) response = self.client.get(url) # because we don't have the sufficient permissions yet to use it - eq_(response.status_code, 403) + assert response.status_code == 403 user = User.objects.create(username='test') self._add_permission(user, 'view_pii') @@ -253,10 +253,10 @@ def test_UnredactedCrash(self): token.permissions.add(view_exploitability_perm) response = self.client.get(url, HTTP_AUTH_TOKEN=token.key) - eq_(response.status_code, 400) - eq_(response['Content-Type'], 'application/json; charset=UTF-8') + assert response.status_code == 400 + assert response['Content-Type'] == 'application/json; charset=UTF-8' dump = json.loads(response.content) - ok_(dump['errors']['crash_id']) + assert dump['errors']['crash_id'] def mocked_get(**params): if 'datatype' in params and params['datatype'] == 'unredacted': @@ -301,11 +301,11 @@ def mocked_get(**params): response = self.client.get(url, { 'crash_id': '123', }) - eq_(response.status_code, 200) + assert response.status_code == 200 dump = json.loads(response.content) - eq_(dump['uuid'], u'11cb72f5-eb28-41e1-a8e4-849982120611') - ok_('upload_file_minidump_flash2' in dump) - ok_('exploitability' in dump) + assert dump['uuid'] == u'11cb72f5-eb28-41e1-a8e4-849982120611' + assert 'upload_file_minidump_flash2' in dump + assert 'exploitability' in dump def test_RawCrash(self): @@ -352,29 +352,29 @@ def mocked_get(**params): url = reverse('api:model_wrapper', args=('RawCrash',)) response = self.client.get(url) - eq_(response.status_code, 400) - eq_(response['Content-Type'], 'application/json; charset=UTF-8') + assert response.status_code == 400 + assert response['Content-Type'] == 'application/json; charset=UTF-8' dump = json.loads(response.content) - ok_(dump['errors']['crash_id']) + assert dump['errors']['crash_id'] response = self.client.get(url, { 'crash_id': 'abc123' }) - eq_(response.status_code, 200) + assert response.status_code == 200 dump = json.loads(response.content) - ok_('id' in dump) - ok_('URL' not in dump) # right? - ok_('AsyncShutdownTimeout' in dump) - ok_('BIOS_Manufacturer' in dump) - ok_('upload_file_minidump_browser' in dump) - ok_('upload_file_minidump_flash1' in dump) - ok_('upload_file_minidump_flash2' in dump) - ok_('upload_file_minidump_plugin' in dump) + assert 'id' in dump + assert 'URL' not in dump # right? + assert 'AsyncShutdownTimeout' in dump + assert 'BIOS_Manufacturer' in dump + assert 'upload_file_minidump_browser' in dump + assert 'upload_file_minidump_flash1' in dump + assert 'upload_file_minidump_flash2' in dump + assert 'upload_file_minidump_plugin' in dump # `Comments` is scrubbed - ok_('I visited' in dump['Comments']) - ok_('http://p0rn.com' not in dump['Comments']) - ok_('mail@email.com' not in dump['Comments']) + assert 'I visited' in dump['Comments'] + assert 'http://p0rn.com' not in dump['Comments'] + assert 'mail@email.com' not in dump['Comments'] def test_RawCrash_binary_blob(self): @@ -391,15 +391,15 @@ def mocked_get(**params): 'format': 'raw' }) # because we don't have permission - eq_(response.status_code, 403) + assert response.status_code == 403 response = self.client.get(url, { 'crash_id': 'abc', 'format': 'wrong' # note }) # invalid format - eq_(response.status_code, 400) - eq_(response['Content-Type'], 'application/json; charset=UTF-8') + assert response.status_code == 400 + assert response['Content-Type'] == 'application/json; charset=UTF-8' user = self._login() self._add_permission(user, 'view_pii') @@ -408,7 +408,7 @@ def mocked_get(**params): 'format': 'raw' }) # still don't have the right permission - eq_(response.status_code, 403) + assert response.status_code == 403 self._add_permission(user, 'view_rawdump') response = self.client.get(url, { @@ -416,17 +416,17 @@ def mocked_get(**params): 'format': 'raw' }) # finally! - eq_(response.status_code, 200) - eq_(response['Content-Disposition'], 'attachment; filename="abc.dmp"') - eq_(response['Content-Type'], 'application/octet-stream') + assert response.status_code == 200 + assert response['Content-Disposition'] == 'attachment; filename="abc.dmp"' + assert response['Content-Type'] == 'application/octet-stream' def test_Bugs(self): url = reverse('api:model_wrapper', args=('Bugs',)) response = self.client.get(url) - eq_(response.status_code, 400) - eq_(response['Content-Type'], 'application/json; charset=UTF-8') + assert response.status_code == 400 + assert response['Content-Type'] == 'application/json; charset=UTF-8' dump = json.loads(response.content) - ok_(dump['errors']['signatures']) + assert dump['errors']['signatures'] def mocked_get_bugs(**options): return { @@ -442,9 +442,9 @@ def mocked_get_bugs(**options): response = self.client.get(url, { 'signatures': 'one & two', }) - eq_(response.status_code, 200) + assert response.status_code == 200 dump = json.loads(response.content) - ok_(dump['hits']) + assert dump['hits'] def test_SignaturesForBugs(self): @@ -458,25 +458,25 @@ def mocked_get_bugs(**options): url = reverse('api:model_wrapper', args=('SignaturesByBugs',)) response = self.client.get(url) - eq_(response.status_code, 400) - eq_(response['Content-Type'], 'application/json; charset=UTF-8') + assert response.status_code == 400 + assert response['Content-Type'] == 'application/json; charset=UTF-8' dump = json.loads(response.content) - ok_(dump['errors']['bug_ids']) + assert dump['errors']['bug_ids'] response = self.client.get(url, { 'bug_ids': '123456789', }) - eq_(response.status_code, 200) + assert response.status_code == 200 dump = json.loads(response.content) - ok_(dump['hits']) + assert dump['hits'] def test_NewSignatures(self): def mocked_supersearch_get(**params): - eq_(params['product'], [settings.DEFAULT_PRODUCT]) + assert params['product'] == [settings.DEFAULT_PRODUCT] if 'version' in params: - eq_(params['version'], ['1.0', '2.0']) + assert params['version'] == ['1.0', '2.0'] if 'signature' not in params: # Return a list of signatures. @@ -506,20 +506,20 @@ def mocked_supersearch_get(**params): # Test we get expected results. response = self.client.get(url) - eq_(response.status_code, 200) + assert response.status_code == 200 res_expected = [ 'ba', 'zin', ] res = json.loads(response.content) - eq_(res['hits'], res_expected) + assert res['hits'] == res_expected # Test with versions. response = self.client.get(url, { 'version': ['1.0', '2.0'] }) - eq_(response.status_code, 200) + assert response.status_code == 200 # Test with incorrect arguments. response = self.client.get(url, { @@ -527,11 +527,11 @@ def mocked_supersearch_get(**params): 'end_date': 'not a date', 'not_after': 'not a date', }) - eq_(response.status_code, 400) - eq_(response['Content-Type'], 'application/json; charset=UTF-8') + assert response.status_code == 400 + assert response['Content-Type'] == 'application/json; charset=UTF-8' res = json.loads(response.content) - ok_('errors' in res) - eq_(len(res['errors']), 3) + assert 'errors' in res + assert len(res['errors']) == 3 def test_Status(self): @@ -545,10 +545,10 @@ def mocked_get(**options): url = reverse('api:model_wrapper', args=('Status',)) response = self.client.get(url) - eq_(response.status_code, 200) + assert response.status_code == 200 dump = json.loads(response.content) - ok_(dump['socorro_revision']) - ok_(dump['breakpad_revision']) + assert dump['socorro_revision'] + assert dump['breakpad_revision'] def test_CrontabberState(self): # The actual dates dont matter, but it matters that it's a @@ -583,14 +583,14 @@ def mocked_get(**options): url = reverse('api:model_wrapper', args=('CrontabberState',)) response = self.client.get(url) - eq_(response.status_code, 200) + assert response.status_code == 200 dump = json.loads(response.content) - ok_(dump['state']) + assert dump['state'] def test_Field(self): url = reverse('api:model_wrapper', args=('Field',)) response = self.client.get(url) - eq_(response.status_code, 404) + assert response.status_code == 404 def test_hit_or_not_hit_ratelimit(self): @@ -625,7 +625,7 @@ def mocked_get(**options): url = reverse('api:model_wrapper', args=('CrontabberState',)) response = self.client.get(url) - eq_(response.status_code, 200) + assert response.status_code == 200 with self.settings( API_RATE_LIMIT='3/m', API_RATE_LIMIT_AUTHENTICATED='6/m' @@ -635,12 +635,12 @@ def mocked_get(**options): # https://bugzilla.mozilla.org/show_bug.cgi?id=1148470 for __ in range(current_limit * 2): response = self.client.get(url) - eq_(response.status_code, 429) + assert response.status_code == 429 # But it'll work if you use a different X-Forwarded-For IP # because the rate limit is based on your IP address response = self.client.get(url, HTTP_X_FORWARDED_FOR='11.11.11.11') - eq_(response.status_code, 200) + assert response.status_code == 200 user = User.objects.create(username='test') token = Token.objects.create( @@ -649,11 +649,11 @@ def mocked_get(**options): ) response = self.client.get(url, HTTP_AUTH_TOKEN=token.key) - eq_(response.status_code, 200) + assert response.status_code == 200 for __ in range(current_limit): response = self.client.get(url) - eq_(response.status_code, 200) + assert response.status_code == 200 # But even being signed in has a limit. authenticated_limit = 6 # see above mentioned settings override @@ -662,12 +662,12 @@ def mocked_get(**options): response = self.client.get(url) # Even if you're authenticated - sure the limit is higher - # eventually you'll run into the limit there too. - eq_(response.status_code, 429) + assert response.status_code == 429 def test_SuperSearch(self): def mocked_supersearch_get(**params): - ok_('exploitability' not in params) + assert 'exploitability' not in params restricted_params = ( '_facets', @@ -676,12 +676,12 @@ def mocked_supersearch_get(**params): ) for key in restricted_params: if key in params: - ok_('url' not in params[key]) - ok_('email' not in params[key]) - ok_('_cardinality.email' not in params[key]) + assert 'url' not in params[key] + assert 'email' not in params[key] + assert '_cardinality.email' not in params[key] if 'product' in params: - eq_(params['product'], ['WaterWolf', 'NightTrain']) + assert params['product'] == ['WaterWolf', 'NightTrain'] return { 'hits': [ @@ -705,19 +705,19 @@ def mocked_supersearch_get(**params): url = reverse('api:model_wrapper', args=('SuperSearch',)) response = self.client.get(url) - eq_(response.status_code, 200) + assert response.status_code == 200 res = json.loads(response.content) - ok_(res['hits']) - ok_(res['facets']) + assert res['hits'] + assert res['facets'] # Verify forbidden fields are not exposed. - ok_('email' not in res['hits']) - ok_('exploitability' not in res['hits']) - ok_('url' not in res['hits']) + assert 'email' not in res['hits'] + assert 'exploitability' not in res['hits'] + assert 'url' not in res['hits'] # Verify user comments are scrubbed. - ok_('thebig@lebowski.net' not in res['hits'][0]['user_comments']) + assert 'thebig@lebowski.net' not in res['hits'][0]['user_comments'] # Verify it's not possible to use restricted parameters. response = self.client.get(url, { @@ -730,20 +730,20 @@ def mocked_supersearch_get(**params): 'url', 'email', 'product', '_cardinality.email' ], }) - eq_(response.status_code, 200) + assert response.status_code == 200 # Verify values can be lists. response = self.client.get(url, { 'product': ['WaterWolf', 'NightTrain'] }) - eq_(response.status_code, 200) + assert response.status_code == 200 def test_SuperSearchUnredacted(self): def mocked_supersearch_get(**params): - ok_('exploitability' in params) + assert 'exploitability' in params if 'product' in params: - eq_(params['product'], ['WaterWolf', 'NightTrain']) + assert params['product'] == ['WaterWolf', 'NightTrain'] return { 'hits': [ { @@ -768,13 +768,13 @@ def mocked_supersearch_get(**params): url = reverse('api:model_wrapper', args=('SuperSearchUnredacted',)) response = self.client.get(url, {'exploitability': 'high'}) - eq_(response.status_code, 403) - eq_(response['Content-Type'], 'application/json') + assert response.status_code == 403 + assert response['Content-Type'] == 'application/json' error = json.loads(response.content)['error'] permission = Permission.objects.get( codename='view_exploitability' ) - ok_(permission.name in error) + assert permission.name in error # Log in to get permissions. user = self._login() @@ -782,26 +782,26 @@ def mocked_supersearch_get(**params): self._add_permission(user, 'view_exploitability') response = self.client.get(url, {'exploitability': 'high'}) - eq_(response.status_code, 200) + assert response.status_code == 200 res = json.loads(response.content) - ok_(res['hits']) - ok_(res['facets']) + assert res['hits'] + assert res['facets'] # Verify forbidden fields are exposed. - ok_('email' in res['hits'][0]) - ok_('exploitability' in res['hits'][0]) - ok_('url' in res['hits'][0]) + assert 'email' in res['hits'][0] + assert 'exploitability' in res['hits'][0] + assert 'url' in res['hits'][0] # Verify user comments are not scrubbed. - ok_('thebig@lebowski.net' in res['hits'][0]['user_comments']) + assert 'thebig@lebowski.net' in res['hits'][0]['user_comments'] # Verify values can be lists. response = self.client.get(url, { 'exploitability': 'high', 'product': ['WaterWolf', 'NightTrain'] }) - eq_(response.status_code, 200) + assert response.status_code == 200 def test_change_certain_exceptions_to_bad_request(self): @@ -819,11 +819,11 @@ def mocked_supersearch_get(**params): url = reverse('api:model_wrapper', args=('SuperSearch',)) response = self.client.get(url) - eq_(response.status_code, 400) - ok_('That was a bad thing to do' in response.content) + assert response.status_code == 400 + assert 'That was a bad thing to do' in response.content response = self.client.get(url, {'product': 'foobaz'}) - eq_(response.status_code, 400) - ok_('foobaz' in response.content) + assert response.status_code == 400 + assert 'foobaz' in response.content def test_Reprocessing(self): @@ -835,13 +835,13 @@ def mocked_reprocess(crash_ids): url = reverse('api:model_wrapper', args=('Reprocessing',)) response = self.client.get(url) - eq_(response.status_code, 403) + assert response.status_code == 403 params = { 'crash_ids': 'xxxx', } response = self.client.get(url, params, HTTP_AUTH_TOKEN='somecrap') - eq_(response.status_code, 403) + assert response.status_code == 403 user = User.objects.create(username='test') self._add_permission(user, 'reprocess_crashes') @@ -858,8 +858,8 @@ def mocked_reprocess(crash_ids): token.permissions.add(perm) response = self.client.get(url, params, HTTP_AUTH_TOKEN=token.key) - eq_(response.status_code, 405) + assert response.status_code == 405 response = self.client.post(url, params, HTTP_AUTH_TOKEN=token.key) - eq_(response.status_code, 200) - eq_(json.loads(response.content), True) + assert response.status_code == 200 + assert json.loads(response.content) is True