Skip to content

Commit

Permalink
Fixes #1618 - Get all unittests to be network independent.
Browse files Browse the repository at this point in the history
  • Loading branch information
brizental committed Aug 2, 2017
1 parent b5c0547 commit daf6b38
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 35 deletions.
95 changes: 69 additions & 26 deletions tests/test_api_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,22 @@
import sys
import unittest

from mock import patch, MagicMock
from requests import Response
from werkzeug import wsgi

# Add webcompat module to import path
sys.path.append(os.path.realpath(os.pardir))
import webcompat

FIXTURES_PATH = os.getcwd() + '/tests/fixtures'

# Any request that depends on parsing HTTP Headers (basically anything
# on the index route, will need to include the following: environ_base=headers
headers = {'HTTP_USER_AGENT': ('Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; '
'rv:31.0) Gecko/20100101 Firefox/31.0'),
'HTTP_ACCEPT': 'application/json'}
'HTTP_ACCEPT': 'application/json',
'_content': '{}'}


class TestAPIURLs(unittest.TestCase):
Expand All @@ -42,11 +48,17 @@ def test_api_issues_search(self):
def test_api_issues_out_of_range(self):
'''API issue for a non existent number returns JSON 404.'''
# If we reach 1,000,000 webcompat issues we can celebrate
rv = self.app.get('/api/issues/1000000', environ_base=headers)
json_body = json.loads(rv.data)
self.assertEqual(rv.status_code, 404)
self.assertEqual(rv.content_type, 'application/json')
self.assertEqual(json_body['status'], 404)
with patch('requests.get') as github_data:
api_response = MagicMock(spec=Response)
api_response.status_code = 404
api_response.content_type = 'application/json'
api_response.data = '{"message":"Not Found. Lost in Punk Cat Space","status":404}' # nopep8
github_data.return_value = api_response
rv = self.app.get('/api/issues/1000000', environ_base=headers)
json_body = json.loads(rv.data)
self.assertEqual(rv.status_code, 404)
self.assertEqual(rv.content_type, 'application/json')
self.assertEqual(json_body['status'], 404)

def test_api_wrong_route(self):
'''API with wrong route returns JSON 404.'''
Expand All @@ -66,31 +78,62 @@ def test_api_wrong_category(self):

def test_api_labels_without_auth(self):
'''API access to labels without auth returns JSON 200.'''
rv = self.app.get('/api/issues/labels', environ_base=headers)
json_body = json.loads(rv.data)
self.assertEqual(rv.status_code, 200)
self.assertEqual(rv.content_type, 'application/json')
with patch('requests.get') as github_data:
api_response = MagicMock(spec=Response)
api_response.status_code = 200
api_response.content_type = 'application/json'
api_response.content = '{}'
api_response.headers = {
'etag': 'xxx',
'cache-control': 'private, max-age=60, s-maxage=60',
'content-type': 'appication/json',
'link': '<https://api.github.com/repositories/17839063/labels?page=2>; rel="next", <https://api.github.com/repositories/17839063/labels?page=2>; rel="last"'} # nopep8
github_data.return_value = api_response
rv = self.app.get('/api/issues/labels', environ_base=headers)
self.assertEqual(rv.status_code, 200)
self.assertEqual(rv.content_type, 'application/json')

def test_api_comments_link_header_auth(self):
'''API access to comments greater than 30 returns pagination in Link
header of the response.'''
query_string = {'callback': 'foo'}
# Force a JSONP callback response with `query_string` because it gives
# us the header properties we want to test.
rv = self.app.get('/api/issues/398/comments',
query_string=query_string, environ_base=headers)
self.assertTrue = all(x in rv.data for x in ['Link', 'rel', 'next',
'last', 'page'])
self.assertEqual(rv.status_code, 200)
self.assertEqual(rv.content_type, 'application/json')
# API access to comments for an issue with < 30 does not return link a
# header in the response (until GitHub changes it....?)
rv = self.app.get('/api/issues/4/comments', query_string=query_string,
environ_base=headers)
self.assertTrue = not all(x in rv.data for x in ['Link', 'rel', 'next',
with patch('requests.get') as github_data:
# Create mock data. One api_response for each call.
api_response_398 = MagicMock(spec=Response)
api_response_398.status_code = 200
api_response_398.content_type = 'application/json'
api_response_398.content = '{"Link":[["https://api.github.com/repositories/17839063/issues/398/comments?callback=foo&page=2",{"rel":"next"}],["https://api.github.com/repositories/17839063/issues/398/comments?callback=foo&page=4",{"rel":"last"}]]}' # nopep8
api_response_398.headers = {
'etag': 'xxx',
'cache-control': 'private, max-age=60, s-maxage=60',
'content-type': 'appication/json'}
api_response_4 = MagicMock(spec=Response)
api_response_4.status_code = 200
api_response_4.content_type = 'application/json'
api_response_4.content = '{}'
api_response_4.headers = {
'etag': 'xxx',
'cache-control': 'private, max-age=60, s-maxage=60',
'content-type': 'appication/json'}
github_data.side_effect = [api_response_398, api_response_4]
# Force a JSONP callback response with `query_string`
# because it gives us the header properties we want to test.
query_string = {'callback': 'foo'}
rv = self.app.get('/api/issues/398/comments',
query_string=query_string, environ_base=headers)
self.assertTrue = all(x in rv.data for x in ['Link', 'rel', 'next',
'last', 'page'])
self.assertEqual(rv.status_code, 200)
self.assertEqual(rv.content_type, 'application/json')
self.assertEqual(rv.status_code, 200)
self.assertEqual(rv.content_type, 'application/json')
# API access to comments for an issue
# with < 30 does not return link a header in
# the response (until GitHub changes it....?)
rv = self.app.get('/api/issues/4/comments',
query_string=query_string, environ_base=headers)
self.assertTrue = not all(
x in rv.data for x in [
'Link', 'rel', 'next', 'last', 'page'])
self.assertEqual(rv.status_code, 200)
self.assertEqual(rv.content_type, 'application/json')

def test_api_set_labels_without_auth(self):
'''API setting labels without auth returns JSON 403 error code.'''
Expand Down
22 changes: 13 additions & 9 deletions tests/test_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import sys
import unittest

from mock import patch

# Add webcompat module to import path
sys.path.append(os.path.realpath(os.pardir))
import webcompat # nopep8
Expand Down Expand Up @@ -119,15 +121,17 @@ def test_missing_parameters_for_new_issue(self):

def test_new_issue_should_not_crash(self):
'''Checks 500 is not accepted for /issues/new POST.'''
data = {'problem_category': u'mobile_site_bug',
'username': u'',
'description': u'foo',
'submit-type': u'github-proxy-report',
'url': u'http://example.com',
'os': u'Foobar',
'browser': u'BarFoo'}
rv = self.app.post('/issues/new', data=data)
self.assertNotEqual(rv.status_code, 500)
with patch('requests.post') as github_data:
github_data.status_code.return_value = 500
data = {'problem_category': u'mobile_site_bug',
'username': u'',
'description': u'foo',
'submit-type': u'github-proxy-report',
'url': u'http://example.com',
'os': u'Foobar',
'browser': u'BarFoo'}
rv = self.app.post('/issues/new', data=data)
self.assertNotEqual(rv.status_code, 500)


if __name__ == '__main__':
Expand Down

0 comments on commit daf6b38

Please sign in to comment.