trans-api 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,459 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require File.expand_path(File.dirname(__FILE__) + "/../../lib/trans-api")
4
+
5
+
6
+
7
+ #
8
+ # Unit test for Transmission RPC+json
9
+ # https://trac.transmissionbt.com/browser/trunk/extras/rpc-spec.txt
10
+ # Revision: 13328 (2012/11/16)
11
+ #
12
+
13
+ class TransConnect < Test::Unit::TestCase
14
+
15
+ CONFIG = { host: "localhost", port: 9091, user: "admin", pass: "admin", path: "/transmission/rpc" }
16
+
17
+ def setup
18
+ Trans::Api::Client.config = CONFIG
19
+
20
+ # add a testing torrent
21
+ file = File.expand_path(File.dirname(__FILE__) + "/torrents/debian-6.0.6-amd64-CD-1.iso.torrent")
22
+ @torrent = Trans::Api::Torrent.add_file file, paused: true
23
+ sleep 1
24
+ end
25
+
26
+ def teardown
27
+ Trans::Api::Client.config = CONFIG
28
+
29
+ # remove the testing torrent
30
+ id = @torrent.id
31
+ @torrent.delete! delete_local_data: true
32
+ self.signal_wait_until(lambda{|t| t.nil?}) do
33
+ Trans::Api::Torrent.find id
34
+ end
35
+ end
36
+
37
+
38
+
39
+ # SESSIONS
40
+
41
+ def test_session_get
42
+ tc = Trans::Api::Connect.new CONFIG
43
+ session = tc.session_get
44
+ assert session.size > 0, "no arguments found"
45
+ end
46
+
47
+ def test_session_stats
48
+ tc = Trans::Api::Connect.new CONFIG
49
+ session = tc.session_stats
50
+ assert session.size > 0, "no arguments found"
51
+ end
52
+
53
+ def test_session_set
54
+ tc = Trans::Api::Connect.new CONFIG
55
+
56
+ # get session
57
+ session_get = tc.session_get
58
+ assert session_get.size > 0, "no arguments found"
59
+
60
+ original_value = nil
61
+ new_value = nil
62
+
63
+ # loop all arguments until a suitable value is found
64
+ while original_value.nil? && new_value.nil? do
65
+ tmp_old = session_get.first
66
+ tmp_new = [tmp_old.first,nil]
67
+
68
+ # handle a fixnum change
69
+ if tmp_old.last.kind_of? Fixnum
70
+ tmp_new[1] = tmp_old[1] + 1
71
+ original_value = tmp_old
72
+ new_value = tmp_new
73
+ end
74
+ end
75
+
76
+ assert new_value != original_value, "new and original value are the same"
77
+
78
+
79
+ # set new session value
80
+ session_set = tc.session_set(Hash[*new_value])
81
+ session_get = tc.session_get
82
+
83
+ assert session_get.first == new_value, "new value not stored (or being processed)"
84
+
85
+ # set the original value
86
+ session_set = tc.session_set(Hash[*original_value])
87
+ session_get = tc.session_get
88
+
89
+ assert session_get.first == original_value, "old value not stored (or being processed)"
90
+
91
+ end
92
+
93
+ def test_session_close
94
+ tc = Trans::Api::Connect.new CONFIG
95
+ session_get = tc.session_get
96
+ #NOTE: will shut the daemon down!!
97
+ # session_close = tc.session_close
98
+ end
99
+
100
+
101
+ # TORRENTS
102
+
103
+ def test_torrent_get
104
+ tc = Trans::Api::Connect.new CONFIG
105
+
106
+ # receive torrent list
107
+ torrents = tc.torrent_get([:id, :name, :status])
108
+
109
+ # test received torrents
110
+ unless torrents.empty?
111
+ torrents.each do |torrent|
112
+ assert torrent.include?(:id), "torrent missing :id"
113
+ assert torrent.include?(:status), "torrent missing :status"
114
+ assert torrent.include?(:name), "torrent missing :name"
115
+
116
+ # request same torrent from id of the last request
117
+ t_new = tc.torrent_get([:id], [torrent[:id]])
118
+ assert t_new.size == 1
119
+ assert t_new.first[:id] == torrent[:id]
120
+ # request caries only :id, no :name
121
+ assert !t_new.include?(:name)
122
+ end
123
+ else
124
+ assert false, "no torrent files found!"
125
+ end
126
+ end
127
+
128
+ def test_torrent_set
129
+ tc = Trans::Api::Connect.new CONFIG
130
+
131
+ # get torrent list
132
+ torrents = tc.torrent_get([:id, :name, :bandwidthPriority])
133
+
134
+ unless torrents.empty?
135
+ torrents.each do |torrent|
136
+ assert torrent.include?(:bandwidthPriority), "torrent missing :bandwidthPriority"
137
+
138
+ oldvalue = torrent[:bandwidthPriority]
139
+ newvalue = oldvalue + 1
140
+
141
+ # set new value
142
+ tc.torrent_set({bandwidthPriority: newvalue}, [torrent[:id]])
143
+ torrent_new = tc.torrent_get([:id, :bandwidthPriority], [torrent[:id]])
144
+ assert torrent_new.first[:bandwidthPriority] == newvalue
145
+
146
+ # set old value
147
+ tc.torrent_set({bandwidthPriority: oldvalue}, [torrent[:id]])
148
+ torrent_new = tc.torrent_get([:id, :bandwidthPriority], [torrent[:id]])
149
+ assert torrent_new.first[:bandwidthPriority] == oldvalue
150
+
151
+ end
152
+ else
153
+ assert false, "no torrent files found!"
154
+ end
155
+ end
156
+
157
+
158
+ def test_torrent_start_stop_single
159
+ tc = Trans::Api::Connect.new CONFIG
160
+
161
+ torrents = tc.torrent_get([:id, :name, :status])
162
+
163
+ tested = false
164
+
165
+ unless torrents.empty?
166
+ # loop all torrents
167
+ torrents.each do |torrent|
168
+
169
+ old_status = Trans::Api::Torrent::STATUS[torrent[:status]]
170
+
171
+ if old_status == :stopped
172
+
173
+ # toggle start
174
+ tc.torrent_start([torrent[:id]])
175
+
176
+ # wait until started
177
+ new_torrent = nil
178
+ self.signal_wait_until(lambda{|t| Trans::Api::Torrent::STATUS[t.first[:status]] != :stopped}) do
179
+ new_torrent = tc.torrent_get([:id, :name, :status], [torrent[:id]])
180
+ end
181
+ assert Trans::Api::Torrent::STATUS[new_torrent.first[:status]] != :stopped, "torrent signaled for start (not seeding)"
182
+
183
+ # toggle stop
184
+ tc.torrent_stop([torrent[:id]])
185
+
186
+ # wait until stopped
187
+ new_torrent = nil
188
+ self.signal_wait_until(lambda{|t| Trans::Api::Torrent::STATUS[t.first[:status]] == :stopped}) do
189
+ new_torrent = tc.torrent_get([:id, :name, :status], [torrent[:id]])
190
+ end
191
+ assert Trans::Api::Torrent::STATUS[new_torrent.first[:status]] == :stopped, "torrent signaled for stop (not stopped)"
192
+
193
+ tested = true
194
+ break
195
+ end
196
+
197
+ end
198
+ else
199
+ assert false, "no torrent files found!"
200
+ end
201
+ assert tested, "no status :stopped found! (no tests ran)"
202
+ end
203
+
204
+ def test_torrent_start_stop_multi
205
+ tc = Trans::Api::Connect.new CONFIG
206
+ torrents = tc.torrent_get([:id, :name, :status])
207
+
208
+
209
+ unless torrents.empty?
210
+
211
+ # filter for stopped torrents only
212
+ torrents.reject!{|t| Trans::Api::Torrent::STATUS[t[:status]] != :stopped }
213
+
214
+ start_ids = torrents.map{|t| t[:id]}
215
+
216
+ # start all stopped torrents
217
+ tc.torrent_start(start_ids)
218
+
219
+ # wait for all torrents to start
220
+ started_torrents = []
221
+ self.signal_wait_until(lambda{|t| t.reject{|q| Trans::Api::Torrent::STATUS[q[:status]] != :stopped }.size == 0 }) do
222
+ started_torrents = tc.torrent_get([:id, :name, :status], start_ids)
223
+ end
224
+ assert started_torrents.reject{|t| Trans::Api::Torrent::STATUS[t[:status]] != :stopped }.size == 0, "still some unstarted torrents"
225
+
226
+
227
+ # stop all started torrents
228
+ tc.torrent_stop(start_ids)
229
+
230
+ # wait for all torrents to stop
231
+ stopped_torrents = []
232
+ self.signal_wait_until(lambda{|t| t.reject{|q| Trans::Api::Torrent::STATUS[q[:status]] == :stopped }.size == 0 }) do
233
+ stopped_torrents = tc.torrent_get([:id, :name, :status], start_ids)
234
+ end
235
+ assert stopped_torrents.reject{|t| Trans::Api::Torrent::STATUS[t[:status]] == :stopped }.size == 0, "still some running torrents"
236
+
237
+
238
+ else
239
+ assert false, "no torrent files found!"
240
+ end
241
+ end
242
+
243
+ def test_torrent_add_remove_single
244
+ tc = Trans::Api::Connect.new CONFIG
245
+
246
+ # add test file
247
+ file = File.expand_path(File.dirname(__FILE__) + "/torrents/debian-6.0.6-amd64-CD-2.iso.torrent")
248
+ torrent = tc.torrent_add filename: file, paused: true
249
+
250
+ # get submitted torrent
251
+ torrent_get = tc.torrent_get([:id, :name, :status], [torrent[:id]]).first
252
+ assert torrent_get[:id] == torrent[:id]
253
+
254
+ # remove torrent and cleanup
255
+ torrent_remove = tc.torrent_remove({delete_local_data: true}, [torrent[:id]])
256
+
257
+
258
+ torrent_get = nil
259
+ self.signal_wait_until(lambda{|t| t.nil?}) do
260
+ torrent_get = tc.torrent_get([:id, :name, :status], [torrent[:id]]).first
261
+ end
262
+
263
+ assert torrent_get.nil?
264
+
265
+ end
266
+
267
+
268
+ def test_torrent_files_unwatched
269
+ tc = Trans::Api::Connect.new CONFIG
270
+ torrents = tc.torrent_get([:id, :name, :status, :files, :fileStats])
271
+
272
+ torrents.each do |torrent|
273
+ end
274
+
275
+ #TODO: add test here!
276
+
277
+ end
278
+
279
+
280
+ def test_torrent_start_now
281
+ tc = Trans::Api::Connect.new CONFIG
282
+ # get first torrent
283
+ torrent = tc.torrent_get([:id, :name, :status]).first
284
+ # start now
285
+ tc.torrent_start_now([torrent[:id]])
286
+ # reload first torrent
287
+ torrent = tc.torrent_get([:id, :name, :status]).first
288
+ # check started status
289
+ assert Trans::Api::Torrent::STATUS[torrent[:status]] != :stopped
290
+ end
291
+
292
+ def test_torrent_verify
293
+ tc = Trans::Api::Connect.new CONFIG
294
+ torrents = tc.torrent_get([:id, :name, :status])
295
+
296
+ torrents.each do |torrent|
297
+ tc.torrent_verify([torrent[:id]])
298
+ t = tc.torrent_get([:recheckProgress], [torrent[:id]]).first
299
+ assert t[:recheckProgress] > 0
300
+ end
301
+
302
+ end
303
+
304
+ def test_torrent_reannounce
305
+ tc = Trans::Api::Connect.new CONFIG
306
+ torrents = tc.torrent_get([:id, :name, :status])
307
+
308
+ torrents.each do |torrent|
309
+ tc.torrent_reannounce([torrent[:id]])
310
+ end
311
+ # TODO : check for peers
312
+ end
313
+
314
+
315
+
316
+ def test_torrent_set_location
317
+ tc = Trans::Api::Connect.new CONFIG
318
+
319
+ # new target
320
+ file = File.expand_path(File.dirname(__FILE__) + "/torrents/download_tmp/")
321
+
322
+ # load torrent
323
+ torrents = tc.torrent_get([:id, :name, :status, :downloadDir])
324
+ assert torrents.size >0
325
+ torrents.each do |torrent|
326
+ tc.torrent_set_location({move: true, location: file} ,[torrent[:id]])
327
+ end
328
+
329
+ # reload torrent
330
+ torrents = tc.torrent_get([:id, :name, :status, :downloadDir])
331
+ assert torrents.size >0
332
+ torrents.each do |torrent|
333
+ assert torrent[:downloadDir] == file
334
+ end
335
+
336
+ end
337
+
338
+
339
+
340
+
341
+ # MISC
342
+
343
+ def test_blocklist_update
344
+ tc = Trans::Api::Connect.new CONFIG
345
+ response = tc.blocklist_update
346
+ assert response.include? :blocklist_size
347
+ assert response[:blocklist_size].class == Fixnum
348
+ end
349
+
350
+ def test_port_test
351
+ tc = Trans::Api::Connect.new CONFIG
352
+ response = tc.port_test
353
+ assert response.include? :port_is_open
354
+ assert response[:port_is_open].class == FalseClass || response[:port_is_open].class == TrueClass
355
+ end
356
+
357
+
358
+ # QUEUE
359
+
360
+ def test_queue_movement
361
+ tc = Trans::Api::Connect.new CONFIG
362
+
363
+ torrents = []
364
+
365
+ # add a bunch of files
366
+ file = File.expand_path(File.dirname(__FILE__) + "/torrents/debian-6.0.6-amd64-CD-2.iso.torrent")
367
+ torrents << tc.torrent_add(filename: file, paused: true)
368
+ file = File.expand_path(File.dirname(__FILE__) + "/torrents/debian-6.0.6-amd64-CD-3.iso.torrent")
369
+ torrents << tc.torrent_add(filename: file, paused: true)
370
+ file = File.expand_path(File.dirname(__FILE__) + "/torrents/debian-6.0.6-amd64-CD-4.iso.torrent")
371
+ torrents << tc.torrent_add(filename: file, paused: true)
372
+ file = File.expand_path(File.dirname(__FILE__) + "/torrents/debian-6.0.6-amd64-CD-5.iso.torrent")
373
+ torrents << tc.torrent_add(filename: file, paused: true)
374
+ file = File.expand_path(File.dirname(__FILE__) + "/torrents/debian-6.0.6-amd64-CD-6.iso.torrent")
375
+ torrents << tc.torrent_add(filename: file, paused: true)
376
+ file = File.expand_path(File.dirname(__FILE__) + "/torrents/debian-6.0.6-amd64-CD-7.iso.torrent")
377
+ torrents << tc.torrent_add(filename: file, paused: true)
378
+
379
+ # move bottom
380
+ tc.queue_move_bottom [torrents.first[:id]]
381
+
382
+
383
+ # collect all torrents
384
+ all = tc.torrent_get([:id, :name, :status, :queuePosition])
385
+
386
+ # isolate first and last
387
+ torrent_first = all.first
388
+ torrent_last = all.last
389
+
390
+ # reverse queue order
391
+ tc.queue_move_bottom [torrent_first[:id]]
392
+ tc.queue_move_top [torrent_last[:id]]
393
+
394
+ # check if order is reversed
395
+ assert tc.torrent_get([:id, :name, :status, :queuePosition], [torrent_first[:id]]).first[:queuePosition] == all.size - 1
396
+ assert tc.torrent_get([:id, :name, :status, :queuePosition], [torrent_last[:id]]).first[:queuePosition] == 0
397
+
398
+
399
+
400
+ # get reference torrent (queueposition == 0)
401
+ ref = nil
402
+ all = tc.torrent_get([:id, :name, :status, :queuePosition])
403
+ all.each do |t|
404
+ if t[:queuePosition] == 0
405
+ ref = t
406
+ break
407
+ end
408
+ end
409
+
410
+ # move down the queue list
411
+ i = 0
412
+ while i < all.size
413
+ all = tc.torrent_get([:id, :name, :status, :queuePosition])
414
+ all.each do |t|
415
+ if t[:queuePosition] == i
416
+ assert ref[:name] == t[:name]
417
+ tc.queue_move_down [t[:id]]
418
+ end
419
+ end
420
+ i += 1
421
+ end
422
+
423
+ # move up the queue list (use the last reference)
424
+ while i >= 0
425
+ all = tc.torrent_get([:id, :name, :status, :queuePosition])
426
+ all.each do |t|
427
+ if t[:queuePosition] == i
428
+ assert ref[:name] == t[:name]
429
+ tc.queue_move_up [t[:id]]
430
+ end
431
+ end
432
+ i -= 1
433
+ end
434
+
435
+
436
+ # cleanup added torrents
437
+ torrents.each do |torrent|
438
+ tc.torrent_remove [torrent[:id]]
439
+ sleep 0.5 # don't crash the rpc daemon!
440
+ end
441
+ end
442
+
443
+
444
+
445
+
446
+ # UTILS, probe block as long as pr callback returns false
447
+
448
+ def signal_wait_until(pr, &block)
449
+ #NOTE: busy waiting!!!
450
+ while true do
451
+ torrent = yield
452
+ # puts torrent
453
+ break if pr.call torrent
454
+ end
455
+ end
456
+
457
+
458
+ end
459
+
@@ -0,0 +1,82 @@
1
+
2
+ require 'rubygems'
3
+ require 'test/unit'
4
+ require File.expand_path(File.dirname(__FILE__) + "/../../lib/trans-api")
5
+
6
+
7
+
8
+ #
9
+ # Unit test for Transmission RPC+json
10
+ # https://trac.transmissionbt.com/browser/trunk/extras/rpc-spec.txt
11
+ # Revision: 13328 (2012/11/16)
12
+ #
13
+
14
+ class TransSessionObject < Test::Unit::TestCase
15
+
16
+ CONFIG = { host: "localhost", port: 9091, user: "admin", pass: "admin", path: "/transmission/rpc" }
17
+
18
+ def setup
19
+ Trans::Api::Client.config = CONFIG
20
+ end
21
+
22
+
23
+ def test_session_singleton
24
+
25
+ # load first instance
26
+ session1 = Trans::Api::Session.instance
27
+ # load second instance
28
+ session2 = Trans::Api::Session.instance
29
+
30
+ # compare instance objects (should be equal)
31
+ assert session1 == session2
32
+
33
+ end
34
+
35
+
36
+ def test_session_set_peer_limit_global
37
+ # session object
38
+ session = Trans::Api::Session.instance
39
+
40
+ # read value
41
+ oldvalue = session.peer_limit_global
42
+
43
+ # set new value
44
+ session.peer_limit_global = oldvalue + 1
45
+ session.save!
46
+
47
+ # read new value
48
+ newvalue = session.peer_limit_global
49
+ assert newvalue == oldvalue + 1, "newvalue not oldvalue+1"
50
+
51
+ # restore old value
52
+ session.peer_limit_global = oldvalue
53
+ session.save!
54
+
55
+ assert session.peer_limit_global == oldvalue, "oldvalue not stored"
56
+ end
57
+
58
+ def test_session_fields
59
+ # session object
60
+ session = Trans::Api::Session.instance
61
+
62
+ assert session.fields_and_values.size > 0, "no fields and values loaded"
63
+ assert session.fields.size > 0, "no fields loaded"
64
+
65
+ end
66
+
67
+ def test_session_stats
68
+ # session object
69
+ session = Trans::Api::Session.instance
70
+
71
+ assert session.stats!, "no stats loaded"
72
+ end
73
+
74
+
75
+ def test_session_secure
76
+ session = Trans::Api::Session.instance
77
+
78
+ #TODO: add security test here!
79
+ end
80
+
81
+
82
+ end