def test_deserialize_instance_with_bad_format_url_should_give_error(self):
     serializer = ServiceInstanceSerializer(data={'uri': 1, 'version': '1.0', 'environment': 'test',
                                           'class_name': 'test_class'})
     self.assertEquals(False, serializer.is_valid())
     self.assertEquals("invalid",
                       serializer.errors['uri'][0],
                       'Invalid error message')
 def test_deserialize_instance_with_empty_string_environment_should_give_error(self):
     serializer = ServiceInstanceSerializer(data={'uri': "http", 'version': '1.0', 'environment': '',
                                           'class_name': 'test_class'})
     self.assertEquals(False, serializer.is_valid())
     self.assertEquals("invalid",
                       serializer.errors['environment'][0],
                       'Invalid error message')
 def test_deserialize_instance_with_wrong_size_attributes_should_give_error(self):
     attributes = dict((str(e), str(e)) for e in range(129))
     serializer = ServiceInstanceSerializer(data={'uri': 'url_test', 'version': '1.0', 'environment': 'test',
                                           'class_name': 'test_class', 'attributes': attributes
                                         })
     self.assertEquals(False, serializer.is_valid())
     self.assertTrue(match("Invalid parameter value: {*",
                       serializer.errors['attributes'][0]))
 def test_deserialize_instance_with_bad_format_attributes_should_give_error(self):
     serializer = ServiceInstanceSerializer(data={'uri': 'url_test', 'version': '1.0', 'environment': 'test',
                                           'class_name': 'test_class', 'attributes': 25
                                         })
     self.assertEquals(False, serializer.is_valid())
     self.assertEquals("Invalid parameter value: 25",
                       serializer.errors['attributes'][0],
                       'Invalid error message')
    def test_deserialize_several_instances_with_attributes_should_work(self):
        serializer_lists = [
                        {'uri': 'url_test', 'version': '1.0', 'environment': 'test',
                          'class_name': 'test_class', 'attributes':
                           {'key': 'keyvalue'}},
                        {'uri': 'url_test2', 'version': '1.0', 'environment': 'test',
                          'class_name': 'test_class', 'attributes':
                           {'key_dos': 'keyvalue'}}
                ]

        serializer = ServiceInstanceSerializer(data=serializer_lists, many=True)
        self.assertEquals(True, serializer.is_valid(), "Serialization invalid")
        # Ensure that attributes keys are not included when null data is given
        self.assertTrue('key_dos' not in serializer.data[0]['attributes'], 'key_dos is present in attributes')
        self.assertTrue('key' not in serializer.data[1]['attributes'], 'key is present in attributes')
    def test_deserialize_instance_large_params_should_return_invalid(self):
        serializer = ServiceInstanceSerializer(data={'uri': 'a' * 2500, 'version': '1.0', 'environment': 'test',
                                              'class_name': 'test_class'})
        self.assertEquals(False, serializer.is_valid(), "Deserialization invalid")
        self.assertEquals(serializer.errors['uri'][0], 'invalid')

        serializer = ServiceInstanceSerializer(data={'uri': 'url_test', 'version': 'v' * 300, 'environment': 'test',
                                              'class_name': 'test_class'})
        self.assertEquals(False, serializer.is_valid(), "Deserialization invalid")
        self.assertEquals(serializer.errors['version'][0], 'invalid')

        serializer = ServiceInstanceSerializer(data={'uri': 'url_test', 'version': '1.0', 'environment': 'test',
                                              'class_name': 'a' * 600})
        self.assertEquals(False, serializer.is_valid(), "Deserialization invalid")
        self.assertEquals(serializer.errors['class_name'][0], 'invalid')
 def test_deserialize_instance_with_wrong_value_size_attributes_should_give_error(self):
     attributes = {'kye': ''}
     serializer = ServiceInstanceSerializer(data={'uri': 'url_test', 'version': '1.0', 'environment': 'test',
                                           'class_name': 'test_class', 'attributes': attributes
                                         })
     self.assertEquals(False, serializer.is_valid())
     self.assertTrue(match("invalid",
                       serializer.errors['attributes'][0]['kye'][0]))
     attributes = {'key': 'k' * 513}
     serializer = ServiceInstanceSerializer(data={'uri': 'url_test', 'version': '1.0', 'environment': 'test',
                                           'class_name': 'test_class', 'attributes': attributes
                                         })
     self.assertEquals(False, serializer.is_valid())
     self.assertTrue(match("invalid",
                       serializer.errors['attributes'][0]['key'][0]))
 def test_deserialize_instance_with_wrong_key_size_attributes_should_give_error(self):
     attributes = {'': 'value'}
     serializer = ServiceInstanceSerializer(data={'uri': 'url_test', 'version': '1.0', 'environment': 'test',
                                           'class_name': 'test_class', 'attributes': attributes
                                         })
     self.assertEquals(False, serializer.is_valid())
     self.assertTrue(match("Invalid parameter value: empty-string",
                       serializer.errors['attributes'][0]))
     attributes = {'k' * 513: 'value'}
     serializer = ServiceInstanceSerializer(data={'uri': 'url_test', 'version': '1.0', 'environment': 'test',
                                           'class_name': 'test_class', 'attributes': attributes
                                         })
     self.assertEquals(False, serializer.is_valid())
     self.assertTrue(match("Invalid parameter value: {0}".format('k' * 513),
                       serializer.errors['attributes'][0]))
    def test_deserialize_instance_null_required_params_should_return_required(self):
        serializer = ServiceInstanceSerializer(data={'version': '1.0', 'environment': 'test',
                                              'class_name': 'test_class'})
        self.assertEquals(False, serializer.is_valid(), "Deserialization invalid")
        self.assertEquals(serializer.errors['uri'][0], 'required')

        serializer = ServiceInstanceSerializer(data={'version': '1.0', 'environment': 'test',
                                              'class_name': 'test_class', 'uri': None})
        self.assertEquals(False, serializer.is_valid(), "Deserialization invalid")
        self.assertEquals(serializer.errors['uri'][0], 'required')

        serializer = ServiceInstanceSerializer(data={'uri': 'url_test', 'environment': 'test',
                                              'class_name': 'test_class'})
        self.assertEquals(False, serializer.is_valid(), "Deserialization invalid")
        self.assertEquals(serializer.errors['version'][0], 'required')

        serializer = ServiceInstanceSerializer(data={'uri': 'url_test', 'version': '1.0', 'environment': 'test'})
        self.assertEquals(False, serializer.is_valid(), "Deserialization invalid")
        self.assertEquals(serializer.errors['class_name'][0], 'required')
 def test_serialize_valid_instance_should_work(self):
     id_ = ObjectId()
     serializer = ServiceInstanceSerializer({'uri': 'url_test', 'version': '1.0', 'environment': 'test',
                                           'class_name': 'test_class', '_id': id_})
     self.assertEquals(serializer.data['id'], str(id_))
 def test_deserialize_instance_should_work(self):
     serializer = ServiceInstanceSerializer(data={'uri': 'url_test', 'version': '1.0', 'environment': 'test',
                                           'class_name': 'test_class'})
     self.assertEquals(True, serializer.is_valid(), "Serialization invalid")