-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hooking the logistration page into the new HTTP endpoint for email op…
…t in. Update the PR based on Code Review comments, and additional tests. Using underscore js
- Loading branch information
stephensanchez
committed
Dec 1, 2014
1 parent
eacd525
commit 32c9230
Showing
9 changed files
with
197 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,10 +14,12 @@ | |
import ddt | ||
from pytz import UTC | ||
import mock | ||
from xmodule.modulestore.tests.factories import CourseFactory | ||
|
||
from user_api.api import account as account_api, profile as profile_api | ||
|
||
from student.tests.factories import UserFactory | ||
from user_api.models import UserOrgTag | ||
from user_api.tests.factories import UserPreferenceFactory | ||
from django_comment_common import models | ||
from opaque_keys.edx.locations import SlashSeparatedCourseKey | ||
|
@@ -1468,3 +1470,59 @@ def _assert_reg_field(self, extra_fields_setting, expected_field): | |
# Verify that the form description matches what we'd expect | ||
form_desc = json.loads(response.content) | ||
self.assertIn(expected_field, form_desc["fields"]) | ||
|
||
|
||
@ddt.ddt | ||
class UpdateEmailOptInTestCase(ApiTestCase): | ||
"""Tests the UpdateEmailOptInPreference view. """ | ||
|
||
USERNAME = "steve" | ||
EMAIL = "[email protected]" | ||
PASSWORD = "steveopolis" | ||
|
||
def setUp(self): | ||
""" Create a course and user, then log in. """ | ||
super(UpdateEmailOptInTestCase, self).setUp() | ||
self.course = CourseFactory.create() | ||
self.user = UserFactory.create(username=self.USERNAME, email=self.EMAIL, password=self.PASSWORD) | ||
self.client.login(username=self.USERNAME, password=self.PASSWORD) | ||
self.url = reverse("preferences_email_opt_in") | ||
|
||
@ddt.data( | ||
(u"True", u"True"), | ||
(u"true", u"True"), | ||
(u"TrUe", u"True"), | ||
(u"Banana", u"False"), | ||
(u"strawberries", u"False"), | ||
(u"False", u"False"), | ||
) | ||
@ddt.unpack | ||
def test_update_email_opt_in(self, opt, result): | ||
"""Tests the email opt in preference""" | ||
# Register, which should trigger an activation email | ||
response = self.client.post(self.url, { | ||
"course_id": unicode(self.course.id), | ||
"email_opt_in": opt | ||
}) | ||
self.assertHttpOK(response) | ||
preference = UserOrgTag.objects.get( | ||
user=self.user, org=self.course.id.org, key="email-optin" | ||
) | ||
self.assertEquals(preference.value, result) | ||
|
||
@ddt.data( | ||
(True, False), | ||
(False, True), | ||
(False, False) | ||
) | ||
@ddt.unpack | ||
def test_update_email_opt_in_wrong_params(self, use_course_id, use_opt_in): | ||
"""Tests the email opt in preference""" | ||
params = {} | ||
if use_course_id: | ||
params["course_id"] = unicode(self.course.id) | ||
if use_opt_in: | ||
params["email_opt_in"] = u"True" | ||
|
||
response = self.client.post(self.url, params) | ||
self.assertHttpBadRequest(response) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
define(['js/common_helpers/ajax_helpers', 'js/student_account/emailoptin'], | ||
function( AjaxHelpers, EmailOptInInterface ) { | ||
'use strict'; | ||
|
||
describe( 'edx.student.account.EmailOptInInterface', function() { | ||
|
||
var COURSE_KEY = 'edX/DemoX/Fall', | ||
EMAIL_OPT_IN = 'True', | ||
EMAIL_OPT_IN_URL = '/user_api/v1/preferences/email_opt_in/'; | ||
|
||
it('Opts in for organization emails', function() { | ||
// Spy on Ajax requests | ||
var requests = AjaxHelpers.requests( this ); | ||
|
||
// Attempt to enroll the user | ||
EmailOptInInterface.setPreference( COURSE_KEY, EMAIL_OPT_IN ); | ||
|
||
// Expect that the correct request was made to the server | ||
AjaxHelpers.expectRequest( | ||
requests, 'POST', EMAIL_OPT_IN_URL, 'course_id=edX%2FDemoX%2FFall&email_opt_in=True' | ||
); | ||
|
||
// Simulate a successful response from the server | ||
AjaxHelpers.respondWithJson(requests, {}); | ||
}); | ||
}); | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
var edx = edx || {}; | ||
|
||
(function($) { | ||
'use strict'; | ||
|
||
edx.student = edx.student || {}; | ||
edx.student.account = edx.student.account || {}; | ||
|
||
edx.student.account.EmailOptInInterface = { | ||
|
||
urls: { | ||
emailOptInUrl: '/user_api/v1/preferences/email_opt_in/' | ||
}, | ||
|
||
headers: { | ||
'X-CSRFToken': $.cookie('csrftoken') | ||
}, | ||
|
||
/** | ||
* Set the email opt in setting for the organization associated | ||
* with this course. | ||
* @param {string} courseKey Slash-separated course key. | ||
* @param {string} emailOptIn The preference to opt in or out of organization emails. | ||
*/ | ||
setPreference: function( courseKey, emailOptIn, context ) { | ||
return $.ajax({ | ||
url: this.urls.emailOptInUrl, | ||
type: 'POST', | ||
data: {course_id: courseKey, email_opt_in: emailOptIn}, | ||
headers: this.headers, | ||
context: context | ||
}); | ||
} | ||
}; | ||
})(jQuery); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters