<commit_before>import pickle
from unittest.mock import MagicMock

from assertpy import assert_that
from ereuse_devicehub.resources.event.device.live.hooks import save_ip
from ereuse_devicehub.tests.test_resources.test_events import TestEventWithPredefinedDevices


class TestDeviceHubLive(TestEventWithPredefinedDevices):
    def setUp(self, settings_file=None, url_converters=None):
        super().setUp(settings_file, url_converters)
        self.app.config['MAXMIND_ACCOUNT'] = {
            'user': 'example',
            'license key': 'example'
        }
        self.client = self.app.geoip.client

    def test_init(self):
        """
        The app initializes correctly.
        """
        # 'Live' event should be registered in domain
        assert_that(self.domain).contains('devices_live')
        assert_that(self.domain).is_type_of(dict)
        assert_that(self.app.on_insert_devices_live.targets).contains(save_ip)

    def test_live(self):
        """
        The app receives a POST Live and it processes it correctly, geocoding the result, creating a Live event and
        setting it to the right device.
        """
        # Let's mock insights of geoip so it doesn't call to Maxmind
        fixture = self.get_fixture('live', 'live_insights', parse_json=False, extension='pickle', mode='rb')
        return_insights = pickle.loads(fixture)
        self.client.insights = MagicMock(return_value=return_insights)
        post = {'device': '1', '@type': 'devices:Live'}
        result = self.post_and_check('{}/live'.format(self.DEVICE_EVENT), post)
        # Let's check the registered event.
        live = self.get_and_check(self.EVENTS, '', result['_id'])
        assert_that(self.get_fixture('live', 'event')).is_subset_of(live)<commit_msg>Remove duplicated maxmind account config from TestDeviceHubLive<commit_after>import pickle
from unittest.mock import MagicMock

from assertpy import assert_that
from ereuse_devicehub.resources.event.device.live.hooks import save_ip
from ereuse_devicehub.tests.test_resources.test_events import TestEventWithPredefinedDevices


class TestDeviceHubLive(TestEventWithPredefinedDevices):
    def setUp(self, settings_file=None, url_converters=None):
        super().setUp(settings_file, url_converters)
        self.client = self.app.geoip.client

    def test_init(self):
        """
        The app initializes correctly.
        """
        # 'Live' event should be registered in domain
        assert_that(self.domain).contains('devices_live')
        assert_that(self.domain).is_type_of(dict)
        assert_that(self.app.on_insert_devices_live.targets).contains(save_ip)

    def test_live(self):
        """
        The app receives a POST Live and it processes it correctly, geocoding the result, creating a Live event and
        setting it to the right device.
        """
        # Let's mock insights of geoip so it doesn't call to Maxmind
        fixture = self.get_fixture('live', 'live_insights', parse_json=False, extension='pickle', mode='rb')
        return_insights = pickle.loads(fixture)
        self.client.insights = MagicMock(return_value=return_insights)
        post = {'device': '1', '@type': 'devices:Live'}
        result = self.post_and_check('{}/live'.format(self.DEVICE_EVENT), post)
        # Let's check the registered event.
        live = self.get_and_check(self.EVENTS, '', result['_id'])
        assert_that(self.get_fixture('live', 'event')).is_subset_of(live)
{'fraction_non_alphanumeric': 0.10447330447330447, 'fraction_numerical': 0.0005772005772005772, 'mean_word_length': 3.524804177545692, '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': '3441230', 'n_tokens_mistral': 1025, 'n_tokens_neox': 1016, 'n_words': 231}