# Path: invenio_stats/utils.py
# def get_geoip(ip):
#     """Lookup country for IP address."""
#     reader = geolite2.reader()
#     ip_data = reader.get(ip) or {}
#     return ip_data.get('country', {}).get('iso_code')
# 
# def get_user():
#     """User information.
# 
#     .. note::
# 
#        **Privacy note** A users IP address, user agent string, and user id
#        (if logged in) is sent to a message queue, where it is stored for about
#        5 minutes. The information is used to:
# 
#        - Detect robot visits from the user agent string.
#        - Generate an anonymized visitor id (using a random salt per day).
#        - Detect the users host contry based on the IP address.
# 
#        The information is then discarded.
#     """
#     return dict(
#         ip_address=request.remote_addr,
#         user_agent=request.user_agent.string,
#         user_id=(
#             current_user.get_id() if current_user.is_authenticated else None
#         ),
#         session_id=session.get('sid_s')
#     )
# 
# def obj_or_import_string(value, default=None):
#     """Import string or return object.
# 
#     :params value: Import path or class object to instantiate.
#     :params default: Default object to return if the import fails.
#     :returns: The imported object.
#     """
#     if isinstance(value, six.string_types):
#         return import_string(value)
#     elif value:
#         return value
#     return default


from unittest.mock import patch
from invenio_stats.utils import get_geoip, get_user, obj_or_import_string
# -*- coding: utf-8 -*-
#
# This file is part of Invenio.
# Copyright (C) 2017-2018 CERN.
#
# Invenio is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.

"""Test utility functions."""




def myfunc():
    """Example function."""
    pass


def test_get_user(app, mock_users, request_headers):
    """Test the get_user function."""
    header = request_headers['user']
    with patch(
                'invenio_stats.utils.current_user',
                mock_users['authenticated']
            ), app.test_request_context(
                headers=header, environ_base={'REMOTE_ADDR': '142.0.0.1'}
            ):

# Path: invenio_stats/utils.py
# def get_geoip(ip):
#     """Lookup country for IP address."""
#     reader = geolite2.reader()
#     ip_data = reader.get(ip) or {}
#     return ip_data.get('country', {}).get('iso_code')
# 
# def get_user():
#     """User information.
# 
#     .. note::
# 
#        **Privacy note** A users IP address, user agent string, and user id
#        (if logged in) is sent to a message queue, where it is stored for about
#        5 minutes. The information is used to:
# 
#        - Detect robot visits from the user agent string.
#        - Generate an anonymized visitor id (using a random salt per day).
#        - Detect the users host contry based on the IP address.
# 
#        The information is then discarded.
#     """
#     return dict(
#         ip_address=request.remote_addr,
#         user_agent=request.user_agent.string,
#         user_id=(
#             current_user.get_id() if current_user.is_authenticated else None
#         ),
#         session_id=session.get('sid_s')
#     )
# 
# def obj_or_import_string(value, default=None):
#     """Import string or return object.
# 
#     :params value: Import path or class object to instantiate.
#     :params default: Default object to return if the import fails.
#     :returns: The imported object.
#     """
#     if isinstance(value, six.string_types):
#         return import_string(value)
#     elif value:
#         return value
#     return default


# Path: tests/test_utils.py
from unittest.mock import patch
from invenio_stats.utils import get_geoip, get_user, obj_or_import_string

# -*- coding: utf-8 -*-
#
# This file is part of Invenio.
# Copyright (C) 2017-2018 CERN.
#
# Invenio is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.

"""Test utility functions."""




def myfunc():
    """Example function."""
    pass


def test_get_user(app, mock_users, request_headers):
    """Test the get_user function."""
    header = request_headers['user']
    with patch(
                'invenio_stats.utils.current_user',
                mock_users['authenticated']
            ), app.test_request_context(
                headers=header, environ_base={'REMOTE_ADDR': '142.0.0.1'}
            ):

        user = get_user()