Exemplo n.º 1
0
 def test_get_options(self):
     "Test OPTIONS request."
     route_map = [(r"^/example/", dummy_api)]
     api_config = {"arg1": True, "arg2": 42}
     request = DummyRequest(api_mode="prod",
                            api_version="1.0",
                            api_name="dummy_api")
     request.method = "OPTIONS"
     request.setHeader("Access-Control-Request-Headers", "Content-Type")
     router = urldispatch.APIRouter(route_map,
                                    config=api_config,
                                    cross_origin_domains="*")
     resource = router.getChild("/example/", request)
     self.assertTrue(isinstance(resource, urldispatch.CORSInterrogation))
     resource.render_OPTIONS(request)
     self.assertEquals(request.getHeader("Content-Type"),
                       "application/json; charset=utf-8")
     self.assertEquals(request.getHeader("Access-Control-Allow-Origin"),
                       "*")
     self.assertEquals(
         request.getHeader("Access-Control-Allow-Credentials"), "true")
     self.assertEquals(request.getHeader("Access-Control-Allow-Headers"),
                       "Content-Type")
     self.assertEquals(
         request.getHeader("Access-Control-Allow-Methods"),
         "PUT,GET,DELETE,POST,HEAD,TRACE,CONNECT,PROPFIND,PROPPATCH,MKCOL,COPY,MOVE,LOCK,UNLOCK"
     )
Exemplo n.º 2
0
 def test_options_request_with_acrh(self):
     "Test a OPTIONS request w/ supplied Access-Control-Request-Headers header."
     route_map = [(r"^/example/", dummy_api)]
     api_config = {"arg1": True, "arg2": 42}
     router = urldispatch.APIRouter(route_map,
                                    config=api_config,
                                    cross_origin_domains="*")
     request = DummyRequest(api_mode="", api_version="", api_name="")
     request.setHeader("Access-Control-Request-Headers", "Content-Type")
     resource = urldispatch.CORSInterrogation(request, api_router=router)
     body = resource.render_OPTIONS(request)
     self.assertEquals(request.response_code, 200)
     self.assertEquals(request.getHeader("Content-Type"),
                       "application/json; charset=utf-8")
     self.assertEquals(request.getHeader("Access-Control-Allow-Origin"),
                       "*")
     self.assertEquals(
         request.getHeader("Access-Control-Allow-Credentials"), "true")
     self.assertEquals(request.getHeader("Access-Control-Allow-Headers"),
                       "Content-Type")
     self.assertEquals(
         request.getHeader("Access-Control-Allow-Methods"),
         "PUT,GET,DELETE,POST,HEAD,TRACE,CONNECT,PROPFIND,PROPPATCH,MKCOL,COPY,MOVE,LOCK,UNLOCK"
     )
     self.assertEquals(body, "")
Exemplo n.º 3
0
 def test_good_page_overrides_default(self):
     "Valid page argument overrides page from default."
     test_request = DummyRequest()
     test_request.args["arg1"] = ["test"]
     test_request.args["page"] = ["1"]
     res = self.dummy_render_func2(test_request)
     self.assertEqual(res["page"][0], "1")
Exemplo n.º 4
0
 def test_json_validate_success(self):
     "Request URL arguments successfully validated."
     test_request = DummyRequest()
     test_request.setHeader("Content-Type",
                            "application/json; charset=utf-8")
     test_request.args["arg1"] = "test"
     res = self.dummy_render_func2(test_request)
     self.assertEqual(res, "okey dokey")
Exemplo n.º 5
0
    def test_no_content_type_set(self):
        "Request does not have a Content-Type set"
        test_request = DummyRequest()

        outer_wrap = webapi.url_arguments(["arg1"], "application/json")
        inner_wrap = outer_wrap(self.dummy_render_func)
        self.assertEqual(str(webapi.ContentTypeError(test_request)),
                         inner_wrap(self, test_request))
