def create_backend(): backend = TestSMSBackend(domain=None, name=TEST_BACKEND, authorized_domains=[], is_global=True) backend._id = backend.name backend.save() sms_backend_mapping = BackendMapping(is_global=True, prefix="*", backend_id=backend.get_id) sms_backend_mapping.save() return sms_backend_mapping, backend
class BaseReminderTestCase(BaseAccountingTest): def setUp(self): super(BaseReminderTestCase, self).setUp() self.domain_obj = Domain(name="test") self.domain_obj.save() # Prevent resource conflict self.domain_obj = Domain.get(self.domain_obj._id) self.account, _ = BillingAccount.get_or_create_account_by_domain( self.domain_obj.name, created_by="tests" ) advanced_plan_version = DefaultProductPlan.get_default_plan_by_domain( self.domain_obj, edition=SoftwarePlanEdition.ADVANCED) self.subscription = Subscription.new_domain_subscription( self.account, self.domain_obj.name, advanced_plan_version ) self.subscription.is_active = True self.subscription.save() self.sms_backend = TestSMSBackend(named="MOBILE_BACKEND_TEST", is_global=True) self.sms_backend.save() self.sms_backend_mapping = BackendMapping(is_global=True,prefix="*",backend_id=self.sms_backend._id) self.sms_backend_mapping.save() def tearDown(self): self.sms_backend_mapping.delete() self.sms_backend.delete() SubscriptionAdjustment.objects.all().delete() self.subscription.delete() self.account.delete() self.domain_obj.delete()
class OptTestCase(BaseAccountingTest): def setUp(self): super(OptTestCase, self).setUp() self.domain = "opt-test" self.domain_obj = Domain(name=self.domain) self.domain_obj.save() generator.instantiate_accounting_for_tests() self.account = BillingAccount.get_or_create_account_by_domain( self.domain_obj.name, created_by="automated-test" )[0] plan = DefaultProductPlan.get_default_plan_by_domain(self.domain_obj, edition=SoftwarePlanEdition.ADVANCED) self.subscription = Subscription.new_domain_subscription(self.account, self.domain_obj.name, plan) self.subscription.is_active = True self.subscription.save() self.backend = TestSMSBackend(is_global=True) self.backend.save() self.backend_mapping = BackendMapping(is_global=True, prefix="*", backend_id=self.backend._id) self.backend_mapping.save() def test_opt_out_and_opt_in(self): self.assertEqual(PhoneNumber.objects.count(), 0) incoming("99912345678", "stop", "GVI") self.assertEqual(PhoneNumber.objects.count(), 1) phone_number = PhoneNumber.objects.get(phone_number="99912345678") self.assertFalse(phone_number.send_sms) incoming("99912345678", "start", "GVI") self.assertEqual(PhoneNumber.objects.count(), 1) phone_number = PhoneNumber.objects.get(phone_number="99912345678") self.assertTrue(phone_number.send_sms) def test_sending_to_opted_out_number(self): self.assertEqual(PhoneNumber.objects.count(), 0) self.assertTrue(send_sms(self.domain, None, "999123456789", "hello")) incoming("999123456789", "stop", "GVI") self.assertEqual(PhoneNumber.objects.count(), 1) phone_number = PhoneNumber.objects.get(phone_number="999123456789") self.assertFalse(phone_number.send_sms) self.assertFalse(send_sms(self.domain, None, "999123456789", "hello")) def tearDown(self): self.backend_mapping.delete() self.backend.delete() self.domain_obj.delete() SubscriptionAdjustment.objects.all().delete() self.subscription.delete() self.account.delete()
def global_backend_map(request): backend_classes = get_available_backends() global_backends = SMSBackend.view( "sms/global_backends", classes=backend_classes, include_docs=True, reduce=False ).all() current_map = {} catchall_entry = None for entry in BackendMapping.view("sms/backend_map", startkey=["*"], endkey=["*", {}], include_docs=True).all(): if entry.prefix == "*": catchall_entry = entry else: current_map[entry.prefix] = entry if request.method == "POST": form = BackendMapForm(request.POST) if form.is_valid(): new_backend_map = form.cleaned_data.get("backend_map") new_catchall_backend_id = form.cleaned_data.get("catchall_backend_id") for prefix, entry in current_map.items(): if prefix not in new_backend_map: current_map[prefix].delete() del current_map[prefix] for prefix, backend_id in new_backend_map.items(): if prefix in current_map: current_map[prefix].backend_id = backend_id current_map[prefix].save() else: current_map[prefix] = BackendMapping(is_global=True, prefix=prefix, backend_id=backend_id) current_map[prefix].save() if new_catchall_backend_id is None: if catchall_entry is not None: catchall_entry.delete() catchall_entry = None else: if catchall_entry is None: catchall_entry = BackendMapping(is_global=True, prefix="*", backend_id=new_catchall_backend_id) else: catchall_entry.backend_id = new_catchall_backend_id catchall_entry.save() messages.success(request, _("Changes Saved.")) else: initial = { "catchall_backend_id" : catchall_entry.backend_id if catchall_entry is not None else None, "backend_map" : [{"prefix" : prefix, "backend_id" : entry.backend_id} for prefix, entry in current_map.items()], } form = BackendMapForm(initial=initial) context = { "backends" : global_backends, "form" : form, } return render(request, "sms/backend_map.html", context)
class BaseReminderTestCase(TestCase): def setUp(self): self.domain_obj = Domain(name="test") self.domain_obj.save() # Prevent resource conflict self.domain_obj = Domain.get(self.domain_obj._id) self.sms_backend = TestSMSBackend(named="MOBILE_BACKEND_TEST", is_global=True) self.sms_backend.save() self.sms_backend_mapping = BackendMapping(is_global=True,prefix="*",backend_id=self.sms_backend._id) self.sms_backend_mapping.save() def tearDown(self): self.sms_backend_mapping.delete() self.sms_backend.delete() self.domain_obj.delete()
class OptTestCase(TestCase): def setUp(self): self.domain = "opt-test" self.domain_obj = Domain(name=self.domain) self.domain_obj.save() self.backend = TestSMSBackend(is_global=True) self.backend.save() self.backend_mapping = BackendMapping( is_global=True, prefix="*", backend_id=self.backend._id, ) self.backend_mapping.save() def test_opt_out_and_opt_in(self): self.assertEqual(PhoneNumber.objects.count(), 0) incoming("99912345678", "stop", "GVI") self.assertEqual(PhoneNumber.objects.count(), 1) phone_number = PhoneNumber.objects.get(phone_number="99912345678") self.assertFalse(phone_number.send_sms) incoming("99912345678", "start", "GVI") self.assertEqual(PhoneNumber.objects.count(), 1) phone_number = PhoneNumber.objects.get(phone_number="99912345678") self.assertTrue(phone_number.send_sms) def test_sending_to_opted_out_number(self): self.assertEqual(PhoneNumber.objects.count(), 0) self.assertTrue(send_sms(self.domain, None, "999123456789", "hello")) incoming("999123456789", "stop", "GVI") self.assertEqual(PhoneNumber.objects.count(), 1) phone_number = PhoneNumber.objects.get(phone_number="999123456789") self.assertFalse(phone_number.send_sms) self.assertFalse(send_sms(self.domain, None, "999123456789", "hello")) def tearDown(self): self.backend_mapping.delete() self.backend.delete() self.domain_obj.delete()
class TouchformsTestCase(LiveServerTestCase): """ For now, these test cases need to be run manually. Before running, the following dependencies must be met: 1. touchforms/backend/localsettings.py: URL_ROOT = "http://localhost:8081/a/{{DOMAIN}}" 2. Django localsettings.py: TOUCHFORMS_API_USER = "******" TOUCHFORMS_API_PASSWORD = "******" 3. Start touchforms """ users = None apps = None keywords = None groups = None def create_domain(self, domain): domain_obj = Domain(name=domain) domain_obj.use_default_sms_response = True domain_obj.default_sms_response = "Default SMS Response" domain_obj.save() generator.instantiate_accounting_for_tests() self.account = BillingAccount.get_or_create_account_by_domain( domain_obj.name, created_by="automated-test", )[0] plan = DefaultProductPlan.get_default_plan_by_domain( domain_obj, edition=SoftwarePlanEdition.ADVANCED ) self.subscription = Subscription.new_domain_subscription( self.account, domain_obj.name, plan ) self.subscription.is_active = True self.subscription.save() return domain_obj def create_mobile_worker(self, username, password, phone_number, save_vn=True): user = CommCareUser.create(self.domain, username, password, phone_number=phone_number) if save_vn: user.save_verified_number(self.domain, phone_number, True, None) self.users.append(user) return user def update_case_owner(self, case, owner): case_block = CaseBlock( create=False, case_id=case._id, case_type='participant', owner_id=owner._id, user_id=owner._id, version=V2 ).as_xml(format_datetime=json_format_datetime) post_case_blocks([case_block], {'domain': self.domain}) def add_parent_access(self, user, case): case_block = CaseBlock( create=True, case_id=uuid.uuid4().hex, case_type='magic_map', owner_id=user._id, version=V2, index={'parent': ('participant', case._id)} ).as_xml(format_datetime=json_format_datetime) post_case_blocks([case_block], {'domain': self.domain}) def create_web_user(self, username, password): user = WebUser.create(self.domain, username, password) self.users.append(user) return user def create_group(self, name, users): group = Group( domain=self.domain, name=name, users=[user._id for user in users], case_sharing=True, ) group.save() self.groups.append(group) return group def load_app(self, filename, dirname=None): dirname = dirname or os.path.dirname(os.path.abspath(__file__)) full_filename = "%s/%s" % (dirname, filename) with open(full_filename, "r") as f: app_source = f.read() app_source = json.loads(app_source) app = import_app(app_source, self.domain) self.apps.append(app) return app def create_sms_keyword(self, keyword, reply_sms, override_open_sessions=True, initiator_filter=None, recipient=RECIPIENT_SENDER, recipient_id=None): sk = SurveyKeyword( domain=self.domain, keyword=keyword, description=keyword, actions=[SurveyKeywordAction( recipient=recipient, recipient_id=recipient_id, action=METHOD_SMS, message_content=reply_sms, form_unique_id=None, use_named_args=False, named_args={}, named_args_separator=None, )], delimiter=None, override_open_sessions=override_open_sessions, initiator_doc_type_filter=initiator_filter or [], ) sk.save() self.keywords.append(sk) return sk def create_survey_keyword(self, keyword, form_unique_id, delimiter=None, override_open_sessions=True, initiator_filter=None): sk = SurveyKeyword( domain=self.domain, keyword=keyword, description=keyword, actions=[SurveyKeywordAction( recipient=RECIPIENT_SENDER, recipient_id=None, action=METHOD_SMS_SURVEY, message_content=None, form_unique_id=form_unique_id, use_named_args=False, named_args={}, named_args_separator=None, )], delimiter=delimiter, override_open_sessions=override_open_sessions, initiator_doc_type_filter=initiator_filter or [], ) sk.save() self.keywords.append(sk) return sk def create_structured_sms_keyword(self, keyword, form_unique_id, reply_sms, delimiter=None, named_args=None, named_args_separator=None, override_open_sessions=True, initiator_filter=None): sk = SurveyKeyword( domain=self.domain, keyword=keyword, description=keyword, actions=[ SurveyKeywordAction( recipient=RECIPIENT_SENDER, recipient_id=None, action=METHOD_SMS, message_content=reply_sms, form_unique_id=None, use_named_args=False, named_args={}, named_args_separator=None, ), SurveyKeywordAction( recipient=RECIPIENT_SENDER, recipient_id=None, action=METHOD_STRUCTURED_SMS, message_content=None, form_unique_id=form_unique_id, use_named_args=(named_args is not None), named_args=(named_args or {}), named_args_separator=named_args_separator, ) ], delimiter=delimiter, override_open_sessions=override_open_sessions, initiator_doc_type_filter=initiator_filter or [], ) sk.save() self.keywords.append(sk) return sk def create_site(self): site = Site(id=settings.SITE_ID, domain=self.live_server_url, name=self.live_server_url) site.save() return site def get_case(self, external_id): return get_one_case_in_domain_by_external_id(self.domain, external_id) def assertCasePropertyEquals(self, case, prop, value): self.assertEquals(case.get_case_property(prop), value) def get_last_form_submission(self): result = get_forms_by_type(self.domain, 'XFormInstance', recent_first=True, limit=1) return result[0] if len(result) > 0 else None def assertNoNewSubmission(self, last_submission): new_submission = self.get_last_form_submission() self.assertEquals(last_submission._id, new_submission._id) def assertFormQuestionEquals(self, form, question, value, cast=None): self.assertIn(question, form.form) form_value = form.form[question] if cast: form_value = cast(form_value) self.assertEquals(form_value, value) def get_last_outbound_sms(self, contact): # Not clear why this should be necessary, but without it the latest # sms may not be returned sleep(0.25) sms = SMSLog.view("sms/by_recipient", startkey=[contact.doc_type, contact._id, "SMSLog", "O", {}], endkey=[contact.doc_type, contact._id, "SMSLog", "O"], descending=True, include_docs=True, reduce=False, ).first() return sms def get_last_outbound_call(self, contact): # Not clear why this should be necessary, but without it the latest # call may not be returned sleep(0.25) call = CallLog.view("sms/by_recipient", startkey=[contact.doc_type, contact._id, "CallLog", "O", {}], endkey=[contact.doc_type, contact._id, "CallLog", "O"], descending=True, include_docs=True, reduce=False, ).first() return call def get_open_session(self, contact): return SQLXFormsSession.get_open_sms_session(self.domain, contact._id) def assertLastOutboundSMSEquals(self, contact, message): sms = self.get_last_outbound_sms(contact) self.assertIsNotNone(sms) self.assertEqual(sms.text, message) return sms def assertMetadataEqual(self, sms, xforms_session_couch_id=None, workflow=None): if xforms_session_couch_id: self.assertEqual(sms.xforms_session_couch_id, xforms_session_couch_id) if workflow: self.assertEqual(sms.workflow, workflow) def setUp(self): self.users = [] self.apps = [] self.keywords = [] self.groups = [] self.site = self.create_site() self.domain = "test-domain" self.domain_obj = self.create_domain(self.domain) self.create_web_user("touchforms_user", "123") self.backend = TestSMSBackend(name="TEST", is_global=True) self.backend.save() self.backend_mapping = BackendMapping(is_global=True, prefix="*", backend_id=self.backend._id) self.backend_mapping.save() settings.DEBUG = True def tearDown(self): for user in self.users: user.delete_verified_number() user.delete() for app in self.apps: app.delete() for keyword in self.keywords: keyword.delete() for group in self.groups: group.delete() self.domain_obj.delete() self.site.delete() self.backend.delete() self.backend_mapping.delete() SubscriptionAdjustment.objects.all().delete() self.subscription.delete() self.account.delete()
class BackendTestCase(TestCase): def setUp(self): self.domain = "test-domain" self.domain2 = "test-domain2" self.domain_obj = Domain(name=self.domain) self.domain_obj.save() self.domain_obj = Domain.get( self.domain_obj._id) # Prevent resource conflict self.backend1 = TestCaseBackend(name="BACKEND1", is_global=True) self.backend1.save() self.backend2 = TestCaseBackend(name="BACKEND2", is_global=True) self.backend2.save() self.backend3 = TestCaseBackend(name="BACKEND3", is_global=True) self.backend3.save() self.backend4 = TestCaseBackend(name="BACKEND4", is_global=True) self.backend4.save() self.backend5 = TestCaseBackend(name="BACKEND5", domain=self.domain, is_global=False, authorized_domains=[]) self.backend5.save() self.backend6 = TestCaseBackend(name="BACKEND6", domain=self.domain2, is_global=False, authorized_domains=[self.domain]) self.backend6.save() self.backend7 = TestCaseBackend(name="BACKEND7", domain=self.domain2, is_global=False, authorized_domains=[]) self.backend7.save() self.backend8 = TestCaseBackend(name="BACKEND", domain=self.domain, is_global=False, authorized_domains=[]) self.backend8.save() self.backend9 = TestCaseBackend(name="BACKEND", domain=self.domain2, is_global=False, authorized_domains=[self.domain]) self.backend9.save() self.backend10 = TestCaseBackend(name="BACKEND", is_global=True) self.backend10.save() self.backend_mapping1 = BackendMapping(is_global=True, prefix="*", backend_id=self.backend1._id) self.backend_mapping1.save() self.backend_mapping2 = BackendMapping(is_global=True, prefix="1", backend_id=self.backend2._id) self.backend_mapping2.save() self.backend_mapping3 = BackendMapping(is_global=True, prefix="91", backend_id=self.backend3._id) self.backend_mapping3.save() self.backend_mapping4 = BackendMapping(is_global=True, prefix="265", backend_id=self.backend4._id) self.backend_mapping4.save() self.case = CommCareCase(domain=self.domain) self.case.set_case_property("contact_phone_number", "15551234567") self.case.set_case_property("contact_phone_number_is_verified", "1") self.case.save() self.contact = CommConnectCase.wrap(self.case.to_json()) settings.SMS_LOADED_BACKENDS.append( "corehq.apps.sms.tests.TestCaseBackend") def tearDown(self): self.backend1.delete_invoke_doc() self.backend1.delete() self.backend_mapping1.delete() self.backend2.delete_invoke_doc() self.backend2.delete() self.backend_mapping2.delete() self.backend3.delete_invoke_doc() self.backend3.delete() self.backend_mapping3.delete() self.backend4.delete_invoke_doc() self.backend4.delete() self.backend_mapping4.delete() self.backend5.delete_invoke_doc() self.backend5.delete() self.backend6.delete_invoke_doc() self.backend6.delete() self.backend7.delete_invoke_doc() self.backend7.delete() self.contact.delete_verified_number() self.case.delete() self.domain_obj.delete() settings.SMS_LOADED_BACKENDS.pop() def test_backend(self): # Test the backend map self.assertTrue( send_sms(self.domain, None, "15551234567", "Test for BACKEND2")) self.assertFalse(self.backend1.invoke_doc_exists()) self.assertTrue(self.backend2.invoke_doc_exists()) self.assertFalse(self.backend3.invoke_doc_exists()) self.assertFalse(self.backend4.invoke_doc_exists()) self.assertFalse(self.backend5.invoke_doc_exists()) self.assertFalse(self.backend6.invoke_doc_exists()) self.assertFalse(self.backend7.invoke_doc_exists()) self.assertFalse(self.backend8.invoke_doc_exists()) self.assertFalse(self.backend9.invoke_doc_exists()) self.assertFalse(self.backend10.invoke_doc_exists()) self.backend2.delete_invoke_doc() self.assertFalse(self.backend2.invoke_doc_exists()) self.assertTrue( send_sms(self.domain, None, "9100000000", "Test for BACKEND3")) self.assertFalse(self.backend1.invoke_doc_exists()) self.assertFalse(self.backend2.invoke_doc_exists()) self.assertTrue(self.backend3.invoke_doc_exists()) self.assertFalse(self.backend4.invoke_doc_exists()) self.assertFalse(self.backend5.invoke_doc_exists()) self.assertFalse(self.backend6.invoke_doc_exists()) self.assertFalse(self.backend7.invoke_doc_exists()) self.assertFalse(self.backend8.invoke_doc_exists()) self.assertFalse(self.backend9.invoke_doc_exists()) self.assertFalse(self.backend10.invoke_doc_exists()) self.backend3.delete_invoke_doc() self.assertFalse(self.backend3.invoke_doc_exists()) self.assertTrue( send_sms(self.domain, None, "26500000000", "Test for BACKEND4")) self.assertFalse(self.backend1.invoke_doc_exists()) self.assertFalse(self.backend2.invoke_doc_exists()) self.assertFalse(self.backend3.invoke_doc_exists()) self.assertTrue(self.backend4.invoke_doc_exists()) self.assertFalse(self.backend5.invoke_doc_exists()) self.assertFalse(self.backend6.invoke_doc_exists()) self.assertFalse(self.backend7.invoke_doc_exists()) self.assertFalse(self.backend8.invoke_doc_exists()) self.assertFalse(self.backend9.invoke_doc_exists()) self.assertFalse(self.backend10.invoke_doc_exists()) self.backend4.delete_invoke_doc() self.assertFalse(self.backend4.invoke_doc_exists()) self.assertTrue( send_sms(self.domain, None, "25800000000", "Test for BACKEND1")) self.assertTrue(self.backend1.invoke_doc_exists()) self.assertFalse(self.backend2.invoke_doc_exists()) self.assertFalse(self.backend3.invoke_doc_exists()) self.assertFalse(self.backend4.invoke_doc_exists()) self.assertFalse(self.backend5.invoke_doc_exists()) self.assertFalse(self.backend6.invoke_doc_exists()) self.assertFalse(self.backend7.invoke_doc_exists()) self.assertFalse(self.backend8.invoke_doc_exists()) self.assertFalse(self.backend9.invoke_doc_exists()) self.assertFalse(self.backend10.invoke_doc_exists()) self.backend1.delete_invoke_doc() self.assertFalse(self.backend1.invoke_doc_exists()) # Test overriding with a domain-level backend self.domain_obj.default_sms_backend_id = self.backend5._id self.domain_obj.save() self.assertTrue( send_sms(self.domain, None, "15551234567", "Test for BACKEND5")) self.assertFalse(self.backend1.invoke_doc_exists()) self.assertFalse(self.backend2.invoke_doc_exists()) self.assertFalse(self.backend3.invoke_doc_exists()) self.assertFalse(self.backend4.invoke_doc_exists()) self.assertTrue(self.backend5.invoke_doc_exists()) self.assertFalse(self.backend6.invoke_doc_exists()) self.assertFalse(self.backend7.invoke_doc_exists()) self.assertFalse(self.backend8.invoke_doc_exists()) self.assertFalse(self.backend9.invoke_doc_exists()) self.assertFalse(self.backend10.invoke_doc_exists()) self.backend5.delete_invoke_doc() self.assertFalse(self.backend5.invoke_doc_exists()) # Test use of backend that another domain owns but has granted access self.domain_obj.default_sms_backend_id = self.backend6._id self.domain_obj.save() self.assertTrue( send_sms(self.domain, None, "25800000000", "Test for BACKEND6")) self.assertFalse(self.backend1.invoke_doc_exists()) self.assertFalse(self.backend2.invoke_doc_exists()) self.assertFalse(self.backend3.invoke_doc_exists()) self.assertFalse(self.backend4.invoke_doc_exists()) self.assertFalse(self.backend5.invoke_doc_exists()) self.assertTrue(self.backend6.invoke_doc_exists()) self.assertFalse(self.backend7.invoke_doc_exists()) self.assertFalse(self.backend8.invoke_doc_exists()) self.assertFalse(self.backend9.invoke_doc_exists()) self.assertFalse(self.backend10.invoke_doc_exists()) self.backend6.delete_invoke_doc() self.assertFalse(self.backend6.invoke_doc_exists()) # Test backend access control self.domain_obj.default_sms_backend_id = self.backend7._id self.domain_obj.save() self.assertFalse( send_sms(self.domain, None, "25800000000", "Test for BACKEND7")) self.assertFalse(self.backend1.invoke_doc_exists()) self.assertFalse(self.backend2.invoke_doc_exists()) self.assertFalse(self.backend3.invoke_doc_exists()) self.assertFalse(self.backend4.invoke_doc_exists()) self.assertFalse(self.backend5.invoke_doc_exists()) self.assertFalse(self.backend6.invoke_doc_exists()) self.assertFalse(self.backend7.invoke_doc_exists()) self.assertFalse(self.backend8.invoke_doc_exists()) self.assertFalse(self.backend9.invoke_doc_exists()) self.assertFalse(self.backend10.invoke_doc_exists()) # Test sending to verified number with backend map self.domain_obj.default_sms_backend_id = None self.domain_obj.save() verified_number = self.contact.get_verified_number() self.assertTrue(verified_number is not None) self.assertTrue(verified_number.backend_id is None) self.assertEqual(verified_number.phone_number, "15551234567") self.assertTrue( send_sms_to_verified_number(verified_number, "Test for BACKEND2")) self.assertFalse(self.backend1.invoke_doc_exists()) self.assertTrue(self.backend2.invoke_doc_exists()) self.assertFalse(self.backend3.invoke_doc_exists()) self.assertFalse(self.backend4.invoke_doc_exists()) self.assertFalse(self.backend5.invoke_doc_exists()) self.assertFalse(self.backend6.invoke_doc_exists()) self.assertFalse(self.backend7.invoke_doc_exists()) self.assertFalse(self.backend8.invoke_doc_exists()) self.assertFalse(self.backend9.invoke_doc_exists()) self.assertFalse(self.backend10.invoke_doc_exists()) self.backend2.delete_invoke_doc() self.assertFalse(self.backend2.invoke_doc_exists()) # Test sending to verified number with default domain backend self.domain_obj.default_sms_backend_id = self.backend5._id self.domain_obj.save() self.assertTrue( send_sms_to_verified_number(verified_number, "Test for BACKEND5")) self.assertFalse(self.backend1.invoke_doc_exists()) self.assertFalse(self.backend2.invoke_doc_exists()) self.assertFalse(self.backend3.invoke_doc_exists()) self.assertFalse(self.backend4.invoke_doc_exists()) self.assertTrue(self.backend5.invoke_doc_exists()) self.assertFalse(self.backend6.invoke_doc_exists()) self.assertFalse(self.backend7.invoke_doc_exists()) self.assertFalse(self.backend8.invoke_doc_exists()) self.assertFalse(self.backend9.invoke_doc_exists()) self.assertFalse(self.backend10.invoke_doc_exists()) self.backend5.delete_invoke_doc() self.assertFalse(self.backend5.invoke_doc_exists()) # Test sending to verified number with a contact-level backend owned by the domain self.case.set_case_property("contact_backend_id", "BACKEND") self.case.save() self.contact = CommConnectCase.wrap(self.case.to_json()) verified_number = self.contact.get_verified_number() self.assertTrue(verified_number is not None) self.assertEqual(verified_number.backend_id, "BACKEND") self.assertEqual(verified_number.phone_number, "15551234567") self.assertTrue( send_sms_to_verified_number(verified_number, "Test for BACKEND")) self.assertFalse(self.backend1.invoke_doc_exists()) self.assertFalse(self.backend2.invoke_doc_exists()) self.assertFalse(self.backend3.invoke_doc_exists()) self.assertFalse(self.backend4.invoke_doc_exists()) self.assertFalse(self.backend5.invoke_doc_exists()) self.assertFalse(self.backend6.invoke_doc_exists()) self.assertFalse(self.backend7.invoke_doc_exists()) self.assertTrue(self.backend8.invoke_doc_exists()) self.assertFalse(self.backend9.invoke_doc_exists()) self.assertFalse(self.backend10.invoke_doc_exists()) self.backend8.delete_invoke_doc() self.assertFalse(self.backend8.invoke_doc_exists()) # Test sending to verified number with a contact-level backend granted to the domain by another domain self.backend8.delete() self.assertTrue( send_sms_to_verified_number(verified_number, "Test for BACKEND")) self.assertFalse(self.backend1.invoke_doc_exists()) self.assertFalse(self.backend2.invoke_doc_exists()) self.assertFalse(self.backend3.invoke_doc_exists()) self.assertFalse(self.backend4.invoke_doc_exists()) self.assertFalse(self.backend5.invoke_doc_exists()) self.assertFalse(self.backend6.invoke_doc_exists()) self.assertFalse(self.backend7.invoke_doc_exists()) self.assertTrue(self.backend9.invoke_doc_exists()) self.assertFalse(self.backend10.invoke_doc_exists()) self.backend9.delete_invoke_doc() self.assertFalse(self.backend9.invoke_doc_exists()) # Test sending to verified number with a contact-level global backend self.backend9.delete() self.assertTrue( send_sms_to_verified_number(verified_number, "Test for BACKEND")) self.assertFalse(self.backend1.invoke_doc_exists()) self.assertFalse(self.backend2.invoke_doc_exists()) self.assertFalse(self.backend3.invoke_doc_exists()) self.assertFalse(self.backend4.invoke_doc_exists()) self.assertFalse(self.backend5.invoke_doc_exists()) self.assertFalse(self.backend6.invoke_doc_exists()) self.assertFalse(self.backend7.invoke_doc_exists()) self.assertTrue(self.backend10.invoke_doc_exists()) self.backend10.delete_invoke_doc() self.assertFalse(self.backend10.invoke_doc_exists()) # Test raising exception if contact-level backend is not found self.backend10.delete() try: self.assertTrue( send_sms_to_verified_number(verified_number, "Test for BACKEND")) except BadSMSConfigException: pass else: self.assertTrue(False) # Test send_sms_with_backend self.assertTrue( send_sms_with_backend(self.domain, "+15551234567", "Test for BACKEND3", self.backend3._id)) self.assertFalse(self.backend1.invoke_doc_exists()) self.assertFalse(self.backend2.invoke_doc_exists()) self.assertTrue(self.backend3.invoke_doc_exists()) self.assertFalse(self.backend4.invoke_doc_exists()) self.assertFalse(self.backend5.invoke_doc_exists()) self.assertFalse(self.backend6.invoke_doc_exists()) self.assertFalse(self.backend7.invoke_doc_exists()) self.backend3.delete_invoke_doc() self.assertFalse(self.backend3.invoke_doc_exists()) # Test send_sms_with_backend_name self.assertTrue( send_sms_with_backend_name(self.domain, "+15551234567", "Test for BACKEND3", "BACKEND3")) self.assertFalse(self.backend1.invoke_doc_exists()) self.assertFalse(self.backend2.invoke_doc_exists()) self.assertTrue(self.backend3.invoke_doc_exists()) self.assertFalse(self.backend4.invoke_doc_exists()) self.assertFalse(self.backend5.invoke_doc_exists()) self.assertFalse(self.backend6.invoke_doc_exists()) self.assertFalse(self.backend7.invoke_doc_exists()) self.backend3.delete_invoke_doc() self.assertFalse(self.backend3.invoke_doc_exists())
class BackendTestCase(TestCase): def setUp(self): self.domain = "test-domain" self.domain2 = "test-domain2" self.domain_obj = Domain(name=self.domain) self.domain_obj.save() self.domain_obj = Domain.get(self.domain_obj._id) # Prevent resource conflict self.backend1 = TestCaseBackend(name="BACKEND1",is_global=True) self.backend1.save() self.backend2 = TestCaseBackend(name="BACKEND2",is_global=True) self.backend2.save() self.backend3 = TestCaseBackend(name="BACKEND3",is_global=True) self.backend3.save() self.backend4 = TestCaseBackend(name="BACKEND4",is_global=True) self.backend4.save() self.backend5 = TestCaseBackend(name="BACKEND5",domain=self.domain,is_global=False,authorized_domains=[]) self.backend5.save() self.backend6 = TestCaseBackend(name="BACKEND6",domain=self.domain2,is_global=False,authorized_domains=[self.domain]) self.backend6.save() self.backend7 = TestCaseBackend(name="BACKEND7",domain=self.domain2,is_global=False,authorized_domains=[]) self.backend7.save() self.backend8 = TestCaseBackend(name="BACKEND",domain=self.domain,is_global=False,authorized_domains=[]) self.backend8.save() self.backend9 = TestCaseBackend(name="BACKEND",domain=self.domain2,is_global=False,authorized_domains=[self.domain]) self.backend9.save() self.backend10 = TestCaseBackend(name="BACKEND",is_global=True) self.backend10.save() self.backend_mapping1 = BackendMapping(is_global=True,prefix="*",backend_id=self.backend1._id) self.backend_mapping1.save() self.backend_mapping2 = BackendMapping(is_global=True,prefix="1",backend_id=self.backend2._id) self.backend_mapping2.save() self.backend_mapping3 = BackendMapping(is_global=True,prefix="91",backend_id=self.backend3._id) self.backend_mapping3.save() self.backend_mapping4 = BackendMapping(is_global=True,prefix="265",backend_id=self.backend4._id) self.backend_mapping4.save() self.case = CommCareCase(domain=self.domain) self.case.set_case_property("contact_phone_number","15551234567") self.case.set_case_property("contact_phone_number_is_verified", "1") self.case.save() self.contact = CommConnectCase.wrap(self.case.to_json()) settings.SMS_LOADED_BACKENDS.append("corehq.apps.sms.tests.TestCaseBackend") def tearDown(self): self.backend1.delete_invoke_doc() self.backend1.delete() self.backend_mapping1.delete() self.backend2.delete_invoke_doc() self.backend2.delete() self.backend_mapping2.delete() self.backend3.delete_invoke_doc() self.backend3.delete() self.backend_mapping3.delete() self.backend4.delete_invoke_doc() self.backend4.delete() self.backend_mapping4.delete() self.backend5.delete_invoke_doc() self.backend5.delete() self.backend6.delete_invoke_doc() self.backend6.delete() self.backend7.delete_invoke_doc() self.backend7.delete() self.contact.delete_verified_number() self.case.delete() self.domain_obj.delete() settings.SMS_LOADED_BACKENDS.pop() def test_backend(self): # Test the backend map self.assertTrue(send_sms(self.domain, None, "15551234567", "Test for BACKEND2")) self.assertFalse(self.backend1.invoke_doc_exists()) self.assertTrue(self.backend2.invoke_doc_exists()) self.assertFalse(self.backend3.invoke_doc_exists()) self.assertFalse(self.backend4.invoke_doc_exists()) self.assertFalse(self.backend5.invoke_doc_exists()) self.assertFalse(self.backend6.invoke_doc_exists()) self.assertFalse(self.backend7.invoke_doc_exists()) self.assertFalse(self.backend8.invoke_doc_exists()) self.assertFalse(self.backend9.invoke_doc_exists()) self.assertFalse(self.backend10.invoke_doc_exists()) self.backend2.delete_invoke_doc() self.assertFalse(self.backend2.invoke_doc_exists()) self.assertTrue(send_sms(self.domain, None, "9100000000", "Test for BACKEND3")) self.assertFalse(self.backend1.invoke_doc_exists()) self.assertFalse(self.backend2.invoke_doc_exists()) self.assertTrue(self.backend3.invoke_doc_exists()) self.assertFalse(self.backend4.invoke_doc_exists()) self.assertFalse(self.backend5.invoke_doc_exists()) self.assertFalse(self.backend6.invoke_doc_exists()) self.assertFalse(self.backend7.invoke_doc_exists()) self.assertFalse(self.backend8.invoke_doc_exists()) self.assertFalse(self.backend9.invoke_doc_exists()) self.assertFalse(self.backend10.invoke_doc_exists()) self.backend3.delete_invoke_doc() self.assertFalse(self.backend3.invoke_doc_exists()) self.assertTrue(send_sms(self.domain, None, "26500000000", "Test for BACKEND4")) self.assertFalse(self.backend1.invoke_doc_exists()) self.assertFalse(self.backend2.invoke_doc_exists()) self.assertFalse(self.backend3.invoke_doc_exists()) self.assertTrue(self.backend4.invoke_doc_exists()) self.assertFalse(self.backend5.invoke_doc_exists()) self.assertFalse(self.backend6.invoke_doc_exists()) self.assertFalse(self.backend7.invoke_doc_exists()) self.assertFalse(self.backend8.invoke_doc_exists()) self.assertFalse(self.backend9.invoke_doc_exists()) self.assertFalse(self.backend10.invoke_doc_exists()) self.backend4.delete_invoke_doc() self.assertFalse(self.backend4.invoke_doc_exists()) self.assertTrue(send_sms(self.domain, None, "25800000000", "Test for BACKEND1")) self.assertTrue(self.backend1.invoke_doc_exists()) self.assertFalse(self.backend2.invoke_doc_exists()) self.assertFalse(self.backend3.invoke_doc_exists()) self.assertFalse(self.backend4.invoke_doc_exists()) self.assertFalse(self.backend5.invoke_doc_exists()) self.assertFalse(self.backend6.invoke_doc_exists()) self.assertFalse(self.backend7.invoke_doc_exists()) self.assertFalse(self.backend8.invoke_doc_exists()) self.assertFalse(self.backend9.invoke_doc_exists()) self.assertFalse(self.backend10.invoke_doc_exists()) self.backend1.delete_invoke_doc() self.assertFalse(self.backend1.invoke_doc_exists()) # Test overriding with a domain-level backend self.domain_obj.default_sms_backend_id = self.backend5._id self.domain_obj.save() self.assertTrue(send_sms(self.domain, None, "15551234567", "Test for BACKEND5")) self.assertFalse(self.backend1.invoke_doc_exists()) self.assertFalse(self.backend2.invoke_doc_exists()) self.assertFalse(self.backend3.invoke_doc_exists()) self.assertFalse(self.backend4.invoke_doc_exists()) self.assertTrue(self.backend5.invoke_doc_exists()) self.assertFalse(self.backend6.invoke_doc_exists()) self.assertFalse(self.backend7.invoke_doc_exists()) self.assertFalse(self.backend8.invoke_doc_exists()) self.assertFalse(self.backend9.invoke_doc_exists()) self.assertFalse(self.backend10.invoke_doc_exists()) self.backend5.delete_invoke_doc() self.assertFalse(self.backend5.invoke_doc_exists()) # Test use of backend that another domain owns but has granted access self.domain_obj.default_sms_backend_id = self.backend6._id self.domain_obj.save() self.assertTrue(send_sms(self.domain, None, "25800000000", "Test for BACKEND6")) self.assertFalse(self.backend1.invoke_doc_exists()) self.assertFalse(self.backend2.invoke_doc_exists()) self.assertFalse(self.backend3.invoke_doc_exists()) self.assertFalse(self.backend4.invoke_doc_exists()) self.assertFalse(self.backend5.invoke_doc_exists()) self.assertTrue(self.backend6.invoke_doc_exists()) self.assertFalse(self.backend7.invoke_doc_exists()) self.assertFalse(self.backend8.invoke_doc_exists()) self.assertFalse(self.backend9.invoke_doc_exists()) self.assertFalse(self.backend10.invoke_doc_exists()) self.backend6.delete_invoke_doc() self.assertFalse(self.backend6.invoke_doc_exists()) # Test backend access control self.domain_obj.default_sms_backend_id = self.backend7._id self.domain_obj.save() self.assertFalse(send_sms(self.domain, None, "25800000000", "Test for BACKEND7")) self.assertFalse(self.backend1.invoke_doc_exists()) self.assertFalse(self.backend2.invoke_doc_exists()) self.assertFalse(self.backend3.invoke_doc_exists()) self.assertFalse(self.backend4.invoke_doc_exists()) self.assertFalse(self.backend5.invoke_doc_exists()) self.assertFalse(self.backend6.invoke_doc_exists()) self.assertFalse(self.backend7.invoke_doc_exists()) self.assertFalse(self.backend8.invoke_doc_exists()) self.assertFalse(self.backend9.invoke_doc_exists()) self.assertFalse(self.backend10.invoke_doc_exists()) # Test sending to verified number with backend map self.domain_obj.default_sms_backend_id = None self.domain_obj.save() verified_number = self.contact.get_verified_number() self.assertTrue(verified_number is not None) self.assertTrue(verified_number.backend_id is None) self.assertEqual(verified_number.phone_number, "15551234567") self.assertTrue(send_sms_to_verified_number(verified_number, "Test for BACKEND2")) self.assertFalse(self.backend1.invoke_doc_exists()) self.assertTrue(self.backend2.invoke_doc_exists()) self.assertFalse(self.backend3.invoke_doc_exists()) self.assertFalse(self.backend4.invoke_doc_exists()) self.assertFalse(self.backend5.invoke_doc_exists()) self.assertFalse(self.backend6.invoke_doc_exists()) self.assertFalse(self.backend7.invoke_doc_exists()) self.assertFalse(self.backend8.invoke_doc_exists()) self.assertFalse(self.backend9.invoke_doc_exists()) self.assertFalse(self.backend10.invoke_doc_exists()) self.backend2.delete_invoke_doc() self.assertFalse(self.backend2.invoke_doc_exists()) # Test sending to verified number with default domain backend self.domain_obj.default_sms_backend_id = self.backend5._id self.domain_obj.save() self.assertTrue(send_sms_to_verified_number(verified_number, "Test for BACKEND5")) self.assertFalse(self.backend1.invoke_doc_exists()) self.assertFalse(self.backend2.invoke_doc_exists()) self.assertFalse(self.backend3.invoke_doc_exists()) self.assertFalse(self.backend4.invoke_doc_exists()) self.assertTrue(self.backend5.invoke_doc_exists()) self.assertFalse(self.backend6.invoke_doc_exists()) self.assertFalse(self.backend7.invoke_doc_exists()) self.assertFalse(self.backend8.invoke_doc_exists()) self.assertFalse(self.backend9.invoke_doc_exists()) self.assertFalse(self.backend10.invoke_doc_exists()) self.backend5.delete_invoke_doc() self.assertFalse(self.backend5.invoke_doc_exists()) # Test sending to verified number with a contact-level backend owned by the domain self.case.set_case_property("contact_backend_id", "BACKEND") self.case.save() self.contact = CommConnectCase.wrap(self.case.to_json()) verified_number = self.contact.get_verified_number() self.assertTrue(verified_number is not None) self.assertEqual(verified_number.backend_id, "BACKEND") self.assertEqual(verified_number.phone_number, "15551234567") self.assertTrue(send_sms_to_verified_number(verified_number, "Test for BACKEND")) self.assertFalse(self.backend1.invoke_doc_exists()) self.assertFalse(self.backend2.invoke_doc_exists()) self.assertFalse(self.backend3.invoke_doc_exists()) self.assertFalse(self.backend4.invoke_doc_exists()) self.assertFalse(self.backend5.invoke_doc_exists()) self.assertFalse(self.backend6.invoke_doc_exists()) self.assertFalse(self.backend7.invoke_doc_exists()) self.assertTrue(self.backend8.invoke_doc_exists()) self.assertFalse(self.backend9.invoke_doc_exists()) self.assertFalse(self.backend10.invoke_doc_exists()) self.backend8.delete_invoke_doc() self.assertFalse(self.backend8.invoke_doc_exists()) # Test sending to verified number with a contact-level backend granted to the domain by another domain self.backend8.delete() self.assertTrue(send_sms_to_verified_number(verified_number, "Test for BACKEND")) self.assertFalse(self.backend1.invoke_doc_exists()) self.assertFalse(self.backend2.invoke_doc_exists()) self.assertFalse(self.backend3.invoke_doc_exists()) self.assertFalse(self.backend4.invoke_doc_exists()) self.assertFalse(self.backend5.invoke_doc_exists()) self.assertFalse(self.backend6.invoke_doc_exists()) self.assertFalse(self.backend7.invoke_doc_exists()) self.assertTrue(self.backend9.invoke_doc_exists()) self.assertFalse(self.backend10.invoke_doc_exists()) self.backend9.delete_invoke_doc() self.assertFalse(self.backend9.invoke_doc_exists()) # Test sending to verified number with a contact-level global backend self.backend9.delete() self.assertTrue(send_sms_to_verified_number(verified_number, "Test for BACKEND")) self.assertFalse(self.backend1.invoke_doc_exists()) self.assertFalse(self.backend2.invoke_doc_exists()) self.assertFalse(self.backend3.invoke_doc_exists()) self.assertFalse(self.backend4.invoke_doc_exists()) self.assertFalse(self.backend5.invoke_doc_exists()) self.assertFalse(self.backend6.invoke_doc_exists()) self.assertFalse(self.backend7.invoke_doc_exists()) self.assertTrue(self.backend10.invoke_doc_exists()) self.backend10.delete_invoke_doc() self.assertFalse(self.backend10.invoke_doc_exists()) # Test raising exception if contact-level backend is not found self.backend10.delete() try: self.assertTrue(send_sms_to_verified_number(verified_number, "Test for BACKEND")) except BadSMSConfigException: pass else: self.assertTrue(False) # Test send_sms_with_backend self.assertTrue(send_sms_with_backend(self.domain, "+15551234567", "Test for BACKEND3", self.backend3._id)) self.assertFalse(self.backend1.invoke_doc_exists()) self.assertFalse(self.backend2.invoke_doc_exists()) self.assertTrue(self.backend3.invoke_doc_exists()) self.assertFalse(self.backend4.invoke_doc_exists()) self.assertFalse(self.backend5.invoke_doc_exists()) self.assertFalse(self.backend6.invoke_doc_exists()) self.assertFalse(self.backend7.invoke_doc_exists()) self.backend3.delete_invoke_doc() self.assertFalse(self.backend3.invoke_doc_exists()) # Test send_sms_with_backend_name self.assertTrue(send_sms_with_backend_name(self.domain, "+15551234567", "Test for BACKEND3", "BACKEND3")) self.assertFalse(self.backend1.invoke_doc_exists()) self.assertFalse(self.backend2.invoke_doc_exists()) self.assertTrue(self.backend3.invoke_doc_exists()) self.assertFalse(self.backend4.invoke_doc_exists()) self.assertFalse(self.backend5.invoke_doc_exists()) self.assertFalse(self.backend6.invoke_doc_exists()) self.assertFalse(self.backend7.invoke_doc_exists()) self.backend3.delete_invoke_doc() self.assertFalse(self.backend3.invoke_doc_exists())
class OptTestCase(BaseAccountingTest): def setUp(self): super(OptTestCase, self).setUp() self.domain = "opt-test" self.domain_obj = Domain(name=self.domain) self.domain_obj.save() generator.instantiate_accounting_for_tests() self.account = BillingAccount.get_or_create_account_by_domain( self.domain_obj.name, created_by="automated-test", )[0] plan = DefaultProductPlan.get_default_plan_by_domain( self.domain_obj, edition=SoftwarePlanEdition.ADVANCED) self.subscription = Subscription.new_domain_subscription( self.account, self.domain_obj.name, plan) self.subscription.is_active = True self.subscription.save() self.backend = TestSMSBackend(is_global=True) self.backend.save() self.backend_mapping = BackendMapping( is_global=True, prefix="*", backend_id=self.backend._id, ) self.backend_mapping.save() def test_opt_out_and_opt_in(self): self.assertEqual(PhoneNumber.objects.count(), 0) incoming("99912345678", "stop", "GVI") self.assertEqual(PhoneNumber.objects.count(), 1) phone_number = PhoneNumber.objects.get(phone_number="99912345678") self.assertFalse(phone_number.send_sms) incoming("99912345678", "start", "GVI") self.assertEqual(PhoneNumber.objects.count(), 1) phone_number = PhoneNumber.objects.get(phone_number="99912345678") self.assertTrue(phone_number.send_sms) def test_sending_to_opted_out_number(self): self.assertEqual(PhoneNumber.objects.count(), 0) self.assertTrue(send_sms(self.domain, None, "999123456789", "hello")) incoming("999123456789", "stop", "GVI") self.assertEqual(PhoneNumber.objects.count(), 1) phone_number = PhoneNumber.objects.get(phone_number="999123456789") self.assertFalse(phone_number.send_sms) self.assertFalse(send_sms(self.domain, None, "999123456789", "hello")) def tearDown(self): self.backend_mapping.delete() self.backend.delete() self.domain_obj.delete() SubscriptionAdjustment.objects.all().delete() self.subscription.delete() self.account.delete()
class BackendTestCase(TestCase): def get_or_create_site(self): site, created = Site.objects.get_or_create(id=settings.SITE_ID) if created: site.domain = 'localhost' site.name = 'localhost' site.save() return (site, created) def setUp(self): self.domain = "test-domain" self.domain2 = "test-domain2" self.site, self.site_created = self.get_or_create_site() self.domain_obj = Domain(name=self.domain) self.domain_obj.save() generator.instantiate_accounting_for_tests() self.account = BillingAccount.get_or_create_account_by_domain( self.domain_obj.name, created_by="automated-test", )[0] plan = DefaultProductPlan.get_default_plan_by_domain( self.domain_obj, edition=SoftwarePlanEdition.ADVANCED ) self.subscription = Subscription.new_domain_subscription( self.account, self.domain_obj.name, plan ) self.subscription.is_active = True self.subscription.save() self.domain_obj = Domain.get(self.domain_obj._id) # Prevent resource conflict self.backend1 = TestCaseBackend(name="BACKEND1",is_global=True) self.backend1.save() self.backend2 = TestCaseBackend(name="BACKEND2",is_global=True) self.backend2.save() self.backend3 = TestCaseBackend(name="BACKEND3",is_global=True) self.backend3.save() self.backend4 = TestCaseBackend(name="BACKEND4",is_global=True) self.backend4.save() self.backend5 = TestCaseBackend(name="BACKEND5",domain=self.domain,is_global=False,authorized_domains=[]) self.backend5.save() self.backend6 = TestCaseBackend(name="BACKEND6",domain=self.domain2,is_global=False,authorized_domains=[self.domain]) self.backend6.save() self.backend7 = TestCaseBackend(name="BACKEND7",domain=self.domain2,is_global=False,authorized_domains=[]) self.backend7.save() self.backend8 = TestCaseBackend(name="BACKEND",domain=self.domain,is_global=False,authorized_domains=[]) self.backend8.save() self.backend9 = TestCaseBackend(name="BACKEND",domain=self.domain2,is_global=False,authorized_domains=[self.domain]) self.backend9.save() self.backend10 = TestCaseBackend(name="BACKEND",is_global=True) self.backend10.save() self.backend_mapping1 = BackendMapping(is_global=True,prefix="*",backend_id=self.backend1._id) self.backend_mapping1.save() self.backend_mapping2 = BackendMapping(is_global=True,prefix="1",backend_id=self.backend2._id) self.backend_mapping2.save() self.backend_mapping3 = BackendMapping(is_global=True,prefix="91",backend_id=self.backend3._id) self.backend_mapping3.save() self.backend_mapping4 = BackendMapping(is_global=True,prefix="265",backend_id=self.backend4._id) self.backend_mapping4.save() self.case = CommCareCase(domain=self.domain) self.case.set_case_property("contact_phone_number","15551234567") self.case.set_case_property("contact_phone_number_is_verified", "1") self.case.save() self.contact = CommConnectCase.wrap(self.case.to_json()) settings.SMS_LOADED_BACKENDS.append("corehq.apps.sms.tests.TestCaseBackend") def tearDown(self): self.backend1.delete_invoke_doc() self.backend1.delete() self.backend_mapping1.delete() self.backend2.delete_invoke_doc() self.backend2.delete() self.backend_mapping2.delete() self.backend3.delete_invoke_doc() self.backend3.delete() self.backend_mapping3.delete() self.backend4.delete_invoke_doc() self.backend4.delete() self.backend_mapping4.delete() self.backend5.delete_invoke_doc() self.backend5.delete() self.backend6.delete_invoke_doc() self.backend6.delete() self.backend7.delete_invoke_doc() self.backend7.delete() self.contact.delete_verified_number() self.case.delete() self.domain_obj.delete() if self.site_created: self.site.delete() settings.SMS_LOADED_BACKENDS.pop() def test_backend(self): # Test the backend map self.assertTrue(send_sms(self.domain, None, "15551234567", "Test for BACKEND2")) self.assertFalse(self.backend1.invoke_doc_exists()) self.assertTrue(self.backend2.invoke_doc_exists()) self.assertFalse(self.backend3.invoke_doc_exists()) self.assertFalse(self.backend4.invoke_doc_exists()) self.assertFalse(self.backend5.invoke_doc_exists()) self.assertFalse(self.backend6.invoke_doc_exists()) self.assertFalse(self.backend7.invoke_doc_exists()) self.assertFalse(self.backend8.invoke_doc_exists()) self.assertFalse(self.backend9.invoke_doc_exists()) self.assertFalse(self.backend10.invoke_doc_exists()) self.backend2.delete_invoke_doc() self.assertFalse(self.backend2.invoke_doc_exists()) self.assertTrue(send_sms(self.domain, None, "9100000000", "Test for BACKEND3")) self.assertFalse(self.backend1.invoke_doc_exists()) self.assertFalse(self.backend2.invoke_doc_exists()) self.assertTrue(self.backend3.invoke_doc_exists()) self.assertFalse(self.backend4.invoke_doc_exists()) self.assertFalse(self.backend5.invoke_doc_exists()) self.assertFalse(self.backend6.invoke_doc_exists()) self.assertFalse(self.backend7.invoke_doc_exists()) self.assertFalse(self.backend8.invoke_doc_exists()) self.assertFalse(self.backend9.invoke_doc_exists()) self.assertFalse(self.backend10.invoke_doc_exists()) self.backend3.delete_invoke_doc() self.assertFalse(self.backend3.invoke_doc_exists()) self.assertTrue(send_sms(self.domain, None, "26500000000", "Test for BACKEND4")) self.assertFalse(self.backend1.invoke_doc_exists()) self.assertFalse(self.backend2.invoke_doc_exists()) self.assertFalse(self.backend3.invoke_doc_exists()) self.assertTrue(self.backend4.invoke_doc_exists()) self.assertFalse(self.backend5.invoke_doc_exists()) self.assertFalse(self.backend6.invoke_doc_exists()) self.assertFalse(self.backend7.invoke_doc_exists()) self.assertFalse(self.backend8.invoke_doc_exists()) self.assertFalse(self.backend9.invoke_doc_exists()) self.assertFalse(self.backend10.invoke_doc_exists()) self.backend4.delete_invoke_doc() self.assertFalse(self.backend4.invoke_doc_exists()) self.assertTrue(send_sms(self.domain, None, "25800000000", "Test for BACKEND1")) self.assertTrue(self.backend1.invoke_doc_exists()) self.assertFalse(self.backend2.invoke_doc_exists()) self.assertFalse(self.backend3.invoke_doc_exists()) self.assertFalse(self.backend4.invoke_doc_exists()) self.assertFalse(self.backend5.invoke_doc_exists()) self.assertFalse(self.backend6.invoke_doc_exists()) self.assertFalse(self.backend7.invoke_doc_exists()) self.assertFalse(self.backend8.invoke_doc_exists()) self.assertFalse(self.backend9.invoke_doc_exists()) self.assertFalse(self.backend10.invoke_doc_exists()) self.backend1.delete_invoke_doc() self.assertFalse(self.backend1.invoke_doc_exists()) # Test overriding with a domain-level backend self.domain_obj.default_sms_backend_id = self.backend5._id self.domain_obj.save() self.assertTrue(send_sms(self.domain, None, "15551234567", "Test for BACKEND5")) self.assertFalse(self.backend1.invoke_doc_exists()) self.assertFalse(self.backend2.invoke_doc_exists()) self.assertFalse(self.backend3.invoke_doc_exists()) self.assertFalse(self.backend4.invoke_doc_exists()) self.assertTrue(self.backend5.invoke_doc_exists()) self.assertFalse(self.backend6.invoke_doc_exists()) self.assertFalse(self.backend7.invoke_doc_exists()) self.assertFalse(self.backend8.invoke_doc_exists()) self.assertFalse(self.backend9.invoke_doc_exists()) self.assertFalse(self.backend10.invoke_doc_exists()) self.backend5.delete_invoke_doc() self.assertFalse(self.backend5.invoke_doc_exists()) # Test use of backend that another domain owns but has granted access self.domain_obj.default_sms_backend_id = self.backend6._id self.domain_obj.save() self.assertTrue(send_sms(self.domain, None, "25800000000", "Test for BACKEND6")) self.assertFalse(self.backend1.invoke_doc_exists()) self.assertFalse(self.backend2.invoke_doc_exists()) self.assertFalse(self.backend3.invoke_doc_exists()) self.assertFalse(self.backend4.invoke_doc_exists()) self.assertFalse(self.backend5.invoke_doc_exists()) self.assertTrue(self.backend6.invoke_doc_exists()) self.assertFalse(self.backend7.invoke_doc_exists()) self.assertFalse(self.backend8.invoke_doc_exists()) self.assertFalse(self.backend9.invoke_doc_exists()) self.assertFalse(self.backend10.invoke_doc_exists()) self.backend6.delete_invoke_doc() self.assertFalse(self.backend6.invoke_doc_exists()) # Test backend access control self.domain_obj.default_sms_backend_id = self.backend7._id self.domain_obj.save() self.assertFalse(send_sms(self.domain, None, "25800000000", "Test for BACKEND7")) self.assertFalse(self.backend1.invoke_doc_exists()) self.assertFalse(self.backend2.invoke_doc_exists()) self.assertFalse(self.backend3.invoke_doc_exists()) self.assertFalse(self.backend4.invoke_doc_exists()) self.assertFalse(self.backend5.invoke_doc_exists()) self.assertFalse(self.backend6.invoke_doc_exists()) self.assertFalse(self.backend7.invoke_doc_exists()) self.assertFalse(self.backend8.invoke_doc_exists()) self.assertFalse(self.backend9.invoke_doc_exists()) self.assertFalse(self.backend10.invoke_doc_exists()) # Test sending to verified number with backend map self.domain_obj.default_sms_backend_id = None self.domain_obj.save() verified_number = self.contact.get_verified_number() self.assertTrue(verified_number is not None) self.assertTrue(verified_number.backend_id is None) self.assertEqual(verified_number.phone_number, "15551234567") self.assertTrue(send_sms_to_verified_number(verified_number, "Test for BACKEND2")) self.assertFalse(self.backend1.invoke_doc_exists()) self.assertTrue(self.backend2.invoke_doc_exists()) self.assertFalse(self.backend3.invoke_doc_exists()) self.assertFalse(self.backend4.invoke_doc_exists()) self.assertFalse(self.backend5.invoke_doc_exists()) self.assertFalse(self.backend6.invoke_doc_exists()) self.assertFalse(self.backend7.invoke_doc_exists()) self.assertFalse(self.backend8.invoke_doc_exists()) self.assertFalse(self.backend9.invoke_doc_exists()) self.assertFalse(self.backend10.invoke_doc_exists()) self.backend2.delete_invoke_doc() self.assertFalse(self.backend2.invoke_doc_exists()) # Test sending to verified number with default domain backend self.domain_obj.default_sms_backend_id = self.backend5._id self.domain_obj.save() self.assertTrue(send_sms_to_verified_number(verified_number, "Test for BACKEND5")) self.assertFalse(self.backend1.invoke_doc_exists()) self.assertFalse(self.backend2.invoke_doc_exists()) self.assertFalse(self.backend3.invoke_doc_exists()) self.assertFalse(self.backend4.invoke_doc_exists()) self.assertTrue(self.backend5.invoke_doc_exists()) self.assertFalse(self.backend6.invoke_doc_exists()) self.assertFalse(self.backend7.invoke_doc_exists()) self.assertFalse(self.backend8.invoke_doc_exists()) self.assertFalse(self.backend9.invoke_doc_exists()) self.assertFalse(self.backend10.invoke_doc_exists()) self.backend5.delete_invoke_doc() self.assertFalse(self.backend5.invoke_doc_exists()) # Test sending to verified number with a contact-level backend owned by the domain self.case.set_case_property("contact_backend_id", "BACKEND") self.case.save() self.contact = CommConnectCase.wrap(self.case.to_json()) verified_number = self.contact.get_verified_number() self.assertTrue(verified_number is not None) self.assertEqual(verified_number.backend_id, "BACKEND") self.assertEqual(verified_number.phone_number, "15551234567") self.assertTrue(send_sms_to_verified_number(verified_number, "Test for BACKEND")) self.assertFalse(self.backend1.invoke_doc_exists()) self.assertFalse(self.backend2.invoke_doc_exists()) self.assertFalse(self.backend3.invoke_doc_exists()) self.assertFalse(self.backend4.invoke_doc_exists()) self.assertFalse(self.backend5.invoke_doc_exists()) self.assertFalse(self.backend6.invoke_doc_exists()) self.assertFalse(self.backend7.invoke_doc_exists()) self.assertTrue(self.backend8.invoke_doc_exists()) self.assertFalse(self.backend9.invoke_doc_exists()) self.assertFalse(self.backend10.invoke_doc_exists()) self.backend8.delete_invoke_doc() self.assertFalse(self.backend8.invoke_doc_exists()) # Test sending to verified number with a contact-level backend granted to the domain by another domain self.backend8.delete() self.assertTrue(send_sms_to_verified_number(verified_number, "Test for BACKEND")) self.assertFalse(self.backend1.invoke_doc_exists()) self.assertFalse(self.backend2.invoke_doc_exists()) self.assertFalse(self.backend3.invoke_doc_exists()) self.assertFalse(self.backend4.invoke_doc_exists()) self.assertFalse(self.backend5.invoke_doc_exists()) self.assertFalse(self.backend6.invoke_doc_exists()) self.assertFalse(self.backend7.invoke_doc_exists()) self.assertTrue(self.backend9.invoke_doc_exists()) self.assertFalse(self.backend10.invoke_doc_exists()) self.backend9.delete_invoke_doc() self.assertFalse(self.backend9.invoke_doc_exists()) # Test sending to verified number with a contact-level global backend self.backend9.delete() self.assertTrue(send_sms_to_verified_number(verified_number, "Test for BACKEND")) self.assertFalse(self.backend1.invoke_doc_exists()) self.assertFalse(self.backend2.invoke_doc_exists()) self.assertFalse(self.backend3.invoke_doc_exists()) self.assertFalse(self.backend4.invoke_doc_exists()) self.assertFalse(self.backend5.invoke_doc_exists()) self.assertFalse(self.backend6.invoke_doc_exists()) self.assertFalse(self.backend7.invoke_doc_exists()) self.assertTrue(self.backend10.invoke_doc_exists()) self.backend10.delete_invoke_doc() self.assertFalse(self.backend10.invoke_doc_exists()) # Test raising exception if contact-level backend is not found self.backend10.delete() try: self.assertTrue(send_sms_to_verified_number(verified_number, "Test for BACKEND")) except BadSMSConfigException: pass else: self.assertTrue(False) # Test send_sms_with_backend self.assertTrue(send_sms_with_backend(self.domain, "+15551234567", "Test for BACKEND3", self.backend3._id)) self.assertFalse(self.backend1.invoke_doc_exists()) self.assertFalse(self.backend2.invoke_doc_exists()) self.assertTrue(self.backend3.invoke_doc_exists()) self.assertFalse(self.backend4.invoke_doc_exists()) self.assertFalse(self.backend5.invoke_doc_exists()) self.assertFalse(self.backend6.invoke_doc_exists()) self.assertFalse(self.backend7.invoke_doc_exists()) self.backend3.delete_invoke_doc() self.assertFalse(self.backend3.invoke_doc_exists()) # Test send_sms_with_backend_name self.assertTrue(send_sms_with_backend_name(self.domain, "+15551234567", "Test for BACKEND3", "BACKEND3")) self.assertFalse(self.backend1.invoke_doc_exists()) self.assertFalse(self.backend2.invoke_doc_exists()) self.assertTrue(self.backend3.invoke_doc_exists()) self.assertFalse(self.backend4.invoke_doc_exists()) self.assertFalse(self.backend5.invoke_doc_exists()) self.assertFalse(self.backend6.invoke_doc_exists()) self.assertFalse(self.backend7.invoke_doc_exists()) self.backend3.delete_invoke_doc() self.assertFalse(self.backend3.invoke_doc_exists()) def test_sms_registration(self): formatted_username = format_username("tester", self.domain) incoming("+9991234567", "JOIN {} WORKER tester".format(self.domain), "TEST_CASE_BACKEND") # Test without mobile worker registration enabled self.assertIsNone(CommCareUser.get_by_username(formatted_username)) # Enable mobile worker registration setattr(self.domain_obj, "sms_mobile_worker_registration_enabled", True) self.domain_obj.save() incoming("+9991234567", "JOIN {} WORKER tester".format(self.domain), "TEST_CASE_BACKEND") self.assertIsNotNone(CommCareUser.get_by_username(formatted_username)) # Test a duplicate registration prev_num_users = num_mobile_users(self.domain) incoming("+9991234568", "JOIN {} WORKER tester".format(self.domain), "TEST_CASE_BACKEND") current_num_users = num_mobile_users(self.domain) self.assertEqual(prev_num_users, current_num_users)