Adds more webfinger tests for helper functions

This commit is contained in:
Mouse Reeve 2022-07-06 10:05:22 -07:00
parent c48fbd8753
commit f4dd14acad
2 changed files with 59 additions and 10 deletions

View file

@ -112,7 +112,17 @@ class ViewsHelpers(TestCase):
request = self.factory.get("", {"q": "Test Book"}, HTTP_USER_AGENT=USER_AGENT) request = self.factory.get("", {"q": "Test Book"}, HTTP_USER_AGENT=USER_AGENT)
self.assertTrue(views.helpers.is_bookwyrm_request(request)) self.assertTrue(views.helpers.is_bookwyrm_request(request))
def test_existing_user(self, *_): def test_handle_remote_webfinger_invalid(self, *_):
"""Various ways you can send a bad query"""
# if there's no query, there's no result
result = views.helpers.handle_remote_webfinger(None)
self.assertIsNone(result)
# malformed user
result = views.helpers.handle_remote_webfinger("noatsymbol")
self.assertIsNone(result)
def test_handle_remote_webfinger_existing_user(self, *_):
"""simple database lookup by username""" """simple database lookup by username"""
result = views.helpers.handle_remote_webfinger("@mouse@local.com") result = views.helpers.handle_remote_webfinger("@mouse@local.com")
self.assertEqual(result, self.local_user) self.assertEqual(result, self.local_user)
@ -124,7 +134,19 @@ class ViewsHelpers(TestCase):
self.assertEqual(result, self.local_user) self.assertEqual(result, self.local_user)
@responses.activate @responses.activate
def test_load_user(self, *_): def test_handle_remote_webfinger_load_user_invalid_result(self, *_):
"""find a remote user using webfinger, but fail"""
username = "mouse@example.com"
responses.add(
responses.GET,
f"https://example.com/.well-known/webfinger?resource=acct:{username}",
status=500,
)
result = views.helpers.handle_remote_webfinger("@mouse@example.com")
self.assertIsNone(result)
@responses.activate
def test_handle_remote_webfinger_load_user(self, *_):
"""find a remote user using webfinger""" """find a remote user using webfinger"""
username = "mouse@example.com" username = "mouse@example.com"
wellknown = { wellknown = {
@ -154,7 +176,7 @@ class ViewsHelpers(TestCase):
self.assertIsInstance(result, models.User) self.assertIsInstance(result, models.User)
self.assertEqual(result.username, "mouse@example.com") self.assertEqual(result.username, "mouse@example.com")
def test_user_on_blocked_server(self, *_): def test_handler_remote_webfinger_user_on_blocked_server(self, *_):
"""find a remote user using webfinger""" """find a remote user using webfinger"""
models.FederatedServer.objects.create( models.FederatedServer.objects.create(
server_name="example.com", status="blocked" server_name="example.com", status="blocked"
@ -163,6 +185,38 @@ class ViewsHelpers(TestCase):
result = views.helpers.handle_remote_webfinger("@mouse@example.com") result = views.helpers.handle_remote_webfinger("@mouse@example.com")
self.assertIsNone(result) self.assertIsNone(result)
@responses.activate
def test_subscribe_remote_webfinger(self, *_):
"""remote subscribe templates"""
query = "mouse@example.com"
response = {
"subject": f"acct:{query}",
"links": [
{
"rel": "self",
"type": "application/activity+json",
"href": "https://example.com/user/mouse",
"template": "hi",
},
{
"rel": "http://ostatus.org/schema/1.0/subscribe",
"type": "application/activity+json",
"href": "https://example.com/user/mouse",
"template": "hello",
},
],
}
responses.add(
responses.GET,
f"https://example.com/.well-known/webfinger?resource=acct:{query}",
json=response,
status=200,
)
template = views.helpers.subscribe_remote_webfinger(query)
self.assertEqual(template, "hello")
template = views.helpers.subscribe_remote_webfinger(f"@{query}")
self.assertEqual(template, "hello")
def test_handle_reading_status_to_read(self, *_): def test_handle_reading_status_to_read(self, *_):
"""posts shelve activities""" """posts shelve activities"""
shelf = self.local_user.shelf_set.get(identifier="to-read") shelf = self.local_user.shelf_set.get(identifier="to-read")

View file

@ -53,9 +53,7 @@ class ReportViews(TestCase):
request = self.factory.get("") request = self.factory.get("")
request.user = self.local_user request.user = self.local_user
result = views.Report.as_view()( result = views.Report.as_view()(
request, request, user_id=self.local_user.id, status_id=self.status.id
user_id=self.local_user.id,
status_id=self.status.id
) )
validate_html(result.render()) validate_html(result.render())
@ -68,10 +66,7 @@ class ReportViews(TestCase):
) )
request = self.factory.get("") request = self.factory.get("")
request.user = self.local_user request.user = self.local_user
result = views.Report.as_view()( result = views.Report.as_view()(request, link_id=link.id)
request,
link_id=link.id
)
validate_html(result.render()) validate_html(result.render())