Skip to content

Commit 6ddb7ee

Browse files
Hubspot course name fix (#3100)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 2d8bad0 commit 6ddb7ee

File tree

10 files changed

+867
-740
lines changed

10 files changed

+867
-740
lines changed

courses/api_test.py

Lines changed: 67 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -925,7 +925,9 @@ def test_sync_course_mode(settings, mocker, mocked_api_response, expect_success)
925925
[1.0, False, True, False, False, False], # noqa: PT007
926926
],
927927
)
928+
@patch("courses.signals.upsert_custom_properties")
928929
def test_course_run_certificate( # noqa: PLR0913
930+
mock_upsert_custom_properties,
929931
user,
930932
passed_grade_with_enrollment,
931933
grade,
@@ -943,7 +945,7 @@ def test_course_run_certificate( # noqa: PLR0913
943945
"hubspot_sync.task_helpers.sync_hubspot_user",
944946
)
945947
mocker.patch(
946-
"hubspot_sync.management.commands.configure_hubspot_properties._upsert_custom_properties",
948+
"hubspot_sync.api.upsert_custom_properties",
947949
)
948950
passed_grade_with_enrollment.grade = grade
949951
passed_grade_with_enrollment.passed = passed
@@ -963,15 +965,18 @@ def test_course_run_certificate( # noqa: PLR0913
963965
assert deleted is exp_deleted
964966

965967

966-
def test_course_run_certificate_idempotent(passed_grade_with_enrollment, mocker, user):
968+
@patch("courses.signals.upsert_custom_properties")
969+
def test_course_run_certificate_idempotent(
970+
mock_upsert_custom_properties, passed_grade_with_enrollment, mocker, user
971+
):
967972
"""
968973
Test that the certificate generation is idempotent
969974
"""
970975
patched_sync_hubspot_user = mocker.patch(
971976
"hubspot_sync.task_helpers.sync_hubspot_user",
972977
)
973978
mocker.patch(
974-
"hubspot_sync.management.commands.configure_hubspot_properties._upsert_custom_properties",
979+
"hubspot_sync.api.upsert_custom_properties",
975980
)
976981
# Certificate is created the first time
977982
certificate, created, deleted = process_course_run_grade_certificate(
@@ -992,12 +997,15 @@ def test_course_run_certificate_idempotent(passed_grade_with_enrollment, mocker,
992997
assert not deleted
993998

994999

995-
def test_course_run_certificate_not_passing(passed_grade_with_enrollment, mocker):
1000+
@patch("courses.signals.upsert_custom_properties")
1001+
def test_course_run_certificate_not_passing(
1002+
mock_upsert_custom_properties, passed_grade_with_enrollment, mocker
1003+
):
9961004
"""
9971005
Test that the certificate is not generated if the grade is set to not passed
9981006
"""
9991007
mocker.patch(
1000-
"hubspot_sync.management.commands.configure_hubspot_properties._upsert_custom_properties",
1008+
"hubspot_sync.api.upsert_custom_properties",
10011009
)
10021010
# Initially the certificate is created
10031011
certificate, created, deleted = process_course_run_grade_certificate(
@@ -1039,16 +1047,20 @@ def test_generate_course_certificates_no_valid_course_run(settings, courses_api_
10391047
)
10401048

10411049

1050+
@patch("courses.signals.upsert_custom_properties")
10421051
def test_generate_course_certificates_self_paced_course(
1043-
mocker, courses_api_logs, passed_grade_with_enrollment
1052+
mock_upsert_custom_properties,
1053+
mocker,
1054+
courses_api_logs,
1055+
passed_grade_with_enrollment,
10441056
):
10451057
"""Test that certificates are generated for self paced course runs independent of course run end date"""
10461058
course_run = passed_grade_with_enrollment.course_run
10471059
user = passed_grade_with_enrollment.user
10481060
course_run.is_self_paced = True
10491061
course_run.save()
10501062
mocker.patch(
1051-
"hubspot_sync.management.commands.configure_hubspot_properties._upsert_custom_properties",
1063+
"hubspot_sync.api.upsert_custom_properties",
10521064
)
10531065
mocker.patch(
10541066
"courses.api.ensure_course_run_grade",
@@ -1073,7 +1085,9 @@ def test_generate_course_certificates_self_paced_course(
10731085
(False, None),
10741086
],
10751087
)
1088+
@patch("courses.signals.upsert_custom_properties")
10761089
def test_course_certificates_with_course_end_date_self_paced_combination( # noqa: PLR0913
1090+
mock_upsert_custom_properties,
10771091
mocker,
10781092
settings,
10791093
courses_api_logs,
@@ -1093,7 +1107,7 @@ def test_course_certificates_with_course_end_date_self_paced_combination( # noq
10931107
"hubspot_sync.task_helpers.sync_hubspot_user",
10941108
)
10951109
mocker.patch(
1096-
"hubspot_sync.management.commands.configure_hubspot_properties._upsert_custom_properties",
1110+
"hubspot_sync.api.upsert_custom_properties",
10971111
)
10981112
mocker.patch(
10991113
"courses.api.exception_logging_generator",
@@ -1112,8 +1126,13 @@ def test_course_certificates_with_course_end_date_self_paced_combination( # noq
11121126
)
11131127

11141128

1129+
@patch("courses.signals.upsert_custom_properties")
11151130
def test_generate_course_certificates_with_course_end_date(
1116-
mocker, courses_api_logs, passed_grade_with_enrollment, settings
1131+
mock_upsert_custom_properties,
1132+
mocker,
1133+
courses_api_logs,
1134+
passed_grade_with_enrollment,
1135+
settings,
11171136
):
11181137
"""Test that certificates are generated for passed grades when there are valid course runs for certificates"""
11191138
course_run = passed_grade_with_enrollment.course_run
@@ -1122,7 +1141,7 @@ def test_generate_course_certificates_with_course_end_date(
11221141

11231142
user = passed_grade_with_enrollment.user
11241143
mocker.patch(
1125-
"hubspot_sync.management.commands.configure_hubspot_properties._upsert_custom_properties",
1144+
"hubspot_sync.api.upsert_custom_properties",
11261145
)
11271146
mocker.patch(
11281147
"courses.api.ensure_course_run_grade",
@@ -1139,10 +1158,11 @@ def test_generate_course_certificates_with_course_end_date(
11391158
)
11401159

11411160

1142-
def test_course_run_certificates_access(mocker):
1161+
@patch("courses.signals.upsert_custom_properties")
1162+
def test_course_run_certificates_access(mock_upsert_custom_properties, mocker):
11431163
"""Tests that the revoke and unrevoke for a course run certificates sets the states properly"""
11441164
mocker.patch(
1145-
"hubspot_sync.management.commands.configure_hubspot_properties._upsert_custom_properties",
1165+
"hubspot_sync.api.upsert_custom_properties",
11461166
)
11471167
test_certificate = CourseRunCertificateFactory.create(is_revoked=False)
11481168

@@ -1264,7 +1284,9 @@ def test_generate_program_certificate_failure_missing_certificates(
12641284
assert len(ProgramCertificate.objects.all()) == 0
12651285

12661286

1287+
@patch("courses.signals.upsert_custom_properties")
12671288
def test_generate_program_certificate_failure_not_all_passed(
1289+
mock_upsert_custom_properties,
12681290
user,
12691291
program_with_requirements, # noqa: F811
12701292
mocker,
@@ -1274,7 +1296,7 @@ def test_generate_program_certificate_failure_not_all_passed(
12741296
if there is not any course_run certificate for the given course.
12751297
"""
12761298
mocker.patch(
1277-
"hubspot_sync.management.commands.configure_hubspot_properties._upsert_custom_properties",
1299+
"hubspot_sync.api.upsert_custom_properties",
12781300
)
12791301
courses = CourseFactory.create_batch(3)
12801302
course_runs = CourseRunFactory.create_batch(3, course=factory.Iterator(courses))
@@ -1291,15 +1313,18 @@ def test_generate_program_certificate_failure_not_all_passed(
12911313
assert len(ProgramCertificate.objects.all()) == 0
12921314

12931315

1294-
def test_generate_program_certificate_success_single_requirement_course(user, mocker):
1316+
@patch("courses.signals.upsert_custom_properties")
1317+
def test_generate_program_certificate_success_single_requirement_course(
1318+
mock_upsert_custom_properties, user, mocker
1319+
):
12951320
"""
12961321
Test that generate_program_certificate generates a program certificate for a Program with a single required Course.
12971322
"""
12981323
patched_sync_hubspot_user = mocker.patch(
12991324
"hubspot_sync.task_helpers.sync_hubspot_user",
13001325
)
13011326
mocker.patch(
1302-
"hubspot_sync.management.commands.configure_hubspot_properties._upsert_custom_properties",
1327+
"hubspot_sync.api.upsert_custom_properties",
13031328
)
13041329
course = CourseFactory.create()
13051330
program = ProgramFactory.create()
@@ -1323,15 +1348,18 @@ def test_generate_program_certificate_success_single_requirement_course(user, mo
13231348
patched_sync_hubspot_user.assert_called_once_with(user)
13241349

13251350

1326-
def test_generate_program_certificate_success_multiple_required_courses(user, mocker):
1351+
@patch("courses.signals.upsert_custom_properties")
1352+
def test_generate_program_certificate_success_multiple_required_courses(
1353+
mock_upsert_custom_properties, user, mocker
1354+
):
13271355
"""
13281356
Test that generate_program_certificate generate a program certificate
13291357
"""
13301358
patched_sync_hubspot_user = mocker.patch(
13311359
"hubspot_sync.task_helpers.sync_hubspot_user",
13321360
)
13331361
mocker.patch(
1334-
"hubspot_sync.management.commands.configure_hubspot_properties._upsert_custom_properties",
1362+
"hubspot_sync.api.upsert_custom_properties",
13351363
)
13361364
courses = CourseFactory.create_batch(3)
13371365
program = ProgramFactory.create()
@@ -1356,13 +1384,16 @@ def test_generate_program_certificate_success_multiple_required_courses(user, mo
13561384
patched_sync_hubspot_user.assert_called_once_with(user)
13571385

13581386

1359-
def test_generate_program_certificate_success_minimum_electives_not_met(user, mocker):
1387+
@patch("courses.signals.upsert_custom_properties")
1388+
def test_generate_program_certificate_success_minimum_electives_not_met(
1389+
mock_upsert_custom_properties, user, mocker
1390+
):
13601391
"""
13611392
Test that generate_program_certificate does not generate a program certificate if minimum electives have not been met.
13621393
"""
13631394
courses = CourseFactory.create_batch(3)
13641395
mocker.patch(
1365-
"hubspot_sync.management.commands.configure_hubspot_properties._upsert_custom_properties",
1396+
"hubspot_sync.api.upsert_custom_properties",
13661397
)
13671398

13681399
# Create Program with 2 minimum elective courses.
@@ -1404,7 +1435,9 @@ def test_generate_program_certificate_success_minimum_electives_not_met(user, mo
14041435
assert len(ProgramCertificate.objects.all()) == 0
14051436

14061437

1438+
@patch("courses.signals.upsert_custom_properties")
14071439
def test_force_generate_program_certificate_success(
1440+
mock_upsert_custom_properties,
14081441
user,
14091442
program_with_requirements, # noqa: F811
14101443
mocker,
@@ -1417,7 +1450,7 @@ def test_force_generate_program_certificate_success(
14171450
"hubspot_sync.task_helpers.sync_hubspot_user",
14181451
)
14191452
mocker.patch(
1420-
"hubspot_sync.management.commands.configure_hubspot_properties._upsert_custom_properties",
1453+
"hubspot_sync.api.upsert_custom_properties",
14211454
)
14221455
courses = CourseFactory.create_batch(3)
14231456
course_runs = CourseRunFactory.create_batch(3, course=factory.Iterator(courses))
@@ -1480,7 +1513,9 @@ def test_program_certificates_access():
14801513
assert test_certificate.is_revoked is False
14811514

14821515

1516+
@patch("courses.signals.upsert_custom_properties")
14831517
def test_generate_program_certificate_failure_not_all_passed_nested_elective_stipulation(
1518+
mock_upsert_custom_properties,
14841519
user,
14851520
mocker,
14861521
):
@@ -1491,7 +1526,7 @@ def test_generate_program_certificate_failure_not_all_passed_nested_elective_sti
14911526
courses = CourseFactory.create_batch(3)
14921527
course_runs = CourseRunFactory.create_batch(3, course=factory.Iterator(courses))
14931528
mocker.patch(
1494-
"hubspot_sync.management.commands.configure_hubspot_properties._upsert_custom_properties",
1529+
"hubspot_sync.api.upsert_custom_properties",
14951530
)
14961531
# Create Program
14971532
program = ProgramFactory.create()
@@ -1566,7 +1601,10 @@ def test_program_enrollment_unenrollment_re_enrollment(
15661601
).exists()
15671602

15681603

1569-
def test_generate_program_certificate_with_subprogram_requirement(user, mocker):
1604+
@patch("courses.signals.upsert_custom_properties")
1605+
def test_generate_program_certificate_with_subprogram_requirement(
1606+
mock_upsert_custom_properties, user, mocker
1607+
):
15701608
"""
15711609
Test that generate_program_certificate considers sub-program (nested program) requirements
15721610
when determining if a user has earned a program certificate.
@@ -1575,7 +1613,7 @@ def test_generate_program_certificate_with_subprogram_requirement(user, mocker):
15751613
"hubspot_sync.task_helpers.sync_hubspot_user",
15761614
)
15771615
mocker.patch(
1578-
"hubspot_sync.management.commands.configure_hubspot_properties._upsert_custom_properties",
1616+
"hubspot_sync.api.upsert_custom_properties",
15791617
)
15801618

15811619
# Create a sub-program that the user will complete
@@ -1620,7 +1658,7 @@ def test_generate_program_certificate_with_subprogram_requirement_missing_certif
16201658
sub-program certificate is missing.
16211659
"""
16221660
mocker.patch(
1623-
"hubspot_sync.management.commands.configure_hubspot_properties._upsert_custom_properties",
1661+
"hubspot_sync.api.upsert_custom_properties",
16241662
)
16251663

16261664
# Create a sub-program
@@ -1643,13 +1681,16 @@ def test_generate_program_certificate_with_subprogram_requirement_missing_certif
16431681
assert len(ProgramCertificate.objects.all()) == 0
16441682

16451683

1646-
def test_generate_program_certificate_with_revoked_subprogram_certificate(user, mocker):
1684+
@patch("courses.signals.upsert_custom_properties")
1685+
def test_generate_program_certificate_with_revoked_subprogram_certificate(
1686+
mock_upsert_custom_properties, user, mocker
1687+
):
16471688
"""
16481689
Test that generate_program_certificate does NOT consider revoked sub-program certificates
16491690
when determining if a user has earned a program certificate.
16501691
"""
16511692
mocker.patch(
1652-
"hubspot_sync.management.commands.configure_hubspot_properties._upsert_custom_properties",
1693+
"hubspot_sync.api.upsert_custom_properties",
16531694
)
16541695

16551696
# Create a sub-program

courses/management/commands/test_manage_certificate.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def test_certificate_management_revoke_unrevoke_invalid_args(
125125
def test_certificate_management_revoke_unrevoke_success(user, revoke, unrevoke, mocker):
126126
"""Test that certificate revoke, un-revoke work as expected and manage the certificate access properly"""
127127
mocker.patch(
128-
"hubspot_sync.management.commands.configure_hubspot_properties._upsert_custom_properties",
128+
"hubspot_sync.api.upsert_custom_properties",
129129
)
130130
course_run = CourseRunFactory.create()
131131
certificate = CourseRunCertificateFactory(
@@ -150,7 +150,7 @@ def test_certificate_management_create(mocker, user, edx_grade_json, revoked):
150150
when a user is provided
151151
"""
152152
mocker.patch(
153-
"hubspot_sync.management.commands.configure_hubspot_properties._upsert_custom_properties",
153+
"hubspot_sync.api.upsert_custom_properties",
154154
)
155155
edx_grade = CurrentGrade(edx_grade_json)
156156
course_run = CourseRunFactory.create()
@@ -192,7 +192,7 @@ def test_certificate_management_create_no_user(mocker, edx_grade_json, user):
192192
enrolled users in a run when no user is provided
193193
"""
194194
mocker.patch(
195-
"hubspot_sync.management.commands.configure_hubspot_properties._upsert_custom_properties",
195+
"hubspot_sync.api.upsert_custom_properties",
196196
)
197197
passed_edx_grade = CurrentGrade(edx_grade_json)
198198
course_run = CourseRunFactory.create()

courses/management/commands/test_manage_program_certificate.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def test_program_certificate_management_create(
130130
creates the program certificate for a user
131131
"""
132132
mocker.patch(
133-
"hubspot_sync.management.commands.configure_hubspot_properties._upsert_custom_properties",
133+
"hubspot_sync.api.upsert_custom_properties",
134134
)
135135
courses = CourseFactory.create_batch(2)
136136
program_with_empty_requirements.add_requirement(courses[0])
@@ -162,7 +162,7 @@ def test_program_certificate_management_force_create(
162162
forcefully creates the certificate for a user
163163
"""
164164
mocker.patch(
165-
"hubspot_sync.management.commands.configure_hubspot_properties._upsert_custom_properties",
165+
"hubspot_sync.api.upsert_custom_properties",
166166
)
167167
courses = CourseFactory.create_batch(3)
168168
course_runs = CourseRunFactory.create_batch(3, course=factory.Iterator(courses))

courses/models_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ def test_course_run_certificate_start_end_dates_and_page_revision(mocker):
420420
Test that the CourseRunCertificate start_end_dates property works properly
421421
"""
422422
mocker.patch(
423-
"hubspot_sync.management.commands.configure_hubspot_properties._upsert_custom_properties",
423+
"hubspot_sync.api.upsert_custom_properties",
424424
)
425425
certificate = CourseRunCertificateFactory.create(
426426
course_run__course__page__certificate_page__product_name="product_name"
@@ -441,7 +441,7 @@ def test_program_certificate_start_end_dates_and_page_revision(user, mocker):
441441
The end date is the date the user received the program certificate.
442442
"""
443443
mocker.patch(
444-
"hubspot_sync.management.commands.configure_hubspot_properties._upsert_custom_properties",
444+
"hubspot_sync.api.upsert_custom_properties",
445445
)
446446
now = now_in_utc()
447447
start_date = now + timedelta(days=1)

courses/signals.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
Signals for mitxonline course certificates
33
"""
44

5+
import logging
6+
57
from django.db import transaction
68
from django.db.models.signals import post_save
79
from django.dispatch import receiver
@@ -11,6 +13,7 @@
1113
CourseRunCertificate,
1214
Program,
1315
)
16+
from hubspot_sync.api import upsert_custom_properties
1417
from hubspot_sync.task_helpers import sync_hubspot_user
1518

1619

@@ -40,4 +43,11 @@ def handle_create_course_run_certificate(
4043
transaction.on_commit(
4144
lambda: generate_multiple_programs_certificate(user, programs)
4245
)
43-
sync_hubspot_user(instance.user)
46+
47+
try:
48+
upsert_custom_properties()
49+
sync_hubspot_user(instance.user)
50+
except Exception: # pylint: disable=broad-except
51+
logger = logging.getLogger(__name__)
52+
logger.exception("Error syncing Hubspot user")
53+
# avoid blocking certificate creation

0 commit comments

Comments
 (0)