James Pic avatar James Pic committed 0f86c5a

Add basic behaviour test

Comments (0)

Files changed (2)

src/authority/fixtures/tests.json

+[
+    {
+        "pk": 1, 
+        "model": "auth.user", 
+        "fields": {
+            "username": "jezdez", 
+            "first_name": "", 
+            "last_name": "", 
+            "is_active": true, 
+            "is_superuser": false, 
+            "is_staff": false, 
+            "last_login": "2009-11-02 03:06:19", 
+            "groups": [], 
+            "user_permissions": [], 
+            "password": "", 
+            "email": "", 
+            "date_joined": "2009-11-02 03:06:19"
+        }
+    }
+]

src/authority/tests.py

+from django.test import TestCase
+from django.contrib.auth.models import User
+from django.contrib.auth.models import Permission as DjangoPermission
+
+import authority
+from authority import permissions
+from authority.models import Permission
+
+class UserPermission(permissions.BasePermission):
+    checks = ('browse',)
+    label = 'user_permission'
+authority.register(User, UserPermission)
+
+class BehaviourTest(TestCase):
+    '''
+    self.user will be given:
+    - django permission add_user (test_add)
+    - authority to delete_user which is him (test_delete)
+
+    This permissions are given in the test case and not in the fixture, for
+    later reference.
+    '''
+    
+    fixtures = ['tests.json',]
+
+    def setUp(self):
+        self.user = User.objects.get(username='jezdez')
+        self.check = UserPermission(self.user)
+
+    def test_no_permission(self):
+        self.assertFalse(self.check.add_user())
+        self.assertFalse(self.check.delete_user())
+        self.assertFalse(self.check.delete_user(self.user))
+
+    def test_add(self):
+        # setup
+        perm = DjangoPermission.objects.get(codename='add_user')
+        self.user.user_permissions.add(perm)
+
+        # test
+        self.assertTrue(self.check.add_user())
+
+    def test_delete(self):
+        perm = Permission(
+            user=self.user,
+            content_object=self.user,
+            codename='user_permission.delete_user',
+            approved=True
+        )
+        perm.save()
+
+        # test
+        self.assertFalse(self.check.delete_user())
+        self.assertTrue(self.check.delete_user(self.user))
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.