From 83d560685a714be13016cce9dfa2867d7960685e Mon Sep 17 00:00:00 2001 From: androiddrew Date: Sun, 22 Oct 2017 10:44:28 -0400 Subject: [PATCH] added application factory and render tests --- cookie_api/__init__.py | 3 +++ cookie_api/app.py | 30 ++++++++++-------------------- cookie_api/auth.py | 5 +++-- tests/test_auth.py | 2 +- tests/test_renders.py | 23 +++++++++++++++++++++++ wsgi.py | 16 +++++++++++++++- 6 files changed, 55 insertions(+), 24 deletions(-) diff --git a/cookie_api/__init__.py b/cookie_api/__init__.py index e69de29..3565ea8 100644 --- a/cookie_api/__init__.py +++ b/cookie_api/__init__.py @@ -0,0 +1,3 @@ +from cookie_api.renders import JSONRenderer +from cookie_api.models.schema import Base +from cookie_api.app import application_factory \ No newline at end of file diff --git a/cookie_api/app.py b/cookie_api/app.py index 82dcd01..6d981cf 100644 --- a/cookie_api/app.py +++ b/cookie_api/app.py @@ -6,11 +6,9 @@ from apistar.handlers import docs_urls, static_urls from apistar.interfaces import Router, Injector, Auth from apistar_jwt.authentication import JWTAuthentication -from apistar_jwt.exceptions import AuthenticationFailed -from cookie_api.renders import JSONRenderer from cookie_api.commands import commands -from cookie_api.models.schema import Base, Cookie +from cookie_api.models.schema import Cookie from cookie_api.auth import auth_routes, auth_components @@ -69,24 +67,16 @@ routes = [ Include('/static', static_urls) ] -settings = { - 'DATABASE': { - 'URL': 'postgresql://apistar:local@localhost/apistar', - 'METADATA': Base.metadata - }, - 'RENDERERS': [JSONRenderer()], - 'JWT': { - 'SECRET': 'thisisasecret' - } -} +routes = routes + auth_routes commands = sqlalchemy_backend.commands + commands -app = App(routes=routes + auth_routes, - settings=settings, - commands=commands, - components=sqlalchemy_backend.components + auth_components - ) +components = sqlalchemy_backend.components + auth_components -if __name__ == "__main__": - app.main() + +def application_factory(**settings): + """Returns an instance of Cookie API""" + return App(settings=settings, + commands=commands, + components=components, + routes=routes) diff --git a/cookie_api/auth.py b/cookie_api/auth.py index 7ed98b8..2e7e5c5 100644 --- a/cookie_api/auth.py +++ b/cookie_api/auth.py @@ -7,15 +7,16 @@ from apistar_jwt.token import JWT from .models.schema import User # /auth/register -# /auth/login -# /auth/logout # /auth/user +# /auth/logout + auth_components = [ Component(JWT, init=get_jwt) ] +# /auth/login def login(settings: Settings, json_data: http.RequestData, session: Session): user_id = json_data.get('email') password = json_data.get('password') diff --git a/tests/test_auth.py b/tests/test_auth.py index 7574faf..8ba03e8 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -33,7 +33,7 @@ def test_non_registered_user_login(): # Test Login -def test_registered_user_login(self): +def test_registered_user_login(): """ Test for login of registered-user login """ pass diff --git a/tests/test_renders.py b/tests/test_renders.py index e69de29..f5a2fb3 100644 --- a/tests/test_renders.py +++ b/tests/test_renders.py @@ -0,0 +1,23 @@ +import datetime as dt +from decimal import Decimal +import json + +from cookie_api.renders import extended_encoder, JSONRenderer + + +def test_extended_encoder_date_parsing(): + test_date = dt.datetime(2017, 5, 10) + assert test_date.isoformat() == extended_encoder(test_date) + + +def test_extended_encoder_decimal_casting(): + test_decimal = Decimal('1.0') + assert 1.0 == extended_encoder(test_decimal) + + +def test_render_with_extended_encoder(): + test_date = dt.datetime(2017, 5, 10) + test_decimal = Decimal('0.1') + expected = dict(my_date="2017-05-10T00:00:00", my_float=0.1) + test_response = dict(my_date=test_date, my_float=test_decimal) + assert json.dumps(expected).encode('utf-8') == JSONRenderer().render(test_response) \ No newline at end of file diff --git a/wsgi.py b/wsgi.py index 52693bf..9de23e7 100644 --- a/wsgi.py +++ b/wsgi.py @@ -1,3 +1,17 @@ +from cookie_api import JSONRenderer, Base, application_factory + + +settings = { + 'DATABASE': { + 'URL': 'postgresql://apistar:local@localhost/apistar', + 'METADATA': Base.metadata + }, + 'RENDERERS': [JSONRenderer()], + 'JWT': { + 'SECRET': 'thisisasecret' + } +} + if __name__ == "__main__": - from cookie_api.app import app + app = application_factory(**settings) app.main()