# Path: spec/python/db_connection.py
# TEST_DATABASE_MYSQL = 'samapper_test'
# TEST_DATABASE_SQLITE = '/tmp/sam_test.db'
# class Mocker(object):
# class Session(dict):
# class env(object):
#     def __init__(self, *args, **kwargs):
#     def __getitem__(self, k):
#     def __setitem__(self, k, v):
#     def __getattr__(self, name):
#         def receiver(*args, **kwargs):
#     def __call__(self, *args, **kwargs):
#     def clear(self):
#     def was_called(self, name, *args, **kwargs):
#     def kill(self):
#     def __init__(self, mock_input=False, login_active=None, mock_session=False, mock_render=False, lang='en'):
#     def __enter__(self):
#     def __exit__(self, type, value, traceback):
# def make_timestamp(timestring):
# def unix_timestamp_to_datetime(stamp):
# def get_test_db_connection():
# def setup_datasources(db, sub_id):
# def template_sql(path, *args):
# def setup_network(db, sub_id, ds_id):
# def setup_node_extras(sub_id):
# 
# Path: sam/models/security/rule_template.py
# def get_all():
# def abs_rule_path(path):
# def get_definition(path, cache={}):
#     def __init__(self, path, yml):
#     def import_yml(self, data):
#     def load_exposed(yml):
#     def load_inclusions(self, yml):
#     def load_action_defaults(yml):
#     def get_exposed(self):
#     def get_action_defaults(self):
#     def get_inclusions(self):
# class RuleTemplate(object):
# 
# Path: sam/constants.py
# LDAP = {
#     'connection_string': config.get('LDAP', 'connection_string', default='')
# }
# def enable_local_mode():
# def init_urls():
# def find_url(target):
# def get_navbar(lang):


import os
import re
import yaml
    import pprint
from spec.python import db_connection
from sam.models.security import rule, rule_template
from sam import constants

def get_dummy_rule():
    base_path = os.path.dirname(__file__)
    rule_path = os.path.join(base_path, os.path.pardir, os.path.pardir, constants.templates_folder, "test_rule.yml")
    r = rule.Rule(123, True, "My Test Rule", "Description of My Test Rule", rule_path)
    r.active = True
    r.exposed_params = r.get_initial_exposed_params()
    r.action_params = r.get_initial_action_params()
    return r


def test_validate_rule():
    # validate_rule(path, db, sub_id, ds_id, args)
    base_path = os.path.dirname(__file__)
    rule_path = os.path.join(base_path, os.path.pardir, os.path.pardir, constants.templates_folder, "test_rule.yml")
    assert rule.validate_rule(rule_path, db, sub_id, ds_full, None) == True

    paths = [os.path.join(constants.rule_templates_path, p) for p in os.listdir(constants.rule_templates_path) if p.endswith("yml")]
    for p in paths:
        print("testing {}".format(p))
        assert rule.validate_rule(p, db, sub_id, ds_full, None) == True


def test_init():
    r = rule.Rule(123, True, "my rule", "description of my rule", 'compromised.yml')
    assert r.id == 123
    assert r.active == True
    assert r.name == "my rule"
    assert r.desc == "description of my rule"
    assert r.path == "compromised.yml"

# Path: spec/python/db_connection.py
# TEST_DATABASE_MYSQL = 'samapper_test'
# TEST_DATABASE_SQLITE = '/tmp/sam_test.db'
# class Mocker(object):
# class Session(dict):
# class env(object):
#     def __init__(self, *args, **kwargs):
#     def __getitem__(self, k):
#     def __setitem__(self, k, v):
#     def __getattr__(self, name):
#         def receiver(*args, **kwargs):
#     def __call__(self, *args, **kwargs):
#     def clear(self):
#     def was_called(self, name, *args, **kwargs):
#     def kill(self):
#     def __init__(self, mock_input=False, login_active=None, mock_session=False, mock_render=False, lang='en'):
#     def __enter__(self):
#     def __exit__(self, type, value, traceback):
# def make_timestamp(timestring):
# def unix_timestamp_to_datetime(stamp):
# def get_test_db_connection():
# def setup_datasources(db, sub_id):
# def template_sql(path, *args):
# def setup_network(db, sub_id, ds_id):
# def setup_node_extras(sub_id):
# 
# Path: sam/models/security/rule_template.py
# def get_all():
# def abs_rule_path(path):
# def get_definition(path, cache={}):
#     def __init__(self, path, yml):
#     def import_yml(self, data):
#     def load_exposed(yml):
#     def load_inclusions(self, yml):
#     def load_action_defaults(yml):
#     def get_exposed(self):
#     def get_action_defaults(self):
#     def get_inclusions(self):
# class RuleTemplate(object):
# 
# Path: sam/constants.py
# LDAP = {
#     'connection_string': config.get('LDAP', 'connection_string', default='')
# }
# def enable_local_mode():
# def init_urls():
# def find_url(target):
# def get_navbar(lang):


# Path: spec/python/models/security/test_rule.py
import os
import re
import yaml
    import pprint
from spec.python import db_connection
from sam.models.security import rule, rule_template
from sam import constants


def get_dummy_rule():
    base_path = os.path.dirname(__file__)
    rule_path = os.path.join(base_path, os.path.pardir, os.path.pardir, constants.templates_folder, "test_rule.yml")
    r = rule.Rule(123, True, "My Test Rule", "Description of My Test Rule", rule_path)
    r.active = True
    r.exposed_params = r.get_initial_exposed_params()
    r.action_params = r.get_initial_action_params()
    return r


def test_validate_rule():
    # validate_rule(path, db, sub_id, ds_id, args)
    base_path = os.path.dirname(__file__)
    rule_path = os.path.join(base_path, os.path.pardir, os.path.pardir, constants.templates_folder, "test_rule.yml")
    assert rule.validate_rule(rule_path, db, sub_id, ds_full, None) == True

    paths = [os.path.join(constants.rule_templates_path, p) for p in os.listdir(constants.rule_templates_path) if p.endswith("yml")]
    for p in paths:
        print("testing {}".format(p))
        assert rule.validate_rule(p, db, sub_id, ds_full, None) == True


def test_init():
    r = rule.Rule(123, True, "my rule", "description of my rule", 'compromised.yml')
    assert r.id == 123
    assert r.active == True
    assert r.name == "my rule"
    assert r.desc == "description of my rule"
    assert r.path == "compromised.yml"

    assert isinstance(r.definition, rule_template.RuleTemplate)