def test_the_change_token_revoking_method_of_user_repository(app):
    """
  GIVEN the TokenRepository instance
  WHEN the save() method is call
  THEN check session method calls and the token revoke value
  """

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

    token = Token()
    token.id = 1
    token.jti = "27d1b1a3-45b4-4a5f-83ed-b823f5ea1dbe"
    token.token_type = "access"
    token.user_identity = "test"
    token.revoked = False
    token.expires = datetime.now()

    token_repository.session.add(token)
    token_repository.session.commit()

    token_repository.change_token_revoking(1, "test", True)
    (token_repository.session.query.return_value.filter_by.
     assert_called_once_with(id=1, user_identity="test"))
    assert token.revoked == True
def test_the_change_token_revoking_method_of_user_repository_with_inexistent_token(
        app):
    """
  GIVEN the TokenRepository instance
  WHEN the save() method is call with inexistent_token
  THEN check TokenNotFound exception throwing
  """

    from app.model import TokenRepository, Token
    from app.exceptions import TokenNotFound

    token_repository = TokenRepository()
    token_repository.session = UnifiedAlchemyMagicMock()

    with pytest.raises(TokenNotFound):
        token_repository.change_token_revoking(100, 'test', True)
def modify_token(token_id: int):
    """Modifies the revocation status of a token.

    Parameters:
        token_id (int): Token ID to be changed.

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

    if not request.is_json:
        abort(400)

    revoke = request.json.get('revoke', None)
    if revoke is None or not isinstance(revoke, bool):
        abort(400)

    # Revoke or unrevoke the token based on what was passed to this function
    user_identity = get_jwt_identity()
    try:
        token_repository = TokenRepository()
        if revoke:
            token_repository.change_token_revoking(token_id, user_identity,
                                                   True)
            return make_response(
                jsonify({
                    'status': 'success',
                    'message': 'Token revoked'
                }), 200)
        else:
            token_repository.change_token_revoking(token_id, user_identity,
                                                   False)
            return make_response(
                jsonify({
                    'status': 'success',
                    'message': 'Token unrevoked'
                }), 200)
    except TokenNotFound:
        return make_response(
            jsonify({
                'status': 'fail',
                'message': 'The specified token was not found'
            }), 404)