コード例 #1
0
    def test_fetch_split_changes(self, mocker):
        """Test split changes fetching API call."""
        httpclient = mocker.Mock(spec=client.HttpClient)
        httpclient.get.return_value = client.HttpResponse(
            200, '{"prop1": "value1"}')
        split_api = splits.SplitsAPI(httpclient, 'some_api_key',
                                     SdkMetadata('1.0', 'some', '1.2.3.4'))
        response = split_api.fetch_splits(123)

        assert response['prop1'] == 'value1'
        assert httpclient.get.mock_calls == [
            mocker.call('sdk',
                        '/splitChanges',
                        'some_api_key',
                        extra_headers={
                            'SplitSDKVersion': '1.0',
                            'SplitSDKMachineIP': '1.2.3.4',
                            'SplitSDKMachineName': 'some'
                        },
                        query={'since': 123})
        ]

        httpclient.reset_mock()

        def raise_exception(*args, **kwargs):
            raise client.HttpClientException('some_message')

        httpclient.get.side_effect = raise_exception
        with pytest.raises(APIException) as exc_info:
            response = split_api.fetch_splits(123)
            assert exc_info.type == APIException
            assert exc_info.value.message == 'some_message'
コード例 #2
0
    def test_post_impressions_ip_address_disabled(self, mocker):
        """Test impressions posting API call."""
        httpclient = mocker.Mock(spec=client.HttpClient)
        httpclient.post.return_value = client.HttpResponse(200, '')
        cfg = DEFAULT_CONFIG.copy()
        cfg.update({'IPAddressesEnabled': False})
        sdk_metadata = get_metadata(cfg)
        impressions_api = impressions.ImpressionsAPI(httpclient,
                                                     'some_api_key',
                                                     sdk_metadata)
        response = impressions_api.flush_impressions(self.impressions)

        call_made = httpclient.post.mock_calls[0]

        # validate positional arguments
        assert call_made[1] == ('events', '/testImpressions/bulk',
                                'some_api_key')

        # validate key-value args (headers)
        assert call_made[2]['extra_headers'] == {
            'SplitSDKVersion': 'python-%s' % __version__,
        }

        # validate key-value args (body)
        assert call_made[2]['body'] == self.expectedImpressions
コード例 #3
0
    def test_post_latencies(self, mocker):
        """Test impressions posting API call."""
        httpclient = mocker.Mock(spec=client.HttpClient)
        httpclient.post.return_value = client.HttpResponse(200, '')
        sdk_metadata = SdkMetadata('python-1.2.3', 'some_machine_name',
                                   '123.123.123.123')
        telemetry_api = telemetry.TelemetryAPI(httpclient, 'some_api_key',
                                               sdk_metadata)
        response = telemetry_api.flush_latencies({
            'l1': [
                1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
                19, 20, 21, 22
            ]
        })

        call_made = httpclient.post.mock_calls[0]

        # validate positional arguments
        assert call_made[1] == ('events', '/metrics/times', 'some_api_key')

        # validate key-value args (headers)
        assert call_made[2]['extra_headers'] == {
            'SplitSDKVersion': 'python-1.2.3',
            'SplitSDKMachineIP': '123.123.123.123',
            'SplitSDKMachineName': 'some_machine_name'
        }

        # validate key-value args (body)
        assert call_made[2]['body'] == [{
            'name':
            'l1',
            'latencies': [
                1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
                19, 20, 21, 22
            ]
        }]

        httpclient.reset_mock()

        def raise_exception(*args, **kwargs):
            raise client.HttpClientException('some_message')

        httpclient.post.side_effect = raise_exception
        with pytest.raises(APIException) as exc_info:
            response = telemetry_api.flush_latencies({
                'l1': [
                    1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
                    18, 19, 20, 21, 22
                ]
            })
            assert exc_info.type == APIException
            assert exc_info.value.message == 'some_message'
コード例 #4
0
    def test_post_impressions(self, mocker):
        """Test impressions posting API call."""
        httpclient = mocker.Mock(spec=client.HttpClient)
        httpclient.post.return_value = client.HttpResponse(200, '')
        cfg = DEFAULT_CONFIG.copy()
        cfg.update({
            'IPAddressesEnabled': True,
            'machineName': 'some_machine_name',
            'machineIp': '123.123.123.123'
        })
        sdk_metadata = get_metadata(cfg)
        impressions_api = impressions.ImpressionsAPI(httpclient,
                                                     'some_api_key',
                                                     sdk_metadata)
        response = impressions_api.flush_impressions(self.impressions)

        call_made = httpclient.post.mock_calls[0]

        # validate positional arguments
        assert call_made[1] == ('events', '/testImpressions/bulk',
                                'some_api_key')

        # validate key-value args (headers)
        assert call_made[2]['extra_headers'] == {
            'SplitSDKVersion': 'python-%s' % __version__,
            'SplitSDKMachineIP': '123.123.123.123',
            'SplitSDKMachineName': 'some_machine_name',
            'SplitSDKImpressionsMode': 'OPTIMIZED'
        }

        # validate key-value args (body)
        assert call_made[2]['body'] == self.expectedImpressions

        httpclient.reset_mock()

        def raise_exception(*args, **kwargs):
            raise client.HttpClientException('some_message')

        httpclient.post.side_effect = raise_exception
        with pytest.raises(APIException) as exc_info:
            response = impressions_api.flush_impressions(self.impressions)
            assert exc_info.type == APIException
            assert exc_info.value.message == 'some_message'
