Commit 2bd26fc0 by Zia Fazal Committed by Matt Drayer

fixed bug in certificate html view

Fixed bug: When certificate id previewed by a user who already has a
certificate generated with different mode it would not display
certificate in right preview mode.
parent 55e2a326
...@@ -12,6 +12,7 @@ from django.test.utils import override_settings ...@@ -12,6 +12,7 @@ from django.test.utils import override_settings
from openedx.core.lib.tests.assertions.events import assert_event_matches from openedx.core.lib.tests.assertions.events import assert_event_matches
from student.tests.factories import UserFactory, CourseEnrollmentFactory from student.tests.factories import UserFactory, CourseEnrollmentFactory
from student.roles import CourseStaffRole
from track.tests import EventTrackingTestCase from track.tests import EventTrackingTestCase
from xmodule.modulestore.tests.factories import CourseFactory from xmodule.modulestore.tests.factories import CourseFactory
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
...@@ -347,6 +348,13 @@ class CertificatesViewsTests(ModuleStoreTestCase, EventTrackingTestCase): ...@@ -347,6 +348,13 @@ class CertificatesViewsTests(ModuleStoreTestCase, EventTrackingTestCase):
course_id=unicode(self.course.id) course_id=unicode(self.course.id)
) )
response = self.client.get(test_url + '?preview=honor') response = self.client.get(test_url + '?preview=honor')
#accessing certificate web view in preview mode without
# staff or instructor access should show invalid certificate
self.assertIn('This is an invalid certificate number', response.content)
CourseStaffRole(self.course.id).add_users(self.user)
response = self.client.get(test_url + '?preview=honor')
self.assertNotIn(self.course.display_name, response.content) self.assertNotIn(self.course.display_name, response.content)
self.assertIn('course_title_0', response.content) self.assertIn('course_title_0', response.content)
self.assertIn('Signatory_Title 0', response.content) self.assertIn('Signatory_Title 0', response.content)
...@@ -359,6 +367,27 @@ class CertificatesViewsTests(ModuleStoreTestCase, EventTrackingTestCase): ...@@ -359,6 +367,27 @@ class CertificatesViewsTests(ModuleStoreTestCase, EventTrackingTestCase):
self.assertIn('Signatory_Title 0', response.content) self.assertIn('Signatory_Title 0', response.content)
@override_settings(FEATURES=FEATURES_WITH_CERTS_ENABLED) @override_settings(FEATURES=FEATURES_WITH_CERTS_ENABLED)
def test_render_html_view_with_preview_mode_when_user_already_has_cert(self):
"""
test certificate web view should render properly in
preview mode even if user who is previewing already has a certificate
generated with different mode.
"""
self._add_course_certificates(count=1, signatory_count=2)
CourseStaffRole(self.course.id).add_users(self.user)
test_url = get_certificate_url(
user_id=self.user.id,
course_id=unicode(self.course.id)
)
# user has already has certificate generated for 'honor' mode
# so let's try to preview in 'verified' mode.
response = self.client.get(test_url + '?preview=verified')
self.assertNotIn(self.course.display_name, response.content)
self.assertIn('course_title_0', response.content)
self.assertIn('Signatory_Title 0', response.content)
@override_settings(FEATURES=FEATURES_WITH_CERTS_ENABLED)
def test_render_html_view_invalid_certificate_configuration(self): def test_render_html_view_invalid_certificate_configuration(self):
test_url = get_certificate_url( test_url = get_certificate_url(
user_id=self.user.id, user_id=self.user.id,
......
...@@ -14,6 +14,7 @@ from django.utils.translation import ugettext as _ ...@@ -14,6 +14,7 @@ from django.utils.translation import ugettext as _
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from courseware.courses import course_image_url from courseware.courses import course_image_url
from courseware.access import has_access
from edxmako.shortcuts import render_to_response from edxmako.shortcuts import render_to_response
from edxmako.template import Template from edxmako.template import Template
from eventtracking import tracker from eventtracking import tracker
...@@ -287,6 +288,7 @@ def render_html_view(request, user_id, course_id): ...@@ -287,6 +288,7 @@ def render_html_view(request, user_id, course_id):
context = {} context = {}
context['platform_name'] = microsite.get_value("platform_name", settings.PLATFORM_NAME) context['platform_name'] = microsite.get_value("platform_name", settings.PLATFORM_NAME)
context['course_id'] = course_id context['course_id'] = course_id
preview_mode = request.GET.get('preview', None)
# Update the view context with the default ConfigurationModel settings # Update the view context with the default ConfigurationModel settings
configuration = CertificateHtmlViewConfiguration.get_config() configuration = CertificateHtmlViewConfiguration.get_config()
...@@ -334,6 +336,13 @@ def render_html_view(request, user_id, course_id): ...@@ -334,6 +336,13 @@ def render_html_view(request, user_id, course_id):
raise CourseDoesNotExist raise CourseDoesNotExist
# Attempt to load the user's generated certificate data # Attempt to load the user's generated certificate data
if preview_mode:
user_certificate = GeneratedCertificate.objects.get(
user=user,
course_id=course_key,
mode=preview_mode
)
else:
user_certificate = GeneratedCertificate.objects.get( user_certificate = GeneratedCertificate.objects.get(
user=user, user=user,
course_id=course_key course_id=course_key
...@@ -342,9 +351,12 @@ def render_html_view(request, user_id, course_id): ...@@ -342,9 +351,12 @@ def render_html_view(request, user_id, course_id):
# If there's no generated certificate data for this user, we need to see if we're in 'preview' mode... # If there's no generated certificate data for this user, we need to see if we're in 'preview' mode...
# If we are, we'll need to create a mock version of the user_certificate container for previewing # If we are, we'll need to create a mock version of the user_certificate container for previewing
except GeneratedCertificate.DoesNotExist: except GeneratedCertificate.DoesNotExist:
if request.GET.get('preview', None): if preview_mode and (
has_access(request.user, 'instructor', course)
or has_access(request.user, 'staff', course)
):
user_certificate = GeneratedCertificate( user_certificate = GeneratedCertificate(
mode=request.GET.get('preview'), mode=preview_mode,
verify_uuid=unicode(uuid4().hex), verify_uuid=unicode(uuid4().hex),
modified_date=datetime.now().date() modified_date=datetime.now().date()
) )
...@@ -383,7 +395,7 @@ def render_html_view(request, user_id, course_id): ...@@ -383,7 +395,7 @@ def render_html_view(request, user_id, course_id):
# Get the active certificate configuration for this course # Get the active certificate configuration for this course
# If we do not have an active certificate, we'll need to send the user to the "Invalid" screen # If we do not have an active certificate, we'll need to send the user to the "Invalid" screen
# Passing in the 'preview' parameter, if specified, will return a configuration, if defined # Passing in the 'preview' parameter, if specified, will return a configuration, if defined
active_configuration = get_active_web_certificate(course, request.GET.get('preview')) active_configuration = get_active_web_certificate(course, preview_mode)
if active_configuration is None: if active_configuration is None:
return render_to_response(invalid_template_path, context) return render_to_response(invalid_template_path, context)
else: else:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment