diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1a82f0f --- /dev/null +++ b/.gitignore @@ -0,0 +1,13 @@ +.env +env +venv +temp +tmp +__pycache__ + +*.pyc +*.sqlite +*.coverage +.DS_Store +env.sh +migrations diff --git a/.python-version b/.python-version new file mode 100644 index 0000000..87ce492 --- /dev/null +++ b/.python-version @@ -0,0 +1 @@ +3.5.2 diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..455facd --- /dev/null +++ b/.travis.yml @@ -0,0 +1,16 @@ +language: python + +python: + - "3.5" + - "3.4" + - "2.7" + +install: + - pip install -r requirements.txt + - pip install coveralls + +script: + - python manage.py cov + +after_success: + coveralls diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..b100feb --- /dev/null +++ b/LICENSE @@ -0,0 +1,8 @@ +The MIT License (MIT) +Copyright (c) 2016 Michael Herman + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..15c2501 --- /dev/null +++ b/README.md @@ -0,0 +1,71 @@ +# Flask JWT Auth + +[![Build Status](https://travis-ci.org/realpython/flask-jwt-auth.svg?branch=master)](https://travis-ci.org/realpython/flask-jwt-auth) + +## Quick Start + +### Basics + +1. Activate a virtualenv +1. Install the requirements + +### Set Environment Variables + +Update *project/server/config.py*, and then run: + +```sh +$ export APP_SETTINGS="project.server.config.DevelopmentConfig" +``` + +or + +```sh +$ export APP_SETTINGS="project.server.config.ProductionConfig" +``` + +### Create DB + +Create the databases in `psql`: + +```sh +$ psql +# create database flask_jwt_auth +# create database flask_jwt_auth_testing +# \q +``` + +Create the tables and run the migrations: + +```sh +$ python manage.py create_db +$ python manage.py db init +$ python manage.py db migrate +``` + +### Run the Application + +```sh +$ python manage.py runserver +``` + +So access the application at the address [http://localhost:5000/](http://localhost:5000/) + +> Want to specify a different port? + +> ```sh +> $ python manage.py runserver -h 0.0.0.0 -p 8080 +> ``` + +### Testing + +Without coverage: + +```sh +$ python manage.py test +``` + +With coverage: + +```sh +$ python manage.py cov +``` diff --git a/manage.py b/manage.py new file mode 100644 index 0000000..195b9d9 --- /dev/null +++ b/manage.py @@ -0,0 +1,74 @@ +# manage.py + + +import os +import unittest +import coverage + +from flask_script import Manager +from flask_migrate import Migrate, MigrateCommand + +COV = coverage.coverage( + branch=True, + include='project/*', + omit=[ + 'project/tests/*', + 'project/server/config.py', + 'project/server/*/__init__.py' + ] +) +COV.start() + +from project.server import app, db + + +migrate = Migrate(app, db) +manager = Manager(app) + +# migrations +manager.add_command('db', MigrateCommand) + + +@manager.command +def test(): + """Runs the unit tests without test coverage.""" + tests = unittest.TestLoader().discover('project/tests', pattern='test*.py') + result = unittest.TextTestRunner(verbosity=2).run(tests) + if result.wasSuccessful(): + return 0 + return 1 + + +@manager.command +def cov(): + """Runs the unit tests with coverage.""" + tests = unittest.TestLoader().discover('project/tests') + result = unittest.TextTestRunner(verbosity=2).run(tests) + if result.wasSuccessful(): + COV.stop() + COV.save() + print('Coverage Summary:') + COV.report() + basedir = os.path.abspath(os.path.dirname(__file__)) + covdir = os.path.join(basedir, 'tmp/coverage') + COV.html_report(directory=covdir) + print('HTML version: file://%s/index.html' % covdir) + COV.erase() + return 0 + return 1 + + +@manager.command +def create_db(): + """Creates the db tables.""" + db.create_all() + + +@manager.command +def drop_db(): + """Drops the db tables.""" + db.drop_all() + + +if __name__ == '__main__': + manager.run() diff --git a/project/__init__.py b/project/__init__.py new file mode 100644 index 0000000..14012e6 --- /dev/null +++ b/project/__init__.py @@ -0,0 +1 @@ +# project/__init__.py diff --git a/project/server/__init__.py b/project/server/__init__.py new file mode 100644 index 0000000..90b6314 --- /dev/null +++ b/project/server/__init__.py @@ -0,0 +1,18 @@ +# project/server/__init__.py + +import os + +from flask import Flask +from flask_bcrypt import Bcrypt +from flask_sqlalchemy import SQLAlchemy + +app = Flask(__name__) + +app_settings = os.getenv( + 'APP_SETTINGS', + 'project.server.config.DevelopmentConfig' +) +app.config.from_object(app_settings) + +bcrypt = Bcrypt(app) +db = SQLAlchemy(app) diff --git a/project/server/config.py b/project/server/config.py new file mode 100644 index 0000000..b681bee --- /dev/null +++ b/project/server/config.py @@ -0,0 +1,37 @@ +# project/server/config.py + +import os +basedir = os.path.abspath(os.path.dirname(__file__)) +postgres_local_base = 'postgresql://postgres:@localhost/' +database_name = 'something' + + +class BaseConfig: + """Base configuration.""" + SECRET_KEY = 'my_precious' + DEBUG = False + BCRYPT_LOG_ROUNDS = 13 + SQLALCHEMY_TRACK_MODIFICATIONS = False + + +class DevelopmentConfig(BaseConfig): + """Development configuration.""" + DEBUG = True + BCRYPT_LOG_ROUNDS = 4 + SQLALCHEMY_DATABASE_URI = postgres_local_base + database_name + + +class TestingConfig(BaseConfig): + """Testing configuration.""" + DEBUG = True + TESTING = True + BCRYPT_LOG_ROUNDS = 4 + SQLALCHEMY_DATABASE_URI = postgres_local_base + database_name + '_test' + PRESERVE_CONTEXT_ON_EXCEPTION = False + + +class ProductionConfig(BaseConfig): + """Production configuration.""" + SECRET_KEY = 'my_precious' + DEBUG = False + SQLALCHEMY_DATABASE_URI = 'postgresql:///example' diff --git a/project/tests/__init__.py b/project/tests/__init__.py new file mode 100644 index 0000000..b452ab6 --- /dev/null +++ b/project/tests/__init__.py @@ -0,0 +1 @@ +# project/server/tests/__init__.py diff --git a/project/tests/base.py b/project/tests/base.py new file mode 100644 index 0000000..899757e --- /dev/null +++ b/project/tests/base.py @@ -0,0 +1,22 @@ +# project/server/tests/base.py + + +from flask_testing import TestCase + +from project.server import app, db + + +class BaseTestCase(TestCase): + """ Base Tests """ + + def create_app(self): + app.config.from_object('project.server.config.TestingConfig') + return app + + def setUp(self): + db.create_all() + db.session.commit() + + def tearDown(self): + db.session.remove() + db.drop_all() diff --git a/project/tests/helpers.py b/project/tests/helpers.py new file mode 100644 index 0000000..70af7a7 --- /dev/null +++ b/project/tests/helpers.py @@ -0,0 +1 @@ +# tests/helpers.py diff --git a/project/tests/test__config.py b/project/tests/test__config.py new file mode 100644 index 0000000..978efba --- /dev/null +++ b/project/tests/test__config.py @@ -0,0 +1,41 @@ +# project/server/tests/test_config.py + + +import unittest + +from flask import current_app +from flask_testing import TestCase + +from project.server import app + + +class TestDevelopmentConfig(TestCase): + def create_app(self): + app.config.from_object('project.server.config.DevelopmentConfig') + return app + + def test_app_is_development(self): + self.assertTrue(app.config['DEBUG'] is True) + self.assertFalse(current_app is None) + + +class TestTestingConfig(TestCase): + def create_app(self): + app.config.from_object('project.server.config.TestingConfig') + return app + + def test_app_is_testing(self): + self.assertTrue(app.config['DEBUG']) + + +class TestProductionConfig(TestCase): + def create_app(self): + app.config.from_object('project.server.config.ProductionConfig') + return app + + def test_app_is_production(self): + self.assertTrue(app.config['DEBUG'] is False) + + +if __name__ == '__main__': + unittest.main() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..1132525 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,20 @@ +alembic==0.8.9 +bcrypt==3.1.2 +cffi==1.9.1 +click==6.6 +coverage==4.2 +Flask==0.12 +Flask-Bcrypt==0.7.1 +Flask-Migrate==2.0.2 +Flask-Script==2.0.5 +Flask-SQLAlchemy==2.1 +Flask-Testing==0.6.1 +itsdangerous==0.24 +Jinja2==2.8 +Mako==1.0.6 +MarkupSafe==0.23 +pycparser==2.17 +python-editor==1.0.3 +six==1.10.0 +SQLAlchemy==1.1.4 +Werkzeug==0.11.13