Ejemplo n.º 1
0
    def test_insert_secret_with_system_multicall(self):
        # create client with secret
        secret = "hello"
        client = Client(secret=secret)

        responses.add_callback(responses.POST,
                               client.server,
                               callback=self.call_params_callback)

        # create params
        params = [[
            {
                "methodName": client.ADD_URI,
                "params": ["param1", "param2"]
            },
            {
                "methodName": client.ADD_URI,
                "params": ["param3", "param4"]
            },
        ]]
        # copy params and insert secret
        expected_params = deepcopy(params)
        for param in expected_params[0]:
            param["params"].insert(0, f"token:{secret}")

        # call function and assert result
        resp = client.call(client.MULTICALL, params, insert_secret=True)
        assert resp == expected_params
Ejemplo n.º 2
0
    def test_insert_secret_with_multicall2(self):
        # create client with secret
        secret = "hello"
        client = Client(secret=secret)

        responses.add_callback(responses.POST,
                               client.server,
                               callback=self.call_params_callback)

        # create params
        params_1 = ["2089b05ecca3d829"]
        params_2 = ["2fa07b6e85c40205"]
        calls = [(client.REMOVE, params_1), (client.REMOVE, params_2)]
        # copy params and insert secret
        expected_params = [[
            {
                "methodName": client.REMOVE,
                "params": deepcopy(params_1)
            },
            {
                "methodName": client.REMOVE,
                "params": deepcopy(params_2)
            },
        ]]
        for param in expected_params[0]:
            param["params"].insert(0, f"token:{secret}")

        # call function and assert result
        resp = client.multicall2(calls, insert_secret=True)
        assert resp == expected_params
Ejemplo n.º 3
0
    def test_multicall2(self):
        client = Client()

        responses.add_callback(responses.POST,
                               client.server,
                               callback=self.call_params_callback)

        # create params
        params_1 = ["2089b05ecca3d829"]
        params_2 = ["2fa07b6e85c40205"]
        calls = [(client.REMOVE, params_1), (client.REMOVE, params_2)]
        # copy params and insert secret
        expected_params = [[
            {
                "methodName": client.REMOVE,
                "params": deepcopy(params_1)
            },
            {
                "methodName": client.REMOVE,
                "params": deepcopy(params_2)
            },
        ]]

        # call function and assert result
        resp = client.multicall2(calls)
        assert resp == expected_params
Ejemplo n.º 4
0
 def test_call_raises_custom_error(self):
     client = Client()
     responses.add(
         responses.POST, client.server, json={"error": {"code": 1, "message": "Custom message"}}, status=200
     )
     with pytest.raises(ClientException, match=r"Custom message") as e:
         client.call("aria2.method")
         assert e.code == 1
Ejemplo n.º 5
0
 def test_call_raises_known_error(self):
     client = Client()
     responses.add(
         responses.POST,
         client.server,
         json={"error": {"code": JSONRPC_PARSER_ERROR, "message": "Custom message"}},
         status=200,
     )
     with pytest.raises(ClientException, match=rf"{JSONRPC_CODES[JSONRPC_PARSER_ERROR]}\nCustom message") as e:
         client.call("aria2.method")
         assert e.code == JSONRPC_PARSER_ERROR
Ejemplo n.º 6
0
    def test_does_not_insert_secret_if_told_so(self):
        # create client with secret
        secret = "hello"
        client = Client(secret=secret)

        responses.add_callback(responses.POST, client.server, callback=self.call_params_callback)

        # create params
        params = ["param1", "param2"]

        # call function and assert result
        resp = client.call("other.method", params, insert_secret=False)
        assert secret not in resp
Ejemplo n.º 7
0
    def test_batch_call(self):
        client = Client()

        responses.add_callback(responses.POST, client.server, callback=self.batch_call_params_callback)

        # create params
        params_1 = ["param1", "param2"]
        params_2 = ["param3", "param4"]
        # copy params and insert secret
        expected_params = [params_1, params_2]

        # call function and assert result
        resp = client.batch_call([(client.ADD_URI, params_1, 0), (client.ADD_METALINK, params_2, 1)])
        assert resp == expected_params
