Exemple #1
0
    def test_get_offers_sync_error(self):
        setup_mock("invalid_request", responses, 400)
        client_options = {"client": "bad", "organization_id": "bad"}
        client = TargetClient.create(client_options)

        get_offers_opts = deepcopy(self.get_offers_options)
        get_offers_opts["request"] = create_delivery_request(
            get_offers_opts["request"])

        with self.assertRaises(
                delivery_api_client.exceptions.ApiException) as err:
            client.get_offers(get_offers_opts)
        self.assertEqual(json.loads(err.exception.body), {
            "status": 400,
            "message": "Invalid request: Invalid imsOrg -  bad"
        })
    def test_get_attributes_from_api_response(self):
        setup_mock("get_attributes", responses)
        opts = deepcopy(self.get_attributes_options)
        opts["request"] = create_delivery_request(opts["request"])

        feature_a = self.client.get_attributes(["feature-flag-a"], opts)

        self.assertEqual(
            feature_a.get_value("feature-flag-a", "payment_experience"),
            "legacy")
        self.assertFalse(
            feature_a.get_value("feature-flag-a", "show_feature_x"))
        self.assertEqual(
            feature_a.get_value("feature-flag-a", "payment_gateway_version"),
            2.3)
        self.assertEqual(
            feature_a.get_value("feature-flag-a", "customer_feedback_value"),
            10)

        self.assertDictEqual(
            feature_a.as_object("feature-flag-a"), {
                "payment_experience": "legacy",
                "show_feature_x": False,
                "payment_gateway_version": 2.3,
                "customer_feedback_value": 10
            })

        features = self.client.get_attributes(
            ["feature-flag-a", "feature-flag-b"])

        self.assertEqual(
            features.get_value("feature-flag-b", "purchase_experience"),
            "beta2")
        self.assertTrue(features.get_value("feature-flag-b", "show_feature_y"))
        self.assertEqual(features.get_value("feature-flag-b", "cart_version"),
                         1.3)
        self.assertEqual(
            features.get_value("feature-flag-b", "customer_survey_value"), 102)

        self.assertDictEqual(
            features.as_object("feature-flag-b"), {
                "purchase_experience": "beta2",
                "show_feature_y": True,
                "cart_version": 1.3,
                "customer_survey_value": 102
            })
    def test_get_attributes_attribute_does_not_exist(self):
        setup_mock("get_attributes", responses)
        opts = deepcopy(self.get_attributes_options)
        prefetch = {"mboxes": [{"name": "feature-flag-a", "index": 2}]}
        execute = {"mboxes": [{"index": 2, "name": "feature-flag-b"}]}
        opts["request"]["prefetch"] = prefetch
        opts["request"]["execute"] = execute
        opts["request"] = create_delivery_request(opts["request"])

        attributes = self.client.get_attributes(["unknown-flag"], opts)

        with self.assertRaises(Exception) as err:
            attributes.get_value("unknown-flag", "payment_experience")

        self.assertEqual(
            "Attribute 'payment_experience' does not exist for mbox 'unknown-flag'",
            str(err.exception))
Exemple #4
0
    def test_get_offers_with_preview(self):
        setup_mock("default", responses)
        opts = deepcopy(self.get_offers_options)
        opts["request"]["preview"] = {"token": "sdfdlbys3355"}
        opts["request"] = create_delivery_request(opts["request"])

        request_spy = spy_decorator(ApiClient.request)
        with patch.object(ApiClient, "request", request_spy):
            result = self.client.get_offers(opts)

            # validate Delivery API request
            expected_req_preview = request_spy.mock.call_args[1]["body"][
                "preview"]
            self.assertEqual(expected_req_preview.get("token"), "sdfdlbys3355")

            # validate Delivery API response
            self.assertEqual(len(responses.calls), 1)
            validate_response(self, result)
Exemple #5
0
    def test_get_offers_async(self):
        setup_mock("default", responses)

        shared = {"has_response": False}
        async_opts = deepcopy(self.get_offers_options)
        async_opts["request"] = create_delivery_request(async_opts["request"])

        def verify_callback(result):
            self.assertEqual(len(responses.calls), 1)
            validate_response(self, result)
            shared["has_response"] = True

        async_opts["callback"] = verify_callback
        thread = self.client.get_offers(async_opts)
        try:
            thread.get(
                timeout=5)  # Blocks current thread to keep test runner alive
        except multiprocessing.context.TimeoutError:
            self.fail("Test case timed out waiting for callback to be invoked")
        self.assertTrue(shared.get("has_response"))
    def test_get_attributes_add_mbox_to_delivery_request(self):
        setup_mock("get_attributes", responses)
        opts = deepcopy(self.get_attributes_options)
        prefetch = {"mboxes": [{"name": "feature-flag-a", "index": 2}]}
        opts["request"]["prefetch"] = prefetch
        opts["request"]["execute"] = None
        opts["request"] = create_delivery_request(opts["request"])

        attributes = self.client.get_attributes(["feature-flag-b"], opts)

        self.assertTrue(
            attributes.get_value("feature-flag-b", "show_feature_y"))

        self.assertDictEqual(
            attributes.as_object("feature-flag-b"), {
                "purchase_experience": "beta2",
                "show_feature_y": True,
                "cart_version": 1.3,
                "customer_survey_value": 102
            })
Exemple #7
0
    def test_get_offers_with_trace(self):
        setup_mock("debug_trace", responses)
        opts = deepcopy(self.get_offers_options)
        opts["request"]["trace"] = {
            "authorizationToken": "token",
            "usage": {
                "a": "b",
                "c": "d"
            }
        }
        opts["request"] = create_delivery_request(opts["request"])

        request_spy = spy_decorator(ApiClient.request)
        with patch.object(ApiClient, "request", request_spy):
            result = self.client.get_offers(opts)

            # validate Delivery API request
            expected_req_trace = request_spy.mock.call_args[1]["body"]["trace"]
            self.assertTrue(isinstance(expected_req_trace, dict))
            self.assertEqual(expected_req_trace.get("authorizationToken"),
                             "token")
            self.assertEqual(expected_req_trace.get("usage"), {
                "a": "b",
                "c": "d"
            })

            # validate Delivery API response
            self.assertEqual(len(responses.calls), 1)
            validate_response(self, result)

            if not result.get("response").execute.mboxes:
                self.fail("Expected execute mboxes in DeliveryResponse")
            for mbox in result.get("response").execute.mboxes:
                self.assertIsNotNone(mbox.trace)

            if not result.get("response").prefetch.mboxes:
                self.fail("Expected prefetch mboxes in DeliveryResponse")
            for mbox in result.get("response").prefetch.mboxes:
                self.assertIsNotNone(mbox.trace)