From 4f9fe5973131a7c3f0e390a60229d93d98804690 Mon Sep 17 00:00:00 2001
From: Feanil Patel <feanil@edx.org>
Date: Thu, 25 Mar 2021 13:13:05 -0400
Subject: [PATCH] test: Fix date tests that rely on user preference.

The function under test by the StrftimeLocalizedHtmlTest checks the
timezone in the user preferences of the user making the request.  If we
don't explicitly set a request here, it will simply use the last Request
that `crum` cached in the thread local cache.  This broke now
that we sometimes set the crum request.user to an invalid user in other
tests.

This change ensures that we have a valid request for these tests as a
part of the setup of this test class.
---
 common/djangoapps/util/tests/test_date_utils.py | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/common/djangoapps/util/tests/test_date_utils.py b/common/djangoapps/util/tests/test_date_utils.py
index 2e64ae622d4..2e7c581a700 100644
--- a/common/djangoapps/util/tests/test_date_utils.py
+++ b/common/djangoapps/util/tests/test_date_utils.py
@@ -6,11 +6,14 @@ import unittest
 from datetime import datetime, timedelta, tzinfo
 from unittest.mock import patch
 
+import crum
 import ddt
 import pytest
 from markupsafe import Markup
 from pytz import utc
 
+from django.test.client import RequestFactory
+
 from common.djangoapps.util.date_utils import (
     almost_same_datetime, get_default_time_display, get_time_display, strftime_localized, strftime_localized_html
 )
@@ -217,6 +220,12 @@ class StrftimeLocalizedHtmlTest(unittest.TestCase):
     """
     Tests for strftime_localized_html.
     """
+    def setUp(self):
+        super().setUp()
+        request = RequestFactory().request()
+        self.addCleanup(crum.set_current_request, None)
+        crum.set_current_request(request)
+
     @ddt.data(
         None,
         'Africa/Casablanca',
-- 
GitLab