def test_implicit_disabled(): morepath.disable_implicit() config = morepath.setup() app = morepath.App(testing_config=config) @app.path(path='') class Model(object): def __init__(self): pass @reg.generic def one(): return "default one" @app.view(model=Model) def default(self, request): try: return one() except reg.NoImplicitLookupError: return "No implicit found" config.commit() c = Client(app) response = c.get('/') assert response.body == 'No implicit found'
def test_path_and_url_parameter_converter(): config = setup() app = morepath.App(testing_config=config) class Model(object): def __init__(self, id, param): self.id = id self.param = param from datetime import date @app.path(model=Model, path='/{id}', converters=dict(param=date)) def get_model(id=0, param=None): return Model(id, param) @app.view(model=Model) def default(self, request): return "View: %s %s" % (self.id, self.param) @app.view(model=Model, name='link') def link(self, request): return request.link(self) config.commit() c = Client(app) response = c.get('/1/link') assert response.body == '/1'
def test_variable_path_one_step(): config = setup() app = morepath.App(testing_config=config) class Model(object): def __init__(self, name): self.name = name @app.path(model=Model, path='{name}') def get_model(name): return Model(name) @app.view(model=Model) def default(self, request): return "View: %s" % self.name @app.view(model=Model, name='link') def link(self, request): return request.link(self) config.commit() c = Client(app) response = c.get('/foo') assert response.body == 'View: foo' response = c.get('/foo/link') assert response.body == '/foo'
def test_type_hints_and_converters(): config = setup() app = morepath.App(testing_config=config) class Model(object): def __init__(self, d): self.d = d from datetime import date @app.path(model=Model, path='', converters=dict(d=date)) def get_model(d): return Model(d) @app.view(model=Model) def default(self, request): return "View: %s" % self.d @app.view(model=Model, name='link') def link(self, request): return request.link(self) config.commit() c = Client(app) response = c.get('/?d=20140120') assert response.body == "View: 2014-01-20" response = c.get('/link?d=20140120') assert response.body == '/?d=20140120'
def test_link_for_none_means_no_parameter(): config = setup() app = morepath.App(testing_config=config) class Model(object): def __init__(self, id): self.id = id @app.path(model=Model, path='') def get_model(id): return Model(id) @app.view(model=Model) def default(self, request): return "View: %s" % self.id @app.view(model=Model, name='link') def link(self, request): return request.link(self) config.commit() c = Client(app) response = c.get('/') assert response.body == "View: None" response = c.get('/link') assert response.body == '/'
def test_variable_path_parameter_required_with_default(): config = setup() app = morepath.App(testing_config=config) class Model(object): def __init__(self, id): self.id = id @app.path(model=Model, path='', required=['id']) def get_model(id='b'): return Model(id) @app.view(model=Model) def default(self, request): return "View: %s" % self.id @app.view(model=Model, name='link') def link(self, request): return request.link(self) config.commit() c = Client(app) response = c.get('/?id=a') assert response.body == "View: a" response = c.get('/') assert response.status == '400 Bad Request'
def test_simple_path_two_steps(): config = setup() app = morepath.App(testing_config=config) class Model(object): def __init__(self): pass @app.path(model=Model, path='one/two') def get_model(): return Model() @app.view(model=Model) def default(self, request): return "View" @app.view(model=Model, name='link') def link(self, request): return request.link(self) config.commit() c = Client(app) response = c.get('/one/two') assert response.body == 'View' response = c.get('/one/two/link') assert response.body == '/one/two'
def test_url_parameter_implicit_converter(): config = setup() app = morepath.App(testing_config=config) class Model(object): def __init__(self, id): self.id = id @app.path(model=Model, path='/') def get_model(id=0): return Model(id) @app.view(model=Model) def default(self, request): return "View: %s (%s)" % (self.id, type(self.id)) @app.view(model=Model, name='link') def link(self, request): return request.link(self) config.commit() c = Client(app) response = c.get('/?id=1') assert response.body == "View: 1 (<type 'int'>)" response = c.get('/link?id=1') assert response.body == '/?id=1' response = c.get('/?id=broken') assert response.status == '400 Bad Request' response = c.get('/') assert response.body == "View: 0 (<type 'int'>)"
def test_implicit_function(): config = morepath.setup() app = morepath.App(testing_config=config) @app.path(path='') class Model(object): def __init__(self): pass @reg.generic def one(): return "Default one" @reg.generic def two(): return "Default two" @app.function(one) def one_impl(): return two() @app.function(two) def two_impl(): return "The real two" @app.view(model=Model) def default(self, request): return one() config.commit() c = Client(app) response = c.get('/') assert response.body == 'The real two'
def test_mount_parent_link(): config = setup() app = morepath.App('app', testing_config=config) @app.path(path='models/{id}') class Model(object): def __init__(self, id): self.id = id mounted = morepath.App('mounted', variables=['mount_id'], testing_config=config) @mounted.path(path='') class MountedRoot(object): def __init__(self, mount_id): self.mount_id = mount_id @mounted.view(model=MountedRoot) def root_default(self, request): return request.link(Model('one'), mounted=request.mounted().parent()) @app.mount(path='{id}', app=mounted) def get_context(id): return { 'mount_id': id } config.commit() c = Client(app) response = c.get('/foo') assert response.body == '/models/one'
def test_tween_directive(): config = morepath.setup() app = morepath.App(testing_config=config) @app.path(path='') class Root(object): pass @app.view(model=Root) def default(self, request): return "View" @app.tween_factory() def get_modify_response_tween(app, handler): def plusplustween(request): response = handler(request) response.headers['Tween-Header'] = 'FOO' return response return plusplustween config.commit() c = Client(app) response = c.get('/') assert response.body == 'View' assert response.headers['Tween-Header'] == 'FOO'
def test_link_to_none(): config = setup() app = morepath.App(testing_config=config) @app.path(path='') class Root(object): def __init__(self): self.value = 'ROOT' class Model(object): def __init__(self, id): self.id = id @app.view(model=Root) def root_link(self, request): return str(request.link(None) is None) @app.view(model=Root, name='default') def root_link_with_default(self, request): return request.link(None, default='unknown') config.commit() c = Client(app) response = c.get('/') assert response.body == 'True' response = c.get('/default') assert response.body == 'unknown'
def test_variable_path_two_variables(): config = setup() app = morepath.App(testing_config=config) class Model(object): def __init__(self, name, version): self.name = name self.version = version @app.path(model=Model, path='{name}-{version}') def get_model(name, version): return Model(name, version) @app.view(model=Model) def default(self, request): return "View: %s %s" % (self.name, self.version) @app.view(model=Model, name='link') def link(self, request): return request.link(self) config.commit() c = Client(app) response = c.get('foo-one') assert response.body == 'View: foo one' response = c.get('/foo-one/link') assert response.body == '/foo-one'
def test_implicit_variables(): config = setup() app = morepath.App(testing_config=config) @app.path(path='') class Root(object): pass class Model(object): def __init__(self, id): self.id = id @app.path(model=Model, path='{id}') def get_model(id): return Model(id) @app.view(model=Model) def default(self, request): return "The view for model: %s" % self.id @app.view(model=Model, name='link') def link(self, request): return request.link(self) config.commit() c = Client(app) response = c.get('/foo/link') assert response.body == '/foo'
def test_mount(): config = setup() app = morepath.App('app', testing_config=config) mounted = morepath.App('mounted', testing_config=config) @mounted.path(path='') class MountedRoot(object): pass @mounted.view(model=MountedRoot) def root_default(self, request): return "The root" @mounted.view(model=MountedRoot, name='link') def root_link(self, request): return request.link(self) @app.mount(path='{id}', app=mounted) def get_context(): return {} config.commit() c = Client(app) response = c.get('/foo') assert response.body == 'The root' response = c.get('/foo/link') assert response.body == '/foo'
def test_basic_auth_forget(): config = setup() app = morepath.App(testing_config=config) @app.path(path='{id}') class Model(object): def __init__(self, id): self.id = id @app.view(model=Model) def default(self, request): # will not actually do anything as it's a no-op for basic # auth, but at least won't crash response = Response(content_type='text/plain') generic.forget(response, request, lookup=request.lookup) return response @app.identity_policy() def policy(): return BasicAuthIdentityPolicy() config.commit() c = Client(app) response = c.get('/foo') assert response.status == '200 OK' assert response.body == '' assert sorted(response.headers.items()) == [ ('Content-Length', '0'), ('Content-Type', 'text/plain; charset=UTF-8'), ('WWW-Authenticate', 'Basic realm="Realm"'), ]
def test_basic_auth_remember(): config = setup() app = morepath.App(testing_config=config) @app.path(path='{id}', variables=lambda model: {'id': model.id}) class Model(object): def __init__(self, id): self.id = id @app.view(model=Model) def default(self, request): # will not actually do anything as it's a no-op for basic # auth, but at least won't crash response = Response() generic.remember(response, request, Identity('foo'), lookup=request.lookup) return response @app.identity_policy() def policy(): return BasicAuthIdentityPolicy() config.commit() c = Client(app) response = c.get('/foo') assert response.status == '200 OK' assert response.body == ''
def test_abbr_imperative(): config = setup() app = morepath.App(testing_config=config) class Model(object): pass @app.path(path='/', model=Model) def get_model(): return Model() with app.view(model=Model) as view: @view() def default(self, request): return "Default view" @view(name='edit') def edit(self, request): return "Edit view" config.commit() c = Client(app) response = c.get('/') assert response.body == 'Default view' response = c.get('/edit') assert response.body == 'Edit view'
def test_link_to_unknown_model(): config = setup() app = morepath.App(testing_config=config) @app.path(path='') class Root(object): def __init__(self): self.value = 'ROOT' class Model(object): def __init__(self, id): self.id = id @app.view(model=Root) def root_link(self, request): try: return request.link(Model('foo')) except LinkError: return "Link error" @app.view(model=Root, name='default') def root_link_with_default(self, request): try: return request.link(Model('foo'), default='hey') except LinkError: return "Link Error" config.commit() c = Client(app) response = c.get('/') assert response.body == 'Link error' response = c.get('/default') assert response.body == 'Link Error'
def test_mount_child_link_unknown_app(): config = setup() app = morepath.App('app', testing_config=config) mounted = morepath.App('mounted', variables=['mount_id'], testing_config=config) @mounted.path(path='models/{id}') class Model(object): def __init__(self, id): self.id = id @app.path(path='') class Root(object): pass @app.view(model=Root) def app_root_default(self, request): try: return request.link( Model('one'), mounted=request.mounted().child(mounted, id='foo')) except LinkError: return "link error" # no mounting, so mounted is unknown when making link config.commit() c = Client(app) response = c.get('/') assert response.body == 'link error'
def test_overrides_view(): config = setup() app = App(testing_config=config) overriding = App(extends=[app], testing_config=config) @app.path(path='users/{username}') class User(object): def __init__(self, username): self.username = username @app.view(model=User) def render_user(self, request): return "User: %s" % self.username @overriding.view(model=User) def render_user2(self, request): return "USER: %s" % self.username config.commit() cl = Client(app) response = cl.get('/users/foo') assert response.body == 'User: foo' cl = Client(overriding) response = cl.get('/users/foo') assert response.body == 'USER: foo'
def test_mount_context_parameters_empty_context(): config = setup() app = morepath.App('app', testing_config=config) mounted = morepath.App('mounted', variables=['mount_id'], testing_config=config) @mounted.path(path='') class MountedRoot(object): # use a default parameter def __init__(self, mount_id='default'): self.mount_id = mount_id @mounted.view(model=MountedRoot) def root_default(self, request): return "The root for mount id: %s" % self.mount_id # the context does not in fact construct the context. # this means the parameters are instead constructed from the # arguments of the MountedRoot constructor, and these # default to 'default' @app.mount(path='{id}', app=mounted) def get_context(id): return {} config.commit() c = Client(app) response = c.get('/foo') assert response.body == 'The root for mount id: default' # the URL parameter mount_id cannot interfere with the mounting # process response = c.get('/bar?mount_id=blah') assert response.body == 'The root for mount id: default'
def test_variable_path_explicit_trumps_implicit(): config = setup() app = morepath.App(testing_config=config) class Model(object): def __init__(self, id): self.id = id @app.path(model=Model, path='{id}', converters=dict(id=Converter(int))) def get_model(id='foo'): return Model(id) @app.view(model=Model) def default(self, request): return "View: %s (%s)" % (self.id, type(self.id)) @app.view(model=Model, name='link') def link(self, request): return request.link(self) config.commit() c = Client(app) response = c.get('1') assert response.body == "View: 1 (<type 'int'>)" response = c.get('/1/link') assert response.body == '/1' response = c.get('broken') assert response.status == '404 Not Found'
def test_mount_context_parameters(): config = setup() app = morepath.App('app', testing_config=config) mounted = morepath.App('mounted', variables=['mount_id'], testing_config=config) @mounted.path(path='') class MountedRoot(object): def __init__(self, mount_id): assert isinstance(mount_id, int) self.mount_id = mount_id @mounted.view(model=MountedRoot) def root_default(self, request): return "The root for mount id: %s" % self.mount_id @app.mount(path='mounts', app=mounted) def get_context(mount_id=0): return { 'mount_id': mount_id } config.commit() c = Client(app) response = c.get('/mounts?mount_id=1') assert response.body == 'The root for mount id: 1' response = c.get('/mounts') assert response.body == 'The root for mount id: 0'
def test_mount_context(): config = setup() app = morepath.App('app', testing_config=config) mounted = morepath.App('mounted', variables=['mount_id'], testing_config=config) @mounted.path(path='') class MountedRoot(object): def __init__(self, mount_id): self.mount_id = mount_id @mounted.view(model=MountedRoot) def root_default(self, request): return "The root for mount id: %s" % self.mount_id @app.mount(path='{id}', app=mounted) def get_context(id): return { 'mount_id': id } config.commit() c = Client(app) response = c.get('/foo') assert response.body == 'The root for mount id: foo' response = c.get('/bar') assert response.body == 'The root for mount id: bar'
def test_permission_directive_no_identity(): config = setup() app = morepath.App(testing_config=config) class Model(object): def __init__(self, id): self.id = id class Permission(object): pass @app.path(model=Model, path='{id}', variables=lambda model: {'id': model.id}) def get_model(id): return Model(id) @app.permission(model=Model, permission=Permission, identity=None) def get_permission(identity, model, permission): if model.id == 'foo': return True else: return False @app.view(model=Model, permission=Permission) def default(self, request): return "Model: %s" % self.id config.commit() c = Client(app) response = c.get('/foo') assert response.body == 'Model: foo' response = c.get('/bar') assert response.status == '401 Unauthorized'
def test_no_permission(): config = setup() app = morepath.App(testing_config=config) class Model(object): def __init__(self, id): self.id = id class Permission(object): pass @app.path(model=Model, path='{id}', variables=lambda model: {'id': model.id}) def get_model(id): return Model(id) @app.view(model=Model, permission=Permission) def default(self, request): return "Model: %s" % self.id config.commit() c = Client(app) response = c.get('/foo') assert response.status == '401 Unauthorized'
def test_mount_repr(): config = setup() app = morepath.App('app', testing_config=config) mounted = morepath.App('mounted', variables=['mount_id'], testing_config=config) @mounted.path(path='models/{id}') class Model(object): def __init__(self, id): self.id = id @app.path(path='') class Root(object): pass @app.view(model=Root) def app_root_default(self, request): return repr(request.mounted().child(mounted, id='foo')) @app.mount(path='{id}', app=mounted) def get_context(id): return { 'mount_id': id } config.commit() c = Client(app) response = c.get('/') assert response.body == ( "<morepath.Mount of <morepath.App 'mounted'> with " "variables: id='foo', " "parent=<morepath.Mount of <morepath.App 'app'>>>")
def test_extra_predicates(): config = setup() app = App(testing_config=config) @app.path(path="{id}") class Model(object): def __init__(self, id): self.id = id @app.view(model=Model, name="foo", id="a") def get_a(self, request): return "a" @app.view(model=Model, name="foo", id="b") def get_b(self, request): return "b" @app.predicate(name="id", order=2, default="") def get_id(self, request): return self.id config.commit() c = Client(app) response = c.get("/a/foo") assert response.body == "a" response = c.get("/b/foo") assert response.body == "b"
def test_root_link_with_parameters(): config = setup() app = morepath.App(testing_config=config) @app.path(path='') class Root(object): def __init__(self, param=0): assert isinstance(param, int) self.param = param @app.view(model=Root) def default(self, request): return "The view for root: %s" % self.param @app.view(model=Root, name='link') def link(self, request): return request.link(self) config.commit() c = Client(app) response = c.get('/') assert response.body == 'The view for root: 0' response = c.get('/link') assert response.body == '/?param=0' response = c.get('/?param=1') assert response.body == 'The view for root: 1' response = c.get('/link?param=1') assert response.body == '/?param=1'
def __init__(self, pipeline=None, redirect=False): app = pipeline or client_pipeline() if redirect: app = auto_redirect_filter(app) else: pass Client.__init__(self, pipeline=app, assert_=_200_or_302)
def options(self, url, query_string=None, post={}, headers={}, status=None): kw = dict(query_string=query_string, headers=headers, post=post) kw["assert_"] = get_assert(status) return Client.options(self, url, **kw)
def post(self, url, query_string=None, post={}, headers={}, files={}, status=None): kw = dict(query_string=query_string, headers=headers, post=post, files=files) kw["assert_"] = get_assert(status) return Client.post(self, url, **kw)
""" passing parameters as a query string """ from webobtoolkit.client import Client client = Client() result = client.get("http://ajax.googleapis.com/ajax/services/search/web", query_string=dict(v="1.0", q="define: HTTP")).json for k, v in result.items(): print k, ":", v
def head(self, url, query_string=None, headers={}, status=None): kw = dict(query_string=query_string, headers=headers) kw["assert_"] = get_assert(status) return Client.head(self, url, **kw)
""" passing parameters as a form post """ from webobtoolkit.client import Client client = Client() def assert_success(request, response): """ if response status != 200 then raise an error """ if response.status_int != 200: raise Exception("Did not get a valid response from %s" % request.url) print client.post("http://ajax.googleapis.com/ajax/services/search/web", post=dict(v="1.0", q="define: HTTP"), assert_=assert_success)
""" getting a response from wikipedia.org """ from webobtoolkit.client import Client client = Client() print client.get("http://en.wikipedia.org/wiki/HTTP")
""" uploading files example """ from webobtoolkit.client import Client, client_pipeline from webob import Request, Response def application(environ, start_response): """this application merely spits out the keys of the form that was posted. we are using webob Request and Response for brevity """ request = Request(environ) return Response(str(request.POST.keys()))(environ, start_response) client = Client(pipeline=client_pipeline(application)) print client.post("/", files=dict(file1=("myfile.txt", "this is a file containing this text")))