def mock_send(self, status_code=200, response=None, report=None): msg = QueuedSMS( phone_number='+15554443333', text="the message", direction="O", ) msg.save = lambda: None # prevent db access in SimpleTestCase query = querystring({ "apikey": API_KEY, "country_code": "0", "mobile_number": msg.phone_number, "message": msg.text, "concat": "TRUE", }) if response is None: response = {"requestID": "1234561234567asdf123"} with requests_mock.Mocker() as mock: mock.get( "http://api.trumpia.com/http/v2/sendverificationsms" + query, request_headers={"Accept": "application/json"}, status_code=status_code, json=(response if status_code == 200 else {}), ) if "requestID" in response: self.mock_report(mock, response["requestID"], report) self.backend.send(msg) return msg
def test_get_text_and_content_type(self): self.assertEqual( KarixBackend.get_text_and_content_type(QueuedSMS(text='abc')), ('abc', 'PM')) self.assertEqual( KarixBackend.get_text_and_content_type( QueuedSMS(text='\u0928\u092e\u0938\u094d\u0924\u0947')), ('\u0928\u092e\u0938\u094d\u0924\u0947', 'UC'))
def test_get_text_and_lang_id(self): self.assertEqual( AirtelTCLBackend.get_text_and_lang_id(QueuedSMS(text='abc')), ('abc', None)) self.assertEqual( AirtelTCLBackend.get_text_and_lang_id( QueuedSMS(text='\u0928\u092e\u0938\u094d\u0924\u0947')), ('नमस्ते', '2'))
def mock_send(self, **kwargs): msg = QueuedSMS( phone_number='+255111222333', text="the message", ) msg.save = lambda: None # prevent db access in SimpleTestCase with requests_mock.Mocker() as mock: mock.get(StarfishBackend.get_url(), **kwargs) StarfishBackend().send(msg) return msg
def test_get_json_payload_ascii(self, get_timestamp_mock): get_timestamp_mock.return_value = datetime(2018, 9, 7, 12, 34, 56) backend = AirtelTCLBackend() backend.set_extra_fields( user_name='abc', sender_id='def', circle_name='ghi', campaign_name='jkl', ) msg_obj = QueuedSMS(text='message', phone_number='+910123456789') payload = backend.get_json_payload(msg_obj) self.assertEqual( payload, { 'timeStamp': '07092018123456', 'keyword': 'ICDS', 'dataSet': [{ 'MSISDN': '0123456789', 'OA': 'def', 'CIRCLE_NAME': 'ghi', 'CAMPAIGN_NAME': 'jkl', 'MESSAGE': 'message', 'USER_NAME': 'abc', 'CHANNEL': 'SMS', }], })
def test_get_json_payload_ascii(self): backend = KarixBackend() backend.set_extra_fields( username='******', password='******', sender_id='SENDER', ) msg_obj = QueuedSMS(text='test', phone_number='+919999999999') payload = backend.get_json_payload(msg_obj) self.assertEqual( json.loads(json.dumps(payload)), { 'ver': '1.0', 'key': 'YWJjOjEyMw==', 'messages': [ { 'dest': ['919999999999'], 'send': 'SENDER', 'text': 'test', 'type': 'PM', 'vp': '1440', }, ], })
def test_get_json_payload_unicode(self, get_timestamp_mock): get_timestamp_mock.return_value = datetime(2018, 9, 7, 12, 34, 56) backend = AirtelTCLBackend() backend.set_extra_fields( user_name='abc', sender_id='def', circle_name='ghi', campaign_name='jkl', ) msg_obj = QueuedSMS(text='\u0928\u092e\u0938\u094d\u0924\u0947', phone_number='+916123456789') payload = backend.get_json_payload(msg_obj) self.assertEqual( payload, { 'timeStamp': '07092018123456', 'keyword': 'ICDS', 'dataSet': [{ 'MSISDN': '6123456789', 'OA': 'def', 'CIRCLE_NAME': 'ghi', 'CAMPAIGN_NAME': 'jkl', 'MESSAGE': 'नमस्ते', 'USER_NAME': 'abc', 'CHANNEL': 'SMS', 'LANG_ID': '2', }], })
def test_handle_failure(self): backend = StartEnterpriseBackend() queued_sms = QueuedSMS() with patch( 'corehq.messaging.smsbackends.start_enterprise.models.notify_exception' ) as notify_patch: backend.handle_response(queued_sms, 200, RECOGNIZED_ERROR_MESSAGE) self.assertEqual(queued_sms.system_error_message, SMS.ERROR_TOO_MANY_UNSUCCESSFUL_ATTEMPTS) self.assertTrue(queued_sms.error) notify_patch.assert_called_once_with( None, "Error with the Start Enterprise Backend: %s" % RECOGNIZED_ERROR_MESSAGE) with self.assertRaisesMessage(StartEnterpriseBackendException, "Received unexpected status code: 500"): backend.handle_response(queued_sms, 500, '') with self.assertRaisesMessage( StartEnterpriseBackendException, "Unrecognized response from Start Enterprise gateway: %s" % UNRECOGNIZED_ERROR_MESSAGE): backend.handle_response(queued_sms, 200, UNRECOGNIZED_ERROR_MESSAGE)
def test_handle_success(self): backend = IvoryCoastMTNBackend() queued_sms = QueuedSMS() backend.handle_response(queued_sms, 200, SUCCESS_RESPONSE) self.assertEqual(queued_sms.backend_message_id, '164b210b-748c-41d2-97f4-88d166415423')
def test_passes_trial_check(self, domain_is_on_trial_patch, set_system_error_patch, delay_patch, enqueue_patch): client = get_redis_client() sms = QueuedSMS(domain='trial-project') domain_is_on_trial_patch.return_value = True key = 'sms-sent-on-trial-for-trial-project' # Test when key doesn't exist yet self.assertIsNone(client.get(key)) self.assertTrue(passes_trial_check(sms)) self.assertEqual(client.get(key), 1) self.assertGreater(client.ttl(key), 89 * 24 * 60 * 60) # Test with existing key self.assertTrue(passes_trial_check(sms)) self.assertEqual(client.get(key), 2) self.assertGreater(client.ttl(key), 89 * 24 * 60 * 60) # Test when limit is exceeded client.set(key, MAX_TRIAL_SMS) self.assertFalse(passes_trial_check(sms)) set_system_error_patch.assert_called_once() # Test when not on trial domain_is_on_trial_patch.return_value = False self.assertTrue(passes_trial_check(sms))
def test_handle_success(self): backend = StartEnterpriseBackend() queued_sms = QueuedSMS() backend.handle_response(queued_sms, 200, SUCCESSFUL_RESPONSE) self.assertEqual(queued_sms.backend_message_id, SUCCESSFUL_RESPONSE) self.assertFalse(queued_sms.error)
def test_handle_failure(self): backend = IvoryCoastMTNBackend() queued_sms = QueuedSMS() with self.assertRaises(IvoryCoastMTNError): backend.handle_response(queued_sms, 200, ERROR_RESPONSE) self.assertEqual(queued_sms.backend_message_id, '1756d6f2-e15f-4397-9670-47b98e92ba62')
def get_items_to_be_processed(self, utcnow): for sms in QueuedSMS.get_queued_sms(): if DATA_MIGRATION.enabled(sms.domain): continue yield { 'id': sms.pk, 'key': json_format_datetime(sms.datetime_to_process), }
def setUp(self): self.vertex_backend = VertexBackend() self.vertex_backend.set_extra_fields( username=TEST_USERNAME, password=TEST_PASSWORD, senderid=TEST_SENDER_ID, ) self.queued_sms = QueuedSMS(phone_number=TEST_PHONE_NUMBER, )
def test_get_sms_from_queued_sms(): test_data = get_test_sms_fields("test_domain", datetime.utcnow(), 123) expected = _get_sms_fields_to_copy() eq(set(test_data), expected, set(test_data) ^ expected) queued_sms = QueuedSMS(**test_data) sms = get_sms_from_queued_sms(queued_sms) for field, value in test_data.items(): eq(getattr(sms, field), value, field)
def test_handle_success(self): backend = StartEnterpriseBackend() queued_sms = QueuedSMS(couch_id=uuid.uuid4().hex) backend.handle_response(queued_sms, 200, SUCCESSFUL_RESPONSE) dlr = StartEnterpriseDeliveryReceipt.objects.get( sms_id=queued_sms.couch_id) self.addCleanup(dlr.delete) self.assertEqual(dlr.message_id, SUCCESSFUL_RESPONSE) self.assertFalse(queued_sms.error)
def test_unicode(self): queued_sms = QueuedSMS( phone_number=TEST_PHONE_NUMBER, text=TEST_UNICODE_MESSAGE, ) self.assertEqual( self.backend.get_params(queued_sms), { 'usr': TEST_USERNAME, 'pass': TEST_PASSWORD, 'msisdn': strip_plus(TEST_PHONE_NUMBER), 'sid': TEST_SENDER_ID, 'mt': LONG_UNICODE_MSG_TYPE, 'msg': '0928092E0938094D09240947', })
def test_handle_response(self): with self.assertRaises(KarixError): KarixBackend.handle_response(QueuedSMS(), MockResponse(500, {})) with self.assertRaises(KarixError): KarixBackend.handle_response(QueuedSMS(), MockResponse(200, {})) with self.assertRaises(KarixError): KarixBackend.handle_response( QueuedSMS(), MockResponse(200, {'status': { 'code': '-999' }})) msg_obj = QueuedSMS() KarixBackend.handle_response( msg_obj, MockResponse(200, { 'ackid': '123', 'status': { 'code': '200' } })) self.assertEqual(msg_obj.backend_message_id, '123')
def test_ascii(self): queued_sms = QueuedSMS( phone_number=TEST_PHONE_NUMBER, text=TEST_TEXT_MESSAGE, ) self.assertEqual( self.backend.get_params(queued_sms), { 'usr': TEST_USERNAME, 'pass': TEST_PASSWORD, 'msisdn': strip_plus(TEST_PHONE_NUMBER), 'sid': TEST_SENDER_ID, 'mt': LONG_TEXT_MSG_TYPE, 'msg': TEST_TEXT_MESSAGE, })
def setUp(self): self.vertex_backend = VertexBackend() self.queued_sms = QueuedSMS()
def create_tasks(self): for queued_sms in QueuedSMS.get_queued_sms(): if queued_sms.domain and skip_domain(queued_sms.domain): continue self.enqueue(queued_sms)
def get_items_to_be_processed(self, utcnow): for sms in QueuedSMS.get_queued_sms(): yield { 'id': sms.pk, 'key': json_format_datetime(sms.datetime_to_process), }