<commit_before>"""
All tests regarding channel.py
"""

from django.test import TestCase
from django.test.utils import override_settings
from django.core.exceptions import ImproperlyConfigured

from edx_notifications.channels.channel import (
    get_notification_channel,
    reset_notification_channels,
    BaseNotificationChannelProvider,
)

from edx_notifications.channels.durable import BaseDurableNotificationChannel

from edx_notifications.data import (
    NotificationType,
)

# list all known channel providers
_NOTIFICATION_CHANNEL_PROVIDERS = {
    'default': {
        'class': 'edx_notifications.channels.durable.BaseDurableNotificationChannel',
        'options': {
            'display_name': 'channel_default',
            'display_description': 'channel_description_default',
        }
    },
    'channel1': {
        'class': 'edx_notifications.channels.durable.BaseDurableNotificationChannel',
        'options': {
            'display_name': 'channel_name1',
            'display_description': 'channel_description1',
        }
    },
    'channel2': {
        'class': 'edx_notifications.channels.durable.BaseDurableNotificationChannel',
        'options': {
            'display_name': 'channel_name2',
            'display_description': 'channel_description2',
        }
    },
    'channel3': {
        'class': 'edx_notifications.channels.durable.BaseDurableNotificationChannel',
        'options': {
            'display_name': 'channel_name3',
            'display_description': 'channel_description3',
        }
    },
    'channel4': {
        'class': 'edx_notifications.channels.durable.BaseDurableNotificationChannel',
        'options': {
            'display_name': 'channel_name4',
            'display_description': 'channel_description4',
        }
    },
    'channel5': {
        'class': 'edx_notifications.channels.durable.BaseDurableNotificationChannel',
        'options': {
            'display_name': 'channel_name5',
            'display_description': 'channel_description5',
        }
    },
    'channel6': {
        'class': 'edx_notifications.channels.durable.BaseDurableNotificationChannel',
        'options': {
            'display_name': 'channel_name6',
            'display_description': 'channel_description6',
        }
    },
}

# list all of the mappings of notification types to channel
_NOTIFICATION_CHANNEL_PROVIDER_TYPE_MAPS = {
    '*': 'default',  # default global mapping
    'edx-edx_notifications.*': 'channel1',
    'edx-edx_notifications.channels.*': 'channel2',
    'edx-edx_notifications.channels.tests.*': 'channel3',
    'edx-edx_notifications.channels.tests.test_channel.*': 'channel4',
    'edx-edx_notifications.channels.tests.test_channel.channeltests.*': 'channel5',
    'edx-edx_notifications.channels.tests.test_channel.channeltests.foo': 'channel6'
}


