Exemplo n.º 1
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.º 2
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.º 3
0
class UtilityFunctionsTestCase(unittest.TestCase):
    def setUp(self):
        self.request = DummyRequest(api_mode="", api_version="", api_name="")

    def test_get_version_no_header_no_queryarg(self):
        "Version extraction - no version supplied"
        result = urldispatch.get_version(self.request)
        self.assertEquals(result["api"], "")
        self.assertEquals(result["version"], "")
        self.assertEquals(result["mode"], "")

    def test_get_version_in_query_arg(self):
        "Version extraction - version in query string"
        self.request.args["X-DigiTar-API-Version"] = ["dummy_api-1.0+prod"]
        result = urldispatch.get_version(self.request)
        self.assertEquals(result["api"], "dummy_api")
        self.assertEquals(result["version"], "1.0")
        self.assertEquals(result["mode"], "prod")

    def test_get_version_in_header(self):
        "Version extraction - version in header"
        self.request.setHeader("X-DigiTar-API-Version", "dummy_api-1.0+prod")
        result = urldispatch.get_version(self.request)
        self.assertEquals(result["api"], "dummy_api")
        self.assertEquals(result["version"], "1.0")
        self.assertEquals(result["mode"], "prod")

    def test_get_version_bad_version(self):
        "Version extraction - malformed version"
        self.request.setHeader("X-DigiTar-API-Version", "badmojo")
        self.assertRaises(IndexError, urldispatch.get_version, self.request)

    def tearDown(self):
        del (self.request)
Exemplo n.º 4
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.º 5
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.º 6
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.º 7
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.º 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_page_invalid_int_fail(self):
     "page argument in request is not a valid integer."
     test_request = DummyRequest()
     test_request.args["arg1"] = ["test"]
     test_request.args["page"] = ["xyz"]
     res = self.dummy_render_func2(test_request)
     self.assertEqual(res,
                      str(webapi.ValueError(test_request, "page", "Argument must be an integer.")))
Exemplo n.º 10
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.º 11
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.º 12
0
 def test_page_len_gt_max_page_len_fail(self):
     "page_len argument in request is > max_page_len."
     test_request = DummyRequest()
     test_request.args["arg1"] = ["test"]
     test_request.args["page_len"] = ["501"]
     res = self.dummy_render_func2(test_request)
     self.assertEqual(res,
                      str(webapi.ValueError(test_request, "page_len", "Argument must be 500 or less.")))
Exemplo n.º 13
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.º 14
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.º 15
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.º 16
0
 def test_page_lt_zero_fail(self):
     "page argument in request is < 0."
     test_request = DummyRequest()
     test_request.args["arg1"] = ["test"]
     test_request.args["page"] = ["-1"]
     res = self.dummy_render_func2(test_request)
     self.assertEqual(res,
                      str(webapi.ValueError(test_request, "page", "Argument must be 0 or greater.")))
Exemplo n.º 17
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.º 18
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.º 19
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.º 20
0
 def test_content_required_arg_missing_optional_flag_false(self):
     "Request JSON arguments missing required argument."
     test_request = DummyRequest()
     test_request.setHeader("Content-Type", "application/json; charset=utf-8")
     test_request.write(json.dumps({"arg2" : 1}))
     outer_wrap = webapi.json_arguments([("arg1", unicode, False)])
     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.º 21
0
 def test_content_arg_type_mismatch_optional_arg(self):
     "Request JSON optional arguments contain data which doesn't match expected type."
     test_request = DummyRequest()
     test_request.setHeader("Content-Type", "application/json; charset=utf-8")
     test_request.write(json.dumps({"arg1" : 1}))
     outer_wrap = webapi.json_arguments([("arg1", unicode, True)])
     inner_wrap = outer_wrap(self.dummy_render_func)
     self.assertEqual(str(webapi.ValueError(test_request, "arg1", "Must be of type unicode")),
                      inner_wrap(self, test_request))
