Example #1
0
    def request(self,
                body='',
                uri=b'https://www.globaleaks.org/',
                user_id=None,
                role=None,
                multilang=False,
                headers=None,
                client_addr=None,
                handler_cls=None,
                attached_file=None,
                kwargs={}):
        """
        Constructs a handler for preforming mock requests using the bag of params described below.
        """
        from globaleaks.rest import api

        if handler_cls is None:
            handler_cls = self._handler

        request = forge_request(uri=uri,
                                headers=headers,
                                body=body,
                                client_addr=client_addr,
                                method=b'GET')

        x = api.APIResourceWrapper()
        x.preprocess(request)

        if not getattr(handler_cls, 'decorated', False):
            for method in ['get', 'post', 'put', 'delete']:
                if getattr(handler_cls, method, None) is not None:
                    decorators.decorate_method(handler_cls, method)
                    handler_cls.decorated = True

        handler = handler_cls(self.state, request, **kwargs)

        if multilang:
            request.language = None

        if user_id is None and role is not None:
            if role == 'admin':
                user_id = self.dummyAdminUser['id']
            elif role == 'receiver':
                user_id = self.dummyReceiverUser_1['id']
            elif role == 'custodian':
                user_id = self.dummyCustodianUser['id']

        if headers is not None and headers.get('x-session', None) is not None:
            handler.request.headers[b'x-session'] = headers.get(
                'x-session').encode()

        elif role is not None:
            session = Sessions.new(1, user_id, role, False, USER_PRV_KEY)
            handler.request.headers[b'x-session'] = session.id.encode()

        if handler.upload_handler:
            handler.uploaded_file = self.get_dummy_file(
                u'upload.raw', attached_file)

        return handler
Example #2
0
    def setUp(self):
        yield TestGL.setUp(self)

        self.api = api.APIResourceWrapper()

        yield tw(db_update_enabled_languages, 1, ['en', 'ar', 'it'], 'en')
        yield refresh_memory_variables()
Example #3
0
    def setUp(self):
        yield TestGL.setUp(self)

        from globaleaks.rest import api
        self.api = api.APIResourceWrapper()

        yield update_enabled_languages(1, ['en', 'ar', 'it'], 'en')
        yield refresh_memory_variables()
Example #4
0
    def request(self,
                body='',
                uri='https://www.globaleaks.org/',
                user_id=None,
                role=None,
                multilang=False,
                headers=None,
                client_addr=None,
                method='GET',
                handler_cls=None,
                attached_file={},
                kwargs={}):
        """
        Constructs a handler for preforming mock requests using the bag of params described below.
        """
        from globaleaks.rest import api

        if handler_cls is None:
            handler_cls = self._handler

        request = forge_request(uri=uri,
                                headers=headers,
                                body=body,
                                client_addr=client_addr,
                                method='GET',
                                attached_file=attached_file)

        x = api.APIResourceWrapper()
        x.preprocess(request)

        if not getattr(handler_cls, 'decorated', False):
            for method in ['get', 'post', 'put', 'delete']:
                if getattr(handler_cls, method, None) is not None:
                    api.decorate_method(handler_cls, method)
                    handler_cls.decorated = True

        handler = handler_cls(State, request, **kwargs)

        if multilang:
            request.language = None

        if user_id is None and role is not None:
            if role == 'admin':
                user_id = self.dummyAdminUser['id']
            elif role == 'receiver':
                user_id = self.dummyReceiverUser_1['id']
            elif role == 'custodian':
                user_id = self.dummyCustodianUser['id']

        if role is not None:
            session = new_session(user_id, role, 'enabled')
            handler.request.headers['x-session'] = session.id

        return handler
Example #5
0
    def setUp(self):
        yield TestGL.setUp(self)

        from globaleaks.rest import api
        self.api = api.APIResourceWrapper()
Example #6
0
    def request(self,
                jbody=None,
                user_id=None,
                role=None,
                headers=None,
                body='',
                path=None,
                remote_ip='0.0.0.0',
                method='MOCK',
                handler_cls=None,
                attached_file={},
                kwargs={}):
        """
        Constructs a handler for preforming mock requests using the bag of params described below.

        Args:

            jbody:
                The body of the request as a dict (it will be automatically
                converted to string)

            body:
                The body of the request as a string

            user_id:
                when simulating authentication the session should be bound
                to a certain user_id.

            role:
                when simulating authentication the session should be bound
                to a certain role.

            method:
                HTTP method, e.g. "GET" or "POST"

            headers:
                Dict of headers to pass on the request

            remote_ip:
                If a particular remote_ip should be set.

            handler_cls:
                The type of handler that will respond to the request. If this is not set self._handler is used.

            attached_file:
                A dict to place in the request.args.files obj
        """
        if jbody and not body:
            body = json.dumps(jbody)
        elif body and jbody:
            raise ValueError('jbody and body in conflict')

        if handler_cls is None:
            handler_cls = self._handler

        request = DummyRequest([''])

        def getResponseBody():
            return ''.join(request.written)

        request.path = ''
        request.code = 200
        request.language = 'en'
        request.client_ip = '127.0.0.1'
        request.client_proto = 'https'
        request.client_using_tor = False

        request.getResponseBody = getResponseBody

        request.client = IPv4Address('TCP', '1.2.3.4', 12345)

        request.args = {}
        if attached_file is not None:
            request.args = {'file': [attached_file]}

        if headers is not None:
            for k, v in headers.iteritems():
                request.requestHeaders.setRawHeaders(bytes(k), [bytes(v)])

        request.headers = request.getAllHeaders()

        from globaleaks.rest import api
        x = api.APIResourceWrapper()
        x.preprocess(request)

        if path is not None:
            if not path.startswith('/'):
                raise ValueError('Must pass a valid url path')
            request.path = path

        class fakeBody(object):
            def read(self):
                return body

            def close(self):
                pass

        request.content = fakeBody()

        from globaleaks.rest.api import decorate_method
        if not getattr(handler_cls, 'decorated', False):
            for method in ['get', 'post', 'put', 'delete']:
                if getattr(handler_cls, method, None) is not None:
                    decorate_method(handler_cls, method)
                    handler_cls.decorated = True

        handler = handler_cls(request, **kwargs)

        if user_id is None and role is not None:
            if role == 'admin':
                user_id = self.dummyAdminUser['id']
            elif role == 'receiver':
                user_id = self.dummyReceiverUser_1['id']
            elif role == 'custodian':
                user_id = self.dummyCustodianUser['id']

        if role is not None:
            session = GLSession(user_id, role, 'enabled')
            handler.request.headers['x-session'] = session.id

        return handler