Files
edx-platform/common/djangoapps/student/tests/test_parental_controls.py
Dillon Dumesnil 7b1fead04c feat: AA-1138: Adds ability to have Weekly Goal Celebration Modal in MFE
Adds celebrate_weekly_goal to the CourseEnrollmentCelebration and includes logic
for only returning True if the db field is true and the learner has hit their goal
this week. Adds ability to set to false via the API already used by the frontend.
Default db value is False, but all new enrollments after merge will be set to True.
2022-01-14 13:59:22 -05:00

88 lines
3.5 KiB
Python

"""Unit tests for parental controls."""
from django.test import TestCase
from django.test.utils import override_settings
from django.utils.timezone import now
from common.djangoapps.student.models import UserProfile
from common.djangoapps.student.tests.factories import UserFactory
class ProfileParentalControlsTest(TestCase):
"""Unit tests for requires_parental_consent."""
password = "test"
def setUp(self):
super().setUp()
self.user = UserFactory.create(password=self.password)
self.profile = UserProfile.objects.get(id=self.user.id)
def set_year_of_birth(self, year_of_birth):
"""
Helper method that creates a mock profile for the specified user.
"""
self.profile.year_of_birth = year_of_birth
self.profile.save()
def test_no_year_of_birth(self):
"""Verify the behavior for users with no specified year of birth."""
assert self.profile.requires_parental_consent()
assert self.profile.requires_parental_consent(default_requires_consent=True)
assert not self.profile.requires_parental_consent(default_requires_consent=False)
@override_settings(PARENTAL_CONSENT_AGE_LIMIT=None)
def test_no_parental_controls(self):
"""Verify the behavior for all users when parental controls are not enabled."""
assert not self.profile.requires_parental_consent()
assert not self.profile.requires_parental_consent(default_requires_consent=True)
assert not self.profile.requires_parental_consent(default_requires_consent=False)
# Verify that even a child does not require parental consent
current_year = now().year
self.set_year_of_birth(current_year - 10)
assert not self.profile.requires_parental_consent()
def test_adult_user(self):
"""Verify the behavior for an adult."""
current_year = now().year
self.set_year_of_birth(current_year - 20)
assert not self.profile.requires_parental_consent()
assert self.profile.requires_parental_consent(age_limit=21)
def test_child_user(self):
"""Verify the behavior for a child."""
current_year = now().year
# Verify for a child born 13 years agp
self.set_year_of_birth(current_year - 13)
assert self.profile.requires_parental_consent()
assert self.profile.requires_parental_consent(year=current_year)
assert not self.profile.requires_parental_consent(year=(current_year + 1))
# Verify for a child born 14 years ago
self.set_year_of_birth(current_year - 14)
assert not self.profile.requires_parental_consent()
assert not self.profile.requires_parental_consent(year=current_year)
def test_profile_image(self):
"""Verify that a profile's image obeys parental controls."""
# Verify that an image cannot be set for a user with no year of birth set
self.profile.profile_image_uploaded_at = now()
self.profile.save()
assert not self.profile.has_profile_image
# Verify that an image can be set for an adult user
current_year = now().year
self.set_year_of_birth(current_year - 20)
self.profile.profile_image_uploaded_at = now()
self.profile.save()
assert self.profile.has_profile_image
# verify that a user's profile image is removed when they switch to requiring parental controls
self.set_year_of_birth(current_year - 10)
self.profile.save()
assert not self.profile.has_profile_image