Exemplo n.º 22
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.º 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_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.º 25
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.º 26
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.º 27
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.º 28
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.º 29
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.º 30
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.º 31
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.º 32
0
 def test_get_child_route_match_api_match(self):
     "Test route match with good API name match in version header"
     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("/example/", request)
     self.assertTrue(isinstance(resource, urldispatch.VersionRouter))
     self.assertEquals(resource.api_router, router)
Exemplo n.º 33
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.º 34
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.º 35
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.º 36
0
 def test_page_invalid_int_fail(self):
     "page argument in request is not a valid integer."
     test_request = DummyRequest()
     test_request.args["arg1"] = ["test"]
     test_request.args["page"] = ["xyz"]
     res = self.dummy_render_func2(test_request)
     self.assertEqual(
         res,
         str(
             webapi.ValueError(test_request, "page",
                               "Argument must be an integer.")))
Exemplo n.º 37
0
 def test_page_lt_zero_fail(self):
     "page argument in request is < 0."
     test_request = DummyRequest()
     test_request.args["arg1"] = ["test"]
     test_request.args["page"] = ["-1"]
     res = self.dummy_render_func2(test_request)
     self.assertEqual(
         res,
         str(
             webapi.ValueError(test_request, "page",
                               "Argument must be 0 or greater.")))
Exemplo n.º 38
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.º 39
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.º 40
0
 def test_page_len_gt_max_page_len_fail(self):
     "page_len argument in request is > max_page_len."
     test_request = DummyRequest()
     test_request.args["arg1"] = ["test"]
     test_request.args["page_len"] = ["501"]
     res = self.dummy_render_func2(test_request)
     self.assertEqual(
         res,
         str(
             webapi.ValueError(test_request, "page_len",
                               "Argument must be 500 or less.")))
Exemplo n.º 41
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.º 42
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.º 43
0
 def test_get_secure_cookie_expired(self):
     "Test retrieving an invalid secure cookie."
     auth.install_secure_cookies(["supersecret"])
     raw_value = "testvalue"
     timestamp = "1357260056"
     signature = "d304db1dbf1bc2fcb4eb6bc71bfd22cae4e74b74"
     secure_cookie_val = "%s|%s|%s" % (base64.b64encode(raw_value), timestamp, signature)
     request = DummyRequest()
     request.received_cookies["testkey"] = secure_cookie_val
     value = auth.get_secure_cookie(request, "testkey")
     self.assertTrue(isinstance(value, webapi.ExpiredSecureCookieError))
Exemplo n.º 44
0
 def test_get_secure_cookie_ok(self):
     "Validate retrieving a secure cookie."
     auth.install_secure_cookies(["supersecret"])
     raw_value = "testvalue"
     timestamp = "1360023531"
     signature = "e90904d67de2fd6e4d4f3c9a736e3b8c457526f9"
     secure_cookie_val = "%s|%s|%s" % (base64.b64encode(raw_value), timestamp, signature)
     request = DummyRequest()
     request.received_cookies["testkey"] = secure_cookie_val
     value = auth.get_secure_cookie(request, "testkey", expiry_days=36500)
     self.assertEqual(value, raw_value)
Exemplo n.º 45
0
 def test_get_secure_cookie_invalid(self):
     "Test retrieving an invalid secure cookie."
     auth.install_secure_cookies(["supersecret"])
     raw_value = "testvalue"
     timestamp = "1360023531"
     signature = "badsig"
     secure_cookie_val = "%s|%s|%s" % (base64.b64encode(raw_value), timestamp, signature)
     request = DummyRequest()
     request.received_cookies["testkey"] = secure_cookie_val
     value = auth.get_secure_cookie(request, "testkey")
     self.assertTrue(isinstance(value, webapi.InvalidSecureCookieError))
Exemplo n.º 46
0
 def test_get_secure_cookie_invalid(self):
     "Test retrieving an invalid secure cookie."
     auth.install_secure_cookies(["supersecret"])
     raw_value = "testvalue"
     timestamp = "1360023531"
     signature = "badsig"
     secure_cookie_val = "%s|%s|%s" % (base64.b64encode(raw_value),
                                       timestamp, signature)
     request = DummyRequest()
     request.received_cookies["testkey"] = secure_cookie_val
     value = auth.get_secure_cookie(request, "testkey")
     self.assertTrue(isinstance(value, webapi.InvalidSecureCookieError))
