Exemplo n.º 1
0
    def test_auth_resp_invalid(self):
        con = self._with_pck((MSG_AUTH_RESP, dict(status=False)))

        p = Protocol(con, "foo")
        p._loop()

        ok_(not p.authenticated)
Exemplo n.º 2
0
    def test_wait_for_auth(self):
        con = self._connection()
        p = Protocol(con, "foo")
        p._authed = True
        p._authed_event.set()

        p._wait_for_auth()
Exemplo n.º 3
0
    def test_wait_for_auth(self):
        con = self._connection()
        p = Protocol(con, "foo")
        p._authed = True
        p._authed_event.set()

        p._wait_for_auth()
Exemplo n.º 4
0
    def test_auth_resp_invalid(self):
        con = self._with_pck((MSG_AUTH_RESP, dict(status=False)))

        p = Protocol(con, "foo")
        p._loop()

        ok_(not p.authenticated)
Exemplo n.º 5
0
    def test_responds_to_challenge(self, p):
        con = self._with_pck((MSG_CHALLENGE, dict(challenge="0" * 64)))
        p = Protocol(con, "foo")

        p._loop()

        con.send_packet.assert_called_once_with(
            MSG_AUTH, dict(signature=sentinel.signed))
Exemplo n.º 6
0
    def test_accept_auth_resp(self):
        con = self._with_pck((MSG_AUTH_RESP, dict(status=True)))

        p = Protocol(con, "foo")

        p._loop()

        ok_(p.authenticated)
Exemplo n.º 7
0
    def test_accept_auth_resp(self):
        con = self._with_pck((MSG_AUTH_RESP, dict(status=True)))

        p = Protocol(con, "foo")

        p._loop()

        ok_(p.authenticated)
Exemplo n.º 8
0
    def test_responds_to_challenge(self, p):
        con = self._with_pck((MSG_CHALLENGE, dict(challenge="0" * 64)))
        p = Protocol(con, "foo")

        p._loop()

        con.send_packet.assert_called_once_with(
            MSG_AUTH,  dict(signature=sentinel.signed)
        )
Exemplo n.º 9
0
    def test_stop(self):
        con = self._connection()
        p = Protocol(con, "foo")
        th = p._th = Mock()

        p.stop()

        con.stop.assert_called_once()
        th.join.assert_called_once()
        th.kill.assert_called_once()
Exemplo n.º 10
0
    def test_stop(self):
        con = self._connection()
        p = Protocol(con, "foo")
        th = p._th = Mock()

        p.stop()

        con.stop.assert_called_once()
        th.join.assert_called_once()
        th.kill.assert_called_once()
Exemplo n.º 11
0
    def _start(self):
        serv, cli, clifd = get_sockets()
        secret = "__NO_AUTH__"
        if self._secured:  # pragma: nocover
            raise RuntimeError("Secure connections are not supported yet")

        self._proc = open_process(clifd, secret)
        self._con = Connection(serv)
        self._proto = Protocol(self._con,
                               None if not self._secured else secret)
        self._proto.start()
Exemplo n.º 12
0
    def test_request_async(self):
        con = self._with_pck((MSG_RESPONSE, self._response_data))

        p = Protocol(con, "foo")
        p._authed = True

        args = dict(somearg=sentinel.somearg)
        handle = p.request(sentinel.mtd, **args)

        p._loop()

        # calling handle should wait the response
        ret = handle()

        eq_(ret, "foo")
        con.send_packet.assert_called_once_with(MSG_REQUEST, {
            "reqid": 1,
            "action": sentinel.mtd,
            "args": args
            })
Exemplo n.º 13
0
    def _start(self):
        serv, cli, clifd = get_sockets()
        secret = "__NO_AUTH__"
        if self._secured: # pragma: nocover
            raise RuntimeError("Secure connections are not supported yet")

        self._proc = open_process(clifd, secret)
        self._con = Connection(serv)
        self._proto = Protocol(self._con,
                               None if not self._secured else secret)
        self._proto.start()
Exemplo n.º 14
0
    def test_request_sync(self):
        con = self._with_pck((MSG_RESPONSE, self._response_data))

        p = Protocol(con, "foo")
        p._authed = True

        def fn():
            time.sleep(0.001)
            p._loop()

        th = gevent.Greenlet(fn)
        th.start()

        args = dict(somearg=sentinel.somearg)
        ret = p.request_sync(sentinel.mtd, timeout=1, **args)

        eq_(ret, "foo")
        con.send_packet.assert_called_once_with(MSG_REQUEST, {
            "reqid": 1,
            "action": sentinel.mtd,
            "args": args
            })
        th.join()
