class ChannelsViewTest(unittest.TestCase): def setUp(self): self.c = Client(views.handler, BaseResponse) def test_POSTing_with_create_client_id_header_should_create_new_channel(self): r = self.c.post(path="/channels/", headers={"Create-Client-Id":"1"}) self.assertEquals(r.status_code, 200) self.assertTrue("1" in views.clients)
def test_ie7_unc_path(): client = Client(form_data_consumer, Response) data_file = join(dirname(__file__), 'multipart', 'ie7_full_path_request.txt') data = get_contents(data_file) boundary = '---------------------------7da36d1b4a0164' response = client.post('/?object=cb_file_upload_multiple', data=data, content_type= 'multipart/form-data; boundary="%s"' % boundary, content_length=len(data)) lines = response.data.split('\n', 3) assert lines[0] == repr(u'Sellersburg Town Council Meeting 02-22-2010doc.doc'), lines[0]
def test_multipart(): """Tests multipart parsing against data collected from webbrowsers""" resources = join(dirname(__file__), 'multipart') client = Client(form_data_consumer, Response) repository = [ ('firefox3-2png1txt', '---------------------------186454651713519341951581030105', [ (u'anchor.png', 'file1', 'image/png', 'file1.png'), (u'application_edit.png', 'file2', 'image/png', 'file2.png') ], u'example text'), ('firefox3-2pnglongtext', '---------------------------14904044739787191031754711748', [ (u'accept.png', 'file1', 'image/png', 'file1.png'), (u'add.png', 'file2', 'image/png', 'file2.png') ], u'--long text\r\n--with boundary\r\n--lookalikes--'), ('opera8-2png1txt', '----------zEO9jQKmLc2Cq88c23Dx19', [ (u'arrow_branch.png', 'file1', 'image/png', 'file1.png'), (u'award_star_bronze_1.png', 'file2', 'image/png', 'file2.png') ], u'blafasel öäü'), ('webkit3-2png1txt', '----WebKitFormBoundaryjdSFhcARk8fyGNy6', [ (u'gtk-apply.png', 'file1', 'image/png', 'file1.png'), (u'gtk-no.png', 'file2', 'image/png', 'file2.png') ], u'this is another text with ümläüts'), ('ie6-2png1txt', '---------------------------7d91b03a20128', [ (u'file1.png', 'file1', 'image/x-png', 'file1.png'), (u'file2.png', 'file2', 'image/x-png', 'file2.png') ], u'ie6 sucks :-/') ] for name, boundary, files, text in repository: folder = join(resources, name) data = get_contents(join(folder, 'request.txt')) for filename, field, content_type, fsname in files: response = client.post('/?object=' + field, data=data, content_type= 'multipart/form-data; boundary="%s"' % boundary, content_length=len(data)) lines = response.data.split('\n', 3) assert lines[0] == repr(filename) assert lines[1] == repr(field) assert lines[2] == repr(content_type) assert lines[3] == get_contents(join(folder, fsname)) response = client.post('/?object=text', data=data, content_type= 'multipart/form-data; boundary="%s"' % boundary, content_length=len(data)) assert response.data == repr(text)
class TasksViewTest(unittest.TestCase): def setUp(self): self.c = Client(views.handler, BaseResponse) # clear state views.TASKS = {} views.clients = {} views.subscriptions = {} def test_POST(self): t = models.Task(name='Task1') r = self.c.post(path='/tasks/', headers={'Content-Type':tubes.JSON}, data=t.to_json_str()) # check response self.assertEquals(r.status_code, 201) task = json.loads(r.data) self.assertEquals(task['name'], 'Task1') self.assertTrue('/tasks/0' in r.headers.get('Location')) # back-end task = views.TASKS['0'] self.assertTrue(task != None) self.assertEquals(task.name, 'Task1') def test_PUT(self): views.TASKS['0'] = models.Task() r = self.c.put(path='/tasks/0', headers={'Content-Type':tubes.JSON}, data=models.Task(name='Task_0').to_json_str()) self.assertEquals(r.status_code, 200) # check response task = json.loads(r.data) self.assertEquals(task['name'], 'Task_0') # back-end task = views.TASKS['0'] self.assertEquals(task.name, 'Task_0') def test_DELETE(self): views.TASKS['0'] = models.Task() r = self.c.delete(path='/tasks/0') self.assertEquals(r.status_code, 204) self.assertTrue(views.TASKS.get('0') == None) def test_GET_tasks(self): views.TASKS['0'] = models.Task(name='foo') r = self.c.get(path='/tasks/') self.assertTrue('foo' in r.data) def test_GET_task(self): views.TASKS['0'] = models.Task(name='foo') r = self.c.get(path='/tasks/0') self.assertTrue('foo' in r.data)
def test_ie7_unc_path(): client = Client(form_data_consumer, Response) data_file = join(dirname(__file__), 'multipart', 'ie7_full_path_request.txt') data = get_contents(data_file) boundary = '---------------------------7da36d1b4a0164' response = client.post('/?object=cb_file_upload_multiple', data=data, content_type='multipart/form-data; boundary="%s"' % boundary, content_length=len(data)) lines = response.data.split('\n', 3) assert lines[0] == repr( u'Sellersburg Town Council Meeting 02-22-2010doc.doc'), lines[0]
def test_post_with_redirect(self): form = { 'title': 'タイトル2', 'handle': 'ハンドル名2', 'message': 'メッセージ2', } sut = Client(app, Response) # Werkzeugのドキュメントには見当たらなかったが、Flaskと同じようにしたら動作 # http://flask.pocoo.org/docs/0.12/testing/#logging-in-and-out actual = sut.post('/', data=form, follow_redirects=True) assert actual.status_code == 200 assert actual.headers['content-type'] == 'text/html; charset=UTF-8' body = actual.get_data(as_text=True) assert 'テスト掲示板' in body assert 'タイトル2' in body assert 'ハンドル名2' in body assert 'メッセージ2' in body
def register_user( client: Client, first_name: str = "first_name", last_name: str = "last_name", email: str = "*****@*****.**", password: str = "password", ) -> Response: resp = client.post( url_for("user.create_user"), json={ "firstName": first_name, "lastName": last_name, "email": email, "password": password, }, ) return resp
def test_send_transaction_risk_error(self, authorized_client: Client, monkeypatch): def send_transaction_mock_risk_failure( sender_id: int, amount: int, currency: DiemCurrency, destination_address: str, destination_subaddress: Optional[str] = None, payment_type: Optional[TransactionType] = None, ) -> Optional[Transaction]: raise transaction_service.RiskCheckError("Risk!") monkeypatch.setattr(transaction_service, "send_transaction", send_transaction_mock_risk_failure) rv: Response = authorized_client.post("/account/transactions", json=TestSendTransaction.tx_data) assert rv.status_code == HTTPStatus.FAILED_DEPENDENCY
def from_wsgi(schema_path: str, app: Any, base_url: Optional[str] = None, **kwargs: Any) -> GraphQLSchema: """Load GraphQL schema from a WSGI app. :param str schema_path: An in-app relative URL to the schema. :param app: A WSGI app instance. :param Optional[str] base_url: Base URL to send requests to. :return: GraphQLSchema """ setup_headers(kwargs) kwargs.setdefault("json", {"query": INTROSPECTION_QUERY}) client = Client(app, WSGIResponse) response = client.post(schema_path, **kwargs) HTTPError.check_response(response, schema_path) return from_dict(raw_schema=response.json["data"], location=schema_path, base_url=base_url, app=app)
class TaskViewTest(GAETestBase): CLEANUP_USED_KIND = True def setUp(self): init_recording() app = get_application() self.client = Client(app, BaseResponse) def tearDown(self): disable_recording() def test_post(self): data = { "name": "foo", } response = self.client.post('/tasks', data=data, follow_redirects=True) actual = Task.all().get() self.assertEquals("foo", actual.name) def test_delete(self): key = Task(name="fuga").put() tasks = Task.all().fetch(100) self.assertEquals(1, len(tasks)) response = self.client.delete('/tasks/%s' % key, follow_redirects=True) tasks = Task.all().fetch(100) self.assertEquals(0, len(tasks)) def test_put(self): key = Task(name="hoge").put() tasks = Task.all().fetch(100) self.assertEquals(1, len(tasks)) input_stream = StringIO('{ "name" : "fuga" }') response = self.client.put('/tasks/%s' % key, input_stream=input_stream, follow_redirects=True) actual = Task.get(key) self.assertEquals("fuga", actual.name)
def test_send_transaction_to_self_error(self, authorized_client: Client, monkeypatch): def send_transaction_mock_self_as_destination_failure( sender_id: int, amount: int, currency: DiemCurrency, destination_address: str, destination_subaddress: Optional[str] = None, payment_type: Optional[TransactionType] = None, ) -> Optional[Transaction]: raise transaction_service.SelfAsDestinationError( "Don't send to self!") monkeypatch.setattr( transaction_service, "send_transaction", send_transaction_mock_self_as_destination_failure, ) rv: Response = authorized_client.post("/account/transactions", json=TestSendTransaction.tx_data) assert rv.status_code == HTTPStatus.FORBIDDEN
def test_send_transaction(self, authorized_client: Client, allow_user_to_account, send_transaction_mock) -> None: rv: Response = authorized_client.post("/account/transactions", json=TestSendTransaction.tx_data) assert rv.status_code == 200 transaction = rv.get_json() assert transaction == { "id": 5, "amount": 100, "currency": DiemCurrency.Coin1.value, "direction": TransactionDirection.SENT.value, "status": TransactionStatus.PENDING.value, "timestamp": "2020-06-23T19:50:26.989849", "source": { "full_addr": "tlb1p4242424242424242424242424gfzee6zpu2ma3gx6wpuy", "user_id": "122ce7420f15bec5", "vasp_name": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", }, "destination": { "user_id": TestSendTransaction.receiver_subaddress, "vasp_name": TestSendTransaction.receiver_address, "full_addr": "tlb1pztdjx2z8wp0q25jakqeklk0nxj2wmk2kg9whu8cxdewwy", }, "blockchain_tx": { "amount": TestSendTransaction.amount, "destination": TestSendTransaction.receiver_address, "expirationTime": "", "sequenceNumber": 10, "source": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "status": "pending", "version": 55, }, "is_internal": False, }
class SolaceTestCase(unittest.TestCase): """Subclass of the standard test case that creates and drops the database.""" def setUp(self): from solace import database, settings, templating from solace.application import application self.__old_settings = dict(settings.__dict__) settings.revert_to_default() settings.DATABASE_URI = 'sqlite:///' + TEST_DATABASE settings.TRACK_QUERIES = True settings.DATABASE_ECHO = False settings.MAIL_LOG_FILE = tempfile.NamedTemporaryFile() database.refresh_engine() database.init() self.client = Client(application, TestResponse) self.is_logged_in = False def get_session(self): from solace import settings for cookie in self.client.cookie_jar: if cookie.name == settings.COOKIE_NAME: value = unquote_header_value(cookie.value) return SecureCookie.unserialize(value, settings.SECRET_KEY) def get_exchange_token(self): return loads(self.client.get('/_request_exchange_token').data)['token'] def get_mails(self): from solace import settings pos = settings.MAIL_LOG_FILE.tell() settings.MAIL_LOG_FILE.seek(0) mails = settings.MAIL_LOG_FILE.read().split('\n%s\n\n' % ('-' * 79)) settings.MAIL_LOG_FILE.seek(pos) return [message_from_string(x) for x in mails if x] def normalize_local_path(self, path): if path in ('', '.'): path = path elif path.startswith(BASE_URL): path = path[len(BASE_URL) - 1:] return path def submit_form(self, path, data, follow_redirects=False): response = self.client.get(path) try: tree = html.fromstring(response.data) form = tree.xpath('//form')[0] #was: form = response.html.xpath('//form')[0] except IndexError: raise RuntimeError('no form on page') csrf_token = form.xpath('//input[@name="_csrf_token"]')[0] data['_csrf_token'] = csrf_token.attrib['value'] action = self.normalize_local_path(form.attrib['action']) return self.client.post(action, method=form.attrib['method'].upper(), data=data, follow_redirects=follow_redirects) def login(self, username, password): try: return self.submit_form('/login', { 'username': username, 'password': password }) finally: self.is_logged_in = True def logout(self): self.is_logged_in = False return self.client.get('/logout?_xt=%s' % self.get_exchange_token()) def tearDown(self): from solace import database, settings database.refresh_engine() try: os.remove(TEST_DATABASE) except OSError: pass settings.__dict__.clear() settings.__dict__.update(self.__old_settings) del self.is_logged_in
def test_multipart(): """Tests multipart parsing against data collected from webbrowsers""" resources = join(dirname(__file__), "multipart") client = Client(form_data_consumer, Response) repository = [ ( "firefox3-2png1txt", "---------------------------186454651713519341951581030105", [ (u"anchor.png", "file1", "image/png", "file1.png"), (u"application_edit.png", "file2", "image/png", "file2.png"), ], u"example text", ), ( "firefox3-2pnglongtext", "---------------------------14904044739787191031754711748", [(u"accept.png", "file1", "image/png", "file1.png"), (u"add.png", "file2", "image/png", "file2.png")], u"--long text\r\n--with boundary\r\n--lookalikes--", ), ( "opera8-2png1txt", "----------zEO9jQKmLc2Cq88c23Dx19", [ (u"arrow_branch.png", "file1", "image/png", "file1.png"), (u"award_star_bronze_1.png", "file2", "image/png", "file2.png"), ], u"blafasel öäü", ), ( "webkit3-2png1txt", "----WebKitFormBoundaryjdSFhcARk8fyGNy6", [(u"gtk-apply.png", "file1", "image/png", "file1.png"), (u"gtk-no.png", "file2", "image/png", "file2.png")], u"this is another text with ümläüts", ), ( "ie6-2png1txt", "---------------------------7d91b03a20128", [(u"file1.png", "file1", "image/x-png", "file1.png"), (u"file2.png", "file2", "image/x-png", "file2.png")], u"ie6 sucks :-/", ), ] for name, boundary, files, text in repository: folder = join(resources, name) data = get_contents(join(folder, "request.txt")) for filename, field, content_type, fsname in files: response = client.post( "/?object=" + field, data=data, content_type='multipart/form-data; boundary="%s"' % boundary, content_length=len(data), ) lines = response.data.split("\n", 3) assert lines[0] == repr(filename) assert lines[1] == repr(field) assert lines[2] == repr(content_type) assert lines[3] == get_contents(join(folder, fsname)) response = client.post( "/?object=text", data=data, content_type='multipart/form-data; boundary="%s"' % boundary, content_length=len(data), ) assert response.data == repr(text)
class SolaceTestCase(unittest.TestCase): """Subclass of the standard test case that creates and drops the database.""" def setUp(self): from solace import database, settings, templating from solace.application import application self.__old_settings = dict(settings.__dict__) settings.revert_to_default() settings.DATABASE_URI = 'sqlite:///' + TEST_DATABASE settings.TRACK_QUERIES = True settings.DATABASE_ECHO = False settings.MAIL_LOG_FILE = tempfile.NamedTemporaryFile() database.refresh_engine() database.init() self.client = Client(application, TestResponse) self.is_logged_in = False def get_session(self): from solace import settings for cookie in self.client.cookie_jar: if cookie.name == settings.COOKIE_NAME: value = unquote_header_value(cookie.value) return SecureCookie.unserialize(value, settings.SECRET_KEY) def get_exchange_token(self): return loads(self.client.get('/_request_exchange_token').data)['token'] def get_mails(self): from solace import settings pos = settings.MAIL_LOG_FILE.tell() settings.MAIL_LOG_FILE.seek(0) mails = settings.MAIL_LOG_FILE.read().split('\n%s\n\n' % ('-' * 79)) settings.MAIL_LOG_FILE.seek(pos) return [message_from_string(x) for x in mails if x] def normalize_local_path(self, path): if path in ('', '.'): path = path elif path.startswith(BASE_URL): path = path[len(BASE_URL) - 1:] return path def submit_form(self, path, data, follow_redirects=False): response = self.client.get(path) try: form = html_xpath(response.html, '//html:form')[0] except IndexError: raise RuntimeError('no form on page') csrf_token = html_xpath(form, '//html:input[@name="_csrf_token"]')[0] data['_csrf_token'] = csrf_token.attrib['value'] action = self.normalize_local_path(form.attrib['action']) return self.client.post(action, method=form.attrib['method'].upper(), data=data, follow_redirects=follow_redirects) def login(self, username, password): try: return self.submit_form('/login', { 'username': username, 'password': password }) finally: self.is_logged_in = True def logout(self): self.is_logged_in = False return self.client.get('/logout?_xt=%s' % self.get_exchange_token()) def tearDown(self): from solace import database, settings database.refresh_engine() try: os.remove(TEST_DATABASE) except OSError: pass settings.__dict__.clear() settings.__dict__.update(self.__old_settings) del self.is_logged_in
class WebTestCase(unittest.TestCase): def setUp(self): self.test_doc_path = mkdtemp() self.doc = open_document(path.join(self.test_doc_path, 'test_doc.db')) self.doc.create_note({'desc': 'note 1'}) self.doc.create_note({'desc': 'note 2'}) self.app = server.CorkApp(self.doc) self.client = Client(self.app, BaseResponse) def tearDown(self): rmtree(self.test_doc_path) def failUnlessJsonResponse(self, resp, json_data): self.failUnlessEqual(resp.status_code, 200) self.failUnlessEqual(resp.headers['Content-Type'], 'application/json') self.failUnlessEqual(json.loads(resp.data), json_data) def test_notes_listing(self): self.failUnlessJsonResponse(self.client.get('/notes'), [0, 1, 2]) def test_get_note(self): self.failUnlessJsonResponse(self.client.get('/notes/0'), { 'props': {'desc': 'ROOT'}, 'children': [1, 2], }) def test_change_note(self): test_props = {'desc': 'new content here', 'a': 'b'} resp = self.client.post('/notes/1', data={'props': json.dumps(test_props)}) self.doc.abort() # checking if transaction was committed self.failUnlessEqual(dict(self.doc.notes[1]), test_props) self.failUnlessJsonResponse(resp, {'props': test_props, 'children': []}) def test_create_note(self): resp = self.client.post('/notes', data={ 'parent_id': 1, 'props': json.dumps({'f': 'g'})}) self.failUnlessJsonResponse(resp, 3) self.doc.abort() # checking if transaction was committed self.failUnlessEqual(len(self.doc.notes), 4) self.failUnlessEqual(dict(self.doc.notes[3]), {'f': 'g'}) self.failUnlessEqual(list(self.doc.notes[1].children_ids()), [3]) self.failUnlessEqual(list(self.doc.notes[0].children_ids()), [1, 2]) def test_set_parent(self): resp = self.client.post('/notes/2/parent', data={'parent_id': 1}) self.failUnlessEqual(resp.status_code, 200) self.doc.abort() # checking if transaction was committed self.failUnlessEqual(list(self.doc.notes[1].children_ids()), [2]) self.failUnlessEqual(list(self.doc.notes[0].children_ids()), [1]) def test_remove_note(self): self.client.post('/notes/2/parent', data={'parent_id': 1}) self.failUnless(1 in self.doc.notes) self.failUnless(1 in list(self.doc.notes[0].children_ids())) self.failUnless(2 in self.doc.notes) self.failUnless(2 in list(self.doc.notes[1].children_ids())) resp = self.client.delete('/notes/1') self.failUnlessJsonResponse(resp, 'ok') self.failIf(1 in self.doc.notes) self.failIf(1 in list(self.doc.notes[0].children_ids())) self.failIf(2 in self.doc.notes) def test_custom_html(self): gsm = component.getGlobalSiteManager() def customViewAdapter(note): if note.id == test_note_id: return CustomView(note) gsm.registerSubscriptionAdapter(customViewAdapter, required=[INote], provided=INoteView) test_note_id = self.doc.create_note({'a': 'b'}).id self.failUnlessJsonResponse(self.client.get('/notes/%d' % test_note_id), { 'props': {'a': 'b'}, 'children': [], 'html': '<em>hello custom!</em>', }) gsm.unregisterSubscriptionAdapter(customViewAdapter, required=[INote], provided=INoteView) def test_ajax(self): gsm = component.getGlobalSiteManager() def customViewAdapter(note): if note.id == test_note_id: return CustomView(note) gsm.registerSubscriptionAdapter(customViewAdapter, required=[INote], provided=INoteView) test_note_id = self.doc.create_note({}).id resp = self.client.post('/notes/3/ajax', data={'args': json.dumps({'token': 'asdf'})}) self.failUnlessJsonResponse(resp, '-asdf-') gsm.unregisterSubscriptionAdapter(customViewAdapter, required=[INote], provided=INoteView)
class TestViews(unittest.TestCase): def setUp(self): self.app = make_wsgi('Testruns') self.client = Client(self.app, BaseResponse) def tearDown(self): self.client = None self.app = None def test_responding_view_base(self): r = self.client.get('tests/rvb') self.assertEqual(r.status, '200 OK') self.assertEqual(r.data, b'Hello World!') def test_responding_view_base_with_snippet(self): r = self.client.get('tests/rvbwsnip') self.assertEqual(r.status, '200 OK') self.assertEqual(r.data, b'Hello World!') def test_get(self): r = self.client.get('tests/get') self.assertEqual(r.status, '200 OK') self.assertEqual(r.data, b'Hello World!') def test_post(self): r = self.client.post('tests/post') self.assertEqual(r.status, '200 OK') self.assertEqual(r.data, b'Hello World!') def test_404_noroute(self): r = self.client.get('nothere') self.assertEqual(r.status, '404 NOT FOUND') self.assertTrue(b'Not Found' in r.data) self.assertTrue( b'If you entered the URL manually please check your spelling ' b'and try again.' in r.data) def test_nomodule(self): try: self.client.get('tests/badmod') self.fail( 'should have got ProgrammingError since URL exists but module does not' ) except HierarchyImportError as e: assert 'An object for View endpoint "fatfinger:NotExistant" was not found' == str( e), e def test_noview(self): try: self.client.get('tests/noview') self.fail( 'should have got ProgrammingError since URL exists but view does not' ) except HierarchyImportError as e: assert 'An object for View endpoint "tests:NotExistant" was not found' == str( e), e def test_prep(self): r = self.client.get('tests/prep') self.assertEqual(r.status, '200 OK') self.assertEqual(r.data, b'Hello World!') def test_hideexception(self): settings.exception_handling = ['handle'] r = self.client.get('tests/raiseexc') self.assertEqual(r.status, '500 INTERNAL SERVER ERROR') def test_2gets(self): r = self.client.get('tests/get') self.assertEqual(r.status, '200 OK') self.assertEqual(r.data, b'Hello World!') r = self.client.get('tests/get') self.assertEqual(r.status, '200 OK') self.assertEqual(r.data, b'Hello World!') def test_forward(self): r = self.client.get('tests/doforward') self.assertEqual(r.status, '200 OK') self.assertEqual(r.data, b'forward to me') def test_text(self): r = self.client.get('tests/text') self.assertEqual(r.status, '200 OK') self.assertEqual(r.data, b'Hello World!') self.assertEqual(r.headers['Content-Type'], 'text/plain; charset=utf-8') def test_textwsnip(self): r = self.client.get('tests/textwsnip') self.assertEqual(r.status, '200 OK') self.assertEqual(r.data, b'Hello World!') self.assertEqual(r.headers['Content-Type'], 'text/plain; charset=utf-8') def test_textwsnip2(self): r = self.client.get('tests/textwsnip2') self.assertEqual(r.status, '200 OK') self.assertEqual(r.data, b'Hello World!') self.assertEqual(r.headers['Content-Type'], 'text/plain; charset=utf-8') def test_html(self): r = self.client.get('tests/html') self.assertEqual(r.status, '200 OK') self.assertEqual(r.data, b'Hello World!') self.assertEqual(r.headers['Content-Type'], 'text/html; charset=utf-8') def test_redirect(self): r = self.client.get('tests/redirect') self.assertEqual(r.status_code, 302) self.assertEqual(r.headers['Location'], 'http://localhost/some/other/page') def test_permredirect(self): r = self.client.get('tests/permredirect') self.assertEqual(r.status_code, 301) self.assertEqual(r.headers['Location'], 'http://localhost/some/other/page') def test_custredirect(self): r = self.client.get('tests/custredirect') self.assertEqual(r.status_code, 303) self.assertEqual(r.headers['Location'], 'http://localhost/some/other/page') def test_heraise(self): r = self.client.get('tests/heraise') self.assertEqual(r.status_code, 503) assert b'server is temporarily unable' in r.data def test_errordoc(self): settings.error_docs[503] = 'tests:Rvb' r = self.client.get('tests/heraise') self.assertEqual(r.status_code, 503) self.assertEqual(r.status, '503 SERVICE UNAVAILABLE') self.assertEqual(r.data, b'Hello World!') def test_errordocexc(self): settings.error_docs[503] = 'tests:RaiseExc' try: r = self.client.get('tests/heraise') except ValueError as e: self.assertTrue('exception for testing' in str(e)) else: self.fail( 'should have gotten an exception b/c our error handler raised one' ) # now turn exception handling on and we should see a generic 500 # response since the document handler raised an exception settings.exception_handling = ['handle'] r = self.client.get('tests/heraise') self.assertEqual(r.status_code, 500) assert b'Internal Server Error' in r.data def test_forwardloop(self): try: self.client.get('tests/forwardloop') except ProgrammingError as e: self.assertTrue('forward loop detected:' in str(e)) else: self.fail('excpected exception for a forward loop') def test_urlargs(self): r = self.client.get('tests/urlargs') self.assertEqual(r.status, '200 OK') self.assertEqual(r.data, b'Hello World!') r = self.client.get('tests/urlargs/fred') self.assertEqual(r.status, '200 OK') self.assertEqual(r.data, b'Hello fred!') r = self.client.get('tests/urlargs/10') self.assertEqual(r.status, '200 OK') self.assertEqual(r.data, b'Give me a name!') def test_getargs(self): r = self.client.get('tests/getargs') self.assertEqual(r.status, '200 OK') self.assertEqual(r.data, b'Hello World!') r = self.client.get('tests/getargs?towho=fred&greeting=Hi&extra=bar') self.assertEqual(r.status, '200 OK') self.assertEqual(r.data, b'Hello fred!') def test_getargs2(self): r = self.client.get('tests/getargs2') self.assertEqual(r.status, '200 OK') self.assertEqual(r.data, b'Hello World!') r = self.client.get('tests/getargs2?towho=fred') self.assertEqual(r.status, '200 OK') self.assertEqual(r.data, b'Hello fred!') r = self.client.get('tests/getargs2?num=10') self.assertEqual(r.status, '200 OK') self.assertEqual(r.data, b'Hello World, 10!') r = self.client.get('tests/getargs2?num=ten') self.assertEqual(r.status, '200 OK') self.assertEqual(r.data, b'Hello World!') def test_getargs3(self): r = self.client.get('tests/getargs3?num=ten&num2=ten') self.assertEqual(r.status_code, 400) # we are no longer going to manipulate the response object to include # user messages self.assertTrue(b'integer' not in r.data) # If you want user messages included in an error response # you need to use an error document that will include them, like so: settings.error_docs[400] = 'tests:UserMessages' r = self.client.get('tests/getargs3?num=ten&num2=ten') self.assertEqual(r.status_code, 400) assert b'(error) num2: num: must be an integer' in r.data, r.data assert b'(error) num: Please enter an integer value' in r.data, r.data def test_reqgetargs(self): settings.error_docs[400] = 'tests:UserMessages' r = self.client.get('/tests/reqgetargs?num=10&num2=10') self.assertEqual(r.status_code, 200) self.assertEqual(r.data, b'Hello World, 10 10 10!') r = self.client.get('/tests/reqgetargs?num2=ten') self.assertEqual(r.status_code, 400) self.assertTrue(b'(error) num: Please enter a value' in r.data, r.data) self.assertTrue( b'(error) num2: Please enter an integer value' in r.data) r = self.client.get('tests/reqgetargs?num1&num=2') self.assertEqual(r.status_code, 200) self.assertEqual(r.data, b'Hello World, 2 10 10!') def test_listgetargs(self): r = self.client.get('tests/listgetargs?nums=1&nums=2') self.assertEqual(r.status_code, 200) self.assertEqual(r.data, b'[1, 2]') r = self.client.get('tests/listgetargs?nums=ten&nums=2') self.assertEqual(r.status_code, 200) self.assertEqual(r.data, b'[2]') def test_customvalidator(self): r = self.client.get('tests/customvalidator?num=asek') self.assertEqual(r.status_code, 200) self.assertEqual(r.data, b'10') r = self.client.get('tests/customvalidator') self.assertEqual(r.status_code, 200) self.assertEqual(r.data, b'10') r = self.client.get('tests/customvalidator?num=5') self.assertEqual(r.status_code, 200) self.assertEqual(r.data, b'5') def test_badvalidator(self): try: self.client.get('tests/badvalidator') except TypeError as e: self.assertEqual( 'processor must be a Formencode validator or a callable', str(e)) else: self.fail('excpected exception for bad validator') def test_static(self): r = self.client.get('/static/app/helloworld.html') self.assertEqual(r.status_code, 200) self.assertEqual(r.get_data(), b'Hello World!') r = self.client.get('/static/app/helloworld2.html') self.assertEqual(r.status_code, 200) self.assertEqual(r.get_data().strip(), b'Hellow blazewebtestapp2!') def test_app2(self): # app2's test module won't have its settings imported # b/c app1's settings module is blocking it. Therefore, the # route doesn't get added and we get a 404 r = self.client.get('tests/rvbapp2') self.assertEqual(r.status_code, 404) def test_appfallback(self): r = self.client.get('tests/appfallback') self.assertEqual(r.status_code, 200) self.assertEqual(r.data, b'Hello app2!') def test_htmltemplatefilearg(self): r = self.client.get('tests/htmltemplatefilearg') self.assertEqual(r.status_code, 200) self.assertEqual(r.data, b'Hello File Arg!') def test_htmltemplateinheritance(self): """ test inheritance at the module level from a supporting app """ r = self.client.get('tests/templateinheritance') self.assertEqual(r.status_code, 200) self.assertEqual(r.data, b'Hello Template Inheritance!') def test_parenttemplate(self): """ test extending a template from the parent application """ r = self.client.get('tests/parenttemplate') self.assertEqual(r.status_code, 200) self.assertEqual(r.data, b'Hello Parent Template!') def test_parenttemplateinheritance(self): """ test extending a template from a supporting app""" r = self.client.get('tests/parenttemplateinheritance') self.assertEqual(r.status_code, 200) self.assertEqual(r.data, b'Hello App2 Parent Template!') def test_modlevelpriority(self): """ make sure that when inheriting that a module level template in a supporting app takes precidence over a template level app in the main module """ r = self.client.get('tests/modlevelpriority') self.assertEqual(r.status_code, 200) self.assertEqual(r.data, b'Hello mod level priority!') def test_disabled_module(self): """ a disabled module should not be processed and therefore we should get a 404""" r = self.client.get('/disabled/notthere') self.assertEqual(r.status, '404 NOT FOUND') self.assertTrue(b'Not Found' in r.data) self.assertTrue( b'If you entered the URL manually please check your spelling and ' b'try again.' in r.data) def test_render_endpoint(self): # app level endpoint r = self.client.get('/tests/tchooser/endpoint') assert b'app level' in r.data, r.data # render content r = self.client.get('/tests/tchooser/content') assert b'Hello World!' in r.data, r.data def test_render_template_directly(self): # app level endpoint r = self.client.get('/tests/text.txt/&fred') assert r.headers[ 'Content-Type'] == 'text/plain; charset=utf-8', r.headers assert b'Hello &fred!' in r.data, r.data def test_jsonify_exception(self): # we have exception handling turned off during testing, so we should # get the exception passed all the way up try: r = self.client.get('/jsonify-exception') assert False except NameError as e: if 'foo' not in str(e): raise try: settings.exception_handling = ['handle'] r = self.client.get('/jsonify-exception') assert r.status_code == 500, r.status_code data = jsonmod.loads(r.data.decode()) assert data['error'] == 1, data assert data['data'] is None, data finally: settings.exception_handling = None