From d7a7bcc1d7519f8802ce9a9f7bae3beec63608c0 Mon Sep 17 00:00:00 2001 From: Clinton Blackburn <clinton.blackburn@gmail.com> Date: Wed, 16 Nov 2016 23:51:05 -0500 Subject: [PATCH] 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 --- common/djangoapps/student/tests/test_views.py | 26 ++++++++++++++++++++++++++ common/djangoapps/student/views.py | 6 ++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/common/djangoapps/student/tests/test_views.py b/common/djangoapps/student/tests/test_views.py index e6b64f1..c22c61b 100644 --- a/common/djangoapps/student/tests/test_views.py +++ b/common/djangoapps/student/tests/test_views.py @@ -1,11 +1,13 @@ """ Test the student dashboard view. """ +import json import unittest import ddt from django.conf import settings from django.core.urlresolvers import reverse +from django.test import RequestFactory from django.test import TestCase from edx_oauth2_provider.constants import AUTHORIZED_CLIENTS_SESSION_KEY 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.factories import CourseFactory +from student.cookies import get_user_info_cookie_data from student.helpers import DISABLE_UNENROLL_CERT_STATES from student.models import CourseEnrollment, LogoutViewConfiguration from student.tests.factories import UserFactory, CourseEnrollmentFactory @@ -195,3 +198,26 @@ class LogoutTests(TestCase): 'target': '/', } 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) diff --git a/common/djangoapps/student/views.py b/common/djangoapps/student/views.py index 73f7443..021bca0 100644 --- a/common/djangoapps/student/views.py +++ b/common/djangoapps/student/views.py @@ -107,7 +107,7 @@ from student.helpers import ( DISABLE_UNENROLL_CERT_STATES, 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 shoppingcart.models import DonationConfiguration, CourseRegistrationCode @@ -785,7 +785,9 @@ def dashboard(request): '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 -- libgit2 0.26.0