Skip to content

Commit f32f8e8

Browse files
authored
fix: Resolves faling tests when CERTIFICATE_LINKEDIN setting is configured at common.py #37428 (#37437)
In order to be able to share course certificate to LinkedIn, configure SOCIAL_SHARING_SETTINGS with a value True for CERTIFICATE_LINKEDIN entry. The setting is not available by default, but when used there were failing tests and this change ensures all tests pass. Fixes: #37428 (comment)
1 parent f4f14a6 commit f32f8e8

File tree

2 files changed

+176
-97
lines changed

2 files changed

+176
-97
lines changed

common/djangoapps/student/tests/test_certificates.py

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@
1717
from openedx.core.djangolib.testing.utils import skip_unless_lms
1818
from lms.djangoapps.certificates.api import get_certificate_url
1919
from lms.djangoapps.certificates.data import CertificateStatuses
20-
from lms.djangoapps.certificates.tests.factories import (
21-
GeneratedCertificateFactory,
22-
LinkedInAddToProfileConfigurationFactory
23-
)
20+
from lms.djangoapps.certificates.tests.factories import GeneratedCertificateFactory
21+
from openedx.core.djangoapps.site_configuration.tests.test_util import with_site_configuration_context
2422

2523
# pylint: disable=no-member
2624

@@ -155,22 +153,29 @@ def test_unverified_certificate_message(self, enrollment_mode):
155153
'do not have a current verified identity with {platform_name}'
156154
.format(platform_name=settings.PLATFORM_NAME))
157155

158-
def test_post_to_linkedin_visibility(self):
156+
@ddt.data(
157+
(True, True),
158+
(False, False),
159+
)
160+
@ddt.unpack
161+
def test_post_to_linkedin_visibility(self, certificate_linkedin_enabled, linkedin_button_visible):
159162
"""
160-
Verifies that the post certificate to linked button
161-
does not appear by default (when config is not set)
162-
Then Verifies that the post certificate to linked button appears
163-
as expected once a config is set
163+
Verify the LinkedIn "Add to Profile" button visibility based on configuration.
164+
165+
Tests that:
166+
1. When CERTIFICATE_LINKEDIN is False, the LinkedIn button is not visible
167+
2. When CERTIFICATE_LINKEDIN is True, the LinkedIn button appears as expected
164168
"""
165169
self._create_certificate('honor')
166170

167-
# until we set up the configuration, the LinkedIn action
168-
# button should not be visible
169-
self._check_linkedin_visibility(False)
170-
171-
LinkedInAddToProfileConfigurationFactory()
172-
# now we should see it
173-
self._check_linkedin_visibility(True)
171+
# LinkedIn sharing Status True/False
172+
# When CERTIFICATE_LINKEDIN is set to False in site configuration,
173+
# the LinkedIn "Add to Profile" button should not be visible to users
174+
# but if set to True the LinkedIn "Add to Profile" button should be visible
175+
# to users allowing them to share their certificate on LinkedIn
176+
SITE_CONFIGURATION = {"SOCIAL_SHARING_SETTINGS": {"CERTIFICATE_LINKEDIN": certificate_linkedin_enabled}}
177+
with with_site_configuration_context(configuration=SITE_CONFIGURATION):
178+
self._check_linkedin_visibility(linkedin_button_visible)
174179

175180

176181
@ddt.ddt

common/djangoapps/student/tests/tests.py

Lines changed: 155 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase, SharedModuleStoreTestCase # lint-amnesty, pylint: disable=wrong-import-order
5252
from xmodule.modulestore.tests.factories import CourseFactory, check_mongo_calls # lint-amnesty, pylint: disable=wrong-import-order
5353
from xmodule.data import CertificatesDisplayBehaviors # lint-amnesty, pylint: disable=wrong-import-order
54+
from openedx.core.djangoapps.site_configuration.tests.test_util import with_site_configuration_context
55+
5456
log = logging.getLogger(__name__)
5557