Exemplo n.º 6
0
 def test_get_child_route_no_api_match(self):
     "Test route match with no API name match in version header"
     request = DummyRequest(api_mode="", api_version="", api_name="")
     request.setHeader("X-DigiTar-API-Version", "badmojoapi-1.0+prod")
     route_map = [(r"^/example/", dummy_api)]
     router = urldispatch.APIRouter(route_map)
     resource = router.getChild("/example/", request)
     self.assertTrue(isinstance(resource, urldispatch.UnknownVersion))
Exemplo n.º 7
0
    def test_no_content_type_set(self):
        "Request does not have a Content-Type set"
        test_request = DummyRequest()

        outer_wrap = webapi.json_arguments([("arg1", unicode)])
        inner_wrap = outer_wrap(self.dummy_render_func)
        self.assertEqual(str(webapi.ContentTypeError(test_request)),
                         inner_wrap(self, test_request))
Exemplo n.º 8
0
 def test_get_child_version_unknown(self):
     "Test version extraction with bad header"
     request = DummyRequest(api_mode="", api_version="", api_name="")
     request.setHeader("X-DigiTar-API-Version", "dummyapi")
     route_map = [(r"^/example/", dummy_api)]
     router = urldispatch.APIRouter(route_map)
     resource = router.getChild("/example/", request)
     self.assertTrue(isinstance(resource, urldispatch.UnknownVersion))
Exemplo n.º 9
0
 def test_content_type_no_charset(self):
     "Request Content-Type does not have a character set specified"
     test_request = DummyRequest()
     test_request.setHeader("Content-Type", "application/json")
     outer_wrap = webapi.json_arguments([("arg1", unicode)])
     inner_wrap = outer_wrap(self.dummy_render_func)
     self.assertEqual(str(webapi.CharsetNotUTF8Error(test_request)),
                      inner_wrap(self, test_request))
Exemplo n.º 10
0
 def test_content_type_content_type_mismatch(self):
     "Request Content-Type doesn't match expected type."
     test_request = DummyRequest()
     test_request.setHeader("Content-Type", "image/jpeg; charset=utf-8")
     outer_wrap = webapi.json_arguments([("arg1", unicode)],
                                        content_type="application/json")
     inner_wrap = outer_wrap(self.dummy_render_func)
     self.assertEqual(str(webapi.ContentTypeError(test_request)),
                      inner_wrap(self, test_request))
Exemplo n.º 11
0
 def setUp(self):
     self.request = DummyRequest(api_mode="",
                                 api_version="1.0",
                                 api_name="")
     self.route_map = [("^/test/me/(?P<group1>.+)", calls.PingCall)]
     self.api_router = urldispatch.APIRouter(self.route_map)
     self.version_map = {"1.0": (r"1.0", v1_0)}
     self.version_router = urldispatch.VersionRouter(
         self.version_map, self.api_router)
Exemplo n.º 12
0
 def test_good_page_and_page_len_overrides_default(self):
     "Valid page & page_len arguments override page & page_len from defaults."
     test_request = DummyRequest()
     test_request.args["arg1"] = ["test"]
     test_request.args["page"] = ["1"]
     test_request.args["page_len"] = ["50"]
     res = self.dummy_render_func2(test_request)
     self.assertEqual(res["page"][0], "1")
     self.assertEqual(res["page_len"][0], "50")
Exemplo n.º 13
0
 def test_get_no_header(self):
     request = DummyRequest(api_mode="", api_version="", api_name="")
     resource = urldispatch.UnknownVersion()
     body = resource.render_GET(request)
     self.assertEquals(request.response_code, 406)
     self.assertEquals(request.getHeader("Content-Type"),
                       "application/json; charset=utf-8")
     self.assertEquals(body, str(webapi.UnknownAPIVersionError(request,
                                                               "")))
Exemplo n.º 14
0
 def test_content_type_charset_not_utf8(self):
     "Request Content-Type characters set is not UTF-8"
     test_request = DummyRequest()
     test_request.setHeader("Content-Type",
                            "application/json; charset=iso-1022-jp")
     outer_wrap = webapi.json_arguments([("arg1", unicode)])
     inner_wrap = outer_wrap(self.dummy_render_func)
     self.assertEqual(str(webapi.CharsetNotUTF8Error(test_request)),
                      inner_wrap(self, test_request))
