def test_metrics_capture(self): app = TestApp(self.config.make_wsgi_app()) # Monkey-patch the app to make legitimate hawk-signed requests. user_id = 42 auth_policy = self.config.registry.getUtility(IAuthenticationPolicy) req = Request.blank("http://localhost/") auth_token, auth_secret = auth_policy.encode_hawk_id(req, user_id) def new_do_request(req, *args, **kwds): hawkauthlib.sign_request(req, auth_token, auth_secret) return orig_do_request(req, *args, **kwds) orig_do_request = app.do_request app.do_request = new_do_request # Make a request that hits the database, capturing its logs. with testfixtures.LogCapture() as logs: app.get("/1.5/42/info/collections") # DB usage metrics should have been generated in a log message. for r in logs.records: if "syncstorage.storage.sql.db.execute" in r.__dict__: break else: assert False, "metrics were not collected"
def test_content_type_with_no_body_should_pass(self): app = TestApp(main({})) request = app.RequestClass.blank("/newsletter", method="POST", headers={"Content-Type": "application/json"}) response = app.do_request(request, 200, True) self.assertEqual(response.status_code, 200) self.assertEqual(response.json["body"], {})
def test_content_type_missing_with_no_body_should_pass(self): app = TestApp(main({})) # requesting without a Content-Type header nor a body should # return a 200. request = app.RequestClass.blank('/newsletter', method='POST') response = app.do_request(request, 200, True) self.assertEqual(response.status_code, 200) self.assertEqual(response.json['body'], {})
def test_content_type_with_no_body_should_pass(self): app = TestApp(main({})) request = app.RequestClass.blank('/newsletter', method='POST', headers={'Content-Type': 'application/json'}) response = app.do_request(request, 200, True) self.assertEqual(response.status_code, 200) self.assertEqual(response.json['body'], {})
def test_content_type_missing_with_no_body_should_pass(self): # test that a Content-Type request headers is present app = TestApp(main({})) # requesting without a Content-Type header nor a body should # return a 200. request = app.RequestClass.blank('/service5', method='POST') response = app.do_request(request, 200, True) self.assertEqual(response.status_code, 200)
def test_content_type_with_no_body_should_pass(self): app = TestApp(main({})) request = app.RequestClass.blank('/newsletter', method='POST', headers={'Content-Type': 'application/json'}) response = app.do_request(request, 200, True) self.assertEqual(response.status_code, 200) self.assertEqual(response.json['body'], {})
def test_content_type_missing_with_no_body_should_pass(self): app = TestApp(main({})) # requesting without a Content-Type header nor a body should # return a 200. request = app.RequestClass.blank('/newsletter', method='POST') response = app.do_request(request, 200, True) self.assertEqual(response.status_code, 200) self.assertEqual(response.json['body'], {})
def test_content_type_missing(self): # test that a Content-Type request headers is present app = TestApp(main({})) # requesting without a Content-Type header should return a 415 ... request = app.RequestClass.blank('/service5', method='POST') response = app.do_request(request, 415, True) # ... with an appropriate json error structure error_location = response.json['errors'][0]['location'] error_name = response.json['errors'][0]['name'] error_description = response.json['errors'][0]['description'] self.assertEqual('header', error_location) self.assertEqual('Content-Type', error_name) self.assertTrue('application/json' in error_description)
def test_content_type_missing(self): # test that a Content-Type request headers is present app = TestApp(main({})) # requesting without a Content-Type header should return a 415 ... request = app.RequestClass.blank('/service5', method='POST') response = app.do_request(request, 415, True) # ... with an appropriate json error structure error_location = response.json['errors'][0]['location'] error_name = response.json['errors'][0]['name'] error_description = response.json['errors'][0]['description'] self.assertEqual('header', error_location) self.assertEqual('Content-Type', error_name) self.assertTrue('application/json' in error_description)
def test_content_type_missing(self): # test that a Content-Type request headers is present app = TestApp(main({})) # requesting without a Content-Type header should return a 415 ... request = app.RequestClass.blank("/service5", method="POST") response = app.do_request(request, 415, True) # ... with an appropriate json error structure error_location = response.json["errors"][0]["location"] error_name = response.json["errors"][0]["name"] error_description = response.json["errors"][0]["description"] self.assertEqual("header", error_location) self.assertEqual("Content-Type", error_name) self.assertTrue("application/json" in error_description)
def test_read_max_on_auth_fail(self): ''' KerberosAuthMiddleware's ``read_max_on_auth_fail`` should allow customizing reading of request bodies of unauthenticated requests. ''' body = b'body of unauthenticated request' for read_max in (0, 5, 100, _DEFAULT_READ_MAX, float('inf')): # When we drop Py2, we can use `with self.subTest(read_max=read_max):` here. app = TestApp(KerberosAuthMiddleware(index, read_max_on_auth_fail=read_max)) req = TestRequest.blank('/', method='POST', body=body) resp = app.do_request(req, status=401) if read_max < len(body): expect_read = 0 else: expect_read = min(read_max, len(body)) self.assertEqual(req.body_file.input.tell(), expect_read)
def test_content_type_missing(self): # test that a Content-Type request headers is present app = TestApp(main({})) # Requesting without a Content-Type header should # return "415 Unsupported Media Type" ... request = app.RequestClass.blank("/service5", method="POST", POST="some data") response = app.do_request(request, 415, True) self.assertEqual(response.status_code, 415) # ... with an appropriate json error structure. error_location = response.json["errors"][0]["location"] error_name = response.json["errors"][0]["name"] error_description = response.json["errors"][0]["description"] self.assertEqual("header", error_location) self.assertEqual("Content-Type", error_name) self.assertIn("application/json", error_description)
def _make_test_app(self): app = TestApp(self.config.make_wsgi_app()) # Monkey-patch the app to make legitimate hawk-signed requests. user_id = 42 auth_policy = self.config.registry.getUtility(IAuthenticationPolicy) req = Request.blank("http://localhost/") auth_token, auth_secret = auth_policy.encode_hawk_id(req, user_id) def new_do_request(req, *args, **kwds): hawkauthlib.sign_request(req, auth_token, auth_secret) return orig_do_request(req, *args, **kwds) orig_do_request = app.do_request app.do_request = new_do_request return app
def _make_test_app(self): app = TestApp(self.config.make_wsgi_app()) # Monkey-patch the app to make legitimate hawk-signed requests. user_id = 42 auth_policy = self.config.registry.getUtility(IAuthenticationPolicy) req = Request.blank("http://localhost/") auth_token, auth_secret = auth_policy.encode_hawk_id(req, user_id) def new_do_request(req, *args, **kwds): hawkauthlib.sign_request(req, auth_token, auth_secret) return orig_do_request(req, *args, **kwds) orig_do_request = app.do_request app.do_request = new_do_request return app
def test_content_type_missing(self): # test that a Content-Type request headers is present app = TestApp(main({})) # Requesting without a Content-Type header should # return "415 Unsupported Media Type" ... request = app.RequestClass.blank('/service5', method='POST', POST="some data") response = app.do_request(request, 415, True) self.assertEqual(response.status_code, 415) # ... with an appropriate json error structure. error_location = response.json['errors'][0]['location'] error_name = response.json['errors'][0]['name'] error_description = response.json['errors'][0]['description'] self.assertEqual('header', error_location) self.assertEqual('Content-Type', error_name) self.assertIn('application/json', error_description)
def test_content_type_missing(self): # test that a Content-Type request headers is present app = TestApp(main({})) # Requesting without a Content-Type header should # return "415 Unsupported Media Type" ... request = app.RequestClass.blank('/service5', method='POST', POST="some data") response = app.do_request(request, 415, True) self.assertEqual(response.status_code, 415) # ... with an appropriate json error structure. error_location = response.json['errors'][0]['location'] error_name = response.json['errors'][0]['name'] error_description = response.json['errors'][0]['description'] self.assertEqual('header', error_location) self.assertEqual('Content-Type', error_name) self.assertIn('application/json', error_description)
def test_metrics_capture_for_batch_uploads(self): app = TestApp(self.config.make_wsgi_app()) # Monkey-patch the app to make legitimate hawk-signed requests. user_id = 42 auth_policy = self.config.registry.getUtility(IAuthenticationPolicy) req = Request.blank("http://localhost/") auth_token, auth_secret = auth_policy.encode_hawk_id(req, user_id) def new_do_request(req, *args, **kwds): hawkauthlib.sign_request(req, auth_token, auth_secret) return orig_do_request(req, *args, **kwds) orig_do_request = app.do_request app.do_request = new_do_request collection = "/1.5/42/storage/col1" with testfixtures.LogCapture() as logs: bso = {"id": "1", "payload": "x"} res = app.post_json(collection + "?batch=true", [bso]) batch = res.json["batch"] for r in logs.records: if "syncstorage.storage.sql.append_items_to_batch" in r.__dict__: break else: assert False, "timer metrics were not emitted" with testfixtures.LogCapture() as logs: endpoint = collection + "?batch={0}&commit=true".format(batch) app.post_json(endpoint, []) # DB timing metrics should have been generated in a log message. for r in logs.records: if "syncstorage.storage.sql.apply_batch" in r.__dict__: break else: assert False, "timer metrics were not emitted"
def test_metrics_capture_for_batch_uploads(self): app = TestApp(self.config.make_wsgi_app()) # Monkey-patch the app to make legitimate hawk-signed requests. user_id = 42 auth_policy = self.config.registry.getUtility(IAuthenticationPolicy) req = Request.blank("http://localhost/") auth_token, auth_secret = auth_policy.encode_hawk_id(req, user_id) def new_do_request(req, *args, **kwds): hawkauthlib.sign_request(req, auth_token, auth_secret) return orig_do_request(req, *args, **kwds) orig_do_request = app.do_request app.do_request = new_do_request collection = "/1.5/42/storage/col1" with testfixtures.LogCapture() as logs: bso = {"id": "1", "payload": "x"} res = app.post_json(collection + "?batch=true", [bso]) batch = res.json["batch"] for r in logs.records: if "syncstorage.storage.sql.append_items_to_batch" in r.__dict__: break else: assert False, "timer metrics were not emitted" with testfixtures.LogCapture() as logs: endpoint = collection + "?batch={0}&commit=true".format(batch) app.post_json(endpoint, []) # DB timing metrics should have been generated in a log message. for r in logs.records: if "syncstorage.storage.sql.apply_batch" in r.__dict__: break else: assert False, "timer metrics were not emitted"
def do_request(self, req, status, expect_errors): ret = TestApp.do_request(self, req, status, expect_errors) self.prune_empty_cookies() return ret