5658
BETA_TESTER_METHOD = 'common.djangoapps.student.helpers.access.is_beta_tester'
@@ -90,85 +92,132 @@ def test_cert_info(self):
9092
download_url='http://s3.edx/cert'
9193
)
9294
enrollment = CourseEnrollmentFactory(user=user, course_id=course.id, mode=CourseMode.VERIFIED)
95+
# LinkedIn sharing disabled as we are expecting 'linked_in_url': None in our test case
96+
SITE_CONFIGURATION = {
97+
'SOCIAL_SHARING_SETTINGS': {
98+
'CERTIFICATE_LINKEDIN': False
99+
}
100+
}
101+
with with_site_configuration_context(configuration=SITE_CONFIGURATION):
102+
assert _cert_info(user, enrollment, None) == {
103+
'status': 'processing',
104+
'show_survey_button': False,
105+
'can_unenroll': True,
106+
}
93107

94-
assert _cert_info(user, enrollment, None) ==\
95-
{'status': 'processing', 'show_survey_button': False, 'can_unenroll': True}
96-
97-
cert_status = {'status': 'unavailable', 'mode': 'honor', 'uuid': None}
98-
assert _cert_info(user, enrollment, cert_status) == {'status': 'processing', 'show_survey_button': False,
99-
'mode': 'honor', 'linked_in_url': None,
100-
'can_unenroll': True}
108+
cert_status = {'status': 'unavailable', 'mode': 'honor', 'uuid': None}
109+
assert _cert_info(user, enrollment, cert_status) == {
110+
'status': 'processing',
111+
'show_survey_button': False,
112+
'mode': 'honor',
113+
'linked_in_url': None,
114+
'can_unenroll': True,
115+
}
101116

102-
cert_status = {'status': 'generating', 'grade': '0.67', 'mode': 'honor', 'uuid': None}
103-
with patch('lms.djangoapps.grades.course_grade_factory.CourseGradeFactory.read') as patch_persisted_grade:
104-
patch_persisted_grade.return_value = Mock(percent=1.0)
105-
assert _cert_info(user, enrollment, cert_status) == {'status': 'generating', 'show_survey_button': True,
106-
'survey_url': survey_url, 'grade': '1.0',
107-
'mode': 'honor', 'linked_in_url': None,
108-
'can_unenroll': False}
117+
cert_status = {'status': 'generating', 'grade': '0.67', 'mode': 'honor', 'uuid': None}
118+
with patch('lms.djangoapps.grades.course_grade_factory.CourseGradeFactory.read') as patch_persisted_grade:
119+
patch_persisted_grade.return_value = Mock(percent=1.0)
120+
assert _cert_info(user, enrollment, cert_status) == {
121+
'status': 'generating',
122+
'show_survey_button': True,
123+
'survey_url': survey_url,
124+
'grade': '1.0',
125+
'mode': 'honor',
126+
'linked_in_url': None,
127+
'can_unenroll': False,
128+
}
129+
130+
cert_status = {'status': 'generating', 'grade': '0.67', 'mode': 'honor', 'uuid': None}
131+
assert _cert_info(user, enrollment, cert_status) == {
132+
'status': 'generating',
133+
'show_survey_button': True,
134+
'survey_url': survey_url,
135+
'grade': '0.67',
136+
'mode': 'honor',
137+
'linked_in_url': None,
138+
'can_unenroll': False,
139+
}
109140

110-
cert_status = {'status': 'generating', 'grade': '0.67', 'mode': 'honor', 'uuid': None}
111-
assert _cert_info(user, enrollment, cert_status) == {'status': 'generating', 'show_survey_button': True,
112-
'survey_url': survey_url, 'grade': '0.67', 'mode': 'honor',
113-
'linked_in_url': None, 'can_unenroll': False}
141+
cert_status = {
142+
'status': 'downloadable',
143+
'grade': '0.67',
144+
'download_url': cert.download_url,
145+
'mode': 'honor',
146+
'uuid': 'fakeuuidbutitsfine',
147+
}
148+
assert _cert_info(user, enrollment, cert_status) == {
149+
'status': 'downloadable',
150+
'download_url': cert.download_url,
151+
'show_survey_button': True,
152+
'survey_url': survey_url,
153+
'grade': '0.67',
154+
'mode': 'honor',
155+
'linked_in_url': None,
156+
'can_unenroll': False,
157+
}
114158

