def test_mount_link_prefix(): class App(morepath.App): pass class Mounted(morepath.App): def __init__(self, mount_id): self.mount_id = mount_id @App.mount(path='/mnt/{id}', app=Mounted, variables=lambda a: dict(id=a.mount_id)) def get_mounted(id): return Mounted(mount_id=id) @App.path(path='') class AppRoot(object): pass @Mounted.path(path='') class MountedRoot(object): pass @App.link_prefix() def link_prefix(request): return 'http://app' @Mounted.link_prefix() def mounted_link_prefix(request): return 'http://mounted' @App.view(model=AppRoot, name='get-root-link') def get_root_link(self, request): return request.link(self) @Mounted.view(model=MountedRoot, name='get-mounted-root-link') def get_mounted_root_link(self, request): return request.link(self) @Mounted.view(model=MountedRoot, name='get-root-link-through-mount') def get_root_link_through_mount(self, request): parent = request.app.parent return request.view(AppRoot(), app=parent, name='get-root-link') dectate.commit(App, Mounted) c = Client(App()) # response = c.get('/get-root-link') # assert response.body == b'http://app/' # response = c.get('/mnt/1/get-mounted-root-link') # assert response.body == b'http://mounted/mnt/1' response = c.get('/mnt/1/get-root-link-through-mount') assert response.body == b'http://app/' response = c.get('/get-root-link') assert response.body == b'http://app/'
def test_group(): c = Client(App()) response = c.get("/groups/1") group = { "@id": "/groups/1", "name": "Admin", "users": ["*****@*****.**"] } assert_dict_contains_subset(group, response.json)
def test_deferred_deferred_view(): class root(morepath.App): pass class alpha(morepath.App): pass class beta(morepath.App): pass @root.path(path='') class RootModel(object): pass @root.json(model=RootModel) def root_model_default(self, request): return request.view(AlphaModel()) @alpha.path(path='') class AlphaModel(object): pass @alpha.json(model=AlphaModel) def alpha_model_default(self, request): return {"model": "alpha"} @beta.path(path='') class BetaModel(object): pass @beta.json(model=BetaModel) def beta_model_default(self, request): return request.view(AlphaModel()) @root.mount(app=alpha, path='alpha') def mount_alpha(): return alpha() @root.mount(app=beta, path='beta') def mount_beta(): return beta() @beta.defer_links(model=AlphaModel) def defer_links_parent(app, obj): return app.parent @root.defer_links(model=AlphaModel) def defer_links_alpha(app, obj): return app.child(alpha()) c = Client(root()) response = c.get('/') assert response.json == {'model': 'alpha'} response = c.get('/beta') assert response.json == {'model': 'alpha'}
def test_error_handling_handles_errors_caused_by_a_lack_of_query(): wsgi = graphql_wsgi(TestSchema, pretty=True) c = Client(wsgi) response = c.get('/', status=400) assert response.json == { 'errors': [{'message': 'Must provide query string.'}] }
def test_nested(): c = Client(nested.outer_app()) response = c.get('/inner/foo') assert response.body == b'The view for model: foo' response = c.get('/inner/foo/link') assert response.body == b'http://localhost/inner/foo'
def test_view_user_delete(gazette_app): admin = Client(gazette_app) login_admin(admin) client_1 = Client(gazette_app) login_editor_1(client_1) client_1.get('/dashboard') client_2 = Client(gazette_app) login_editor_1(client_2) client_2.get('/dashboard') manage = admin.get('/users').click("Löschen", href='editor1') manage = manage.form.submit().maybe_follow() assert "Benutzer gelöscht." in manage client_1.get('/dashboard', status=403) client_2.get('/dashboard', status=403)
def test_non_aligned_page(): c = Client(App()) r = c.get('/?start=5') assert ([a.text for a in r.html.select('table a') ] == [p.name for p in person_db[5:15]]) prev_url, = [a['href'] for a in r.html.findAll('a', text='Previous')] assert ([a.text for a in c.get(prev_url).html.select('table a') ] == [p.name for p in person_db[:10]])
def test_basic(): c = Client(basic.app()) response = c.get('/foo') assert response.body == b'The view for model: foo' response = c.get('/foo/link') assert response.body == b'http://localhost/foo'
def test_schema_requests(tmp_path: Path) -> None: ''' Test requests in the form `GET /schema/{schema-name}`. ''' with using_context(sample_context(tmp_path)): get = Client(API()).get assert get('/schemas/spec').json == get_spec_schema() assert get('/schemas/spec-list').json == get_spec_list_schema() assert get('/schemas/spec-dict').json == get_spec_dict_schema()
def test_add_submit(): c = Client(App()) response = c.post( '/documents/add_submit', {'title': 'My Title', 'content': 'My Content'} ) assert response.body == b'<p>Awesome 1</p>'
def test_root(): morepath.scan(synonymista_api) morepath.commit(synonymista_api.App) client = Client(synonymista_api.App()) root = client.get('/') assert root.status_code == 200 assert len(root.json['greetings']) == 2
def test_cookie_identity_policy(): class app(morepath.App): pass @app.path(path='{id}') class Model(object): def __init__(self, id): self.id = id class Permission(object): pass @app.permission_rule(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_identity(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_identity(response, request, lookup=request.lookup) return response @app.identity_policy() def policy(): return DumbCookieIdentityPolicy() @app.verify_identity() def verify_identity(identity): return True dectate.commit(app) c = Client(app(), cookiejar=CookieJar()) response = c.get('/foo', status=403) response = c.get('/foo/log_in') response = c.get('/foo', status=200) assert response.body == b'Model: foo' response = c.get('/foo/log_out') response = c.get('/foo', status=403)
def test_documents(): c = Client(App()) collection_response = c.get('/documents') assert collection_response.json["documents"] == [] assert "http://localhost/documents/add" in collection_response.json["add"] assert collection_response.json["previous"] is None assert collection_response.json["next"] is None
def test_nested(): c = Client(nested.outer_app()) response = c.get("/inner/foo") assert response.body == b"The view for model: foo" response = c.get("/inner/foo/link") assert response.body == b"http://localhost/inner/foo"
def test_basic(): c = Client(basic.app()) response = c.get("/foo") assert response.body == b"The view for model: foo" response = c.get("/foo/link") assert response.body == b"http://localhost/foo"
def test_view_notices_index(gazette_app): with freeze_time("2017-11-01 11:00"): client = Client(gazette_app) login_publisher(client) # new notice manage = client.get('/notices/drafted/new-notice') manage.form['title'] = "Erneuerungswahlen" manage.form['organization'] = '200' manage.form['category'] = '11' manage.form['issues'] = ['2017-44', '2017-45'] manage.form['text'] = "1. Oktober 2017" manage.form['author_place'] = 'Govikon' manage.form['author_name'] = 'State Chancellerist' manage.form['author_date'] = '2019-01-01' manage.form.submit() client.get('/notice/erneuerungswahlen/submit').form.submit() client.get('/notice/erneuerungswahlen/accept').form.submit() manage = client.get('/notices/drafted/new-notice') manage.form['title'] = "Kantonsratswahlen" manage.form['organization'] = '300' manage.form['category'] = '12' manage.form['issues'] = ['2017-45', '2017-46'] manage.form['text'] = "10. Oktober 2017" manage.form['author_place'] = 'Govikon' manage.form['author_name'] = 'State Chancellerist' manage.form['author_date'] = '2019-01-01' manage.form.submit() client.get('/notice/kantonsratswahlen/submit').form.submit() client.get('/notice/kantonsratswahlen/accept').form.submit() publish_issue(client, '2017-44') publish_issue(client, '2017-45') publish_issue(client, '2017-46') response = client.get('/notices/published/index') assert response.headers['Content-Type'] == 'application/pdf' assert response.headers['Content-Disposition'] == \ 'inline; filename=amtsblatt-govikon.pdf' reader = PdfFileReader(BytesIO(response.body)) assert [page.extractText() for page in reader.pages ] == [('© 2017 Govikon\n1\nAmtsblatt\nStichwortverzeichnis\n' 'Organisationen\n' 'C\n' 'Civic Community 2017-44-1, 2017-45-2\n' 'M\n' 'Municipality 2017-45-3, 2017-46-4\n'), ('Amtsblatt\n© 2017 Govikon\n2\n' 'Rubriken\n' 'E\n' 'Education 2017-44-1, 2017-45-2\n' 'S\n' 'Submissions 2017-45-3, 2017-46-4\n')]
def test_basic_scenario(): class app(morepath.App): pass @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) dectate.commit(app) c = Client(app()) response = c.get('/foo') assert response.body == b'The view for model: foo' response = c.get('/foo/link') assert response.body == b'http://localhost/foo' response = c.get('/foo/json') assert response.body == b'{"id": "foo"}' response = c.get('/') assert response.body == b'The root: ROOT' # + is to make sure we get the view, not the sub-model response = c.get('/+link') assert response.body == b'http://localhost/'
def test_template(): c = Client(template.App()) response = c.get("/persons/world") assert (response.body == b"""\ <html> <body> <p>Hello world!</p> </body> </html>""")
def test_scanned_static_method(): dectate.commit(method.app) c = Client(method.app()) response = c.get('/static') assert response.body == b'Static Method' root = method.Root() assert isinstance(root.static_method(), method.StaticMethod)
def test_static_assets(): c = Client(App()) r = c.get('/') scripts = r.html.select('script') assert len(scripts) == 2 for s in scripts: c.get(s['src'])
def get_client(app, config='settings.yml'): if isinstance(config, str): with open(os.path.join(os.path.dirname(__file__), config)) as f: settings = yaml.load(f) or {} else: settings = config appobj = create_app(app, settings) c = Client(appobj) return c
def test_404_http_exception(): class app(morepath.App): pass @app.path(path="") class Root(object): pass c = Client(app()) c.get("/", status=404)
def test_root(): morepath.scan(rassle) morepath.commit(rassle.App) client = Client(rassle.App()) root = client.get('/') assert root.status_code == 200 assert '/greeting/world' in root assert '/greeting/mundo' in root
def test_artifact_delete_requests(tmp_path: Path) -> None: ''' Test requests in the form `DELETE /artifacts{/path*}`. ''' with using_context(sample_context(tmp_path)): delete = Client(API()).delete delete('/artifacts/x') assert listdir(tmp_path) == ['y'] delete('/artifacts/y') assert listdir(tmp_path) == []
def test_change_todo(): c = Client(App()) changed_todo_json = json.dumps( {"title": "Changed Test", "completed": True} ) response = c.put('/todos/1', changed_todo_json) changed_todo_response = {"@id": "http://localhost/todos/1", "title": "Changed Test", "completed": True} assert response.json == changed_todo_response
def test_POST_functionaly_POST_raw_text_query_with_GET_variable_values(): wsgi = graphql_wsgi(TestSchema) c = Client(wsgi) response = c.post("/?variables=%s" % json.dumps({'who': 'Dolly'}), b'query helloWho($who: String){ test(who: $who) }', content_type='application/graphql') assert response.json == {'data': {'test': 'Hello Dolly'}}
def test_POST_functionality_url_encoded_query_with_GET_variable_values(): wsgi = graphql_wsgi(TestSchema) c = Client(wsgi) response = c.post( "/?variables=%s" % json.dumps({'who': 'Dolly'}), {'query': b'query helloWho($who: String){ test(who: $who) }'}) assert response.json == {'data': {'test': 'Hello Dolly'}}
def test_abbr(): dectate.commit(abbr.app) c = Client(abbr.app()) response = c.get('/foo') assert response.body == b'Default view: foo' response = c.get('/foo/edit') assert response.body == b'Edit view: foo'
def test_POST_functionality_allows_other_UTF_charsets(): wsgi = graphql_wsgi(TestSchema) c = Client(wsgi) response = c.post('/', u'{ test(who: "World") }'.encode('utf_16_le'), content_type='application/graphql; charset=utf-16') assert response.json == {'data': {'test': 'Hello World'}}
def test_view_exceptions(gazette_app): client = Client(gazette_app) assert ( "Sie versuchen eine Seite zu öffnen, für die Sie nicht autorisiert " "sind") in client.get('/groups', status=403) assert ( "Die angeforderte Seite konnte nicht gefunden werden.") in client.get( '/groupz', status=404)