django-pobject / pobject / tests.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from django.contrib.auth.models import User
from django.http import HttpRequest
from django.test import TestCase
from exceptional_middleware.responses import HttpForbidden
from pobject import P
from pobject.decorators import permission_required

def is_family(request, *args, **kwargs):
    return True

def is_father(request, *args, **kwargs):
    return True

def is_male(request, *args, **kwargs):
    return True

def is_female(request, *args, **kwargs):
    return False

def is_mother(request, *args, **kwargs):
    return False

def is_foe(request, *args, **kwargs):
    return False


class TestPobject(TestCase):

    def setUp(self):
        self.request = HttpRequest()
        self.request.user = User.objects.create_user('test', 'test@example.com')

    def test_pobject(self):
        p = P(is_family) & P(is_male, is_family) & ~P(is_foe)
        self.assertEquals(isinstance(p, P), True)

        expr_counter = 0
        while True:
            if p.next_expr:
                p = p.next_expr.p
                expr_counter += 1
            else:
                break
        self.assertEquals(expr_counter, 2)

    def assert_permission_required(self, *checkers, **kwargs):
        raises = kwargs.pop('raises', None)

        @permission_required('', *checkers)
        def test_view(request, *args, **kwargs):
            return True

        if not raises:
            self.assertEquals(test_view(self.request), True)
        else:
            self.assertRaises(raises, test_view, self.request)


    def test_is_family_plain_func1(self):
        self.assert_permission_required(
            is_family
        )

    def test_is_family_plain_func2(self):
        self.assert_permission_required(
            is_family, is_father
        )

    def test_is_family_pobject1(self):
        self.assert_permission_required(
            P(is_family)
        )

    def test_is_family_pobject2(self):
        self.assert_permission_required(
            P(is_family, is_father)
        )

    def test_is_family_pobject2_and(self):
        self.assert_permission_required(
            P(is_family) & P(is_father)
        )

    def test_is_family_pobject2_or(self):
        self.assert_permission_required(
            P(is_family) | P(is_father)
        )

    def test_is_not_foe(self):
        self.assert_permission_required(
            P(is_foe), raises=HttpForbidden
        )

    def test_is_not_foe_inverted(self):
        self.assert_permission_required(
            ~P(is_foe)
        )

    def test_is_family_and_not_foe(self):
        self.assert_permission_required(
            P(is_family) & ~P(is_foe)
        )

    def test_is_family_xor_foe(self):
        self.assert_permission_required(
            P(is_family) ^ P(is_foe)
        )

    def test_is_family_xor_male(self):
        self.assert_permission_required(
            P(is_family) ^ P(is_male), raises=HttpForbidden
        )

    def test_is_family_and_male(self):
        self.assert_permission_required(
            P(is_family) & P(is_male)
        )

    def test_is_father_or_mother(self):
        self.assert_permission_required(
            P(is_father) ^ P(is_mother)
        )

    def test_is_father_and_female(self):
        self.assert_permission_required(
            P(is_father) & P(is_female), raises=HttpForbidden
        )

    def test_is_father_male_not_mother_or_foe1(self):
        self.assert_permission_required(
            P(is_father, is_male) & ~P(is_mother) & ~P(is_foe)
        )

    def test_is_father_male_not_mother_or_foe2(self):
        self.assert_permission_required(
            P(is_father, is_male) & ~(P(is_mother) | P(is_foe))
        )

    def test_is_mother_only(self):
        self.assert_permission_required(
            P(is_family, is_female) & ~(P(is_father)), raises=HttpForbidden
        )
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.