Create custom exception class for modifying an immutable object.
parent
91cecb7b0c
commit
6bf3584616
|
@ -4,7 +4,8 @@ import collections
|
||||||
import datetime as dt
|
import datetime as dt
|
||||||
import json
|
import json
|
||||||
|
|
||||||
from .exceptions import ExtraFieldsError, InvalidValueError, MissingFieldsError
|
from .exceptions import ExtraFieldsError, ImmutableError, InvalidValueError, \
|
||||||
|
MissingFieldsError
|
||||||
from .utils import format_datetime, get_timestamp, NOW
|
from .utils import format_datetime, get_timestamp, NOW
|
||||||
|
|
||||||
__all__ = ['STIXJSONEncoder', '_STIXBase']
|
__all__ = ['STIXJSONEncoder', '_STIXBase']
|
||||||
|
@ -85,8 +86,7 @@ class _STIXBase(collections.Mapping):
|
||||||
|
|
||||||
def __setattr__(self, name, value):
|
def __setattr__(self, name, value):
|
||||||
if name != '_inner' and not name.startswith("_STIXBase__"):
|
if name != '_inner' and not name.startswith("_STIXBase__"):
|
||||||
print(name)
|
raise ImmutableError
|
||||||
raise ValueError("Cannot modify properties after creation.")
|
|
||||||
super(_STIXBase, self).__setattr__(name, value)
|
super(_STIXBase, self).__setattr__(name, value)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
|
|
|
@ -42,3 +42,10 @@ class ExtraFieldsError(STIXError, TypeError):
|
||||||
msg = "Unexpected field(s) for {0}: ({1})."
|
msg = "Unexpected 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 ImmutableError(STIXError, ValueError):
|
||||||
|
"""Attempted to modify an object after creation"""
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
super(ImmutableError, self).__init__("Cannot modify properties after creation.")
|
||||||
|
|
|
@ -125,7 +125,7 @@ def test_indicator_revoked_invalid():
|
||||||
|
|
||||||
|
|
||||||
def test_cannot_assign_to_indicator_attributes(indicator):
|
def test_cannot_assign_to_indicator_attributes(indicator):
|
||||||
with pytest.raises(ValueError) as excinfo:
|
with pytest.raises(stix2.exceptions.ImmutableError) as excinfo:
|
||||||
indicator.valid_from = dt.datetime.now()
|
indicator.valid_from = dt.datetime.now()
|
||||||
|
|
||||||
assert str(excinfo.value) == "Cannot modify properties after creation."
|
assert str(excinfo.value) == "Cannot modify properties after creation."
|
||||||
|
|
|
@ -89,7 +89,7 @@ def test_malware_required_field_name():
|
||||||
|
|
||||||
|
|
||||||
def test_cannot_assign_to_malware_attributes(malware):
|
def test_cannot_assign_to_malware_attributes(malware):
|
||||||
with pytest.raises(ValueError) as excinfo:
|
with pytest.raises(stix2.exceptions.ImmutableError) as excinfo:
|
||||||
malware.name = "Cryptolocker II"
|
malware.name = "Cryptolocker II"
|
||||||
|
|
||||||
assert str(excinfo.value) == "Cannot modify properties after creation."
|
assert str(excinfo.value) == "Cannot modify properties after creation."
|
||||||
|
|
|
@ -98,7 +98,7 @@ def test_relationship_required_field_target_ref():
|
||||||
|
|
||||||
|
|
||||||
def test_cannot_assign_to_relationship_attributes(relationship):
|
def test_cannot_assign_to_relationship_attributes(relationship):
|
||||||
with pytest.raises(ValueError) as excinfo:
|
with pytest.raises(stix2.exceptions.ImmutableError) as excinfo:
|
||||||
relationship.relationship_type = "derived-from"
|
relationship.relationship_type = "derived-from"
|
||||||
|
|
||||||
assert str(excinfo.value) == "Cannot modify properties after creation."
|
assert str(excinfo.value) == "Cannot modify properties after creation."
|
||||||
|
|
Loading…
Reference in New Issue