From 6d1af6fb27ebc90c34ea393c509cfbfd00098c21 Mon Sep 17 00:00:00 2001 From: drebs Date: Thu, 30 May 2013 12:03:59 -0300 Subject: Add tests for events signaling. --- src/leap/soledad/tests/__init__.py | 7 +- src/leap/soledad/tests/test_soledad.py | 167 ++++++++++++++++++++++++++++++++- 2 files changed, 170 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/leap/soledad/tests/__init__.py b/src/leap/soledad/tests/__init__.py index c00fb847..9fec5530 100644 --- a/src/leap/soledad/tests/__init__.py +++ b/src/leap/soledad/tests/__init__.py @@ -22,6 +22,9 @@ from leap.common.testing.basetest import BaseLeapTest # instance in each test. #----------------------------------------------------------------------------- +ADDRESS = 'leap@leap.se' + + class BaseSoledadTest(BaseLeapTest): """ Instantiates Soledad for usage in tests. @@ -31,7 +34,7 @@ class BaseSoledadTest(BaseLeapTest): # config info self.db1_file = os.path.join(self.tempdir, "db1.u1db") self.db2_file = os.path.join(self.tempdir, "db2.u1db") - self.email = 'leap@leap.se' + self.email = ADDRESS # open test dbs self._db1 = u1db.open(self.db1_file, create=True, document_factory=LeapDocument) @@ -48,7 +51,7 @@ class BaseSoledadTest(BaseLeapTest): os.unlink(f) self._soledad.close() - def _soledad_instance(self, user='leap@leap.se', passphrase='123', + def _soledad_instance(self, user=ADDRESS, passphrase='123', prefix='', secrets_path=Soledad.STORAGE_SECRETS_FILE_NAME, local_db_path='soledad.u1db', server_url='', diff --git a/src/leap/soledad/tests/test_soledad.py b/src/leap/soledad/tests/test_soledad.py index 1c0e6d4a..f78d60f0 100644 --- a/src/leap/soledad/tests/test_soledad.py +++ b/src/leap/soledad/tests/test_soledad.py @@ -25,14 +25,29 @@ import os import re import tempfile import simplejson as json +from mock import Mock from leap.common.testing.basetest import BaseLeapTest -from leap.soledad.tests import BaseSoledadTest +from leap.common.events import ( + server, + component, + events_pb2 as proto, + register, + signal, +) +from leap.soledad.tests import ( + BaseSoledadTest, + ADDRESS, +) +from leap import soledad from leap.soledad import Soledad from leap.soledad.crypto import SoledadCrypto from leap.soledad.shared_db import SoledadSharedDatabase -from leap.soledad.backends.leap_backend import LeapDocument +from leap.soledad.backends.leap_backend import ( + LeapDocument, + LeapSyncTarget, +) class AuxMethodsTestCase(BaseSoledadTest): @@ -138,3 +153,151 @@ class SoledadSharedDBTestCase(BaseSoledadTest): self.assertTrue( self._doc_put.doc_id == doc_id, 'Wrong doc_id when putting recovery document.') + + +class SoledadSignalingTestCase(BaseSoledadTest): + """ + These tests ensure signals are correctly emmited by Soledad. + """ + + EVENTS_SERVER_PORT = 8090 + + def setUp(self): + BaseSoledadTest.setUp(self) + # mock signaling + soledad.events.signal = Mock() + + def tearDown(self): + pass + + def _pop_mock_call(self, mocked): + mocked.call_args_list.pop() + mocked.mock_calls.pop() + mocked.call_args = mocked.call_args_list[-1] + + def test_stage2_bootstrap_signals(self): + """ + Test that a fresh soledad emits all bootstrap signals. + """ + soledad.events.signal.reset_mock() + # get a fresh instance so it emits all bootstrap signals + sol = self._soledad_instance( + secrets_path='alternative.json', + local_db_path='alternative.u1db') + # reverse call order so we can verify in the order the signals were + # expected + soledad.events.signal.mock_calls.reverse() + soledad.events.signal.call_args = \ + soledad.events.signal.call_args_list[0] + soledad.events.signal.call_args_list.reverse() + # assert signals + soledad.events.signal.assert_called_with( + proto.SOLEDAD_DOWNLOADING_KEYS, + ADDRESS, + ) + self._pop_mock_call(soledad.events.signal) + soledad.events.signal.assert_called_with( + proto.SOLEDAD_DONE_DOWNLOADING_KEYS, + ADDRESS, + ) + self._pop_mock_call(soledad.events.signal) + soledad.events.signal.assert_called_with( + proto.SOLEDAD_CREATING_KEYS, + ADDRESS, + ) + self._pop_mock_call(soledad.events.signal) + soledad.events.signal.assert_called_with( + proto.SOLEDAD_DONE_CREATING_KEYS, + ADDRESS, + ) + self._pop_mock_call(soledad.events.signal) + soledad.events.signal.assert_called_with( + proto.SOLEDAD_DOWNLOADING_KEYS, + ADDRESS, + ) + self._pop_mock_call(soledad.events.signal) + soledad.events.signal.assert_called_with( + proto.SOLEDAD_DONE_DOWNLOADING_KEYS, + ADDRESS, + ) + self._pop_mock_call(soledad.events.signal) + soledad.events.signal.assert_called_with( + proto.SOLEDAD_UPLOADING_KEYS, + ADDRESS, + ) + self._pop_mock_call(soledad.events.signal) + soledad.events.signal.assert_called_with( + proto.SOLEDAD_DONE_UPLOADING_KEYS, + ADDRESS, + ) + + def test_stage1_bootstrap_signals(self): + """ + Test that an existent soledad emits some of the bootstrap signals. + """ + soledad.events.signal.reset_mock() + # get an existent instance so it emits only some of bootstrap signals + sol = self._soledad_instance() + # reverse call order so we can verify in the order the signals were + # expected + soledad.events.signal.mock_calls.reverse() + soledad.events.signal.call_args = \ + soledad.events.signal.call_args_list[0] + soledad.events.signal.call_args_list.reverse() + # assert signals + soledad.events.signal.assert_called_with( + proto.SOLEDAD_DOWNLOADING_KEYS, + ADDRESS, + ) + self._pop_mock_call(soledad.events.signal) + soledad.events.signal.assert_called_with( + proto.SOLEDAD_DONE_DOWNLOADING_KEYS, + ADDRESS, + ) + self._pop_mock_call(soledad.events.signal) + soledad.events.signal.assert_called_with( + proto.SOLEDAD_UPLOADING_KEYS, + ADDRESS, + ) + self._pop_mock_call(soledad.events.signal) + soledad.events.signal.assert_called_with( + proto.SOLEDAD_DONE_UPLOADING_KEYS, + ADDRESS, + ) + + + def test_sync_signals(self): + """ + Test Soledad emits SOLEDAD_CREATING_KEYS signal. + """ + soledad.events.signal.reset_mock() + # get a fresh instance so it emits all bootstrap signals + sol = self._soledad_instance() + # mock the actual db sync so soledad does not try to connect to the + # server + sol._db.sync = Mock() + # do the sync + sol.sync() + # assert the signal has been emitted + soledad.events.signal.assert_called_with( + proto.SOLEDAD_DONE_DATA_SYNC, + ADDRESS, + ) + + def test_need_sync_signals(self): + """ + Test Soledad emits SOLEDAD_CREATING_KEYS signal. + """ + soledad.events.signal.reset_mock() + sol = self._soledad_instance() + # mock the sync target + LeapSyncTarget.get_sync_info = Mock(return_value=[0,0,0,0,2]) + # mock our generation so soledad thinks there's new data to sync + sol._db._get_generation = Mock(return_value=1) + # check for new data to sync + sol.need_sync('http://provider/userdb') + # assert the signal has been emitted + soledad.events.signal.assert_called_with( + proto.SOLEDAD_NEW_DATA_TO_SYNC, + ADDRESS, + ) -- cgit v1.2.3