2012-12-19 15 views
6

Ich habe eine Flask-App, die Flask-Restless verwendet, um eine API zu bedienen.Wie kann ich diese Flask App testen?

Ich habe gerade einige Authentifizierung geschrieben, die

  1. prüft, ob der Verbraucher Host
  2. erkannt
  3. Die Anforderung enthält einen Hash (berechnet durch den Anforderungs-Inhalt für POST und URL-Verschlüsselung für mit einem geheimen API auszukommen Taste) und
  4. Der Hash ist gültig

ich für diese in der Lage sein, einige Unit-Tests zu schreiben, aber ich bin nicht sicher, wie denn meine Funktionen die Anfrage o verwenden Gegenstand. Sollte ich mich über das Anfrageobjekt lustig machen?

Ich würde gerne einige Tipps zu diesem Thema.

Config

API_CONSUMERS = [{'name': 'localhost', 
        'host': '12.0.0.1:5000', 
        'api_key': 'Ahth2ea5Ohngoop5'}, 
       {'name': 'localhost2', 
        'host': '127.0.0.1:5001', 
        'api_key': 'Ahth2ea5Ohngoop6'}] 

Authentifizierungsmethoden

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() 
+0

Haben Sie [Testing Flask Applications] (http://flask.pooco.org/docs/testing/) angeschaut? – sean

+1

Ich denke, Sie verwenden test_request_object() http://flask.pooco.org/docs/quickstart/#accessing-request-data – ninMonkey

+0

Ah, Sie können auf etwas da Affe sein, danke. –

Antwort

11

test_request_object() hat den Trick, dank Affe.

from flask import request 

with app.test_request_context('/hello', method='POST'): 
    # now you can do something with the request until the 
    # end of the with block, such as basic assertions: 
    assert request.path == '/hello' 
    assert request.method == 'POST' 
+1

Würden Sie bitte mehr Code schreiben? Es ist hilfreich zu sehen, wie dies in den Kontext des Unit-Test-Frameworks passt. – BoltzmannBrain

0

ich eine Nase-Test-Suite gemacht, dass meine Graffiti App getestet, indem nur die Methode aufrufen und die URL-Segment wie der Methodenparameter . d.h .:

response = self.app.do_something("/item/1234567890") 
assert response.status_code == 200