def test_update_object_http_error(self):
        """Test whether an exception is thrown when the HTTP error is not 404 or 400"""

        obj_data = read_file('data/object_index-pattern')
        attributes = {"version": "2"}

        httpretty.register_uri(httpretty.PUT,
                               OBJECT_URL,
                               body=obj_data,
                               status=500)

        client = SavedObjects(KIBANA_URL)
        with self.assertRaises(requests.exceptions.HTTPError):
            _ = client.update_object(OBJECT_TYPE, OBJECT_ID, attributes)
    def test_update_object_not_found(self):
        """Test whether a warning is logged when the object is not found"""

        obj_data = read_file('data/object_index-pattern')
        attributes = {"version": "2"}

        httpretty.register_uri(httpretty.PUT,
                               OBJECT_URL,
                               body=obj_data,
                               status=404)

        client = SavedObjects(KIBANA_URL)
        with self.assertLogs(logger, level='WARNING') as cm:
            obj = client.update_object(OBJECT_TYPE, OBJECT_ID, attributes)
            self.assertEqual(
                cm.output[0], 'WARNING:archimedes.clients.saved_objects:'
                'No ' + OBJECT_TYPE + ' found with id: ' + OBJECT_ID)
            self.assertIsNone(obj)
    def test_update_object(self):
        """Test the method update_object"""

        obj_data = read_file('data/object_index-pattern')
        attributes = {"version": "2"}

        httpretty.register_uri(httpretty.PUT,
                               OBJECT_URL,
                               body=obj_data,
                               status=200)

        client = SavedObjects(KIBANA_URL)
        with self.assertLogs(logger, level='INFO') as cm:
            obj = client.update_object(OBJECT_TYPE, OBJECT_ID, attributes)
            self.assertEqual(
                cm.output[0], 'INFO:archimedes.clients.saved_objects:'
                'Object ' + OBJECT_TYPE + ' with id ' + OBJECT_ID + ' updated')
            self.assertDictEqual(obj, json.loads(obj_data))
    def test_update_object_not_updated(self):
        """Test whether a warning is logged when the object is not updated"""

        obj_data = read_file('data/object_index-pattern')
        obj_attr = "version"
        obj_new_value = "2"

        httpretty.register_uri(httpretty.PUT,
                               OBJECT_URL,
                               body=obj_data,
                               status=400)

        client = SavedObjects(KIBANA_URL)
        with self.assertLogs(logger, level='WARNING') as cm:
            obj = client.update_object(OBJECT_TYPE, OBJECT_ID, obj_attr,
                                       obj_new_value)
            self.assertEqual(
                cm.output[0],
                'WARNING:archimedes.clients.saved_objects:Impossible to update '
                'attribute ' + obj_attr + ' with value ' + obj_new_value + ', '
                'for ' + OBJECT_TYPE + ' with id ' + OBJECT_ID)
            self.assertIsNone(obj)