def test_impression_event(self):
        project_config = self.project_config
        experiment = self.project_config.get_experiment_from_key(
            'test_experiment')
        variation = self.project_config.get_variation_from_id(
            experiment.key, '111128')
        user_id = 'test_user'

        impression_event = UserEventFactory.create_impression_event(
            project_config, experiment, '111128', 'flag_key', 'rule_key',
            'rule_type', True, user_id, None)

        self.assertEqual(self.project_config.project_id,
                         impression_event.event_context.project_id)
        self.assertEqual(self.project_config.revision,
                         impression_event.event_context.revision)
        self.assertEqual(self.project_config.account_id,
                         impression_event.event_context.account_id)
        self.assertEqual(
            self.project_config.anonymize_ip,
            impression_event.event_context.anonymize_ip,
        )
        self.assertEqual(self.project_config.bot_filtering,
                         impression_event.bot_filtering)
        self.assertEqual(experiment, impression_event.experiment)
        self.assertEqual(variation, impression_event.variation)
        self.assertEqual(user_id, impression_event.user_id)
Exemplo n.º 2
0
    def test_impression_event__with_attributes(self):
        project_config = self.project_config
        experiment = self.project_config.get_experiment_from_key(
            'test_experiment')
        variation = self.project_config.get_variation_from_id(
            experiment.key, '111128')
        user_id = 'test_user'

        user_attributes = {'test_attribute': 'test_value', 'boolean_key': True}

        impression_event = UserEventFactory.create_impression_event(
            project_config, experiment, '111128', user_id, user_attributes)

        expected_attrs = EventFactory.build_attribute_list(
            user_attributes, project_config)

        self.assertEqual(self.project_config.project_id,
                         impression_event.event_context.project_id)
        self.assertEqual(self.project_config.revision,
                         impression_event.event_context.revision)
        self.assertEqual(self.project_config.account_id,
                         impression_event.event_context.account_id)
        self.assertEqual(
            self.project_config.anonymize_ip,
            impression_event.event_context.anonymize_ip,
        )
        self.assertEqual(self.project_config.bot_filtering,
                         impression_event.bot_filtering)
        self.assertEqual(experiment, impression_event.experiment)
        self.assertEqual(variation, impression_event.variation)
        self.assertEqual(user_id, impression_event.user_id)
        self.assertEqual(
            [x.__dict__ for x in expected_attrs],
            [x.__dict__ for x in impression_event.visitor_attributes],
        )
    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 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,
            )
    def test_create_impression_event__with_user_agent_when_bot_filtering_is_disabled(self,):
        """ Test that create_impression_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': [
                        {
                            'decisions': [
                                {'variation_id': '111129', 'experiment_id': '111127', 'campaign_id': '111182',
                                 'metadata': {'flag_key': '',
                                              'rule_key': 'rule_key',
                                              'rule_type': 'experiment',
                                              'variation_key': 'variation',
                                              'enabled': True},
                                 }
                            ],
                            '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'
        ), mock.patch(
            'optimizely.project_config.ProjectConfig.get_bot_filtering_value', return_value=False,
        ):
            event_obj = UserEventFactory.create_impression_event(
                self.project_config,
                self.project_config.get_experiment_from_key('test_experiment'),
                '111129',
                '',
                'rule_key',
                'experiment',
                True,
                'test_user',
                {'$opt_user_agent': 'Chrome'},
            )

        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,
        )