forked from mirrors/bookwyrm
Adds tests for blocked server checks
This commit is contained in:
parent
194fcb8055
commit
34b790a086
2 changed files with 27 additions and 0 deletions
|
@ -6,6 +6,7 @@ from unittest.mock import patch
|
|||
|
||||
from django.http import HttpResponseNotAllowed, HttpResponseNotFound
|
||||
from django.test import TestCase, Client
|
||||
from django.test.client import RequestFactory
|
||||
import responses
|
||||
|
||||
from bookwyrm import models, views
|
||||
|
@ -18,6 +19,7 @@ class Inbox(TestCase):
|
|||
def setUp(self):
|
||||
""" basic user and book data """
|
||||
self.client = Client()
|
||||
self.factory = RequestFactory()
|
||||
self.local_user = models.User.objects.create_user(
|
||||
"mouse@example.com",
|
||||
"mouse@mouse.com",
|
||||
|
@ -936,3 +938,26 @@ class Inbox(TestCase):
|
|||
views.inbox.activity_task(activity)
|
||||
self.assertTrue(redis_mock.called)
|
||||
self.assertFalse(models.UserBlocks.objects.exists())
|
||||
|
||||
def test_is_blocked_user_agent(self):
|
||||
""" check for blocked servers """
|
||||
request = self.factory.post(
|
||||
"",
|
||||
HTTP_USER_AGENT="http.rb/4.4.1 (Mastodon/3.3.0; +https://mastodon.social/)",
|
||||
)
|
||||
self.assertFalse(views.inbox.is_blocked_user_agent(request))
|
||||
|
||||
models.FederatedServer.objects.create(
|
||||
server_name="mastodon.social", status="blocked"
|
||||
)
|
||||
self.assertTrue(views.inbox.is_blocked_user_agent(request))
|
||||
|
||||
def test_is_blocked_activity(self):
|
||||
""" check for blocked servers """
|
||||
activity = {"actor": "https://mastodon.social/user/whaatever/else"}
|
||||
self.assertFalse(views.inbox.is_blocked_user_agent(activity))
|
||||
|
||||
models.FederatedServer.objects.create(
|
||||
server_name="mastodon.social", status="blocked"
|
||||
)
|
||||
self.assertTrue(views.inbox.is_blocked_user_agent(activity))
|
||||
|
|
|
@ -68,6 +68,8 @@ def is_blocked_user_agent(request):
|
|||
""" check if a request is from a blocked server based on user agent """
|
||||
# check user agent
|
||||
user_agent = request.headers.get("User-Agent")
|
||||
if not user_agent:
|
||||
return False
|
||||
domain = re.match(regex.domain, user_agent)
|
||||
if not domain:
|
||||
# idk, we'll try again later with the actor
|
||||
|
|
Loading…
Reference in a new issue