From 922498114a60016a29bdf457e4725968195fe196 Mon Sep 17 00:00:00 2001 From: Johannes Christ Date: Thu, 16 Aug 2018 22:43:34 +0200 Subject: Make test user superuser. --- api/tests/base.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'api') diff --git a/api/tests/base.py b/api/tests/base.py index aa8157bc..06f7233c 100644 --- a/api/tests/base.py +++ b/api/tests/base.py @@ -3,9 +3,13 @@ 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', 'test@example.com', 'testpass') +test_user, _created = User.objects.get_or_create( + username='test', + email='test@example.com', + password='testpass', + is_superuser=True, + is_staff=True +) class APISubdomainTestCase(APITestCase): @@ -13,6 +17,12 @@ class APISubdomainTestCase(APITestCase): Configures the test client to use the proper subdomain for requests and forces authentication for the test user. + The test user is considered staff and superuser. + If you want to test for a custom user (for example, to test model permissions), + create the user, assign the relevant permissions, and use + `self.client.force_authenticate(user=created_user)` to force authentication + through the created 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. -- cgit v1.2.3