Exemplo n.º 47
0
 def test_get_child_route_match_api_match(self):
     "Test route match with good API name match in version header"
     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("/example/", request)
     self.assertTrue(isinstance(resource, urldispatch.VersionRouter))
     self.assertEquals(resource.api_router, router)
Exemplo n.º 48
0
 def test_get_secure_cookie_ok(self):
     "Validate retrieving a secure cookie."
     auth.install_secure_cookies(["supersecret"])
     raw_value = "testvalue"
     timestamp = "1360023531"
     signature = "e90904d67de2fd6e4d4f3c9a736e3b8c457526f9"
     secure_cookie_val = "%s|%s|%s" % (base64.b64encode(raw_value),
                                       timestamp, signature)
     request = DummyRequest()
     request.received_cookies["testkey"] = secure_cookie_val
     value = auth.get_secure_cookie(request, "testkey", expiry_days=36500)
     self.assertEqual(value, raw_value)
Exemplo n.º 49
0
 def test_post(self):
     "Test unknown API call return error code & message via POST"
     request = DummyRequest(api_mode="", api_version="", api_name="")
     request.setHeader("X-DigiTar-API-Version", "dummyapi")
     request.uri = "dummy.com/dummyapi/call"
     resource = urldispatch.UnknownCall()
     body = resource.render_POST(request)
     self.assertEquals(request.response_code, 404)
     self.assertEquals(request.getHeader("Content-Type"),
                       "application/json; charset=utf-8")
     self.assertEquals(body, str(webapi.UnknownAPICallError(request,
                                                            "call")))
Exemplo n.º 50
0
 def test_get_secure_cookie_expired_multiple_secrets(self):
     "Test retrieving an invalid secure cookie with multiple secrets installed."
     auth.install_secure_cookies(["supersecret1", "supersecret"])
     raw_value = "testvalue"
     timestamp = "1357260056"
     signature = "d304db1dbf1bc2fcb4eb6bc71bfd22cae4e74b74"
     secure_cookie_val = "%s|%s|%s" % (base64.b64encode(raw_value),
                                       timestamp, signature)
     request = DummyRequest()
     request.received_cookies["testkey"] = secure_cookie_val
     value = auth.get_secure_cookie(request, "testkey")
     self.assertTrue(isinstance(value, webapi.ExpiredSecureCookieError))
Exemplo n.º 51
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.º 52
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.º 53
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.º 54
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.º 55
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.º 56
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.º 57
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.º 58
0
class UtilityFunctionsTestCase(unittest.TestCase):
    
    def setUp(self):
        self.request = DummyRequest(api_mode="", api_version="", api_name="")
    
    def test_get_version_no_header_no_queryarg(self):
        "Version extraction - no version supplied"
        result = urldispatch.get_version(self.request)
        self.assertEquals(result["api"], "")
        self.assertEquals(result["version"], "")
        self.assertEquals(result["mode"], "")
        
    
    def test_get_version_in_query_arg(self):
        "Version extraction - version in query string"
        self.request.args["X-DigiTar-API-Version"] = ["dummy_api-1.0+prod"]
        result = urldispatch.get_version(self.request)
        self.assertEquals(result["api"], "dummy_api")
        self.assertEquals(result["version"], "1.0")
        self.assertEquals(result["mode"], "prod")
    
    def test_get_version_in_header(self):
        "Version extraction - version in header"
        self.request.setHeader("X-DigiTar-API-Version", "dummy_api-1.0+prod")
        result = urldispatch.get_version(self.request)
        self.assertEquals(result["api"], "dummy_api")
        self.assertEquals(result["version"], "1.0")
        self.assertEquals(result["mode"], "prod")
    
    def test_get_version_bad_version(self):
        "Version extraction - malformed version"
        self.request.setHeader("X-DigiTar-API-Version", "badmojo")
        self.assertRaises(IndexError, urldispatch.get_version, self.request)
    
    def tearDown(self):
        del(self.request)
Exemplo n.º 59
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.º 60
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, "")