def test_receive_one_message(self): # mock out the xmpp connection and have it connect and authenticate fake_connection = flexmock(name='fake_connection', _sock="the socket") fake_client = flexmock(name='fake_client', Connection=fake_connection) fake_client.should_receive('connect').and_return(True) fake_client.should_receive('auth').with_args( self.appid, self.password, resource='').and_return(True) # also add in mocks for when messages are received or when we see # presence notifications fake_client.should_receive('RegisterHandler').and_return() # add in a mock for when we send our presence message to the XMPP server fake_client.should_receive('sendInitPresence').and_return() # and make sure that we only process one message fake_client.should_receive('Process').with_args(1).once() flexmock(xmpp) xmpp.should_receive('Client').with_args(self.login_ip, debug=[]) \ .and_return(fake_client) # finally, mock out 'select', and have it put in a message flexmock(select) message = {"the socket": "xmpp"} select.should_receive('select').with_args(['the socket'], [], [], 1) \ .and_return(message, None, None) receiver = XMPPReceiver(self.appid, self.login_ip, self.load_balancer_ip, self.password) actual_messages_sent = receiver.listen_for_messages( messages_to_listen_for=1) self.assertEquals(1, actual_messages_sent)
def test_receive_one_message(self): # mock out the xmpp connection and have it connect and authenticate fake_connection = flexmock(name='fake_connection', _sock="the socket") fake_client = flexmock(name='fake_client', Connection=fake_connection) fake_client.should_receive('connect').and_return(True) fake_client.should_receive('auth').with_args(self.appid, self.password, resource='').and_return(True) # also add in mocks for when messages are received or when we see # presence notifications fake_client.should_receive('RegisterHandler').and_return() # add in a mock for when we send our presence message to the XMPP server fake_client.should_receive('sendInitPresence').and_return() # and make sure that we only process one message fake_client.should_receive('Process').with_args(1).once() flexmock(xmpp) xmpp.should_receive('Client').with_args(self.login_ip, debug=[]) \ .and_return(fake_client) # finally, mock out 'select', and have it put in a message flexmock(select) message = {"the socket" : "xmpp"} select.should_receive('select').with_args(['the socket'], [], [], 1) \ .and_return(message, None, None) receiver = XMPPReceiver(self.appid, self.login_ip, self.load_balancer_ip, self.password) actual_messages_sent = receiver.listen_for_messages( messages_to_listen_for=1) self.assertEquals(1, actual_messages_sent)
def test_presence_message(self): # since we mock out the xmpp client in previous tests, we can't rely on it # to call the xmpp_presence method. therefore, let's test it separately. fake_subscribed_presence = flexmock(name='subscribed') fake_subscribe_presence = flexmock(name='subscribe') fake_from = flexmock(name='fake_from') fake_from.should_receive('getStripped').and_return('me@public1') flexmock(xmpp) xmpp.should_receive('Presence').with_args( to=fake_from, typ='subscribed').and_return(fake_subscribed_presence) xmpp.should_receive('Presence').with_args( to=fake_from, typ='subscribe').and_return(fake_subscribe_presence) fake_conn = flexmock(name='fake_conn') fake_conn.should_receive('send').with_args(fake_subscribed_presence) fake_conn.should_receive('send').with_args(fake_subscribe_presence) fake_event = flexmock(name='fake_event') fake_event.should_receive('getFrom').and_return(fake_from) fake_event.should_receive('getPayload').and_return('doesnt matter') fake_event.should_receive('getType').and_return('subscribe') receiver = XMPPReceiver(self.appid, self.login_ip, self.load_balancer_ip, self.password) receiver.xmpp_presence(fake_conn, fake_event)
def test_receive_one_message(self): # mock out the xmpp connection and have it connect and authenticate fake_connection = flexmock(name='fake_connection', _sock="the socket") fake_client = flexmock(name='fake_client', Connection=fake_connection) fake_client.should_receive('connect').and_return(True) fake_client.should_receive('auth').with_args( self.appid, self.password, resource='').and_return(True) # also add in mocks for when messages are received or when we see # presence notifications fake_client.should_receive('RegisterHandler').and_return() # add in a mock for when we send our presence message to the XMPP server fake_client.should_receive('sendInitPresence').and_return() # and make sure that we only process one message fake_client.should_receive('Process').and_return(len('the message')).\ and_return(0) flexmock(xmpp) xmpp.should_receive('Client').with_args(self.login_ip, debug=[]) \ .and_return(fake_client) receiver = XMPPReceiver(self.appid, self.login_ip, self.load_balancer_ip, self.password) receiver.listen_for_messages()
def test_presence_message(self): # since we mock out the xmpp client in previous tests, we can't rely on it # to call the xmpp_presence method. therefore, let's test it separately. fake_subscribed_presence = flexmock(name='subscribed') fake_subscribe_presence = flexmock(name='subscribe') fake_from = flexmock(name='fake_from') fake_from.should_receive('getStripped').and_return('me@public1') flexmock(xmpp) xmpp.should_receive('Presence').with_args(to=fake_from, typ='subscribed').and_return(fake_subscribed_presence) xmpp.should_receive('Presence').with_args(to=fake_from, typ='subscribe').and_return(fake_subscribe_presence) fake_conn = flexmock(name='fake_conn') fake_conn.should_receive('send').with_args(fake_subscribed_presence) fake_conn.should_receive('send').with_args(fake_subscribe_presence) fake_event = flexmock(name='fake_event') fake_event.should_receive('getFrom').and_return(fake_from) fake_event.should_receive('getPayload').and_return('doesnt matter') fake_event.should_receive('getType').and_return('subscribe') receiver = XMPPReceiver(self.appid, self.login_ip, self.load_balancer_ip, self.password) receiver.xmpp_presence(fake_conn, fake_event)
def test_connect_to_xmpp_but_it_is_down(self): # mock out the xmpp connection and have it not connect fake_client = flexmock(name="fake_client") fake_client.should_receive("connect").and_return(None) flexmock(xmpp) xmpp.should_receive("Client").with_args(self.login_ip, debug=[]).and_return(fake_client) receiver = XMPPReceiver(self.appid, self.login_ip, self.app_port, self.password) self.assertRaises(SystemExit, receiver.listen_for_messages, messages_to_listen_for=1)
def test_connect_to_xmpp_but_cannot_auth(self): # mock out the xmpp connection and have it connect, but not authenticate fake_client = flexmock(name="fake_client") fake_client.should_receive("connect").and_return(True) fake_client.should_receive("auth").with_args(self.appid, self.password, resource="").and_return(None) flexmock(xmpp) xmpp.should_receive("Client").with_args(self.login_ip, debug=[]).and_return(fake_client) receiver = XMPPReceiver(self.appid, self.login_ip, self.password) self.assertRaises(SystemExit, receiver.listen_for_messages, messages_to_listen_for=1)
def test_connect_to_xmpp_but_it_is_down(self): # mock out the xmpp connection and have it not connect fake_client = flexmock(name='fake_client') fake_client.should_receive('connect').and_return(None) flexmock(xmpp) xmpp.should_receive('Client').with_args(self.login_ip, debug=[]) \ .and_return(fake_client) receiver = XMPPReceiver(self.appid, self.login_ip, self.load_balancer_ip, self.password) self.assertRaises(SystemExit, receiver.listen_for_messages)
def test_send_xmpp_message(self): # mock out logging flexmock(logging) logging.should_receive('info').and_return() # set up a fake xmpp message fake_xmpp_message = flexmock(name='xmpp_message') # and slip in our mocked message flexmock(xmpppy) flexmock(xmpppy.protocol) xmpppy.protocol.should_receive('Message').with_args(frm=self.my_jid, to=re.compile('one|two'), body=self.message, typ=self.message_type) \ .and_return(fake_xmpp_message) # set up a fake xmpp client fake_xmpp_client = flexmock(name='xmpp') fake_xmpp_client.should_receive('connect').with_args(secure=False) \ .and_return() fake_xmpp_client.should_receive('auth').with_args( self.appid, self.uasecret, resource='').and_return() fake_xmpp_client.should_receive('send').with_args( fake_xmpp_message).and_return() # and slip in our mocked xmpp client in lieu of the real one xmpppy.should_receive('Client').with_args(self.domain, debug=[]) \ .and_return(fake_xmpp_client) xmpp = xmpp_service_real.XmppService(log=logging.info, service_name='xmpp', domain=self.domain, uaserver='public-ip', uasecret=self.uasecret) # Set up a mocked XMPPRequest, that contains the message we want to send and # who we want to send it to. fake_request = flexmock(name='xmpp_message_request') fake_request.should_receive('jid_list').and_return(['one', 'two']) fake_request.should_receive('body').and_return(self.message) fake_request.should_receive('type').and_return(self.message_type) # _Dynamic_SendMessage will put a NO_ERROR message in the response if the # xmpp message was sent successfully, so just make sure the method returned # and that the mocked response has that status. fake_response = flexmock(name='xmpp_message_response') fake_response.should_receive('add_status') \ .with_args(xmpp_service_pb.XmppMessageResponse.NO_ERROR).and_return() self.assertEquals( None, xmpp._Dynamic_SendMessage(fake_request, fake_response))
def test_connect_to_xmpp_but_cannot_auth(self): # mock out the xmpp connection and have it connect, but not authenticate fake_client = flexmock(name='fake_client') fake_client.should_receive('connect').and_return(True) fake_client.should_receive('auth').with_args( self.appid, self.password, resource='').and_return(None) flexmock(xmpp) xmpp.should_receive('Client').with_args(self.login_ip, debug=[]) \ .and_return(fake_client) receiver = XMPPReceiver(self.appid, self.login_ip, self.password) self.assertRaises(SystemExit, receiver.listen_for_messages, messages_to_listen_for=1)
def test_send_xmpp_message(self): # mock out logging flexmock(logging) logging.should_receive('info').and_return() # set up a fake xmpp message fake_xmpp_message = flexmock(name='xmpp_message') # and slip in our mocked message flexmock(xmpppy) flexmock(xmpppy.protocol) xmpppy.protocol.should_receive('Message').with_args(frm=self.my_jid, to=re.compile('one|two'), body=self.message, typ=self.message_type) \ .and_return(fake_xmpp_message) # set up a fake xmpp client fake_xmpp_client = flexmock(name='xmpp') fake_xmpp_client.should_receive('connect').with_args( (self.ejabberd_ip, self.xmpp_port), secure=False) fake_xmpp_client.should_receive('auth').with_args(self.appid, self.uasecret, resource='').and_return() fake_xmpp_client.should_receive('send').with_args( fake_xmpp_message).and_return() # and slip in our mocked xmpp client in lieu of the real one xmpppy.should_receive('Client').with_args(self.domain, debug=[]) \ .and_return(fake_xmpp_client) xmpp = xmpp_service_real.XmppService( self.ejabberd_ip, log=logging.info, service_name='xmpp', domain=self.domain, uaserver='public-ip', uasecret=self.uasecret) # Set up a mocked XMPPRequest, that contains the message we want to send and # who we want to send it to. fake_request = flexmock(name='xmpp_message_request') fake_request.should_receive('jid_list').and_return(['one', 'two']) fake_request.should_receive('body').and_return(self.message) fake_request.should_receive('type').and_return(self.message_type) # _Dynamic_SendMessage will put a NO_ERROR message in the response if the # xmpp message was sent successfully, so just make sure the method returned # and that the mocked response has that status. fake_response = flexmock(name='xmpp_message_response') fake_response.should_receive('add_status') \ .with_args(xmpp_service_pb.XmppMessageResponse.NO_ERROR).and_return() self.assertEquals(None, xmpp._Dynamic_SendMessage(fake_request, fake_response))
def test_send_channel_message(self): # mock out logging flexmock(logging) logging.should_receive('info').and_return() # set up a fake xmpp message fake_xmpp_message = flexmock(name='xmpp_message') # and slip in our mocked message flexmock(xmpppy) flexmock(xmpppy.protocol) xmpppy.protocol.should_receive('Message').with_args( frm=self.my_jid, to=re.compile('channel.*key@domain'), body=self.message, typ=self.message_type).and_return(fake_xmpp_message) # set up a fake xmpp client fake_xmpp_client = flexmock(name='xmpp') fake_xmpp_client.should_receive('connect').with_args(secure=False) \ .and_return() fake_xmpp_client.should_receive('auth').with_args( self.appid, self.uasecret, resource='').and_return() fake_xmpp_client.should_receive('send').with_args( fake_xmpp_message).and_return() # and slip in our mocked xmpp client in lieu of the real one xmpppy.should_receive('Client').with_args(self.domain, debug=[]) \ .and_return(fake_xmpp_client) xmpp = xmpp_service_real.XmppService(log=logging.info, service_name='xmpp', domain=self.domain, uaserver='public-ip', uasecret=self.uasecret) # Set up a mocked XMPPRequest, that contains the message we want to send and # who we want to send it to. fake_request = flexmock(name='xmpp_message_request') fake_request.should_receive('message').and_return(self.message) fake_request.should_receive('application_key').and_return('key') # Unlike _Dynamic_SendMessage, the channel version doesn't set the NO_ERROR # status, and it doesn't actually return anything, so just make sure that # no exceptions were thrown. self.assertEquals(None, xmpp._Dynamic_SendChannelMessage(fake_request, None))
def test_send_channel_message(self): # mock out logging flexmock(logging) logging.should_receive('info').and_return() # set up a fake xmpp message fake_xmpp_message = flexmock(name='xmpp_message') # and slip in our mocked message flexmock(xmpppy) flexmock(xmpppy.protocol) xmpppy.protocol.should_receive('Message').with_args(frm=self.my_jid, to=re.compile('channel.*key@domain'), body=self.message, typ=self.message_type).and_return(fake_xmpp_message) # set up a fake xmpp client fake_xmpp_client = flexmock(name='xmpp') fake_xmpp_client.should_receive('connect').with_args( (self.ejabberd_ip, self.xmpp_port), secure=False) fake_xmpp_client.should_receive('auth').with_args(self.appid, self.uasecret, resource='').and_return() fake_xmpp_client.should_receive('send').with_args( fake_xmpp_message).and_return() # and slip in our mocked xmpp client in lieu of the real one xmpppy.should_receive('Client').with_args(self.domain, debug=[]) \ .and_return(fake_xmpp_client) xmpp = xmpp_service_real.XmppService( self.ejabberd_ip, log=logging.info, service_name='xmpp', domain=self.domain, uaserver='public-ip', uasecret=self.uasecret) # Set up a mocked XMPPRequest, that contains the message we want to send and # who we want to send it to. fake_request = flexmock(name='xmpp_message_request') fake_request.should_receive('message').and_return(self.message) fake_request.should_receive('application_key').and_return('key') # Unlike _Dynamic_SendMessage, the channel version doesn't set the NO_ERROR # status, and it doesn't actually return anything, so just make sure that # no exceptions were thrown. self.assertEquals(None, xmpp._Dynamic_SendChannelMessage(fake_request, None))
def test_presence_message(self): # since we mock out the xmpp client in previous tests, we can't rely on it # to call the xmpp_presence method. therefore, let's test it separately. fake_subscribed_presence = flexmock(name="subscribed") fake_subscribe_presence = flexmock(name="subscribe") fake_from = flexmock(name="fake_from") fake_from.should_receive("getStripped").and_return("me@public1") flexmock(xmpp) xmpp.should_receive("Presence").with_args(to=fake_from, typ="subscribed").and_return(fake_subscribed_presence) xmpp.should_receive("Presence").with_args(to=fake_from, typ="subscribe").and_return(fake_subscribe_presence) fake_conn = flexmock(name="fake_conn") fake_conn.should_receive("send").with_args(fake_subscribed_presence) fake_conn.should_receive("send").with_args(fake_subscribe_presence) fake_event = flexmock(name="fake_event") fake_event.should_receive("getFrom").and_return(fake_from) fake_event.should_receive("getPayload").and_return("doesnt matter") fake_event.should_receive("getType").and_return("subscribe") receiver = XMPPReceiver(self.appid, self.login_ip, self.app_port, self.password) receiver.xmpp_presence(fake_conn, fake_event)