Exemplo n.º 15
0
    def test_request_sync(self):
        con = self._with_pck((MSG_RESPONSE, self._response_data))

        p = Protocol(con, "foo")
        p._authed = True

        def fn():
            time.sleep(0.001)
            p._loop()

        th = gevent.Greenlet(fn)
        th.start()

        args = dict(somearg=sentinel.somearg)
        ret = p.request_sync(sentinel.mtd, timeout=1, **args)

        eq_(ret, "foo")
        con.send_packet.assert_called_once_with(MSG_REQUEST, {
            "reqid": 1,
            "action": sentinel.mtd,
            "args": args
        })
        th.join()
Exemplo n.º 16
0
    def test_request_async(self):
        con = self._with_pck((MSG_RESPONSE, self._response_data))

        p = Protocol(con, "foo")
        p._authed = True

        args = dict(somearg=sentinel.somearg)
        handle = p.request(sentinel.mtd, **args)

        p._loop()

        # calling handle should wait the response
        ret = handle()

        eq_(ret, "foo")
        con.send_packet.assert_called_once_with(MSG_REQUEST, {
            "reqid": 1,
            "action": sentinel.mtd,
            "args": args
        })
Exemplo n.º 17
0
class Client(object):
    """Primary class for interfacing with node

    The client class provides an easy to use interface for calling
    node functions, handling context and requirements.
    """

    def __init__(self, secured=False):
        """Spawns a new node process and sets up communication"""
        self._secured = secured
        self._start()

    def _start(self):
        serv, cli, clifd = get_sockets()
        secret = "__NO_AUTH__"
        if self._secured: # pragma: nocover
            raise RuntimeError("Secure connections are not supported yet")

        self._proc = open_process(clifd, secret)
        self._con = Connection(serv)
        self._proto = Protocol(self._con,
                               None if not self._secured else secret)
        self._proto.start()

    def eval(self, code, context=None):
        """Evaluates the code and returns the result

        Evaluates the passed in string as JavaScript code. This code
        may optionally be run in a context.

        :param code: The code to be evaluated
        :type code: str
        :param context: Name of the context to run the code in.
        :type context: str
        :return: The result of evaluating the expression, as best
                 represented in python.
        """

        return self._proto.request_sync("eval", code=code, context=context)

    def stop(self):
        """Stops the client and terminates the node process"""
        self._proto.stop()
        self._proc.terminate()

    def context(self, name, reqs=[]):
        """Creates a named context

        Creates a named context with the passed in requirements
        pre-included, the context will have it's own global object
        making it hard to interfere with other things running in node.

        :param name: Name of the context to be created
        :type name: str
        :param reqs: The names of the requirements to be loaded into
                     the context.
        :type reqs: list
        :return: a context object for the passed in name.
        :rtype: :class:`Context`
        """
        name = self._proto.request_sync("mkcontext",
                                        name=name,
                                        requirements=reqs)
        return Context(self, name)

    def get(self, path, context):
        """Gets the value of a javascript variable.

        Gets the value of a Javascript name/object path in the given
        context. The passed in path should be a list containing the
        path to the value that you want to read. For example a list
        with the elements 'console' and 'log' corresponds to
        "console.log".

        :param path: The path to the value to be retrived.
        :type path: list
        :return: The value of the object that the path points to.
        :rtype: :class:`JSError` or a JavaScript object.
        """
        return self._proto.request_sync("get", path=path, context=context)

    def set(self, path, val, context):
        """Sets the value of a javascript variable.

        Sets the value of a Javascript name/object path in the given
        context. The passed in path should be a list containing the
        path to teh value that you wish to set. For example a list
        with the elements 'console' and 'log' corresponds to
        "console.log". The new value has to be a convertable to JSON
        and thus javascript. Therefore nested dicts are fine but other
        objects will fail.

        :param path: The path to the value to be retrived.
        :type path: list
        :param val: The value to that the path should be set to
        :type val: int, str, list, tuple, dict, their subclasses
                   and nested structures comprising of these.

        :param context: The context to run the command in.
        :type context: str

        :return: The changed value as it is represented in JavaScript.
        :rtype: A JavaScript object.
        """
        return self._proto.request_sync("set", path=path,
                                        value=val,
                                        context=context)

    def call(self, path, args, context):
        """Calls the function at path with the passed-in args

        Calls the function specified by the path using the passed in
        arguments. Functions are always called in the context of the
        next to last part of the path. This ensures that the calling
        ["console","log"] is called in the context of console. The
        arguments should be a list or tuple each containing an object
        that is covertable to JSON.

        :param path: The path to the function to be called.
        :type path: list
        :param args: A list arguments to call the function with.
                     All the arguments have to convertable to JSON.
        :param context: The context containing the function
        :type context: str

        :return: The value returned by the function.
        :rtype: A JavaScript object.
        """
        return self._proto.request_sync("call", path=path,
                                        args=args,
                                        context=context)
