Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

* Add unittests for limit_to_usergroups

* Add a test project. TODO: use this for all tests
  • Loading branch information...
commit 707e261b426f93e4beeb1adf0271bf37e1208772 1 parent 6a226be
@jedie authored
View
55 django_tools/tests/__init__.py
@@ -0,0 +1,55 @@
+# coding: utf-8
+
+#Hack to automatically import all tests
+#from django_tools.tests.test_limit_to_usergroups import LimitToUsergroupsTest
+
+import os
+import glob
+import unittest
+
+if __name__ == "__main__":
+ # run unittest directly
+ os.environ["DJANGO_SETTINGS_MODULE"] = "django_tools_test_project.test_settings"
+
+from django.test.testcases import SimpleTestCase
+
+
+if __name__ != "__main__": # do it only if we imported
+ print "import all django-tools tests:"
+ _BASE_PATH = os.path.abspath(os.path.dirname(__file__))
+# print "_BASE_PATH:", _BASE_PATH
+ for filename in glob.glob("*.py"):
+ if not filename.startswith("test_"):
+ continue
+# print "_"*79
+# print filename
+ file_basename = os.path.splitext(filename)[0]
+ _module = "django_tools.tests.%s" % file_basename
+# print _module
+ if file_basename in locals():
+ # print "Skip!"
+ continue
+ _test_module = __import__(_module, globals(), locals(), ["*"])
+ # print "globals:", globals()
+ # print "locals:", locals().keys()
+# print _test_module
+
+ # assimilate all local settings from modul, see: http://stackoverflow.com/a/2916810/746522
+ for key in dir(_test_module):
+ if key.startswith("_"):
+ continue
+ if "Test" in key:
+ print "Add test class: %s.%s" % (_module, key)
+ obj = getattr(_test_module, key)
+ locals()[key] = obj
+
+
+if __name__ == "__main__":
+ # Run this unitest directly
+ from django.core import management
+ management.call_command('test',
+ "django_tools",
+ verbosity=2,
+# failfast=True
+ )
+# unittest.main()
View
7 django_tools/tests/models.py
@@ -1,7 +0,0 @@
-# coding: utf-8
-
-from django.forms.models import ModelForm
-from django_tools.fields.sign_separated import SignSeparatedModelField
-
-
-
View
148 django_tools/tests/test_limit_to_usergroups.py
@@ -0,0 +1,148 @@
+# coding: utf-8
+
+"""
+ Test limit to usergroups
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+
+ Unittests for limit_to_usergroups.py, see:
+ https://github.com/jedie/django-tools/blob/master/django_tools/limit_to_usergroups.py
+
+ :copyleft: 2012 by the django-tools team, see AUTHORS for more details.
+ :license: GNU GPL v3 or above, see LICENSE for more details.
+"""
+
+import unittest
+
+if __name__ == "__main__":
+ # run unittest directly
+ import os
+ os.environ["DJANGO_SETTINGS_MODULE"] = "django_tools_test_project.test_settings"
+
+from django.test import TestCase
+from django.core import management
+from django.contrib.auth.models import Group, AnonymousUser
+
+from django_tools import limit_to_usergroups
+from django_tools.limit_to_usergroups import get_verbose_limit_name,\
+ has_permission
+from django_tools.unittest_utils.unittest_base import BaseTestCase
+from django_tools_test_project.django_tools_test_app.models import LimitToUsergroupsTestModel
+
+
+class LimitToUsergroupsTest1(TestCase):
+ def setUp(self):
+ self.instance = LimitToUsergroupsTestModel.objects.create(
+ permit_edit = limit_to_usergroups.UsergroupsModelField.SUPERUSERS,
+ permit_view = limit_to_usergroups.UsergroupsModelField.ANONYMOUS_USERS,
+ )
+
+ def test_basic(self):
+ self.assertEqual(
+ self.instance.permit_edit,
+ limit_to_usergroups.UsergroupsModelField.SUPERUSERS
+ )
+ self.assertEqual(
+ self.instance.permit_view,
+ limit_to_usergroups.UsergroupsModelField.ANONYMOUS_USERS
+ )
+
+
+class LimitToUsergroupsTest2(BaseTestCase, TestCase):
+ def _pre_setup(self):
+ super(LimitToUsergroupsTest2, self)._pre_setup()
+ self.create_testusers()
+
+ def setUp(self):
+ self.user1 = self.login(usertype="staff")
+
+ self.instance1 = LimitToUsergroupsTestModel.objects.create(
+ permit_edit = limit_to_usergroups.UsergroupsModelField.STAFF_USERS,
+ permit_view = limit_to_usergroups.UsergroupsModelField.ANONYMOUS_USERS,
+ )
+ self.user_group = Group.objects.create(name="user group 1")
+
+ self.instance2 = LimitToUsergroupsTestModel.objects.create(
+ permit_edit = self.user_group.pk,
+ permit_view = limit_to_usergroups.UsergroupsModelField.NORMAL_USERS,
+ )
+
+ def test_filter_permission1(self):
+ queryset = LimitToUsergroupsTestModel.objects.all()
+
+ # Anonymous can view all
+ items = limit_to_usergroups.filter_permission(
+ queryset, permit_view=self.user1
+ )
+ self.assertEqual(len(items), 2)
+
+ def test_filter_permission2(self):
+ queryset = LimitToUsergroupsTestModel.objects.all()
+
+ # edit only for staff or users in the right user group
+ items = limit_to_usergroups.filter_permission(
+ queryset, permit_edit=self.user1
+ )
+ self.assertEqual(len(items), 1)
+
+ def test_filter_permission3(self):
+ queryset = LimitToUsergroupsTestModel.objects.all()
+
+ # Check if he is in the user group
+ self.user1.groups.add(self.user_group)
+ self.user1.save()
+ items = limit_to_usergroups.filter_permission(
+ queryset, permit_edit=self.user1
+ )
+ self.assertEqual(len(items), 2)
+
+ def test_has_permission1(self):
+ anonymous = AnonymousUser()
+
+ self.assertTrue(
+ has_permission(self.instance1, permit_view=anonymous)
+ )
+ self.assertFalse(
+ has_permission(self.instance1, permit_edit=anonymous)
+ )
+
+ def test_has_permission2(self):
+ anonymous = AnonymousUser()
+
+ self.assertFalse(
+ has_permission(self.instance2, permit_view=anonymous, permit_edit=anonymous)
+ )
+
+ def test_has_permission3(self):
+ user = self.login(usertype="normal")
+ self.assertFalse(
+ has_permission(self.instance2, permit_view=user, permit_edit=user)
+ )
+ user.groups.add(self.user_group)
+ user.save()
+ self.assertTrue(
+ has_permission(self.instance2, permit_view=user, permit_edit=user)
+ )
+
+ def test_verbose_limit_name1(self):
+ self.assertEqual(
+ get_verbose_limit_name(limit_to_usergroups.UsergroupsModelField.STAFF_USERS),
+ u'staff users'
+ )
+
+ def test_verbose_limit_name2(self):
+ self.assertEqual(
+ get_verbose_limit_name(self.user_group.pk),
+ u'user group 1'
+ )
+
+
+if __name__ == "__main__":
+ # Run this unitest directly
+ management.call_command('test',
+ "django_tools.LimitToUsergroupsTest1",
+ "django_tools.LimitToUsergroupsTest2",
+ verbosity=2,
+# failfast=True
+ )
+ unittest.main()
+
View
1  django_tools_test_project/__init__.py
@@ -0,0 +1 @@
+"""Test project for django-tools."""
View
0  django_tools_test_project/django_tools_test_app/__init__.py
No changes.
View
17 django_tools_test_project/django_tools_test_app/models.py
@@ -0,0 +1,17 @@
+# coding: utf-8
+
+"""
+ django-tools test models
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+
+ :copyleft: 2012 by the django-tools team, see AUTHORS for more details.
+ :license: GNU GPL v3 or above, see LICENSE for more details.
+"""
+
+from django.db import models
+from django_tools import limit_to_usergroups
+
+
+class LimitToUsergroupsTestModel(models.Model):
+ permit_edit = limit_to_usergroups.UsergroupsModelField()
+ permit_view = limit_to_usergroups.UsergroupsModelField()
View
8 django_tools/tests/test_settings.py → django_tools_test_project/test_settings.py
@@ -1,16 +1,14 @@
# coding: utf-8
import os
-import sys
-
-sys.path.insert(0, os.path.dirname(__file__))
DEBUG = True
+TEMPLATE_DEBUG = DEBUG
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
- 'NAME': ":memory:"
+ 'NAME': os.path.join(os.path.abspath(os.path.dirname(__file__)), "test.db3"),
}
}
@@ -31,6 +29,6 @@
'django_tools',
'django_tools.local_sync_cache',
- 'django_tools.tests',
+ 'django_tools_test_project.django_tools_test_app',
)
SITE_ID = 1
Please sign in to comment.
Something went wrong with that request. Please try again.