##// END OF EJS Templates
Fixed validation test.
neko259 -
r180:c2ede590 default
parent child Browse files
Show More
@@ -1,172 +1,173 b''
1 # coding=utf-8
1 # coding=utf-8
2 from django.utils.unittest import TestCase
2 from django.utils.unittest import TestCase
3 from django.test.client import Client
3 from django.test.client import Client
4 import time
4
5
5 import boards
6 import boards
6
7
7 from boards.models import Post, Tag
8 from boards.models import Post, Tag
8 from neboard import settings
9 from neboard import settings
9
10
10 TEST_TEXT = 'test text'
11 TEST_TEXT = 'test text'
11
12
12 NEW_THREAD_PAGE = '/'
13 NEW_THREAD_PAGE = '/'
13 THREAD_PAGE_ONE = '/thread/1/'
14 THREAD_PAGE_ONE = '/thread/1/'
14 THREAD_PAGE = '/thread/'
15 THREAD_PAGE = '/thread/'
15 TAG_PAGE = '/tag/'
16 TAG_PAGE = '/tag/'
16 HTTP_CODE_REDIRECT = 302
17 HTTP_CODE_REDIRECT = 302
17 HTTP_CODE_OK = 200
18 HTTP_CODE_OK = 200
18 HTTP_CODE_NOT_FOUND = 404
19 HTTP_CODE_NOT_FOUND = 404
19
20
20
21
21 class BoardTests(TestCase):
22 class BoardTests(TestCase):
22 def _create_post(self):
23 def _create_post(self):
23 return Post.objects.create_post(title='title',
24 return Post.objects.create_post(title='title',
24 text='text')
25 text='text')
25
26
26 def test_post_add(self):
27 def test_post_add(self):
27 post = self._create_post()
28 post = self._create_post()
28
29
29 self.assertIsNotNone(post)
30 self.assertIsNotNone(post)
30 self.assertEqual(boards.models.NO_PARENT, post.parent)
31 self.assertEqual(boards.models.NO_PARENT, post.parent)
31
32
32 def test_delete_post(self):
33 def test_delete_post(self):
33 post = self._create_post()
34 post = self._create_post()
34 post_id = post.id
35 post_id = post.id
35
36
36 Post.objects.delete_post(post)
37 Post.objects.delete_post(post)
37
38
38 self.assertFalse(Post.objects.exists(post_id))
39 self.assertFalse(Post.objects.exists(post_id))
39
40
40 def test_delete_posts_by_ip(self):
41 def test_delete_posts_by_ip(self):
41 post = self._create_post()
42 post = self._create_post()
42 post_id = post.id
43 post_id = post.id
43
44
44 Post.objects.delete_posts_by_ip('0.0.0.0')
45 Post.objects.delete_posts_by_ip('0.0.0.0')
45
46
46 self.assertFalse(Post.objects.exists(post_id))
47 self.assertFalse(Post.objects.exists(post_id))
47
48
48 # Authentication tests
49 # Authentication tests
49
50
50 def test_get_thread(self):
51 def test_get_thread(self):
51 opening_post = self._create_post()
52 opening_post = self._create_post()
52
53
53 for i in range(0, 2):
54 for i in range(0, 2):
54 Post.objects.create_post('title', 'text',thread=opening_post)
55 Post.objects.create_post('title', 'text',thread=opening_post)
55
56
56 thread = Post.objects.get_thread(opening_post.id)
57 thread = Post.objects.get_thread(opening_post.id)
57
58
58 self.assertEqual(3, len(thread))
59 self.assertEqual(3, len(thread))
59
60
60 def test_create_post_with_tag(self):
61 def test_create_post_with_tag(self):
61 tag = Tag.objects.create(name='test_tag')
62 tag = Tag.objects.create(name='test_tag')
62 post = Post.objects.create_post(title='title', text='text', tags=[tag])
63 post = Post.objects.create_post(title='title', text='text', tags=[tag])
63 self.assertIsNotNone(post)
64 self.assertIsNotNone(post)
64
65
65 def test_thread_max_count(self):
66 def test_thread_max_count(self):
66 for i in range(settings.MAX_THREAD_COUNT + 1):
67 for i in range(settings.MAX_THREAD_COUNT + 1):
67 self._create_post()
68 self._create_post()
68
69
69 self.assertEqual(settings.MAX_THREAD_COUNT,
70 self.assertEqual(settings.MAX_THREAD_COUNT,
70 len(Post.objects.get_threads()))
71 len(Post.objects.get_threads()))
71
72
72 def test_pages(self):
73 def test_pages(self):
73 """Test that the thread list is properly split into pages"""
74 """Test that the thread list is properly split into pages"""
74
75
75 for i in range(settings.MAX_THREAD_COUNT):
76 for i in range(settings.MAX_THREAD_COUNT):
76 self._create_post()
77 self._create_post()
77
78
78 all_threads = Post.objects.get_threads()
79 all_threads = Post.objects.get_threads()
79
80
80 posts_in_second_page = Post.objects.get_threads(page=1)
81 posts_in_second_page = Post.objects.get_threads(page=1)
81 first_post = posts_in_second_page[0]
82 first_post = posts_in_second_page[0]
82
83
83 self.assertEqual(all_threads[settings.THREADS_PER_PAGE].id,
84 self.assertEqual(all_threads[settings.THREADS_PER_PAGE].id,
84 first_post.id)
85 first_post.id)
85
86
86 def test_post_validation(self):
87 def test_post_validation(self):
87 """Test the validation of the post form"""
88 """Test the validation of the post form"""
88
89
89 # Disable captcha for the test
90 # Disable captcha for the test
90 captcha_enabled = settings.ENABLE_CAPTCHA
91 captcha_enabled = settings.ENABLE_CAPTCHA
91 settings.ENABLE_CAPTCHA = False
92 settings.ENABLE_CAPTCHA = False
92
93
93 Post.objects.all().delete()
94 Post.objects.all().delete()
94
95
95 client = Client()
96 client = Client()
96
97
97 valid_tags = u'tag1 tag_2 Ρ‚Π΅Π³_3'
98 valid_tags = u'tag1 tag_2 Ρ‚Π΅Π³_3'
98 invalid_tags = u'$%_356 ---'
99 invalid_tags = u'$%_356 ---'
99
100
100 response = client.post(NEW_THREAD_PAGE, {'title': 'test title',
101 response = client.post(NEW_THREAD_PAGE, {'title': 'test title',
101 'text': TEST_TEXT,
102 'text': TEST_TEXT,
102 'tags': valid_tags})
103 'tags': valid_tags})
103 self.assertEqual(response.status_code, HTTP_CODE_REDIRECT,
104 self.assertEqual(response.status_code, HTTP_CODE_REDIRECT,
104 msg='Posting new message failed: got code ' +
105 msg='Posting new message failed: got code ' +
105 str(response.status_code))
106 str(response.status_code))
106
107
107 self.assertEqual(1, Post.objects.count(),
108 self.assertEqual(1, Post.objects.count(),
108 msg='No posts were created')
109 msg='No posts were created')
109
110
110 client.post(NEW_THREAD_PAGE, {'text': TEST_TEXT,
111 client.post(NEW_THREAD_PAGE, {'text': TEST_TEXT,
111 'tags': invalid_tags})
112 'tags': invalid_tags})
112 self.assertEqual(1, Post.objects.count(), msg='The validation passed '
113 self.assertEqual(1, Post.objects.count(), msg='The validation passed '
113 'where it should fail')
114 'where it should fail')
114
115
115 # TODO Some workaround and test for the "waiting" validation should
116 # Wait fot the posting delay or we won't be able to post
116 # exist here
117 time.sleep(settings.POSTING_DELAY + 1)
117 response = client.post(THREAD_PAGE_ONE, {'text': TEST_TEXT,
118 response = client.post(THREAD_PAGE_ONE, {'text': TEST_TEXT,
118 'tags': valid_tags})
119 'tags': valid_tags})
119 self.assertEqual(HTTP_CODE_REDIRECT, response.status_code,
120 self.assertEqual(HTTP_CODE_REDIRECT, response.status_code,
120 msg=u'Posting new message failed: got code ' +
121 msg=u'Posting new message failed: got code ' +
121 str(response.status_code))
122 str(response.status_code))
122
123
123 self.assertEqual(2, Post.objects.count(),
124 self.assertEqual(2, Post.objects.count(),
124 msg=u'No posts were created')
125 msg=u'No posts were created')
125
126
126 # Restore captcha setting
127 # Restore captcha setting
127 settings.ENABLE_CAPTCHA = captcha_enabled
128 settings.ENABLE_CAPTCHA = captcha_enabled
128
129
129 # TODO This test fails for now. We must check for 404.html instead of
130 # TODO This test fails for now. We must check for 404.html instead of
130 # code 404
131 # code 404
131 def test_404(self):
132 def test_404(self):
132 """Test receiving error 404 when opening a non-existent page"""
133 """Test receiving error 404 when opening a non-existent page"""
133
134
134 Post.objects.all().delete()
135 Post.objects.all().delete()
135 Tag.objects.all().delete()
136 Tag.objects.all().delete()
136
137
137 tag_name = u'test_tag'
138 tag_name = u'test_tag'
138 tags, = [Tag.objects.get_or_create(name=tag_name)]
139 tags, = [Tag.objects.get_or_create(name=tag_name)]
139 client = Client()
140 client = Client()
140
141
141 Post.objects.create_post('title', TEST_TEXT, tags=tags)
142 Post.objects.create_post('title', TEST_TEXT, tags=tags)
142
143
143 existing_post_id = Post.objects.all()[0].id
144 existing_post_id = Post.objects.all()[0].id
144 response_existing = client.get(THREAD_PAGE + str(existing_post_id) +
145 response_existing = client.get(THREAD_PAGE + str(existing_post_id) +
145 '/')
146 '/')
146 self.assertEqual(HTTP_CODE_OK, response_existing.status_code,
147 self.assertEqual(HTTP_CODE_OK, response_existing.status_code,
147 u'Cannot open existing thread')
148 u'Cannot open existing thread')
148
149
149 response_not_existing = client.get(THREAD_PAGE + str(
150 response_not_existing = client.get(THREAD_PAGE + str(
150 existing_post_id + 1) + '/')
151 existing_post_id + 1) + '/')
151 response_not_existing.get_full_path()
152 response_not_existing.get_full_path()
152 self.assertEqual(HTTP_CODE_NOT_FOUND,
153 self.assertEqual(HTTP_CODE_NOT_FOUND,
153 response_not_existing.status_code,
154 response_not_existing.status_code,
154 u'Not existing thread is opened')
155 u'Not existing thread is opened')
155
156
156 response_existing = client.get(TAG_PAGE + tag_name + '/')
157 response_existing = client.get(TAG_PAGE + tag_name + '/')
157 self.assertEqual(HTTP_CODE_OK,
158 self.assertEqual(HTTP_CODE_OK,
158 response_existing.status_code,
159 response_existing.status_code,
159 u'Cannot open existing tag')
160 u'Cannot open existing tag')
160
161
161 response_not_existing = client.get(TAG_PAGE + u'not_tag' + '/')
162 response_not_existing = client.get(TAG_PAGE + u'not_tag' + '/')
162 self.assertEqual(HTTP_CODE_NOT_FOUND,
163 self.assertEqual(HTTP_CODE_NOT_FOUND,
163 response_not_existing.status_code,
164 response_not_existing.status_code,
164 u'Not existing tag is opened')
165 u'Not existing tag is opened')
165
166
166 reply_id = Post.objects.create_post('', TEST_TEXT,
167 reply_id = Post.objects.create_post('', TEST_TEXT,
167 parent_id=existing_post_id)
168 parent_id=existing_post_id)
168 response_not_existing = client.get(THREAD_PAGE + str(
169 response_not_existing = client.get(THREAD_PAGE + str(
169 reply_id) + '/')
170 reply_id) + '/')
170 self.assertEqual(HTTP_CODE_NOT_FOUND,
171 self.assertEqual(HTTP_CODE_NOT_FOUND,
171 response_not_existing.status_code,
172 response_not_existing.status_code,
172 u'Not existing thread is opened')
173 u'Not existing thread is opened')
General Comments 0
You need to be logged in to leave comments. Login now