Add fixture to mock domain

master
Nick Krichevsky 2023-07-10 21:39:30 -04:00
parent a9ce2c0511
commit c93780ab35
2 changed files with 29 additions and 31 deletions

View File

@ -1,6 +1,19 @@
import unittest.mock
import django.contrib.sites.models
import pytest import pytest
@pytest.fixture
def mock_domain():
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,
):
yield
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def enable_db_access_for_all_tests(db): def enable_db_access_for_all_tests(db):
pass pass

View File

@ -18,7 +18,20 @@ class TestWebfingerGet:
res = client.get("/.well-known/webfinger?resource=acct:wint@my.website") res = client.get("/.well-known/webfinger?resource=acct:wint@my.website")
assert res.status_code == 404 assert res.status_code == 404
def test_known_user_returns_serializer_data(self): def test_known_user_returns_serializer_data(self, mock_domain):
client = rest_framework.test.APIClient()
poster = models.Poster()
poster.save()
user = models.LocalUser(
display_name="dril", username="wint", password="hunter2", poster=poster
)
user.save()
res = client.get("/.well-known/webfinger?resource=acct:wint@my.website")
assert res.status_code == 200
# We could test more properties of this, but we will leave that to the serializer test
assert res.data["subject"] == "acct:wint@my.website"
def test_wrong_domain_in_request_returns_404(self, mock_domain):
client = rest_framework.test.APIClient() client = rest_framework.test.APIClient()
poster = models.Poster() poster = models.Poster()
poster.save() poster.save()
@ -27,33 +40,5 @@ class TestWebfingerGet:
) )
user.save() user.save()
fake_site = django.contrib.sites.models.Site( res = client.get("/.well-known/webfinger?resource=acct:wint@example.com")
domain="my.website", name="My Website" assert res.status_code == 404
)
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")
assert res.status_code == 200
# We could test more properties of this, but we will leave that to the serializer test
assert res.data["subject"] == "acct:wint@my.website"
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")
assert res.status_code == 404