def test_no_request(self):

        context = SecurityContext()
        context.token = Token('key', 'user')

        with self.assertRaises(NoRequestFoundException):
            AccessMapListener(AccessMap(), context, RoleHierarchy()).handle(Event())
    def test_token_already_set(self):
        c = SecurityContext()
        c.token = Token('key', 'user')

        e = Event()
        h = AnonymousAuthenticationHandler('provider_name', c)

        h.handle(e)

        self.assertNotIsInstance(c.token, AnonymousToken)
    def test_is_granted(self):
        context = SecurityContext()

        context.token = AnonymousToken('provider', 'ann.')

        self.assertFalse(context.is_granted(''))
        self.assertFalse(context.is_granted('role'))

        context.token.roles.append('role')

        self.assertTrue(context.is_granted('role'))
    def test_no_rule_in_access_map(self):
        r = Request()
        r.path = ""

        context = SecurityContext()
        context.token = Token('key', 'user')

        with self.assertRaises(AccessDeniedException):
           AccessMapListener(AccessMap(), context, RoleHierarchy()).handle(Event({
                'request': r
            }))
    def test_match(self):
        r = Request()
        r.path = "/blog/2012/12/12-myblog.html"

        context = SecurityContext()
        context.token = AnonymousToken('key', 'anon.', 'IS_AUTHENTICATED_ANONYMOUSLY')

        AccessMapListener(AccessMap([(re.compile("/blog.*"), ['IS_AUTHENTICATED_ANONYMOUSLY'])]), context, RoleHierarchy()).handle(Event({
            'request': r
        }))

        # mark the test as valid ...
        self.assertTrue(True)