def test_get_list_renders_correct_comments(self): template_loader.templates['page/comment_list.html'] = '' c = create_comment(self.publishable, self.publishable.content_type) c2 = create_comment(self.publishable, self.publishable.content_type) response = self.client.get(self.get_url()) tools.assert_equals(200, response.status_code) tools.assert_equals([c, c2], list(response.context['comment_list']))
def test_get_comment_for_user(self): boy = User.objects.create(username='******') girl = User.objects.create(username='******') first = create_comment(self.publishable, self.publishable.content_type, comment='first', user=boy) second = create_comment(self.publishable, self.publishable.content_type, comment='second', user=girl) tools.assert_equals(first, views.update_comment.get_comment_for_user(self.publishable, boy, first.pk)) tools.assert_raises(comments.get_model().DoesNotExist, lambda: views.update_comment.get_comment_for_user(self.publishable, boy, 1024)) tools.assert_raises(comments.get_model().DoesNotExist, lambda: views.update_comment.get_comment_for_user(self.publishable, boy, second.pk))
def test_aa(self): day = datetime.now().strftime('%Y%m%d') create_comment(self.publishable, self.publishable.content_type, user_name='kvbik', submit_date=utc_localize(datetime(2010, 10, 10, 10, 10, 10))) ct_id = self.publishable.content_type_id tools.assert_equals(set([ 'slidingccount:WINDOWS', 'slidingccount:KEYS', 'comcount:2', 'lastcom:2', 'slidingccount:2', 'comcount:c:1', 'comcount:c:2', 'lastcom:c:1', 'lastcom:c:2', 'slidingccount:c:1', 'slidingccount:c:2', 'lastcom:d:1', 'lastcom:d:2', 'comcount:d:1', 'comcount:d:2', 'slidingccount:d:1', 'slidingccount:d:2', 'lastcom:ct:%d' % ct_id, 'comcount:ct:%d' % ct_id, 'slidingccount:ct:%d' % ct_id, 'lastcom:pub:%d:1' % ct_id, 'comcount:pub:%d:1' % ct_id, 'slidingccount:2:%s' % day, 'slidingccount:c:1:%s' % day, 'slidingccount:c:2:%s' % day, 'slidingccount:d:1:%s' % day, 'slidingccount:d:2:%s' % day, 'slidingccount:ct:%d:%s' % (ct_id, day), ]), set(client.keys('*'))) if use_tz: # timestamps are stored in utc time tstamp = '1286705410.0' else: tstamp = '1286698210.0' tools.assert_equals({'submit_date': tstamp, 'user_id': '', 'username': '******', 'comment': '', 'url': ''}, client.hgetall('lastcom:pub:%d:1' % ct_id)) tools.assert_equals('1', client.get('comcount:pub:%d:1' % ct_id))
def setUp(self): super(TestSignals, self).setUp() reset_signaled_comment() comment_removed.connect(handle_signal) create_basic_categories(self) create_and_place_a_publishable(self) self.comment = create_comment(self.publishable, self.publishable.content_type)
def test_post_works_for_correct_data_with_parent(self): c = create_comment(self.publishable, self.publishable.content_type) form = comments.get_form()(target_object=self.publishable, parent=c.pk) response = self.client.post(self.get_url('new'), self.get_form_data(form)) tools.assert_equals(302, response.status_code) tools.assert_equals(2, comments.get_model().objects.count()) child = comments.get_model().objects.exclude(pk=c.pk)[0] tools.assert_equals(c, child.parent)
def _create_comment(self): " Util method to create and return a new commnt. " return create_comment( self.publishable, self.publishable.content_type, user_name='kvbik', submit_date=utc_localize(datetime(2010, 10, 10, 10, 10, 10)) )
def test_post_passes_parent_on_get_to_template_if_specified(self): template_loader.templates['page/comment_form.html'] = '' c = create_comment(self.publishable, self.publishable.content_type) response = self.client.get(self.get_url('new', c.pk)) tools.assert_equals(200, response.status_code) tools.assert_true('parent' in response.context) tools.assert_equals(c, response.context['parent']) form = response.context['form'] tools.assert_equals(str(c.pk), form.parent)
def test_correct_page_in_redirect(self): " Assert that `views.comment_detail()` returns an HttpResponseRedirect to the expected location. " # Create a handful of comments for i in range(0, 22): create_comment(self.publishable, self.publishable.content_type) tools.assert_false(hasattr(self.publishable, 'results_per_page')) tools.assert_false(hasattr(self.publishable, 'reverse_comment_ordering')) # Call the `comment_detail()` view to get the absolute url for comment.id == 4 passing no kwargs # `results_per_page` should be 10 # `reverse_ordering` should be True comment_4 = comments.get_model().objects.get(pk=4) response = self.client.get(self.get_url(comment_4.id)) tools.assert_equals(response.status_code, 302) url = urlparse(response['location']) tools.assert_equals(url.path, comment_4.content_object.get_absolute_url()) tools.assert_equals(url.query, 'p=%d&comment_id=%s' % (2, comment_4.id)) tools.assert_equals(url.fragment, str(comment_4.id)) # Call the `comment_detail()` view to get the absolute url for comment.id == 4 passing `reverse_ordering` = False # `results_per_page` should be 10 # `reverse_ordering` should be False comment_4 = comments.get_model().objects.get(pk=4) response = self.client.get(self.get_url(comment_4.id), {'reverse_ordering': ''}) tools.assert_equals(response.status_code, 302) url = urlparse(response['location']) tools.assert_equals(url.path, comment_4.content_object.get_absolute_url()) tools.assert_equals(url.query, 'p=%d&comment_id=%s' % (1, comment_4.id)) tools.assert_equals(url.fragment, str(comment_4.id)) # Call the `comment_detail()` view to get the absolute url for comment.id == 4 passing `results_per_page` = 1 # `results_per_page` should be 1 # `reverse_ordering` should be True comment_4 = comments.get_model().objects.get(pk=4) response = self.client.get(self.get_url(comment_4.id), {'results_per_page': '1'}) tools.assert_equals(response.status_code, 302) url = urlparse(response['location']) tools.assert_equals(url.path, comment_4.content_object.get_absolute_url()) tools.assert_equals(url.query, 'p=%d&comment_id=%s' % (19, comment_4.id)) tools.assert_equals(url.fragment, str(comment_4.id))
def post_comment_as_logged_in_user(self): c = create_comment(self.publishable, self.publishable.content_type) boy = User.objects.create(username='******', email='*****@*****.**') boy.set_password('boy') boy.save() self.client.login(username='******', password='******') form = comments.get_form()(target_object=self.publishable, parent=c.pk) data = { 'name': '', 'email': '', } response = self.client.post(self.get_url('new'), self.get_form_data(form, **data)) tools.assert_equals(302, response.status_code) tools.assert_equals(2, comments.get_model().objects.count()) child = comments.get_model().objects.exclude(pk=c.pk)[0] tools.assert_equals(u'boy', child.user_name) tools.assert_equals(u'*****@*****.**', child.user_email)
def test_get_list_returns_first_page_with_no_params(self): template_loader.templates['page/comment_list.html'] = '' a = create_comment(self.publishable, self.publishable.content_type) d = create_comment(self.publishable, self.publishable.content_type) ab = create_comment(self.publishable, self.publishable.content_type, parent_id=a.pk) de = create_comment(self.publishable, self.publishable.content_type, parent_id=d.pk) def_ = create_comment(self.publishable, self.publishable.content_type, parent_id=de.pk) ac = create_comment(self.publishable, self.publishable.content_type, parent_id=a.pk) response = self.client.get(self.get_url()) tools.assert_equals(200, response.status_code) tools.assert_equals([a, ab, ac], list(response.context['comment_list']))
def test_get_list_returns_second_page_if_asked_to(self): template_loader.templates['page/comment_list.html'] = '' a = create_comment(self.publishable, self.publishable.content_type) d = create_comment(self.publishable, self.publishable.content_type) ab = create_comment(self.publishable, self.publishable.content_type, parent_id=a.pk) de = create_comment(self.publishable, self.publishable.content_type, parent_id=d.pk) def_ = create_comment(self.publishable, self.publishable.content_type, parent_id=de.pk) ac = create_comment(self.publishable, self.publishable.content_type, parent_id=a.pk) response = self.client.get(self.get_url(), {'p': 2}) tools.assert_equals(200, response.status_code) tools.assert_equals([d, de, def_], list(response.context['comment_list']))
def test_get_list_renders_only_given_branches_if_asked_to(self): template_loader.templates['page/comment_list.html'] = '' a = create_comment(self.publishable, self.publishable.content_type) d = create_comment(self.publishable, self.publishable.content_type) ab = create_comment(self.publishable, self.publishable.content_type, parent_id=a.pk) de = create_comment(self.publishable, self.publishable.content_type, parent_id=d.pk) def_ = create_comment(self.publishable, self.publishable.content_type, parent_id=de.pk) ac = create_comment(self.publishable, self.publishable.content_type, parent_id=a.pk) response = self.client.get(self.get_url() + '?ids=%s&ids=%s' % (a.pk, d.pk)) tools.assert_equals(200, response.status_code) tools.assert_equals([a, ab, ac, d, de, def_], list(response.context['comment_list']))
def test_get_list_renders_correct_comments_including_tree_order(self): template_loader.templates['page/comment_list.html'] = '' a = create_comment(self.publishable, self.publishable.content_type) d = create_comment(self.publishable, self.publishable.content_type) ab = create_comment(self.publishable, self.publishable.content_type, parent_id=a.pk) de = create_comment(self.publishable, self.publishable.content_type, parent_id=d.pk) def_ = create_comment(self.publishable, self.publishable.content_type, parent_id=de.pk) ac = create_comment(self.publishable, self.publishable.content_type, parent_id=a.pk) response = self.client.get(self.get_url()) tools.assert_equals(200, response.status_code) tools.assert_equals([a, ab, ac, d, de, def_], list(response.context['comment_list']))
def test_user_doesnt_pass_test(self): """ Assert that 404 is raised if the `COMMENTS_ALLOW_MODERATOR_UPDATE` setting is True but the user editing the comment is NOT the comment owner and does NOT pass the `user_can_access_comment` method. """ # Assert that moderator update flags are set as expected tools.assert_true(hasattr(settings, 'COMMENTS_ALLOW_UPDATE')) tools.assert_true(hasattr(settings, 'COMMENTS_ALLOW_MODERATOR_UPDATE')) # Create a new comment with user_foo comment = create_comment(self.publishable, self.publishable.content_type, comment='first', user=self.user_foo) # Try to edit this comment with user_bar and assert it raises 404 b/c COMMENTS_ALLOW_MODERATOR_UPDATE is set to False self.client.login(username='******', password='******') response = self.client.get(self.get_url('update', comment.id)) tools.assert_equals(404, response.status_code)
def test_results_per_page_kwarg_is_passed_correctly(self, mock__get_results_per_page): " Assert that `views._get_results_per_page()` is called with the expected args from `views.comment_detail()`. " comment_1 = create_comment(self.publishable, self.publishable.content_type) # 1. Call the `comment_detail()` passing no kwargs response = self.client.get(self.get_url(comment_1.id)) tools.assert_equals(response.status_code, 302) # Assert that `_get_results_per_page()` was called with the publishable and the default `results_per_page` value (which is 10) mock__get_results_per_page.assert_called_once_with(self.publishable, 10) mock__get_results_per_page.reset_mock() # 2. Call the `comment_detail()` passing passing `results_per_page` kwarg response = self.client.get(self.get_url(comment_1.id), {'results_per_page': 25}) tools.assert_equals(response.status_code, 302) # Assert that `_get_results_per_page()` was called with the publishable and the passed `results_per_page` kwarg which is 25 mock__get_results_per_page.assert_called_once_with(self.publishable, 25)
def test_reverse_ordering_kwarg_is_passed_correctly(self, mock__get_comment_order): " Assert that `views._get_comment_order()` is called with the expected args. " comment_1 = create_comment(self.publishable, self.publishable.content_type) # 1. Call the `comment_detail()` passing no kwargs response = self.client.get(self.get_url(comment_1.id)) tools.assert_equals(response.status_code, 302) # Assert that `_get_comment_order()` was called with the publishable and the default `reverse_ordering` value (which is False) mock__get_comment_order.assert_called_once_with(self.publishable, None) mock__get_comment_order.reset_mock() # 2. Call the `comment_detail()` passing passing `reverse_ordering` kwarg response = self.client.get(self.get_url(comment_1.id), {'reverse_ordering': '1'}) tools.assert_equals(response.status_code, 302) # Assert that `_get_comment_order()` was called with the publishable and the passed `reverse_ordering` kwarg (which is True) mock__get_comment_order.assert_called_once_with(self.publishable, True)
def test_non_moderator_allowed_edits(self): """ Assert that 404 is raised if the `COMMENTS_ALLOW_MODERATOR_UPDATE` setting is False or does not exist and the user editing the comment is NOT the comment owner. """ # Assert that moderator update flags are set as expected tools.assert_true(hasattr(settings, 'COMMENTS_ALLOW_UPDATE')) # Patch the COMMENTS_ALLOW_MODERATOR_UPDATE for the test settings.COMMENTS_ALLOW_MODERATOR_UPDATE = False # Create a new comment with user_foo comment = create_comment(self.publishable, self.publishable.content_type, comment='first', user=self.user_foo) # Try to edit this comment with user_bar and assert it raises 404 b/c COMMENTS_ALLOW_MODERATOR_UPDATE is set to False self.client.login(username='******', password='******') response = self.client.get(self.get_url('update', comment.id)) tools.assert_equals(404, response.status_code) # Reset the COMMENTS_ALLOW_MODERATOR_UPDATE value settings.COMMENTS_ALLOW_MODERATOR_UPDATE = True
def test_comment_updated_signal(self, mock_signal): " Assert that the `comment_updated` signal is sent as expected after a comment is edited. " # Create a new comment with user_foo comment = create_comment(self.publishable, self.publishable.content_type, comment='first', user=self.user_foo) # Create a new staff user and assert that they pass the default test user_staff = User.objects.create_superuser(username='******', email='*****@*****.**', password='******') tools.assert_true(views.update_comment.user_can_access_comment(user_staff)) # Instantiate the edit form with form = comments.get_form()(target_object=self.publishable) form_data = self.get_form_data(form) UPDATED_COMMENT_TEXT = 'update me!' form_data.update({'comment': UPDATED_COMMENT_TEXT}) # Try to edit this comment with user_bar (whos IS staff) self.client.login(username='******', password='******') self.client.post(self.get_url('update', comment.id), form_data) # Assert that the `comment_updated` signal was called mock_signal.send.assert_called()
def test_user_passes_test(self): """ Assert that the user CAN edit the comment if they are not the owner but they do have the appropriate permissions (and if the apporpriate settings are configured). """ # Assert that moderator update flags are set as expected tools.assert_true(hasattr(settings, 'COMMENTS_ALLOW_UPDATE')) tools.assert_true(hasattr(settings, 'COMMENTS_ALLOW_MODERATOR_UPDATE')) # Create a new comment with user_foo comment = create_comment(self.publishable, self.publishable.content_type, comment='first', user=self.user_foo) # Create a new staff user and assert that they pass the default test user_staff = User.objects.create_superuser(username='******', email='*****@*****.**', password='******') tools.assert_true(views.update_comment.user_can_access_comment(user_staff)) # Try to edit this comment with user_bar (whos IS staff) self.client.login(username='******', password='******') response = self.client.get(self.get_url('update', comment.id)) # Assert that the response is 200 and that the appropriate tpl is being called tools.assert_equals(200, response.status_code) tools.assert_equals('page/comment_update.html', response.template.name)
def test_comment_update_with_post(self): " Assert that a POST request with valid data successfully updates the comment. " # Create a new comment with user_foo comment = create_comment(self.publishable, self.publishable.content_type, comment='first', user=self.user_foo) # Create a new staff user and assert that they pass the default test user_staff = User.objects.create_superuser(username='******', email='*****@*****.**', password='******') tools.assert_true(views.update_comment.user_can_access_comment(user_staff)) # Instantiate the edit form with form = comments.get_form()(target_object=self.publishable) form_data = self.get_form_data(form) UPDATED_COMMENT_TEXT = 'update me!' form_data.update({'comment': UPDATED_COMMENT_TEXT}) # Try to edit this comment with user_bar (whos IS staff) self.client.login(username='******', password='******') self.client.post(self.get_url('update', comment.id), form_data) # Refetch the comment from the orm and assert that it has indeed been updated comment = comments.get_model().objects.get(id=comment.id) tools.assert_equals(UPDATED_COMMENT_TEXT, comment.comment) tools.assert_equals(self.user_foo, comment.user)
def test_default_comment_options_for_article(self): create_comment(self.publishable, self.publishable.content_type) t = template.Template('''{% load ellacomments_tags %}{% get_comment_options for obj as opts %}{% if not opts.blocked %}XX{% endif %}''') tools.assert_equals(u'XX', t.render(template.Context({'obj': self.only_publishable})))
def test_comment_list_for_article_is_picked_up_through_publishable(self): create_comment(self.publishable, self.publishable.content_type) t = template.Template('''{% load ellacomments_tags %}{% get_comment_list for obj as var_name%}{{ var_name|length }}''') tools.assert_equals('1', t.render(template.Context({'obj': self.only_publishable})))
def test_get_update_comment_form(self): comment = create_comment(self.publishable, self.publishable.content_type, comment='some comment') form = views.update_comment.get_update_comment_form(self.publishable, comment, None) tools.assert_equals('some comment', form.initial['comment'])
def test_comment_count_for_article_is_picked_up_through_article(self): create_comment(self.publishable, self.publishable.content_type) # put some value directly to redis, so we know it was taken from there client.set(COMCOUNT_KEY % (self.publishable.content_type.pk, self.publishable.pk), '10') t = template.Template('''{% load ellacomments_tags %}{% get_comment_count for obj as var_name%}{{ var_name }}''') tools.assert_equals('10', t.render(template.Context({'obj': self.publishable})))