def auth(app, request):
    """Creates HTTP authorization header.

    Parameters:    
        app (flask.app.Flask): The application instance.
        request (FixtureRequest): A request for a fixture from a test or fixture function

    Returns:
       headers: a dictionary with HTTP authorization header for a basic authentication
    """

    from flask_jwt_extended import (
        jwt_required, create_access_token, create_refresh_token
    )
    from app.model import TokenRepository

    access_token_encoded = create_access_token(identity='test')
    refresh_token_encoded = create_refresh_token(identity='test')

    token_repository = TokenRepository()
    token_repository.save(access_token_encoded, app.config["JWT_IDENTITY_CLAIM"])
    token_repository.save(refresh_token_encoded, app.config["JWT_IDENTITY_CLAIM"])

    headers = {
        'access_token': {'Authorization': 'Bearer ' + access_token_encoded},
        'refresh_token': {'Authorization': 'Bearer ' + refresh_token_encoded},
    }        

    return headers
def login() -> Response:
    """Login of the user by creating a valid token.

    Returns:
        response: flask.Response object with the application/json mimetype.
    """

    data = request.json

    # checkint the json data
    if not request.is_json or not data.get('username') or not data.get(
            'password'):
        abort(400)

    # authenticating the user
    user = UserRepository().authenticate(data.get('username'),
                                         data.get('password'))
    if not user:
        response = make_response(
            jsonify({
                'status': 'fail',
                'message': 'Username or Password not valid'
            }), 401)

    else:
        access_token_encoded = create_access_token(identity=user.username)
        refresh_token_encoded = create_refresh_token(identity=user.username)

        token_repository = TokenRepository()
        token_repository.save(access_token_encoded,
                              current_app.config["JWT_IDENTITY_CLAIM"])
        token_repository.save(refresh_token_encoded,
                              current_app.config["JWT_IDENTITY_CLAIM"])

        response = make_response(
            jsonify({
                'status': 'success',
                'data': {
                    'access_token': access_token_encoded,
                    'refresh_token': refresh_token_encoded
                }
            }), 200)

    return response
def refresh():
    """Create a new access token.

    Returns:
        response: flask.Response object with the application/json mimetype.
    """

    current_user = get_jwt_identity()
    access_token_encoded = create_access_token(identity=current_user)

    token_repository = TokenRepository()
    token_repository.save(access_token_encoded,
                          current_app.config["JWT_IDENTITY_CLAIM"])

    return make_response(
        jsonify({
            'status': 'success',
            'data': {
                'access_token': access_token_encoded
            }
        }), 200)
def test_the_save_method_of_user_repository(app):
    """
  GIVEN the TokenRepository instance
  WHEN the save() method is call
  THEN check session method calls and the token returned
  """

    from app.model import TokenRepository, Token
    token_repository = TokenRepository()
    token_repository.session = UnifiedAlchemyMagicMock()

    access_token_encoded = create_access_token("test")
    token = token_repository.save(access_token_encoded,
                                  app.config["JWT_IDENTITY_CLAIM"])

    token_repository.session.add.assert_called_once_with(token)
    token_repository.session.commit.assert_called_once_with()
    assert token.user_identity == 'test'