Skip to content

Commit

Permalink
fix testing issue
Browse files Browse the repository at this point in the history
  • Loading branch information
WenjunYuAnny committed Nov 14, 2023
1 parent 4d3c8b2 commit 003db99
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 31 deletions.
25 changes: 18 additions & 7 deletions events/templates/events/event-detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ <h5>{{ comment.user }}:</h5>
</div>
<p>{{ comment.content }}</p>
<!-- Toggle reply form -->
<button type="button" class="btn btn-outline-primary btn-block comment-button" onclick="showReplyForm({{ comment.id }}, {{ comment.is_private|lower }});">Reply</button>
<button type="button" class="btn btn-outline-primary btn-block comment-button" onclick="showReplyForm({{ event.id }},{{ comment.id }},{{ comment.is_private|lower }})">Reply</button>
<!-- Hidden Reply form -->
<div id="reply-form-placeholder-{{ comment.id }}"></div>

Expand Down Expand Up @@ -205,7 +205,7 @@ <h5>{{ reply.user }}:</h5>
{% endfor %}
<div id="reply-form" style="display:none;">
<div class="sticky-note-container gray-container mt-3">
<form action="{% url 'events:add-comment' event.id %}" method="post">
<form action="" method="post" id="reply-form-content">
{% csrf_token %}
<div class="form-group">
<input type="hidden" name="parent_id" value="{{ comment.id }}" id="parent_id">
Expand All @@ -216,7 +216,7 @@ <h5>{{ reply.user }}:</h5>
<input type="checkbox" name="is_private" id="is_private-reply" class="form-check-input">
<label for="is_private" class="form-check-label">Make This Comment Private</label>
</div>
<button type="submit" class="btn btn-primary btn-block comment-button" id="add-comment">Reply</button>
<button type="submit" class="btn btn-primary btn-block reply-button" id="add-reply">Reply</button>
</div>
</form>
</div>
Expand Down Expand Up @@ -309,18 +309,25 @@ <h3>Approved Participants ({{ approved_join_count }}) </h3>
window.location = url;
}

function showReplyForm(commentId, isParentPrivate) {
function showReplyForm(eventId, commentId, isParentPrivate) {
console.log("Event ID:", eventId, "Comment ID:", commentId);
// Move the reply form to the placeholder of the selected comment
var placeholder = document.getElementById("reply-form-placeholder-" + commentId);
var placeholder = document.getElementById('reply-form-placeholder-' + commentId);
var form = document.getElementById("reply-form");
var formContent = document.getElementById('reply-form-content');

if (!placeholder || !form) {
console.error("Element not found. Placeholder or form is null.");
console.error("Element not found. Placeholder is null.");
return;
}
if (!form) {
console.error("Element not found. form is null.");
return;
}
placeholder.appendChild(form);

// Set the parent_id value
document.getElementById('parent_id').value = commentId;


var isPrivateCheckbox = document.getElementById('is_private-reply');
if (isParentPrivate) {
Expand All @@ -332,6 +339,10 @@ <h3>Approved Participants ({{ approved_join_count }}) </h3>
}
// Show the form
form.style.display = 'block';

formContent.action = '/events/' + 'events/' + eventId + '/comment/' + commentId + '/reply/';
console.log(formContent.action);

}
</script>
{% endblock %}
Expand Down
107 changes: 93 additions & 14 deletions events/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,42 @@ def test_event_creator_comments_only_filter(self):
self.assertContains(response, creator_comment.content)
self.assertContains(response, user_comment.content)

def test_comment_author_can_see_private_reply(self):
self.client.login(username="testuser", password="testpassword")
user_comment = Comment.objects.create(
user=self.user,
event=self.event,
content="User private comment",
is_private=True,
)
private_reply = Comment.objects.create(
user=self.creator,
event=self.event,
content="Private reply",
is_private=True,
parent=user_comment,
)
response = self.client.get(reverse("events:event-detail", args=[self.event.id]))
self.assertContains(response, private_reply.content)

def test_not_logged_in_comment_attempt(self):
self.client.logout() # Ensure the user is logged out
self.client.post(
reverse("events:add-comment", args=[self.event.id]),
{
"content": "Unauthorized reply attempt",
},
)

self.assertEqual(Comment.objects.count(), 0) # No new comment should be added

def test_comment_str_representation(self):
self.comment = Comment.objects.create(
user=self.user, event=self.event, content="This is a test comment"
)
expected_str = f'{self.user.username}\'s comment: "This is a test comment..."'
self.assertEqual(str(self.comment), expected_str)


class ReplyTestCase(TestCase):
def setUp(self):
Expand Down Expand Up @@ -967,22 +1003,65 @@ def setUp(self):
is_private=True,
parent=None,
)
self.add_reply_url = reverse("events:add-comment", args=[self.event.id])
self.add_reply_url = reverse(
"events:add-reply", args=[self.event.id, self.parent.id]
)

