def test_gateway(self): # FIXME: this is hideous from django.conf import settings settings.OPERA_SERVICE = "dummy" settings.OPERA_PASSWORD = "******" settings.OPERA_CHANNEL = "dummy" from txtalert.apps.gateway.backends.opera.backend import Gateway gateway = Gateway( url="http://testserver/xmlrpc", service_id="dummy_service_id", password="******", channel="dummy_channel", verbose=True # putting it on verbose because this # gateway should never do any real # http calls as part of the test, # if it is, we'll see it and we'll # know something is wrong ) class MockedEAPIGateway(object): """Mocked XMLRPC interface""" def SendSMS(msisdns, smstexts, delivery=None, expiry=None, priority="standard", receipt="Y"): return {"Identifier": "a" * 8} setattr(gateway.proxy, "EAPIGateway", MockedEAPIGateway()) sent_smss = gateway.send_sms(self.user, ["27123456789"], ["testing hello"]) self.assertEquals(sent_smss.count(), 1) self.assertEquals(sent_smss[0].user, self.user) self.assertEquals(sent_smss[0].identifier, "a" * 8) self.assertEquals(sent_smss[0].msisdn, "27123456789") self.assertEquals(sent_smss[0].smstext, "testing hello")
def test_send_sms(self): [ send_sms, ] = gateway.send_sms(self.user, ['27123456789'], ['testing']) self.failUnless(send_sms.user == self.user) self.failUnless( SendSMS.objects.filter(user=self.user, msisdn='27123456789'))
def test_gateway(self): # FIXME: this is hideous from django.conf import settings settings.OPERA_SERVICE = 'dummy' settings.OPERA_PASSWORD = '******' settings.OPERA_CHANNEL = 'dummy' from txtalert.apps.gateway.backends.opera.backend import Gateway gateway = Gateway(url="http://testserver/xmlrpc", service_id='dummy_service_id', password='******', channel='dummy_channel', verbose=True # putting it on verbose because this # gateway should never do any real # http calls as part of the test, # if it is, we'll see it and we'll # know something is wrong ) class MockedEAPIGateway(object): """Mocked XMLRPC interface""" def SendSMS(msisdns, smstexts, delivery=None, expiry=None, priority='standard', receipt='Y'): return { 'Identifier': 'a' * 8 } setattr(gateway.proxy, 'EAPIGateway', MockedEAPIGateway()) sent_smss = gateway.send_sms(self.user,['27123456789'],['testing hello']) self.assertEquals(sent_smss.count(), 1) self.assertEquals(sent_smss[0].user, self.user) self.assertEquals(sent_smss[0].identifier, 'a' * 8) self.assertEquals(sent_smss[0].msisdn, '27123456789') self.assertEquals(sent_smss[0].smstext, 'testing hello')
def test_gateway(self): # FIXME: this is hideous from django.conf import settings settings.OPERA_SERVICE = 'dummy' settings.OPERA_PASSWORD = '******' settings.OPERA_CHANNEL = 'dummy' from txtalert.apps.gateway.backends.opera.backend import Gateway gateway = Gateway( url="http://testserver/xmlrpc", service_id='dummy_service_id', password='******', channel='dummy_channel', verbose=True # putting it on verbose because this # gateway should never do any real # http calls as part of the test, # if it is, we'll see it and we'll # know something is wrong ) class MockedEAPIGateway(object): """Mocked XMLRPC interface""" def SendSMS(msisdns, smstexts, delivery=None, expiry=None, priority='standard', receipt='Y'): return {'Identifier': 'a' * 8} setattr(gateway.proxy, 'EAPIGateway', MockedEAPIGateway()) sent_smss = gateway.send_sms(self.user, ['27123456789'], ['testing hello']) self.assertEquals(sent_smss.count(), 1) self.assertEquals(sent_smss[0].user, self.user) self.assertEquals(sent_smss[0].identifier, 'a' * 8) self.assertEquals(sent_smss[0].msisdn, '27123456789') self.assertEquals(sent_smss[0].smstext, 'testing hello')
def test_good_receipt_processing(self): """Test the receipt XML we get back from Opera when a message has been sent successfully""" raw_xml_post = """ <?xml version="1.0"?> <!DOCTYPE receipts> <receipts> <receipt> <msgid>26567958</msgid> <reference>001efc31</reference> <msisdn>+44727204592</msisdn> <status>D</status> <timestamp>20080831T15:59:24</timestamp> <billed>NO</billed> </receipt> <receipt> <msgid>26750677</msgid> <reference>001f4041</reference> <msisdn>+44733476814</msisdn> <status>D</status> <timestamp>20080907T09:42:28</timestamp> <billed>NO</billed> </receipt> </receipts> """ # fake us having sent SMSs and having stored the proper identifiers tree = ET.fromstring(raw_xml_post.strip()) receipts = map(element_to_namedtuple, tree.findall('receipt')) for receipt in receipts: # FIXME: we need normalization FAST [send_sms] = gateway.send_sms(self.user, [receipt.msisdn.replace("+", "")], ['testing %s' % receipt.reference]) # manually specifiy the identifier so we can compare it later with the # posted receipt send_sms.identifier = receipt.reference send_sms.save() # mimick POSTed receipt from Opera add_perms_to_user('user', 'can_place_sms_receipt') from django.test.client import RequestFactory factory = RequestFactory( HTTP_AUTHORIZATION=basic_auth_string('user', 'password')) request = factory.post('/', raw_xml_post.strip(), content_type='application/xml; charset=utf-8;') request.user = User.objects.get(username='******') # ugly monkey patching to avoid us having to use a URL to test the opera # backend from txtalert.apps.gateway.backends.opera.views import sms_receipt_handler response = sms_receipt_handler(request) # it should return a JSON response self.assertEquals(response['Content-Type'], 'application/json; charset=utf-8') # test the json response from django.utils import simplejson data = simplejson.loads(response.content) self.assertTrue(data.keys(), ['fail', 'success']) # all should've succeeded self.assertEquals(len(data['success']), 2) # we should get the dict back representing the receipt self.assertEquals([r._asdict() for r in receipts], data['success']) # check database state for receipt in receipts: # FIXME: normalization please ... send_sms = SendSMS.objects.get(msisdn=receipt.msisdn.replace( "+", ""), identifier=receipt.reference) self.assertEquals( send_sms.delivery_timestamp, datetime.strptime(receipt.timestamp, OPERA_TIMESTAMP_FORMAT)) self.assertEquals(send_sms.status, 'D') self.assertEquals(send_sms.user, self.user)
def test_bad_receipt_processing(self): """Test behaviour when we receive a receipt that doesn't match anything we know""" raw_xml_post = """ <?xml version="1.0"?> <!DOCTYPE receipts> <receipts> <receipt> <msgid>26567958</msgid> <reference>001efc31</reference> <msisdn>+44727204592</msisdn> <status>D</status> <timestamp>20080831T15:59:24</timestamp> <billed>NO</billed> </receipt> <receipt> <msgid>26750677</msgid> <reference>001f4041</reference> <msisdn>+44733476814</msisdn> <status>D</status> <timestamp>20080907T09:42:28</timestamp> <billed>NO</billed> </receipt> </receipts> """ # fake us having sent SMSs and having stored the proper identifiers tree = ET.fromstring(raw_xml_post.strip()) receipts = map(element_to_namedtuple, tree.findall('receipt')) for receipt in receipts: [send_sms] = gateway.send_sms(self.user, [receipt.msisdn], ['testing %s' % receipt.reference]) # manually specifiy the identifier so we can compare it later with the # posted receipt send_sms.identifier = 'a-bad-id' # this is going to cause a failure send_sms.save() # mimick POSTed receipt from Opera add_perms_to_user('user', 'can_place_sms_receipt') from django.test.client import RequestFactory factory = RequestFactory( HTTP_AUTHORIZATION=basic_auth_string('user', 'password')) request = factory.post('/', raw_xml_post.strip(), content_type='application/xml; charset=utf-8;') request.user = User.objects.get(username='******') from txtalert.apps.gateway.backends.opera.views import sms_receipt_handler response = sms_receipt_handler(request) # it should return a JSON response self.assertEquals(response['Content-Type'], 'application/json; charset=utf-8') # test the json response from django.utils import simplejson data = simplejson.loads(response.content) self.assertTrue(data.keys(), ['fail', 'success']) # all should've failed self.assertEquals(len(data['fail']), 2) # we should get the dict back representing the receipt self.assertEquals([r._asdict() for r in receipts], data['fail']) # check database state for receipt in receipts: self.assertRaises( SendSMS.DoesNotExist, # exception expected SendSMS.objects.get, # callback user=self.user, # args msisdn=receipt.msisdn, identifier=receipt.reference)