From 2648a63903255bfc11d857745433652e1a384a35 Mon Sep 17 00:00:00 2001 From: Awais Jibran Date: Mon, 6 Jun 2016 15:38:21 +0500 Subject: [PATCH] Fix email opt in list --- .../management/commands/email_opt_in_list.py | 4 ++- .../tests/test_email_opt_in_list.py | 25 ++++++++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/openedx/core/djangoapps/user_api/management/commands/email_opt_in_list.py b/openedx/core/djangoapps/user_api/management/commands/email_opt_in_list.py index 61c2d3e483..86d8a6bd7b 100644 --- a/openedx/core/djangoapps/user_api/management/commands/email_opt_in_list.py +++ b/openedx/core/djangoapps/user_api/management/commands/email_opt_in_list.py @@ -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, diff --git a/openedx/core/djangoapps/user_api/management/tests/test_email_opt_in_list.py b/openedx/core/djangoapps/user_api/management/tests/test_email_opt_in_list.py index ca8f54ea42..ab6b71c5f1 100644 --- a/openedx/core/djangoapps/user_api/management/tests/test_email_opt_in_list.py +++ b/openedx/core/djangoapps/user_api/management/tests/test_email_opt_in_list.py @@ -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": (