##// END OF EJS Templates
Fixed pages test
neko259 -
r494:ec493e2b 1.6-dev
parent child Browse files
Show More
@@ -1,233 +1,233 b''
1 # coding=utf-8
1 # coding=utf-8
2 from django.test import TestCase
2 from django.test import TestCase
3 from django.test.client import Client
3 from django.test.client import Client
4 import time
4 import time
5
5
6 from boards.models import Post, Tag
6 from boards.models import Post, Tag
7 from neboard import settings
7 from neboard import settings
8
8
9 PAGE_404 = 'boards/404.html'
9 PAGE_404 = 'boards/404.html'
10
10
11 TEST_TEXT = 'test text'
11 TEST_TEXT = 'test text'
12
12
13 NEW_THREAD_PAGE = '/'
13 NEW_THREAD_PAGE = '/'
14 THREAD_PAGE_ONE = '/thread/1/'
14 THREAD_PAGE_ONE = '/thread/1/'
15 THREAD_PAGE = '/thread/'
15 THREAD_PAGE = '/thread/'
16 TAG_PAGE = '/tag/'
16 TAG_PAGE = '/tag/'
17 HTTP_CODE_REDIRECT = 302
17 HTTP_CODE_REDIRECT = 302
18 HTTP_CODE_OK = 200
18 HTTP_CODE_OK = 200
19 HTTP_CODE_NOT_FOUND = 404
19 HTTP_CODE_NOT_FOUND = 404
20
20
21
21
22 class PostTests(TestCase):
22 class PostTests(TestCase):
23
23
24 def _create_post(self):
24 def _create_post(self):
25 return Post.objects.create_post(title='title',
25 return Post.objects.create_post(title='title',
26 text='text')
26 text='text')
27
27
28 def test_post_add(self):
28 def test_post_add(self):
29 """Test adding post"""
29 """Test adding post"""
30
30
31 post = self._create_post()
31 post = self._create_post()
32
32
33 self.assertIsNotNone(post, 'No post was created')
33 self.assertIsNotNone(post, 'No post was created')
34
34
35 def test_delete_post(self):
35 def test_delete_post(self):
36 """Test post deletion"""
36 """Test post deletion"""
37
37
38 post = self._create_post()
38 post = self._create_post()
39 post_id = post.id
39 post_id = post.id
40
40
41 Post.objects.delete_post(post)
41 Post.objects.delete_post(post)
42
42
43 self.assertFalse(Post.objects.filter(id=post_id).exists())
43 self.assertFalse(Post.objects.filter(id=post_id).exists())
44
44
45 def test_delete_thread(self):
45 def test_delete_thread(self):
46 """Test thread deletion"""
46 """Test thread deletion"""
47
47
48 opening_post = self._create_post()
48 opening_post = self._create_post()
49 thread = opening_post.thread_new
49 thread = opening_post.thread_new
50 reply = Post.objects.create_post("", "", thread=thread)
50 reply = Post.objects.create_post("", "", thread=thread)
51
51
52 thread.delete_with_posts()
52 thread.delete_with_posts()
53
53
54 self.assertFalse(Post.objects.filter(id=reply.id).exists())
54 self.assertFalse(Post.objects.filter(id=reply.id).exists())
55
55
56 def test_post_to_thread(self):
56 def test_post_to_thread(self):
57 """Test adding post to a thread"""
57 """Test adding post to a thread"""
58
58
59 op = self._create_post()
59 op = self._create_post()
60 post = Post.objects.create_post("", "", thread=op.thread_new)
60 post = Post.objects.create_post("", "", thread=op.thread_new)
61
61
62 self.assertIsNotNone(post, 'Reply to thread wasn\'t created')
62 self.assertIsNotNone(post, 'Reply to thread wasn\'t created')
63 self.assertEqual(op.thread_new.last_edit_time, post.pub_time,
63 self.assertEqual(op.thread_new.last_edit_time, post.pub_time,
64 'Post\'s create time doesn\'t match thread last edit'
64 'Post\'s create time doesn\'t match thread last edit'
65 ' time')
65 ' time')
66
66
67 def test_delete_posts_by_ip(self):
67 def test_delete_posts_by_ip(self):
68 """Test deleting posts with the given ip"""
68 """Test deleting posts with the given ip"""
69
69
70 post = self._create_post()
70 post = self._create_post()
71 post_id = post.id
71 post_id = post.id
72
72
73 Post.objects.delete_posts_by_ip('0.0.0.0')
73 Post.objects.delete_posts_by_ip('0.0.0.0')
74
74
75 self.assertFalse(Post.objects.filter(id=post_id).exists())
75 self.assertFalse(Post.objects.filter(id=post_id).exists())
76
76
77 def test_get_thread(self):
77 def test_get_thread(self):
78 """Test getting all posts of a thread"""
78 """Test getting all posts of a thread"""
79
79
80 opening_post = self._create_post()
80 opening_post = self._create_post()
81
81
82 for i in range(0, 2):
82 for i in range(0, 2):
83 Post.objects.create_post('title', 'text',
83 Post.objects.create_post('title', 'text',
84 thread=opening_post.thread_new)
84 thread=opening_post.thread_new)
85
85
86 thread = opening_post.thread_new
86 thread = opening_post.thread_new
87
87
88 self.assertEqual(3, thread.replies.count())
88 self.assertEqual(3, thread.replies.count())
89
89
90 def test_create_post_with_tag(self):
90 def test_create_post_with_tag(self):
91 """Test adding tag to post"""
91 """Test adding tag to post"""
92
92
93 tag = Tag.objects.create(name='test_tag')
93 tag = Tag.objects.create(name='test_tag')
94 post = Post.objects.create_post(title='title', text='text', tags=[tag])
94 post = Post.objects.create_post(title='title', text='text', tags=[tag])
95
95
96 thread = post.thread_new
96 thread = post.thread_new
97 self.assertIsNotNone(post, 'Post not created')
97 self.assertIsNotNone(post, 'Post not created')
98 self.assertTrue(tag in thread.tags.all(), 'Tag not added to thread')
98 self.assertTrue(tag in thread.tags.all(), 'Tag not added to thread')
99 self.assertTrue(thread in tag.threads.all(), 'Thread not added to tag')
99 self.assertTrue(thread in tag.threads.all(), 'Thread not added to tag')
100
100
101 def test_thread_max_count(self):
101 def test_thread_max_count(self):
102 """Test deletion of old posts when the max thread count is reached"""
102 """Test deletion of old posts when the max thread count is reached"""
103
103
104 for i in range(settings.MAX_THREAD_COUNT + 1):
104 for i in range(settings.MAX_THREAD_COUNT + 1):
105 self._create_post()
105 self._create_post()
106
106
107 self.assertEqual(settings.MAX_THREAD_COUNT,
107 self.assertEqual(settings.MAX_THREAD_COUNT,
108 len(Post.objects.get_threads()))
108 len(Post.objects.get_threads()))
109
109
110 def test_pages(self):
110 def test_pages(self):
111 """Test that the thread list is properly split into pages"""
111 """Test that the thread list is properly split into pages"""
112
112
113 for i in range(settings.MAX_THREAD_COUNT):
113 for i in range(settings.MAX_THREAD_COUNT):
114 self._create_post()
114 self._create_post()
115
115
116 all_threads = Post.objects.get_threads()
116 all_threads = Post.objects.get_threads()
117
117
118 posts_in_second_page = Post.objects.get_threads(page=1)
118 posts_in_second_page = Post.objects.get_threads(page=2)
119 first_post = posts_in_second_page[0]
119 first_post = posts_in_second_page[0]
120
120
121 self.assertEqual(all_threads[settings.THREADS_PER_PAGE].id,
121 self.assertEqual(all_threads[settings.THREADS_PER_PAGE].id,
122 first_post.id)
122 first_post.id)
123
123
124 def test_linked_tag(self):
124 def test_linked_tag(self):
125 """Test adding a linked tag"""
125 """Test adding a linked tag"""
126
126
127 linked_tag = Tag.objects.create(name=u'tag1')
127 linked_tag = Tag.objects.create(name=u'tag1')
128 tag = Tag.objects.create(name=u'tag2', linked=linked_tag)
128 tag = Tag.objects.create(name=u'tag2', linked=linked_tag)
129
129
130 post = Post.objects.create_post("", "", tags=[tag])
130 post = Post.objects.create_post("", "", tags=[tag])
131
131
132 self.assertTrue(linked_tag in post.thread_new.tags.all(),
132 self.assertTrue(linked_tag in post.thread_new.tags.all(),
133 'Linked tag was not added')
133 'Linked tag was not added')
134
134
135
135
136 class PagesTest(TestCase):
136 class PagesTest(TestCase):
137
137
138 def test_404(self):
138 def test_404(self):
139 """Test receiving error 404 when opening a non-existent page"""
139 """Test receiving error 404 when opening a non-existent page"""
140
140
141 tag_name = u'test_tag'
141 tag_name = u'test_tag'
142 tag = Tag.objects.create(name=tag_name)
142 tag = Tag.objects.create(name=tag_name)
143 client = Client()
143 client = Client()
144
144
145 Post.objects.create_post('title', TEST_TEXT, tags=[tag])
145 Post.objects.create_post('title', TEST_TEXT, tags=[tag])
146
146
147 existing_post_id = Post.objects.all()[0].id
147 existing_post_id = Post.objects.all()[0].id
148 response_existing = client.get(THREAD_PAGE + str(existing_post_id) +
148 response_existing = client.get(THREAD_PAGE + str(existing_post_id) +
149 '/')
149 '/')
150 self.assertEqual(HTTP_CODE_OK, response_existing.status_code,
150 self.assertEqual(HTTP_CODE_OK, response_existing.status_code,
151 u'Cannot open existing thread')
151 u'Cannot open existing thread')
152
152
153 response_not_existing = client.get(THREAD_PAGE + str(
153 response_not_existing = client.get(THREAD_PAGE + str(
154 existing_post_id + 1) + '/')
154 existing_post_id + 1) + '/')
155 self.assertEqual(PAGE_404,
155 self.assertEqual(PAGE_404,
156 response_not_existing.templates[0].name,
156 response_not_existing.templates[0].name,
157 u'Not existing thread is opened')
157 u'Not existing thread is opened')
158
158
159 response_existing = client.get(TAG_PAGE + tag_name + '/')
159 response_existing = client.get(TAG_PAGE + tag_name + '/')
160 self.assertEqual(HTTP_CODE_OK,
160 self.assertEqual(HTTP_CODE_OK,
161 response_existing.status_code,
161 response_existing.status_code,
162 u'Cannot open existing tag')
162 u'Cannot open existing tag')
163
163
164 response_not_existing = client.get(TAG_PAGE + u'not_tag' + '/')
164 response_not_existing = client.get(TAG_PAGE + u'not_tag' + '/')
165 self.assertEqual(PAGE_404,
165 self.assertEqual(PAGE_404,
166 response_not_existing.templates[0].name,
166 response_not_existing.templates[0].name,
167 u'Not existing tag is opened')
167 u'Not existing tag is opened')
168
168
169 reply_id = Post.objects.create_post('', TEST_TEXT,
169 reply_id = Post.objects.create_post('', TEST_TEXT,
170 thread=Post.objects.all()[0]
170 thread=Post.objects.all()[0]
171 .thread)
171 .thread)
172 response_not_existing = client.get(THREAD_PAGE + str(
172 response_not_existing = client.get(THREAD_PAGE + str(
173 reply_id) + '/')
173 reply_id) + '/')
174 self.assertEqual(PAGE_404,
174 self.assertEqual(PAGE_404,
175 response_not_existing.templates[0].name,
175 response_not_existing.templates[0].name,
176 u'Reply is opened as a thread')
176 u'Reply is opened as a thread')
177
177
178
178
179 class FormTest(TestCase):
179 class FormTest(TestCase):
180 def test_post_validation(self):
180 def test_post_validation(self):
181 # Disable captcha for the test
181 # Disable captcha for the test
182 captcha_enabled = settings.ENABLE_CAPTCHA
182 captcha_enabled = settings.ENABLE_CAPTCHA
183 settings.ENABLE_CAPTCHA = False
183 settings.ENABLE_CAPTCHA = False
184
184
185 client = Client()
185 client = Client()
186
186
187 valid_tags = u'tag1 tag_2 Ρ‚Π΅Π³_3'
187 valid_tags = u'tag1 tag_2 Ρ‚Π΅Π³_3'
188 invalid_tags = u'$%_356 ---'
188 invalid_tags = u'$%_356 ---'
189
189
190 response = client.post(NEW_THREAD_PAGE, {'title': 'test title',
190 response = client.post(NEW_THREAD_PAGE, {'title': 'test title',
191 'text': TEST_TEXT,
191 'text': TEST_TEXT,
192 'tags': valid_tags})
192 'tags': valid_tags})
193 self.assertEqual(response.status_code, HTTP_CODE_REDIRECT,
193 self.assertEqual(response.status_code, HTTP_CODE_REDIRECT,
194 msg='Posting new message failed: got code ' +
194 msg='Posting new message failed: got code ' +
195 str(response.status_code))
195 str(response.status_code))
196
196
197 self.assertEqual(1, Post.objects.count(),
197 self.assertEqual(1, Post.objects.count(),
198 msg='No posts were created')
198 msg='No posts were created')
199
199
200 client.post(NEW_THREAD_PAGE, {'text': TEST_TEXT,
200 client.post(NEW_THREAD_PAGE, {'text': TEST_TEXT,
201 'tags': invalid_tags})
201 'tags': invalid_tags})
202 self.assertEqual(1, Post.objects.count(), msg='The validation passed '
202 self.assertEqual(1, Post.objects.count(), msg='The validation passed '
203 'where it should fail')
203 'where it should fail')
204
204
205 # Change posting delay so we don't have to wait for 30 seconds or more
205 # Change posting delay so we don't have to wait for 30 seconds or more
206 old_posting_delay = settings.POSTING_DELAY
206 old_posting_delay = settings.POSTING_DELAY
207 # Wait fot the posting delay or we won't be able to post
207 # Wait fot the posting delay or we won't be able to post
208 settings.POSTING_DELAY = 1
208 settings.POSTING_DELAY = 1
209 time.sleep(settings.POSTING_DELAY + 1)
209 time.sleep(settings.POSTING_DELAY + 1)
210 response = client.post(THREAD_PAGE_ONE, {'text': TEST_TEXT,
210 response = client.post(THREAD_PAGE_ONE, {'text': TEST_TEXT,
211 'tags': valid_tags})
211 'tags': valid_tags})
212 self.assertEqual(HTTP_CODE_REDIRECT, response.status_code,
212 self.assertEqual(HTTP_CODE_REDIRECT, response.status_code,
213 msg=u'Posting new message failed: got code ' +
213 msg=u'Posting new message failed: got code ' +
214 str(response.status_code))
214 str(response.status_code))
215 # Restore posting delay
215 # Restore posting delay
216 settings.POSTING_DELAY = old_posting_delay
216 settings.POSTING_DELAY = old_posting_delay
217
217
218 self.assertEqual(2, Post.objects.count(),
218 self.assertEqual(2, Post.objects.count(),
219 msg=u'No posts were created')
219 msg=u'No posts were created')
220
220
221 # Restore captcha setting
221 # Restore captcha setting
222 settings.ENABLE_CAPTCHA = captcha_enabled
222 settings.ENABLE_CAPTCHA = captcha_enabled
223
223
224
224
225 class ViewTest(TestCase):
225 class ViewTest(TestCase):
226 def test_index(self):
226 def test_index(self):
227 client = Client()
227 client = Client()
228
228
229 response = client.get('/')
229 response = client.get('/')
230 self.assertEqual(HTTP_CODE_OK, response.status_code, 'Index page not '
230 self.assertEqual(HTTP_CODE_OK, response.status_code, 'Index page not '
231 'opened')
231 'opened')
232 self.assertEqual('boards/posting_general.html', response.templates[0]
232 self.assertEqual('boards/posting_general.html', response.templates[0]
233 .name, 'Index page should open posting_general template') No newline at end of file
233 .name, 'Index page should open posting_general template')
General Comments 0
You need to be logged in to leave comments. Login now