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/test_soledad.py | 167 ++++++++++++++++++++++++++++++++- 1 file changed, 165 insertions(+), 2 deletions(-) (limited to 'src/leap/soledad/tests/test_soledad.py') 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 From f61d5e113d690e6aff12b3f507efb9f8bb795e5d Mon Sep 17 00:00:00 2001 From: drebs Date: Thu, 30 May 2013 12:08:38 -0300 Subject: Fix pep8 style. --- src/leap/soledad/tests/test_soledad.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'src/leap/soledad/tests/test_soledad.py') diff --git a/src/leap/soledad/tests/test_soledad.py b/src/leap/soledad/tests/test_soledad.py index f78d60f0..09711f19 100644 --- a/src/leap/soledad/tests/test_soledad.py +++ b/src/leap/soledad/tests/test_soledad.py @@ -265,7 +265,6 @@ class SoledadSignalingTestCase(BaseSoledadTest): ADDRESS, ) - def test_sync_signals(self): """ Test Soledad emits SOLEDAD_CREATING_KEYS signal. @@ -291,7 +290,7 @@ class SoledadSignalingTestCase(BaseSoledadTest): 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]) + 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 -- cgit v1.2.3