Skip to content

Commit

Permalink
Added ability to login as arbitrary user
Browse files Browse the repository at this point in the history
  • Loading branch information
coagulant committed Jan 11, 2012
1 parent e562cd2 commit 69259df
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .idea/dictionaries/prophet.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion django_any/test.py
Expand Up @@ -40,7 +40,15 @@ def _request_context_forms(context):
class Client(DjangoClient):
def login_as(self, **kwargs):
password = xunit.any_string()
user = any_user(password=password, **kwargs)
if 'user' in kwargs:
user = kwargs['user']
try:
user.set_password(password)
user.save()
except Exception:
raise AssertionError('Bad user object')
else:
user = any_user(password=password, **kwargs)

if self.login(username=user.username, password=password):
return user
Expand Down
9 changes: 9 additions & 0 deletions django_any/tests/test_client.py
@@ -1,6 +1,7 @@
# -*- coding: utf-8; mode: django -*-
from django.conf.urls.defaults import patterns, include
from django.contrib import admin
from django.contrib.auth.models import User
from django.test import TestCase
from django_any.test import Client

Expand Down Expand Up @@ -47,6 +48,14 @@ def test_login_as_super_user(self):
response = self.client.get('/admin/')
self.assertEquals(200, response.status_code)

def test_login_as_custom_user(self):
user = User.objects.create_user('john', 'lennon@thebeatles.com', 'johnpassword')
self.assertTrue(self.client.login_as(user=user))

def test_login_as_failed(self):
user = None
self.assertRaises(AssertionError, self.client.login_as, user=user)

def test_post_any_data(self):
response = self.client.post_any_data('/view/')
self.assertRedirects(response, '/view/')
Expand Down
6 changes: 6 additions & 0 deletions docs/changelog.rst
Expand Up @@ -3,6 +3,12 @@
Changelog
=========

0.2.2
~~~~~

* Fixed pip installation
* Added ability to login_as arbitrary user

0.2.1
~~~~~

Expand Down
11 changes: 8 additions & 3 deletions docs/contrib.rst
Expand Up @@ -35,14 +35,15 @@ Creating users
Custom test client
------------------

.. currentmodule:: django_any.contrib
.. currentmodule:: django_any.test

Django any has custom test clent, that extends default django client.
It provides two useful methods for authorisation and posting forms.
Django-whatever has custom test clent, that extends default django client.
It provides two useful methods for authorization and forms posting.

.. function:: login_as(self, **kwargs):

Log into site as random user. Key-valued arguments are the same as for ``any_user`` function.
To log in as specific user, provide argument ``user`` (note, that user password will be reset).

.. code-block:: django
Expand All @@ -51,6 +52,10 @@ It provides two useful methods for authorisation and posting forms.
# log in as admin
self.client.login_as(is_superuser=True)
# log in as specific user
user = User.objects.create_user('john', 'lennon@thebeatles.com', 'johnpassword')
self.client.login_as(user=user)
.. _post_any_data:
.. function:: post_any_data(self, url, extra=None, context_forms=_request_context_forms, **kwargs):
Expand Down

0 comments on commit 69259df

Please sign in to comment.