1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
|
# -*- coding: utf-8 -*-
# 2008-12, Erik Svensson <erik.public@gmail.com>
# Licensed under the MIT license.
import os
import unittest
import base64
from six import iteritems, string_types, PY3
if PY3:
from urllib.parse import urlparse
else:
from urlparse import urlparse
import json
import transmissionrpc.constants
from transmissionrpc import TransmissionError, Client, HTTPHandler
def tree_differences(a, b):
return node_differences(a, b, '.')
def node_differences(a, b, root):
errors = []
if isinstance(a, dict) and isinstance(b, dict):
for k, v in iteritems(a):
node = root + '.' + k
if k not in b:
errors.append('Field %s missing from b at %s' % (k, node))
else:
errors.extend(node_differences(a[k], b[k], node))
for k, v in iteritems(b):
node = root + '.' + k
if k not in a:
errors.append('Field %s missing from a at %s' % (k, node))
elif isinstance(a, list) and isinstance(b, list):
for v in a:
if v not in b:
errors.append('Value %s missing from b at %s' % (v[0:32], root))
for v in b:
if v not in a:
errors.append('Value %s missing from a at %s' % (v[0:32], root))
else:
if a != b:
errors.append('Value %s != %s at %s' % (a[0:32], b[0:32], root))
return errors
class TestHTTPHandler(HTTPHandler):
def __init__(self, test_name=None):
self.url = None
self.user = None
self.password = None
self.tests = None
self.test_index = 0
if test_name:
test_file = test_name + '.json'
here = os.path.dirname(os.path.abspath(__file__))
test_path = os.path.join(here, 'data', test_file)
fd = open(test_path, 'r')
test_data = json.load(fd)
fd.close()
if 'test sequence' in test_data:
self.tests = test_data['test sequence']
def set_authentication(self, url, user, password):
urlo = urlparse(url)
if urlo.scheme == '':
raise ValueError('URL should have a scheme.')
else:
self.url = url
if user and password:
if isinstance(user, string_types):
self.user = user
else:
raise TypeError('Invalid type for user.')
if isinstance(password, string_types):
self.password = password
else:
raise TypeError('Invalid type for password.')
elif user or password:
raise ValueError('User AND password or neither.')
def request(self, url, query, headers, timeout):
response = {}
if self.url and self.url != url:
raise ValueError('New URL?!')
urlo = urlparse(url)
if urlo.scheme == '':
raise ValueError('URL should have a scheme.')
else:
self.url = url
q = json.loads(query)
if self.tests:
test_data = self.tests[self.test_index]
self.test_index += 1
errors = tree_differences(test_data['request'], q)
if len(errors) > 0:
errors = '\n\t'.join(errors)
raise Exception('Invalid request\n%s\n%s\n. Errors: %s\n' % (json.dumps(q, indent=2), json.dumps(test_data['request'], indent=2), errors))
if 'response' in test_data:
response = test_data['response']
else:
response['tag'] = int(q['tag'])
response['result'] = 'success'
return json.dumps(response)
def createClient(*args, **kwargs):
test_name = None
if 'test_name' in kwargs:
test_name = kwargs['test_name']
del kwargs['test_name']
kwargs['http_handler'] = TestHTTPHandler(test_name)
return Client(*args, **kwargs)
class ClientTest(unittest.TestCase):
def testConstruction(self):
tc = createClient(test_name='construction')
self.assertEqual(tc.url, 'http://localhost:%d/transmission/rpc' % (transmissionrpc.constants.DEFAULT_PORT))
tc = createClient('127.0.0.1', 7000, user='user', password='secret', test_name='construction')
self.assertEqual(tc.url, 'http://127.0.0.1:7000/transmission/rpc')
tc = createClient('127.0.0.1', 7000, user='user', password='secret', test_name='construction')
self.assertEqual(tc.url, 'http://127.0.0.1:7000/transmission/rpc')
tc = createClient('127.0.0.1', 7000, user='user', test_name='construction')
self.assertEqual(tc.url, 'http://127.0.0.1:7000/transmission/rpc')
tc = createClient('127.0.0.1', 7000, password='secret', test_name='construction')
self.assertEqual(tc.url, 'http://127.0.0.1:7000/transmission/rpc')
tc = createClient('127.0.0.1', 7000, password='secret', timeout=0.1, test_name='construction')
self.assertEqual(tc.url, 'http://127.0.0.1:7000/transmission/rpc')
self.assertEqual(tc.timeout, 0.1)
tc = createClient('127.0.0.1', 7000, password='secret', timeout=10, test_name='construction')
self.assertEqual(tc.timeout, 10.0)
tc = createClient('127.0.0.1', 7000, password='secret', timeout=10, test_name='construction')
self.assertEqual(tc.timeout, 10.0)
def testTimeoutProperty(self):
tc = createClient('127.0.0.1', 12345, timeout=10, test_name='construction')
self.assertEqual(tc.timeout, 10.0)
tc.timeout = 0.1
self.assertEqual(tc.timeout, 0.1)
tc.timeout = 100
self.assertEqual(tc.timeout, 100.0)
tc.timeout = 100
self.assertEqual(tc.timeout, 100.0)
del tc.timeout
self.assertEqual(tc.timeout, transmissionrpc.constants.DEFAULT_TIMEOUT)
tc.timeout = '100.1'
self.assertEqual(tc.timeout, 100.1)
self.failUnlessRaises(ValueError, tc._set_timeout, '10 years')
def testAddOld(self):
tc = createClient(test_name='add')
data = 'data'
r = tc.add(data)[0]
self.assertEqual(r.id, 0)
self.assertEqual(r.hashString, 'A000')
self.assertEqual(r.name, 'testtransfer0')
r = tc.add(data, paused=True)[1]
self.assertEqual(r.id, 1)
self.assertEqual(r.hashString, 'A001')
self.assertEqual(r.name, 'testtransfer1')
r = tc.add(data, download_dir='/tmp')[2]
self.assertEqual(r.id, 2)
self.assertEqual(r.hashString, 'A002')
self.assertEqual(r.name, 'testtransfer2')
r = tc.add(data, peer_limit=10)[3]
self.assertEqual(r.id, 3)
self.assertEqual(r.hashString, 'A003')
self.assertEqual(r.name, 'testtransfer3')
r = tc.add(data, paused=True, download_dir='/tmp', peer_limit=10)[4]
self.assertEqual(r.id, 4)
self.assertEqual(r.hashString, 'A004')
self.assertEqual(r.name, 'testtransfer4')
self.failUnlessRaises(ValueError, tc.add, data, peer_limit='apa')
def testAddUriOld(self):
data_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data')
tc = createClient(test_name='adduri')
r = tc.add_uri('torrent.txt', paused=False, download_dir='/var/downloads', peer_limit=1)[0]
self.assertEqual(r.id, 0)
self.assertEqual(r.hashString, 'A000')
self.assertEqual(r.name, 'testtransfer0')
r = tc.add_uri('file://' + os.path.join(data_path, 'torrent.txt'), paused=True, download_dir='/tmp', peer_limit=200)[1]
self.assertEqual(r.id, 1)
self.assertEqual(r.hashString, 'A001')
self.assertEqual(r.name, 'testtransfer1')
def testAddTorrent(self):
data_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data')
tc = createClient(test_name='add_torrent_base64')
torrent_path = os.path.join(data_path, 'ubuntu-12.04.2-alternate-amd64.iso.torrent')
data = open(torrent_path, 'rb').read()
data_b64 = base64.b64encode(data).decode('utf-8')
r = tc.add_torrent(data_b64)
self.assertEqual(r.id, 0)
self.assertEqual(r.hashString, 'a21c45469c565f3fb9595e4e9707e6e9d45abca6')
self.assertEqual(r.name, 'ubuntu-12.04.2-alternate-amd64.iso')
tc = createClient(test_name='adduri')
self.assertRaises(ValueError, tc.add_torrent, None)
r = tc.add_torrent('torrent.txt', paused=False, download_dir='/var/downloads', peer_limit=1)
self.assertEqual(r.id, 0)
self.assertEqual(r.hashString, 'A000')
self.assertEqual(r.name, 'testtransfer0')
r = tc.add_torrent('file://' + os.path.join(data_path, 'torrent.txt'), paused=True, download_dir='/tmp', peer_limit=200)
self.assertEqual(r.id, 1)
self.assertEqual(r.hashString, 'A001')
self.assertEqual(r.name, 'testtransfer1')
def testRemoveOld(self):
tc = createClient(test_name='remove')
tc.remove(['b000', 2, 3])
tc.remove(1, delete_data=True)
tc.remove('b002', delete_data=False)
def testRemoveTorrent(self):
tc = createClient(test_name='remove')
tc.remove_torrent(['b000', 2, 3])
tc.remove_torrent(1, delete_data=True)
tc.remove_torrent('b002', delete_data=False)
def testStartOld(self):
tc = createClient(test_name='start')
tc.start(['abcdef', 20, 30])
tc.start(1)
tc.start('a0123456789')
def testStartTorrent(self):
tc = createClient(test_name='start')
tc.start_torrent(['abcdef', 20, 30])
tc.start_torrent(1)
tc.start_torrent('a0123456789')
def testStopOld(self):
tc = createClient(test_name='stop')
tc.stop(2)
tc.stop('bad')
tc.stop(['bad', 'ba5', '30', 20])
def testStopTorrent(self):
tc = createClient(test_name='stop')
tc.stop(2)
tc.stop('bad')
tc.stop(['bad', 'ba5', '30', 20])
def testVerifyOld(self):
tc = createClient(test_name='verify')
tc.verify(10000)
tc.verify('d')
tc.verify(['a', 'b', 'c'])
def testVerifyTorrent(self):
tc = createClient(test_name='verify')
tc.verify_torrent(10000)
tc.verify_torrent('d')
tc.verify_torrent(['a', 'b', 'c'])
def testInfo(self):
tc = createClient(test_name='info')
r = tc.info()
self.assertTrue(2 in r)
self.assertTrue(3 in r)
t = r[2]
self.assertEqual(t.id, 2)
self.assertEqual(t.name, 'ubuntu-10.04-server-amd64.iso')
self.assertEqual(t.hashString, 'ab8ea951c022d4745a9b06ab8020b952a52b71ca')
def testGetTorrent(self):
tc = createClient(test_name='get_torrent')
torrent = tc.get_torrent(2)
self.assertEqual(torrent.id, 2)
self.assertEqual(torrent.name, 'ubuntu-10.04-server-amd64.iso')
self.assertEqual(torrent.hashString, 'ab8ea951c022d4745a9b06ab8020b952a52b71ca')
tc = createClient(test_name='get_torrent_hash')
torrent = tc.get_torrent('ab8ea951c022d4745a9b06ab8020b952a52b71ca')
self.assertEqual(torrent.id, 2)
self.assertEqual(torrent.name, 'ubuntu-10.04-server-amd64.iso')
self.assertEqual(torrent.hashString, 'ab8ea951c022d4745a9b06ab8020b952a52b71ca')
def testGetTorrents(self):
tc = createClient(test_name='info')
r = tc.get_torrents()
for torrent in r:
if torrent.id == 2:
self.assertEqual(torrent.name, 'ubuntu-10.04-server-amd64.iso')
self.assertEqual(torrent.hashString, 'ab8ea951c022d4745a9b06ab8020b952a52b71ca')
elif torrent.id == 3:
self.assertEqual(torrent.name, 'ubuntu-10.04-alternate-i386.iso')
self.assertEqual(torrent.hashString, 'a33e98826003515e46ef5075fcbf4914b307abe2')
else:
self.fail("Unknown torrent")
def testGetTorrentsRange(self):
tc = createClient(test_name='get_torrents_2to3')
r = tc.get_torrents([2,3])
for torrent in r:
if torrent.id == 2:
self.assertEqual(torrent.name, 'ubuntu-10.04-server-amd64.iso')
self.assertEqual(torrent.hashString, 'ab8ea951c022d4745a9b06ab8020b952a52b71ca')
elif torrent.id == 3:
self.assertEqual(torrent.name, 'ubuntu-10.04-alternate-i386.iso')
self.assertEqual(torrent.hashString, 'a33e98826003515e46ef5075fcbf4914b307abe2')
else:
self.fail("Unknown torrent")
tc = createClient(test_name='get_torrents_2to3')
r = tc.get_torrents("2:3")
for torrent in r:
if torrent.id == 2:
self.assertEqual(torrent.name, 'ubuntu-10.04-server-amd64.iso')
self.assertEqual(torrent.hashString, 'ab8ea951c022d4745a9b06ab8020b952a52b71ca')
elif torrent.id == 3:
self.assertEqual(torrent.name, 'ubuntu-10.04-alternate-i386.iso')
self.assertEqual(torrent.hashString, 'a33e98826003515e46ef5075fcbf4914b307abe2')
else:
self.fail("Unknown torrent")
tc = createClient(test_name='get_torrents_2to3')
r = tc.get_torrents("2,3")
for torrent in r:
if torrent.id == 2:
self.assertEqual(torrent.name, 'ubuntu-10.04-server-amd64.iso')
self.assertEqual(torrent.hashString, 'ab8ea951c022d4745a9b06ab8020b952a52b71ca')
elif torrent.id == 3:
self.assertEqual(torrent.name, 'ubuntu-10.04-alternate-i386.iso')
self.assertEqual(torrent.hashString, 'a33e98826003515e46ef5075fcbf4914b307abe2')
else:
self.fail("Unknown torrent")
tc = createClient(test_name='get_torrents_2to3')
r = tc.get_torrents("2 3")
for torrent in r:
if torrent.id == 2:
self.assertEqual(torrent.name, 'ubuntu-10.04-server-amd64.iso')
self.assertEqual(torrent.hashString, 'ab8ea951c022d4745a9b06ab8020b952a52b71ca')
elif torrent.id == 3:
self.assertEqual(torrent.name, 'ubuntu-10.04-alternate-i386.iso')
self.assertEqual(torrent.hashString, 'a33e98826003515e46ef5075fcbf4914b307abe2')
else:
self.fail("Unknown torrent")
def testGetTorrentsHashes(self):
tc = createClient(test_name='get_torrents_hashes')
r = tc.get_torrents(["ab8ea951c022d4745a9b06ab8020b952a52b71ca", "a33e98826003515e46ef5075fcbf4914b307abe2"])
for torrent in r:
if torrent.id == 2:
self.assertEqual(torrent.name, 'ubuntu-10.04-server-amd64.iso')
self.assertEqual(torrent.hashString, 'ab8ea951c022d4745a9b06ab8020b952a52b71ca')
elif torrent.id == 3:
self.assertEqual(torrent.name, 'ubuntu-10.04-alternate-i386.iso')
self.assertEqual(torrent.hashString, 'a33e98826003515e46ef5075fcbf4914b307abe2')
else:
self.fail("Unknown torrent")
tc = createClient(test_name='get_torrents_hashes')
r = tc.get_torrents("ab8ea951c022d4745a9b06ab8020b952a52b71ca,a33e98826003515e46ef5075fcbf4914b307abe2")
for torrent in r:
if torrent.id == 2:
self.assertEqual(torrent.name, 'ubuntu-10.04-server-amd64.iso')
self.assertEqual(torrent.hashString, 'ab8ea951c022d4745a9b06ab8020b952a52b71ca')
elif torrent.id == 3:
self.assertEqual(torrent.name, 'ubuntu-10.04-alternate-i386.iso')
self.assertEqual(torrent.hashString, 'a33e98826003515e46ef5075fcbf4914b307abe2')
else:
self.fail("Unknown torrent")
tc = createClient(test_name='get_torrents_hashes')
r = tc.get_torrents("ab8ea951c022d4745a9b06ab8020b952a52b71ca a33e98826003515e46ef5075fcbf4914b307abe2")
for torrent in r:
if torrent.id == 2:
self.assertEqual(torrent.name, 'ubuntu-10.04-server-amd64.iso')
self.assertEqual(torrent.hashString, 'ab8ea951c022d4745a9b06ab8020b952a52b71ca')
elif torrent.id == 3:
self.assertEqual(torrent.name, 'ubuntu-10.04-alternate-i386.iso')
self.assertEqual(torrent.hashString, 'a33e98826003515e46ef5075fcbf4914b307abe2')
else:
self.fail("Unknown torrent")
def testParseId(self):
from transmissionrpc.client import parse_torrent_id
self.assertEqual(parse_torrent_id(None), None)
self.assertEqual(parse_torrent_id(10), 10)
self.assertEqual(parse_torrent_id(10.0), 10)
self.assertEqual(parse_torrent_id(10.5), None)
self.assertEqual(parse_torrent_id("10"), 10)
self.assertEqual(parse_torrent_id("A"), "A")
self.assertEqual(parse_torrent_id("a21c45469c565f3fb9595e4e9707e6e9d45abca6"), "a21c45469c565f3fb9595e4e9707e6e9d45abca6")
self.assertEqual(parse_torrent_id("T"), None)
self.assertEqual(parse_torrent_id([10]), None)
self.assertEqual(parse_torrent_id((10, 11)), None)
self.assertEqual(parse_torrent_id({10: 10}), None)
def testParseIds(self):
from transmissionrpc.client import parse_torrent_ids
self.assertEqual(parse_torrent_ids(None), [])
self.assertEqual(parse_torrent_ids(10), [10])
self.assertEqual(parse_torrent_ids(10.0), [10])
self.assertEqual(parse_torrent_ids("10"), [10])
self.assertEqual(parse_torrent_ids("A"), ["A"])
self.assertEqual(parse_torrent_ids("a21c45469c565f3fb9595e4e9707e6e9d45abca6"), ["a21c45469c565f3fb9595e4e9707e6e9d45abca6"])
self.assertEqual(parse_torrent_ids(",, "), [])
self.assertEqual(parse_torrent_ids("1,2,3"), [1,2,3])
self.assertEqual(parse_torrent_ids("1:3"), [1,2,3])
self.assertRaises(ValueError, parse_torrent_ids, "A:3")
self.assertRaises(ValueError, parse_torrent_ids, "T")
self.assertEqual(parse_torrent_ids([10]), [10])
self.assertEqual(parse_torrent_ids((10, 11)), [10, 11])
self.assertRaises(ValueError, parse_torrent_ids, {10: 10})
def suite():
suite = unittest.TestLoader().loadTestsFromTestCase(ClientTest)
return suite
|