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