def test_create_conversion_event__with_user_agent_when_bot_filtering_is_disabled(self,):
        """ Test that create_conversion_event creates Event object
    with right params when user agent attribute is provided and
    bot filtering is disabled """

        expected_params = {
            'account_id': '12001',
            'project_id': '111001',
            'visitors': [
                {
                    'visitor_id': 'test_user',
                    'attributes': [
                        {
                            'type': 'custom',
                            'value': 'Chrome',
                            'entity_id': '$opt_user_agent',
                            'key': '$opt_user_agent',
                        },
                        {
                            'type': 'custom',
                            'value': False,
                            'entity_id': '$opt_bot_filtering',
                            'key': '$opt_bot_filtering',
                        },
                    ],
                    'snapshots': [
                        {
                            'events': [
                                {
                                    'timestamp': 42123,
                                    'entity_id': '111095',
                                    'uuid': 'a68cf1ad-0393-4e18-af87-efe8f01a7c9c',
                                    'key': 'test_event',
                                }
                            ]
                        }
                    ],
                }
            ],
            'client_name': 'python-sdk',
            'client_version': version.__version__,
            'enrich_decisions': True,
            'anonymize_ip': False,
            'revision': '42',
        }

        with mock.patch('time.time', return_value=42.123), mock.patch(
            'uuid.uuid4', return_value='a68cf1ad-0393-4e18-af87-efe8f01a7c9c'
        ), mock.patch(
            'optimizely.project_config.ProjectConfig.get_bot_filtering_value', return_value=False,
        ):
            event_obj = UserEventFactory.create_conversion_event(
                self.project_config, 'test_event', 'test_user', {'$opt_user_agent': 'Chrome'}, None,
            )

        log_event = EventFactory.create_log_event(event_obj, self.logger)

        self._validate_event_object(
            log_event, EventFactory.EVENT_ENDPOINT, expected_params, EventFactory.HTTP_VERB, EventFactory.HTTP_HEADERS,
        )
    def test_create_impression_event(self):
        """ Test that create_impression_event creates LogEvent object with right params. """

        expected_params = {
            'account_id': '12001',
            'project_id': '111001',
            'visitors': [
                {
                    'visitor_id': 'test_user',
                    'attributes': [],
                    'snapshots': [
                        {
                            'decisions': [
                                {'variation_id': '111129', 'experiment_id': '111127', 'campaign_id': '111182',
                                 'metadata': {'flag_key': '',
                                              'rule_key': 'rule_key',
                                              'rule_type': 'experiment',
                                              'variation_key': 'variation',
                                              'enabled': False}}
                            ],
                            'events': [
                                {
                                    'timestamp': 42123,
                                    'entity_id': '111182',
                                    'uuid': 'a68cf1ad-0393-4e18-af87-efe8f01a7c9c',
                                    'key': 'campaign_activated',
                                }
                            ],
                        }
                    ],
                }
            ],
            'client_name': 'python-sdk',
            'client_version': version.__version__,
            'enrich_decisions': True,
            'anonymize_ip': False,
            'revision': '42',
        }

        with mock.patch('time.time', return_value=42.123), mock.patch(
            'uuid.uuid4', return_value='a68cf1ad-0393-4e18-af87-efe8f01a7c9c'
        ):
            event_obj = UserEventFactory.create_impression_event(
                self.project_config,
                self.project_config.get_experiment_from_key('test_experiment'),
                '111129',
                '',
                'rule_key',
                'experiment',
                False,
                'test_user',
                None,
            )

        log_event = EventFactory.create_log_event(event_obj, self.logger)

        self._validate_event_object(
            log_event, EventFactory.EVENT_ENDPOINT, expected_params, EventFactory.HTTP_VERB, EventFactory.HTTP_HEADERS,
        )
    def test_create_conversion_event__when_event_is_used_in_multiple_experiments(self):
        """ Test that create_conversion_event creates Event object with
    right params when multiple experiments use the same event. """

        expected_params = {
            'client_version': version.__version__,
            'project_id': '111001',
            'visitors': [
                {
                    'attributes': [
                        {'entity_id': '111094', 'type': 'custom', 'value': 'test_value', 'key': 'test_attribute'}
                    ],
                    'visitor_id': 'test_user',
                    'snapshots': [
                        {
                            'events': [
                                {
                                    'uuid': 'a68cf1ad-0393-4e18-af87-efe8f01a7c9c',
                                    'tags': {'non-revenue': 'abc', 'revenue': 4200, 'value': 1.234},
                                    'timestamp': 42123,
                                    'revenue': 4200,
                                    'value': 1.234,
                                    'key': 'test_event',
                                    'entity_id': '111095',
                                }
                            ]
                        }
                    ],
                }
            ],
            'account_id': '12001',
            'client_name': 'python-sdk',
            'enrich_decisions': True,
            'anonymize_ip': False,
            'revision': '42',
        }

        with mock.patch('time.time', return_value=42.123), mock.patch(
            'uuid.uuid4', return_value='a68cf1ad-0393-4e18-af87-efe8f01a7c9c'
        ):
            event_obj = UserEventFactory.create_conversion_event(
                self.project_config,
                'test_event',
                'test_user',
                {'test_attribute': 'test_value'},
                {'revenue': 4200, 'value': 1.234, 'non-revenue': 'abc'},
            )

        log_event = EventFactory.create_log_event(event_obj, self.logger)

        self._validate_event_object(
            log_event, EventFactory.EVENT_ENDPOINT, expected_params, EventFactory.HTTP_VERB, EventFactory.HTTP_HEADERS,
        )
