Add optional email_opt_in parameter to enrollment end point.
JIRA: MA-286
This commit is contained in:
@@ -15,6 +15,7 @@ from xmodule.modulestore.tests.factories import CourseFactory
|
||||
from util.testing import UrlResetMixin
|
||||
from enrollment import api
|
||||
from enrollment.errors import CourseEnrollmentError
|
||||
from openedx.core.djangoapps.user_api.models import UserOrgTag
|
||||
from student.tests.factories import UserFactory, CourseModeFactory
|
||||
from student.models import CourseEnrollment
|
||||
from embargo.test_utils import restrict_course
|
||||
@@ -82,6 +83,30 @@ class EnrollmentTest(ModuleStoreTestCase, APITestCase):
|
||||
self.assertEqual('honor', data['mode'])
|
||||
self.assertTrue(data['is_active'])
|
||||
|
||||
@ddt.data(
|
||||
(True, u"True"),
|
||||
(False, u"False"),
|
||||
(None, None)
|
||||
)
|
||||
@ddt.unpack
|
||||
def test_email_opt_in_true(self, opt_in, pref_value):
|
||||
"""
|
||||
Verify that the email_opt_in parameter sets the underlying flag.
|
||||
And that if the argument is not present, then it does not affect the flag
|
||||
"""
|
||||
def _assert_no_opt_in_set():
|
||||
""" Check the tag doesn't exit"""
|
||||
with self.assertRaises(UserOrgTag.DoesNotExist):
|
||||
UserOrgTag.objects.get(user=self.user, org=self.course.id.org, key="email-optin")
|
||||
|
||||
_assert_no_opt_in_set()
|
||||
self._create_enrollment(email_opt_in=opt_in)
|
||||
if opt_in is None:
|
||||
_assert_no_opt_in_set()
|
||||
else:
|
||||
preference = UserOrgTag.objects.get(user=self.user, org=self.course.id.org, key="email-optin")
|
||||
self.assertEquals(preference.value, pref_value)
|
||||
|
||||
def test_enroll_prof_ed(self):
|
||||
# Create the prod ed mode.
|
||||
CourseModeFactory.create(
|
||||
@@ -207,21 +232,20 @@ class EnrollmentTest(ModuleStoreTestCase, APITestCase):
|
||||
)
|
||||
self.assertEqual(resp.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
def _create_enrollment(self, course_id=None, username=None, expected_status=status.HTTP_200_OK):
|
||||
def _create_enrollment(self, course_id=None, username=None, expected_status=status.HTTP_200_OK, email_opt_in=None):
|
||||
"""Enroll in the course and verify the URL we are sent to. """
|
||||
course_id = unicode(self.course.id) if course_id is None else course_id
|
||||
username = self.user.username if username is None else username
|
||||
"""Enroll in the course and verify the URL we are sent to. """
|
||||
|
||||
resp = self.client.post(
|
||||
reverse('courseenrollments'),
|
||||
{
|
||||
'course_details': {
|
||||
'course_id': course_id
|
||||
},
|
||||
'user': username
|
||||
params = {
|
||||
'course_details': {
|
||||
'course_id': course_id
|
||||
},
|
||||
format='json'
|
||||
)
|
||||
'user': username
|
||||
}
|
||||
if email_opt_in is not None:
|
||||
params['email_opt_in'] = email_opt_in
|
||||
resp = self.client.post(reverse('courseenrollments'), params, format='json')
|
||||
self.assertEqual(resp.status_code, expected_status)
|
||||
|
||||
if expected_status == status.HTTP_200_OK:
|
||||
|
||||
@@ -5,6 +5,9 @@ consist primarily of authentication, request validation, and serialization.
|
||||
"""
|
||||
from ipware.ip import get_ip
|
||||
from django.conf import settings
|
||||
from opaque_keys import InvalidKeyError
|
||||
from opaque_keys.edx.locator import CourseLocator
|
||||
from openedx.core.djangoapps.user_api import api as user_api
|
||||
from rest_framework import status
|
||||
from rest_framework.authentication import OAuth2Authentication
|
||||
from rest_framework import permissions
|
||||
@@ -204,6 +207,9 @@ class EnrollmentListView(APIView):
|
||||
|
||||
* course_id: The unique identifier for the course.
|
||||
|
||||
* email_opt_in: A boolean indicating whether the user
|
||||
wishes to opt into email from the organization running this course. Optional
|
||||
|
||||
**Response Values**
|
||||
|
||||
A collection of course enrollments for the user, or for the newly created enrollment. Each course enrollment contains:
|
||||
@@ -311,7 +317,12 @@ class EnrollmentListView(APIView):
|
||||
)
|
||||
|
||||
try:
|
||||
return Response(api.add_enrollment(user, unicode(course_id)))
|
||||
response = api.add_enrollment(user, unicode(course_id))
|
||||
email_opt_in = request.DATA.get('email_opt_in', None)
|
||||
if email_opt_in is not None:
|
||||
org = course_id.org
|
||||
user_api.profile.update_email_opt_in(request.user, org, email_opt_in)
|
||||
return Response(response)
|
||||
except CourseModeNotFoundError as error:
|
||||
return Response(
|
||||
status=status.HTTP_400_BAD_REQUEST,
|
||||
|
||||
Reference in New Issue
Block a user