Skip to content

Commit

Permalink
Test redirect to unread post
Browse files Browse the repository at this point in the history
  • Loading branch information
rafalp committed Sep 4, 2024
1 parent 8da60c2 commit 7540020
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 3 deletions.
2 changes: 1 addition & 1 deletion misago/readtracker/tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def get_thread_read_time(request: HttpRequest, thread: Thread) -> datetime:
def mark_thread_read(user: "User", thread: Thread, read_time: datetime):
create_row = True

if thread.read_time:
if getattr(thread, "read_time", None):
create_row = not ReadThread.objects.filter(
user=user,
thread=thread,
Expand Down
151 changes: 149 additions & 2 deletions misago/threads/tests/test_redirects_views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from django.urls import reverse
from django.utils import timezone

from ...readtracker.tracker import mark_thread_read
from ..test import reply_thread


def test_thread_last_post_redirect_view_returns_redirect_link(client, thread):
def test_thread_last_post_redirect_view_returns_redirect(client, thread):
reply = reply_thread(thread)

response = client.get(
Expand All @@ -24,7 +26,7 @@ def test_thread_last_post_redirect_view_returns_redirect_link(client, thread):
)


def test_private_thread_last_post_redirect_view_returns_redirect_link(
def test_private_thread_last_post_redirect_view_returns_redirect(
user_client, user_private_thread
):
reply = reply_thread(user_private_thread)
Expand All @@ -45,3 +47,148 @@ def test_private_thread_last_post_redirect_view_returns_redirect_link(
)
+ f"#post-{reply.id}"
)


def test_thread_unread_post_redirect_view_returns_redirect_to_last_post_for_anonymous_user(
client, thread
):
reply_thread(thread, posted_on=timezone.now())
reply = reply_thread(thread, posted_on=timezone.now())

response = client.get(
reverse(
"misago:thread-unread-post",
kwargs={"id": thread.id, "slug": thread.slug},
)
)

assert response.status_code == 302
assert (
response["location"]
== reverse(
"misago:thread",
kwargs={"id": thread.id, "slug": thread.slug},
)
+ f"#post-{reply.id}"
)


def test_thread_unread_post_redirect_view_returns_redirect_to_first_unread_post_for_user(
user, user_client, thread
):
mark_thread_read(user, thread, thread.first_post.posted_on)

reply = reply_thread(thread, posted_on=timezone.now())
reply_thread(thread, posted_on=timezone.now())

response = user_client.get(
reverse(
"misago:thread-unread-post",
kwargs={"id": thread.id, "slug": thread.slug},
)
)

assert response.status_code == 302
assert (
response["location"]
== reverse(
"misago:thread",
kwargs={"id": thread.id, "slug": thread.slug},
)
+ f"#post-{reply.id}"
)


def test_thread_unread_post_redirect_view_returns_redirect_to_last_post_for_read_thread(
user, user_client, thread
):
reply_thread(thread, posted_on=timezone.now())
reply = reply_thread(thread, posted_on=timezone.now())

read_on = timezone.now()
mark_thread_read(user, thread, read_on)

response = user_client.get(
reverse(
"misago:thread-unread-post",
kwargs={"id": thread.id, "slug": thread.slug},
)
)

assert response.status_code == 302
assert (
response["location"]
== reverse(
"misago:thread",
kwargs={"id": thread.id, "slug": thread.slug},
)
+ f"#post-{reply.id}"
)


def test_private_thread_unread_post_redirect_view_returns_error_404_for_anonymous_client(
client, user_private_thread
):
response = client.get(
reverse(
"misago:private-thread-unread-post",
kwargs={"id": user_private_thread.id, "slug": user_private_thread.slug},
)
)

assert response.status_code == 403


def test_private_thread_unread_post_redirect_view_returns_redirect_to_first_unread_post_for_user(
user, user_client, user_private_thread
):
mark_thread_read(
user, user_private_thread, user_private_thread.first_post.posted_on
)

reply = reply_thread(user_private_thread, posted_on=timezone.now())
reply_thread(user_private_thread, posted_on=timezone.now())

response = user_client.get(
reverse(
"misago:private-thread-unread-post",
kwargs={"id": user_private_thread.id, "slug": user_private_thread.slug},
)
)

assert response.status_code == 302
assert (
response["location"]
== reverse(
"misago:private-thread",
kwargs={"id": user_private_thread.id, "slug": user_private_thread.slug},
)
+ f"#post-{reply.id}"
)


def test_private_thread_unread_post_redirect_view_returns_redirect_to_last_post_for_read_thread(
user, user_client, user_private_thread
):
reply_thread(user_private_thread, posted_on=timezone.now())
reply = reply_thread(user_private_thread, posted_on=timezone.now())

read_on = timezone.now()
mark_thread_read(user, user_private_thread, read_on)

response = user_client.get(
reverse(
"misago:private-thread-unread-post",
kwargs={"id": user_private_thread.id, "slug": user_private_thread.slug},
)
)

assert response.status_code == 302
assert (
response["location"]
== reverse(
"misago:private-thread",
kwargs={"id": user_private_thread.id, "slug": user_private_thread.slug},
)
+ f"#post-{reply.id}"
)
2 changes: 2 additions & 0 deletions misago/threads/views/redirect.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def get(self, request: HttpRequest, id: int, slug: str, **kwargs) -> HttpRespons
paginator = self.get_thread_posts_paginator(request, queryset)

if post:
print(post.id, post.posted_on)
post_id = post.id
offset = queryset.filter(id__lt=post_id).count()
page = paginator.get_item_page(offset)
Expand Down Expand Up @@ -62,6 +63,7 @@ def get_post(
read_times.append(thread.category_read_time)

read_time = max(read_times)
print(read_time)
return queryset.filter(posted_on__gt=read_time).first()


Expand Down

0 comments on commit 7540020

Please sign in to comment.