Exemplo n.º 1
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.º 2
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.º 3
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.º 4
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.º 5
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.º 6
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.º 7
0
 def test_options_request_no_acrh(self):
     "Test an OPTIONS request w/ no 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="")
     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"),
                       "Access-Control-Allow-Origin,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.º 8
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.º 9
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.º 10
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.º 11
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.º 12
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.º 13
0
class URLMatchJSONResourceTestCase(unittest.TestCase):
    
    def setUp(self):
        self.request = DummyRequest(api_mode="", api_version="", api_name="")
        self.route_map = [("^/test/me/(?P<group1>.+)", object())]
    
    def test_init_resource(self):
        "Verify URLMatchJSONResource is correctly initialized."
        phony_call_router = object()
        res = urldispatch.URLMatchJSONResource(self.request,
                                               url_matches=self.route_map,
                                               call_router=phony_call_router)
        self.assertEquals(self.route_map,
                          res.url_matches)
        self.assertEquals(phony_call_router,
                          res.call_router)
    
    def test_render_no_deferred(self):
        "Render function w/ non-deferred return."
        
        def render_GET(request):
            return "test123"
        
        phony_call_router = object()
        res = urldispatch.URLMatchJSONResource(self.request,
                                               url_matches=self.route_map,
                                               call_router=phony_call_router)
        res.render_GET = render_GET
        ret_res = res.render(self.request)
        self.assertEqual("test123", ret_res)
    
    def test_render_no_deferred_failed(self):
        "Render function w/ failed non-deferred return."
        
        def render_GET(request):
            raise Exception("Unexpected catastrophe!")
            return "test123"
        
        phony_call_router = object()
        res = urldispatch.URLMatchJSONResource(self.request,
                                               url_matches=self.route_map,
                                               call_router=phony_call_router)
        res.render_GET = render_GET
        ret_res = res.render(self.request)
        
        actual_response = json.loads(ret_res)
        self.assertEqual(actual_response["error"]["exception_class"], "UnexpectedServerError")
        self.assertEqual(actual_response["error"]["error_code"], 505)
        self.assertTrue("Unexpected catastrophe!" in actual_response["error"]["exception_text"])
    
    def test_render_cors(self):
        "Render function w/ non-deferred return & Access-Request-Allow-Origin header."
        
        def render_GET(request):
            return "test123"
        
        phony_call_router = object()
        self.request.method = "GET"
        res = urldispatch.URLMatchJSONResource(self.request,
                                               url_matches=self.route_map,
                                               call_router=phony_call_router)
        res.render_GET = render_GET
        ret_res = res.render(self.request)
        self.assertEqual("test123", ret_res)
    
    def test_render_nonnull_deferred_success(self):
        "Non-null render function w/ successful deferred return."
        d = defer.Deferred()
        
        def render_GET(request):
            return d
        
        phony_api_router = type('obj', (object,), {'cross_origin_domains' : '*', 'inhibit_http_caching' : True})
        phony_version_router = type('obj', (object,), {'api_router' : phony_api_router})
        phony_call_router = type('obj', (object,), {'version_router' : phony_version_router})
        
        res = urldispatch.URLMatchJSONResource(self.request,
                                               url_matches=self.route_map,
                                               call_router=phony_call_router)
        res.render_GET = render_GET
        ret_res = res.render(self.request)
        self.assertEqual(NOT_DONE_YET, ret_res)
        d.callback("heya")
        self.assertEqual("*", self.request.getHeader("Access-Control-Allow-Origin"))
        self.assertEquals("true", self.request.getHeader("Access-Control-Allow-Credentials"))
        self.assertEquals("no-cache", self.request.getHeader("Cache-Control"))
        self.assertEquals("no-cache", self.request.getHeader("Pragma"))
        self.assertEqual("heya",
                         self.request.content.getvalue())
    
    def test_render_null_deferred_success(self):
        "Null render function w/ successful deferred return."
        d = defer.Deferred()
        
        def render_GET(request):
            return d
        
        phony_call_router = object()
        res = urldispatch.URLMatchJSONResource(self.request,
                                               url_matches=self.route_map,
                                               call_router=phony_call_router)
        res.render_GET = render_GET
        ret_res = res.render(self.request)
        self.assertEqual(NOT_DONE_YET, ret_res)
        d.callback(None)
        self.assertEqual("",
                         self.request.content.getvalue())
    
    def test_render_deferred_failure(self):
        "Render function w/ failed deferred return."
        d = defer.Deferred()
        
        def render_GET(request):
            return d
        
        phony_call_router = object()
        self.request.method = "GET"
        res = urldispatch.URLMatchJSONResource(self.request,
                                               url_matches=self.route_map,
                                               call_router=phony_call_router)
        res.render_GET = render_GET
        ret_res = res.render(self.request)
        self.assertEqual(NOT_DONE_YET, ret_res)
        fail = Failure(Exception("test error"))
        d.errback(fail=fail)
        self.assertEqual(str(webapi.UnexpectedServerError(self.request,
                                                          fail.getErrorMessage())),
                         self.request.content.getvalue())