115-
cert_status = {
116-
'status': 'downloadable',
117-
'grade': '0.67',
118-
'download_url': cert.download_url,
119-
'mode': 'honor',
120-
'uuid': 'fakeuuidbutitsfine',
121-
}
122-
assert _cert_info(user, enrollment, cert_status) == {'status': 'downloadable',
123-
'download_url': cert.download_url,
124-
'show_survey_button': True, 'survey_url': survey_url,
125-
'grade': '0.67', 'mode': 'honor', 'linked_in_url': None,
126-
'can_unenroll': False}
159+
cert_status = {
160+
'status': 'notpassing',
161+
'grade': '0.67',
162+
'download_url': cert.download_url,
163+
'mode': 'honor',
164+
'uuid': 'fakeuuidbutitsfine',
165+
}
166+
assert _cert_info(user, enrollment, cert_status) == {
167+
'status': 'notpassing',
168+
'show_survey_button': True,
169+
'survey_url': survey_url,
170+
'grade': '0.67',
171+
'mode': 'honor',
172+
'linked_in_url': None,
173+
'can_unenroll': True,
174+
}
127175

128-
cert_status = {
129-
'status': 'notpassing', 'grade': '0.67',
130-
'download_url': cert.download_url,
131-
'mode': 'honor',
132-
'uuid': 'fakeuuidbutitsfine',
133-
}
134-
assert _cert_info(user, enrollment, cert_status) == {'status': 'notpassing', 'show_survey_button': True,
135-
'survey_url': survey_url, 'grade': '0.67', 'mode': 'honor',
136-
'linked_in_url': None, 'can_unenroll': True}
137-
138-
# Test a course that doesn't have a survey specified
139-
course2 = CourseOverviewFactory.create(
140-
end_of_course_survey_url=None,
141-
certificates_display_behavior='end',
142-
)
143-
enrollment2 = CourseEnrollmentFactory(user=user, course_id=course2.id, mode=CourseMode.VERIFIED)
176+
# Test a course that doesn't have a survey specified
177+
course2 = CourseOverviewFactory.create(
178+
end_of_course_survey_url=None,
179+
certificates_display_behavior='end',
180+
)
181+
enrollment2 = CourseEnrollmentFactory(user=user, course_id=course2.id, mode=CourseMode.VERIFIED)
144182

145-
cert_status = {
146-
'status': 'notpassing', 'grade': '0.67',
147-
'download_url': cert.download_url, 'mode': 'honor', 'uuid': 'fakeuuidbutitsfine'
148-
}
149-
assert _cert_info(user, enrollment2, cert_status) == {'status': 'notpassing', 'show_survey_button': False,
150-
'grade': '0.67', 'mode': 'honor', 'linked_in_url': None,
151-
'can_unenroll': True}
183+
cert_status = {
184+
'status': 'notpassing', 'grade': '0.67',
185+
'download_url': cert.download_url, 'mode': 'honor', 'uuid': 'fakeuuidbutitsfine'
186+
}
187+
assert _cert_info(user, enrollment2, cert_status) == {
188+
'status': 'notpassing',
189+
'show_survey_button': False,
190+
'grade': '0.67',
191+
'mode': 'honor',
192+
'linked_in_url': None,
193+
'can_unenroll': True
194+
}
152195

153-
course3 = CourseOverviewFactory.create(
154-
end_of_course_survey_url=None,
155-
certificates_display_behavior='early_no_info',
156-
)
157-
enrollment3 = CourseEnrollmentFactory(user=user, course_id=course3.id, mode=CourseMode.VERIFIED)
158-
# test when the display is unavailable or notpassing, we get the correct results out
159-
course2.certificates_display_behavior = CertificatesDisplayBehaviors.EARLY_NO_INFO
160-
cert_status = {'status': 'unavailable', 'mode': 'honor', 'uuid': None}
161-
assert _cert_info(user, enrollment3, cert_status) == {'status': 'processing', 'show_survey_button': False,
162-
'can_unenroll': True}
196+
course3 = CourseOverviewFactory.create(
197+
end_of_course_survey_url=None,
198+
certificates_display_behavior='early_no_info',
199+
)
200+
enrollment3 = CourseEnrollmentFactory(user=user, course_id=course3.id, mode=CourseMode.VERIFIED)
201+
# test when the display is unavailable or notpassing, we get the correct results out
202+
course2.certificates_display_behavior = CertificatesDisplayBehaviors.EARLY_NO_INFO
203+
cert_status = {'status': 'unavailable', 'mode': 'honor', 'uuid': None}
204+
assert _cert_info(user, enrollment3, cert_status) == {
205+
'status': 'processing',
206+
'show_survey_button': False,
207+
'can_unenroll': True
208+
}
163209