Beispiel #4
0
    def test_event_processor__dispatch_raises_exception(self):
        """ Test that process logs dispatch failure gracefully. """

        user_event = self._build_conversion_event(self.event_name)
        log_event = EventFactory.create_log_event(user_event, self.optimizely.logger)

        with mock.patch.object(self.optimizely, 'logger') as mock_client_logging, mock.patch.object(
            self.event_dispatcher, 'dispatch_event', side_effect=Exception('Failed to send.'),
        ):

            event_processor = ForwardingEventProcessor(
                self.event_dispatcher, mock_client_logging, self.notification_center
            )
            event_processor.process(user_event)

        mock_client_logging.exception.assert_called_once_with(
            'Error dispatching event: ' + str(log_event) + ' Failed to send.'
        )
    def test_create_conversion_event(self):
        """ Test that create_conversion_event creates Event object
    with right params when no attributes are provided. """

        expected_params = {
            'account_id': '12001',
            'project_id': '111001',
            'visitors': [
                {
                    'visitor_id': 'test_user',
                    'attributes': [],
                    'snapshots': [
                        {
                            'events': [
                                {
                                    'timestamp': 42123,
                                    'entity_id': '111095',
                                    'uuid': 'a68cf1ad-0393-4e18-af87-efe8f01a7c9c',
                                    'key': 'test_event',
                                }
                            ]
                        }
                    ],
                }
            ],
            'client_name': 'python-sdk',
            'client_version': version.__version__,
            'enrich_decisions': True,
            'anonymize_ip': False,
            'revision': '42',
        }

        with mock.patch('time.time', return_value=42.123), mock.patch(
            'uuid.uuid4', return_value='a68cf1ad-0393-4e18-af87-efe8f01a7c9c'
        ):
            event_obj = UserEventFactory.create_conversion_event(
                self.project_config, 'test_event', 'test_user', None, None
            )

        log_event = EventFactory.create_log_event(event_obj, self.logger)

        self._validate_event_object(
            log_event, EventFactory.EVENT_ENDPOINT, expected_params, EventFactory.HTTP_VERB, EventFactory.HTTP_HEADERS,
        )
        def side_effect(*args, **kwargs):
            attribute_key = args[0]
            if attribute_key == 'boolean_key' or attribute_key == 'double_key':
                return True

            return False

            attributes = {
                'test_attribute': 'test_value',
                'boolean_key': True,
                'integer_key': 0,
                'double_key': 5.5,
            }

            with mock.patch('time.time', return_value=42.123), mock.patch(
                'uuid.uuid4', return_value='a68cf1ad-0393-4e18-af87-efe8f01a7c9c'
            ), mock.patch(
                'optimizely.helpers.validator.is_attribute_valid', side_effect=side_effect,
            ):

                event_obj = UserEventFactory.create_impression_event(
                    self.project_config,
                    self.project_config.get_experiment_from_key('test_experiment'),
                    '111129',
                    '',
                    'experiment',
                    'test_user',
                    attributes,
                )

            log_event = EventFactory.create_log_event(event_obj, self.logger)

            self._validate_event_object(
                log_event,
                EventFactory.EVENT_ENDPOINT,
                expected_params,
                EventFactory.HTTP_VERB,
                EventFactory.HTTP_HEADERS,
            )