Skip to content

Commit 3c2fcb8

Browse files
authored
Merge pull request #526 from python-discord/subdomains-to-query-paths
2 parents 3e0c866 + ce75197 commit 3c2fcb8

32 files changed

+339
-384
lines changed

poetry.lock

Lines changed: 24 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pydis_site/apps/admin/urls.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from django.urls import path
33

44

5+
app_name = 'admin'
56
urlpatterns = (
67
path('', admin.site.urls),
78
)

pydis_site/apps/api/models/bot/message_deletion_context.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from django.db import models
2-
from django_hosts.resolvers import reverse
2+
from django.urls import reverse
33

44
from pydis_site.apps.api.models.bot.user import User
55
from pydis_site.apps.api.models.mixins import ModelReprMixin
@@ -33,7 +33,7 @@ class MessageDeletionContext(ModelReprMixin, models.Model):
3333
@property
3434
def log_url(self) -> str:
3535
"""Create the url for the deleted message logs."""
36-
return reverse('logs', host="staff", args=(self.id,))
36+
return reverse('staff:logs', args=(self.id,))
3737

3838
class Meta:
3939
"""Set the ordering for list views to newest first."""

pydis_site/apps/api/tests/base.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
)
1212

1313

14-
class APISubdomainTestCase(APITestCase):
14+
class AuthenticatedAPITestCase(APITestCase):
1515
"""
1616
Configures the test client.
1717
@@ -24,14 +24,13 @@ class APISubdomainTestCase(APITestCase):
2424
`self.client.force_authenticate(user=created_user)` to force authentication
2525
through the created user.
2626
27-
Using this performs the following niceties for you which ease writing tests:
28-
- setting the `HTTP_HOST` request header to `api.pythondiscord.local:8000`, and
27+
Using this performs the following nicety for you which eases writing tests:
2928
- forcing authentication for the test user.
3029
If you don't want to force authentication (for example, to test a route's response
3130
for an unauthenticated user), un-force authentication by using the following:
3231
33-
>>> from pydis_site.apps.api.tests.base import APISubdomainTestCase
34-
>>> class UnauthedUserTestCase(APISubdomainTestCase):
32+
>>> from pydis_site.apps.api.tests.base import AuthenticatedAPITestCase
33+
>>> class UnauthedUserTestCase(AuthenticatedAPITestCase):
3534
... def setUp(self):
3635
... super().setUp()
3736
... self.client.force_authentication(user=None)
@@ -42,30 +41,26 @@ class APISubdomainTestCase(APITestCase):
4241
... resp = self.client.delete('/my-publicly-readable-endpoint/42')
4342
... self.assertEqual(resp.status_code, 401)
4443
45-
Make sure to include the `super().setUp(self)` call, otherwise, you may get
46-
status code 404 for some URLs due to the missing `HTTP_HOST` header.
47-
4844
## Example
4945
Using this in a test case is rather straightforward:
5046
51-
>>> from pydis_site.apps.api.tests.base import APISubdomainTestCase
52-
>>> class MyAPITestCase(APISubdomainTestCase):
47+
>>> from pydis_site.apps.api.tests.base import AuthenticatedAPITestCase
48+
>>> class MyAPITestCase(AuthenticatedAPITestCase):
5349
... def test_that_it_works(self):
5450
... response = self.client.get('/my-endpoint')
5551
... self.assertEqual(response.status_code, 200)
5652
57-
To reverse URLs of the API host, you need to use `django_hosts`:
53+
To reverse URLs of the API host, you need to use `django.urls`:
5854
59-
>>> from django_hosts.resolvers import reverse
60-
>>> from pydis_site.apps.api.tests.base import APISubdomainTestCase
61-
>>> class MyReversedTestCase(APISubdomainTestCase):
55+
>>> from django.urls import reverse
56+
>>> from pydis_site.apps.api.tests.base import AuthenticatedAPITestCase
57+
>>> class MyReversedTestCase(AuthenticatedAPITestCase):
6258
... def test_my_endpoint(self):
63-
... url = reverse('user-detail', host='api')
59+
... url = reverse('api:user-detail')
6460
... response = self.client.get(url)
6561
... self.assertEqual(response.status_code, 200)
6662
"""
6763

6864
def setUp(self):
6965
super().setUp()
70-
self.client.defaults['HTTP_HOST'] = 'api.pythondiscord.local:8000'
7166
self.client.force_authenticate(test_user)

pydis_site/apps/api/tests/test_deleted_messages.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
from datetime import datetime
22

3+
from django.urls import reverse
34
from django.utils import timezone
4-
from django_hosts.resolvers import reverse
55

6-
from .base import APISubdomainTestCase
6+
from .base import AuthenticatedAPITestCase
77
from ..models import MessageDeletionContext, User
88

99

10-
class DeletedMessagesWithoutActorTests(APISubdomainTestCase):
10+
class DeletedMessagesWithoutActorTests(AuthenticatedAPITestCase):
1111
@classmethod
1212
def setUpTestData(cls):
1313
cls.author = User.objects.create(
@@ -40,14 +40,14 @@ def setUpTestData(cls):
4040
}
4141

4242
def test_accepts_valid_data(self):
43-
url = reverse('bot:messagedeletioncontext-list', host='api')
43+
url = reverse('api:bot:messagedeletioncontext-list')
4444
response = self.client.post(url, data=self.data)
4545
self.assertEqual(response.status_code, 201)
4646
[context] = MessageDeletionContext.objects.all()
4747
self.assertIsNone(context.actor)
4848

4949

50-
class DeletedMessagesWithActorTests(APISubdomainTestCase):
50+
class DeletedMessagesWithActorTests(AuthenticatedAPITestCase):
5151
@classmethod
5252
def setUpTestData(cls):
5353
cls.author = cls.actor = User.objects.create(
@@ -72,14 +72,14 @@ def setUpTestData(cls):
7272
}
7373

7474
def test_accepts_valid_data_and_sets_actor(self):
75-
url = reverse('bot:messagedeletioncontext-list', host='api')
75+
url = reverse('api:bot:messagedeletioncontext-list')
7676
response = self.client.post(url, data=self.data)
7777
self.assertEqual(response.status_code, 201)
7878
[context] = MessageDeletionContext.objects.all()
7979
self.assertEqual(context.actor.id, self.actor.id)
8080

8181

82-
class DeletedMessagesLogURLTests(APISubdomainTestCase):
82+
class DeletedMessagesLogURLTests(AuthenticatedAPITestCase):
8383
@classmethod
8484
def setUpTestData(cls):
8585
cls.author = cls.actor = User.objects.create(
@@ -94,6 +94,6 @@ def setUpTestData(cls):
9494
)
9595

9696
def test_valid_log_url(self):
97-
expected_url = reverse('logs', host="staff", args=(1,))
97+
expected_url = reverse('staff:logs', args=(1,))
9898
[context] = MessageDeletionContext.objects.all()
9999
self.assertEqual(context.log_url, expected_url)

0 commit comments

Comments
 (0)