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 7, 2015
1 parent 35b52f6 commit 1025bfc
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions webcompat/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,3 +308,23 @@ 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(mock_endpoint):
'''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.'''
def wrap(func):
def wrapped_func(*args, **kwargs):
if app.config['TESTING']:
# Note, this file path is relative to helpers.py
file = open(os.path.join(os.path.dirname(__file__),
os.pardir,
'tests/fixtures',
mock_endpoint))
return file.read()
return func(*args, **kwargs)
return wrapped_func
return wrap

0 comments on commit 1025bfc

Please sign in to comment.