Fixes domain block tests

This commit is contained in:
Mouse Reeve 2021-04-05 16:47:48 -07:00
parent 34b790a086
commit a4b892dfad
3 changed files with 21 additions and 4 deletions

View file

@ -44,3 +44,19 @@ class FederationViews(TestCase):
self.assertIsInstance(result, TemplateResponse)
result.render()
self.assertEqual(result.status_code, 200)
def test_server_page_post(self):
""" block and unblock a server """
server = models.FederatedServer.objects.create(server_name="hi.there.com")
self.assertEqual(server.status, "federated")
view = views.FederatedServer.as_view()
request = self.factory.post("")
request.user = self.local_user
request.user.is_superuser = True
view(request, server.id)
self.assertEqual(server.status, "blocked")
view(request, server.id)
self.assertEqual(server.status, "federated")

View file

@ -955,9 +955,9 @@ class Inbox(TestCase):
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))
self.assertFalse(views.inbox.is_blocked_activity(activity))
models.FederatedServer.objects.create(
server_name="mastodon.social", status="blocked"
)
self.assertTrue(views.inbox.is_blocked_user_agent(activity))
self.assertTrue(views.inbox.is_blocked_activity(activity))

View file

@ -70,7 +70,8 @@ def is_blocked_user_agent(request):
user_agent = request.headers.get("User-Agent")
if not user_agent:
return False
domain = re.match(regex.domain, user_agent)
url = re.search(r"+https?://{:s}/?".format(regex.domain), user_agent)
domain = urlparse(url).netloc
if not domain:
# idk, we'll try again later with the actor
return False
@ -89,7 +90,7 @@ def is_blocked_activity(activity_json):
def is_blocked(domain):
""" is this domain blocked? """
return models.FederatedServer.object.filter(
return models.FederatedServer.objects.filter(
server_name=domain, status="blocked"
).exists()