Peter Sanchez avatar Peter Sanchez committed 5101226

Finally adding tests. Very incomplete

Comments (0)

Files changed (4)

 Dependencies
 ------------
 
-It was written for Python 2.4+ and Django 1.2.4 but it should 
-work just fine with Django 1.1+
+It was written for Python 2.6+ and Django 1.4+
 
 It depends on your project using the django.contrib.session framework.
 

impersonate/fixtures/test_data.json

+[
+    {
+        "pk": 1, 
+        "model": "auth.user", 
+        "fields": {
+            "username": "user1", 
+            "first_name": "", 
+            "last_name": "", 
+            "is_active": true, 
+            "is_superuser": true, 
+            "is_staff": true, 
+            "last_login": "2012-11-18T21:39:07.470Z", 
+            "groups": [], 
+            "user_permissions": [], 
+            "password": "pbkdf2_sha256$10000$rFUAeNe8N8AC$1BYYCs9seuca2NqBLLtO3wmNVdnBeSbASF2LhhFbp1k=", 
+            "email": "user1@test-email.com", 
+            "date_joined": "2012-11-18T21:39:07.470Z"
+        }
+    }, 
+    {
+        "pk": 2, 
+        "model": "auth.user", 
+        "fields": {
+            "username": "user2", 
+            "first_name": "", 
+            "last_name": "", 
+            "is_active": true, 
+            "is_superuser": true, 
+            "is_staff": true, 
+            "last_login": "2012-11-18T21:39:17.669Z", 
+            "groups": [], 
+            "user_permissions": [], 
+            "password": "pbkdf2_sha256$10000$rFUAeNe8N8AC$1BYYCs9seuca2NqBLLtO3wmNVdnBeSbASF2LhhFbp1k=",
+            "email": "user2@test-email.com", 
+            "date_joined": "2012-11-18T21:39:17.669Z"
+        }
+    }, 
+    {
+        "pk": 3, 
+        "model": "auth.user", 
+        "fields": {
+            "username": "user3", 
+            "first_name": "", 
+            "last_name": "", 
+            "is_active": true, 
+            "is_superuser": false, 
+            "is_staff": true, 
+            "last_login": "2012-11-19T01:47:54.213Z", 
+            "groups": [], 
+            "user_permissions": [], 
+            "password": "pbkdf2_sha256$10000$rFUAeNe8N8AC$1BYYCs9seuca2NqBLLtO3wmNVdnBeSbASF2LhhFbp1k=", 
+            "email": "user3@test-email.com", 
+            "date_joined": "2012-11-19T01:47:54.213Z"
+        }
+    }, 
+    {
+        "pk": 4, 
+        "model": "auth.user", 
+        "fields": {
+            "username": "user4", 
+            "first_name": "", 
+            "last_name": "", 
+            "is_active": true, 
+            "is_superuser": false, 
+            "is_staff": false, 
+            "last_login": "2013-03-11T20:53:07.138Z", 
+            "groups": [], 
+            "user_permissions": [], 
+            "password": "pbkdf2_sha256$10000$rFUAeNe8N8AC$1BYYCs9seuca2NqBLLtO3wmNVdnBeSbASF2LhhFbp1k=",
+            "email": "user4@test-email.com", 
+            "date_joined": "2013-03-11T20:51:37.424Z"
+        }
+    }
+]

impersonate/tests.py

+'''
+    Tests
+    test_data.json contains 4 User accounts.
+    user1:
+        id = 1
+        is_superuser = True
+        is_staff = True
+    user2:
+        id = 2
+        is_superuser = True
+        is_staff = True
+    user3:
+        id = 3
+        is_superuser = False
+        is_staff = True
+    user4:
+        id = 4
+        is_superuser = False
+        is_staff = False
+'''
+from django.test import TestCase
+from django.utils import unittest
+from django.test.client import Client
+from django.contrib.auth.models import User
+from django.core.urlresolvers import reverse
+
+try:
+    # Django 1.5 check
+    from django.contrib.auth import get_user_model
+except ImportError:
+    get_user_model = None
+
+
+class TestImpersonation(TestCase):
+    fixtures = ['test_data.json']
+
+    def setUp(self):
+        self.client = Client()
+        self.user_model = get_user_model() if get_user_model else User
+
+    def test_user_count(self):
+        self.assertEqual(self.user_model.objects.count(), 4)
+
+    def test_dont_impersonate_superuser(self):
+        self.client.login(username='user1', password='foobar')
+        self.client.get(reverse('impersonate-start', args=[2]))
+        self.assertEqual(self.client.session.get('_impersonate'), None)
+        self.client.logout()
+
+    def test_successful_impersonation(self):
+        self.client.login(username='user1', password='foobar')
+        self.client.get(reverse('impersonate-start', args=[4]))
+        self.assertEqual(self.client.session['_impersonate'].id, 4)
+        self.client.get(reverse('impersonate-stop'))
+        self.assertEqual(self.client.session.get('_impersonate'), None)
+        self.client.logout()
+import sys
+from django.conf import settings
+
+
+APP_NAME = 'impersonate'
+
+settings.configure(
+    DEBUG=True,
+    DATABASES={
+        'default': {
+            'ENGINE': 'django.db.backends.sqlite3',
+        }
+    },
+    USE_TZ=True,
+    ROOT_URLCONF='{}.urls'.format(APP_NAME),
+    INSTALLED_APPS=(
+        'django.contrib.auth',
+        'django.contrib.contenttypes',
+        'django.contrib.sessions',
+        'django.contrib.admin',
+        APP_NAME,
+    ),
+)
+
+from django.test.simple import DjangoTestSuiteRunner
+test_runner = DjangoTestSuiteRunner(verbosity=1)
+failures = test_runner.run_tests([APP_NAME])
+if failures:
+    sys.exit(failures)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.