Browse Source

Merge pull request #31 from hvdklauw/add_pytest

Added test framework
sh4nks 11 years ago
parent
commit
f3a84767d7

+ 62 - 0
flaskbb/configs/testing.py

@@ -0,0 +1,62 @@
+"""
+    flaskbb.configs.testing
+    ~~~~~~~~~~~~~~~~~~~~
+
+    This is the FlaskBB's testing config.
+
+    :copyright: (c) 2014 by the FlaskBB Team.
+    :license: BSD, see LICENSE for more details.
+"""
+from flaskbb.configs.default import DefaultConfig
+
+
+class TestingConfig(DefaultConfig):
+
+    # Indicates that it is a testing environment
+    DEBUG = False
+    TESTING = True
+
+    # SQLAlchemy connection options
+    # This will create in the applications folder (where manage.py is)
+    # a database named flaskbb.sqlite.
+    SQLALCHEMY_DATABASE_URI = (
+        'sqlite:///' + DefaultConfig._basedir + '/' + 'flaskbb_testing.sqlite'
+    )
+
+    # This will print all SQL statements
+    SQLALCHEMY_ECHO = False
+
+    # Security
+    SECRET_KEY = "SecretKeyForSessionSigning"
+    WTF_CSRF_ENABLED = True
+    WTF_CSRF_SECRET_KEY = "reallyhardtoguess"
+
+    # Recaptcha
+    # To get recaptcha, visit the link below:
+    # https://www.google.com/recaptcha/admin/create
+    # Those keys are only going to work on localhost!
+    RECAPTCHA_ENABLED = True
+    RECAPTCHA_USE_SSL = False
+    RECAPTCHA_PUBLIC_KEY = "6LcZB-0SAAAAAGIddBuSFU9aBpHKDa16p5gSqnxK"
+    RECAPTCHA_PRIVATE_KEY = "6LcZB-0SAAAAAPuPHhazscMJYa2mBe7MJSoWXrUu"
+    RECAPTCHA_OPTIONS = {"theme": "white"}
+
+    # Mail
+    # Local SMTP Server
+    #MAIL_SERVER = "localhost"
+    #MAIL_PORT = 25
+    #MAIL_USE_SSL = False
+    #MAIL_USERNAME = ""
+    #MAIL_PASSWORD = ""
+    #MAIL_DEFAULT_SENDER = "noreply@example.org"
+
+    # Google Mail Example
+    MAIL_SERVER = "smtp.gmail.com"
+    MAIL_PORT = 465
+    MAIL_USE_SSL = True
+    MAIL_USERNAME = "your_username@gmail.com"
+    MAIL_PASSWORD = "your_password"
+    MAIL_DEFAULT_SENDER = ("Your Name", "your_username@gmail.com")
+
+    # The user who should recieve the error logs
+    ADMINS = ["your_admin_user@gmail.com"]

+ 9 - 18
flaskbb/utils/populate.py