Ejemplo n.º 8
0
    def test_insert_secret_with_aria2_method_call(self):
        # create client with secret
        secret = "hello"
        client = Client(secret=secret)

        responses.add_callback(responses.POST, client.server, callback=self.call_params_callback)

        # create params
        params = ["param1", "param2"]
        # copy params and insert secret
        expected_params = deepcopy(params)
        expected_params.insert(0, secret)

        # call function and assert result
        resp = client.call(client.ADD_URI, params, insert_secret=True)
        assert resp == expected_params
Ejemplo n.º 9
0
    def test_insert_secret_with_batch_call(self):
        # create client with secret
        secret = "hello"
        client = Client(secret=secret)

        responses.add_callback(responses.POST, client.server, callback=self.batch_call_params_callback)

        # create params
        params_1 = ["param1", "param2"]
        params_2 = ["param3", "param4"]
        # copy params and insert secret
        expected_params = [deepcopy(params_1), deepcopy(params_2)]
        for p in expected_params:
            p.insert(0, secret)

        # call function and assert result
        resp = client.batch_call(
            [(client.ADD_URI, params_1, 0), (client.ADD_METALINK, params_2, 1)], insert_secret=True
        )
        assert resp == expected_params
Ejemplo n.º 10
0
    def __init__(self, tmp_dir, port, config=None, session=None, secret=""):
        self.tmp_dir = tmp_dir
        self.port = port

        # create the command used to launch an aria2c process
        command = [
            "aria2c",
            f"--dir={self.tmp_dir}",
            "--file-allocation=none",
            "--quiet",
            "--enable-rpc=true",
            f"--rpc-listen-port={self.port}",
        ]
        if config:
            command.append(f"--conf-path={config}")
        else:
            # command.append("--no-conf")
            config = CONFIGS_DIR / "default.conf"
            command.append(f"--conf-path={config}")
        if session:
            if isinstance(session, list):
                session_path = self.tmp_dir / "_session.txt"
                with open(session_path, "w") as stream:
                    stream.write("\n".join(session))
                command.append(f"--input-file={session_path}")
            else:
                session_path = SESSIONS_DIR / session
                if not session_path.exists():
                    raise ValueError(f"no such session: {session}")
                command.append(f"--input-file={session_path}")
        if secret:
            command.append(f"--rpc-secret={secret}")

        self.command = command
        self.process = None

        # create the client with port
        self.client = Client(port=self.port, secret=secret, timeout=20)

        # create the API instance
        self.api = API(self.client)
Ejemplo n.º 11
0
    def __init__(self,
                 host=DEFAULT_HOST,
                 port=DEFAULT_PORT,
                 secret="",
                 poll=1.0,
                 global_options=None):  # nosec
        # type: (str, int, str, float, Optional[dict]) -> None
        """Initialize the aria2 client with `host`, `port` and `secret`.
        Poll aria2 every `poll` seconds.
        """

        self.aria2 = Client(host, port, secret)
        self.poll = poll

        if global_options:
            global_options = self.default_global_options.copy()
            global_options.update(global_options)
            self.aria2.change_global_option(global_options)
        else:
            self.aria2.change_global_option(self.default_global_options)

        self.gids = set()  # type: Set[str]
Ejemplo n.º 12
0
 def test_listen_to_notifications_no_server(self):
     client = Client(port=7035)
     client.listen_to_notifications(timeout=1)
Ejemplo n.º 13
0
 def test_client_str_returns_client_server(self):
     host = "https://example.com/"
     port = 7100
     client = Client(host, port)
     assert client.server == f"{host.rstrip('/')}:{port}/jsonrpc" == str(
         client)
Ejemplo n.º 14
0
def test_listen_to_notifications_then_stop(port):
    api = API(Client(port=port))
    api.listen_to_notifications(threaded=True, timeout=1)
    api.stop_listening()
    assert api.listener is None