Use headers dict instead of HTTP_* kwargs or request.META

This commit is contained in:
Bart Schuurmans 2024-04-01 15:28:01 +02:00
parent 224fae7a87
commit 624115bf11
5 changed files with 47 additions and 19 deletions

View file

@ -72,12 +72,12 @@ class Signature(TestCase):
urlsplit(self.rat.inbox).path,
data=data,
content_type="application/json",
**{
"HTTP_DATE": now,
"HTTP_SIGNATURE": signature,
"HTTP_DIGEST": digest,
"HTTP_CONTENT_TYPE": "application/activity+json; charset=utf-8",
"HTTP_HOST": NETLOC,
headers={
"date": now,
"signature": signature,
"digest": digest,
"content-type": "application/activity+json; charset=utf-8",
"host": NETLOC,
},
)

View file

@ -134,7 +134,10 @@ class Inbox(TestCase):
"""check for blocked servers"""
request = self.factory.post(
"",
HTTP_USER_AGENT="http.rb/4.4.1 (Mastodon/3.3.0; +https://mastodon.social/)",
headers={
# pylint: disable-next=line-too-long
"user-agent": "http.rb/4.4.1 (Mastodon/3.3.0; +https://mastodon.social/)",
},
)
self.assertIsNone(views.inbox.raise_is_blocked_user_agent(request))

View file

@ -113,11 +113,20 @@ class ViewsHelpers(TestCase): # pylint: disable=too-many-public-methods
request = self.factory.get(
"",
{"q": "Test Book"},
HTTP_USER_AGENT="http.rb/4.4.1 (Mastodon/3.3.0; +https://mastodon.social/)",
headers={
# pylint: disable-next=line-too-long
"user-agent": "http.rb/4.4.1 (Mastodon/3.3.0; +https://mastodon.social/)",
},
)
self.assertFalse(views.helpers.is_bookwyrm_request(request))
request = self.factory.get("", {"q": "Test Book"}, HTTP_USER_AGENT=USER_AGENT)
request = self.factory.get(
"",
{"q": "Test Book"},
headers={
"user-agent": USER_AGENT,
},
)
self.assertTrue(views.helpers.is_bookwyrm_request(request))
def test_handle_remote_webfinger_invalid(self, *_):
@ -271,8 +280,12 @@ class ViewsHelpers(TestCase): # pylint: disable=too-many-public-methods
def test_redirect_to_referer_outside_domain(self, *_):
"""safely send people on their way"""
request = self.factory.get("/path")
request.META = {"HTTP_REFERER": "http://outside.domain/name"}
request = self.factory.get(
"/path",
headers={
"referer": "http://outside.domain/name",
},
)
result = views.helpers.redirect_to_referer(
request, "user-feed", self.local_user.localname
)
@ -280,21 +293,33 @@ class ViewsHelpers(TestCase): # pylint: disable=too-many-public-methods
def test_redirect_to_referer_outside_domain_with_fallback(self, *_):
"""invalid domain with regular params for the redirect function"""
request = self.factory.get("/path")
request.META = {"HTTP_REFERER": "https://outside.domain/name"}
request = self.factory.get(
"/path",
headers={
"referer": "http://outside.domain/name",
},
)
result = views.helpers.redirect_to_referer(request)
self.assertEqual(result.url, "/")
def test_redirect_to_referer_valid_domain(self, *_):
"""redirect to within the app"""
request = self.factory.get("/path")
request.META = {"HTTP_REFERER": f"{BASE_URL}/and/a/path"}
request = self.factory.get(
"/path",
headers={
"referer": f"{BASE_URL}/and/a/path",
},
)
result = views.helpers.redirect_to_referer(request)
self.assertEqual(result.url, f"{BASE_URL}/and/a/path")
def test_redirect_to_referer_with_get_args(self, *_):
"""if the path has get params (like sort) they are preserved"""
request = self.factory.get("/path")
request.META = {"HTTP_REFERER": f"{BASE_URL}/and/a/path?sort=hello"}
request = self.factory.get(
"/path",
headers={
"referer": f"{BASE_URL}/and/a/path?sort=hello",
},
)
result = views.helpers.redirect_to_referer(request)
self.assertEqual(result.url, f"{BASE_URL}/and/a/path?sort=hello")

View file

@ -122,7 +122,7 @@ class OutboxView(TestCase):
privacy="public",
)
request = self.factory.get("", {"page": 1}, HTTP_USER_AGENT=USER_AGENT)
request = self.factory.get("", {"page": 1}, headers={"user-agent": USER_AGENT})
result = views.Outbox.as_view()(request, "mouse")
data = json.loads(result.content)

View file

@ -231,7 +231,7 @@ def maybe_redirect_local_path(request, model):
def redirect_to_referer(request, *args, **kwargs):
"""Redirect to the referrer, if it's in our domain, with get params"""
# make sure the refer is part of this instance
validated = validate_url_domain(request.META.get("HTTP_REFERER"))
validated = validate_url_domain(request.headers.get("referer", ""))
if validated:
return redirect(validated)