Implement hiding and showing functionality for user exams and collections
This commit is contained in:
@@ -182,4 +182,103 @@ def test_people_page_and_user_search(client, create_users, create_cid_manager):
|
||||
assert b"No users found matching" in response_no_match.content
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_hide_exams_and_collections(client, create_users):
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from physics.models import Exam as PhysicsExam
|
||||
from atlas.models import CaseCollection
|
||||
from generic.models import UserHiddenItem
|
||||
|
||||
user1, user2 = create_users
|
||||
|
||||
# 1. Create a PhysicsExam and associate with user1
|
||||
exam = PhysicsExam.objects.create(
|
||||
name="Test Physics Exam", exam_mode=True, active=True
|
||||
)
|
||||
user1.user_physics_exams.add(exam)
|
||||
|
||||
# 2. Create a CaseCollection and associate with user1
|
||||
collection = CaseCollection.objects.create(
|
||||
name="Test Case Collection", exam_mode=True, active=True
|
||||
)
|
||||
user1.user_casecollection_exams.add(collection)
|
||||
|
||||
# Log in as user1
|
||||
client.force_login(user1)
|
||||
|
||||
# Verify they are visible on user scores page initially
|
||||
response = client.get(reverse("user_scores"))
|
||||
assert response.status_code == 200
|
||||
assert b"Test Physics Exam" in response.content
|
||||
|
||||
# Load atlas collections partial
|
||||
response_cc = client.get(reverse("atlas:user_collections"))
|
||||
assert response_cc.status_code == 200
|
||||
assert b"Test Case Collection" in response_cc.content
|
||||
|
||||
# 3. Hide the PhysicsExam
|
||||
exam_ct = ContentType.objects.get_for_model(PhysicsExam)
|
||||
response_hide_exam = client.post(
|
||||
reverse("generic:toggle_hide_item"),
|
||||
{"content_type_id": exam_ct.id, "object_id": exam.id},
|
||||
)
|
||||
assert response_hide_exam.status_code == 200
|
||||
assert response_hide_exam.headers.get("HX-Refresh") == "true"
|
||||
|
||||
# Verify UserHiddenItem exists
|
||||
assert UserHiddenItem.objects.filter(
|
||||
user=user1, content_type=exam_ct, object_id=exam.id
|
||||
).exists()
|
||||
|
||||
# Verify exam is hidden now from standard user_scores
|
||||
response_hidden = client.get(reverse("user_scores"))
|
||||
assert response_hidden.status_code == 200
|
||||
assert b"Test Physics Exam" not in response_hidden.content
|
||||
|
||||
# But present when show_hidden=true is requested
|
||||
response_show_hidden = client.get(reverse("user_scores") + "?show_hidden=true")
|
||||
assert response_show_hidden.status_code == 200
|
||||
assert b"Test Physics Exam" in response_show_hidden.content
|
||||
assert b"Hidden" in response_show_hidden.content
|
||||
|
||||
# 4. Hide the CaseCollection
|
||||
cc_ct = ContentType.objects.get_for_model(CaseCollection)
|
||||
response_hide_cc = client.post(
|
||||
reverse("generic:toggle_hide_item"),
|
||||
{"content_type_id": cc_ct.id, "object_id": collection.pk},
|
||||
)
|
||||
assert response_hide_cc.status_code == 200
|
||||
assert response_hide_cc.headers.get("HX-Refresh") == "true"
|
||||
|
||||
assert UserHiddenItem.objects.filter(
|
||||
user=user1, content_type=cc_ct, object_id=collection.pk
|
||||
).exists()
|
||||
|
||||
# Verify collection is hidden from normal view
|
||||
response_cc_hidden = client.get(reverse("atlas:user_collections"))
|
||||
assert response_cc_hidden.status_code == 200
|
||||
assert b"Test Case Collection" not in response_cc_hidden.content
|
||||
|
||||
# Verify collection is visible in show_hidden view
|
||||
response_cc_show_hidden = client.get(reverse("atlas:user_collections") + "?show_hidden=true")
|
||||
assert response_cc_show_hidden.status_code == 200
|
||||
assert b"Test Case Collection" in response_cc_show_hidden.content
|
||||
assert b"Hidden" in response_cc_show_hidden.content
|
||||
|
||||
# 5. Unhide the PhysicsExam by toggling again
|
||||
response_unhide_exam = client.post(
|
||||
reverse("generic:toggle_hide_item"),
|
||||
{"content_type_id": exam_ct.id, "object_id": exam.id},
|
||||
)
|
||||
assert response_unhide_exam.status_code == 200
|
||||
assert not UserHiddenItem.objects.filter(
|
||||
user=user1, content_type=exam_ct, object_id=exam.id
|
||||
).exists()
|
||||
|
||||
response_unhidden = client.get(reverse("user_scores"))
|
||||
assert response_unhidden.status_code == 200
|
||||
assert b"Test Physics Exam" in response_unhidden.content
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user