Empty context within template tag render() method after using django.test.client.Client.login() method

475 views
Skip to first unread message

jondbaker

unread,
Sep 22, 2013, 7:36:24 PM9/22/13
to django...@googlegroups.com
Within my integration tests, it appears that after using the login() method of django.test.client.Client the context dict consists only of: 

[{'False': False, 'None': None, 'True': True}, {}, {}].

If I remove the use of login(), then the request dict consists of:

[{'False': False, 'None': None, 'True': True}, {'object': <User: te...@test.com>, 'user': <User: te...@test.com>, u'view': <accounts.views.AccountDetail object at 0x13bedd0>}, {u'csrf_token': <django.utils.functional.__proxy__ object at 0x1430090>}, {'perms': <django.contrib.auth.context_processors.PermWrapper object at 0x14301d0>, 'user': <django.utils.functional.SimpleLazyObject object at 0x13be390>}, {}, {u'MEDIA_URL': '/media/'}, {'messages': <django.contrib.messages.storage.fallback.FallbackStorage object at 0x1393c10>}, {u'request': <WSGIRequest
path:/accounts/,
GET:<QueryDict: {}>,
POST:<QueryDict: {}>,
COOKIES:{'sessionid': 'q27przqeidsnfykuufu7m6lo7nw606qw'},
META:{u'CONTENT_TYPE': 'text/html; charset=utf-8',
 u'CSRF_COOKIE': u'9mWW7nGMJQAgbs3KBB3oEPofbLBCkLW2',
 u'HTTP_COOKIE': u' sessionid=q27przqeidsnfykuufu7m6lo7nw606qw; Domain=None; expires=None; Max-Age=None; Path=/; secure',
 u'PATH_INFO': u'/accounts/',
 u'QUERY_STRING': '',
 u'REMOTE_ADDR': '127.0.0.1',
 u'REQUEST_METHOD': 'GET',
 u'SCRIPT_NAME': u'',
 u'SERVER_NAME': 'testserver',
 u'SERVER_PORT': '80',
 u'SERVER_PROTOCOL': 'HTTP/1.1',
 u'wsgi.errors': <_io.BytesIO object at 0x2358050>,
 u'wsgi.input': <django.test.client.FakePayload object at 0x2ac6d90>,
 u'wsgi.multiprocess': True,
 u'wsgi.multithread': False,
 u'wsgi.run_once': False,
 u'wsgi.url_scheme': 'http',
 u'wsgi.version': (1, 0)}>}, {u'STATIC_URL': '/static/'}, {}]

I noticed the problem when a template tag was trying to access context["request"].path and a KeyError was raised after using login. Both outputs above are the result of printing the context var from within the render() method of a template tag. Any help would be greatly appreciated, as I'm a bit stumped.

Thanks,
Jonathan

Jonathan Baker

unread,
Sep 23, 2013, 10:12:13 AM9/23/13
to django...@googlegroups.com
Below are some snippets involved in generating the error. The 'test_get_unauthenticated' works as expected, but 'test_get_ok' fails while rendering the template response and calling the menu tag of django-menu (full module: https://github.com/rossp/django-menu/blob/master/menu/templatetags/menubuilder.py).

integration_tests.py
class IntegrationTestCase(TestCase):
    def setUp(self):
        self.client = Client()
        self.test_domain = "http://testserver"

class AccountDetail(IntegrationTestCase):
    def test_get_unauthenticated(self):
        """302"""
        destination = reverse("account_detail")
        response = self.client.get(destination)
        self.assertRedirects(response, reverse("login") + "?next=" + destination)

    def test_get_ok(self):
        """200"""
        user = UserFactory.create()
        self.client.login(username=user.username, password="testPass")
        response = self.client.get(reverse("account_detail"))
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, "accounts/account_detail.html")

menubuilder.py
class MenuObject(template.Node):
    def __init__(self, menu_name):
        self.menu_name = menu_name

    def render(self, context):
        current_path = context['request'].path
        user = context['request'].user
        context['menuitems'] = get_items(self.menu_name, current_path, user)
        return ''


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.



--
Jonathan D. Baker
Developer
http://jonathandbaker.com
Reply all
Reply to author
Forward
0 new messages