def _get_request_context(role): return request.Request( testing.create_environ( path='/', headers={'X_ROLES': role} ) )
def test_authorize_bad_action_no_exception(self): action = "example:denied" ctx = request.Request( testing.create_environ(path="/", headers={ "X_USER_ID": "fake", "X_PROJECT_ID": "fake", "X_ROLES": "member" })) result = policy.authorize(ctx.context, action, {}, False) self.assertFalse(result)
def test_authorize_bad_action_throws(self): action = "example:denied" ctx = request.Request( testing.create_environ(path="/", headers={ "X_USER_ID": "fake", "X_PROJECT_ID": "fake", "X_ROLES": "member" })) self.assertRaises(os_policy.PolicyNotAuthorized, policy.authorize, ctx.context, action, {})
def test_use_context_from_request(self): req = request.Request( testing.create_environ( path='/', headers={ 'X_AUTH_TOKEN': '111', 'X_USER_ID': '222', 'X_PROJECT_ID': '333', 'X_ROLES': 'terminator,predator' } ) ) self.assertEqual('111', req.context.auth_token) self.assertEqual('222', req.user_id) self.assertEqual('333', req.project_id) self.assertEqual(['terminator', 'predator'], req.roles)
def test_ignore_case_role_check(self): lowercase_action = "example:lowercase_monasca_user" uppercase_action = "example:uppercase_monasca_user" monasca_user_context = request.Request( testing.create_environ(path="/", headers={ "X_USER_ID": "monasca_user", "X_PROJECT_ID": "fake", "X_ROLES": "MONASCA_user" })) self.assertTrue( policy.authorize(monasca_user_context.context, lowercase_action, {})) self.assertTrue( policy.authorize(monasca_user_context.context, uppercase_action, {}))
def test_validate_context_type(self): with mock.patch.object(validation, 'validate_content_type') as vc_type, \ mock.patch.object(validation, 'validate_payload_size') as vp_size, \ mock.patch.object(validation, 'validate_cross_tenant') as vc_tenant: req = request.Request(testing.create_environ()) vc_type.side_effect = Exception() try: req.validate(['test']) except Exception as ex: self.assertEqual(1, vc_type.call_count) self.assertEqual(0, vp_size.call_count) self.assertEqual(0, vc_tenant.call_count) self.assertIsInstance(ex, Exception)