Beispiel #1
0
 def test_callbacks(self):
     def get_userid(ipaddr):
         if str(ipaddr).startswith('192'):
             return 'LAN-user'
         if str(ipaddr).startswith('127'):
             return 'localhost-user'
         return None
     def get_principals(userid, ipaddr):
         principals = {
             'LAN-user': ['view'],
             'localhost-user': ['view', 'edit'],
             }
         return principals.get(userid, [])
         
     policy = IPAuthenticationPolicy("all", get_userid=get_userid,
                                            get_principals=get_principals)
     # Addresses outside the range don't authenticate
     request = DummyRequest(environ={"REMOTE_ADDR": "192.168.0.1"})
     self.assertEqual(policy.unauthenticated_userid(request), "LAN-user")
     self.assertEqual(policy.authenticated_userid(request), "LAN-user")
     self.assertEqual(policy.effective_principals(request),
                      ["LAN-user", Everyone, Authenticated, 'view'])
     request = DummyRequest(environ={"REMOTE_ADDR": "127.0.0.1"})
     self.assertEqual(policy.unauthenticated_userid(request),
                      "localhost-user")
     self.assertEqual(policy.authenticated_userid(request),
                      "localhost-user")
     self.assertEqual(policy.effective_principals(request),
                      ["localhost-user", Everyone, Authenticated,
                       'view', 'edit'])
     request = DummyRequest(environ={"REMOTE_ADDR": "86.8.8.8"})
     self.assertEqual(policy.unauthenticated_userid(request), None)
     self.assertEqual(policy.authenticated_userid(request), None)
     self.assertEqual(policy.effective_principals(request), [Everyone])
Beispiel #2
0
 def test_principals(self):
     policy = IPAuthenticationPolicy(["123.123.0.0/16"], principals="test")
     # Addresses outside the range don't get metadata set
     request = DummyRequest(environ={"REMOTE_ADDR": "192.168.0.1"})
     self.assertEqual(policy.effective_principals(request), [Everyone])
     # Addresses inside the range do get metadata set
     request = DummyRequest(environ={"REMOTE_ADDR": "123.123.0.1"})
     self.assertEqual(policy.effective_principals(request),
                      [Everyone, "test"])
     policy.userid = "user"
     self.assertEqual(policy.effective_principals(request),
                      ["user", Everyone, Authenticated, "test"])
Beispiel #3
0
 def test_principals(self):
     policy = IPAuthenticationPolicy(["123.123.0.0/16"],
                                     principals=["test"])
     # Addresses outside the range don't get metadata set
     request = DummyRequest(environ={"REMOTE_ADDR": "192.168.0.1"})
     self.assertEquals(policy.effective_principals(request), [Everyone])
     # Addresses inside the range do get metadata set
     request = DummyRequest(environ={"REMOTE_ADDR": "123.123.0.1"})
     self.assertEquals(policy.effective_principals(request),
                       [Everyone, Authenticated, "test"])
     policy.userid = "user"
     self.assertEquals(policy.effective_principals(request),
                       ["user", Everyone, Authenticated, "test"])
Beispiel #4
0
    def test_callbacks(self):
        def get_userid(ipaddr):
            if str(ipaddr).startswith('192'):
                return 'LAN-user'
            if str(ipaddr).startswith('127'):
                return 'localhost-user'
            return None

        def get_principals(userid, ipaddr):
            principals = {
                'LAN-user': ['view'],
                'localhost-user': ['view', 'edit'],
            }
            return principals.get(userid, [])

        policy = IPAuthenticationPolicy("all",
                                        get_userid=get_userid,
                                        get_principals=get_principals)
        # Addresses outside the range don't authenticate
        request = DummyRequest(environ={"REMOTE_ADDR": "192.168.0.1"})
        self.assertEqual(policy.unauthenticated_userid(request), "LAN-user")
        self.assertEqual(policy.authenticated_userid(request), "LAN-user")
        self.assertEqual(policy.effective_principals(request),
                         ["LAN-user", Everyone, Authenticated, 'view'])
        request = DummyRequest(environ={"REMOTE_ADDR": "127.0.0.1"})
        self.assertEqual(policy.unauthenticated_userid(request),
                         "localhost-user")
        self.assertEqual(policy.authenticated_userid(request),
                         "localhost-user")
        self.assertEqual(
            policy.effective_principals(request),
            ["localhost-user", Everyone, Authenticated, 'view', 'edit'])
        request = DummyRequest(environ={"REMOTE_ADDR": "86.8.8.8"})
        self.assertEqual(policy.unauthenticated_userid(request), None)
        self.assertEqual(policy.authenticated_userid(request), None)
        self.assertEqual(policy.effective_principals(request), [Everyone])