diff options
author | 2018-08-16 19:29:36 +0200 | |
---|---|---|
committer | 2018-08-16 19:29:36 +0200 | |
commit | 70db6ba866c2cdaec047f180b0e092bdd7e7d0a6 (patch) | |
tree | 2415f0241f4f61c34249e3741292982dd75cc4b3 /api | |
parent | Remove broken `apk install` command. (diff) |
Add a base class for API test cases.
Diffstat (limited to 'api')
-rw-r--r-- | api/tests/base.py | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/api/tests/base.py b/api/tests/base.py new file mode 100644 index 00000000..01286ca0 --- /dev/null +++ b/api/tests/base.py @@ -0,0 +1,60 @@ +from django.contrib.auth.models import User +from rest_framework.authtoken.models import Token +from rest_framework.test import APIClient, APITestCase + + +test_user = User.objects.get(username='test') +if test_user is None: + test_user = User.objects.create_user('test', '[email protected]', 'testpass') + + +class APISubdomainTestCase(APITestCase): + """ + Configures the test client to use the proper subdomain + for requests and forces authentication for the test user. + + Using this performs the following niceties for you which ease writing tests: + - setting the `HTTP_HOST` request header to `api.pythondiscord.local:8000`, and + - forcing authentication for the test user. + If you don't want to force authentication (for example, to test a route's response + for an unauthenticated user), un-force authentication by using the following: + + >>> from api.test.base import APISubdomainTestCase + >>> class UnauthedUserTestCase(APISubdomainTestCase): + ... def setUp(self): + ... super().setUp(self) + ... self.client.force_authentication(user=None) + ... def test_can_read_objects_at_my_endpoint(self): + ... resp = self.client.get('/my-publicly-readable-endpoint') + ... self.assertEqual(resp.status_code, 200) + ... def test_cannot_delete_objects_at_my_endpoint(self): + ... resp = self.client.delete('/my-publicly-readable-endpoint/42') + ... self.assertEqual(resp.status_code, 401) + + Make sure to include the `super().setUp(self)` call, otherwise, you may get + status code 404 for some URLs due to the missing `HTTP_HOST` header. + + ## Example + Using this in a test case is rather straightforward: + + >>> from api.tests.base import APISubdomainTestCase + >>> class MyAPITestCase(APISubdomainTestCase): + ... def test_that_it_works(self): + ... response = self.client.get('/my-endpoint') + ... self.assertEqual(response.status_code, 200) + + To reverse URLs of the API host, you need to use `django_hosts`: + + >>> from django_hosts.resolvers import reverse + >>> from api.test.base import APISubdomainTestCase + >>> class MyReversedTestCase(APISubdomainTestCase): + ... def test_my_endpoint(self): + ... url = reverse('user-detail', host='api') + ... response = self.client.get(url) + ... self.assertEqual(response.status_code, 200) + """ + + def setUp(self): + super().setUp() + self.client.defaults['HTTP_HOST'] = 'api.pythondiscord.local:8000' + self.client.force_authenticate(test_user) |