def test_get_file_content(self): request = Request(Environ()) stream = _io.BytesIO("hello world".encode()) request.files = {"file": FileStorage(stream=stream, filename="foo")} self.assertEqual("hello world", BaseHandler._get_file_content(request).decode())
def test_call_route_args(self, name_mock, content_mock, ip_mock): handler = TestBaseHandler.DummyHandler() env = Environ({"wsgi.input": None}) request = Request(env) res = handler._call(handler.dummy_endpoint, {"param": "42"}, request) self.assertEqual(43, res["incremented"])
def test_call_general_attrs(self, name_mock, content_mock, ip_mock): handler = TestBaseHandler.DummyHandler() env = Environ({"wsgi.input": None}) request = Request(env) res = handler._call(handler.myip, {}, request) self.assertEqual("1.2.3.4", res)
def test_call_default(self, name_mock, content_mock, ip_mock): handler = TestBaseHandler.DummyHandler() env = Environ({"wsgi.input": None}) request = Request(env) res = handler._call(handler.dummy_endpoint, {}, request) self.assertEqual(124, res["incremented"])
def test_call_file(self): handler = TestBaseHandler.DummyHandler() env = Environ({"wsgi.input": None}) request = Request(env) request.files = {"file": FileStorage(filename="foo")} res = handler._call(handler.file, {}, request) self.assertEqual("foo", res)
def test_call_fail_cast_parameter(self, name_mock, content_mock, ip_mock): handler = TestBaseHandler.DummyHandler() env = Environ({"wsgi.input": None}) request = Request(env) request.form = {"param": "nope"} with self.assertRaises(BadRequest): handler._call(handler.dummy_endpoint, {}, request)
def test_call_cast_parameter(self, name_mock, content_mock, ip_mock): handler = TestBaseHandler.DummyHandler() env = Environ({"wsgi.input": None}) request = Request(env) request.form = {'param': '42'} res = handler._call(handler.dummy_endpoint, {}, request) self.assertEqual(43, res["incremented"])
def test_call_with_error(self, name_mock, content_mock, ip_mock): handler = TestBaseHandler.DummyHandler() env = Environ({"wsgi.input": None}) request = Request(env) with self.assertRaises(Forbidden) as ex: handler._call(handler.required, {"param": 42}, request) response = ex.exception.response self.assertIn("NOBUONO", response.data.decode()) self.assertIn("nononono", response.data.decode())
def test_call_required_args(self, name_mock, content_mock, ip_mock): handler = TestBaseHandler.DummyHandler() env = Environ({"wsgi.input": None}) request = Request(env) with self.assertRaises(BadRequest) as ex: handler._call(handler.required, {}, request) response = ex.exception.response self.assertIn("MISSING_PARAMETER", response.data.decode()) self.assertIn("param", response.data.decode())
def test_call(self, name_mock, content_mock, ip_mock): handler = TestBaseHandler.DummyHandler() env = Environ({"wsgi.input": None}) request = Request(env) request.form = {"param": 42} res = handler._call(handler.dummy_endpoint, {}, request) self.assertEqual(43, res["incremented"]) Logger.c.execute("SELECT * FROM logs WHERE category = 'HTTP'") row = Logger.c.fetchone() self.assertIn("1.2.3.4", row[3]) self.assertIn("dummy_endpoint", row[3])
def test_call_with_decorators(self): handler = TestBaseHandler.DummyHandler() env = Environ({"wsgi.input": None}) request = Request(env) Database.add_user("token", "", "") Database.add_task("poldo", "", "", 1, 1) Database.add_input("inputid", "token", "poldo", 1, "", 42) Database.add_output("outputid", "inputid", "", 42, "") handler._call(handler.with_decorators, { "input_id": "inputid", "output_id": "outputid" }, request)
def test_get_ip_no_proxies(self): Config.num_proxies = 0 request = Request(Environ(REMOTE_ADDR="1.2.3.4")) ip = BaseHandler.get_ip(request) self.assertEqual("1.2.3.4", ip)
def test_get_file_content_no_file(self): request = Request(Environ()) request.files = {} self.assertIsNone(BaseHandler._get_file_content(request))
def test_get_file_name(self): request = Request(Environ()) request.files = {"file": FileStorage(filename="foo")} self.assertEqual("foo", BaseHandler._get_file_name(request))