Exemplo n.º 15
0
    def test_unexpected_server_error(self):
        "Validate UnexpectedServerError"
        request = DummyRequest()
        obj_error = webapi.UnexpectedServerError(request, "what error?")

        self.assertEquals(obj_error.error_code, 505)
        self.assertEquals(obj_error.exception_class, "UnexpectedServerError")
        self.assertEquals(obj_error.exception_text , "An unexpected error has occurred processing the request. " \
                                        "Error: 'what error?'")
        self.assertEquals(request.response_code, 409)
Exemplo n.º 16
0
 def test_json_validate_success(self):
     "Request JSON arguments successfully validated."
     test_request = DummyRequest()
     test_request.setHeader("Content-Type",
                            "application/json; charset=utf-8")
     test_request.write(json.dumps({"arg1": unicode("hi")}))
     self.dummy_render_func2(test_request)
     self.assertTrue(hasattr(test_request, "jsonArgs"))
     self.assertTrue(test_request.jsonArgs.has_key("arg1"))
     self.assertEqual(test_request.jsonArgs["arg1"], unicode("hi"))
Exemplo n.º 17
0
    def test_content_type_error(self):
        "Validate ContentTypeError"
        request = DummyRequest()
        obj_error = webapi.ContentTypeError(request)

        self.assertEquals(obj_error.error_code, 503)
        self.assertEquals(obj_error.exception_class, "ContentTypeError")
        self.assertEquals(obj_error.exception_text , "The Content-Type of the API request was not of the expected " + \
                                        "format...or the API/version requested does not exist.")
        self.assertEquals(request.response_code, 406)
Exemplo n.º 18
0
    def test_unknown_api_call_error(self):
        "Validate UnknownAPICallError"
        request = DummyRequest()
        obj_error = webapi.UnknownAPICallError(request, "test_call")

        self.assertEquals(obj_error.error_code, 203)
        self.assertEquals(obj_error.exception_class, "UnknownAPICallError")
        self.assertEquals(obj_error.exception_text,
                          "The requested API call 'test_call' is unknown.")
        self.assertEquals(request.response_code, 404)
Exemplo n.º 19
0
    def test_request_missing_argument(self):
        "Request does not have a Content-Type set"
        test_request = DummyRequest()

        outer_wrap = webapi.url_arguments(["arg1"])
        inner_wrap = outer_wrap(self.dummy_render_func)
        self.assertEqual(
            str(webapi.ValueError(test_request, "arg1",
                                  "Argument is missing.")),
            inner_wrap(self, test_request))
Exemplo n.º 20
0
    def test_charset_not_utf8_error(self):
        "Validate CharsetNOtUTF8Error"
        request = DummyRequest()
        obj_error = webapi.CharsetNotUTF8Error(request)

        self.assertEquals(obj_error.error_code, 504)
        self.assertEquals(obj_error.exception_class, "CharsetNotUTF8Error")
        self.assertEquals(obj_error.exception_text , "The Content-Type of the API request did not specify a charset, " \
                                        "or the charset specified was not UTF-8.")
        self.assertEquals(request.response_code, 409)
Exemplo n.º 21
0
 def test_content_not_dict(self):
     "Request content contained in a dictionary/hash."
     test_request = DummyRequest()
     test_request.setHeader("Content-Type",
                            "application/json; charset=utf-8")
     test_request.write(json.dumps("not a dict"))
     outer_wrap = webapi.json_arguments([("arg1", unicode)])
     inner_wrap = outer_wrap(self.dummy_render_func)
     self.assertEqual(str(webapi.RequestNotHashError(test_request)),
                      inner_wrap(self, test_request))
Exemplo n.º 22
0
 def test_post(self):
     "Test unknown version return error code & message via POST"
     request = DummyRequest(api_mode="", api_version="", api_name="")
     resource = urldispatch.UnknownVersion()
     body = resource.render_POST(request)
     self.assertEquals(request.response_code, 406)
     self.assertEquals(request.getHeader("Content-Type"),
                       "application/json; charset=utf-8")
     self.assertEquals(body, str(webapi.UnknownAPIVersionError(request,
                                                               "")))
