| import unittest |
| from app import app, db, Board, Post, BannedIP, prune_board |
|
|
| class GhostBoardTestCase(unittest.TestCase): |
| def setUp(self): |
| app.config['TESTING'] = True |
| app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:' |
| app.config['WTF_CSRF_ENABLED'] = False |
| self.app = app.test_client() |
| with app.app_context(): |
| db.create_all() |
| b = Board(slug='tst', name='Test Board', description='Testing') |
| db.session.add(b) |
| db.session.commit() |
|
|
| def tearDown(self): |
| with app.app_context(): |
| db.session.remove() |
| db.drop_all() |
|
|
| def test_board_exists(self): |
| response = self.app.get('/tst/') |
| self.assertEqual(response.status_code, 200) |
| self.assertIn(b'Test Board', response.data) |
|
|
| def test_create_thread(self): |
| with app.app_context(): |
| |
| response = self.app.post('/tst/', data=dict( |
| content='Test Thread Content' |
| ), follow_redirects=True) |
| self.assertEqual(response.status_code, 200) |
| self.assertIn(b'Test Thread Content', response.data) |
| |
| post = Post.query.first() |
| self.assertIsNotNone(post) |
| self.assertIsNone(post.thread_id) |
|
|
| def test_pruning(self): |
| with app.app_context(): |
| board = Board.query.filter_by(slug='tst').first() |
| |
| for i in range(101): |
| p = Post(board_id=board.id, content=f'Thread {i}') |
| db.session.add(p) |
| db.session.commit() |
| |
| self.assertEqual(Post.query.count(), 101) |
| prune_board(board.id) |
| self.assertEqual(Post.query.count(), 100) |
|
|
| def test_ban_check(self): |
| with app.app_context(): |
| banned = BannedIP(ip_address='127.0.0.1', reason='Test Ban') |
| db.session.add(banned) |
| db.session.commit() |
| |
| response = self.app.post('/tst/', data=dict( |
| content='Should fail' |
| )) |
| self.assertEqual(response.status_code, 403) |
|
|
| if __name__ == '__main__': |
| unittest.main() |
|
|