Example #1
0
class ResourceTestCase(unittest.TestCase):

    def setUp(self):
        self.api = RESTApi(routes)

    def test_regular_response(self):
        request = DiabloDummyRequest([''])
        request.path = '/testregular'
        request.headers = {'content-type': 'application/json'}
        resource = self.api.getChild('/testregular', request)
        d = _render(resource, request)
        def rendered(ignored):
            response = ''.join(request.written)
            response_obj = json.loads(response)
            self.assertEquals(response_obj, regular_result)
        d.addCallback(rendered)
        return d

    def test_deferred_response(self):
        request = DiabloDummyRequest([''])
        request.path = '/testdeferred'
        request.headers = {'content-type': 'application/json'}
        resource = self.api.getChild('/testdeferred', request)
        d = _render(resource, request)
        def rendered(ignored):
            response = ''.join(request.written)
            response_obj = json.loads(response)
            self.assertEquals(response_obj, deferred_result)
        d.addCallback(rendered)
        return d
Example #2
0
class PostResourceTest(unittest.TestCase):
  
    def setUp(self):
        self.api = RESTApi(routes)

    def test_post_resource(self):
        request = DiabloDummyRequest([''])
        request.method = 'POST'
        request.path = '/a/test/resource'
        request.headers = {'content-type': 'application/json'}
        request.data = json.dumps({'key2': 'value2'})
        resource = self.api.getChild('/ignored', request)
        d = _render(resource, request)
        def rendered(ignored):
            self.assertEquals(request.responseCode, OK)
        d.addCallback(rendered)
        
        request2 = DiabloDummyRequest([''])
        request2.path = '/a/test/resource/key2'
        request2.headers = {'content-type': 'application/json'}
        resource2 = self.api.getChild('/ignored', request2)
        def doGet(ignored):
          d2 = _render(resource2, request2)
          def get_rendered(ignored):
              response = ''.join(request2.written)
              response_obj = json.loads(response)
              self.assertEquals(response_obj, 'value2')
          d2.addCallback(get_rendered)
          return d2
        d.addCallback(doGet)
        return d
Example #3
0
class TestErrors(unittest.TestCase):
    def setUp(self):
        self.api = RESTApi(routes)

    def test_exec_handler_fails_http(self):
        request = DiabloDummyRequest([])
        request.method = 'GET'
        request.path = '/error/1'
        resource = self.api.getChild('/error/1', request)
        d = _render(resource, request)

        def rendered(ignored):
            response = ''.join(request.written)
            self.assertEquals(NOT_FOUND, request.responseCode)
            self.assertEquals("Can't find it, sorry.", response)
        d.addCallback(rendered)
        return d

    def test_exec_handler_fails_unknown(self):
        request = DiabloDummyRequest([])
        request.method = 'GET'
        request.path = '/error/2'
        resource = self.api.getChild('/error/2', request)
        d = _render(resource, request)

        def rendered(ignored):
            response = ''.join(request.written)
            self.assertEquals(INTERNAL_SERVER_ERROR, request.responseCode)
            self.assertEquals("Unknown exception.", response)
        d.addCallback(rendered)
        return d

    def test_processing_fails_http(self):
        request = DiabloDummyRequest([])
        request.method = 'GET'
        request.path = '/error/3'
        resource = self.api.getChild('/error/3', request)
        d = _render(resource, request)

        def rendered(ignored):
            response = ''.join(request.written)
            self.assertEquals(CONFLICT, request.responseCode)
            self.assertEquals("There's a conflict my friend.", response)
        d.addCallback(rendered)
        return d

    def test_processing_fails_unknown(self):
        request = DiabloDummyRequest([])
        request.method = 'GET'
        request.path = '/error/4'
        resource = self.api.getChild('/error/4', request)
        d = _render(resource, request)

        def rendered(ignored):
            response = ''.join(request.written)
            self.assertEquals(INTERNAL_SERVER_ERROR, request.responseCode)
            self.assertEquals("Bad type.", response)
        d.addCallback(rendered)
        return d
