# Path: doorman/utils.py
# def assemble_configuration(node):
# def assemble_options(node):
# def assemble_file_paths(node):
# def assemble_schedule(node):
# def assemble_packs(node):
# def assemble_distributed_queries(node):
# def create_query_pack_from_upload(upload):
# def get_node_health(node):
# def pretty_operator(cond):
# def pretty_field(field):
# def quote(s, quote='"'):
# def create_mock_db():
# def validate_osquery_query(query):
# def learn_from_result(result, node):
# def process_result(result, node):
# def extract_results(result):
# def flash_errors(form):
# def get_paginate_options(request, model, choices, existing_query=None,
#                          default='id', page=1, max_pp=20, default_sort='asc'):
#     def default(self, o):
# def render_column(value, column):
# PRETTY_OPERATORS = {
#     'equal': 'equals',
#     'not_equal': "doesn't equal",
#     'begins_with': 'begins with',
#     'not_begins_with': "doesn't begins with",
#     'contains': 'contains',
#     'not_contains': "doesn't contain",
#     'ends_with': 'ends with',
#     'not_ends_with': "doesn't end with",
#     'is_empty': 'is empty',
#     'is_not_empty': 'is not empty',
#     'less': 'less than',
#     'less_or_equal': 'less than or equal',
#     'greater': 'greater than',
#     'greater_or_equal': 'greater than or equal',
#     'matches_regex': 'matches regex',
#     'not_matches_regex': "doesn't match regex",
# }
# PRETTY_FIELDS = {
#     'query_name': 'Query name',
#     'action': 'Action',
#     'host_identifier': 'Host identifier',
#     'timestamp': 'Timestamp',
# }
# PRINTABLE = string.ascii_letters + string.digits + string.punctuation + ' '
# class DateTimeEncoder(json.JSONEncoder):


import json
import datetime as dt
import pytest
from flask import current_app
from doorman.utils import (
    DateTimeEncoder,
    osquery_mock_db,
    quote,
    validate_osquery_query,
)
        assert validate_osquery_query(query) is False

    def test_bad_table(self):
        query = 'SELECT * FROM a_table_that_does_not_exist;'
        assert validate_osquery_query(query) is False

    def test_custom_schema(self):
        query = 'SELECT * FROM custom_table;'
        assert validate_osquery_query(query) is False

        try:
            # This is a bit hacky, but it clears the mock DB so the next call
            # will re-create it.
            osquery_mock_db.db = None

            current_app.config['DOORMAN_EXTRA_SCHEMA'].append(
                'CREATE TABLE custom_table (id INTEGER);'
            )
            assert validate_osquery_query(query) is True
        finally:
            # Remove final entry from the config.
            current_app.config['DOORMAN_EXTRA_SCHEMA'] = \
                current_app.config['DOORMAN_EXTRA_SCHEMA'][:-1]


class TestQuote:

    def test_will_quote_string(self):
        assert quote('foobar') == '"foobar"'
        assert quote('foo bar baz') == '"foo bar baz"'

# Path: doorman/utils.py
# def assemble_configuration(node):
# def assemble_options(node):
# def assemble_file_paths(node):
# def assemble_schedule(node):
# def assemble_packs(node):
# def assemble_distributed_queries(node):
# def create_query_pack_from_upload(upload):
# def get_node_health(node):
# def pretty_operator(cond):
# def pretty_field(field):
# def quote(s, quote='"'):
# def create_mock_db():
# def validate_osquery_query(query):
# def learn_from_result(result, node):
# def process_result(result, node):
# def extract_results(result):
# def flash_errors(form):
# def get_paginate_options(request, model, choices, existing_query=None,
#                          default='id', page=1, max_pp=20, default_sort='asc'):
#     def default(self, o):
# def render_column(value, column):
# PRETTY_OPERATORS = {
#     'equal': 'equals',
#     'not_equal': "doesn't equal",
#     'begins_with': 'begins with',
#     'not_begins_with': "doesn't begins with",
#     'contains': 'contains',
#     'not_contains': "doesn't contain",
#     'ends_with': 'ends with',
#     'not_ends_with': "doesn't end with",
#     'is_empty': 'is empty',
#     'is_not_empty': 'is not empty',
#     'less': 'less than',
#     'less_or_equal': 'less than or equal',
#     'greater': 'greater than',
#     'greater_or_equal': 'greater than or equal',
#     'matches_regex': 'matches regex',
#     'not_matches_regex': "doesn't match regex",
# }
# PRETTY_FIELDS = {
#     'query_name': 'Query name',
#     'action': 'Action',
#     'host_identifier': 'Host identifier',
#     'timestamp': 'Timestamp',
# }
# PRINTABLE = string.ascii_letters + string.digits + string.punctuation + ' '
# class DateTimeEncoder(json.JSONEncoder):


# Path: tests/test_utils.py
import json
import datetime as dt
import pytest
from flask import current_app
from doorman.utils import (
    DateTimeEncoder,
    osquery_mock_db,
    quote,
    validate_osquery_query,
)

        assert validate_osquery_query(query) is False

    def test_bad_table(self):
        query = 'SELECT * FROM a_table_that_does_not_exist;'
        assert validate_osquery_query(query) is False

    def test_custom_schema(self):
        query = 'SELECT * FROM custom_table;'
        assert validate_osquery_query(query) is False

        try:
            # This is a bit hacky, but it clears the mock DB so the next call
            # will re-create it.
            osquery_mock_db.db = None

            current_app.config['DOORMAN_EXTRA_SCHEMA'].append(
                'CREATE TABLE custom_table (id INTEGER);'
            )
            assert validate_osquery_query(query) is True
        finally:
            # Remove final entry from the config.
            current_app.config['DOORMAN_EXTRA_SCHEMA'] = \
                current_app.config['DOORMAN_EXTRA_SCHEMA'][:-1]


class TestQuote:

    def test_will_quote_string(self):
        assert quote('foobar') == '"foobar"'
        assert quote('foo bar baz') == '"foo bar baz"'

        assert quote('foobar', quote='`') == '`foobar`'