def _get_request_context(role='fake_role'): return request.Request( testing.create_environ(path="/", query_string="tenant_id=fake_tenant_id", headers={ "X_PROJECT_ID": "fake_project_id", "X_ROLES": role }))
def test_policy_validation_without_target(self): req = request.Request( self.create_environ(path='/', headers={ 'X_AUTH_TOKEN': '111', 'X_USER_ID': '222', 'X_PROJECT_ID': '333', })) self.assertEqual(True, req.can('example:allowed'))
def test_policy_validation_with_target(self): req = request.Request( self.create_environ(path='/', headers={ 'X_AUTH_TOKEN': '111', 'X_USER_ID': '222', 'X_PROJECT_ID': '333', })) target = {'project_id': req.project_id, 'user_id': req.user_id} self.assertEqual(True, req.can('example:allowed', target))
def test_default_limit(self, page_limit): 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(page_limit, req.limit)
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_valid_limit(self): expected_limit = 10 req = request.Request( testing.create_environ(path='/', query_string='limit=%d' % expected_limit, headers={ 'X_AUTH_TOKEN': '111', 'X_USER_ID': '222', 'X_PROJECT_ID': '333', 'X_ROLES': 'terminator,predator' })) self.assertEqual(expected_limit, req.limit)
def test_to_big_limit(self): req = request.Request( self.create_environ( path='/', headers={ 'X_AUTH_TOKEN': '111', 'X_USER_ID': '222', 'X_PROJECT_ID': '333', 'X_ROLES': 'terminator,predator' }, query_string='limit={}'.format(const.PAGE_LIMIT + 1), )) self.assertEqual(const.PAGE_LIMIT, req.limit)
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_invalid_limit(self): req = request.Request( testing.create_environ(path='/', query_string='limit=abc', headers={ 'X_AUTH_TOKEN': '111', 'X_USER_ID': '222', 'X_PROJECT_ID': '333', 'X_ROLES': 'terminator,predator' })) # note(trebskit) assertRaises fails to call property # so we need the actual function def property_wrapper(): return req.limit self.assertRaises(exceptions.HTTPUnprocessableEntityError, property_wrapper)
def test_validate_json_content_type(self): req = request.Request( testing.create_environ( headers={'Content-Type': 'application/json'})) helpers.validate_json_content_type(req)
def test_from_json_incorrect_message(self): req = request.Request( testing.create_environ(body='incorrect message', )) self.assertRaises(errors.HTTPBadRequest, helpers.from_json, req)
def test_from_json(self): body_json = {'test_body': 'test'} req = request.Request( testing.create_environ(body=rest_utils.as_json(body_json), )) response = helpers.from_json(req) self.assertEqual(body_json, response)
def test_validate_json_content_type_incorrect_content_type(self): req = request.Request( testing.create_environ( headers={'Content-Type': 'multipart/form-data'})) self.assertRaises(errors.HTTPBadRequest, helpers.validate_json_content_type, req)
def test_validate_json_content_type_missing_content_type(self): req = request.Request(testing.create_environ()) self.assertRaises(errors.HTTPBadRequest, helpers.validate_json_content_type, req)
def _get_request_context(role): return request.Request( testing.create_environ(path='/', headers={'X_ROLES': role}))