brian-zhao / google-app-engine-django

Automatically exported from code.google.com/p/google-app-engine-django
Apache License 2.0
0 stars 0 forks source link

Patch django.test.Client so it suports login and logout #159

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Here is a simple monkeypatch, that enables django.test.Client to use login
and logout when running in the GAE environment.

Original issue reported on code.google.com by alefnula on 28 Jan 2010 at 12:32

Attachments:

GoogleCodeExporter commented 8 years ago
Can you provide an example/documentation on how to implement this? I may be 
trying to 
use the patch incorrectly. Here is basically what I tried:

from appengine_django.monkeypatch_test_client import NewClient

c = NewClient()
c.login()
resp = c.get('...')

which gives an exception:
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/lib/python2.5/site-packages/django/test/client.py", line 281, in get
    response = self.request(**r)
  File "/usr/lib/python2.5/site-packages/django/core/handlers/base.py", line 92, in 
get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "/home/rbradley/aidev/3gae/rand-aidev1/appengine_django/auth/decorators.py", 
line 30, in login_required_wrapper
    return HttpResponseRedirect(users.create_login_url(request.path))
  File "/usr/local/google_appengine/google/appengine/api/users.py", line 172, in 
create_login_url
    apiproxy_stub_map.MakeSyncCall('user', 'CreateLoginURL', req, resp)
  File "/usr/local/google_appengine/google/appengine/api/apiproxy_stub_map.py", line 
78, in MakeSyncCall
    return apiproxy.MakeSyncCall(service, call, request, response)
  File "/usr/local/google_appengine/google/appengine/api/apiproxy_stub_map.py", line 
278, in MakeSyncCall
    rpc.CheckSuccess()
  File "/usr/local/google_appengine/google/appengine/api/apiproxy_rpc.py", line 111, 
in CheckSuccess
    raise self.exception
KeyError: 'SERVER_NAME'

Original comment by rand.bra...@gmail.com on 18 Feb 2010 at 1:06

GoogleCodeExporter commented 8 years ago
You should also set next environment variables for testing:

# Setup needed for GAE
os.environ['SERVER_NAME'] = 'localhost'
os.environ['SERVER_PORT'] = '8000'

Basically you just need to import the patch and use the standard Django test 
client.

$ python manage.py shell

In [1]: import monkeypatch_test_client
In [2]: from django.test import Client
In [3]: c = Client()
In [4]: c.login()
Out[4]: True
In [5]: c.get('/')
Out[5]: <django.http.HttpResponseNotFound object at 0x020099D0>
In [6]: _.status_code
Out[6]: 404

But you should use it in your test cases so the test server is running. (that's 
why I
got 404).

Original comment by alefnula on 18 Feb 2010 at 3:27

GoogleCodeExporter commented 8 years ago

Original comment by m...@google.com on 8 Jun 2010 at 12:00