Exemplo n.º 14
0
class URLMatchJSONResourceTestCase(unittest.TestCase):
    def setUp(self):
        self.request = DummyRequest(api_mode="", api_version="", api_name="")
        self.route_map = [("^/test/me/(?P<group1>.+)", object())]

    def test_init_resource(self):
        "Verify URLMatchJSONResource is correctly initialized."
        phony_call_router = object()
        res = urldispatch.URLMatchJSONResource(self.request,
                                               url_matches=self.route_map,
                                               call_router=phony_call_router)
        self.assertEquals(self.route_map, res.url_matches)
        self.assertEquals(phony_call_router, res.call_router)

    def test_render_no_deferred(self):
        "Render function w/ non-deferred return."

        def render_GET(request):
            return "test123"

        phony_call_router = object()
        res = urldispatch.URLMatchJSONResource(self.request,
                                               url_matches=self.route_map,
                                               call_router=phony_call_router)
        res.render_GET = render_GET
        ret_res = res.render(self.request)
        self.assertEqual("test123", ret_res)

    def test_render_no_deferred_failed(self):
        "Render function w/ failed non-deferred return."

        def render_GET(request):
            raise Exception("Unexpected catastrophe!")
            return "test123"

        phony_call_router = object()
        res = urldispatch.URLMatchJSONResource(self.request,
                                               url_matches=self.route_map,
                                               call_router=phony_call_router)
        res.render_GET = render_GET
        ret_res = res.render(self.request)

        actual_response = json.loads(ret_res)
        self.assertEqual(actual_response["error"]["exception_class"],
                         "UnexpectedServerError")
        self.assertEqual(actual_response["error"]["error_code"], 505)
        self.assertTrue("Unexpected catastrophe!" in actual_response["error"]
                        ["exception_text"])

    def test_render_cors(self):
        "Render function w/ non-deferred return & Access-Request-Allow-Origin header."

        def render_GET(request):
            return "test123"

        phony_call_router = object()
        self.request.method = "GET"
        res = urldispatch.URLMatchJSONResource(self.request,
                                               url_matches=self.route_map,
                                               call_router=phony_call_router)
        res.render_GET = render_GET
        ret_res = res.render(self.request)
        self.assertEqual("test123", ret_res)

    def test_render_nonnull_deferred_success(self):
        "Non-null render function w/ successful deferred return."
        d = defer.Deferred()

        def render_GET(request):
            return d

        phony_api_router = type('obj', (object, ), {
            'cross_origin_domains': '*',
            'inhibit_http_caching': True
        })
        phony_version_router = type('obj', (object, ),
                                    {'api_router': phony_api_router})
        phony_call_router = type('obj', (object, ),
                                 {'version_router': phony_version_router})

        res = urldispatch.URLMatchJSONResource(self.request,
                                               url_matches=self.route_map,
                                               call_router=phony_call_router)
        res.render_GET = render_GET
        ret_res = res.render(self.request)
        self.assertEqual(NOT_DONE_YET, ret_res)
        d.callback("heya")
        self.assertEqual("*",
                         self.request.getHeader("Access-Control-Allow-Origin"))
        self.assertEquals(
            "true", self.request.getHeader("Access-Control-Allow-Credentials"))
        self.assertEquals("no-cache", self.request.getHeader("Cache-Control"))
        self.assertEquals("no-cache", self.request.getHeader("Pragma"))
        self.assertEqual("heya", self.request.content.getvalue())

    def test_render_null_deferred_success(self):
        "Null render function w/ successful deferred return."
        d = defer.Deferred()

        def render_GET(request):
            return d

        phony_call_router = object()
        res = urldispatch.URLMatchJSONResource(self.request,
                                               url_matches=self.route_map,
                                               call_router=phony_call_router)
        res.render_GET = render_GET
        ret_res = res.render(self.request)
        self.assertEqual(NOT_DONE_YET, ret_res)
        d.callback(None)
        self.assertEqual("", self.request.content.getvalue())

    def test_render_deferred_failure(self):
        "Render function w/ failed deferred return."
        d = defer.Deferred()

        def render_GET(request):
            return d

        phony_call_router = object()
        self.request.method = "GET"
        res = urldispatch.URLMatchJSONResource(self.request,
                                               url_matches=self.route_map,
                                               call_router=phony_call_router)
        res.render_GET = render_GET
        ret_res = res.render(self.request)
        self.assertEqual(NOT_DONE_YET, ret_res)
        fail = Failure(Exception("test error"))
        d.errback(fail=fail)
        self.assertEqual(
            str(
                webapi.UnexpectedServerError(self.request,
                                             fail.getErrorMessage())),
            self.request.content.getvalue())