def test_mako(self): class RootController(object): @expose('mako:mako.html') def index(self, name='Jonathan'): return dict(name=name) @expose('mako:mako_bad.html') def badtemplate(self): return dict() app = TestApp( Pecan(RootController(), template_path=self.template_path) ) r = app.get('/') assert r.status_int == 200 assert b_("<h1>Hello, Jonathan!</h1>") in r.body r = app.get('/index.html?name=World') assert r.status_int == 200 assert b_("<h1>Hello, World!</h1>") in r.body error_msg = None try: r = app.get('/badtemplate.html') except Exception as e: for error_f in error_formatters: error_msg = error_f(e) if error_msg: break assert error_msg is not None
def test_get_with_var_args(self): class OthersController(object): @expose() def index(self, one, two, three): return 'NESTED: %s, %s, %s' % (one, two, three) class ThingsController(RestController): others = OthersController() @expose() def get_one(self, *args): return ', '.join(args) class RootController(object): things = ThingsController() # create the app app = TestApp(make_app(RootController())) # test get request r = app.get('/things/one/two/three') assert r.status_int == 200 assert r.body == b_('one, two, three') # test nested get request r = app.get('/things/one/two/three/others/') assert r.status_int == 200 assert r.body == b_('NESTED: one, two, three')
def test_secure_attribute(self): authorized = False class SubController(object): @expose() def index(self): return 'Hello from sub!' class RootController(object): @expose() def index(self): return 'Hello from root!' sub = secure(SubController(), lambda: authorized) app = TestApp(make_app(RootController())) response = app.get('/') assert response.status_int == 200 assert response.body == b_('Hello from root!') response = app.get('/sub/', expect_errors=True) assert response.status_int == 401 authorized = True response = app.get('/sub/') assert response.status_int == 200 assert response.body == b_('Hello from sub!')
def test_nested_generic(self): class SubSubController(object): @expose(generic=True) def index(self): return 'GET' @index.when(method='DELETE', template='json') def do_delete(self, name, *args): return dict(result=name, args=', '.join(args)) class SubController(object): sub = SubSubController() class RootController(object): sub = SubController() app = TestApp(Pecan(RootController())) r = app.get('/sub/sub/') assert r.status_int == 200 assert r.body == b_('GET') r = app.delete('/sub/sub/joe/is/cool') assert r.status_int == 200 assert r.body == b_(dumps(dict(result='joe', args='is, cool')))
def test_simple_generic(self): class RootController(object): @expose(generic=True) def index(self): pass @index.when(method='POST', template='json') def do_post(self): return dict(result='POST') @index.when(method='GET') def do_get(self): return 'GET' app = TestApp(Pecan(RootController())) r = app.get('/') assert r.status_int == 200 assert r.body == b_('GET') r = app.post('/') assert r.status_int == 200 assert r.body == b_(dumps(dict(result='POST'))) r = app.get('/do_get', status=404) assert r.status_int == 404
def test_streaming_response(self): class RootController(object): @expose(content_type='text/plain') def test(self, foo): if foo == 'stream': # mimic large file contents = six.BytesIO(b_('stream')) response.content_type = 'application/octet-stream' contents.seek(0, os.SEEK_END) response.content_length = contents.tell() contents.seek(0, os.SEEK_SET) response.app_iter = contents return response else: return 'plain text' app = TestApp(Pecan(RootController())) r = app.get('/test/stream') assert r.content_type == 'application/octet-stream' assert r.body == b_('stream') r = app.get('/test/plain') assert r.content_type == 'text/plain' assert r.body == b_('plain text')
def test_isolated_hook_with_global_hook(self): run_hook = [] class SimpleHook(PecanHook): def __init__(self, id): self.id = str(id) def on_route(self, state): run_hook.append('on_route' + self.id) def before(self, state): run_hook.append('before' + self.id) def after(self, state): run_hook.append('after' + self.id) def on_error(self, state, e): run_hook.append('error' + self.id) class SubController(HookController): __hooks__ = [SimpleHook(2)] @expose() def index(self): run_hook.append('inside_sub') return 'Inside here!' class RootController(object): @expose() def index(self): run_hook.append('inside') return 'Hello, World!' sub = SubController() app = TestApp(make_app(RootController(), hooks=[SimpleHook(1)])) response = app.get('/') assert response.status_int == 200 assert response.body == b_('Hello, World!') assert len(run_hook) == 4 assert run_hook[0] == 'on_route1' assert run_hook[1] == 'before1' assert run_hook[2] == 'inside' assert run_hook[3] == 'after1' run_hook = [] response = app.get('/sub/') assert response.status_int == 200 assert response.body == b_('Inside here!') assert len(run_hook) == 6 assert run_hook[0] == 'on_route1' assert run_hook[1] == 'before2' assert run_hook[2] == 'before1' assert run_hook[3] == 'inside_sub' assert run_hook[4] == 'after1' assert run_hook[5] == 'after2'
def test_isolated_hook_with_global_hook(self): run_hook = [] class SimpleHook(PecanHook): def __init__(self, id): self.id = str(id) def on_route(self, state): run_hook.append("on_route" + self.id) def before(self, state): run_hook.append("before" + self.id) def after(self, state): run_hook.append("after" + self.id) def on_error(self, state, e): run_hook.append("error" + self.id) class SubController(HookController): __hooks__ = [SimpleHook(2)] @expose() def index(self): run_hook.append("inside_sub") return "Inside here!" class RootController(object): @expose() def index(self): run_hook.append("inside") return "Hello, World!" sub = SubController() app = TestApp(make_app(RootController(), hooks=[SimpleHook(1)])) response = app.get("/") assert response.status_int == 200 assert response.body == b_("Hello, World!") assert len(run_hook) == 4 assert run_hook[0] == "on_route1" assert run_hook[1] == "before1" assert run_hook[2] == "inside" assert run_hook[3] == "after1" run_hook = [] response = app.get("/sub/") assert response.status_int == 200 assert response.body == b_("Inside here!") assert len(run_hook) == 6 assert run_hook[0] == "on_route1" assert run_hook[1] == "before2" assert run_hook[2] == "before1" assert run_hook[3] == "inside_sub" assert run_hook[4] == "after1" assert run_hook[5] == "after2"
def test_nested_get_all_with_lookup(self): class BarsController(RestController): @expose() def get_one(self, foo_id, id): return '4' @expose() def get_all(self, foo_id): return '3' @expose('json') def _lookup(self, id, *remainder): redirect('/lookup-hit/') class FoosController(RestController): bars = BarsController() @expose() def get_one(self, id): return '2' @expose() def get_all(self): return '1' class RootController(object): foos = FoosController() # create the app app = TestApp(make_app(RootController())) r = app.get('/foos/') assert r.status_int == 200 assert r.body == b_('1') r = app.get('/foos/1/') assert r.status_int == 200 assert r.body == b_('2') r = app.get('/foos/1/bars/') assert r.status_int == 200 assert r.body == b_('3') r = app.get('/foos/1/bars/2/') assert r.status_int == 200 assert r.body == b_('4') r = app.get('/foos/bars/', status=400) assert r.status_int == 400 r = app.get('/foos/bars/', status=400) r = app.get('/foos/bars/1') assert r.status_int == 302 assert r.headers['Location'].endswith('/lookup-hit/')
def test_nested_get_all_with_lookup(self): class BarsController(RestController): @expose() def get_one(self, foo_id, id): return '4' @expose() def get_all(self, foo_id): return '3' @expose('json') def _lookup(self, id, *remainder): redirect('/lookup-hit/') class FoosController(RestController): bars = BarsController() @expose() def get_one(self, id): return '2' @expose() def get_all(self): return '1' class RootController(object): foos = FoosController() # create the app app = TestApp(make_app(RootController())) r = app.get('/foos/') assert r.status_int == 200 assert r.body == b_('1') r = app.get('/foos/1/') assert r.status_int == 200 assert r.body == b_('2') r = app.get('/foos/1/bars/') assert r.status_int == 200 assert r.body == b_('3') r = app.get('/foos/1/bars/2/') assert r.status_int == 200 assert r.body == b_('4') r = app.get('/foos/bars/') assert r.status_int == 302 assert r.headers['Location'].endswith('/lookup-hit/') r = app.get('/foos/bars/1') assert r.status_int == 302 assert r.headers['Location'].endswith('/lookup-hit/')
def test_simple_secure(self): authorized = False class SecretController(SecureController): @expose() def index(self): return 'Index' @expose() @unlocked def allowed(self): return 'Allowed!' @classmethod def check_permissions(cls): return authorized class RootController(object): @expose() def index(self): return 'Hello, World!' @expose() @secure(lambda: False) def locked(self): return 'No dice!' @expose() @secure(lambda: True) def unlocked(self): return 'Sure thing' secret = SecretController() app = TestApp(make_app( RootController(), debug=True, static_root='tests/static' )) response = app.get('/') assert response.status_int == 200 assert response.body == b_('Hello, World!') response = app.get('/unlocked') assert response.status_int == 200 assert response.body == b_('Sure thing') response = app.get('/locked', expect_errors=True) assert response.status_int == 401 response = app.get('/secret/', expect_errors=True) assert response.status_int == 401 response = app.get('/secret/allowed') assert response.status_int == 200 assert response.body == b_('Allowed!')
def test_isolation_level(self): run_hook = [] class RootController(object): @expose(generic=True) def index(self): run_hook.append('inside') return 'I should not be isolated' @isolation_level('SERIALIZABLE') @index.when(method='POST') def isolated(self): run_hook.append('inside') return "I should be isolated" def gen(event): return lambda: run_hook.append(event) def gen_start(event): return lambda level: run_hook.append(' '.join((event, level))) def start(isolation_level=''): run_hook.append('start ' + isolation_level) app = TestApp(make_app(RootController(), hooks=[ IsolatedTransactionHook( start=start, start_ro=gen('start_ro'), commit=gen('commit'), rollback=gen('rollback'), clear=gen('clear') ) ])) run_hook = [] response = app.get('/') assert response.status_int == 200 assert response.body == b_('I should not be isolated') assert len(run_hook) == 3 assert run_hook[0] == 'start_ro' assert run_hook[1] == 'inside' assert run_hook[2] == 'clear' run_hook = [] response = app.post('/') assert response.status_int == 200 assert response.body == b_('I should be isolated') assert len(run_hook) == 5 assert run_hook[0] == 'start ' assert run_hook[1] == 'start SERIALIZABLE' assert run_hook[2] == 'inside' assert run_hook[3] == 'commit' assert run_hook[4] == 'clear'
def test_isolation_level(self): run_hook = [] class RootController(object): @expose(generic=True) def index(self): run_hook.append('inside') return 'I should not be isolated' @isolation_level('SERIALIZABLE') @index.when(method='POST') def isolated(self): run_hook.append('inside') return "I should be isolated" def gen(event): return lambda: run_hook.append(event) def gen_start(event): return lambda level: run_hook.append(' '.join((event, level))) def start(isolation_level=''): run_hook.append('start ' + isolation_level) app = TestApp( make_app(RootController(), hooks=[ IsolatedTransactionHook(start=start, start_ro=gen('start_ro'), commit=gen('commit'), rollback=gen('rollback'), clear=gen('clear')) ])) run_hook = [] response = app.get('/') assert response.status_int == 200 assert response.body == b_('I should not be isolated') assert len(run_hook) == 3 assert run_hook[0] == 'start_ro' assert run_hook[1] == 'inside' assert run_hook[2] == 'clear' run_hook = [] response = app.post('/') assert response.status_int == 200 assert response.body == b_('I should be isolated') assert len(run_hook) == 5 assert run_hook[0] == 'start ' assert run_hook[1] == 'start SERIALIZABLE' assert run_hook[2] == 'inside' assert run_hook[3] == 'commit' assert run_hook[4] == 'clear'
def error_docs_app(environ, start_response): if environ["PATH_INFO"] == "/not_found": start_response("404 Not found", [("Content-type", "text/plain")]) return [b_("Not found")] elif environ["PATH_INFO"] == "/error": start_response("200 OK", [("Content-type", "text/plain")]) return [b_("Page not found")] elif environ["PATH_INFO"] == "/recurse": raise ForwardRequestException("/recurse") else: return simple_app(environ, start_response)
def test_simple_secure(self): authorized = False class SecretController(SecureController): @expose() def index(self): return "Index" @expose() @unlocked def allowed(self): return "Allowed!" @classmethod def check_permissions(cls): return authorized class RootController(object): @expose() def index(self): return "Hello, World!" @expose() @secure(lambda: False) def locked(self): return "No dice!" @expose() @secure(lambda: True) def unlocked(self): return "Sure thing" secret = SecretController() app = TestApp(make_app(RootController(), debug=True, static_root="tests/static")) response = app.get("/") assert response.status_int == 200 assert response.body == b_("Hello, World!") response = app.get("/unlocked") assert response.status_int == 200 assert response.body == b_("Sure thing") response = app.get("/locked", expect_errors=True) assert response.status_int == 401 response = app.get("/secret/", expect_errors=True) assert response.status_int == 401 response = app.get("/secret/allowed") assert response.status_int == 200 assert response.body == b_("Allowed!")
def test_nested_get_all(self): class BarsController(RestController): @expose() def get_one(self, foo_id, id): return '4' @expose() def get_all(self, foo_id): return '3' class FoosController(RestController): bars = BarsController() @expose() def get_one(self, id): return '2' @expose() def get_all(self): return '1' class RootController(object): foos = FoosController() # create the app app = TestApp(make_app(RootController())) r = app.get('/foos/') assert r.status_int == 200 assert r.body == b_('1') r = app.get('/foos/1/') assert r.status_int == 200 assert r.body == b_('2') r = app.get('/foos/1/bars/') assert r.status_int == 200 assert r.body == b_('3') r = app.get('/foos/1/bars/2/') assert r.status_int == 200 assert r.body == b_('4') r = app.get('/foos/bars/', status=404) assert r.status_int == 404 r = app.get('/foos/bars/1', status=404) assert r.status_int == 404
def test_custom_with_trailing_slash(self): class CustomController(RestController): _custom_actions = { 'detail': ['GET'], 'create': ['POST'], 'update': ['PUT'], 'remove': ['DELETE'], } @expose() def detail(self): return 'DETAIL' @expose() def create(self): return 'CREATE' @expose() def update(self, id): return id @expose() def remove(self, id): return id app = TestApp(make_app(CustomController())) r = app.get('/detail') assert r.status_int == 200 assert r.body == b_('DETAIL') r = app.get('/detail/') assert r.status_int == 200 assert r.body == b_('DETAIL') r = app.post('/create') assert r.status_int == 200 assert r.body == b_('CREATE') r = app.post('/create/') assert r.status_int == 200 assert r.body == b_('CREATE') r = app.put('/update/123') assert r.status_int == 200 assert r.body == b_('123') r = app.put('/update/123/') assert r.status_int == 200 assert r.body == b_('123') r = app.delete('/remove/456') assert r.status_int == 200 assert r.body == b_('456') r = app.delete('/remove/456/') assert r.status_int == 200 assert r.body == b_('456')
def test_kajiki(self): class RootController(object): @expose("kajiki:kajiki.html") def index(self, name="Jonathan"): return dict(name=name) app = TestApp(Pecan(RootController(), template_path=self.template_path)) r = app.get("/") assert r.status_int == 200 assert b_("<h1>Hello, Jonathan!</h1>") in r.body r = app.get("/index.html?name=World") assert r.status_int == 200 assert b_("<h1>Hello, World!</h1>") in r.body
def test_basic_single_default_hook(self): _stdout = StringIO() class RootController(object): @expose() def index(self): return "Hello, World!" app = TestApp(make_app(RootController(), hooks=lambda: [RequestViewerHook(writer=_stdout)])) response = app.get("/") out = _stdout.getvalue() assert response.status_int == 200 assert response.body == b_("Hello, World!") assert "path" in out assert "method" in out assert "status" in out assert "method" in out assert "params" in out assert "hooks" in out assert "200 OK" in out assert "['RequestViewerHook']" in out assert "/" in out
def test_item_not_in_defaults(self): _stdout = StringIO() class RootController(object): @expose() def index(self): return "Hello, World!" app = TestApp( make_app(RootController(), hooks=lambda: [RequestViewerHook(config={"items": ["date"]}, writer=_stdout)]) ) response = app.get("/") out = _stdout.getvalue() assert response.status_int == 200 assert response.body == b_("Hello, World!") assert "date" in out assert "method" not in out assert "status" not in out assert "method" not in out assert "params" not in out assert "hooks" not in out assert "200 OK" not in out assert "['RequestViewerHook']" not in out assert "/" not in out
def test_basic_single_hook(self): run_hook = [] class RootController(object): @expose() def index(self): run_hook.append("inside") return "Hello, World!" class SimpleHook(PecanHook): def on_route(self, state): run_hook.append("on_route") def before(self, state): run_hook.append("before") def after(self, state): run_hook.append("after") def on_error(self, state, e): run_hook.append("error") app = TestApp(make_app(RootController(), hooks=[SimpleHook()])) response = app.get("/") assert response.status_int == 200 assert response.body == b_("Hello, World!") assert len(run_hook) == 4 assert run_hook[0] == "on_route" assert run_hook[1] == "before" assert run_hook[2] == "inside" assert run_hook[3] == "after"
def test_basic_single_hook(self): run_hook = [] class RootController(object): @expose() def index(self, req, resp): run_hook.append('inside') return 'Hello, World!' class SimpleHook(PecanHook): def on_route(self, state): run_hook.append('on_route') def before(self, state): run_hook.append('before') def after(self, state): run_hook.append('after') def on_error(self, state, e): run_hook.append('error') app = TestApp( Pecan(RootController(), hooks=[SimpleHook()], use_context_locals=False)) response = app.get('/') assert response.status_int == 200 assert response.body == b_('Hello, World!') assert len(run_hook) == 4 assert run_hook[0] == 'on_route' assert run_hook[1] == 'before' assert run_hook[2] == 'inside' assert run_hook[3] == 'after'
def test_independent_check_success(self): self.secret_cls.independent_authorization = True response = self.app.get('/secret/independent') assert response.status_int == 200 assert response.body == b_('Independent Security') assert len(self.permissions_checked) == 1 assert 'independent' in self.permissions_checked
def test_wrapped_attribute_success(self): self.secret_cls.independent_authorization = True response = self.app.get('/secret/wrapped/') assert response.status_int == 200 assert response.body == b_('Index wrapped') assert len(self.permissions_checked) == 1 assert 'independent' in self.permissions_checked
def test_item_not_in_defaults(self): _stdout = StringIO() class RootController(object): @expose() def index(self): return 'Hello, World!' app = TestApp( make_app(RootController(), hooks=lambda: [ RequestViewerHook(config={'items': ['date']}, writer=_stdout) ])) response = app.get('/') out = _stdout.getvalue() assert response.status_int == 200 assert response.body == b_('Hello, World!') assert 'date' in out assert 'method' not in out assert 'status' not in out assert 'method' not in out assert 'params' not in out assert 'hooks' not in out assert '200 OK' not in out assert "['RequestViewerHook']" not in out assert '/' not in out
def test_basic_single_default_hook(self): _stdout = StringIO() class RootController(object): @expose() def index(self): return 'Hello, World!' app = TestApp( make_app(RootController(), hooks=lambda: [RequestViewerHook(writer=_stdout)])) response = app.get('/') out = _stdout.getvalue() assert response.status_int == 200 assert response.body == b_('Hello, World!') assert 'path' in out assert 'method' in out assert 'status' in out assert 'method' in out assert 'params' in out assert 'hooks' in out assert '200 OK' in out assert "['RequestViewerHook']" in out assert '/' in out
def test_error_endpoint_with_query_string(self): app = TestApp(RecursiveMiddleware(ErrorDocumentMiddleware( four_oh_four_app, {404: '/error/404?foo=bar'} ))) r = app.get('/', expect_errors=True) assert r.status_int == 404 assert r.body == b_('Error: 404\nQS: foo=bar')
def test_optional_arg_with_multiple_url_encoded_dictionary_kwargs(self): r = self.app_.post('/optional', { 'id': 'Some%20Number', 'dummy': 'dummy' }) assert r.status_int == 200 assert r.body == b_('optional: Some%20Number')
def test_multiple_optional_args_with_multiple_dict_kwargs(self): r = self.app_.post( '/multiple_optional', {'one': '1', 'two': '2', 'three': '3', 'four': '4'} ) assert r.status_int == 200 assert r.body == b_('multiple_optional: 1, 2, 3')
def test_post_many_remainders_with_many_kwargs(self): r = self.app_.post( '/eater/10', {'id': 'ten', 'month': '1', 'day': '12', 'dummy': 'dummy'} ) assert r.status_int == 200 assert r.body == b_('eater: 10, dummy, day=12, month=1')
def test_item_not_in_defaults(self): _stdout = StringIO() class RootController(object): @expose() def index(self): return 'Hello, World!' app = TestApp( make_app( RootController(), hooks=lambda: [ RequestViewerHook( config={'items': ['date']}, writer=_stdout ) ] ) ) response = app.get('/') out = _stdout.getvalue() assert response.status_int == 200 assert response.body == b_('Hello, World!') assert 'date' in out assert 'method' not in out assert 'status' not in out assert 'method' not in out assert 'params' not in out assert 'hooks' not in out assert '200 OK' not in out assert "['RequestViewerHook']" not in out assert '/' not in out
def test_basic_single_default_hook(self): _stdout = StringIO() class RootController(object): @expose() def index(self): return 'Hello, World!' app = TestApp( make_app( RootController(), hooks=lambda: [ RequestViewerHook(writer=_stdout) ] ) ) response = app.get('/') out = _stdout.getvalue() assert response.status_int == 200 assert response.body == b_('Hello, World!') assert 'path' in out assert 'method' in out assert 'status' in out assert 'method' in out assert 'params' in out assert 'hooks' in out assert '200 OK' in out assert "['RequestViewerHook']" in out assert '/' in out
def test_single_blacklist_item(self): _stdout = StringIO() class RootController(object): @expose() def index(self): return 'Hello, World!' app = TestApp( make_app( RootController(), hooks=lambda: [ RequestViewerHook( config={'blacklist': ['/']}, writer=_stdout ) ] ) ) response = app.get('/') out = _stdout.getvalue() assert response.status_int == 200 assert response.body == b_('Hello, World!') assert out == ''
def test_404_with_lookup(self): class LookupController(RestController): def __init__(self, _id): self._id = _id @expose() def get_all(self): return 'ID: %s' % self._id class ThingsController(RestController): @expose() def _lookup(self, _id, *remainder): return LookupController(_id), remainder class RootController(object): things = ThingsController() # create the app app = TestApp(make_app(RootController())) # these should 404 for path in ('/things', '/things/'): r = app.get(path, expect_errors=True) assert r.status_int == 404 r = app.get('/things/foo') assert r.status_int == 200 assert r.body == b_('ID: foo')
def test_project_pecan_shell_command(self): # Start the server proc = subprocess.Popen([ os.path.join(self.bin, 'pecan'), 'shell', 'testing123/config.py' ], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE ) self.poll(proc) out, _ = proc.communicate( b_('{"model" : model, "conf" : conf, "app" : app}') ) assert 'testing123.model' in out.decode(), out assert 'Config(' in out.decode(), out assert 'webtest.app.TestApp' in out.decode(), out try: # just in case stdin doesn't close proc.terminate() except: pass
def test_variable_post_mixed(self): r = self.app_.post( '/variable_all/7', {'id': 'seven', 'month': '1', 'day': '12'} ) assert r.status_int == 200 assert r.body == b_('variable_all: 7, day=12, id=seven, month=1')
def test_basic_single_hook(self): run_hook = [] class RootController(object): @expose() def index(self): run_hook.append('inside') return 'Hello, World!' class SimpleHook(PecanHook): def on_route(self, state): run_hook.append('on_route') def before(self, state): run_hook.append('before') def after(self, state): run_hook.append('after') def on_error(self, state, e): run_hook.append('error') app = TestApp(make_app(RootController(), hooks=[SimpleHook()])) response = app.get('/') assert response.status_int == 200 assert response.body == b_('Hello, World!') assert len(run_hook) == 4 assert run_hook[0] == 'on_route' assert run_hook[1] == 'before' assert run_hook[2] == 'inside' assert run_hook[3] == 'after'
def test_variable_post_mixed(self): r = self.app_.post('/variable_all/7', { 'id': 'seven', 'month': '1', 'day': '12' }) assert r.status_int == 200 assert r.body == b_('variable_all: 7, day=12, id=seven, month=1')
def test_cyclical_protection(self): self.secret_cls.authorized = True self.deepsecret_cls.authorized = True response = self.app.get('/secret/1/deepsecret/2/deepsecret/') assert response.status_int == 200 assert response.body == b_('Deep Secret') assert 'secretcontroller' in self.permissions_checked assert 'deepsecret' in self.permissions_checked
def test_error_with_recursion_loop(self): app = TestApp( RecursiveMiddleware( ErrorDocumentMiddleware(four_oh_four_app, {404: '/'}))) r = app.get('/', expect_errors=True) assert r.status_int == 404 assert r.body == b_( 'Error: 404 Not Found. (Error page could not be fetched)')
def test_error_endpoint_with_query_string(self): app = TestApp( RecursiveMiddleware( ErrorDocumentMiddleware(four_oh_four_app, {404: '/error/404?foo=bar'}))) r = app.get('/', expect_errors=True) assert r.status_int == 404 assert r.body == b_('Error: 404\nQS: foo=bar')
def test_multiple_variable_kwargs_with_encoded_dict_kwargs(self): r = self.app_.post('/variable_kwargs', { 'id': 'Three%21', 'dummy': 'This%20is%20a%20test' }) assert r.status_int == 200 result = 'variable_kwargs: dummy=This%20is%20a%20test, id=Three%21' assert r.body == b_(result)
def test_simple_nested_rest(self): class BarController(RestController): @expose() def post(self): return "BAR-POST" @expose() def delete(self, id_): return "BAR-%s" % id_ class FooController(RestController): bar = BarController() @expose() def post(self): return "FOO-POST" @expose() def delete(self, id_): return "FOO-%s" % id_ class RootController(object): foo = FooController() # create the app app = TestApp(make_app(RootController())) r = app.post('/foo') assert r.status_int == 200 assert r.body == b_("FOO-POST") r = app.delete('/foo/1') assert r.status_int == 200 assert r.body == b_("FOO-1") r = app.post('/foo/bar') assert r.status_int == 200 assert r.body == b_("BAR-POST") r = app.delete('/foo/bar/2') assert r.status_int == 200 assert r.body == b_("BAR-2")
def test_multiple_optional_args_with_multiple_encoded_dict_kwargs(self): r = self.app_.post('/multiple_optional', { 'one': 'One%21', 'two': 'Two%21', 'three': 'Three%21', 'four': '4' }) assert r.status_int == 200 assert r.body == b_('multiple_optional: One%21, Two%21, Three%21')
def test_post_many_remainders_with_many_kwargs(self): r = self.app_.post('/eater/10', { 'id': 'ten', 'month': '1', 'day': '12', 'dummy': 'dummy' }) assert r.status_int == 200 assert r.body == b_('eater: 10, dummy, day=12, month=1')
def test_protected_lookup(self): response = self.app.get('/secret/hi/', expect_errors=True) assert response.status_int == 401 self.secret_cls.authorized = True response = self.app.get('/secret/hi/') assert response.status_int == 200 assert response.body == b_('Index hi') assert 'secretcontroller' in self.permissions_checked
def test_multiple_optional_args_with_multiple_dict_kwargs(self): r = self.app_.post('/multiple_optional', { 'one': '1', 'two': '2', 'three': '3', 'four': '4' }) assert r.status_int == 200 assert r.body == b_('multiple_optional: 1, 2, 3')
def test_config_with_syntax_error(self): from pecan import configuration with tempfile.NamedTemporaryFile('wb') as f: f.write(b_('\n'.join(['if false', 'var = 3']))) f.flush() configuration.Config({}) self.assertRaises(SyntaxError, configuration.conf_from_file, f.name)
def test_lookup_to_wrapped_attribute_on_self(self): self.secret_cls.authorized = True self.secret_cls.independent_authorization = True response = self.app.get('/secret/lookup_wrapped/') assert response.status_int == 200 assert response.body == b_('Index wrapped') assert len(self.permissions_checked) == 2 assert 'independent' in self.permissions_checked assert 'secretcontroller' in self.permissions_checked
def test_locals_are_not_used(self): with mock.patch('threading.local', side_effect=AssertionError()): app = TestApp(Pecan(self.root(), use_context_locals=False)) r = app.get('/') assert r.status_int == 200 assert r.body == b_('Hello, World!') self.assertRaises(AssertionError, Pecan, self.root)