Example #1
0
    def test_receipt_msisdn_normalization(self):
        send_sms = SendSMS.objects.create(user=self.user,
                                          msisdn='27761234567',
                                          identifier='abcdefg',
                                          expiry=datetime.today() +
                                          timedelta(days=1),
                                          delivery=datetime.today())
        self.assertEquals(send_sms.status, 'v')  # unknown

        raw_xml_post = """
        <?xml version="1.0"?>
        <!DOCTYPE receipts>
        <receipts>
          <receipt>
            <msgid>26567958</msgid>
            <reference>abcdefg</reference>
            <msisdn>+27761234567</msisdn>
            <status>D</status>
            <timestamp>20080831T15:59:24</timestamp>
            <billed>NO</billed>
          </receipt>
        </receipts>
        """

        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

        # mimick POSTed receipt from Opera
        add_perms_to_user('user', 'can_place_sms_receipt')

        # push the request
        response = sms_receipt_handler(request)

        # check the database response
        updated_send_sms = SendSMS.objects.get(pk=send_sms.pk)
        self.assertEquals(updated_send_sms.user, self.user)
        self.assertEquals(updated_send_sms.status, 'D')  # delivered
Example #2
0
    def test_receipt_msisdn_normalization(self):
        send_sms = SendSMS.objects.create(
            user=self.user,
            msisdn="27761234567",
            identifier="abcdefg",
            expiry=datetime.today() + timedelta(days=1),
            delivery=datetime.today(),
        )
        self.assertEquals(send_sms.status, "v")  # unknown

        raw_xml_post = """
        <?xml version="1.0"?>
        <!DOCTYPE receipts>
        <receipts>
          <receipt>
            <msgid>26567958</msgid>
            <reference>abcdefg</reference>
            <msisdn>+27761234567</msisdn>
            <status>D</status>
            <timestamp>20080831T15:59:24</timestamp>
            <billed>NO</billed>
          </receipt>
        </receipts>
        """

        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

        # mimick POSTed receipt from Opera
        add_perms_to_user("user", "can_place_sms_receipt")

        # push the request
        response = sms_receipt_handler(request)

        # check the database response
        updated_send_sms = SendSMS.objects.get(pk=send_sms.pk)
        self.assertEquals(updated_send_sms.user, self.user)
        self.assertEquals(updated_send_sms.status, "D")  # delivered
Example #3
0
    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)
Example #4
0
    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,
            )
Example #5
0
    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)
Example #6
0
    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)