Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override Settings for django test does not seem to work

Tags:

python

django

I have to override settings in a test in Django

@override_settings(XYZ_REDIRECT="http://localhost:8000")
@override_settings(TOKEN_TIMEOUT=0)
class CustomTestCase(TestCase):

    def setUp(self):
        self.token = self._generate_auth_token()
        self.client = Client()

    def test_token_expiry(self):
        feoken_count = 0
        client = Client()
        client.post('/api/v1/auth/login/', {'token': 'AF'})
        # Over HERE TOKEN_TIMEOUT is not changed
        self.assertEqual(ABCN.objects.count(), feoken_count)

The override settings decorator, however seems not to work. In the other side of the route, I have this code.

from fetchcore_server.settings import AUTH0_SERVER_URL, TOKEN_TIMEOUT
....
def post(self, request, *args, **kwargs):
    if 'token' in request.data:
        FetchcoreToken.objects.filter(expiry_time__lte=timezone.now()).delete()
        print TOKEN_TIMEOUT # this is still original value
        token = request.data['token']
        try:
            fetchcore_token = FetchcoreToken.objects.get(token=token)
            user = fetchcore_token.user
            user_id = user.id

I tried using the with self.settings(TOKEN_TIMEOUT=0) but even that did not work.

I am not sure how I'm using this wrong

Django docs on the subject: https://docs.djangoproject.com/en/1.11/topics/testing/tools/

In case it is relevant, this is how I run the test

python manage.py test api.tests.integration.v1.users.AuthUserTestCase
like image 662
cjds Avatar asked Jan 03 '23 12:01

cjds


1 Answers

You problem is that you are using import of settings directly,

from fetchcore_server.settings import AUTH0_SERVER_URL, TOKEN_TIMEOUT

but you should use settings object provided by django https://docs.djangoproject.com/en/1.11/topics/settings/#using-settings-in-python-code

from django.conf import settings
....
def post(self, request, *args, **kwargs):
    if 'token' in request.data:
        FetchcoreToken.objects.filter(expiry_time__lte=timezone.now()).delete()
        print settings.TOKEN_TIMEOUT # this is still original value
        token = request.data['token']
        try:
            fetchcore_token = FetchcoreToken.objects.get(token=token)
            user = fetchcore_token.user
            user_id = user.id

Also as a sidenote, you could provide all overloaded settings at once

@override_settings(XYZ_REDIRECT="http://localhost:8000", TOKEN_TIMEOUT=0)
class CustomTestCase(TestCase):
like image 75
Sardorbek Imomaliev Avatar answered Jan 06 '23 01:01

Sardorbek Imomaliev