데코레이터 authorized()
을 정의하는 파이썬 모듈 security.py
이 있습니다.플라스크 요청에 패치하여 파이썬 패키지의 데코레이터에 대한 단위 테스트
데코레이터를 테스트하고 싶습니다. 데코레이터는 플라스크 요청 헤더를 수신합니다. 나는 내가 쓴 decorator.The 시험이 같은 STH하는 @patch
사용하여 플라스크 요청 헤더를 조롱 할
def authorized():
def _authorized(wrapped_func):
def _wrap(*args, **kwargs):
if 'token' not in request.headers:
LOG.warning("warning")
abort(401)
return None
return wrapped_func(*args, **kwargs)
return _wrap
return _authorized
: 데코레이터는 다음과 같이 s 번째입니다
@patch('security.request.headers', Mock(side_effect=lambda *args, **kwargs: MockHeaders({})))
def test_no_authorization_token_in_header(self):
@security.authorized()
def decorated_func(token='abc'):
return access_token
result = decorated_func()
self.assertEqual(result, None)
class MockHeaders(object):
def __init__(self, json_data):
self.json_data=json_data
하지만 난 항상 다음을 얻을 오류 :
name = 'request'
def _lookup_req_object(name):
top = _request_ctx_stack.top
if top is None:
raise RuntimeError(_request_ctx_err_msg)
RuntimeError: Working outside of request context.
This typically means that you attempted to use functionality that needed
an active HTTP request. Consult the documentation on testing for
information about how to avoid this problem.
어떻게해야합니까?
@MartijnPieters 네 클래스가 이후에 권리 또한 메타 데이터 다른 장식이 제대로 전달되어 추가 할 수 있도록
functools.wraps()
를 사용한다 패치로 꾸며진 방법 – yellowbowlmigration죄송합니다. Mea Culpa. –