Skip to content

Commit

Permalink
Fixes #1618 - Gets unittest mock data to mock *ONLY* GitHub API data.
Browse files Browse the repository at this point in the history
  • Loading branch information
brizental committed Aug 3, 2017
1 parent daf6b38 commit 3f04d62
Showing 1 changed file with 25 additions and 46 deletions.
71 changes: 25 additions & 46 deletions tests/test_api_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,32 @@
'''Tests for our API URL endpoints.'''

import json
import os.path
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',
'_content': '{}'}
'HTTP_ACCEPT': 'application/json'}


def mock_api_response(dict):
''' Helper method to create a mock response from the Github API.'''
api_response = MagicMock(spec=Response)
api_response.content_type = 'application/json'
api_response.headers = {
'etag': 'thisisatest',
'cache-control': 'thisisatest',
'content-type': 'application/json'
}
for k, v in dict.iteritems():
setattr(api_response, k, v)
return api_response


class TestAPIURLs(unittest.TestCase):
Expand All @@ -47,14 +53,9 @@ 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
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)
with patch('webcompat.helpers.proxy_request') as github_data:
github_data.return_value = mock_api_response({'status_code': 404, 'content': '{ "message": "Not Found", "documentation_url": "https://developer.github.com/v3" }'}) # nopep8
rv = self.app.get('/api/issues/1', environ_base=headers)
json_body = json.loads(rv.data)
self.assertEqual(rv.status_code, 404)
self.assertEqual(rv.content_type, 'application/json')
Expand All @@ -78,43 +79,21 @@ def test_api_wrong_category(self):

def test_api_labels_without_auth(self):
'''API access to labels without auth returns JSON 200.'''
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
with patch('webcompat.helpers.proxy_request') as github_data:
github_data.return_value = mock_api_response(
{'status_code': 200, 'content': '{}'})
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.'''
with patch('requests.get') as github_data:
with patch('webcompat.helpers.proxy_request') 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]
github_data.side_effect = [mock_api_response(
{'status_code': 200, '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
mock_api_response({'status_code': 200, 'content': '{}'})]
# Force a JSONP callback response with `query_string`
# because it gives us the header properties we want to test.
query_string = {'callback': 'foo'}
Expand Down

0 comments on commit 3f04d62

Please sign in to comment.