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.
This commit is contained in:
Feanil Patel
2021-03-25 13:13:05 -04:00
parent c2eabf6cca
commit 4f9fe59731

View File

@@ -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',