Skip to content

Commit

Permalink
fix: Integraty error in enrollments api (#33265)
Browse files Browse the repository at this point in the history
  • Loading branch information
AhtishamShahid authored Sep 15, 2023
1 parent 5fcad88 commit 202e790
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
24 changes: 16 additions & 8 deletions openedx/core/djangoapps/enrollments/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
from unittest.mock import patch
from urllib.parse import quote

import pytest
import ddt
import httpretty
import pytest
import pytz
from django.conf import settings
from django.core.cache import cache
Expand All @@ -20,32 +20,35 @@
from django.test import Client
from django.test.utils import override_settings
from django.urls import reverse
from edx_toggles.toggles.testutils import override_waffle_flag
from freezegun import freeze_time
from rest_framework import status
from rest_framework.test import APITestCase
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
from xmodule.modulestore.tests.factories import CourseFactory, check_mongo_calls_range

from common.djangoapps.course_modes.models import CourseMode
from common.djangoapps.course_modes.tests.factories import CourseModeFactory
from common.djangoapps.student.models import CourseEnrollment
from common.djangoapps.student.roles import CourseStaffRole
from common.djangoapps.student.tests.factories import AdminFactory, SuperuserFactory, UserFactory
from common.djangoapps.util.models import RateLimitConfiguration
from common.djangoapps.util.testing import UrlResetMixin
from openedx.core.djangoapps.content.course_overviews.models import CourseOverview
from openedx.core.djangoapps.course_groups import cohorts
from openedx.core.djangoapps.embargo.models import Country, CountryAccessRule, RestrictedCourse
from openedx.core.djangoapps.embargo.test_utils import restrict_course
from openedx.core.djangoapps.enrollments import api, data
from openedx.core.djangoapps.enrollments.errors import CourseEnrollmentError
from openedx.core.djangoapps.enrollments.views import EnrollmentUserThrottle
from openedx.core.djangoapps.notifications.handlers import ENABLE_NOTIFICATIONS
from openedx.core.djangoapps.notifications.models import CourseNotificationPreference
from openedx.core.djangoapps.oauth_dispatch.jwt import create_jwt_for_user
from openedx.core.djangoapps.user_api.models import RetirementState, UserOrgTag, UserRetirementStatus
from openedx.core.djangolib.testing.utils import skip_unless_lms
from openedx.core.lib.django_test_client_utils import get_absolute_url
from openedx.features.enterprise_support.tests import FAKE_ENTERPRISE_CUSTOMER
from openedx.features.enterprise_support.tests.mixins.enterprise import EnterpriseServiceMockMixin
from common.djangoapps.student.models import CourseEnrollment
from common.djangoapps.student.roles import CourseStaffRole
from common.djangoapps.student.tests.factories import AdminFactory, SuperuserFactory, UserFactory
from common.djangoapps.util.models import RateLimitConfiguration
from common.djangoapps.util.testing import UrlResetMixin
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
from xmodule.modulestore.tests.factories import CourseFactory, check_mongo_calls_range


class EnrollmentTestMixin:
Expand Down Expand Up @@ -153,6 +156,7 @@ def _get_enrollments(self):


@override_settings(EDX_API_KEY="i am a key")
@override_waffle_flag(ENABLE_NOTIFICATIONS, True)
@ddt.ddt
@skip_unless_lms
class EnrollmentTest(EnrollmentTestMixin, ModuleStoreTestCase, APITestCase, EnterpriseServiceMockMixin):
Expand Down Expand Up @@ -195,6 +199,10 @@ def setUp(self):
password=self.PASSWORD,
)
self.client.login(username=self.USERNAME, password=self.PASSWORD)
CourseNotificationPreference.objects.create(
user=self.user,
course_id=self.course.id,
)

@ddt.data(
# Default (no course modes in the database)
Expand Down
11 changes: 6 additions & 5 deletions openedx/core/djangoapps/notifications/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import logging

from django.core.exceptions import ObjectDoesNotExist
from django.db import IntegrityError
from django.db import IntegrityError, transaction
from django.dispatch import receiver
from openedx_events.learning.signals import (
COURSE_ENROLLMENT_CREATED,
Expand All @@ -28,10 +28,11 @@ def course_enrollment_post_save(signal, sender, enrollment, metadata, **kwargs):
"""
if ENABLE_NOTIFICATIONS.is_enabled(enrollment.course.course_key):
try:
CourseNotificationPreference.objects.create(
user_id=enrollment.user.id,
course_id=enrollment.course.course_key
)
with transaction.atomic():
CourseNotificationPreference.objects.create(
user_id=enrollment.user.id,
course_id=enrollment.course.course_key
)
except IntegrityError:
log.info(f'CourseNotificationPreference already exists for user {enrollment.user.id} '
f'and course {enrollment.course.course_key}')
Expand Down

0 comments on commit 202e790

Please sign in to comment.