##// END OF EJS Templates
Fixed API related tests
neko259 -
r1175:3d7a615a decentral
parent child Browse files
Show More
@@ -1,163 +1,156 b''
1 from django.core.paginator import Paginator
1 from django.core.paginator import Paginator
2 from django.test import TestCase
2 from django.test import TestCase
3 from boards import settings
3 from boards import settings
4 from boards.models import Tag, Post, Thread, KeyPair
4 from boards.models import Tag, Post, Thread, KeyPair
5
5
6
6
7 class PostTests(TestCase):
7 class PostTests(TestCase):
8
8
9 def _create_post(self):
9 def _create_post(self):
10 tag, created = Tag.objects.get_or_create(name='test_tag')
10 tag, created = Tag.objects.get_or_create(name='test_tag')
11 return Post.objects.create_post(title='title', text='text',
11 return Post.objects.create_post(title='title', text='text',
12 tags=[tag])
12 tags=[tag])
13
13
14 def test_post_add(self):
14 def test_post_add(self):
15 """Test adding post"""
15 """Test adding post"""
16
16
17 post = self._create_post()
17 post = self._create_post()
18
18
19 self.assertIsNotNone(post, 'No post was created.')
19 self.assertIsNotNone(post, 'No post was created.')
20 self.assertEqual('test_tag', post.get_thread().tags.all()[0].name,
20 self.assertEqual('test_tag', post.get_thread().tags.all()[0].name,
21 'No tags were added to the post.')
21 'No tags were added to the post.')
22
22
23 def test_delete_post(self):
23 def test_delete_post(self):
24 """Test post deletion"""
24 """Test post deletion"""
25
25
26 post = self._create_post()
26 post = self._create_post()
27 post_id = post.id
27 post_id = post.id
28
28
29 post.delete()
29 post.delete()
30
30
31 self.assertFalse(Post.objects.filter(id=post_id).exists())
31 self.assertFalse(Post.objects.filter(id=post_id).exists())
32
32
33 def test_delete_thread(self):
33 def test_delete_thread(self):
34 """Test thread deletion"""
34 """Test thread deletion"""
35
35
36 opening_post = self._create_post()
36 opening_post = self._create_post()
37 thread = opening_post.get_thread()
37 thread = opening_post.get_thread()
38 reply = Post.objects.create_post("", "", thread=thread)
38 reply = Post.objects.create_post("", "", thread=thread)
39
39
40 thread.delete()
40 thread.delete()
41
41
42 self.assertFalse(Post.objects.filter(id=reply.id).exists(),
42 self.assertFalse(Post.objects.filter(id=reply.id).exists(),
43 'Reply was not deleted with the thread.')
43 'Reply was not deleted with the thread.')
44 self.assertFalse(Post.objects.filter(id=opening_post.id).exists(),
44 self.assertFalse(Post.objects.filter(id=opening_post.id).exists(),
45 'Opening post was not deleted with the thread.')
45 'Opening post was not deleted with the thread.')
46
46
47 def test_post_to_thread(self):
47 def test_post_to_thread(self):
48 """Test adding post to a thread"""
48 """Test adding post to a thread"""
49
49
50 op = self._create_post()
50 op = self._create_post()
51 post = Post.objects.create_post("", "", thread=op.get_thread())
51 post = Post.objects.create_post("", "", thread=op.get_thread())
52
52
53 self.assertIsNotNone(post, 'Reply to thread wasn\'t created')
53 self.assertIsNotNone(post, 'Reply to thread wasn\'t created')
54 self.assertEqual(op.get_thread().last_edit_time, post.pub_time,
54 self.assertEqual(op.get_thread().last_edit_time, post.pub_time,
55 'Post\'s create time doesn\'t match thread last edit'
55 'Post\'s create time doesn\'t match thread last edit'
56 ' time')
56 ' time')
57
57
58 def test_delete_posts_by_ip(self):
58 def test_delete_posts_by_ip(self):
59 """Test deleting posts with the given ip"""
59 """Test deleting posts with the given ip"""
60
60
61 post = self._create_post()
61 post = self._create_post()
62 post_id = post.id
62 post_id = post.id
63
63
64 Post.objects.delete_posts_by_ip('0.0.0.0')
64 Post.objects.delete_posts_by_ip('0.0.0.0')
65
65
66 self.assertFalse(Post.objects.filter(id=post_id).exists())
66 self.assertFalse(Post.objects.filter(id=post_id).exists())
67
67
68 def test_get_thread(self):
68 def test_get_thread(self):
69 """Test getting all posts of a thread"""
69 """Test getting all posts of a thread"""
70
70
71 opening_post = self._create_post()
71 opening_post = self._create_post()
72
72
73 for i in range(2):
73 for i in range(2):
74 Post.objects.create_post('title', 'text',
74 Post.objects.create_post('title', 'text',
75 thread=opening_post.get_thread())
75 thread=opening_post.get_thread())
76
76
77 thread = opening_post.get_thread()
77 thread = opening_post.get_thread()
78
78
79 self.assertEqual(3, thread.get_replies().count())
79 self.assertEqual(3, thread.get_replies().count())
80
80
81 def test_create_post_with_tag(self):
81 def test_create_post_with_tag(self):
82 """Test adding tag to post"""
82 """Test adding tag to post"""
83
83
84 tag = Tag.objects.create(name='test_tag')
84 tag = Tag.objects.create(name='test_tag')
85 post = Post.objects.create_post(title='title', text='text', tags=[tag])
85 post = Post.objects.create_post(title='title', text='text', tags=[tag])
86
86
87 thread = post.get_thread()
87 thread = post.get_thread()
88 self.assertIsNotNone(post, 'Post not created')
88 self.assertIsNotNone(post, 'Post not created')
89 self.assertTrue(tag in thread.tags.all(), 'Tag not added to thread')
89 self.assertTrue(tag in thread.tags.all(), 'Tag not added to thread')
90
90
91 def test_thread_max_count(self):
91 def test_thread_max_count(self):
92 """Test deletion of old posts when the max thread count is reached"""
92 """Test deletion of old posts when the max thread count is reached"""
93
93
94 for i in range(settings.get_int('Messages', 'MaxThreadCount') + 1):
94 for i in range(settings.get_int('Messages', 'MaxThreadCount') + 1):
95 self._create_post()
95 self._create_post()
96
96
97 self.assertEqual(settings.get_int('Messages', 'MaxThreadCount'),
97 self.assertEqual(settings.get_int('Messages', 'MaxThreadCount'),
98 len(Thread.objects.filter(archived=False)))
98 len(Thread.objects.filter(archived=False)))
99
99
100 def test_pages(self):
100 def test_pages(self):
101 """Test that the thread list is properly split into pages"""
101 """Test that the thread list is properly split into pages"""
102
102
103 for i in range(settings.get_int('Messages', 'MaxThreadCount')):
103 for i in range(settings.get_int('Messages', 'MaxThreadCount')):
104 self._create_post()
104 self._create_post()
105
105
106 all_threads = Thread.objects.filter(archived=False)
106 all_threads = Thread.objects.filter(archived=False)
107
107
108 paginator = Paginator(Thread.objects.filter(archived=False),
108 paginator = Paginator(Thread.objects.filter(archived=False),
109 settings.get_int('View', 'ThreadsPerPage'))
109 settings.get_int('View', 'ThreadsPerPage'))
110 posts_in_second_page = paginator.page(2).object_list
110 posts_in_second_page = paginator.page(2).object_list
111 first_post = posts_in_second_page[0]
111 first_post = posts_in_second_page[0]
112
112
113 self.assertEqual(all_threads[settings.get_int('View', 'ThreadsPerPage')].id,
113 self.assertEqual(all_threads[settings.get_int('View', 'ThreadsPerPage')].id,
114 first_post.id)
114 first_post.id)
115
115
116 def test_reflinks(self):
116 def test_reflinks(self):
117 """
117 """
118 Tests that reflinks are parsed within post and connecting replies
118 Tests that reflinks are parsed within post and connecting replies
119 to the replied posts.
119 to the replied posts.
120
120
121 Local reflink example: [post]123[/post]
121 Local reflink example: [post]123[/post]
122 Global reflink example: [post]key_type::key::123[/post]
122 Global reflink example: [post]key_type::key::123[/post]
123 """
123 """
124
124
125 key = KeyPair.objects.generate_key(primary=True)
125 key = KeyPair.objects.generate_key(primary=True)
126
126
127 tag = Tag.objects.create(name='test_tag')
127 tag = Tag.objects.create(name='test_tag')
128
128
129 post = Post.objects.create_post(title='', text='', tags=[tag])
129 post = Post.objects.create_post(title='', text='', tags=[tag])
130 post_local_reflink = Post.objects.create_post(title='',
130 post_local_reflink = Post.objects.create_post(title='',
131 text='[post]%d[/post]' % post.id, thread=post.get_thread())
131 text='[post]%d[/post]' % post.id, thread=post.get_thread())
132
132
133 self.assertTrue(post_local_reflink in post.referenced_posts.all(),
133 self.assertTrue(post_local_reflink in post.referenced_posts.all(),
134 'Local reflink not connecting posts.')
134 'Local reflink not connecting posts.')
135
135
136 post_global_reflink = Post.objects.create_post(title='',
137 text='[post]%s::%s::%d[/post]' % (
138 post.global_id.key_type, post.global_id.key, post.id),
139 thread=post.get_thread())
140
141 self.assertTrue(post_global_reflink in post.referenced_posts.all(),
142 'Global reflink not connecting posts.')
143
136
144 def test_thread_replies(self):
137 def test_thread_replies(self):
145 """
138 """
146 Tests that the replies can be queried from a thread in all possible
139 Tests that the replies can be queried from a thread in all possible
147 ways.
140 ways.
148 """
141 """
149
142
150 tag = Tag.objects.create(name='test_tag')
143 tag = Tag.objects.create(name='test_tag')
151 opening_post = Post.objects.create_post(title='title', text='text',
144 opening_post = Post.objects.create_post(title='title', text='text',
152 tags=[tag])
145 tags=[tag])
153 thread = opening_post.get_thread()
146 thread = opening_post.get_thread()
154
147
155 reply1 = Post.objects.create_post(title='title', text='text', thread=thread)
148 reply1 = Post.objects.create_post(title='title', text='text', thread=thread)
156 reply2 = Post.objects.create_post(title='title', text='text', thread=thread)
149 reply2 = Post.objects.create_post(title='title', text='text', thread=thread)
157
150
158 replies = thread.get_replies()
151 replies = thread.get_replies()
159 self.assertTrue(len(replies) > 0, 'No replies found for thread.')
152 self.assertTrue(len(replies) > 0, 'No replies found for thread.')
160
153
161 replies = thread.get_replies(view_fields_only=True)
154 replies = thread.get_replies(view_fields_only=True)
162 self.assertTrue(len(replies) > 0,
155 self.assertTrue(len(replies) > 0,
163 'No replies found for thread with view fields only.')
156 'No replies found for thread with view fields only.')
@@ -1,47 +1,48 b''
1 import logging
1 import logging
2 from django.core.urlresolvers import reverse, NoReverseMatch
2 from django.core.urlresolvers import reverse, NoReverseMatch
3 from django.test import TestCase, Client
3 from django.test import TestCase, Client
4 from boards import urls
4 from boards import urls
5
5
6
6
7 logger = logging.getLogger(__name__)
7 logger = logging.getLogger(__name__)
8
8
9 HTTP_CODE_OK = 200
9 HTTP_CODE_OK = 200
10
10
11 EXCLUDED_VIEWS = {
11 EXCLUDED_VIEWS = {
12 'banned',
12 'banned',
13 'get_thread_diff',
13 'get_thread_diff',
14 'api_sync_pull',
14 }
15 }
15
16
16
17
17 class ViewTest(TestCase):
18 class ViewTest(TestCase):
18
19
19 def test_all_views(self):
20 def test_all_views(self):
20 """
21 """
21 Try opening all views defined in ulrs.py that don't need additional
22 Try opening all views defined in ulrs.py that don't need additional
22 parameters
23 parameters
23 """
24 """
24
25
25 client = Client()
26 client = Client()
26 for url in urls.urlpatterns:
27 for url in urls.urlpatterns:
27 try:
28 try:
28 view_name = url.name
29 view_name = url.name
29 if view_name in EXCLUDED_VIEWS:
30 if view_name in EXCLUDED_VIEWS:
30 logger.debug('View {} is excluded.'.format(view_name))
31 logger.debug('View {} is excluded.'.format(view_name))
31 continue
32 continue
32
33
33 logger.debug('Testing view %s' % view_name)
34 logger.debug('Testing view %s' % view_name)
34
35
35 try:
36 try:
36 response = client.get(reverse(view_name))
37 response = client.get(reverse(view_name))
37
38
38 self.assertEqual(HTTP_CODE_OK, response.status_code,
39 self.assertEqual(HTTP_CODE_OK, response.status_code,
39 'View not opened: {}'.format(view_name))
40 'View not opened: {}'.format(view_name))
40 except NoReverseMatch:
41 except NoReverseMatch:
41 # This view just needs additional arguments
42 # This view just needs additional arguments
42 pass
43 pass
43 except Exception as e:
44 except Exception as e:
44 self.fail('Got exception %s at %s view' % (e, view_name))
45 self.fail('Got exception %s at %s view' % (e, view_name))
45 except AttributeError:
46 except AttributeError:
46 # This is normal, some views do not have names
47 # This is normal, some views do not have names
47 pass
48 pass
General Comments 0
You need to be logged in to leave comments. Login now