Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Tests for template tag. #16

Merged
merged 2 commits into from about 2 years ago

3 participants

Filip Wasilewski Chris Glass Alvin Mites
Filip Wasilewski
nigma commented March 06, 2012

I've created tests for what I would consider the correct behaviour of the template tag.

Proposed fixes from #15 makes this test set to pass.

Chris Glass
Owner

Oh whow, you're awesome :)

Chris Glass chrisglass merged commit 4b43928 into from March 06, 2012
Chris Glass chrisglass closed this March 06, 2012
Alvin Mites

Thank you for writing the tests, neglected to get back to this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
1  rulez/tests/__init__.py
@@ -2,3 +2,4 @@
2 2
 from registry import *
3 3
 from backend import *
4 4
 from signals import *
  5
+from templatetags import *
108  rulez/tests/templatetags.py
... ...
@@ -0,0 +1,108 @@
  1
+#-*- coding: utf-8 -*-
  2
+
  3
+from django.test.testcases import TestCase
  4
+from django.contrib.auth.models import AnonymousUser, User
  5
+from django.template import Template, Context, TemplateSyntaxError
  6
+
  7
+from rulez import registry
  8
+from rulez.tests.backend import MockUser
  9
+
  10
+
  11
+class MockModel(object):
  12
+    pk = 999
  13
+
  14
+    def mock_positive_permission(self, user):
  15
+        return True
  16
+
  17
+    def mock_negative_permission(self, user):
  18
+        return False
  19
+
  20
+
  21
+class TemplatetagTestCase(TestCase):
  22
+
  23
+    def create_fixtures(self):
  24
+        self.user = MockUser()
  25
+        self.inactive_user = MockUser(is_active=False)
  26
+        self.model = MockModel()
  27
+
  28
+    def render_template(self, template, context):
  29
+        context = Context(context)
  30
+        return Template(template).render(context)
  31
+
  32
+    def assertYesHeCan(self, permission, user):
  33
+        registry.register(permission, MockModel)
  34
+        rendered = self.render_template(
  35
+            "{% load rulez_perms %}"
  36
+            "{% rulez_perms " + permission + " object as can %}"
  37
+            "{% if can %}yes he can{% else %}no can do{% endif %}",
  38
+            {
  39
+                "user": user,
  40
+                "object": MockModel()
  41
+            }
  42
+        )
  43
+        self.assertEqual(rendered, "yes he can")
  44
+
  45
+    def assertNoHeCant(self, permission, user):
  46
+        registry.register(permission, MockModel)
  47
+        rendered = self.render_template(
  48
+            "{% load rulez_perms %}"
  49
+            "{% rulez_perms " + permission + " object as can %}"
  50
+            "{% if can %}yes he can{% else %}no he can't{% endif %}",
  51
+            {
  52
+                "user": user,
  53
+                "object": MockModel()
  54
+            }
  55
+        )
  56
+        self.assertEqual(rendered, "no he can't")
  57
+
  58
+    def test_active_user_against_positive_permission(self):
  59
+        self.assertYesHeCan("mock_positive_permission", User(is_active=True))
  60
+
  61
+    def test_active_user_for_negative_permission(self):
  62
+        self.assertNoHeCant("mock_negative_permission", User(is_active=True))
  63
+
  64
+    def test_inactive_user_against_positive_permission(self):
  65
+        self.assertNoHeCant("mock_positive_permission", User(is_active=False))
  66
+
  67
+    def test_inactive_user_against_negative_permission(self):
  68
+        self.assertNoHeCant("mock_negative_permission", User(is_active=False))
  69
+
  70
+    def test_anonymous_user_against_positive_permission(self):
  71
+        self.assertYesHeCan("mock_positive_permission", AnonymousUser())
  72
+
  73
+    def test_anonymous_user_against_negative_permission(self):
  74
+        self.assertNoHeCant("mock_negative_permission", AnonymousUser())
  75
+
  76
+    def test_active_user_against_missing_permission(self):
  77
+        permission = "missing"
  78
+        rendered = self.render_template(
  79
+            "{% load rulez_perms %}"
  80
+            "{% rulez_perms " + permission + " object as can %}"
  81
+            "{% if can %}yes he can{% else %}no he can't{% endif %}",
  82
+            {
  83
+                "user": User(is_active=True),
  84
+                "object": MockModel()
  85
+            }
  86
+        )
  87
+        self.assertEqual(rendered, "no he can't")
  88
+
  89
+    def test_invalid_user(self):
  90
+        self.assertRaisesRegexp((TemplateSyntaxError, AttributeError),
  91
+            "'NoneType' object has no attribute 'has_perm'",
  92
+            self.render_template,
  93
+            "{% load rulez_perms %}{% rulez_perms mock_positive_permission object as can %}", {
  94
+                "object": MockModel(), "user": None
  95
+            })
  96
+
  97
+    def test_tag_syntax(self):
  98
+        registry.register("mock_positive_permission", MockModel)
  99
+
  100
+        # TODO: error messages from template tag a bit are confusing.
  101
+        self.assertRaisesRegexp(TemplateSyntaxError, "tag requires exactly three arguments", self.render_template,
  102
+            "{% load rulez_perms %}{% rulez_perms mock_positive_permission object %}", {})
  103
+
  104
+        self.assertRaisesRegexp(TemplateSyntaxError, "tag requires exactly three arguments", self.render_template,
  105
+            "{% load rulez_perms %}{% rulez_perms mock_positive_permission object can %}", {})
  106
+
  107
+        self.assertRaisesRegexp(TemplateSyntaxError, "third argument to tag must be 'as'", self.render_template,
  108
+            "{% load rulez_perms %}{% rulez_perms mock_positive_permission object can can %}", {})
4  tests/testapp/settings.py
@@ -145,3 +145,7 @@
145 145
         },
146 146
     }
147 147
 }
  148
+
  149
+AUTHENTICATION_BACKENDS = [
  150
+    "rulez.backends.ObjectPermissionBackend",
  151
+]
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.