Exemplo n.º 1
0
async def test_login(response, exception, exp_exception, mocker):
    client = qbittorrent.QbittorrentClientApi(config={
        'username': '******',
        'password': '******',
        'foo': 'bar',
    })
    if exception:
        mocker.patch.object(client, '_request',
                            AsyncMock(side_effect=exception))
    else:
        mocker.patch.object(client, '_request',
                            AsyncMock(return_value=response))

    if exp_exception:
        with pytest.raises(type(exp_exception),
                           match=rf'^{re.escape(str(exp_exception))}$'):
            await client._login()
    else:
        await client._login()

    assert client._request.call_args_list == [
        call(
            path='auth/login',
            data={
                'username': '******',
                'password': '******'
            },
        )
    ]
Exemplo n.º 2
0
async def test_session(mocker):
    client = qbittorrent.QbittorrentClientApi()
    mocker.patch.object(client, '_login', AsyncMock())
    mocker.patch.object(client, '_logout', AsyncMock())
    assert client._login.call_args_list == []
    assert client._logout.call_args_list == []
    async with client._session():
        assert client._login.call_args_list == [call()]
        assert client._logout.call_args_list == []
    assert client._login.call_args_list == [call()]
    assert client._logout.call_args_list == [call()]
Exemplo n.º 3
0
async def test_request(config_url, path, exp_request_url, mocker):
    post_mock = mocker.patch('upsies.utils.http.post', AsyncMock())
    client = qbittorrent.QbittorrentClientApi(config={'url': config_url})
    data = 'mock data'
    files = 'mock files'
    await client._request(path, data=data, files=files)
    assert post_mock.call_args_list == [
        call(url=exp_request_url,
             headers=client._headers,
             data=data,
             files=files)
    ]
Exemplo n.º 4
0
async def test_get_torrent_hash(exception, exp_exception, mocker):
    torrent_path = 'path/to/file.torrent'
    torrent_hash = 'D34DB33F'
    client = qbittorrent.QbittorrentClientApi()
    mocker.patch.object(
        client,
        'read_torrent',
        side_effect=exception,
        return_value=Mock(infohash=torrent_hash),
    )
    if exp_exception:
        with pytest.raises(type(exp_exception),
                           match=rf'^{re.escape(str(exp_exception))}$'):
            client._get_torrent_hash(torrent_path)
    else:
        assert client._get_torrent_hash(torrent_path) == torrent_hash
    assert client.read_torrent.call_args_list == [call(torrent_path)]
Exemplo n.º 5
0
async def test_add_torrent_handles_RequestError_from_adding(
        check_after_add, download_path, response, exception, exp_exception,
        mocker):
    torrent_path = 'path/to/file.torrent'
    torrent_hash = 'D34DB33F'
    client = qbittorrent.QbittorrentClientApi(
        config={'check_after_add': check_after_add})

    mocker.patch.object(client, '_login', AsyncMock())
    mocker.patch.object(client, '_logout', AsyncMock())

    if exception:
        mocker.patch.object(client, '_request',
                            AsyncMock(side_effect=exception))
    else:
        mocker.patch.object(client, '_request',
                            AsyncMock(return_value=response))
    mocker.patch.object(client, '_get_torrent_hash',
                        Mock(return_value=torrent_hash))

    coro = client.add_torrent(torrent_path, download_path=download_path)
    if exp_exception:
        with pytest.raises(type(exp_exception),
                           match=rf'^{re.escape(str(exp_exception))}$'):
            await coro
        assert client._get_torrent_hash.call_args_list == []
    else:
        assert await coro == torrent_hash
        assert client._get_torrent_hash.call_args_list == [call(torrent_path)]

    exp_files = {
        'torrents': {
            'file': 'path/to/file.torrent',
            'mimetype': 'application/x-bittorrent',
        },
    }
    exp_data = {
        'skip_checking': 'false' if check_after_add else 'true',
    }
    if download_path:
        exp_data['savepath'] = os.path.abspath(download_path)
    assert client._request.call_args_list == [
        call('torrents/add', files=exp_files, data=exp_data)
    ]
Exemplo n.º 6
0
async def test_add_torrent_handles_RequestError_from_authentication(
        login_exception, logout_exception, exp_exception, mocker):
    client = qbittorrent.QbittorrentClientApi()
    mocker.patch.object(client, '_login',
                        AsyncMock(side_effect=login_exception))
    mocker.patch.object(client, '_logout',
                        AsyncMock(side_effect=logout_exception))
    mocker.patch.object(client, '_request', AsyncMock(return_value='Ok.'))
    mocker.patch.object(client, '_get_torrent_hash',
                        Mock(return_value='D34DB33F'))

    coro = client.add_torrent('path/to/file.torrent')
    if exp_exception:
        with pytest.raises(type(exp_exception),
                           match=rf'^{re.escape(str(exp_exception))}$'):
            await coro
    else:
        assert await coro == 'D34DB33F'

    assert client._login.call_args_list == [call()]
    if not login_exception:
        assert client._logout.call_args_list == [call()]
    else:
        assert client._logout.call_args_list == []
Exemplo n.º 7
0
def test_attributes(config, exp_headers):
    client = qbittorrent.QbittorrentClientApi(config=config)
    assert client._headers == exp_headers
Exemplo n.º 8
0
async def test_logout(mocker):
    client = qbittorrent.QbittorrentClientApi()
    mocker.patch.object(client, '_request', AsyncMock())
    await client._logout()
    assert client._request.call_args_list == [call(path='auth/logout')]