# this test is not working at all
def test_create_reply(self):
self.client.logout()
self.client.login(username="testuser", password="password")
reply = Comment.objects.create(
user=self.user,
event=self.event,
content="Test reply",
is_private=True,
parent=self.parent,
self.client.login(username="testuser", password="testpassword")
reply_content = "This is a reply"
response = self.client.post(
reverse("events:add-reply", args=[self.event.id, self.parent.id]),
{
"content": reply_content,
},
)
# response = self.client.post(self.add_reply_url, {'content': 'Test comment', 'parent': self.parent})
# self.assertEqual(response.status_code, 302) # Assuming successful post redirects
# self.assertEqual(Comment.objects.count(), 2)
# reply = Comment.objects.get(parent=self.parent)
self.assertEqual(reply.content, "Test reply")

self.assertEqual(
response.status_code, 302
) # Assuming a redirect after successful posting
self.assertEqual(Comment.objects.count(), 2)
reply = Comment.objects.latest("id")
self.assertEqual(reply.content, reply_content)
self.assertEqual(reply.parent, self.parent)

def test_reply_to_nonexistent_comment(self):
self.client.logout()
self.client.login(username="testuser", password="testpassword")
response = self.client.post(
reverse("events:add-reply", args=[self.event.id, 99999]),
{
"content": "Reply to non-existent comment",
},
)
self.assertEqual(
response.status_code, 404
) # Expected to fail with a 404 Not Found

def test_reply_to_private_comment(self):
self.client.logout()
self.client.login(username="testuser", password="testpassword")

response = self.client.post(
reverse("events:add-reply", args=[self.event.id, self.parent.id]),
{
"content": "Reply to private comment",
},
)
self.assertEqual(response.status_code, 302)
reply = Comment.objects.latest("id")
self.assertEqual(reply.parent, self.parent)
self.assertTrue(
reply.is_private
) # Assuming replies inherit the privacy of the parent comment

def test_not_logged_in_reply_attempt(self):
self.client.logout() # Ensure the user is logged out
self.client.post(
reverse("events:add-reply", args=[self.event.id, self.parent.id]),
{
"content": "Unauthorized reply attempt",
},
)
self.assertEqual(Comment.objects.count(), 1) # No new comment should be added
5 changes: 5 additions & 0 deletions events/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,9 @@
name="remove-approved-request",
),
path("event/<int:event_id>/add-comment/", views.addComment, name="add-comment"),
path(
"events/<int:event_id>/comment/<int:comment_id>/reply/",
views.addReply,
name="add-reply",
),
]
43 changes: 33 additions & 10 deletions events/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,21 +487,14 @@ def get_locations(request):
def addComment(request, event_id):
event = get_object_or_404(Event, id=event_id)
parent_id = request.POST.get("parent_id")
if parent_id:
# handle the case when it's a reply of a reply
return HttpResponseBadRequest("Cantnot reply to a nested comment")
form = CommentForm(request.POST)
if form.is_valid():
comment = form.save(commit=False)
comment.user = request.user
comment.event = event
if parent_id:
parent_comment = Comment.objects.get(id=parent_id)
# make sure that the parent comment is a comment not a reply
if parent_comment.parent is None:
comment.parent = parent_comment
if parent_comment.is_private:
comment.is_private = True
else:
# handle the case when it's a reply of a reply
return HttpResponseBadRequest("Cantnot reply to a nested comment")
comment.save()
return redirect(
"events:event-detail", event_id=event.id
Expand All @@ -512,3 +505,33 @@ def addComment(request, event_id):
return redirect(
"events:event-detail", event_id=event.id
) # redirect to event detail page


@login_required
@require_POST
def addReply(request, event_id, comment_id):
event = get_object_or_404(Event, id=event_id)
parent_comment = get_object_or_404(Comment, id=comment_id)
form = CommentForm(request.POST)
if form.is_valid():
reply = form.save(commit=False)
reply.user = request.user
reply.event = event
# make sure that the parent comment is a comment not a reply
if parent_comment.parent is None:
reply.parent = parent_comment
if parent_comment.is_private:
reply.is_private = True
else:
# handle the case when it's a reply of a reply
return HttpResponseBadRequest("Cantnot reply to a nested comment")
reply.save()
return redirect(
"events:event-detail", event_id=event.id
) # redirect to event detail page
else:
for error in form.errors:
messages.warning(request, f"{error}: :{form.errors[error]}")
return redirect(
"events:event-detail", event_id=event.id
) # redirect to event detail page

0 comments on commit 003db99

Please sign in to comment.