1. Tim Savage
  2. django-extras

Commits

Tim Savage  committed 955d813

Updated tests to fit into design. Added tests for owner mixin objects.

  • Participants
  • Parent commits ca5df31
  • Branches default

Comments (0)

Files changed (15)

File .idea/django-extras.iml

View file
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="PYTHON_MODULE" version="4">
+  <component name="NewModuleRootManager">
+    <content url="file://$MODULE_DIR$" />
+    <orderEntry type="jdk" jdkName="Python 2.7.1 virtualenv at ~/Projects/ve" jdkType="Python SDK" />
+    <orderEntry type="sourceFolder" forTests="false" />
+  </component>
+</module>
+

File .idea/encodings.xml

View file
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="Encoding" useUTFGuessing="true" native2AsciiForPropertiesFiles="false" />
+</project>
+

File .idea/inspectionProfiles/profiles_settings.xml

View file
+<component name="InspectionProjectProfileManager">
+  <settings>
+    <option name="PROJECT_PROFILE" />
+    <option name="USE_PROJECT_PROFILE" value="false" />
+    <version value="1.0" />
+  </settings>
+</component>

File .idea/misc.xml

View file
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="ProjectResources">
+    <default-html-doctype>http://www.w3.org/1999/xhtml</default-html-doctype>
+  </component>
+  <component name="ProjectRootManager" version="2" project-jdk-name="Python 2.7.1 virtualenv at ~/Projects/ve" project-jdk-type="Python SDK" />
+</project>
+

File .idea/modules.xml

View file
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="ProjectModuleManager">
+    <modules>
+      <module fileurl="file://$PROJECT_DIR$/.idea/django-extras.iml" filepath="$PROJECT_DIR$/.idea/django-extras.iml" />
+    </modules>
+  </component>
+</project>
+

File .idea/scopes/scope_settings.xml

View file
+<component name="DependencyValidationManager">
+  <state>
+    <option name="SKIP_IMPORT_STATEMENTS" value="false" />
+  </state>
+</component>

File .idea/vcs.xml

View file
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="VcsDirectoryMappings">
+    <mapping directory="$PROJECT_DIR$" vcs="hg4idea" />
+  </component>
+</project>
+

File django_extras/contrib/auth/fixtures/owners.json

-[
-    {
-        "model":"auth.user",
-        "pk":1,
-        "fields":{
-            "username": "standard 1",
-            "first_name":"Standard",
-            "last_name":"User 1"
-        }
-    },
-    {
-        "model":"auth.user",
-        "pk":2,
-        "fields":{
-            "username": "standard 2",
-            "first_name":"Standard",
-            "last_name":"User 2"
-        }
-    },
-    {
-        "model":"auth.user",
-        "pk":3,
-        "fields":{
-            "username": "staff",
-            "first_name":"Staff",
-            "last_name":"User"
-        }
-    },
-    {
-        "model":"auth.user",
-        "pk":4,
-        "fields":{
-            "username": "admin",
-            "first_name":"Admin",
-            "last_name":"User"
-        }
-    }
-]

File django_extras/contrib/auth/models.py

View file
         if allow_staff or allow_superuser:
             if not user:
                 user = User.objects.only('is_staff', 'is_superuser').get(pk=user_pk)
-            if user.is_staff or user.is_superuser:
+            if (allow_staff and user.is_staff) or (allow_superuser and user.is_superuser):
                 return True
         return user_pk in self._get_owner_pks()
 
         return [self.owner]
 
     def _get_owner_pks(self):
-        return [self.owner.pk]
+        return [self.owner_id]
 
 
 class MultipleOwnerMixin(OwnerMixinBase):

File django_extras/contrib/auth/tests.py

-from django import test
-from django.db import models
-from django.contrib.auth.models import User
-from django_extras.contrib.auth.models import SingleOwnerMixin, MultipleOwnerMixin
-
-
-class SingleOwner(SingleOwnerMixin, models.Model):
-    name = models.CharField(max_length=50)
-    description = models.TextField()
-
-
-class MultiOwner(MultipleOwnerMixin, models.Model):
-    name = models.CharField(max_length=50)
-    description = models.TextField()
-
-
-class SingleOwnerTestCase(test.TransactionTestCase):
-    fixtures = ['owners.json']
-
-    def setUp(self):
-        self.user1 = User.objects.get(pk=1)
-        self.user2 = User.objects.get(pk=2)
-        self.user_staff = User.objects.get(pk=3)
-        self.user_admin = User.objects.get(pk=4)
-
-        SingleOwner.objects.create(name="Test 1", description="123", owner_id=1)
-        SingleOwner.objects.create(name="Test 2", description="123", owner_id=2)
-        SingleOwner.objects.create(name="Test 3", description="123", owner_id=3) # Staff
-        SingleOwner.objects.create(name="Test 4", description="123", owner_id=4) # Admin
-
-    def testOwnedBy(self):
-        s = SingleOwner.objects.get(1)
-        self.assertTrue(s.owned_by(self.user1))
-
-
-class MultiOwnerTestTestCase(test.TransactionTestCase):
-    fixtures = ['owners.json']
-

File django_extras/fixtures/owners.json

View file
+[
+    {
+        "model":"auth.user",
+        "pk":1,
+        "fields":{
+            "username": "standard 1",
+            "first_name":"Standard",
+            "last_name":"User 1"
+        }
+    },
+    {
+        "model":"auth.user",
+        "pk":2,
+        "fields":{
+            "username": "standard 2",
+            "first_name":"Standard",
+            "last_name":"User 2"
+        }
+    },
+    {
+        "model":"auth.user",
+        "pk":3,
+        "fields":{
+            "username": "staff",
+            "first_name":"Staff",
+            "last_name":"User",
+            "is_staff": true,
+            "is_superuser": false
+        }
+    },
+    {
+        "model":"auth.user",
+        "pk":4,
+        "fields":{
+            "username": "admin",
+            "first_name":"Admin",
+            "last_name":"User",
+            "is_superuser": true
+        }
+    }
+]

