Created
February 20, 2009 09:57
-
-
Save akaihola/67402 to your computer and use it in GitHub Desktop.
Test project to verify that django.test.client populates request.user
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*.pyc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% autoescape off %}{{ request_class }}().user.username == {{ username }}{% endautoescape %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from django.core.management import execute_manager | |
try: | |
import settings # Assumed to be in the same directory. | |
except ImportError: | |
import sys | |
sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n(If the file settings.py does indeed exist, it's causing an ImportError somehow.)\n" % __file__) | |
sys.exit(1) | |
if __name__ == "__main__": | |
execute_manager(settings) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ROOT_URLCONF = 'urls' | |
DATABASE_ENGINE = 'sqlite3' | |
INSTALLED_APPS = ( | |
'django.contrib.auth', | |
'django.contrib.contenttypes', | |
'django.contrib.sessions', | |
'testapp', | |
) | |
TEMPLATE_DIRS = 'templates', |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.test import TestCase | |
from django.contrib.auth.models import User | |
class RequestUserTest(TestCase): | |
def test_r(self): | |
response = self.client.get('/') | |
self.assertEqual(response.content, "WSGIRequest().user.username == ''") | |
User.objects.create_user(username='testuser', | |
password='password', | |
email='[email protected]') | |
result = self.client.login(username='testuser', password='password') | |
self.assertEqual(result, True) | |
response = self.client.get('/') | |
self.assertEqual(response.content, | |
"WSGIRequest().user.username == u'testuser'") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.conf.urls.defaults import * | |
from django.http import HttpResponse | |
from django.template import RequestContext | |
from django.template.loader import render_to_string | |
class View(HttpResponse): | |
""" | |
>>> class request: | |
... method = 'GET' | |
>>> print unicode(View(request, _render=False)).strip() | |
Content-Type: text/html; charset=utf-8 | |
>>> class MyView(View): | |
... def GET(self, request): | |
... return HttpResponseRedirect('/elsewhere/') | |
>>> print unicode(MyView(request, _render=False)).strip() | |
Content-Type: text/html; charset=utf-8 | |
Location: /elsewhere/ | |
""" | |
template_name = 'default.html' | |
_http_methods = ( | |
'GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'OPTIONS', 'TRACE', 'CONNECT') | |
def __init__(self, request, *args, **kwargs): | |
render = kwargs.pop('_render', True) | |
try: | |
handler = getattr(self, request.method) | |
except AttributeError: | |
result = HttpResponseNotAllowed( | |
[method for method in self._http_methods | |
if hasattr(self, method)]) | |
else: | |
result = handler(request, *args, **kwargs) | |
if isinstance(result, HttpResponse): | |
self.__class__ = result.__class__ | |
self.__dict__ = result.__dict__ | |
else: | |
self.context = result | |
if render: | |
content = self.render(request, *args, **kwargs) | |
else: | |
content = '' | |
super(View, self).__init__(content) | |
def render(self, request, *args, **kwargs): | |
return render_to_string(self.template_name, | |
self.context, | |
RequestContext(request)) | |
def set_response(self, cls, *args): | |
self.__class__ = cls | |
self.__init__(*args) | |
class TestView(View): | |
def GET(self, request, *args, **kwargs): | |
return dict(request_class=request.__class__.__name__, | |
username=repr(request.user.username)) | |
urlpatterns = patterns( | |
'', | |
url(r'^$', TestView), | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment