From b6a943c39215bd43bcf3278fdf7a2a0f4c20700d Mon Sep 17 00:00:00 2001 From: Robert Raposa Date: Thu, 28 Oct 2021 10:05:48 -0400 Subject: [PATCH] fix: errors with anonymous user (#29042) There are several errors that appear in monitoring when calls are made with an anonymous user. This resolves one (or more). --- openedx/core/djangoapps/user_api/course_tag/api.py | 3 +++ .../core/djangoapps/user_api/course_tag/tests/test_api.py | 6 +++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/openedx/core/djangoapps/user_api/course_tag/api.py b/openedx/core/djangoapps/user_api/course_tag/api.py index 36c4ee4f87..a12c19905a 100644 --- a/openedx/core/djangoapps/user_api/course_tag/api.py +++ b/openedx/core/djangoapps/user_api/course_tag/api.py @@ -69,6 +69,9 @@ def get_course_tag(user, course_id, key): Returns: string value, or None if there is no value saved """ + if user.is_anonymous: + return None + if BulkCourseTags.is_prefetched(course_id): try: return BulkCourseTags.get_course_tag(user.id, course_id, key) diff --git a/openedx/core/djangoapps/user_api/course_tag/tests/test_api.py b/openedx/core/djangoapps/user_api/course_tag/tests/test_api.py index b8f279c919..ab336d6f3f 100644 --- a/openedx/core/djangoapps/user_api/course_tag/tests/test_api.py +++ b/openedx/core/djangoapps/user_api/course_tag/tests/test_api.py @@ -2,7 +2,7 @@ Test the user course tag API. """ - +from django.contrib.auth.models import AnonymousUser from django.test import TestCase from opaque_keys.edx.locator import CourseLocator @@ -21,6 +21,10 @@ class TestCourseTagAPI(TestCase): self.course_id = CourseLocator('test_org', 'test_course_number', 'test_run') self.test_key = 'test_key' + def test_get_course_tag_for_anonymous_user(self): + tag = course_tag_api.get_course_tag(AnonymousUser(), self.course_id, self.test_key) + assert tag is None + def test_get_set_course_tag(self): # get a tag that doesn't exist tag = course_tag_api.get_course_tag(self.user, self.course_id, self.test_key)