# Path: invenio_stats/contrib/event_builders.py
# def file_download_event_builder(event, sender_app, obj=None, **kwargs):
#     """Build a file-download event."""
#     event.update(dict(
#         # When:
#         timestamp=datetime.datetime.utcnow().isoformat(),
#         # What:
#         bucket_id=str(obj.bucket_id),
#         file_id=str(obj.file_id),
#         file_key=obj.key,
#         size=obj.file.size,
#         referrer=request.referrer,
#         # Who:
#         **get_user()
#     ))
#     return event
# 
# def record_view_event_builder(event, sender_app, pid=None, record=None,
#                               **kwargs):
#     """Build a record-view event."""
#     event.update(dict(
#         # When:
#         timestamp=datetime.datetime.utcnow().isoformat(),
#         # What:
#         record_id=str(record.id),
#         pid_type=pid.pid_type,
#         pid_value=str(pid.pid_value),
#         referrer=request.referrer,
#         # Who:
#         **get_user()
#     ))
#     return event
# 
# Path: invenio_stats/utils.py
# 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')
#     )


import datetime
from unittest.mock import patch
from invenio_stats.contrib.event_builders import file_download_event_builder, \
    record_view_event_builder
from invenio_stats.utils import get_user
    def utcnow(cls):
        return cls(2017, 1, 1)


headers = {'USER_AGENT':
           'Mozilla/5.0 (Windows NT 6.1; WOW64) '
           'AppleWebKit/537.36 (KHTML, like Gecko)'
           'Chrome/45.0.2454.101 Safari/537.36'}


def test_file_download_event_builder(app, mock_user_ctx,
                                     sequential_ids, objects):
    """Test the file-download event builder."""
    file_obj = objects[0]
    file_obj.bucket_id = sequential_ids[0]

    with app.test_request_context(headers=headers):
        event = {}
        with patch('datetime.datetime', NewDate):
            file_download_event_builder(event, app, file_obj)
        assert event == dict(
            # When:
            timestamp=NewDate.utcnow().isoformat(),
            # What:
            bucket_id=str(file_obj.bucket_id),
            file_id=str(file_obj.file_id),
            file_key=file_obj.key,
            size=file_obj.file.size,
            referrer=None,
            # Who:

# Path: invenio_stats/contrib/event_builders.py
# def file_download_event_builder(event, sender_app, obj=None, **kwargs):
#     """Build a file-download event."""
#     event.update(dict(
#         # When:
#         timestamp=datetime.datetime.utcnow().isoformat(),
#         # What:
#         bucket_id=str(obj.bucket_id),
#         file_id=str(obj.file_id),
#         file_key=obj.key,
#         size=obj.file.size,
#         referrer=request.referrer,
#         # Who:
#         **get_user()
#     ))
#     return event
# 
# def record_view_event_builder(event, sender_app, pid=None, record=None,
#                               **kwargs):
#     """Build a record-view event."""
#     event.update(dict(
#         # When:
#         timestamp=datetime.datetime.utcnow().isoformat(),
#         # What:
#         record_id=str(record.id),
#         pid_type=pid.pid_type,
#         pid_value=str(pid.pid_value),
#         referrer=request.referrer,
#         # Who:
#         **get_user()
#     ))
#     return event
# 
# Path: invenio_stats/utils.py
# 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')
#     )


# Path: tests/contrib/test_event_builders.py
import datetime
from unittest.mock import patch
from invenio_stats.contrib.event_builders import file_download_event_builder, \
    record_view_event_builder
from invenio_stats.utils import get_user

    def utcnow(cls):
        return cls(2017, 1, 1)


headers = {'USER_AGENT':
           'Mozilla/5.0 (Windows NT 6.1; WOW64) '
           'AppleWebKit/537.36 (KHTML, like Gecko)'
           'Chrome/45.0.2454.101 Safari/537.36'}


def test_file_download_event_builder(app, mock_user_ctx,
                                     sequential_ids, objects):
    """Test the file-download event builder."""
    file_obj = objects[0]
    file_obj.bucket_id = sequential_ids[0]

    with app.test_request_context(headers=headers):
        event = {}
        with patch('datetime.datetime', NewDate):
            file_download_event_builder(event, app, file_obj)
        assert event == dict(
            # When:
            timestamp=NewDate.utcnow().isoformat(),
            # What:
            bucket_id=str(file_obj.bucket_id),
            file_id=str(file_obj.file_id),
            file_key=file_obj.key,
            size=file_obj.file.size,
            referrer=None,
            # Who:

            **get_user()