Ejemplo n.º 1
0
    def testDefaultRouterIsReturnedIfNoAclsAreDefined(self):
        auth_mgr = api_auth_manager.APIAuthorizationManager(
            [], DefaultDummyAuthManagerTestApiRouter)

        router = auth_mgr.GetRouterForUser("u1")
        self.assertEqual(router.__class__,
                         DefaultDummyAuthManagerTestApiRouter)
Ejemplo n.º 2
0
 def testDefaultRouterIsReturnedIfNoConfigFileDefined(self):
     """The default router is returned if no API.RouterACLConfigFile defined."""
     with test_lib.ConfigOverrider({"API.RouterACLConfigFile": ""}):
         auth_mgr = api_auth_manager.APIAuthorizationManager()
         router = auth_mgr.GetRouterForUser("u1")
         self.assertEqual(router.__class__,
                          DefaultDummyAuthManagerTestApiRouter)
Ejemplo n.º 3
0
    def testReturnsFirstRouterWhenMatchingByUser(self):
        acls = """
router: "DummyAuthManagerTestApiRouter"
users:
- "u1"
- "u3"
---
router: "DummyAuthManagerTestApiRouter2"
users:
- "u1"
- "u2"
---
router: "DummyAuthManagerTestApiRouter3"
users:
- "u2"
- "u4"
"""
        with mock.patch.object(__builtin__, "open",
                               mock.mock_open(read_data=acls)):
            auth_mgr = api_auth_manager.APIAuthorizationManager()

        router = auth_mgr.GetRouterForUser("u2")
        self.assertEqual(router.__class__, DummyAuthManagerTestApiRouter2)

        router = auth_mgr.GetRouterForUser("u4")
        self.assertTrue(router.__class__, DummyAuthManagerTestApiRouter3)
Ejemplo n.º 4
0
  def testMatchingByGroupWorks(self):
    acls = """
router: "DummyAuthManagerTestApiRouter2"
groups:
- "g1"
"""

    with mock.patch.object(__builtin__, "open", mock.mock_open(read_data=acls)):
      auth_mgr = api_auth_manager.APIAuthorizationManager()

    router = auth_mgr.GetRouterForUser("u1")
    self.assertEqual(router.__class__, DummyAuthManagerTestApiRouter2)
Ejemplo n.º 5
0
  def testReturnsDefaultOnNoMatchByUser(self):
    acls = """
router: "DummyAuthManagerTestApiRouter"
users:
- "u1"
- "u2"
"""
    with mock.patch.object(__builtin__, "open", mock.mock_open(read_data=acls)):
      auth_mgr = api_auth_manager.APIAuthorizationManager()

    router = auth_mgr.GetRouterForUser("u4")
    self.assertEqual(router.__class__, DefaultDummyAuthManagerTestApiRouter)
Ejemplo n.º 6
0
  def testConfigurableRouterIsInitializedWithoutParameters(self):
    acls = """
router: "DummyAuthManagerTestConfigurableApiRouter"
users:
- "u1"
"""

    with mock.patch.object(__builtin__, "open", mock.mock_open(read_data=acls)):
      auth_mgr = api_auth_manager.APIAuthorizationManager()

    router = auth_mgr.GetRouterForUser("u1")
    self.assertEqual(router.params.foo, "")
    self.assertEqual(router.params.bar, 0)
Ejemplo n.º 7
0
    def testRaisesWhenNonConfigurableRouterInitializedWithParams(self):
        acls = """
router: "DummyAuthManagerTestApiRouter"
router_params:
  foo: "Oh no!"
  bar: 42
users:
- "u1"
"""

        with self.assertRaises(
                api_auth_manager.ApiCallRouterDoesNotExpectParameters):
            with mock.patch.object(__builtin__, "open",
                                   mock.mock_open(read_data=acls)):
                api_auth_manager.APIAuthorizationManager()
Ejemplo n.º 8
0
  def testMatchesIfOneOfUsersIsMatching(self):
    acls = """
router: "DummyAuthManagerTestApiRouter"
users:
- "u1"
- "u2"
"""
    with mock.patch.object(__builtin__, "open", mock.mock_open(read_data=acls)):
      auth_mgr = api_auth_manager.APIAuthorizationManager()

    router = auth_mgr.GetRouterForUser("u1")
    self.assertEqual(router.__class__, DummyAuthManagerTestApiRouter)

    router = auth_mgr.GetRouterForUser("u2")
    self.assertEqual(router.__class__, DummyAuthManagerTestApiRouter)
Ejemplo n.º 9
0
  def testReturnsDefaultRouterWhenNothingMatchesByGroup(self):
    acls = """
router: "DummyAuthManagerTestApiRouter"
groups:
- "g5"
---
router: "DummyAuthManagerTestApiRouter2"
groups:
- "g6"
"""
    with mock.patch.object(__builtin__, "open", mock.mock_open(read_data=acls)):
      auth_mgr = api_auth_manager.APIAuthorizationManager()

    router = auth_mgr.GetRouterForUser("u1")
    self.assertEqual(router.__class__, DefaultDummyAuthManagerTestApiRouter)
Ejemplo n.º 10
0
  def testReturnsFirstMatchingRouterWhenItMatchesByGroupAndOtherByUser(self):
    acls = """
router: "DummyAuthManagerTestApiRouter"
groups:
- "g3"
---
router: "DummyAuthManagerTestApiRouter2"
users:
- "u1"
"""

    with mock.patch.object(__builtin__, "open", mock.mock_open(read_data=acls)):
      auth_mgr = api_auth_manager.APIAuthorizationManager()

    router = auth_mgr.GetRouterForUser("u1")
    self.assertEqual(router.__class__, DummyAuthManagerTestApiRouter)