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