class FoneworxClientTestCase(TestCase): def setUp(self): self.client = Client('username', 'password', connection=TestConnection()) def tearDown(self): pass def test_dict_to_xml(self): """dict to xml should recursively be able to parse dicts and output neat XML!""" d = { "hello": [{ "world": [{ "recursive": [{ "i": "am" }] }] }] } xml = dict_to_xml(d, root=Element("testing")) xml_string = tostring(xml, 'utf-8') xml_test = u"<testing><hello><world><recursive><i>am" \ "</i></recursive></world></hello></testing>" self.assertEquals(xml_string, xml_test) def test_xml_to_dict(self): """xml to dict should do the opposite of dict_to_xml""" xml = fromstring(u"<testing><hello><world><recursive><i>am" \ "</i></recursive></world></hello></testing>") testing, dictionary = xml_to_dict(xml) self.assertEquals(testing, "testing") self.assertEquals(dictionary, { "hello": [{ "world": [{ "recursive": [{ "i": "am" }] }] }] }) def test_dict_to_xml_unicode(self): """shouldn't trip on unicode characters""" d = { "hello": u"wørl∂" } xml = dict_to_xml(d, root=Element("unicode")) xml_string = tostring(xml, 'utf-8') @inlineCallbacks def test_login(self): session_id = yield self.client.login() self.assertEquals(session_id, 'my_session_id') @inlineCallbacks def test_session_id_property(self): session_id = yield self.client.get_session_id() self.assertEquals(session_id, 'my_session_id') @inlineCallbacks def test_logout(self): status = yield self.client.logout() self.assertEquals(status, 'Success') @inlineCallbacks def test_new_messages(self): response = yield self.client.new_messages() self.assertEquals(response, [{ 'parent_sms_id': 'parent sms id 1', 'msisdn': '+27123456789', 'destination': '+27123456789', 'timereceived': datetime(2010, 7, 14, 12, 15, 11), 'message': 'hello world', 'sms_id': 'sms id 1' }, { 'parent_sms_id': 'parent sms id 2', 'msisdn': '+27123456789', 'destination': '+27123456789', 'timereceived': datetime(2010, 7, 14, 12, 15, 11), 'message': 'hello world', 'sms_id': 'sms id 2' }]) @inlineCallbacks def test_new_messages_since_timestamp(self): response = yield self.client.new_messages(since=datetime.now()) self.assertEquals(response, [{ 'parent_sms_id': 'parent sms id 1', 'msisdn': '+27123456789', 'destination': '+27123456789', 'timereceived': datetime(2010, 7, 14, 12, 15, 11), 'message': 'hello world', 'sms_id': 'sms id 1' }]) @inlineCallbacks def test_delete_messages(self): response = yield self.client.delete_message('sms id 1') self.assertEquals(response, "Success") @inlineCallbacks def test_send_messages(self): response = yield self.client.send_messages([{ "msisdn": "+27123456789", "message": "hello world", "rule": "?", "send_at": datetime.now().strftime("%Y-%m-%d %H:%M:%S") }, { "msisdn": "+27123456789", "message": "hello world", "rule": "?", "send_at": datetime.now().strftime("%Y-%m-%d %H:%M:%S") }]) # check the mocked responses from the API self.assertEquals([d['submit'] for d in response], ['fail', 'success']) self.assertEquals([d['sms_id'] for d in response], ['sms 1', 'sms 2']) @inlineCallbacks def test_sent_messages(self): response = yield self.client.sent_messages() self.assertTrue(response) self.assertEquals(len(response), 1) # get the dict sms = response.pop() self.assertEquals(sms, { 'sms_id': 'sms id 1', 'status_id': Status('3'), 'status_text': 'Delivered', 'time_submitted': datetime(2010, 7, 20, 11, 00,00), 'time_processed': datetime(2010, 7, 20, 12, 00,00), 'rule': None, 'short_message': 'Hello World', 'destination_addr': '+27123456789' }) @inlineCallbacks def test_delete_sent_message(self): response = yield self.client.delete_sent_message('sms id 1') self.assertEquals(response, "Success") response = yield self.client.delete_sent_message('sms id 2') self.assertEquals(response, 'fail') self.assertFailure( self.client.delete_sent_message('an obviously wrong id'), # a deferred ApiException )
class FoneworxConnectionTestCase(TestCase): """Not to be automated, use for manual testing only""" def setUp(self): self.username = os.environ.get('USERNAME') or raw_input("Username: "******"Pasword: ") self.client = Client(self.username, self.password, connection=TwistedConnection( 'www.fwxgwsa.co.za', 50000, )) # log.startLogging(sys.stdout) def tearDown(self): pass @inlineCallbacks def test_login(self): session_id = yield self.client.login() log.msg("Got session id: %s" % session_id) @inlineCallbacks def test_logout(self): status = yield self.client.logout() self.assertEquals(status, 'Success') log.msg("Status: %s" % status) @inlineCallbacks def test_new_messages(self): new_messages = yield self.client.new_messages(since=datetime(2010,7,26)) self.assertTrue(isinstance(new_messages, list)) log.msg("New messages: %s" % new_messages) @inlineCallbacks def test_send_messages(self): sent_messages = yield self.client.send_messages([ { 'msisdn': os.environ.get('MSISDN') or raw_input('enter msisdn: '), 'message': 'hello world from test send messages' } ]) self.assertTrue(isinstance(sent_messages, list)) self.assertTrue(len(sent_messages), 1) sent_sms = sent_messages.pop() self.assertEquals(sent_sms['submit'], 'Success') @inlineCallbacks def test_sent_messages(self): sent_messages = yield self.client.sent_messages( give_detail=1, since=datetime(2000,1,1) ) self.assertTrue(isinstance(sent_messages, list)) self.assertTrue(len(sent_messages)) @inlineCallbacks def test_full_stack(self): start = datetime.now() print """Please send a test SMS to Foneworx in order to fill""" \ """ the inbox.""" while True: print 'Checking for new SMSs every 2 seconds' received = yield self.client.new_messages(since=start) if received: break time.sleep(2) sms = received.pop() print 'Replying to an SMS received from', sms['msisdn'] sent = yield self.client.send_messages([{ 'msisdn': sms['msisdn'], 'message': "Hi! you said: %s" % sms['message'] }]) print 'Waiting until delivered' while True: statuses = yield self.client.sent_messages(since=start, give_detail=True) if statuses: matching_statuses = [status for status in statuses if (status['destination_addr'] in sms['msisdn'])] if matching_statuses: status = matching_statuses.pop() if status['status_text'] == 'Delivered': print 'Delivered!' break else: print 'Not delivered yet:', status['status_text'] else: print 'Did not find any matching statuses' time.sleep(2) print 'Deleting the received message' delete = yield self.client.delete_message(sms['sms_id']) print 'Deleted:', delete print 'Deleting the sent message' delete = yield self.client.delete_sent_message(status['sms_id']) print 'Deleted:', delete print 'Logging out' logout = yield self.client.logout() print 'Logged out:', logout