@@ -30,9 +30,7 @@ GROUPS = OrderedDict((
         'postreply': True,
         'locktopic': True,
         'movetopic': True,
-        'mergetopic': True,
-        'viewtopic': True,
-        'viewprofile': True
+        'mergetopic': True
     }),
     ('Super Moderator', {
         'description': 'The Super Moderator Group',
@@ -48,9 +46,7 @@ GROUPS = OrderedDict((
         'postreply': True,
         'locktopic': True,
         'movetopic': True,
-        'mergetopic': True,
-        'viewtopic': True,
-        'viewprofiles': True
+        'mergetopic': True
     }),
     ('Moderator', {
         'description': 'The Moderator Group',
@@ -66,9 +62,7 @@ GROUPS = OrderedDict((
         'postreply': True,
         'locktopic': True,
         'movetopic': True,
-        'mergetopic': True,
-        'viewtopic': True,
-        'viewprofile': True
+        'mergetopic': True
     }),
     ('Member', {
         'description': 'The Member Group',
@@ -84,9 +78,7 @@ GROUPS = OrderedDict((
         'postreply': True,
         'locktopic': False,
         'movetopic': False,
-        'mergetopic': False,
-        'viewtopic': True,
-        'viewprofile': True
+        'mergetopic': False
     }),
     ('Banned', {
         'description': 'The Banned Group',
@@ -102,9 +94,7 @@ GROUPS = OrderedDict((
         'postreply': False,
         'locktopic': False,
         'movetopic': False,
-        'mergetopic': False,
-        'viewtopic': False,
-        'viewprofile': False
+        'mergetopic': False
     }),
     ('Guest', {
         'description': 'The Guest Group',
@@ -120,9 +110,7 @@ GROUPS = OrderedDict((
         'postreply': False,
         'locktopic': False,
         'movetopic': False,
-        'mergetopic': False,
-        'viewtopic': False,
-        'viewprofile': False
+        'mergetopic': False
     })
 ))
 
@@ -131,6 +119,7 @@ def create_default_groups():
     """
     This will create the 5 default groups
     """
+    result = []
     for key, value in GROUPS.items():
         group = Group(name=key)
 
@@ -138,6 +127,8 @@ def create_default_groups():
             setattr(group, k, v)
 
         group.save()
+        result.append(group)
+    return result
 
 
 def create_admin_user(username, password, email):

+ 1 - 1
flaskbb/utils/widgets.py

@@ -28,7 +28,7 @@ class SelectDateWidget(object):
         '%Y': 'select_date_year'
     }
 
-    def __init__(self, years=range(1930, datetime.utcnow().year+1)):
+    def __init__(self, years=range(1930, datetime.utcnow().year + 1)):
         super(SelectDateWidget, self).__init__()
         self.FORMAT_CHOICES['%Y'] = [(x, str(x)) for x in years]
 

+ 1 - 1
manage.py

@@ -16,7 +16,7 @@ from flask import current_app
 from sqlalchemy.exc import IntegrityError, OperationalError
 from flask.ext.script import (Manager, Shell, Server, prompt, prompt_pass,
                               prompt_bool)
-from flask.ext.migrate import MigrateCommand, upgrade as db_upgrade
+from flask.ext.migrate import MigrateCommand
 
 from flaskbb import create_app
 from flaskbb.extensions import db

+ 3 - 0
pytest.ini

@@ -0,0 +1,3 @@
+[pytest]
+norecursedirs = docs flaskbb logs migrations whoosh_index
+addopts = --strict --random -vvl

+ 6 - 0
requirements.txt

@@ -23,5 +23,11 @@ itsdangerous==0.23
 redis==2.9.1
 simplejson==3.3.3
 wsgiref==0.1.2
+
+
 https://github.com/miguelgrinberg/Flask-WhooshAlchemy/tarball/master#egg=Flask-WhooshAlchemy
 https://github.com/frol/postmarkup/tarball/master#egg=postmarkup
+
+# Testing
+pytest==2.5.2
+pytest-random==0.02

+ 0 - 0
tests/__init__.py


+ 1 - 0
tests/conftest.py

@@ -0,0 +1 @@
+from tests.fixtures.app import *

+ 0 - 0
tests/fixtures/__init__.py


+ 35 - 0
tests/fixtures/app.py

@@ -0,0 +1,35 @@
+import pytest
+
+from flaskbb import create_app
+from flaskbb.extensions import db
+from flaskbb.configs.testing import TestingConfig as Config
+from flaskbb.utils.populate import create_default_groups
+
+
+@pytest.yield_fixture(autouse=True)
+def application():
+    """application with context."""
+    app = create_app(Config)
+
+    ctx = app.app_context()
+    ctx.push()
+
+    yield app
+
+    ctx.pop()
+
+
+@pytest.fixture()
+def default_groups():
+    """Creates the default groups"""
+    return create_default_groups()
+
+
+@pytest.yield_fixture()
+def database(default_groups):
+    """database setup."""
+    db.create_all()  # Maybe use migration instead?
+
+    yield db
+
+    db.drop_all()

+ 0 - 0
tests/unit/__init__.py


+ 0 - 0
tests/unit/utils/__init__.py


+ 10 - 0
tests/unit/utils/test_helpers.py

@@ -0,0 +1,10 @@
+#-*- coding: utf-8 -*-
+from flaskbb.utils.helpers import slugify
+
+
+def test_slugify():
+    """Test the slugify helper method."""
+
+    assert slugify(u'Hello world') == u'hello-world'
+
+    assert slugify(u'¿Cómo está?') == u'como-esta'

+ 26 - 0
tests/unit/utils/test_populate.py

@@ -0,0 +1,26 @@
+import pytest
+
+from flaskbb.utils.populate import create_default_groups, GROUPS
+from flaskbb.user.models import Group
+
+
+@pytest.fixture()
+def default_groups():
+    """Overwrite for the default fixture, in this case we don't want the default groups created."""
+    return []
+
+
+def test_create_default_groups(database):
+    """Test that the default groups are created correctly."""
+
+    assert Group.query.count() == 0
+
+    create_default_groups()
+
+    assert Group.query.count() == len(GROUPS)
+
+    for key, attributes in GROUPS.items():
+        group = Group.query.filter_by(name=key).first()
+
+        for attribute, value in attributes.items():
+            assert getattr(group, attribute) == value