forked from mirrors/bookwyrm
Fixes domain block tests
This commit is contained in:
parent
34b790a086
commit
a4b892dfad
3 changed files with 21 additions and 4 deletions
|
@ -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")
|
||||
|
|
|
@ -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))
|
||||
|
|
|
@ -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()
|
||||
|
||||
|
|
Loading…
Reference in a new issue