Python 如何对使用HMAC Auth的烧瓶应用程序进行单元测试
我想测试HMAC Auth是否正在处理以下烧瓶“POST”功能:Python 如何对使用HMAC Auth的烧瓶应用程序进行单元测试,python,unit-testing,flask,hmac,Python,Unit Testing,Flask,Hmac,我想测试HMAC Auth是否正在处理以下烧瓶“POST”功能: @app.route('/v1/stt/analyze/jobs', methods=['POST']) @app.route('/v2/stt/analyze/jobs', methods=['POST']) @hmac_auth("create") def create_analyze_job(): req_data = request.get_json() request_id = ...
@app.route('/v1/stt/analyze/jobs', methods=['POST'])
@app.route('/v2/stt/analyze/jobs', methods=['POST'])
@hmac_auth("create")
def create_analyze_job():
req_data = request.get_json()
request_id = ...
res_obj = {'request_id': str(request_id), "ver": "1.02"}
resp = Response(json.dumps(res_obj), status=201, mimetype='application/json')
return resp
class ProducerTestCase(unittest.TestCase):
def setUp(self):
self.app = producer.app.test_client()
accountmgr = DictAccountBroker(accounts=test_accounts)
hmacmgr = HmacManager(accountmgr, self.app)
self.app.testing = True
self.app.debug = True
def test_hmac_post_jobs(self):
url_path = "/v1/stt/analyze/jobs"
url = BASE_URL + url_path
valid_data_analyze_job = "some json data"
key = "testclient1"
secret = "dfgdfx$"
# generating hmac headers
headers = genrate_auth_headers(key, secret, "POST", url_path,
payload=valid_data_analyze_job, use_bearer=False,
date_header_name="X-Ai-Date")
service_name_header = {
"X-Ai-Service-Name": "postcall"
}
content_type_header = {
'Content-Type': 'application/json; charset=utf-8'
}
headers.update(service_name_header)
headers.update(content_type_header)
response = self.app.post(url, data=json.dumps(valid_data_analyze_job), headers=headers)
当我发送如上所示的self.app.post时,我从服务器上得到500。但是如果我不发送auth头。像这样:
response = self.app.post(url, data=json.dumps(valid_data_analyze_job), content_type='application/json')
它会很好用的。但它并没有以这种方式测试HMAC。
我遗漏了什么?顺便说一句,如果我在应用程序运行时向其发送请求。不是通过self.client()而是作为常规请求。它工作得很好,所以标题本身就可以了