Update how fixtures work during testing.

stix2.1
Greg Back 2017-01-18 16:57:26 -08:00
parent 022f344b94
commit e4e75e459b
2 changed files with 41 additions and 36 deletions

View File

@ -1,5 +1,5 @@
import collections
from datetime import datetime
import datetime
import json
import uuid
@ -72,7 +72,7 @@ class Indicator(_STIXBase):
# TODO: do we care about the performance penalty of creating this
# if we won't need it?
now = datetime.now(tz=pytz.UTC)
now = datetime.datetime.now(tz=pytz.UTC)
if not kwargs.get('type'):
kwargs['type'] = 'indicator'
@ -140,7 +140,7 @@ class Malware(_STIXBase):
# TODO: do we care about the performance penalty of creating this
# if we won't need it?
now = datetime.now(tz=pytz.UTC)
now = datetime.datetime.now(tz=pytz.UTC)
if not kwargs.get('type'):
kwargs['type'] = 'malware'
@ -215,7 +215,7 @@ class Relationship(_STIXBase):
# TODO: do we care about the performance penalty of creating this
# if we won't need it?
now = datetime.now(tz=pytz.UTC)
now = datetime.datetime.now(tz=pytz.UTC)
if not kwargs.get('type'):
kwargs['type'] = 'relationship'

View File

