Add exception for extra/invalid custom properties.
parent
32ff00559e
commit
91cecb7b0c
|
@ -4,7 +4,7 @@ import collections
|
||||||
import datetime as dt
|
import datetime as dt
|
||||||
import json
|
import json
|
||||||
|
|
||||||
from .exceptions import InvalidValueError, MissingFieldsError
|
from .exceptions import ExtraFieldsError, InvalidValueError, MissingFieldsError
|
||||||
from .utils import format_datetime, get_timestamp, NOW
|
from .utils import format_datetime, get_timestamp, NOW
|
||||||
|
|
||||||
__all__ = ['STIXJSONEncoder', '_STIXBase']
|
__all__ = ['STIXJSONEncoder', '_STIXBase']
|
||||||
|
@ -53,7 +53,7 @@ class _STIXBase(collections.Mapping):
|
||||||
# Detect any keyword arguments not allowed for a specific type
|
# Detect any keyword arguments not allowed for a specific type
|
||||||
extra_kwargs = list(set(kwargs) - set(cls._properties))
|
extra_kwargs = list(set(kwargs) - set(cls._properties))
|
||||||
if extra_kwargs:
|
if extra_kwargs:
|
||||||
raise TypeError("unexpected keyword arguments: " + str(extra_kwargs))
|
raise ExtraFieldsError(cls, extra_kwargs)
|
||||||
|
|
||||||
required_fields = get_required_properties(cls._properties)
|
required_fields = get_required_properties(cls._properties)
|
||||||
missing_kwargs = set(required_fields) - set(kwargs)
|
missing_kwargs = set(required_fields) - set(kwargs)
|
||||||
|
|
|
@ -17,7 +17,7 @@ class InvalidValueError(STIXError, ValueError):
|
||||||
|
|
||||||
|
|
||||||
class MissingFieldsError(STIXError, ValueError):
|
class MissingFieldsError(STIXError, ValueError):
|
||||||
"""Missing required field(s) when construting STIX object."""
|
"""Missing required field(s) when constructing STIX object."""
|
||||||
|
|
||||||
def __init__(self, cls, fields):
|
def __init__(self, cls, fields):
|
||||||
super(MissingFieldsError, self).__init__()
|
super(MissingFieldsError, self).__init__()
|
||||||
|
@ -28,3 +28,17 @@ class MissingFieldsError(STIXError, ValueError):
|
||||||
msg = "Missing required field(s) for {0}: ({1})."
|
msg = "Missing required field(s) for {0}: ({1})."
|
||||||
return msg.format(self.cls.__name__,
|
return msg.format(self.cls.__name__,
|
||||||
", ".join(x for x in self.fields))
|
", ".join(x for x in self.fields))
|
||||||
|
|
||||||
|
|
||||||
|
class ExtraFieldsError(STIXError, TypeError):
|
||||||
|
"""Extra field(s) were provided when constructing STIX object."""
|
||||||
|
|
||||||
|
def __init__(self, cls, fields):
|
||||||
|
super(ExtraFieldsError, self).__init__()
|
||||||
|
self.cls = cls
|
||||||
|
self.fields = sorted(list(fields))
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
msg = "Unexpected field(s) for {0}: ({1})."
|
||||||
|
return msg.format(self.cls.__name__,
|
||||||
|
", ".join(x for x in self.fields))
|
||||||
|
|
|
@ -132,9 +132,12 @@ def test_cannot_assign_to_indicator_attributes(indicator):
|
||||||
|
|
||||||
|
|
||||||
def test_invalid_kwarg_to_indicator():
|
def test_invalid_kwarg_to_indicator():
|
||||||
with pytest.raises(TypeError) as excinfo:
|
with pytest.raises(stix2.exceptions.ExtraFieldsError) as excinfo:
|
||||||
stix2.Indicator(my_custom_property="foo", **INDICATOR_KWARGS)
|
stix2.Indicator(my_custom_property="foo", **INDICATOR_KWARGS)
|
||||||
assert str(excinfo.value) == "unexpected keyword arguments: ['my_custom_property']"
|
|
||||||
|
assert excinfo.value.cls == stix2.Indicator
|
||||||
|
assert excinfo.value.fields == ['my_custom_property']
|
||||||
|
assert str(excinfo.value) == "Unexpected field(s) for Indicator: (my_custom_property)."
|
||||||
|
|
||||||
|
|
||||||
def test_created_modified_time_are_identical_by_default():
|
def test_created_modified_time_are_identical_by_default():
|
||||||
|
|
|
@ -96,6 +96,9 @@ def test_cannot_assign_to_malware_attributes(malware):
|
||||||
|
|
||||||
|
|
||||||
def test_invalid_kwarg_to_malware():
|
def test_invalid_kwarg_to_malware():
|
||||||
with pytest.raises(TypeError) as excinfo:
|
with pytest.raises(stix2.exceptions.ExtraFieldsError) as excinfo:
|
||||||
stix2.Malware(my_custom_property="foo", **MALWARE_KWARGS)
|
stix2.Malware(my_custom_property="foo", **MALWARE_KWARGS)
|
||||||
assert str(excinfo.value) == "unexpected keyword arguments: ['my_custom_property']"
|
|
||||||
|
assert excinfo.value.cls == stix2.Malware
|
||||||
|
assert excinfo.value.fields == ['my_custom_property']
|
||||||
|
assert str(excinfo.value) == "Unexpected field(s) for Malware: (my_custom_property)."
|
||||||
|
|
|
@ -105,9 +105,12 @@ def test_cannot_assign_to_relationship_attributes(relationship):
|
||||||
|
|
||||||
|
|
||||||
def test_invalid_kwarg_to_relationship():
|
def test_invalid_kwarg_to_relationship():
|
||||||
with pytest.raises(TypeError) as excinfo:
|
with pytest.raises(stix2.exceptions.ExtraFieldsError) as excinfo:
|
||||||
stix2.Relationship(my_custom_property="foo", **RELATIONSHIP_KWARGS)
|
stix2.Relationship(my_custom_property="foo", **RELATIONSHIP_KWARGS)
|
||||||
assert str(excinfo.value) == "unexpected keyword arguments: ['my_custom_property']" in str(excinfo)
|
|
||||||
|
assert excinfo.value.cls == stix2.Relationship
|
||||||
|
assert excinfo.value.fields == ['my_custom_property']
|
||||||
|
assert str(excinfo.value) == "Unexpected field(s) for Relationship: (my_custom_property)."
|
||||||
|
|
||||||
|
|
||||||
def test_create_relationship_from_objects_rather_than_ids(indicator, malware):
|
def test_create_relationship_from_objects_rather_than_ids(indicator, malware):
|
||||||
|
|
|
@ -75,9 +75,12 @@ def test_sighting_type_must_be_sightings():
|
||||||
|
|
||||||
|
|
||||||
def test_invalid_kwarg_to_sighting():
|
def test_invalid_kwarg_to_sighting():
|
||||||
with pytest.raises(TypeError) as excinfo:
|
with pytest.raises(stix2.exceptions.ExtraFieldsError) as excinfo:
|
||||||
stix2.Sighting(my_custom_property="foo", **SIGHTING_KWARGS)
|
stix2.Sighting(my_custom_property="foo", **SIGHTING_KWARGS)
|
||||||
assert str(excinfo.value) == "unexpected keyword arguments: ['my_custom_property']" in str(excinfo)
|
|
||||||
|
assert excinfo.value.cls == stix2.Sighting
|
||||||
|
assert excinfo.value.fields == ['my_custom_property']
|
||||||
|
assert str(excinfo.value) == "Unexpected field(s) for Sighting: (my_custom_property)."
|
||||||
|
|
||||||
|
|
||||||
def test_create_sighting_from_objects_rather_than_ids(malware): # noqa: F811
|
def test_create_sighting_from_objects_rather_than_ids(malware): # noqa: F811
|
||||||
|
|
Loading…
Reference in New Issue