Show More
1 | NO CONTENT: new file 100644 |
|
NO CONTENT: new file 100644 |
@@ -0,0 +1,5 b'' | |||||
|
1 | class MockRequest: | |||
|
2 | def __init__(self): | |||
|
3 | self.session = dict() | |||
|
4 | self.GET = dict() | |||
|
5 | self.POST = dict() No newline at end of file |
@@ -0,0 +1,13 b'' | |||||
|
1 | from django.test import TestCase | |||
|
2 | from boards.abstracts.settingsmanager import get_settings_manager | |||
|
3 | from boards.tests.mocks import MockRequest | |||
|
4 | ||||
|
5 | ||||
|
6 | class AbstractTest(TestCase): | |||
|
7 | def test_settings_manager(self): | |||
|
8 | request = MockRequest() | |||
|
9 | settings_manager = get_settings_manager(request) | |||
|
10 | ||||
|
11 | settings_manager.set_setting('test_setting', 'test_value') | |||
|
12 | self.assertEqual('test_value', settings_manager.get_setting( | |||
|
13 | 'test_setting'), 'Setting update failed.') No newline at end of file |
@@ -0,0 +1,50 b'' | |||||
|
1 | import simplejson | |||
|
2 | ||||
|
3 | from django.test import TestCase | |||
|
4 | ||||
|
5 | from boards.models import Tag, Post | |||
|
6 | from boards.tests.mocks import MockRequest | |||
|
7 | from boards.utils import datetime_to_epoch | |||
|
8 | from boards.views.api import api_get_threaddiff | |||
|
9 | ||||
|
10 | ||||
|
11 | class ApiTest(TestCase): | |||
|
12 | def test_thread_diff(self): | |||
|
13 | tag = Tag.objects.create(name='test_tag') | |||
|
14 | opening_post = Post.objects.create_post(title='title', text='text', | |||
|
15 | tags=[tag]) | |||
|
16 | ||||
|
17 | last_edit_time = datetime_to_epoch(opening_post.last_edit_time) | |||
|
18 | ||||
|
19 | # Check the exact timestamp post was added | |||
|
20 | empty_response = api_get_threaddiff(MockRequest(), | |||
|
21 | str(opening_post.thread_new.id), | |||
|
22 | str(last_edit_time)) | |||
|
23 | diff = simplejson.loads(empty_response.content) | |||
|
24 | self.assertEqual(0, len(diff['added']), | |||
|
25 | 'There must be no added posts in the diff.') | |||
|
26 | self.assertEqual(0, len(diff['updated']), | |||
|
27 | 'There must be no updated posts in the diff.') | |||
|
28 | ||||
|
29 | reply = Post.objects.create_post(title='', | |||
|
30 | text='[post]%d[/post]\ntext' % opening_post.id, | |||
|
31 | thread=opening_post.thread_new) | |||
|
32 | ||||
|
33 | # Check the timestamp before post was added | |||
|
34 | response = api_get_threaddiff(MockRequest(), | |||
|
35 | str(opening_post.thread_new.id), | |||
|
36 | str(last_edit_time)) | |||
|
37 | diff = simplejson.loads(response.content) | |||
|
38 | self.assertEqual(1, len(diff['added']), | |||
|
39 | 'There must be 1 added posts in the diff.') | |||
|
40 | self.assertEqual(1, len(diff['updated']), | |||
|
41 | 'There must be 1 updated posts in the diff.') | |||
|
42 | ||||
|
43 | empty_response = api_get_threaddiff(MockRequest(), | |||
|
44 | str(opening_post.thread_new.id), | |||
|
45 | str(datetime_to_epoch(reply.last_edit_time))) | |||
|
46 | diff = simplejson.loads(empty_response.content) | |||
|
47 | self.assertEqual(0, len(diff['added']), | |||
|
48 | 'There must be no added posts in the diff.') | |||
|
49 | self.assertEqual(0, len(diff['updated']), | |||
|
50 | 'There must be no updated posts in the diff.') No newline at end of file |
@@ -0,0 +1,55 b'' | |||||
|
1 | from django.test import TestCase, Client | |||
|
2 | import time | |||
|
3 | from boards import settings | |||
|
4 | from boards.models import Post | |||
|
5 | import neboard | |||
|
6 | ||||
|
7 | ||||
|
8 | TEST_TAG = 'test_tag' | |||
|
9 | ||||
|
10 | PAGE_404 = 'boards/404.html' | |||
|
11 | ||||
|
12 | TEST_TEXT = 'test text' | |||
|
13 | ||||
|
14 | NEW_THREAD_PAGE = '/' | |||
|
15 | THREAD_PAGE_ONE = '/thread/1/' | |||
|
16 | HTTP_CODE_REDIRECT = 302 | |||
|
17 | ||||
|
18 | ||||
|
19 | class FormTest(TestCase): | |||
|
20 | def test_post_validation(self): | |||
|
21 | client = Client() | |||
|
22 | ||||
|
23 | valid_tags = 'tag1 tag_2 тег_3' | |||
|
24 | invalid_tags = '$%_356 ---' | |||
|
25 | ||||
|
26 | response = client.post(NEW_THREAD_PAGE, {'title': 'test title', | |||
|
27 | 'text': TEST_TEXT, | |||
|
28 | 'tags': valid_tags}) | |||
|
29 | self.assertEqual(response.status_code, HTTP_CODE_REDIRECT, | |||
|
30 | msg='Posting new message failed: got code ' + | |||
|
31 | str(response.status_code)) | |||
|
32 | ||||
|
33 | self.assertEqual(1, Post.objects.count(), | |||
|
34 | msg='No posts were created') | |||
|
35 | ||||
|
36 | client.post(NEW_THREAD_PAGE, {'text': TEST_TEXT, | |||
|
37 | 'tags': invalid_tags}) | |||
|
38 | self.assertEqual(1, Post.objects.count(), msg='The validation passed ' | |||
|
39 | 'where it should fail') | |||
|
40 | ||||
|
41 | # Change posting delay so we don't have to wait for 30 seconds or more | |||
|
42 | old_posting_delay = neboard.settings.POSTING_DELAY | |||
|
43 | # Wait fot the posting delay or we won't be able to post | |||
|
44 | neboard.settings.POSTING_DELAY = 1 | |||
|
45 | time.sleep(neboard.settings.POSTING_DELAY + 1) | |||
|
46 | response = client.post(THREAD_PAGE_ONE, {'text': TEST_TEXT, | |||
|
47 | 'tags': valid_tags}) | |||
|
48 | self.assertEqual(HTTP_CODE_REDIRECT, response.status_code, | |||
|
49 | msg='Posting new message failed: got code ' + | |||
|
50 | str(response.status_code)) | |||
|
51 | # Restore posting delay | |||
|
52 | settings.POSTING_DELAY = old_posting_delay | |||
|
53 | ||||
|
54 | self.assertEqual(2, Post.objects.count(), | |||
|
55 | msg='No posts were created') |
@@ -0,0 +1,39 b'' | |||||
|
1 | from django.test import TestCase | |||
|
2 | from boards.models import KeyPair, GlobalId | |||
|
3 | from boards.views.sync import generate_request_get | |||
|
4 | ||||
|
5 | ||||
|
6 | class KeyTest(TestCase): | |||
|
7 | def test_create_key(self): | |||
|
8 | key = KeyPair.objects.generate_key('ecdsa') | |||
|
9 | ||||
|
10 | self.assertIsNotNone(key, 'The key was not created.') | |||
|
11 | ||||
|
12 | def test_validation(self): | |||
|
13 | key = KeyPair.objects.generate_key(key_type='ecdsa') | |||
|
14 | message = 'msg' | |||
|
15 | signature = key.sign(message) | |||
|
16 | valid = KeyPair.objects.verify(key.public_key, message, signature, | |||
|
17 | key_type='ecdsa') | |||
|
18 | ||||
|
19 | self.assertTrue(valid, 'Message verification failed.') | |||
|
20 | ||||
|
21 | def test_primary_constraint(self): | |||
|
22 | KeyPair.objects.generate_key(key_type='ecdsa', primary=True) | |||
|
23 | ||||
|
24 | try: | |||
|
25 | KeyPair.objects.generate_key(key_type='ecdsa', primary=True) | |||
|
26 | self.fail('Exception should be thrown indicating there can be only' | |||
|
27 | ' one primary key.') | |||
|
28 | except Exception: | |||
|
29 | pass | |||
|
30 | ||||
|
31 | def test_request_get(self): | |||
|
32 | model_id = GlobalId(key_type='test', key='test key', local_id='1') | |||
|
33 | model_id.save() | |||
|
34 | ||||
|
35 | self.assertTrue('<request type="get" version="1.0"><model ' | |||
|
36 | 'name="post" version="1.0"><id key="test key" ' | |||
|
37 | 'local-id="1" type="test" /></model></request>' in | |||
|
38 | generate_request_get([model_id]), | |||
|
39 | 'Wrong XML generated for the GET request.') |
@@ -0,0 +1,56 b'' | |||||
|
1 | from django.test import TestCase, Client | |||
|
2 | from boards.models import Tag, Post | |||
|
3 | ||||
|
4 | TEST_TEXT = 'test' | |||
|
5 | ||||
|
6 | NEW_THREAD_PAGE = '/' | |||
|
7 | THREAD_PAGE_ONE = '/thread/1/' | |||
|
8 | THREAD_PAGE = '/thread/' | |||
|
9 | TAG_PAGE = '/tag/' | |||
|
10 | HTTP_CODE_REDIRECT = 302 | |||
|
11 | HTTP_CODE_OK = 200 | |||
|
12 | HTTP_CODE_NOT_FOUND = 404 | |||
|
13 | ||||
|
14 | PAGE_404 = 'boards/404.html' | |||
|
15 | ||||
|
16 | ||||
|
17 | class PagesTest(TestCase): | |||
|
18 | ||||
|
19 | def test_404(self): | |||
|
20 | """Test receiving error 404 when opening a non-existent page""" | |||
|
21 | ||||
|
22 | tag_name = 'test_tag' | |||
|
23 | tag = Tag.objects.create(name=tag_name) | |||
|
24 | client = Client() | |||
|
25 | ||||
|
26 | Post.objects.create_post('title', TEST_TEXT, tags=[tag]) | |||
|
27 | ||||
|
28 | existing_post_id = Post.objects.all()[0].id | |||
|
29 | response_existing = client.get(THREAD_PAGE + str(existing_post_id) + | |||
|
30 | '/') | |||
|
31 | self.assertEqual(HTTP_CODE_OK, response_existing.status_code, | |||
|
32 | 'Cannot open existing thread') | |||
|
33 | ||||
|
34 | response_not_existing = client.get(THREAD_PAGE + str( | |||
|
35 | existing_post_id + 1) + '/') | |||
|
36 | self.assertEqual(PAGE_404, response_not_existing.templates[0].name, | |||
|
37 | 'Not existing thread is opened') | |||
|
38 | ||||
|
39 | response_existing = client.get(TAG_PAGE + tag_name + '/') | |||
|
40 | self.assertEqual(HTTP_CODE_OK, | |||
|
41 | response_existing.status_code, | |||
|
42 | 'Cannot open existing tag') | |||
|
43 | ||||
|
44 | response_not_existing = client.get(TAG_PAGE + 'not_tag' + '/') | |||
|
45 | self.assertEqual(PAGE_404, | |||
|
46 | response_not_existing.templates[0].name, | |||
|
47 | 'Not existing tag is opened') | |||
|
48 | ||||
|
49 | reply_id = Post.objects.create_post('', TEST_TEXT, | |||
|
50 | thread=Post.objects.all()[0] | |||
|
51 | .get_thread()) | |||
|
52 | response_not_existing = client.get(THREAD_PAGE + str( | |||
|
53 | reply_id) + '/') | |||
|
54 | self.assertEqual(PAGE_404, | |||
|
55 | response_not_existing.templates[0].name, | |||
|
56 | 'Reply is opened as a thread') |
@@ -0,0 +1,112 b'' | |||||
|
1 | from django.core.paginator import Paginator | |||
|
2 | from django.test import TestCase | |||
|
3 | from boards import settings | |||
|
4 | from boards.models import Tag, Post, Thread | |||
|
5 | ||||
|
6 | ||||
|
7 | class PostTests(TestCase): | |||
|
8 | ||||
|
9 | def _create_post(self): | |||
|
10 | tag = Tag.objects.create(name='test_tag') | |||
|
11 | return Post.objects.create_post(title='title', text='text', | |||
|
12 | tags=[tag]) | |||
|
13 | ||||
|
14 | def test_post_add(self): | |||
|
15 | """Test adding post""" | |||
|
16 | ||||
|
17 | post = self._create_post() | |||
|
18 | ||||
|
19 | self.assertIsNotNone(post, 'No post was created.') | |||
|
20 | self.assertEqual('test_tag', post.get_thread().tags.all()[0].name, | |||
|
21 | 'No tags were added to the post.') | |||
|
22 | ||||
|
23 | def test_delete_post(self): | |||
|
24 | """Test post deletion""" | |||
|
25 | ||||
|
26 | post = self._create_post() | |||
|
27 | post_id = post.id | |||
|
28 | ||||
|
29 | Post.objects.delete_post(post) | |||
|
30 | ||||
|
31 | self.assertFalse(Post.objects.filter(id=post_id).exists()) | |||
|
32 | ||||
|
33 | def test_delete_thread(self): | |||
|
34 | """Test thread deletion""" | |||
|
35 | ||||
|
36 | opening_post = self._create_post() | |||
|
37 | thread = opening_post.get_thread() | |||
|
38 | reply = Post.objects.create_post("", "", thread=thread) | |||
|
39 | ||||
|
40 | thread.delete() | |||
|
41 | ||||
|
42 | self.assertFalse(Post.objects.filter(id=reply.id).exists()) | |||
|
43 | ||||
|
44 | def test_post_to_thread(self): | |||
|
45 | """Test adding post to a thread""" | |||
|
46 | ||||
|
47 | op = self._create_post() | |||
|
48 | post = Post.objects.create_post("", "", thread=op.get_thread()) | |||
|
49 | ||||
|
50 | self.assertIsNotNone(post, 'Reply to thread wasn\'t created') | |||
|
51 | self.assertEqual(op.get_thread().last_edit_time, post.pub_time, | |||
|
52 | 'Post\'s create time doesn\'t match thread last edit' | |||
|
53 | ' time') | |||
|
54 | ||||
|
55 | def test_delete_posts_by_ip(self): | |||
|
56 | """Test deleting posts with the given ip""" | |||
|
57 | ||||
|
58 | post = self._create_post() | |||
|
59 | post_id = post.id | |||
|
60 | ||||
|
61 | Post.objects.delete_posts_by_ip('0.0.0.0') | |||
|
62 | ||||
|
63 | self.assertFalse(Post.objects.filter(id=post_id).exists()) | |||
|
64 | ||||
|
65 | def test_get_thread(self): | |||
|
66 | """Test getting all posts of a thread""" | |||
|
67 | ||||
|
68 | opening_post = self._create_post() | |||
|
69 | ||||
|
70 | for i in range(2): | |||
|
71 | Post.objects.create_post('title', 'text', | |||
|
72 | thread=opening_post.get_thread()) | |||
|
73 | ||||
|
74 | thread = opening_post.get_thread() | |||
|
75 | ||||
|
76 | self.assertEqual(3, thread.replies.count()) | |||
|
77 | ||||
|
78 | def test_create_post_with_tag(self): | |||
|
79 | """Test adding tag to post""" | |||
|
80 | ||||
|
81 | tag = Tag.objects.create(name='test_tag') | |||
|
82 | post = Post.objects.create_post(title='title', text='text', tags=[tag]) | |||
|
83 | ||||
|
84 | thread = post.get_thread() | |||
|
85 | self.assertIsNotNone(post, 'Post not created') | |||
|
86 | self.assertTrue(tag in thread.tags.all(), 'Tag not added to thread') | |||
|
87 | self.assertTrue(thread in tag.threads.all(), 'Thread not added to tag') | |||
|
88 | ||||
|
89 | def test_thread_max_count(self): | |||
|
90 | """Test deletion of old posts when the max thread count is reached""" | |||
|
91 | ||||
|
92 | for i in range(settings.MAX_THREAD_COUNT + 1): | |||
|
93 | self._create_post() | |||
|
94 | ||||
|
95 | self.assertEqual(settings.MAX_THREAD_COUNT, | |||
|
96 | len(Thread.objects.filter(archived=False))) | |||
|
97 | ||||
|
98 | def test_pages(self): | |||
|
99 | """Test that the thread list is properly split into pages""" | |||
|
100 | ||||
|
101 | for i in range(settings.MAX_THREAD_COUNT): | |||
|
102 | self._create_post() | |||
|
103 | ||||
|
104 | all_threads = Thread.objects.filter(archived=False) | |||
|
105 | ||||
|
106 | paginator = Paginator(Thread.objects.filter(archived=False), | |||
|
107 | settings.THREADS_PER_PAGE) | |||
|
108 | posts_in_second_page = paginator.page(2).object_list | |||
|
109 | first_post = posts_in_second_page[0] | |||
|
110 | ||||
|
111 | self.assertEqual(all_threads[settings.THREADS_PER_PAGE].id, | |||
|
112 | first_post.id) No newline at end of file |
@@ -0,0 +1,38 b'' | |||||
|
1 | import logging | |||
|
2 | from django.core.urlresolvers import reverse, NoReverseMatch | |||
|
3 | from django.test import TestCase, Client | |||
|
4 | from boards import urls | |||
|
5 | ||||
|
6 | ||||
|
7 | logger = logging.getLogger(__name__) | |||
|
8 | ||||
|
9 | HTTP_CODE_OK = 200 | |||
|
10 | ||||
|
11 | ||||
|
12 | class ViewTest(TestCase): | |||
|
13 | ||||
|
14 | def test_all_views(self): | |||
|
15 | """ | |||
|
16 | Try opening all views defined in ulrs.py that don't need additional | |||
|
17 | parameters | |||
|
18 | """ | |||
|
19 | ||||
|
20 | client = Client() | |||
|
21 | for url in urls.urlpatterns: | |||
|
22 | try: | |||
|
23 | view_name = url.name | |||
|
24 | logger.debug('Testing view %s' % view_name) | |||
|
25 | ||||
|
26 | try: | |||
|
27 | response = client.get(reverse(view_name)) | |||
|
28 | ||||
|
29 | self.assertEqual(HTTP_CODE_OK, response.status_code, | |||
|
30 | '%s view not opened' % view_name) | |||
|
31 | except NoReverseMatch: | |||
|
32 | # This view just needs additional arguments | |||
|
33 | pass | |||
|
34 | except Exception as e: | |||
|
35 | self.fail('Got exception %s at %s view' % (e, view_name)) | |||
|
36 | except AttributeError: | |||
|
37 | # This is normal, some views do not have names | |||
|
38 | pass |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
General Comments 0
You need to be logged in to leave comments.
Login now