164-
cert_status = {
165-
'status': 'notpassing', 'grade': '0.67',
166-
'download_url': cert.download_url,
167-
'mode': 'honor',
168-
'uuid': 'fakeuuidbutitsfine'
169-
}
170-
assert _cert_info(user, enrollment3, cert_status) == {'status': 'processing', 'show_survey_button': False,
171-
'can_unenroll': True}
210+
cert_status = {
211+
'status': 'notpassing', 'grade': '0.67',
212+
'download_url': cert.download_url,
213+
'mode': 'honor',
214+
'uuid': 'fakeuuidbutitsfine'
215+
}
216+
assert _cert_info(user, enrollment3, cert_status) == {
217+
'status': 'processing',
218+
'show_survey_button': False,
219+
'can_unenroll': True
220+
}
172221

173222
def test_cert_info_beta_tester(self):
174223
user = UserFactory.create()
@@ -192,15 +241,22 @@ def test_cert_info_beta_tester(self):
192241
'uuid': 'blah',
193242
}
194243
with patch(BETA_TESTER_METHOD, return_value=False):
195-
assert _cert_info(user, enrollment, cert_status) == {
196-
'status': status,
197-
'download_url': cert.download_url,
198-
'show_survey_button': False,
199-
'grade': grade,
200-
'mode': mode,
201-
'linked_in_url': None,
202-
'can_unenroll': False
244+
# LinkedIn sharing disabled as we are expecting 'linked_in_url': None in our test case
245+
SITE_CONFIGURATION = {
246+
'SOCIAL_SHARING_SETTINGS': {
247+
'CERTIFICATE_LINKEDIN': False
248+
}
203249
}
250+
with with_site_configuration_context(configuration=SITE_CONFIGURATION):
251+
assert _cert_info(user, enrollment, cert_status) == {
252+
'status': status,
253+
'download_url': cert.download_url,
254+
'show_survey_button': False,
255+
'grade': grade,
256+
'mode': mode,
257+
'linked_in_url': None,
258+
'can_unenroll': False
259+
}
204260

205261
with patch(BETA_TESTER_METHOD, return_value=True):
206262
assert _cert_info(user, enrollment, cert_status) == {
@@ -397,7 +453,16 @@ def test_linked_in_add_to_profile_btn_not_appearing_without_config(self):
397453
grade='67',
398454
download_url=download_url
399455
)
400-
response = self.client.get(reverse('dashboard'))
456+
# LinkedIn sharing disabled
457+
# When CERTIFICATE_LINKEDIN is set to False in site configuration,
458+
# the LinkedIn "Add to Profile" button should not be visible to users
459+
SITE_CONFIGURATION = {
460+
'SOCIAL_SHARING_SETTINGS': {
461+
'CERTIFICATE_LINKEDIN': False
462+
}
463+
}
464+
with with_site_configuration_context(configuration=SITE_CONFIGURATION):
465+
response = self.client.get(reverse('dashboard'))
401466

402467
assert response.status_code == 200
403468
self.assertNotContains(response, 'Add Certificate to LinkedIn')
@@ -435,7 +500,16 @@ def test_linked_in_add_to_profile_btn_with_certificate(self):
435500
grade='67',
436501
download_url='https://www.edx.org'
437502
)
438-
response = self.client.get(reverse('dashboard'))
503+
# LinkedIn sharing disabled
504+
# When CERTIFICATE_LINKEDIN is set to False in site configuration,
505+
# the LinkedIn "Add to Profile" button should not be visible to users
506+
SITE_CONFIGURATION = {
507+
'SOCIAL_SHARING_SETTINGS': {
508+
'CERTIFICATE_LINKEDIN': True
509+
}
510+
}
511+
with with_site_configuration_context(configuration=SITE_CONFIGURATION):
512+
response = self.client.get(reverse('dashboard'))
439513

440514
assert response.status_code == 200
441515
self.assertContains(response, 'Add Certificate to LinkedIn')

0 commit comments

Comments
 (0)