diff options
author | Duda Dornelles <dudassdornelles@gmail.com> | 2015-01-22 15:50:36 -0200 |
---|---|---|
committer | Pixpoa pairing <pixpoapairing@pixelated-project.org> | 2015-01-22 16:50:53 -0200 |
commit | 86ed99a7d78e4690d103f4334dc0eef03c47e402 (patch) | |
tree | dafd5d0aca3efb6f3c8afdc755c88423fba5991f /service/test/support | |
parent | 81552abcfe67f61b68ad7cbf424738f7788f148c (diff) |
#224 fixing tests and app for twisted migration
Diffstat (limited to 'service/test/support')
-rw-r--r-- | service/test/support/integration/app_test_client.py | 42 | ||||
-rw-r--r-- | service/test/support/test_helper.py | 4 |
2 files changed, 33 insertions, 13 deletions
diff --git a/service/test/support/integration/app_test_client.py b/service/test/support/integration/app_test_client.py index 67bc1124..860b9c40 100644 --- a/service/test/support/integration/app_test_client.py +++ b/service/test/support/integration/app_test_client.py @@ -33,10 +33,16 @@ from pixelated.adapter.model.mail import PixelatedMail from pixelated.adapter.search import SearchEngine from test.support.integration.model import MailBuilder from test.support.test_helper import request_mock +from twisted.internet import reactor +from twisted.internet.defer import Deferred from twisted.web.resource import getChildForRequest +from twisted.web.server import Site class AppTestClient: + INDEX_KEY = '\xde3?\x87\xff\xd9\xd3\x14\xf0\xa7>\x1f%C{\x16.\\\xae\x8c\x13\xa7\xfb\x04\xd4]+\x8d_\xed\xd1\x8d\x0bI' \ + '\x8a\x0e\xa4tm\xab\xbf\xb4\xa5\x99\x00d\xd5w\x9f\x18\xbc\x1d\xd4_W\xd2\xb6\xe8H\x83\x1b\xd8\x9d\xad' + def __init__(self, soledad_test_folder='/tmp/soledad-test/test'): self.soledad = initialize_soledad(tempdir=soledad_test_folder) @@ -50,7 +56,7 @@ class AppTestClient: self.app = pixelated.runserver.app self.soledad_querier = SoledadQuerier(self.soledad) - self.soledad_querier.get_index_masterkey = lambda: '\xde3?\x87\xff\xd9\xd3\x14\xf0\xa7>\x1f%C{\x16.\\\xae\x8c\x13\xa7\xfb\x04\xd4]+\x8d_\xed\xd1\x8d\x0bI\x8a\x0e\xa4tm\xab\xbf\xb4\xa5\x99\x00d\xd5w\x9f\x18\xbc\x1d\xd4_W\xd2\xb6\xe8H\x83\x1b\xd8\x9d\xad' + self.soledad_querier.get_index_masterkey = lambda: self.INDEX_KEY self.account = SoledadBackedAccount('test', self.soledad, MagicMock()) self.mailboxes = Mailboxes(self.account, self.soledad_querier) @@ -67,27 +73,37 @@ class AppTestClient: self.app.resource.initialize(self.soledad_querier, self.search_engine, self.mail_service, self.draft_service) def _render(self, request, as_json=True): + def get_str(_str): + return json.loads(_str) if as_json else _str + def get_request_written_data(_=None): - written_data = None - if len(request.written): - written_data = request.written[0] + written_data = request.getWrittenData() if written_data: - return json.loads(written_data) if as_json else written_data + return get_str(written_data) resource = getChildForRequest(self.app.resource, request) - result = resource.render(request) + if isinstance(result, basestring): + return get_str(result), request + if request.finished: - return get_request_written_data(), request + d = Deferred() + d.addCallback(get_request_written_data) + return d, request else: - return request.notifyFinish().addCallback(lambda _: request).addCallback(get_request_written_data), request - - def run_on_a_thread(self, logfile='/tmp/app_test_client.log', port=4567, host='localhost'): - worker = lambda: self.app.run(host=host, port=port, logFile=open(logfile, 'w')) - process = multiprocessing.Process(target=worker) + d = request.notifyFinish() + d.addCallback(lambda _: request) + d.addCallback(get_request_written_data) + return d, request + + def run_on_a_thread(self, logfile='/tmp/app_test_client.log', port=4567, host='0.0.0.0'): + def _start(): + reactor.listenTCP(port, Site(self.app.resource), interface=host) + reactor.run() + process = multiprocessing.Process(target=_start) process.start() - time.sleep(1) # just let it start + time.sleep(1) return lambda: process.terminate() def get(self, path, get_args, as_json=True): diff --git a/service/test/support/test_helper.py b/service/test/support/test_helper.py index d2387d0c..54685008 100644 --- a/service/test/support/test_helper.py +++ b/service/test/support/test_helper.py @@ -91,6 +91,10 @@ class PixRequestMock(DummyRequest): self.content = None self.code = None + def getWrittenData(self): + if len(self.written): + return self.written[0] + def request_mock(path='', method='GET', body='', headers={}): dummy = PixRequestMock(path.split('/')) |