tests.py
88 lines
| 2.2 KiB
| text/x-python
|
PythonLexer
/ boards / tests.py
Ilyas
|
r13 | from django.utils.unittest import TestCase | ||
from django.test.client import Client | ||||
neko259
|
r7 | import boards | ||
neko259
|
r0 | |||
Ilyas
|
r13 | from boards.models import Post, Admin | ||
neko259
|
r0 | |||
neko259
|
r8 | |||
neko259
|
r0 | class BoardTests(TestCase): | ||
neko259
|
r16 | def _create_post(self): | ||
neko259
|
r5 | return Post.objects.create_post(title = 'title', | ||
neko259
|
r7 | text = 'text') | ||
neko259
|
r0 | |||
def test_post_add(self): | ||||
neko259
|
r16 | post = self._create_post() | ||
neko259
|
r0 | |||
neko259
|
r1 | self.assertIsNotNone(post) | ||
neko259
|
r7 | self.assertEqual(boards.models.NO_PARENT, post.parent) | ||
neko259
|
r0 | |||
def test_delete_post(self): | ||||
neko259
|
r16 | post = self._create_post() | ||
neko259
|
r8 | post_id = post.id | ||
neko259
|
r0 | |||
neko259
|
r1 | Post.objects.delete_post(post) | ||
neko259
|
r0 | |||
neko259
|
r16 | self.assertFalse(Post.objects.exists(post_id)) | ||
neko259
|
r0 | |||
def test_delete_posts_by_ip(self): | ||||
neko259
|
r16 | post = self._create_post() | ||
neko259
|
r8 | post_id = post.id | ||
neko259
|
r0 | |||
neko259
|
r1 | Post.objects.delete_posts_by_ip('0.0.0.0') | ||
neko259
|
r0 | |||
neko259
|
r16 | self.assertFalse(Post.objects.exists(post_id)) | ||
Ilyas
|
r13 | |||
# Authentication tests | ||||
def _create_test_user(self): | ||||
admin = Admin(name = 'test_username12313584353165', | ||||
password = 'test_userpassword135135512') | ||||
admin.save() | ||||
return admin | ||||
def test_admin_login(self): | ||||
client = Client() | ||||
self.assertFalse('admin' in client.session) | ||||
admin = self._create_test_user() | ||||
response = client.post('/boards/login', | ||||
{'name': admin.name, 'password': admin.password}) | ||||
# it means that login passed and user are redirected to another page | ||||
self.assertEqual(302, response.status_code) | ||||
self.assertTrue('admin' in client.session) | ||||
self.assertTrue(client.session['admin']) | ||||
admin.delete() | ||||
wrong_name = 'sd2f1s3d21fs3d21f' | ||||
wrong_password = 'sd2f1s3d21fs3d21fsdfsd' | ||||
client.post('/boards/login', {'name': wrong_name, 'password': wrong_password}) | ||||
self.assertFalse(client.session['admin']) | ||||
def test_admin_logout(self): | ||||
client = Client() | ||||
self.assertFalse('admin' in client.session) | ||||
admin = self._create_test_user() | ||||
client.post('/boards/login', | ||||
{'name': admin.name, 'password': admin.password}) | ||||
self.assertTrue(client.session['admin']) | ||||
client.get('/boards/logout') | ||||
self.assertFalse(client.session['admin']) | ||||
admin.delete() | ||||