diff options
Diffstat (limited to 'askbot/tests')
-rw-r--r-- | askbot/tests/__init__.py | 3 | ||||
-rw-r--r-- | askbot/tests/__init__.py.orig | 19 | ||||
-rw-r--r-- | askbot/tests/badge_tests.py | 64 | ||||
-rw-r--r-- | askbot/tests/db_api_tests.py | 31 | ||||
-rw-r--r-- | askbot/tests/haystack_search_tests.py | 101 | ||||
-rw-r--r-- | askbot/tests/page_load_tests.py | 4 | ||||
-rw-r--r-- | askbot/tests/post_model_tests.py | 6 | ||||
-rw-r--r-- | askbot/tests/question_views_tests.py | 207 | ||||
-rw-r--r-- | askbot/tests/user_views_tests.py | 39 |
9 files changed, 414 insertions, 60 deletions
diff --git a/askbot/tests/__init__.py b/askbot/tests/__init__.py index fcef288b..7e7e3c48 100644 --- a/askbot/tests/__init__.py +++ b/askbot/tests/__init__.py @@ -14,9 +14,12 @@ from askbot.tests.markup_test import * from askbot.tests.post_model_tests import * from askbot.tests.thread_model_tests import * from askbot.tests.reply_by_email_tests import * +from askbot.tests.haystack_search_tests import * from askbot.tests.email_parsing_tests import * from askbot.tests.widget_tests import * from askbot.tests.category_tree_tests import CategoryTreeTests +from askbot.tests.question_views_tests import * from askbot.tests.user_model_tests import UserModelTests +from askbot.tests.user_views_tests import * from askbot.tests.utils_tests import * from askbot.tests.view_context_tests import * diff --git a/askbot/tests/__init__.py.orig b/askbot/tests/__init__.py.orig deleted file mode 100644 index 905c90df..00000000 --- a/askbot/tests/__init__.py.orig +++ /dev/null @@ -1,19 +0,0 @@ -from askbot.tests.cache_tests import * -from askbot.tests.email_alert_tests import * -from askbot.tests.on_screen_notification_tests import * -from askbot.tests.page_load_tests import * -from askbot.tests.permission_assertion_tests import * -from askbot.tests.db_api_tests import * -from askbot.tests.skin_tests import * -from askbot.tests.badge_tests import * -from askbot.tests.management_command_tests import * -from askbot.tests.search_state_tests import * -from askbot.tests.form_tests import * -from askbot.tests.follow_tests import * -from askbot.tests.templatefilter_tests import * -from askbot.tests.markup_test import * -from askbot.tests.post_model_tests import * -from askbot.tests.thread_model_tests import * -from askbot.tests.reply_by_email_tests import * -from askbot.tests.category_tree_tests import CategoryTreeTests -from askbot.tests.user_model_tests import UserModelTests diff --git a/askbot/tests/badge_tests.py b/askbot/tests/badge_tests.py index 0ed4b343..c184db6f 100644 --- a/askbot/tests/badge_tests.py +++ b/askbot/tests/badge_tests.py @@ -24,7 +24,7 @@ class BadgeTests(AskbotTestCase): def assert_accepted_answer_badge_works(self, badge_key = None, - min_score = None, + min_points = None, expected_count = 1, previous_count = 0, trigger = None @@ -32,7 +32,7 @@ class BadgeTests(AskbotTestCase): assert(trigger in ('accept_best_answer', 'upvote_answer')) question = self.post_question(user = self.u1) answer = self.post_answer(user = self.u2, question = question) - answer.score = min_score - 1 + answer.points = min_points - 1 answer.save() recipient = answer.author @@ -47,30 +47,30 @@ class BadgeTests(AskbotTestCase): self.u1.upvote(answer) self.assert_have_badge(badge_key, recipient, expected_count) - def assert_upvoted_answer_badge_works(self, + def assert_upvoted_answer_badge_works(self, badge_key = None, - min_score = None, + min_points = None, multiple = False ): """test answer badge where answer author is the recipient where badge award is triggered by upvotes - * min_score - minimum # of upvotes required + * min_points - minimum # of upvotes required * multiple - multiple award or not * badge_key - key on askbot.models.badges.Badge object """ question = self.post_question(user = self.u1) answer = self.post_answer(user = self.u2, question = question) - answer.score = min_score - 1 + answer.points = min_points - 1 answer.save() self.u1.upvote(answer) self.assert_have_badge(badge_key, recipient = self.u2) self.u3.upvote(answer) self.assert_have_badge(badge_key, recipient = self.u2, expected_count = 1) - + #post another question and check that there are no new badges question2 = self.post_question(user = self.u1) answer2 = self.post_answer(user = self.u2, question = question2) - answer2.score = min_score - 1 + answer2.score = min_points - 1 answer2.save() self.u1.upvote(answer2) @@ -85,28 +85,28 @@ class BadgeTests(AskbotTestCase): expected_count = expected_count ) - def assert_upvoted_question_badge_works(self, + def assert_upvoted_question_badge_works(self, badge_key = None, - min_score = None, + min_points = None, multiple = False ): """test question badge where question author is the recipient where badge award is triggered by upvotes - * min_score - minimum # of upvotes required + * min_points - minimum # of upvotes required * multiple - multiple award or not * badge_key - key on askbot.models.badges.Badge object """ question = self.post_question(user = self.u1) - question.score = min_score - 1 + question.points = min_points - 1 question.save() self.u2.upvote(question) self.assert_have_badge(badge_key, recipient = self.u1) self.u3.upvote(question) self.assert_have_badge(badge_key, recipient = self.u1, expected_count = 1) - + #post another question and check that there are no new badges question2 = self.post_question(user = self.u1) - question2.score = min_score - 1 + question2.points = min_points - 1 question2.save() self.u2.upvote(question2) @@ -123,13 +123,13 @@ class BadgeTests(AskbotTestCase): def test_disciplined_badge(self): question = self.post_question(user = self.u1) - question.score = settings.DISCIPLINED_BADGE_MIN_UPVOTES + question.points = settings.DISCIPLINED_BADGE_MIN_UPVOTES question.save() self.u1.delete_question(question) self.assert_have_badge('disciplined', recipient = self.u1) question2 = self.post_question(user = self.u1) - question2.score = settings.DISCIPLINED_BADGE_MIN_UPVOTES + question2.points = settings.DISCIPLINED_BADGE_MIN_UPVOTES question2.save() self.u1.delete_question(question2) self.assert_have_badge('disciplined', recipient = self.u1, expected_count = 2) @@ -137,7 +137,7 @@ class BadgeTests(AskbotTestCase): def test_peer_pressure_badge(self): question = self.post_question(user = self.u1) answer = self.post_answer(user = self.u1, question = question) - answer.score = -1*settings.PEER_PRESSURE_BADGE_MIN_DOWNVOTES + answer.points = -1*settings.PEER_PRESSURE_BADGE_MIN_DOWNVOTES answer.save() self.u1.delete_answer(answer) self.assert_have_badge('peer-pressure', recipient = self.u1) @@ -145,21 +145,21 @@ class BadgeTests(AskbotTestCase): def test_teacher_badge(self): self.assert_upvoted_answer_badge_works( badge_key = 'teacher', - min_score = settings.TEACHER_BADGE_MIN_UPVOTES, + min_points = settings.TEACHER_BADGE_MIN_UPVOTES, multiple = False ) def test_nice_answer_badge(self): self.assert_upvoted_answer_badge_works( badge_key = 'nice-answer', - min_score = settings.NICE_ANSWER_BADGE_MIN_UPVOTES, + min_points = settings.NICE_ANSWER_BADGE_MIN_UPVOTES, multiple = True ) def test_nice_question_badge(self): self.assert_upvoted_question_badge_works( badge_key = 'nice-question', - min_score = settings.NICE_QUESTION_BADGE_MIN_UPVOTES, + min_points = settings.NICE_QUESTION_BADGE_MIN_UPVOTES, multiple = True ) @@ -227,7 +227,7 @@ class BadgeTests(AskbotTestCase): question = self.post_question(user = self.u1) answer = self.post_answer(user = self.u1, question = question) min_votes = settings.SELF_LEARNER_BADGE_MIN_UPVOTES - answer.score = min_votes - 1 + answer.points = min_votes - 1 answer.save() self.u2.upvote(answer) self.assert_have_badge('self-learner', recipient = self.u1) @@ -235,14 +235,14 @@ class BadgeTests(AskbotTestCase): #copy-paste of the first question, except expect second badge question = self.post_question(user = self.u1) answer = self.post_answer(user = self.u1, question = question) - answer.score = min_votes - 1 + answer.points = min_votes - 1 answer.save() self.u2.upvote(answer) self.assert_have_badge('self-learner', recipient = self.u1, expected_count = 2) question = self.post_question(user = self.u2) answer = self.post_answer(user = self.u1, question = question) - answer.score = min_votes - 1 + answer.points = min_votes - 1 answer.save() self.u2.upvote(answer) #no badge when asker != answerer @@ -282,13 +282,13 @@ class BadgeTests(AskbotTestCase): def assert_enlightened_badge_works(self, trigger): self.assert_accepted_answer_badge_works( 'enlightened', - min_score = settings.ENLIGHTENED_BADGE_MIN_UPVOTES, + min_points = settings.ENLIGHTENED_BADGE_MIN_UPVOTES, expected_count = 1, trigger = trigger ) self.assert_accepted_answer_badge_works( 'enlightened', - min_score = settings.ENLIGHTENED_BADGE_MIN_UPVOTES, + min_points = settings.ENLIGHTENED_BADGE_MIN_UPVOTES, expected_count = 1, previous_count = 1, trigger = trigger @@ -297,13 +297,13 @@ class BadgeTests(AskbotTestCase): def assert_guru_badge_works(self, trigger): self.assert_accepted_answer_badge_works( 'guru', - min_score = settings.GURU_BADGE_MIN_UPVOTES, + min_points = settings.GURU_BADGE_MIN_UPVOTES, expected_count = 1, trigger = trigger ) self.assert_accepted_answer_badge_works( 'guru', - min_score = settings.GURU_BADGE_MIN_UPVOTES, + min_points = settings.GURU_BADGE_MIN_UPVOTES, previous_count = 1, expected_count = 2, trigger = trigger @@ -330,8 +330,8 @@ class BadgeTests(AskbotTestCase): user = self.u2, question = question, timestamp = future - ) - answer.score = settings.NECROMANCER_BADGE_MIN_UPVOTES - 1 + ) + answer.points = settings.NECROMANCER_BADGE_MIN_UPVOTES - 1 answer.save() self.assert_have_badge('necromancer', self.u2, expected_count = 0) self.u1.upvote(answer) @@ -457,7 +457,7 @@ class BadgeTests(AskbotTestCase): self.u1.toggle_favorite_question(question) """no gaming""" self.assert_have_badge('stellar-question', self.u1, 0) - + def test_stellar_badge3(self): question = self.post_question(user = self.u1) settings.update('STELLAR_QUESTION_BADGE_MIN_STARS', 2) @@ -480,9 +480,9 @@ class BadgeTests(AskbotTestCase): self.post_comment(user = self.u1, parent_post = question) self.assert_have_badge('commentator', self.u1, 0) - self.post_comment(user = self.u1, parent_post = question) + self.post_comment(user = self.u1, parent_post = question) self.assert_have_badge('commentator', self.u1, 1) - self.post_comment(user = self.u1, parent_post = question) + self.post_comment(user = self.u1, parent_post = question) self.assert_have_badge('commentator', self.u1, 1) def test_taxonomist_badge(self): diff --git a/askbot/tests/db_api_tests.py b/askbot/tests/db_api_tests.py index 5ad26e8a..5477990a 100644 --- a/askbot/tests/db_api_tests.py +++ b/askbot/tests/db_api_tests.py @@ -9,6 +9,7 @@ from django.test.client import Client from django.conf import settings from django.contrib.auth.models import AnonymousUser from django import forms +from askbot import exceptions as askbot_exceptions from askbot.tests.utils import AskbotTestCase from askbot.tests.utils import with_settings from askbot import models @@ -38,6 +39,7 @@ class DBApiTests(AskbotTestCase): user = user, question = question, ) + return self.answer def assert_post_is_deleted(self, post): self.assertTrue(post.deleted == True) @@ -82,6 +84,25 @@ class DBApiTests(AskbotTestCase): ) return self.reload_object(q) + def test_user_cannot_post_two_answers(self): + question = self.post_question(user=self.user) + answer = self.post_answer(question=question, user=self.user) + self.assertRaises( + askbot_exceptions.AnswerAlreadyGiven, + self.post_answer, + question=question, + user=self.user + ) + + def test_user_can_post_answer_after_deleting_one(self): + question = self.post_question(user=self.user) + answer = self.post_answer(question=question, user=self.user) + self.user.delete_answer(answer=answer) + answer2 = self.post_answer(question=question, user=self.user) + answers = question.thread.get_answers(user=self.user) + self.assertEqual(answers.count(), 1) + self.assertEqual(answers[0], answer2) + def test_post_anonymous_question(self): q = self.ask_anonymous_question() self.assertTrue(q.is_anonymous) @@ -173,13 +194,13 @@ class DBApiTests(AskbotTestCase): count = models.Tag.objects.filter(name='one-tag').count() self.assertEquals(count, 0) - + @with_settings(MAX_TAG_LENGTH=200, MAX_TAGS_PER_POST=50) def test_retag_tags_too_long_raises(self): tags = "aoaoesuouooeueooeuoaeuoeou aostoeuoaethoeastn oasoeoa nuhoasut oaeeots aoshootuheotuoehao asaoetoeatuoasu o aoeuethut aoaoe uou uoetu uouuou ao aouosutoeh" question = self.post_question(user=self.user) self.assertRaises( - exceptions.ValidationError, + exceptions.ValidationError, self.user.retag_question, question=question, tags=tags ) @@ -415,10 +436,10 @@ class CommentTests(AskbotTestCase): def test_other_user_can_cancel_upvote(self): self.test_other_user_can_upvote_comment() comment = models.Post.objects.get_comments().get(id = self.comment.id) - self.assertEquals(comment.score, 1) + self.assertEquals(comment.points, 1) self.other_user.upvote(comment, cancel = True) comment = models.Post.objects.get_comments().get(id = self.comment.id) - self.assertEquals(comment.score, 0) + self.assertEquals(comment.points, 0) class GroupTests(AskbotTestCase): def setUp(self): @@ -526,7 +547,7 @@ class GroupTests(AskbotTestCase): #because answer groups always inherit thread groups self.edit_answer(user=self.u1, answer=answer, is_private=True) self.assertEqual(answer.groups.count(), 1) - + #here we have a simple case - the comment to answer was posted #by the answer author!!! #won't work when comment was by someone else diff --git a/askbot/tests/haystack_search_tests.py b/askbot/tests/haystack_search_tests.py new file mode 100644 index 00000000..7a8bfcfd --- /dev/null +++ b/askbot/tests/haystack_search_tests.py @@ -0,0 +1,101 @@ +"""Tests haystack indexes and queries""" +from django.core import exceptions +from django.conf import settings +from django.contrib.auth.models import User +from askbot.tests.utils import AskbotTestCase, skipIf +from askbot import models +import datetime + +class HaystackSearchTests(AskbotTestCase): + """tests methods on User object, + that were added for askbot + """ + def setUp(self): + self._old_value = getattr(settings, 'ENABLE_HAYSTACK_SEARCH', False) + setattr(settings, "ENABLE_HAYSTACK_SEARCH", True) + + self.user = self.create_user(username='gepeto') + self.other_user = self.create_user(username = 'pinocho') + self.other_user.location = 'Managua' + self.other_user.about = "I'm made of wood, gepeto made me" + self.other_user.save() + body_1 = '''Lorem turpis purus? Amet mattis eu et sociis phasellus + montes elementum proin ut urna enim, velit, tincidunt quis ut, + et integer mus? Nunc! Vut sed? Ac tincidunt egestas adipiscing, + magna et pulvinar mid est urna ultricies, turpis tristique nisi, + cum. Urna. Purus elit porttitor nisi porttitor ridiculus tincidunt + amet duis, gepeto''' + #from Baldy of Nome by Esther Birdsall Darling + body_2 = ''' With unseeing eyes and dragging steps, the boy trudged along the snowy + trail, dreading the arrival at Golconda Camp. For there was the House of + Judgment, where all of the unfortunate events of that most unhappy day + would be reviewed sternly, lorem''' + self.question1 = self.post_question( + user=self.user, + body_text=body_1, + title='Test title 1' + ) + self.question2 = self.post_question( + user=self.other_user, + body_text=body_2, + title='Test title 2, Baldy of Nome' + ) + self.answer1 = self.post_answer( + user=self.user, + question = self.question1, + body_text="This is a answer for question 1" + ) + self.answer1 = self.post_answer( + user=self.other_user, + question = self.question2, + body_text="Just a random text to fill the space" + ) + + def tearDown(self): + setattr(settings, "ENABLE_HAYSTACK_SEARCH", self._old_value) + + @skipIf('haystack' not in settings.INSTALLED_APPS, + 'Haystack not setup') + def test_title_search(self): + #title search + title_search_qs = models.Thread.objects.get_for_query('title') + title_search_qs_2 = models.Thread.objects.get_for_query('Nome') + self.assertEquals(title_search_qs.count(), 2) + self.assertEquals(title_search_qs_2.count(), 1) + + @skipIf('haystack' not in settings.INSTALLED_APPS, + 'Haystack not setup') + def test_body_search(self): + + #bodysearch + body_search_qs = models.Thread.objects.get_for_query('Lorem') + self.assertEquals(body_search_qs.count(), 2) + body_search_qs_2 = models.Thread.objects.get_for_query('steps') + self.assertEquals(body_search_qs_2.count(), 1) + + @skipIf('haystack' not in settings.INSTALLED_APPS, + 'Haystack not setup') + def test_user_profile_search(self): + #must return pinocho + user_profile_qs = models.get_users_by_text_query('wood') + self.assertEquals(user_profile_qs.count(), 1) + + #returns both gepeto and pinocho because gepeto nickname + #and gepeto name in pinocho's profile + user_profile_qs = models.get_users_by_text_query('gepeto') + self.assertEquals(user_profile_qs.count(), 2) + + @skipIf('haystack' not in settings.INSTALLED_APPS, + 'Haystack not setup') + def test_get_django_queryset(self): + '''makes a query that can return multiple models and test + get_django_queryset() method from AskbotSearchQuerySet''' + #gepeto is present in profile and in question + from askbot.search.haystack import AskbotSearchQuerySet + qs = AskbotSearchQuerySet().filter(content='gepeto').get_django_queryset(User) + for instance in qs: + self.assertTrue(isinstance(instance, User)) + + qs = AskbotSearchQuerySet().filter(content='gepeto').get_django_queryset(models.Thread) + for instance in qs: + self.assertTrue(isinstance(instance, models.Thread)) diff --git a/askbot/tests/page_load_tests.py b/askbot/tests/page_load_tests.py index 3805d012..4efac0f0 100644 --- a/askbot/tests/page_load_tests.py +++ b/askbot/tests/page_load_tests.py @@ -166,13 +166,15 @@ class PageLoadTestCase(AskbotTestCase): group.save() user = self.create_user('user') user.join_group(group) - self.post_question(user=user, title='alibaba', group_id=group.id) + question = self.post_question(user=user, title='alibaba', group_id=group.id) + #ask for data anonymously - should get nothing query_data = {'query': 'alibaba'} response = self.client.get(reverse('api_get_questions'), query_data) response_data = simplejson.loads(response.content) self.assertEqual(len(response_data), 0) + #log in - should get the question self.client.login(method='force', user_id=user.id) response = self.client.get(reverse('api_get_questions'), query_data) response_data = simplejson.loads(response.content) diff --git a/askbot/tests/post_model_tests.py b/askbot/tests/post_model_tests.py index 1a3a9c49..e61fcd2d 100644 --- a/askbot/tests/post_model_tests.py +++ b/askbot/tests/post_model_tests.py @@ -618,7 +618,7 @@ class ThreadRenderCacheUpdateTests(AskbotTestCase): def test_question_upvote_downvote(self): question = self.post_question() - question.score = 5 + question.points = 5 question.vote_up_count = 7 question.vote_down_count = 2 question.save() @@ -631,7 +631,7 @@ class ThreadRenderCacheUpdateTests(AskbotTestCase): data = simplejson.loads(response.content) self.assertEqual(1, data['success']) - self.assertEqual(6, data['count']) # 6 == question.score(5) + 1 + self.assertEqual(6, data['count']) # 6 == question.points(5) + 1 thread = Thread.objects.get(id=question.thread.id) @@ -647,7 +647,7 @@ class ThreadRenderCacheUpdateTests(AskbotTestCase): data = simplejson.loads(response.content) self.assertEqual(1, data['success']) - self.assertEqual(5, data['count']) # 6 == question.score(6) - 1 + self.assertEqual(5, data['count']) # 6 == question.points(6) - 1 thread = Thread.objects.get(id=question.thread.id) diff --git a/askbot/tests/question_views_tests.py b/askbot/tests/question_views_tests.py new file mode 100644 index 00000000..b1836f9e --- /dev/null +++ b/askbot/tests/question_views_tests.py @@ -0,0 +1,207 @@ +from bs4 import BeautifulSoup +from askbot.conf import settings as askbot_settings +from askbot import const +from askbot.tests.utils import AskbotTestCase +from askbot import models +from askbot.models.tag import get_global_group +from django.core.urlresolvers import reverse + + +class PrivateQuestionViewsTests(AskbotTestCase): + + def setUp(self): + self._backup = askbot_settings.GROUPS_ENABLED + askbot_settings.update('GROUPS_ENABLED', True) + self.user = self.create_user('user') + self.group = models.Group.objects.create( + name='the group', openness=models.Group.OPEN + ) + self.user.join_group(self.group) + self.qdata = { + 'title': 'test question title', + 'text': 'test question text' + } + self.client.login(user_id=self.user.id, method='force') + + def tearDown(self): + askbot_settings.update('GROUPS_ENABLED', self._backup) + + def test_post_private_question(self): + data = self.qdata + data['post_privately'] = 'checked' + response1 = self.client.post(reverse('ask'), data=data) + response2 = self.client.get(response1['location']) + dom = BeautifulSoup(response2.content) + title = dom.find('h1').text + self.assertTrue(const.POST_STATUS['private'] in title) + question = models.Thread.objects.get(id=1) + self.assertEqual(question.title, self.qdata['title']) + self.assertFalse(get_global_group() in set(question.groups.all())) + + #private question is not accessible to unauthorized users + self.client.logout() + response = self.client.get(question._question_post().get_absolute_url()) + self.assertEqual(response.status_code, 302) + self.assertEqual(response.content, '') + #private question link is not shown on the main page + #to unauthorized users + response = self.client.get(reverse('questions')) + self.assertFalse(self.qdata['title'] in response.content) + #private question link is not shown on the poster profile + #to the unauthorized users + response = self.client.get(self.user.get_profile_url()) + self.assertFalse(self.qdata['title'] in response.content) + + def test_publish_private_question(self): + question = self.post_question(user=self.user, is_private=True) + title = question.thread.get_title() + self.assertTrue(const.POST_STATUS['private'] in title) + data = self.qdata + #data['post_privately'] = 'false' + data['select_revision'] = 'false' + data['text'] = 'edited question text' + response1 = self.client.post( + reverse('edit_question', kwargs={'id':question.id}), + data=data + ) + response2 = self.client.get(question.get_absolute_url()) + dom = BeautifulSoup(response2.content) + title = dom.find('h1').text + self.assertTrue(get_global_group() in set(question.groups.all())) + self.assertEqual(title, self.qdata['title']) + + self.client.logout() + response = self.client.get(question.get_absolute_url()) + self.assertTrue('edited question text' in response.content) + + def test_privatize_public_question(self): + question = self.post_question(user=self.user) + title = question.thread.get_title() + self.assertFalse(const.POST_STATUS['private'] in title) + data = self.qdata + data['post_privately'] = 'checked' + data['select_revision'] = 'false' + response1 = self.client.post( + reverse('edit_question', kwargs={'id':question.id}), + data=data + ) + response2 = self.client.get(question.get_absolute_url()) + dom = BeautifulSoup(response2.content) + title = dom.find('h1').text + self.assertFalse(get_global_group() in set(question.groups.all())) + self.assertTrue(const.POST_STATUS['private'] in title) + + def test_private_checkbox_is_on_when_editing_private_question(self): + question = self.post_question(user=self.user, is_private=True) + response = self.client.get( + reverse('edit_question', kwargs={'id':question.id}) + ) + dom = BeautifulSoup(response.content) + checkbox = dom.find( + 'input', attrs={'type': 'checkbox', 'name': 'post_privately'} + ) + self.assertEqual(checkbox['checked'], 'checked') + + def test_private_checkbox_is_off_when_editing_public_question(self): + question = self.post_question(user=self.user) + response = self.client.get( + reverse('edit_question', kwargs={'id':question.id}) + ) + dom = BeautifulSoup(response.content) + checkbox = dom.find( + 'input', attrs={'type': 'checkbox', 'name': 'post_privately'} + ) + self.assertEqual(checkbox.get('checked', False), False) + + +class PrivateAnswerViewsTests(AskbotTestCase): + + def setUp(self): + self._backup = askbot_settings.GROUPS_ENABLED + askbot_settings.update('GROUPS_ENABLED', True) + self.user = self.create_user('user') + group = models.Group.objects.create( + name='the group', openness=models.Group.OPEN + ) + self.user.join_group(group) + self.question = self.post_question(user=self.user) + self.client.login(user_id=self.user.id, method='force') + + def tearDown(self): + askbot_settings.update('GROUPS_ENABLED', self._backup) + + def test_post_private_answer(self): + response1 = self.client.post( + reverse('answer', kwargs={'id': self.question.id}), + data={'text': 'some answer text', 'post_privately': 'checked'} + ) + answer = self.question.thread.get_answers(user=self.user)[0] + self.assertFalse(get_global_group() in set(answer.groups.all())) + self.client.logout() + + user2 = self.create_user('user2') + self.client.login(user_id=user2.id, method='force') + response = self.client.get(self.question.get_absolute_url()) + self.assertFalse('some answer text' in response.content) + + self.client.logout() + response = self.client.get(self.question.get_absolute_url()) + self.assertFalse('some answer text' in response.content) + + + def test_private_checkbox_is_on_when_editing_private_answer(self): + answer = self.post_answer( + question=self.question, user=self.user, is_private=True + ) + response = self.client.get( + reverse('edit_answer', kwargs={'id': answer.id}) + ) + dom = BeautifulSoup(response.content) + checkbox = dom.find( + 'input', attrs={'type': 'checkbox', 'name': 'post_privately'} + ) + self.assertEqual(checkbox['checked'], 'checked') + + def test_privaet_checkbox_is_off_when_editing_public_answer(self): + answer = self.post_answer(question=self.question, user=self.user) + response = self.client.get( + reverse('edit_answer', kwargs={'id': answer.id}) + ) + dom = BeautifulSoup(response.content) + checkbox = dom.find( + 'input', attrs={'type': 'checkbox', 'name': 'post_privately'} + ) + self.assertEqual(checkbox.get('checked', False), False) + + def test_publish_private_answer(self): + answer = self.post_answer( + question=self.question, user=self.user, is_private=True + ) + self.client.post( + reverse('edit_answer', kwargs={'id': answer.id}), + data={'text': 'edited answer text', 'select_revision': 'false'} + ) + answer = self.reload_object(answer) + self.assertTrue(get_global_group() in answer.groups.all()) + self.client.logout() + response = self.client.get(self.question.get_absolute_url()) + self.assertTrue('edited answer text' in response.content) + + + def test_privatize_public_answer(self): + answer = self.post_answer(question=self.question, user=self.user) + self.client.post( + reverse('edit_answer', kwargs={'id': answer.id}), + data={ + 'text': 'edited answer text', + 'post_privately': 'checked', + 'select_revision': 'false' + } + ) + #check that answer is not visible to the "everyone" group + answer = self.reload_object(answer) + self.assertFalse(get_global_group() in answer.groups.all()) + #check that countent is not seen by an anonymous user + self.client.logout() + response = self.client.get(self.question.get_absolute_url()) + self.assertFalse('edited answer text' in response.content) diff --git a/askbot/tests/user_views_tests.py b/askbot/tests/user_views_tests.py new file mode 100644 index 00000000..489cf76a --- /dev/null +++ b/askbot/tests/user_views_tests.py @@ -0,0 +1,39 @@ +from askbot.tests.utils import AskbotTestCase +from askbot.views.users import owner_or_moderator_required +from django.contrib.auth.models import AnonymousUser +from django.core.urlresolvers import reverse +from django.http import HttpResponseRedirect +from mock import Mock +import urllib +import urlparse + +class UserViewsTests(AskbotTestCase): + + def test_owner_or_mod_required_passes_url_parameters(self): + @owner_or_moderator_required + def mock_view(request, user, context): + return None + + request = Mock(spec=('path', 'REQUEST', 'user')) + request.user = AnonymousUser() + request.REQUEST = {'abra': 'cadabra', 'foo': 'bar'} + request.path = '/some/path/' + user = self.create_user('user') + response = mock_view(request, user, {}) + self.assertEqual(isinstance(response, HttpResponseRedirect), True) + + url = response['location'] + parsed_url = urlparse.urlparse(url) + + self.assertEqual(parsed_url.path, reverse('user_signin')) + + next = dict(urlparse.parse_qsl(parsed_url.query))['next'] + next_url = urllib.unquote(next) + parsed_url = urlparse.urlparse(next_url) + + self.assertEqual(parsed_url.path, request.path) + + query = dict(urlparse.parse_qsl(parsed_url.query)) + self.assertEqual(set(query.keys()), set(['foo', 'abra'])) + self.assertEqual(set(query.values()), set(['bar', 'cadabra'])) + self.assertEqual(query['abra'], 'cadabra') |