From 836b0bb7ddd1ef7f2142a3d31dde233228c9cc48 Mon Sep 17 00:00:00 2001 From: Sorrel Bri Date: Fri, 4 Oct 2019 11:24:11 -0700 Subject: [PATCH] config tests --- app.py | 17 +++++++++-------- config.py | 32 ++++++++++++++++++++++++++++++++ manage.py | 39 +++++++++++++++++++++++++++++++++++++++ models.py | 1 - models/User.py | 7 ++++++- requirements.txt | 2 ++ tests/__init__.py | 0 tests/base.py | 14 ++++++++++++++ tests/test_config.py | 14 ++++++++++++++ 9 files changed, 116 insertions(+), 10 deletions(-) create mode 100644 config.py create mode 100644 manage.py delete mode 100644 models.py create mode 100644 tests/__init__.py create mode 100644 tests/base.py create mode 100644 tests/test_config.py diff --git a/app.py b/app.py index 8e7d1be..876a94f 100644 --- a/app.py +++ b/app.py @@ -14,15 +14,13 @@ from flask_cors import CORS app = Flask(__name__) CORS(app) -# base directory -basedir = os.path.abspath(os.path.dirname(__file__)) - -# dev database -DATABASE = 'postgresql://localhost/browser-go' - # config database -app.config['SQLALCHEMY_DATABASE_URI'] = DATABASE -app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False +app_settings = os.getenv( + 'APP_SETTINGS', + 'project.server.config.DevelopmentConfig' +) + +app.config.from_object(app_settings) #init bcrypt bcrypt = Bcrypt(app) @@ -54,5 +52,8 @@ PORT = 8000 def hello_world(): return 'Hello World' +@app.route('/api') + + if __name__ == '__main__': app.run(debug=DEBUG, port=PORT) \ No newline at end of file diff --git a/config.py b/config.py new file mode 100644 index 0000000..e6c7f95 --- /dev/null +++ b/config.py @@ -0,0 +1,32 @@ + +# local db +DATABASE = 'postgresql://localhost/browser-go' + +class BaseConfig: + """Base configuration.""" + SECRET_KEY = os.getenv('SECRET_KEY') + DEBUG = False + BCRYPT_LOG_ROUNDS = 13 + SQLALCHEMY_TRACK_MODIFICATIONS = False + +class DevelopmentConfig(BaseConfig): + """Development configuration.""" + DEBUG = True + BCRYPT_LOG_ROUNDS = 4 + SQLALCHEMY_DATABASE_URI = DATABASE + + +class TestingConfig(BaseConfig): + """Testing configuration.""" + DEBUG = True + TESTING = True + BCRYPT_LOG_ROUNDS = 4 + SQLALCHEMY_DATABASE_URI = DATABASE + PRESERVE_CONTEXT_ON_EXCEPTION = False + + +class ProductionConfig(BaseConfig): + """Production configuration.""" + SECRET_KEY = '' + DEBUG = False + SQLALCHEMY_DATABASE_URI = 'postgresql:///' \ No newline at end of file diff --git a/manage.py b/manage.py new file mode 100644 index 0000000..70172ba --- /dev/null +++ b/manage.py @@ -0,0 +1,39 @@ +import os +import unittest + +from flask_script import Manager +from flask_migrate import Migrate, MigrateCommand + +from app 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('browser-go-api/tests', pattern='test*.py') + result = unittest.TextTestRunner(verbosity=2).run(tests) + if result.wasSuccessful(): + 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() \ No newline at end of file diff --git a/models.py b/models.py deleted file mode 100644 index e9ba0dd..0000000 --- a/models.py +++ /dev/null @@ -1 +0,0 @@ -from app import db, ma diff --git a/models/User.py b/models/User.py index b2888eb..94754bb 100644 --- a/models/User.py +++ b/models/User.py @@ -1,4 +1,5 @@ from ..app import db, ma, bcrypt +import datetime import enum class Ranks(enum.Enum): # with minimal Elo rating @@ -39,6 +40,7 @@ class Ranks(enum.Enum): # with minimal Elo rating K28 = "Twenty-Eight Kyu" K29 = "Twenty-Nine Kyu" K30 = "Thirty Kyu" # Elo -900 + RU = "Unknown Rank" class User(db.Model): __tablename__ = "users" @@ -52,7 +54,10 @@ class User(db.Model): elo = db.Column(db.Integer) rank_certainty = db.Column(db.Boolean, nullable=False, default=False) - def __init__(self): + def __init__(self, email, password, admin=False,): + self.email = email self.password = bcrypt.generate_password_hash( password, app.config.get('BCRYPT_LOG_ROUNDS') ).decode() + self.registered_on = datetime.datetime.now() + self.admin = admin diff --git a/requirements.txt b/requirements.txt index 547158f..f300d47 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,7 +8,9 @@ Flask-Bcrypt==0.7.1 Flask-Cors==3.0.8 flask-marshmallow==0.10.1 Flask-Migrate==2.5.2 +Flask-Script==2.0.6 Flask-SQLAlchemy==2.4.1 +Flask-Testing==0.7.1 isort==4.3.21 itsdangerous==1.1.0 Jinja2==2.10.1 diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/base.py b/tests/base.py new file mode 100644 index 0000000..1bed7a2 --- /dev/null +++ b/tests/base.py @@ -0,0 +1,14 @@ +from flask_testing import TestCase +from ..app import app, db + +class BaseTestCase(TestCase): + """ Base Tests """ + + def create_app(self): + pass + + def setUp(self): + pass + + def tearDown(self): + pass \ No newline at end of file diff --git a/tests/test_config.py b/tests/test_config.py new file mode 100644 index 0000000..df01f5a --- /dev/null +++ b/tests/test_config.py @@ -0,0 +1,14 @@ +import unittest + +from flask import current_app +from flask_testing import TestCase + +from ..app import app + +class TestDevelopmentConfig(TestCase): + def create_app(self): + app.config.from_object('browser-go-api.config.DevelopmentConfig') + return app + + def test_app_is_development(self): + self.assertFalse(app.config['SECRET_KEY'] is 'my_precious') \ No newline at end of file