मेरे पास एक फ्लास्क ऐप है जो एपीआई की सेवा के लिए फ्लास्क-रेस्टलेस का उपयोग कर रहा है।मैं इस फ्लास्क ऐप का परीक्षण कैसे कर सकता हूं?
मैं बस कुछ प्रमाणीकरण कि जाँच करता है
- उपभोक्ताओं मेजबान
- मान्यता प्राप्त के लिए एक गुप्त एपीआई के साथ मिलता अनुरोध हैश (पोस्ट और URL के अनुरोध अवयवों को एनक्रिप्ट द्वारा गणना में शामिल किया जाता है तो लिखा है कुंजी) और
- हैश मान्य है
मैं इसके लिए कुछ इकाई परीक्षण लिखने में सक्षम होना चाहता हूँ, लेकिन मुझे यकीन है कि कैसे क्योंकि मेरे कार्यों अनुरोध ओ का उपयोग नहीं कर रहा हूँ bject। क्या मुझे अनुरोध वस्तु का मज़ाक उड़ाया जाना चाहिए?
इस पर कुछ सलाह पसंद आएगी।
कॉन्फ़िग
API_CONSUMERS = [{'name': 'localhost',
'host': '12.0.0.1:5000',
'api_key': 'Ahth2ea5Ohngoop5'},
{'name': 'localhost2',
'host': '127.0.0.1:5001',
'api_key': 'Ahth2ea5Ohngoop6'}]
प्रमाणीकरण की विधियां
import hashlib
from flask import request
def is_authenticated(app):
"""
Checks that the consumers host is valid, the request has a hash and the
hash is the same when we excrypt the data with that hosts api key
Arguments:
app -- instance of the application
"""
consumers = app.config.get('API_CONSUMERS')
host = request.host
try:
api_key = next(d['api_key'] for d in consumers if d['host'] == host)
except StopIteration:
app.logger.info('Authentication failed: Unknown Host (' + host + ')')
return False
if not request.headers.get('hash'):
app.logger.info('Authentication failed: Missing Hash (' + host + ')')
return False
if request.method == 'GET':
hash = calculate_hash_from_url(api_key)
elif request.method == 'POST':
hash = calculate_hash_from_content(api_key)
if hash != request.headers.get('hash'):
app.logger.info('Authentication failed: Hash Mismatch (' + host + ')')
return False
return True
def calculate_hash_from_url(api_key):
"""
Calculates the hash using the url and that hosts api key
Arguments:
api_key -- api key for this host
"""
data_to_hash = request.base_url + '?' + request.query_string
data_to_hash += api_key
return hashlib.sha1(request_uri).hexdigest()
def calculate_hash_from_content(api_key):
"""
Calculates the hash using the request data and that hosts api key
Arguments:
api_key -- api key for this host
"""
data_to_hash = request.data
data_to_hash += api_key
return hashlib.sha1(data_to_hash).hexdigest()
क्या आपने [टेस्टिंग फ्लास्क एप्लिकेशन] (http://flask.pocoo.org/docs/testing/) देखा है? – sean
मुझे लगता है कि आप test_request_object() http://flask.pocoo.org/docs/quickstart/#accessing-request-data – ninMonkey
आह का उपयोग करते हैं, आह, आप वहां बंदर पर कुछ हो सकते हैं, धन्यवाद। –