Skip to content

Commit

Permalink
Make post_save CourseRunCertificate signal work (#2388)
Browse files Browse the repository at this point in the history
  • Loading branch information
annagav authored Sep 11, 2024
1 parent 4c743c9 commit c4e3375
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 9 deletions.
53 changes: 47 additions & 6 deletions courses/api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,9 @@ def test_course_run_certificate( # noqa: PLR0913
patched_sync_hubspot_user = mocker.patch(
"hubspot_sync.task_helpers.sync_hubspot_user",
)
mocker.patch(
"hubspot_sync.management.commands.configure_hubspot_properties._upsert_custom_properties",
)
passed_grade_with_enrollment.grade = grade
passed_grade_with_enrollment.passed = passed
if not paid:
Expand All @@ -934,6 +937,9 @@ def test_course_run_certificate_idempotent(passed_grade_with_enrollment, mocker,
patched_sync_hubspot_user = mocker.patch(
"hubspot_sync.task_helpers.sync_hubspot_user",
)
mocker.patch(
"hubspot_sync.management.commands.configure_hubspot_properties._upsert_custom_properties",
)
# Certificate is created the first time
certificate, created, deleted = process_course_run_grade_certificate(
passed_grade_with_enrollment
Expand All @@ -953,10 +959,13 @@ def test_course_run_certificate_idempotent(passed_grade_with_enrollment, mocker,
assert not deleted


def test_course_run_certificate_not_passing(passed_grade_with_enrollment):
def test_course_run_certificate_not_passing(passed_grade_with_enrollment, mocker):
"""
Test that the certificate is not generated if the grade is set to not passed
"""
mocker.patch(
"hubspot_sync.management.commands.configure_hubspot_properties._upsert_custom_properties",
)
# Initially the certificate is created
certificate, created, deleted = process_course_run_grade_certificate(
passed_grade_with_enrollment
Expand Down Expand Up @@ -1005,7 +1014,9 @@ def test_generate_course_certificates_self_paced_course(
user = passed_grade_with_enrollment.user
course_run.is_self_paced = True
course_run.save()

mocker.patch(
"hubspot_sync.management.commands.configure_hubspot_properties._upsert_custom_properties",
)
mocker.patch(
"courses.api.ensure_course_run_grade",
return_value=(passed_grade_with_enrollment, True, False),
Expand Down Expand Up @@ -1045,6 +1056,12 @@ def test_course_certificates_with_course_end_date_self_paced_combination( # noq

user = passed_grade_with_enrollment.user

mocker.patch(
"hubspot_sync.task_helpers.sync_hubspot_user",
)
mocker.patch(
"hubspot_sync.management.commands.configure_hubspot_properties._upsert_custom_properties",
)
mocker.patch(
"courses.api.exception_logging_generator",
return_value=[(passed_grade_with_enrollment, user)],
Expand All @@ -1071,7 +1088,9 @@ def test_generate_course_certificates_with_course_end_date(
course_run.save()

user = passed_grade_with_enrollment.user

mocker.patch(
"hubspot_sync.management.commands.configure_hubspot_properties._upsert_custom_properties",
)
mocker.patch(
"courses.api.ensure_course_run_grade",
return_value=(passed_grade_with_enrollment, True, False),
Expand All @@ -1087,8 +1106,11 @@ def test_generate_course_certificates_with_course_end_date(
)


def test_course_run_certificates_access():
def test_course_run_certificates_access(mocker):
"""Tests that the revoke and unrevoke for a course run certificates sets the states properly"""
mocker.patch(
"hubspot_sync.management.commands.configure_hubspot_properties._upsert_custom_properties",
)
test_certificate = CourseRunCertificateFactory.create(is_revoked=False)

# Revoke a certificate
Expand Down Expand Up @@ -1213,11 +1235,15 @@ def test_generate_program_certificate_failure_missing_certificates(
def test_generate_program_certificate_failure_not_all_passed(
user,
program_with_requirements, # noqa: F811
mocker,
):
"""
Test that generate_program_certificate return (None, False) and not create program certificate
if there is not any course_run certificate for the given course.
"""
mocker.patch(
"hubspot_sync.management.commands.configure_hubspot_properties._upsert_custom_properties",
)
courses = CourseFactory.create_batch(3)
course_runs = CourseRunFactory.create_batch(3, course=factory.Iterator(courses))
CourseRunCertificateFactory.create_batch(
Expand All @@ -1240,6 +1266,9 @@ def test_generate_program_certificate_success_single_requirement_course(user, mo
patched_sync_hubspot_user = mocker.patch(
"hubspot_sync.task_helpers.sync_hubspot_user",
)
mocker.patch(
"hubspot_sync.management.commands.configure_hubspot_properties._upsert_custom_properties",
)
course = CourseFactory.create()
program = ProgramFactory.create()
ProgramRequirementFactory.add_root(program)
Expand Down Expand Up @@ -1270,6 +1299,9 @@ def test_generate_program_certificate_success_multiple_required_courses(user, mo
patched_sync_hubspot_user = mocker.patch(
"hubspot_sync.task_helpers.sync_hubspot_user",
)
mocker.patch(
"hubspot_sync.management.commands.configure_hubspot_properties._upsert_custom_properties",
)
courses = CourseFactory.create_batch(3)
program = ProgramFactory.create()
ProgramRequirementFactory.add_root(program)
Expand All @@ -1294,11 +1326,14 @@ def test_generate_program_certificate_success_multiple_required_courses(user, mo
patched_sync_hubspot_user.assert_called_once_with(user)


def test_generate_program_certificate_success_minimum_electives_not_met(user):
def test_generate_program_certificate_success_minimum_electives_not_met(user, mocker):
"""
Test that generate_program_certificate does not generate a program certificate if minimum electives have not been met.
"""
courses = CourseFactory.create_batch(3)
mocker.patch(
"hubspot_sync.management.commands.configure_hubspot_properties._upsert_custom_properties",
)

# Create Program with 2 minimum elective courses.
program = ProgramFactory.create()
Expand Down Expand Up @@ -1352,6 +1387,9 @@ def test_force_generate_program_certificate_success(
patched_sync_hubspot_user = mocker.patch(
"hubspot_sync.task_helpers.sync_hubspot_user",
)
mocker.patch(
"hubspot_sync.management.commands.configure_hubspot_properties._upsert_custom_properties",
)
courses = CourseFactory.create_batch(3)
course_runs = CourseRunFactory.create_batch(3, course=factory.Iterator(courses))
CourseRunCertificateFactory.create_batch(
Expand Down Expand Up @@ -1415,14 +1453,17 @@ def test_program_certificates_access():

def test_generate_program_certificate_failure_not_all_passed_nested_elective_stipulation(
user,
mocker,
):
"""
Test that generate_program_certificate returns (None, False) and does not create a program certificate
if the learner has not met the elective requirements due to a nested operator.
"""
courses = CourseFactory.create_batch(3)
course_runs = CourseRunFactory.create_batch(3, course=factory.Iterator(courses))

mocker.patch(
"hubspot_sync.management.commands.configure_hubspot_properties._upsert_custom_properties",
)
# Create Program
program = ProgramFactory.create()
ProgramRequirementFactory.add_root(program)
Expand Down
5 changes: 4 additions & 1 deletion courses/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ class CoursesConfig(AppConfig):
name = "courses"

def ready(self):
pass
"""
Ready handler. Import signals.
"""
import courses.signals # noqa: F401
10 changes: 8 additions & 2 deletions courses/models_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,10 +414,13 @@ def test_course_course_number():
assert course.course_number == "Test101"


def test_course_run_certificate_start_end_dates_and_page_revision():
def test_course_run_certificate_start_end_dates_and_page_revision(mocker):
"""
Test that the CourseRunCertificate start_end_dates property works properly
"""
mocker.patch(
"hubspot_sync.management.commands.configure_hubspot_properties._upsert_custom_properties",
)
certificate = CourseRunCertificateFactory.create(
course_run__course__page__certificate_page__product_name="product_name"
)
Expand All @@ -430,12 +433,15 @@ def test_course_run_certificate_start_end_dates_and_page_revision():
)


def test_program_certificate_start_end_dates_and_page_revision(user):
def test_program_certificate_start_end_dates_and_page_revision(user, mocker):
"""
Test that the ProgramCertificate start_end_dates property works properly.
The start date is the start date of the first course run passed.
The end date is the date the user received the program certificate.
"""
mocker.patch(
"hubspot_sync.management.commands.configure_hubspot_properties._upsert_custom_properties",
)
now = now_in_utc()
start_date = now + timedelta(days=1)
end_date = now + timedelta(days=100)
Expand Down

0 comments on commit c4e3375

Please sign in to comment.