@ -10,7 +10,7 @@ import stix2
amsterdam = pytz.timezone('Europe/Amsterdam')
eastern = pytz.timezone('US/Eastern')
FAKE_TIME = datetime.datetime(2017, 01, 01, 12, 34, 56)
FAKE_TIME = datetime.datetime(2017, 01, 01, 12, 34, 56, tzinfo=pytz.utc)
# Inspired by: http://stackoverflow.com/a/24006251
@ -19,12 +19,16 @@ def clock(monkeypatch):
class mydatetime(datetime.datetime):
@classmethod
def now(cls):
def now(cls, tz=None):
return FAKE_TIME
monkeypatch.setattr(datetime, 'datetime', mydatetime)
def test_clock(clock):
assert datetime.datetime.now() == FAKE_TIME
@pytest.fixture
def uuid4(monkeypatch):
def wrapper():
@ -47,7 +51,6 @@ def test_my_uuid4_fixture(uuid4):
assert uuid.uuid4() == "00000000-0000-0000-0000-000000000104"
@pytest.mark.parametrize('dt,timestamp', [
(datetime.datetime(2017, 1, 1, tzinfo=pytz.utc), '2017-01-01T00:00:00Z'),
(amsterdam.localize(datetime.datetime(2017, 1, 1)), '2016-12-31T23:00:00Z'),
@ -83,17 +86,17 @@ RELATIONSHIP_KWARGS = dict(
@pytest.fixture
def indicator():
return stix2.Indicator(id=INDICATOR_ID, **INDICATOR_KWARGS)
def indicator(uuid4, clock):
return stix2.Indicator(**INDICATOR_KWARGS)
@pytest.fixture
def malware():
return stix2.Malware(id=MALWARE_ID, **MALWARE_KWARGS)
def malware(uuid4, clock):
return stix2.Malware(**MALWARE_KWARGS)
@pytest.fixture
def relationship():
def relationship(uuid4, clock):
return stix2.Relationship(**RELATIONSHIP_KWARGS)
@ -129,20 +132,20 @@ def test_indicator_with_all_required_fields():
def test_indicator_autogenerated_fields(indicator):
assert indicator.type == 'indicator'
assert indicator.id.startswith('indicator--')
assert indicator.created is not None
assert indicator.modified is not None
assert indicator.id == 'indicator--00000000-0000-0000-0000-000000000001'
assert indicator.created == FAKE_TIME
assert indicator.modified == FAKE_TIME
assert indicator.labels == ['malicious-activity']
assert indicator.pattern == "[file:hashes.MD5 = 'd41d8cd98f00b204e9800998ecf8427e']"
assert indicator.valid_from is not None
assert indicator.valid_from == FAKE_TIME
assert indicator['type'] == 'indicator'
assert indicator['id'].startswith('indicator--')
assert indicator['created'] is not None
assert indicator['modified'] is not None
assert indicator['id'] == 'indicator--00000000-0000-0000-0000-000000000001'
assert indicator['created'] == FAKE_TIME
assert indicator['modified'] == FAKE_TIME
assert indicator['labels'] == ['malicious-activity']
assert indicator['pattern'] == "[file:hashes.MD5 = 'd41d8cd98f00b204e9800998ecf8427e']"
assert indicator['valid_from'] is not None
assert indicator['valid_from'] == FAKE_TIME
def test_indicator_type_must_be_indicator():
@ -214,16 +217,16 @@ def test_malware_with_all_required_fields():
def test_malware_autogenerated_fields(malware):
assert malware.type == 'malware'
assert malware.id.startswith('malware--')
assert malware.created is not None
assert malware.modified is not None
assert malware.id == 'malware--00000000-0000-0000-0000-000000000001'
assert malware.created == FAKE_TIME
assert malware.modified == FAKE_TIME
assert malware.labels == ['ransomware']
assert malware.name == "Cryptolocker"
assert malware['type'] == 'malware'
assert malware['id'].startswith('malware--')
assert malware['created'] is not None
assert malware['modified'] is not None
assert malware['id'] == 'malware--00000000-0000-0000-0000-000000000001'
assert malware['created'] == FAKE_TIME
assert malware['modified'] == FAKE_TIME
assert malware['labels'] == ['ransomware']
assert malware['name'] == "Cryptolocker"
@ -296,17 +299,17 @@ def test_relationship_all_required_fields():
def test_relationship_autogenerated_fields(relationship):
assert relationship.type == 'relationship'
assert relationship.id.startswith('relationship--')
assert relationship.created is not None
assert relationship.modified is not None
assert relationship.id == 'relationship--00000000-0000-0000-0000-000000000001'
assert relationship.created == FAKE_TIME
assert relationship.modified == FAKE_TIME
assert relationship.relationship_type == 'indicates'
assert relationship.source_ref == INDICATOR_ID
assert relationship.target_ref == MALWARE_ID
assert relationship['type'] == 'relationship'
assert relationship['id'].startswith('relationship--')
assert relationship['created'] is not None
assert relationship['modified'] is not None
assert relationship['id'] == 'relationship--00000000-0000-0000-0000-000000000001'
assert relationship['created'] == FAKE_TIME
assert relationship['modified'] == FAKE_TIME
assert relationship['relationship_type'] == 'indicates'
assert relationship['source_ref'] == INDICATOR_ID
assert relationship['target_ref'] == MALWARE_ID
@ -370,13 +373,15 @@ def test_create_relationship_from_objects_rather_than_ids(indicator, malware):
)
assert relationship.relationship_type == 'indicates'
assert relationship.source_ref == INDICATOR_ID
assert relationship.target_ref == MALWARE_ID
assert relationship.source_ref == 'indicator--00000000-0000-0000-0000-000000000001'
assert relationship.target_ref == 'malware--00000000-0000-0000-0000-000000000002'
assert relationship.id == 'relationship--00000000-0000-0000-0000-000000000003'
def test_create_relationship_with_positional_args(indicator, malware):
relationship = stix2.Relationship(indicator, 'indicates', malware)
assert relationship.relationship_type == 'indicates'
assert relationship.source_ref == INDICATOR_ID
assert relationship.target_ref == MALWARE_ID
assert relationship.source_ref == 'indicator--00000000-0000-0000-0000-000000000001'
assert relationship.target_ref == 'malware--00000000-0000-0000-0000-000000000002'
assert relationship.id == 'relationship--00000000-0000-0000-0000-000000000003'