def test_adding_invalid_directory(self): """Test that an invalid directory cannot be added""" session = model.get_db_session() add_directory(directory='/directory/does/not/exist', session=session) dir_count = session.query(model.Directory).count() self.assertEqual(dir_count, 0) session.close()
def test_adding_directory(self): """Test that a directory can be added""" session = model.get_db_session() add_directory(directory='/home', session=session) directory = session.query(model.Directory).first() self.assertEqual(directory.path, '/home') session.close()
def test_adding_duplicate_directory_case_sensitivity(self): """Test that differing case duplicate directories cannot be added""" session = model.get_db_session() add_directory(directory='/home', session=session) dir_count_1 = session.query(model.Directory).count() self.assertEqual(dir_count_1, 1) add_directory(directory='/HOME', session=session) dir_count_2 = session.query(model.Directory).count() self.assertEqual(dir_count_2, 1) session.close()
def test_print_monitored_directories(self): """Test printing monitored directories""" session = model.get_db_session() add_directory(directory='/home', session=session) captured_output = io.StringIO() sys.stdout = captured_output # redirect stdout print_monitored_directories(session=session) self.assertEqual(captured_output.getvalue(), '\nMonitored Directories:\n\n/home\n') session.close() sys.stdout = sys.__stdout__ # stop redirecting stdout
def test_print_monitored_directories_when_none(self): """Test printing directories when none have been added""" captured_output = io.StringIO() sys.stdout = captured_output # redirect stdout session = model.get_db_session() print_monitored_directories(session) self.assertEqual( captured_output.getvalue(), '\nThere are currently no directories being monitored.\n', ) session.close() sys.stdout = sys.__stdout__ # stop redirecting stdout
def setUp(self): settings.DB_LOCATION = "sqlite:///:memory:" self.setUpPyfakefs() self.session = model.get_db_session() self.path_1 = "/home" self.directory_1 = model.Directory(path=self.path_1) self.session.add(self.directory_1) self.path_2 = "/var" self.directory_2 = model.Directory(path=self.path_2) self.session.add(self.directory_2) self.session.commit() self.file_1_name = "file_1" self.file_1_content = "test" self.fake_file_1 = self.fs.create_file( f"{self.path_1}/{self.file_1_name}", contents=self.file_1_content) self.db_file_1 = model.File( name=self.file_1_name, size=len(self.file_1_content), directory_id=self.directory_1.id, ) self.session.add(self.db_file_1) self.file_2_name = "file_2" self.file_2_content = "test" self.fake_file_2 = self.fs.create_file( f"{self.path_1}/{self.file_2_name}") self.db_file_2 = model.File( name=self.file_2_name, size=len(self.file_2_content), directory_id=self.directory_1.id, ) self.session.commit()
def test_create_db_tables(self): """Test that the database is created when get_db_session""" session = model.get_db_session() self.assertEqual(session.query(model.Directory).count(), 0) self.assertEqual(session.query(model.File).count(), 0) session.close()
session.commit() print('\nDirectory Added.') else: print('\nDirectory is already being monitored.') else: print('\nInvalid directory.') def print_monitored_directories(session: Session) -> None: """Print all directory paths in the database""" directories = session.query(model.Directory).all() if len(directories) > 0: print('\nMonitored Directories:\n') for directory in directories: print(directory.path) else: print('\nThere are currently no directories being monitored.') if __name__ == "__main__": session = model.get_db_session() atexit.register(session.close) # always close db connection on exit option = get_menu_input() while option is not None: if option == 1: print_monitored_directories(session) elif option == 2: directory = input('\nEnter full path (eg: /var/log/nginx)\n>') add_directory(directory=directory, session=session) option = get_menu_input()
from sqlalchemy.orm import relationship, sessionmaker from sqlalchemy import create_engine, or_, and_ import json, datetime, decimal from functools import wraps from datetime import datetime from flask.ext.autodoc import Autodoc import requests from redis import Redis from oauth2client.client import flow_from_clientsecrets from oauth2client.client import FlowExchangeError import httplib2 from flask.ext.httpauth import HTTPBasicAuth import requests auth = HTTPBasicAuth() session = get_db_session() CLIENT_ID = json.loads(open('client_secrets.json', 'r').read())['web']['client_id'] app = Flask(__name__, static_url_path = "") auto = Autodoc(app) @app.after_request def inject_headers(response): return limit.inject_x_rate_headers(response) #test code # @app.route('/rate-limited') # @limit.ratelimit(limit=2, per=10 * 1) # def index(): # return jsonify({'response':'This is a rate limited response'}) def get_not_implemented_msg():