Unittesting authentication layout

I wanted to make fun of the validate_tokendecorator when writing unit test for one of the views

#views.py
from third_part.module import vaidate_token
from setting import config
class myViews:
     @validate_token([config['issuer'], config['secret_key']])
     def get_data():
         #Do stuff
         return json.loads(data)

Here validate_token is the thirtd_party module for authorizing the request, and the token is issued by a third party, so I do not want to validate_token authentication for my tests

below is my sample test code.

test_views.py

@patch('views.validate_token', lambda x: x)
def test_get_data(self):
    endpoint = '/app/get_data'
    res = self.client.get(endpoint)
    assert res.status_code==200

I tried to mock while running the tests
But it does not work as expected and gives error 401.

how can I mock / fix the decorator for tests nothing is missing here

Thanks in advance.

+4
source share
1 answer

Here is an example that might help you. The file structure is below.

app.py

from flask import Flask
from third_part.example import validate_token

app = Flask(__name__)

@app.route('/')
@validate_token()
def index():
    return 'hi'

if __name__ == '__main__':
    app.run(debug=True)

/third_part/example.py

from functools import wraps

def validate_token():
    def validate(func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            raise Exception('Token error. Just for example')
            return func(*args, **kwargs)
        return wrapper
    return validate

tests.py:

from app import app

app.testing = True

def test_index():
    with app.test_client() as client:
        client.get('/')

tests.py ( , ):

    @wraps(func)
    def wrapper(*args, **kwargs):
>       raise Exception('Token error. Just for example')
E       Exception: Token error. Just for example

, ( patch). tests.py:

from functools import wraps
from mock import patch

def mock_decorator():
    def decorator(f):
        @wraps(f)
        def decorated_function(*args, **kwargs):
            return f(*args, **kwargs)
        return decorated_function
    return decorator

patch('third_part.example.validate_token', mock_decorator).start()
# !important thing - import of app after patch()
from app import app

app.testing = True

def test_index():
    with app.test_client() as client:
        client.get('/')

( patch). tests.py:

from functools import wraps
from third_part import example

def mock_decorator():
    def decorator(f):
        @wraps(f)
        def decorated_function(*args, **kwargs):
            return f(*args, **kwargs)
        return decorated_function
    return decorator
# !important thing - import of app after replace
example.validate_token = mock_decorator

from app import app
app.testing = True

def test_index():
    with app.test_client() as client:
        client.get('/')

test.py( ):

tests.py .                                                               [100%]

=========================== 1 passed in 0.09 seconds ===========================

. , , . , validate_token views, third_part.module

, .

+2

Source: https://habr.com/ru/post/1691089/


All Articles