Ejemplo n.º 1
0
def test_entity_creation():
    """Tests the creation of a single entity."""
    ent = Entity(name='asd')
    assert ent.id is None
    ent = ent.save()
    assert isinstance(ent, Entity)
    assert ent.id is not None
Ejemplo n.º 2
0
def clean_db():
    # pylint: disable=protected-access
    # We need to access the collections to make sure they are in the cache
    Entity._get_collection()
    Malware._get_collection()
    Observable._get_collection()
    Hostname._get_collection()
    Tag._get_collection()
    Vocabs._get_collection()
    db.clear()
Ejemplo n.º 3
0
def test_entity_get_or_create():
    """Tests the get_or_create function on entities."""
    count = len(Entity.list())
    first_object = Entity.get_or_create(name='entity0')
    second_object = Entity.get_or_create(name='entity0')
    assert count == len(Entity.list())
    assert first_object.id == second_object.id
    Entity.get_or_create(name='entity999')
    assert count + 1 == len(Entity.list())
Ejemplo n.º 4
0
def test_different_entity_types():
    """Tests that Entity objects are created with their respective types."""
    allitems = Entity.list()
    malware_count = 0
    entity_count = 0
    for item in allitems:
        if isinstance(item, Malware):
            malware_count += 1
        elif isinstance(item, Entity):
            entity_count += 1
    assert entity_count == 10
    assert malware_count == 2
Ejemplo n.º 5
0
def test_entity_formatting():
    """Tests correct entity formatting to string."""
    ent = Entity(name='asd').save()
    assert str(ent) == "<Entity('asd')>"
Ejemplo n.º 6
0
def test_entitys_list():
    """Tests fetching all entities in the database."""
    allitems = Entity.list()
    assert isinstance(allitems[0], Entity)
    assert len(allitems) == 10
Ejemplo n.º 7
0
def test_invalid_entity_name():
    """Tests that an entity with an invalid name cannot be created."""
    with pytest.raises(ValidationError):
        Entity(name=123).save()
    with pytest.raises(ValidationError):
        Entity(name='').save()
Ejemplo n.º 8
0
def test_entity_get():
    """Tests fetching a single entity by id."""
    ent = Entity(name='asd').save()
    fecthed_entity = Entity.get(ent.id)
    assert isinstance(fecthed_entity, Entity)
    assert fecthed_entity.id == ent.id
Ejemplo n.º 9
0
def test_malware_is_typed():
    """Tests that malware listed through Entities has correct type."""
    all_objects = Entity.list()
    for obj in all_objects:
        assert isinstance(obj, Malware)
Ejemplo n.º 10
0
def populate_entities():
    entities = []
    for num in range(10):
        entity = Entity.get_or_create(name='entity{0:d}'.format(num))
        entities.append(entity)
    return entities