Julio Biason
11 years ago
5 changed files with 162 additions and 0 deletions
@ -0,0 +1,33 @@
|
||||
#!/usr/bin/env python |
||||
# -*- encoding: utf-8 -*- |
||||
|
||||
"""User management.""" |
||||
|
||||
from flask import Blueprint |
||||
from flask import jsonify |
||||
from flask import request |
||||
|
||||
from luncho.helpers import ForceJSON |
||||
from luncho.helpers import JSONError |
||||
|
||||
from luncho.server import User |
||||
from luncho.server import db |
||||
|
||||
token = Blueprint('token', __name__) |
||||
|
||||
@token.route('', methods=['POST']) |
||||
@ForceJSON(required=['username', 'password']) |
||||
def get_token(): |
||||
"""Return an access token to the user. Request must be: |
||||
{ "username": "username", "password": "hash" }""" |
||||
json = request.get_json(force=True) |
||||
|
||||
user = User.query.filter_by(username=json['username']).first() |
||||
if user is None: |
||||
return JSONError(404, 'User does not exist') |
||||
|
||||
if not user.passhash == json['password']: |
||||
return JSONError(401, 'Invalid password') |
||||
|
||||
return jsonify(status='OK', |
||||
token=user.get_token()) |
@ -0,0 +1,76 @@
|
||||
#!/usr/bin/env python |
||||
# -*- encoding: utf-8 -*- |
||||
|
||||
import unittest |
||||
import json |
||||
|
||||
from luncho import server |
||||
|
||||
from luncho.server import User |
||||
|
||||
|
||||
class TestToken(unittest.TestCase): |
||||
"""Test token requests.""" |
||||
|
||||
def setUp(self): |
||||
# leave the database blank to make it in memory |
||||
server.app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://' |
||||
server.app.config['TESTING'] = True |
||||
|
||||
self.app = server.app.test_client() |
||||
server.db.create_all() |
||||
|
||||
# add a user |
||||
self.test_user = User(username='test', |
||||
fullname='Testing user', |
||||
passhash='hash') |
||||
server.db.session.add(self.test_user) |
||||
server.db.session.commit() |
||||
|
||||
def tearDown(self): |
||||
server.db.drop_all(bind=None) |
||||
|
||||
def test_create_token(self): |
||||
"""Test requesting a token""" |
||||
request = {'username': 'test', |
||||
'password': 'hash'} |
||||
rv = self.app.post('/token/', |
||||
data=json.dumps(request), |
||||
content_type='application/json') |
||||
|
||||
self.assertEqual(rv.status_code, 200) |
||||
response = json.loads(rv.data) |
||||
self.assertTrue('status' in response) |
||||
self.assertEqual(response['status'], 'OK') |
||||
self.assertTrue('token' in response) |
||||
# we can't check the token itself 'cause it should change every day |
||||
|
||||
def test_reget_token(self): |
||||
"""Check if getting the token twice will produce the same token.""" |
||||
request = {'username': 'test', |
||||
'password': 'hash'} |
||||
rv = self.app.post('/token/', |
||||
data=json.dumps(request), |
||||
content_type='application/json') |
||||
|
||||
self.assertEqual(rv.status_code, 200) |
||||
response = json.loads(rv.data) |
||||
|
||||
# re-request the token |
||||
rv = self.app.post('/token/', |
||||
data=json.dumps(request), |
||||
content_type='application/json') |
||||
|
||||
self.assertTrue(rv.status_code, 200) |
||||
self.assertEqual(response['token'], json.loads(rv.data)['token']) |
||||
|
||||
def test_no_such_user(self): |
||||
"""Check the result of getting a token for a user that doesn't |
||||
exist.""" |
||||
request = {'username': 'username', |
||||
'password': 'hash'} |
||||
rv = self.app.post('/token/', |
||||
data=json.dumps(request), |
||||
content_type='application/json') |
||||
|
||||
self.assertEqual(rv.status_code, 404) |
Loading…
Reference in new issue