Skip to content

Commit

Permalink
Issue #712. Add api_mock decorator factory for mocking w/ fixtures.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Taylor committed Oct 8, 2015
1 parent 7438884 commit 7896e67
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions webcompat/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import urlparse

from babel.dates import format_timedelta
from flask import g
from flask import session
from ua_parser import user_agent_parser

Expand Down Expand Up @@ -308,3 +309,27 @@ def get_comment_data(request_data):
of a request's data object.'''
comment_data = json.loads(request_data)
return json.dumps({"body": comment_data['rawBody']})


def api_mock(auth_response, non_auth_response):
'''Decorator factory for mocking out API reponses
This allows us to send back fixture files when in TESTING mode, rather
than making API requests over the network. See /api/endponts.py
for usage.'''
# These file paths are relative to helpers.py
FIXTURES_PATH = os.path.join(os.path.dirname(__file__), os.pardir,
'tests/fixtures')

def wrap(func):
def wrapped_func(*args, **kwargs):
if app.config['TESTING']:
if g.user and auth_response is not None:
file = open(os.path.join(FIXTURES_PATH, auth_response))
return file.read()
elif non_auth_response is not None:
file = open(os.path.join(FIXTURES_PATH, non_auth_response))
return file.read()
return func(*args, **kwargs)
return wrapped_func
return wrap

0 comments on commit 7896e67

Please sign in to comment.