From 836b0bb7ddd1ef7f2142a3d31dde233228c9cc48 Mon Sep 17 00:00:00 2001 From: Sorrel Bri Date: Fri, 4 Oct 2019 11:24:11 -0700 Subject: [PATCH 1/2] 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 From df04427ea0b586702dffe4b2298109f4ef043058 Mon Sep 17 00:00:00 2001 From: Sorrel Bri Date: Fri, 4 Oct 2019 15:00:47 -0700 Subject: [PATCH 2/2] create api blueprint --- {games => api}/__init__.py | 0 api/api.py | 14 ++++++++++++++ {home => api/games}/__init__.py | 0 {games => api/games}/game.py | 0 {rooms => api/home}/__init__.py | 0 {home => api/home}/home.py | 0 {users => api/rooms}/__init__.py | 0 {rooms => api/rooms}/room.py | 0 api/users/__init__.py | 0 api/users/user_endpoint.py | 5 +++++ app.py | 16 +++++++--------- config.py | 2 +- models.py | 6 ++++++ models/Game.py | 2 +- models/GameRoom.py | 2 +- models/Message.py | 2 +- models/Move.py | 2 +- models/TimeSettings.py | 2 +- models/User.py | 2 +- users/user.py | 11 ----------- 20 files changed, 39 insertions(+), 27 deletions(-) rename {games => api}/__init__.py (100%) create mode 100644 api/api.py rename {home => api/games}/__init__.py (100%) rename {games => api/games}/game.py (100%) rename {rooms => api/home}/__init__.py (100%) rename {home => api/home}/home.py (100%) rename {users => api/rooms}/__init__.py (100%) rename {rooms => api/rooms}/room.py (100%) create mode 100644 api/users/__init__.py create mode 100644 api/users/user_endpoint.py create mode 100644 models.py delete mode 100644 users/user.py diff --git a/games/__init__.py b/api/__init__.py similarity index 100% rename from games/__init__.py rename to api/__init__.py diff --git a/api/api.py b/api/api.py new file mode 100644 index 0000000..5b1e268 --- /dev/null +++ b/api/api.py @@ -0,0 +1,14 @@ +from flask import Blueprint, request, jsonify, session +from .users.user_endpoint import UserEndpoint + +api = Blueprint('api', __name__, url_prefix='/api') + + +@api.route('/home', methods=['GET']) +def api_home(): + response = {"message": "home page"} + return jsonify(response) + +@api.route('/users') +def api_users(): + return jsonify(UserEndpoint.users()) diff --git a/home/__init__.py b/api/games/__init__.py similarity index 100% rename from home/__init__.py rename to api/games/__init__.py diff --git a/games/game.py b/api/games/game.py similarity index 100% rename from games/game.py rename to api/games/game.py diff --git a/rooms/__init__.py b/api/home/__init__.py similarity index 100% rename from rooms/__init__.py rename to api/home/__init__.py diff --git a/home/home.py b/api/home/home.py similarity index 100% rename from home/home.py rename to api/home/home.py diff --git a/users/__init__.py b/api/rooms/__init__.py similarity index 100% rename from users/__init__.py rename to api/rooms/__init__.py diff --git a/rooms/room.py b/api/rooms/room.py similarity index 100% rename from rooms/room.py rename to api/rooms/room.py diff --git a/api/users/__init__.py b/api/users/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/api/users/user_endpoint.py b/api/users/user_endpoint.py new file mode 100644 index 0000000..e147fed --- /dev/null +++ b/api/users/user_endpoint.py @@ -0,0 +1,5 @@ + +class UserEndpoint(object): + def users(): + response = {"message": "users page"} + return response diff --git a/app.py b/app.py index 876a94f..a689d04 100644 --- a/app.py +++ b/app.py @@ -17,12 +17,12 @@ CORS(app) # config database app_settings = os.getenv( 'APP_SETTINGS', - 'project.server.config.DevelopmentConfig' + 'config.DevelopmentConfig' ) app.config.from_object(app_settings) -#init bcrypt +# init bcrypt bcrypt = Bcrypt(app) # init database @@ -32,12 +32,7 @@ db = SQLAlchemy(app) ma = Marshmallow(app) # init all db models -from .models.User import User -from .models.GameRoom import GameRoom -from .models.TimeSettings import TimeSettings -from .models.Game import Game -from .models.Move import Move -from .models.Message import Message +import models migrate = Migrate(app, db) @@ -52,7 +47,10 @@ PORT = 8000 def hello_world(): return 'Hello World' -@app.route('/api') +# Blue prints +from api.api import api + +app.register_blueprint(api) if __name__ == '__main__': diff --git a/config.py b/config.py index e6c7f95..0b3f00f 100644 --- a/config.py +++ b/config.py @@ -1,4 +1,4 @@ - +import os # local db DATABASE = 'postgresql://localhost/browser-go' diff --git a/models.py b/models.py new file mode 100644 index 0000000..5fa00f1 --- /dev/null +++ b/models.py @@ -0,0 +1,6 @@ +from models.User import User +from models.GameRoom import GameRoom +from models.TimeSettings import TimeSettings +from models.Game import Game +from models.Move import Move +from models.Message import Message \ No newline at end of file diff --git a/models/Game.py b/models/Game.py index f8ec676..742205e 100644 --- a/models/Game.py +++ b/models/Game.py @@ -1,4 +1,4 @@ -from ..app import db, ma +from app import db, ma import enum class Players(enum.Enum): diff --git a/models/GameRoom.py b/models/GameRoom.py index 5da8f4e..3e5239c 100644 --- a/models/GameRoom.py +++ b/models/GameRoom.py @@ -1,4 +1,4 @@ -from ..app import db, ma +from app import db, ma import enum class Languages(enum.Enum): diff --git a/models/Message.py b/models/Message.py index 7a273a4..3a81404 100644 --- a/models/Message.py +++ b/models/Message.py @@ -1,4 +1,4 @@ -from ..app import db, ma +from app import db, ma import enum class Players(enum.Enum): diff --git a/models/Move.py b/models/Move.py index d3dd264..a3afe12 100644 --- a/models/Move.py +++ b/models/Move.py @@ -1,4 +1,4 @@ -from ..app import db, ma +from app import db, ma import enum class Players(enum.Enum): diff --git a/models/TimeSettings.py b/models/TimeSettings.py index bdf142e..0c8c5bb 100644 --- a/models/TimeSettings.py +++ b/models/TimeSettings.py @@ -1,4 +1,4 @@ -from ..app import db, ma +from app import db, ma import enum class TimeTypes(enum.Enum): diff --git a/models/User.py b/models/User.py index 94754bb..83a9302 100644 --- a/models/User.py +++ b/models/User.py @@ -1,4 +1,4 @@ -from ..app import db, ma, bcrypt +from app import db, ma, bcrypt import datetime import enum diff --git a/users/user.py b/users/user.py deleted file mode 100644 index fd9b6d3..0000000 --- a/users/user.py +++ /dev/null @@ -1,11 +0,0 @@ -from flask import Blueprint - -user = Blueprint('users', __name__) - -@user.route('/') -def func(): - pass - -@user.route('/') -def func(): - pass \ No newline at end of file