Skip to content

Commit

Permalink
v1
Browse files Browse the repository at this point in the history
  • Loading branch information
mjhea0 committed Dec 26, 2016
0 parents commit 3a851f5
Show file tree
Hide file tree
Showing 14 changed files with 324 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.env
env
venv
temp
tmp
__pycache__

*.pyc
*.sqlite
*.coverage
.DS_Store
env.sh
migrations
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.5.2
16 changes: 16 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
71 changes: 71 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
```
74 changes: 74 additions & 0 deletions manage.py
Original file line number Diff line number Diff line change
@@ -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()
1 change: 1 addition & 0 deletions project/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# project/__init__.py
18 changes: 18 additions & 0 deletions project/server/__init__.py
Original file line number Diff line number Diff line change
@@ -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)
37 changes: 37 additions & 0 deletions project/server/config.py
Original file line number Diff line number Diff line change
@@ -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'
1 change: 1 addition & 0 deletions project/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# project/server/tests/__init__.py
22 changes: 22 additions & 0 deletions project/tests/base.py
Original file line number Diff line number Diff line change
@@ -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()
1 change: 1 addition & 0 deletions project/tests/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# tests/helpers.py
41 changes: 41 additions & 0 deletions project/tests/test__config.py
Original file line number Diff line number Diff line change
@@ -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()
20 changes: 20 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 3a851f5

Please sign in to comment.