|
|
|
@ -15,21 +15,64 @@ class TestUsers(unittest.TestCase):
|
|
|
|
|
server.app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://' |
|
|
|
|
server.app.config['TESTING'] = True |
|
|
|
|
|
|
|
|
|
print server.app.config['SQLALCHEMY_DATABASE_URI'] |
|
|
|
|
self.app = server.app.test_client() |
|
|
|
|
server.db.create_all() |
|
|
|
|
|
|
|
|
|
# def tearDown(self): |
|
|
|
|
# os.unlink(server.app.config['SQLITE_FILENAME']) |
|
|
|
|
def tearDown(self): |
|
|
|
|
server.db.drop_all(bind=None) |
|
|
|
|
|
|
|
|
|
def test_create_user(self): |
|
|
|
|
"""Simple user creation.""" |
|
|
|
|
request = {'username': 'username', |
|
|
|
|
'full_name': 'full name', |
|
|
|
|
'password': 'hash'} |
|
|
|
|
rv = self.app.put('/user/', |
|
|
|
|
data=json.dumps(request), |
|
|
|
|
content_type='application/json') |
|
|
|
|
|
|
|
|
|
self.assertEqual(rv.status_code, 200) |
|
|
|
|
self.assertEqual(json.loads(rv.data), {'status': 'OK'}) |
|
|
|
|
|
|
|
|
|
def test_duplicate_user(self): |
|
|
|
|
"""Check the status for trying to create a user that it is already |
|
|
|
|
in the database.""" |
|
|
|
|
self.test_create_user() # create the first user |
|
|
|
|
|
|
|
|
|
# now duplicate |
|
|
|
|
request = {'username': 'username', |
|
|
|
|
'full_name': 'full name', |
|
|
|
|
'password': 'hash'} |
|
|
|
|
rv = self.app.put('/user/', |
|
|
|
|
data=json.dumps(request), |
|
|
|
|
content_type='application/json') |
|
|
|
|
|
|
|
|
|
expected = {"status": "ERROR", |
|
|
|
|
"error": "username already exists"} |
|
|
|
|
|
|
|
|
|
self.assertEqual(rv.status_code, 409) |
|
|
|
|
self.assertEqual(json.loads(rv.data), expected) |
|
|
|
|
|
|
|
|
|
def test_no_json(self): |
|
|
|
|
"""Check the status when doing a request that it's not JSON.""" |
|
|
|
|
rv = self.app.put('/user/', |
|
|
|
|
data='', |
|
|
|
|
content_type='text/html') |
|
|
|
|
|
|
|
|
|
expected = {"error": "Request MUST be in JSON format", |
|
|
|
|
"status": "ERROR"} |
|
|
|
|
self.assertEqual(rv.status_code, 400) |
|
|
|
|
self.assertEqual(json.loads(rv.data), expected) |
|
|
|
|
|
|
|
|
|
def test_missing_fields(self): |
|
|
|
|
request = {'password': 'hash'} |
|
|
|
|
rv = self.app.put('/user/', |
|
|
|
|
data=json.dumps(request), |
|
|
|
|
content_type='application/json') |
|
|
|
|
|
|
|
|
|
resp = {'error': 'Missing fields: username, full_name', |
|
|
|
|
'status': 'ERROR'} |
|
|
|
|
self.assertEqual(rv.status_code, 400) |
|
|
|
|
self.assertEqual(json.loads(rv.data), resp) |
|
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
|
|
unittest.main() |
|
|
|
|