2015-09-28 07:37:35 +00:00
|
|
|
import sys, json
|
2015-06-21 02:18:21 +00:00
|
|
|
from twisted.trial import unittest
|
2015-09-28 23:44:00 +00:00
|
|
|
from twisted.internet.defer import gatherResults
|
2015-07-25 00:47:46 +00:00
|
|
|
from ..twisted.transcribe import Wormhole, UsageError
|
2015-09-27 00:59:39 +00:00
|
|
|
from .common import ServerBase
|
2015-06-21 02:18:21 +00:00
|
|
|
|
2015-09-22 06:21:40 +00:00
|
|
|
class Basic(ServerBase, unittest.TestCase):
|
2015-06-21 02:18:21 +00:00
|
|
|
def test_basic(self):
|
2015-09-24 19:37:55 +00:00
|
|
|
appid = b"appid"
|
2015-07-25 00:47:46 +00:00
|
|
|
w1 = Wormhole(appid, self.relayurl)
|
|
|
|
w2 = Wormhole(appid, self.relayurl)
|
2015-06-21 02:18:21 +00:00
|
|
|
d = w1.get_code()
|
|
|
|
def _got_code(code):
|
|
|
|
w2.set_code(code)
|
2015-09-28 06:09:51 +00:00
|
|
|
d1 = w1.get_data(b"data1")
|
|
|
|
d2 = w2.get_data(b"data2")
|
2015-09-28 23:44:00 +00:00
|
|
|
return gatherResults([d1,d2], True)
|
2015-06-21 02:18:21 +00:00
|
|
|
d.addCallback(_got_code)
|
|
|
|
def _done(dl):
|
2015-09-28 23:44:00 +00:00
|
|
|
(dataX, dataY) = dl
|
2015-09-28 06:09:51 +00:00
|
|
|
self.assertEqual(dataX, b"data2")
|
|
|
|
self.assertEqual(dataY, b"data1")
|
2015-06-21 02:18:21 +00:00
|
|
|
d.addCallback(_done)
|
|
|
|
return d
|
|
|
|
|
|
|
|
def test_fixed_code(self):
|
2015-09-24 19:37:55 +00:00
|
|
|
appid = b"appid"
|
2015-07-25 00:47:46 +00:00
|
|
|
w1 = Wormhole(appid, self.relayurl)
|
|
|
|
w2 = Wormhole(appid, self.relayurl)
|
2015-06-21 02:18:21 +00:00
|
|
|
w1.set_code("123-purple-elephant")
|
|
|
|
w2.set_code("123-purple-elephant")
|
2015-09-28 06:09:51 +00:00
|
|
|
d1 = w1.get_data(b"data1")
|
|
|
|
d2 = w2.get_data(b"data2")
|
2015-09-28 23:44:00 +00:00
|
|
|
d = gatherResults([d1,d2], True)
|
2015-06-21 02:18:21 +00:00
|
|
|
def _done(dl):
|
2015-09-28 23:44:00 +00:00
|
|
|
(dataX, dataY) = dl
|
2015-09-28 06:09:51 +00:00
|
|
|
self.assertEqual(dataX, b"data2")
|
|
|
|
self.assertEqual(dataY, b"data1")
|
2015-06-21 02:18:21 +00:00
|
|
|
d.addCallback(_done)
|
|
|
|
return d
|
|
|
|
|
|
|
|
def test_errors(self):
|
2015-09-24 19:37:55 +00:00
|
|
|
appid = b"appid"
|
2015-07-25 00:47:46 +00:00
|
|
|
w1 = Wormhole(appid, self.relayurl)
|
2015-06-21 02:18:21 +00:00
|
|
|
self.assertRaises(UsageError, w1.get_verifier)
|
2015-09-28 06:09:51 +00:00
|
|
|
self.assertRaises(UsageError, w1.get_data, b"data")
|
2015-06-21 02:18:21 +00:00
|
|
|
w1.set_code("123-purple-elephant")
|
|
|
|
self.assertRaises(UsageError, w1.set_code, "123-nope")
|
|
|
|
self.assertRaises(UsageError, w1.get_code)
|
2015-07-25 00:47:46 +00:00
|
|
|
w2 = Wormhole(appid, self.relayurl)
|
2015-06-21 02:18:21 +00:00
|
|
|
d = w2.get_code()
|
|
|
|
self.assertRaises(UsageError, w2.get_code)
|
|
|
|
return d
|
|
|
|
|
|
|
|
def test_serialize(self):
|
2015-09-24 19:37:55 +00:00
|
|
|
appid = b"appid"
|
2015-07-25 00:47:46 +00:00
|
|
|
w1 = Wormhole(appid, self.relayurl)
|
2015-06-21 02:18:21 +00:00
|
|
|
self.assertRaises(UsageError, w1.serialize) # too early
|
2015-07-25 00:47:46 +00:00
|
|
|
w2 = Wormhole(appid, self.relayurl)
|
2015-06-21 02:18:21 +00:00
|
|
|
d = w1.get_code()
|
|
|
|
def _got_code(code):
|
|
|
|
self.assertRaises(UsageError, w2.serialize) # too early
|
|
|
|
w2.set_code(code)
|
|
|
|
w2.serialize() # ok
|
|
|
|
s = w1.serialize()
|
|
|
|
self.assertEqual(type(s), type(""))
|
|
|
|
unpacked = json.loads(s) # this is supposed to be JSON
|
|
|
|
self.assertEqual(type(unpacked), dict)
|
2015-07-25 00:47:46 +00:00
|
|
|
new_w1 = Wormhole.from_serialized(s)
|
2015-09-28 06:09:51 +00:00
|
|
|
d1 = new_w1.get_data(b"data1")
|
|
|
|
d2 = w2.get_data(b"data2")
|
2015-09-28 23:44:00 +00:00
|
|
|
return gatherResults([d1,d2], True)
|
2015-06-21 02:18:21 +00:00
|
|
|
d.addCallback(_got_code)
|
|
|
|
def _done(dl):
|
2015-09-28 23:44:00 +00:00
|
|
|
(dataX, dataY) = dl
|
2015-09-28 06:09:51 +00:00
|
|
|
self.assertEqual(dataX, b"data2")
|
|
|
|
self.assertEqual(dataY, b"data1")
|
2015-06-21 02:18:21 +00:00
|
|
|
self.assertRaises(UsageError, w2.serialize) # too late
|
|
|
|
d.addCallback(_done)
|
|
|
|
return d
|
2015-09-28 07:37:35 +00:00
|
|
|
|
|
|
|
if sys.version_info[0] >= 3:
|
|
|
|
Basic.skip = "twisted is not yet sufficiently ported to py3"
|
|
|
|
# as of 15.4.0, Twisted is still missing:
|
|
|
|
# * web.client.Agent (for all non-EventSource POSTs in transcribe.py)
|
|
|
|
# * python.logfile (to allow daemonization of 'wormhole server')
|