Skip to content

Commit

Permalink
Initial Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jackson Chadfield committed Jul 31, 2019
1 parent 547181f commit 1035c00
Show file tree
Hide file tree
Showing 14 changed files with 132 additions and 13 deletions.
6 changes: 4 additions & 2 deletions OpenLearn/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
from . import settings


def create_app():
def create_app(config=settings.ConfigType.Auto):
"""Create application factory, as explained here: http://flask.pocoo.org/docs/patterns/appfactories/.
"""
app = Flask(__name__.split(".")[0], instance_relative_config=True)

settings.configure_app(app)
settings.configure_app(app, config)

register_extensions(app)
register_blueprints(app)
Expand All @@ -39,6 +39,8 @@ def register_extensions(app):
def load_user(user_id):
return database.User.query.get(int(user_id))

extensions.login_manager.login_view = "public.login"


def register_blueprints(app):
"""Register Flask blueprints."""
Expand Down
34 changes: 30 additions & 4 deletions OpenLearn/settings.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# -*- coding: utf-8 -*-
import enum
import os
from typing import Optional

import OpenLearn

config = {
Expand All @@ -10,7 +13,25 @@
}


class ConfigType(enum.Enum):
Auto = 0
Development = "development"
Testing = "testing"
Production = "production"

@property
def config(self):
return {
ConfigType.Development: lambda: DevelopmentConfig,
ConfigType.Testing: lambda: TestingConfig,
ConfigType.Production: lambda: ProductionConfig,
ConfigType.Auto: lambda: ConfigType(os.getenv("FLASK_ENV", "development")).config
}[self]()


class BaseConfig(object):
CONFIG_FILE = "config.cfg"

DEBUG = False
TESTING = False
VERSION = OpenLearn.__version__
Expand All @@ -25,15 +46,20 @@ class DevelopmentConfig(BaseConfig):


class TestingConfig(BaseConfig):
CONFIG_FILE = "testing.cfg"

DEBUG = False
TESTING = True

SQLALCHEMY_DATABASE_URI = "sqlite://"
WTF_CSRF_ENABLED = False
BCRYPT_LOG_ROUNDS = 4


class ProductionConfig(BaseConfig):
pass


def configure_app(app):
config_name = os.getenv("FLASK_ENV", "default")
app.config.from_object(config[config_name])
app.config.from_pyfile('config.cfg', silent=False)
def configure_app(app, config_type: ConfigType):
app.config.from_object(config_type.config)
app.config.from_pyfile(app.config["CONFIG_FILE"], silent=False)
2 changes: 1 addition & 1 deletion OpenLearn/templates/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{% set noscript_can_close = true %}

{% block nav_items %}
<a class="siimple-navbar-item" href="{{ url_for("public.sign_in") }}"><i class="fas fa-sign-in-alt"></i> Sign In</a>
<a class="siimple-navbar-item" href="{{ url_for("public.login") }}"><i class="fas fa-sign-in-alt"></i> Sign In</a>
<a class="siimple-navbar-item" href="{{ url_for("student.join_room") }}"><i class="fas fa-users"></i> Join a Room</a>
{% endblock %}

Expand Down
2 changes: 1 addition & 1 deletion OpenLearn/templates/public/register.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
id="room-code-form">
{{ form.hidden_tag() }}
<div class="siimple-form-title">Register</div>
<div class="siimple-form-detail">Already have an account? <a href="{{ url_for("public.sign_in") }}"
<div class="siimple-form-detail">Already have an account? <a href="{{ url_for("public.login") }}"
class="siimple-link">Sign In Here</a></div>
<div class="siimple-form-field">
{{ form.username.label(class_="siimple-form-field-label") }}
Expand Down
1 change: 1 addition & 0 deletions OpenLearn/templates/student/join.html
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
}
};
xhttp.open("POST", "{{url_for("student.join_room")}}", true);
xhttp.setRequestHeader("X-CSRFToken", "{{ form.csrf_token.current_token }}");
xhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhttp.send(requestBody);
});
Expand Down
4 changes: 2 additions & 2 deletions OpenLearn/views/public/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ def index():


