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_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_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_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_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_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_extends(): config = setup() app = App(testing_config=config) extending = 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 @extending.view(model=User, name='edit') def edit_user(self, request): return "Edit user: %s" % self.username config.commit() cl = Client(app) response = cl.get('/users/foo') assert response.body == 'User: foo' response = cl.get('/users/foo/edit') assert response.status == '404 Not Found' cl = Client(extending) response = cl.get('/users/foo') assert response.body == 'User: foo' response = cl.get('/users/foo/edit') assert response.body == 'Edit user: foo'
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_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_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 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_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_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_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_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_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_overrides_model(): 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.path(model=User, path='users/{username}') def get_user(username): if username != 'bar': return None return User(username) config.commit() cl = Client(app) response = cl.get('/users/foo') assert response.body == 'User: foo' response = cl.get('/users/bar') assert response.body == 'User: bar' cl = Client(overriding) response = cl.get('/users/foo') assert response.status == '404 Not Found' response = cl.get('/users/bar') assert response.body == 'User: bar'
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_run_app_with_context_without_it(): config = setup() app = morepath.App('app', variables=['mount_id'], testing_config=config) config.commit() c = Client(app) with pytest.raises(MountError): c.get('/foo')
def test_implicit_function_mounted(): config = morepath.setup() alpha = morepath.App(testing_config=config) beta = morepath.App(testing_config=config, variables=['id']) @alpha.mount(path='mounted/{id}', app=beta) def mount_beta(id): return {'id': id} class AlphaRoot(object): pass class Root(object): def __init__(self, id): self.id = id @alpha.path(path='/', model=AlphaRoot) def get_alpha_root(): return AlphaRoot() @beta.path(path='/', model=Root) def get_root(id): return Root(id) @reg.generic def one(): return "Default one" @reg.generic def two(): return "Default two" @beta.function(one) def one_impl(): return two() @beta.function(two) def two_impl(): return "The real two" @alpha.view(model=AlphaRoot) def alpha_default(self, request): return one() @beta.view(model=Root) def default(self, request): return "View for %s, message: %s" % (self.id, one()) config.commit() c = Client(alpha) response = c.get('/mounted/1') assert response.body == 'View for 1, message: The real two' response = c.get('/') assert response.body == 'Default one'
def test_basic_testing_config(): 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.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) @app.view(model=Model, name='json', render=morepath.render_json) def json(self, request): return {'id': self.id} @app.view(model=Root) def root_default(self, request): return "The root: %s" % self.value @app.view(model=Root, name='link') def root_link(self, request): return request.link(self) config.commit() c = Client(app) response = c.get('/foo') assert response.body == 'The view for model: foo' response = c.get('/foo/link') assert response.body == '/foo' response = c.get('/foo/json') assert response.body == '{"id": "foo"}' response = c.get('/') assert response.body == 'The root: ROOT' # + is to make sure we get the view, not the sub-model response = c.get('/+link') assert response.body == '/'
def test_cookie_identity_policy(): config = setup() app = morepath.App(testing_config=config) @app.path(path='{id}') class Model(object): def __init__(self, id): self.id = id class Permission(object): pass @app.permission(model=Model, permission=Permission) def get_permission(identity, model, permission): return identity.userid == 'user' @app.view(model=Model, permission=Permission) def default(self, request): return "Model: %s" % self.id @app.view(model=Model, name='log_in') def log_in(self, request): response = Response() generic.remember(response, request, Identity(userid='user', payload='Amazing'), lookup=request.lookup) return response @app.view(model=Model, name='log_out') def log_out(self, request): response = Response() generic.forget(response, request, lookup=request.lookup) return response @app.identity_policy() def policy(): return DumbCookieIdentityPolicy() config.commit() c = Client(client_pipeline(app, cookie_support=True)) response = c.get('/foo') assert response.status == '401 Unauthorized' response = c.get('/foo/log_in') response = c.get('/foo') assert response.status == '200 OK' assert response.body == 'Model: foo' response = c.get('/foo/log_out') response = c.get('/foo') assert response.status == '401 Unauthorized'
def test_policy_action(): config = setup() config.scan(identity_policy) config.commit() c = Client(identity_policy.app) response = c.get('/foo') assert response.body == 'Model: foo' response = c.get('/bar') assert response.status == '401 Unauthorized'
def test_abbr(): config = setup() config.scan(abbr) config.commit() c = Client(abbr.app) response = c.get('/foo') assert response.body == 'Default view: foo' response = c.get('/foo/edit') assert response.body == 'Edit view: foo'
def test_basic(): config = setup() config.scan(basic) config.commit() c = Client(basic.app) response = c.get('/foo') assert response.body == 'The view for model: foo' response = c.get('/foo/link') assert response.body == '/foo'
def test_nested(): config = setup() config.scan(nested) config.commit() c = Client(nested.outer_app) response = c.get('/inner/foo') assert response.body == 'The view for model: foo' response = c.get('/inner/foo/link') assert response.body == '/inner/foo'
def test_custom_date_converter(): config = setup() app = morepath.App(testing_config=config) class Model(object): def __init__(self, d): self.d = d from datetime import date from time import strptime, mktime def date_decode(s): return date.fromtimestamp(mktime(strptime(s, '%d-%m-%Y'))) def date_encode(d): return d.strftime('%d-%m-%Y') @app.converter(type=date) def date_converter(): return Converter(date_decode, date_encode) @app.path(model=Model, path='/') def get_model(d=date(2011, 1, 1)): 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=10-11-2012') assert response.body == "View: 2012-11-10" response = c.get('/') assert response.body == "View: 2011-01-01" response = c.get('/link?d=10-11-2012') assert response.body == '/?d=10-11-2012' response = c.get('/link') assert response.body == '/?d=01-01-2011' response = c.get('/?d=broken') assert response.status == '400 Bad Request'
""" 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 get(self, url, query_string=None, headers={}, status=None): kw = dict(query_string=query_string, headers=headers) kw["assert_"] = get_assert(status) return Client.get(self, url, **kw)
""" getting a response from wikipedia.org """ from webobtoolkit.client import Client client = Client() print client.get("http://en.wikipedia.org/wiki/HTTP")