|
|
@ -8,18 +8,15 @@ from luncho import server |
|
|
|
|
|
|
|
|
|
|
|
from luncho.server import User |
|
|
|
from luncho.server import User |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from base import LunchoTests |
|
|
|
|
|
|
|
|
|
|
|
class TestToken(unittest.TestCase): |
|
|
|
|
|
|
|
|
|
|
|
class TestToken(LunchoTests): |
|
|
|
"""Test token requests.""" |
|
|
|
"""Test token requests.""" |
|
|
|
|
|
|
|
|
|
|
|
def setUp(self): |
|
|
|
def setUp(self): |
|
|
|
# leave the database blank to make it in memory |
|
|
|
# leave the database blank to make it in memory |
|
|
|
server.app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://' |
|
|
|
super(TestToken, self).setUp() |
|
|
|
server.app.config['TESTING'] = True |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.app = server.app.test_client() |
|
|
|
|
|
|
|
server.db.create_all() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# add a user |
|
|
|
# add a user |
|
|
|
self.test_user = User(username='test', |
|
|
|
self.test_user = User(username='test', |
|
|
|
fullname='Testing user', |
|
|
|
fullname='Testing user', |
|
|
@ -28,7 +25,7 @@ class TestToken(unittest.TestCase): |
|
|
|
server.db.session.commit() |
|
|
|
server.db.session.commit() |
|
|
|
|
|
|
|
|
|
|
|
def tearDown(self): |
|
|
|
def tearDown(self): |
|
|
|
server.db.drop_all(bind=None) |
|
|
|
super(TestToken, self).tearDown() |
|
|
|
|
|
|
|
|
|
|
|
def test_create_token(self): |
|
|
|
def test_create_token(self): |
|
|
|
"""Test requesting a token""" |
|
|
|
"""Test requesting a token""" |
|
|
@ -38,10 +35,8 @@ class TestToken(unittest.TestCase): |
|
|
|
data=json.dumps(request), |
|
|
|
data=json.dumps(request), |
|
|
|
content_type='application/json') |
|
|
|
content_type='application/json') |
|
|
|
|
|
|
|
|
|
|
|
self.assertEqual(rv.status_code, 200) |
|
|
|
self.assertJsonOk(rv) |
|
|
|
response = json.loads(rv.data) |
|
|
|
response = json.loads(rv.data) |
|
|
|
self.assertTrue('status' in response) |
|
|
|
|
|
|
|
self.assertEqual(response['status'], 'OK') |
|
|
|
|
|
|
|
self.assertTrue('token' in response) |
|
|
|
self.assertTrue('token' in response) |
|
|
|
# we can't check the token itself 'cause it should change every day |
|
|
|
# we can't check the token itself 'cause it should change every day |
|
|
|
|
|
|
|
|
|
|
@ -52,16 +47,14 @@ class TestToken(unittest.TestCase): |
|
|
|
rv = self.app.post('/token/', |
|
|
|
rv = self.app.post('/token/', |
|
|
|
data=json.dumps(request), |
|
|
|
data=json.dumps(request), |
|
|
|
content_type='application/json') |
|
|
|
content_type='application/json') |
|
|
|
|
|
|
|
self.assertJsonOk(rv) |
|
|
|
self.assertEqual(rv.status_code, 200) |
|
|
|
|
|
|
|
response = json.loads(rv.data) |
|
|
|
response = json.loads(rv.data) |
|
|
|
|
|
|
|
|
|
|
|
# re-request the token |
|
|
|
# re-request the token |
|
|
|
rv = self.app.post('/token/', |
|
|
|
rv = self.app.post('/token/', |
|
|
|
data=json.dumps(request), |
|
|
|
data=json.dumps(request), |
|
|
|
content_type='application/json') |
|
|
|
content_type='application/json') |
|
|
|
|
|
|
|
self.assertJsonOk(rv) |
|
|
|
self.assertTrue(rv.status_code, 200) |
|
|
|
|
|
|
|
self.assertEqual(response['token'], json.loads(rv.data)['token']) |
|
|
|
self.assertEqual(response['token'], json.loads(rv.data)['token']) |
|
|
|
|
|
|
|
|
|
|
|
def test_no_such_user(self): |
|
|
|
def test_no_such_user(self): |
|
|
@ -72,5 +65,16 @@ class TestToken(unittest.TestCase): |
|
|
|
rv = self.app.post('/token/', |
|
|
|
rv = self.app.post('/token/', |
|
|
|
data=json.dumps(request), |
|
|
|
data=json.dumps(request), |
|
|
|
content_type='application/json') |
|
|
|
content_type='application/json') |
|
|
|
|
|
|
|
self.assertJsonError(rv, 404, 'User does not exist') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_wrong_password(self): |
|
|
|
|
|
|
|
"""Try to log with the wrong password.""" |
|
|
|
|
|
|
|
request = {'username': 'test', |
|
|
|
|
|
|
|
'password': 'nothing'} |
|
|
|
|
|
|
|
rv = self.app.post('/token/', |
|
|
|
|
|
|
|
data=json.dumps(request), |
|
|
|
|
|
|
|
content_type='application/json') |
|
|
|
|
|
|
|
self.assertJsonError(rv, 401, 'Invalid password') |
|
|
|
|
|
|
|
|
|
|
|
self.assertEqual(rv.status_code, 404) |
|
|
|
if __name__ == '__main__': |
|
|
|
|
|
|
|
unittest.main() |
|
|
|