def test_dont_cdn_absolute_url_rewrite_when_off(self): '''No re-writing if CDN is off''' a_test_config = test_config.copy() a_test_config["USE_CDN"] = False pybald.configure(config_object=a_test_config) asset_url = AssetUrl(my_static_resource_explicit) self.assertEqual(str(asset_url), str(my_static_resource_explicit))
def test_dont_cdn_absolute_url_rewrite_when_off(self): '''No re-writing if CDN is off''' a_test_config = test_config.copy() a_test_config["USE_CDN"] = False pybald.configure(config_object=a_test_config) asset_url = AssetUrl(my_static_resource_explicit) self.assertEqual(str(asset_url), str(my_static_resource_explicit))
def test_dont_cdn_url_rewrite_for_unowned_link(self): '''Don't rewrite links on hosts we don't own''' request_config().protocol = "https" a_test_config = test_config.copy() a_test_config["USE_CDN"] = True pybald.configure(a_test_config) asset_url = AssetUrl(not_owned_static_resource) self.assertEqual(str(asset_url), str(not_owned_static_resource))
def test_default_protocol(self): '''Test that the CDN rewrite is smart enough to use the raw CDN if https and we don't have https certs for static hosts.''' a_test_config = test_config.copy() a_test_config["USE_CDN"] = True pybald.configure(config_object=a_test_config) asset_url = AssetUrl(my_static_resource) self.assertEqual(str(asset_url), 'https://awesomecdn.com/static_stuff.js')
def test_cdn_absolute_url_rewrite_http(self): '''Test that an absolute link gets changed to a CDN link''' request_config().protocol = "http" a_test_config = test_config.copy() a_test_config["USE_CDN"] = True pybald.configure(config_object=a_test_config) asset_url = AssetUrl(my_static_resource_explicit) self.assertEqual(str(asset_url), 'http://s0.sample.com/static_stuff.js')
def test_dont_cdn_url_rewrite_for_unowned_link(self): '''Don't rewrite links on hosts we don't own''' request_config().protocol = "https" a_test_config = test_config.copy() a_test_config["USE_CDN"] = True pybald.configure(a_test_config) asset_url = AssetUrl(not_owned_static_resource) self.assertEqual(str(asset_url), str(not_owned_static_resource))
def test_url_param_injection(self): '''Test that rendering a url parameter escapes any dangerous html''' pybald.configure(config_object=test_conf) app = Router(routes=map, controllers=[XssController]) app = ErrorMiddleware(app) resp = Request.blank('/test_xss_url_param?url_parameter=%3C/script%3E%3Cscript%3Ealert%28document.cookie%29%3C/script%3E').get_response(app) self.assertEqual(resp.body, b'\n<div></script><script>alert(document.cookie)</script></div>\n')
def test_cdn_absolute_url_rewrite_http(self): '''Test that an absolute link gets changed to a CDN link''' request_config().protocol = "http" a_test_config = test_config.copy() a_test_config["USE_CDN"] = True pybald.configure(config_object=a_test_config) asset_url = AssetUrl(my_static_resource_explicit) self.assertEqual(str(asset_url), 'http://s0.sample.com/static_stuff.js')
def test_serve_png_file(self): "Serve a file from a static asset path (temporary)" pybald.configure(config_object=test_conf) app = StaticServer(application=None, path=self.test_dir) resp = Request.blank('/pybald.png').get_response(app) self.assertEqual(resp.headers['Content-Type'], 'image/png') self.assertEqual(resp.headers['Content-Length'], '1342') self.assertEqual(resp.body, binary_file_data)
def test_default_protocol(self): '''Test that the CDN rewrite is smart enough to use the raw CDN if https and we don't have https certs for static hosts.''' a_test_config = test_config.copy() a_test_config["USE_CDN"] = True pybald.configure(config_object=a_test_config) asset_url = AssetUrl(my_static_resource) self.assertEqual(str(asset_url), 'https://awesomecdn.com/static_stuff.js')
def test_404(self): "Return a 404 response" pybald.configure(config_object=test_conf) app = Router(routes=map, controllers=[SampleController]) app = ErrorMiddleware(app) try: resp = Request.blank('/not_there').get_response(app) except Exception as err: self.fail("Exception Generated or Fell Through Error Handler {0}".format(err)) self.assertEqual(resp.status_code, 404) self.assertEqual(str(resp.text), not_found_response)
def test_stack_trace(self): "When in debug mode, throw an Exception and generate a stack trace" pybald.configure(config_object=test_conf) app = Router(routes=map, controllers=[SampleController]) app = ErrorMiddleware(app) try: resp = Request.blank('/throw_exception').get_response(app) except Exception as err: self.fail("Exception Generated or Fell Through Error Handler {0}".format(err)) self.assertEqual(resp.status_code, 500) self.assertEqual(stack_trace_head, str(resp.text)[:len(stack_trace_head)])
def test_non_stack_trace(self): "When *NOT* in debug mode, throw an Exception and return a generic error" test_w_debug_conf = test_conf.copy() test_w_debug_conf.update(dict(debug=False)) pybald.configure(config_object=test_w_debug_conf) app = Router(routes=map, controllers=[SampleController]) app = ErrorMiddleware(app) try: resp = Request.blank('/throw_exception').get_response(app) except Exception as err: self.fail("Exception Generated or Fell Through Error Handler {0}".format(err)) self.assertEqual(resp.status_code, 500) self.assertEqual(str(resp.text), general_fault_response)
def test_url_param_injection(self): '''Test that rendering a url parameter escapes any dangerous html''' pybald.configure(config_object=test_conf) app = Router(routes=map, controllers=[XssController]) app = ErrorMiddleware(app) resp = Request.blank( '/test_xss_url_param?url_parameter=%3C/script%3E%3Cscript%3Ealert%28document.cookie%29%3C/script%3E' ).get_response(app) self.assertEqual( resp.body, b'\n<div></script><script>alert(document.cookie)</script></div>\n' )
def setUp(self): context = pybald.configure(config_file="tests/sample_project/project.py") def map(urls): urls.connect("home", r"/") self.app = Router(routes=map, controllers=[])
def setUp(self): context = pybald.configure(config_file="tests/sample_project/project.py") def map(urls): urls.connect("home", r"/") self.app = Router(routes=map, controllers=[])
def setUp(self): pybald.configure(config_object={'debug': False}) def map(urls): urls.connect('test1', r'/test1', controller='test1') urls.connect('test2', r'/test2', controller='test2', action='index') urls.connect('method_test1', r'/method', controller='test2', action='get', conditions=dict(method=["GET"])) urls.connect('method_test2', r'/method', controller='test2', action='delete', conditions=dict(method=["DELETE"])) # test3 is a 404 missing url urls.connect('test4', r'/test4', controller='test4', action='index') urls.connect('test5', r'/test5', controller='test2', action='_iminvalid') urls.redirect('/here', '/there', _redirect_code='302 Found') class Test1Controller(object): def index(self, environ, start_response): start_response('200 OK', [('Content-Type','text/plain')]) return ["test1"] class Test2Controller(object): def index(self, environ, start_response): start_response('200 OK', [('Content-Type','text/plain')]) return ["test2"] def delete(self, environ, start_response): start_response('200 OK', [('Content-Type','text/plain')]) return ["test2_delete"] def get(self, environ, start_response): start_response('200 OK', [('Content-Type','text/plain')]) return ["test2_get"] def _iminvalid(self, environ, start_response): start_response('200 OK', [('Content-Type','text/plain')]) return ["test5_invalid_action"] self.app = Router(routes=map, controllers=[Test1Controller, Test2Controller])
def setUp(self): context = pybald.configure(config_object=dict(env_name="ControllerTest"))
def setUp(self): pybald.configure(config_object=test_config)
def setUp(self): context = pybald.configure(config_object=dict( env_name="ControllerTest"))
def setUp(self): config_obj = dict(env_name="TestFromObject", database_engine_uri='sqlite:///:memory:') pybald.configure(config_object=config_obj)
def test_read_from_object(self): "Pass pybald configuration as a dictionary of values" config_obj = dict(conf_object=True, env_name="TestFromObject") context = pybald.configure(config_object=config_obj) assert context.config.conf_object == True assert context.config.env_name == "TestFromObject"
def test_read_from_file(self): "Read a pybald config from a file" context = pybald.configure(config_file="tests/sample_project/project.py") assert context.config.sample_config == True assert context.config.env_name == 'SampleTestProjectEnvironment'
def test_missing_file(self): "Exit if specified config file is missing" with self.assertRaises(SystemExit) as context: pybald.configure(config_file="tests/sample_project/not_there.py")
def setUp(self): config_obj = dict(env_name="TestFromObject", database_engine_uri='sqlite:///:memory:') pybald.configure(config_object=config_obj)
def setUp(self): context._reset() pybald.configure(config_object=dict(database_engine_uri='sqlite:///:memory:'))
import os import pybald from pybald import context from pybald.core.controllers import Controller, action from pybald.core.router import Router BASE_DIR = os.environ['PYTHONPATH'] CONFIGPATH = os.path.join(BASE_DIR, 'project.py') pybald.configure(config_file=CONFIGPATH) def map(urls): urls.connect( "user", r"/user/new/", controller="user", action="registration") urls.connect( "login", r"/auth/login/", controller="user", action="login") urls.connect( "logout", r"/auth/logout/", controller="user", action="logout") urls.connect( "bucketlist-list", r"/bucketlists/", controller="bucketlist", action="list", conditions={"method": ["GET"]} ) urls.connect( "bucketlist-create", r"/bucketlists/", controller="bucketlist", action="create", conditions={"method": ["POST"]} ) urls.connect( "bucketlist-detail", r"/bucketlists/{id}/", controller="bucketlist", action="detail", conditions={"method": ["GET"]}
def test_default_config(self): "Start without a config, a default configuration is used" context = pybald.configure() assert context.config.debug == True assert context.config.env_name == 'Default'
def setUp(self): # rewind the constants file each time test_constants.seek(0) constants_values = constants.read(constants_file=test_constants) context = pybald.configure( page_options={'constants': constants_values})
import pybald from pybald.core.router import Router if __name__ == "__main__": from pybald.core.logs import default_debug_log default_debug_log() context = pybald.configure(__name__) from pybald.core.controllers import Controller, action from pybald.core.middleware.errors import ErrorMiddleware from pybald.db import models def map(urls): urls.connect('home', r'/', controller='sample') urls.connect('variable_test', r'/variable_test/{variable}', controller='sample', action='variable_test') # errors urls.connect('throw_exception', r'/throw_exception', controller='sample', action='throw_exception') class SampleModel(models.Model): text = models.Column(models.Text) class SampleController(Controller): @action def index(self, req): self.sample_variable = "Testing" @action def throw_exception(self, req): raise Exception("This is a test exception")
def setUp(self): context._reset() pybald.configure(config_object=dict( database_engine_uri='sqlite:///:memory:'))
def setUp(self): pybald.configure(config_file="tests/sample_project/project.py")