@override_settings(NOTIFICATION_CHANNEL_PROVIDERS=_NOTIFICATION_CHANNEL_PROVIDERS)
@override_settings(NOTIFICATION_CHANNEL_PROVIDER_TYPE_MAPS=_NOTIFICATION_CHANNEL_PROVIDER_TYPE_MAPS)
class ChannelTests(TestCase):
    """
    Tests for channel.py
    """

    def setUp(self):
        """
        Harnessing
        """
        reset_notification_channels()
        self.test_user_id = 1001  # an arbitrary user_id
        self.test_msg_type = NotificationType(
            name='edx-edx_notifications.channels.tests.test_channel.channeltests.foo'
        )

    def test_cannot_create_instance(self):
        """
        BaseNotificationChannelProvider is an abstract class and we should not be able
        to create an instance of it
        """

        with self.assertRaises(TypeError):
            BaseNotificationChannelProvider()  # pylint: disable=abstract-class-instantiated

    def test_get_provider(self):
        """
        Makes sure we get an instance of the registered store provider
        """

        provider = get_notification_channel(self.test_user_id, self.test_msg_type)

        self.assertIsNotNone(provider)
        self.assertTrue(isinstance(provider, BaseDurableNotificationChannel))

        self.assertEqual(provider.name, 'channel6')
        self.assertEqual(provider.display_name, 'channel_name6')
        self.assertEqual(provider.display_description, 'channel_description6')

        # now verify that the wildcard hierarchy rules
        # work, by making a msg_type name which will match one of
        # the intermediate items in the hierarchy

        provider = get_notification_channel(
            self.test_user_id,
            NotificationType(
                name='edx-edx_notifications.channels.tests.another_one'
            )
        )

        self.assertEqual(provider.name, 'channel3')
        self.assertEqual(provider.display_name, 'channel_name3')
        self.assertEqual(provider.display_description, 'channel_description3')

        provider = get_notification_channel(
            self.test_user_id,
            NotificationType(
                name='edx-edx_notifications.channels.diff_subpath.diff_leaf'
            )
        )

        self.assertEqual(provider.name, 'channel2')
        self.assertEqual(provider.display_name, 'channel_name2')
        self.assertEqual(provider.display_description, 'channel_description2')

    @override_settings(NOTIFICATION_CHANNEL_PROVIDERS=None)
    def test_missing_provider_config(self):
        """
        Make sure we are throwing exceptions on poor channel configuration
        """

        with self.assertRaises(ImproperlyConfigured):
            get_notification_channel(self.test_user_id, self.test_msg_type)

    @override_settings(NOTIFICATION_CHANNEL_PROVIDER_TYPE_MAPS=None)
    def test_missing_maps_config(self):
        """
        Make sure we are throwing exceptions on poor channel mappings configuration
        """

        with self.assertRaises(ImproperlyConfigured):
            get_notification_channel(self.test_user_id, self.test_msg_type)

    @override_settings(NOTIFICATION_CHANNEL_PROVIDER_TYPE_MAPS={'edx-edx_notifications.bogus': 'bogus'})
    def test_missing_global_mapping(self):
        """
        Make sure we are throwing exceptions when global mapping is missing
        """

        with self.assertRaises(ImproperlyConfigured):
            get_notification_channel(self.test_user_id, self.test_msg_type)

    @override_settings(NOTIFICATION_CHANNEL_PROVIDER_TYPE_MAPS={'*': 'bogus'})
    def test_bad_mapping(self):<commit_msg>fix names in channel tests<commit_after>"""
All tests regarding channel.py
"""

from django.test import TestCase
from django.test.utils import override_settings
from django.core.exceptions import ImproperlyConfigured

from edx_notifications.channels.channel import (
    get_notification_channel,
    reset_notification_channels,
    BaseNotificationChannelProvider,
)

from edx_notifications.channels.durable import BaseDurableNotificationChannel

from edx_notifications.data import (
    NotificationType,
)

# list all known channel providers
_NOTIFICATION_CHANNEL_PROVIDERS = {
    'default': {
        'class': 'edx_notifications.channels.durable.BaseDurableNotificationChannel',
        'options': {
            'display_name': 'channel_default',
            'display_description': 'channel_description_default',
        }
    },
    'channel1': {
        'class': 'edx_notifications.channels.durable.BaseDurableNotificationChannel',
        'options': {
            'display_name': 'channel_name1',
            'display_description': 'channel_description1',
        }
    },
    'channel2': {
        'class': 'edx_notifications.channels.durable.BaseDurableNotificationChannel',
        'options': {
            'display_name': 'channel_name2',
            'display_description': 'channel_description2',
        }
    },
    'channel3': {
        'class': 'edx_notifications.channels.durable.BaseDurableNotificationChannel',
        'options': {
            'display_name': 'channel_name3',
            'display_description': 'channel_description3',
        }
    },
    'channel4': {
        'class': 'edx_notifications.channels.durable.BaseDurableNotificationChannel',
        'options': {
            'display_name': 'channel_name4',
            'display_description': 'channel_description4',
        }
    },
    'channel5': {
        'class': 'edx_notifications.channels.durable.BaseDurableNotificationChannel',
        'options': {
            'display_name': 'channel_name5',
            'display_description': 'channel_description5',
        }
    },
    'channel6': {
        'class': 'edx_notifications.channels.durable.BaseDurableNotificationChannel',
        'options': {
            'display_name': 'channel_name6',
            'display_description': 'channel_description6',
        }
    },
}

