Login

Lazy Man's Django test case

Author:
prestontimmons
Posted:
November 13, 2011
Language:
Python
Version:
1.3
Score:
0 (after 0 ratings)

Decrease the verbosity of writing view tests.

Also available here: https://gist.github.com/1310549.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
"""
Decrease the verbosity of writing view tests.

Old way:

    self.client.get(reverse("my-view"))
    self.client.post(reverse("my-view"), data={"key": "value"})
    self.client.login("username", "password")
    self.client.get(reverse("my-other-view"))
    self.client.logout()

    self.client.post(reverse("my-other-view"), data={"key": "value"}
    self.assertEqual(response.status_code, 302)
    self.assertEqual(response["Location"], "http://remoteserver/")

    session = self.client.session
    session["key"] = "value"
    session.save()

New way:

    self.client.get("my-view")
    self.client.post("my-view", data={"key": "value"})
    with self.login("username", "password"):
        self.client.get("my-other-view")

    self.assertRedirectsTo(response, "http://remoteserver/")

    session = self.session
    session["key"] = "value"
    session.save()

By default also patches the template loader and initiates the client session.
"""

from django.conf import settings
from django.core.urlresolvers import reverse
from django.template import Template
from django.test import TestCase
from django.utils.importlib import import_module
import django.template.loader


class login(object):
    def __init__(self, testcase, user, password):
        self.testcase = testcase
        success = testcase.client.login(username=user, password=password)
        self.testcase.assertTrue(
            success,
            "login with username=%r, password=%r failed" % (user, password)
        )

    def __enter__(self):
        pass

    def __exit__(self, *args):
        self.testcase.client.logout()


class LazyTestCase(TestCase):
    patch_templates = True

    def login(self, user, password):
        return login(self, user, password)

    def get(self, url_name, data=None, *args, **kwargs):
        data = data or {}
        return self.client.get(
            reverse(url_name, args=args, kwargs=kwargs), data
        )

    def post(self, url_name, data=None, *args, **kwargs):
        return self.client.post(
            reverse(url_name, args=args, kwargs=kwargs), data
        )

    @property
    def session(self):
        return self.client.session

    def load_data(self):
        return

    def setUp(self):
        if self.patch_templates:
            self.get_template = django.template.loader.get_template
            def get_template(*args, **kwargs):
                return Template("")
            django.template.loader.get_template = get_template

        if "django.contrib.sessions" in settings.INSTALLED_APPS:
            # Workaround for https://code.djangoproject.com/ticket/15740
            engine = import_module(settings.SESSION_ENGINE)
            store = engine.SessionStore()
            store.save()
            self.client.cookies[settings.SESSION_COOKIE_NAME] = store.session_key

        self.load_data()

    def tearDown(self):
        if self.patch_templates:
            django.template.loader.get_template = self.get_template

    def assertRedirectsTo(self, response, url):
        """
        Assert that a response redirects to a specific url without trying to
        load the other page.
        """
        self.assertEqual(response.status_code, 302)
        self.assertEqual(response["Location"], url)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

Please login first before commenting.