Skip to content

Commit c998d47

Browse files
committed
Move subdomains to query paths.
In more detail: - Use Django URL namespaces (e.g. `api:bot:infractions`) instead of `django_hosts` host argument. - Update the hosts file setup documentation to remove subdomain entries. - Update the hosts file setup documentation to mention that the entry of `pythondiscord.local` is not required and mainly for convenience. - Rename the `APISubdomainTestCase` to the more fitting `AuthenticatedAPITestCase`, as authentication is all that is left that the class is doing. - Drop dependency to `django_hosts`.
1 parent 512bd17 commit c998d47

32 files changed

+311
-367
lines changed

poetry.lock

Lines changed: 1 addition & 13 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)
Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,61 @@
1-
from django_hosts.resolvers import reverse
1+
from django.urls import reverse
22

3-
from .base import APISubdomainTestCase
3+
from .base import AuthenticatedAPITestCase
44
from ..models import DocumentationLink
55

66

7-
class UnauthedDocumentationLinkAPITests(APISubdomainTestCase):
7+
class UnauthedDocumentationLinkAPITests(AuthenticatedAPITestCase):
88
def setUp(self):
99
super().setUp()
1010
self.client.force_authenticate(user=None)
1111

1212
def test_detail_lookup_returns_401(self):
13-
url = reverse('bot:documentationlink-detail', args=('whatever',), host='api')
13+
url = reverse('api:bot:documentationlink-detail', args=('whatever',))
1414
response = self.client.get(url)
1515

1616
self.assertEqual(response.status_code, 401)
1717

1818
def test_list_returns_401(self):
19-
url = reverse('bot:documentationlink-list', host='api')
19+
url = reverse('api:bot:documentationlink-list')
2020
response = self.client.get(url)
2121

2222
self.assertEqual(response.status_code, 401)
2323

2424
def test_create_returns_401(self):
25-
url = reverse('bot:documentationlink-list', host='api')
25+
url = reverse('api:bot:documentationlink-list')
2626
response = self.client.post(url, data={'hi': 'there'})
2727

2828
self.assertEqual(response.status_code, 401)
2929

3030
def test_delete_returns_401(self):
31-
url = reverse('bot:documentationlink-detail', args=('whatever',), host='api')
31+
url = reverse('api:bot:documentationlink-detail', args=('whatever',))
3232
response = self.client.delete(url)
3333

3434
self.assertEqual(response.status_code, 401)
3535

3636

37-
class EmptyDatabaseDocumentationLinkAPITests(APISubdomainTestCase):
37+
class EmptyDatabaseDocumentationLinkAPITests(AuthenticatedAPITestCase):
3838
def test_detail_lookup_returns_404(self):
39-
url = reverse('bot:documentationlink-detail', args=('whatever',), host='api')
39+
url = reverse('api:bot:documentationlink-detail', args=('whatever',))
4040
response = self.client.get(url)
4141

4242
self.assertEqual(response.status_code, 404)
4343

4444
def test_list_all_returns_empty_list(self):
45-
url = reverse('bot:documentationlink-list', host='api')
45+
url = reverse('api:bot:documentationlink-list')
4646
response = self.client.get(url)
4747

4848
self.assertEqual(response.status_code, 200)
4949
self.assertEqual(response.json(), [])
5050

5151
def test_delete_returns_404(self):
52-
url = reverse('bot:documentationlink-detail', args=('whatever',), host='api')
52+
url = reverse('api:bot:documentationlink-detail', args=('whatever',))
5353
response = self.client.delete(url)
5454

5555
self.assertEqual(response.status_code, 404)
5656

5757

58-
class DetailLookupDocumentationLinkAPITests(APISubdomainTestCase):
58+
class DetailLookupDocumentationLinkAPITests(AuthenticatedAPITestCase):
5959
@classmethod
6060
def setUpTestData(cls):
6161
cls.doc_link = DocumentationLink.objects.create(
@@ -71,27 +71,27 @@ def setUpTestData(cls):
7171
}
7272

7373
def test_detail_lookup_unknown_package_returns_404(self):
74-
url = reverse('bot:documentationlink-detail', args=('whatever',), host='api')
74+
url = reverse('api:bot:documentationlink-detail', args=('whatever',))
7575
response = self.client.get(url)
7676

7777
self.assertEqual(response.status_code, 404)
7878