# list all of the mappings of notification types to channel
_NOTIFICATION_CHANNEL_PROVIDER_TYPE_MAPS = {
    '*': 'default',  # default global mapping
    'edx_notifications.*': 'channel1',
    'edx_notifications.channels.*': 'channel2',
    'edx_notifications.channels.tests.*': 'channel3',
    'edx_notifications.channels.tests.test_channel.*': 'channel4',
    'edx_notifications.channels.tests.test_channel.channeltests.*': 'channel5',
    'edx_notifications.channels.tests.test_channel.channeltests.foo': 'channel6'
}


@override_settings(NOTIFICATION_CHANNEL_PROVIDERS=_NOTIFICATION_CHANNEL_PROVIDERS)
@override_settings(NOTIFICATION_CHANNEL_PROVIDER_TYPE_MAPS=_NOTIFICATION_CHANNEL_PROVIDER_TYPE_MAPS)
class ChannelTests(TestCase):
    """
    Tests for channel.py
    """

    def setUp(self):
        """
        Harnessing
        """
        reset_notification_channels()
        self.test_user_id = 1001  # an arbitrary user_id
        self.test_msg_type = NotificationType(
            name='edx_notifications.channels.tests.test_channel.channeltests.foo'
        )

    def test_cannot_create_instance(self):
        """
        BaseNotificationChannelProvider is an abstract class and we should not be able
        to create an instance of it
        """

        with self.assertRaises(TypeError):
            BaseNotificationChannelProvider()  # pylint: disable=abstract-class-instantiated

    def test_get_provider(self):
        """
        Makes sure we get an instance of the registered store provider
        """

        provider = get_notification_channel(self.test_user_id, self.test_msg_type)

        self.assertIsNotNone(provider)
        self.assertTrue(isinstance(provider, BaseDurableNotificationChannel))

        self.assertEqual(provider.name, 'channel6')
        self.assertEqual(provider.display_name, 'channel_name6')
        self.assertEqual(provider.display_description, 'channel_description6')

        # now verify that the wildcard hierarchy rules
        # work, by making a msg_type name which will match one of
        # the intermediate items in the hierarchy

        provider = get_notification_channel(
            self.test_user_id,
            NotificationType(
                name='edx_notifications.channels.tests.another_one'
            )
        )

        self.assertEqual(provider.name, 'channel3')
        self.assertEqual(provider.display_name, 'channel_name3')
        self.assertEqual(provider.display_description, 'channel_description3')

        provider = get_notification_channel(
            self.test_user_id,
            NotificationType(
                name='edx_notifications.channels.diff_subpath.diff_leaf'
            )
        )

        self.assertEqual(provider.name, 'channel2')
        self.assertEqual(provider.display_name, 'channel_name2')
        self.assertEqual(provider.display_description, 'channel_description2')

    @override_settings(NOTIFICATION_CHANNEL_PROVIDERS=None)
    def test_missing_provider_config(self):
        """
        Make sure we are throwing exceptions on poor channel configuration
        """

        with self.assertRaises(ImproperlyConfigured):
            get_notification_channel(self.test_user_id, self.test_msg_type)

    @override_settings(NOTIFICATION_CHANNEL_PROVIDER_TYPE_MAPS=None)
    def test_missing_maps_config(self):
        """
        Make sure we are throwing exceptions on poor channel mappings configuration
        """

        with self.assertRaises(ImproperlyConfigured):
            get_notification_channel(self.test_user_id, self.test_msg_type)

    @override_settings(NOTIFICATION_CHANNEL_PROVIDER_TYPE_MAPS={'edx_notifications.bogus': 'bogus'})
    def test_missing_global_mapping(self):
        """
        Make sure we are throwing exceptions when global mapping is missing
        """

        with self.assertRaises(ImproperlyConfigured):
            get_notification_channel(self.test_user_id, self.test_msg_type)

    @override_settings(NOTIFICATION_CHANNEL_PROVIDER_TYPE_MAPS={'*': 'bogus'})
    def test_bad_mapping(self):
{'fraction_non_alphanumeric': 0.09418196017180788, 'fraction_numerical': 0.005778992581023038, 'mean_word_length': 3.1918166939443533, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3826164', 'n_tokens_mistral': 3599, 'n_tokens_neox': 3560, 'n_words': 587}