WIP new tests

This commit is contained in:
Brian Warner 2017-11-03 16:13:47 -07:00
parent d36e0c44bd
commit 37ace81e6e
2 changed files with 69 additions and 0 deletions

View File

@ -0,0 +1,28 @@
from __future__ import print_function, unicode_literals
import mock
from twisted.trial import unittest
from ..transit_server import Transit
class Connection(unittest.TestCase):
def test_connection(self):
ts = Transit(blur_usage=60, usage_logfile=None, stats_file=None)
c1 = mock.Mock()
c2 = mock.Mock()
ts.connection_got_token("token1", "side1", c1)
self.assertEqual(c1.mock_calls, [])
ts.connection_got_token("token1", "side2", c2)
self.assertEqual(c1.mock_calls, [mock.call.buddy_connected(c2)])
self.assertEqual(c2.mock_calls, [mock.call.buddy_connected(c1)])
ts.transitFinished(c1, "token1", "side1", "desc1")
ts.transitFinished(c2, "token1", "side2", "desc2")
def test_lonely(self):
ts = Transit(blur_usage=60, usage_logfile=None, stats_file=None)
c1 = mock.Mock()
ts.connection_got_token("token1", "side1", c1)
self.assertEqual(c1.mock_calls, [])
ts.transitFinished(c1, "token1", "side1", "desc1")

View File

@ -0,0 +1,41 @@
from __future__ import print_function, unicode_literals
import mock
from twisted.trial import unittest
from ..transit_server import Transit
class FakeConnection(object):
def __init__(self, token):
self._token = token
def describeToken(self):
return self._token
def buddy_connected(self, other):
pass
class Logging(unittest.TestCase):
def test_connection_yeslog(self):
ts = Transit(blur_usage=None, usage_logfile=None, stats_file=None)
c1 = FakeConnection("c1")
c2 = FakeConnection("c2")
expected = []
with mock.patch("twisted.python.log.msg") as m:
ts.connection_got_token("token1", "side1", c1)
expected.append(mock.call("transit relay 1: c1"))
self.assertEqual(m.mock_calls, expected)
ts.connection_got_token("token1", "side2", c2)
expected.append(mock.call("transit relay 2: c2"))
self.assertEqual(m.mock_calls, expected)
ts.transitFinished(c1, "token1", "side1", "desc1")
expected.append(mock.call("transitFinished desc1"))
self.assertEqual(m.mock_calls, expected)
def test_connection_nolog(self):
ts = Transit(blur_usage=60, usage_logfile=None, stats_file=None)
c1 = FakeConnection("c1")
with mock.patch("twisted.python.log.msg") as m:
ts.connection_got_token("token1", "side1", c1)
self.assertEqual(m.mock_calls, [])
ts.connection_got_token("token1", "side2", c1)
self.assertEqual(m.mock_calls, [])