Skip to content

Commit 3a851f5

Browse files
committed
v1
0 parents  commit 3a851f5

14 files changed

+324
-0
lines changed

.gitignore

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.env
2+
env
3+
venv
4+
temp
5+
tmp
6+
__pycache__
7+
8+
*.pyc
9+
*.sqlite
10+
*.coverage
11+
.DS_Store
12+
env.sh
13+
migrations

.python-version

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.5.2

.travis.yml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
language: python
2+
3+
python:
4+
- "3.5"
5+
- "3.4"
6+
- "2.7"
7+
8+
install:
9+
- pip install -r requirements.txt
10+
- pip install coveralls
11+
12+
script:
13+
- python manage.py cov
14+
15+
after_success:
16+
coveralls

LICENSE

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
The MIT License (MIT)
2+
Copyright (c) 2016 Michael Herman
3+
4+
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:
5+
6+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7+
8+
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.

README.md

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Flask JWT Auth
2+
3+
[![Build Status](https://travis-ci.org/realpython/flask-jwt-auth.svg?branch=master)](https://travis-ci.org/realpython/flask-jwt-auth)
4+
5+
## Quick Start
6+
7+
### Basics
8+
9+
1. Activate a virtualenv
10+
1. Install the requirements
11+
12+
### Set Environment Variables
13+
14+
Update *project/server/config.py*, and then run:
15+
16+
```sh
17+
$ export APP_SETTINGS="project.server.config.DevelopmentConfig"
18+
```
19+
20+
or
21+
22+
```sh
23+
$ export APP_SETTINGS="project.server.config.ProductionConfig"
24+
```
25+
26+
### Create DB
27+
28+
Create the databases in `psql`:
29+
30+
```sh
31+
$ psql
32+
# create database flask_jwt_auth
33+
# create database flask_jwt_auth_testing
34+
# \q
35+
```
36+
37+
Create the tables and run the migrations:
38+
39+
```sh
40+
$ python manage.py create_db
41+
$ python manage.py db init
42+
$ python manage.py db migrate
43+
```
44+
45+
### Run the Application
46+
47+
```sh
48+
$ python manage.py runserver
49+
```
50+
51+
So access the application at the address [http://localhost:5000/](http://localhost:5000/)
52+
53+
> Want to specify a different port?
54+
55+
> ```sh
56+
> $ python manage.py runserver -h 0.0.0.0 -p 8080
57+
> ```
58+
59+
### Testing
60+
61+
Without coverage:
62+
63+
```sh
64+
$ python manage.py test
65+
```
66+
67+
With coverage:
68+
69+
```sh
70+
$ python manage.py cov
71+
```

manage.py

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# manage.py
2+
3+
4+
import os
5+
import unittest
6+
import coverage
7+
8+
from flask_script import Manager
9+
from flask_migrate import Migrate, MigrateCommand
10+
11+
COV = coverage.coverage(
12+
branch=True,
13+
include='project/*',
14+
omit=[
15+
'project/tests/*',
16+
'project/server/config.py',
17+
'project/server/*/__init__.py'
18+
]
19+
)
20+
COV.start()
21+
22+
from project.server import app, db
23+
24+
25+
migrate = Migrate(app, db)
26+
manager = Manager(app)
27+
28+
# migrations
29+
manager.add_command('db', MigrateCommand)
30+
31+
32+
@manager.command
33+
def test():
34+
"""Runs the unit tests without test coverage."""
35+
tests = unittest.TestLoader().discover('project/tests', pattern='test*.py')
36+
result = unittest.TextTestRunner(verbosity=2).run(tests)
37+
if result.wasSuccessful():
38+
return 0
39+
return 1
40+
41+
42+
@manager.command
43+
def cov():
44+
"""Runs the unit tests with coverage."""
45+
tests = unittest.TestLoader().discover('project/tests')
46+
result = unittest.TextTestRunner(verbosity=2).run(tests)
47+
if result.wasSuccessful():
48+
COV.stop()
49+
COV.save()
50+
print('Coverage Summary:')
51+
COV.report()
52+
basedir = os.path.abspath(os.path.dirname(__file__))
53+
covdir = os.path.join(basedir, 'tmp/coverage')
54+
COV.html_report(directory=covdir)
55+
print('HTML version: file://%s/index.html' % covdir)
56+
COV.erase()
57+
return 0
58+
return 1
59+
60+
61+
@manager.command
62+
def create_db():
63+
"""Creates the db tables."""
64+
db.create_all()
65+
66+
67+
@manager.command
68+
def drop_db():
69+
"""Drops the db tables."""
70+
db.drop_all()
71+
72+
73+
if __name__ == '__main__':
74+
manager.run()

project/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# project/__init__.py

project/server/__init__.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# project/server/__init__.py
2+
3+
import os
4+
5+
from flask import Flask
6+
from flask_bcrypt import Bcrypt
7+
from flask_sqlalchemy import SQLAlchemy
8+
9+
app = Flask(__name__)
10+
11+
app_settings = os.getenv(
12+
'APP_SETTINGS',
13+
'project.server.config.DevelopmentConfig'
14+
)
15+
app.config.from_object(app_settings)
16+
17+
bcrypt = Bcrypt(app)
18+
db = SQLAlchemy(app)

project/server/config.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# project/server/config.py
2+
3+
import os
4+
basedir = os.path.abspath(os.path.dirname(__file__))
5+
postgres_local_base = 'postgresql://postgres:@localhost/'
6+
database_name = 'something'
7+
8+
9+
class BaseConfig:
10+
"""Base configuration."""
11+
SECRET_KEY = 'my_precious'
12+
DEBUG = False
13+
BCRYPT_LOG_ROUNDS = 13
14+
SQLALCHEMY_TRACK_MODIFICATIONS = False
15+
16+
17+
class DevelopmentConfig(BaseConfig):
18+
"""Development configuration."""
19+
DEBUG = True
20+
BCRYPT_LOG_ROUNDS = 4
21+
SQLALCHEMY_DATABASE_URI = postgres_local_base + database_name
22+
23+
24+
class TestingConfig(BaseConfig):
25+
"""Testing configuration."""
26+
DEBUG = True
27+
TESTING = True
28+
BCRYPT_LOG_ROUNDS = 4
29+
SQLALCHEMY_DATABASE_URI = postgres_local_base + database_name + '_test'
30+
PRESERVE_CONTEXT_ON_EXCEPTION = False
31+
32+
33+
class ProductionConfig(BaseConfig):
34+
"""Production configuration."""
35+
SECRET_KEY = 'my_precious'
36+
DEBUG = False
37+
SQLALCHEMY_DATABASE_URI = 'postgresql:///example'

project/tests/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# project/server/tests/__init__.py

project/tests/base.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# project/server/tests/base.py
2+
3+
4+
from flask_testing import TestCase
5+
6+
from project.server import app, db
7+
8+
9+
class BaseTestCase(TestCase):
10+
""" Base Tests """
11+
12+
def create_app(self):
13+
app.config.from_object('project.server.config.TestingConfig')
14+
return app
15+
16+
def setUp(self):
17+
db.create_all()
18+
db.session.commit()
19+
20+
def tearDown(self):
21+
db.session.remove()
22+
db.drop_all()

project/tests/helpers.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# tests/helpers.py

project/tests/test__config.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# project/server/tests/test_config.py
2+
3+
4+
import unittest
5+
6+
from flask import current_app
7+
from flask_testing import TestCase
8+
9+
from project.server import app
10+
11+
12+
class TestDevelopmentConfig(TestCase):
13+
def create_app(self):
14+
app.config.from_object('project.server.config.DevelopmentConfig')
15+
return app
16+
17+
def test_app_is_development(self):
18+
self.assertTrue(app.config['DEBUG'] is True)
19+
self.assertFalse(current_app is None)
20+
21+
22+
class TestTestingConfig(TestCase):
23+
def create_app(self):
24+
app.config.from_object('project.server.config.TestingConfig')
25+
return app
26+
27+
def test_app_is_testing(self):
28+
self.assertTrue(app.config['DEBUG'])
29+
30+
31+
class TestProductionConfig(TestCase):
32+
def create_app(self):
33+
app.config.from_object('project.server.config.ProductionConfig')
34+
return app
35+
36+
def test_app_is_production(self):
37+
self.assertTrue(app.config['DEBUG'] is False)
38+
39+
40+
if __name__ == '__main__':
41+
unittest.main()

requirements.txt

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
alembic==0.8.9
2+
bcrypt==3.1.2
3+
cffi==1.9.1
4+
click==6.6
5+
coverage==4.2
6+
Flask==0.12
7+
Flask-Bcrypt==0.7.1
8+
Flask-Migrate==2.0.2
9+
Flask-Script==2.0.5
10+
Flask-SQLAlchemy==2.1
11+
Flask-Testing==0.6.1
12+
itsdangerous==0.24
13+
Jinja2==2.8
14+
Mako==1.0.6
15+
MarkupSafe==0.23
16+
pycparser==2.17
17+
python-editor==1.0.3
18+
six==1.10.0
19+
SQLAlchemy==1.1.4
20+
Werkzeug==0.11.13

0 commit comments

Comments
 (0)