sh4nks 11 лет назад
Родитель
Сommit
013b752de3

+ 2 - 0
flaskbb/configs/testing.py

@@ -23,6 +23,8 @@ class TestingConfig(DefaultConfig):
         'sqlite://'
         'sqlite://'
     )
     )
 
 
+    SERVER_NAME = "localhost:5000"
+
     # This will print all SQL statements
     # This will print all SQL statements
     SQLALCHEMY_ECHO = False
     SQLALCHEMY_ECHO = False
 
 

+ 3 - 4
flaskbb/forum/models.py

@@ -141,8 +141,7 @@ class Post(db.Model):
                          db.ForeignKey("topics.id",
                          db.ForeignKey("topics.id",
                                        use_alter=True,
                                        use_alter=True,
                                        name="fk_post_topic_id",
                                        name="fk_post_topic_id",
-                                       ondelete="CASCADE"),
-                         nullable=False)
+                                       ondelete="CASCADE"))
     user_id = db.Column(db.Integer, db.ForeignKey("users.id"), nullable=False)
     user_id = db.Column(db.Integer, db.ForeignKey("users.id"), nullable=False)
     username = db.Column(db.String(15), nullable=False)
     username = db.Column(db.String(15), nullable=False)
     content = db.Column(db.Text, nullable=False)
     content = db.Column(db.Text, nullable=False)
@@ -579,8 +578,8 @@ class Forum(db.Model):
         return "<{} {}>".format(self.__class__.__name__, self.id)
         return "<{} {}>".format(self.__class__.__name__, self.id)
 
 
     def update_last_post(self):
     def update_last_post(self):
