diff --git a/lms/static/js/spec/student_account/utils_spec.js b/lms/static/js/spec/student_account/utils_spec.js index 445ddae7f1..03ecbe6fb9 100644 --- a/lms/static/js/spec/student_account/utils_spec.js +++ b/lms/static/js/spec/student_account/utils_spec.js @@ -21,5 +21,19 @@ define(['jquery', 'js/student_account/utils'], expect(user).toEqual(userInfo); }); }); + + describe('userFromEdxUserCookie', function() { + var user; + + beforeEach(function() { + $.cookie('edx-user-info', null); + }); + + it('returns empty user information when cookie is absent', function() { + spyOn($, 'cookie').and.returnValue(null); + user = Utils.userFromEdxUserCookie(); + expect(user).toEqual({}); + }); + }); } ); diff --git a/lms/static/js/student_account/utils.js b/lms/static/js/student_account/utils.js index fb539b658c..bdb8ee8361 100644 --- a/lms/static/js/student_account/utils.js +++ b/lms/static/js/student_account/utils.js @@ -27,6 +27,10 @@ cookie = document.cookie.match('(^|;)\\s*' + edxUserCookie + '\\s*=\\s*([^;]+)'); userCookie = cookie ? cookie.pop() : $.cookie(edxUserCookie); + if (!userCookie) { + return {}; + } + // returns the user object from cookie. Replaces '054' with ',' and removes '\' user = userCookie.replace(/\\/g, '').replace(/054/g, ','); user = user.substring(1, user.length - 1);