7979
def test_detail_lookup_created_package_returns_package(self):
80-
url = reverse('bot:documentationlink-detail', args=(self.doc_link.package,), host='api')
80+
url = reverse('api:bot:documentationlink-detail', args=(self.doc_link.package,))
8181
response = self.client.get(url)
8282

8383
self.assertEqual(response.status_code, 200)
8484
self.assertEqual(response.json(), self.doc_json)
8585

8686
def test_list_all_packages_shows_created_package(self):
87-
url = reverse('bot:documentationlink-list', host='api')
87+
url = reverse('api:bot:documentationlink-list')
8888
response = self.client.get(url)
8989

9090
self.assertEqual(response.status_code, 200)
9191
self.assertEqual(response.json(), [self.doc_json])
9292

9393
def test_create_invalid_body_returns_400(self):
94-
url = reverse('bot:documentationlink-list', host='api')
94+
url = reverse('api:bot:documentationlink-list')
9595
response = self.client.post(url, data={'i': 'am', 'totally': 'valid'})
9696

9797
self.assertEqual(response.status_code, 400)
@@ -103,7 +103,7 @@ def test_create_invalid_url_returns_400(self):
103103
'inventory_url': 'totally an url'
104104
}
105105

106-
url = reverse('bot:documentationlink-list', host='api')
106+
url = reverse('api:bot:documentationlink-list')
107107
response = self.client.post(url, data=body)
108108

109109
self.assertEqual(response.status_code, 400)
@@ -114,13 +114,13 @@ def test_create_invalid_package_name_returns_400(self):
114114
with self.subTest(package_name=case):
115115
body = self.doc_json.copy()
116116
body['package'] = case
117-
url = reverse('bot:documentationlink-list', host='api')
117+
url = reverse('api:bot:documentationlink-list')
118118
response = self.client.post(url, data=body)
119119

120120
self.assertEqual(response.status_code, 400)
121121

122122

123-
class DocumentationLinkCreationTests(APISubdomainTestCase):
123+
class DocumentationLinkCreationTests(AuthenticatedAPITestCase):
124124
def setUp(self):
125125
super().setUp()
126126

@@ -130,27 +130,27 @@ def setUp(self):
130130
'inventory_url': 'https://docs.example.com'
131131
}
132132

133-
url = reverse('bot:documentationlink-list', host='api')
133+
url = reverse('api:bot:documentationlink-list')
134134
response = self.client.post(url, data=self.body)
135135

136136
self.assertEqual(response.status_code, 201)
137137

138138
def test_package_in_full_list(self):
139-
url = reverse('bot:documentationlink-list', host='api')
139+
url = reverse('api:bot:documentationlink-list')
140140
response = self.client.get(url)
141141

142142
self.assertEqual(response.status_code, 200)
143143
self.assertEqual(response.json(), [self.body])
144144

145145
def test_detail_lookup_works_with_package(self):
146-
url = reverse('bot:documentationlink-detail', args=(self.body['package'],), host='api')
146+
url = reverse('api:bot:documentationlink-detail', args=(self.body['package'],))
147147
response = self.client.get(url)
148148

149149
self.assertEqual(response.status_code, 200)
150150
self.assertEqual(response.json(), self.body)
151151

152152

153-
class DocumentationLinkDeletionTests(APISubdomainTestCase):
153+
class DocumentationLinkDeletionTests(AuthenticatedAPITestCase):
154154
@classmethod
155155
def setUpTestData(cls):
156156
cls.doc_link = DocumentationLink.objects.create(
@@ -160,13 +160,13 @@ def setUpTestData(cls):
160160
)
161161

162162
def test_unknown_package_returns_404(self):
163-
url = reverse('bot:documentationlink-detail', args=('whatever',), host='api')
163+
url = reverse('api:bot:documentationlink-detail', args=('whatever',))
164164
response = self.client.delete(url)
165165

166166
self.assertEqual(response.status_code, 404)
167167

168168
def test_delete_known_package_returns_204(self):
169-
url = reverse('bot:documentationlink-detail', args=(self.doc_link.package,), host='api')
169+
url = reverse('api:bot:documentationlink-detail', args=(self.doc_link.package,))
170170
response = self.client.delete(url)
171171

172172
self.assertEqual(response.status_code, 204)

0 commit comments

Comments
 (0)