コード例 #5
0
    def test_auth(self, mocker):
        """Test auth API call."""
        token = "eyJhbGciOiJIUzI1NiIsImtpZCI6IjVZOU05US45QnJtR0EiLCJ0eXAiOiJKV1QifQ.eyJ4LWFibHktY2FwYWJpbGl0eSI6IntcIk56TTJNREk1TXpjMF9NVGd5TlRnMU1UZ3dOZz09X3NlZ21lbnRzXCI6W1wic3Vic2NyaWJlXCJdLFwiTnpNMk1ESTVNemMwX01UZ3lOVGcxTVRnd05nPT1fc3BsaXRzXCI6W1wic3Vic2NyaWJlXCJdLFwiY29udHJvbF9wcmlcIjpbXCJzdWJzY3JpYmVcIixcImNoYW5uZWwtbWV0YWRhdGE6cHVibGlzaGVyc1wiXSxcImNvbnRyb2xfc2VjXCI6W1wic3Vic2NyaWJlXCIsXCJjaGFubmVsLW1ldGFkYXRhOnB1Ymxpc2hlcnNcIl19IiwieC1hYmx5LWNsaWVudElkIjoiY2xpZW50SWQiLCJleHAiOjE2MDIwODgxMjcsImlhdCI6MTYwMjA4NDUyN30.5_MjWonhs6yoFhw44hNJm3H7_YMjXpSW105DwjjppqE"
        httpclient = mocker.Mock(spec=client.HttpClient)
        payload = '{{"pushEnabled": true, "token": "{token}"}}'.format(
            token=token)
        cfg = DEFAULT_CONFIG.copy()
        cfg.update({
            'IPAddressesEnabled': True,
            'machineName': 'some_machine_name',
            'machineIp': '123.123.123.123'
        })
        sdk_metadata = get_metadata(cfg)
        httpclient.get.return_value = client.HttpResponse(200, payload)
        auth_api = auth.AuthAPI(httpclient, 'some_api_key', sdk_metadata)
        response = auth_api.authenticate()

        assert response.push_enabled == True
        assert response.token == token

        call_made = httpclient.get.mock_calls[0]

        # validate positional arguments
        assert call_made[1] == ('auth', '/v2/auth', 'some_api_key')

        # validate key-value args (headers)
        assert call_made[2]['extra_headers'] == {
            'SplitSDKVersion': 'python-%s' % __version__,
            'SplitSDKMachineIP': '123.123.123.123',
            'SplitSDKMachineName': 'some_machine_name'
        }

        httpclient.reset_mock()

        def raise_exception(*args, **kwargs):
            raise client.HttpClientException('some_message')

        httpclient.get.side_effect = raise_exception
        with pytest.raises(APIException) as exc_info:
            response = auth_api.authenticate()
            assert exc_info.type == APIException
            assert exc_info.value.message == 'some_message'
コード例 #6
0
    def test_fetch_segment_changes(self, mocker):
        """Test segment changes fetching API call."""
        httpclient = mocker.Mock(spec=client.HttpClient)
        httpclient.get.return_value = client.HttpResponse(
            200, '{"prop1": "value1"}')
        segment_api = segments.SegmentsAPI(httpclient, 'some_api_key')
        response = segment_api.fetch_segment('some_segment', 123)

        assert response['prop1'] == 'value1'
        assert httpclient.get.mock_calls == [
            mocker.call('sdk', '/segmentChanges/some_segment', 'some_api_key',
                        {'since': 123})
        ]

        httpclient.reset_mock()

        def raise_exception(*args, **kwargs):
            raise client.HttpClientException('some_message')

        httpclient.get.side_effect = raise_exception
        with pytest.raises(APIException) as exc_info:
            response = segment_api.fetch_segment('some_segment', 123)
            assert exc_info.type == APIException
            assert exc_info.value.message == 'some_message'
