Update how fixtures work during testing.
parent
022f344b94
commit
e4e75e459b
|
@ -1,5 +1,5 @@
|
||||||
import collections
|
import collections
|
||||||
from datetime import datetime
|
import datetime
|
||||||
import json
|
import json
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
|
@ -72,7 +72,7 @@ class Indicator(_STIXBase):
|
||||||
|
|
||||||
# TODO: do we care about the performance penalty of creating this
|
# TODO: do we care about the performance penalty of creating this
|
||||||
# if we won't need it?
|
# if we won't need it?
|
||||||
now = datetime.now(tz=pytz.UTC)
|
now = datetime.datetime.now(tz=pytz.UTC)
|
||||||
|
|
||||||
if not kwargs.get('type'):
|
if not kwargs.get('type'):
|
||||||
kwargs['type'] = 'indicator'
|
kwargs['type'] = 'indicator'
|
||||||
|
@ -140,7 +140,7 @@ class Malware(_STIXBase):
|
||||||
|
|
||||||
# TODO: do we care about the performance penalty of creating this
|
# TODO: do we care about the performance penalty of creating this
|
||||||
# if we won't need it?
|
# if we won't need it?
|
||||||
now = datetime.now(tz=pytz.UTC)
|
now = datetime.datetime.now(tz=pytz.UTC)
|
||||||
|
|
||||||
if not kwargs.get('type'):
|
if not kwargs.get('type'):
|
||||||
kwargs['type'] = 'malware'
|
kwargs['type'] = 'malware'
|
||||||
|
@ -215,7 +215,7 @@ class Relationship(_STIXBase):
|
||||||
|
|
||||||
# TODO: do we care about the performance penalty of creating this
|
# TODO: do we care about the performance penalty of creating this
|
||||||
# if we won't need it?
|
# if we won't need it?
|
||||||
now = datetime.now(tz=pytz.UTC)
|
now = datetime.datetime.now(tz=pytz.UTC)
|
||||||
|
|
||||||
if not kwargs.get('type'):
|
if not kwargs.get('type'):
|
||||||
kwargs['type'] = 'relationship'
|
kwargs['type'] = 'relationship'
|
||||||
|
|
|
@ -10,7 +10,7 @@ import stix2
|
||||||
|
|
||||||
amsterdam = pytz.timezone('Europe/Amsterdam')
|
amsterdam = pytz.timezone('Europe/Amsterdam')
|
||||||
eastern = pytz.timezone('US/Eastern')
|
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
|
# Inspired by: http://stackoverflow.com/a/24006251
|
||||||
|
@ -19,12 +19,16 @@ def clock(monkeypatch):
|
||||||
|
|
||||||
class mydatetime(datetime.datetime):
|
class mydatetime(datetime.datetime):
|
||||||
@classmethod
|
@classmethod
|
||||||
def now(cls):
|
def now(cls, tz=None):
|
||||||
return FAKE_TIME
|
return FAKE_TIME
|
||||||
|
|
||||||
monkeypatch.setattr(datetime, 'datetime', mydatetime)
|
monkeypatch.setattr(datetime, 'datetime', mydatetime)
|
||||||
|
|
||||||
|
|
||||||
|
def test_clock(clock):
|
||||||
|
assert datetime.datetime.now() == FAKE_TIME
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def uuid4(monkeypatch):
|
def uuid4(monkeypatch):
|
||||||
def wrapper():
|
def wrapper():
|
||||||
|
@ -47,7 +51,6 @@ def test_my_uuid4_fixture(uuid4):
|
||||||
assert uuid.uuid4() == "00000000-0000-0000-0000-000000000104"
|
assert uuid.uuid4() == "00000000-0000-0000-0000-000000000104"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('dt,timestamp', [
|
@pytest.mark.parametrize('dt,timestamp', [
|
||||||
(datetime.datetime(2017, 1, 1, tzinfo=pytz.utc), '2017-01-01T00:00:00Z'),
|
(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'),
|
(amsterdam.localize(datetime.datetime(2017, 1, 1)), '2016-12-31T23:00:00Z'),
|
||||||
|
@ -83,17 +86,17 @@ RELATIONSHIP_KWARGS = dict(
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def indicator():
|
def indicator(uuid4, clock):
|
||||||
return stix2.Indicator(id=INDICATOR_ID, **INDICATOR_KWARGS)
|
return stix2.Indicator(**INDICATOR_KWARGS)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def malware():
|
def malware(uuid4, clock):
|
||||||
return stix2.Malware(id=MALWARE_ID, **MALWARE_KWARGS)
|
return stix2.Malware(**MALWARE_KWARGS)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def relationship():
|
def relationship(uuid4, clock):
|
||||||
return stix2.Relationship(**RELATIONSHIP_KWARGS)
|
return stix2.Relationship(**RELATIONSHIP_KWARGS)
|
||||||
|
|
||||||
|
|
||||||
|
@ -129,20 +132,20 @@ def test_indicator_with_all_required_fields():
|
||||||
|
|
||||||
def test_indicator_autogenerated_fields(indicator):
|
def test_indicator_autogenerated_fields(indicator):
|
||||||
assert indicator.type == 'indicator'
|
assert indicator.type == 'indicator'
|
||||||
assert indicator.id.startswith('indicator--')
|
assert indicator.id == 'indicator--00000000-0000-0000-0000-000000000001'
|
||||||
assert indicator.created is not None
|
assert indicator.created == FAKE_TIME
|
||||||
assert indicator.modified is not None
|
assert indicator.modified == FAKE_TIME
|
||||||
assert indicator.labels == ['malicious-activity']
|
assert indicator.labels == ['malicious-activity']
|
||||||
assert indicator.pattern == "[file:hashes.MD5 = 'd41d8cd98f00b204e9800998ecf8427e']"
|
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['type'] == 'indicator'
|
||||||
assert indicator['id'].startswith('indicator--')
|
assert indicator['id'] == 'indicator--00000000-0000-0000-0000-000000000001'
|
||||||
assert indicator['created'] is not None
|
assert indicator['created'] == FAKE_TIME
|
||||||
assert indicator['modified'] is not None
|
assert indicator['modified'] == FAKE_TIME
|
||||||
assert indicator['labels'] == ['malicious-activity']
|
assert indicator['labels'] == ['malicious-activity']
|
||||||
assert indicator['pattern'] == "[file:hashes.MD5 = 'd41d8cd98f00b204e9800998ecf8427e']"
|
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():
|
def test_indicator_type_must_be_indicator():
|
||||||
|
@ -214,16 +217,16 @@ def test_malware_with_all_required_fields():
|
||||||
|
|
||||||
def test_malware_autogenerated_fields(malware):
|
def test_malware_autogenerated_fields(malware):
|
||||||
assert malware.type == 'malware'
|
assert malware.type == 'malware'
|
||||||
assert malware.id.startswith('malware--')
|
assert malware.id == 'malware--00000000-0000-0000-0000-000000000001'
|
||||||
assert malware.created is not None
|
assert malware.created == FAKE_TIME
|
||||||
assert malware.modified is not None
|
assert malware.modified == FAKE_TIME
|
||||||
assert malware.labels == ['ransomware']
|
assert malware.labels == ['ransomware']
|
||||||
assert malware.name == "Cryptolocker"
|
assert malware.name == "Cryptolocker"
|
||||||
|
|
||||||
assert malware['type'] == 'malware'
|
assert malware['type'] == 'malware'
|
||||||
assert malware['id'].startswith('malware--')
|
assert malware['id'] == 'malware--00000000-0000-0000-0000-000000000001'
|
||||||
assert malware['created'] is not None
|
assert malware['created'] == FAKE_TIME
|
||||||
assert malware['modified'] is not None
|
assert malware['modified'] == FAKE_TIME
|
||||||
assert malware['labels'] == ['ransomware']
|
assert malware['labels'] == ['ransomware']
|
||||||
assert malware['name'] == "Cryptolocker"
|
assert malware['name'] == "Cryptolocker"
|
||||||
|
|
||||||
|
@ -296,17 +299,17 @@ def test_relationship_all_required_fields():
|
||||||
|
|
||||||
def test_relationship_autogenerated_fields(relationship):
|
def test_relationship_autogenerated_fields(relationship):
|
||||||
assert relationship.type == 'relationship'
|
assert relationship.type == 'relationship'
|
||||||
assert relationship.id.startswith('relationship--')
|
assert relationship.id == 'relationship--00000000-0000-0000-0000-000000000001'
|
||||||
assert relationship.created is not None
|
assert relationship.created == FAKE_TIME
|
||||||
assert relationship.modified is not None
|
assert relationship.modified == FAKE_TIME
|
||||||
assert relationship.relationship_type == 'indicates'
|
assert relationship.relationship_type == 'indicates'
|
||||||
assert relationship.source_ref == INDICATOR_ID
|
assert relationship.source_ref == INDICATOR_ID
|
||||||
assert relationship.target_ref == MALWARE_ID
|
assert relationship.target_ref == MALWARE_ID
|
||||||
|
|
||||||
assert relationship['type'] == 'relationship'
|
assert relationship['type'] == 'relationship'
|
||||||
assert relationship['id'].startswith('relationship--')
|
assert relationship['id'] == 'relationship--00000000-0000-0000-0000-000000000001'
|
||||||
assert relationship['created'] is not None
|
assert relationship['created'] == FAKE_TIME
|
||||||
assert relationship['modified'] is not None
|
assert relationship['modified'] == FAKE_TIME
|
||||||
assert relationship['relationship_type'] == 'indicates'
|
assert relationship['relationship_type'] == 'indicates'
|
||||||
assert relationship['source_ref'] == INDICATOR_ID
|
assert relationship['source_ref'] == INDICATOR_ID
|
||||||
assert relationship['target_ref'] == MALWARE_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.relationship_type == 'indicates'
|
||||||
assert relationship.source_ref == INDICATOR_ID
|
assert relationship.source_ref == 'indicator--00000000-0000-0000-0000-000000000001'
|
||||||
assert relationship.target_ref == MALWARE_ID
|
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):
|
def test_create_relationship_with_positional_args(indicator, malware):
|
||||||
relationship = stix2.Relationship(indicator, 'indicates', malware)
|
relationship = stix2.Relationship(indicator, 'indicates', malware)
|
||||||
|
|
||||||
assert relationship.relationship_type == 'indicates'
|
assert relationship.relationship_type == 'indicates'
|
||||||
assert relationship.source_ref == INDICATOR_ID
|
assert relationship.source_ref == 'indicator--00000000-0000-0000-0000-000000000001'
|
||||||
assert relationship.target_ref == MALWARE_ID
|
assert relationship.target_ref == 'malware--00000000-0000-0000-0000-000000000002'
|
||||||
|
assert relationship.id == 'relationship--00000000-0000-0000-0000-000000000003'
|
||||||
|
|
Loading…
Reference in New Issue