def test_store_submission_and_download_archive(dirs: DirectoryStructure, client: Client): job_id = "job-id-xxxx" store_response = client.post("/submissions/" + job_id, data={ "foo.txt": (BytesIO(b"foocontent"), "foo.txt"), "bar/baz/bah.txt": (BytesIO(b"bahcontent"), "bar/baz/bah.txt") }) assert store_response.status_code == 200 assert store_response.json == { "archive_path": "/submission_archives/job-id-xxxx.zip", "result_path": "/results/job-id-xxxx.zip" } job_dir = os.path.join(dirs.submission_dir, job_id) assert os.path.isdir(job_dir) assert os.path.exists(os.path.join(job_dir, "foo.txt")) assert os.path.exists(os.path.join(job_dir, "bar", "baz", "bah.txt")) fetch_response = client.get(store_response.json["archive_path"]) assert fetch_response.status_code == 200 with ZipFile(BytesIO(fetch_response.data)) as archive: assert archive.namelist() == [ 'job-id-xxxx/', 'job-id-xxxx/bar/', 'job-id-xxxx/foo.txt', 'job-id-xxxx/bar/baz/', 'job-id-xxxx/bar/baz/bah.txt' ] assert os.path.exists(os.path.join(dirs.archive_dir, job_id + ".zip"))
class TestXyvio(unittest.TestCase): def setUp(self): self.client = Client(app, BaseResponse) def test_base36_encode(self): self.assertEqual('1', base36_encode(1)) self.assertEqual('a', base36_encode(10)) self.assertEqual('10', base36_encode(36)) self.assertEqual('20', base36_encode(72)) def test_new_url(self): resp = self.client.get('/') self.assertEqual(200, resp.status_code) def test_follow_short_link(self): resp = self.client.get('/some_other_random') self.assertEqual(404, resp.status_code) resp = self.client.get('/1') self.assertEqual(302, resp.status_code) def test_short_link_details(self): resp = self.client.get('/+') self.assertEqual(404, resp.status_code) resp = self.client.get('/1+') self.assertEqual(200, resp.status_code) def test_is_valid_url(self): pass def tearDown(self): pass
def flask_url_fetcher(url): redirect_chain = set() while 1: result = dispatcher(url) if result is None: return next_fetcher(url) app, base_url, path = result client = Client(app, response_wrapper=Response) if isinstance(path, unicode): # TODO: double-check this. Apparently Werzeug %-unquotes bytes # but not Unicode URLs. (IRI vs. URI or something.) path = path.encode('utf8') response = client.get(path, base_url=base_url) if response.status_code == 200: return dict( string=response.data, mime_type=response.mimetype, encoding=response.charset, redirected_url=url) # The test client can follow redirects, but do it ourselves # to get access to the redirected URL. elif response.status_code in (301, 302, 303, 305, 307): redirect_chain.add(url) url = response.location if url in redirect_chain: raise ClientRedirectError('loop detected') else: raise ValueError('Flask-WeasyPrint got HTTP status %s for %s%s' % (response.status, base_url, path))
def test_smart_static_css(): """ Contents of CSS files served by SmartStatic should have url(), @import and other internal links updated to include hashes. """ routes = ( ('/<path:filepath>', 'test', SmartStatic(directory=static_folder)), ) css_path = os.path.join(static_folder, 'css', 'test.css') with open(css_path, 'rb') as f: css_hash = get_hash(f) app = spa.App(routes) c = Client(app, spa.Response) resp = c.get('/css/test.%s.css' % css_hash) assert resp.status_code == 200 # css-relative url assert b'url("blah.c9a8f43433e4.css")' in resp.data # absolute path url assert b'url("/css/blah2.54197c389773.css")' in resp.data # url with IE compatibility hack assert b'url("/font/lato-regular-webfont.97add36de4b3.eot?#iefix")' in resp.data # url with fragment but no query string assert b'url("/font/lato-regular-webfont.01ee9ec2a839.svg#svgFontName")' in resp.data # css-relative url with query string assert b'url("../img/background.fb32250cea28.png?foo=bar")' in resp.data
def test_responder(self): def foo(environ, start_response): return BaseResponse('Test') client = Client(wsgi.responder(foo), BaseResponse) response = client.get('/') assert response.status_code == 200 assert response.data == 'Test'
class TestModeratedComments(unittest.TestCase): def setUp(self): fd, self.path = tempfile.mkstemp() conf = core.Config.load(None) conf.set("general", "dbpath", self.path) conf.set("moderation", "enabled", "true") conf.set("guard", "enabled", "off") class App(Isso, core.Mixin): pass self.app = App(conf) self.app.wsgi_app = FakeIP(self.app.wsgi_app) self.client = Client(self.app, Response) def tearDown(self): os.unlink(self.path) def testAddComment(self): rv = self.client.post('/new?uri=test', data=json.dumps({"text": "..."})) assert rv.status_code == 202 assert self.client.get('/id/1').status_code == 200 assert self.client.get('/?uri=test').status_code == 404 self.app.db.comments.activate(1) assert self.client.get('/?uri=test').status_code == 200
def test_append_slash_redirect(): def app(env, sr): return utils.append_slash_redirect(env)(env, sr) client = Client(app, BaseResponse) response = client.get('foo', base_url='http://example.org/app') assert response.status_code == 301 assert response.headers['Location'] == 'http://example.org/app/foo/'
class ServerTestCase(BaseTestCase): def setUp(self): super(ServerTestCase, self).setUp() self.app = FlaskApp(__name__) self.app.route('/', methods=['POST', 'GET'])(hello_world) ctx = self.app.test_request_context() ctx.push() self.addCleanup(ctx.pop) self.client = Client(self.app, response_wrapper=Response) def test_trace_generated(self): response = self.client.get('/') self.assertEqual(response.status_code, 200) self.assertIn(TRACE_HEADER, response.headers.keys()) def test_trace_is_unique_per_request(self): response = self.client.get('/') value1 = response.headers[TRACE_HEADER] response = self.client.get('/') value2 = response.headers[TRACE_HEADER] self.assertNotEqual(value1, value2) def test_trace_prefixing(self): response = self.client.get('/') value = response.headers[TRACE_HEADER] self.assertTrue(value.startswith(TRACE_PREFIX), value) def test_multiple_requests_appended(self): headers = {TRACE_HEADER: 'Trace123'} response = self.client.get('/', headers=headers) value = response.headers[TRACE_HEADER] self.assertTrue(value.startswith('Trace123,' + TRACE_PREFIX))
def test_json_render(render_json=None): if render_json is None: render_json = JSONRender(dev_mode=True) app = Application([('/', hello_world_ctx, render_json), ('/<name>/', hello_world_ctx, render_json), ('/beta/<name>/', complex_context, render_json)]) yield ok_, callable(app.routes[0]._execute) yield ok_, callable(app.routes[0]._render) c = Client(app, BaseResponse) resp = c.get('/') yield eq_, resp.status_code, 200 resp_data = json.loads(resp.data) yield eq_, resp_data['name'], 'world' resp = c.get('/Kurt/') yield eq_, resp.status_code, 200 resp_data = json.loads(resp.data) yield eq_, resp_data['name'], 'Kurt' resp = c.get('/beta/Rajkumar/') yield eq_, resp.status_code, 200 resp_data = json.loads(resp.data) yield eq_, resp_data['name'], 'Rajkumar' yield ok_, resp_data['date'] yield ok_, len(resp_data) > 4
def test_nonbreaking_exc(): app = Application([('/', lambda: NotFound(is_breaking=False)), ('/', lambda: 'so hot in here', render_basic)]) client = Client(app, BaseResponse) resp = client.get('/') yield eq_, resp.status_code, 200 yield eq_, resp.data, 'so hot in here'
def test_simple(self): app = CORSMiddleware(hello_world, origin=origin([ "https://example.tld/", "http://example.tld/", ]), allowed=("Foo", "Bar"), exposed=("Spam", )) client = Client(app, Response) rv = client.get("/", headers={"Origin": "https://example.tld"}) self.assertEqual( rv.headers["Access-Control-Allow-Origin"], "https://example.tld") self.assertEqual( rv.headers["Access-Control-Allow-Credentials"], "true") self.assertEqual( rv.headers["Access-Control-Allow-Methods"], "HEAD, GET, POST, PUT, DELETE") self.assertEqual( rv.headers["Access-Control-Allow-Headers"], "Foo, Bar") self.assertEqual(rv.headers["Access-Control-Expose-Headers"], "Spam") a = client.get("/", headers={"Origin": "http://example.tld"}) self.assertEqual( a.headers["Access-Control-Allow-Origin"], "http://example.tld") b = client.get("/", headers={"Origin": "http://example.tld"}) self.assertEqual( b.headers["Access-Control-Allow-Origin"], "http://example.tld") c = client.get("/", headers={"Origin": "http://foo.other"}) self.assertEqual( c.headers["Access-Control-Allow-Origin"], "https://example.tld")
def test_auth_unique_uri(self): from rest_api_framework.pagination import Pagination ressources = [{"id": "azerty"}] authentication = ApiKeyAuthentication( PythonListDataStore(ressources, ApiModel) ) class ApiAppAuth(ApiApp): controller = { "list_verbs": ["GET", "POST"], "unique_verbs": ["GET", "PUT", "DElETE"], "options": {"pagination": Pagination(20), "authentication": authentication, "authorization": ApiKeyAuthorization } } client = Client( WSGIDispatcher([ApiAppAuth]), response_wrapper=BaseResponse) resp = client.get("/address/1/?apikey=azerty") self.assertEqual(resp.status_code, 200) resp = client.get("/address/1/?apikey=querty") self.assertEqual(resp.status_code, 401)
def test_responder(self): def foo(environ, start_response): return BaseResponse(b'Test') client = Client(wsgi.responder(foo), BaseResponse) response = client.get('/') self.assert_equal(response.status_code, 200) self.assert_equal(response.data, b'Test')
def test_broken_error_render(): rt = Route('/<number:int>', odd_endpoint, render_basic) app = Application([rt], render_error=render_error_broken) cl = Client(app, BaseResponse) err_resp = cl.get('/2') yield eq_, err_resp.status_code, 500 yield ok_, 'not in my house' in err_resp.data
def test_post_auth(self): """ Test a read only api. GET should be ok, POST and PUT should not """ from rest_api_framework.pagination import Pagination ressources = [{"id": "azerty"}] authentication = ApiKeyAuthentication( PythonListDataStore(ressources, ApiModel) ) class ApiAppAuth(ApiApp): controller = { "list_verbs": ["GET"], "unique_verbs": ["GET"], "options": {"pagination": Pagination(20), "authentication": authentication, "authorization": ApiKeyAuthorization } } client = Client( WSGIDispatcher([ApiAppAuth]), response_wrapper=BaseResponse) resp = client.get("/address/1/?apikey=azerty") self.assertEqual(resp.status_code, 200) resp = client.post("/address/?apikey=azerty", data=json.dumps({'name': 'bob', 'age': 34})) self.assertEqual(resp.status_code, 405)
def test_mount_context(): app = morepath.App('app') mounted = morepath.App('mounted') class MountedRoot(object): def __init__(self, mount_id): self.mount_id = mount_id def root_default(request, model): return "The root for mount id: %s" % model.mount_id def get_context(id): return { 'mount_id': id } c = setup() c.configurable(app) c.configurable(mounted) c.action(app.mount(path='{id}', app=mounted), get_context) c.action(mounted.root(), MountedRoot) c.action(mounted.view(model=MountedRoot), root_default) c.commit() c = Client(app, Response) response = c.get('/foo') assert response.data == 'The root for mount id: foo' response = c.get('/bar') assert response.data == 'The root for mount id: bar'
def test_mount_parent_link(): app = morepath.App('app') class Model(object): def __init__(self, id): self.id = id mounted = morepath.App('mounted') class MountedRoot(object): def __init__(self, mount_id): self.mount_id = mount_id def root_default(request, model): return request.link(Model('one'), mounted=request.mounted().parent()) def get_context(id): return { 'mount_id': id } c = setup() c.configurable(app) c.configurable(mounted) c.action(app.model(path='models/{id}', variables=lambda m: {'id': m.id}), Model) c.action(app.mount(path='{id}', app=mounted), get_context) c.action(mounted.root(), MountedRoot) c.action(mounted.view(model=MountedRoot), root_default) c.commit() c = Client(app, Response) response = c.get('/foo') assert response.data == 'models/one'
def test_json_directive(): app = morepath.App() class Model(object): def __init__(self, id): self.id = id def default(request, model): return "The view for model: %s" % model.id def json(request, model): return {'id': model.id} c = setup() c.configurable(app) c.action(app.model(path='{id}', variables=lambda model: {'id': model.id}), Model) c.action(app.json(model=Model), json) c.commit() c = Client(app, Response) response = c.get('/foo') assert response.data == '{"id": "foo"}'
def test_mount_empty_context(): app = morepath.App('app') mounted = morepath.App('mounted') class MountedRoot(object): pass def root_default(request, model): return "The root" def root_link(request, model): return request.link(model) def get_context(): pass c = setup() c.configurable(app) c.configurable(mounted) c.action(app.mount(path='{id}', app=mounted), get_context) c.action(mounted.root(), MountedRoot) c.action(mounted.view(model=MountedRoot), root_default) c.action(mounted.view(model=MountedRoot, name='link'), root_link) c.commit() c = Client(app, Response) response = c.get('/foo') assert response.data == 'The root' response = c.get('/foo/link') assert response.data == 'foo'
def test_link_to_unknown_model(): app = morepath.App() class Root(object): def __init__(self): self.value = 'ROOT' class Model(object): def __init__(self, id): self.id = id def root_link(request, model): try: return request.link(Model('foo')) except LinkError: return "Link error" c = setup() c.configurable(app) c.action(app.root(), Root) c.action(app.view(model=Root), root_link) c.commit() c = Client(app, Response) response = c.get('/') assert response.data == 'Link error'
def test_simple_root(): app = morepath.App() class Hello(object): pass hello = Hello() def hello_model(): return hello def hello_view(request, model): return 'hello' c = setup() c.configurable(app) c.action(app.root(model=Hello), hello_model) c.action(app.view(model=Hello), hello_view) c.commit() c = Client(app, Response) response = c.get('/') assert response.data == 'hello'
def test_base_request(): """Base request behavior""" client = Client(request_demo_app, RequestTestResponse) # get requests response = client.get('/?foo=bar&foo=hehe') assert response['args'] == MultiDict([('foo', 'bar'), ('foo', 'hehe')]) assert response['args_as_list'] == [('foo', ['bar', 'hehe'])] assert response['form'] == MultiDict() assert response['form_as_list'] == [] assert response['data'] == '' assert_environ(response['environ'], 'GET') # post requests with form data response = client.post('/?blub=blah', data='foo=blub+hehe&blah=42', content_type='application/x-www-form-urlencoded') assert response['args'] == MultiDict([('blub', 'blah')]) assert response['args_as_list'] == [('blub', ['blah'])] assert response['form'] == MultiDict([('foo', 'blub hehe'), ('blah', '42')]) assert response['data'] == '' # currently we do not guarantee that the values are ordered correctly # for post data. ## assert response['form_as_list'] == [('foo', ['blub hehe']), ('blah', ['42'])] assert_environ(response['environ'], 'POST') # post requests with json data json = '{"foo": "bar", "blub": "blah"}' response = client.post('/?a=b', data=json, content_type='application/json') assert response['data'] == json assert response['args'] == MultiDict([('a', 'b')]) assert response['form'] == MultiDict()
def test_resent_cookie(): """Test that the client resends cookies on subsequent requests """ c = Client(cookie_app) c.open() appiter, code, headers = c.open() assert ''.join(appiter) == 'test=test'
def test_get_list(self): client = Client(WSGIDispatcher([ApiApp]), response_wrapper=BaseResponse) resp = client.get("/address/") self.assertEqual(resp.status_code, 200) self.assertIsInstance(json.loads(resp.data)["object_list"], list)
def runtest(self): fs = self._prepareMockFs() url = self.spec.get('url') if url is None: raise Exception("Missing URL in test spec.") expected_status = self.spec.get('status', 200) expected_headers = self.spec.get('headers') expected_output = self.spec.get('out') expected_contains = self.spec.get('out_contains') from werkzeug.test import Client from werkzeug.wrappers import BaseResponse from piecrust.serving.server import Server with mock_fs_scope(fs): server = Server(fs.path('/kitchen')) test_app = self._TestApp(server) client = Client(test_app, BaseResponse) resp = client.get(url) assert expected_status == resp.status_code if expected_headers: for k, v in expected_headers.items(): assert v == resp.headers.get(k) actual = resp.data.decode('utf8').rstrip() if expected_output: assert expected_output.rstrip() == actual if expected_contains: assert expected_contains.rstrip() in actual
class TestJsonRestService(object): def setup(self): def _setup_json(app): app.add_url('/', '', view=_Service()) app = make_app(_setup_json, 'test_output') self.client = Client(app) def test_get(self): ai, st, h = self.client.open(method='GET') s = list(ai)[0] assert 'GET' in s def test_post(self): ai, st, h = self.client.open(method='POST') s = list(ai)[0] assert 'POST' in s def test_put(self): ai, st, h = self.client.open(method='PUT') s = list(ai)[0] assert 'PUT' in s def test_delete(self): ai, st, h = self.client.open(method='DELETE') s = list(ai)[0] assert 'DELETE' in s
def test_check_login(): from glashammer.bundles.auth import setup_auth, login from glashammer.bundles.sessions import get_session called = [] def check(u, p): called.append((u, p)) return u == p def view(req): login('a') return Response() def setup_app(app): app.add_setup(setup_auth) app.connect_event('password-check', check) app.add_url('/', 'a/b', view=view) app = make_app(setup_app, 'test_output') c = Client(app) c.open() session = get_session() token_key = get_app().conf['auth/token_key'] assert token_key in session assert session[token_key] == 'a'
def test_base_pagination_offset(self): client = Client(WSGIDispatcher([ApiApp]), response_wrapper=BaseResponse) resp = client.get("/address/?offset=2") self.assertEqual( json.loads(resp.data)["object_list"][0]['ressource_uri'], "/address/2/")
class TestJsonRest(object): def setup(self): app = load_app_from_path('examples/jsonrest/run.py') self.c = Client(app) def test_index(self): iter, status, headers = self.c.open() s = ''.join(iter) assert """ <a href="#" id="get_link">GET</a> <a href="#" id="post_link">POST</a> <a href="#" id="put_link">PUT</a> <a href="#" id="delete_link">DELETE</a> """.strip('\n') in s def test_get(self): iter, status, headers = self.c.open('/svc') d = loads(''.join(iter)) assert d['type'] == 'GET' def test_put(self): iter, status, headers = self.c.put('/svc') d = loads(''.join(iter)) assert d['type'] == 'PUT' def test_delete(self): iter, status, headers = self.c.delete('/svc') d = loads(''.join(iter)) assert d['type'] == 'DELETE' def test_post(self): iter, status, headers = self.c.post('/svc') d = loads(''.join(iter)) assert d['type'] == 'POST'
class WebsiteTest(TestCase): SQLALCHEMY_DATABASE_URI = 'sqlite://' TESTING = True def create_app(self): return application def setUp(self): db.create_all() self.client = Client(application, BaseResponse) def tearDown(self): db.session.remove() db.drop_all() def test_home_page_status_code(self): response = self.client.get('/') self.assertEqual(response.status_code, 200) def test_manage_page_status_code(self): response = self.client.get('/manage') self.assertEqual(response.status_code, 200)
class PlaylistAPITestCase(unittest.TestCase): @mock.patch("klangbecken.api.ExternalAuth", lambda app, *args, **kwargs: app) def setUp(self): from klangbecken.api import klangbecken_api from klangbecken.playlist import FileAddition, MetadataChange self.upload_analyzer = mock.Mock(return_value=[ FileAddition("testfile"), MetadataChange("testkey", "testvalue"), ]) self.update_analyzer = mock.Mock(return_value=["UpdateChange"]) self.processor = mock.MagicMock() with mock.patch("klangbecken.api.DEFAULT_UPLOAD_ANALYZERS", [self.upload_analyzer]), mock.patch( "klangbecken.api.DEFAULT_UPDATE_ANALYZERS", [self.update_analyzer]), mock.patch( "klangbecken.api.DEFAULT_PROCESSORS", [self.processor]): app = klangbecken_api( "secret", "data_dir", "player.sock", ) self.client = Client(app) @mock.patch("werkzeug.datastructures.FileStorage.save", lambda *args: None) @mock.patch("os.remove", lambda fname: None) def testUpload(self): from klangbecken.playlist import FileAddition, MetadataChange # Correct upload resp = self.client.post( "/playlist/music/", data={"file": (io.BytesIO(b"testcontent"), "test.mp3")}) self.assertEqual(resp.status_code, 200) data = json.loads(resp.data) fileId = list(data.keys())[0] self.assertEqual(fileId, str(uuid.UUID(fileId))) self.assertEqual( list(data.values())[0], { "testkey": "testvalue", "original_filename": "test.mp3", "uploader": "" }, ) self.update_analyzer.assert_not_called() self.upload_analyzer.assert_called_once() args = self.upload_analyzer.call_args[0] self.assertEqual(args[0], "music") self.assertEqual(args[1], fileId) self.assertEqual(args[2], "mp3") self.assertTrue(isinstance(args[3], str)) self.assertTrue(args[3].startswith("data_dir/upload/")) self.processor.assert_called_once_with( "data_dir", "music", fileId, "mp3", [ FileAddition("testfile"), MetadataChange("testkey", "testvalue"), MetadataChange("original_filename", "test.mp3"), MetadataChange("uploader", ""), ], ) self.upload_analyzer.reset_mock() self.processor.reset_mock() # Wrong attribute name resp = self.client.post( "/playlist/music/", data={"not-file": (io.BytesIO(b"testcontent"), "test.mp3")}, ) self.assertEqual(resp.status_code, 422) self.assertIn(b"No file attribute named", resp.data) self.update_analyzer.assert_not_called() self.upload_analyzer.assert_not_called() self.processor.assert_not_called() # File as normal text attribute resp = self.client.post("/playlist/music/", data={ "file": "testcontent", "filename": "test.mp3" }) self.assertEqual(resp.status_code, 422) self.assertIn(b"No file attribute named", resp.data) self.update_analyzer.assert_not_called() self.upload_analyzer.assert_not_called() self.processor.assert_not_called() def testUpdate(self): # Update weight correctly fileId = str(uuid.uuid4()) resp = self.client.put( "/playlist/music/" + fileId + ".mp3", data=json.dumps({"weight": 4}), content_type="text/json", ) self.assertEqual(resp.status_code, 200) self.update_analyzer.assert_called_once_with("music", fileId, "mp3", {"weight": 4}) self.upload_analyzer.assert_not_called() self.processor.assert_called_once_with("data_dir", "music", fileId, "mp3", ["UpdateChange"]) self.update_analyzer.reset_mock() self.processor.reset_mock() # Update artist and title correctly resp = self.client.put( "/playlist/music/" + fileId + ".mp3", data=json.dumps({ "artist": "A", "title": "B" }), content_type="text/json", ) self.assertEqual(resp.status_code, 200) self.update_analyzer.assert_called_once_with("music", fileId, "mp3", { "artist": "A", "title": "B" }) self.processor.assert_called_once_with("data_dir", "music", fileId, "mp3", ["UpdateChange"]) self.update_analyzer.reset_mock() self.processor.reset_mock() # Update with invalid json format resp = self.client.put( "/playlist/music/" + fileId + ".mp3", data='{ a: " }', content_type="text/json", ) self.assertEqual(resp.status_code, 415) self.assertIn(b"invalid JSON", resp.data) self.update_analyzer.assert_not_called() # Update with invalid unicode format resp = self.client.put("/playlist/music/" + fileId + ".mp3", data=b"\xFF", content_type="text/json") self.assertEqual(resp.status_code, 415) self.assertIn(b"invalid UTF-8 data", resp.data) self.update_analyzer.assert_not_called() def testDelete(self): from klangbecken.playlist import FileDeletion fileId = str(uuid.uuid4()) resp = self.client.delete("/playlist/music/" + fileId + ".mp3") self.assertEqual(resp.status_code, 200) self.update_analyzer.assert_not_called() self.upload_analyzer.assert_not_called() self.processor.assert_called_once_with("data_dir", "music", fileId, "mp3", [FileDeletion()]) self.upload_analyzer.reset_mock() self.processor.reset_mock()
class TestFileByDigestMiddleware(unittest.TestCase): def setUp(self): # We need to wrap the generator in a list because of a # shortcoming of future's bytes implementation. # Choose a size that is larger than FileCacher.CHUNK_SIZE. self.content = \ bytes([random.getrandbits(8) for _ in range(2 ** 14 + 1024)]) self.digest = bytes_digest(self.content) self.filename = "foobar.pdf" self.mimetype = "image/jpeg" self.file_cacher = Mock() self.file_cacher.get_file = Mock( side_effect=lambda digest: io.BytesIO(self.content)) self.file_cacher.get_size = Mock(return_value=len(self.content)) self.serve_file = True self.provide_filename = True self.wsgi_app = \ FileServerMiddleware(self.file_cacher,self.wrapped_wsgi_app) self.environ_builder = EnvironBuilder("/some/url") self.client = Client(self.wsgi_app, Response) @responder def wrapped_wsgi_app(self, environ, start_response): self.assertEqual(environ, self.environ) if self.serve_file: headers = {FileServerMiddleware.DIGEST_HEADER: self.digest} if self.provide_filename: headers[FileServerMiddleware.FILENAME_HEADER] = self.filename return Response(headers=headers, mimetype=self.mimetype) else: return Response(b"some other content", mimetype="text/plain") def request(self, headers=None): if headers is not None: for key, value in headers: self.environ_builder.headers.add(key, value) self.environ = self.environ_builder.get_environ() return self.client.open(self.environ) def test_success(self): response = self.request() self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, self.mimetype) self.assertEqual( response.headers.get("content-disposition"), "attachment; filename=%s" % quote_header_value(self.filename)) self.assertTupleEqual(response.get_etag(), (self.digest, False)) self.assertEqual(response.accept_ranges, "bytes") self.assertGreater(response.cache_control.max_age, 0) self.assertTrue(response.cache_control.private) self.assertFalse(response.cache_control.public) self.assertEqual(response.get_data(), self.content) self.file_cacher.get_file.assert_called_once_with(self.digest) def test_not_a_file(self): self.serve_file = False response = self.request() self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "text/plain") self.assertEqual(response.get_data(), b"some other content") def test_no_filename(self): self.provide_filename = False response = self.request() self.assertNotIn("content-disposition", response.headers) def test_not_found(self): self.file_cacher.get_file.side_effect = KeyError() response = self.request() self.assertEqual(response.status_code, 404) self.file_cacher.get_file.assert_called_once_with(self.digest) def test_tombstone(self): self.file_cacher.get_file.side_effect = TombstoneError() response = self.request() self.assertEqual(response.status_code, 503) self.file_cacher.get_file.assert_called_once_with(self.digest) def test_conditional_request(self): # Test an etag that matches. response = self.request(headers=[("If-None-Match", self.digest)]) self.assertEqual(response.status_code, 304) self.assertEqual(len(response.get_data()), 0) def test_conditional_request_no_match(self): # Test an etag that doesn't match. response = self.request(headers=[("If-None-Match", "not the etag")]) self.assertEqual(response.status_code, 200) self.assertEqual(response.get_data(), self.content) def test_range_request(self): # Test a range that is strictly included. response = self.request(headers=[("Range", "bytes=256-767")]) self.assertEqual(response.status_code, 206) self.assertEqual(response.content_range.units, "bytes") self.assertEqual(response.content_range.start, 256) self.assertEqual(response.content_range.stop, 768) self.assertEqual(response.content_range.length, 1024) self.assertEqual(response.get_data(), self.content[256:768]) def test_range_request_end_overflows(self): # Test a range that ends after the end of the file. response = self.request(headers=[("Range", "bytes=256-2047")]) self.assertEqual(response.status_code, 206) self.assertEqual(response.content_range.units, "bytes") self.assertEqual(response.content_range.start, 256) self.assertEqual(response.content_range.stop, 1024) self.assertEqual(response.content_range.length, 1024) self.assertEqual(response.get_data(), self.content[256:]) def test_range_request_start_overflows(self): # Test a range that starts after the end of the file. response = self.request(headers=[("Range", "bytes=1536-")]) self.assertEqual(response.status_code, 416)
def report_download(self, data, token): """This function is used by 'qwebactionmanager.js' in order to trigger the download of a pdf/controller report. :param data: a javascript array JSON.stringified containg report internal url ([0]) and type [1] :returns: Response with a filetoken cookie and an attachment header """ requestcontent = json.loads(data) url, type = requestcontent[0], requestcontent[1] try: if type == 'qweb-pdf': reportname = url.split('/report/pdf/')[1].split('?')[0] docids = None if '/' in reportname: reportname, docids = reportname.split('/') if docids: # Generic report: response = self.report_routes(reportname, docids=docids, converter='pdf') else: # Particular report: data = url_decode( url.split('?') [1]).items() # decoding the args represented in JSON response = self.report_routes(reportname, converter='pdf', **dict(data)) report = request.env['report']._get_report_from_name( reportname) filename = "%s.%s" % (report.name, "pdf") if docids: ids = [int(x) for x in docids.split(",")] obj = request.env[report.model].browse(ids) if report.print_report_name and not len(obj) > 1: filename = safe_eval(report.print_report_name, { 'object': obj, 'time': time }) response.headers.add('Content-Disposition', content_disposition(filename)) response.set_cookie('fileToken', token) return response elif type == 'controller': reqheaders = Headers(request.httprequest.headers) response = Client(request.httprequest.app, BaseResponse).get(url, headers=reqheaders, follow_redirects=True) response.set_cookie('fileToken', token) return response else: return except Exception, e: se = _serialize_exception(e) error = {'code': 200, 'message': "Odoo Server Error", 'data': se} return request.make_response(html_escape(json.dumps(error)))
def test_mockview(): app, admin = setup() view = MockModelView(Model) admin.add_view(view) eq_(view.model, Model) eq_(view.name, 'Model') eq_(view.endpoint, 'model') # Verify scaffolding eq_(view._sortable_columns, ['col1', 'col2', 'col3']) eq_(view._create_form_class, Form) eq_(view._edit_form_class, Form) eq_(view._search_supported, False) eq_(view._filters, None) client = app.test_client() # Make model view requests rv = client.get('/admin/model/') eq_(rv.status_code, 200) # Test model creation view rv = client.get('/admin/model/new/') eq_(rv.status_code, 200) rv = client.post('/admin/model/new/', data=dict(col1='test1', col2='test2', col3='test3')) eq_(rv.status_code, 302) eq_(len(view.created_models), 1) model = view.created_models.pop() eq_(model.id, 3) eq_(model.col1, 'test1') eq_(model.col2, 'test2') eq_(model.col3, 'test3') # Try model edit view rv = client.get('/admin/model/edit/?id=3') eq_(rv.status_code, 200) data = rv.data.decode('utf-8') ok_('test1' in data) rv = client.post('/admin/model/edit/?id=3', data=dict(col1='test!', col2='test@', col3='test#')) eq_(rv.status_code, 302) eq_(len(view.updated_models), 1) model = view.updated_models.pop() eq_(model.col1, 'test!') eq_(model.col2, 'test@') eq_(model.col3, 'test#') rv = client.get('/admin/model/edit/?id=4') eq_(rv.status_code, 302) # Attempt to delete model rv = client.post('/admin/model/delete/?id=3') eq_(rv.status_code, 302) eq_(rv.headers['location'], 'http://localhost/admin/model/') # Create a dispatched application to test that edit view's "save and # continue" functionality works when app is not located at root dummy_app = Flask('dummy_app') dispatched_app = DispatcherMiddleware(dummy_app, {'/dispatched': app}) dispatched_client = Client(dispatched_app) app_iter, status, headers = dispatched_client.post( '/dispatched/admin/model/edit/?id=3', data=dict(col1='another test!', col2='test@', col3='test#', _continue_editing='True')) eq_(status, '302 FOUND') eq_(headers['Location'], 'http://localhost/dispatched/admin/model/edit/?id=3') model = view.updated_models.pop() eq_(model.col1, 'another test!')
def test_client(self): from werkzeug.test import Client return Client(self, self.response_class, use_cookies=True)
def report_download(self, data, token): """This function is used by 'qwebactionmanager.js' in order to trigger the download of a pdf/controller report. :param data: a javascript array JSON.stringified containg report internal url ([0]) and type [1] :returns: Response with a filetoken cookie and an attachment header """ requestcontent = simplejson.loads(data) url, type = requestcontent[0], requestcontent[1] try: if type == 'qweb-pdf': reportname = url.split('/report/pdf/')[1].split('?')[0] docids = None if '/' in reportname: reportname, docids = reportname.split('/') if docids: # Generic report: response = self.report_routes(reportname, docids=docids, converter='pdf') else: # Particular report: data = url_decode( url.split('?') [1]).items() # decoding the args represented in JSON response = self.report_routes(reportname, converter='pdf', **dict(data)) if reportname == 'jjuice_fedex.report_print_label': cr, context = request.cr, request.context info = request.registry('create.shipment.fedex').read( cr, SUPERUSER_ID, int(docids), ['to_person_name', 'tracking_number']) str_list = [] str_list.append(time.strftime("%Y-%m-%d")) str_list.append( info.get('to_person_name', 'unavailable') or 'unavailable') str_list.append( info.get('tracking_number', 'unavailable') or 'unavailable') label_name = '_'.join(str_list) response.headers.add( 'Content-Disposition', 'attachment; filename=%s.pdf;' % label_name) else: response.headers.add( 'Content-Disposition', 'attachment; filename=%s.pdf;' % reportname) response.set_cookie('fileToken', token) return response elif type == 'controller': reqheaders = Headers(request.httprequest.headers) response = Client(request.httprequest.app, BaseResponse).get(url, headers=reqheaders, follow_redirects=True) response.set_cookie('fileToken', token) return response else: return except Exception, e: se = _serialize_exception(e) error = {'code': 200, 'message': "Odoo Server Error", 'data': se} return request.make_response(html_escape(simplejson.dumps(error)))
class PlayerAPITestCase(unittest.TestCase): def setUp(self): from klangbecken.api import player_api self.liquidsoap_client = mock.MagicMock(name="LiquidsoapClient") self.liquidsoap_client_class = mock.Mock( return_value=self.liquidsoap_client) self.liquidsoap_client.__enter__ = mock.Mock( return_value=self.liquidsoap_client) self.tempdir = tempfile.mkdtemp() app = player_api("inexistant.sock", self.tempdir) os.mkdir(os.path.join(self.tempdir, "music")) with open(os.path.join(self.tempdir, "music", "titi.mp3"), "w"): pass self.client = Client(app) def tearDown(self): shutil.rmtree(self.tempdir) def testInfo(self): self.liquidsoap_client.info = mock.Mock(return_value="info") with mock.patch("klangbecken.api.LiquidsoapClient", self.liquidsoap_client_class): resp = self.client.get("/") self.assertEqual(resp.status_code, 200) self.assertIn(b"info", resp.data) self.liquidsoap_client.info.assert_called_once_with() def testReloadPlaylist(self): self.liquidsoap_client.command = mock.Mock(return_value="") with mock.patch("klangbecken.api.LiquidsoapClient", self.liquidsoap_client_class): resp = self.client.post("/reload/jingles") self.assertEqual(resp.status_code, 200) self.liquidsoap_client.command.assert_called_once_with( "jingles.reload") def testQueueListCorrect(self): self.liquidsoap_client.queue = mock.Mock(return_value="queue") with mock.patch("klangbecken.api.LiquidsoapClient", self.liquidsoap_client_class): resp = self.client.get("/queue/") self.assertEqual(resp.status_code, 200) self.assertIn(b"queue", resp.data) self.liquidsoap_client.queue.assert_called_once_with() def testQueuePushCorrect(self): self.liquidsoap_client.push = mock.Mock(return_value="my_id") with mock.patch("klangbecken.api.LiquidsoapClient", self.liquidsoap_client_class): resp = self.client.post("/queue/", data=json.dumps( {"filename": "music/titi.mp3"})) self.assertEqual(resp.status_code, 200) data = json.loads(resp.data) self.assertEqual(data["queue_id"], "my_id") self.liquidsoap_client.push.assert_called_once_with( os.path.join(self.tempdir, "music", "titi.mp3")) def testQueuePushIncorrect(self): self.liquidsoap_client.push = mock.Mock(return_value="my_track_id") with mock.patch("klangbecken.api.LiquidsoapClient", self.liquidsoap_client_class): resp = self.client.post("/queue/", data=json.dumps( {"filename": "music/tata.mp3"})) self.assertEqual(resp.status_code, 404) self.liquidsoap_client.push.assert_not_called() with mock.patch("klangbecken.api.LiquidsoapClient", self.liquidsoap_client_class): resp = self.client.post("/queue/", data=json.dumps( {"filename": "music/titi.abc"})) self.assertEqual(resp.status_code, 422) self.liquidsoap_client.push.assert_not_called() with mock.patch("klangbecken.api.LiquidsoapClient", self.liquidsoap_client_class): resp = self.client.post("/queue/", data=json.dumps({"file": "music/titi.mp3"})) self.assertEqual(resp.status_code, 422) self.liquidsoap_client.push.assert_not_called() def testQueueDelete(self): with mock.patch("klangbecken.api.LiquidsoapClient", self.liquidsoap_client_class): resp = self.client.delete("/queue/15") self.assertEqual(resp.status_code, 200) self.liquidsoap_client.delete.assert_called_once_with("15")
def test_client_without_reload(self): app = Revelation(self.slide, media=self.media, reloader=False) client = Client(app, BaseResponse) response = client.get("/") self.assertNotIn("reloader", response.data.decode("utf8"))
def test_prefix_call_invalid() -> None: """An invalid prefix will lead to error page.""" app = PrefixMiddleware(_make_app(), "prefix") client = Client(app) resp = client.get('/test') assert resp.status.split(" ")[:1] == ['404']
class GenericAPITestCase(unittest.TestCase): @mock.patch("klangbecken.api.ExternalAuth", lambda app, *args, **kwargs: app) def setUp(self): from klangbecken.api import klangbecken_api with mock.patch("klangbecken.api.DEFAULT_UPLOAD_ANALYZERS", []), mock.patch( "klangbecken.api.DEFAULT_UPDATE_ANALYZERS", []), mock.patch( "klangbecken.api.DEFAULT_PROCESSORS", []): self.app = klangbecken_api( "secret", "data_dir", "player.sock", ) self.client = Client(self.app) def test_application(self): self.assertTrue(callable(self.app)) def testUrls(self): resp = self.client.get("/") self.assertEqual(resp.status_code, 200) resp = self.client.get("/playlist/music/") self.assertEqual(resp.status_code, 405) resp = self.client.get("/playlist/jingles/") self.assertEqual(resp.status_code, 405) resp = self.client.get("/playlist/nonexistant/") self.assertEqual(resp.status_code, 404) resp = self.client.get("/öäü/") self.assertEqual(resp.status_code, 404) resp = self.client.post("/playlist/jingles") self.assertIn(resp.status_code, (301, 308)) resp = self.client.post("/playlist/music/") self.assertEqual(resp.status_code, 422) resp = self.client.post("/playlist/jingles/something") self.assertEqual(resp.status_code, 404) resp = self.client.put("/playlist/music/") self.assertEqual(resp.status_code, 405) resp = self.client.put("/playlist/jingles/something") self.assertEqual(resp.status_code, 404) resp = self.client.put("/playlist/jingles/something.mp3") self.assertEqual(resp.status_code, 404) resp = self.client.put("/playlist/music/" + str(uuid.uuid4())) self.assertEqual(resp.status_code, 404) resp = self.client.put("/playlist/music/" + str(uuid.uuid4()) + ".mp3") self.assertEqual(resp.status_code, 415) resp = self.client.put("/playlist/classics/" + str(uuid.uuid4()) + ".mp3") self.assertEqual(resp.status_code, 415) resp = self.client.put("/playlist/jingles/" + str(uuid.uuid4()) + ".mp3") self.assertEqual(resp.status_code, 415) resp = self.client.put("/playlist/jingles/" + str(uuid.uuid4()) + ".ttt") self.assertEqual(resp.status_code, 404) resp = self.client.delete("/playlist/music/") self.assertEqual(resp.status_code, 405) resp = self.client.delete("/playlist/jingles/something") self.assertEqual(resp.status_code, 404) resp = self.client.delete("/playlist/jingles/something.mp3") self.assertEqual(resp.status_code, 404) resp = self.client.delete("/playlist/music/" + str(uuid.uuid4())) self.assertEqual(resp.status_code, 404) resp = self.client.delete("/playlist/music/" + str(uuid.uuid4()) + ".mp3") self.assertEqual(resp.status_code, 200) resp = self.client.delete("/playlist/classics/" + str(uuid.uuid4()) + ".mp3") self.assertEqual(resp.status_code, 200) resp = self.client.delete("/playlist/jingles/" + str(uuid.uuid4()) + ".mp3") self.assertEqual(resp.status_code, 200) resp = self.client.delete("/playlist/music/" + str(uuid.uuid4()) + ".ttt") self.assertEqual(resp.status_code, 404) resp = self.client.get("/player/") self.assertEqual(resp.status_code, 404) self.assertIn(b"Player not running", resp.data)
def get_test_client(): from frappe.app import application return Client(application)
def test_client_request_ok(self): client = Client(self.app, BaseResponse) response = client.get("/") self.assertEqual(response.status, "200 OK") self.assertEqual(response.headers.get("Content-Type"), "text/html")
def test_api_quota_detail_page_no_data(self): """ Test the quota details page when there is no data """ response = Client.open( self.client, path="/api/quota/wrongguid/", headers=valid_header) self.assertEqual(response.status_code, 404)
def client(): return Client(application)
def test_main_page(self): """ Test the main page """ response = Client.open(self.client, path='/', headers=valid_header) self.assertEqual(response.status_code, 200)
def test_ie_fixes(self): @fixers.InternetExplorerFix @Request.application def application(request): response = Response('binary data here', mimetype='application/vnd.ms-excel') response.headers['Vary'] = 'Cookie' response.headers[ 'Content-Disposition'] = 'attachment; filename=foo.xls' return response c = Client(application, Response) response = c.get( '/', headers=[('User-Agent', 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)')]) # IE gets no vary assert response.data == 'binary data here' assert 'vary' not in response.headers assert response.headers[ 'content-disposition'] == 'attachment; filename=foo.xls' assert response.headers['content-type'] == 'application/vnd.ms-excel' # other browsers do c = Client(application, Response) response = c.get('/') assert response.data == 'binary data here' assert 'vary' in response.headers cc = ResponseCacheControl() cc.no_cache = True @fixers.InternetExplorerFix @Request.application def application(request): response = Response('binary data here', mimetype='application/vnd.ms-excel') response.headers['Pragma'] = ', '.join(pragma) response.headers['Cache-Control'] = cc.to_header() response.headers[ 'Content-Disposition'] = 'attachment; filename=foo.xls' return response # IE has no pragma or cache control pragma = ('no-cache', ) c = Client(application, Response) response = c.get( '/', headers=[('User-Agent', 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)')]) assert response.data == 'binary data here' assert 'pragma' not in response.headers assert 'cache-control' not in response.headers assert response.headers[ 'content-disposition'] == 'attachment; filename=foo.xls' # IE has simplified pragma pragma = ('no-cache', 'x-foo') cc.proxy_revalidate = True response = c.get( '/', headers=[('User-Agent', 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)')]) assert response.data == 'binary data here' assert response.headers['pragma'] == 'x-foo' assert response.headers['cache-control'] == 'proxy-revalidate' assert response.headers[ 'content-disposition'] == 'attachment; filename=foo.xls' # regular browsers get everything response = c.get('/') assert response.data == 'binary data here' assert response.headers['pragma'] == 'no-cache, x-foo' cc = parse_cache_control_header(response.headers['cache-control'], cls=ResponseCacheControl) assert cc.no_cache assert cc.proxy_revalidate assert response.headers[ 'content-disposition'] == 'attachment; filename=foo.xls'
def inner(): return Client(pyramid_config.make_wsgi_app())
def test_api_quotas_page(self): """ Test the quota list page """ response = Client.open( self.client, path="/api/quotas/", headers=valid_header) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.json['Quotas']), 2)
def test_simple_wsgi(): client = Client(simple_wsgi, BaseResponse) resp = client.get('/any_path') assert resp.status_code == 200 assert resp.data == b''
def client(): from utilery.views import app return Client(app, BaseResponse)
class AppTests(TestCase): def setUp(self): super().setUp() patcher = patch("reload_app.app.BigQueryWorker") worker_cls = patcher.start() self.mock_worker = worker_cls.return_value = Mock() self.addCleanup(patcher.stop) patcher = patch("google.cloud.pubsub_v1.PublisherClient") publisher_cls = patcher.start() self.mock_publisher = publisher_cls.return_value = Mock() self.addCleanup(patcher.stop) patcher = patch("reload_app.app.DogStatsdMetrics") dogstatsd_cls = patcher.start() self.mock_dogstatsd = dogstatsd_cls.return_value = Mock(spec=[ "setup", "gauge", "increment", "decrement", "histogram", "timing", "timed", ]) self.addCleanup(patcher.stop) patcher = patch("reload_app.app.geo_by_addr") geo_by_addr_fn = patcher.start() self.mock_geo_by_addr = geo_by_addr_fn.return_value = None self.addCleanup(patcher.stop) patcher = patch("reload_app.app.user_agent_parser.Parse") user_agent_parser_fn = patcher.start() self.mock_user_agent_parser = user_agent_parser_fn.return_value = { "os": { "family": "Mac OS X" }, "user_agent": { "family": "Chrome" }, } self.addCleanup(patcher.stop) if not getattr(self, "client", None): app = make_app_from_environ() self.client = Client(app, BaseResponse) def test_good_input(self): sent_data = { "url": "https://sentry.io/", "referrer": "/referrer/", "user_id": "10", } resp = self.client.post("/page/", data=json.dumps(sent_data)) assert resp.status_code == 201 assert self.mock_worker.queue.call_count == 1 row = self.mock_worker.queue.call_args[0][0] for key in list(sent_data.keys()) + [ "id", "received_at", "context", "sent_at" ]: assert key in row # /events/ endpoint. sent_data.update( event_name="assistant.guide_dismissed", guide=5, step=6, unknown_field="something", ) # Make sure events from dev clients aren't accepted. sent_data["url"] = "dev.getsentry.net:8000/" resp = self.client.post("/event/", data=json.dumps(sent_data)) assert resp.status_code == 201 assert self.mock_publisher.publish.call_count == 0 sent_data["url"] = "https://blog.sentry.io/" resp = self.client.post("/event/", data=json.dumps(sent_data)) assert resp.status_code == 201 assert self.mock_publisher.publish.call_count == 1 row = json.loads(self.mock_publisher.publish.call_args[1]["data"]) # Make sure the UUID format is valid. UUID(bytes=b64decode(row["uuid"])) for key in ("timestamp", "type", "data"): assert key in row data = row["data"] for key in list( sent_data.keys()) + ["received_at", "context", "sent_at"]: if key not in ("event_name", "unknown_field"): assert key in data assert "unknown_field" not in data def test_metric_increment(self): metric_data = {"metric_name": "app.page.bundle-load-fail"} resp = self.client.post("/metric/", data=json.dumps(metric_data)) assert resp.status_code == 201 assert self.mock_dogstatsd.increment.call_count == 1 assert self.mock_dogstatsd.increment.call_args[0] == ( "app.page.bundle-load-fail", 1, ) def test_metric_valid_tags(self): metric_data = { "metric_name": "app.component.render", "value": 123, "tags": { "name": "Main" }, } resp = self.client.post("/metric/", data=json.dumps(metric_data)) assert resp.status_code == 201 assert self.mock_dogstatsd.timing.call_count == 1 assert self.mock_dogstatsd.timing.call_args[0] == ( "app.component.render", 123) assert self.mock_dogstatsd.timing.call_args[1] == { "tags": { "name": "Main", "country_code": "unknown", "browser": "Chrome", "os": "Mac OS X", } } def test_metric_timing(self): metric_data = {"value": 123, "metric_name": "app.page.body-load"} resp = self.client.post("/metric/", data=json.dumps(metric_data)) assert resp.status_code == 201 assert self.mock_dogstatsd.timing.call_count == 1 assert self.mock_dogstatsd.timing.call_args[0] == ( "app.page.body-load", 123) def test_invalid_metric_name(self): metric_data = {"value": 123, "metric_name": "invalid_metric_name"} resp = self.client.post("/metric/", data=json.dumps(metric_data)) assert resp.status_code == 400 assert (resp.data == b"invalid_metric_name: bad request check if valid metric name") def test_invalid_metric_tags(self): metric_data = { "value": 123, "metric_name": "app.page.body-load", "tags": { "invalid": "Invalid" }, } resp = self.client.post("/metric/", data=json.dumps(metric_data)) assert resp.status_code == 400 assert resp.data == b"app.page.body-load: bad request check if valid tag name" def test_globally_allowed_tags(self): metric_data = { "value": 123, "metric_name": "app.page.body-load", "tags": { "release": "release-name" }, } resp = self.client.post("/metric/", data=json.dumps(metric_data)) assert resp.status_code == 201 assert self.mock_dogstatsd.timing.call_count == 1 assert self.mock_dogstatsd.timing.call_args[0] == ( "app.page.body-load", 123) assert (self.mock_dogstatsd.timing.call_args[1]["tags"]["release"] == "release-name") def test_batch_metrics_with_valid_and_invalid_metrics(self): data = json.dumps([ { "value": 123, "metric_name": "invalid_metric_name" }, { "value": 123, "metric_name": "app.page.body-load", "tags": { "invalid": "Invalid" }, }, { "value": 123, "metric_name": "app.page.body-load" }, ]) resp = self.client.post("/metric/", data=data) assert resp.status_code == 400 assert self.mock_dogstatsd.timing.call_count == 1 assert self.mock_dogstatsd.timing.mock_calls[0] == call( "app.page.body-load", 123, tags={ "country_code": "unknown", "browser": "Chrome", "os": "Mac OS X" }, ) assert resp.data == ( b"invalid_metric_name: bad request check if valid metric name\n" b"app.page.body-load: bad request check if valid tag name") def test_batch_metrics(self): data = json.dumps([ { "value": 123, "metric_name": "app.page.body-load" }, { "metric_name": "app.component.render", "value": 123, "tags": { "name": "Main" }, }, ]) resp = self.client.post("/metric/", data=data) assert resp.status_code == 201 assert self.mock_dogstatsd.timing.call_count == 2 assert self.mock_dogstatsd.timing.mock_calls[0] == call( "app.page.body-load", 123, tags={ "country_code": "unknown", "browser": "Chrome", "os": "Mac OS X" }, ) assert self.mock_dogstatsd.timing.mock_calls[1] == call( "app.component.render", 123, tags={ "country_code": "unknown", "name": "Main", "browser": "Chrome", "os": "Mac OS X", }, ) def test_bad_input(self): sent_data = { "url": "/url/", "referrer": "/referrer/", "user_id": "10;" } resp = self.client.post("/page/", data=json.dumps(sent_data)) assert resp.status_code == 400 sent_data.update(user_id=10, event_name="click") resp = self.client.post("/event/", data=json.dumps(sent_data)) assert resp.status_code == 400 sent_data.update(event_name="assistant.guide_dismissed", step="bad type") resp = self.client.post("/event/", data=json.dumps(sent_data)) assert resp.status_code == 400
def report_download(self, data, token): """This function is used by 'qwebactionmanager.js' in order to trigger the download of a pdf/controller report. :param data: a javascript array JSON.stringified containg report internal url ([0]) and type [1] :returns: Response with a filetoken cookie and an attachment header """ requestcontent = simplejson.loads(data) url, type = requestcontent[0], requestcontent[1] try: if type == 'qweb-pdf': reportname = url.split('/report/pdf/')[1].split('?')[0] docids = None if '/' in reportname: reportname, docids = reportname.split('/') if docids: # Generic report: response = self.report_routes(reportname, docids=docids, converter='pdf') else: # Particular report: data = url_decode(url.split('?')[1]).items() # decoding the args represented in JSON response = self.report_routes(reportname, converter='pdf', **dict(data)) #################### TO CHANGE NAME OF THE QWEB REPORT PDF FILE ################## if docids: if reportname == "account.report_invoice": inv_obj = request.registry['account.invoice'] lst_inv = [] lst_inv = docids.split(",") for ele_inv in lst_inv: inv_browse = inv_obj.browse(request.cr, request.uid, [int(ele_inv)]) if inv_browse[0].type == 'out_invoice' or inv_browse[0].type == 'in_invoice': if inv_browse[0].number: reportname = 'Invoice' + '(' + str(inv_browse[0].number or '') + ')' else: reportname = 'Invoice' else: if inv_browse[0].number: reportname = "Refund" + '(' + str(inv_browse[0].number or '') + ')' else: reportname = 'Refund' if reportname == "sale.report_saleorder": sale_obj = request.registry['sale.order'] lst = [] lst = docids.split(",") for ele in lst: sale_browse = sale_obj.browse(request.cr, request.uid, [int(ele)]) if sale_browse[0].state in ['draft', 'sent']: if sale_browse[0].name: reportname = "Quotation" + '(' + str(sale_browse[0].name) + ')' else: reportname = "Quotation" else : if sale_browse[0].name: reportname = "Order" + '(' + str(sale_browse[0].name) + ')' else: reportname = "Order" if reportname == "purchase.report_purchaseorder": purchase_obj = request.registry['purchase.order'] lst = [] lst = docids.split(",") for ele in lst: purchase_browse = purchase_obj.browse(request.cr, request.uid, [int(ele)]) if purchase_browse[0].state in ['draft', 'sent']: if purchase_browse[0].name: reportname = "Purchase Order" + '(' + str(purchase_browse[0].name) + ')' else: reportname = "Purchase Order" else : if purchase_browse[0].name: reportname = "Purchase Order" + '(' + str(purchase_browse[0].name) + ')' else: reportname = "Purchase Order" #################################################################################### response.headers.add('Content-Disposition', 'attachment; filename=%s.pdf;' % reportname) response.set_cookie('fileToken', token) return response elif type =='controller': reqheaders = Headers(request.httprequest.headers) response = Client(request.httprequest.app, BaseResponse).get(url, headers=reqheaders, follow_redirects=True) response.set_cookie('fileToken', token) return response else: return except Exception, e: se = _serialize_exception(e) error = { 'code': 200, 'message': "Odoo Server Error", 'data': se } return request.make_response(html_escape(simplejson.dumps(error)))
def test_no_entry(): client = Client(APP, response_wrapper=BaseResponse) resp = client.get('/simhash?timestamp=20180000000000&url=nonexistingdomain.org') assert resp.status_code == 200 data = json.loads(resp.data.decode('utf-8')) assert data == {'message': 'CAPTURE_NOT_FOUND', 'status': 'error'}
def run_test(self, satosa_config_dict, sp_conf, idp_conf, saml_backend_config, frontend_config): subject_id = "testuser1" # proxy config satosa_config_dict["FRONTEND_MODULES"] = [frontend_config] satosa_config_dict["BACKEND_MODULES"] = [saml_backend_config] satosa_config_dict["INTERNAL_ATTRIBUTES"]["attributes"] = { attr_name: { "saml": [attr_name] } for attr_name in USERS[subject_id] } frontend_metadata, backend_metadata = create_entity_descriptors( SATOSAConfig(satosa_config_dict)) # application test_client = Client(make_app(SATOSAConfig(satosa_config_dict)), Response) # config test SP frontend_metadata_str = str( frontend_metadata[frontend_config["name"]][0]) sp_conf["metadata"]["inline"].append(frontend_metadata_str) fakesp = FakeSP(SPConfig().load(sp_conf)) # create auth req destination, req_args = fakesp.make_auth_req( frontend_metadata[frontend_config["name"]][0].entity_id) auth_req = urlparse(destination).path + "?" + urlencode(req_args) # make auth req to proxy proxied_auth_req = test_client.get(auth_req) assert proxied_auth_req.status == "303 See Other" # config test IdP backend_metadata_str = str( backend_metadata[saml_backend_config["name"]][0]) idp_conf["metadata"]["inline"].append(backend_metadata_str) fakeidp = FakeIdP(USERS, config=IdPConfig().load(idp_conf)) # create auth resp req_params = dict( parse_qsl(urlparse(proxied_auth_req.data.decode("utf-8")).query)) url, authn_resp = fakeidp.handle_auth_req( req_params["SAMLRequest"], req_params["RelayState"], BINDING_HTTP_REDIRECT, subject_id, response_binding=BINDING_HTTP_REDIRECT) # make auth resp to proxy authn_resp_req = urlparse(url).path + "?" + urlencode(authn_resp) authn_resp = test_client.get(authn_resp_req) assert authn_resp.status == "303 See Other" # verify auth resp from proxy resp_dict = dict( parse_qsl(urlparse(authn_resp.data.decode("utf-8")).query)) auth_resp = fakesp.parse_authn_request_response( resp_dict["SAMLResponse"], BINDING_HTTP_REDIRECT) assert auth_resp.ava == USERS[subject_id]
def test_root(): client = Client(APP, response_wrapper=BaseResponse) resp = client.get('/') data = resp.data.decode('utf-8') assert data.startswith("wayback-discover-diff")
def test_task_no_snapshots(): client = Client(APP, response_wrapper=BaseResponse) resp = client.get('/simhash?url=nonexistingdomain.org&year=1999') data = json.loads(resp.data.decode('utf-8')) assert data == {'message': 'NO_CAPTURES', 'status': 'error'}
class TestApp(TestCase): def setUp(self) -> None: self.app = Hotpot() self.client = Client(self.app) def get_response_body(self, location: str, method="GET", client=None): if client is None: client = self.client if method == "GET": response = client.get(f"{location}") elif method == "POST": response = client.post(f"{location}") elif method == "PUT": response = client.put(f"{location}") elif method == "DELETE": response = client.delete(f"{location}") elif method == "PATCH": response = client.patch(f"{location}") else: raise RuntimeError("Not Supported HTTP Method") return str(list(response[0])[0], 'utf-8') def test_http_methods(self): @self.app.route("/") def index(_app: 'Hotpot', request: RequestBase): return ResponseBase(f"{request.method}") self.assertEqual("GET", self.get_response_body("/", method="GET")) self.assertEqual("POST", self.get_response_body("/", method="POST")) self.assertEqual("PUT", self.get_response_body("/", method="PUT")) self.assertEqual("DELETE", self.get_response_body("/", method="DELETE")) self.assertEqual("PATCH", self.get_response_body("/", method="PATCH")) def test_no_route(self): response = self.client.get("/") self.assertEqual(str(list(response[0])[0], 'utf-8'), "Not Found") def test_no_parameter_route(self): @self.app.route("/index1") def index1(_app, request: Request): return {"index": 1} @self.app.route("/index2") def index2(_app, request: Request): response = JSONResponse({"index": 2}) return response self.assertEqual(self.get_response_body("/index1"), r'{"index": 1}') self.assertEqual(self.get_response_body("/index2"), r'{"index": 2}') def test_parameter_route(self): @self.app.route("/year/<int:year>/") def view_year(_app, request: Request, year: int): return {"year": year} @self.app.route("/name/<string:name>/") def view_name(_app, request: Request, name: str): return {"name": name} self.assertEqual(self.get_response_body("/year/2021/"), r'{"year": 2021}') self.assertEqual(self.get_response_body("/name/hotpot/"), r'{"name": "hotpot"}') def test_endpoint_same(self): @self.app.route("/explicit", endpoint="explicit") def explicit(_app, request): return ResponseBase("explicit") @self.app.route("/test1", endpoint="explicit") def explicit(_app, request): return ResponseBase("explicit_cover") @self.app.route("/implicit") def implicit(_app, request): return ResponseBase("implicit") @self.app.route("/implicit") def implicit(_app, request): return ResponseBase("implicit_cover") # last view function will cover previous one self.assertEqual("explicit_cover", self.get_response_body("/explicit")) self.assertEqual("implicit_cover", self.get_response_body("/implicit")) def test_endpoint_different(self): @self.app.route("/explicit", endpoint="explicit") def explicit(_app, request): return ResponseBase("explicit") @self.app.route("/explicit", endpoint="explicit2") def explicit2(_app, request): return ResponseBase("explicit2") @self.app.route("/implicit") def implicit(_app, request): return ResponseBase("implicit") @self.app.route("/implicit") def implicit2(_app, request): return ResponseBase("implicit2") # if same root, different endpoint name, the previous one will be used self.assertEqual("explicit", self.get_response_body("/explicit")) self.assertEqual("implicit", self.get_response_body("/implicit")) def test_different_root_same_endpoint(self): @self.app.route("/index", endpoint="index") def index(_app, request): return ResponseBase("index") @self.app.route("/index2", endpoint="index") def index(_app, request): return ResponseBase("index2") # if different root and same endpoint, last view function will be used for all routes that map to the function self.assertEqual("index2", self.get_response_body("/index")) self.assertEqual("index2", self.get_response_body("/index2")) def test_combine_app_common(self): app1 = Hotpot(main_app=True, name="app1") @app1.route("/app1/index") def index(_app, request): return ResponseBase("app1.index") app2 = Hotpot(main_app=False, name="app2") @app2.route("/app2/index") def index(_app, request): return ResponseBase("app2.index") app1.combine_app(app2) client = Client(app1) self.assertEqual("app1.index", self.get_response_body("/app1/index", client=client)) self.assertEqual("app2.index", self.get_response_body("/app2/index", client=client)) def test_combine_app_common_with_base_rule(self): app1 = Hotpot(main_app=True, name="app1", base_rule="/app1") @app1.route("/index") def index(_app, request): return ResponseBase("app1.index") app2 = Hotpot(main_app=False, name="app2", base_rule="/app2") @app2.route("/index") def index(_app, request): return ResponseBase("app2.index") app1.combine_app(app2) client = Client(app1) self.assertEqual("app1.index", self.get_response_body("/app1/index", client=client)) self.assertEqual("app2.index", self.get_response_body("/app2/index", client=client)) # ---------- test decorator about before(after)_request(response,app) ---------- def test_decorator_after_app(self): app = Hotpot() db = dict() db['test'] = 0 app.app_global.db = db @app.after_app() def close_db(my_app: Hotpot): my_app.app_global.db['test'] = 1 del app self.assertEqual(db['test'], 1) def test_decorator_before_request(self): @self.app.before_request() def before_request(_app: 'Hotpot'): _app.app_global.custom_variable = "hotpot" with self.assertRaises(KeyError): _ = self.app.app_global.custom_variable self.get_response_body("/") self.assertEqual("hotpot", self.app.app_global.custom_variable) def test_decorator_after_request(self): @self.app.route("/") def index(_app, request): return {"index": True} @self.app.after_request() def after_request(_app, request) -> RequestBase: environ = request.environ environ['PATH_INFO'] = "/" new_request = RequestBase(environ) return new_request self.assertEqual(r'{"index": true}', self.get_response_body("/random")) def test_decorator_before_response(self): @self.app.before_response() def before_response(_app: 'Hotpot'): _app.app_global.custom_variable = "hotpot" with self.assertRaises(KeyError): _ = self.app.app_global.custom_variable self.get_response_body("/") self.assertEqual("hotpot", self.app.app_global.custom_variable) def test_decorator_after_response(self): @self.app.route("/") def index(_app, request): return {"index": True} @self.app.after_response() def after_response(_app, response: ResponseBase) -> ResponseBase: response = ResponseBase(response=r'{"test":true}') return response self.assertEqual(r'{"test":true}', self.get_response_body("/")) # -------------test http exception view------------- def test_view_exception_all(self): @self.app.view_exception_all() def view_exception_all(_app, error): return JSONResponse({"Custom HttpException": True}) @self.app.view_exception_404() def view_exception_404(_app, error): return JSONResponse({"Not Found": True}) self.assertNotEqual(self.get_response_body("/"), r'{"Not Found": true}') self.assertEqual(self.get_response_body("/"), r'{"Custom HttpException": true}') def test_view_exception_404(self): @self.app.view_exception_404() def view_exception_404(_app, error): return JSONResponse({"Not Found": True}) self.assertEqual(self.get_response_body("/"), r'{"Not Found": true}')
def test_job_params(): client = Client(APP, response_wrapper=BaseResponse) resp = client.get('/job') data = json.loads(resp.data.decode('utf-8')) assert data == dict(status='error', info='job_id param is required.')
def setUp(self): self.session_store = MockSessionStore() app = make_app(self.session_store) self.client = Client(app, BaseResponse, use_cookies=True)
def setUp(self) -> None: self.app = Hotpot() self.client = Client(self.app)