def test_comment_traversal(self): """Test comment rendering pages""" # Create a couple of comments to traverse comment = models.Post.objects.create(title="Test", author=self.owner, content="Test", type=models.Post.COMMENT, root=self.post, parent=self.post) comment2 = models.Post.objects.create(title="Test", author=self.owner, content="Test", type=models.Post.COMMENT, root=self.post, parent=comment) url = reverse("post_view", kwargs=dict(uid=self.post.uid)) request = fake_request(url=url, data={}, user=self.owner) response = views.post_view(request=request, uid=self.post.uid) self.assertTrue(response.status_code == 200, 'Error rendering comments')
def test_drag_and_drop(self): """ Test AJAX function used to drag and drop. """ # Create comment to self.parent comment1 = models.Post.objects.create(title="Test", author=self.owner, content="Test", type=models.Post.COMMENT, root=self.post, parent=self.post) # Create another post with parent being above comment comment2 = models.Post.objects.create(title="Test", author=self.owner, content="Test", type=models.Post.COMMENT, root=self.post, parent=comment1) comment3 = models.Post.objects.create(title="Test", author=self.owner, content="Test", type=models.Post.COMMENT, root=self.post, parent=comment2) # Move comment3 to comment1 data = {'parent': comment1.uid, 'uid': comment3.uid} url = reverse('drag_and_drop') request = fake_request(url=url, data=data, user=self.owner) json_response = ajax.drag_and_drop(request) self.process_response(json_response)
def preform_votes(self, post, user): for vtype in ["upvote", "bookmark", "accept"]: data = {"vote_type": vtype, "post_uid": post.uid} request = fake_request(url=reverse('vote'), data=data, user=user) response = ajax.ajax_vote(request) self.assertEqual(response.status_code, 200, f"Could not preform vote:{vtype}.")
def test_digest(self): """ Test AJAX function that toggles users digest options """ data = {'pref': 'daily'} url = reverse('ajax_digest') request = fake_request(url=url, data=data, user=self.owner) json_response = ajax.ajax_digest(request) self.process_response(json_response)
def test_post_answer(self): """ Test submitting answer through the post view """ url = reverse("post_view", kwargs=dict(uid=self.post.uid)) # Get form data data = dict(content="testing answer", parent_uid=self.post.uid) request = fake_request(url=url, data=data, user=self.owner) response = views.post_view(request=request, uid=self.post.uid) return
def test_duplicate_post(self): "Test duplicate post moderation" data = {"dupe": "google.com"} url = reverse('post_moderate', kwargs=dict(uid=self.post.uid)) request = fake_request(url=url, data=data, user=self.owner) response = views.post_moderate(request=request, uid=self.post.uid) self.process_response(response) pass
def moderate(self, choices, post, extra={}): for action in choices: data = {"action": action} data.update(extra) url = reverse('post_moderate', kwargs=dict(uid=post.uid)) request = fake_request(url=url, data=data, user=self.owner) response = views.post_moderate(request=request, uid=post.uid) self.process_response(response) return
def test_ajax_subs(self): for stype in ["unfollow", "messages", "email", "all", "default"]: data = {"sub_type": stype, "root_uid": self.post.uid} request = fake_request(url=reverse('vote'), data=data, user=self.owner) response = ajax.ajax_subs(request) self.assertEqual( response.status_code, 200, f"Could not preform subscription action:{stype}.")
def test_similar_posts(self): """ Test AJAX function used to produce the similar posts sidebar. """ data = {'query': 'Test'} url = reverse('similar_posts', kwargs=dict(uid=self.post.uid)) request = fake_request(url=url, data=data, user=self.owner, method='GET') toplevel_response = ajax.similar_posts(request, uid=self.post.uid) self.process_response(toplevel_response)
def test_inplace_form(self): """ Test AJAX function used to render inplace form. """ data = {'parent': self.post.uid, 'uid': self.post.uid, 'top': '1'} url = reverse('inplace_form') request = fake_request(url=url, data=data, user=self.owner, method='GET') toplevel_response = ajax.inplace_form(request) self.process_response(toplevel_response)
def test_inplace_create(self): """ Test AJAX function used to preform """ nontoplevel_data = { 'content': "Addin a new comment foo bar", 'parent': self.post.uid } url = reverse('ajax_comment_create') request = fake_request(url=url, data=nontoplevel_data, user=self.owner) nontoplevel_response = ajax.ajax_comment_create(request) self.process_response(nontoplevel_response)
def test_inplace_edit(self): """ Test AJAX function for inplace edits """ data = { 'content': 'New content here foo bar bar', 'title': 'New title here foo bar bar', 'post_type': models.Post.FORUM, 'tag_val': ['new', 'tag'] } url = reverse('ajax_edit', kwargs=dict(uid=self.post.uid)) request = fake_request(url=url, data=data, user=self.owner) json_response = ajax.ajax_edit(request, uid=self.post.uid) self.process_response(json_response)
def test_post_create(self): """Test post creation with POST request""" content = f"@{self.owner.username} " + "testing " * 10 # Create fake request data = { 'post_type': models.Post.QUESTION, 'title': 'title tested post', "tag_val": "tested,test3", "content": content } request = fake_request(url=reverse('post_create'), data=data, user=self.owner) response = views.new_post(request=request)
def test_search_view(self): """ Test search view """ url = reverse("post_search") # Get form data data = dict(query="Test post") request = fake_request(url=url, data=data, method="GET", user=self.owner) # Preform search on test index response = views.post_search(request=request) self.assertEqual(response.status_code, 200, "Error preforming search.")
def Xtest_edit_post(self): """ Test post edit for root and descendants """ url = reverse("post_edit", kwargs=dict(uid=self.post.uid)) title = "Test title for long test" tag_val = "foo,bar,foo" content = "Test the content with more things " longform_data = dict(title=title, tag_val=tag_val, content=content, post_type=models.Post.TUTORIAL) longform_request = fake_request(url=url, data=longform_data, user=self.owner) longform_response = views.edit_post(request=longform_request, uid=self.post.uid)