@blueprint.route("/login")
def sign_in():
def login():
form = LoginForm()
if form.validate_on_submit():
login_user(form.user)
return redirect(url_for("teacher.dashboard"))
return render_template("sign-in.html", form=form)
return render_template("login.html", form=form)


@blueprint.route("/register", methods=["GET", "POST"])
Expand Down
1 change: 0 additions & 1 deletion OpenLearn/views/public/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ def validate(self):
return True



class RegisterForm(FlaskForm):
username = StringField("Username", validators=[
InputRequired(),
Expand Down
2 changes: 1 addition & 1 deletion OpenLearn/views/teacher/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ def dashboard():
@blueprint.route("/logout")
def logout():
logout_user()
return redirect(url_for("public.sign_in"))
return redirect(url_for("public.login"))
11 changes: 10 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ flask-login = "^0.4.1"
[tool.poetry.dev-dependencies]
flask-debugtoolbar = "^0.10.1"
flask-testing = "^0.7.1"
nose = "^1.3"

[build-system]
requires = ["poetry>=0.12"]
Expand Down
1 change: 1 addition & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# -*- coding: utf-8 -*-
16 changes: 16 additions & 0 deletions tests/test_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# -*- coding: utf-8 -*-
import configparser
import pathlib
import unittest

import OpenLearn


class TestApp(unittest.TestCase):
def test_version_number(self):
"""Ensure the version is the same everywhere"""
config_path = pathlib.Path(OpenLearn.__file__).parent.parent / "pyproject.toml"
parser = configparser.ConfigParser()
parser.read(config_path)
version_number_config_file = parser["tool.poetry"]["version"].strip('"')
self.assertEqual(version_number_config_file, OpenLearn.__version__)
19 changes: 19 additions & 0 deletions tests/test_utilities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import os
import unittest
from unittest import mock

from OpenLearn import settings


class TestSettingsUtilities(unittest.TestCase):
@mock.patch.dict(os.environ, {'FLASK_ENV': 'development'})
def test_config_type_auto_resolution_development(self):
self.assertIs(settings.ConfigType.Auto.config, settings.DevelopmentConfig)

@mock.patch.dict(os.environ, {'FLASK_ENV': 'production'})
def test_config_type_auto_resolution_production(self):
self.assertIs(settings.ConfigType.Auto.config, settings.ProductionConfig)

@mock.patch.dict(os.environ, {'FLASK_ENV': 'testing'})
def test_config_type_auto_resolution_testing(self):
self.assertIs(settings.ConfigType.Auto.config, settings.TestingConfig)
45 changes: 45 additions & 0 deletions tests/test_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# -*- coding: utf-8 -*-
import unittest

import flask_login
from flask import url_for
from flask_testing import TestCase

from OpenLearn import settings, database
from OpenLearn.app import create_app
from OpenLearn.extensions import db


class BaseTestCase(TestCase):
def create_app(self):
app = create_app(config=settings.ConfigType.Testing)
app.testing = True
return app

def setUp(self):
db.create_all()
self.user = database.User(username="testUser", password="password@123")

def tearDown(self):
db.session.remove()
db.drop_all()


class TestPublicView(BaseTestCase):
def test_index_page(self):
response = self.client.get(url_for('public.index'))
self.assert200(response)
self.assertTrue(flask_login.current_user.is_anonymous)
self.assertTemplateUsed('index.html')

def test_register_page(self):
response = self.client.get(url_for('public.register'))
self.assert200(response)
self.assertTrue(flask_login.current_user.is_anonymous)
self.assertTemplateUsed('register.html')

def test_login_page(self):
response = self.client.get(url_for('public.login'))
self.assert200(response)
self.assertTrue(flask_login.current_user.is_anonymous)
self.assertTemplateUsed('login.html')

0 comments on commit 1035c00

Please sign in to comment.