Julio Biason
11 years ago
5 changed files with 88 additions and 2 deletions
@ -0,0 +1,35 @@
|
||||
#!/usr/bin/env python |
||||
# -*- encoding: utf-8 -*- |
||||
|
||||
"""Helper functions.""" |
||||
|
||||
from functools import wraps |
||||
|
||||
from flask import request |
||||
from flask import jsonify |
||||
|
||||
class ForceJSON(object): |
||||
def __init__(self, required=None): |
||||
self.required = required or [] |
||||
|
||||
def __call__(self, func): |
||||
@wraps(func) |
||||
def check_json(*args, **kwargs): |
||||
json = request.get_json(force=True, silent=True) |
||||
if not json: |
||||
return jsonify(status='ERROR', |
||||
error='Request MUST be in JSON format'), 400 |
||||
|
||||
# now we have the JSON, let's check if all the fields are here. |
||||
missing = [] |
||||
for field in required or []: |
||||
if not field in json: |
||||
missing.append(field) |
||||
|
||||
if missing: |
||||
return jsonify(status='ERROR', |
||||
error='Missing fields: {fields}'.format( |
||||
fields=', '.join(missing))) |
||||
|
||||
return func(*args, **kwargs) |
||||
return check_json |
@ -0,0 +1,32 @@
|
||||
#!/usr/bin/env python |
||||
# -*- encoding: utf-8 -*- |
||||
|
||||
import os |
||||
import tempfile |
||||
import unittest |
||||
import json |
||||
|
||||
from luncho import server |
||||
|
||||
|
||||
class TestUsers(unittest.TestCase): |
||||
"""Test users request.""" |
||||
|
||||
def setUp(self): |
||||
(_, server.app.config['SQLITE_FILENAME']) = tempfile.mkstemp() |
||||
self.app = server.app.test_client() |
||||
|
||||
def tearDown(self): |
||||
os.unlick(server.app.config['SQLITE_FILENAME']) |
||||
|
||||
def test_create_user(self): |
||||
request = {'username': 'username', |
||||
'full_name': 'full name', |
||||
'password': 'hash'} |
||||
rv = self.app.put('/users/', |
||||
data=json.dumps(request), |
||||
content_type='application/json') |
||||
print rv.data |
||||
|
||||
if __name__ == '__main__': |
||||
unittest.main() |
Loading…
Reference in new issue