-        """Updates the last post. This is useful if you move a topic
-        in another forum
+        """Updates the last post in the forum.
+        This is useful if you move a topic in another forum.
         """
         """
         last_post = Post.query.\
         last_post = Post.query.\
             filter(Post.topic_id == Topic.id,
             filter(Post.topic_id == Topic.id,

+ 2 - 0
tests/conftest.py

@@ -1 +1,3 @@
 from tests.fixtures.app import *
 from tests.fixtures.app import *
+from tests.fixtures.forum import *
+from tests.fixtures.user import *

+ 31 - 0
tests/fixtures/forum.py

@@ -0,0 +1,31 @@
+import pytest
+
+from flaskbb.forum.models import Forum, Category, Topic, Post
+
+
+@pytest.fixture
+def category(database):
+    category = Category(title="Test Category")
+    category.save()
+    return category
+
+
+@pytest.fixture
+def forum(category):
+    forum = Forum(title="Test Forum", category_id=category.id)
+    forum.save()
+    return forum
+
+
+@pytest.fixture
+def topic_moderator(forum, moderator_user):
+    topic = Topic(title="Test Topic Moderator")
+    post = Post(content="Test Content Moderator")
+    return topic.save(forum=forum, user=moderator_user, post=post)
+
+
+@pytest.fixture
+def topic_normal(forum, normal_user):
+    topic = Topic(title="Test Topic Normal")
+    post = Post(content="Test Content Normal")
+    return topic.save(forum=forum, user=normal_user, post=post)

+ 43 - 0
tests/fixtures/user.py

@@ -0,0 +1,43 @@
+import pytest
+
+from flaskbb.user.models import User
+
+
+@pytest.fixture
+def moderator_user(forum, default_groups):
+    """Creates a test user for whom the permissions should be checked"""
+
+    user = User(username="test_mod", email="test_mod@example.org",
+                password="test", primary_group_id=default_groups[2].id)
+    user.save()
+
+    forum.moderators.append(user)
+    forum.save()
+    return user
+
+
+@pytest.fixture
+def normal_user(default_groups):
+    """Creates a user with normal permissions"""
+    user = User(username="test_normal", email="test_normal@example.org",
+                password="test", primary_group_id=default_groups[3].id)
+    user.save()
+    return user
+
+
+@pytest.fixture
+def admin_user(default_groups):
+    """Creates a admin user"""
+    user = User(username="test_admin", email="test_admin@example.org",
+                password="test", primary_group_id=default_groups[0].id)
+    user.save()
+    return user
+
+
+@pytest.fixture
+def super_moderator_user(default_groups):
+    """Creates a super moderator user"""
+    user = User(username="test_super_mod", email="test_super@example.org",
+                password="test", primary_group_id=default_groups[1].id)
+    user.save()
+    return user

+ 167 - 0
tests/unit/test_forum_models.py

@@ -0,0 +1,167 @@
+from flaskbb.forum.models import Category, Forum, Topic, Post
+from flaskbb.user.models import User
+
+
+def test_category_save(database):
+    category = Category(title="Test Category")
+    category.save()
+
+    assert category.title == "Test Category"
+
+
+def test_category_delete(category):
+    # TODO: Test user post counts
+    category.delete()
+
+    category = Category.query.filter_by(id=category.id).first()
+
+    assert category is None
+
+
+def test_category_delete_with_forum(forum):
+    forum.category.delete()
+
+    assert forum is not None
+    assert forum.category is not None
+
+    category = Category.query.filter_by(id=forum.category.id).first()
+    forum = Forum.query.filter_by(id=forum.id).first()
+
+    assert forum is None
+    assert category is None
+
+
+def test_forum_save(category, moderator_user):
+    forum = Forum(title="Test Forum", category_id=category.id)
+    forum.save()
+
+    assert forum.title == "Test Forum"
+
+    # Test with adding a moderator
+    forum.save([moderator_user])
+
+    for moderator in forum.moderators:
+        assert moderator == moderator_user
+
+
+def test_forum_delete(forum):
+    # TODO: Test user post counts
+    forum.delete()
+
+    forum = Forum.query.filter_by(id=forum.id).first()
+
+    assert forum is None
+
+
+def test_forum_delete_with_user(topic_normal, normal_user):
+
+    assert normal_user.post_count == 1
+
+    topic_normal.forum.delete([normal_user])
+
+    forum = Forum.query.filter_by(id=topic_normal.forum_id).first()
+
+    assert forum is None
+
+    assert normal_user.post_count == 0
+
+
+def test_forum_update_last_post(topic_normal, normal_user):
+    post = Post(content="Test Content 2")
+    post.save(topic=topic_normal, user=normal_user)
+
+    assert topic_normal.forum.last_post == post
+
+    post.delete()
+
+    topic_normal.forum.update_last_post()
+
+    assert topic_normal.forum.last_post == topic_normal.first_post
+
+
+def test_forum_url(forum):
+    assert forum.url == "http://localhost:5000/forum/1-test-forum"
+
+
+def test_forum_slugify(forum):
+    assert forum.slug == "test-forum"
+
+
+def test_topic_save(forum, normal_user):
+    post = Post(content="Test Content")
+    topic = Topic(title="Test Title")
+
+    assert forum.last_post_id is None
+    assert forum.post_count == 0
+    assert forum.topic_count == 0
+
+    topic.save(forum=forum, post=post, user=normal_user)
+
+    assert topic.title == "Test Title"
+
+    # The first post in the topic is also the last post
+    assert topic.first_post_id == post.id
+    assert topic.last_post_id == post.id
+
+    assert forum.last_post_id == post.id
+    assert forum.post_count == 1
+    assert forum.topic_count == 1
+
+
+def test_topic_delete(topic_normal):
+    assert topic_normal.user.post_count == 1
+    assert topic_normal.post_count == 1
+    assert topic_normal.forum.topic_count == 1
+    assert topic_normal.forum.post_count == 1
+
+    topic_normal.delete(users=[topic_normal.user])
+
+    forum = Forum.query.filter_by(id=topic_normal.forum_id).first()
+    user = User.query.filter_by(id=topic_normal.user_id).first()
+    topic_normal = Topic.query.filter_by(id=topic_normal.id).first()
+
+    assert topic_normal is None
+    assert user.post_count == 0
+    assert forum.topic_count == 0
+    assert forum.post_count == 0
+
+
+def test_topic_merge(topic_normal, topic_moderator):
+    assert topic_normal.merge(topic_moderator)
+
+    topic_normal = Topic.query.filter_by(id=topic_normal.id).first()
+    assert topic_normal is None
+    assert topic_moderator.post_count == 2
+
+
+def test_topic_merge_other_forum(topic_normal):
+    """You cannot merge a topic with a topic from another forum"""
+    forum_other = Forum(title="Test Forum 2", category_id=1)
+    forum_other.save()
+
+    topic_other = Topic(title="Test Topic 2")
+    post_other = Post(content="Test Content 2")
+    topic_other.save(user=topic_normal.user, forum=forum_other, post=post_other)
+
+    assert not topic_normal.merge(topic_other)
+
+
+def test_topic_move(topic_normal):
+    forum_other = Forum(title="Test Forum 2", category_id=1)
+    forum_other.save()
+
+    forum_old = Forum.query.filter_by(id=topic_normal.forum_id).first()
+
+    assert topic_normal.move(forum_other)
+
+    assert forum_old.topics == []
+
+    assert forum_old.topic_count == 0
+    assert forum_old.post_count == 0
+
+    assert forum_other.topic_count == 1
+    assert forum_other.post_count == 1
+
+
+def test_topic_move_same_forum(topic_normal):
+    assert not topic_normal.move(topic_normal.forum)

+ 1 - 58
tests/unit/utils/test_permissions.py

@@ -4,68 +4,11 @@
 """
 """
 import pytest
 import pytest
 
 
-from flaskbb.user.models import User
-from flaskbb.forum.models import Forum, Category, Topic, Post
+from flaskbb.forum.models import Topic, Post
 from flaskbb.utils.permissions import *
 from flaskbb.utils.permissions import *
 
 
 
 
 @pytest.fixture
 @pytest.fixture
-def category(database):
-    category = Category(title="Test Category")
-    category.save()
-    return category
-
-
-@pytest.fixture
-def forum(category):
-    forum = Forum(title="Test Forum", category_id=category.id)
-    forum.save()
-    return forum
-
-
-@pytest.fixture
-def moderator_user(forum, default_groups):
-    """Creates a test user for whom the permissions should be checked"""
-
-    # This should be moved to own fixture which you then use to link the user
-    # to the forum
-    user = User(username="test_mod", email="test_mod@example.org",
-                password="test", primary_group_id=default_groups[2].id)
-    user.save()
-
-    forum.moderators.append(user)
-    forum.save()
-    return user
-
-
-@pytest.fixture
-def normal_user(default_groups):
-    """Creates a user with normal permissions"""
-    user = User(username="test_normal", email="test_normal@example.org",
-                password="test", primary_group_id=default_groups[3].id)
-    user.save()
-    return user
-
-
-@pytest.fixture
-def admin_user(default_groups):
-    """Creates a admin user"""
-    user = User(username="test_admin", email="test_admin@example.org",
-                password="test", primary_group_id=default_groups[0].id)
-    user.save()
-    return user
-
-
-@pytest.fixture
-def super_moderator_user(default_groups):
-    """Creates a super moderator user"""
-    user = User(username="test_super_mod", email="test_super@example.org",
-                password="test", primary_group_id=default_groups[1].id)
-    user.save()
-    return user
-
-
-@pytest.fixture
 def topic_moderator(forum, moderator_user):
 def topic_moderator(forum, moderator_user):
     topic = Topic(title="Test Topic Moderator")
     topic = Topic(title="Test Topic Moderator")
     post = Post(content="Test Content Moderator")
     post = Post(content="Test Content Moderator")