コード例 #7
0
    def test_post_events(self, mocker):
        """Test impressions posting API call."""
        httpclient = mocker.Mock(spec=client.HttpClient)
        httpclient.post.return_value = client.HttpResponse(200, '')
        sdk_metadata = SdkMetadata('python-1.2.3', 'some_machine_name',
                                   '123.123.123.123')
        events_api = events.EventsAPI(httpclient, 'some_api_key', sdk_metadata)
        response = events_api.flush_events([
            Event('k1', 'user', 'purchase', 12.50, 123456),
            Event('k2', 'user', 'purchase', 12.50, 123456),
            Event('k3', 'user', 'purchase', None, 123456),
            Event('k4', 'user', 'purchase', None, 123456)
        ])

        call_made = httpclient.post.mock_calls[0]

        # validate positional arguments
        assert call_made[1] == ('events', '/events/bulk', 'some_api_key')

        # validate key-value args (headers)
        assert call_made[2]['extra_headers'] == {
            'SplitSDKVersion': 'python-1.2.3',
            'SplitSDKMachineIP': '123.123.123.123',
            'SplitSDKMachineName': 'some_machine_name'
        }

        # validate key-value args (body)
        assert call_made[2]['body'] == [{
            'key': 'k1',
            'trafficTypeName': 'user',
            'eventTypeId': 'purchase',
            'value': 12.50,
            'timestamp': 123456
        }, {
            'key': 'k2',
            'trafficTypeName': 'user',
            'eventTypeId': 'purchase',
            'value': 12.50,
            'timestamp': 123456
        }, {
            'key': 'k3',
            'trafficTypeName': 'user',
            'eventTypeId': 'purchase',
            'value': None,
            'timestamp': 123456
        }, {
            'key': 'k4',
            'trafficTypeName': 'user',
            'eventTypeId': 'purchase',
            'value': None,
            'timestamp': 123456
        }]

        httpclient.reset_mock()

        def raise_exception(*args, **kwargs):
            raise client.HttpClientException('some_message')

        httpclient.post.side_effect = raise_exception
        with pytest.raises(APIException) as exc_info:
            response = events_api.flush_events([
                Event('k1', 'user', 'purchase', 12.50, 123456),
                Event('k2', 'user', 'purchase', 12.50, 123456),
                Event('k3', 'user', 'purchase', None, 123456),
                Event('k4', 'user', 'purchase', None, 123456)
            ])
            assert exc_info.type == APIException
            assert exc_info.value.message == 'some_message'
コード例 #8
0
    def test_post_impressions(self, mocker):
        """Test impressions posting API call."""
        httpclient = mocker.Mock(spec=client.HttpClient)
        httpclient.post.return_value = client.HttpResponse(200, '')
        sdk_metadata = SdkMetadata('python-1.2.3', 'some_machine_name',
                                   '123.123.123.123')
        impressions_api = impressions.ImpressionsAPI(httpclient,
                                                     'some_api_key',
                                                     sdk_metadata)
        response = impressions_api.flush_impressions([
            Impression('k1', 'f1', 'on', 'l1', 123456, 'b1', 321654),
            Impression('k2', 'f2', 'off', 'l1', 123456, 'b1', 321654),
            Impression('k3', 'f1', 'on', 'l1', 123456, 'b1', 321654),
        ])

        call_made = httpclient.post.mock_calls[0]

        # validate positional arguments
        assert call_made[1] == ('events', '/testImpressions/bulk',
                                'some_api_key')

        # validate key-value args (headers)
        assert call_made[2]['extra_headers'] == {
            'SplitSDKVersion': 'python-1.2.3',
            'SplitSDKMachineIP': '123.123.123.123',
            'SplitSDKMachineName': 'some_machine_name'
        }

        # validate key-value args (body)
        assert call_made[2]['body'] == [{
            'testName':
            'f1',
            'keyImpressions': [
                {
                    'keyName': 'k1',
                    'bucketingKey': 'b1',
                    'treatment': 'on',
                    'label': 'l1',
                    'time': 321654,
                    'changeNumber': 123456
                },
                {
                    'keyName': 'k3',
                    'bucketingKey': 'b1',
                    'treatment': 'on',
                    'label': 'l1',
                    'time': 321654,
                    'changeNumber': 123456
                },
            ],
        }, {
            'testName':
            'f2',
            'keyImpressions': [
                {
                    'keyName': 'k2',
                    'bucketingKey': 'b1',
                    'treatment': 'off',
                    'label': 'l1',
                    'time': 321654,
                    'changeNumber': 123456
                },
            ]
        }]

        httpclient.reset_mock()

        def raise_exception(*args, **kwargs):
            raise client.HttpClientException('some_message')

        httpclient.post.side_effect = raise_exception
        with pytest.raises(APIException) as exc_info:
            response = impressions_api.flush_impressions([
                Impression('k1', 'f1', 'on', 'l1', 123456, 'b1', 321654),
                Impression('k2', 'f2', 'off', 'l1', 123456, 'b1', 321654),
                Impression('k3', 'f1', 'on', 'l1', 123456, 'b1', 321654),
            ])
            assert exc_info.type == APIException
            assert exc_info.value.message == 'some_message'