def test_filename(self, loadmock): """ Test if file is set correctly """ result = 'test.ini' cl = Config(filename=result) self.assertEqual(cl.filename, result)
def setUp(self): self.tmp = NamedTemporaryFile(delete=True) self.content = dedent( """ [testing] list = item1 item2 item3 list_single = item1 foo = 1 bar = foo enabled = True disabled = False """) self.f = open(self.tmp.name, "w") self.f.write(self.content) self.f.close() self.config = Config(filename=self.tmp.name)
def __init__(self, config=Config()): Thread.__init__(self) self.config = config self.store = self.config.get('blocklister', 'store', default="/tmp") self.interval = self.config.get_int('blocklister', 'update_interval', default=120) self.refresh_list = self.config.get_int('blocklister', 'refresh_list', default=2)
def test_load_searchfile(self, exists_mock): """ Test lookup function """ exists_mock = True cl = Config(filename='testing.ini') result = cl.loadedfiles expected = ['/etc/blocklister/testing.ini', '~/.testing.ini', 'testing.ini'] self.assertEqual(result, expected)
from flask import Flask, request, render_template, make_response from flask.ext.limiter import Limiter from blocklister import __version__, __changelog__ from blocklister.models import Blocklist from blocklister.config import Config from blocklister.exc import FetcherException, EmptyListError app = Flask(__name__) limiter = Limiter(app, headers_enabled=True) config = Config() store = config.get('blocklister', 'store', default="/tmp") @app.errorhandler(IOError) def handle_filenotavailable(exc): msg = "File on disk is not available" response = make_response(msg, 500) response.headers['Content-Type'] = "text/plain" return response @app.errorhandler(ValueError) def handle_unknown_blacklist(exc): routes = [ "/{}".format(x.__name__.lower()) for x in Blocklist.__subclasses__() ] msg = render_template( 'unknown_blacklist.jinja2', routes=routes,
import logging from flask import Flask, request, render_template, make_response from flask.ext.limiter import Limiter from blocklister import __version__, __changelog__ from blocklister.models import Blocklist from blocklister.config import Config from blocklister.exc import FetcherException, EmptyListError from blocklister.summerizer import Summerizer LOG = logging.getLogger(__name__) app = Flask(__name__) limiter = Limiter(app, headers_enabled=True) config = Config() store = config.get('blocklister', 'store', default="/tmp") dedupe = config.get_boolean('blocklister', 'deduplicate', default=False) @app.errorhandler(IOError) def handle_filenotavailable(exc): msg = "File on disk is not available" response = make_response(msg, 500) response.headers['Content-Type'] = "text/plain" return response @app.errorhandler(ValueError) def handle_unknown_blacklist(exc): routes = ["/%s" % x.__name__.lower() for x in Blocklist.__subclasses__()] msg = render_template(
import logging from flask import Flask, request, render_template, make_response from flask.ext.limiter import Limiter from blocklister import __version__, __changelog__ from blocklister.models import Blocklist from blocklister.config import Config from blocklister.exc import FetcherException, EmptyListError from blocklister.summerizer import Summerizer LOG = logging.getLogger(__name__) app = Flask(__name__) limiter = Limiter(app, headers_enabled=True) config = Config() store = config.get('blocklister', 'store', default="/tmp") dedupe = config.get_boolean('blocklister', 'deduplicate', default=False) @app.errorhandler(IOError) def handle_filenotavailable(exc): msg = "File on disk is not available" response = make_response(msg, 500) response.headers['Content-Type'] = "text/plain" return response @app.errorhandler(ValueError) def handle_unknown_blacklist(exc): routes = [ "/%s" % x.__name__.lower() for x in Blocklist.__subclasses__()
class TestConfig(unittest.TestCase): def setUp(self): self.tmp = NamedTemporaryFile(delete=True) self.content = dedent( """ [testing] list = item1 item2 item3 list_single = item1 foo = 1 bar = foo enabled = True disabled = False """) self.f = open(self.tmp.name, "w") self.f.write(self.content) self.f.close() self.config = Config(filename=self.tmp.name) @patch('blocklister.config.Config._load') def test_filename(self, loadmock): """ Test if file is set correctly """ result = 'test.ini' cl = Config(filename=result) self.assertEqual(cl.filename, result) def test_load(self): """ Test loading an ini file """ result = self.config.loadedfiles expected = [self.tmp.name] self.assertEqual(result, expected) def test_get(self): """ test_get getting the testing option from temp ini file """ result = self.config.get('testing', 'bar') expected = 'foo' self.assertEqual(result, expected) def test_get_default(self): """ Test get default for non existing option """ result = self.config.get('testing', 'notfound', default='amp') expected = 'amp' self.assertEqual(result, expected) def test_get_list(self): result = self.config.get_list('testing', 'list') expected = ['item1', 'item2', 'item3'] self.assertCountEqual(result, expected) def test_get_list_single_item(self): result = self.config.get_list('testing', 'list_single') expected = ['item1'] self.assertCountEqual(result, expected) def test_get_list_default(self): result = self.config.get_list( 'testing', 'listNotFound', default=['1', '2']) expected = ['1', '2'] self.assertCountEqual(result, expected) def test_get_int(self): result = self.config.get_int('testing', 'foo') expected = 1 self.assertEqual(result, expected) def test_get_int_valueerror(self): with self.assertRaises(ConfigError): self.config.get_int('testing', 'bar') @patch('blocklister.config.exists') def test_load_searchfile(self, exists_mock): """ Test lookup function """ exists_mock = True cl = Config(filename='testing.ini') result = cl.loadedfiles expected = ['/etc/blocklister/testing.ini', '~/.testing.ini', 'testing.ini'] self.assertEqual(result, expected)
class TestConfig(unittest.TestCase): def setUp(self): self.tmp = NamedTemporaryFile(delete=True) self.content = dedent( """ [testing] list = item1 item2 item3 list_single = item1 foo = 1 bar = foo enabled = True disabled = False """) self.f = open(self.tmp.name, "w") self.f.write(self.content) self.f.close() self.config = Config(filename=self.tmp.name) @patch('blocklister.config.Config._load') def test_filename(self, loadmock): """ Test if file is set correctly """ result = 'test.ini' cl = Config(filename=result) self.assertEqual(cl.filename, result) def test_load(self): """ Test loading an ini file """ result = self.config.loadedfiles expected = [self.tmp.name] self.assertEqual(result, expected) def test_get(self): """ test_get getting the testing option from temp ini file """ result = self.config.get('testing', 'bar') expected = 'foo' self.assertEqual(result, expected) def test_get_default(self): """ Test get default for non existing option """ result = self.config.get('testing', 'notfound', default='amp') expected = 'amp' self.assertEqual(result, expected) def test_get_list(self): result = self.config.get_list('testing', 'list') expected = ['item1', 'item2', 'item3'] if sys.version_info[0] == 3: # noqa self.assertCountEqual(result, expected) else: self.assertItemsEqual(result, expected) def test_get_list_single_item(self): result = self.config.get_list('testing', 'list_single') expected = ['item1'] if sys.version_info[0] == 3: # noqa self.assertCountEqual(result, expected) else: self.assertItemsEqual(result, expected) def test_get_list_default(self): result = self.config.get_list( 'testing', 'listNotFound', default=['1', '2']) expected = ['1', '2'] if sys.version_info[0] == 3: # noqa self.assertCountEqual(result, expected) else: self.assertItemsEqual(result, expected) def test_get_int(self): result = self.config.get_int('testing', 'foo') expected = 1 self.assertEqual(result, expected) def test_get_int_valueerror(self): with self.assertRaises(ConfigError): self.config.get_int('testing', 'bar') def test_get_boolean(self): result = self.config.get_boolean('testing', 'enabled', default=False) expected = True self.assertEqual(result, expected) @patch('blocklister.config.exists') def test_load_searchfile(self, exists_mock): """ Test lookup function """ exists_mock = True cl = Config(filename='testing.ini') result = cl.loadedfiles expected = ['/etc/blocklister/testing.ini', '~/.testing.ini', 'testing.ini'] self.assertEqual(result, expected)