Exemplo n.º 23
0
 def test_content_invalid_json(self):
     "Request content not valid JSON."
     test_request = DummyRequest()
     test_request.setHeader("Content-Type",
                            "application/json; charset=utf-8")
     test_request.write("boingo! bad json!")
     outer_wrap = webapi.json_arguments([("arg1", unicode)])
     inner_wrap = outer_wrap(self.dummy_render_func)
     self.assertEqual(str(webapi.JSONDecodeError(test_request)),
                      inner_wrap(self, test_request))
Exemplo n.º 24
0
 def test_get_child_no_route_match(self):
     "Test no route match"
     route_map = [(r"^/example/", dummy_api)]
     api_config = {"arg1": True, "arg2": 42}
     request = DummyRequest(api_mode="prod",
                            api_version="1.0",
                            api_name="dummy_api")
     request.setHeader("X-DigiTar-API-Version", "dummy_api-1.0+prod")
     router = urldispatch.APIRouter(route_map, config=api_config)
     resource = router.getChild("/badmojo/", request)
     self.assertTrue(isinstance(resource, urldispatch.UnknownAPI))
Exemplo n.º 25
0
 def test_invalid_credentials(self):
     "Invalid HTTP BasicAuth credentials."
     test_request = DummyRequest(user="******", password="******")
     outer_wrap = webapi.auth_http_basic(auth_test)
     inner_wrap = outer_wrap(self.dummy_render_func)
     inner_wrap(self, test_request)
     self.assertEqual(test_request.response_code, 401)
     self.assertEqual(test_request.getHeader("WWW-Authenticate"),
                      'Basic realm="Shiji"')
     self.assertEqual(str(webapi.AccessDeniedError(test_request)),
                      test_request.content.getvalue())
Exemplo n.º 26
0
    def test_write_json(self):
        "Validate write_json outputs JSON & sets content length"

        test_request = DummyRequest()
        test_obj = {"test_key": "test_value"}
        webapi.write_json(test_request, test_obj)

        self.assertEquals(test_request.getAllHeaders()["Content-Length"],
                          len(json.dumps(test_obj)))
        self.assertEquals(test_request.content.getvalue(),
                          json.dumps(test_obj))
Exemplo n.º 27
0
    def test_request_not_hash_error(self):
        "Validate RequestNotHashError"
        request = DummyRequest()
        obj_error = webapi.RequestNotHashError(request)

        self.assertEquals(obj_error.error_code, 206)
        self.assertEquals(obj_error.exception_class, "RequestNotHashError")
        self.assertEquals(
            obj_error.exception_text,
            "Request body must be a JSON-encoded hash table/dictionary.")
        self.assertEquals(request.response_code, 409)
Exemplo n.º 28
0
    def test_access_denied_error(self):
        "Validate AccessDeniedError"
        request = DummyRequest()
        obj_error = webapi.AccessDeniedError(request)

        self.assertEquals(obj_error.error_code, 501)
        self.assertEquals(obj_error.exception_class, "AccessDeniedError")
        self.assertEquals(
            obj_error.exception_text,
            "Insufficient permission to perform the requested action.")
        self.assertEquals(request.response_code, 409)
Exemplo n.º 29
0
    def test_value_error(self):
        "Validate ValueError"
        request = DummyRequest()
        obj_error = webapi.ValueError(request, "arg_name", "extra description")

        self.assertEquals(obj_error.error_code, 502)
        self.assertEquals(obj_error.exception_class, "ValueError")
        self.assertEquals(
            obj_error.exception_text,
            "Invalid value for argument 'arg_name'. extra description")
        self.assertEquals(request.response_code, 409)
Exemplo n.º 30
0
    def test_expired_secure_cookie_error(self):
        "Validate ExpiredSecureCookieError"
        request = DummyRequest()
        obj_error = webapi.ExpiredSecureCookieError(request, "mycookie")

        self.assertEquals(obj_error.error_code, 508)
        self.assertEquals(obj_error.exception_class,
                          "ExpiredSecureCookieError")
        self.assertEquals(obj_error.exception_text,
                          "Secure cookie 'mycookie' is expired.")
        self.assertEquals(request.response_code, 409)