Add the default enrollment start date on course creation (#30954)

* fix: main page course listing

The course is visible on the main page right after creation when the feature toggle `CREATE_COURSE_WITH_DEFAULT_ENROLLMENT_START_DATE` is on.

So anonymous users can see them and access the course about page
for the courses without valid data (e.g. they will see the default
course overview)

When courses list filtering is processed it checks the `see_exists`
permission for the anonymous user.
Actually, `see_exists` means `can_load` OR `can_enroll`.

`can_load` is False in our case because the course start in the future.

But `can_enroll` returns True because the course's enrollment_start
and enrollment_end dates are blank:
```
enrollment_start = courselike.enrollment_start or datetime.min.replace(tzinfo=UTC)
enrollment_end = courselike.enrollment_end or datetime.max.replace(tzinfo=UTC)
if enrollment_start < now < enrollment_end:
    debug("Allow: in enrollment period")
    return ACCESS_GRANTED
```

Set the enrollment_start the same as a course start by default
This commit is contained in:
Eugene Dyudyunov
2023-08-22 17:42:19 +03:00
committed by GitHub
parent a5b1fdad38
commit 4340a832f7
4 changed files with 74 additions and 24 deletions

View File

@@ -4,20 +4,22 @@
import itertools
import unittest
from datetime import datetime, timedelta
import sys
from unittest.mock import Mock, patch
import pytest
import ddt
from dateutil import parser
from django.conf import settings
from django.test import override_settings
from fs.memoryfs import MemoryFS
from opaque_keys.edx.keys import CourseKey
import pytest
from pytz import utc
from xblock.runtime import DictKeyValueStore, KvsFieldData
from openedx.core.lib.teams_config import TeamsConfig, DEFAULT_COURSE_RUN_MAX_TEAM_SIZE
import xmodule.course_block
from xmodule.course_metadata_utils import DEFAULT_START_DATE
from xmodule.data import CertificatesDisplayBehaviors
from xmodule.modulestore.xml import ImportSystem, XMLModuleStore
from xmodule.modulestore.exceptions import InvalidProctoringProvider
@@ -32,10 +34,22 @@ _LAST_WEEK = _TODAY - timedelta(days=7)
_NEXT_WEEK = _TODAY + timedelta(days=7)
class CourseFieldsTestCase(unittest.TestCase):
@ddt.ddt()
class CourseFieldsTestCase(unittest.TestCase): # lint-amnesty, pylint: disable=missing-class-docstring
def test_default_start_date(self):
assert xmodule.course_block.CourseFields.start.default == datetime(2030, 1, 1, tzinfo=utc)
assert xmodule.course_block.CourseFields.start.default == DEFAULT_START_DATE
@ddt.data(True, False)
def test_default_enrollment_start_date(self, should_have_default_enroll_start):
features = settings.FEATURES.copy()
features['CREATE_COURSE_WITH_DEFAULT_ENROLLMENT_START_DATE'] = should_have_default_enroll_start
with override_settings(FEATURES=features):
# reimport, so settings override could take effect
del sys.modules['xmodule.course_block']
import xmodule.course_block # lint-amnesty, pylint: disable=redefined-outer-name, reimported
expected = DEFAULT_START_DATE if should_have_default_enroll_start else None
assert xmodule.course_block.CourseFields.enrollment_start.default == expected
class DummySystem(ImportSystem): # lint-amnesty, pylint: disable=abstract-method, missing-class-docstring