Example #4
0
class ResourceRoutingTest(unittest.TestCase):

    def setUp(self):
        self.api = RESTApi(routes)

    def test_basic_route(self):
        request = DiabloDummyRequest([''])
        request.path = '/a/useless/path'
        request.headers = {'content-type': 'application/json'}
        resource = self.api.getChild('/ignored', request)
        d = _render(resource, request)

        def rendered(ignored):
            response = ''.join(request.written)
            response_obj = json.loads(response)
            self.assertEquals(response_obj, {'nothing': 'something'})
        d.addCallback(rendered)
        return d

    def test_re_group_route_wo_group(self):
        request = DiabloDummyRequest([''])
        request.path = '/a/useful/path'
        request.headers = {'content-type': 'application/json'}
        resource = self.api.getChild('/ignored', request)
        d = _render(resource, request)

        def rendered(ignored):
            response = ''.join(request.written)
            response_obj = json.loads(response)
            self.assertEquals(response_obj, {'something': 'nothing'})
        d.addCallback(rendered)
        return d

    def test_re_group_route_w_group(self):
        request = DiabloDummyRequest([''])
        request.path = '/a/useful/path/1234567890'
        request.headers = {'content-type': 'application/json'}
        resource = self.api.getChild('/ignored', request)
        d = _render(resource, request)

        def rendered(ignored):
            response = ''.join(request.written)
            response_obj = json.loads(response)
            self.assertEquals(response_obj, {'something': 'nothing'})
        d.addCallback(rendered)
        return d

    def test_re_group_route_w_invalid_group(self):
        request = DiabloDummyRequest([''])
        request.path = '/a/useful/path/1invalid01'
        request.headers = {'content-type': 'application/json'}
        resource = self.api.getChild('/ignored', request)
        d = _render(resource, request)

        def rendered(ignored):
            log.msg('ignored', ignored)
            self.assertEquals(request.responseCode, NotFound().code)
        d.addCallback(rendered)
        return d
Example #5
0
class UrlFormatTestCase(unittest.TestCase):

    def setUp(self):
        self.api = RESTApi(routes)

    def test_json_url(self):
        request = DiabloDummyRequest([])
        request.path = '/testregular.json'
        resource = self.api.getChild('/testregular', request)
        d = _render(resource, request)

        def rendered(ignored):
            response = ''.join(request.written)
            log.msg('Response', response)
            response_obj = jsonMapper._parse_data(response, 'utf-8')
            self.assertEquals(response_obj, regular_result)
            content_header = request.outgoingHeaders.get('content-type', None)
            content_type = content_header.split(';')[0] if content_header else None
            self.assertEquals(content_type, 'application/json')
        d.addCallback(rendered)
        return d

    def test_xml_url(self):
        request = DiabloDummyRequest([])
        request.path = '/testregular.xml'
        resource = self.api.getChild('/testregular', request)
        d = _render(resource, request)

        def rendered(ignored):
            response = ''.join(request.written)
            log.msg('Response', response)
            response_obj = xmlMapper._parse_data(response, 'utf-8')
            self.assertEquals(response_obj, regular_result)
            content_header = request.outgoingHeaders.get('content-type', None)
            content_type = content_header.split(';')[0] if content_header else None
            self.assertEquals(content_type, 'text/xml')
        d.addCallback(rendered)
        return d

    def test_yaml_url(self):
        request = DiabloDummyRequest([])
        request.method = 'GET'
        request.path = '/testregular.yaml'
        resource = self.api.getChild('/testregular', request)
        d = _render(resource, request)

        def rendered(ignored):
            response = ''.join(request.written)
            log.msg('Response', response)
            response_obj = yamlMapper._parse_data(response, 'utf-8')
            self.assertEquals(response_obj, regular_result)
            content_header = request.outgoingHeaders.get('content-type', None)
            content_type = content_header.split(';')[0] if content_header else None
            self.assertEquals(content_type, 'application/yaml')
        d.addCallback(rendered)
        return d
