<s> from contextlib import contextmanager <EOL> from contextlib import nested <EOL> import copy <EOL> import mock <EOL> import testify as T <EOL> from pushmanager . core . settings import Settings <EOL> import pushmanager . core . xmppclient <EOL> class CoreXMPPClientTest ( T . TestCase ) : <EOL> @ T . setup <EOL> def setup_fakexmpp_attrs ( self ) : <EOL> self . fake_client_attrs = { <EOL> "<STR_LIT>" : True , <EOL> "<STR_LIT>" : True , <EOL> "<STR_LIT>" : True , <EOL> "<STR_LIT>" : True , <EOL> "<STR_LIT>" : True , <EOL> "<STR_LIT>" : True , <EOL> "<STR_LIT>" : True , <EOL> "<STR_LIT>" : True <EOL> } <EOL> self . fake_queue_attrs = { <EOL> "<STR_LIT>" : ( '<STR_LIT>' , "<STR_LIT>" ) <EOL> } <EOL> @ T . setup <EOL> def mock_settings ( self ) : <EOL> self . MockedSettings = copy . deepcopy ( Settings ) <EOL> @ contextmanager <EOL> def fake_xmpp_connect ( self ) : <EOL> with nested ( <EOL> mock . patch ( "<STR_LIT>" % __name__ ) , <EOL> mock . patch ( "<STR_LIT>" % __name__ ) , <EOL> mock . patch ( "<STR_LIT>" % __name__ ) , <EOL> mock . patch ( "<STR_LIT>" % __name__ ) , <EOL> ) : <EOL> pushmanager . core . xmppclient . xmpp . Client . configure_mock ( ** self . fake_client_attrs ) <EOL> jabber_client = pushmanager . core . xmppclient . XMPPQueue . _xmpp_connect_and_auth ( ) <EOL> yield jabber_client <EOL> def test_xmpp_connect ( self ) : <EOL> with self . fake_xmpp_connect ( ) as jabber_client : <EOL> T . assert_equal ( jabber_client . connect . call_count , <NUM_LIT:1> ) <EOL> T . assert_equal ( jabber_client . auth . call_count , <NUM_LIT:1> ) <EOL> def test_xmpp_reconnect ( self ) : <EOL> with self . fake_xmpp_connect ( ) as jabber_client : <EOL> jabber_client . isConnected . return_value = False <EOL> pushmanager . core . xmppclient . XMPPQueue . _xmpp_check_and_reconnect ( jabber_client ) <EOL> T . assert_equal ( jabber_client . reconnectAndReauth . call_count , <NUM_LIT:1> ) <EOL> def test_process_queue_item_successful ( self ) : <EOL> with nested ( <EOL> mock . patch ( "<STR_LIT>" % __name__ ) , <EOL> mock . patch ( "<STR_LIT>" % __name__ ) <EOL> ) : <EOL> jabber_client = mock . MagicMock ( ) <EOL> pushmanager . core . xmppclient . XMPPQueue . message_queue . configure_mock ( ** self . fake_queue_attrs ) <EOL> pushmanager . core . xmppclient . XMPPQueue . _process_queue_item ( jabber_client ) <EOL> T . assert_equal ( jabber_client . send . call_count , <NUM_LIT:1> ) <EOL> T . assert_equal ( pushmanager . core . xmppclient . XMPPQueue . message_queue . get . call_count , <NUM_LIT:1> ) <EOL> T . assert_equal ( pushmanager . core . xmppclient . XMPPQueue . message_queue . put . call_count , <NUM_LIT:0> ) <EOL> T . assert_equal ( pushmanager . core . xmppclient . XMPPQueue . message_queue . task_done . call_count , <NUM_LIT:1> ) <EOL> def test_process_queue_item_retry ( self ) : <EOL> def raise_ioerror ( * args ) : <EOL> raise IOError ( "<STR_LIT>" ) <EOL> with nested ( <EOL> mock . patch ( "<STR_LIT>" % __name__ ) , <EOL> mock . patch ( "<STR_LIT>" % __name__ ) , <EOL> mock . patch ( "<STR_LIT>" % __name__ ) <EOL> ) : <EOL> jabber_client = mock . MagicMock ( ) <EOL> jabber_client . send . side_effect = raise_ioerror <EOL> pushmanager . core . xmppclient . XMPPQueue . message_queue . configure_mock ( ** self . fake_queue_attrs ) <EOL> retry_count = pushmanager . core . xmppclient . XMPPQueue . MAX_RETRY_COUNT <EOL> for _ in range ( retry_count ) : <EOL> pushmanager . core . xmppclient . XMPPQueue . _process_queue_item ( jabber_client ) <EOL> T . assert_equal ( jabber_client . send . call_count , retry_count ) <EOL> T . assert_equal ( pushmanager . core . xmppclient . XMPPQueue . message_queue . get . call_count , retry_count ) <EOL> T . assert_equal ( pushmanager . core . xmppclient . XMPPQueue . message_queue . task_done . call_count , retry_count ) <EOL> T . assert_equal ( pushmanager . core . xmppclient . XMPPQueue . message_queue . put . call_count , retry_count - <NUM_LIT:1> ) <EOL> def test_enqueue_user_xmpp_with_string ( self ) : <EOL> fake_domain = "<STR_LIT>" <EOL> fake_user = "<STR_LIT>" <EOL> self . MockedSettings [ '<STR_LIT>' ] [ '<STR_LIT>' ] = fake_domain <EOL> with mock . patch . dict ( pushmanager . core . xmppclient . Settings , self . MockedSettings ) : <EOL> with mock . patch . object ( pushmanager . core . xmppclient . XMPPQueue , "<STR_LIT>" ) as mock_enqueue_xmpp : <EOL> pushmanager . core . xmppclient . XMPPQueue . enqueue_user_xmpp ( fake_user , '<STR_LIT:message>' ) <EOL> mock_enqueue_xmpp . assert_called_with ( "<STR_LIT>" % ( fake_user , fake_domain ) , '<STR_LIT:message>' ) <EOL> def test_enqueue_user_xmpp_with_list ( self ) : <EOL> fake_domain = "<STR_LIT>" <EOL> fake_users = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> self . MockedSettings [ '<STR_LIT>' ] [ '<STR_LIT>' ] = fake_domain <EOL> self . MockedSettings [ '<STR_LIT>' ] [ '<STR_LIT>' ] = [ ] <EOL> with mock . patch . dict ( pushmanager . core . xmppclient . Settings , self . MockedSettings ) : <EOL> with mock . patch . object ( pushmanager . core . xmppclient . XMPPQueue , "<STR_LIT>" ) as mock_enqueue_xmpp : <EOL> pushmanager . core . xmppclient . XMPPQueue . enqueue_user_xmpp ( fake_users , '<STR_LIT:message>' ) <EOL> fake_ids = [ "<STR_LIT>" % ( fake_user , fake_domain ) for fake_user in fake_users ] <EOL> mock_enqueue_xmpp . assert_called_with ( fake_ids , '<STR_LIT:message>' ) <EOL> def test_enqueue_user_xmpp_with_notifyonly ( self ) : <EOL> fake_domain = "<STR_LIT>" <EOL> fake_users = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> self . MockedSettings [ '<STR_LIT>' ] [ '<STR_LIT>' ] = fake_domain <EOL> self . MockedSettings [ '<STR_LIT>' ] [ '<STR_LIT>' ] = [ '<STR_LIT>' ] <EOL> with mock . patch . dict ( pushmanager . core . xmppclient . Settings , self . MockedSettings ) : <EOL> with mock . patch . object ( pushmanager . core . xmppclient . XMPPQueue , "<STR_LIT>" ) as mock_enqueue_xmpp : <EOL> pushmanager . core . xmppclient . XMPPQueue . enqueue_user_xmpp ( fake_users , '<STR_LIT:message>' ) <EOL> fake_ids = [ "<STR_LIT>" % ( fake_user , fake_domain ) for fake_user in fake_users ] <EOL> mock_enqueue_xmpp . assert_called_with ( <EOL> [ '<STR_LIT>' % ( user , fake_domain ) for user in self . MockedSettings [ '<STR_LIT>' ] [ '<STR_LIT>' ] ] , <EOL> "<STR_LIT>" % fake_ids ) </s>