Exemplo n.º 18
0
    def test_start(self):
        con = self._connection()

        p = Protocol(con, "foo")
        p.start()
        con.send_packet.assert_called_once_with(ord('h'), {"hello": "hello"})
Exemplo n.º 19
0
class Client(object):
    """Primary class for interfacing with node

    The client class provides an easy to use interface for calling
    node functions, handling context and requirements.
    """
    def __init__(self, secured=False):
        """Spawns a new node process and sets up communication"""
        self._secured = secured
        self._start()

    def _start(self):
        serv, cli, clifd = get_sockets()
        secret = "__NO_AUTH__"
        if self._secured:  # pragma: nocover
            raise RuntimeError("Secure connections are not supported yet")

        self._proc = open_process(clifd, secret)
        self._con = Connection(serv)
        self._proto = Protocol(self._con,
                               None if not self._secured else secret)
        self._proto.start()

    def eval(self, code, context=None):
        """Evaluates the code and returns the result

        Evaluates the passed in string as JavaScript code. This code
        may optionally be run in a context.

        :param code: The code to be evaluated
        :type code: str
        :param context: Name of the context to run the code in.
        :type context: str
        :return: The result of evaluating the expression, as best
                 represented in python.
        """

        return self._proto.request_sync("eval", code=code, context=context)

    def stop(self):
        """Stops the client and terminates the node process"""
        self._proto.stop()
        self._proc.terminate()

    def context(self, name, reqs=[]):
        """Creates a named context

        Creates a named context with the passed in requirements
        pre-included, the context will have it's own global object
        making it hard to interfere with other things running in node.

        :param name: Name of the context to be created
        :type name: str
        :param reqs: The names of the requirements to be loaded into
                     the context.
        :type reqs: list
        :return: a context object for the passed in name.
        :rtype: :class:`Context`
        """
        name = self._proto.request_sync("mkcontext",
                                        name=name,
                                        requirements=reqs)
        return Context(self, name)

    def get(self, path, context):
        """Gets the value of a javascript variable.

        Gets the value of a Javascript name/object path in the given
        context. The passed in path should be a list containing the
        path to the value that you want to read. For example a list
        with the elements 'console' and 'log' corresponds to
        "console.log".

        :param path: The path to the value to be retrived.
        :type path: list
        :return: The value of the object that the path points to.
        :rtype: :class:`JSError` or a JavaScript object.
        """
        return self._proto.request_sync("get", path=path, context=context)

    def set(self, path, val, context):
        """Sets the value of a javascript variable.

        Sets the value of a Javascript name/object path in the given
        context. The passed in path should be a list containing the
        path to teh value that you wish to set. For example a list
        with the elements 'console' and 'log' corresponds to
        "console.log". The new value has to be a convertable to JSON
        and thus javascript. Therefore nested dicts are fine but other
        objects will fail.

        :param path: The path to the value to be retrived.
        :type path: list
        :param val: The value to that the path should be set to
        :type val: int, str, list, tuple, dict, their subclasses
                   and nested structures comprising of these.

        :param context: The context to run the command in.
        :type context: str

        :return: The changed value as it is represented in JavaScript.
        :rtype: A JavaScript object.
        """
        return self._proto.request_sync("set",
                                        path=path,
                                        value=val,
                                        context=context)

    def call(self, path, args, context):
        """Calls the function at path with the passed-in args

        Calls the function specified by the path using the passed in
        arguments. Functions are always called in the context of the
        next to last part of the path. This ensures that the calling
        ["console","log"] is called in the context of console. The
        arguments should be a list or tuple each containing an object
        that is covertable to JSON.

        :param path: The path to the function to be called.
        :type path: list
        :param args: A list arguments to call the function with.
                     All the arguments have to convertable to JSON.
        :param context: The context containing the function
        :type context: str

        :return: The value returned by the function.
        :rtype: A JavaScript object.
        """
        return self._proto.request_sync("call",
                                        path=path,
                                        args=args,
                                        context=context)
Exemplo n.º 20
0
    def test_start(self):
        con = self._connection()

        p = Protocol(con, "foo")
        p.start()
        con.send_packet.assert_called_once_with(ord('h'), {"hello": "hello"})