Add exception for extra/invalid custom properties.

stix2.1
Greg Back 2017-04-18 14:56:16 -05:00
parent 32ff00559e
commit 91cecb7b0c
6 changed files with 37 additions and 11 deletions

View File

@ -4,7 +4,7 @@ import collections
import datetime as dt
import json
from .exceptions import InvalidValueError, MissingFieldsError
from .exceptions import ExtraFieldsError, InvalidValueError, MissingFieldsError
from .utils import format_datetime, get_timestamp, NOW
__all__ = ['STIXJSONEncoder', '_STIXBase']
@ -53,7 +53,7 @@ class _STIXBase(collections.Mapping):
# Detect any keyword arguments not allowed for a specific type
extra_kwargs = list(set(kwargs) - set(cls._properties))
if extra_kwargs:
raise TypeError("unexpected keyword arguments: " + str(extra_kwargs))
raise ExtraFieldsError(cls, extra_kwargs)
required_fields = get_required_properties(cls._properties)
missing_kwargs = set(required_fields) - set(kwargs)

View File

@ -17,7 +17,7 @@ class InvalidValueError(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):
super(MissingFieldsError, self).__init__()
@ -28,3 +28,17 @@ class MissingFieldsError(STIXError, ValueError):
msg = "Missing required field(s) for {0}: ({1})."
return msg.format(self.cls.__name__,
", ".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))

View File

@ -132,9 +132,12 @@ def test_cannot_assign_to_indicator_attributes(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)
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():

View File

@ -96,6 +96,9 @@ def test_cannot_assign_to_malware_attributes(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)
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)."

View File

@ -105,9 +105,12 @@ def test_cannot_assign_to_relationship_attributes(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)
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):

View File

@ -75,9 +75,12 @@ def test_sighting_type_must_be_sightings():
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)
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