Пример #1
0
    def test_send_notifications_display_type(self):
        event_token = "B8C2FP2IuBgmeJcDfXHjGpNWHtnQtQrJfmRrQugEa2qCnQ9Y9OaLL2gsdrWQTvE54PwSz67rmXWmSnkXpSSS2Q=="
        options = [
            Option(content="<h1>it's firefox</h1>",
                   type=OptionType.HTML,
                   event_token=event_token)
        ]
        mbox = MboxResponse(options=options, metrics=[], name="browser-mbox")
        self.provider.add_notification(mbox)

        self.provider.send_notifications()

        time.sleep(1)
        self.assertEqual(self.mock_notify.call_count, 1)
        self.assertEqual(
            len(self.mock_notify.call_args[0][0]["request"].notifications), 1)
        received = to_dict(
            self.mock_notify.call_args[0][0]["request"].notifications[0])
        expected = {
            "id":
            "expect.any(String)",
            "impressionId":
            "expect.any(String)",
            "timestamp":
            "expect.any(Number)",
            "type":
            "display",
            "mbox": {
                "name": "browser-mbox"
            },
            "tokens": [
                "B8C2FP2IuBgmeJcDfXHjGpNWHtnQtQrJfmRrQugEa2qCnQ9Y9OaLL2gsdrWQTvE54PwSz67rmXWmSnkXpSSS2Q=="
            ]
        }
        expect_to_match_object(received, expected)
Пример #2
0
    def test_get_offers_server_side(self):
        expected_result = deepcopy(EXPECTED_PREFETCH_RESULT)
        expected_result["response"]["edgeHost"] = "mboxedge28.tt.omtrdc.net"
        expected_result["target_location_hint_cookie"] = {
            "name": "mboxEdgeCluster",
            "value": "28",
            "maxAge": 1860
        }
        expected_result["meta"] = {
            "decisioning_method": DecisioningMethod.SERVER_SIDE.value
        }

        get_offers_opts = {
            "request": PREFETCH_REQUEST,
            "session_id": "dummy_session"
        }

        client_opts = dict(CONFIG)
        client_opts["decisioning_method"] = DecisioningMethod.SERVER_SIDE.value

        with patch.object(DeliveryApi, "execute", return_value=create_delivery_response(DELIVERY_API_RESPONSE)):
            client = TargetClient.create(client_opts)
            result = client.get_offers(get_offers_opts)
            result_dict = to_dict(result)
            expect_to_match_object(result_dict, expected_result)
Пример #3
0
    def test_get_offers_on_device_decisioning_emits_notifications(self):
        get_offers_opts = {
            "request": EXECUTE_REQUEST,
            "session_id": "dummy_session"
        }

        client_opts = dict(CONFIG)
        client_opts["decisioning_method"] = DecisioningMethod.ON_DEVICE.value
        client_opts["target_location_hint"] = "28"

        # send_notifications call
        with patch.object(DeliveryApi, "execute", return_value=create_delivery_response({})) \
                as mock_delivery_api:

            with patch("target_decisioning_engine.artifact_provider.urllib3.PoolManager") as mock_artifact_provider:
                artifact_instance = mock_artifact_provider.return_value
                artifact_response = HTTPResponse(status=OK, body=json.dumps(ARTIFACT_AB_SIMPLE))
                artifact_instance.request.return_value = artifact_response

                client = TargetClient.create(client_opts)
                self.assertEqual(mock_artifact_provider.call_count, 1)

                result = client.get_offers(get_offers_opts)
                self.assertIsNotNone(result.get("response"))
                self.assertEqual(result.get("response").status, OK)
                self.assertIsNotNone(result.get("response").execute)

                time.sleep(1)  # notifications sent async
                self.assertEqual(mock_delivery_api.call_count, 1)
                notification_request = to_dict(mock_delivery_api.call_args[0][2])
                expect_to_match_object(notification_request, EXPECTED_NOTIFICATION_REQUEST)
Пример #4
0
    def test_send_notifications_no_duplicates(self):
        event_token = "yYWdmhDasVXGPWlpX1TRZDSAQdPpz2XBromX4n+pX9jf5r+rP39VCIaiiZlXOAYq"
        content = [{
            "type":
            "insertAfter",
            "selector":
            "HTML > BODY > DIV:nth-of-type(1) > H1:nth-of-type(1)",
            "cssSelector":
            "HTML > BODY > DIV:nth-of-type(1) > H1:nth-of-type(1)",
            "content":
            "<p id='action_insert_15882850825432970'>Better to remain silent and be thought a fool \
                    than to speak out and remove all doubt.</p>"
        }]
        options = [
            Option(content=content,
                   type=OptionType.ACTIONS,
                   event_token=event_token)
        ]
        metrics = [
            Metric(type=MetricType.CLICK,
                   event_token="/GMYvcjhUsR6WVqQElppUw==",
                   selector="#action_insert_15882853393943012")
        ]
        mbox = MboxResponse(options=options,
                            metrics=metrics,
                            name="target-global-mbox")
        self.provider.add_notification(mbox)
        self.provider.add_notification(mbox)  # duplicate

        self.provider.send_notifications()

        time.sleep(1)
        self.assertEqual(self.mock_notify.call_count, 1)
        self.assertEqual(
            len(self.mock_notify.call_args[0][0]["request"].notifications), 1)
        received = to_dict(
            self.mock_notify.call_args[0][0]["request"].notifications[0])
        expected = {
            "id":
            "expect.any(String)",
            "impressionId":
            "expect.any(String)",
            "timestamp":
            "expect.any(Number)",
            "type":
            "display",
            "mbox": {
                "name": "target-global-mbox"
            },
            "tokens": [
                "yYWdmhDasVXGPWlpX1TRZDSAQdPpz2XBromX4n+pX9jf5r+rP39VCIaiiZlXOAYq"
            ]
        }
        expect_to_match_object(received, expected)
Пример #5
0
    def test_get_offers_valid_on_device_decisioning_response(self):
        get_offers_opts = {
            "request": PREFETCH_REQUEST,
            "session_id": "dummy_session"
        }

        client_opts = dict(CONFIG)
        client_opts["decisioning_method"] = DecisioningMethod.ON_DEVICE.value
        client_opts["target_location_hint"] = "28"

        with patch("target_decisioning_engine.artifact_provider.urllib3.PoolManager") as mock_artifact_provider:
            artifact_instance = mock_artifact_provider.return_value
            artifact_response = HTTPResponse(status=OK, body=json.dumps(ARTIFACT_AB_SIMPLE))
            artifact_instance.request.return_value = artifact_response

            with patch.object(DeliveryApi, "execute", return_value=create_delivery_response(LOCATION_HINT_RESPONSE)):
                client = TargetClient.create(client_opts)
                result = client.get_offers(get_offers_opts)
                result_dict = to_dict(result)
                expect_to_match_object(result_dict, EXPECTED_PREFETCH_RESULT)
Пример #6
0
def validate_metrics(result_metrics, expected_metrics):
    """Validate metrics"""
    result_metrics = to_dict(result_metrics)
    for index, expected_metric in enumerate(expected_metrics):
        expect_to_match_object(result_metrics[index], expected_metric)