diff --git a/.gitignore b/.gitignore index 1face41..5914f64 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ benv/ *.pyc +.python-version \ No newline at end of file diff --git a/blogexample/app.py b/blogexample/app.py index 7c19bbb..f626bf0 100644 --- a/blogexample/app.py +++ b/blogexample/app.py @@ -1,8 +1,9 @@ #!/usr/bin/env python -from flask import Flask, render_template, session, request, jsonify, url_for -from flask_sqlalchemy import SQLAlchemy +import os +from flask import Flask, jsonify, render_template, request, session, url_for +from flask_sqlalchemy import SQLAlchemy from werkzeug.debug import DebuggedApplication # from flask_ckeditor import CKEditor#, CKEditorField @@ -15,19 +16,18 @@ def create_app(main=True, debug=True): """Create an application.""" app = Flask(__name__) - app.config.from_object('config.settings') - app.config['SECRET_KEY'] = 'secret!' + config_type = os.getenv("CONFIG_TYPE", default="config.settings.DevelopmentConfig") + app.config.from_object(config_type) - from blogexample.blueprints.blog import blog + app.register_blueprint(blog) - + db.init_app(app) # ckeditor.init_app(app) if app.debug: app.wsgi_app = DebuggedApplication(app.wsgi_app, evalex=True) - - return app + return app diff --git a/blogexample/blueprints/blog/views.py b/blogexample/blueprints/blog/views.py index c58ca71..7ba835d 100644 --- a/blogexample/blueprints/blog/views.py +++ b/blogexample/blueprints/blog/views.py @@ -51,7 +51,6 @@ def add_post(): if Post.create_blogpost(params): flash('Post has been created successfully.', 'success') return redirect(url_for('blog.show_posts')) - return render_template('add.html', form=form, blogpost=blogpost) diff --git a/blogexample/tests/__init__.py b/blogexample/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/blogexample/tests/test_views.py b/blogexample/tests/test_views.py new file mode 100644 index 0000000..67e80f7 --- /dev/null +++ b/blogexample/tests/test_views.py @@ -0,0 +1,66 @@ +import os + +import pytest +from flask import url_for + +from blogexample.app import create_app, db +from blogexample.blueprints.blog.forms import AddPostForm +from blogexample.blueprints.blog.models import Post + + +@pytest.fixture(scope="function") +def client(): + os.environ["CONFIG_TYPE"] = "config.settings.TestingConfig" + flask_app = create_app() + + with flask_app.test_client() as testing_client: + with flask_app.app_context(): + + db.create_all() + yield testing_client # testing phase + db.drop_all() + + +def test_index(client): + response = client.get(url_for("blog.index")) + assert response.status_code == 200 + + +def test_add_post(client): + form_data = { + "title": "Test Title", + "body": "Test Body", + "taglist": "two, new", + "visible": True, + } + + response = client.post( + url_for("blog.add_post"), data=form_data, follow_redirects=True + ) + assert response.status_code == 200 + + # test if the post exists in db + post = Post.query.filter_by(title="Test Title").first() + assert post is not None + assert post.body == "Test Body" + + +def test_show_posts(client): + + test_post_1 = Post( + title="First Post", body="Body of Post 1", visible=True, url="first-post" + ) + test_post_2 = Post( + title="Second Post", body="Body of Post 1", visible=True, url="second-post" + ) + + db.session.add(test_post_1) + db.session.add(test_post_2) + + db.session.commit() + + response = client.get(url_for("blog.show_posts")) + assert response.status_code == 200 + assert b'First Post' in response.data + + diff --git a/config/settings.py b/config/settings.py index 1cf2d55..c57045f 100755 --- a/config/settings.py +++ b/config/settings.py @@ -1,14 +1,32 @@ +import os from datetime import timedelta -DEBUG = True +BASEDIR = os.path.abspath(os.path.dirname(__file__)) -SERVER_NAME = 'localhost:8000' -SECRET_KEY = 'insecurekeyfordev' +class Config(object): + FLASK_ENV = "development" + SERVER_NAME = "localhost:8000" + DEBUG = False + TESTING = False + REMEMBER_COOKIE_DURATION = timedelta(days=90) + SECRET_KEY = os.getenv("SECRET_KEY", default="BAD_SECRET_KEY") -# SQLAlchemy. -db_uri = 'postgresql://flaskblog:blogpassword@postgres:5432/flaskblog' -SQLALCHEMY_DATABASE_URI = db_uri -SQLALCHEMY_TRACK_MODIFICATIONS = False + SQLALCHEMY_DATABASE_URI = ( + "postgresql://flaskblog:blogpassword@postgres:5432/flaskblog" + ) + SQLALCHEMY_TRACK_MODIFICATIONS = False -REMEMBER_COOKIE_DURATION = timedelta(days=90) + +class ProductionConfig(Config): + FLASK_ENV = "production" + + +class DevelopmentConfig(Config): + DEBUG = True + + +class TestingConfig(Config): + TESTING = True + SQLALCHEMY_DATABASE_URI = f"sqlite:///{os.path.join(BASEDIR, 'test.db')}" + WTF_CSRF_ENABLED = False diff --git a/config/test.db b/config/test.db new file mode 100644 index 0000000..6fd428c Binary files /dev/null and b/config/test.db differ diff --git a/image.png b/image.png new file mode 100644 index 0000000..fbc33fd Binary files /dev/null and b/image.png differ diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 0000000..9a08eef --- /dev/null +++ b/pytest.ini @@ -0,0 +1,6 @@ +[pytest] +addopts = -v +testpaths = ./blogexample/tests +filterwarnings = + ignore::DeprecationWarning + ignore::RuntimeWarning diff --git a/requirements.txt b/requirements.txt index d5f92bc..ce63010 100644 --- a/requirements.txt +++ b/requirements.txt @@ -20,3 +20,4 @@ WTForms-Alchemy==0.16.9 flask_ckeditor pytz +pytest \ No newline at end of file