Setting user info cookie on learner dashboard

The user info is now updated every time a user loads the learner dashboard. Given that this is the page most learners land on after enrolling in a course, updating this cookie here will ensure that the enrollment status hash is relatively up-to-date.

ECOM-4895
This commit is contained in:
Clinton Blackburn
2016-11-16 23:51:05 -05:00
parent f0030334af
commit d7a7bcc1d7
2 changed files with 30 additions and 2 deletions

View File

@@ -1,11 +1,13 @@
""" """
Test the student dashboard view. Test the student dashboard view.
""" """
import json
import unittest import unittest
import ddt import ddt
from django.conf import settings from django.conf import settings
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.test import RequestFactory
from django.test import TestCase from django.test import TestCase
from edx_oauth2_provider.constants import AUTHORIZED_CLIENTS_SESSION_KEY from edx_oauth2_provider.constants import AUTHORIZED_CLIENTS_SESSION_KEY
from edx_oauth2_provider.tests.factories import ClientFactory, TrustedClientFactory from edx_oauth2_provider.tests.factories import ClientFactory, TrustedClientFactory
@@ -14,6 +16,7 @@ from pyquery import PyQuery as pq
from xmodule.modulestore.tests.django_utils import SharedModuleStoreTestCase from xmodule.modulestore.tests.django_utils import SharedModuleStoreTestCase
from xmodule.modulestore.tests.factories import CourseFactory from xmodule.modulestore.tests.factories import CourseFactory
from student.cookies import get_user_info_cookie_data
from student.helpers import DISABLE_UNENROLL_CERT_STATES from student.helpers import DISABLE_UNENROLL_CERT_STATES
from student.models import CourseEnrollment, LogoutViewConfiguration from student.models import CourseEnrollment, LogoutViewConfiguration
from student.tests.factories import UserFactory, CourseEnrollmentFactory from student.tests.factories import UserFactory, CourseEnrollmentFactory
@@ -195,3 +198,26 @@ class LogoutTests(TestCase):
'target': '/', 'target': '/',
} }
self.assertDictContainsSubset(expected, response.context_data) # pylint: disable=no-member self.assertDictContainsSubset(expected, response.context_data) # pylint: disable=no-member
@unittest.skipUnless(settings.ROOT_URLCONF == 'lms.urls', 'Test only valid in lms')
class StudentDashboardTests(TestCase):
""" Tests for the student dashboard. """
def setUp(self):
""" Create a course and user, then log in. """
super(StudentDashboardTests, self).setUp()
self.user = UserFactory()
self.client.login(username=self.user.username, password=PASSWORD)
self.path = reverse('dashboard')
def test_user_info_cookie(self):
""" Verify visiting the learner dashboard sets the user info cookie. """
self.assertNotIn(settings.EDXMKTG_USER_INFO_COOKIE_NAME, self.client.cookies)
request = RequestFactory().get(self.path)
request.user = self.user
expected = json.dumps(get_user_info_cookie_data(request))
self.client.get(self.path)
actual = self.client.cookies[settings.EDXMKTG_USER_INFO_COOKIE_NAME].value
self.assertEqual(actual, expected)

View File

@@ -107,7 +107,7 @@ from student.helpers import (
DISABLE_UNENROLL_CERT_STATES, DISABLE_UNENROLL_CERT_STATES,
destroy_oauth_tokens destroy_oauth_tokens
) )
from student.cookies import set_logged_in_cookies, delete_logged_in_cookies from student.cookies import set_logged_in_cookies, delete_logged_in_cookies, set_user_info_cookie
from student.models import anonymous_id_for_user, UserAttribute, EnrollStatusChange from student.models import anonymous_id_for_user, UserAttribute, EnrollStatusChange
from shoppingcart.models import DonationConfiguration, CourseRegistrationCode from shoppingcart.models import DonationConfiguration, CourseRegistrationCode
@@ -785,7 +785,9 @@ def dashboard(request):
'ecommerce_payment_page': ecommerce_service.payment_page_url(), 'ecommerce_payment_page': ecommerce_service.payment_page_url(),
}) })
return render_to_response('dashboard.html', context) response = render_to_response('dashboard.html', context)
set_user_info_cookie(response, request)
return response
def _create_recent_enrollment_message(course_enrollments, course_modes): # pylint: disable=invalid-name def _create_recent_enrollment_message(course_enrollments, course_modes): # pylint: disable=invalid-name