<commit_before># -*- coding: utf-8 -*-
# See http://zulip.readthedocs.io/en/latest/events-system.html for
# high-level documentation on how this system works.
from __future__ import absolute_import
from __future__ import print_function
from typing import Any, Callable, Dict, List, Optional, Union, Text, Tuple

from django.conf import settings
from django.http import HttpRequest, HttpResponse
from django.test import TestCase
from django.utils.timezone import now as timezone_now

from zerver.models import (
    get_client, get_realm, get_recipient, get_stream, get_user,
    Message, RealmDomain, Recipient, UserMessage, UserPresence, UserProfile,
    Realm,
)

from zerver.lib.actions import (
    bulk_add_subscriptions,
    bulk_remove_subscriptions,
    do_add_alert_words,
    check_add_realm_emoji,
    check_send_typing_notification,
    notify_realm_custom_profile_fields,
    do_add_realm_filter,
    do_add_reaction,
    do_remove_reaction,
    do_change_avatar_fields,
    do_change_default_all_public_streams,
    do_change_default_events_register_stream,
    do_change_default_sending_stream,
    do_change_full_name,
    do_change_bot_owner,
    do_change_is_admin,
    do_change_stream_description,
    do_change_subscription_property,
    do_create_user,
    do_deactivate_stream,
    do_deactivate_user,
    do_mark_hotspot_as_read,
    do_reactivate_user,
    do_refer_friend,
    do_regenerate_api_key,
    do_remove_alert_words,
    do_remove_realm_emoji,
    do_remove_realm_filter,
    do_rename_stream,
    do_add_default_stream,
    do_remove_default_stream,
    do_set_muted_topics,
    do_set_realm_property,
    do_set_realm_authentication_methods,
    do_set_realm_message_editing,
    do_update_embedded_data,
    do_update_message,
    do_update_message_flags,
    do_update_muted_topic,
    do_update_pointer,
    do_update_user_presence,
    do_set_user_display_setting,
    do_change_notification_settings,
    do_add_realm_domain,
    do_change_realm_domain,
    do_remove_realm_domain,
    do_change_icon_source,
    log_event,
)
from zerver.lib.events import (
    apply_events,
    fetch_initial_state_data,
)
from zerver.lib.message import render_markdown
from zerver.lib.test_helpers import POSTRequestMock, get_subscription
from zerver.lib.test_classes import (
    ZulipTestCase,
)
from zerver.lib.validator import (
    check_bool, check_dict, check_dict_only, check_float, check_int, check_list, check_string,
    equals, check_none_or, Validator
)

from zerver.views.events_register import _default_all_public_streams, _default_narrow

from zerver.tornado.event_queue import allocate_client_descriptor, EventQueue
from zerver.tornado.views import get_events_backend

from collections import OrderedDict
import mock
import time
import ujson
from six.moves import range


class LogEventsTest(ZulipTestCase):
    def test_log_events(self):
        with self.settings(EVENT_LOG_DIR=None):
            log_event(None)


class EventsEndpointTest(ZulipTestCase):
    def test_events_register_endpoint(self):
        # type: () -> None

        # This test is intended to get minimal coverage on the
        # events_register code paths
        email = self.example_email("hamlet")
        with mock.patch('zerver.views.events_register.do_events_register', return_value={}):<commit_msg>Test that `log_event` auto creates missing log dir.<commit_after># -*- coding: utf-8 -*-
# See http://zulip.readthedocs.io/en/latest/events-system.html for
# high-level documentation on how this system works.
from __future__ import absolute_import
from __future__ import print_function
from typing import Any, Callable, Dict, List, Optional, Union, Text, Tuple
import os
import shutil

from django.conf import settings
from django.http import HttpRequest, HttpResponse
from django.test import TestCase
from django.utils.timezone import now as timezone_now

from zerver.models import (
    get_client, get_realm, get_recipient, get_stream, get_user,
    Message, RealmDomain, Recipient, UserMessage, UserPresence, UserProfile,
    Realm,
)

from zerver.lib.actions import (
    bulk_add_subscriptions,
    bulk_remove_subscriptions,
    do_add_alert_words,
    check_add_realm_emoji,
    check_send_typing_notification,
    notify_realm_custom_profile_fields,
    do_add_realm_filter,
    do_add_reaction,
    do_remove_reaction,
    do_change_avatar_fields,
    do_change_default_all_public_streams,
    do_change_default_events_register_stream,
    do_change_default_sending_stream,
    do_change_full_name,
    do_change_bot_owner,
    do_change_is_admin,
    do_change_stream_description,
    do_change_subscription_property,
    do_create_user,
    do_deactivate_stream,
    do_deactivate_user,
    do_mark_hotspot_as_read,
    do_reactivate_user,
    do_refer_friend,
    do_regenerate_api_key,
    do_remove_alert_words,
    do_remove_realm_emoji,
    do_remove_realm_filter,
    do_rename_stream,
    do_add_default_stream,
    do_remove_default_stream,
    do_set_muted_topics,
    do_set_realm_property,
    do_set_realm_authentication_methods,
    do_set_realm_message_editing,
    do_update_embedded_data,
    do_update_message,
    do_update_message_flags,
    do_update_muted_topic,
    do_update_pointer,
    do_update_user_presence,
    do_set_user_display_setting,
    do_change_notification_settings,
    do_add_realm_domain,
    do_change_realm_domain,
    do_remove_realm_domain,
    do_change_icon_source,
    log_event,
)
from zerver.lib.events import (
    apply_events,
    fetch_initial_state_data,
)
from zerver.lib.message import render_markdown
from zerver.lib.test_helpers import POSTRequestMock, get_subscription
from zerver.lib.test_classes import (
    ZulipTestCase,
)
from zerver.lib.validator import (
    check_bool, check_dict, check_dict_only, check_float, check_int, check_list, check_string,
    equals, check_none_or, Validator
)

from zerver.views.events_register import _default_all_public_streams, _default_narrow

from zerver.tornado.event_queue import allocate_client_descriptor, EventQueue
from zerver.tornado.views import get_events_backend

from collections import OrderedDict
import mock
import time
import ujson
from six.moves import range


class LogEventsTest(ZulipTestCase):
    def test_with_missing_event_log_dir_setting(self):
        # type: () -> None
        with self.settings(EVENT_LOG_DIR=None):
            log_event(None)

    def test_log_event_mkdir(self):
        # type: () -> None
        dir_name = 'var/test-log-dir'

        try:
            shutil.rmtree(dir_name)
        except OSError:
            # assume it doesn't exist already
            pass

        self.assertFalse(os.path.exists(dir_name))
        with self.settings(EVENT_LOG_DIR=dir_name):
            event = {}  # type: Dict[str, int]
            log_event(event)
        self.assertTrue(os.path.exists(dir_name))


class EventsEndpointTest(ZulipTestCase):
    def test_events_register_endpoint(self):
        # type: () -> None

        # This test is intended to get minimal coverage on the
        # events_register code paths
        email = self.example_email("hamlet")
        with mock.patch('zerver.views.events_register.do_events_register', return_value={}):
{'fraction_non_alphanumeric': 0.060823887199336464, 'fraction_numerical': 0.0002764722145424385, 'mean_word_length': 4.359259259259259, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3598868', 'n_tokens_mistral': 2390, 'n_tokens_neox': 2356, 'n_words': 451}