Example #6
0
class DeleteResourceTest(unittest.TestCase):

    def setUp(self):
        self.api = RESTApi(routes)

    def _put_something(self, key, val):
        request = DiabloDummyRequest([''])
        request.method = 'PUT'
        request.path = '/a/test/resource'
        request.headers = {'content-type': 'application/json'}
        request.data = json.dumps({key: val})
        resource = self.api.getChild('/ignored', request)
        d = _render(resource, request)

        def rendered(ignored):
            self.assertEquals(request.responseCode, OK)
        d.addCallback(rendered)
        return d

    def _delete_it(self, ignored, key):
        request = DiabloDummyRequest([''])
        request.method = 'DELETE'
        request.path = '/a/test/resource/key3'
        request.headers = {'content-type': 'application/json'}
        resource = self.api.getChild('/ignored', request)
        d = _render(resource, request)

        def rendered(ignored):
            self.assertEquals(request.responseCode, OK)
        d.addCallback(rendered)
        return d

    def _get_it(self, ignored, key):
        request = DiabloDummyRequest([''])
        request.path = '/a/test/resource/key3'
        request.headers = {'content-type': 'application/json'}
        resource = self.api.getChild('/ignored', request)
        d = _render(resource, request)

        def rendered(ignored):
            self.assertEquals(request.responseCode, NotFound().code)
        d.addCallback(rendered)

    def test_delete_resource(self):
        key, val = 'key3', 'val3'
        d = self._put_something(key, val)
        d.addCallback(self._delete_it, key)
        d.addCallback(self._get_it, key)
        return d
Example #7
0
 def setUp(self):
     self.api = RESTApi(routes)
Example #8
0
 def setUp(self):
     self.api = RESTApi(routes)
     register_authenticator(self.check_creds)
Example #9
0
class AuthResourceTest(unittest.TestCase):
    passwd = {
        'jedi': 'jedixx',
        'sith': 'sithxx'
    }

    def check_creds(self, username, password):
        passwd = self.passwd.get(username, None)
        return passwd == password

    def get_auth_header(self, username, password):
        credsz = base64.b64encode('%s:%s' % (username, password))
        return {
            'authorization': 'Basic %s' % (credsz,)
        }

    def setUp(self):
        self.api = RESTApi(routes)
        register_authenticator(self.check_creds)

    def test_get_unaccessible(self):
        request = DiabloDummyRequest([''])
        request.method = 'GET'
        request.path = '/auth/unaccessible'
        resource = self.api.getChild('/', request)
        d = _render(resource, request)

        def renderer(ignored):
            self.assertEquals(request.responseCode, 403)

        d.addCallback(renderer)

    def test_get_jedi_fail(self):
        request = DiabloDummyRequest([''])
        request.method = 'GET'
        request.path = '/auth/normal'
        request.headers = self.get_auth_header('jedi', 'jedi')

        resource = self.api.getChild('/', request)
        d = _render(resource, request)

        def renderer(ignored):
            self.assertEquals(403, request.responseCode)

        d.addCallback(renderer)

    def test_get_jedi_ok(self):
        request = DiabloDummyRequest([''])
        request.method = 'GET'
        request.path = '/auth/normal'
        request.headers = self.get_auth_header('jedi', 'jedixx')

        resource = self.api.getChild('/', request)
        d = _render(resource, request)

        def renderer(ignored):
            self.assertEquals(200, request.responseCode)

        d.addCallback(renderer)

    def test_get_anonymous(self):
        request = DiabloDummyRequest([''])
        request.method = 'GET'
        request.path = '/auth/normal'
        # request.headers = self.get_auth_header('jedi', 'jedixx')

        resource = self.api.getChild('/', request)
        d = _render(resource, request)

        def renderer(ignored):
            self.assertEquals(401, request.responseCode)

        d.addCallback(renderer)