-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Create new API endpoint for email optin #6060
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,18 @@ | ||
| # -*- coding: utf-8 -*- | ||
| """ Tests for the profile API. """ | ||
| from django.contrib.auth.models import User | ||
|
|
||
| from django.test import TestCase | ||
| import ddt | ||
| from django.test.utils import override_settings | ||
| from nose.tools import raises | ||
| from dateutil.parser import parse as parse_datetime | ||
| from xmodule.modulestore.tests.factories import CourseFactory | ||
| import datetime | ||
|
|
||
| from user_api.api import account as account_api | ||
| from user_api.api import profile as profile_api | ||
| from user_api.models import UserProfile | ||
| from user_api.models import UserProfile, UserOrgTag | ||
|
|
||
|
|
||
| @ddt.ddt | ||
|
|
@@ -94,6 +98,84 @@ def test_update_and_retrieve_preference_info(self): | |
| preferences = profile_api.preference_info(self.USERNAME) | ||
| self.assertEqual(preferences['preference_key'], 'preference_value') | ||
|
|
||
| @ddt.data( | ||
| # Check that a 27 year old can opt-in | ||
| (27, True, u"True"), | ||
|
|
||
| # Check that a 32-year old can opt-out | ||
| (32, False, u"False"), | ||
|
|
||
| # Check that someone 13 years old can opt-in | ||
| (13, True, u"True"), | ||
|
|
||
| # Check that someone 12 years old cannot opt-in | ||
| (12, True, u"False") | ||
| ) | ||
| @ddt.unpack | ||
| @override_settings(EMAIL_OPTIN_MINIMUM_AGE=13) | ||
| def test_update_email_optin(self, age, option, expected_result): | ||
| # Create the course and account. | ||
| course = CourseFactory.create() | ||
| account_api.create_account(self.USERNAME, self.PASSWORD, self.EMAIL) | ||
|
|
||
| # Set year of birth | ||
| user = User.objects.get(username=self.USERNAME) | ||
| profile = UserProfile.objects.get(user=user) | ||
| year_of_birth = datetime.datetime.now().year - age # pylint: disable=maybe-no-member | ||
| profile.year_of_birth = year_of_birth | ||
| profile.save() | ||
|
|
||
| profile_api.update_email_opt_in(self.USERNAME, course.id.org, option) | ||
| result_obj = UserOrgTag.objects.get(user=user, org=course.id.org, key='email-optin') | ||
| self.assertEqual(result_obj.value, expected_result) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a test case for updating a preference that has already been set.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a test case for when the user's year of birth isn't provided.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added the new test cases. Caught some problems with my code, too, so thanks! |
||
|
|
||
| def test_update_email_optin_no_age_set(self): | ||
| # Test that the API still works if no age is specified. | ||
| # Create the course and account. | ||
| course = CourseFactory.create() | ||
| account_api.create_account(self.USERNAME, self.PASSWORD, self.EMAIL) | ||
|
|
||
| user = User.objects.get(username=self.USERNAME) | ||
|
|
||
| profile_api.update_email_opt_in(self.USERNAME, course.id.org, True) | ||
| result_obj = UserOrgTag.objects.get(user=user, org=course.id.org, key='email-optin') | ||
| self.assertEqual(result_obj.value, u"True") | ||
|
|
||
| @ddt.data( | ||
| # Check that a 27 year old can opt-in, then out. | ||
| (27, True, False, u"False"), | ||
|
|
||
| # Check that a 32-year old can opt-out, then in. | ||
| (32, False, True, u"True"), | ||
|
|
||
| # Check that someone 13 years old can opt-in, then out. | ||
| (13, True, False, u"False"), | ||
|
|
||
| # Check that someone 12 years old cannot opt-in, then explicitly out. | ||
| (12, True, False, u"False") | ||
| ) | ||
| @ddt.unpack | ||
| @override_settings(EMAIL_OPTIN_MINIMUM_AGE=13) | ||
| def test_change_email_optin(self, age, option, second_option, expected_result): | ||
| # Create the course and account. | ||
| course = CourseFactory.create() | ||
| account_api.create_account(self.USERNAME, self.PASSWORD, self.EMAIL) | ||
|
|
||
| # Set year of birth | ||
| user = User.objects.get(username=self.USERNAME) | ||
| profile = UserProfile.objects.get(user=user) | ||
| year_of_birth = datetime.datetime.now().year - age # pylint: disable=maybe-no-member | ||
| profile.year_of_birth = year_of_birth | ||
| profile.save() | ||
|
|
||
| profile_api.update_email_opt_in(self.USERNAME, course.id.org, option) | ||
| profile_api.update_email_opt_in(self.USERNAME, course.id.org, second_option) | ||
|
|
||
| result_obj = UserOrgTag.objects.get(user=user, org=course.id.org, key='email-optin') | ||
| self.assertEqual(result_obj.value, expected_result) | ||
|
|
||
|
|
||
|
|
||
| @raises(profile_api.ProfileUserNotFound) | ||
| def test_retrieve_and_update_preference_info_no_user(self): | ||
| preferences = profile_api.preference_info(self.USERNAME) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This shouldn't be necessary, since test settings inherit from common. Maybe this setting needs to be added to
cms/envs/common.pyas well, since the app is incommon/djangoapps?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This wasn't necessary, I had just added it as a precaution in case someone changed the settings locally in their common.py, this test would still pass.