From 463e8c82cf76e443160ad87cdb7416582f3e1430 Mon Sep 17 00:00:00 2001 From: Zia Fazal Date: Wed, 27 Jan 2016 19:34:52 +0500 Subject: [PATCH] Cert Social Sharing customization changes after feedback from matte --- common/djangoapps/student/models.py | 6 +- .../certificates/tests/test_webview_views.py | 124 +++++++++++++++++- lms/djangoapps/certificates/views/webview.py | 10 +- lms/envs/test.py | 2 + 4 files changed, 128 insertions(+), 14 deletions(-) diff --git a/common/djangoapps/student/models.py b/common/djangoapps/student/models.py index 64d21af18b..b293149e69 100644 --- a/common/djangoapps/student/models.py +++ b/common/djangoapps/student/models.py @@ -48,6 +48,7 @@ from xmodule_django.models import CourseKeyField, NoneToEmptyManager from certificates.models import GeneratedCertificate from course_modes.models import CourseMode from enrollment.api import _default_course_mode +from microsite_configuration import microsite import lms.lib.comment_client as cc from openedx.core.djangoapps.commerce.utils import ecommerce_api_client, ECOMMERCE_DATE_FORMAT from openedx.core.djangoapps.content.course_overviews.models import CourseOverview @@ -1842,8 +1843,9 @@ class LinkedInAddToProfileConfiguration(ConfigurationModel): target (str): An identifier for the occurrance of the button. """ + company_identifier = microsite.get_value('LINKEDIN_COMPANY_ID', self.company_identifier) params = OrderedDict([ - ('_ed', self.company_identifier), + ('_ed', company_identifier), ('pfCertificationName', self._cert_name(course_name, cert_mode).encode('utf-8')), ('pfCertificationUrl', cert_url), ('source', source) @@ -1863,7 +1865,7 @@ class LinkedInAddToProfileConfiguration(ConfigurationModel): cert_mode, _(u"{platform_name} Certificate for {course_name}") ).format( - platform_name=settings.PLATFORM_NAME, + platform_name=microsite.get_value('platform_name', settings.PLATFORM_NAME), course_name=course_name ) diff --git a/lms/djangoapps/certificates/tests/test_webview_views.py b/lms/djangoapps/certificates/tests/test_webview_views.py index 11058b8116..78103266ba 100644 --- a/lms/djangoapps/certificates/tests/test_webview_views.py +++ b/lms/djangoapps/certificates/tests/test_webview_views.py @@ -7,6 +7,8 @@ import mock from uuid import uuid4 from nose.plugins.attrib import attr from mock import patch +from urllib import urlencode +from collections import OrderedDict from django.conf import settings from django.core.urlresolvers import reverse @@ -82,6 +84,7 @@ class CertificatesViewsTests(ModuleStoreTestCase, EventTrackingTestCase): self.user.profile.save() self.client.login(username=self.user.username, password='foo') self.request = RequestFactory().request() + self.linknedin_url = 'http://www.linkedin.com/profile/add?{params}' self.cert = GeneratedCertificateFactory.create( user=self.user, @@ -172,20 +175,129 @@ class CertificatesViewsTests(ModuleStoreTestCase, EventTrackingTestCase): test_url = get_certificate_url(course_id=self.course.id, uuid=self.cert.verify_uuid) response = self.client.get(test_url) self.assertEqual(response.status_code, 200) - self.assertIn(urllib.quote_plus(self.request.build_absolute_uri(test_url)), response.content) + params = OrderedDict([ + ('_ed', '0_0dPSPyS070e0HsE9HNz_13_d11_',), + ('pfCertificationName', '{platform_name} Honor Code Certificate for {course_name}'.format( + platform_name=settings.PLATFORM_NAME, + course_name=self.course.display_name, + ),), + ('pfCertificationUrl', self.request.build_absolute_uri(test_url),), + ]) + self.assertIn( + self.linknedin_url.format(params=urlencode(params)), + response.content + ) @override_settings(FEATURES=FEATURES_WITH_CERTS_ENABLED) - @mock.patch("microsite_configuration.microsite.is_request_in_microsite", _fake_is_request_in_microsite) def test_linkedin_share_microsites(self): """ - Test: LinkedIn share URL should not be visible when called from within a microsite (for now) + Test: LinkedIn share URL should be visible when called from within a microsite. """ self._add_course_certificates(count=1, signatory_count=1, is_active=True) test_url = get_certificate_url(course_id=self.cert.course_id, uuid=self.cert.verify_uuid) - response = self.client.get(test_url) + response = self.client.get(test_url, HTTP_HOST=settings.MICROSITE_TEST_HOSTNAME) self.assertEqual(response.status_code, 200) - # the URL should not be present - self.assertNotIn(urllib.quote_plus(self.request.build_absolute_uri(test_url)), response.content) + # the linkedIn share URL with appropriate parameters should be present + params = OrderedDict([ + ('_ed', settings.MICROSITE_CONFIGURATION['test_microsite']['LINKEDIN_COMPANY_ID'],), + ('pfCertificationName', '{platform_name} Honor Code Certificate for {course_name}'.format( + platform_name=settings.MICROSITE_CONFIGURATION['test_microsite']['platform_name'], + course_name=self.course.display_name, + ),), + ('pfCertificationUrl', 'http://' + settings.MICROSITE_TEST_HOSTNAME + test_url,), + ]) + self.assertIn( + self.linknedin_url.format(params=urlencode(params)), + response.content + ) + + @override_settings(FEATURES=FEATURES_WITH_CERTS_ENABLED) + @patch.dict("django.conf.settings.SOCIAL_SHARING_SETTINGS", {"CERTIFICATE_FACEBOOK": True}) + def test_facebook_share_microsites(self): + """ + Test: Facebook share URL should be visible when web cert called from within a white label + site and it should use white label site's FACEBOOK_APP_ID. + """ + self._add_course_certificates(count=1, signatory_count=1, is_active=True) + test_url = get_certificate_url(course_id=self.cert.course_id, uuid=self.cert.verify_uuid) + response = self.client.get(test_url, HTTP_HOST=settings.MICROSITE_TEST_HOSTNAME) + self.assertEqual(response.status_code, 200) + self.assertIn("Post on Facebook", response.content) + self.assertIn(settings.MICROSITE_CONFIGURATION['test_microsite']['FACEBOOK_APP_ID'], response.content) + + @override_settings(FEATURES=FEATURES_WITH_CERTS_ENABLED) + @ddt.data( + (False, False, False), + (False, False, True), + (False, True, True), + (True, True, True), + (True, True, False), + ) + @ddt.unpack + def test_social_sharing_availablity_microsites(self, facebook_sharing, twitter_sharing, linkedin_sharing): + """ + Test: Facebook, Twitter and LinkedIn sharing availability for microsites. + """ + self._add_course_certificates(count=1, signatory_count=1, is_active=True) + test_url = get_certificate_url(course_id=self.cert.course_id, uuid=self.cert.verify_uuid) + social_sharing_settings = dict( + CERTIFICATE_FACEBOOK=facebook_sharing, + CERTIFICATE_TWITTER=twitter_sharing, + CERTIFICATE_LINKEDIN=linkedin_sharing, + ) + with patch("django.conf.settings.MICROSITE_CONFIGURATION", { + "test_microsite": dict( + settings.MICROSITE_CONFIGURATION['test_microsite'], + SOCIAL_SHARING_SETTINGS=social_sharing_settings, + ) + }): + response = self.client.get(test_url, HTTP_HOST=settings.MICROSITE_TEST_HOSTNAME) + self.assertEqual(response.status_code, 200) + self.assertEqual("Post on Facebook" in response.content, facebook_sharing) + self.assertEqual("Share on Twitter" in response.content, twitter_sharing) + self.assertEqual("Add to LinkedIn Profile" in response.content, linkedin_sharing) + + @override_settings(FEATURES=FEATURES_WITH_CERTS_ENABLED) + def test_facebook_default_text_microsites(self): + """ + Test: Facebook sharing default text for microsites. + """ + self._add_course_certificates(count=1, signatory_count=1, is_active=True) + test_url = get_certificate_url(course_id=self.cert.course_id, uuid=self.cert.verify_uuid) + facebook_text = "Facebook text on Test Microsite" + social_sharing_settings = dict( + CERTIFICATE_FACEBOOK=True, + CERTIFICATE_FACEBOOK_TEXT=facebook_text, + ) + with patch("django.conf.settings.MICROSITE_CONFIGURATION", { + "test_microsite": dict( + settings.MICROSITE_CONFIGURATION['test_microsite'], + SOCIAL_SHARING_SETTINGS=social_sharing_settings, + ) + }): + response = self.client.get(test_url, HTTP_HOST=settings.MICROSITE_TEST_HOSTNAME) + self.assertContains(response, facebook_text) + + @override_settings(FEATURES=FEATURES_WITH_CERTS_ENABLED) + def test_twitter_default_text_microsites(self): + """ + Test: Twitter sharing default text for microsites. + """ + self._add_course_certificates(count=1, signatory_count=1, is_active=True) + test_url = get_certificate_url(course_id=self.cert.course_id, uuid=self.cert.verify_uuid) + twitter_text = "Twitter text on Test Microsite" + social_sharing_settings = dict( + CERTIFICATE_TWITTER=True, + CERTIFICATE_TWITTER_TEXT=twitter_text, + ) + with patch("django.conf.settings.MICROSITE_CONFIGURATION", { + "test_microsite": dict( + settings.MICROSITE_CONFIGURATION['test_microsite'], + SOCIAL_SHARING_SETTINGS=social_sharing_settings, + ) + }): + response = self.client.get(test_url, HTTP_HOST=settings.MICROSITE_TEST_HOSTNAME) + self.assertContains(response, twitter_text) @override_settings(FEATURES=FEATURES_WITH_CERTS_ENABLED) def test_rendering_course_organization_data(self): diff --git a/lms/djangoapps/certificates/views/webview.py b/lms/djangoapps/certificates/views/webview.py index a77b41741e..4bd8dce563 100644 --- a/lms/djangoapps/certificates/views/webview.py +++ b/lms/djangoapps/certificates/views/webview.py @@ -250,9 +250,9 @@ def _update_social_context(request, context, course, user, user_certificate, pla """ Updates context dictionary with info required for social sharing. """ - share_settings = getattr(settings, 'SOCIAL_SHARING_SETTINGS', {}) + share_settings = microsite.get_value("SOCIAL_SHARING_SETTINGS", settings.SOCIAL_SHARING_SETTINGS) context['facebook_share_enabled'] = share_settings.get('CERTIFICATE_FACEBOOK', False) - context['facebook_app_id'] = getattr(settings, "FACEBOOK_APP_ID", None) + context['facebook_app_id'] = microsite.get_value("FACEBOOK_APP_ID", settings.FACEBOOK_APP_ID) context['facebook_share_text'] = share_settings.get( 'CERTIFICATE_FACEBOOK_TEXT', _("I completed the {course_title} course on {platform_name}.").format( @@ -282,10 +282,8 @@ def _update_social_context(request, context, course, user, user_certificate, pla # Clicking this button sends the user to LinkedIn where they # can add the certificate information to their profile. linkedin_config = LinkedInAddToProfileConfiguration.current() - - # posting certificates to LinkedIn is not currently - # supported in microsites/White Labels - if linkedin_config.enabled and not microsite.is_request_in_microsite(): + linkedin_share_enabled = share_settings.get('CERTIFICATE_LINKEDIN', linkedin_config.enabled) + if linkedin_share_enabled: context['linked_in_url'] = linkedin_config.add_to_profile_url( course.id, course.display_name, diff --git a/lms/envs/test.py b/lms/envs/test.py index 4ff6fd30f5..352ec57497 100644 --- a/lms/envs/test.py +++ b/lms/envs/test.py @@ -449,6 +449,8 @@ MICROSITE_CONFIGURATION = { "ENABLE_SHOPPING_CART": True, "ENABLE_PAID_COURSE_REGISTRATION": True, "SESSION_COOKIE_DOMAIN": "test_microsite.localhost", + "LINKEDIN_COMPANY_ID": "test", + "FACEBOOK_APP_ID": "12345678908", "urls": { 'ABOUT': 'testmicrosite/about', 'PRIVACY': 'testmicrosite/privacy',