Commit 2648a639 by Awais Jibran

Fix email opt in list

parent 97816857
......@@ -237,7 +237,9 @@ class Command(BaseCommand):
email, full_name, course_id, is_opted_in, pref_set_datetime = row
writer.writerow({
"email": email.encode('utf-8'),
"full_name": full_name.encode('utf-8'),
# There should not be a case where users are without full_names. We only need this safe check because
# of ECOM-1995.
"full_name": full_name.encode('utf-8') if full_name else '',
"course_id": course_id.encode('utf-8'),
"is_opted_in_for_email": is_opted_in if is_opted_in else "True",
"preference_set_datetime": pref_set_datetime if pref_set_datetime else self.DEFAULT_DATETIME_STR,
......
......@@ -10,6 +10,7 @@ from unittest import skipUnless
import ddt
from django.conf import settings
from django.contrib.auth.models import User
from django.core.management.base import CommandError
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
......@@ -257,6 +258,24 @@ class EmailOptInListTest(ModuleStoreTestCase):
with self.assertRaisesRegexp(CommandError, "^File already exists"):
email_opt_in_list.Command().handle(temp_file.name, self.TEST_ORG)
def test_no_user_profile(self):
"""
Tests that command does not break if a user has no profile.
"""
self._create_courses_and_enrollments((self.TEST_ORG, True))
self._set_opt_in_pref(self.user, self.TEST_ORG, True)
# Remove the user profile, and re-fetch user
self.assertTrue(hasattr(self.user, 'profile'))
self.user.profile.delete()
user = User.objects.get(id=self.user.id)
# Test that user do not have profile
self.assertFalse(hasattr(user, 'profile'))
output = self._run_command(self.TEST_ORG)
self._assert_output(output, (user, self.courses[0].id, True))
def _create_courses_and_enrollments(self, *args):
"""Create courses and enrollments.
......@@ -393,7 +412,11 @@ class EmailOptInListTest(ModuleStoreTestCase):
for user, course_id, opt_in_pref in args:
self.assertIn({
"email": user.email.encode('utf-8'),
"full_name": user.profile.name.encode('utf-8'),
"full_name": (
user.profile.name.encode('utf-8')
if hasattr(user, 'profile')
else ''
),
"course_id": unicode(course_id).encode('utf-8'),
"is_opted_in_for_email": unicode(opt_in_pref),
"preference_set_datetime": (
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment