Exemple #1
0
    def setUp(self):
        """Setup function for each unit test."""

        super(GoogleAnalyticsHookTest, self).setUp()

        self.test_tracking_id = 'UA-12323-4'
        self.payload_builder = ga_hook.PayloadBuilder(self.test_tracking_id)

        self.event_test_data = {
            'ec': 'ClientID',
            'ea': 'test_event_action',
            'el': '20190423',
            'ev': 1,
            'cid': '12345.456789'
        }
        self.small_event = {
            'cid': '12345.67890',
            'ec': 'ClientID',
            'ea': 'test_event_action',
            'el': '20190423',
            'ev': 1,
            'z': '1558517072202080'
        }
        # Both of the below are approx 4K of data
        self.medium_event = {**self.small_event, 'ea': 'x' * 3800}
        self.utf8_event = {**self.small_event, 'ea': b'\xf0\xa9\xb8\xbd' * 320}

        self.test_hook = ga_hook.GoogleAnalyticsHook(self.test_tracking_id,
                                                     self.event_test_data)

        self.test_hook._send_http_request = mock.MagicMock(autospec=True)
        self.test_hook._send_http_request.return_value = mock.Mock(ok=True)
        self.test_hook._send_http_request.return_value.status = 200
Exemple #2
0
    def test_ga_hook_send_single_hit(self):
        """Test GoogleAnalyticsHook sends single hit."""
        test_hook = ga_hook.GoogleAnalyticsHook(self.test_tracking_id,
                                                self.event_test_data, False)
        test_payload = self.payload_builder.generate_single_payload(
            ga_hook.HitTypes.EVENT, self.event_test_data)

        with mock.patch('urllib.request.urlopen') as urlopen_mock:
            mock_response = urlopen_mock.return_value.__enter__.return_value
            mock_response.status = 200
            test_hook.send_hit(test_payload)

            urlopen_mock.assert_called_once()
Exemple #3
0
    def test_ga_hook_send_batch_hit_with_return_error_status_code(
            self, r_code):
        """Test GoogleAnalyticsHook sends batch hit with error response."""
        test_hook = ga_hook.GoogleAnalyticsHook(self.test_tracking_id,
                                                self.event_test_data, False)
        test_payload = self.payload_builder.generate_batch_payload(
            ga_hook.HitTypes.EVENT, [self.event_test_data])

        with mock.patch('urllib.request.urlopen') as urlopen_mock:
            mock_response = urlopen_mock.return_value.__enter__.return_value
            mock_response.status = r_code

            with self.assertRaises(
                    errors.DataOutConnectorSendUnsuccessfulError):
                test_hook.send_hit(test_payload,
                                   send_type=ga_hook.SendTypes.BATCH)
Exemple #4
0
    def test_ga_hook_send_batch_hit_with_retrys_on_retriable_error(self):
        """Test GoogleAnalyticsHook retries when retriable error occures."""
        test_hook = ga_hook.GoogleAnalyticsHook(self.test_tracking_id,
                                                self.event_test_data, False)
        test_payload = self.payload_builder.generate_batch_payload(
            ga_hook.HitTypes.EVENT, [self.event_test_data])

        with mock.patch('urllib.request.urlopen') as urlopen_mock:
            urlopen_mock.return_value.__enter__.return_value.status = 429

            try:
                test_hook.send_hit(test_payload)
            except errors.DataOutConnectorSendUnsuccessfulError:
                pass

            self.assertEqual(urlopen_mock.call_count,
                             retry_utils._RETRY_UTILS_MAX_RETRIES)
Exemple #5
0
 def test_ga_hook_get_invalid_tracking_id(self):
     """Test GoogleAnalyticsHook with invalid tracking id."""
     with self.assertRaises(errors.DataOutConnectorValueError):
         ga_hook.GoogleAnalyticsHook('UA-123-b', self.event_test_data)