2023-07-10 14:36:52 +00:00
|
|
|
import unittest.mock
|
|
|
|
|
|
|
|
import django.contrib.sites.models
|
|
|
|
import rest_framework.response
|
2023-07-10 16:41:57 +00:00
|
|
|
import rest_framework.test
|
2023-07-10 14:36:52 +00:00
|
|
|
|
|
|
|
from printpub.user import models
|
|
|
|
|
|
|
|
|
|
|
|
class TestWebfingerGet:
|
|
|
|
def test_request_with_no_resource_gives_400(self):
|
|
|
|
client = rest_framework.test.APIClient()
|
|
|
|
res = client.get("/.well-known/webfinger")
|
|
|
|
assert res.status_code == 400 # type: ignore
|
|
|
|
|
|
|
|
def test_request_with_unknown_user_returns_404(self):
|
|
|
|
client = rest_framework.test.APIClient()
|
|
|
|
res = client.get("/.well-known/webfinger?resource=acct:wint@my.website")
|
|
|
|
assert res.status_code == 404 # type: ignore
|
|
|
|
|
|
|
|
def test_known_user_returns_serializer_data(self):
|
|
|
|
client = rest_framework.test.APIClient()
|
|
|
|
poster = models.Poster()
|
|
|
|
poster.save()
|
|
|
|
user = models.LocalUser(
|
|
|
|
display_name="dril", username="wint", password="hunter2", poster=poster
|
|
|
|
)
|
|
|
|
user.save()
|
|
|
|
|
|
|
|
fake_site = django.contrib.sites.models.Site(
|
|
|
|
domain="my.website", name="My Website"
|
|
|
|
)
|
|
|
|
with unittest.mock.patch(
|
|
|
|
"django.contrib.sites.models.SiteManager.get_current",
|
|
|
|
return_value=fake_site,
|
|
|
|
):
|
|
|
|
res = client.get("/.well-known/webfinger?resource=acct:wint@my.website")
|
2023-07-10 16:30:31 +00:00
|
|
|
assert res.status_code == 200
|
2023-07-10 14:36:52 +00:00
|
|
|
# We could test more properties of this, but we will leave that to the serializer test
|
2023-07-10 16:30:31 +00:00
|
|
|
assert res.data["subject"] == "acct:wint@my.website"
|
2023-07-10 14:36:52 +00:00
|
|
|
|
|
|
|
def test_wrong_domain_in_request_returns_404(self):
|
|
|
|
client = rest_framework.test.APIClient()
|
|
|
|
poster = models.Poster()
|
|
|
|
poster.save()
|
|
|
|
user = models.LocalUser(
|
|
|
|
display_name="dril", username="wint", password="hunter2", poster=poster
|
|
|
|
)
|
|
|
|
user.save()
|
|
|
|
|
|
|
|
fake_site = django.contrib.sites.models.Site(
|
|
|
|
domain="my.website", name="My Website"
|
|
|
|
)
|
|
|
|
with unittest.mock.patch(
|
|
|
|
"django.contrib.sites.models.SiteManager.get_current",
|
|
|
|
return_value=fake_site,
|
|
|
|
):
|
|
|
|
res = client.get("/.well-known/webfinger?resource=acct:wint@example.com")
|
2023-07-10 16:30:31 +00:00
|
|
|
assert res.status_code == 404
|