Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 100 additions & 4 deletions website/tests/test_bio_utils.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
"""
Tests for website.utils.bio_utils.auto_generate_bio and its helpers.

Tests use mocks rather than real Person/Position records so they don't have to
navigate Person.save() side-effects (random image assignment, file I/O); the
unit under test is pure presentation logic.
Most tests use mocks rather than real Person/Position records so they don't
have to navigate Person.save() side-effects (random image assignment, file
I/O); the unit under test is pure presentation logic. MemberBioVisibilityTests
is the exception: a DB-backed regression test that renders the real member page.
"""

from datetime import date, timedelta
from unittest.mock import MagicMock, patch

from django.test import SimpleTestCase
from django.urls import reverse

from website.models import Position, Project, ProjectRole
from website.models.position import Title
from website.tests.base import DatabaseTestCase
from website.utils.bio_utils import (
_join_with_oxford_comma,
auto_generate_bio,
Expand Down Expand Up @@ -99,10 +104,11 @@ def _make_link_person(first_name, last_name, url_name):
return p


def _make_project(name, short_name):
def _make_project(name, short_name, is_visible=True):
p = MagicMock()
p.name = name
p.short_name = short_name
p.is_visible = is_visible
return p


Expand Down Expand Up @@ -283,6 +289,58 @@ def test_one_project_no_pubs(self):
self.assertIn("They contributed to a project called <a href=", bio)
self.assertIn(">Sound Watch</a>.", bio)

def test_hidden_projects_are_not_named(self):
"""Hidden (private) projects must not leak into the public bio."""
# The hidden projects sort BEFORE the visible one, so an implementation
# that filters the count but takes names from the unfiltered sorted
# list would name a hidden project here.
projs = [
_make_project("Zulu Public", "zulupublic"),
_make_project("Alpha Hidden", "alphahidden", is_visible=False),
_make_project("Bravo Unset", "bravounset", is_visible=None),
]
person = _make_person(
is_current_member=True,
is_active=True,
current_title="Undergrad",
projects=projs,
)
bio = auto_generate_bio(person)
self.assertIn("a project called <a href=", bio)
self.assertIn(">Zulu Public</a>", bio)
self.assertNotIn("Alpha Hidden", bio)
self.assertNotIn("Bravo Unset", bio)

def test_hidden_projects_excluded_from_multi_project_count(self):
"""Count and names on the multi-project path ignore hidden projects."""
projs = [
_make_project("Alpha Hidden", "alphahidden", is_visible=False),
_make_project("Mike Public", "mikepublic"),
_make_project("Zulu Public", "zulupublic"),
]
person = _make_person(
is_current_member=True,
is_active=True,
current_title="Undergrad",
projects=projs,
)
bio = auto_generate_bio(person)
self.assertIn("2 projects:", bio)
self.assertIn(">Mike Public</a>", bio)
self.assertIn(">Zulu Public</a>", bio)
self.assertNotIn("Alpha Hidden", bio)

def test_only_hidden_projects_and_no_pubs_has_no_contributions(self):
person = _make_person(
is_current_member=True,
is_active=True,
current_title="Undergrad",
projects=[_make_project("Secret", "secret", is_visible=False)],
)
bio = auto_generate_bio(person)
self.assertNotIn("contributed to", bio)
self.assertNotIn("Secret", bio)

def test_two_projects_uses_and(self):
projs = [
_make_project("Alpha", "alpha"),
Expand Down Expand Up @@ -529,3 +587,41 @@ def test_returns_years_string_for_long_durations(self):
result.endswith(" years"),
msg=f"Expected a years string, got {result!r}",
)


class MemberBioVisibilityTests(DatabaseTestCase):
"""DB-backed regression: the rendered member page never names a hidden project.

Exercises the real Person.get_projects and the member view, which the
mock-based tests above cannot. Covers the bio, the meta description built
from it, and the projects grid.
"""

def test_member_page_does_not_name_hidden_projects(self):
person = self.make_person(first_name="Probe", last_name="Person")
Position.objects.create(
person=person, start_date=date(2020, 1, 1), title=Title.PHD_STUDENT
)
shown = self.make_project(name="Zq Shown", is_visible=True)
hidden = self.make_project(name="Zq Hidden", is_visible=False)
unset = self.make_project(name="Zq Unset", is_visible=True)
# Legacy NULL row: update() bypasses Project.save()'s normalization.
Project.objects.filter(pk=unset.pk).update(is_visible=None)
for proj in (shown, hidden, unset):
ProjectRole.objects.create(
person=person, project=proj, start_date=date(2021, 1, 1)
)
person.refresh_from_db()

response = self.client.get(
reverse(
"website:member_by_name",
kwargs={"member_name": person.url_name},
)
)
self.assertEqual(response.status_code, 200)
html = response.content.decode()
self.assertIn("a project called", html)
self.assertIn("Zq Shown", html)
self.assertNotIn("Zq Hidden", html)
self.assertNotIn("Zq Unset", html)
9 changes: 8 additions & 1 deletion website/utils/bio_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,14 @@ def _contributions_sentence(person):
with "including" + the first three. Publication count is appended as a
plain count rather than a list.
"""
projects = sorted(person.get_projects, key=lambda p: p.name)
# Only name publicly visible projects: this bio renders on the public
# member page, and hidden (private) projects must not leak there. Matches
# the truthy check in views.member.get_member_projects (is_visible may be
# None, which is treated as hidden).
projects = sorted(
(p for p in person.get_projects if p.is_visible),
key=lambda p: p.name,
)
proj_count = len(projects)
pub_count = person.publication_set.count()

Expand Down
Loading