From 6a2cb087fcbe69ddc09a743700ea3ffb80e11dd6 Mon Sep 17 00:00:00 2001 From: Eli Ribble Date: Wed, 4 May 2016 07:16:05 -0600 Subject: [PATCH] Add GET /user/ implementation that works Previously my GET was just a placeholder so I could build URIs against it. Now it actually works and returns information on a user --- tests/api/test_user.py | 12 ++++++++++++ vanth/api/user.py | 10 +++++++--- vanth/platform/user.py | 13 +++++++++++++ 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/tests/api/test_user.py b/tests/api/test_user.py index 2c4706f..1e3d2ed 100644 --- a/tests/api/test_user.py +++ b/tests/api/test_user.py @@ -2,6 +2,8 @@ import json import pytest +import vanth.platform.user + @pytest.mark.usefixtures('db') def test_post(client): @@ -13,3 +15,13 @@ def test_post(client): response = client.post('/user/', data=json.dumps(data)) assert response.status_code == 204 assert response.headers['Location'] + +@pytest.mark.usefixtures('db') +def test_get(client): + location = vanth.platform.user.create('Blue Stahli', 'blue@stahli.com', 'metamorphosis') + response = client.get(location) + assert response.status_code == 200 + assert response.json == { + 'name' : 'Blue Stahli', + 'username' : 'blue@stahli.com', + } diff --git a/vanth/api/user.py b/vanth/api/user.py index 9a6064e..095840a 100644 --- a/vanth/api/user.py +++ b/vanth/api/user.py @@ -1,4 +1,5 @@ import sepiida.endpoints +import sepiida.errors import sepiida.fields import vanth.platform.user @@ -8,7 +9,7 @@ class User(sepiida.endpoints.APIEndpoint): ENDPOINT = '/user/' SIGNATURE = sepiida.fields.JSONObject(s={ 'name' : sepiida.fields.String(), - 'password' : sepiida.fields.String(), + 'password' : sepiida.fields.String(methods=['POST', 'PUT']), 'username' : sepiida.fields.String(), }) @@ -19,5 +20,8 @@ class User(sepiida.endpoints.APIEndpoint): return None, 204, {'Location': uri} @staticmethod - def get(uuid): # pylint: disable=unused-argument - return {} + def get(uuid): + users = vanth.platform.user.by_filter({'uuid': [str(uuid)]}) + if not users: + raise sepiida.errors.ResourceNotFound() + return users[0] diff --git a/vanth/platform/user.py b/vanth/platform/user.py index 55dbcff..3ea3e77 100644 --- a/vanth/platform/user.py +++ b/vanth/platform/user.py @@ -1,12 +1,25 @@ import uuid import chryso.connection +import chryso.queryadapter import passlib.apps import sepiida.routing import vanth.tables +def by_filter(filters): + engine = chryso.connection.get() + + query = vanth.tables.User.select() + query = chryso.queryadapter.map_and_filter(vanth.tables.User, filters, query) + results = engine.execute(query).fetchall() + return [{ + 'username' : result[vanth.tables.User.c.username], + 'password' : result[vanth.tables.User.c.password], + 'name' : result[vanth.tables.User.c.name], + } for result in results] + def create(name, username, password): engine = chryso.connection.get()