Files
interchan/tests/test_posts.py
Alek Ratzloff d3e1f5a978 Add board locking, update tests, and update clean() methods
* Boards can be locked from allowing posts - this is can be useful for
  things like archived boards or locking down in the event of an
  emergency
* Some validation checks for new posts are from the reply/thread form to
  the Post model's clean() method
* Add some new tests, I've really been falling behind on those

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2022-07-12 18:30:16 -07:00

121 lines
4.7 KiB
Python

from datetime import timedelta
from django.core.exceptions import ValidationError
from django.test import TestCase
from board.forms import PostForm, ReplyForm
from board.models import Board, Post
class BumpTestCase(TestCase):
def setUp(self):
Board.objects.create(
url="test", name="test", post_cooldown=timedelta(seconds=0)
)
def test_bumping(self):
board = Board.objects.get(url="test")
self.assertEquals(board.threads.count(), 0)
post1 = Post.objects.create(board=board, text="test 1", ip="127.0.0.1")
post2 = Post.objects.create(board=board, text="test 2", ip="127.0.0.1")
post3 = Post.objects.create(board=board, text="test 3", ip="127.0.0.1")
post4 = Post.objects.create(board=board, text="test 4", ip="127.0.0.1")
self.assertEquals(
list(board.threads.order_by("-last_bump")), [post4, post3, post2, post1]
)
Post.objects.create(board=board, text="bump", ip="127.0.0.1", op=post3)
self.assertEquals(
list(board.threads.order_by("-last_bump")), [post3, post4, post2, post1]
)
Post.objects.create(board=board, text="bump", ip="127.0.0.1", op=post2)
self.assertEquals(
list(board.threads.order_by("-last_bump")), [post2, post3, post4, post1]
)
Post.objects.create(board=board, text="bump", ip="127.0.0.1", op=post1)
self.assertEquals(
list(board.threads.order_by("-last_bump")), [post1, post2, post3, post4]
)
def test_no_bumping(self):
board = Board.objects.get(url="test")
self.assertEquals(board.threads.count(), 0)
board = Board.objects.get(url="test")
self.assertEquals(board.threads.count(), 0)
post1 = Post.objects.create(board=board, text="test 1", ip="127.0.0.1")
post2 = Post.objects.create(board=board, text="test 2", ip="127.0.0.1")
post3 = Post.objects.create(board=board, text="test 3", ip="127.0.0.1")
post4 = Post.objects.create(board=board, text="test 4", ip="127.0.0.1")
self.assertEquals(
list(board.threads.order_by("-last_bump")), [post4, post3, post2, post1]
)
Post.objects.create(
board=board, text="bump", ip="127.0.0.1", op=post3, bump=False
)
self.assertEquals(
list(board.threads.order_by("-last_bump")), [post4, post3, post2, post1]
)
# thread that has been sunk
post2.bump = False
Post.objects.create(
board=board, text="bump", ip="127.0.0.1", op=post2, bump=True
)
self.assertEquals(
list(board.threads.order_by("-last_bump")), [post4, post3, post2, post1]
)
def test_thread_sticky(self):
board = Board.objects.get(url="test")
self.assertEquals(board.threads.count(), 0)
board = Board.objects.get(url="test")
self.assertEquals(board.threads.count(), 0)
post1 = Post.objects.create(
board=board, text="test 1", ip="127.0.0.1", sticky=True
)
post2 = Post.objects.create(board=board, text="test 2", ip="127.0.0.1")
post3 = Post.objects.create(board=board, text="test 3", ip="127.0.0.1")
post4 = Post.objects.create(board=board, text="test 4", ip="127.0.0.1")
self.assertEquals(
list(board.threads.order_by("-sticky", "-last_bump")),
[post1, post4, post3, post2],
)
Post.objects.create(board=board, text="bump", ip="127.0.0.1", op=post3)
self.assertEquals(
list(board.threads.order_by("-sticky", "-last_bump")),
[post1, post3, post4, post2],
)
def test_thread_lock(self):
board = Board.objects.get(url="test")
op = Post.objects.create(board=board, text="test 1", ip="127.0.0.1", lock=True)
reply = Post(board=board, text="reply 1", op=op, ip="127.0.0.1")
self.assertRaises(ValidationError, reply.full_clean)
# Also, make sure that thread locks work after new posts are made
op = Post.objects.create(board=board, text="test 1", ip="127.0.0.1")
reply1 = Post(board=board, text="reply 1", op=op, ip="127.0.0.1")
reply1.full_clean() # should not raise
op.lock = True
reply2 = Post(board=board, text="reply 2", op=op, ip="127.0.0.2")
self.assertRaises(ValidationError, reply2.full_clean)
class BoardReadonlyTestCase(TestCase):
def setUp(self):
Board.objects.create(
url="test", name="test", post_cooldown=timedelta(seconds=0), readonly=True
)
def test_board_readonly(self):
board = Board.objects.get(url="test")
op = Post(board=board, text="test 1", ip="127.0.0.1")
self.assertRaises(ValidationError, op.full_clean)