File django_extras/tests/__init__.py

View file
-__author__ = 'tsavage'
+from contrib.auth import *

File django_extras/tests/contrib/__init__.py

Empty file added.

File django_extras/tests/contrib/auth.py

View file
+from django import test
+from django.db import models
+from django.contrib.auth.models import User
+from django_extras.contrib.auth.models import SingleOwnerMixin, MultipleOwnerMixin
+
+
+class SingleOwner(SingleOwnerMixin, models.Model):
+    name = models.CharField(max_length=50)
+    description = models.TextField()
+
+    class Meta:
+        app_label = 'auth'
+
+
+class MultiOwner(MultipleOwnerMixin, models.Model):
+    name = models.CharField(max_length=50)
+    description = models.TextField()
+
+    class Meta:
+        app_label = 'auth'
+
+
+class SingleOwnerTestCase(test.TransactionTestCase):
+    fixtures = ['owners.json']
+
+    def setUp(self):
+        self.user1 = User.objects.get(pk=1)
+        self.user2 = User.objects.get(pk=2)
+        self.user_staff = User.objects.get(pk=3)
+        self.user_super = User.objects.get(pk=4)
+
+        SingleOwner.objects.create(name="Test 1", description="123", owner_id=1)
+        SingleOwner.objects.create(name="Test 2", description="123", owner_id=2)
+        SingleOwner.objects.create(name="Test 3", description="123", owner_id=3) # Staff
+        SingleOwner.objects.create(name="Test 4", description="123", owner_id=4) # Superuser
+
+    def test_user_has_access_standard(self):
+        s = SingleOwner.objects.get(pk=1)
+        self.assertTrue(s.user_has_access(self.user1))
+        self.assertFalse(s.user_has_access(self.user2))
+        self.assertFalse(s.user_has_access(self.user_staff))
+        self.assertFalse(s.user_has_access(self.user_super))
+
+    def test_user_has_access_staff(self):
+        s = SingleOwner.objects.get(pk=1)
+        self.assertTrue(s.user_has_access(self.user1, allow_staff=True))
+        self.assertFalse(s.user_has_access(self.user2, allow_staff=True))
+        self.assertTrue(s.user_has_access(self.user_staff, allow_staff=True))
+        self.assertFalse(s.user_has_access(self.user_super, allow_staff=True))
+
+    def test_user_has_access_superuser(self):
+        s = SingleOwner.objects.get(pk=1)
+        self.assertTrue(s.user_has_access(self.user1, allow_superuser=True))
+        self.assertFalse(s.user_has_access(self.user2, allow_superuser=True))
+        self.assertFalse(s.user_has_access(self.user_staff, allow_superuser=True))
+        self.assertTrue(s.user_has_access(self.user_super, allow_superuser=True))
+
+    def test_user_has_access_both(self):
+        s = SingleOwner.objects.get(pk=1)
+        self.assertTrue(s.user_has_access(self.user1, allow_staff=True, allow_superuser=True))
+        self.assertFalse(s.user_has_access(self.user2, allow_staff=True, allow_superuser=True))
+        self.assertTrue(s.user_has_access(self.user_staff, allow_staff=True, allow_superuser=True))
+        self.assertTrue(s.user_has_access(self.user_super, allow_staff=True, allow_superuser=True))
+
+    def test_user_has_access_pk_normal(self):
+        s = SingleOwner.objects.get(pk=1)
+        self.assertTrue(s.user_has_access(1))
+        self.assertFalse(s.user_has_access(2))
+        self.assertFalse(s.user_has_access(3))
+        self.assertFalse(s.user_has_access(4))
+
+    def test_user_has_access_pk_both(self):
+        s = SingleOwner.objects.get(pk=1)
+        self.assertTrue(s.user_has_access(1, allow_staff=True, allow_superuser=True))
+        self.assertFalse(s.user_has_access(2, allow_staff=True, allow_superuser=True))
+        self.assertTrue(s.user_has_access(3, allow_staff=True, allow_superuser=True))
+        self.assertTrue(s.user_has_access(4, allow_staff=True, allow_superuser=True))
+
+
+class MultiOwnerTestTestCase(test.TransactionTestCase):
+    fixtures = ['owners.json']
+
+    def setUp(self):
+        self.user1 = User.objects.get(pk=1)
+        self.user2 = User.objects.get(pk=2)
+        self.user_staff = User.objects.get(pk=3)
+        self.user_super = User.objects.get(pk=4)
+
+        MultiOwner.objects.create(name="Test 1", description="123").owners.add(self.user1)
+        MultiOwner.objects.create(name="Test 2", description="123")
+        MultiOwner.objects.create(name="Test 3", description="123")
+        MultiOwner.objects.create(name="Test 4", description="123")

File test_runner.py

View file
             os.path.join(os.path.dirname(__file__), 'templates'),
         ),
         'INSTALLED_APPS': (
-            #"django.contrib.auth",
+            "django.contrib.auth",
             "django.contrib.contenttypes",
             "django.contrib.sessions",
             "django.contrib.sites",
 
             "django_extras",
-            "django_extras.contrib.auth",
         ),
     })
     call_command('test', 'django_extras', 'auth')